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