]> git.sur5r.net Git - groeck-it87/blob - it87.c
Synchronize with upstream version
[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         u32 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         if ((data->type == it8603 || nr >= 3) &&
1082             data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1083                 return 0;                       /* Full speed */
1084
1085         return 1;                               /* Manual mode */
1086 }
1087
1088 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1089                         char *buf)
1090 {
1091         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1092         int nr = sattr->nr;
1093         int index = sattr->index;
1094         int speed;
1095         struct it87_data *data = it87_update_device(dev);
1096
1097         speed = has_16bit_fans(data) ?
1098                 FAN16_FROM_REG(data->fan[nr][index]) :
1099                 FAN_FROM_REG(data->fan[nr][index],
1100                              DIV_FROM_REG(data->fan_div[nr]));
1101         return sprintf(buf, "%d\n", speed);
1102 }
1103
1104 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1105                             char *buf)
1106 {
1107         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1108         struct it87_data *data = it87_update_device(dev);
1109         int nr = sensor_attr->index;
1110
1111         return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1112 }
1113
1114 static ssize_t show_pwm_enable(struct device *dev,
1115                                struct device_attribute *attr, char *buf)
1116 {
1117         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1118         struct it87_data *data = it87_update_device(dev);
1119         int nr = sensor_attr->index;
1120
1121         return sprintf(buf, "%d\n", pwm_mode(data, nr));
1122 }
1123
1124 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1125                         char *buf)
1126 {
1127         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1128         struct it87_data *data = it87_update_device(dev);
1129         int nr = sensor_attr->index;
1130
1131         return sprintf(buf, "%d\n",
1132                        pwm_from_reg(data, data->pwm_duty[nr]));
1133 }
1134
1135 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1136                              char *buf)
1137 {
1138         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1139         struct it87_data *data = it87_update_device(dev);
1140         int nr = sensor_attr->index;
1141         unsigned int freq;
1142         int index;
1143
1144         if (has_pwm_freq2(data) && nr == 1)
1145                 index = (data->extra >> 4) & 0x07;
1146         else
1147                 index = (data->fan_ctl >> 4) & 0x07;
1148
1149         freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1150
1151         return sprintf(buf, "%u\n", freq);
1152 }
1153
1154 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1155                        const char *buf, size_t count)
1156 {
1157         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1158         int nr = sattr->nr;
1159         int index = sattr->index;
1160
1161         struct it87_data *data = dev_get_drvdata(dev);
1162         long val;
1163         u8 reg;
1164
1165         if (kstrtol(buf, 10, &val) < 0)
1166                 return -EINVAL;
1167
1168         mutex_lock(&data->update_lock);
1169
1170         if (has_16bit_fans(data)) {
1171                 data->fan[nr][index] = FAN16_TO_REG(val);
1172                 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1173                                  data->fan[nr][index] & 0xff);
1174                 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1175                                  data->fan[nr][index] >> 8);
1176         } else {
1177                 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1178                 switch (nr) {
1179                 case 0:
1180                         data->fan_div[nr] = reg & 0x07;
1181                         break;
1182                 case 1:
1183                         data->fan_div[nr] = (reg >> 3) & 0x07;
1184                         break;
1185                 case 2:
1186                         data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1187                         break;
1188                 }
1189                 data->fan[nr][index] =
1190                   FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1191                 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1192                                  data->fan[nr][index]);
1193         }
1194
1195         mutex_unlock(&data->update_lock);
1196         return count;
1197 }
1198
1199 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1200                            const char *buf, size_t count)
1201 {
1202         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1203         struct it87_data *data = dev_get_drvdata(dev);
1204         int nr = sensor_attr->index;
1205         unsigned long val;
1206         int min;
1207         u8 old;
1208
1209         if (kstrtoul(buf, 10, &val) < 0)
1210                 return -EINVAL;
1211
1212         mutex_lock(&data->update_lock);
1213         old = it87_read_value(data, IT87_REG_FAN_DIV);
1214
1215         /* Save fan min limit */
1216         min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1217
1218         switch (nr) {
1219         case 0:
1220         case 1:
1221                 data->fan_div[nr] = DIV_TO_REG(val);
1222                 break;
1223         case 2:
1224                 if (val < 8)
1225                         data->fan_div[nr] = 1;
1226                 else
1227                         data->fan_div[nr] = 3;
1228         }
1229         val = old & 0x80;
1230         val |= (data->fan_div[0] & 0x07);
1231         val |= (data->fan_div[1] & 0x07) << 3;
1232         if (data->fan_div[2] == 3)
1233                 val |= 0x1 << 6;
1234         it87_write_value(data, IT87_REG_FAN_DIV, val);
1235
1236         /* Restore fan min limit */
1237         data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1238         it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
1239
1240         mutex_unlock(&data->update_lock);
1241         return count;
1242 }
1243
1244 /* Returns 0 if OK, -EINVAL otherwise */
1245 static int check_trip_points(struct device *dev, int nr)
1246 {
1247         const struct it87_data *data = dev_get_drvdata(dev);
1248         int i, err = 0;
1249
1250         if (has_old_autopwm(data)) {
1251                 for (i = 0; i < 3; i++) {
1252                         if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1253                                 err = -EINVAL;
1254                 }
1255                 for (i = 0; i < 2; i++) {
1256                         if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1257                                 err = -EINVAL;
1258                 }
1259         } else if (has_newer_autopwm(data)) {
1260                 for (i = 1; i < 3; i++) {
1261                         if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1262                                 err = -EINVAL;
1263                 }
1264         }
1265
1266         if (err) {
1267                 dev_err(dev,
1268                         "Inconsistent trip points, not switching to automatic mode\n");
1269                 dev_err(dev, "Adjust the trip points and try again\n");
1270         }
1271         return err;
1272 }
1273
1274 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1275                               const char *buf, size_t count)
1276 {
1277         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1278         struct it87_data *data = dev_get_drvdata(dev);
1279         int nr = sensor_attr->index;
1280         long val;
1281
1282         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1283                 return -EINVAL;
1284
1285         /* Check trip points before switching to automatic mode */
1286         if (val == 2) {
1287                 if (check_trip_points(dev, nr) < 0)
1288                         return -EINVAL;
1289         }
1290
1291         mutex_lock(&data->update_lock);
1292
1293         if (val == 0) {
1294                 if (nr < 3 && data->type != it8603) {
1295                         int tmp;
1296                         /* make sure the fan is on when in on/off mode */
1297                         tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1298                         it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1299                         /* set on/off mode */
1300                         data->fan_main_ctrl &= ~BIT(nr);
1301                         it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1302                                          data->fan_main_ctrl);
1303                 } else {
1304                         /* No on/off mode, set maximum pwm value */
1305                         data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1306                         it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1307                                          data->pwm_duty[nr]);
1308                         /* and set manual mode */
1309                         data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1310                                              data->pwm_temp_map[nr] :
1311                                              data->pwm_duty[nr];
1312                         it87_write_value(data, IT87_REG_PWM[nr],
1313                                          data->pwm_ctrl[nr]);
1314                 }
1315         } else {
1316                 if (val == 1)                           /* Manual mode */
1317                         data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1318                                              data->pwm_temp_map[nr] :
1319                                              data->pwm_duty[nr];
1320                 else                                    /* Automatic mode */
1321                         data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1322                 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1323
1324                 if (data->type != it8603 && nr < 3) {
1325                         /* set SmartGuardian mode */
1326                         data->fan_main_ctrl |= BIT(nr);
1327                         it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1328                                          data->fan_main_ctrl);
1329                 }
1330         }
1331
1332         mutex_unlock(&data->update_lock);
1333         return count;
1334 }
1335
1336 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1337                        const char *buf, size_t count)
1338 {
1339         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1340         struct it87_data *data = dev_get_drvdata(dev);
1341         int nr = sensor_attr->index;
1342         long val;
1343
1344         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1345                 return -EINVAL;
1346
1347         mutex_lock(&data->update_lock);
1348         if (has_newer_autopwm(data)) {
1349                 /*
1350                  * If we are in automatic mode, the PWM duty cycle register
1351                  * is read-only so we can't write the value.
1352                  */
1353                 if (data->pwm_ctrl[nr] & 0x80) {
1354                         mutex_unlock(&data->update_lock);
1355                         return -EBUSY;
1356                 }
1357                 data->pwm_duty[nr] = pwm_to_reg(data, val);
1358                 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1359                                  data->pwm_duty[nr]);
1360         } else {
1361                 data->pwm_duty[nr] = pwm_to_reg(data, val);
1362                 /*
1363                  * If we are in manual mode, write the duty cycle immediately;
1364                  * otherwise, just store it for later use.
1365                  */
1366                 if (!(data->pwm_ctrl[nr] & 0x80)) {
1367                         data->pwm_ctrl[nr] = data->pwm_duty[nr];
1368                         it87_write_value(data, IT87_REG_PWM[nr],
1369                                          data->pwm_ctrl[nr]);
1370                 }
1371         }
1372         mutex_unlock(&data->update_lock);
1373         return count;
1374 }
1375
1376 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1377                             const char *buf, size_t count)
1378 {
1379         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1380         struct it87_data *data = dev_get_drvdata(dev);
1381         int nr = sensor_attr->index;
1382         unsigned long val;
1383         int i;
1384
1385         if (kstrtoul(buf, 10, &val) < 0)
1386                 return -EINVAL;
1387
1388         val = clamp_val(val, 0, 1000000);
1389         val *= has_newer_autopwm(data) ? 256 : 128;
1390
1391         /* Search for the nearest available frequency */
1392         for (i = 0; i < 7; i++) {
1393                 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1394                         break;
1395         }
1396
1397         mutex_lock(&data->update_lock);
1398         if (nr == 0) {
1399                 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1400                 data->fan_ctl |= i << 4;
1401                 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1402         } else {
1403                 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1404                 data->extra |= i << 4;
1405                 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1406         }
1407         mutex_unlock(&data->update_lock);
1408
1409         return count;
1410 }
1411
1412 static ssize_t show_pwm_temp_map(struct device *dev,
1413                                  struct device_attribute *attr, char *buf)
1414 {
1415         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1416         struct it87_data *data = it87_update_device(dev);
1417         int nr = sensor_attr->index;
1418         int map;
1419
1420         map = data->pwm_temp_map[nr];
1421         if (map >= 3)
1422                 map = 0;        /* Should never happen */
1423         if (nr >= 3)            /* pwm channels 3..6 map to temp4..6 */
1424                 map += 3;
1425
1426         return sprintf(buf, "%d\n", (int)BIT(map));
1427 }
1428
1429 static ssize_t set_pwm_temp_map(struct device *dev,
1430                                 struct device_attribute *attr, const char *buf,
1431                                 size_t count)
1432 {
1433         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1434         struct it87_data *data = dev_get_drvdata(dev);
1435         int nr = sensor_attr->index;
1436         long val;
1437         u8 reg;
1438
1439         if (kstrtol(buf, 10, &val) < 0)
1440                 return -EINVAL;
1441
1442         if (nr >= 3)
1443                 val -= 3;
1444
1445         switch (val) {
1446         case BIT(0):
1447                 reg = 0x00;
1448                 break;
1449         case BIT(1):
1450                 reg = 0x01;
1451                 break;
1452         case BIT(2):
1453                 reg = 0x02;
1454                 break;
1455         default:
1456                 return -EINVAL;
1457         }
1458
1459         mutex_lock(&data->update_lock);
1460         data->pwm_temp_map[nr] = reg;
1461         /*
1462          * If we are in automatic mode, write the temp mapping immediately;
1463          * otherwise, just store it for later use.
1464          */
1465         if (data->pwm_ctrl[nr] & 0x80) {
1466                 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1467                 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1468         }
1469         mutex_unlock(&data->update_lock);
1470         return count;
1471 }
1472
1473 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1474                              char *buf)
1475 {
1476         struct it87_data *data = it87_update_device(dev);
1477         struct sensor_device_attribute_2 *sensor_attr =
1478                         to_sensor_dev_attr_2(attr);
1479         int nr = sensor_attr->nr;
1480         int point = sensor_attr->index;
1481
1482         return sprintf(buf, "%d\n",
1483                        pwm_from_reg(data, data->auto_pwm[nr][point]));
1484 }
1485
1486 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1487                             const char *buf, size_t count)
1488 {
1489         struct it87_data *data = dev_get_drvdata(dev);
1490         struct sensor_device_attribute_2 *sensor_attr =
1491                         to_sensor_dev_attr_2(attr);
1492         int nr = sensor_attr->nr;
1493         int point = sensor_attr->index;
1494         int regaddr;
1495         long val;
1496
1497         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1498                 return -EINVAL;
1499
1500         mutex_lock(&data->update_lock);
1501         data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1502         if (has_newer_autopwm(data))
1503                 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1504         else
1505                 regaddr = IT87_REG_AUTO_PWM(nr, point);
1506         it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1507         mutex_unlock(&data->update_lock);
1508         return count;
1509 }
1510
1511 static ssize_t show_auto_pwm_slope(struct device *dev,
1512                                    struct device_attribute *attr, char *buf)
1513 {
1514         struct it87_data *data = it87_update_device(dev);
1515         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1516         int nr = sensor_attr->index;
1517
1518         return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1519 }
1520
1521 static ssize_t set_auto_pwm_slope(struct device *dev,
1522                                   struct device_attribute *attr,
1523                                   const char *buf, size_t count)
1524 {
1525         struct it87_data *data = dev_get_drvdata(dev);
1526         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1527         int nr = sensor_attr->index;
1528         unsigned long val;
1529
1530         if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1531                 return -EINVAL;
1532
1533         mutex_lock(&data->update_lock);
1534         data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1535         it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1536                          data->auto_pwm[nr][1]);
1537         mutex_unlock(&data->update_lock);
1538         return count;
1539 }
1540
1541 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1542                               char *buf)
1543 {
1544         struct it87_data *data = it87_update_device(dev);
1545         struct sensor_device_attribute_2 *sensor_attr =
1546                         to_sensor_dev_attr_2(attr);
1547         int nr = sensor_attr->nr;
1548         int point = sensor_attr->index;
1549         int reg;
1550
1551         if (has_old_autopwm(data) || point)
1552                 reg = data->auto_temp[nr][point];
1553         else
1554                 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1555
1556         return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
1557 }
1558
1559 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1560                              const char *buf, size_t count)
1561 {
1562         struct it87_data *data = dev_get_drvdata(dev);
1563         struct sensor_device_attribute_2 *sensor_attr =
1564                         to_sensor_dev_attr_2(attr);
1565         int nr = sensor_attr->nr;
1566         int point = sensor_attr->index;
1567         long val;
1568         int reg;
1569
1570         if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1571                 return -EINVAL;
1572
1573         mutex_lock(&data->update_lock);
1574         if (has_newer_autopwm(data) && !point) {
1575                 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1576                 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1577                 data->auto_temp[nr][0] = reg;
1578                 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1579         } else {
1580                 reg = TEMP_TO_REG(val);
1581                 data->auto_temp[nr][point] = reg;
1582                 if (has_newer_autopwm(data))
1583                         point--;
1584                 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1585         }
1586         mutex_unlock(&data->update_lock);
1587         return count;
1588 }
1589
1590 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1591 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1592                             0, 1);
1593 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1594                           set_fan_div, 0);
1595
1596 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1597 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1598                             1, 1);
1599 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1600                           set_fan_div, 1);
1601
1602 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1603 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1604                             2, 1);
1605 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1606                           set_fan_div, 2);
1607
1608 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1609 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1610                             3, 1);
1611
1612 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1613 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1614                             4, 1);
1615
1616 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1617 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1618                             5, 1);
1619
1620 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1621                           show_pwm_enable, set_pwm_enable, 0);
1622 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1623 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1624                           set_pwm_freq, 0);
1625 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
1626                           show_pwm_temp_map, set_pwm_temp_map, 0);
1627 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1628                             show_auto_pwm, set_auto_pwm, 0, 0);
1629 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1630                             show_auto_pwm, set_auto_pwm, 0, 1);
1631 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1632                             show_auto_pwm, set_auto_pwm, 0, 2);
1633 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1634                             show_auto_pwm, NULL, 0, 3);
1635 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1636                             show_auto_temp, set_auto_temp, 0, 1);
1637 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1638                             show_auto_temp, set_auto_temp, 0, 0);
1639 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1640                             show_auto_temp, set_auto_temp, 0, 2);
1641 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1642                             show_auto_temp, set_auto_temp, 0, 3);
1643 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1644                             show_auto_temp, set_auto_temp, 0, 4);
1645 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1646                             show_auto_pwm, set_auto_pwm, 0, 0);
1647 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1648                           show_auto_pwm_slope, set_auto_pwm_slope, 0);
1649
1650 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1651                           show_pwm_enable, set_pwm_enable, 1);
1652 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1653 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1654 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
1655                           show_pwm_temp_map, set_pwm_temp_map, 1);
1656 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1657                             show_auto_pwm, set_auto_pwm, 1, 0);
1658 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1659                             show_auto_pwm, set_auto_pwm, 1, 1);
1660 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1661                             show_auto_pwm, set_auto_pwm, 1, 2);
1662 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1663                             show_auto_pwm, NULL, 1, 3);
1664 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1665                             show_auto_temp, set_auto_temp, 1, 1);
1666 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1667                             show_auto_temp, set_auto_temp, 1, 0);
1668 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1669                             show_auto_temp, set_auto_temp, 1, 2);
1670 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1671                             show_auto_temp, set_auto_temp, 1, 3);
1672 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1673                             show_auto_temp, set_auto_temp, 1, 4);
1674 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1675                             show_auto_pwm, set_auto_pwm, 1, 0);
1676 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1677                           show_auto_pwm_slope, set_auto_pwm_slope, 1);
1678
1679 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1680                           show_pwm_enable, set_pwm_enable, 2);
1681 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1682 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1683 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
1684                           show_pwm_temp_map, set_pwm_temp_map, 2);
1685 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1686                             show_auto_pwm, set_auto_pwm, 2, 0);
1687 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1688                             show_auto_pwm, set_auto_pwm, 2, 1);
1689 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1690                             show_auto_pwm, set_auto_pwm, 2, 2);
1691 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1692                             show_auto_pwm, NULL, 2, 3);
1693 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1694                             show_auto_temp, set_auto_temp, 2, 1);
1695 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1696                             show_auto_temp, set_auto_temp, 2, 0);
1697 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1698                             show_auto_temp, set_auto_temp, 2, 2);
1699 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1700                             show_auto_temp, set_auto_temp, 2, 3);
1701 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1702                             show_auto_temp, set_auto_temp, 2, 4);
1703 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1704                             show_auto_pwm, set_auto_pwm, 2, 0);
1705 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1706                           show_auto_pwm_slope, set_auto_pwm_slope, 2);
1707
1708 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1709                           show_pwm_enable, set_pwm_enable, 3);
1710 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1711 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1712 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
1713                           show_pwm_temp_map, set_pwm_temp_map, 3);
1714 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1715                             show_auto_temp, set_auto_temp, 2, 1);
1716 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1717                             show_auto_temp, set_auto_temp, 2, 0);
1718 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1719                             show_auto_temp, set_auto_temp, 2, 2);
1720 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1721                             show_auto_temp, set_auto_temp, 2, 3);
1722 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1723                             show_auto_pwm, set_auto_pwm, 3, 0);
1724 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1725                           show_auto_pwm_slope, set_auto_pwm_slope, 3);
1726
1727 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1728                           show_pwm_enable, set_pwm_enable, 4);
1729 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1730 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1731 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
1732                           show_pwm_temp_map, set_pwm_temp_map, 4);
1733 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1734                             show_auto_temp, set_auto_temp, 2, 1);
1735 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1736                             show_auto_temp, set_auto_temp, 2, 0);
1737 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1738                             show_auto_temp, set_auto_temp, 2, 2);
1739 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1740                             show_auto_temp, set_auto_temp, 2, 3);
1741 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1742                             show_auto_pwm, set_auto_pwm, 4, 0);
1743 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1744                           show_auto_pwm_slope, set_auto_pwm_slope, 4);
1745
1746 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1747                           show_pwm_enable, set_pwm_enable, 5);
1748 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1749 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1750 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
1751                           show_pwm_temp_map, set_pwm_temp_map, 5);
1752 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1753                             show_auto_temp, set_auto_temp, 2, 1);
1754 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1755                             show_auto_temp, set_auto_temp, 2, 0);
1756 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1757                             show_auto_temp, set_auto_temp, 2, 2);
1758 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1759                             show_auto_temp, set_auto_temp, 2, 3);
1760 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1761                             show_auto_pwm, set_auto_pwm, 5, 0);
1762 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1763                           show_auto_pwm_slope, set_auto_pwm_slope, 5);
1764
1765 /* Alarms */
1766 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1767                            char *buf)
1768 {
1769         struct it87_data *data = it87_update_device(dev);
1770
1771         return sprintf(buf, "%u\n", data->alarms);
1772 }
1773 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1774
1775 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1776                           char *buf)
1777 {
1778         struct it87_data *data = it87_update_device(dev);
1779         int bitnr = to_sensor_dev_attr(attr)->index;
1780
1781         return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1782 }
1783
1784 static ssize_t clear_intrusion(struct device *dev,
1785                                struct device_attribute *attr, const char *buf,
1786                                size_t count)
1787 {
1788         struct it87_data *data = dev_get_drvdata(dev);
1789         int config;
1790         long val;
1791
1792         if (kstrtol(buf, 10, &val) < 0 || val != 0)
1793                 return -EINVAL;
1794
1795         mutex_lock(&data->update_lock);
1796         config = it87_read_value(data, IT87_REG_CONFIG);
1797         if (config < 0) {
1798                 count = config;
1799         } else {
1800                 config |= BIT(5);
1801                 it87_write_value(data, IT87_REG_CONFIG, config);
1802                 /* Invalidate cache to force re-read */
1803                 data->valid = 0;
1804         }
1805         mutex_unlock(&data->update_lock);
1806
1807         return count;
1808 }
1809
1810 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1811 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1812 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1813 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1814 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1815 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1816 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1817 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1818 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1819 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1820 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1821 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1822 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1823 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1824 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1825 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1826 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1827 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1828                           show_alarm, clear_intrusion, 4);
1829
1830 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1831                          char *buf)
1832 {
1833         struct it87_data *data = it87_update_device(dev);
1834         int bitnr = to_sensor_dev_attr(attr)->index;
1835
1836         return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1837 }
1838
1839 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1840                         const char *buf, size_t count)
1841 {
1842         int bitnr = to_sensor_dev_attr(attr)->index;
1843         struct it87_data *data = dev_get_drvdata(dev);
1844         long val;
1845
1846         if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
1847                 return -EINVAL;
1848
1849         mutex_lock(&data->update_lock);
1850         data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1851         if (val)
1852                 data->beeps |= BIT(bitnr);
1853         else
1854                 data->beeps &= ~BIT(bitnr);
1855         it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1856         mutex_unlock(&data->update_lock);
1857         return count;
1858 }
1859
1860 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1861                           show_beep, set_beep, 1);
1862 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1863 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1864 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1865 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1866 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1867 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1868 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1869 /* fanX_beep writability is set later */
1870 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1871 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1872 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1873 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1874 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1875 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1876 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1877                           show_beep, set_beep, 2);
1878 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1879 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1880
1881 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1882                             char *buf)
1883 {
1884         struct it87_data *data = dev_get_drvdata(dev);
1885
1886         return sprintf(buf, "%u\n", data->vrm);
1887 }
1888
1889 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1890                              const char *buf, size_t count)
1891 {
1892         struct it87_data *data = dev_get_drvdata(dev);
1893         unsigned long val;
1894
1895         if (kstrtoul(buf, 10, &val) < 0)
1896                 return -EINVAL;
1897
1898         data->vrm = val;
1899
1900         return count;
1901 }
1902 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1903
1904 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1905                             char *buf)
1906 {
1907         struct it87_data *data = it87_update_device(dev);
1908
1909         return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
1910 }
1911 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1912
1913 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1914                           char *buf)
1915 {
1916         static const char * const labels[] = {
1917                 "+5V",
1918                 "5VSB",
1919                 "Vbat",
1920         };
1921         static const char * const labels_it8721[] = {
1922                 "+3.3V",
1923                 "3VSB",
1924                 "Vbat",
1925         };
1926         struct it87_data *data = dev_get_drvdata(dev);
1927         int nr = to_sensor_dev_attr(attr)->index;
1928         const char *label;
1929
1930         if (has_12mv_adc(data) || has_10_9mv_adc(data))
1931                 label = labels_it8721[nr];
1932         else
1933                 label = labels[nr];
1934
1935         return sprintf(buf, "%s\n", label);
1936 }
1937 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1938 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1939 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1940 /* AVCC3 */
1941 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1942
1943 static umode_t it87_in_is_visible(struct kobject *kobj,
1944                                   struct attribute *attr, int index)
1945 {
1946         struct device *dev = container_of(kobj, struct device, kobj);
1947         struct it87_data *data = dev_get_drvdata(dev);
1948         int i = index / 5;      /* voltage index */
1949         int a = index % 5;      /* attribute index */
1950
1951         if (index >= 40) {      /* in8 and higher only have input attributes */
1952                 i = index - 40 + 8;
1953                 a = 0;
1954         }
1955
1956         if (!(data->has_in & BIT(i)))
1957                 return 0;
1958
1959         if (a == 4 && !data->has_beep)
1960                 return 0;
1961
1962         return attr->mode;
1963 }
1964
1965 static struct attribute *it87_attributes_in[] = {
1966         &sensor_dev_attr_in0_input.dev_attr.attr,
1967         &sensor_dev_attr_in0_min.dev_attr.attr,
1968         &sensor_dev_attr_in0_max.dev_attr.attr,
1969         &sensor_dev_attr_in0_alarm.dev_attr.attr,
1970         &sensor_dev_attr_in0_beep.dev_attr.attr,        /* 4 */
1971
1972         &sensor_dev_attr_in1_input.dev_attr.attr,
1973         &sensor_dev_attr_in1_min.dev_attr.attr,
1974         &sensor_dev_attr_in1_max.dev_attr.attr,
1975         &sensor_dev_attr_in1_alarm.dev_attr.attr,
1976         &sensor_dev_attr_in1_beep.dev_attr.attr,        /* 9 */
1977
1978         &sensor_dev_attr_in2_input.dev_attr.attr,
1979         &sensor_dev_attr_in2_min.dev_attr.attr,
1980         &sensor_dev_attr_in2_max.dev_attr.attr,
1981         &sensor_dev_attr_in2_alarm.dev_attr.attr,
1982         &sensor_dev_attr_in2_beep.dev_attr.attr,        /* 14 */
1983
1984         &sensor_dev_attr_in3_input.dev_attr.attr,
1985         &sensor_dev_attr_in3_min.dev_attr.attr,
1986         &sensor_dev_attr_in3_max.dev_attr.attr,
1987         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1988         &sensor_dev_attr_in3_beep.dev_attr.attr,        /* 19 */
1989
1990         &sensor_dev_attr_in4_input.dev_attr.attr,
1991         &sensor_dev_attr_in4_min.dev_attr.attr,
1992         &sensor_dev_attr_in4_max.dev_attr.attr,
1993         &sensor_dev_attr_in4_alarm.dev_attr.attr,
1994         &sensor_dev_attr_in4_beep.dev_attr.attr,        /* 24 */
1995
1996         &sensor_dev_attr_in5_input.dev_attr.attr,
1997         &sensor_dev_attr_in5_min.dev_attr.attr,
1998         &sensor_dev_attr_in5_max.dev_attr.attr,
1999         &sensor_dev_attr_in5_alarm.dev_attr.attr,
2000         &sensor_dev_attr_in5_beep.dev_attr.attr,        /* 29 */
2001
2002         &sensor_dev_attr_in6_input.dev_attr.attr,
2003         &sensor_dev_attr_in6_min.dev_attr.attr,
2004         &sensor_dev_attr_in6_max.dev_attr.attr,
2005         &sensor_dev_attr_in6_alarm.dev_attr.attr,
2006         &sensor_dev_attr_in6_beep.dev_attr.attr,        /* 34 */
2007
2008         &sensor_dev_attr_in7_input.dev_attr.attr,
2009         &sensor_dev_attr_in7_min.dev_attr.attr,
2010         &sensor_dev_attr_in7_max.dev_attr.attr,
2011         &sensor_dev_attr_in7_alarm.dev_attr.attr,
2012         &sensor_dev_attr_in7_beep.dev_attr.attr,        /* 39 */
2013
2014         &sensor_dev_attr_in8_input.dev_attr.attr,       /* 40 */
2015         &sensor_dev_attr_in9_input.dev_attr.attr,       /* 41 */
2016         &sensor_dev_attr_in10_input.dev_attr.attr,      /* 41 */
2017         &sensor_dev_attr_in11_input.dev_attr.attr,      /* 41 */
2018         &sensor_dev_attr_in12_input.dev_attr.attr,      /* 41 */
2019         NULL
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);