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