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