]> git.sur5r.net Git - groeck-it87/blob - it87.c
Support for newer autopwm, plus other changes and cleanup
[groeck-it87] / it87.c
1 /*
2  *  it87.c - Part of lm_sensors, Linux kernel modules for hardware
3  *           monitoring.
4  *
5  *  The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6  *  parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7  *  addition to an Environment Controller (Enhanced Hardware Monitor and
8  *  Fan Controller)
9  *
10  *  This driver supports only the Environment Controller in the IT8705F and
11  *  similar parts.  The other devices are supported by different drivers.
12  *
13  *  Supports: IT8603E  Super I/O chip w/LPC interface
14  *            IT8620E  Super I/O chip w/LPC interface
15  *            IT8623E  Super I/O chip w/LPC interface
16  *            IT8705F  Super I/O chip w/LPC interface
17  *            IT8712F  Super I/O chip w/LPC interface
18  *            IT8716F  Super I/O chip w/LPC interface
19  *            IT8718F  Super I/O chip w/LPC interface
20  *            IT8720F  Super I/O chip w/LPC interface
21  *            IT8721F  Super I/O chip w/LPC interface
22  *            IT8726F  Super I/O chip w/LPC interface
23  *            IT8728F  Super I/O chip w/LPC interface
24  *            IT8758E  Super I/O chip w/LPC interface
25  *            IT8771E  Super I/O chip w/LPC interface
26  *            IT8772E  Super I/O chip w/LPC interface
27  *            IT8781F  Super I/O chip w/LPC interface
28  *            IT8782F  Super I/O chip w/LPC interface
29  *            IT8783E/F Super I/O chip w/LPC interface
30  *            IT8786E  Super I/O chip w/LPC interface
31  *            IT8790E  Super I/O chip w/LPC interface
32  *            Sis950   A clone of the IT8705F
33  *
34  *  Copyright (C) 2001 Chris Gauthron
35  *  Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
36  *
37  *  This program is free software; you can redistribute it and/or modify
38  *  it under the terms of the GNU General Public License as published by
39  *  the Free Software Foundation; either version 2 of the License, or
40  *  (at your option) any later version.
41  *
42  *  This program is distributed in the hope that it will be useful,
43  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
44  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45  *  GNU General Public License for more details.
46  */
47
48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
49
50 #include <linux/bitops.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/jiffies.h>
55 #include <linux/platform_device.h>
56 #include <linux/hwmon.h>
57 #include <linux/hwmon-sysfs.h>
58 #include <linux/hwmon-vid.h>
59 #include <linux/err.h>
60 #include <linux/mutex.h>
61 #include <linux/sysfs.h>
62 #include <linux/string.h>
63 #include <linux/dmi.h>
64 #include <linux/acpi.h>
65 #include <linux/io.h>
66 #include "compat.h"
67
68 #define DRVNAME "it87"
69
70 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
71              it8772, it8781, it8782, it8783, it8786, it8790, it8603, it8620 };
72
73 static unsigned short force_id;
74 module_param(force_id, ushort, 0);
75 MODULE_PARM_DESC(force_id, "Override the detected device ID");
76
77 static struct platform_device *it87_pdev[2];
78
79 #define REG_2E  0x2e    /* The register to read/write */
80 #define REG_4E  0x4e    /* Secondary register to read/write */
81
82 #define DEV     0x07    /* Register: Logical device select */
83 #define PME     0x04    /* The device with the fan registers in it */
84
85 /* The device with the IT8718F/IT8720F VID value in it */
86 #define GPIO    0x07
87
88 #define DEVID   0x20    /* Register: Device ID */
89 #define DEVREV  0x22    /* Register: Device Revision */
90
91 static inline int superio_inb(int ioreg, int reg)
92 {
93         outb(reg, ioreg);
94         return inb(ioreg + 1);
95 }
96
97 static inline void superio_outb(int ioreg, int reg, int val)
98 {
99         outb(reg, ioreg);
100         outb(val, ioreg + 1);
101 }
102
103 static int superio_inw(int ioreg, int reg)
104 {
105         int val;
106
107         outb(reg++, ioreg);
108         val = inb(ioreg + 1) << 8;
109         outb(reg, ioreg);
110         val |= inb(ioreg + 1);
111         return val;
112 }
113
114 static inline void superio_select(int ioreg, int ldn)
115 {
116         outb(DEV, ioreg);
117         outb(ldn, ioreg + 1);
118 }
119
120 static inline int superio_enter(int ioreg)
121 {
122         /*
123          * Try to reserve ioreg and ioreg + 1 for exclusive access.
124          */
125         if (!request_muxed_region(ioreg, 2, DRVNAME))
126                 return -EBUSY;
127
128         outb(0x87, ioreg);
129         outb(0x01, ioreg);
130         outb(0x55, ioreg);
131         outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
132         return 0;
133 }
134
135 static inline void superio_exit(int ioreg)
136 {
137         outb(0x02, ioreg);
138         outb(0x02, ioreg + 1);
139         release_region(ioreg, 2);
140 }
141
142 /* Logical device 4 registers */
143 #define IT8712F_DEVID 0x8712
144 #define IT8705F_DEVID 0x8705
145 #define IT8716F_DEVID 0x8716
146 #define IT8718F_DEVID 0x8718
147 #define IT8720F_DEVID 0x8720
148 #define IT8721F_DEVID 0x8721
149 #define IT8726F_DEVID 0x8726
150 #define IT8728F_DEVID 0x8728
151 #define IT8771E_DEVID 0x8771
152 #define IT8772E_DEVID 0x8772
153 #define IT8781F_DEVID 0x8781
154 #define IT8782F_DEVID 0x8782
155 #define IT8783E_DEVID 0x8783
156 #define IT8786E_DEVID 0x8786
157 #define IT8790E_DEVID 0x8790
158 #define IT8603E_DEVID 0x8603
159 #define IT8620E_DEVID 0x8620
160 #define IT8623E_DEVID 0x8623
161 #define IT87_ACT_REG  0x30
162 #define IT87_BASE_REG 0x60
163
164 /* Logical device 7 registers (IT8712F and later) */
165 #define IT87_SIO_GPIO1_REG      0x25
166 #define IT87_SIO_GPIO2_REG      0x26
167 #define IT87_SIO_GPIO3_REG      0x27
168 #define IT87_SIO_GPIO4_REG      0x28
169 #define IT87_SIO_GPIO5_REG      0x29
170 #define IT87_SIO_PINX1_REG      0x2a    /* Pin selection */
171 #define IT87_SIO_PINX2_REG      0x2c    /* Pin selection */
172 #define IT87_SIO_SPI_REG        0xef    /* SPI function pin select */
173 #define IT87_SIO_VID_REG        0xfc    /* VID value */
174 #define IT87_SIO_BEEP_PIN_REG   0xf6    /* Beep pin mapping */
175
176 /* Update battery voltage after every reading if true */
177 static bool update_vbat;
178
179 /* Not all BIOSes properly configure the PWM registers */
180 static bool fix_pwm_polarity;
181
182 /* Many IT87 constants specified below */
183
184 /* Length of ISA address segment */
185 #define IT87_EXTENT 8
186
187 /* Length of ISA address segment for Environmental Controller */
188 #define IT87_EC_EXTENT 2
189
190 /* Offset of EC registers from ISA base address */
191 #define IT87_EC_OFFSET 5
192
193 /* Where are the ISA address/data registers relative to the EC base address */
194 #define IT87_ADDR_REG_OFFSET 0
195 #define IT87_DATA_REG_OFFSET 1
196
197 /*----- The IT87 registers -----*/
198
199 #define IT87_REG_CONFIG        0x00
200
201 #define IT87_REG_ALARM1        0x01
202 #define IT87_REG_ALARM2        0x02
203 #define IT87_REG_ALARM3        0x03
204
205 /*
206  * The IT8718F and IT8720F have the VID value in a different register, in
207  * Super-I/O configuration space.
208  */
209 #define IT87_REG_VID           0x0a
210 /*
211  * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
212  * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
213  * mode.
214  */
215 #define IT87_REG_FAN_DIV       0x0b
216 #define IT87_REG_FAN_16BIT     0x0c
217
218 /*
219  * Monitors:
220  * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
221  * - up to 6 temp (1 to 6)
222  * - up to 6 fan (1 to 6)
223  */
224
225 static const u8 IT87_REG_FAN[]         = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
226 static const u8 IT87_REG_FAN_MIN[]     = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
227 static const u8 IT87_REG_FANX[]        = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
228 static const u8 IT87_REG_FANX_MIN[]    = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
229 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
230
231 #define IT87_REG_FAN_MAIN_CTRL 0x13
232 #define IT87_REG_FAN_CTL       0x14
233 static const u8 IT87_REG_PWM[]         = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
234 static const u8 IT87_REG_PWM_DUTY[]    = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
235
236 static const u8 IT87_REG_VIN[]  = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
237                                     0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
238
239 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
240
241 #define IT87_REG_VIN_MAX(nr)   (0x30 + (nr) * 2)
242 #define IT87_REG_VIN_MIN(nr)   (0x31 + (nr) * 2)
243 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
244 #define IT87_REG_TEMP_LOW(nr)  (0x41 + (nr) * 2)
245
246 #define IT87_REG_VIN_ENABLE    0x50
247 #define IT87_REG_TEMP_ENABLE   0x51
248 #define IT87_REG_TEMP_EXTRA    0x55
249 #define IT87_REG_BEEP_ENABLE   0x5c
250
251 #define IT87_REG_CHIPID        0x58
252
253 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
254
255 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
256 #define IT87_REG_AUTO_PWM(nr, i)  (IT87_REG_AUTO_BASE[nr] + 5 + (i))
257
258 #define IT87_REG_TEMP456_ENABLE 0x77
259
260 #define NUM_VIN                 ARRAY_SIZE(IT87_REG_VIN)
261 #define NUM_VIN_LIMIT           8
262 #define NUM_TEMP                6
263 #define NUM_TEMP_OFFSET         ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
264 #define NUM_TEMP_LIMIT          3
265 #define NUM_FAN                 ARRAY_SIZE(IT87_REG_FAN)
266 #define NUM_FAN_DIV             3
267 #define NUM_PWM                 ARRAY_SIZE(IT87_REG_PWM)
268 #define NUM_AUTO_PWM            ARRAY_SIZE(IT87_REG_PWM)
269
270 struct it87_devices {
271         const char *name;
272         const char * const suffix;
273         u16 features;
274         u8 peci_mask;
275         u8 old_peci_mask;
276 };
277
278 #define FEAT_12MV_ADC           BIT(0)
279 #define FEAT_NEWER_AUTOPWM      BIT(1)
280 #define FEAT_OLD_AUTOPWM        BIT(2)
281 #define FEAT_16BIT_FANS         BIT(3)
282 #define FEAT_TEMP_OFFSET        BIT(4)
283 #define FEAT_TEMP_PECI          BIT(5)
284 #define FEAT_TEMP_OLD_PECI      BIT(6)
285 #define FEAT_FAN16_CONFIG       BIT(7)  /* Need to enable 16-bit fans */
286 #define FEAT_FIVE_FANS          BIT(8)  /* Supports five fans */
287 #define FEAT_VID                BIT(9)  /* Set if chip supports VID */
288 #define FEAT_IN7_INTERNAL       BIT(10) /* Set if in7 is internal */
289 #define FEAT_SIX_FANS           BIT(11) /* Supports six fans */
290 #define FEAT_AVCC3              BIT(12) /* Chip supports in9/AVCC3 */
291 #define FEAT_SIX_PWM            BIT(13) /* Chip supports 6 pwm chn */
292 #define FEAT_PWM_FREQ2          BIT(14) /* Separate pwm freq 2 */
293 #define FEAT_SIX_TEMP           BIT(15) /* Up to 6 temp sensors */
294
295 static const struct it87_devices it87_devices[] = {
296         [it87] = {
297                 .name = "it87",
298                 .suffix = "F",
299                 .features = FEAT_OLD_AUTOPWM,   /* may need to overwrite */
300         },
301         [it8712] = {
302                 .name = "it8712",
303                 .suffix = "F",
304                 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
305                                                 /* may need to overwrite */
306         },
307         [it8716] = {
308                 .name = "it8716",
309                 .suffix = "F",
310                 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
311                   | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
312         },
313         [it8718] = {
314                 .name = "it8718",
315                 .suffix = "F",
316                 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
317                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
318                   | FEAT_PWM_FREQ2,
319                 .old_peci_mask = 0x4,
320         },
321         [it8720] = {
322                 .name = "it8720",
323                 .suffix = "F",
324                 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
325                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
326                   | FEAT_PWM_FREQ2,
327                 .old_peci_mask = 0x4,
328         },
329         [it8721] = {
330                 .name = "it8721",
331                 .suffix = "F",
332                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
333                   | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
334                   | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
335                   | FEAT_PWM_FREQ2,
336                 .peci_mask = 0x05,
337                 .old_peci_mask = 0x02,  /* Actually reports PCH */
338         },
339         [it8728] = {
340                 .name = "it8728",
341                 .suffix = "F",
342                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
343                   | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
344                   | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
345                 .peci_mask = 0x07,
346         },
347         [it8771] = {
348                 .name = "it8771",
349                 .suffix = "E",
350                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
351                   | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
352                   | FEAT_PWM_FREQ2,
353                                 /* PECI: guesswork */
354                                 /* 12mV ADC (OHM) */
355                                 /* 16 bit fans (OHM) */
356                                 /* three fans, always 16 bit (guesswork) */
357                 .peci_mask = 0x07,
358         },
359         [it8772] = {
360                 .name = "it8772",
361                 .suffix = "E",
362                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
363                   | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
364                   | FEAT_PWM_FREQ2,
365                                 /* PECI (coreboot) */
366                                 /* 12mV ADC (HWSensors4, OHM) */
367                                 /* 16 bit fans (HWSensors4, OHM) */
368                                 /* three fans, always 16 bit (datasheet) */
369                 .peci_mask = 0x07,
370         },
371         [it8781] = {
372                 .name = "it8781",
373                 .suffix = "F",
374                 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
375                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
376                 .old_peci_mask = 0x4,
377         },
378         [it8782] = {
379                 .name = "it8782",
380                 .suffix = "F",
381                 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
382                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
383                 .old_peci_mask = 0x4,
384         },
385         [it8783] = {
386                 .name = "it8783",
387                 .suffix = "E/F",
388                 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
389                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
390                 .old_peci_mask = 0x4,
391         },
392         [it8786] = {
393                 .name = "it8786",
394                 .suffix = "E",
395                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
396                   | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
397                   | FEAT_PWM_FREQ2,
398                 .peci_mask = 0x07,
399         },
400         [it8790] = {
401                 .name = "it8790",
402                 .suffix = "E",
403                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
404                   | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
405                   | FEAT_PWM_FREQ2,
406                 .peci_mask = 0x07,
407         },
408         [it8603] = {
409                 .name = "it8603",
410                 .suffix = "E",
411                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
412                   | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
413                   | FEAT_AVCC3 | FEAT_PWM_FREQ2,
414                 .peci_mask = 0x07,
415         },
416         [it8620] = {
417                 .name = "it8620",
418                 .suffix = "E",
419                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
420                   | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
421                   | FEAT_IN7_INTERNAL | FEAT_AVCC3 | FEAT_SIX_PWM
422                   | FEAT_PWM_FREQ2 | FEAT_SIX_TEMP,
423                 .peci_mask = 0x07,
424         },
425 };
426
427 #define has_16bit_fans(data)    ((data)->features & FEAT_16BIT_FANS)
428 #define has_12mv_adc(data)      ((data)->features & FEAT_12MV_ADC)
429 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
430 #define has_old_autopwm(data)   ((data)->features & FEAT_OLD_AUTOPWM)
431 #define has_temp_offset(data)   ((data)->features & FEAT_TEMP_OFFSET)
432 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
433                                  ((data)->peci_mask & BIT(nr)))
434 #define has_temp_old_peci(data, nr) \
435                                 (((data)->features & FEAT_TEMP_OLD_PECI) && \
436                                  ((data)->old_peci_mask & BIT(nr)))
437 #define has_fan16_config(data)  ((data)->features & FEAT_FAN16_CONFIG)
438 #define has_five_fans(data)     ((data)->features & (FEAT_FIVE_FANS | \
439                                                      FEAT_SIX_FANS))
440 #define has_vid(data)           ((data)->features & FEAT_VID)
441 #define has_in7_internal(data)  ((data)->features & FEAT_IN7_INTERNAL)
442 #define has_six_fans(data)      ((data)->features & FEAT_SIX_FANS)
443 #define has_avcc3(data)         ((data)->features & FEAT_AVCC3)
444 #define has_six_pwm(data)       ((data)->features & FEAT_SIX_PWM)
445 #define has_pwm_freq2(data)     ((data)->features & FEAT_PWM_FREQ2)
446 #define has_six_temp(data)      ((data)->features & FEAT_SIX_TEMP)
447
448 struct it87_sio_data {
449         enum chips type;
450         /* Values read from Super-I/O config space */
451         u8 revision;
452         u8 vid_value;
453         u8 beep_pin;
454         u8 internal;    /* Internal sensors can be labeled */
455         /* Features skipped based on config or DMI */
456         u16 skip_in;
457         u8 skip_vid;
458         u8 skip_fan;
459         u8 skip_pwm;
460         u8 skip_temp;
461 };
462
463 /*
464  * For each registered chip, we need to keep some data in memory.
465  * The structure is dynamically allocated.
466  */
467 struct it87_data {
468         const struct attribute_group *groups[7];
469         enum chips type;
470         u16 features;
471         u8 peci_mask;
472         u8 old_peci_mask;
473
474         unsigned short addr;
475         const char *name;
476         struct mutex update_lock;
477         char valid;             /* !=0 if following fields are valid */
478         unsigned long last_updated;     /* In jiffies */
479
480         u16 in_scaled;          /* Internal voltage sensors are scaled */
481         u16 in_internal;        /* Bitfield, internal sensors (for labels) */
482         u16 has_in;             /* Bitfield, voltage sensors enabled */
483         u8 in[NUM_VIN][3];              /* [nr][0]=in, [1]=min, [2]=max */
484         u8 has_fan;             /* Bitfield, fans enabled */
485         u16 fan[NUM_FAN][2];    /* Register values, [nr][0]=fan, [1]=min */
486         u8 has_temp;            /* Bitfield, temp sensors enabled */
487         s8 temp[NUM_TEMP][4];   /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
488         u8 sensor;              /* Register value (IT87_REG_TEMP_ENABLE) */
489         u8 extra;               /* Register value (IT87_REG_TEMP_EXTRA) */
490         u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
491         bool has_vid;           /* True if VID supported */
492         u8 vid;                 /* Register encoding, combined */
493         u8 vrm;
494         u32 alarms;             /* Register encoding, combined */
495         bool has_beep;          /* true if beep supported */
496         u8 beeps;               /* Register encoding */
497         u8 fan_main_ctrl;       /* Register value */
498         u8 fan_ctl;             /* Register value */
499
500         /*
501          * The following 3 arrays correspond to the same registers up to
502          * the IT8720F. The meaning of bits 6-0 depends on the value of bit
503          * 7, and we want to preserve settings on mode changes, so we have
504          * to track all values separately.
505          * Starting with the IT8721F, the manual PWM duty cycles are stored
506          * in separate registers (8-bit values), so the separate tracking
507          * is no longer needed, but it is still done to keep the driver
508          * simple.
509          */
510         u8 has_pwm;             /* Bitfield, pwm control enabled */
511         u8 pwm_ctrl[NUM_PWM];   /* Register value */
512         u8 pwm_duty[NUM_PWM];   /* Manual PWM value set by user */
513         u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
514
515         /* Automatic fan speed control registers */
516         u8 auto_pwm[NUM_AUTO_PWM][4];   /* [nr][3] is hard-coded */
517         s8 auto_temp[NUM_AUTO_PWM][5];  /* [nr][0] is point1_temp_hyst */
518 };
519
520 static int adc_lsb(const struct it87_data *data, int nr)
521 {
522         int lsb = has_12mv_adc(data) ? 12 : 16;
523
524         if (data->in_scaled & BIT(nr))
525                 lsb <<= 1;
526         return lsb;
527 }
528
529 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
530 {
531         val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
532         return clamp_val(val, 0, 255);
533 }
534
535 static int in_from_reg(const struct it87_data *data, int nr, int val)
536 {
537         return val * adc_lsb(data, nr);
538 }
539
540 static inline u8 FAN_TO_REG(long rpm, int div)
541 {
542         if (rpm == 0)
543                 return 255;
544         rpm = clamp_val(rpm, 1, 1000000);
545         return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
546 }
547
548 static inline u16 FAN16_TO_REG(long rpm)
549 {
550         if (rpm == 0)
551                 return 0xffff;
552         return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
553 }
554
555 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
556                                 1350000 / ((val) * (div)))
557 /* The divider is fixed to 2 in 16-bit mode */
558 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
559                              1350000 / ((val) * 2))
560
561 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
562                                     ((val) + 500) / 1000), -128, 127))
563 #define TEMP_FROM_REG(val) ((val) * 1000)
564
565 static u8 pwm_to_reg(const struct it87_data *data, long val)
566 {
567         if (has_newer_autopwm(data))
568                 return val;
569         else
570                 return val >> 1;
571 }
572
573 static int pwm_from_reg(const struct it87_data *data, u8 reg)
574 {
575         if (has_newer_autopwm(data))
576                 return reg;
577         else
578                 return (reg & 0x7f) << 1;
579 }
580
581 static int DIV_TO_REG(int val)
582 {
583         int answer = 0;
584
585         while (answer < 7 && (val >>= 1))
586                 answer++;
587         return answer;
588 }
589
590 #define DIV_FROM_REG(val) BIT(val)
591
592 /*
593  * PWM base frequencies. The frequency has to be divided by either 128 or 256,
594  * depending on the chip type, to calculate the actual PWM frequency.
595  *
596  * Some of the chip datasheets suggest a base frequency of 51 kHz instead
597  * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
598  * of 200 Hz. Sometimes both PWM frequency select registers are affected,
599  * sometimes just one. It is unknown if this is a datasheet error or real,
600  * so this is ignored for now.
601  */
602 static const unsigned int pwm_freq[8] = {
603         48000000,
604         24000000,
605         12000000,
606         8000000,
607         6000000,
608         3000000,
609         1500000,
610         750000,
611 };
612
613 /*
614  * Must be called with data->update_lock held, except during initialization.
615  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
616  * would slow down the IT87 access and should not be necessary.
617  */
618 static int it87_read_value(struct it87_data *data, u8 reg)
619 {
620         outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
621         return inb_p(data->addr + IT87_DATA_REG_OFFSET);
622 }
623
624 /*
625  * Must be called with data->update_lock held, except during initialization.
626  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
627  * would slow down the IT87 access and should not be necessary.
628  */
629 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
630 {
631         outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
632         outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
633 }
634
635 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
636 {
637         data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
638         if (has_newer_autopwm(data)) {
639                 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
640                 data->pwm_duty[nr] = it87_read_value(data,
641                                                      IT87_REG_PWM_DUTY[nr]);
642         } else {
643                 if (data->pwm_ctrl[nr] & 0x80)  /* Automatic mode */
644                         data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
645                 else                            /* Manual mode */
646                         data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
647         }
648
649         if (has_old_autopwm(data)) {
650                 int i;
651
652                 for (i = 0; i < 5 ; i++)
653                         data->auto_temp[nr][i] = it87_read_value(data,
654                                                 IT87_REG_AUTO_TEMP(nr, i));
655                 for (i = 0; i < 3 ; i++)
656                         data->auto_pwm[nr][i] = it87_read_value(data,
657                                                 IT87_REG_AUTO_PWM(nr, i));
658         } else if (has_newer_autopwm(data)) {
659                 int i;
660
661                 /*
662                  * 0: temperature hysteresis (base + 5)
663                  * 1: fan off temperature (base + 0)
664                  * 2: fan start temperature (base + 1)
665                  * 3: fan max temperature (base + 2)
666                  */
667                 data->auto_temp[nr][0] =
668                         it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
669
670                 for (i = 0; i < 3 ; i++)
671                         data->auto_temp[nr][i + 1] =
672                                 it87_read_value(data,
673                                                 IT87_REG_AUTO_TEMP(nr, i));
674                 /*
675                  * 0: start pwm value (base + 3)
676                  * 1: pwm slope (base + 4, 1/8th pwm)
677                  */
678                 data->auto_pwm[nr][0] =
679                         it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
680                 data->auto_pwm[nr][1] =
681                         it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
682         }
683 }
684
685 static struct it87_data *it87_update_device(struct device *dev)
686 {
687         struct it87_data *data = dev_get_drvdata(dev);
688         int i;
689
690         mutex_lock(&data->update_lock);
691
692         if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
693             !data->valid) {
694                 if (update_vbat) {
695                         /*
696                          * Cleared after each update, so reenable.  Value
697                          * returned by this read will be previous value
698                          */
699                         it87_write_value(data, IT87_REG_CONFIG,
700                                 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
701                 }
702                 for (i = 0; i < NUM_VIN; i++) {
703                         if (!(data->has_in & BIT(i)))
704                                 continue;
705
706                         data->in[i][0] =
707                                 it87_read_value(data, IT87_REG_VIN[i]);
708
709                         /* VBAT and AVCC don't have limit registers */
710                         if (i >= NUM_VIN_LIMIT)
711                                 continue;
712
713                         data->in[i][1] =
714                                 it87_read_value(data, IT87_REG_VIN_MIN(i));
715                         data->in[i][2] =
716                                 it87_read_value(data, IT87_REG_VIN_MAX(i));
717                 }
718
719                 for (i = 0; i < NUM_FAN; i++) {
720                         /* Skip disabled fans */
721                         if (!(data->has_fan & BIT(i)))
722                                 continue;
723
724                         data->fan[i][1] =
725                                 it87_read_value(data, IT87_REG_FAN_MIN[i]);
726                         data->fan[i][0] = it87_read_value(data,
727                                        IT87_REG_FAN[i]);
728                         /* Add high byte if in 16-bit mode */
729                         if (has_16bit_fans(data)) {
730                                 data->fan[i][0] |= it87_read_value(data,
731                                                 IT87_REG_FANX[i]) << 8;
732                                 data->fan[i][1] |= it87_read_value(data,
733                                                 IT87_REG_FANX_MIN[i]) << 8;
734                         }
735                 }
736                 for (i = 0; i < NUM_TEMP; i++) {
737                         if (!(data->has_temp & BIT(i)))
738                                 continue;
739                         data->temp[i][0] =
740                                 it87_read_value(data, IT87_REG_TEMP(i));
741
742                         if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
743                                 data->temp[i][3] =
744                                   it87_read_value(data,
745                                                   IT87_REG_TEMP_OFFSET[i]);
746
747                         if (i >= NUM_TEMP_LIMIT)
748                                 continue;
749
750                         data->temp[i][1] =
751                                 it87_read_value(data, IT87_REG_TEMP_LOW(i));
752                         data->temp[i][2] =
753                                 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
754                 }
755
756                 /* Newer chips don't have clock dividers */
757                 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
758                         i = it87_read_value(data, IT87_REG_FAN_DIV);
759                         data->fan_div[0] = i & 0x07;
760                         data->fan_div[1] = (i >> 3) & 0x07;
761                         data->fan_div[2] = (i & 0x40) ? 3 : 1;
762                 }
763
764                 data->alarms =
765                         it87_read_value(data, IT87_REG_ALARM1) |
766                         (it87_read_value(data, IT87_REG_ALARM2) << 8) |
767                         (it87_read_value(data, IT87_REG_ALARM3) << 16);
768                 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
769
770                 data->fan_main_ctrl = it87_read_value(data,
771                                 IT87_REG_FAN_MAIN_CTRL);
772                 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
773                 for (i = 0; i < NUM_PWM; i++) {
774                         if (!(data->has_pwm & BIT(i)))
775                                 continue;
776                         it87_update_pwm_ctrl(data, i);
777                 }
778
779                 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
780                 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
781                 /*
782                  * The IT8705F does not have VID capability.
783                  * The IT8718F and later don't use IT87_REG_VID for the
784                  * same purpose.
785                  */
786                 if (data->type == it8712 || data->type == it8716) {
787                         data->vid = it87_read_value(data, IT87_REG_VID);
788                         /*
789                          * The older IT8712F revisions had only 5 VID pins,
790                          * but we assume it is always safe to read 6 bits.
791                          */
792                         data->vid &= 0x3f;
793                 }
794                 data->last_updated = jiffies;
795                 data->valid = 1;
796         }
797
798         mutex_unlock(&data->update_lock);
799
800         return data;
801 }
802
803 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
804                        char *buf)
805 {
806         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
807         struct it87_data *data = it87_update_device(dev);
808         int index = sattr->index;
809         int nr = sattr->nr;
810
811         return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
812 }
813
814 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
815                       const char *buf, size_t count)
816 {
817         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
818         struct it87_data *data = dev_get_drvdata(dev);
819         int index = sattr->index;
820         int nr = sattr->nr;
821         unsigned long val;
822
823         if (kstrtoul(buf, 10, &val) < 0)
824                 return -EINVAL;
825
826         mutex_lock(&data->update_lock);
827         data->in[nr][index] = in_to_reg(data, nr, val);
828         it87_write_value(data,
829                          index == 1 ? IT87_REG_VIN_MIN(nr)
830                                     : IT87_REG_VIN_MAX(nr),
831                          data->in[nr][index]);
832         mutex_unlock(&data->update_lock);
833         return count;
834 }
835
836 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
837 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
838                             0, 1);
839 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
840                             0, 2);
841
842 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
843 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
844                             1, 1);
845 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
846                             1, 2);
847
848 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
849 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
850                             2, 1);
851 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
852                             2, 2);
853
854 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
855 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
856                             3, 1);
857 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
858                             3, 2);
859
860 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
861 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
862                             4, 1);
863 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
864                             4, 2);
865
866 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
867 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
868                             5, 1);
869 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
870                             5, 2);
871
872 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
873 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
874                             6, 1);
875 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
876                             6, 2);
877
878 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
879 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
880                             7, 1);
881 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
882                             7, 2);
883
884 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
885 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
886 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
887 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
888 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
889
890 /* Up to 6 temperatures */
891 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
892                          char *buf)
893 {
894         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
895         int nr = sattr->nr;
896         int index = sattr->index;
897         struct it87_data *data = it87_update_device(dev);
898
899         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
900 }
901
902 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
903                         const char *buf, size_t count)
904 {
905         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
906         int nr = sattr->nr;
907         int index = sattr->index;
908         struct it87_data *data = dev_get_drvdata(dev);
909         long val;
910         u8 reg, regval;
911
912         if (kstrtol(buf, 10, &val) < 0)
913                 return -EINVAL;
914
915         mutex_lock(&data->update_lock);
916
917         switch (index) {
918         default:
919         case 1:
920                 reg = IT87_REG_TEMP_LOW(nr);
921                 break;
922         case 2:
923                 reg = IT87_REG_TEMP_HIGH(nr);
924                 break;
925         case 3:
926                 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
927                 if (!(regval & 0x80)) {
928                         regval |= 0x80;
929                         it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
930                 }
931                 data->valid = 0;
932                 reg = IT87_REG_TEMP_OFFSET[nr];
933                 break;
934         }
935
936         data->temp[nr][index] = TEMP_TO_REG(val);
937         it87_write_value(data, reg, data->temp[nr][index]);
938         mutex_unlock(&data->update_lock);
939         return count;
940 }
941
942 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
943 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
944                             0, 1);
945 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
946                             0, 2);
947 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
948                             set_temp, 0, 3);
949 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
950 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
951                             1, 1);
952 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
953                             1, 2);
954 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
955                             set_temp, 1, 3);
956 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
957 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
958                             2, 1);
959 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
960                             2, 2);
961 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
962                             set_temp, 2, 3);
963 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
964 static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
965 static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
966
967 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
968                               char *buf)
969 {
970         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
971         int nr = sensor_attr->index;
972         struct it87_data *data = it87_update_device(dev);
973         u8 reg = data->sensor;      /* In case value is updated while used */
974         u8 extra = data->extra;
975
976         if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
977             (has_temp_old_peci(data, nr) && (extra & 0x80)))
978                 return sprintf(buf, "6\n");  /* Intel PECI */
979         if (reg & (1 << nr))
980                 return sprintf(buf, "3\n");  /* thermal diode */
981         if (reg & (8 << nr))
982                 return sprintf(buf, "4\n");  /* thermistor */
983         return sprintf(buf, "0\n");      /* disabled */
984 }
985
986 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
987                              const char *buf, size_t count)
988 {
989         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
990         int nr = sensor_attr->index;
991
992         struct it87_data *data = dev_get_drvdata(dev);
993         long val;
994         u8 reg, extra;
995
996         if (kstrtol(buf, 10, &val) < 0)
997                 return -EINVAL;
998
999         reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1000         reg &= ~(1 << nr);
1001         reg &= ~(8 << nr);
1002         if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1003                 reg &= 0x3f;
1004         extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1005         if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1006                 extra &= 0x7f;
1007         if (val == 2) { /* backwards compatibility */
1008                 dev_warn(dev,
1009                          "Sensor type 2 is deprecated, please use 4 instead\n");
1010                 val = 4;
1011         }
1012         /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1013         if (val == 3)
1014                 reg |= 1 << nr;
1015         else if (val == 4)
1016                 reg |= 8 << nr;
1017         else if (has_temp_peci(data, nr) && val == 6)
1018                 reg |= (nr + 1) << 6;
1019         else if (has_temp_old_peci(data, nr) && val == 6)
1020                 extra |= 0x80;
1021         else if (val != 0)
1022                 return -EINVAL;
1023
1024         mutex_lock(&data->update_lock);
1025         data->sensor = reg;
1026         data->extra = extra;
1027         it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
1028         if (has_temp_old_peci(data, nr))
1029                 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1030         data->valid = 0;        /* Force cache refresh */
1031         mutex_unlock(&data->update_lock);
1032         return count;
1033 }
1034
1035 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1036                           set_temp_type, 0);
1037 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1038                           set_temp_type, 1);
1039 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1040                           set_temp_type, 2);
1041
1042 /* 6 Fans */
1043
1044 static int pwm_mode(const struct it87_data *data, int nr)
1045 {
1046         if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1047                 return 0;                               /* Full speed */
1048         if (data->pwm_ctrl[nr] & 0x80) {
1049                 return 2;                               /* Automatic mode */
1050         } else {
1051                 if ((data->type == it8603 || nr >= 3) &&
1052                     data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1053                         return 0;                       /* Full speed */
1054                 return 1;                               /* Manual mode */
1055         }
1056 }
1057
1058 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1059                         char *buf)
1060 {
1061         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1062         int nr = sattr->nr;
1063         int index = sattr->index;
1064         int speed;
1065         struct it87_data *data = it87_update_device(dev);
1066
1067         speed = has_16bit_fans(data) ?
1068                 FAN16_FROM_REG(data->fan[nr][index]) :
1069                 FAN_FROM_REG(data->fan[nr][index],
1070                              DIV_FROM_REG(data->fan_div[nr]));
1071         return sprintf(buf, "%d\n", speed);
1072 }
1073
1074 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1075                             char *buf)
1076 {
1077         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1078         struct it87_data *data = it87_update_device(dev);
1079         int nr = sensor_attr->index;
1080
1081         return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1082 }
1083
1084 static ssize_t show_pwm_enable(struct device *dev,
1085                                struct device_attribute *attr, char *buf)
1086 {
1087         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1088         struct it87_data *data = it87_update_device(dev);
1089         int nr = sensor_attr->index;
1090
1091         return sprintf(buf, "%d\n", pwm_mode(data, nr));
1092 }
1093
1094 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1095                         char *buf)
1096 {
1097         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1098         struct it87_data *data = it87_update_device(dev);
1099         int nr = sensor_attr->index;
1100
1101         return sprintf(buf, "%d\n",
1102                        pwm_from_reg(data, data->pwm_duty[nr]));
1103 }
1104
1105 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1106                              char *buf)
1107 {
1108         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1109         struct it87_data *data = it87_update_device(dev);
1110         int nr = sensor_attr->index;
1111         unsigned int freq;
1112         int index;
1113
1114         if (has_pwm_freq2(data) && nr == 1)
1115                 index = (data->extra >> 4) & 0x07;
1116         else
1117                 index = (data->fan_ctl >> 4) & 0x07;
1118
1119         freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1120
1121         return sprintf(buf, "%u\n", freq);
1122 }
1123
1124 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1125                        const char *buf, size_t count)
1126 {
1127         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1128         int nr = sattr->nr;
1129         int index = sattr->index;
1130
1131         struct it87_data *data = dev_get_drvdata(dev);
1132         long val;
1133         u8 reg;
1134
1135         if (kstrtol(buf, 10, &val) < 0)
1136                 return -EINVAL;
1137
1138         mutex_lock(&data->update_lock);
1139
1140         if (has_16bit_fans(data)) {
1141                 data->fan[nr][index] = FAN16_TO_REG(val);
1142                 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1143                                  data->fan[nr][index] & 0xff);
1144                 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1145                                  data->fan[nr][index] >> 8);
1146         } else {
1147                 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1148                 switch (nr) {
1149                 case 0:
1150                         data->fan_div[nr] = reg & 0x07;
1151                         break;
1152                 case 1:
1153                         data->fan_div[nr] = (reg >> 3) & 0x07;
1154                         break;
1155                 case 2:
1156                         data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1157                         break;
1158                 }
1159                 data->fan[nr][index] =
1160                   FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1161                 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1162                                  data->fan[nr][index]);
1163         }
1164
1165         mutex_unlock(&data->update_lock);
1166         return count;
1167 }
1168
1169 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1170                            const char *buf, size_t count)
1171 {
1172         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1173         struct it87_data *data = dev_get_drvdata(dev);
1174         int nr = sensor_attr->index;
1175         unsigned long val;
1176         int min;
1177         u8 old;
1178
1179         if (kstrtoul(buf, 10, &val) < 0)
1180                 return -EINVAL;
1181
1182         mutex_lock(&data->update_lock);
1183         old = it87_read_value(data, IT87_REG_FAN_DIV);
1184
1185         /* Save fan min limit */
1186         min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1187
1188         switch (nr) {
1189         case 0:
1190         case 1:
1191                 data->fan_div[nr] = DIV_TO_REG(val);
1192                 break;
1193         case 2:
1194                 if (val < 8)
1195                         data->fan_div[nr] = 1;
1196                 else
1197                         data->fan_div[nr] = 3;
1198         }
1199         val = old & 0x80;
1200         val |= (data->fan_div[0] & 0x07);
1201         val |= (data->fan_div[1] & 0x07) << 3;
1202         if (data->fan_div[2] == 3)
1203                 val |= 0x1 << 6;
1204         it87_write_value(data, IT87_REG_FAN_DIV, val);
1205
1206         /* Restore fan min limit */
1207         data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1208         it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
1209
1210         mutex_unlock(&data->update_lock);
1211         return count;
1212 }
1213
1214 /* Returns 0 if OK, -EINVAL otherwise */
1215 static int check_trip_points(struct device *dev, int nr)
1216 {
1217         const struct it87_data *data = dev_get_drvdata(dev);
1218         int i, err = 0;
1219
1220         if (has_old_autopwm(data)) {
1221                 for (i = 0; i < 3; i++) {
1222                         if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1223                                 err = -EINVAL;
1224                 }
1225                 for (i = 0; i < 2; i++) {
1226                         if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1227                                 err = -EINVAL;
1228                 }
1229         } else if (has_newer_autopwm(data)) {
1230                 for (i = 1; i < 3; i++) {
1231                         if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1232                                 err = -EINVAL;
1233                 }
1234         }
1235
1236         if (err) {
1237                 dev_err(dev,
1238                         "Inconsistent trip points, not switching to automatic mode\n");
1239                 dev_err(dev, "Adjust the trip points and try again\n");
1240         }
1241         return err;
1242 }
1243
1244 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1245                               const char *buf, size_t count)
1246 {
1247         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1248         struct it87_data *data = dev_get_drvdata(dev);
1249         int nr = sensor_attr->index;
1250         long val;
1251
1252         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1253                 return -EINVAL;
1254
1255         /* Check trip points before switching to automatic mode */
1256         if (val == 2) {
1257                 if (check_trip_points(dev, nr) < 0)
1258                         return -EINVAL;
1259         }
1260
1261         mutex_lock(&data->update_lock);
1262
1263         if (val == 0) {
1264                 if (nr < 3 && data->type != it8603) {
1265                         int tmp;
1266                         /* make sure the fan is on when in on/off mode */
1267                         tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1268                         it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1269                         /* set on/off mode */
1270                         data->fan_main_ctrl &= ~BIT(nr);
1271                         it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1272                                          data->fan_main_ctrl);
1273                 } else {
1274                         /* No on/off mode, set maximum pwm value */
1275                         data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1276                         it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1277                                          data->pwm_duty[nr]);
1278                         /* and set manual mode */
1279                         data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1280                                              data->pwm_temp_map[nr] :
1281                                              data->pwm_duty[nr];
1282                         it87_write_value(data, IT87_REG_PWM[nr],
1283                                          data->pwm_ctrl[nr]);
1284                 }
1285         } else {
1286                 if (val == 1)                           /* Manual mode */
1287                         data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1288                                              data->pwm_temp_map[nr] :
1289                                              data->pwm_duty[nr];
1290                 else                                    /* Automatic mode */
1291                         data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1292                 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1293
1294                 if (data->type != it8603 && nr < 3) {
1295                         /* set SmartGuardian mode */
1296                         data->fan_main_ctrl |= BIT(nr);
1297                         it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1298                                          data->fan_main_ctrl);
1299                 }
1300         }
1301
1302         mutex_unlock(&data->update_lock);
1303         return count;
1304 }
1305
1306 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1307                        const char *buf, size_t count)
1308 {
1309         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1310         struct it87_data *data = dev_get_drvdata(dev);
1311         int nr = sensor_attr->index;
1312         long val;
1313
1314         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1315                 return -EINVAL;
1316
1317         mutex_lock(&data->update_lock);
1318         if (has_newer_autopwm(data)) {
1319                 /*
1320                  * If we are in automatic mode, the PWM duty cycle register
1321                  * is read-only so we can't write the value.
1322                  */
1323                 if (data->pwm_ctrl[nr] & 0x80) {
1324                         mutex_unlock(&data->update_lock);
1325                         return -EBUSY;
1326                 }
1327                 data->pwm_duty[nr] = pwm_to_reg(data, val);
1328                 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1329                                  data->pwm_duty[nr]);
1330         } else {
1331                 data->pwm_duty[nr] = pwm_to_reg(data, val);
1332                 /*
1333                  * If we are in manual mode, write the duty cycle immediately;
1334                  * otherwise, just store it for later use.
1335                  */
1336                 if (!(data->pwm_ctrl[nr] & 0x80)) {
1337                         data->pwm_ctrl[nr] = data->pwm_duty[nr];
1338                         it87_write_value(data, IT87_REG_PWM[nr],
1339                                          data->pwm_ctrl[nr]);
1340                 }
1341         }
1342         mutex_unlock(&data->update_lock);
1343         return count;
1344 }
1345
1346 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1347                             const char *buf, size_t count)
1348 {
1349         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1350         struct it87_data *data = dev_get_drvdata(dev);
1351         int nr = sensor_attr->index;
1352         unsigned long val;
1353         int i;
1354
1355         if (kstrtoul(buf, 10, &val) < 0)
1356                 return -EINVAL;
1357
1358         val = clamp_val(val, 0, 1000000);
1359         val *= has_newer_autopwm(data) ? 256 : 128;
1360
1361         /* Search for the nearest available frequency */
1362         for (i = 0; i < 7; i++) {
1363                 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1364                         break;
1365         }
1366
1367         mutex_lock(&data->update_lock);
1368         if (nr == 0) {
1369                 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1370                 data->fan_ctl |= i << 4;
1371                 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1372         } else {
1373                 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1374                 data->extra |= i << 4;
1375                 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1376         }
1377         mutex_unlock(&data->update_lock);
1378
1379         return count;
1380 }
1381
1382 static ssize_t show_pwm_temp_map(struct device *dev,
1383                                  struct device_attribute *attr, char *buf)
1384 {
1385         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1386         struct it87_data *data = it87_update_device(dev);
1387         int nr = sensor_attr->index;
1388         int map;
1389
1390         map = data->pwm_temp_map[nr];
1391         if (map >= 3)
1392                 map = 0;        /* Should never happen */
1393         if (nr >= 3)            /* pwm channels 3..6 map to temp4..6 */
1394                 map += 3;
1395
1396         return sprintf(buf, "%d\n", (int)BIT(map));
1397 }
1398
1399 static ssize_t set_pwm_temp_map(struct device *dev,
1400                                 struct device_attribute *attr, const char *buf,
1401                                 size_t count)
1402 {
1403         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1404         struct it87_data *data = dev_get_drvdata(dev);
1405         int nr = sensor_attr->index;
1406         long val;
1407         u8 reg;
1408
1409         if (kstrtol(buf, 10, &val) < 0)
1410                 return -EINVAL;
1411
1412         if (nr >= 3)
1413                 val -= 3;
1414
1415         switch (val) {
1416         case BIT(0):
1417                 reg = 0x00;
1418                 break;
1419         case BIT(1):
1420                 reg = 0x01;
1421                 break;
1422         case BIT(2):
1423                 reg = 0x02;
1424                 break;
1425         default:
1426                 return -EINVAL;
1427         }
1428
1429         mutex_lock(&data->update_lock);
1430         data->pwm_temp_map[nr] = reg;
1431         /*
1432          * If we are in automatic mode, write the temp mapping immediately;
1433          * otherwise, just store it for later use.
1434          */
1435         if (data->pwm_ctrl[nr] & 0x80) {
1436                 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1437                 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1438         }
1439         mutex_unlock(&data->update_lock);
1440         return count;
1441 }
1442
1443 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1444                              char *buf)
1445 {
1446         struct it87_data *data = it87_update_device(dev);
1447         struct sensor_device_attribute_2 *sensor_attr =
1448                         to_sensor_dev_attr_2(attr);
1449         int nr = sensor_attr->nr;
1450         int point = sensor_attr->index;
1451
1452         return sprintf(buf, "%d\n",
1453                        pwm_from_reg(data, data->auto_pwm[nr][point]));
1454 }
1455
1456 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1457                             const char *buf, size_t count)
1458 {
1459         struct it87_data *data = dev_get_drvdata(dev);
1460         struct sensor_device_attribute_2 *sensor_attr =
1461                         to_sensor_dev_attr_2(attr);
1462         int nr = sensor_attr->nr;
1463         int point = sensor_attr->index;
1464         int regaddr;
1465         long val;
1466
1467         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1468                 return -EINVAL;
1469
1470         mutex_lock(&data->update_lock);
1471         data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1472         if (has_newer_autopwm(data))
1473                 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1474         else
1475                 regaddr = IT87_REG_AUTO_PWM(nr, point);
1476         it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1477         mutex_unlock(&data->update_lock);
1478         return count;
1479 }
1480
1481 static ssize_t show_auto_pwm_slope(struct device *dev,
1482                                    struct device_attribute *attr, char *buf)
1483 {
1484         struct it87_data *data = it87_update_device(dev);
1485         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1486         int nr = sensor_attr->index;
1487
1488         return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1489 }
1490
1491 static ssize_t set_auto_pwm_slope(struct device *dev,
1492                                   struct device_attribute *attr,
1493                                   const char *buf, size_t count)
1494 {
1495         struct it87_data *data = dev_get_drvdata(dev);
1496         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1497         int nr = sensor_attr->index;
1498         unsigned long val;
1499
1500         if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1501                 return -EINVAL;
1502
1503         mutex_lock(&data->update_lock);
1504         data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1505         it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1506                          data->auto_pwm[nr][1]);
1507         mutex_unlock(&data->update_lock);
1508         return count;
1509 }
1510
1511 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1512                               char *buf)
1513 {
1514         struct it87_data *data = it87_update_device(dev);
1515         struct sensor_device_attribute_2 *sensor_attr =
1516                         to_sensor_dev_attr_2(attr);
1517         int nr = sensor_attr->nr;
1518         int point = sensor_attr->index;
1519         int reg;
1520
1521         if (has_old_autopwm(data) || point)
1522                 reg = data->auto_temp[nr][point];
1523         else
1524                 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1525
1526         return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
1527 }
1528
1529 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1530                              const char *buf, size_t count)
1531 {
1532         struct it87_data *data = dev_get_drvdata(dev);
1533         struct sensor_device_attribute_2 *sensor_attr =
1534                         to_sensor_dev_attr_2(attr);
1535         int nr = sensor_attr->nr;
1536         int point = sensor_attr->index;
1537         long val;
1538         int reg;
1539
1540         if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1541                 return -EINVAL;
1542
1543         mutex_lock(&data->update_lock);
1544         if (has_newer_autopwm(data) && !point) {
1545                 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1546                 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1547                 data->auto_temp[nr][0] = reg;
1548                 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1549         } else {
1550                 reg = TEMP_TO_REG(val);
1551                 data->auto_temp[nr][point] = reg;
1552                 if (has_newer_autopwm(data))
1553                         point--;
1554                 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1555         }
1556         mutex_unlock(&data->update_lock);
1557         return count;
1558 }
1559
1560 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1561 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1562                             0, 1);
1563 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1564                           set_fan_div, 0);
1565
1566 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1567 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1568                             1, 1);
1569 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1570                           set_fan_div, 1);
1571
1572 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1573 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1574                             2, 1);
1575 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1576                           set_fan_div, 2);
1577
1578 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1579 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1580                             3, 1);
1581
1582 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1583 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1584                             4, 1);
1585
1586 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1587 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1588                             5, 1);
1589
1590 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1591                           show_pwm_enable, set_pwm_enable, 0);
1592 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1593 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1594                           set_pwm_freq, 0);
1595 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
1596                           show_pwm_temp_map, set_pwm_temp_map, 0);
1597 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1598                             show_auto_pwm, set_auto_pwm, 0, 0);
1599 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1600                             show_auto_pwm, set_auto_pwm, 0, 1);
1601 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1602                             show_auto_pwm, set_auto_pwm, 0, 2);
1603 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1604                             show_auto_pwm, NULL, 0, 3);
1605 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1606                             show_auto_temp, set_auto_temp, 0, 1);
1607 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1608                             show_auto_temp, set_auto_temp, 0, 0);
1609 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1610                             show_auto_temp, set_auto_temp, 0, 2);
1611 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1612                             show_auto_temp, set_auto_temp, 0, 3);
1613 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1614                             show_auto_temp, set_auto_temp, 0, 4);
1615 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1616                             show_auto_pwm, set_auto_pwm, 0, 0);
1617 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1618                           show_auto_pwm_slope, set_auto_pwm_slope, 0);
1619
1620 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1621                           show_pwm_enable, set_pwm_enable, 1);
1622 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1623 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1624 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
1625                           show_pwm_temp_map, set_pwm_temp_map, 1);
1626 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1627                             show_auto_pwm, set_auto_pwm, 1, 0);
1628 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1629                             show_auto_pwm, set_auto_pwm, 1, 1);
1630 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1631                             show_auto_pwm, set_auto_pwm, 1, 2);
1632 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1633                             show_auto_pwm, NULL, 1, 3);
1634 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1635                             show_auto_temp, set_auto_temp, 1, 1);
1636 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1637                             show_auto_temp, set_auto_temp, 1, 0);
1638 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1639                             show_auto_temp, set_auto_temp, 1, 2);
1640 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1641                             show_auto_temp, set_auto_temp, 1, 3);
1642 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1643                             show_auto_temp, set_auto_temp, 1, 4);
1644 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1645                             show_auto_pwm, set_auto_pwm, 1, 0);
1646 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1647                           show_auto_pwm_slope, set_auto_pwm_slope, 1);
1648
1649 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1650                           show_pwm_enable, set_pwm_enable, 2);
1651 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1652 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1653 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
1654                           show_pwm_temp_map, set_pwm_temp_map, 2);
1655 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1656                             show_auto_pwm, set_auto_pwm, 2, 0);
1657 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1658                             show_auto_pwm, set_auto_pwm, 2, 1);
1659 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1660                             show_auto_pwm, set_auto_pwm, 2, 2);
1661 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1662                             show_auto_pwm, NULL, 2, 3);
1663 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1664                             show_auto_temp, set_auto_temp, 2, 1);
1665 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1666                             show_auto_temp, set_auto_temp, 2, 0);
1667 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1668                             show_auto_temp, set_auto_temp, 2, 2);
1669 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1670                             show_auto_temp, set_auto_temp, 2, 3);
1671 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1672                             show_auto_temp, set_auto_temp, 2, 4);
1673 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1674                             show_auto_pwm, set_auto_pwm, 2, 0);
1675 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1676                           show_auto_pwm_slope, set_auto_pwm_slope, 2);
1677
1678 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1679                           show_pwm_enable, set_pwm_enable, 3);
1680 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1681 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1682 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
1683                           show_pwm_temp_map, set_pwm_temp_map, 3);
1684 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1685                             show_auto_temp, set_auto_temp, 2, 1);
1686 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1687                             show_auto_temp, set_auto_temp, 2, 0);
1688 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1689                             show_auto_temp, set_auto_temp, 2, 2);
1690 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1691                             show_auto_temp, set_auto_temp, 2, 3);
1692 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1693                             show_auto_pwm, set_auto_pwm, 3, 0);
1694 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1695                           show_auto_pwm_slope, set_auto_pwm_slope, 3);
1696
1697 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1698                           show_pwm_enable, set_pwm_enable, 4);
1699 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1700 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1701 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
1702                           show_pwm_temp_map, set_pwm_temp_map, 4);
1703 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1704                             show_auto_temp, set_auto_temp, 2, 1);
1705 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1706                             show_auto_temp, set_auto_temp, 2, 0);
1707 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1708                             show_auto_temp, set_auto_temp, 2, 2);
1709 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1710                             show_auto_temp, set_auto_temp, 2, 3);
1711 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1712                             show_auto_pwm, set_auto_pwm, 4, 0);
1713 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1714                           show_auto_pwm_slope, set_auto_pwm_slope, 4);
1715
1716 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1717                           show_pwm_enable, set_pwm_enable, 5);
1718 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1719 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1720 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
1721                           show_pwm_temp_map, set_pwm_temp_map, 5);
1722 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1723                             show_auto_temp, set_auto_temp, 2, 1);
1724 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1725                             show_auto_temp, set_auto_temp, 2, 0);
1726 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1727                             show_auto_temp, set_auto_temp, 2, 2);
1728 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1729                             show_auto_temp, set_auto_temp, 2, 3);
1730 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1731                             show_auto_pwm, set_auto_pwm, 5, 0);
1732 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1733                           show_auto_pwm_slope, set_auto_pwm_slope, 5);
1734
1735 /* Alarms */
1736 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1737                            char *buf)
1738 {
1739         struct it87_data *data = it87_update_device(dev);
1740
1741         return sprintf(buf, "%u\n", data->alarms);
1742 }
1743 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1744
1745 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1746                           char *buf)
1747 {
1748         struct it87_data *data = it87_update_device(dev);
1749         int bitnr = to_sensor_dev_attr(attr)->index;
1750
1751         return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1752 }
1753
1754 static ssize_t clear_intrusion(struct device *dev,
1755                                struct device_attribute *attr, const char *buf,
1756                                size_t count)
1757 {
1758         struct it87_data *data = dev_get_drvdata(dev);
1759         int config;
1760         long val;
1761
1762         if (kstrtol(buf, 10, &val) < 0 || val != 0)
1763                 return -EINVAL;
1764
1765         mutex_lock(&data->update_lock);
1766         config = it87_read_value(data, IT87_REG_CONFIG);
1767         if (config < 0) {
1768                 count = config;
1769         } else {
1770                 config |= BIT(5);
1771                 it87_write_value(data, IT87_REG_CONFIG, config);
1772                 /* Invalidate cache to force re-read */
1773                 data->valid = 0;
1774         }
1775         mutex_unlock(&data->update_lock);
1776
1777         return count;
1778 }
1779
1780 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1781 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1782 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1783 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1784 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1785 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1786 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1787 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1788 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1789 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1790 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1791 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1792 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1793 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1794 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1795 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1796 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1797 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1798                           show_alarm, clear_intrusion, 4);
1799
1800 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1801                          char *buf)
1802 {
1803         struct it87_data *data = it87_update_device(dev);
1804         int bitnr = to_sensor_dev_attr(attr)->index;
1805
1806         return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1807 }
1808
1809 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1810                         const char *buf, size_t count)
1811 {
1812         int bitnr = to_sensor_dev_attr(attr)->index;
1813         struct it87_data *data = dev_get_drvdata(dev);
1814         long val;
1815
1816         if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
1817                 return -EINVAL;
1818
1819         mutex_lock(&data->update_lock);
1820         data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1821         if (val)
1822                 data->beeps |= BIT(bitnr);
1823         else
1824                 data->beeps &= ~BIT(bitnr);
1825         it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1826         mutex_unlock(&data->update_lock);
1827         return count;
1828 }
1829
1830 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1831                           show_beep, set_beep, 1);
1832 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1833 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1834 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1835 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1836 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1837 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1838 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1839 /* fanX_beep writability is set later */
1840 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1841 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1842 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1843 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1844 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1845 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1846 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1847                           show_beep, set_beep, 2);
1848 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1849 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1850
1851 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1852                             char *buf)
1853 {
1854         struct it87_data *data = dev_get_drvdata(dev);
1855
1856         return sprintf(buf, "%u\n", data->vrm);
1857 }
1858
1859 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1860                              const char *buf, size_t count)
1861 {
1862         struct it87_data *data = dev_get_drvdata(dev);
1863         unsigned long val;
1864
1865         if (kstrtoul(buf, 10, &val) < 0)
1866                 return -EINVAL;
1867
1868         data->vrm = val;
1869
1870         return count;
1871 }
1872 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1873
1874 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1875                             char *buf)
1876 {
1877         struct it87_data *data = it87_update_device(dev);
1878
1879         return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
1880 }
1881 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1882
1883 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1884                           char *buf)
1885 {
1886         static const char * const labels[] = {
1887                 "+5V",
1888                 "5VSB",
1889                 "Vbat",
1890         };
1891         static const char * const labels_it8721[] = {
1892                 "+3.3V",
1893                 "3VSB",
1894                 "Vbat",
1895         };
1896         struct it87_data *data = dev_get_drvdata(dev);
1897         int nr = to_sensor_dev_attr(attr)->index;
1898
1899         return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1900                                                        : labels[nr]);
1901 }
1902 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1903 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1904 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1905 /* AVCC3 */
1906 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1907
1908 static umode_t it87_in_is_visible(struct kobject *kobj,
1909                                   struct attribute *attr, int index)
1910 {
1911         struct device *dev = container_of(kobj, struct device, kobj);
1912         struct it87_data *data = dev_get_drvdata(dev);
1913         int i = index / 5;      /* voltage index */
1914         int a = index % 5;      /* attribute index */
1915
1916         if (index >= 40) {      /* in8 and higher only have input attributes */
1917                 i = index - 40 + 8;
1918                 a = 0;
1919         }
1920
1921         if (!(data->has_in & BIT(i)))
1922                 return 0;
1923
1924         if (a == 4 && !data->has_beep)
1925                 return 0;
1926
1927         return attr->mode;
1928 }
1929
1930 static struct attribute *it87_attributes_in[] = {
1931         &sensor_dev_attr_in0_input.dev_attr.attr,
1932         &sensor_dev_attr_in0_min.dev_attr.attr,
1933         &sensor_dev_attr_in0_max.dev_attr.attr,
1934         &sensor_dev_attr_in0_alarm.dev_attr.attr,
1935         &sensor_dev_attr_in0_beep.dev_attr.attr,        /* 4 */
1936
1937         &sensor_dev_attr_in1_input.dev_attr.attr,
1938         &sensor_dev_attr_in1_min.dev_attr.attr,
1939         &sensor_dev_attr_in1_max.dev_attr.attr,
1940         &sensor_dev_attr_in1_alarm.dev_attr.attr,
1941         &sensor_dev_attr_in1_beep.dev_attr.attr,        /* 9 */
1942
1943         &sensor_dev_attr_in2_input.dev_attr.attr,
1944         &sensor_dev_attr_in2_min.dev_attr.attr,
1945         &sensor_dev_attr_in2_max.dev_attr.attr,
1946         &sensor_dev_attr_in2_alarm.dev_attr.attr,
1947         &sensor_dev_attr_in2_beep.dev_attr.attr,        /* 14 */
1948
1949         &sensor_dev_attr_in3_input.dev_attr.attr,
1950         &sensor_dev_attr_in3_min.dev_attr.attr,
1951         &sensor_dev_attr_in3_max.dev_attr.attr,
1952         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1953         &sensor_dev_attr_in3_beep.dev_attr.attr,        /* 19 */
1954
1955         &sensor_dev_attr_in4_input.dev_attr.attr,
1956         &sensor_dev_attr_in4_min.dev_attr.attr,
1957         &sensor_dev_attr_in4_max.dev_attr.attr,
1958         &sensor_dev_attr_in4_alarm.dev_attr.attr,
1959         &sensor_dev_attr_in4_beep.dev_attr.attr,        /* 24 */
1960
1961         &sensor_dev_attr_in5_input.dev_attr.attr,
1962         &sensor_dev_attr_in5_min.dev_attr.attr,
1963         &sensor_dev_attr_in5_max.dev_attr.attr,
1964         &sensor_dev_attr_in5_alarm.dev_attr.attr,
1965         &sensor_dev_attr_in5_beep.dev_attr.attr,        /* 29 */
1966
1967         &sensor_dev_attr_in6_input.dev_attr.attr,
1968         &sensor_dev_attr_in6_min.dev_attr.attr,
1969         &sensor_dev_attr_in6_max.dev_attr.attr,
1970         &sensor_dev_attr_in6_alarm.dev_attr.attr,
1971         &sensor_dev_attr_in6_beep.dev_attr.attr,        /* 34 */
1972
1973         &sensor_dev_attr_in7_input.dev_attr.attr,
1974         &sensor_dev_attr_in7_min.dev_attr.attr,
1975         &sensor_dev_attr_in7_max.dev_attr.attr,
1976         &sensor_dev_attr_in7_alarm.dev_attr.attr,
1977         &sensor_dev_attr_in7_beep.dev_attr.attr,        /* 39 */
1978
1979         &sensor_dev_attr_in8_input.dev_attr.attr,       /* 40 */
1980         &sensor_dev_attr_in9_input.dev_attr.attr,       /* 41 */
1981         &sensor_dev_attr_in10_input.dev_attr.attr,      /* 41 */
1982         &sensor_dev_attr_in11_input.dev_attr.attr,      /* 41 */
1983         &sensor_dev_attr_in12_input.dev_attr.attr,      /* 41 */
1984 };
1985
1986 static const struct attribute_group it87_group_in = {
1987         .attrs = it87_attributes_in,
1988         .is_visible = it87_in_is_visible,
1989 };
1990
1991 static umode_t it87_temp_is_visible(struct kobject *kobj,
1992                                     struct attribute *attr, int index)
1993 {
1994         struct device *dev = container_of(kobj, struct device, kobj);
1995         struct it87_data *data = dev_get_drvdata(dev);
1996         int i = index / 7;      /* temperature index */
1997         int a = index % 7;      /* attribute index */
1998
1999         if (index >= 21) {
2000                 i = index - 21 + 3;
2001                 a = 0;
2002         }
2003
2004         if (!(data->has_temp & BIT(i)))
2005                 return 0;
2006
2007         if (a == 5 && !has_temp_offset(data))
2008                 return 0;
2009
2010         if (a == 6 && !data->has_beep)
2011                 return 0;
2012
2013         return attr->mode;
2014 }
2015
2016 static struct attribute *it87_attributes_temp[] = {
2017         &sensor_dev_attr_temp1_input.dev_attr.attr,
2018         &sensor_dev_attr_temp1_max.dev_attr.attr,
2019         &sensor_dev_attr_temp1_min.dev_attr.attr,
2020         &sensor_dev_attr_temp1_type.dev_attr.attr,
2021         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2022         &sensor_dev_attr_temp1_offset.dev_attr.attr,    /* 5 */
2023         &sensor_dev_attr_temp1_beep.dev_attr.attr,      /* 6 */
2024
2025         &sensor_dev_attr_temp2_input.dev_attr.attr,     /* 7 */
2026         &sensor_dev_attr_temp2_max.dev_attr.attr,
2027         &sensor_dev_attr_temp2_min.dev_attr.attr,
2028         &sensor_dev_attr_temp2_type.dev_attr.attr,
2029         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2030         &sensor_dev_attr_temp2_offset.dev_attr.attr,
2031         &sensor_dev_attr_temp2_beep.dev_attr.attr,
2032
2033         &sensor_dev_attr_temp3_input.dev_attr.attr,     /* 14 */
2034         &sensor_dev_attr_temp3_max.dev_attr.attr,
2035         &sensor_dev_attr_temp3_min.dev_attr.attr,
2036         &sensor_dev_attr_temp3_type.dev_attr.attr,
2037         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2038         &sensor_dev_attr_temp3_offset.dev_attr.attr,
2039         &sensor_dev_attr_temp3_beep.dev_attr.attr,
2040
2041         &sensor_dev_attr_temp4_input.dev_attr.attr,     /* 21 */
2042         &sensor_dev_attr_temp5_input.dev_attr.attr,
2043         &sensor_dev_attr_temp6_input.dev_attr.attr,
2044         NULL
2045 };
2046
2047 static const struct attribute_group it87_group_temp = {
2048         .attrs = it87_attributes_temp,
2049         .is_visible = it87_temp_is_visible,
2050 };
2051
2052 static umode_t it87_is_visible(struct kobject *kobj,
2053                                struct attribute *attr, int index)
2054 {
2055         struct device *dev = container_of(kobj, struct device, kobj);
2056         struct it87_data *data = dev_get_drvdata(dev);
2057
2058         if ((index == 2 || index == 3) && !data->has_vid)
2059                 return 0;
2060
2061         if (index > 3 && !(data->in_internal & BIT(index - 4)))
2062                 return 0;
2063
2064         return attr->mode;
2065 }
2066
2067 static struct attribute *it87_attributes[] = {
2068         &dev_attr_alarms.attr,
2069         &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2070         &dev_attr_vrm.attr,                             /* 2 */
2071         &dev_attr_cpu0_vid.attr,                        /* 3 */
2072         &sensor_dev_attr_in3_label.dev_attr.attr,       /* 4 .. 7 */
2073         &sensor_dev_attr_in7_label.dev_attr.attr,
2074         &sensor_dev_attr_in8_label.dev_attr.attr,
2075         &sensor_dev_attr_in9_label.dev_attr.attr,
2076         NULL
2077 };
2078
2079 static const struct attribute_group it87_group = {
2080         .attrs = it87_attributes,
2081         .is_visible = it87_is_visible,
2082 };
2083
2084 static umode_t it87_fan_is_visible(struct kobject *kobj,
2085                                    struct attribute *attr, int index)
2086 {
2087         struct device *dev = container_of(kobj, struct device, kobj);
2088         struct it87_data *data = dev_get_drvdata(dev);
2089         int i = index / 5;      /* fan index */
2090         int a = index % 5;      /* attribute index */
2091
2092         if (index >= 15) {      /* fan 4..6 don't have divisor attributes */
2093                 i = (index - 15) / 4 + 3;
2094                 a = (index - 15) % 4;
2095         }
2096
2097         if (!(data->has_fan & BIT(i)))
2098                 return 0;
2099
2100         if (a == 3) {                           /* beep */
2101                 if (!data->has_beep)
2102                         return 0;
2103                 /* first fan beep attribute is writable */
2104                 if (i == __ffs(data->has_fan))
2105                         return attr->mode | S_IWUSR;
2106         }
2107
2108         if (a == 4 && has_16bit_fans(data))     /* divisor */
2109                 return 0;
2110
2111         return attr->mode;
2112 }
2113
2114 static struct attribute *it87_attributes_fan[] = {
2115         &sensor_dev_attr_fan1_input.dev_attr.attr,
2116         &sensor_dev_attr_fan1_min.dev_attr.attr,
2117         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2118         &sensor_dev_attr_fan1_beep.dev_attr.attr,       /* 3 */
2119         &sensor_dev_attr_fan1_div.dev_attr.attr,        /* 4 */
2120
2121         &sensor_dev_attr_fan2_input.dev_attr.attr,
2122         &sensor_dev_attr_fan2_min.dev_attr.attr,
2123         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2124         &sensor_dev_attr_fan2_beep.dev_attr.attr,
2125         &sensor_dev_attr_fan2_div.dev_attr.attr,        /* 9 */
2126
2127         &sensor_dev_attr_fan3_input.dev_attr.attr,
2128         &sensor_dev_attr_fan3_min.dev_attr.attr,
2129         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2130         &sensor_dev_attr_fan3_beep.dev_attr.attr,
2131         &sensor_dev_attr_fan3_div.dev_attr.attr,        /* 14 */
2132
2133         &sensor_dev_attr_fan4_input.dev_attr.attr,      /* 15 */
2134         &sensor_dev_attr_fan4_min.dev_attr.attr,
2135         &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2136         &sensor_dev_attr_fan4_beep.dev_attr.attr,
2137
2138         &sensor_dev_attr_fan5_input.dev_attr.attr,      /* 19 */
2139         &sensor_dev_attr_fan5_min.dev_attr.attr,
2140         &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2141         &sensor_dev_attr_fan5_beep.dev_attr.attr,
2142
2143         &sensor_dev_attr_fan6_input.dev_attr.attr,      /* 23 */
2144         &sensor_dev_attr_fan6_min.dev_attr.attr,
2145         &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2146         &sensor_dev_attr_fan6_beep.dev_attr.attr,
2147         NULL
2148 };
2149
2150 static const struct attribute_group it87_group_fan = {
2151         .attrs = it87_attributes_fan,
2152         .is_visible = it87_fan_is_visible,
2153 };
2154
2155 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2156                                    struct attribute *attr, int index)
2157 {
2158         struct device *dev = container_of(kobj, struct device, kobj);
2159         struct it87_data *data = dev_get_drvdata(dev);
2160         int i = index / 4;      /* pwm index */
2161         int a = index % 4;      /* attribute index */
2162
2163         if (!(data->has_pwm & BIT(i)))
2164                 return 0;
2165
2166         /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2167         if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2168                 return attr->mode | S_IWUSR;
2169
2170         /* pwm2_freq is writable if there are two pwm frequency selects */
2171         if (has_pwm_freq2(data) && i == 1 && a == 2)
2172                 return attr->mode | S_IWUSR;
2173
2174         return attr->mode;
2175 }
2176
2177 static struct attribute *it87_attributes_pwm[] = {
2178         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2179         &sensor_dev_attr_pwm1.dev_attr.attr,
2180         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2181         &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2182
2183         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2184         &sensor_dev_attr_pwm2.dev_attr.attr,
2185         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2186         &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2187
2188         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2189         &sensor_dev_attr_pwm3.dev_attr.attr,
2190         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2191         &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2192
2193         &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2194         &sensor_dev_attr_pwm4.dev_attr.attr,
2195         &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2196         &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2197
2198         &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2199         &sensor_dev_attr_pwm5.dev_attr.attr,
2200         &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2201         &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2202
2203         &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2204         &sensor_dev_attr_pwm6.dev_attr.attr,
2205         &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2206         &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2207
2208         NULL
2209 };
2210
2211 static const struct attribute_group it87_group_pwm = {
2212         .attrs = it87_attributes_pwm,
2213         .is_visible = it87_pwm_is_visible,
2214 };
2215
2216 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2217                                         struct attribute *attr, int index)
2218 {
2219         struct device *dev = container_of(kobj, struct device, kobj);
2220         struct it87_data *data = dev_get_drvdata(dev);
2221         int i = index / 11;     /* pwm index */
2222         int a = index % 11;     /* attribute index */
2223
2224         if (index >= 33) {      /* pwm 4..6 */
2225                 i = (index - 33) / 6 + 3;
2226                 a = (index - 33) % 6 + 4;
2227         }
2228
2229         if (!(data->has_pwm & BIT(i)))
2230                 return 0;
2231
2232         if (has_newer_autopwm(data)) {
2233                 if (a < 4)      /* no auto point pwm */
2234                         return 0;
2235                 if (a == 8)     /* no auto_point4 */
2236                         return 0;
2237         }
2238         if (has_old_autopwm(data)) {
2239                 if (a >= 9)     /* no pwm_auto_start, pwm_auto_slope */
2240                         return 0;
2241         }
2242
2243         return attr->mode;
2244 }
2245
2246 static struct attribute *it87_attributes_auto_pwm[] = {
2247         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2248         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2249         &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2250         &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2251         &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2252         &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2253         &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2254         &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2255         &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2256         &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2257         &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2258
2259         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,    /* 11 */
2260         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2261         &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2262         &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2263         &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2264         &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2265         &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2266         &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2267         &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2268         &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2269         &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2270
2271         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,    /* 22 */
2272         &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2273         &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2274         &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2275         &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2276         &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2277         &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2278         &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2279         &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2280         &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2281         &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2282
2283         &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,   /* 33 */
2284         &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2285         &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2286         &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2287         &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2288         &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2289
2290         &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2291         &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2292         &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2293         &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2294         &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2295         &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2296
2297         &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2298         &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2299         &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2300         &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2301         &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2302         &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2303
2304         NULL,
2305 };
2306
2307 static const struct attribute_group it87_group_auto_pwm = {
2308         .attrs = it87_attributes_auto_pwm,
2309         .is_visible = it87_auto_pwm_is_visible,
2310 };
2311
2312 /* SuperIO detection - will change isa_address if a chip is found */
2313 static int __init it87_find(int sioaddr, unsigned short *address,
2314                             struct it87_sio_data *sio_data)
2315 {
2316         int err;
2317         u16 chip_type;
2318         const char *board_vendor, *board_name;
2319         const struct it87_devices *config;
2320
2321         err = superio_enter(sioaddr);
2322         if (err)
2323                 return err;
2324
2325         err = -ENODEV;
2326         chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
2327
2328         switch (chip_type) {
2329         case IT8705F_DEVID:
2330                 sio_data->type = it87;
2331                 break;
2332         case IT8712F_DEVID:
2333                 sio_data->type = it8712;
2334                 break;
2335         case IT8716F_DEVID:
2336         case IT8726F_DEVID:
2337                 sio_data->type = it8716;
2338                 break;
2339         case IT8718F_DEVID:
2340                 sio_data->type = it8718;
2341                 break;
2342         case IT8720F_DEVID:
2343                 sio_data->type = it8720;
2344                 break;
2345         case IT8721F_DEVID:
2346                 sio_data->type = it8721;
2347                 break;
2348         case IT8728F_DEVID:
2349                 sio_data->type = it8728;
2350                 break;
2351         case IT8771E_DEVID:
2352                 sio_data->type = it8771;
2353                 break;
2354         case IT8772E_DEVID:
2355                 sio_data->type = it8772;
2356                 break;
2357         case IT8781F_DEVID:
2358                 sio_data->type = it8781;
2359                 break;
2360         case IT8782F_DEVID:
2361                 sio_data->type = it8782;
2362                 break;
2363         case IT8783E_DEVID:
2364                 sio_data->type = it8783;
2365                 break;
2366         case IT8786E_DEVID:
2367                 sio_data->type = it8786;
2368                 break;
2369         case IT8790E_DEVID:
2370                 sio_data->type = it8790;
2371                 break;
2372         case IT8603E_DEVID:
2373         case IT8623E_DEVID:
2374                 sio_data->type = it8603;
2375                 break;
2376         case IT8620E_DEVID:
2377                 sio_data->type = it8620;
2378                 break;
2379         case 0xffff:    /* No device at all */
2380                 goto exit;
2381         default:
2382                 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
2383                 goto exit;
2384         }
2385
2386         superio_select(sioaddr, PME);
2387         if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
2388                 pr_info("Device not activated, skipping\n");
2389                 goto exit;
2390         }
2391
2392         *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
2393         if (*address == 0) {
2394                 pr_info("Base address not set, skipping\n");
2395                 goto exit;
2396         }
2397
2398         err = 0;
2399         sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
2400         pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2401                 it87_devices[sio_data->type].suffix,
2402                 *address, sio_data->revision);
2403
2404         config = &it87_devices[sio_data->type];
2405
2406         /* in7 (VSB or VCCH5V) is always internal on some chips */
2407         if (has_in7_internal(config))
2408                 sio_data->internal |= BIT(1);
2409
2410         /* in8 (Vbat) is always internal */
2411         sio_data->internal |= BIT(2);
2412
2413         /* in9 (AVCC3), always internal if supported */
2414         if (has_avcc3(config))
2415                 sio_data->internal |= BIT(3); /* in9 is AVCC */
2416         else
2417                 sio_data->skip_in |= BIT(9);
2418
2419         if (!has_six_pwm(config))
2420                 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
2421
2422         if (!has_vid(config))
2423                 sio_data->skip_vid = 1;
2424
2425         /* Read GPIO config and VID value from LDN 7 (GPIO) */
2426         if (sio_data->type == it87) {
2427                 /* The IT8705F has a different LD number for GPIO */
2428                 superio_select(sioaddr, 5);
2429                 sio_data->beep_pin = superio_inb(sioaddr,
2430                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
2431         } else if (sio_data->type == it8783) {
2432                 int reg25, reg27, reg2a, reg2c, regef;
2433
2434                 superio_select(sioaddr, GPIO);
2435
2436                 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2437                 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2438                 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2439                 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2440                 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
2441
2442                 /* Check if fan3 is there or not */
2443                 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2444                         sio_data->skip_fan |= BIT(2);
2445                 if ((reg25 & BIT(4)) ||
2446                     (!(reg2a & BIT(1)) && (regef & BIT(0))))
2447                         sio_data->skip_pwm |= BIT(2);
2448
2449                 /* Check if fan2 is there or not */
2450                 if (reg27 & BIT(7))
2451                         sio_data->skip_fan |= BIT(1);
2452                 if (reg27 & BIT(3))
2453                         sio_data->skip_pwm |= BIT(1);
2454
2455                 /* VIN5 */
2456                 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2457                         sio_data->skip_in |= BIT(5); /* No VIN5 */
2458
2459                 /* VIN6 */
2460                 if (reg27 & BIT(1))
2461                         sio_data->skip_in |= BIT(6); /* No VIN6 */
2462
2463                 /*
2464                  * VIN7
2465                  * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2466                  */
2467                 if (reg27 & BIT(2)) {
2468                         /*
2469                          * The data sheet is a bit unclear regarding the
2470                          * internal voltage divider for VCCH5V. It says
2471                          * "This bit enables and switches VIN7 (pin 91) to the
2472                          * internal voltage divider for VCCH5V".
2473                          * This is different to other chips, where the internal
2474                          * voltage divider would connect VIN7 to an internal
2475                          * voltage source. Maybe that is the case here as well.
2476                          *
2477                          * Since we don't know for sure, re-route it if that is
2478                          * not the case, and ask the user to report if the
2479                          * resulting voltage is sane.
2480                          */
2481                         if (!(reg2c & BIT(1))) {
2482                                 reg2c |= BIT(1);
2483                                 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2484                                              reg2c);
2485                                 pr_notice("Routing internal VCCH5V to in7.\n");
2486                         }
2487                         pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2488                         pr_notice("Please report if it displays a reasonable voltage.\n");
2489                 }
2490
2491                 if (reg2c & BIT(0))
2492                         sio_data->internal |= BIT(0);
2493                 if (reg2c & BIT(1))
2494                         sio_data->internal |= BIT(1);
2495
2496                 sio_data->beep_pin = superio_inb(sioaddr,
2497                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
2498         } else if (sio_data->type == it8603) {
2499                 int reg27, reg29;
2500
2501                 superio_select(sioaddr, GPIO);
2502
2503                 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2504
2505                 /* Check if fan3 is there or not */
2506                 if (reg27 & BIT(6))
2507                         sio_data->skip_pwm |= BIT(2);
2508                 if (reg27 & BIT(7))
2509                         sio_data->skip_fan |= BIT(2);
2510
2511                 /* Check if fan2 is there or not */
2512                 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2513                 if (reg29 & BIT(1))
2514                         sio_data->skip_pwm |= BIT(1);
2515                 if (reg29 & BIT(2))
2516                         sio_data->skip_fan |= BIT(1);
2517
2518                 sio_data->skip_in |= BIT(5); /* No VIN5 */
2519                 sio_data->skip_in |= BIT(6); /* No VIN6 */
2520
2521                 sio_data->beep_pin = superio_inb(sioaddr,
2522                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
2523         } else if (sio_data->type == it8620) {
2524                 int reg;
2525
2526                 superio_select(sioaddr, GPIO);
2527
2528                 /* Check for pwm5 */
2529                 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2530                 if (reg & BIT(6))
2531                         sio_data->skip_pwm |= BIT(4);
2532
2533                 /* Check for fan4, fan5 */
2534                 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2535                 if (!(reg & BIT(5)))
2536                         sio_data->skip_fan |= BIT(3);
2537                 if (!(reg & BIT(4)))
2538                         sio_data->skip_fan |= BIT(4);
2539
2540                 /* Check for pwm3, fan3 */
2541                 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2542                 if (reg & BIT(6))
2543                         sio_data->skip_pwm |= BIT(2);
2544                 if (reg & BIT(7))
2545                         sio_data->skip_fan |= BIT(2);
2546
2547                 /* Check for pwm4 */
2548                 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
2549                 if (!(reg & BIT(4)))
2550                         sio_data->skip_pwm |= BIT(3);
2551
2552                 /* Check for pwm2, fan2 */
2553                 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2554                 if (reg & BIT(1))
2555                         sio_data->skip_pwm |= BIT(1);
2556                 if (reg & BIT(2))
2557                         sio_data->skip_fan |= BIT(1);
2558                 /* Check for pwm6, fan6 */
2559                 if (!(reg & BIT(7))) {
2560                         sio_data->skip_pwm |= BIT(5);
2561                         sio_data->skip_fan |= BIT(5);
2562                 }
2563
2564                 sio_data->beep_pin = superio_inb(sioaddr,
2565                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
2566         } else {
2567                 int reg;
2568                 bool uart6;
2569
2570                 superio_select(sioaddr, GPIO);
2571
2572                 /* Check for fan4, fan5 */
2573                 if (has_five_fans(config)) {
2574                         reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2575                         switch (sio_data->type) {
2576                         case it8718:
2577                                 if (reg & BIT(5))
2578                                         sio_data->skip_fan |= BIT(3);
2579                                 if (reg & BIT(4))
2580                                         sio_data->skip_fan |= BIT(4);
2581                                 break;
2582                         case it8720:
2583                         case it8721:
2584                         case it8728:
2585                                 if (!(reg & BIT(5)))
2586                                         sio_data->skip_fan |= BIT(3);
2587                                 if (!(reg & BIT(4)))
2588                                         sio_data->skip_fan |= BIT(4);
2589                                 break;
2590                         default:
2591                                 break;
2592                         }
2593                 }
2594
2595                 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2596                 if (!sio_data->skip_vid) {
2597                         /* We need at least 4 VID pins */
2598                         if (reg & 0x0f) {
2599                                 pr_info("VID is disabled (pins used for GPIO)\n");
2600                                 sio_data->skip_vid = 1;
2601                         }
2602                 }
2603
2604                 /* Check if fan3 is there or not */
2605                 if (reg & BIT(6))
2606                         sio_data->skip_pwm |= BIT(2);
2607                 if (reg & BIT(7))
2608                         sio_data->skip_fan |= BIT(2);
2609
2610                 /* Check if fan2 is there or not */
2611                 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2612                 if (reg & BIT(1))
2613                         sio_data->skip_pwm |= BIT(1);
2614                 if (reg & BIT(2))
2615                         sio_data->skip_fan |= BIT(1);
2616
2617                 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2618                     !(sio_data->skip_vid))
2619                         sio_data->vid_value = superio_inb(sioaddr,
2620                                                           IT87_SIO_VID_REG);
2621
2622                 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2623
2624                 uart6 = sio_data->type == it8782 && (reg & BIT(2));
2625
2626                 /*
2627                  * The IT8720F has no VIN7 pin, so VCCH should always be
2628                  * routed internally to VIN7 with an internal divider.
2629                  * Curiously, there still is a configuration bit to control
2630                  * this, which means it can be set incorrectly. And even
2631                  * more curiously, many boards out there are improperly
2632                  * configured, even though the IT8720F datasheet claims
2633                  * that the internal routing of VCCH to VIN7 is the default
2634                  * setting. So we force the internal routing in this case.
2635                  *
2636                  * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2637                  * If UART6 is enabled, re-route VIN7 to the internal divider
2638                  * if that is not already the case.
2639                  */
2640                 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2641                         reg |= BIT(1);
2642                         superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
2643                         pr_notice("Routing internal VCCH to in7\n");
2644                 }
2645                 if (reg & BIT(0))
2646                         sio_data->internal |= BIT(0);
2647                 if (reg & BIT(1))
2648                         sio_data->internal |= BIT(1);
2649
2650                 /*
2651                  * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2652                  * While VIN7 can be routed to the internal voltage divider,
2653                  * VIN5 and VIN6 are not available if UART6 is enabled.
2654                  *
2655                  * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2656                  * is the temperature source. Since we can not read the
2657                  * temperature source here, skip_temp is preliminary.
2658                  */
2659                 if (uart6) {
2660                         sio_data->skip_in |= BIT(5) | BIT(6);
2661                         sio_data->skip_temp |= BIT(2);
2662                 }
2663
2664                 sio_data->beep_pin = superio_inb(sioaddr,
2665                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
2666         }
2667         if (sio_data->beep_pin)
2668                 pr_info("Beeping is supported\n");
2669
2670         /* Disable specific features based on DMI strings */
2671         board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2672         board_name = dmi_get_system_info(DMI_BOARD_NAME);
2673         if (board_vendor && board_name) {
2674                 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2675                     strcmp(board_name, "FN68PT") == 0) {
2676                         /*
2677                          * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2678                          * connected to a fan, but to something else. One user
2679                          * has reported instant system power-off when changing
2680                          * the PWM2 duty cycle, so we disable it.
2681                          * I use the board name string as the trigger in case
2682                          * the same board is ever used in other systems.
2683                          */
2684                         pr_info("Disabling pwm2 due to hardware constraints\n");
2685                         sio_data->skip_pwm = BIT(1);
2686                 }
2687         }
2688
2689 exit:
2690         superio_exit(sioaddr);
2691         return err;
2692 }
2693
2694 /* Called when we have found a new IT87. */
2695 static void it87_init_device(struct platform_device *pdev)
2696 {
2697         struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2698         struct it87_data *data = platform_get_drvdata(pdev);
2699         int tmp, i;
2700         u8 mask;
2701
2702         /*
2703          * For each PWM channel:
2704          * - If it is in automatic mode, setting to manual mode should set
2705          *   the fan to full speed by default.
2706          * - If it is in manual mode, we need a mapping to temperature
2707          *   channels to use when later setting to automatic mode later.
2708          *   Use a 1:1 mapping by default (we are clueless.)
2709          * In both cases, the value can (and should) be changed by the user
2710          * prior to switching to a different mode.
2711          * Note that this is no longer needed for the IT8721F and later, as
2712          * these have separate registers for the temperature mapping and the
2713          * manual duty cycle.
2714          */
2715         for (i = 0; i < NUM_AUTO_PWM; i++) {
2716                 data->pwm_temp_map[i] = i;
2717                 data->pwm_duty[i] = 0x7f;       /* Full speed */
2718                 data->auto_pwm[i][3] = 0x7f;    /* Full speed, hard-coded */
2719         }
2720
2721         /*
2722          * Some chips seem to have default value 0xff for all limit
2723          * registers. For low voltage limits it makes no sense and triggers
2724          * alarms, so change to 0 instead. For high temperature limits, it
2725          * means -1 degree C, which surprisingly doesn't trigger an alarm,
2726          * but is still confusing, so change to 127 degrees C.
2727          */
2728         for (i = 0; i < NUM_VIN_LIMIT; i++) {
2729                 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2730                 if (tmp == 0xff)
2731                         it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2732         }
2733         for (i = 0; i < NUM_TEMP_LIMIT; i++) {
2734                 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2735                 if (tmp == 0xff)
2736                         it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2737         }
2738
2739         /*
2740          * Temperature channels are not forcibly enabled, as they can be
2741          * set to two different sensor types and we can't guess which one
2742          * is correct for a given system. These channels can be enabled at
2743          * run-time through the temp{1-3}_type sysfs accessors if needed.
2744          */
2745
2746         /* Check if voltage monitors are reset manually or by some reason */
2747         tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2748         if ((tmp & 0xff) == 0) {
2749                 /* Enable all voltage monitors */
2750                 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2751         }
2752
2753         /* Check if tachometers are reset manually or by some reason */
2754         mask = 0x70 & ~(sio_data->skip_fan << 4);
2755         data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2756         if ((data->fan_main_ctrl & mask) == 0) {
2757                 /* Enable all fan tachometers */
2758                 data->fan_main_ctrl |= mask;
2759                 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2760                                  data->fan_main_ctrl);
2761         }
2762         data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2763
2764         tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2765
2766         /* Set tachometers to 16-bit mode if needed */
2767         if (has_fan16_config(data)) {
2768                 if (~tmp & 0x07 & data->has_fan) {
2769                         dev_dbg(&pdev->dev,
2770                                 "Setting fan1-3 to 16-bit mode\n");
2771                         it87_write_value(data, IT87_REG_FAN_16BIT,
2772                                          tmp | 0x07);
2773                 }
2774         }
2775
2776         /* Check for additional fans */
2777         if (has_five_fans(data)) {
2778                 if (tmp & BIT(4))
2779                         data->has_fan |= BIT(3); /* fan4 enabled */
2780                 if (tmp & BIT(5))
2781                         data->has_fan |= BIT(4); /* fan5 enabled */
2782                 if (has_six_fans(data) && (tmp & BIT(2)))
2783                         data->has_fan |= BIT(5); /* fan6 enabled */
2784         }
2785
2786         /* Fan input pins may be used for alternative functions */
2787         data->has_fan &= ~sio_data->skip_fan;
2788
2789         /* Check if pwm5, pwm6 are enabled */
2790         if (has_six_pwm(data)) {
2791                 /* The following code may be IT8620E specific */
2792                 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2793                 if ((tmp & 0xc0) == 0xc0)
2794                         sio_data->skip_pwm |= BIT(4);
2795                 if (!(tmp & BIT(3)))
2796                         sio_data->skip_pwm |= BIT(5);
2797         }
2798
2799         /* Start monitoring */
2800         it87_write_value(data, IT87_REG_CONFIG,
2801                          (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2802                          | (update_vbat ? 0x41 : 0x01));
2803 }
2804
2805 /* Return 1 if and only if the PWM interface is safe to use */
2806 static int it87_check_pwm(struct device *dev)
2807 {
2808         struct it87_data *data = dev_get_drvdata(dev);
2809         /*
2810          * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2811          * and polarity set to active low is sign that this is the case so we
2812          * disable pwm control to protect the user.
2813          */
2814         int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2815
2816         if ((tmp & 0x87) == 0) {
2817                 if (fix_pwm_polarity) {
2818                         /*
2819                          * The user asks us to attempt a chip reconfiguration.
2820                          * This means switching to active high polarity and
2821                          * inverting all fan speed values.
2822                          */
2823                         int i;
2824                         u8 pwm[3];
2825
2826                         for (i = 0; i < ARRAY_SIZE(pwm); i++)
2827                                 pwm[i] = it87_read_value(data,
2828                                                          IT87_REG_PWM[i]);
2829
2830                         /*
2831                          * If any fan is in automatic pwm mode, the polarity
2832                          * might be correct, as suspicious as it seems, so we
2833                          * better don't change anything (but still disable the
2834                          * PWM interface).
2835                          */
2836                         if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2837                                 dev_info(dev,
2838                                          "Reconfiguring PWM to active high polarity\n");
2839                                 it87_write_value(data, IT87_REG_FAN_CTL,
2840                                                  tmp | 0x87);
2841                                 for (i = 0; i < 3; i++)
2842                                         it87_write_value(data,
2843                                                          IT87_REG_PWM[i],
2844                                                          0x7f & ~pwm[i]);
2845                                 return 1;
2846                         }
2847
2848                         dev_info(dev,
2849                                  "PWM configuration is too broken to be fixed\n");
2850                 }
2851
2852                 dev_info(dev,
2853                          "Detected broken BIOS defaults, disabling PWM interface\n");
2854                 return 0;
2855         } else if (fix_pwm_polarity) {
2856                 dev_info(dev,
2857                          "PWM configuration looks sane, won't touch\n");
2858         }
2859
2860         return 1;
2861 }
2862
2863 static int it87_probe(struct platform_device *pdev)
2864 {
2865         struct it87_data *data;
2866         struct resource *res;
2867         struct device *dev = &pdev->dev;
2868         struct it87_sio_data *sio_data = dev_get_platdata(dev);
2869         int enable_pwm_interface;
2870         struct device *hwmon_dev;
2871
2872         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2873         if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2874                                  DRVNAME)) {
2875                 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2876                         (unsigned long)res->start,
2877                         (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2878                 return -EBUSY;
2879         }
2880
2881         data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2882         if (!data)
2883                 return -ENOMEM;
2884
2885         data->addr = res->start;
2886         data->type = sio_data->type;
2887         data->features = it87_devices[sio_data->type].features;
2888         data->peci_mask = it87_devices[sio_data->type].peci_mask;
2889         data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
2890         /*
2891          * IT8705F Datasheet 0.4.1, 3h == Version G.
2892          * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2893          * These are the first revisions with 16-bit tachometer support.
2894          */
2895         switch (data->type) {
2896         case it87:
2897                 if (sio_data->revision >= 0x03) {
2898                         data->features &= ~FEAT_OLD_AUTOPWM;
2899                         data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
2900                 }
2901                 break;
2902         case it8712:
2903                 if (sio_data->revision >= 0x08) {
2904                         data->features &= ~FEAT_OLD_AUTOPWM;
2905                         data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2906                                           FEAT_FIVE_FANS;
2907                 }
2908                 break;
2909         default:
2910                 break;
2911         }
2912
2913         /* Now, we do the remaining detection. */
2914         if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
2915             it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2916                 return -ENODEV;
2917
2918         platform_set_drvdata(pdev, data);
2919
2920         mutex_init(&data->update_lock);
2921
2922         /* Check PWM configuration */
2923         enable_pwm_interface = it87_check_pwm(dev);
2924
2925         /* Starting with IT8721F, we handle scaling of internal voltages */
2926         if (has_12mv_adc(data)) {
2927                 if (sio_data->internal & BIT(0))
2928                         data->in_scaled |= BIT(3);      /* in3 is AVCC */
2929                 if (sio_data->internal & BIT(1))
2930                         data->in_scaled |= BIT(7);      /* in7 is VSB */
2931                 if (sio_data->internal & BIT(2))
2932                         data->in_scaled |= BIT(8);      /* in8 is Vbat */
2933                 if (sio_data->internal & BIT(3))
2934                         data->in_scaled |= BIT(9);      /* in9 is AVCC */
2935         } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2936                    sio_data->type == it8783) {
2937                 if (sio_data->internal & BIT(0))
2938                         data->in_scaled |= BIT(3);      /* in3 is VCC5V */
2939                 if (sio_data->internal & BIT(1))
2940                         data->in_scaled |= BIT(7);      /* in7 is VCCH5V */
2941         }
2942
2943         data->has_temp = 0x07;
2944         if (sio_data->skip_temp & BIT(2)) {
2945                 if (sio_data->type == it8782 &&
2946                     !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2947                         data->has_temp &= ~BIT(2);
2948         }
2949
2950         data->in_internal = sio_data->internal;
2951         data->has_in = 0x3ff & ~sio_data->skip_in;
2952
2953         if (has_six_temp(data)) {
2954                 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
2955
2956                 /* Check for additional temperature sensors */
2957                 if ((reg & 0x03) >= 0x02)
2958                         data->has_temp |= BIT(3);
2959                 if (((reg >> 2) & 0x03) >= 0x02)
2960                         data->has_temp |= BIT(4);
2961                 if (((reg >> 4) & 0x03) >= 0x02)
2962                         data->has_temp |= BIT(5);
2963
2964                 /* Check for additional voltage sensors */
2965                 if ((reg & 0x03) == 0x01)
2966                         data->has_in |= BIT(10);
2967                 if (((reg >> 2) & 0x03) == 0x01)
2968                         data->has_in |= BIT(11);
2969                 if (((reg >> 4) & 0x03) == 0x01)
2970                         data->has_in |= BIT(12);
2971         }
2972
2973         data->has_beep = !!sio_data->beep_pin;
2974
2975         /* Initialize the IT87 chip */
2976         it87_init_device(pdev);
2977
2978         if (!sio_data->skip_vid) {
2979                 data->has_vid = true;
2980                 data->vrm = vid_which_vrm();
2981                 /* VID reading from Super-I/O config space if available */
2982                 data->vid = sio_data->vid_value;
2983         }
2984
2985         /* Prepare for sysfs hooks */
2986         data->groups[0] = &it87_group;
2987         data->groups[1] = &it87_group_in;
2988         data->groups[2] = &it87_group_temp;
2989         data->groups[3] = &it87_group_fan;
2990
2991         if (enable_pwm_interface) {
2992                 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
2993                 data->has_pwm &= ~sio_data->skip_pwm;
2994
2995                 data->groups[4] = &it87_group_pwm;
2996                 if (has_old_autopwm(data) || has_newer_autopwm(data))
2997                         data->groups[5] = &it87_group_auto_pwm;
2998         }
2999
3000         hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3001                                         it87_devices[sio_data->type].name,
3002                                         data, data->groups);
3003         return PTR_ERR_OR_ZERO(hwmon_dev);
3004 }
3005
3006 static struct platform_driver it87_driver = {
3007         .driver = {
3008                 .name   = DRVNAME,
3009         },
3010         .probe  = it87_probe,
3011 };
3012
3013 static int __init it87_device_add(int index, unsigned short address,
3014                                   const struct it87_sio_data *sio_data)
3015 {
3016         struct platform_device *pdev;
3017         struct resource res = {
3018                 .start  = address + IT87_EC_OFFSET,
3019                 .end    = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
3020                 .name   = DRVNAME,
3021                 .flags  = IORESOURCE_IO,
3022         };
3023         int err;
3024
3025         err = acpi_check_resource_conflict(&res);
3026         if (err)
3027                 return err;
3028
3029         pdev = platform_device_alloc(DRVNAME, address);
3030         if (!pdev)
3031                 return -ENOMEM;
3032
3033         err = platform_device_add_resources(pdev, &res, 1);
3034         if (err) {
3035                 pr_err("Device resource addition failed (%d)\n", err);
3036                 goto exit_device_put;
3037         }
3038
3039         err = platform_device_add_data(pdev, sio_data,
3040                                        sizeof(struct it87_sio_data));
3041         if (err) {
3042                 pr_err("Platform data allocation failed\n");
3043                 goto exit_device_put;
3044         }
3045
3046         err = platform_device_add(pdev);
3047         if (err) {
3048                 pr_err("Device addition failed (%d)\n", err);
3049                 goto exit_device_put;
3050         }
3051
3052         it87_pdev[index] = pdev;
3053         return 0;
3054
3055 exit_device_put:
3056         platform_device_put(pdev);
3057         return err;
3058 }
3059
3060 static int __init sm_it87_init(void)
3061 {
3062         int sioaddr[2] = { REG_2E, REG_4E };
3063         struct it87_sio_data sio_data;
3064         unsigned short isa_address;
3065         bool found = false;
3066         int i, err;
3067
3068         err = platform_driver_register(&it87_driver);
3069         if (err)
3070                 return err;
3071
3072         for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3073                 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3074                 isa_address = 0;
3075                 err = it87_find(sioaddr[i], &isa_address, &sio_data);
3076                 if (err || isa_address == 0)
3077                         continue;
3078
3079                 err = it87_device_add(i, isa_address, &sio_data);
3080                 if (err)
3081                         goto exit_dev_unregister;
3082                 found = true;
3083         }
3084
3085         if (!found) {
3086                 err = -ENODEV;
3087                 goto exit_unregister;
3088         }
3089         return 0;
3090
3091 exit_dev_unregister:
3092         /* NULL check handled by platform_device_unregister */
3093         platform_device_unregister(it87_pdev[0]);
3094 exit_unregister:
3095         platform_driver_unregister(&it87_driver);
3096         return err;
3097 }
3098
3099 static void __exit sm_it87_exit(void)
3100 {
3101         /* NULL check handled by platform_device_unregister */
3102         platform_device_unregister(it87_pdev[1]);
3103         platform_device_unregister(it87_pdev[0]);
3104         platform_driver_unregister(&it87_driver);
3105 }
3106
3107 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
3108 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
3109 module_param(update_vbat, bool, 0);
3110 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3111 module_param(fix_pwm_polarity, bool, 0);
3112 MODULE_PARM_DESC(fix_pwm_polarity,
3113                  "Force PWM polarity to active high (DANGEROUS)");
3114 MODULE_LICENSE("GPL");
3115
3116 module_init(sm_it87_init);
3117 module_exit(sm_it87_exit);