]> git.sur5r.net Git - groeck-it87/blob - it87.c
fix offset register access
[groeck-it87] / 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  *            IT8720F  Super I/O chip w/LPC interface
18  *            IT8721F  Super I/O chip w/LPC interface
19  *            IT8726F  Super I/O chip w/LPC interface
20  *            IT8728F  Super I/O chip w/LPC interface
21  *            IT8758E  Super I/O chip w/LPC interface
22  *            IT8782F  Super I/O chip w/LPC interface
23  *            IT8783E/F Super I/O chip w/LPC interface
24  *            Sis950   A clone of the IT8705F
25  *
26  *  Copyright (C) 2001 Chris Gauthron
27  *  Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
28  *
29  *  This program is free software; you can redistribute it and/or modify
30  *  it under the terms of the GNU General Public License as published by
31  *  the Free Software Foundation; either version 2 of the License, or
32  *  (at your option) any later version.
33  *
34  *  This program is distributed in the hope that it will be useful,
35  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
36  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  *  GNU General Public License for more details.
38  *
39  *  You should have received a copy of the GNU General Public License
40  *  along with this program; if not, write to the Free Software
41  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42  */
43
44 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/slab.h>
49 #include <linux/jiffies.h>
50 #include <linux/platform_device.h>
51 #include <linux/hwmon.h>
52 #include <linux/hwmon-sysfs.h>
53 #include <linux/hwmon-vid.h>
54 #include <linux/err.h>
55 #include <linux/mutex.h>
56 #include <linux/sysfs.h>
57 #include <linux/string.h>
58 #include <linux/dmi.h>
59 #include <linux/acpi.h>
60 #include <linux/io.h>
61 #include <linux/version.h>
62 #include "compat.h"
63
64 #define DRVNAME "it87"
65
66 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8782,
67              it8783 };
68
69 static unsigned short force_id;
70 module_param(force_id, ushort, 0);
71 MODULE_PARM_DESC(force_id, "Override the detected device ID");
72
73 static struct platform_device *pdev;
74
75 #define REG     0x2e    /* The register to read/write */
76 #define DEV     0x07    /* Register: Logical device select */
77 #define VAL     0x2f    /* The value to read/write */
78 #define PME     0x04    /* The device with the fan registers in it */
79
80 /* The device with the IT8718F/IT8720F VID value in it */
81 #define GPIO    0x07
82
83 #define DEVID   0x20    /* Register: Device ID */
84 #define DEVREV  0x22    /* Register: Device Revision */
85
86 static inline int superio_inb(int reg)
87 {
88         outb(reg, REG);
89         return inb(VAL);
90 }
91
92 static inline void superio_outb(int reg, int val)
93 {
94         outb(reg, REG);
95         outb(val, VAL);
96 }
97
98 static int superio_inw(int reg)
99 {
100         int val;
101         outb(reg++, REG);
102         val = inb(VAL) << 8;
103         outb(reg, REG);
104         val |= inb(VAL);
105         return val;
106 }
107
108 static inline void superio_select(int ldn)
109 {
110         outb(DEV, REG);
111         outb(ldn, VAL);
112 }
113
114 static inline int superio_enter(void)
115 {
116         /*
117          * Try to reserve REG and REG + 1 for exclusive access.
118          */
119         if (!request_muxed_region(REG, 2, DRVNAME))
120                 return -EBUSY;
121
122         outb(0x87, REG);
123         outb(0x01, REG);
124         outb(0x55, REG);
125         outb(0x55, REG);
126         return 0;
127 }
128
129 static inline void superio_exit(void)
130 {
131         outb(0x02, REG);
132         outb(0x02, VAL);
133         release_region(REG, 2);
134 }
135
136 /* Logical device 4 registers */
137 #define IT8712F_DEVID 0x8712
138 #define IT8705F_DEVID 0x8705
139 #define IT8716F_DEVID 0x8716
140 #define IT8718F_DEVID 0x8718
141 #define IT8720F_DEVID 0x8720
142 #define IT8721F_DEVID 0x8721
143 #define IT8726F_DEVID 0x8726
144 #define IT8728F_DEVID 0x8728
145 #define IT8782F_DEVID 0x8782
146 #define IT8783E_DEVID 0x8783
147 #define IT87_ACT_REG  0x30
148 #define IT87_BASE_REG 0x60
149
150 /* Logical device 7 registers (IT8712F and later) */
151 #define IT87_SIO_GPIO1_REG      0x25
152 #define IT87_SIO_GPIO3_REG      0x27
153 #define IT87_SIO_GPIO5_REG      0x29
154 #define IT87_SIO_PINX1_REG      0x2a    /* Pin selection */
155 #define IT87_SIO_PINX2_REG      0x2c    /* Pin selection */
156 #define IT87_SIO_SPI_REG        0xef    /* SPI function pin select */
157 #define IT87_SIO_VID_REG        0xfc    /* VID value */
158 #define IT87_SIO_BEEP_PIN_REG   0xf6    /* Beep pin mapping */
159
160 /* Update battery voltage after every reading if true */
161 static bool update_vbat;
162
163 /* Not all BIOSes properly configure the PWM registers */
164 static bool fix_pwm_polarity;
165
166 /* Many IT87 constants specified below */
167
168 /* Length of ISA address segment */
169 #define IT87_EXTENT 8
170
171 /* Length of ISA address segment for Environmental Controller */
172 #define IT87_EC_EXTENT 2
173
174 /* Offset of EC registers from ISA base address */
175 #define IT87_EC_OFFSET 5
176
177 /* Where are the ISA address/data registers relative to the EC base address */
178 #define IT87_ADDR_REG_OFFSET 0
179 #define IT87_DATA_REG_OFFSET 1
180
181 /*----- The IT87 registers -----*/
182
183 #define IT87_REG_CONFIG        0x00
184
185 #define IT87_REG_ALARM1        0x01
186 #define IT87_REG_ALARM2        0x02
187 #define IT87_REG_ALARM3        0x03
188
189 /*
190  * The IT8718F and IT8720F have the VID value in a different register, in
191  * Super-I/O configuration space.
192  */
193 #define IT87_REG_VID           0x0a
194 /*
195  * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
196  * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
197  * mode.
198  */
199 #define IT87_REG_FAN_DIV       0x0b
200 #define IT87_REG_FAN_16BIT     0x0c
201
202 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
203
204 static const u8 IT87_REG_FAN[]          = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
205 static const u8 IT87_REG_FAN_MIN[]      = { 0x10, 0x11, 0x12, 0x84, 0x86 };
206 static const u8 IT87_REG_FANX[]         = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
207 static const u8 IT87_REG_FANX_MIN[]     = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
208 static const u8 IT87_REG_TEMP_OFFSET[]  = { 0x56, 0x57, 0x59 };
209
210 #define IT87_REG_FAN_MAIN_CTRL 0x13
211 #define IT87_REG_FAN_CTL       0x14
212 #define IT87_REG_PWM(nr)       (0x15 + (nr))
213 #define IT87_REG_PWM_DUTY(nr)  (0x63 + (nr) * 8)
214
215 #define IT87_REG_VIN(nr)       (0x20 + (nr))
216 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
217
218 #define IT87_REG_VIN_MAX(nr)   (0x30 + (nr) * 2)
219 #define IT87_REG_VIN_MIN(nr)   (0x31 + (nr) * 2)
220 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
221 #define IT87_REG_TEMP_LOW(nr)  (0x41 + (nr) * 2)
222
223 #define IT87_REG_VIN_ENABLE    0x50
224 #define IT87_REG_TEMP_ENABLE   0x51
225 #define IT87_REG_TEMP_EXTRA    0x55
226 #define IT87_REG_BEEP_ENABLE   0x5c
227
228 #define IT87_REG_CHIPID        0x58
229
230 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
231 #define IT87_REG_AUTO_PWM(nr, i)  (0x65 + (nr) * 8 + (i))
232
233 struct it87_devices {
234         const char * const name;
235         u16 features;
236 };
237
238 #define FEAT_12MV_ADC           (1 << 0)
239 #define FEAT_NEWER_AUTOPWM      (1 << 1)
240 #define FEAT_OLD_AUTOPWM        (1 << 2)
241 #define FEAT_16BIT_FAN          (1 << 3)
242
243 static const struct it87_devices it87_devices[] = {
244         [it87] = {
245                 .name = "it87",
246                 .features = FEAT_OLD_AUTOPWM,   /* may need to override */
247         },
248         [it8712] = {
249                 .name = "it8712",
250                 .features = FEAT_OLD_AUTOPWM,   /* may need to overwrite */
251         },
252         [it8716] = {
253                 .name = "it8716",
254                 .features = FEAT_16BIT_FAN,
255         },
256         [it8718] = {
257                 .name = "it8718",
258                 .features = FEAT_16BIT_FAN,
259         },
260         [it8720] = {
261                 .name = "it8720",
262                 .features = FEAT_16BIT_FAN,
263         },
264         [it8721] = {
265                 .name = "it8721",
266                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FAN,
267         },
268         [it8728] = {
269                 .name = "it8728",
270                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FAN,
271         },
272         [it8782] = {
273                 .name = "it8782",
274                 .features = FEAT_16BIT_FAN,
275         },
276         [it8783] = {
277                 .name = "it8783",
278                 .features = FEAT_16BIT_FAN,
279         },
280 };
281
282 #define has_16bit_fans(data)    ((data)->features & FEAT_16BIT_FAN)
283 #define has_12mv_adc(data)      ((data)->features & FEAT_12MV_ADC)
284 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
285 #define has_old_autopwm(data)   ((data)->features & FEAT_OLD_AUTOPWM)
286
287 struct it87_sio_data {
288         enum chips type;
289         /* Values read from Super-I/O config space */
290         u8 revision;
291         u8 vid_value;
292         u8 beep_pin;
293         u8 internal;    /* Internal sensors can be labeled */
294         /* Features skipped based on config or DMI */
295         u16 skip_in;
296         u8 skip_vid;
297         u8 skip_fan;
298         u8 skip_pwm;
299         u8 skip_temp;
300 };
301
302 /*
303  * For each registered chip, we need to keep some data in memory.
304  * The structure is dynamically allocated.
305  */
306 struct it87_data {
307         struct device *hwmon_dev;
308         enum chips type;
309         u32 features;
310
311         unsigned short addr;
312         const char *name;
313         struct mutex update_lock;
314         char valid;             /* !=0 if following fields are valid */
315         unsigned long last_updated;     /* In jiffies */
316
317         u16 in_scaled;          /* Internal voltage sensors are scaled */
318         u8 in[9][3];            /* [nr][0]=in, [1]=min, [2]=max */
319         u8 has_fan;             /* Bitfield, fans enabled */
320         u16 fan[5][2];          /* Register values, [nr][0]=fan, [1]=min */
321         u8 has_temp;            /* Bitfield, temp sensors enabled */
322         s8 temp[3][4];          /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
323         u8 sensor;              /* Register value */
324         u8 fan_div[3];          /* Register encoding, shifted right */
325         u8 vid;                 /* Register encoding, combined */
326         u8 vrm;
327         u32 alarms;             /* Register encoding, combined */
328         u8 beeps;               /* Register encoding */
329         u8 fan_main_ctrl;       /* Register value */
330         u8 fan_ctl;             /* Register value */
331
332         /*
333          * The following 3 arrays correspond to the same registers up to
334          * the IT8720F. The meaning of bits 6-0 depends on the value of bit
335          * 7, and we want to preserve settings on mode changes, so we have
336          * to track all values separately.
337          * Starting with the IT8721F, the manual PWM duty cycles are stored
338          * in separate registers (8-bit values), so the separate tracking
339          * is no longer needed, but it is still done to keep the driver
340          * simple.
341          */
342         u8 pwm_ctrl[3];         /* Register value */
343         u8 pwm_duty[3];         /* Manual PWM value set by user */
344         u8 pwm_temp_map[3];     /* PWM to temp. chan. mapping (bits 1-0) */
345
346         /* Automatic fan speed control registers */
347         u8 auto_pwm[3][4];      /* [nr][3] is hard-coded */
348         s8 auto_temp[3][5];     /* [nr][0] is point1_temp_hyst */
349 };
350
351 static int adc_lsb(const struct it87_data *data, int nr)
352 {
353         int lsb = has_12mv_adc(data) ? 12 : 16;
354         if (data->in_scaled & (1 << nr))
355                 lsb <<= 1;
356         return lsb;
357 }
358
359 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
360 {
361         val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
362         return SENSORS_LIMIT(val, 0, 255);
363 }
364
365 static int in_from_reg(const struct it87_data *data, int nr, int val)
366 {
367         return val * adc_lsb(data, nr);
368 }
369
370 static inline u8 FAN_TO_REG(long rpm, int div)
371 {
372         if (rpm == 0)
373                 return 255;
374         rpm = SENSORS_LIMIT(rpm, 1, 1000000);
375         return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
376                              254);
377 }
378
379 static inline u16 FAN16_TO_REG(long rpm)
380 {
381         if (rpm == 0)
382                 return 0xffff;
383         return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
384 }
385
386 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
387                                 1350000 / ((val) * (div)))
388 /* The divider is fixed to 2 in 16-bit mode */
389 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
390                              1350000 / ((val) * 2))
391
392 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
393                                         ((val) + 500) / 1000), -128, 127))
394 #define TEMP_FROM_REG(val) ((val) * 1000)
395
396 static u8 pwm_to_reg(const struct it87_data *data, long val)
397 {
398         if (has_newer_autopwm(data))
399                 return val;
400         else
401                 return val >> 1;
402 }
403
404 static int pwm_from_reg(const struct it87_data *data, u8 reg)
405 {
406         if (has_newer_autopwm(data))
407                 return reg;
408         else
409                 return (reg & 0x7f) << 1;
410 }
411
412
413 static int DIV_TO_REG(int val)
414 {
415         int answer = 0;
416         while (answer < 7 && (val >>= 1))
417                 answer++;
418         return answer;
419 }
420 #define DIV_FROM_REG(val) (1 << (val))
421
422 static const unsigned int pwm_freq[8] = {
423         48000000 / 128,
424         24000000 / 128,
425         12000000 / 128,
426         8000000 / 128,
427         6000000 / 128,
428         3000000 / 128,
429         1500000 / 128,
430         750000 / 128,
431 };
432
433 static int it87_probe(struct platform_device *pdev);
434 static int __devexit it87_remove(struct platform_device *pdev);
435
436 static int it87_read_value(struct it87_data *data, u8 reg);
437 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
438 static struct it87_data *it87_update_device(struct device *dev);
439 static int it87_check_pwm(struct device *dev);
440 static void it87_init_device(struct platform_device *pdev);
441
442
443 static struct platform_driver it87_driver = {
444         .driver = {
445                 .owner  = THIS_MODULE,
446                 .name   = DRVNAME,
447         },
448         .probe  = it87_probe,
449         .remove = __devexit_p(it87_remove),
450 };
451
452 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
453                        char *buf)
454 {
455         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
456         int nr = sattr->nr;
457         int index = sattr->index;
458
459         struct it87_data *data = it87_update_device(dev);
460         return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
461 }
462
463 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
464                       const char *buf, size_t count)
465 {
466         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
467         int nr = sattr->nr;
468         int index = sattr->index;
469
470         struct it87_data *data = dev_get_drvdata(dev);
471         unsigned long val;
472
473         if (kstrtoul(buf, 10, &val) < 0)
474                 return -EINVAL;
475
476         mutex_lock(&data->update_lock);
477         data->in[nr][index] = in_to_reg(data, nr, val);
478         it87_write_value(data,
479                          index == 1 ? IT87_REG_VIN_MIN(nr)
480                                     : IT87_REG_VIN_MAX(nr),
481                          data->in[nr][index]);
482         mutex_unlock(&data->update_lock);
483         return count;
484 }
485
486 #define S_IRUGOWU       (S_IRUGO | S_IWUSR)
487
488 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
489 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGOWU, show_in, set_in, 0, 1);
490 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGOWU, show_in, set_in, 0, 2);
491
492 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
493 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGOWU, show_in, set_in, 1, 1);
494 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGOWU, show_in, set_in, 1, 2);
495
496 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
497 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGOWU, show_in, set_in, 2, 1);
498 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGOWU, show_in, set_in, 2, 2);
499
500 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
501 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGOWU, show_in, set_in, 3, 1);
502 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGOWU, show_in, set_in, 3, 2);
503
504 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
505 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGOWU, show_in, set_in, 4, 1);
506 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGOWU, show_in, set_in, 4, 2);
507
508 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
509 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGOWU, show_in, set_in, 5, 1);
510 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGOWU, show_in, set_in, 5, 2);
511
512 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
513 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGOWU, show_in, set_in, 6, 1);
514 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGOWU, show_in, set_in, 6, 2);
515
516 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
517 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGOWU, show_in, set_in, 7, 1);
518 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGOWU, show_in, set_in, 7, 2);
519
520 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
521
522 /* 3 temperatures */
523 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
524                          char *buf)
525 {
526         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
527         int nr = sattr->nr;
528         int index = sattr->index;
529         struct it87_data *data = it87_update_device(dev);
530
531         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
532 }
533
534 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
535                         const char *buf, size_t count)
536 {
537         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
538         int nr = sattr->nr;
539         int index = sattr->index;
540         struct it87_data *data = dev_get_drvdata(dev);
541         long val;
542         u8 reg;
543
544         if (kstrtol(buf, 10, &val) < 0)
545                 return -EINVAL;
546
547         mutex_lock(&data->update_lock);
548         data->temp[nr][index] = TEMP_TO_REG(val);
549         if (index == 3) {
550                 reg = it87_read_value(data, IT87_REG_BEEP_ENABLE);
551                 reg |= 0x80;
552                 it87_write_value(data, IT87_REG_BEEP_ENABLE, reg);
553         }
554         it87_write_value(data,
555                          index == 1 ? IT87_REG_TEMP_LOW(nr)
556                                     : index == 2 ? IT87_REG_TEMP_HIGH(nr)
557                                                  : IT87_REG_TEMP_OFFSET[nr],
558                          data->temp[nr][index]);
559         data->valid = 0;
560         mutex_unlock(&data->update_lock);
561         return count;
562 }
563
564 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
565 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGOWU, show_temp, set_temp, 0, 1);
566 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGOWU, show_temp, set_temp, 0, 2);
567 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGOWU, show_temp, set_temp, 0, 3);
568 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
569 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGOWU, show_temp, set_temp, 1, 1);
570 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGOWU, show_temp, set_temp, 1, 2);
571 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGOWU, show_temp, set_temp, 1, 3);
572 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
573 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGOWU, show_temp, set_temp, 2, 1);
574 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGOWU, show_temp, set_temp, 2, 2);
575 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGOWU, show_temp, set_temp, 2, 3);
576
577 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
578                          char *buf)
579 {
580         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
581         int nr = sensor_attr->index;
582         struct it87_data *data = it87_update_device(dev);
583         u8 reg = data->sensor;      /* In case value is updated while used */
584
585         if (reg & (1 << nr))
586                 return sprintf(buf, "3\n");  /* thermal diode */
587         if (reg & (8 << nr))
588                 return sprintf(buf, "4\n");  /* thermistor */
589         return sprintf(buf, "0\n");      /* disabled */
590 }
591
592 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
593                         const char *buf, size_t count)
594 {
595         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
596         int nr = sensor_attr->index;
597
598         struct it87_data *data = dev_get_drvdata(dev);
599         long val;
600         u8 reg;
601
602         if (kstrtol(buf, 10, &val) < 0)
603                 return -EINVAL;
604
605         reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
606         reg &= ~(1 << nr);
607         reg &= ~(8 << nr);
608         if (val == 2) { /* backwards compatibility */
609                 dev_warn(dev,
610                          "Sensor type 2 is deprecated, please use 4 instead\n");
611                 val = 4;
612         }
613         /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
614         if (val == 3)
615                 reg |= 1 << nr;
616         else if (val == 4)
617                 reg |= 8 << nr;
618         else if (val != 0)
619                 return -EINVAL;
620
621         mutex_lock(&data->update_lock);
622         data->sensor = reg;
623         it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
624         data->valid = 0;        /* Force cache refresh */
625         mutex_unlock(&data->update_lock);
626         return count;
627 }
628
629 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGOWU, show_type, set_type, 0);
630 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGOWU, show_type, set_type, 1);
631 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGOWU, show_type, set_type, 2);
632
633 /* 3 Fans */
634
635 static int pwm_mode(const struct it87_data *data, int nr)
636 {
637         int ctrl = data->fan_main_ctrl & (1 << nr);
638
639         if (ctrl == 0)                                  /* Full speed */
640                 return 0;
641         if (data->pwm_ctrl[nr] & 0x80)                  /* Automatic mode */
642                 return 2;
643         else                                            /* Manual mode */
644                 return 1;
645 }
646
647 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
648                         char *buf)
649 {
650         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
651         int nr = sattr->nr;
652         int index = sattr->index;
653         int speed;
654         struct it87_data *data = it87_update_device(dev);
655
656         speed = has_16bit_fans(data) ?
657                 FAN16_FROM_REG(data->fan[nr][index]) :
658                 FAN_FROM_REG(data->fan[nr][index],
659                              DIV_FROM_REG(data->fan_div[nr]));
660         return sprintf(buf, "%d\n", speed);
661 }
662
663 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
664                 char *buf)
665 {
666         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
667         int nr = sensor_attr->index;
668
669         struct it87_data *data = it87_update_device(dev);
670         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
671 }
672 static ssize_t show_pwm_enable(struct device *dev,
673                 struct device_attribute *attr, char *buf)
674 {
675         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
676         int nr = sensor_attr->index;
677
678         struct it87_data *data = it87_update_device(dev);
679         return sprintf(buf, "%d\n", pwm_mode(data, nr));
680 }
681 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
682                 char *buf)
683 {
684         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
685         int nr = sensor_attr->index;
686
687         struct it87_data *data = it87_update_device(dev);
688         return sprintf(buf, "%d\n",
689                        pwm_from_reg(data, data->pwm_duty[nr]));
690 }
691 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
692                 char *buf)
693 {
694         struct it87_data *data = it87_update_device(dev);
695         int index = (data->fan_ctl >> 4) & 0x07;
696
697         return sprintf(buf, "%u\n", pwm_freq[index]);
698 }
699
700 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
701                        const char *buf, size_t count)
702 {
703         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
704         int nr = sattr->nr;
705         int index = sattr->index;
706
707         struct it87_data *data = dev_get_drvdata(dev);
708         long val;
709         u8 reg;
710
711         if (kstrtol(buf, 10, &val) < 0)
712                 return -EINVAL;
713
714         mutex_lock(&data->update_lock);
715
716         if (!has_16bit_fans(data)) {
717                 reg = it87_read_value(data, IT87_REG_FAN_DIV);
718                 switch (nr) {
719                 case 0:
720                         data->fan_div[nr] = reg & 0x07;
721                         break;
722                 case 1:
723                         data->fan_div[nr] = (reg >> 3) & 0x07;
724                         break;
725                 case 2:
726                         data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
727                         break;
728                 }
729                 data->fan[nr][index] =
730                   FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
731                 it87_write_value(data, IT87_REG_FAN_MIN[nr],
732                                  data->fan[nr][index]);
733         } else {
734                 data->fan[nr][index] = FAN16_TO_REG(val);
735                 it87_write_value(data, IT87_REG_FAN_MIN[nr],
736                                  data->fan[nr][index] & 0xff);
737                 it87_write_value(data, IT87_REG_FANX_MIN[nr],
738                                  data->fan[nr][index] >> 8);
739         }
740
741         mutex_unlock(&data->update_lock);
742         return count;
743 }
744
745 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
746                 const char *buf, size_t count)
747 {
748         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
749         int nr = sensor_attr->index;
750
751         struct it87_data *data = dev_get_drvdata(dev);
752         unsigned long val;
753         int min;
754         u8 old;
755
756         if (kstrtoul(buf, 10, &val) < 0)
757                 return -EINVAL;
758
759         mutex_lock(&data->update_lock);
760         old = it87_read_value(data, IT87_REG_FAN_DIV);
761
762         /* Save fan min limit */
763         min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
764
765         switch (nr) {
766         case 0:
767         case 1:
768                 data->fan_div[nr] = DIV_TO_REG(val);
769                 break;
770         case 2:
771                 if (val < 8)
772                         data->fan_div[nr] = 1;
773                 else
774                         data->fan_div[nr] = 3;
775         }
776         val = old & 0x80;
777         val |= (data->fan_div[0] & 0x07);
778         val |= (data->fan_div[1] & 0x07) << 3;
779         if (data->fan_div[2] == 3)
780                 val |= 0x1 << 6;
781         it87_write_value(data, IT87_REG_FAN_DIV, val);
782
783         /* Restore fan min limit */
784         data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
785         it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
786
787         mutex_unlock(&data->update_lock);
788         return count;
789 }
790
791 /* Returns 0 if OK, -EINVAL otherwise */
792 static int check_trip_points(struct device *dev, int nr)
793 {
794         const struct it87_data *data = dev_get_drvdata(dev);
795         int i, err = 0;
796
797         if (has_old_autopwm(data)) {
798                 for (i = 0; i < 3; i++) {
799                         if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
800                                 err = -EINVAL;
801                 }
802                 for (i = 0; i < 2; i++) {
803                         if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
804                                 err = -EINVAL;
805                 }
806         }
807
808         if (err) {
809                 dev_err(dev,
810                         "Inconsistent trip points, not switching to automatic mode\n");
811                 dev_err(dev, "Adjust the trip points and try again\n");
812         }
813         return err;
814 }
815
816 static ssize_t set_pwm_enable(struct device *dev,
817                 struct device_attribute *attr, const char *buf, size_t count)
818 {
819         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
820         int nr = sensor_attr->index;
821
822         struct it87_data *data = dev_get_drvdata(dev);
823         long val;
824
825         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
826                 return -EINVAL;
827
828         /* Check trip points before switching to automatic mode */
829         if (val == 2) {
830                 if (check_trip_points(dev, nr) < 0)
831                         return -EINVAL;
832         }
833
834         mutex_lock(&data->update_lock);
835
836         if (val == 0) {
837                 int tmp;
838                 /* make sure the fan is on when in on/off mode */
839                 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
840                 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
841                 /* set on/off mode */
842                 data->fan_main_ctrl &= ~(1 << nr);
843                 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
844                                  data->fan_main_ctrl);
845         } else {
846                 if (val == 1)                           /* Manual mode */
847                         data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
848                                              data->pwm_temp_map[nr] :
849                                              data->pwm_duty[nr];
850                 else                                    /* Automatic mode */
851                         data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
852                 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
853                 /* set SmartGuardian mode */
854                 data->fan_main_ctrl |= (1 << nr);
855                 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
856                                  data->fan_main_ctrl);
857         }
858
859         mutex_unlock(&data->update_lock);
860         return count;
861 }
862 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
863                 const char *buf, size_t count)
864 {
865         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
866         int nr = sensor_attr->index;
867
868         struct it87_data *data = dev_get_drvdata(dev);
869         long val;
870
871         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
872                 return -EINVAL;
873
874         mutex_lock(&data->update_lock);
875         if (has_newer_autopwm(data)) {
876                 /*
877                  * If we are in automatic mode, the PWM duty cycle register
878                  * is read-only so we can't write the value.
879                  */
880                 if (data->pwm_ctrl[nr] & 0x80) {
881                         mutex_unlock(&data->update_lock);
882                         return -EBUSY;
883                 }
884                 data->pwm_duty[nr] = pwm_to_reg(data, val);
885                 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
886                                  data->pwm_duty[nr]);
887         } else {
888                 data->pwm_duty[nr] = pwm_to_reg(data, val);
889                 /*
890                  * If we are in manual mode, write the duty cycle immediately;
891                  * otherwise, just store it for later use.
892                  */
893                 if (!(data->pwm_ctrl[nr] & 0x80)) {
894                         data->pwm_ctrl[nr] = data->pwm_duty[nr];
895                         it87_write_value(data, IT87_REG_PWM(nr),
896                                          data->pwm_ctrl[nr]);
897                 }
898         }
899         mutex_unlock(&data->update_lock);
900         return count;
901 }
902 static ssize_t set_pwm_freq(struct device *dev,
903                 struct device_attribute *attr, const char *buf, size_t count)
904 {
905         struct it87_data *data = dev_get_drvdata(dev);
906         unsigned long val;
907         int i;
908
909         if (kstrtoul(buf, 10, &val) < 0)
910                 return -EINVAL;
911
912         /* Search for the nearest available frequency */
913         for (i = 0; i < 7; i++) {
914                 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
915                         break;
916         }
917
918         mutex_lock(&data->update_lock);
919         data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
920         data->fan_ctl |= i << 4;
921         it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
922         mutex_unlock(&data->update_lock);
923
924         return count;
925 }
926 static ssize_t show_pwm_temp_map(struct device *dev,
927                 struct device_attribute *attr, char *buf)
928 {
929         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
930         int nr = sensor_attr->index;
931
932         struct it87_data *data = it87_update_device(dev);
933         int map;
934
935         if (data->pwm_temp_map[nr] < 3)
936                 map = 1 << data->pwm_temp_map[nr];
937         else
938                 map = 0;                        /* Should never happen */
939         return sprintf(buf, "%d\n", map);
940 }
941 static ssize_t set_pwm_temp_map(struct device *dev,
942                 struct device_attribute *attr, const char *buf, size_t count)
943 {
944         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
945         int nr = sensor_attr->index;
946
947         struct it87_data *data = dev_get_drvdata(dev);
948         long val;
949         u8 reg;
950
951         /*
952          * This check can go away if we ever support automatic fan speed
953          * control on newer chips.
954          */
955         if (!has_old_autopwm(data)) {
956                 dev_notice(dev, "Mapping change disabled for safety reasons\n");
957                 return -EINVAL;
958         }
959
960         if (kstrtol(buf, 10, &val) < 0)
961                 return -EINVAL;
962
963         switch (val) {
964         case (1 << 0):
965                 reg = 0x00;
966                 break;
967         case (1 << 1):
968                 reg = 0x01;
969                 break;
970         case (1 << 2):
971                 reg = 0x02;
972                 break;
973         default:
974                 return -EINVAL;
975         }
976
977         mutex_lock(&data->update_lock);
978         data->pwm_temp_map[nr] = reg;
979         /*
980          * If we are in automatic mode, write the temp mapping immediately;
981          * otherwise, just store it for later use.
982          */
983         if (data->pwm_ctrl[nr] & 0x80) {
984                 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
985                 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
986         }
987         mutex_unlock(&data->update_lock);
988         return count;
989 }
990
991 static ssize_t show_auto_pwm(struct device *dev,
992                 struct device_attribute *attr, char *buf)
993 {
994         struct it87_data *data = it87_update_device(dev);
995         struct sensor_device_attribute_2 *sensor_attr =
996                         to_sensor_dev_attr_2(attr);
997         int nr = sensor_attr->nr;
998         int point = sensor_attr->index;
999
1000         return sprintf(buf, "%d\n",
1001                        pwm_from_reg(data, data->auto_pwm[nr][point]));
1002 }
1003
1004 static ssize_t set_auto_pwm(struct device *dev,
1005                 struct device_attribute *attr, const char *buf, size_t count)
1006 {
1007         struct it87_data *data = dev_get_drvdata(dev);
1008         struct sensor_device_attribute_2 *sensor_attr =
1009                         to_sensor_dev_attr_2(attr);
1010         int nr = sensor_attr->nr;
1011         int point = sensor_attr->index;
1012         long val;
1013
1014         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1015                 return -EINVAL;
1016
1017         mutex_lock(&data->update_lock);
1018         data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1019         it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1020                          data->auto_pwm[nr][point]);
1021         mutex_unlock(&data->update_lock);
1022         return count;
1023 }
1024
1025 static ssize_t show_auto_temp(struct device *dev,
1026                 struct device_attribute *attr, char *buf)
1027 {
1028         struct it87_data *data = it87_update_device(dev);
1029         struct sensor_device_attribute_2 *sensor_attr =
1030                         to_sensor_dev_attr_2(attr);
1031         int nr = sensor_attr->nr;
1032         int point = sensor_attr->index;
1033
1034         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1035 }
1036
1037 static ssize_t set_auto_temp(struct device *dev,
1038                 struct device_attribute *attr, const char *buf, size_t count)
1039 {
1040         struct it87_data *data = dev_get_drvdata(dev);
1041         struct sensor_device_attribute_2 *sensor_attr =
1042                         to_sensor_dev_attr_2(attr);
1043         int nr = sensor_attr->nr;
1044         int point = sensor_attr->index;
1045         long val;
1046
1047         if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1048                 return -EINVAL;
1049
1050         mutex_lock(&data->update_lock);
1051         data->auto_temp[nr][point] = TEMP_TO_REG(val);
1052         it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1053                          data->auto_temp[nr][point]);
1054         mutex_unlock(&data->update_lock);
1055         return count;
1056 }
1057
1058 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1059 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGOWU, show_fan, set_fan, 0, 1);
1060 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGOWU, show_fan_div, set_fan_div, 0);
1061
1062 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1063 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGOWU, show_fan, set_fan, 1, 1);
1064 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGOWU, show_fan_div, set_fan_div, 1);
1065
1066 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1067 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGOWU, show_fan, set_fan, 2, 1);
1068 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGOWU, show_fan_div, set_fan_div, 2);
1069
1070 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1071 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGOWU, show_fan, set_fan, 3, 1);
1072
1073 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1074 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGOWU, show_fan, set_fan, 4, 1);
1075
1076 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGOWU,
1077                           show_pwm_enable, set_pwm_enable, 0);
1078 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGOWU, show_pwm, set_pwm, 0);
1079 static DEVICE_ATTR(pwm1_freq, S_IRUGOWU, show_pwm_freq, set_pwm_freq);
1080 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGOWU,
1081                           show_pwm_temp_map, set_pwm_temp_map, 0);
1082 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGOWU,
1083                             show_auto_pwm, set_auto_pwm, 0, 0);
1084 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGOWU,
1085                             show_auto_pwm, set_auto_pwm, 0, 1);
1086 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGOWU,
1087                             show_auto_pwm, set_auto_pwm, 0, 2);
1088 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1089                             show_auto_pwm, NULL, 0, 3);
1090 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGOWU,
1091                             show_auto_temp, set_auto_temp, 0, 1);
1092 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGOWU,
1093                             show_auto_temp, set_auto_temp, 0, 0);
1094 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGOWU,
1095                             show_auto_temp, set_auto_temp, 0, 2);
1096 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGOWU,
1097                             show_auto_temp, set_auto_temp, 0, 3);
1098 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGOWU,
1099                             show_auto_temp, set_auto_temp, 0, 4);
1100
1101 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGOWU,
1102                           show_pwm_enable, set_pwm_enable, 1);
1103 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGOWU, show_pwm, set_pwm, 1);
1104 static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1105 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGOWU,
1106                           show_pwm_temp_map, set_pwm_temp_map, 1);
1107 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGOWU,
1108                             show_auto_pwm, set_auto_pwm, 1, 0);
1109 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGOWU,
1110                             show_auto_pwm, set_auto_pwm, 1, 1);
1111 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGOWU,
1112                             show_auto_pwm, set_auto_pwm, 1, 2);
1113 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1114                             show_auto_pwm, NULL, 1, 3);
1115 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGOWU,
1116                             show_auto_temp, set_auto_temp, 1, 1);
1117 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGOWU,
1118                             show_auto_temp, set_auto_temp, 1, 0);
1119 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGOWU,
1120                             show_auto_temp, set_auto_temp, 1, 2);
1121 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGOWU,
1122                             show_auto_temp, set_auto_temp, 1, 3);
1123 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGOWU,
1124                             show_auto_temp, set_auto_temp, 1, 4);
1125
1126 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGOWU,
1127                           show_pwm_enable, set_pwm_enable, 2);
1128 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGOWU, show_pwm, set_pwm, 2);
1129 static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1130 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGOWU,
1131                           show_pwm_temp_map, set_pwm_temp_map, 2);
1132 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGOWU,
1133                             show_auto_pwm, set_auto_pwm, 2, 0);
1134 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGOWU,
1135                             show_auto_pwm, set_auto_pwm, 2, 1);
1136 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGOWU,
1137                             show_auto_pwm, set_auto_pwm, 2, 2);
1138 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1139                             show_auto_pwm, NULL, 2, 3);
1140 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGOWU,
1141                             show_auto_temp, set_auto_temp, 2, 1);
1142 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGOWU,
1143                             show_auto_temp, set_auto_temp, 2, 0);
1144 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGOWU,
1145                             show_auto_temp, set_auto_temp, 2, 2);
1146 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGOWU,
1147                             show_auto_temp, set_auto_temp, 2, 3);
1148 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGOWU,
1149                             show_auto_temp, set_auto_temp, 2, 4);
1150
1151 /* Alarms */
1152 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1153                 char *buf)
1154 {
1155         struct it87_data *data = it87_update_device(dev);
1156         return sprintf(buf, "%u\n", data->alarms);
1157 }
1158 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1159
1160 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1161                 char *buf)
1162 {
1163         int bitnr = to_sensor_dev_attr(attr)->index;
1164         struct it87_data *data = it87_update_device(dev);
1165         return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1166 }
1167
1168 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1169                 *attr, const char *buf, size_t count)
1170 {
1171         struct it87_data *data = dev_get_drvdata(dev);
1172         long val;
1173         int config;
1174
1175         if (kstrtol(buf, 10, &val) < 0 || val != 0)
1176                 return -EINVAL;
1177
1178         mutex_lock(&data->update_lock);
1179         config = it87_read_value(data, IT87_REG_CONFIG);
1180         if (config < 0) {
1181                 count = config;
1182         } else {
1183                 config |= 1 << 5;
1184                 it87_write_value(data, IT87_REG_CONFIG, config);
1185                 /* Invalidate cache to force re-read */
1186                 data->valid = 0;
1187         }
1188         mutex_unlock(&data->update_lock);
1189
1190         return count;
1191 }
1192
1193 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1194 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1195 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1196 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1197 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1198 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1199 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1200 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1201 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1202 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1203 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1204 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1205 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1206 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1207 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1208 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1209 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1210                           show_alarm, clear_intrusion, 4);
1211
1212 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1213                 char *buf)
1214 {
1215         int bitnr = to_sensor_dev_attr(attr)->index;
1216         struct it87_data *data = it87_update_device(dev);
1217         return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1218 }
1219 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1220                 const char *buf, size_t count)
1221 {
1222         int bitnr = to_sensor_dev_attr(attr)->index;
1223         struct it87_data *data = dev_get_drvdata(dev);
1224         long val;
1225
1226         if (kstrtol(buf, 10, &val) < 0
1227          || (val != 0 && val != 1))
1228                 return -EINVAL;
1229
1230         mutex_lock(&data->update_lock);
1231         data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1232         if (val)
1233                 data->beeps |= (1 << bitnr);
1234         else
1235                 data->beeps &= ~(1 << bitnr);
1236         it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1237         mutex_unlock(&data->update_lock);
1238         return count;
1239 }
1240
1241 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1242                           show_beep, set_beep, 1);
1243 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1244 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1245 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1246 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1247 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1248 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1249 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1250 /* fanX_beep writability is set later */
1251 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1252 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1253 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1254 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1255 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1256 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1257                           show_beep, set_beep, 2);
1258 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1259 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1260
1261 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1262                 char *buf)
1263 {
1264         struct it87_data *data = dev_get_drvdata(dev);
1265         return sprintf(buf, "%u\n", data->vrm);
1266 }
1267 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1268                 const char *buf, size_t count)
1269 {
1270         struct it87_data *data = dev_get_drvdata(dev);
1271         unsigned long val;
1272
1273         if (kstrtoul(buf, 10, &val) < 0)
1274                 return -EINVAL;
1275
1276         data->vrm = val;
1277
1278         return count;
1279 }
1280 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1281
1282 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1283                 char *buf)
1284 {
1285         struct it87_data *data = it87_update_device(dev);
1286         return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1287 }
1288 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1289
1290 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1291                 char *buf)
1292 {
1293         static const char * const labels[] = {
1294                 "+5V",
1295                 "5VSB",
1296                 "Vbat",
1297         };
1298         static const char * const labels_it8721[] = {
1299                 "+3.3V",
1300                 "3VSB",
1301                 "Vbat",
1302         };
1303         struct it87_data *data = dev_get_drvdata(dev);
1304         int nr = to_sensor_dev_attr(attr)->index;
1305
1306         return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1307                                                        : labels[nr]);
1308 }
1309 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1310 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1311 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1312
1313 static ssize_t show_name(struct device *dev, struct device_attribute
1314                          *devattr, char *buf)
1315 {
1316         struct it87_data *data = dev_get_drvdata(dev);
1317         return sprintf(buf, "%s\n", data->name);
1318 }
1319 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1320
1321 static struct attribute *it87_attributes_in[9][5] = {
1322 {
1323         &sensor_dev_attr_in0_input.dev_attr.attr,
1324         &sensor_dev_attr_in0_min.dev_attr.attr,
1325         &sensor_dev_attr_in0_max.dev_attr.attr,
1326         &sensor_dev_attr_in0_alarm.dev_attr.attr,
1327         NULL
1328 }, {
1329         &sensor_dev_attr_in1_input.dev_attr.attr,
1330         &sensor_dev_attr_in1_min.dev_attr.attr,
1331         &sensor_dev_attr_in1_max.dev_attr.attr,
1332         &sensor_dev_attr_in1_alarm.dev_attr.attr,
1333         NULL
1334 }, {
1335         &sensor_dev_attr_in2_input.dev_attr.attr,
1336         &sensor_dev_attr_in2_min.dev_attr.attr,
1337         &sensor_dev_attr_in2_max.dev_attr.attr,
1338         &sensor_dev_attr_in2_alarm.dev_attr.attr,
1339         NULL
1340 }, {
1341         &sensor_dev_attr_in3_input.dev_attr.attr,
1342         &sensor_dev_attr_in3_min.dev_attr.attr,
1343         &sensor_dev_attr_in3_max.dev_attr.attr,
1344         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1345         NULL
1346 }, {
1347         &sensor_dev_attr_in4_input.dev_attr.attr,
1348         &sensor_dev_attr_in4_min.dev_attr.attr,
1349         &sensor_dev_attr_in4_max.dev_attr.attr,
1350         &sensor_dev_attr_in4_alarm.dev_attr.attr,
1351         NULL
1352 }, {
1353         &sensor_dev_attr_in5_input.dev_attr.attr,
1354         &sensor_dev_attr_in5_min.dev_attr.attr,
1355         &sensor_dev_attr_in5_max.dev_attr.attr,
1356         &sensor_dev_attr_in5_alarm.dev_attr.attr,
1357         NULL
1358 }, {
1359         &sensor_dev_attr_in6_input.dev_attr.attr,
1360         &sensor_dev_attr_in6_min.dev_attr.attr,
1361         &sensor_dev_attr_in6_max.dev_attr.attr,
1362         &sensor_dev_attr_in6_alarm.dev_attr.attr,
1363         NULL
1364 }, {
1365         &sensor_dev_attr_in7_input.dev_attr.attr,
1366         &sensor_dev_attr_in7_min.dev_attr.attr,
1367         &sensor_dev_attr_in7_max.dev_attr.attr,
1368         &sensor_dev_attr_in7_alarm.dev_attr.attr,
1369         NULL
1370 }, {
1371         &sensor_dev_attr_in8_input.dev_attr.attr,
1372         NULL
1373 } };
1374
1375 static const struct attribute_group it87_group_in[9] = {
1376         { .attrs = it87_attributes_in[0] },
1377         { .attrs = it87_attributes_in[1] },
1378         { .attrs = it87_attributes_in[2] },
1379         { .attrs = it87_attributes_in[3] },
1380         { .attrs = it87_attributes_in[4] },
1381         { .attrs = it87_attributes_in[5] },
1382         { .attrs = it87_attributes_in[6] },
1383         { .attrs = it87_attributes_in[7] },
1384         { .attrs = it87_attributes_in[8] },
1385 };
1386
1387 static struct attribute *it87_attributes_temp[3][7] = {
1388 {
1389         &sensor_dev_attr_temp1_input.dev_attr.attr,
1390         &sensor_dev_attr_temp1_max.dev_attr.attr,
1391         &sensor_dev_attr_temp1_min.dev_attr.attr,
1392         &sensor_dev_attr_temp1_offset.dev_attr.attr,
1393         &sensor_dev_attr_temp1_type.dev_attr.attr,
1394         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1395         NULL
1396 } , {
1397         &sensor_dev_attr_temp2_input.dev_attr.attr,
1398         &sensor_dev_attr_temp2_max.dev_attr.attr,
1399         &sensor_dev_attr_temp2_min.dev_attr.attr,
1400         &sensor_dev_attr_temp2_offset.dev_attr.attr,
1401         &sensor_dev_attr_temp2_type.dev_attr.attr,
1402         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1403         NULL
1404 } , {
1405         &sensor_dev_attr_temp3_input.dev_attr.attr,
1406         &sensor_dev_attr_temp3_max.dev_attr.attr,
1407         &sensor_dev_attr_temp3_min.dev_attr.attr,
1408         &sensor_dev_attr_temp3_offset.dev_attr.attr,
1409         &sensor_dev_attr_temp3_type.dev_attr.attr,
1410         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1411         NULL
1412 } };
1413
1414 static const struct attribute_group it87_group_temp[3] = {
1415         { .attrs = it87_attributes_temp[0] },
1416         { .attrs = it87_attributes_temp[1] },
1417         { .attrs = it87_attributes_temp[2] },
1418 };
1419
1420 static struct attribute *it87_attributes[] = {
1421         &dev_attr_alarms.attr,
1422         &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1423         &dev_attr_name.attr,
1424         NULL
1425 };
1426
1427 static const struct attribute_group it87_group = {
1428         .attrs = it87_attributes,
1429 };
1430
1431 static struct attribute *it87_attributes_in_beep[] = {
1432         &sensor_dev_attr_in0_beep.dev_attr.attr,
1433         &sensor_dev_attr_in1_beep.dev_attr.attr,
1434         &sensor_dev_attr_in2_beep.dev_attr.attr,
1435         &sensor_dev_attr_in3_beep.dev_attr.attr,
1436         &sensor_dev_attr_in4_beep.dev_attr.attr,
1437         &sensor_dev_attr_in5_beep.dev_attr.attr,
1438         &sensor_dev_attr_in6_beep.dev_attr.attr,
1439         &sensor_dev_attr_in7_beep.dev_attr.attr,
1440         NULL
1441 };
1442
1443 static struct attribute *it87_attributes_temp_beep[] = {
1444         &sensor_dev_attr_temp1_beep.dev_attr.attr,
1445         &sensor_dev_attr_temp2_beep.dev_attr.attr,
1446         &sensor_dev_attr_temp3_beep.dev_attr.attr,
1447 };
1448
1449 static struct attribute *it87_attributes_fan[5][3+1] = { {
1450         &sensor_dev_attr_fan1_input.dev_attr.attr,
1451         &sensor_dev_attr_fan1_min.dev_attr.attr,
1452         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1453         NULL
1454 }, {
1455         &sensor_dev_attr_fan2_input.dev_attr.attr,
1456         &sensor_dev_attr_fan2_min.dev_attr.attr,
1457         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1458         NULL
1459 }, {
1460         &sensor_dev_attr_fan3_input.dev_attr.attr,
1461         &sensor_dev_attr_fan3_min.dev_attr.attr,
1462         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1463         NULL
1464 }, {
1465         &sensor_dev_attr_fan4_input.dev_attr.attr,
1466         &sensor_dev_attr_fan4_min.dev_attr.attr,
1467         &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1468         NULL
1469 }, {
1470         &sensor_dev_attr_fan5_input.dev_attr.attr,
1471         &sensor_dev_attr_fan5_min.dev_attr.attr,
1472         &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1473         NULL
1474 } };
1475
1476 static const struct attribute_group it87_group_fan[5] = {
1477         { .attrs = it87_attributes_fan[0] },
1478         { .attrs = it87_attributes_fan[1] },
1479         { .attrs = it87_attributes_fan[2] },
1480         { .attrs = it87_attributes_fan[3] },
1481         { .attrs = it87_attributes_fan[4] },
1482 };
1483
1484 static const struct attribute *it87_attributes_fan_div[] = {
1485         &sensor_dev_attr_fan1_div.dev_attr.attr,
1486         &sensor_dev_attr_fan2_div.dev_attr.attr,
1487         &sensor_dev_attr_fan3_div.dev_attr.attr,
1488 };
1489
1490 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1491         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1492         &sensor_dev_attr_pwm1.dev_attr.attr,
1493         &dev_attr_pwm1_freq.attr,
1494         &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1495         NULL
1496 }, {
1497         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1498         &sensor_dev_attr_pwm2.dev_attr.attr,
1499         &dev_attr_pwm2_freq.attr,
1500         &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1501         NULL
1502 }, {
1503         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1504         &sensor_dev_attr_pwm3.dev_attr.attr,
1505         &dev_attr_pwm3_freq.attr,
1506         &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1507         NULL
1508 } };
1509
1510 static const struct attribute_group it87_group_pwm[3] = {
1511         { .attrs = it87_attributes_pwm[0] },
1512         { .attrs = it87_attributes_pwm[1] },
1513         { .attrs = it87_attributes_pwm[2] },
1514 };
1515
1516 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1517         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1518         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1519         &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1520         &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1521         &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1522         &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1523         &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1524         &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1525         &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1526         NULL
1527 }, {
1528         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1529         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1530         &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1531         &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1532         &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1533         &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1534         &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1535         &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1536         &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1537         NULL
1538 }, {
1539         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1540         &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1541         &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1542         &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1543         &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1544         &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1545         &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1546         &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1547         &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1548         NULL
1549 } };
1550
1551 static const struct attribute_group it87_group_autopwm[3] = {
1552         { .attrs = it87_attributes_autopwm[0] },
1553         { .attrs = it87_attributes_autopwm[1] },
1554         { .attrs = it87_attributes_autopwm[2] },
1555 };
1556
1557 static struct attribute *it87_attributes_fan_beep[] = {
1558         &sensor_dev_attr_fan1_beep.dev_attr.attr,
1559         &sensor_dev_attr_fan2_beep.dev_attr.attr,
1560         &sensor_dev_attr_fan3_beep.dev_attr.attr,
1561         &sensor_dev_attr_fan4_beep.dev_attr.attr,
1562         &sensor_dev_attr_fan5_beep.dev_attr.attr,
1563 };
1564
1565 static struct attribute *it87_attributes_vid[] = {
1566         &dev_attr_vrm.attr,
1567         &dev_attr_cpu0_vid.attr,
1568         NULL
1569 };
1570
1571 static const struct attribute_group it87_group_vid = {
1572         .attrs = it87_attributes_vid,
1573 };
1574
1575 static struct attribute *it87_attributes_label[] = {
1576         &sensor_dev_attr_in3_label.dev_attr.attr,
1577         &sensor_dev_attr_in7_label.dev_attr.attr,
1578         &sensor_dev_attr_in8_label.dev_attr.attr,
1579         NULL
1580 };
1581
1582 static const struct attribute_group it87_group_label = {
1583         .attrs = it87_attributes_label,
1584 };
1585
1586 /* SuperIO detection - will change isa_address if a chip is found */
1587 static int __init it87_find(unsigned short *address,
1588         struct it87_sio_data *sio_data)
1589 {
1590         int err;
1591         u16 chip_type;
1592         const char *board_vendor, *board_name;
1593
1594         err = superio_enter();
1595         if (err)
1596                 return err;
1597
1598         err = -ENODEV;
1599         chip_type = force_id ? force_id : superio_inw(DEVID);
1600
1601         switch (chip_type) {
1602         case IT8705F_DEVID:
1603                 sio_data->type = it87;
1604                 break;
1605         case IT8712F_DEVID:
1606                 sio_data->type = it8712;
1607                 break;
1608         case IT8716F_DEVID:
1609         case IT8726F_DEVID:
1610                 sio_data->type = it8716;
1611                 break;
1612         case IT8718F_DEVID:
1613                 sio_data->type = it8718;
1614                 break;
1615         case IT8720F_DEVID:
1616                 sio_data->type = it8720;
1617                 break;
1618         case IT8721F_DEVID:
1619                 sio_data->type = it8721;
1620                 break;
1621         case IT8728F_DEVID:
1622                 sio_data->type = it8728;
1623                 break;
1624         case IT8782F_DEVID:
1625                 sio_data->type = it8782;
1626                 break;
1627         case IT8783E_DEVID:
1628                 sio_data->type = it8783;
1629                 break;
1630         case 0xffff:    /* No device at all */
1631                 goto exit;
1632         default:
1633                 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1634                 goto exit;
1635         }
1636
1637         superio_select(PME);
1638         if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1639                 pr_info("Device not activated, skipping\n");
1640                 goto exit;
1641         }
1642
1643         *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1644         if (*address == 0) {
1645                 pr_info("Base address not set, skipping\n");
1646                 goto exit;
1647         }
1648
1649         err = 0;
1650         sio_data->revision = superio_inb(DEVREV) & 0x0f;
1651         pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1652                 chip_type, *address, sio_data->revision);
1653
1654         /* in8 (Vbat) is always internal */
1655         sio_data->internal = (1 << 2);
1656
1657         /* Read GPIO config and VID value from LDN 7 (GPIO) */
1658         if (sio_data->type == it87) {
1659                 /* The IT8705F doesn't have VID pins at all */
1660                 sio_data->skip_vid = 1;
1661
1662                 /* The IT8705F has a different LD number for GPIO */
1663                 superio_select(5);
1664                 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1665         } else if (sio_data->type == it8783) {
1666                 int reg25, reg27, reg2A, reg2C, regEF;
1667
1668                 sio_data->skip_vid = 1; /* No VID */
1669
1670                 superio_select(GPIO);
1671
1672                 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1673                 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1674                 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1675                 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1676                 regEF = superio_inb(IT87_SIO_SPI_REG);
1677
1678                 /* Check if fan3 is there or not */
1679                 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1680                         sio_data->skip_fan |= (1 << 2);
1681                 if ((reg25 & (1 << 4))
1682                     || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1683                         sio_data->skip_pwm |= (1 << 2);
1684
1685                 /* Check if fan2 is there or not */
1686                 if (reg27 & (1 << 7))
1687                         sio_data->skip_fan |= (1 << 1);
1688                 if (reg27 & (1 << 3))
1689                         sio_data->skip_pwm |= (1 << 1);
1690
1691                 /* VIN5 */
1692                 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1693                         sio_data->skip_in |= (1 << 5); /* No VIN5 */
1694
1695                 /* VIN6 */
1696                 if (reg27 & (1 << 1))
1697                         sio_data->skip_in |= (1 << 6); /* No VIN6 */
1698
1699                 /*
1700                  * VIN7
1701                  * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1702                  */
1703                 if (reg27 & (1 << 2)) {
1704                         /*
1705                          * The data sheet is a bit unclear regarding the
1706                          * internal voltage divider for VCCH5V. It says
1707                          * "This bit enables and switches VIN7 (pin 91) to the
1708                          * internal voltage divider for VCCH5V".
1709                          * This is different to other chips, where the internal
1710                          * voltage divider would connect VIN7 to an internal
1711                          * voltage source. Maybe that is the case here as well.
1712                          *
1713                          * Since we don't know for sure, re-route it if that is
1714                          * not the case, and ask the user to report if the
1715                          * resulting voltage is sane.
1716                          */
1717                         if (!(reg2C & (1 << 1))) {
1718                                 reg2C |= (1 << 1);
1719                                 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1720                                 pr_notice("Routing internal VCCH5V to in7.\n");
1721                         }
1722                         pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1723                         pr_notice("Please report if it displays a reasonable voltage.\n");
1724                 }
1725
1726                 if (reg2C & (1 << 0))
1727                         sio_data->internal |= (1 << 0);
1728                 if (reg2C & (1 << 1))
1729                         sio_data->internal |= (1 << 1);
1730
1731                 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1732
1733         } else {
1734                 int reg;
1735                 bool uart6;
1736
1737                 superio_select(GPIO);
1738
1739                 reg = superio_inb(IT87_SIO_GPIO3_REG);
1740                 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1741                     sio_data->type == it8782) {
1742                         /*
1743                          * IT8721F/IT8758E, and IT8782F don't have VID pins
1744                          * at all, not sure about the IT8728F.
1745                          */
1746                         sio_data->skip_vid = 1;
1747                 } else {
1748                         /* We need at least 4 VID pins */
1749                         if (reg & 0x0f) {
1750                                 pr_info("VID is disabled (pins used for GPIO)\n");
1751                                 sio_data->skip_vid = 1;
1752                         }
1753                 }
1754
1755                 /* Check if fan3 is there or not */
1756                 if (reg & (1 << 6))
1757                         sio_data->skip_pwm |= (1 << 2);
1758                 if (reg & (1 << 7))
1759                         sio_data->skip_fan |= (1 << 2);
1760
1761                 /* Check if fan2 is there or not */
1762                 reg = superio_inb(IT87_SIO_GPIO5_REG);
1763                 if (reg & (1 << 1))
1764                         sio_data->skip_pwm |= (1 << 1);
1765                 if (reg & (1 << 2))
1766                         sio_data->skip_fan |= (1 << 1);
1767
1768                 if ((sio_data->type == it8718 || sio_data->type == it8720)
1769                  && !(sio_data->skip_vid))
1770                         sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1771
1772                 reg = superio_inb(IT87_SIO_PINX2_REG);
1773
1774                 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1775
1776                 /*
1777                  * The IT8720F has no VIN7 pin, so VCCH should always be
1778                  * routed internally to VIN7 with an internal divider.
1779                  * Curiously, there still is a configuration bit to control
1780                  * this, which means it can be set incorrectly. And even
1781                  * more curiously, many boards out there are improperly
1782                  * configured, even though the IT8720F datasheet claims
1783                  * that the internal routing of VCCH to VIN7 is the default
1784                  * setting. So we force the internal routing in this case.
1785                  *
1786                  * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1787                  * If UART6 is enabled, re-route VIN7 to the internal divider
1788                  * if that is not already the case.
1789                  */
1790                 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
1791                         reg |= (1 << 1);
1792                         superio_outb(IT87_SIO_PINX2_REG, reg);
1793                         pr_notice("Routing internal VCCH to in7\n");
1794                 }
1795                 if (reg & (1 << 0))
1796                         sio_data->internal |= (1 << 0);
1797                 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1798                     sio_data->type == it8728)
1799                         sio_data->internal |= (1 << 1);
1800
1801                 /*
1802                  * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1803                  * While VIN7 can be routed to the internal voltage divider,
1804                  * VIN5 and VIN6 are not available if UART6 is enabled.
1805                  *
1806                  * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1807                  * is the temperature source. Since we can not read the
1808                  * temperature source here, skip_temp is preliminary.
1809                  */
1810                 if (uart6) {
1811                         sio_data->skip_in |= (1 << 5) | (1 << 6);
1812                         sio_data->skip_temp |= (1 << 2);
1813                 }
1814
1815                 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1816         }
1817         if (sio_data->beep_pin)
1818                 pr_info("Beeping is supported\n");
1819
1820         /* Disable specific features based on DMI strings */
1821         board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1822         board_name = dmi_get_system_info(DMI_BOARD_NAME);
1823         if (board_vendor && board_name) {
1824                 if (strcmp(board_vendor, "nVIDIA") == 0
1825                  && strcmp(board_name, "FN68PT") == 0) {
1826                         /*
1827                          * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1828                          * connected to a fan, but to something else. One user
1829                          * has reported instant system power-off when changing
1830                          * the PWM2 duty cycle, so we disable it.
1831                          * I use the board name string as the trigger in case
1832                          * the same board is ever used in other systems.
1833                          */
1834                         pr_info("Disabling pwm2 due to hardware constraints\n");
1835                         sio_data->skip_pwm = (1 << 1);
1836                 }
1837         }
1838
1839 exit:
1840         superio_exit();
1841         return err;
1842 }
1843
1844 static void it87_remove_files(struct device *dev)
1845 {
1846         struct it87_data *data = platform_get_drvdata(pdev);
1847         struct it87_sio_data *sio_data = dev->platform_data;
1848         int i;
1849
1850         sysfs_remove_group(&dev->kobj, &it87_group);
1851         for (i = 0; i < 9; i++) {
1852                 if (sio_data->skip_in & (1 << i))
1853                         continue;
1854                 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1855                 if (it87_attributes_in_beep[i])
1856                         sysfs_remove_file(&dev->kobj,
1857                                           it87_attributes_in_beep[i]);
1858         }
1859         for (i = 0; i < 3; i++) {
1860                 if (!(data->has_temp & (1 << i)))
1861                         continue;
1862                 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1863                 if (sio_data->beep_pin)
1864                         sysfs_remove_file(&dev->kobj,
1865                                           it87_attributes_temp_beep[i]);
1866         }
1867         for (i = 0; i < 5; i++) {
1868                 if (!(data->has_fan & (1 << i)))
1869                         continue;
1870                 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
1871                 if (sio_data->beep_pin)
1872                         sysfs_remove_file(&dev->kobj,
1873                                           it87_attributes_fan_beep[i]);
1874                 if (i < 3 && !has_16bit_fans(data))
1875                         sysfs_remove_file(&dev->kobj,
1876                                           it87_attributes_fan_div[i]);
1877         }
1878         for (i = 0; i < 3; i++) {
1879                 if (sio_data->skip_pwm & (1 << 0))
1880                         continue;
1881                 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1882                 if (has_old_autopwm(data))
1883                         sysfs_remove_group(&dev->kobj,
1884                                            &it87_group_autopwm[i]);
1885         }
1886         if (!sio_data->skip_vid)
1887                 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1888         sysfs_remove_group(&dev->kobj, &it87_group_label);
1889 }
1890
1891 static int __devinit it87_probe(struct platform_device *pdev)
1892 {
1893         struct it87_data *data;
1894         struct resource *res;
1895         struct device *dev = &pdev->dev;
1896         struct it87_sio_data *sio_data = dev->platform_data;
1897         int err = 0, i;
1898         int enable_pwm_interface;
1899         int fan_beep_need_rw;
1900
1901         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1902         if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1903                                  DRVNAME)) {
1904                 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1905                         (unsigned long)res->start,
1906                         (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1907                 return -EBUSY;
1908         }
1909
1910         data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
1911         if (!data)
1912                 return -ENOMEM;
1913
1914         data->addr = res->start;
1915         data->type = sio_data->type;
1916         data->features = it87_devices[data->type].features;
1917         data->name = it87_devices[sio_data->type].name;
1918         /*
1919          * IT8705F Datasheet 0.4.1, 3h == Version G.
1920          * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
1921          * These are the first revisions with 16-bit tachometer support.
1922          */
1923         switch (data->type) {
1924         case it87:
1925                 if (sio_data->revision >= 0x03) {
1926                         data->features &= ~FEAT_OLD_AUTOPWM;
1927                         data->features |= FEAT_16BIT_FAN;
1928                 }
1929                 break;
1930         case it8712:
1931                 if (sio_data->revision >= 0x08) {
1932                         data->features &= ~FEAT_OLD_AUTOPWM;
1933                         data->features |= FEAT_16BIT_FAN;
1934                 }
1935                 break;
1936         default:
1937                 break;
1938         }
1939
1940         /* Now, we do the remaining detection. */
1941         if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1942          || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
1943                 return -ENODEV;
1944
1945         platform_set_drvdata(pdev, data);
1946
1947         mutex_init(&data->update_lock);
1948
1949         /* Check PWM configuration */
1950         enable_pwm_interface = it87_check_pwm(dev);
1951
1952         /* Starting with IT8721F, we handle scaling of internal voltages */
1953         if (has_12mv_adc(data)) {
1954                 if (sio_data->internal & (1 << 0))
1955                         data->in_scaled |= (1 << 3);    /* in3 is AVCC */
1956                 if (sio_data->internal & (1 << 1))
1957                         data->in_scaled |= (1 << 7);    /* in7 is VSB */
1958                 if (sio_data->internal & (1 << 2))
1959                         data->in_scaled |= (1 << 8);    /* in8 is Vbat */
1960         } else if (sio_data->type == it8782 || sio_data->type == it8783) {
1961                 if (sio_data->internal & (1 << 0))
1962                         data->in_scaled |= (1 << 3);    /* in3 is VCC5V */
1963                 if (sio_data->internal & (1 << 1))
1964                         data->in_scaled |= (1 << 7);    /* in7 is VCCH5V */
1965         }
1966
1967         data->has_temp = 0x07;
1968         if (sio_data->skip_temp & (1 << 2)) {
1969                 if (sio_data->type == it8782
1970                     && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
1971                         data->has_temp &= ~(1 << 2);
1972         }
1973
1974         /* Initialize the IT87 chip */
1975         it87_init_device(pdev);
1976
1977         /* Register sysfs hooks */
1978         err = sysfs_create_group(&dev->kobj, &it87_group);
1979         if (err)
1980                 return err;
1981
1982         for (i = 0; i < 9; i++) {
1983                 if (sio_data->skip_in & (1 << i))
1984                         continue;
1985                 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
1986                 if (err)
1987                         goto error;
1988                 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
1989                         err = sysfs_create_file(&dev->kobj,
1990                                                 it87_attributes_in_beep[i]);
1991                         if (err)
1992                                 goto error;
1993                 }
1994         }
1995
1996         for (i = 0; i < 3; i++) {
1997                 if (!(data->has_temp & (1 << i)))
1998                         continue;
1999                 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2000                 if (err)
2001                         goto error;
2002                 if (sio_data->beep_pin) {
2003                         err = sysfs_create_file(&dev->kobj,
2004                                                 it87_attributes_temp_beep[i]);
2005                         if (err)
2006                                 goto error;
2007                 }
2008         }
2009
2010         /* Do not create fan files for disabled fans */
2011         fan_beep_need_rw = 1;
2012         for (i = 0; i < 5; i++) {
2013                 if (!(data->has_fan & (1 << i)))
2014                         continue;
2015                 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2016                 if (err)
2017                         goto error;
2018
2019                 if (i < 3 && !has_16bit_fans(data)) {
2020                         err = sysfs_create_file(&dev->kobj,
2021                                                 it87_attributes_fan_div[i]);
2022                         if (err)
2023                                 goto error;
2024                 }
2025
2026                 if (sio_data->beep_pin) {
2027                         err = sysfs_create_file(&dev->kobj,
2028                                                 it87_attributes_fan_beep[i]);
2029                         if (err)
2030                                 goto error;
2031                         if (!fan_beep_need_rw)
2032                                 continue;
2033
2034                         /*
2035                          * As we have a single beep enable bit for all fans,
2036                          * only the first enabled fan has a writable attribute
2037                          * for it.
2038                          */
2039                         if (sysfs_chmod_file(&dev->kobj,
2040                                              it87_attributes_fan_beep[i],
2041                                              S_IRUGO | S_IWUSR))
2042                                 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2043                                         i + 1);
2044                         fan_beep_need_rw = 0;
2045                 }
2046         }
2047
2048         if (enable_pwm_interface) {
2049                 for (i = 0; i < 3; i++) {
2050                         if (sio_data->skip_pwm & (1 << i))
2051                                 continue;
2052                         err = sysfs_create_group(&dev->kobj,
2053                                                  &it87_group_pwm[i]);
2054                         if (err)
2055                                 goto error;
2056
2057                         if (!has_old_autopwm(data))
2058                                 continue;
2059                         err = sysfs_create_group(&dev->kobj,
2060                                                  &it87_group_autopwm[i]);
2061                         if (err)
2062                                 goto error;
2063                 }
2064         }
2065
2066         if (!sio_data->skip_vid) {
2067                 data->vrm = vid_which_vrm();
2068                 /* VID reading from Super-I/O config space if available */
2069                 data->vid = sio_data->vid_value;
2070                 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2071                 if (err)
2072                         goto error;
2073         }
2074
2075         /* Export labels for internal sensors */
2076         for (i = 0; i < 3; i++) {
2077                 if (!(sio_data->internal & (1 << i)))
2078                         continue;
2079                 err = sysfs_create_file(&dev->kobj,
2080                                         it87_attributes_label[i]);
2081                 if (err)
2082                         goto error;
2083         }
2084
2085         data->hwmon_dev = hwmon_device_register(dev);
2086         if (IS_ERR(data->hwmon_dev)) {
2087                 err = PTR_ERR(data->hwmon_dev);
2088                 goto error;
2089         }
2090
2091         return 0;
2092
2093 error:
2094         it87_remove_files(dev);
2095         return err;
2096 }
2097
2098 static int __devexit it87_remove(struct platform_device *pdev)
2099 {
2100         struct it87_data *data = platform_get_drvdata(pdev);
2101
2102         hwmon_device_unregister(data->hwmon_dev);
2103         it87_remove_files(&pdev->dev);
2104
2105         return 0;
2106 }
2107
2108 /*
2109  * Must be called with data->update_lock held, except during initialization.
2110  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2111  * would slow down the IT87 access and should not be necessary.
2112  */
2113 static int it87_read_value(struct it87_data *data, u8 reg)
2114 {
2115         outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2116         return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2117 }
2118
2119 /*
2120  * Must be called with data->update_lock held, except during initialization.
2121  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2122  * would slow down the IT87 access and should not be necessary.
2123  */
2124 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2125 {
2126         outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2127         outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2128 }
2129
2130 /* Return 1 if and only if the PWM interface is safe to use */
2131 static int __devinit it87_check_pwm(struct device *dev)
2132 {
2133         struct it87_data *data = dev_get_drvdata(dev);
2134         /*
2135          * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2136          * and polarity set to active low is sign that this is the case so we
2137          * disable pwm control to protect the user.
2138          */
2139         int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2140         if ((tmp & 0x87) == 0) {
2141                 if (fix_pwm_polarity) {
2142                         /*
2143                          * The user asks us to attempt a chip reconfiguration.
2144                          * This means switching to active high polarity and
2145                          * inverting all fan speed values.
2146                          */
2147                         int i;
2148                         u8 pwm[3];
2149
2150                         for (i = 0; i < 3; i++)
2151                                 pwm[i] = it87_read_value(data,
2152                                                          IT87_REG_PWM(i));
2153
2154                         /*
2155                          * If any fan is in automatic pwm mode, the polarity
2156                          * might be correct, as suspicious as it seems, so we
2157                          * better don't change anything (but still disable the
2158                          * PWM interface).
2159                          */
2160                         if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2161                                 dev_info(dev,
2162                                          "Reconfiguring PWM to active high polarity\n");
2163                                 it87_write_value(data, IT87_REG_FAN_CTL,
2164                                                  tmp | 0x87);
2165                                 for (i = 0; i < 3; i++)
2166                                         it87_write_value(data,
2167                                                          IT87_REG_PWM(i),
2168                                                          0x7f & ~pwm[i]);
2169                                 return 1;
2170                         }
2171
2172                         dev_info(dev,
2173                                  "PWM configuration is too broken to be fixed\n");
2174                 }
2175
2176                 dev_info(dev,
2177                          "Detected broken BIOS defaults, disabling PWM interface\n");
2178                 return 0;
2179         } else if (fix_pwm_polarity) {
2180                 dev_info(dev,
2181                          "PWM configuration looks sane, won't touch\n");
2182         }
2183
2184         return 1;
2185 }
2186
2187 /* Called when we have found a new IT87. */
2188 static void __devinit it87_init_device(struct platform_device *pdev)
2189 {
2190         struct it87_sio_data *sio_data = pdev->dev.platform_data;
2191         struct it87_data *data = platform_get_drvdata(pdev);
2192         int tmp, i;
2193         u8 mask;
2194
2195         /*
2196          * For each PWM channel:
2197          * - If it is in automatic mode, setting to manual mode should set
2198          *   the fan to full speed by default.
2199          * - If it is in manual mode, we need a mapping to temperature
2200          *   channels to use when later setting to automatic mode later.
2201          *   Use a 1:1 mapping by default (we are clueless.)
2202          * In both cases, the value can (and should) be changed by the user
2203          * prior to switching to a different mode.
2204          * Note that this is no longer needed for the IT8721F and later, as
2205          * these have separate registers for the temperature mapping and the
2206          * manual duty cycle.
2207          */
2208         for (i = 0; i < 3; i++) {
2209                 data->pwm_temp_map[i] = i;
2210                 data->pwm_duty[i] = 0x7f;       /* Full speed */
2211                 data->auto_pwm[i][3] = 0x7f;    /* Full speed, hard-coded */
2212         }
2213
2214         /*
2215          * Some chips seem to have default value 0xff for all limit
2216          * registers. For low voltage limits it makes no sense and triggers
2217          * alarms, so change to 0 instead. For high temperature limits, it
2218          * means -1 degree C, which surprisingly doesn't trigger an alarm,
2219          * but is still confusing, so change to 127 degrees C.
2220          */
2221         for (i = 0; i < 8; i++) {
2222                 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2223                 if (tmp == 0xff)
2224                         it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2225         }
2226         for (i = 0; i < 3; i++) {
2227                 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2228                 if (tmp == 0xff)
2229                         it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2230         }
2231
2232         /*
2233          * Temperature channels are not forcibly enabled, as they can be
2234          * set to two different sensor types and we can't guess which one
2235          * is correct for a given system. These channels can be enabled at
2236          * run-time through the temp{1-3}_type sysfs accessors if needed.
2237          */
2238
2239         /* Check if voltage monitors are reset manually or by some reason */
2240         tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2241         if ((tmp & 0xff) == 0) {
2242                 /* Enable all voltage monitors */
2243                 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2244         }
2245
2246         /* Check if tachometers are reset manually or by some reason */
2247         mask = 0x70 & ~(sio_data->skip_fan << 4);
2248         data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2249         if ((data->fan_main_ctrl & mask) == 0) {
2250                 /* Enable all fan tachometers */
2251                 data->fan_main_ctrl |= mask;
2252                 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2253                                  data->fan_main_ctrl);
2254         }
2255         data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2256
2257         /* Set tachometers to 16-bit mode if needed */
2258         if (has_16bit_fans(data)) {
2259                 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2260                 if (~tmp & 0x07 & data->has_fan) {
2261                         dev_dbg(&pdev->dev,
2262                                 "Setting fan1-3 to 16-bit mode\n");
2263                         it87_write_value(data, IT87_REG_FAN_16BIT,
2264                                          tmp | 0x07);
2265                 }
2266                 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2267                 if (data->type != it87 && data->type != it8782 &&
2268                     data->type != it8783) {
2269                         if (tmp & (1 << 4))
2270                                 data->has_fan |= (1 << 3); /* fan4 enabled */
2271                         if (tmp & (1 << 5))
2272                                 data->has_fan |= (1 << 4); /* fan5 enabled */
2273                 }
2274         }
2275
2276         /* Fan input pins may be used for alternative functions */
2277         data->has_fan &= ~sio_data->skip_fan;
2278
2279         /* Start monitoring */
2280         it87_write_value(data, IT87_REG_CONFIG,
2281                          (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2282                          | (update_vbat ? 0x41 : 0x01));
2283 }
2284
2285 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2286 {
2287         data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2288         if (has_newer_autopwm(data)) {
2289                 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2290                 data->pwm_duty[nr] = it87_read_value(data,
2291                                                      IT87_REG_PWM_DUTY(nr));
2292         } else {
2293                 if (data->pwm_ctrl[nr] & 0x80)  /* Automatic mode */
2294                         data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2295                 else                            /* Manual mode */
2296                         data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2297         }
2298
2299         if (has_old_autopwm(data)) {
2300                 int i;
2301
2302                 for (i = 0; i < 5 ; i++)
2303                         data->auto_temp[nr][i] = it87_read_value(data,
2304                                                 IT87_REG_AUTO_TEMP(nr, i));
2305                 for (i = 0; i < 3 ; i++)
2306                         data->auto_pwm[nr][i] = it87_read_value(data,
2307                                                 IT87_REG_AUTO_PWM(nr, i));
2308         }
2309 }
2310
2311 static struct it87_data *it87_update_device(struct device *dev)
2312 {
2313         struct it87_data *data = dev_get_drvdata(dev);
2314         int i;
2315
2316         mutex_lock(&data->update_lock);
2317
2318         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2319             || !data->valid) {
2320                 if (update_vbat) {
2321                         /*
2322                          * Cleared after each update, so reenable.  Value
2323                          * returned by this read will be previous value
2324                          */
2325                         it87_write_value(data, IT87_REG_CONFIG,
2326                                 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2327                 }
2328                 for (i = 0; i <= 7; i++) {
2329                         data->in[i][0] =
2330                                 it87_read_value(data, IT87_REG_VIN(i));
2331                         data->in[i][1] =
2332                                 it87_read_value(data, IT87_REG_VIN_MIN(i));
2333                         data->in[i][2] =
2334                                 it87_read_value(data, IT87_REG_VIN_MAX(i));
2335                 }
2336                 /* in8 (battery) has no limit registers */
2337                 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2338
2339                 for (i = 0; i < 5; i++) {
2340                         /* Skip disabled fans */
2341                         if (!(data->has_fan & (1 << i)))
2342                                 continue;
2343
2344                         data->fan[i][1] =
2345                                 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2346                         data->fan[i][0] = it87_read_value(data,
2347                                        IT87_REG_FAN[i]);
2348                         /* Add high byte if in 16-bit mode */
2349                         if (has_16bit_fans(data)) {
2350                                 data->fan[i][0] |= it87_read_value(data,
2351                                                 IT87_REG_FANX[i]) << 8;
2352                                 data->fan[i][1] |= it87_read_value(data,
2353                                                 IT87_REG_FANX_MIN[i]) << 8;
2354                         }
2355                 }
2356                 for (i = 0; i < 3; i++) {
2357                         if (!(data->has_temp & (1 << i)))
2358                                 continue;
2359                         data->temp[i][0] =
2360                                 it87_read_value(data, IT87_REG_TEMP(i));
2361                         data->temp[i][1] =
2362                                 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2363                         data->temp[i][2] =
2364                                 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2365                         data->temp[i][3] =
2366                                 it87_read_value(data, IT87_REG_TEMP_OFFSET[i]);
2367                 }
2368
2369                 /* Newer chips don't have clock dividers */
2370                 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2371                         i = it87_read_value(data, IT87_REG_FAN_DIV);
2372                         data->fan_div[0] = i & 0x07;
2373                         data->fan_div[1] = (i >> 3) & 0x07;
2374                         data->fan_div[2] = (i & 0x40) ? 3 : 1;
2375                 }
2376
2377                 data->alarms =
2378                         it87_read_value(data, IT87_REG_ALARM1) |
2379                         (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2380                         (it87_read_value(data, IT87_REG_ALARM3) << 16);
2381                 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2382
2383                 data->fan_main_ctrl = it87_read_value(data,
2384                                 IT87_REG_FAN_MAIN_CTRL);
2385                 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2386                 for (i = 0; i < 3; i++)
2387                         it87_update_pwm_ctrl(data, i);
2388
2389                 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2390                 /*
2391                  * The IT8705F does not have VID capability.
2392                  * The IT8718F and later don't use IT87_REG_VID for the
2393                  * same purpose.
2394                  */
2395                 if (data->type == it8712 || data->type == it8716) {
2396                         data->vid = it87_read_value(data, IT87_REG_VID);
2397                         /*
2398                          * The older IT8712F revisions had only 5 VID pins,
2399                          * but we assume it is always safe to read 6 bits.
2400                          */
2401                         data->vid &= 0x3f;
2402                 }
2403                 data->last_updated = jiffies;
2404                 data->valid = 1;
2405         }
2406
2407         mutex_unlock(&data->update_lock);
2408
2409         return data;
2410 }
2411
2412 static int __init it87_device_add(unsigned short address,
2413                                   const struct it87_sio_data *sio_data)
2414 {
2415         struct resource res = {
2416                 .start  = address + IT87_EC_OFFSET,
2417                 .end    = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2418                 .name   = DRVNAME,
2419                 .flags  = IORESOURCE_IO,
2420         };
2421         int err;
2422
2423         err = acpi_check_resource_conflict(&res);
2424         if (err)
2425                 goto exit;
2426
2427         pdev = platform_device_alloc(DRVNAME, address);
2428         if (!pdev) {
2429                 err = -ENOMEM;
2430                 pr_err("Device allocation failed\n");
2431                 goto exit;
2432         }
2433
2434         err = platform_device_add_resources(pdev, &res, 1);
2435         if (err) {
2436                 pr_err("Device resource addition failed (%d)\n", err);
2437                 goto exit_device_put;
2438         }
2439
2440         err = platform_device_add_data(pdev, sio_data,
2441                                        sizeof(struct it87_sio_data));
2442         if (err) {
2443                 pr_err("Platform data allocation failed\n");
2444                 goto exit_device_put;
2445         }
2446
2447         err = platform_device_add(pdev);
2448         if (err) {
2449                 pr_err("Device addition failed (%d)\n", err);
2450                 goto exit_device_put;
2451         }
2452
2453         return 0;
2454
2455 exit_device_put:
2456         platform_device_put(pdev);
2457 exit:
2458         return err;
2459 }
2460
2461 static int __init sm_it87_init(void)
2462 {
2463         int err;
2464         unsigned short isa_address = 0;
2465         struct it87_sio_data sio_data;
2466
2467         memset(&sio_data, 0, sizeof(struct it87_sio_data));
2468         err = it87_find(&isa_address, &sio_data);
2469         if (err)
2470                 return err;
2471         err = platform_driver_register(&it87_driver);
2472         if (err)
2473                 return err;
2474
2475         err = it87_device_add(isa_address, &sio_data);
2476         if (err) {
2477                 platform_driver_unregister(&it87_driver);
2478                 return err;
2479         }
2480
2481         return 0;
2482 }
2483
2484 static void __exit sm_it87_exit(void)
2485 {
2486         platform_device_unregister(pdev);
2487         platform_driver_unregister(&it87_driver);
2488 }
2489
2490
2491 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <khali@linux-fr.org>");
2492 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2493 module_param(update_vbat, bool, 0);
2494 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2495 module_param(fix_pwm_polarity, bool, 0);
2496 MODULE_PARM_DESC(fix_pwm_polarity,
2497                  "Force PWM polarity to active high (DANGEROUS)");
2498 MODULE_LICENSE("GPL");
2499
2500 module_init(sm_it87_init);
2501 module_exit(sm_it87_exit);