[PATCH] I2C: Separate non-i2c hwmon drivers from i2c-core (5/9)
[safe/jmp/linux-2.6] / drivers / hwmon / lm78.c
1 /*
2     lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> 
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-isa.h>
27 #include <linux/i2c-sensor.h>
28 #include <linux/hwmon.h>
29 #include <linux/err.h>
30 #include <asm/io.h>
31
32 /* Addresses to scan */
33 static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
34                                         0x25, 0x26, 0x27, 0x28, 0x29,
35                                         0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
36                                         0x2f, I2C_CLIENT_END };
37 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
38 static unsigned short isa_address = 0x290;
39
40 /* Insmod parameters */
41 SENSORS_INSMOD_2(lm78, lm79);
42
43 /* Many LM78 constants specified below */
44
45 /* Length of ISA address segment */
46 #define LM78_EXTENT 8
47
48 /* Where are the ISA address/data registers relative to the base address */
49 #define LM78_ADDR_REG_OFFSET 5
50 #define LM78_DATA_REG_OFFSET 6
51
52 /* The LM78 registers */
53 #define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
54 #define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
55 #define LM78_REG_IN(nr) (0x20 + (nr))
56
57 #define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
58 #define LM78_REG_FAN(nr) (0x28 + (nr))
59
60 #define LM78_REG_TEMP 0x27
61 #define LM78_REG_TEMP_OVER 0x39
62 #define LM78_REG_TEMP_HYST 0x3a
63
64 #define LM78_REG_ALARM1 0x41
65 #define LM78_REG_ALARM2 0x42
66
67 #define LM78_REG_VID_FANDIV 0x47
68
69 #define LM78_REG_CONFIG 0x40
70 #define LM78_REG_CHIPID 0x49
71 #define LM78_REG_I2C_ADDR 0x48
72
73
74 /* Conversions. Rounding and limit checking is only done on the TO_REG 
75    variants. */
76
77 /* IN: mV, (0V to 4.08V)
78    REG: 16mV/bit */
79 static inline u8 IN_TO_REG(unsigned long val)
80 {
81         unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
82         return (nval + 8) / 16;
83 }
84 #define IN_FROM_REG(val) ((val) *  16)
85
86 static inline u8 FAN_TO_REG(long rpm, int div)
87 {
88         if (rpm <= 0)
89                 return 255;
90         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
91 }
92
93 static inline int FAN_FROM_REG(u8 val, int div)
94 {
95         return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
96 }
97
98 /* TEMP: mC (-128C to +127C)
99    REG: 1C/bit, two's complement */
100 static inline s8 TEMP_TO_REG(int val)
101 {
102         int nval = SENSORS_LIMIT(val, -128000, 127000) ;
103         return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
104 }
105
106 static inline int TEMP_FROM_REG(s8 val)
107 {
108         return val * 1000;
109 }
110
111 /* VID: mV
112    REG: (see doc/vid) */
113 static inline int VID_FROM_REG(u8 val)
114 {
115         return val==0x1f ? 0 : val>=0x10 ? 5100-val*100 : 2050-val*50;
116 }
117
118 #define DIV_FROM_REG(val) (1 << (val))
119
120 /* There are some complications in a module like this. First off, LM78 chips
121    may be both present on the SMBus and the ISA bus, and we have to handle
122    those cases separately at some places. Second, there might be several
123    LM78 chips available (well, actually, that is probably never done; but
124    it is a clean illustration of how to handle a case like that). Finally,
125    a specific chip may be attached to *both* ISA and SMBus, and we would
126    not like to detect it double. Fortunately, in the case of the LM78 at
127    least, a register tells us what SMBus address we are on, so that helps
128    a bit - except if there could be more than one SMBus. Groan. No solution
129    for this yet. */
130
131 /* This module may seem overly long and complicated. In fact, it is not so
132    bad. Quite a lot of bookkeeping is done. A real driver can often cut
133    some corners. */
134
135 /* For each registered LM78, we need to keep some data in memory. That
136    data is pointed to by lm78_list[NR]->data. The structure itself is
137    dynamically allocated, at the same time when a new lm78 client is
138    allocated. */
139 struct lm78_data {
140         struct i2c_client client;
141         struct class_device *class_dev;
142         struct semaphore lock;
143         enum chips type;
144
145         struct semaphore update_lock;
146         char valid;             /* !=0 if following fields are valid */
147         unsigned long last_updated;     /* In jiffies */
148
149         u8 in[7];               /* Register value */
150         u8 in_max[7];           /* Register value */
151         u8 in_min[7];           /* Register value */
152         u8 fan[3];              /* Register value */
153         u8 fan_min[3];          /* Register value */
154         s8 temp;                /* Register value */
155         s8 temp_over;           /* Register value */
156         s8 temp_hyst;           /* Register value */
157         u8 fan_div[3];          /* Register encoding, shifted right */
158         u8 vid;                 /* Register encoding, combined */
159         u16 alarms;             /* Register encoding, combined */
160 };
161
162
163 static int lm78_attach_adapter(struct i2c_adapter *adapter);
164 static int lm78_isa_attach_adapter(struct i2c_adapter *adapter);
165 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
166 static int lm78_detach_client(struct i2c_client *client);
167
168 static int lm78_read_value(struct i2c_client *client, u8 register);
169 static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
170 static struct lm78_data *lm78_update_device(struct device *dev);
171 static void lm78_init_client(struct i2c_client *client);
172
173
174 static struct i2c_driver lm78_driver = {
175         .owner          = THIS_MODULE,
176         .name           = "lm78",
177         .id             = I2C_DRIVERID_LM78,
178         .flags          = I2C_DF_NOTIFY,
179         .attach_adapter = lm78_attach_adapter,
180         .detach_client  = lm78_detach_client,
181 };
182
183 static struct i2c_driver lm78_isa_driver = {
184         .owner          = THIS_MODULE,
185         .name           = "lm78-isa",
186         .attach_adapter = lm78_isa_attach_adapter,
187         .detach_client  = lm78_detach_client,
188 };
189
190
191 /* 7 Voltages */
192 static ssize_t show_in(struct device *dev, char *buf, int nr)
193 {
194         struct lm78_data *data = lm78_update_device(dev);
195         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
196 }
197
198 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
199 {
200         struct lm78_data *data = lm78_update_device(dev);
201         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
202 }
203
204 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
205 {
206         struct lm78_data *data = lm78_update_device(dev);
207         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
208 }
209
210 static ssize_t set_in_min(struct device *dev, const char *buf,
211                 size_t count, int nr)
212 {
213         struct i2c_client *client = to_i2c_client(dev);
214         struct lm78_data *data = i2c_get_clientdata(client);
215         unsigned long val = simple_strtoul(buf, NULL, 10);
216
217         down(&data->update_lock);
218         data->in_min[nr] = IN_TO_REG(val);
219         lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
220         up(&data->update_lock);
221         return count;
222 }
223
224 static ssize_t set_in_max(struct device *dev, const char *buf,
225                 size_t count, int nr)
226 {
227         struct i2c_client *client = to_i2c_client(dev);
228         struct lm78_data *data = i2c_get_clientdata(client);
229         unsigned long val = simple_strtoul(buf, NULL, 10);
230
231         down(&data->update_lock);
232         data->in_max[nr] = IN_TO_REG(val);
233         lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
234         up(&data->update_lock);
235         return count;
236 }
237         
238 #define show_in_offset(offset)                                  \
239 static ssize_t                                                  \
240         show_in##offset (struct device *dev, struct device_attribute *attr, char *buf)          \
241 {                                                               \
242         return show_in(dev, buf, offset);                       \
243 }                                                               \
244 static DEVICE_ATTR(in##offset##_input, S_IRUGO,                 \
245                 show_in##offset, NULL);                         \
246 static ssize_t                                                  \
247         show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)   \
248 {                                                               \
249         return show_in_min(dev, buf, offset);                   \
250 }                                                               \
251 static ssize_t                                                  \
252         show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf)   \
253 {                                                               \
254         return show_in_max(dev, buf, offset);                   \
255 }                                                               \
256 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
257                 const char *buf, size_t count)                  \
258 {                                                               \
259         return set_in_min(dev, buf, count, offset);             \
260 }                                                               \
261 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
262                 const char *buf, size_t count)                  \
263 {                                                               \
264         return set_in_max(dev, buf, count, offset);             \
265 }                                                               \
266 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,         \
267                 show_in##offset##_min, set_in##offset##_min);   \
268 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,         \
269                 show_in##offset##_max, set_in##offset##_max);
270
271 show_in_offset(0);
272 show_in_offset(1);
273 show_in_offset(2);
274 show_in_offset(3);
275 show_in_offset(4);
276 show_in_offset(5);
277 show_in_offset(6);
278
279 /* Temperature */
280 static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
281 {
282         struct lm78_data *data = lm78_update_device(dev);
283         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
284 }
285
286 static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
287 {
288         struct lm78_data *data = lm78_update_device(dev);
289         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
290 }
291
292 static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
293 {
294         struct i2c_client *client = to_i2c_client(dev);
295         struct lm78_data *data = i2c_get_clientdata(client);
296         long val = simple_strtol(buf, NULL, 10);
297
298         down(&data->update_lock);
299         data->temp_over = TEMP_TO_REG(val);
300         lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
301         up(&data->update_lock);
302         return count;
303 }
304
305 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
306 {
307         struct lm78_data *data = lm78_update_device(dev);
308         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
309 }
310
311 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
312 {
313         struct i2c_client *client = to_i2c_client(dev);
314         struct lm78_data *data = i2c_get_clientdata(client);
315         long val = simple_strtol(buf, NULL, 10);
316
317         down(&data->update_lock);
318         data->temp_hyst = TEMP_TO_REG(val);
319         lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
320         up(&data->update_lock);
321         return count;
322 }
323
324 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
325 static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
326                 show_temp_over, set_temp_over);
327 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
328                 show_temp_hyst, set_temp_hyst);
329
330 /* 3 Fans */
331 static ssize_t show_fan(struct device *dev, char *buf, int nr)
332 {
333         struct lm78_data *data = lm78_update_device(dev);
334         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
335                 DIV_FROM_REG(data->fan_div[nr])) );
336 }
337
338 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
339 {
340         struct lm78_data *data = lm78_update_device(dev);
341         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
342                 DIV_FROM_REG(data->fan_div[nr])) );
343 }
344
345 static ssize_t set_fan_min(struct device *dev, const char *buf,
346                 size_t count, int nr)
347 {
348         struct i2c_client *client = to_i2c_client(dev);
349         struct lm78_data *data = i2c_get_clientdata(client);
350         unsigned long val = simple_strtoul(buf, NULL, 10);
351
352         down(&data->update_lock);
353         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
354         lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
355         up(&data->update_lock);
356         return count;
357 }
358
359 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
360 {
361         struct lm78_data *data = lm78_update_device(dev);
362         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
363 }
364
365 /* Note: we save and restore the fan minimum here, because its value is
366    determined in part by the fan divisor.  This follows the principle of
367    least suprise; the user doesn't expect the fan minimum to change just
368    because the divisor changed. */
369 static ssize_t set_fan_div(struct device *dev, const char *buf,
370         size_t count, int nr)
371 {
372         struct i2c_client *client = to_i2c_client(dev);
373         struct lm78_data *data = i2c_get_clientdata(client);
374         unsigned long val = simple_strtoul(buf, NULL, 10);
375         unsigned long min;
376         u8 reg;
377
378         down(&data->update_lock);
379         min = FAN_FROM_REG(data->fan_min[nr],
380                            DIV_FROM_REG(data->fan_div[nr]));
381
382         switch (val) {
383         case 1: data->fan_div[nr] = 0; break;
384         case 2: data->fan_div[nr] = 1; break;
385         case 4: data->fan_div[nr] = 2; break;
386         case 8: data->fan_div[nr] = 3; break;
387         default:
388                 dev_err(&client->dev, "fan_div value %ld not "
389                         "supported. Choose one of 1, 2, 4 or 8!\n", val);
390                 up(&data->update_lock);
391                 return -EINVAL;
392         }
393
394         reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
395         switch (nr) {
396         case 0:
397                 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
398                 break;
399         case 1:
400                 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
401                 break;
402         }
403         lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
404
405         data->fan_min[nr] =
406                 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
407         lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
408         up(&data->update_lock);
409
410         return count;
411 }
412
413 #define show_fan_offset(offset)                                         \
414 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
415 {                                                                       \
416         return show_fan(dev, buf, offset - 1);                          \
417 }                                                                       \
418 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf)  \
419 {                                                                       \
420         return show_fan_min(dev, buf, offset - 1);                      \
421 }                                                                       \
422 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf)  \
423 {                                                                       \
424         return show_fan_div(dev, buf, offset - 1);                      \
425 }                                                                       \
426 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr,               \
427                 const char *buf, size_t count)                          \
428 {                                                                       \
429         return set_fan_min(dev, buf, count, offset - 1);                \
430 }                                                                       \
431 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
432 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,                \
433                 show_fan_##offset##_min, set_fan_##offset##_min);
434
435 static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
436                 size_t count)
437 {
438         return set_fan_div(dev, buf, count, 0) ;
439 }
440
441 static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
442                 size_t count)
443 {
444         return set_fan_div(dev, buf, count, 1) ;
445 }
446
447 show_fan_offset(1);
448 show_fan_offset(2);
449 show_fan_offset(3);
450
451 /* Fan 3 divisor is locked in H/W */
452 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
453                 show_fan_1_div, set_fan_1_div);
454 static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
455                 show_fan_2_div, set_fan_2_div);
456 static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
457
458 /* VID */
459 static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
460 {
461         struct lm78_data *data = lm78_update_device(dev);
462         return sprintf(buf, "%d\n", VID_FROM_REG(data->vid));
463 }
464 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
465
466 /* Alarms */
467 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
468 {
469         struct lm78_data *data = lm78_update_device(dev);
470         return sprintf(buf, "%u\n", data->alarms);
471 }
472 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
473
474 /* This function is called when:
475      * lm78_driver is inserted (when this module is loaded), for each
476        available adapter
477      * when a new adapter is inserted (and lm78_driver is still present) */
478 static int lm78_attach_adapter(struct i2c_adapter *adapter)
479 {
480         if (!(adapter->class & I2C_CLASS_HWMON))
481                 return 0;
482         return i2c_detect(adapter, &addr_data, lm78_detect);
483 }
484
485 static int lm78_isa_attach_adapter(struct i2c_adapter *adapter)
486 {
487         return lm78_detect(adapter, isa_address, -1);
488 }
489
490 /* This function is called by i2c_detect */
491 int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
492 {
493         int i, err;
494         struct i2c_client *new_client;
495         struct lm78_data *data;
496         const char *client_name = "";
497         int is_isa = i2c_is_isa_adapter(adapter);
498
499         if (!is_isa &&
500             !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
501                 err = -ENODEV;
502                 goto ERROR0;
503         }
504
505         /* Reserve the ISA region */
506         if (is_isa)
507                 if (!request_region(address, LM78_EXTENT,
508                                     lm78_isa_driver.name)) {
509                         err = -EBUSY;
510                         goto ERROR0;
511                 }
512
513         /* Probe whether there is anything available on this address. Already
514            done for SMBus clients */
515         if (kind < 0) {
516                 if (is_isa) {
517
518 #define REALLY_SLOW_IO
519                         /* We need the timeouts for at least some LM78-like
520                            chips. But only if we read 'undefined' registers. */
521                         i = inb_p(address + 1);
522                         if (inb_p(address + 2) != i) {
523                                 err = -ENODEV;
524                                 goto ERROR1;
525                         }
526                         if (inb_p(address + 3) != i) {
527                                 err = -ENODEV;
528                                 goto ERROR1;
529                         }
530                         if (inb_p(address + 7) != i) {
531                                 err = -ENODEV;
532                                 goto ERROR1;
533                         }
534 #undef REALLY_SLOW_IO
535
536                         /* Let's just hope nothing breaks here */
537                         i = inb_p(address + 5) & 0x7f;
538                         outb_p(~i & 0x7f, address + 5);
539                         if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
540                                 outb_p(i, address + 5);
541                                 err = -ENODEV;
542                                 goto ERROR1;
543                         }
544                 }
545         }
546
547         /* OK. For now, we presume we have a valid client. We now create the
548            client structure, even though we cannot fill it completely yet.
549            But it allows us to access lm78_{read,write}_value. */
550
551         if (!(data = kmalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
552                 err = -ENOMEM;
553                 goto ERROR1;
554         }
555         memset(data, 0, sizeof(struct lm78_data));
556
557         new_client = &data->client;
558         if (is_isa)
559                 init_MUTEX(&data->lock);
560         i2c_set_clientdata(new_client, data);
561         new_client->addr = address;
562         new_client->adapter = adapter;
563         new_client->driver = is_isa ? &lm78_isa_driver : &lm78_driver;
564         new_client->flags = 0;
565
566         /* Now, we do the remaining detection. */
567         if (kind < 0) {
568                 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
569                         err = -ENODEV;
570                         goto ERROR2;
571                 }
572                 if (!is_isa && (lm78_read_value(
573                                 new_client, LM78_REG_I2C_ADDR) != address)) {
574                         err = -ENODEV;
575                         goto ERROR2;
576                 }
577         }
578
579         /* Determine the chip type. */
580         if (kind <= 0) {
581                 i = lm78_read_value(new_client, LM78_REG_CHIPID);
582                 if (i == 0x00 || i == 0x20      /* LM78 */
583                  || i == 0x40)                  /* LM78-J */
584                         kind = lm78;
585                 else if ((i & 0xfe) == 0xc0)
586                         kind = lm79;
587                 else {
588                         if (kind == 0)
589                                 dev_warn(&adapter->dev, "Ignoring 'force' "
590                                         "parameter for unknown chip at "
591                                         "adapter %d, address 0x%02x\n",
592                                         i2c_adapter_id(adapter), address);
593                         err = -ENODEV;
594                         goto ERROR2;
595                 }
596         }
597
598         if (kind == lm78) {
599                 client_name = "lm78";
600         } else if (kind == lm79) {
601                 client_name = "lm79";
602         }
603
604         /* Fill in the remaining client fields and put into the global list */
605         strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
606         data->type = kind;
607
608         data->valid = 0;
609         init_MUTEX(&data->update_lock);
610
611         /* Tell the I2C layer a new client has arrived */
612         if ((err = i2c_attach_client(new_client)))
613                 goto ERROR2;
614
615         /* Initialize the LM78 chip */
616         lm78_init_client(new_client);
617
618         /* A few vars need to be filled upon startup */
619         for (i = 0; i < 3; i++) {
620                 data->fan_min[i] = lm78_read_value(new_client,
621                                         LM78_REG_FAN_MIN(i));
622         }
623
624         /* Register sysfs hooks */
625         data->class_dev = hwmon_device_register(&new_client->dev);
626         if (IS_ERR(data->class_dev)) {
627                 err = PTR_ERR(data->class_dev);
628                 goto ERROR3;
629         }
630
631         device_create_file(&new_client->dev, &dev_attr_in0_input);
632         device_create_file(&new_client->dev, &dev_attr_in0_min);
633         device_create_file(&new_client->dev, &dev_attr_in0_max);
634         device_create_file(&new_client->dev, &dev_attr_in1_input);
635         device_create_file(&new_client->dev, &dev_attr_in1_min);
636         device_create_file(&new_client->dev, &dev_attr_in1_max);
637         device_create_file(&new_client->dev, &dev_attr_in2_input);
638         device_create_file(&new_client->dev, &dev_attr_in2_min);
639         device_create_file(&new_client->dev, &dev_attr_in2_max);
640         device_create_file(&new_client->dev, &dev_attr_in3_input);
641         device_create_file(&new_client->dev, &dev_attr_in3_min);
642         device_create_file(&new_client->dev, &dev_attr_in3_max);
643         device_create_file(&new_client->dev, &dev_attr_in4_input);
644         device_create_file(&new_client->dev, &dev_attr_in4_min);
645         device_create_file(&new_client->dev, &dev_attr_in4_max);
646         device_create_file(&new_client->dev, &dev_attr_in5_input);
647         device_create_file(&new_client->dev, &dev_attr_in5_min);
648         device_create_file(&new_client->dev, &dev_attr_in5_max);
649         device_create_file(&new_client->dev, &dev_attr_in6_input);
650         device_create_file(&new_client->dev, &dev_attr_in6_min);
651         device_create_file(&new_client->dev, &dev_attr_in6_max);
652         device_create_file(&new_client->dev, &dev_attr_temp1_input);
653         device_create_file(&new_client->dev, &dev_attr_temp1_max);
654         device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
655         device_create_file(&new_client->dev, &dev_attr_fan1_input);
656         device_create_file(&new_client->dev, &dev_attr_fan1_min);
657         device_create_file(&new_client->dev, &dev_attr_fan1_div);
658         device_create_file(&new_client->dev, &dev_attr_fan2_input);
659         device_create_file(&new_client->dev, &dev_attr_fan2_min);
660         device_create_file(&new_client->dev, &dev_attr_fan2_div);
661         device_create_file(&new_client->dev, &dev_attr_fan3_input);
662         device_create_file(&new_client->dev, &dev_attr_fan3_min);
663         device_create_file(&new_client->dev, &dev_attr_fan3_div);
664         device_create_file(&new_client->dev, &dev_attr_alarms);
665         device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
666
667         return 0;
668
669 ERROR3:
670         i2c_detach_client(new_client);
671 ERROR2:
672         kfree(data);
673 ERROR1:
674         if (is_isa)
675                 release_region(address, LM78_EXTENT);
676 ERROR0:
677         return err;
678 }
679
680 static int lm78_detach_client(struct i2c_client *client)
681 {
682         struct lm78_data *data = i2c_get_clientdata(client);
683         int err;
684
685         hwmon_device_unregister(data->class_dev);
686
687         if ((err = i2c_detach_client(client))) {
688                 dev_err(&client->dev,
689                     "Client deregistration failed, client not detached.\n");
690                 return err;
691         }
692
693         if(i2c_is_isa_client(client))
694                 release_region(client->addr, LM78_EXTENT);
695
696         kfree(data);
697
698         return 0;
699 }
700
701 /* The SMBus locks itself, but ISA access must be locked explicitly! 
702    We don't want to lock the whole ISA bus, so we lock each client
703    separately.
704    We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
705    would slow down the LM78 access and should not be necessary.  */
706 static int lm78_read_value(struct i2c_client *client, u8 reg)
707 {
708         int res;
709         if (i2c_is_isa_client(client)) {
710                 struct lm78_data *data = i2c_get_clientdata(client);
711                 down(&data->lock);
712                 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
713                 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
714                 up(&data->lock);
715                 return res;
716         } else
717                 return i2c_smbus_read_byte_data(client, reg);
718 }
719
720 /* The SMBus locks itself, but ISA access muse be locked explicitly! 
721    We don't want to lock the whole ISA bus, so we lock each client
722    separately.
723    We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
724    would slow down the LM78 access and should not be necessary. 
725    There are some ugly typecasts here, but the good new is - they should
726    nowhere else be necessary! */
727 static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
728 {
729         if (i2c_is_isa_client(client)) {
730                 struct lm78_data *data = i2c_get_clientdata(client);
731                 down(&data->lock);
732                 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
733                 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
734                 up(&data->lock);
735                 return 0;
736         } else
737                 return i2c_smbus_write_byte_data(client, reg, value);
738 }
739
740 /* Called when we have found a new LM78. It should set limits, etc. */
741 static void lm78_init_client(struct i2c_client *client)
742 {
743         u8 config = lm78_read_value(client, LM78_REG_CONFIG);
744
745         /* Start monitoring */
746         if (!(config & 0x01))
747                 lm78_write_value(client, LM78_REG_CONFIG,
748                                  (config & 0xf7) | 0x01);
749 }
750
751 static struct lm78_data *lm78_update_device(struct device *dev)
752 {
753         struct i2c_client *client = to_i2c_client(dev);
754         struct lm78_data *data = i2c_get_clientdata(client);
755         int i;
756
757         down(&data->update_lock);
758
759         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
760             || !data->valid) {
761
762                 dev_dbg(&client->dev, "Starting lm78 update\n");
763
764                 for (i = 0; i <= 6; i++) {
765                         data->in[i] =
766                             lm78_read_value(client, LM78_REG_IN(i));
767                         data->in_min[i] =
768                             lm78_read_value(client, LM78_REG_IN_MIN(i));
769                         data->in_max[i] =
770                             lm78_read_value(client, LM78_REG_IN_MAX(i));
771                 }
772                 for (i = 0; i < 3; i++) {
773                         data->fan[i] =
774                             lm78_read_value(client, LM78_REG_FAN(i));
775                         data->fan_min[i] =
776                             lm78_read_value(client, LM78_REG_FAN_MIN(i));
777                 }
778                 data->temp = lm78_read_value(client, LM78_REG_TEMP);
779                 data->temp_over =
780                     lm78_read_value(client, LM78_REG_TEMP_OVER);
781                 data->temp_hyst =
782                     lm78_read_value(client, LM78_REG_TEMP_HYST);
783                 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
784                 data->vid = i & 0x0f;
785                 if (data->type == lm79)
786                         data->vid |=
787                             (lm78_read_value(client, LM78_REG_CHIPID) &
788                              0x01) << 4;
789                 else
790                         data->vid |= 0x10;
791                 data->fan_div[0] = (i >> 4) & 0x03;
792                 data->fan_div[1] = i >> 6;
793                 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
794                     (lm78_read_value(client, LM78_REG_ALARM2) << 8);
795                 data->last_updated = jiffies;
796                 data->valid = 1;
797
798                 data->fan_div[2] = 1;
799         }
800
801         up(&data->update_lock);
802
803         return data;
804 }
805
806 static int __init sm_lm78_init(void)
807 {
808         int res;
809
810         res = i2c_add_driver(&lm78_driver);
811         if (res)
812                 return res;
813
814         res = i2c_isa_add_driver(&lm78_isa_driver);
815         if (res) {
816                 i2c_del_driver(&lm78_driver);
817                 return res;
818         }
819
820         return 0;
821 }
822
823 static void __exit sm_lm78_exit(void)
824 {
825         i2c_isa_del_driver(&lm78_isa_driver);
826         i2c_del_driver(&lm78_driver);
827 }
828
829
830
831 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
832 MODULE_DESCRIPTION("LM78/LM79 driver");
833 MODULE_LICENSE("GPL");
834
835 module_init(sm_lm78_init);
836 module_exit(sm_lm78_exit);