e7f14e61d6f2b1fb052ab42fd6ea5747c571eff8
[safe/jmp/linux-2.6] / drivers / hwmon / it87.c
1 /*
2     it87.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring.
4
5     Supports: IT8705F  Super I/O chip w/LPC interface
6               IT8712F  Super I/O chip w/LPC interface & SMBus
7               IT8716F  Super I/O chip w/LPC interface
8               Sis950   A clone of the IT8705F
9
10     Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com> 
11     Largely inspired by lm78.c of the same package
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28 /*
29     djg@pdp8.net David Gesswein 7/18/01
30     Modified to fix bug with not all alarms enabled.
31     Added ability to read battery voltage and select temperature sensor
32     type at module load time.
33 */
34
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/jiffies.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-isa.h>
41 #include <linux/hwmon.h>
42 #include <linux/hwmon-sysfs.h>
43 #include <linux/hwmon-vid.h>
44 #include <linux/err.h>
45 #include <linux/mutex.h>
46 #include <asm/io.h>
47
48
49 /* Addresses to scan */
50 static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
51 static unsigned short isa_address;
52
53 /* Insmod parameters */
54 I2C_CLIENT_INSMOD_3(it87, it8712, it8716);
55
56 #define REG     0x2e    /* The register to read/write */
57 #define DEV     0x07    /* Register: Logical device select */
58 #define VAL     0x2f    /* The value to read/write */
59 #define PME     0x04    /* The device with the fan registers in it */
60 #define DEVID   0x20    /* Register: Device ID */
61 #define DEVREV  0x22    /* Register: Device Revision */
62
63 static inline int
64 superio_inb(int reg)
65 {
66         outb(reg, REG);
67         return inb(VAL);
68 }
69
70 static int superio_inw(int reg)
71 {
72         int val;
73         outb(reg++, REG);
74         val = inb(VAL) << 8;
75         outb(reg, REG);
76         val |= inb(VAL);
77         return val;
78 }
79
80 static inline void
81 superio_select(void)
82 {
83         outb(DEV, REG);
84         outb(PME, VAL);
85 }
86
87 static inline void
88 superio_enter(void)
89 {
90         outb(0x87, REG);
91         outb(0x01, REG);
92         outb(0x55, REG);
93         outb(0x55, REG);
94 }
95
96 static inline void
97 superio_exit(void)
98 {
99         outb(0x02, REG);
100         outb(0x02, VAL);
101 }
102
103 #define IT8712F_DEVID 0x8712
104 #define IT8705F_DEVID 0x8705
105 #define IT8716F_DEVID 0x8716
106 #define IT87_ACT_REG  0x30
107 #define IT87_BASE_REG 0x60
108
109 /* Update battery voltage after every reading if true */
110 static int update_vbat;
111
112 /* Not all BIOSes properly configure the PWM registers */
113 static int fix_pwm_polarity;
114
115 /* Chip Type */
116
117 static u16 chip_type;
118
119 /* Many IT87 constants specified below */
120
121 /* Length of ISA address segment */
122 #define IT87_EXTENT 8
123
124 /* Where are the ISA address/data registers relative to the base address */
125 #define IT87_ADDR_REG_OFFSET 5
126 #define IT87_DATA_REG_OFFSET 6
127
128 /*----- The IT87 registers -----*/
129
130 #define IT87_REG_CONFIG        0x00
131
132 #define IT87_REG_ALARM1        0x01
133 #define IT87_REG_ALARM2        0x02
134 #define IT87_REG_ALARM3        0x03
135
136 #define IT87_REG_VID           0x0a
137 /* Warning: register 0x0b is used for something completely different in
138    new chips/revisions. I suspect only 16-bit tachometer mode will work
139    for these. */
140 #define IT87_REG_FAN_DIV       0x0b
141 #define IT87_REG_FAN_16BIT     0x0c
142
143 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
144
145 #define IT87_REG_FAN(nr)       (0x0d + (nr))
146 #define IT87_REG_FAN_MIN(nr)   (0x10 + (nr))
147 #define IT87_REG_FANX(nr)      (0x18 + (nr))
148 #define IT87_REG_FANX_MIN(nr)  (0x1b + (nr))
149 #define IT87_REG_FAN_MAIN_CTRL 0x13
150 #define IT87_REG_FAN_CTL       0x14
151 #define IT87_REG_PWM(nr)       (0x15 + (nr))
152
153 #define IT87_REG_VIN(nr)       (0x20 + (nr))
154 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
155
156 #define IT87_REG_VIN_MAX(nr)   (0x30 + (nr) * 2)
157 #define IT87_REG_VIN_MIN(nr)   (0x31 + (nr) * 2)
158 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
159 #define IT87_REG_TEMP_LOW(nr)  (0x41 + (nr) * 2)
160
161 #define IT87_REG_I2C_ADDR      0x48
162
163 #define IT87_REG_VIN_ENABLE    0x50
164 #define IT87_REG_TEMP_ENABLE   0x51
165
166 #define IT87_REG_CHIPID        0x58
167
168 #define IN_TO_REG(val)  (SENSORS_LIMIT((((val) + 8)/16),0,255))
169 #define IN_FROM_REG(val) ((val) * 16)
170
171 static inline u8 FAN_TO_REG(long rpm, int div)
172 {
173         if (rpm == 0)
174                 return 255;
175         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
176         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
177                              254);
178 }
179
180 static inline u16 FAN16_TO_REG(long rpm)
181 {
182         if (rpm == 0)
183                 return 0xffff;
184         return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
185 }
186
187 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
188 /* The divider is fixed to 2 in 16-bit mode */
189 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
190
191 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
192                                         ((val)+500)/1000),-128,127))
193 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
194
195 #define PWM_TO_REG(val)   ((val) >> 1)
196 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
197
198 static int DIV_TO_REG(int val)
199 {
200         int answer = 0;
201         while ((val >>= 1) != 0)
202                 answer++;
203         return answer;
204 }
205 #define DIV_FROM_REG(val) (1 << (val))
206
207
208 /* For each registered IT87, we need to keep some data in memory. That
209    data is pointed to by it87_list[NR]->data. The structure itself is
210    dynamically allocated, at the same time when a new it87 client is
211    allocated. */
212 struct it87_data {
213         struct i2c_client client;
214         struct class_device *class_dev;
215         struct mutex lock;
216         enum chips type;
217
218         struct mutex update_lock;
219         char valid;             /* !=0 if following fields are valid */
220         unsigned long last_updated;     /* In jiffies */
221
222         u8 in[9];               /* Register value */
223         u8 in_max[9];           /* Register value */
224         u8 in_min[9];           /* Register value */
225         u16 fan[3];             /* Register values, possibly combined */
226         u16 fan_min[3];         /* Register values, possibly combined */
227         u8 temp[3];             /* Register value */
228         u8 temp_high[3];        /* Register value */
229         u8 temp_low[3];         /* Register value */
230         u8 sensor;              /* Register value */
231         u8 fan_div[3];          /* Register encoding, shifted right */
232         u8 vid;                 /* Register encoding, combined */
233         u8 vrm;
234         u32 alarms;             /* Register encoding, combined */
235         u8 fan_main_ctrl;       /* Register value */
236         u8 manual_pwm_ctl[3];   /* manual PWM value set by user */
237 };
238
239
240 static int it87_attach_adapter(struct i2c_adapter *adapter);
241 static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
242 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
243 static int it87_detach_client(struct i2c_client *client);
244
245 static int it87_read_value(struct i2c_client *client, u8 reg);
246 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
247 static struct it87_data *it87_update_device(struct device *dev);
248 static int it87_check_pwm(struct i2c_client *client);
249 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
250
251
252 static struct i2c_driver it87_driver = {
253         .driver = {
254                 .name   = "it87",
255         },
256         .id             = I2C_DRIVERID_IT87,
257         .attach_adapter = it87_attach_adapter,
258         .detach_client  = it87_detach_client,
259 };
260
261 static struct i2c_driver it87_isa_driver = {
262         .driver = {
263                 .owner  = THIS_MODULE,
264                 .name   = "it87-isa",
265         },
266         .attach_adapter = it87_isa_attach_adapter,
267         .detach_client  = it87_detach_client,
268 };
269
270
271 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
272                 char *buf)
273 {
274         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
275         int nr = sensor_attr->index;
276
277         struct it87_data *data = it87_update_device(dev);
278         return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
279 }
280
281 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
282                 char *buf)
283 {
284         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
285         int nr = sensor_attr->index;
286
287         struct it87_data *data = it87_update_device(dev);
288         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
289 }
290
291 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
292                 char *buf)
293 {
294         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
295         int nr = sensor_attr->index;
296
297         struct it87_data *data = it87_update_device(dev);
298         return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
299 }
300
301 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
302                 const char *buf, size_t count)
303 {
304         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
305         int nr = sensor_attr->index;
306
307         struct i2c_client *client = to_i2c_client(dev);
308         struct it87_data *data = i2c_get_clientdata(client);
309         unsigned long val = simple_strtoul(buf, NULL, 10);
310
311         mutex_lock(&data->update_lock);
312         data->in_min[nr] = IN_TO_REG(val);
313         it87_write_value(client, IT87_REG_VIN_MIN(nr), 
314                         data->in_min[nr]);
315         mutex_unlock(&data->update_lock);
316         return count;
317 }
318 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
319                 const char *buf, size_t count)
320 {
321         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
322         int nr = sensor_attr->index;
323
324         struct i2c_client *client = to_i2c_client(dev);
325         struct it87_data *data = i2c_get_clientdata(client);
326         unsigned long val = simple_strtoul(buf, NULL, 10);
327
328         mutex_lock(&data->update_lock);
329         data->in_max[nr] = IN_TO_REG(val);
330         it87_write_value(client, IT87_REG_VIN_MAX(nr), 
331                         data->in_max[nr]);
332         mutex_unlock(&data->update_lock);
333         return count;
334 }
335
336 #define show_in_offset(offset)                                  \
337 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO,          \
338                 show_in, NULL, offset);
339
340 #define limit_in_offset(offset)                                 \
341 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,  \
342                 show_in_min, set_in_min, offset);               \
343 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,  \
344                 show_in_max, set_in_max, offset);
345
346 show_in_offset(0);
347 limit_in_offset(0);
348 show_in_offset(1);
349 limit_in_offset(1);
350 show_in_offset(2);
351 limit_in_offset(2);
352 show_in_offset(3);
353 limit_in_offset(3);
354 show_in_offset(4);
355 limit_in_offset(4);
356 show_in_offset(5);
357 limit_in_offset(5);
358 show_in_offset(6);
359 limit_in_offset(6);
360 show_in_offset(7);
361 limit_in_offset(7);
362 show_in_offset(8);
363
364 /* 3 temperatures */
365 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
366                 char *buf)
367 {
368         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369         int nr = sensor_attr->index;
370
371         struct it87_data *data = it87_update_device(dev);
372         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
373 }
374 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
375                 char *buf)
376 {
377         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
378         int nr = sensor_attr->index;
379
380         struct it87_data *data = it87_update_device(dev);
381         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
382 }
383 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
384                 char *buf)
385 {
386         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
387         int nr = sensor_attr->index;
388
389         struct it87_data *data = it87_update_device(dev);
390         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
391 }
392 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
393                 const char *buf, size_t count)
394 {
395         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
396         int nr = sensor_attr->index;
397
398         struct i2c_client *client = to_i2c_client(dev);
399         struct it87_data *data = i2c_get_clientdata(client);
400         int val = simple_strtol(buf, NULL, 10);
401
402         mutex_lock(&data->update_lock);
403         data->temp_high[nr] = TEMP_TO_REG(val);
404         it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
405         mutex_unlock(&data->update_lock);
406         return count;
407 }
408 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
409                 const char *buf, size_t count)
410 {
411         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412         int nr = sensor_attr->index;
413
414         struct i2c_client *client = to_i2c_client(dev);
415         struct it87_data *data = i2c_get_clientdata(client);
416         int val = simple_strtol(buf, NULL, 10);
417
418         mutex_lock(&data->update_lock);
419         data->temp_low[nr] = TEMP_TO_REG(val);
420         it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
421         mutex_unlock(&data->update_lock);
422         return count;
423 }
424 #define show_temp_offset(offset)                                        \
425 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO,                \
426                 show_temp, NULL, offset - 1);                           \
427 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,        \
428                 show_temp_max, set_temp_max, offset - 1);               \
429 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,        \
430                 show_temp_min, set_temp_min, offset - 1);
431
432 show_temp_offset(1);
433 show_temp_offset(2);
434 show_temp_offset(3);
435
436 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
437                 char *buf)
438 {
439         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
440         int nr = sensor_attr->index;
441
442         struct it87_data *data = it87_update_device(dev);
443         u8 reg = data->sensor; /* In case the value is updated while we use it */
444         
445         if (reg & (1 << nr))
446                 return sprintf(buf, "3\n");  /* thermal diode */
447         if (reg & (8 << nr))
448                 return sprintf(buf, "2\n");  /* thermistor */
449         return sprintf(buf, "0\n");      /* disabled */
450 }
451 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
452                 const char *buf, size_t count)
453 {
454         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
455         int nr = sensor_attr->index;
456
457         struct i2c_client *client = to_i2c_client(dev);
458         struct it87_data *data = i2c_get_clientdata(client);
459         int val = simple_strtol(buf, NULL, 10);
460
461         mutex_lock(&data->update_lock);
462
463         data->sensor &= ~(1 << nr);
464         data->sensor &= ~(8 << nr);
465         /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
466         if (val == 3)
467             data->sensor |= 1 << nr;
468         else if (val == 2)
469             data->sensor |= 8 << nr;
470         else if (val != 0) {
471                 mutex_unlock(&data->update_lock);
472                 return -EINVAL;
473         }
474         it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
475         mutex_unlock(&data->update_lock);
476         return count;
477 }
478 #define show_sensor_offset(offset)                                      \
479 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR,       \
480                 show_sensor, set_sensor, offset - 1);
481
482 show_sensor_offset(1);
483 show_sensor_offset(2);
484 show_sensor_offset(3);
485
486 /* 3 Fans */
487 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
488                 char *buf)
489 {
490         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
491         int nr = sensor_attr->index;
492
493         struct it87_data *data = it87_update_device(dev);
494         return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr], 
495                                 DIV_FROM_REG(data->fan_div[nr])));
496 }
497 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
498                 char *buf)
499 {
500         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
501         int nr = sensor_attr->index;
502
503         struct it87_data *data = it87_update_device(dev);
504         return sprintf(buf,"%d\n",
505                 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
506 }
507 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
508                 char *buf)
509 {
510         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
511         int nr = sensor_attr->index;
512
513         struct it87_data *data = it87_update_device(dev);
514         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
515 }
516 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
517                 char *buf)
518 {
519         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
520         int nr = sensor_attr->index;
521
522         struct it87_data *data = it87_update_device(dev);
523         return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
524 }
525 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
526                 char *buf)
527 {
528         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
529         int nr = sensor_attr->index;
530
531         struct it87_data *data = it87_update_device(dev);
532         return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
533 }
534 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
535                 const char *buf, size_t count)
536 {
537         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
538         int nr = sensor_attr->index;
539
540         struct i2c_client *client = to_i2c_client(dev);
541         struct it87_data *data = i2c_get_clientdata(client);
542         int val = simple_strtol(buf, NULL, 10);
543         u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
544
545         mutex_lock(&data->update_lock);
546         switch (nr) {
547         case 0: data->fan_div[nr] = reg & 0x07; break;
548         case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
549         case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
550         }
551
552         data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
553         it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
554         mutex_unlock(&data->update_lock);
555         return count;
556 }
557 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
558                 const char *buf, size_t count)
559 {
560         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
561         int nr = sensor_attr->index;
562
563         struct i2c_client *client = to_i2c_client(dev);
564         struct it87_data *data = i2c_get_clientdata(client);
565         int val = simple_strtol(buf, NULL, 10);
566         int i, min[3];
567         u8 old;
568
569         mutex_lock(&data->update_lock);
570         old = it87_read_value(client, IT87_REG_FAN_DIV);
571
572         for (i = 0; i < 3; i++)
573                 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
574
575         switch (nr) {
576         case 0:
577         case 1:
578                 data->fan_div[nr] = DIV_TO_REG(val);
579                 break;
580         case 2:
581                 if (val < 8)
582                         data->fan_div[nr] = 1;
583                 else
584                         data->fan_div[nr] = 3;
585         }
586         val = old & 0x80;
587         val |= (data->fan_div[0] & 0x07);
588         val |= (data->fan_div[1] & 0x07) << 3;
589         if (data->fan_div[2] == 3)
590                 val |= 0x1 << 6;
591         it87_write_value(client, IT87_REG_FAN_DIV, val);
592
593         for (i = 0; i < 3; i++) {
594                 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
595                 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
596         }
597         mutex_unlock(&data->update_lock);
598         return count;
599 }
600 static ssize_t set_pwm_enable(struct device *dev,
601                 struct device_attribute *attr, const char *buf, size_t count)
602 {
603         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
604         int nr = sensor_attr->index;
605
606         struct i2c_client *client = to_i2c_client(dev);
607         struct it87_data *data = i2c_get_clientdata(client);
608         int val = simple_strtol(buf, NULL, 10);
609
610         mutex_lock(&data->update_lock);
611
612         if (val == 0) {
613                 int tmp;
614                 /* make sure the fan is on when in on/off mode */
615                 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
616                 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
617                 /* set on/off mode */
618                 data->fan_main_ctrl &= ~(1 << nr);
619                 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
620         } else if (val == 1) {
621                 /* set SmartGuardian mode */
622                 data->fan_main_ctrl |= (1 << nr);
623                 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
624                 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
625                 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
626         } else {
627                 mutex_unlock(&data->update_lock);
628                 return -EINVAL;
629         }
630
631         mutex_unlock(&data->update_lock);
632         return count;
633 }
634 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
635                 const char *buf, size_t count)
636 {
637         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
638         int nr = sensor_attr->index;
639
640         struct i2c_client *client = to_i2c_client(dev);
641         struct it87_data *data = i2c_get_clientdata(client);
642         int val = simple_strtol(buf, NULL, 10);
643
644         if (val < 0 || val > 255)
645                 return -EINVAL;
646
647         mutex_lock(&data->update_lock);
648         data->manual_pwm_ctl[nr] = val;
649         if (data->fan_main_ctrl & (1 << nr))
650                 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
651         mutex_unlock(&data->update_lock);
652         return count;
653 }
654
655 #define show_fan_offset(offset)                                 \
656 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,         \
657                 show_fan, NULL, offset - 1);                    \
658 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
659                 show_fan_min, set_fan_min, offset - 1);         \
660 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
661                 show_fan_div, set_fan_div, offset - 1);
662
663 show_fan_offset(1);
664 show_fan_offset(2);
665 show_fan_offset(3);
666
667 #define show_pwm_offset(offset)                                         \
668 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR,      \
669                 show_pwm_enable, set_pwm_enable, offset - 1);           \
670 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR,               \
671                 show_pwm, set_pwm, offset - 1);
672
673 show_pwm_offset(1);
674 show_pwm_offset(2);
675 show_pwm_offset(3);
676
677 /* A different set of callbacks for 16-bit fans */
678 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
679                 char *buf)
680 {
681         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
682         int nr = sensor_attr->index;
683         struct it87_data *data = it87_update_device(dev);
684         return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
685 }
686
687 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
688                 char *buf)
689 {
690         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
691         int nr = sensor_attr->index;
692         struct it87_data *data = it87_update_device(dev);
693         return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
694 }
695
696 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
697                 const char *buf, size_t count)
698 {
699         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
700         int nr = sensor_attr->index;
701         struct i2c_client *client = to_i2c_client(dev);
702         struct it87_data *data = i2c_get_clientdata(client);
703         int val = simple_strtol(buf, NULL, 10);
704
705         mutex_lock(&data->update_lock);
706         data->fan_min[nr] = FAN16_TO_REG(val);
707         it87_write_value(client, IT87_REG_FAN_MIN(nr),
708                          data->fan_min[nr] & 0xff);
709         it87_write_value(client, IT87_REG_FANX_MIN(nr),
710                          data->fan_min[nr] >> 8);
711         mutex_unlock(&data->update_lock);
712         return count;
713 }
714
715 /* We want to use the same sysfs file names as 8-bit fans, but we need
716    different variable names, so we have to use SENSOR_ATTR instead of
717    SENSOR_DEVICE_ATTR. */
718 #define show_fan16_offset(offset) \
719 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
720         = SENSOR_ATTR(fan##offset##_input, S_IRUGO,             \
721                 show_fan16, NULL, offset - 1);                  \
722 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
723         = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,     \
724                 show_fan16_min, set_fan16_min, offset - 1)
725
726 show_fan16_offset(1);
727 show_fan16_offset(2);
728 show_fan16_offset(3);
729
730 /* Alarms */
731 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
732 {
733         struct it87_data *data = it87_update_device(dev);
734         return sprintf(buf, "%u\n", data->alarms);
735 }
736 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
737
738 static ssize_t
739 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
740 {
741         struct it87_data *data = it87_update_device(dev);
742         return sprintf(buf, "%u\n", data->vrm);
743 }
744 static ssize_t
745 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
746 {
747         struct i2c_client *client = to_i2c_client(dev);
748         struct it87_data *data = i2c_get_clientdata(client);
749         u32 val;
750
751         val = simple_strtoul(buf, NULL, 10);
752         data->vrm = val;
753
754         return count;
755 }
756 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
757 #define device_create_file_vrm(client) \
758 device_create_file(&client->dev, &dev_attr_vrm)
759
760 static ssize_t
761 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
762 {
763         struct it87_data *data = it87_update_device(dev);
764         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
765 }
766 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
767 #define device_create_file_vid(client) \
768 device_create_file(&client->dev, &dev_attr_cpu0_vid)
769
770 /* This function is called when:
771      * it87_driver is inserted (when this module is loaded), for each
772        available adapter
773      * when a new adapter is inserted (and it87_driver is still present) */
774 static int it87_attach_adapter(struct i2c_adapter *adapter)
775 {
776         if (!(adapter->class & I2C_CLASS_HWMON))
777                 return 0;
778         return i2c_probe(adapter, &addr_data, it87_detect);
779 }
780
781 static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
782 {
783         return it87_detect(adapter, isa_address, -1);
784 }
785
786 /* SuperIO detection - will change isa_address if a chip is found */
787 static int __init it87_find(unsigned short *address)
788 {
789         int err = -ENODEV;
790
791         superio_enter();
792         chip_type = superio_inw(DEVID);
793         if (chip_type != IT8712F_DEVID
794          && chip_type != IT8716F_DEVID
795          && chip_type != IT8705F_DEVID)
796                 goto exit;
797
798         superio_select();
799         if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
800                 pr_info("it87: Device not activated, skipping\n");
801                 goto exit;
802         }
803
804         *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
805         if (*address == 0) {
806                 pr_info("it87: Base address not set, skipping\n");
807                 goto exit;
808         }
809
810         err = 0;
811         pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
812                 chip_type, *address, superio_inb(DEVREV) & 0x0f);
813
814 exit:
815         superio_exit();
816         return err;
817 }
818
819 /* This function is called by i2c_probe */
820 static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
821 {
822         int i;
823         struct i2c_client *new_client;
824         struct it87_data *data;
825         int err = 0;
826         const char *name = "";
827         int is_isa = i2c_is_isa_adapter(adapter);
828         int enable_pwm_interface;
829
830         if (!is_isa && 
831             !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
832                 goto ERROR0;
833
834         /* Reserve the ISA region */
835         if (is_isa)
836                 if (!request_region(address, IT87_EXTENT,
837                                     it87_isa_driver.driver.name))
838                         goto ERROR0;
839
840         /* For now, we presume we have a valid client. We create the
841            client structure, even though we cannot fill it completely yet.
842            But it allows us to access it87_{read,write}_value. */
843
844         if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
845                 err = -ENOMEM;
846                 goto ERROR1;
847         }
848
849         new_client = &data->client;
850         if (is_isa)
851                 mutex_init(&data->lock);
852         i2c_set_clientdata(new_client, data);
853         new_client->addr = address;
854         new_client->adapter = adapter;
855         new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
856         new_client->flags = 0;
857
858         /* Now, we do the remaining detection. */
859
860         if (kind < 0) {
861                 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
862                   || (!is_isa
863                    && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
864                         err = -ENODEV;
865                         goto ERROR2;
866                 }
867         }
868
869         /* Determine the chip type. */
870         if (kind <= 0) {
871                 i = it87_read_value(new_client, IT87_REG_CHIPID);
872                 if (i == 0x90) {
873                         kind = it87;
874                         if (is_isa) {
875                                 switch (chip_type) {
876                                 case IT8712F_DEVID:
877                                         kind = it8712;
878                                         break;
879                                 case IT8716F_DEVID:
880                                         kind = it8716;
881                                         break;
882                                 }
883                         }
884                 }
885                 else {
886                         if (kind == 0)
887                                 dev_info(&adapter->dev, 
888                                         "Ignoring 'force' parameter for unknown chip at "
889                                         "adapter %d, address 0x%02x\n",
890                                         i2c_adapter_id(adapter), address);
891                         err = -ENODEV;
892                         goto ERROR2;
893                 }
894         }
895
896         if (kind == it87) {
897                 name = "it87";
898         } else if (kind == it8712) {
899                 name = "it8712";
900         } else if (kind == it8716) {
901                 name = "it8716";
902         }
903
904         /* Fill in the remaining client fields and put it into the global list */
905         strlcpy(new_client->name, name, I2C_NAME_SIZE);
906         data->type = kind;
907         data->valid = 0;
908         mutex_init(&data->update_lock);
909
910         /* Tell the I2C layer a new client has arrived */
911         if ((err = i2c_attach_client(new_client)))
912                 goto ERROR2;
913
914         if (!is_isa)
915                 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
916                          "hardware monitoring chips is deprecated. Please "
917                          "report if you still rely on it.\n");
918
919         /* Check PWM configuration */
920         enable_pwm_interface = it87_check_pwm(new_client);
921
922         /* Initialize the IT87 chip */
923         it87_init_client(new_client, data);
924
925         /* Register sysfs hooks */
926         data->class_dev = hwmon_device_register(&new_client->dev);
927         if (IS_ERR(data->class_dev)) {
928                 err = PTR_ERR(data->class_dev);
929                 goto ERROR3;
930         }
931
932         device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
933         device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
934         device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
935         device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
936         device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
937         device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
938         device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
939         device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
940         device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
941         device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
942         device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
943         device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
944         device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
945         device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
946         device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
947         device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
948         device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
949         device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
950         device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
951         device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
952         device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
953         device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
954         device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
955         device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
956         device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
957         device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
958         device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
959         device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
960         device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
961         device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
962         device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
963         device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
964         device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
965         device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
966         device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
967         device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
968         device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
969
970         if (data->type == it8716) { /* 16-bit tachometers */
971                 device_create_file(&new_client->dev,
972                                    &sensor_dev_attr_fan1_input16.dev_attr);
973                 device_create_file(&new_client->dev,
974                                    &sensor_dev_attr_fan2_input16.dev_attr);
975                 device_create_file(&new_client->dev,
976                                    &sensor_dev_attr_fan3_input16.dev_attr);
977                 device_create_file(&new_client->dev,
978                                    &sensor_dev_attr_fan1_min16.dev_attr);
979                 device_create_file(&new_client->dev,
980                                    &sensor_dev_attr_fan2_min16.dev_attr);
981                 device_create_file(&new_client->dev,
982                                    &sensor_dev_attr_fan3_min16.dev_attr);
983         } else {
984                 device_create_file(&new_client->dev,
985                                    &sensor_dev_attr_fan1_input.dev_attr);
986                 device_create_file(&new_client->dev,
987                                    &sensor_dev_attr_fan2_input.dev_attr);
988                 device_create_file(&new_client->dev,
989                                    &sensor_dev_attr_fan3_input.dev_attr);
990                 device_create_file(&new_client->dev,
991                                    &sensor_dev_attr_fan1_min.dev_attr);
992                 device_create_file(&new_client->dev,
993                                    &sensor_dev_attr_fan2_min.dev_attr);
994                 device_create_file(&new_client->dev,
995                                    &sensor_dev_attr_fan3_min.dev_attr);
996                 device_create_file(&new_client->dev,
997                                    &sensor_dev_attr_fan1_div.dev_attr);
998                 device_create_file(&new_client->dev,
999                                    &sensor_dev_attr_fan2_div.dev_attr);
1000                 device_create_file(&new_client->dev,
1001                                    &sensor_dev_attr_fan3_div.dev_attr);
1002         }
1003
1004         device_create_file(&new_client->dev, &dev_attr_alarms);
1005         if (enable_pwm_interface) {
1006                 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
1007                 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
1008                 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
1009                 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
1010                 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
1011                 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
1012         }
1013
1014         if (data->type == it8712 || data->type == it8716) {
1015                 data->vrm = vid_which_vrm();
1016                 device_create_file_vrm(new_client);
1017                 device_create_file_vid(new_client);
1018         }
1019
1020         return 0;
1021
1022 ERROR3:
1023         i2c_detach_client(new_client);
1024 ERROR2:
1025         kfree(data);
1026 ERROR1:
1027         if (is_isa)
1028                 release_region(address, IT87_EXTENT);
1029 ERROR0:
1030         return err;
1031 }
1032
1033 static int it87_detach_client(struct i2c_client *client)
1034 {
1035         struct it87_data *data = i2c_get_clientdata(client);
1036         int err;
1037
1038         hwmon_device_unregister(data->class_dev);
1039
1040         if ((err = i2c_detach_client(client)))
1041                 return err;
1042
1043         if(i2c_is_isa_client(client))
1044                 release_region(client->addr, IT87_EXTENT);
1045         kfree(data);
1046
1047         return 0;
1048 }
1049
1050 /* The SMBus locks itself, but ISA access must be locked explicitly! 
1051    We don't want to lock the whole ISA bus, so we lock each client
1052    separately.
1053    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1054    would slow down the IT87 access and should not be necessary. */
1055 static int it87_read_value(struct i2c_client *client, u8 reg)
1056 {
1057         struct it87_data *data = i2c_get_clientdata(client);
1058
1059         int res;
1060         if (i2c_is_isa_client(client)) {
1061                 mutex_lock(&data->lock);
1062                 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1063                 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
1064                 mutex_unlock(&data->lock);
1065                 return res;
1066         } else
1067                 return i2c_smbus_read_byte_data(client, reg);
1068 }
1069
1070 /* The SMBus locks itself, but ISA access muse be locked explicitly! 
1071    We don't want to lock the whole ISA bus, so we lock each client
1072    separately.
1073    We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1074    would slow down the IT87 access and should not be necessary. */
1075 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
1076 {
1077         struct it87_data *data = i2c_get_clientdata(client);
1078
1079         if (i2c_is_isa_client(client)) {
1080                 mutex_lock(&data->lock);
1081                 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1082                 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
1083                 mutex_unlock(&data->lock);
1084                 return 0;
1085         } else
1086                 return i2c_smbus_write_byte_data(client, reg, value);
1087 }
1088
1089 /* Return 1 if and only if the PWM interface is safe to use */
1090 static int it87_check_pwm(struct i2c_client *client)
1091 {
1092         /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1093          * and polarity set to active low is sign that this is the case so we
1094          * disable pwm control to protect the user. */
1095         int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1096         if ((tmp & 0x87) == 0) {
1097                 if (fix_pwm_polarity) {
1098                         /* The user asks us to attempt a chip reconfiguration.
1099                          * This means switching to active high polarity and
1100                          * inverting all fan speed values. */
1101                         int i;
1102                         u8 pwm[3];
1103
1104                         for (i = 0; i < 3; i++)
1105                                 pwm[i] = it87_read_value(client,
1106                                                          IT87_REG_PWM(i));
1107
1108                         /* If any fan is in automatic pwm mode, the polarity
1109                          * might be correct, as suspicious as it seems, so we
1110                          * better don't change anything (but still disable the
1111                          * PWM interface). */
1112                         if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1113                                 dev_info(&client->dev, "Reconfiguring PWM to "
1114                                          "active high polarity\n");
1115                                 it87_write_value(client, IT87_REG_FAN_CTL,
1116                                                  tmp | 0x87);
1117                                 for (i = 0; i < 3; i++)
1118                                         it87_write_value(client,
1119                                                          IT87_REG_PWM(i),
1120                                                          0x7f & ~pwm[i]);
1121                                 return 1;
1122                         }
1123
1124                         dev_info(&client->dev, "PWM configuration is "
1125                                  "too broken to be fixed\n");
1126                 }
1127
1128                 dev_info(&client->dev, "Detected broken BIOS "
1129                          "defaults, disabling PWM interface\n");
1130                 return 0;
1131         } else if (fix_pwm_polarity) {
1132                 dev_info(&client->dev, "PWM configuration looks "
1133                          "sane, won't touch\n");
1134         }
1135
1136         return 1;
1137 }
1138
1139 /* Called when we have found a new IT87. */
1140 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1141 {
1142         int tmp, i;
1143
1144         /* initialize to sane defaults:
1145          * - if the chip is in manual pwm mode, this will be overwritten with
1146          *   the actual settings on the chip (so in this case, initialization
1147          *   is not needed)
1148          * - if in automatic or on/off mode, we could switch to manual mode,
1149          *   read the registers and set manual_pwm_ctl accordingly, but currently
1150          *   this is not implemented, so we initialize to something sane */
1151         for (i = 0; i < 3; i++) {
1152                 data->manual_pwm_ctl[i] = 0xff;
1153         }
1154
1155         /* Check if temperature channnels are reset manually or by some reason */
1156         tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1157         if ((tmp & 0x3f) == 0) {
1158                 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1159                 tmp = (tmp & 0xc0) | 0x2a;
1160                 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1161         }
1162         data->sensor = tmp;
1163
1164         /* Check if voltage monitors are reset manually or by some reason */
1165         tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1166         if ((tmp & 0xff) == 0) {
1167                 /* Enable all voltage monitors */
1168                 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1169         }
1170
1171         /* Check if tachometers are reset manually or by some reason */
1172         data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1173         if ((data->fan_main_ctrl & 0x70) == 0) {
1174                 /* Enable all fan tachometers */
1175                 data->fan_main_ctrl |= 0x70;
1176                 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1177         }
1178
1179         /* Set tachometers to 16-bit mode if needed */
1180         if (data->type == it8716) {
1181                 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
1182                 if ((tmp & 0x07) != 0x07) {
1183                         dev_dbg(&client->dev,
1184                                 "Setting fan1-3 to 16-bit mode\n");
1185                         it87_write_value(client, IT87_REG_FAN_16BIT,
1186                                          tmp | 0x07);
1187                 }
1188         }
1189
1190         /* Set current fan mode registers and the default settings for the
1191          * other mode registers */
1192         for (i = 0; i < 3; i++) {
1193                 if (data->fan_main_ctrl & (1 << i)) {
1194                         /* pwm mode */
1195                         tmp = it87_read_value(client, IT87_REG_PWM(i));
1196                         if (tmp & 0x80) {
1197                                 /* automatic pwm - not yet implemented, but
1198                                  * leave the settings made by the BIOS alone
1199                                  * until a change is requested via the sysfs
1200                                  * interface */
1201                         } else {
1202                                 /* manual pwm */
1203                                 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1204                         }
1205                 }
1206         }
1207
1208         /* Start monitoring */
1209         it87_write_value(client, IT87_REG_CONFIG,
1210                          (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1211                          | (update_vbat ? 0x41 : 0x01));
1212 }
1213
1214 static struct it87_data *it87_update_device(struct device *dev)
1215 {
1216         struct i2c_client *client = to_i2c_client(dev);
1217         struct it87_data *data = i2c_get_clientdata(client);
1218         int i;
1219
1220         mutex_lock(&data->update_lock);
1221
1222         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1223             || !data->valid) {
1224
1225                 if (update_vbat) {
1226                         /* Cleared after each update, so reenable.  Value
1227                           returned by this read will be previous value */       
1228                         it87_write_value(client, IT87_REG_CONFIG,
1229                            it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1230                 }
1231                 for (i = 0; i <= 7; i++) {
1232                         data->in[i] =
1233                             it87_read_value(client, IT87_REG_VIN(i));
1234                         data->in_min[i] =
1235                             it87_read_value(client, IT87_REG_VIN_MIN(i));
1236                         data->in_max[i] =
1237                             it87_read_value(client, IT87_REG_VIN_MAX(i));
1238                 }
1239                 data->in[8] =
1240                     it87_read_value(client, IT87_REG_VIN(8));
1241                 /* Temperature sensor doesn't have limit registers, set
1242                    to min and max value */
1243                 data->in_min[8] = 0;
1244                 data->in_max[8] = 255;
1245
1246                 for (i = 0; i < 3; i++) {
1247                         data->fan_min[i] =
1248                             it87_read_value(client, IT87_REG_FAN_MIN(i));
1249                         data->fan[i] = it87_read_value(client,
1250                                        IT87_REG_FAN(i));
1251                         /* Add high byte if in 16-bit mode */
1252                         if (data->type == it8716) {
1253                                 data->fan[i] |= it87_read_value(client,
1254                                                 IT87_REG_FANX(i)) << 8;
1255                                 data->fan_min[i] |= it87_read_value(client,
1256                                                 IT87_REG_FANX_MIN(i)) << 8;
1257                         }
1258                 }
1259                 for (i = 0; i < 3; i++) {
1260                         data->temp[i] =
1261                             it87_read_value(client, IT87_REG_TEMP(i));
1262                         data->temp_high[i] =
1263                             it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1264                         data->temp_low[i] =
1265                             it87_read_value(client, IT87_REG_TEMP_LOW(i));
1266                 }
1267
1268                 /* Newer chips don't have clock dividers */
1269                 if (data->type != it8716) {
1270                         i = it87_read_value(client, IT87_REG_FAN_DIV);
1271                         data->fan_div[0] = i & 0x07;
1272                         data->fan_div[1] = (i >> 3) & 0x07;
1273                         data->fan_div[2] = (i & 0x40) ? 3 : 1;
1274                 }
1275
1276                 data->alarms =
1277                         it87_read_value(client, IT87_REG_ALARM1) |
1278                         (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1279                         (it87_read_value(client, IT87_REG_ALARM3) << 16);
1280                 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1281
1282                 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1283                 /* The 8705 does not have VID capability */
1284                 if (data->type == it8712 || data->type == it8716) {
1285                         data->vid = it87_read_value(client, IT87_REG_VID);
1286                         /* The older IT8712F revisions had only 5 VID pins,
1287                            but we assume it is always safe to read 6 bits. */
1288                         data->vid &= 0x3f;
1289                 }
1290                 data->last_updated = jiffies;
1291                 data->valid = 1;
1292         }
1293
1294         mutex_unlock(&data->update_lock);
1295
1296         return data;
1297 }
1298
1299 static int __init sm_it87_init(void)
1300 {
1301         int res;
1302
1303         res = i2c_add_driver(&it87_driver);
1304         if (res)
1305                 return res;
1306
1307         if (!it87_find(&isa_address)) {
1308                 res = i2c_isa_add_driver(&it87_isa_driver);
1309                 if (res) {
1310                         i2c_del_driver(&it87_driver);
1311                         return res;
1312                 }
1313         }
1314
1315         return 0;
1316 }
1317
1318 static void __exit sm_it87_exit(void)
1319 {
1320         if (isa_address)
1321                 i2c_isa_del_driver(&it87_isa_driver);
1322         i2c_del_driver(&it87_driver);
1323 }
1324
1325
1326 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1327 MODULE_DESCRIPTION("IT8705F/8712F/8716F, SiS950 driver");
1328 module_param(update_vbat, bool, 0);
1329 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1330 module_param(fix_pwm_polarity, bool, 0);
1331 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1332 MODULE_LICENSE("GPL");
1333
1334 module_init(sm_it87_init);
1335 module_exit(sm_it87_exit);