This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / i2c / chips / lm87.c
1 /*
2  * lm87.c
3  *
4  * Copyright (C) 2000       Frodo Looijaard <frodol@dds.nl>
5  *                          Philip Edelbrock <phil@netroedge.com>
6  *                          Stephen Rousset <stephen.rousset@rocketlogix.com>
7  *                          Dan Eaton <dan.eaton@rocketlogix.com>
8  * Copyright (C) 2004       Jean Delvare <khali@linux-fr.org>
9  *
10  * Original port to Linux 2.6 by Jeff Oliver.
11  *
12  * The LM87 is a sensor chip made by National Semiconductor. It monitors up
13  * to 8 voltages (including its own power source), up to three temperatures
14  * (its own plus up to two external ones) and up to two fans. The default
15  * configuration is 6 voltages, two temperatures and two fans (see below).
16  * Voltages are scaled internally with ratios such that the nominal value of
17  * each voltage correspond to a register value of 192 (which means a
18  * resolution of about 0.5% of the nominal value). Temperature values are
19  * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
20  * datasheet can be obtained from National's website at:
21  *   http://www.national.com/pf/LM/LM87.html
22  *
23  * Some functions share pins, so not all functions are available at the same
24  * time. Which are depends on the hardware setup. This driver assumes that
25  * the BIOS configured the chip correctly. In that respect, it  differs from
26  * the original driver (from lm_sensors for Linux 2.4), which would force the
27  * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
28  * chipset wiring.
29  * For reference, here is the list of exclusive functions:
30  *  - in0+in5 (default) or temp3
31  *  - fan1 (default) or in6
32  *  - fan2 (default) or in7
33  *  - VID lines (default) or IRQ lines (not handled by this driver)
34  *
35  * The LM87 additionally features an analog output, supposedly usable to
36  * control the speed of a fan. All new chips use pulse width modulation
37  * instead. The LM87 is the only hardware monitoring chipset I know of
38  * which uses amplitude modulation. Be careful when using this feature.
39  *
40  * This program is free software; you can redistribute it and/or modify
41  * it under the terms of the GNU General Public License as published by
42  * the Free Software Foundation; either version 2 of the License, or
43  * (at your option) any later version.
44  *
45  * This program is distributed in the hope that it will be useful,
46  * but WITHOUT ANY WARRANTY; without even the implied warranty of
47  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48  * GNU General Public License for more details.
49  *
50  * You should have received a copy of the GNU General Public License
51  * along with this program; if not, write to the Free Software
52  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
53  */
54
55 #include <linux/config.h>
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/i2c.h>
60 #include <linux/i2c-sensor.h>
61 #include <linux/i2c-vid.h>
62
63 /*
64  * Addresses to scan
65  * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
66  */
67
68 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
69 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
70
71 /*
72  * Insmod parameters
73  */
74
75 SENSORS_INSMOD_1(lm87);
76
77 /*
78  * The LM87 registers
79  */
80
81 /* nr in 0..5 */
82 #define LM87_REG_IN(nr)                 (0x20 + (nr))
83 #define LM87_REG_IN_MAX(nr)             (0x2B + (nr) * 2)
84 #define LM87_REG_IN_MIN(nr)             (0x2C + (nr) * 2)
85 /* nr in 0..1 */
86 #define LM87_REG_AIN(nr)                (0x28 + (nr))
87 #define LM87_REG_AIN_MIN(nr)            (0x1A + (nr))
88 #define LM87_REG_AIN_MAX(nr)            (0x3B + (nr))
89
90 static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
91 static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
92 static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
93
94 #define LM87_REG_TEMP_HW_INT_LOCK       0x13
95 #define LM87_REG_TEMP_HW_EXT_LOCK       0x14
96 #define LM87_REG_TEMP_HW_INT            0x17
97 #define LM87_REG_TEMP_HW_EXT            0x18
98
99 /* nr in 0..1 */
100 #define LM87_REG_FAN(nr)                (0x28 + (nr))
101 #define LM87_REG_FAN_MIN(nr)            (0x3B + (nr))
102 #define LM87_REG_AOUT                   0x19
103
104 #define LM87_REG_CONFIG                 0x40
105 #define LM87_REG_CHANNEL_MODE           0x16
106 #define LM87_REG_VID_FAN_DIV            0x47
107 #define LM87_REG_VID4                   0x49
108
109 #define LM87_REG_ALARMS1                0x41
110 #define LM87_REG_ALARMS2                0x42
111
112 #define LM87_REG_COMPANY_ID             0x3E
113 #define LM87_REG_REVISION               0x3F
114
115 /*
116  * Conversions and various macros
117  * The LM87 uses signed 8-bit values for temperatures.
118  */
119
120 #define IN_FROM_REG(reg,scale)  (((reg) * (scale) + 96) / 192)
121 #define IN_TO_REG(val,scale)    ((val) <= 0 ? 0 : \
122                                  (val) * 192 >= (scale) * 255 ? 255 : \
123                                  ((val) * 192 + (scale)/2) / (scale))
124
125 #define TEMP_FROM_REG(reg)      ((reg) * 1000)
126 #define TEMP_TO_REG(val)        ((val) <= -127500 ? -128 : \
127                                  (val) >= 126500 ? 127 : \
128                                  (((val) < 0 ? (val)-500 : (val)+500) / 1000))
129
130 #define FAN_FROM_REG(reg,div)   ((reg) == 255 || (reg) == 0 ? 0 : \
131                                  1350000 + (reg)*(div) / 2) / ((reg)*(div))
132 #define FAN_TO_REG(val,div)     ((val)*(div) * 255 <= 1350000 ? 255 : \
133                                  (1350000 + (val)*(div) / 2) / ((val)*(div)))
134
135 #define FAN_DIV_FROM_REG(reg)   (1 << (reg))
136
137 /* analog out is 9.80mV/LSB */
138 #define AOUT_FROM_REG(reg)      (((reg) * 98 + 5) / 10)
139 #define AOUT_TO_REG(val)        ((val) <= 0 ? 0 : \
140                                  (val) >= 2500 ? 255 : \
141                                  ((val) * 10 + 49) / 98)
142
143 /* nr in 0..1 */
144 #define CHAN_NO_FAN(nr)         (1 << (nr))
145 #define CHAN_TEMP3              (1 << 2)
146 #define CHAN_VCC_5V             (1 << 3)
147 #define CHAN_NO_VID             (1 << 8)
148
149 /*
150  * Functions declaration
151  */
152
153 static int lm87_attach_adapter(struct i2c_adapter *adapter);
154 static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
155 static void lm87_init_client(struct i2c_client *client);
156 static int lm87_detach_client(struct i2c_client *client);
157 static struct lm87_data *lm87_update_device(struct device *dev);
158
159 /*
160  * Driver data (common to all clients)
161  */
162
163 static struct i2c_driver lm87_driver = {
164         .owner          = THIS_MODULE,
165         .name           = "lm87",
166         .id             = I2C_DRIVERID_LM87,
167         .flags          = I2C_DF_NOTIFY,
168         .attach_adapter = lm87_attach_adapter,
169         .detach_client  = lm87_detach_client,
170 };
171
172 /*
173  * Client data (each client gets its own)
174  */
175
176 struct lm87_data {
177         struct i2c_client client;
178         struct semaphore update_lock;
179         char valid; /* zero until following fields are valid */
180         unsigned long last_updated; /* In jiffies */
181
182         u8 channel;             /* register value */
183
184         u8 in[8];               /* register value */
185         u8 in_max[8];           /* register value */
186         u8 in_min[8];           /* register value */
187         u16 in_scale[8];
188
189         s8 temp[3];             /* register value */
190         s8 temp_high[3];        /* register value */
191         s8 temp_low[3];         /* register value */
192         s8 temp_crit_int;       /* min of two register values */
193         s8 temp_crit_ext;       /* min of two register values */
194
195         u8 fan[2];              /* register value */
196         u8 fan_min[2];          /* register value */
197         u8 fan_div[2];          /* register value, shifted right */
198         u8 aout;                /* register value */
199
200         u16 alarms;             /* register values, combined */
201         u8 vid;                 /* register values, combined */
202         u8 vrm;
203 };
204
205 /*
206  * Internal variables
207  */
208
209 static int lm87_id;
210
211 /*
212  * Sysfs stuff
213  */
214
215 static inline int lm87_read_value(struct i2c_client *client, u8 reg)
216 {
217         return i2c_smbus_read_byte_data(client, reg);
218 }
219
220 static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
221 {
222         return i2c_smbus_write_byte_data(client, reg, value);
223 }
224
225 #define show_in(offset) \
226 static ssize_t show_in##offset##_input(struct device *dev, char *buf) \
227 { \
228         struct lm87_data *data = lm87_update_device(dev); \
229         return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
230                        data->in_scale[offset])); \
231 } \
232 static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
233 { \
234         struct lm87_data *data = lm87_update_device(dev); \
235         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
236                        data->in_scale[offset])); \
237 } \
238 static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
239 { \
240         struct lm87_data *data = lm87_update_device(dev); \
241         return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
242                        data->in_scale[offset])); \
243 } \
244 static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
245                 show_in##offset##_input, NULL);
246 show_in(0);
247 show_in(1);
248 show_in(2);
249 show_in(3);
250 show_in(4);
251 show_in(5);
252 show_in(6);
253 show_in(7);
254
255 static void set_in_min(struct device *dev, const char *buf, int nr)
256 {
257         struct i2c_client *client = to_i2c_client(dev);
258         struct lm87_data *data = i2c_get_clientdata(client);
259         long val = simple_strtol(buf, NULL, 10);
260         data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
261         lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
262                          LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
263 }
264
265 static void set_in_max(struct device *dev, const char *buf, int nr)
266 {
267         struct i2c_client *client = to_i2c_client(dev);
268         struct lm87_data *data = i2c_get_clientdata(client);
269         long val = simple_strtol(buf, NULL, 10);
270         data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
271         lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
272                          LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
273 }
274
275 #define set_in(offset) \
276 static ssize_t set_in##offset##_min(struct device *dev, \
277                 const char *buf, size_t count) \
278 { \
279         set_in_min(dev, buf, offset); \
280         return count; \
281 } \
282 static ssize_t set_in##offset##_max(struct device *dev, \
283                 const char *buf, size_t count) \
284 { \
285         set_in_max(dev, buf, offset); \
286         return count; \
287 } \
288 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
289                 show_in##offset##_min, set_in##offset##_min); \
290 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
291                 show_in##offset##_max, set_in##offset##_max);
292 set_in(0);
293 set_in(1);
294 set_in(2);
295 set_in(3);
296 set_in(4);
297 set_in(5);
298 set_in(6);
299 set_in(7);
300
301 #define show_temp(offset) \
302 static ssize_t show_temp##offset##_input(struct device *dev, char *buf) \
303 { \
304         struct lm87_data *data = lm87_update_device(dev); \
305         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
306 } \
307 static ssize_t show_temp##offset##_low(struct device *dev, char *buf) \
308 { \
309         struct lm87_data *data = lm87_update_device(dev); \
310         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
311 } \
312 static ssize_t show_temp##offset##_high(struct device *dev, char *buf) \
313 { \
314         struct lm87_data *data = lm87_update_device(dev); \
315         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
316 }\
317 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
318                 show_temp##offset##_input, NULL);
319 show_temp(1);
320 show_temp(2);
321 show_temp(3);
322
323 static void set_temp_low(struct device *dev, const char *buf, int nr)
324 {
325     struct i2c_client *client = to_i2c_client(dev);
326     struct lm87_data *data = i2c_get_clientdata(client);
327     long val = simple_strtol(buf, NULL, 10);
328     data->temp_low[nr] = TEMP_TO_REG(val);
329     lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
330 }
331
332 static void set_temp_high(struct device *dev, const char *buf, int nr)
333 {
334     struct i2c_client *client = to_i2c_client(dev);
335     struct lm87_data *data = i2c_get_clientdata(client);
336     long val = simple_strtol(buf, NULL, 10);
337     data->temp_high[nr] = TEMP_TO_REG(val);
338     lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
339 }
340
341 #define set_temp(offset) \
342 static ssize_t set_temp##offset##_low(struct device *dev, \
343                 const char *buf, size_t count) \
344 { \
345         set_temp_low(dev, buf, offset-1); \
346         return count; \
347 } \
348 static ssize_t set_temp##offset##_high(struct device *dev, \
349                 const char *buf, size_t count) \
350 { \
351         set_temp_high(dev, buf, offset-1); \
352         return count; \
353 } \
354 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
355                 show_temp##offset##_high, set_temp##offset##_high); \
356 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
357                 show_temp##offset##_low, set_temp##offset##_low);
358 set_temp(1);
359 set_temp(2);
360 set_temp(3);
361
362 static ssize_t show_temp_crit_int(struct device *dev, char *buf)
363 {
364         struct lm87_data *data = lm87_update_device(dev);
365         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
366 }
367
368 static ssize_t show_temp_crit_ext(struct device *dev, char *buf)
369 {
370         struct lm87_data *data = lm87_update_device(dev);
371         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
372 }
373
374 static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
375 static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
376 static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
377
378 #define show_fan(offset) \
379 static ssize_t show_fan##offset##_input(struct device *dev, char *buf) \
380 { \
381         struct lm87_data *data = lm87_update_device(dev); \
382         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
383                        FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
384 } \
385 static ssize_t show_fan##offset##_min(struct device *dev, char *buf) \
386 { \
387         struct lm87_data *data = lm87_update_device(dev); \
388         return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
389                        FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
390 } \
391 static ssize_t show_fan##offset##_div(struct device *dev, char *buf) \
392 { \
393         struct lm87_data *data = lm87_update_device(dev); \
394         return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
395 } \
396 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
397                 show_fan##offset##_input, NULL);
398 show_fan(1);
399 show_fan(2);
400
401 static void set_fan_min(struct device *dev, const char *buf, int nr)
402 {
403         struct i2c_client *client = to_i2c_client(dev);
404         struct lm87_data *data = i2c_get_clientdata(client);
405         long val = simple_strtol(buf, NULL, 10);
406         data->fan_min[nr] = FAN_TO_REG(val,
407                             FAN_DIV_FROM_REG(data->fan_div[nr]));
408         lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
409 }
410
411 /* Note: we save and restore the fan minimum here, because its value is
412    determined in part by the fan clock divider.  This follows the principle
413    of least suprise; the user doesn't expect the fan minimum to change just
414    because the divider changed. */
415 static ssize_t set_fan_div(struct device *dev, const char *buf,
416                 size_t count, int nr)
417 {
418         struct i2c_client *client = to_i2c_client(dev);
419         struct lm87_data *data = i2c_get_clientdata(client);
420         long val = simple_strtol(buf, NULL, 10);
421         unsigned long min = FAN_FROM_REG(data->fan_min[nr],
422                             FAN_DIV_FROM_REG(data->fan_div[nr]));
423         u8 reg;
424
425         switch (val) {
426         case 1: data->fan_div[nr] = 0; break;
427         case 2: data->fan_div[nr] = 1; break;
428         case 4: data->fan_div[nr] = 2; break;
429         case 8: data->fan_div[nr] = 3; break;
430         default: return -EINVAL;
431         }
432
433         reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
434         switch (nr) {
435         case 0:
436             reg = (reg & 0xCF) | (data->fan_div[0] << 4);
437             break;
438         case 1:
439             reg = (reg & 0x3F) | (data->fan_div[1] << 6);
440             break;
441         }
442         lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
443
444         data->fan_min[nr] = FAN_TO_REG(min, val);
445         lm87_write_value(client, LM87_REG_FAN_MIN(nr),
446                          data->fan_min[nr]);
447         return count;
448 }
449
450 #define set_fan(offset) \
451 static ssize_t set_fan##offset##_min(struct device *dev, const char *buf, \
452                 size_t count) \
453 { \
454         set_fan_min(dev, buf, offset-1); \
455         return count; \
456 } \
457 static ssize_t set_fan##offset##_div(struct device *dev, const char *buf, \
458                 size_t count) \
459 { \
460         return set_fan_div(dev, buf, count, offset-1); \
461 } \
462 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
463                 show_fan##offset##_min, set_fan##offset##_min); \
464 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
465                 show_fan##offset##_div, set_fan##offset##_div);
466 set_fan(1);
467 set_fan(2);
468
469 static ssize_t show_alarms(struct device *dev, char *buf)
470 {
471         struct lm87_data *data = lm87_update_device(dev);
472         return sprintf(buf, "%d\n", data->alarms);
473 }
474 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
475
476 static ssize_t show_vid(struct device *dev, char *buf)
477 {
478         struct lm87_data *data = lm87_update_device(dev);
479         return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
480 }
481 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
482
483 static ssize_t show_vrm(struct device *dev, char *buf)
484 {
485         struct lm87_data *data = lm87_update_device(dev);
486         return sprintf(buf, "%d\n", data->vrm);
487 }
488 static ssize_t set_vrm(struct device *dev, const char *buf, size_t count)
489 {
490         struct i2c_client *client = to_i2c_client(dev);
491         struct lm87_data *data = i2c_get_clientdata(client);
492         data->vrm = simple_strtoul(buf, NULL, 10);
493         return count;
494 }
495 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
496
497 static ssize_t show_aout(struct device *dev, char *buf)
498 {
499         struct lm87_data *data = lm87_update_device(dev);
500         return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
501 }
502 static ssize_t set_aout(struct device *dev, const char *buf, size_t count)
503 {
504         struct i2c_client *client = to_i2c_client(dev);
505         struct lm87_data *data = i2c_get_clientdata(client);
506         long val = simple_strtol(buf, NULL, 10);
507         data->aout = AOUT_TO_REG(val);
508         lm87_write_value(client, LM87_REG_AOUT, data->aout);
509         return count;
510 }
511 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
512
513 /*
514  * Real code
515  */
516
517 static int lm87_attach_adapter(struct i2c_adapter *adapter)
518 {
519         if (!(adapter->class & I2C_CLASS_HWMON))
520                 return 0;
521         return i2c_detect(adapter, &addr_data, lm87_detect);
522 }
523
524 /*
525  * The following function does more than just detection. If detection
526  * succeeds, it also registers the new chip.
527  */
528 static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
529 {
530         struct i2c_client *new_client;
531         struct lm87_data *data;
532         int err = 0;
533
534         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
535                 goto exit;
536
537         if (!(data = kmalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
538                 err = -ENOMEM;
539                 goto exit;
540         }
541         memset(data, 0, sizeof(struct lm87_data));
542
543         /* The common I2C client data is placed right before the
544            LM87-specific data. */
545         new_client = &data->client;
546         i2c_set_clientdata(new_client, data);
547         new_client->addr = address;
548         new_client->adapter = adapter;
549         new_client->driver = &lm87_driver;
550         new_client->flags = 0;
551
552         /* Default to an LM87 if forced */
553         if (kind == 0)
554                 kind = lm87;
555
556         /* Now, we do the remaining detection. */
557         if (kind < 0) {
558                 u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
559
560                 if (rev < 0x01 || rev > 0x08
561                  || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
562                  || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
563                         dev_dbg(&adapter->dev,
564                                 "LM87 detection failed at 0x%02x.\n",
565                                 address);
566                         goto exit_free;
567                 }
568         }
569
570         /* We can fill in the remaining client fields */
571         strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
572         new_client->id = lm87_id++;
573         data->valid = 0;
574         init_MUTEX(&data->update_lock);
575
576         /* Tell the I2C layer a new client has arrived */
577         if ((err = i2c_attach_client(new_client)))
578                 goto exit_free;
579
580         /* Initialize the LM87 chip */
581         lm87_init_client(new_client);
582
583         data->in_scale[0] = 2500;
584         data->in_scale[1] = 2700;
585         data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
586         data->in_scale[3] = 5000;
587         data->in_scale[4] = 12000;
588         data->in_scale[5] = 2700;
589         data->in_scale[6] = 1875;
590         data->in_scale[7] = 1875;
591
592         /* Register sysfs hooks */
593         device_create_file(&new_client->dev, &dev_attr_in1_input);
594         device_create_file(&new_client->dev, &dev_attr_in1_min);
595         device_create_file(&new_client->dev, &dev_attr_in1_max);
596         device_create_file(&new_client->dev, &dev_attr_in2_input);
597         device_create_file(&new_client->dev, &dev_attr_in2_min);
598         device_create_file(&new_client->dev, &dev_attr_in2_max);
599         device_create_file(&new_client->dev, &dev_attr_in3_input);
600         device_create_file(&new_client->dev, &dev_attr_in3_min);
601         device_create_file(&new_client->dev, &dev_attr_in3_max);
602         device_create_file(&new_client->dev, &dev_attr_in4_input);
603         device_create_file(&new_client->dev, &dev_attr_in4_min);
604         device_create_file(&new_client->dev, &dev_attr_in4_max);
605
606         if (data->channel & CHAN_NO_FAN(0)) {
607                 device_create_file(&new_client->dev, &dev_attr_in6_input);
608                 device_create_file(&new_client->dev, &dev_attr_in6_min);
609                 device_create_file(&new_client->dev, &dev_attr_in6_max);
610         } else {
611                 device_create_file(&new_client->dev, &dev_attr_fan1_input);
612                 device_create_file(&new_client->dev, &dev_attr_fan1_min);
613                 device_create_file(&new_client->dev, &dev_attr_fan1_div);
614         }
615         if (data->channel & CHAN_NO_FAN(1)) {
616                 device_create_file(&new_client->dev, &dev_attr_in7_input);
617                 device_create_file(&new_client->dev, &dev_attr_in7_min);
618                 device_create_file(&new_client->dev, &dev_attr_in7_max);
619         } else {
620                 device_create_file(&new_client->dev, &dev_attr_fan2_input);
621                 device_create_file(&new_client->dev, &dev_attr_fan2_min);
622                 device_create_file(&new_client->dev, &dev_attr_fan2_div);
623         }
624
625         device_create_file(&new_client->dev, &dev_attr_temp1_input);
626         device_create_file(&new_client->dev, &dev_attr_temp1_max);
627         device_create_file(&new_client->dev, &dev_attr_temp1_min);
628         device_create_file(&new_client->dev, &dev_attr_temp1_crit);
629         device_create_file(&new_client->dev, &dev_attr_temp2_input);
630         device_create_file(&new_client->dev, &dev_attr_temp2_max);
631         device_create_file(&new_client->dev, &dev_attr_temp2_min);
632         device_create_file(&new_client->dev, &dev_attr_temp2_crit);
633
634         if (data->channel & CHAN_TEMP3) {
635                 device_create_file(&new_client->dev, &dev_attr_temp3_input);
636                 device_create_file(&new_client->dev, &dev_attr_temp3_max);
637                 device_create_file(&new_client->dev, &dev_attr_temp3_min);
638                 device_create_file(&new_client->dev, &dev_attr_temp3_crit);
639         } else {
640                 device_create_file(&new_client->dev, &dev_attr_in0_input);
641                 device_create_file(&new_client->dev, &dev_attr_in0_min);
642                 device_create_file(&new_client->dev, &dev_attr_in0_max);
643                 device_create_file(&new_client->dev, &dev_attr_in5_input);
644                 device_create_file(&new_client->dev, &dev_attr_in5_min);
645                 device_create_file(&new_client->dev, &dev_attr_in5_max);
646         }
647
648         if (!(data->channel & CHAN_NO_VID)) {
649                 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
650                 device_create_file(&new_client->dev, &dev_attr_vrm);
651         }
652
653         device_create_file(&new_client->dev, &dev_attr_alarms);
654         device_create_file(&new_client->dev, &dev_attr_aout_output);
655
656         return 0;
657
658 exit_free:
659         kfree(data);
660 exit:
661         return err;
662 }
663
664 static void lm87_init_client(struct i2c_client *client)
665 {
666         struct lm87_data *data = i2c_get_clientdata(client);
667         u8 config;
668
669         data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
670         data->vrm = i2c_which_vrm();
671
672         config = lm87_read_value(client, LM87_REG_CONFIG);
673         if (!(config & 0x01)) {
674                 int i;
675
676                 /* Limits are left uninitialized after power-up */
677                 for (i = 1; i < 6; i++) {
678                         lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
679                         lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
680                 }
681                 for (i = 0; i < 2; i++) {
682                         lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
683                         lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
684                         lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
685                         lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
686                 }
687                 if (data->channel & CHAN_TEMP3) {
688                         lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
689                         lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
690                 } else {
691                         lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
692                         lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
693                 }
694         }
695         if ((config & 0x81) != 0x01) {
696                 /* Start monitoring */
697                 lm87_write_value(client, LM87_REG_CONFIG,
698                                  (config & 0xF7) | 0x01);
699         }
700 }
701
702 static int lm87_detach_client(struct i2c_client *client)
703 {
704         int err;
705
706         if ((err = i2c_detach_client(client))) {
707                 dev_err(&client->dev, "Client deregistration failed, "
708                         "client not detached.\n");
709                 return err;
710         }
711
712         kfree(i2c_get_clientdata(client));
713         return 0;
714 }
715
716 static struct lm87_data *lm87_update_device(struct device *dev)
717 {
718         struct i2c_client *client = to_i2c_client(dev);
719         struct lm87_data *data = i2c_get_clientdata(client);
720
721         down(&data->update_lock);
722
723         if (jiffies - data->last_updated > HZ
724           || jiffies < data->last_updated
725           || !data->valid) {
726                 int i, j;
727
728                 dev_dbg(&client->dev, "Updating data.\n");
729
730                 i = (data->channel & CHAN_TEMP3) ? 1 : 0;
731                 j = (data->channel & CHAN_TEMP3) ? 5 : 6;
732                 for (; i < j; i++) {
733                         data->in[i] = lm87_read_value(client,
734                                       LM87_REG_IN(i));
735                         data->in_min[i] = lm87_read_value(client,
736                                           LM87_REG_IN_MIN(i));
737                         data->in_max[i] = lm87_read_value(client,
738                                           LM87_REG_IN_MAX(i));
739                 }
740
741                 for (i = 0; i < 2; i++) {
742                         if (data->channel & CHAN_NO_FAN(i)) {
743                                 data->in[6+i] = lm87_read_value(client,
744                                                 LM87_REG_AIN(i));
745                                 data->in_max[6+i] = lm87_read_value(client,
746                                                     LM87_REG_AIN_MAX(i));
747                                 data->in_min[6+i] = lm87_read_value(client,
748                                                     LM87_REG_AIN_MIN(i));
749
750                         } else {
751                                 data->fan[i] = lm87_read_value(client,
752                                                LM87_REG_FAN(i));
753                                 data->fan_min[i] = lm87_read_value(client,
754                                                    LM87_REG_FAN_MIN(i));
755                         }
756                 }
757
758                 j = (data->channel & CHAN_TEMP3) ? 3 : 2;
759                 for (i = 0 ; i < j; i++) {
760                         data->temp[i] = lm87_read_value(client,
761                                         LM87_REG_TEMP[i]);
762                         data->temp_high[i] = lm87_read_value(client,
763                                              LM87_REG_TEMP_HIGH[i]);
764                         data->temp_low[i] = lm87_read_value(client,
765                                             LM87_REG_TEMP_LOW[i]);
766                 }
767
768                 i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
769                 j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
770                 data->temp_crit_int = min(i, j);
771
772                 i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
773                 j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
774                 data->temp_crit_ext = min(i, j);
775
776                 i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
777                 data->fan_div[0] = (i >> 4) & 0x03;
778                 data->fan_div[1] = (i >> 6) & 0x03;
779                 data->vid = (i & 0x0F)
780                           | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
781                              << 4;
782
783                 data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
784                              | (lm87_read_value(client, LM87_REG_ALARMS2)
785                                 << 8);
786                 data->aout = lm87_read_value(client, LM87_REG_AOUT);
787
788                 data->last_updated = jiffies;
789                 data->valid = 1;
790         }
791
792         up(&data->update_lock);
793
794         return data;
795 }
796
797 static int __init sensors_lm87_init(void)
798 {
799         return i2c_add_driver(&lm87_driver);
800 }
801
802 static void __exit sensors_lm87_exit(void)
803 {
804         i2c_del_driver(&lm87_driver);
805 }
806
807 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
808 MODULE_DESCRIPTION("LM87 driver");
809 MODULE_LICENSE("GPL");
810
811 module_init(sensors_lm87_init);
812 module_exit(sensors_lm87_exit);