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