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