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