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