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