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