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