]> git.sur5r.net Git - groeck-it87/blob - it87.c
Checkpatch cleanup: Line length
[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  *            IT8607E  Super I/O chip w/LPC interface
15  *            IT8613E  Super I/O chip w/LPC interface
16  *            IT8620E  Super I/O chip w/LPC interface
17  *            IT8622E  Super I/O chip w/LPC interface
18  *            IT8623E  Super I/O chip w/LPC interface
19  *            IT8625E  Super I/O chip w/LPC interface
20  *            IT8628E  Super I/O chip w/LPC interface
21  *            IT8655E  Super I/O chip w/LPC interface
22  *            IT8665E  Super I/O chip w/LPC interface
23  *            IT8686E  Super I/O chip w/LPC interface
24  *            IT8705F  Super I/O chip w/LPC interface
25  *            IT8712F  Super I/O chip w/LPC interface
26  *            IT8716F  Super I/O chip w/LPC interface
27  *            IT8718F  Super I/O chip w/LPC interface
28  *            IT8720F  Super I/O chip w/LPC interface
29  *            IT8721F  Super I/O chip w/LPC interface
30  *            IT8726F  Super I/O chip w/LPC interface
31  *            IT8728F  Super I/O chip w/LPC interface
32  *            IT8732F  Super I/O chip w/LPC interface
33  *            IT8758E  Super I/O chip w/LPC interface
34  *            IT8771E  Super I/O chip w/LPC interface
35  *            IT8772E  Super I/O chip w/LPC interface
36  *            IT8781F  Super I/O chip w/LPC interface
37  *            IT8782F  Super I/O chip w/LPC interface
38  *            IT8783E/F Super I/O chip w/LPC interface
39  *            IT8786E  Super I/O chip w/LPC interface
40  *            IT8790E  Super I/O chip w/LPC interface
41  *            IT8792E  Super I/O chip w/LPC interface
42  *            Sis950   A clone of the IT8705F
43  *
44  *  Copyright (C) 2001 Chris Gauthron
45  *  Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
46  *
47  *  This program is free software; you can redistribute it and/or modify
48  *  it under the terms of the GNU General Public License as published by
49  *  the Free Software Foundation; either version 2 of the License, or
50  *  (at your option) any later version.
51  *
52  *  This program is distributed in the hope that it will be useful,
53  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
54  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55  *  GNU General Public License for more details.
56  */
57
58 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
59
60 #include <linux/bitops.h>
61 #include <linux/module.h>
62 #include <linux/init.h>
63 #include <linux/slab.h>
64 #include <linux/jiffies.h>
65 #include <linux/platform_device.h>
66 #include <linux/hwmon.h>
67 #include <linux/hwmon-sysfs.h>
68 #include <linux/hwmon-vid.h>
69 #include <linux/err.h>
70 #include <linux/mutex.h>
71 #include <linux/sysfs.h>
72 #include <linux/string.h>
73 #include <linux/dmi.h>
74 #include <linux/acpi.h>
75 #include <linux/io.h>
76 #include "compat.h"
77
78 #ifndef IT87_DRIVER_VERSION
79 #define IT87_DRIVER_VERSION     "<not provided>"
80 #endif
81
82 #define DRVNAME "it87"
83
84 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
85              it8771, it8772, it8781, it8782, it8783, it8786, it8790,
86              it8792, it8603, it8607, it8613, it8620, it8622, it8625, it8628,
87              it8655, it8665, it8686 };
88
89 static unsigned short force_id;
90 module_param(force_id, ushort, 0000);
91 MODULE_PARM_DESC(force_id, "Override the detected device ID");
92
93 static bool ignore_resource_conflict;
94 module_param(ignore_resource_conflict, bool, 0000);
95 MODULE_PARM_DESC(ignore_resource_conflict, "Ignore ACPI resource conflict");
96
97 static bool mmio;
98 module_param(mmio, bool, 0000);
99 MODULE_PARM_DESC(mmio, "Use MMIO if available");
100
101 static struct platform_device *it87_pdev[2];
102
103 #define REG_2E  0x2e    /* The register to read/write */
104 #define REG_4E  0x4e    /* Secondary register to read/write */
105
106 #define DEV     0x07    /* Register: Logical device select */
107 #define PME     0x04    /* The device with the fan registers in it */
108
109 /* The device with the IT8718F/IT8720F VID value in it */
110 #define GPIO    0x07
111
112 #define DEVID   0x20    /* Register: Device ID */
113 #define DEVREV  0x22    /* Register: Device Revision */
114
115 static inline void __superio_enter(int ioreg)
116 {
117         outb(0x87, ioreg);
118         outb(0x01, ioreg);
119         outb(0x55, ioreg);
120         outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
121 }
122
123 static inline int superio_inb(int ioreg, int reg)
124 {
125         int val;
126
127         outb(reg, ioreg);
128         val = inb(ioreg + 1);
129
130         return val;
131 }
132
133 static inline void superio_outb(int ioreg, int reg, int val)
134 {
135         outb(reg, ioreg);
136         outb(val, ioreg + 1);
137 }
138
139 static int superio_inw(int ioreg, int reg)
140 {
141         return (superio_inb(ioreg, reg) << 8) | superio_inb(ioreg, reg + 1);
142 }
143
144 static inline void superio_select(int ioreg, int ldn)
145 {
146         outb(DEV, ioreg);
147         outb(ldn, ioreg + 1);
148 }
149
150 static inline int superio_enter(int ioreg)
151 {
152         /*
153          * Try to reserve ioreg and ioreg + 1 for exclusive access.
154          */
155         if (!request_muxed_region(ioreg, 2, DRVNAME))
156                 return -EBUSY;
157
158         __superio_enter(ioreg);
159         return 0;
160 }
161
162 static inline void superio_exit(int ioreg, bool doexit)
163 {
164         if (doexit) {
165                 outb(0x02, ioreg);
166                 outb(0x02, ioreg + 1);
167         }
168         release_region(ioreg, 2);
169 }
170
171 /* Logical device 4 registers */
172 #define IT8712F_DEVID 0x8712
173 #define IT8705F_DEVID 0x8705
174 #define IT8716F_DEVID 0x8716
175 #define IT8718F_DEVID 0x8718
176 #define IT8720F_DEVID 0x8720
177 #define IT8721F_DEVID 0x8721
178 #define IT8726F_DEVID 0x8726
179 #define IT8728F_DEVID 0x8728
180 #define IT8732F_DEVID 0x8732
181 #define IT8792E_DEVID 0x8733
182 #define IT8771E_DEVID 0x8771
183 #define IT8772E_DEVID 0x8772
184 #define IT8781F_DEVID 0x8781
185 #define IT8782F_DEVID 0x8782
186 #define IT8783E_DEVID 0x8783
187 #define IT8786E_DEVID 0x8786
188 #define IT8790E_DEVID 0x8790
189 #define IT8603E_DEVID 0x8603
190 #define IT8607E_DEVID 0x8607
191 #define IT8613E_DEVID 0x8613
192 #define IT8620E_DEVID 0x8620
193 #define IT8622E_DEVID 0x8622
194 #define IT8623E_DEVID 0x8623
195 #define IT8625E_DEVID 0x8625
196 #define IT8628E_DEVID 0x8628
197 #define IT8655E_DEVID 0x8655
198 #define IT8665E_DEVID 0x8665
199 #define IT8686E_DEVID 0x8686
200
201 /* Logical device 4 (Environmental Monitor) registers */
202 #define IT87_ACT_REG            0x30
203 #define IT87_BASE_REG           0x60
204 #define IT87_SPECIAL_CFG_REG    0xf3    /* special configuration register */
205
206 /* Global configuration registers (IT8712F and later) */
207 #define IT87_EC_HWM_MIO_REG     0x24    /* MMIO configuration register */
208 #define IT87_SIO_GPIO1_REG      0x25
209 #define IT87_SIO_GPIO2_REG      0x26
210 #define IT87_SIO_GPIO3_REG      0x27
211 #define IT87_SIO_GPIO4_REG      0x28
212 #define IT87_SIO_GPIO5_REG      0x29
213 #define IT87_SIO_GPIO9_REG      0xd3
214 #define IT87_SIO_PINX1_REG      0x2a    /* Pin selection */
215 #define IT87_SIO_PINX2_REG      0x2c    /* Pin selection */
216 #define IT87_SIO_PINX4_REG      0x2d    /* Pin selection */
217
218 /* Logical device 7 (GPIO) registers (IT8712F and later) */
219 #define IT87_SIO_SPI_REG        0xef    /* SPI function pin select */
220 #define IT87_SIO_VID_REG        0xfc    /* VID value */
221 #define IT87_SIO_BEEP_PIN_REG   0xf6    /* Beep pin mapping */
222
223 /* Update battery voltage after every reading if true */
224 static bool update_vbat;
225
226 /* Not all BIOSes properly configure the PWM registers */
227 static bool fix_pwm_polarity;
228
229 /* Many IT87 constants specified below */
230
231 /* Length of ISA address segment */
232 #define IT87_EXTENT 8
233
234 /* Length of ISA address segment for Environmental Controller */
235 #define IT87_EC_EXTENT 2
236
237 /* Offset of EC registers from ISA base address */
238 #define IT87_EC_OFFSET 5
239
240 /* Where are the ISA address/data registers relative to the EC base address */
241 #define IT87_ADDR_REG_OFFSET 0
242 #define IT87_DATA_REG_OFFSET 1
243
244 /*----- The IT87 registers -----*/
245
246 #define IT87_REG_CONFIG        0x00
247
248 #define IT87_REG_ALARM1        0x01
249 #define IT87_REG_ALARM2        0x02
250 #define IT87_REG_ALARM3        0x03
251
252 #define IT87_REG_BANK           0x06
253
254 /*
255  * The IT8718F and IT8720F have the VID value in a different register, in
256  * Super-I/O configuration space.
257  */
258 #define IT87_REG_VID           0x0a
259 /*
260  * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
261  * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
262  * mode.
263  */
264 #define IT87_REG_FAN_DIV       0x0b
265 #define IT87_REG_FAN_16BIT     0x0c
266
267 /*
268  * Monitors:
269  * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
270  * - up to 6 temp (1 to 6)
271  * - up to 6 fan (1 to 6)
272  */
273
274 static const u8 IT87_REG_FAN[] =        { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
275 static const u8 IT87_REG_FAN_MIN[] =    { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
276 static const u8 IT87_REG_FANX[] =       { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
277 static const u8 IT87_REG_FANX_MIN[] =   { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
278
279 static const u8 IT87_REG_FAN_8665[] =   { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x93 };
280 static const u8 IT87_REG_FAN_MIN_8665[] = {
281                                         0x10, 0x11, 0x12, 0x84, 0x86, 0xb2 };
282 static const u8 IT87_REG_FANX_8665[] =  { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x94 };
283 static const u8 IT87_REG_FANX_MIN_8665[] = {
284                                         0x1b, 0x1c, 0x1d, 0x85, 0x87, 0xb3 };
285
286 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59, 0x5a, 0x90, 0x91 };
287
288 static const u8 IT87_REG_TEMP_OFFSET_8686[] = {
289                                         0x56, 0x57, 0x59, 0x90, 0x91, 0x92 };
290
291 #define IT87_REG_FAN_MAIN_CTRL 0x13
292 #define IT87_REG_FAN_CTL       0x14
293
294 static const u8 IT87_REG_PWM[] =        { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
295 static const u8 IT87_REG_PWM_8665[] =   { 0x15, 0x16, 0x17, 0x1e, 0x1f, 0x92 };
296
297 static const u8 IT87_REG_PWM_DUTY[] =   { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
298
299 static const u8 IT87_REG_VIN[]  = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
300                                     0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
301
302 #define IT87_REG_TEMP(nr)      (0x29 + (nr))
303
304 #define IT87_REG_VIN_MAX(nr)   (0x30 + (nr) * 2)
305 #define IT87_REG_VIN_MIN(nr)   (0x31 + (nr) * 2)
306
307 static const u8 IT87_REG_TEMP_HIGH[] =  { 0x40, 0x42, 0x44, 0x46, 0xb4, 0xb6 };
308 static const u8 IT87_REG_TEMP_LOW[] =   { 0x41, 0x43, 0x45, 0x47, 0xb5, 0xb7 };
309
310 static const u8 IT87_REG_TEMP_HIGH_8686[] = {
311                                         0x40, 0x42, 0x44, 0xb4, 0xb6, 0xb8 };
312 static const u8 IT87_REG_TEMP_LOW_8686[] = {
313                                         0x41, 0x43, 0x45, 0xb5, 0xb7, 0xb9 };
314
315 #define IT87_REG_VIN_ENABLE    0x50
316 #define IT87_REG_TEMP_ENABLE   0x51
317 #define IT87_REG_TEMP_EXTRA    0x55
318 #define IT87_REG_BEEP_ENABLE   0x5c
319
320 #define IT87_REG_CHIPID        0x58
321
322 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
323
324 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
325 #define IT87_REG_AUTO_PWM(nr, i)  (IT87_REG_AUTO_BASE[nr] + 5 + (i))
326
327 #define IT87_REG_TEMP456_ENABLE 0x77
328
329 static const u16 IT87_REG_TEMP_SRC1[] = { 0x21d, 0x21e, 0x21f };
330 #define IT87_REG_TEMP_SRC2      0x23d
331
332 #define NUM_VIN                 ARRAY_SIZE(IT87_REG_VIN)
333 #define NUM_VIN_LIMIT           8
334 #define NUM_TEMP                6
335 #define NUM_FAN                 ARRAY_SIZE(IT87_REG_FAN)
336 #define NUM_FAN_DIV             3
337 #define NUM_PWM                 ARRAY_SIZE(IT87_REG_PWM)
338 #define NUM_AUTO_PWM            ARRAY_SIZE(IT87_REG_PWM)
339
340 struct it87_devices {
341         const char *name;
342         const char * const suffix;
343         u32 features;
344         u8 num_temp_limit;
345         u8 num_temp_offset;
346         u8 num_temp_map;        /* Number of temperature sources for pwm */
347         u8 peci_mask;
348         u8 old_peci_mask;
349         u8 smbus_bitmap;        /* SMBus enable bits in extra config register */
350         u8 ec_special_config;
351 };
352
353 #define FEAT_12MV_ADC           BIT(0)
354 #define FEAT_NEWER_AUTOPWM      BIT(1)
355 #define FEAT_OLD_AUTOPWM        BIT(2)
356 #define FEAT_16BIT_FANS         BIT(3)
357 #define FEAT_TEMP_PECI          BIT(5)
358 #define FEAT_TEMP_OLD_PECI      BIT(6)
359 #define FEAT_FAN16_CONFIG       BIT(7)  /* Need to enable 16-bit fans */
360 #define FEAT_FIVE_FANS          BIT(8)  /* Supports five fans */
361 #define FEAT_VID                BIT(9)  /* Set if chip supports VID */
362 #define FEAT_IN7_INTERNAL       BIT(10) /* Set if in7 is internal */
363 #define FEAT_SIX_FANS           BIT(11) /* Supports six fans */
364 #define FEAT_10_9MV_ADC         BIT(12)
365 #define FEAT_AVCC3              BIT(13) /* Chip supports in9/AVCC3 */
366 #define FEAT_FIVE_PWM           BIT(14) /* Chip supports 5 pwm chn */
367 #define FEAT_SIX_PWM            BIT(15) /* Chip supports 6 pwm chn */
368 #define FEAT_PWM_FREQ2          BIT(16) /* Separate pwm freq 2 */
369 #define FEAT_SIX_TEMP           BIT(17) /* Up to 6 temp sensors */
370 #define FEAT_VIN3_5V            BIT(18) /* VIN3 connected to +5V */
371 #define FEAT_FOUR_FANS          BIT(19) /* Supports four fans */
372 #define FEAT_FOUR_PWM           BIT(20) /* Supports four fan controls */
373 #define FEAT_BANK_SEL           BIT(21) /* Chip has multi-bank support */
374 #define FEAT_SCALING            BIT(22) /* Internal voltage scaling */
375 #define FEAT_FANCTL_ONOFF       BIT(23) /* chip has FAN_CTL ON/OFF */
376 #define FEAT_11MV_ADC           BIT(24)
377 #define FEAT_NEW_TEMPMAP        BIT(25) /* new temp input selection */
378 #define FEAT_MMIO               BIT(26) /* Chip supports MMIO */
379 #define FEAT_FOUR_TEMP          BIT(27)
380
381 static const struct it87_devices it87_devices[] = {
382         [it87] = {
383                 .name = "it87",
384                 .suffix = "F",
385                 .features = FEAT_OLD_AUTOPWM | FEAT_FANCTL_ONOFF,
386                                                 /* may need to overwrite */
387                 .num_temp_limit = 3,
388                 .num_temp_offset = 0,
389                 .num_temp_map = 3,
390         },
391         [it8712] = {
392                 .name = "it8712",
393                 .suffix = "F",
394                 .features = FEAT_OLD_AUTOPWM | FEAT_VID | FEAT_FANCTL_ONOFF,
395                                                 /* may need to overwrite */
396                 .num_temp_limit = 3,
397                 .num_temp_offset = 0,
398                 .num_temp_map = 3,
399         },
400         [it8716] = {
401                 .name = "it8716",
402                 .suffix = "F",
403                 .features = FEAT_16BIT_FANS | FEAT_VID
404                   | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2
405                   | FEAT_FANCTL_ONOFF,
406                 .num_temp_limit = 3,
407                 .num_temp_offset = 3,
408                 .num_temp_map = 3,
409         },
410         [it8718] = {
411                 .name = "it8718",
412                 .suffix = "F",
413                 .features = FEAT_16BIT_FANS | FEAT_VID
414                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
415                   | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
416                 .num_temp_limit = 3,
417                 .num_temp_offset = 3,
418                 .num_temp_map = 3,
419                 .old_peci_mask = 0x4,
420         },
421         [it8720] = {
422                 .name = "it8720",
423                 .suffix = "F",
424                 .features = FEAT_16BIT_FANS | FEAT_VID
425                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
426                   | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
427                 .num_temp_limit = 3,
428                 .num_temp_offset = 3,
429                 .num_temp_map = 3,
430                 .old_peci_mask = 0x4,
431         },
432         [it8721] = {
433                 .name = "it8721",
434                 .suffix = "F",
435                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
436                   | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
437                   | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
438                   | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
439                 .num_temp_limit = 3,
440                 .num_temp_offset = 3,
441                 .num_temp_map = 3,
442                 .peci_mask = 0x05,
443                 .old_peci_mask = 0x02,  /* Actually reports PCH */
444         },
445         [it8728] = {
446                 .name = "it8728",
447                 .suffix = "F",
448                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
449                   | FEAT_TEMP_PECI | FEAT_FIVE_FANS
450                   | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_SCALING
451                   | FEAT_FANCTL_ONOFF,
452                 .num_temp_limit = 6,
453                 .num_temp_offset = 3,
454                 .num_temp_map = 3,
455                 .peci_mask = 0x07,
456         },
457         [it8732] = {
458                 .name = "it8732",
459                 .suffix = "F",
460                 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
461                   | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
462                   | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_FOUR_FANS
463                   | FEAT_FOUR_PWM | FEAT_FANCTL_ONOFF,
464                 .num_temp_limit = 3,
465                 .num_temp_offset = 3,
466                 .num_temp_map = 3,
467                 .peci_mask = 0x07,
468                 .old_peci_mask = 0x02,  /* Actually reports PCH */
469         },
470         [it8771] = {
471                 .name = "it8771",
472                 .suffix = "E",
473                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
474                   | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
475                   | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
476                                 /* PECI: guesswork */
477                                 /* 12mV ADC (OHM) */
478                                 /* 16 bit fans (OHM) */
479                                 /* three fans, always 16 bit (guesswork) */
480                 .num_temp_limit = 3,
481                 .num_temp_offset = 3,
482                 .num_temp_map = 3,
483                 .peci_mask = 0x07,
484         },
485         [it8772] = {
486                 .name = "it8772",
487                 .suffix = "E",
488                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
489                   | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
490                   | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
491                                 /* PECI (coreboot) */
492                                 /* 12mV ADC (HWSensors4, OHM) */
493                                 /* 16 bit fans (HWSensors4, OHM) */
494                                 /* three fans, always 16 bit (datasheet) */
495                 .num_temp_limit = 3,
496                 .num_temp_offset = 3,
497                 .num_temp_map = 3,
498                 .peci_mask = 0x07,
499         },
500         [it8781] = {
501                 .name = "it8781",
502                 .suffix = "F",
503                 .features = FEAT_16BIT_FANS
504                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
505                   | FEAT_FANCTL_ONOFF,
506                 .num_temp_limit = 3,
507                 .num_temp_offset = 3,
508                 .num_temp_map = 3,
509                 .old_peci_mask = 0x4,
510         },
511         [it8782] = {
512                 .name = "it8782",
513                 .suffix = "F",
514                 .features = FEAT_16BIT_FANS
515                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
516                   | FEAT_FANCTL_ONOFF,
517                 .num_temp_limit = 3,
518                 .num_temp_offset = 3,
519                 .num_temp_map = 3,
520                 .old_peci_mask = 0x4,
521         },
522         [it8783] = {
523                 .name = "it8783",
524                 .suffix = "E/F",
525                 .features = FEAT_16BIT_FANS
526                   | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
527                   | FEAT_FANCTL_ONOFF,
528                 .num_temp_limit = 3,
529                 .num_temp_offset = 3,
530                 .num_temp_map = 3,
531                 .old_peci_mask = 0x4,
532         },
533         [it8786] = {
534                 .name = "it8786",
535                 .suffix = "E",
536                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
537                   | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
538                   | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
539                 .num_temp_limit = 3,
540                 .num_temp_offset = 3,
541                 .num_temp_map = 3,
542                 .peci_mask = 0x07,
543         },
544         [it8790] = {
545                 .name = "it8790",
546                 .suffix = "E",
547                 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
548                   | FEAT_16BIT_FANS | FEAT_TEMP_PECI
549                   | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
550                 .num_temp_limit = 3,
551                 .num_temp_offset = 3,
552                 .num_temp_map = 3,
553                 .peci_mask = 0x07,
554         },
555         [it8792] = {
556                 .name = "it8792",
557                 .suffix = "E",
558                 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
559                   | FEAT_16BIT_FANS | FEAT_TEMP_PECI
560                   | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
561                 .num_temp_limit = 3,
562                 .num_temp_offset = 3,
563                 .num_temp_map = 3,
564                 .peci_mask = 0x07,
565         },
566         [it8603] = {
567                 .name = "it8603",
568                 .suffix = "E",
569                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
570                   | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
571                   | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
572                 .num_temp_limit = 3,
573                 .num_temp_offset = 3,
574                 .num_temp_map = 4,
575                 .peci_mask = 0x07,
576         },
577         [it8607] = {
578                 .name = "it8607",
579                 .suffix = "E",
580                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
581                   | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL | FEAT_NEW_TEMPMAP
582                   | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING
583                   | FEAT_FANCTL_ONOFF,
584                 .num_temp_limit = 3,
585                 .num_temp_offset = 3,
586                 .num_temp_map = 6,
587                 .peci_mask = 0x07,
588         },
589         [it8613] = {
590                 .name = "it8613",
591                 .suffix = "E",
592                 .features = FEAT_NEWER_AUTOPWM | FEAT_11MV_ADC | FEAT_16BIT_FANS
593                   | FEAT_TEMP_PECI | FEAT_FIVE_FANS
594                   | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
595                   | FEAT_AVCC3 | FEAT_SCALING | FEAT_NEW_TEMPMAP,
596                 .num_temp_limit = 6,
597                 .num_temp_offset = 6,
598                 .num_temp_map = 6,
599                 .peci_mask = 0x07,
600         },
601         [it8620] = {
602                 .name = "it8620",
603                 .suffix = "E",
604                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
605                   | FEAT_TEMP_PECI | FEAT_SIX_FANS
606                   | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
607                   | FEAT_SIX_TEMP | FEAT_VIN3_5V | FEAT_SCALING
608                   | FEAT_FANCTL_ONOFF,
609                 .num_temp_limit = 3,
610                 .num_temp_offset = 3,
611                 .num_temp_map = 3,
612                 .peci_mask = 0x07,
613         },
614         [it8622] = {
615                 .name = "it8622",
616                 .suffix = "E",
617                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
618                   | FEAT_TEMP_PECI | FEAT_FIVE_FANS | FEAT_FOUR_TEMP
619                   | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
620                   | FEAT_AVCC3 | FEAT_VIN3_5V | FEAT_SCALING,
621                 .num_temp_limit = 3,
622                 .num_temp_offset = 3,
623                 .num_temp_map = 4,
624                 .peci_mask = 0x0f,
625                 .smbus_bitmap = BIT(1) | BIT(2),
626         },
627         [it8625] = {
628                 .name = "it8625",
629                 .suffix = "E",
630                 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
631                   | FEAT_AVCC3 | FEAT_NEW_TEMPMAP
632                   | FEAT_11MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
633                   | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_SCALING,
634                 .num_temp_limit = 6,
635                 .num_temp_offset = 6,
636                 .num_temp_map = 6,
637                 .smbus_bitmap = BIT(1) | BIT(2),
638         },
639         [it8628] = {
640                 .name = "it8628",
641                 .suffix = "E",
642                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
643                   | FEAT_TEMP_PECI | FEAT_SIX_FANS
644                   | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
645                   | FEAT_SIX_TEMP | FEAT_SCALING | FEAT_AVCC3
646                   | FEAT_FANCTL_ONOFF,
647                 .num_temp_limit = 6,
648                 .num_temp_offset = 3,
649                 .num_temp_map = 3,
650                 .peci_mask = 0x07,
651         },
652         [it8655] = {
653                 .name = "it8655",
654                 .suffix = "E",
655                 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
656                   | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
657                   | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_BANK_SEL
658                   | FEAT_MMIO,
659                 .num_temp_limit = 6,
660                 .num_temp_offset = 6,
661                 .num_temp_map = 6,
662                 .smbus_bitmap = BIT(2),
663         },
664         [it8665] = {
665                 .name = "it8665",
666                 .suffix = "E",
667                 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
668                   | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
669                   | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
670                   | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_MMIO,
671                 .num_temp_limit = 6,
672                 .num_temp_offset = 6,
673                 .num_temp_map = 6,
674                 .smbus_bitmap = BIT(2),
675         },
676         [it8686] = {
677                 .name = "it8686",
678                 .suffix = "E",
679                 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
680                   | FEAT_SIX_FANS | FEAT_NEW_TEMPMAP
681                   | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
682                   | FEAT_SIX_TEMP | FEAT_BANK_SEL | FEAT_SCALING | FEAT_AVCC3,
683                 .num_temp_limit = 6,
684                 .num_temp_offset = 6,
685                 .num_temp_map = 7,
686                 .smbus_bitmap = BIT(1) | BIT(2),
687         },
688 };
689
690 #define has_16bit_fans(data)    ((data)->features & FEAT_16BIT_FANS)
691 #define has_12mv_adc(data)      ((data)->features & FEAT_12MV_ADC)
692 #define has_10_9mv_adc(data)    ((data)->features & FEAT_10_9MV_ADC)
693 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
694 #define has_old_autopwm(data)   ((data)->features & FEAT_OLD_AUTOPWM)
695 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
696                                  ((data)->peci_mask & BIT(nr)))
697 #define has_temp_old_peci(data, nr) \
698                                 (((data)->features & FEAT_TEMP_OLD_PECI) && \
699                                  ((data)->old_peci_mask & BIT(nr)))
700 #define has_fan16_config(data)  ((data)->features & FEAT_FAN16_CONFIG)
701 #define has_five_fans(data)     ((data)->features & (FEAT_FIVE_FANS | \
702                                                      FEAT_SIX_FANS))
703 #define has_vid(data)           ((data)->features & FEAT_VID)
704 #define has_in7_internal(data)  ((data)->features & FEAT_IN7_INTERNAL)
705 #define has_six_fans(data)      ((data)->features & FEAT_SIX_FANS)
706 #define has_avcc3(data)         ((data)->features & FEAT_AVCC3)
707 #define has_five_pwm(data)      ((data)->features & (FEAT_FIVE_PWM \
708                                                      | FEAT_SIX_PWM))
709 #define has_six_pwm(data)       ((data)->features & FEAT_SIX_PWM)
710 #define has_pwm_freq2(data)     ((data)->features & FEAT_PWM_FREQ2)
711 #define has_six_temp(data)      ((data)->features & FEAT_SIX_TEMP)
712 #define has_vin3_5v(data)       ((data)->features & FEAT_VIN3_5V)
713 #define has_four_fans(data)     ((data)->features & (FEAT_FOUR_FANS | \
714                                                      FEAT_FIVE_FANS | \
715                                                      FEAT_SIX_FANS))
716 #define has_four_pwm(data)      ((data)->features & (FEAT_FOUR_PWM | \
717                                                      FEAT_FIVE_PWM \
718                                                      | FEAT_SIX_PWM))
719 #define has_bank_sel(data)      ((data)->features & FEAT_BANK_SEL)
720 #define has_scaling(data)       ((data)->features & FEAT_SCALING)
721 #define has_fanctl_onoff(data)  ((data)->features & FEAT_FANCTL_ONOFF)
722 #define has_11mv_adc(data)      ((data)->features & FEAT_11MV_ADC)
723 #define has_new_tempmap(data)   ((data)->features & FEAT_NEW_TEMPMAP)
724 #define has_mmio(data)          ((data)->features & FEAT_MMIO)
725 #define has_four_temp(data)     ((data)->features & FEAT_FOUR_TEMP)
726
727 struct it87_sio_data {
728         enum chips type;
729         u8 sioaddr;
730         u8 doexit;
731         /* Values read from Super-I/O config space */
732         u8 revision;
733         u8 vid_value;
734         u8 beep_pin;
735         u8 internal;    /* Internal sensors can be labeled */
736         /* Features skipped based on config or DMI */
737         u16 skip_in;
738         u8 skip_vid;
739         u8 skip_fan;
740         u8 skip_pwm;
741         u8 skip_temp;
742         u8 smbus_bitmap;
743         u8 ec_special_config;
744 };
745
746 /*
747  * For each registered chip, we need to keep some data in memory.
748  * The structure is dynamically allocated.
749  */
750 struct it87_data {
751         const struct attribute_group *groups[7];
752         enum chips type;
753         u32 features;
754         u8 peci_mask;
755         u8 old_peci_mask;
756
757         u8 smbus_bitmap;        /* !=0 if SMBus needs to be disabled */
758         u8 ec_special_config;   /* EC special config register restore value */
759         u8 sioaddr;             /* SIO port address */
760         bool doexit;            /* true if exit from sio config is ok */
761
762         void __iomem *mmio;     /* Remapped MMIO address if available */
763         int (*read)(struct it87_data *, u16);
764         void (*write)(struct it87_data *, u16, u8);
765
766         const u8 *REG_FAN;
767         const u8 *REG_FANX;
768         const u8 *REG_FAN_MIN;
769         const u8 *REG_FANX_MIN;
770
771         const u8 *REG_PWM;
772
773         const u8 *REG_TEMP_OFFSET;
774         const u8 *REG_TEMP_LOW;
775         const u8 *REG_TEMP_HIGH;
776
777         unsigned short addr;
778         const char *name;
779         struct mutex update_lock;
780         char valid;             /* !=0 if following fields are valid */
781         unsigned long last_updated;     /* In jiffies */
782
783         u16 in_scaled;          /* Internal voltage sensors are scaled */
784         u16 in_internal;        /* Bitfield, internal sensors (for labels) */
785         u16 has_in;             /* Bitfield, voltage sensors enabled */
786         u8 in[NUM_VIN][3];              /* [nr][0]=in, [1]=min, [2]=max */
787         u8 has_fan;             /* Bitfield, fans enabled */
788         u16 fan[NUM_FAN][2];    /* Register values, [nr][0]=fan, [1]=min */
789         u8 has_temp;            /* Bitfield, temp sensors enabled */
790         s8 temp[NUM_TEMP][4];   /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
791         u8 num_temp_limit;      /* Number of temperature limit registers */
792         u8 num_temp_offset;     /* Number of temperature offset registers */
793         u8 temp_src[4];         /* Up to 4 temperature source registers */
794         u8 sensor;              /* Register value (IT87_REG_TEMP_ENABLE) */
795         u8 extra;               /* Register value (IT87_REG_TEMP_EXTRA) */
796         u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
797         bool has_vid;           /* True if VID supported */
798         u8 vid;                 /* Register encoding, combined */
799         u8 vrm;
800         u32 alarms;             /* Register encoding, combined */
801         bool has_beep;          /* true if beep supported */
802         u8 beeps;               /* Register encoding */
803         u8 fan_main_ctrl;       /* Register value */
804         u8 fan_ctl;             /* Register value */
805
806         /*
807          * The following 3 arrays correspond to the same registers up to
808          * the IT8720F. The meaning of bits 6-0 depends on the value of bit
809          * 7, and we want to preserve settings on mode changes, so we have
810          * to track all values separately.
811          * Starting with the IT8721F, the manual PWM duty cycles are stored
812          * in separate registers (8-bit values), so the separate tracking
813          * is no longer needed, but it is still done to keep the driver
814          * simple.
815          */
816         u8 has_pwm;             /* Bitfield, pwm control enabled */
817         u8 pwm_ctrl[NUM_PWM];   /* Register value */
818         u8 pwm_duty[NUM_PWM];   /* Manual PWM value set by user */
819         u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
820         u8 pwm_temp_map_mask;   /* 0x03 for old, 0x07 for new temp map */
821         u8 pwm_temp_map_shift;  /* 0 for old, 3 for new temp map */
822         u8 pwm_num_temp_map;    /* from config data, 3..7 depending on chip */
823
824         /* Automatic fan speed control registers */
825         u8 auto_pwm[NUM_AUTO_PWM][4];   /* [nr][3] is hard-coded */
826         s8 auto_temp[NUM_AUTO_PWM][5];  /* [nr][0] is point1_temp_hyst */
827 };
828
829 static int adc_lsb(const struct it87_data *data, int nr)
830 {
831         int lsb;
832
833         if (has_12mv_adc(data))
834                 lsb = 120;
835         else if (has_10_9mv_adc(data))
836                 lsb = 109;
837         else if (has_11mv_adc(data))
838                 lsb = 110;
839         else
840                 lsb = 160;
841         if (data->in_scaled & BIT(nr))
842                 lsb <<= 1;
843         return lsb;
844 }
845
846 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
847 {
848         val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
849         return clamp_val(val, 0, 255);
850 }
851
852 static int in_from_reg(const struct it87_data *data, int nr, int val)
853 {
854         return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
855 }
856
857 static inline u8 FAN_TO_REG(long rpm, int div)
858 {
859         if (rpm == 0)
860                 return 255;
861         rpm = clamp_val(rpm, 1, 1000000);
862         return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
863 }
864
865 static inline u16 FAN16_TO_REG(long rpm)
866 {
867         if (rpm == 0)
868                 return 0xffff;
869         return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
870 }
871
872 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
873                                 1350000 / ((val) * (div)))
874 /* The divider is fixed to 2 in 16-bit mode */
875 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
876                              1350000 / ((val) * 2))
877
878 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
879                                     ((val) + 500) / 1000), -128, 127))
880 #define TEMP_FROM_REG(val) ((val) * 1000)
881
882 static u8 pwm_to_reg(const struct it87_data *data, long val)
883 {
884         if (has_newer_autopwm(data))
885                 return val;
886         else
887                 return val >> 1;
888 }
889
890 static int pwm_from_reg(const struct it87_data *data, u8 reg)
891 {
892         if (has_newer_autopwm(data))
893                 return reg;
894         else
895                 return (reg & 0x7f) << 1;
896 }
897
898 static int DIV_TO_REG(int val)
899 {
900         int answer = 0;
901
902         while (answer < 7 && (val >>= 1))
903                 answer++;
904         return answer;
905 }
906
907 #define DIV_FROM_REG(val) BIT(val)
908
909 static u8 temp_map_from_reg(const struct it87_data *data, u8 reg)
910 {
911         u8 map;
912
913         map  = (reg >> data->pwm_temp_map_shift) & data->pwm_temp_map_mask;
914         if (map >= data->pwm_num_temp_map)      /* map is 0-based */
915                 map = 0;
916
917         return map;
918 }
919
920 static u8 temp_map_to_reg(const struct it87_data *data, int nr, u8 map)
921 {
922         u8 ctrl = data->pwm_ctrl[nr];
923
924         return (ctrl & ~(data->pwm_temp_map_mask << data->pwm_temp_map_shift)) |
925                (map << data->pwm_temp_map_shift);
926 }
927
928 /*
929  * PWM base frequencies. The frequency has to be divided by either 128 or 256,
930  * depending on the chip type, to calculate the actual PWM frequency.
931  *
932  * Some of the chip datasheets suggest a base frequency of 51 kHz instead
933  * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
934  * of 200 Hz. Sometimes both PWM frequency select registers are affected,
935  * sometimes just one. It is unknown if this is a datasheet error or real,
936  * so this is ignored for now.
937  */
938 static const unsigned int pwm_freq[8] = {
939         48000000,
940         24000000,
941         12000000,
942         8000000,
943         6000000,
944         3000000,
945         1500000,
946         750000,
947 };
948
949 static int smbus_disable(struct it87_data *data)
950 {
951         int err;
952
953         if (data->smbus_bitmap) {
954                 err = superio_enter(data->sioaddr);
955                 if (err)
956                         return err;
957                 superio_select(data->sioaddr, PME);
958                 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
959                              data->ec_special_config & ~data->smbus_bitmap);
960                 superio_exit(data->sioaddr, data->doexit);
961         }
962         return 0;
963 }
964
965 static int smbus_enable(struct it87_data *data)
966 {
967         int err;
968
969         if (data->smbus_bitmap) {
970                 err = superio_enter(data->sioaddr);
971                 if (err)
972                         return err;
973
974                 superio_select(data->sioaddr, PME);
975                 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
976                              data->ec_special_config);
977                 superio_exit(data->sioaddr, data->doexit);
978         }
979         return 0;
980 }
981
982 static int _it87_io_read(struct it87_data *data, u16 reg)
983 {
984         outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
985         return inb_p(data->addr + IT87_DATA_REG_OFFSET);
986 }
987
988 static void _it87_io_write(struct it87_data *data, u16 reg, u8 value)
989 {
990         outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
991         outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
992 }
993
994 static u8 it87_io_set_bank(struct it87_data *data, u8 bank)
995 {
996         u8 _bank = bank;
997
998         if (has_bank_sel(data)) {
999                 u8 breg = _it87_io_read(data, IT87_REG_BANK);
1000
1001                 _bank = breg >> 5;
1002                 if (bank != _bank) {
1003                         breg &= 0x1f;
1004                         breg |= (bank << 5);
1005                         _it87_io_write(data, IT87_REG_BANK, breg);
1006                 }
1007         }
1008         return _bank;
1009 }
1010
1011 /*
1012  * Must be called with data->update_lock held, except during initialization.
1013  * Must be called with SMBus accesses disabled.
1014  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1015  * would slow down the IT87 access and should not be necessary.
1016  */
1017 static int it87_io_read(struct it87_data *data, u16 reg)
1018 {
1019         u8 bank;
1020         int val;
1021
1022         bank = it87_io_set_bank(data, reg >> 8);
1023         val = _it87_io_read(data, reg & 0xff);
1024         it87_io_set_bank(data, bank);
1025
1026         return val;
1027 }
1028
1029 /*
1030  * Must be called with data->update_lock held, except during initialization.
1031  * Must be called with SMBus accesses disabled
1032  * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1033  * would slow down the IT87 access and should not be necessary.
1034  */
1035 static void it87_io_write(struct it87_data *data, u16 reg, u8 value)
1036 {
1037         u8 bank;
1038
1039         bank = it87_io_set_bank(data, reg >> 8);
1040         _it87_io_write(data, reg & 0xff, value);
1041         it87_io_set_bank(data, bank);
1042 }
1043
1044 static int it87_mmio_read(struct it87_data *data, u16 reg)
1045 {
1046         return readb(data->mmio + reg);
1047 }
1048
1049 static void it87_mmio_write(struct it87_data *data, u16 reg, u8 value)
1050 {
1051         writeb(value, data->mmio + reg);
1052 }
1053
1054 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1055 {
1056         u8 ctrl;
1057
1058         ctrl = data->read(data, data->REG_PWM[nr]);
1059         data->pwm_ctrl[nr] = ctrl;
1060         if (has_newer_autopwm(data)) {
1061                 data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1062                 data->pwm_duty[nr] = data->read(data, IT87_REG_PWM_DUTY[nr]);
1063         } else {
1064                 if (ctrl & 0x80)        /* Automatic mode */
1065                         data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1066                 else                            /* Manual mode */
1067                         data->pwm_duty[nr] = ctrl & 0x7f;
1068         }
1069
1070         if (has_old_autopwm(data)) {
1071                 int i;
1072
1073                 for (i = 0; i < 5 ; i++)
1074                         data->auto_temp[nr][i] = data->read(data,
1075                                                 IT87_REG_AUTO_TEMP(nr, i));
1076                 for (i = 0; i < 3 ; i++)
1077                         data->auto_pwm[nr][i] = data->read(data,
1078                                                 IT87_REG_AUTO_PWM(nr, i));
1079         } else if (has_newer_autopwm(data)) {
1080                 int i;
1081
1082                 /*
1083                  * 0: temperature hysteresis (base + 5)
1084                  * 1: fan off temperature (base + 0)
1085                  * 2: fan start temperature (base + 1)
1086                  * 3: fan max temperature (base + 2)
1087                  */
1088                 data->auto_temp[nr][0] =
1089                         data->read(data, IT87_REG_AUTO_TEMP(nr, 5));
1090
1091                 for (i = 0; i < 3 ; i++)
1092                         data->auto_temp[nr][i + 1] =
1093                                 data->read(data, IT87_REG_AUTO_TEMP(nr, i));
1094                 /*
1095                  * 0: start pwm value (base + 3)
1096                  * 1: pwm slope (base + 4, 1/8th pwm)
1097                  */
1098                 data->auto_pwm[nr][0] =
1099                         data->read(data, IT87_REG_AUTO_TEMP(nr, 3));
1100                 data->auto_pwm[nr][1] =
1101                         data->read(data, IT87_REG_AUTO_TEMP(nr, 4));
1102         }
1103 }
1104
1105 static int it87_lock(struct it87_data *data)
1106 {
1107         int err;
1108
1109         mutex_lock(&data->update_lock);
1110         err = smbus_disable(data);
1111         if (err)
1112                 mutex_unlock(&data->update_lock);
1113         return err;
1114 }
1115
1116 static void it87_unlock(struct it87_data *data)
1117 {
1118         smbus_enable(data);
1119         mutex_unlock(&data->update_lock);
1120 }
1121
1122 static struct it87_data *it87_update_device(struct device *dev)
1123 {
1124         struct it87_data *data = dev_get_drvdata(dev);
1125         int err;
1126         int i;
1127
1128         err = it87_lock(data);
1129         if (err)
1130                 return ERR_PTR(err);
1131
1132         if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
1133             !data->valid) {
1134                 if (update_vbat) {
1135                         /*
1136                          * Cleared after each update, so reenable.  Value
1137                          * returned by this read will be previous value
1138                          */
1139                         data->write(data, IT87_REG_CONFIG,
1140                                     data->read(data, IT87_REG_CONFIG) | 0x40);
1141                 }
1142                 for (i = 0; i < NUM_VIN; i++) {
1143                         if (!(data->has_in & BIT(i)))
1144                                 continue;
1145
1146                         data->in[i][0] = data->read(data, IT87_REG_VIN[i]);
1147
1148                         /* VBAT and AVCC don't have limit registers */
1149                         if (i >= NUM_VIN_LIMIT)
1150                                 continue;
1151
1152                         data->in[i][1] = data->read(data, IT87_REG_VIN_MIN(i));
1153                         data->in[i][2] = data->read(data, IT87_REG_VIN_MAX(i));
1154                 }
1155
1156                 for (i = 0; i < NUM_FAN; i++) {
1157                         /* Skip disabled fans */
1158                         if (!(data->has_fan & BIT(i)))
1159                                 continue;
1160
1161                         data->fan[i][1] = data->read(data,
1162                                                      data->REG_FAN_MIN[i]);
1163                         data->fan[i][0] = data->read(data, data->REG_FAN[i]);
1164                         /* Add high byte if in 16-bit mode */
1165                         if (has_16bit_fans(data)) {
1166                                 data->fan[i][0] |= data->read(data,
1167                                                 data->REG_FANX[i]) << 8;
1168                                 data->fan[i][1] |= data->read(data,
1169                                                 data->REG_FANX_MIN[i]) << 8;
1170                         }
1171                 }
1172                 for (i = 0; i < NUM_TEMP; i++) {
1173                         if (!(data->has_temp & BIT(i)))
1174                                 continue;
1175                         data->temp[i][0] =
1176                                 data->read(data, IT87_REG_TEMP(i));
1177
1178                         if (i >= data->num_temp_limit)
1179                                 continue;
1180
1181                         if (i < data->num_temp_offset)
1182                                 data->temp[i][3] =
1183                                   data->read(data, data->REG_TEMP_OFFSET[i]);
1184
1185                         data->temp[i][1] =
1186                                 data->read(data, data->REG_TEMP_LOW[i]);
1187                         data->temp[i][2] =
1188                                 data->read(data, data->REG_TEMP_HIGH[i]);
1189                 }
1190
1191                 /* Newer chips don't have clock dividers */
1192                 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1193                         i = data->read(data, IT87_REG_FAN_DIV);
1194                         data->fan_div[0] = i & 0x07;
1195                         data->fan_div[1] = (i >> 3) & 0x07;
1196                         data->fan_div[2] = (i & 0x40) ? 3 : 1;
1197                 }
1198
1199                 data->alarms =
1200                         data->read(data, IT87_REG_ALARM1) |
1201                         (data->read(data, IT87_REG_ALARM2) << 8) |
1202                         (data->read(data, IT87_REG_ALARM3) << 16);
1203                 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
1204
1205                 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
1206                 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL);
1207                 for (i = 0; i < NUM_PWM; i++) {
1208                         if (!(data->has_pwm & BIT(i)))
1209                                 continue;
1210                         it87_update_pwm_ctrl(data, i);
1211                 }
1212
1213                 data->sensor = data->read(data, IT87_REG_TEMP_ENABLE);
1214                 data->extra = data->read(data, IT87_REG_TEMP_EXTRA);
1215                 /*
1216                  * The IT8705F does not have VID capability.
1217                  * The IT8718F and later don't use IT87_REG_VID for the
1218                  * same purpose.
1219                  */
1220                 if (data->type == it8712 || data->type == it8716) {
1221                         data->vid = data->read(data, IT87_REG_VID);
1222                         /*
1223                          * The older IT8712F revisions had only 5 VID pins,
1224                          * but we assume it is always safe to read 6 bits.
1225                          */
1226                         data->vid &= 0x3f;
1227                 }
1228                 data->last_updated = jiffies;
1229                 data->valid = 1;
1230         }
1231         it87_unlock(data);
1232         return data;
1233 }
1234
1235 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
1236                        char *buf)
1237 {
1238         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1239         struct it87_data *data = it87_update_device(dev);
1240         int index = sattr->index;
1241         int nr = sattr->nr;
1242
1243         if (IS_ERR(data))
1244                 return PTR_ERR(data);
1245
1246         return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
1247 }
1248
1249 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
1250                       const char *buf, size_t count)
1251 {
1252         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1253         struct it87_data *data = dev_get_drvdata(dev);
1254         int index = sattr->index;
1255         int nr = sattr->nr;
1256         unsigned long val;
1257         int err;
1258
1259         if (kstrtoul(buf, 10, &val) < 0)
1260                 return -EINVAL;
1261
1262         err = it87_lock(data);
1263         if (err)
1264                 return err;
1265
1266         data->in[nr][index] = in_to_reg(data, nr, val);
1267         data->write(data, index == 1 ? IT87_REG_VIN_MIN(nr)
1268                                      : IT87_REG_VIN_MAX(nr),
1269                     data->in[nr][index]);
1270         it87_unlock(data);
1271         return count;
1272 }
1273
1274 static SENSOR_DEVICE_ATTR_2(in0_input, 0444, show_in, NULL, 0, 0);
1275 static SENSOR_DEVICE_ATTR_2(in0_min, 0644, show_in, set_in, 0, 1);
1276 static SENSOR_DEVICE_ATTR_2(in0_max, 0644, show_in, set_in, 0, 2);
1277
1278 static SENSOR_DEVICE_ATTR_2(in1_input, 0444, show_in, NULL, 1, 0);
1279 static SENSOR_DEVICE_ATTR_2(in1_min, 0644, show_in, set_in, 1, 1);
1280 static SENSOR_DEVICE_ATTR_2(in1_max, 0644, show_in, set_in, 1, 2);
1281
1282 static SENSOR_DEVICE_ATTR_2(in2_input, 0444, show_in, NULL, 2, 0);
1283 static SENSOR_DEVICE_ATTR_2(in2_min, 0644, show_in, set_in, 2, 1);
1284 static SENSOR_DEVICE_ATTR_2(in2_max, 0644, show_in, set_in, 2, 2);
1285
1286 static SENSOR_DEVICE_ATTR_2(in3_input, 0444, show_in, NULL, 3, 0);
1287 static SENSOR_DEVICE_ATTR_2(in3_min, 0644, show_in, set_in, 3, 1);
1288 static SENSOR_DEVICE_ATTR_2(in3_max, 0644, show_in, set_in, 3, 2);
1289
1290 static SENSOR_DEVICE_ATTR_2(in4_input, 0444, show_in, NULL, 4, 0);
1291 static SENSOR_DEVICE_ATTR_2(in4_min, 0644, show_in, set_in, 4, 1);
1292 static SENSOR_DEVICE_ATTR_2(in4_max, 0644, show_in, set_in, 4, 2);
1293
1294 static SENSOR_DEVICE_ATTR_2(in5_input, 0444, show_in, NULL, 5, 0);
1295 static SENSOR_DEVICE_ATTR_2(in5_min, 0644, show_in, set_in, 5, 1);
1296 static SENSOR_DEVICE_ATTR_2(in5_max, 0644, show_in, set_in, 5, 2);
1297
1298 static SENSOR_DEVICE_ATTR_2(in6_input, 0444, show_in, NULL, 6, 0);
1299 static SENSOR_DEVICE_ATTR_2(in6_min, 0644, show_in, set_in, 6, 1);
1300 static SENSOR_DEVICE_ATTR_2(in6_max, 0644, show_in, set_in, 6, 2);
1301
1302 static SENSOR_DEVICE_ATTR_2(in7_input, 0444, show_in, NULL, 7, 0);
1303 static SENSOR_DEVICE_ATTR_2(in7_min, 0644, show_in, set_in, 7, 1);
1304 static SENSOR_DEVICE_ATTR_2(in7_max, 0644, show_in, set_in, 7, 2);
1305
1306 static SENSOR_DEVICE_ATTR_2(in8_input, 0444, show_in, NULL, 8, 0);
1307 static SENSOR_DEVICE_ATTR_2(in9_input, 0444, show_in, NULL, 9, 0);
1308 static SENSOR_DEVICE_ATTR_2(in10_input, 0444, show_in, NULL, 10, 0);
1309 static SENSOR_DEVICE_ATTR_2(in11_input, 0444, show_in, NULL, 11, 0);
1310 static SENSOR_DEVICE_ATTR_2(in12_input, 0444, show_in, NULL, 12, 0);
1311
1312 /* Up to 6 temperatures */
1313 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
1314                          char *buf)
1315 {
1316         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1317         int nr = sattr->nr;
1318         int index = sattr->index;
1319         struct it87_data *data = it87_update_device(dev);
1320
1321         if (IS_ERR(data))
1322                 return PTR_ERR(data);
1323
1324         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
1325 }
1326
1327 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
1328                         const char *buf, size_t count)
1329 {
1330         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1331         int nr = sattr->nr;
1332         int index = sattr->index;
1333         struct it87_data *data = dev_get_drvdata(dev);
1334         long val;
1335         u8 reg, regval;
1336         int err;
1337
1338         if (kstrtol(buf, 10, &val) < 0)
1339                 return -EINVAL;
1340
1341         err = it87_lock(data);
1342         if (err)
1343                 return err;
1344
1345         switch (index) {
1346         default:
1347         case 1:
1348                 reg = data->REG_TEMP_LOW[nr];
1349                 break;
1350         case 2:
1351                 reg = data->REG_TEMP_HIGH[nr];
1352                 break;
1353         case 3:
1354                 regval = data->read(data, IT87_REG_BEEP_ENABLE);
1355                 if (!(regval & 0x80)) {
1356                         regval |= 0x80;
1357                         data->write(data, IT87_REG_BEEP_ENABLE, regval);
1358                 }
1359                 data->valid = 0;
1360                 reg = data->REG_TEMP_OFFSET[nr];
1361                 break;
1362         }
1363
1364         data->temp[nr][index] = TEMP_TO_REG(val);
1365         data->write(data, reg, data->temp[nr][index]);
1366         it87_unlock(data);
1367         return count;
1368 }
1369
1370 static SENSOR_DEVICE_ATTR_2(temp1_input, 0444, show_temp, NULL, 0, 0);
1371 static SENSOR_DEVICE_ATTR_2(temp1_min, 0644, show_temp, set_temp, 0, 1);
1372 static SENSOR_DEVICE_ATTR_2(temp1_max, 0644, show_temp, set_temp, 0, 2);
1373 static SENSOR_DEVICE_ATTR_2(temp1_offset, 0644, show_temp, set_temp, 0, 3);
1374 static SENSOR_DEVICE_ATTR_2(temp2_input, 0444, show_temp, NULL, 1, 0);
1375 static SENSOR_DEVICE_ATTR_2(temp2_min, 0644, show_temp, set_temp, 1, 1);
1376 static SENSOR_DEVICE_ATTR_2(temp2_max, 0644, show_temp, set_temp, 1, 2);
1377 static SENSOR_DEVICE_ATTR_2(temp2_offset, 0644, show_temp, set_temp, 1, 3);
1378 static SENSOR_DEVICE_ATTR_2(temp3_input, 0444, show_temp, NULL, 2, 0);
1379 static SENSOR_DEVICE_ATTR_2(temp3_min, 0644, show_temp, set_temp, 2, 1);
1380 static SENSOR_DEVICE_ATTR_2(temp3_max, 0644, show_temp, set_temp, 2, 2);
1381 static SENSOR_DEVICE_ATTR_2(temp3_offset, 0644, show_temp, set_temp, 2, 3);
1382 static SENSOR_DEVICE_ATTR_2(temp4_input, 0444, show_temp, NULL, 3, 0);
1383 static SENSOR_DEVICE_ATTR_2(temp4_min, 0644, show_temp, set_temp, 3, 1);
1384 static SENSOR_DEVICE_ATTR_2(temp4_max, 0644, show_temp, set_temp, 3, 2);
1385 static SENSOR_DEVICE_ATTR_2(temp4_offset, 0644, show_temp, set_temp, 3, 3);
1386 static SENSOR_DEVICE_ATTR_2(temp5_input, 0444, show_temp, NULL, 4, 0);
1387 static SENSOR_DEVICE_ATTR_2(temp5_min, 0644, show_temp, set_temp, 4, 1);
1388 static SENSOR_DEVICE_ATTR_2(temp5_max, 0644, show_temp, set_temp, 4, 2);
1389 static SENSOR_DEVICE_ATTR_2(temp5_offset, 0644, show_temp, set_temp, 4, 3);
1390 static SENSOR_DEVICE_ATTR_2(temp6_input, 0444, show_temp, NULL, 5, 0);
1391 static SENSOR_DEVICE_ATTR_2(temp6_min, 0644, show_temp, set_temp, 5, 1);
1392 static SENSOR_DEVICE_ATTR_2(temp6_max, 0644, show_temp, set_temp, 5, 2);
1393 static SENSOR_DEVICE_ATTR_2(temp6_offset, 0644, show_temp, set_temp, 5, 3);
1394
1395 static const u8 temp_types_8686[NUM_TEMP][9] = {
1396         { 0, 8, 8, 8, 8, 8, 8, 8, 7 },
1397         { 0, 6, 8, 8, 6, 0, 0, 0, 7 },
1398         { 0, 6, 5, 8, 6, 0, 0, 0, 7 },
1399         { 4, 8, 8, 8, 8, 8, 8, 8, 7 },
1400         { 4, 6, 8, 8, 6, 0, 0, 0, 7 },
1401         { 4, 6, 5, 8, 6, 0, 0, 0, 7 },
1402 };
1403
1404 static int get_temp_type(struct it87_data *data, int index)
1405 {
1406         u8 reg, extra;
1407         int ttype, type = 0;
1408
1409         if (has_bank_sel(data)) {
1410                 u8 src1, src2;
1411
1412                 src1 = (data->temp_src[index / 2] >> ((index % 2) * 4)) & 0x0f;
1413
1414                 switch (data->type) {
1415                 case it8686:
1416                         if (src1 < 9)
1417                                 type = temp_types_8686[index][src1];
1418                         break;
1419                 case it8625:
1420                         if (index < 3)
1421                                 break;
1422                 case it8655:
1423                 case it8665:
1424                         if (src1 < 3) {
1425                                 index = src1;
1426                                 break;
1427                         }
1428                         src2 = data->temp_src[3];
1429                         switch (src1) {
1430                         case 3:
1431                                 type = (src2 & BIT(index)) ? 6 : 5;
1432                                 break;
1433                         case 4 ... 8:
1434                                 type = (src2 & BIT(index)) ? 4 : 6;
1435                                 break;
1436                         case 9:
1437                                 type = (src2 & BIT(index)) ? 5 : 0;
1438                                 break;
1439                         default:
1440                                 break;
1441                         }
1442                         return type;
1443                 default:
1444                         return 0;
1445                 }
1446         }
1447         if (type)
1448                 return type;
1449
1450         /* Dectect PECI vs. AMDTSI if possible */
1451         ttype = 6;
1452         if ((has_temp_peci(data, index)) && data->type != it8721) {
1453                 extra = data->read(data, 0x98); /* PCH/AMDTSI host status */
1454                 if (extra & BIT(6))
1455                         ttype = 5;
1456         }
1457
1458         reg = data->read(data, IT87_REG_TEMP_ENABLE);
1459
1460         /* Per chip special detection */
1461         switch (data->type) {
1462         case it8622:
1463                 if (!(reg & 0xc0) && index == 3)
1464                         type = ttype;
1465                 break;
1466         default:
1467                 break;
1468         }
1469
1470         if (type || index >= 3)
1471                 return type;
1472
1473         extra = data->read(data, IT87_REG_TEMP_EXTRA);
1474
1475         if ((has_temp_peci(data, index) && (reg >> 6 == index + 1)) ||
1476             (has_temp_old_peci(data, index) && (extra & 0x80)))
1477                 type = ttype;           /* Intel PECI or AMDTSI */
1478         if (reg & BIT(index))
1479                 type = 3;               /* thermal diode */
1480         else if (reg & BIT(index + 3))
1481                 type = 4;               /* thermistor */
1482
1483         return type;
1484 }
1485
1486 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1487                               char *buf)
1488 {
1489         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1490         struct it87_data *data = it87_update_device(dev);
1491         int type;
1492
1493         if (IS_ERR(data))
1494                 return PTR_ERR(data);
1495
1496         type = get_temp_type(data, sensor_attr->index);
1497         return sprintf(buf, "%d\n", type);
1498 }
1499
1500 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1501                              const char *buf, size_t count)
1502 {
1503         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1504         int nr = sensor_attr->index;
1505
1506         struct it87_data *data = dev_get_drvdata(dev);
1507         long val;
1508         u8 reg, extra;
1509         int err;
1510
1511         if (kstrtol(buf, 10, &val) < 0)
1512                 return -EINVAL;
1513
1514         err = it87_lock(data);
1515         if (err)
1516                 return err;
1517
1518         reg = data->read(data, IT87_REG_TEMP_ENABLE);
1519         reg &= ~(1 << nr);
1520         reg &= ~(8 << nr);
1521         if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1522                 reg &= 0x3f;
1523         extra = data->read(data, IT87_REG_TEMP_EXTRA);
1524         if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1525                 extra &= 0x7f;
1526         if (val == 2) { /* backwards compatibility */
1527                 dev_warn(dev,
1528                          "Sensor type 2 is deprecated, please use 4 instead\n");
1529                 val = 4;
1530         }
1531         /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1532         if (val == 3)
1533                 reg |= 1 << nr;
1534         else if (val == 4)
1535                 reg |= 8 << nr;
1536         else if (has_temp_peci(data, nr) && val == 6)
1537                 reg |= (nr + 1) << 6;
1538         else if (has_temp_old_peci(data, nr) && val == 6)
1539                 extra |= 0x80;
1540         else if (val != 0) {
1541                 count = -EINVAL;
1542                 goto unlock;
1543         }
1544
1545         data->sensor = reg;
1546         data->extra = extra;
1547         data->write(data, IT87_REG_TEMP_ENABLE, data->sensor);
1548         if (has_temp_old_peci(data, nr))
1549                 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1550         data->valid = 0;        /* Force cache refresh */
1551 unlock:
1552         it87_unlock(data);
1553         return count;
1554 }
1555
1556 static SENSOR_DEVICE_ATTR(temp1_type, 0644, show_temp_type, set_temp_type, 0);
1557 static SENSOR_DEVICE_ATTR(temp2_type, 0644, show_temp_type, set_temp_type, 1);
1558 static SENSOR_DEVICE_ATTR(temp3_type, 0644, show_temp_type, set_temp_type, 2);
1559 static SENSOR_DEVICE_ATTR(temp4_type, 0644, show_temp_type, set_temp_type, 3);
1560 static SENSOR_DEVICE_ATTR(temp5_type, 0644, show_temp_type, set_temp_type, 4);
1561 static SENSOR_DEVICE_ATTR(temp6_type, 0644, show_temp_type, set_temp_type, 5);
1562
1563 /* 6 Fans */
1564
1565 static int pwm_mode(const struct it87_data *data, int nr)
1566 {
1567         if (has_fanctl_onoff(data) && nr < 3 &&
1568             !(data->fan_main_ctrl & BIT(nr)))
1569                 return 0;                               /* Full speed */
1570         if (data->pwm_ctrl[nr] & 0x80)
1571                 return 2;                               /* Automatic mode */
1572         if ((!has_fanctl_onoff(data) || nr >= 3) &&
1573             data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1574                 return 0;                       /* Full speed */
1575
1576         return 1;                               /* Manual mode */
1577 }
1578
1579 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1580                         char *buf)
1581 {
1582         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1583         int nr = sattr->nr;
1584         int index = sattr->index;
1585         int speed;
1586         struct it87_data *data = it87_update_device(dev);
1587
1588         if (IS_ERR(data))
1589                 return PTR_ERR(data);
1590
1591         speed = has_16bit_fans(data) ?
1592                 FAN16_FROM_REG(data->fan[nr][index]) :
1593                 FAN_FROM_REG(data->fan[nr][index],
1594                              DIV_FROM_REG(data->fan_div[nr]));
1595         return sprintf(buf, "%d\n", speed);
1596 }
1597
1598 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1599                             char *buf)
1600 {
1601         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1602         struct it87_data *data = it87_update_device(dev);
1603         int nr = sensor_attr->index;
1604
1605         if (IS_ERR(data))
1606                 return PTR_ERR(data);
1607
1608         return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1609 }
1610
1611 static ssize_t show_pwm_enable(struct device *dev,
1612                                struct device_attribute *attr, char *buf)
1613 {
1614         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1615         struct it87_data *data = it87_update_device(dev);
1616         int nr = sensor_attr->index;
1617
1618         if (IS_ERR(data))
1619                 return PTR_ERR(data);
1620
1621         return sprintf(buf, "%d\n", pwm_mode(data, nr));
1622 }
1623
1624 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1625                         char *buf)
1626 {
1627         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1628         struct it87_data *data = it87_update_device(dev);
1629         int nr = sensor_attr->index;
1630
1631         if (IS_ERR(data))
1632                 return PTR_ERR(data);
1633
1634         return sprintf(buf, "%d\n",
1635                        pwm_from_reg(data, data->pwm_duty[nr]));
1636 }
1637
1638 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1639                              char *buf)
1640 {
1641         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1642         struct it87_data *data = it87_update_device(dev);
1643         int nr = sensor_attr->index;
1644         unsigned int freq;
1645         int index;
1646
1647         if (IS_ERR(data))
1648                 return PTR_ERR(data);
1649
1650         if (has_pwm_freq2(data) && nr == 1)
1651                 index = (data->extra >> 4) & 0x07;
1652         else
1653                 index = (data->fan_ctl >> 4) & 0x07;
1654
1655         freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1656
1657         return sprintf(buf, "%u\n", freq);
1658 }
1659
1660 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1661                        const char *buf, size_t count)
1662 {
1663         struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1664         int nr = sattr->nr;
1665         int index = sattr->index;
1666
1667         struct it87_data *data = dev_get_drvdata(dev);
1668         long val;
1669         int err;
1670         u8 reg;
1671
1672         if (kstrtol(buf, 10, &val) < 0)
1673                 return -EINVAL;
1674
1675         err = it87_lock(data);
1676         if (err)
1677                 return err;
1678
1679         if (has_16bit_fans(data)) {
1680                 data->fan[nr][index] = FAN16_TO_REG(val);
1681                 data->write(data, data->REG_FAN_MIN[nr],
1682                             data->fan[nr][index] & 0xff);
1683                 data->write(data, data->REG_FANX_MIN[nr],
1684                             data->fan[nr][index] >> 8);
1685         } else {
1686                 reg = data->read(data, IT87_REG_FAN_DIV);
1687                 switch (nr) {
1688                 case 0:
1689                         data->fan_div[nr] = reg & 0x07;
1690                         break;
1691                 case 1:
1692                         data->fan_div[nr] = (reg >> 3) & 0x07;
1693                         break;
1694                 case 2:
1695                         data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1696                         break;
1697                 }
1698                 data->fan[nr][index] =
1699                   FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1700                 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][index]);
1701         }
1702         it87_unlock(data);
1703         return count;
1704 }
1705
1706 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1707                            const char *buf, size_t count)
1708 {
1709         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1710         struct it87_data *data = dev_get_drvdata(dev);
1711         int nr = sensor_attr->index;
1712         unsigned long val;
1713         int min, err;
1714         u8 old;
1715
1716         if (kstrtoul(buf, 10, &val) < 0)
1717                 return -EINVAL;
1718
1719         err = it87_lock(data);
1720         if (err)
1721                 return err;
1722
1723         old = data->read(data, IT87_REG_FAN_DIV);
1724
1725         /* Save fan min limit */
1726         min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1727
1728         switch (nr) {
1729         case 0:
1730         case 1:
1731                 data->fan_div[nr] = DIV_TO_REG(val);
1732                 break;
1733         case 2:
1734                 if (val < 8)
1735                         data->fan_div[nr] = 1;
1736                 else
1737                         data->fan_div[nr] = 3;
1738         }
1739         val = old & 0x80;
1740         val |= (data->fan_div[0] & 0x07);
1741         val |= (data->fan_div[1] & 0x07) << 3;
1742         if (data->fan_div[2] == 3)
1743                 val |= 0x1 << 6;
1744         data->write(data, IT87_REG_FAN_DIV, val);
1745
1746         /* Restore fan min limit */
1747         data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1748         data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][1]);
1749         it87_unlock(data);
1750         return count;
1751 }
1752
1753 /* Returns 0 if OK, -EINVAL otherwise */
1754 static int check_trip_points(struct device *dev, int nr)
1755 {
1756         const struct it87_data *data = dev_get_drvdata(dev);
1757         int i, err = 0;
1758
1759         if (has_old_autopwm(data)) {
1760                 for (i = 0; i < 3; i++) {
1761                         if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1762                                 err = -EINVAL;
1763                 }
1764                 for (i = 0; i < 2; i++) {
1765                         if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1766                                 err = -EINVAL;
1767                 }
1768         } else if (has_newer_autopwm(data)) {
1769                 for (i = 1; i < 3; i++) {
1770                         if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1771                                 err = -EINVAL;
1772                 }
1773         }
1774
1775         if (err) {
1776                 dev_err(dev,
1777                         "Inconsistent trip points, not switching to automatic mode\n");
1778                 dev_err(dev, "Adjust the trip points and try again\n");
1779         }
1780         return err;
1781 }
1782
1783 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1784                               const char *buf, size_t count)
1785 {
1786         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1787         struct it87_data *data = dev_get_drvdata(dev);
1788         int nr = sensor_attr->index;
1789         long val;
1790         int err;
1791
1792         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1793                 return -EINVAL;
1794
1795         /* Check trip points before switching to automatic mode */
1796         if (val == 2) {
1797                 if (check_trip_points(dev, nr) < 0)
1798                         return -EINVAL;
1799         }
1800
1801         err = it87_lock(data);
1802         if (err)
1803                 return err;
1804
1805         it87_update_pwm_ctrl(data, nr);
1806
1807         if (val == 0) {
1808                 if (nr < 3 && has_fanctl_onoff(data)) {
1809                         int tmp;
1810                         /* make sure the fan is on when in on/off mode */
1811                         tmp = data->read(data, IT87_REG_FAN_CTL);
1812                         data->write(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1813                         /* set on/off mode */
1814                         data->fan_main_ctrl &= ~BIT(nr);
1815                         data->write(data, IT87_REG_FAN_MAIN_CTRL,
1816                                     data->fan_main_ctrl);
1817                 } else {
1818                         u8 ctrl;
1819
1820                         /* No on/off mode, set maximum pwm value */
1821                         data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1822                         data->write(data, IT87_REG_PWM_DUTY[nr],
1823                                     data->pwm_duty[nr]);
1824                         /* and set manual mode */
1825                         if (has_newer_autopwm(data)) {
1826                                 ctrl = temp_map_to_reg(data, nr,
1827                                                        data->pwm_temp_map[nr]);
1828                                 ctrl &= 0x7f;
1829                         } else {
1830                                 ctrl = data->pwm_duty[nr];
1831                         }
1832                         data->pwm_ctrl[nr] = ctrl;
1833                         data->write(data, data->REG_PWM[nr], ctrl);
1834                 }
1835         } else {
1836                 u8 ctrl;
1837
1838                 if (has_newer_autopwm(data)) {
1839                         ctrl = temp_map_to_reg(data, nr,
1840                                                data->pwm_temp_map[nr]);
1841                         if (val == 1)
1842                                 ctrl &= 0x7f;
1843                         else
1844                                 ctrl |= 0x80;
1845                 } else {
1846                         ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1847                 }
1848                 data->pwm_ctrl[nr] = ctrl;
1849                 data->write(data, data->REG_PWM[nr], ctrl);
1850
1851                 if (has_fanctl_onoff(data) && nr < 3) {
1852                         /* set SmartGuardian mode */
1853                         data->fan_main_ctrl |= BIT(nr);
1854                         data->write(data, IT87_REG_FAN_MAIN_CTRL,
1855                                     data->fan_main_ctrl);
1856                 }
1857         }
1858         it87_unlock(data);
1859         return count;
1860 }
1861
1862 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1863                        const char *buf, size_t count)
1864 {
1865         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1866         struct it87_data *data = dev_get_drvdata(dev);
1867         int nr = sensor_attr->index;
1868         long val;
1869         int err;
1870
1871         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1872                 return -EINVAL;
1873
1874         err = it87_lock(data);
1875         if (err)
1876                 return err;
1877
1878         it87_update_pwm_ctrl(data, nr);
1879         if (has_newer_autopwm(data)) {
1880                 /*
1881                  * If we are in automatic mode, the PWM duty cycle register
1882                  * is read-only so we can't write the value.
1883                  */
1884                 if (data->pwm_ctrl[nr] & 0x80) {
1885                         count = -EBUSY;
1886                         goto unlock;
1887                 }
1888                 data->pwm_duty[nr] = pwm_to_reg(data, val);
1889                 data->write(data, IT87_REG_PWM_DUTY[nr],
1890                             data->pwm_duty[nr]);
1891         } else {
1892                 data->pwm_duty[nr] = pwm_to_reg(data, val);
1893                 /*
1894                  * If we are in manual mode, write the duty cycle immediately;
1895                  * otherwise, just store it for later use.
1896                  */
1897                 if (!(data->pwm_ctrl[nr] & 0x80)) {
1898                         data->pwm_ctrl[nr] = data->pwm_duty[nr];
1899                         data->write(data, data->REG_PWM[nr],
1900                                     data->pwm_ctrl[nr]);
1901                 }
1902         }
1903 unlock:
1904         it87_unlock(data);
1905         return count;
1906 }
1907
1908 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1909                             const char *buf, size_t count)
1910 {
1911         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1912         struct it87_data *data = dev_get_drvdata(dev);
1913         int nr = sensor_attr->index;
1914         unsigned long val;
1915         int err;
1916         int i;
1917
1918         if (kstrtoul(buf, 10, &val) < 0)
1919                 return -EINVAL;
1920
1921         val = clamp_val(val, 0, 1000000);
1922         val *= has_newer_autopwm(data) ? 256 : 128;
1923
1924         /* Search for the nearest available frequency */
1925         for (i = 0; i < 7; i++) {
1926                 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1927                         break;
1928         }
1929
1930         err = it87_lock(data);
1931         if (err)
1932                 return err;
1933
1934         if (nr == 0) {
1935                 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL) & 0x8f;
1936                 data->fan_ctl |= i << 4;
1937                 data->write(data, IT87_REG_FAN_CTL, data->fan_ctl);
1938         } else {
1939                 data->extra = data->read(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1940                 data->extra |= i << 4;
1941                 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1942         }
1943         it87_unlock(data);
1944         return count;
1945 }
1946
1947 static ssize_t show_pwm_temp_map(struct device *dev,
1948                                  struct device_attribute *attr, char *buf)
1949 {
1950         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1951         struct it87_data *data = it87_update_device(dev);
1952         int nr = sensor_attr->index;
1953
1954         if (IS_ERR(data))
1955                 return PTR_ERR(data);
1956
1957         return sprintf(buf, "%d\n", data->pwm_temp_map[nr] + 1);
1958 }
1959
1960 static ssize_t set_pwm_temp_map(struct device *dev,
1961                                 struct device_attribute *attr, const char *buf,
1962                                 size_t count)
1963 {
1964         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1965         struct it87_data *data = dev_get_drvdata(dev);
1966         int nr = sensor_attr->index;
1967         unsigned long val;
1968         int err;
1969         u8 map;
1970
1971         if (kstrtoul(buf, 10, &val) < 0)
1972                 return -EINVAL;
1973
1974         if (!val || val > data->pwm_num_temp_map)
1975                 return -EINVAL;
1976
1977         map = val - 1;
1978
1979         err = it87_lock(data);
1980         if (err)
1981                 return err;
1982
1983         it87_update_pwm_ctrl(data, nr);
1984         data->pwm_temp_map[nr] = map;
1985         /*
1986          * If we are in automatic mode, write the temp mapping immediately;
1987          * otherwise, just store it for later use.
1988          */
1989         if (data->pwm_ctrl[nr] & 0x80) {
1990                 data->pwm_ctrl[nr] = temp_map_to_reg(data, nr, map);
1991                 data->write(data, data->REG_PWM[nr], data->pwm_ctrl[nr]);
1992         }
1993         it87_unlock(data);
1994         return count;
1995 }
1996
1997 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1998                              char *buf)
1999 {
2000         struct it87_data *data = it87_update_device(dev);
2001         struct sensor_device_attribute_2 *sensor_attr =
2002                         to_sensor_dev_attr_2(attr);
2003         int nr = sensor_attr->nr;
2004         int point = sensor_attr->index;
2005
2006         if (IS_ERR(data))
2007                 return PTR_ERR(data);
2008
2009         return sprintf(buf, "%d\n",
2010                        pwm_from_reg(data, data->auto_pwm[nr][point]));
2011 }
2012
2013 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
2014                             const char *buf, size_t count)
2015 {
2016         struct it87_data *data = dev_get_drvdata(dev);
2017         struct sensor_device_attribute_2 *sensor_attr =
2018                         to_sensor_dev_attr_2(attr);
2019         int nr = sensor_attr->nr;
2020         int point = sensor_attr->index;
2021         int regaddr;
2022         long val;
2023         int err;
2024
2025         if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
2026                 return -EINVAL;
2027
2028         err = it87_lock(data);
2029         if (err)
2030                 return err;
2031
2032         data->auto_pwm[nr][point] = pwm_to_reg(data, val);
2033         if (has_newer_autopwm(data))
2034                 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
2035         else
2036                 regaddr = IT87_REG_AUTO_PWM(nr, point);
2037         data->write(data, regaddr, data->auto_pwm[nr][point]);
2038         it87_unlock(data);
2039         return count;
2040 }
2041
2042 static ssize_t show_auto_pwm_slope(struct device *dev,
2043                                    struct device_attribute *attr, char *buf)
2044 {
2045         struct it87_data *data = it87_update_device(dev);
2046         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2047         int nr = sensor_attr->index;
2048
2049         if (IS_ERR(data))
2050                 return PTR_ERR(data);
2051
2052         return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
2053 }
2054
2055 static ssize_t set_auto_pwm_slope(struct device *dev,
2056                                   struct device_attribute *attr,
2057                                   const char *buf, size_t count)
2058 {
2059         struct it87_data *data = dev_get_drvdata(dev);
2060         struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2061         int nr = sensor_attr->index;
2062         unsigned long val;
2063         int err;
2064
2065         if (kstrtoul(buf, 10, &val) < 0 || val > 127)
2066                 return -EINVAL;
2067
2068         err = it87_lock(data);
2069         if (err)
2070                 return err;
2071
2072         data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
2073         data->write(data, IT87_REG_AUTO_TEMP(nr, 4), data->auto_pwm[nr][1]);
2074         it87_unlock(data);
2075         return count;
2076 }
2077
2078 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2079                               char *buf)
2080 {
2081         struct it87_data *data = it87_update_device(dev);
2082         struct sensor_device_attribute_2 *sensor_attr =
2083                         to_sensor_dev_attr_2(attr);
2084         int nr = sensor_attr->nr;
2085         int point = sensor_attr->index;
2086         int reg;
2087
2088         if (IS_ERR(data))
2089                 return PTR_ERR(data);
2090
2091         if (has_old_autopwm(data) || point)
2092                 reg = data->auto_temp[nr][point];
2093         else
2094                 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
2095
2096         return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
2097 }
2098
2099 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
2100                              const char *buf, size_t count)
2101 {
2102         struct it87_data *data = dev_get_drvdata(dev);
2103         struct sensor_device_attribute_2 *sensor_attr =
2104                         to_sensor_dev_attr_2(attr);
2105         int nr = sensor_attr->nr;
2106         int point = sensor_attr->index;
2107         long val;
2108         int reg;
2109         int err;
2110
2111         if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
2112                 return -EINVAL;
2113
2114         err = it87_lock(data);
2115         if (err)
2116                 return err;
2117
2118         if (has_newer_autopwm(data) && !point) {
2119                 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
2120                 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
2121                 data->auto_temp[nr][0] = reg;
2122                 data->write(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
2123         } else {
2124                 reg = TEMP_TO_REG(val);
2125                 data->auto_temp[nr][point] = reg;
2126                 if (has_newer_autopwm(data))
2127                         point--;
2128                 data->write(data, IT87_REG_AUTO_TEMP(nr, point), reg);
2129         }
2130         it87_unlock(data);
2131         return count;
2132 }
2133
2134 static SENSOR_DEVICE_ATTR_2(fan1_input, 0444, show_fan, NULL, 0, 0);
2135 static SENSOR_DEVICE_ATTR_2(fan1_min, 0644, show_fan, set_fan, 0, 1);
2136 static SENSOR_DEVICE_ATTR(fan1_div, 0644, show_fan_div, set_fan_div, 0);
2137
2138 static SENSOR_DEVICE_ATTR_2(fan2_input, 0444, show_fan, NULL, 1, 0);
2139 static SENSOR_DEVICE_ATTR_2(fan2_min, 0644, show_fan, set_fan, 1, 1);
2140 static SENSOR_DEVICE_ATTR(fan2_div, 0644, show_fan_div, set_fan_div, 1);
2141
2142 static SENSOR_DEVICE_ATTR_2(fan3_input, 0444, show_fan, NULL, 2, 0);
2143 static SENSOR_DEVICE_ATTR_2(fan3_min, 0644, show_fan, set_fan, 2, 1);
2144 static SENSOR_DEVICE_ATTR(fan3_div, 0644, show_fan_div, set_fan_div, 2);
2145
2146 static SENSOR_DEVICE_ATTR_2(fan4_input, 0444, show_fan, NULL, 3, 0);
2147 static SENSOR_DEVICE_ATTR_2(fan4_min, 0644, show_fan, set_fan, 3, 1);
2148
2149 static SENSOR_DEVICE_ATTR_2(fan5_input, 0444, show_fan, NULL, 4, 0);
2150 static SENSOR_DEVICE_ATTR_2(fan5_min, 0644, show_fan, set_fan, 4, 1);
2151
2152 static SENSOR_DEVICE_ATTR_2(fan6_input, 0444, show_fan, NULL, 5, 0);
2153 static SENSOR_DEVICE_ATTR_2(fan6_min, 0644, show_fan, set_fan, 5, 1);
2154
2155 static SENSOR_DEVICE_ATTR(pwm1_enable, 0644,
2156                           show_pwm_enable, set_pwm_enable, 0);
2157 static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
2158 static SENSOR_DEVICE_ATTR(pwm1_freq, 0644, show_pwm_freq, set_pwm_freq, 0);
2159 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, 0444,
2160                           show_pwm_temp_map, set_pwm_temp_map, 0);
2161 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, 0644,
2162                             show_auto_pwm, set_auto_pwm, 0, 0);
2163 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, 0644,
2164                             show_auto_pwm, set_auto_pwm, 0, 1);
2165 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, 0644,
2166                             show_auto_pwm, set_auto_pwm, 0, 2);
2167 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, 0444,
2168                             show_auto_pwm, NULL, 0, 3);
2169 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, 0644,
2170                             show_auto_temp, set_auto_temp, 0, 1);
2171 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, 0644,
2172                             show_auto_temp, set_auto_temp, 0, 0);
2173 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, 0644,
2174                             show_auto_temp, set_auto_temp, 0, 2);
2175 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, 0644,
2176                             show_auto_temp, set_auto_temp, 0, 3);
2177 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, 0644,
2178                             show_auto_temp, set_auto_temp, 0, 4);
2179 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, 0644,
2180                             show_auto_pwm, set_auto_pwm, 0, 0);
2181 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, 0644,
2182                           show_auto_pwm_slope, set_auto_pwm_slope, 0);
2183
2184 static SENSOR_DEVICE_ATTR(pwm2_enable, 0644,
2185                           show_pwm_enable, set_pwm_enable, 1);
2186 static SENSOR_DEVICE_ATTR(pwm2, 0644, show_pwm, set_pwm, 1);
2187 static SENSOR_DEVICE_ATTR(pwm2_freq, 0444, show_pwm_freq, set_pwm_freq, 1);
2188 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, 0444,
2189                           show_pwm_temp_map, set_pwm_temp_map, 1);
2190 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, 0644,
2191                             show_auto_pwm, set_auto_pwm, 1, 0);
2192 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, 0644,
2193                             show_auto_pwm, set_auto_pwm, 1, 1);
2194 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, 0644,
2195                             show_auto_pwm, set_auto_pwm, 1, 2);
2196 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, 0444,
2197                             show_auto_pwm, NULL, 1, 3);
2198 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, 0644,
2199                             show_auto_temp, set_auto_temp, 1, 1);
2200 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, 0644,
2201                             show_auto_temp, set_auto_temp, 1, 0);
2202 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, 0644,
2203                             show_auto_temp, set_auto_temp, 1, 2);
2204 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, 0644,
2205                             show_auto_temp, set_auto_temp, 1, 3);
2206 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, 0644,
2207                             show_auto_temp, set_auto_temp, 1, 4);
2208 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, 0644,
2209                             show_auto_pwm, set_auto_pwm, 1, 0);
2210 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, 0644,
2211                           show_auto_pwm_slope, set_auto_pwm_slope, 1);
2212
2213 static SENSOR_DEVICE_ATTR(pwm3_enable, 0644,
2214                           show_pwm_enable, set_pwm_enable, 2);
2215 static SENSOR_DEVICE_ATTR(pwm3, 0644, show_pwm, set_pwm, 2);
2216 static SENSOR_DEVICE_ATTR(pwm3_freq, 0444, show_pwm_freq, NULL, 2);
2217 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, 0444,
2218                           show_pwm_temp_map, set_pwm_temp_map, 2);
2219 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, 0644,
2220                             show_auto_pwm, set_auto_pwm, 2, 0);
2221 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, 0644,
2222                             show_auto_pwm, set_auto_pwm, 2, 1);
2223 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, 0644,
2224                             show_auto_pwm, set_auto_pwm, 2, 2);
2225 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, 0444,
2226                             show_auto_pwm, NULL, 2, 3);
2227 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, 0644,
2228                             show_auto_temp, set_auto_temp, 2, 1);
2229 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, 0644,
2230                             show_auto_temp, set_auto_temp, 2, 0);
2231 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, 0644,
2232                             show_auto_temp, set_auto_temp, 2, 2);
2233 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, 0644,
2234                             show_auto_temp, set_auto_temp, 2, 3);
2235 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, 0644,
2236                             show_auto_temp, set_auto_temp, 2, 4);
2237 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, 0644,
2238                             show_auto_pwm, set_auto_pwm, 2, 0);
2239 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, 0644,
2240                           show_auto_pwm_slope, set_auto_pwm_slope, 2);
2241
2242 static SENSOR_DEVICE_ATTR(pwm4_enable, 0644,
2243                           show_pwm_enable, set_pwm_enable, 3);
2244 static SENSOR_DEVICE_ATTR(pwm4, 0644, show_pwm, set_pwm, 3);
2245 static SENSOR_DEVICE_ATTR(pwm4_freq, 0444, show_pwm_freq, NULL, 3);
2246 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, 0444,
2247                           show_pwm_temp_map, set_pwm_temp_map, 3);
2248 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, 0644,
2249                             show_auto_temp, set_auto_temp, 2, 1);
2250 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, 0644,
2251                             show_auto_temp, set_auto_temp, 2, 0);
2252 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, 0644,
2253                             show_auto_temp, set_auto_temp, 2, 2);
2254 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, 0644,
2255                             show_auto_temp, set_auto_temp, 2, 3);
2256 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, 0644,
2257                             show_auto_pwm, set_auto_pwm, 3, 0);
2258 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, 0644,
2259                           show_auto_pwm_slope, set_auto_pwm_slope, 3);
2260
2261 static SENSOR_DEVICE_ATTR(pwm5_enable, 0644,
2262                           show_pwm_enable, set_pwm_enable, 4);
2263 static SENSOR_DEVICE_ATTR(pwm5, 0644, show_pwm, set_pwm, 4);
2264 static SENSOR_DEVICE_ATTR(pwm5_freq, 0444, show_pwm_freq, NULL, 4);
2265 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, 0444,
2266                           show_pwm_temp_map, set_pwm_temp_map, 4);
2267 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, 0644,
2268                             show_auto_temp, set_auto_temp, 2, 1);
2269 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, 0644,
2270                             show_auto_temp, set_auto_temp, 2, 0);
2271 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, 0644,
2272                             show_auto_temp, set_auto_temp, 2, 2);
2273 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, 0644,
2274                             show_auto_temp, set_auto_temp, 2, 3);
2275 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, 0644,
2276                             show_auto_pwm, set_auto_pwm, 4, 0);
2277 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, 0644,
2278                           show_auto_pwm_slope, set_auto_pwm_slope, 4);
2279
2280 static SENSOR_DEVICE_ATTR(pwm6_enable, 0644,
2281                           show_pwm_enable, set_pwm_enable, 5);
2282 static SENSOR_DEVICE_ATTR(pwm6, 0644, show_pwm, set_pwm, 5);
2283 static SENSOR_DEVICE_ATTR(pwm6_freq, 0444, show_pwm_freq, NULL, 5);
2284 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, 0444,
2285                           show_pwm_temp_map, set_pwm_temp_map, 5);
2286 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, 0644,
2287                             show_auto_temp, set_auto_temp, 2, 1);
2288 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, 0644,
2289                             show_auto_temp, set_auto_temp, 2, 0);
2290 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, 0644,
2291                             show_auto_temp, set_auto_temp, 2, 2);
2292 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, 0644,
2293                             show_auto_temp, set_auto_temp, 2, 3);
2294 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, 0644,
2295                             show_auto_pwm, set_auto_pwm, 5, 0);
2296 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, 0644,
2297                           show_auto_pwm_slope, set_auto_pwm_slope, 5);
2298
2299 /* Alarms */
2300 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
2301                            char *buf)
2302 {
2303         struct it87_data *data = it87_update_device(dev);
2304
2305         if (IS_ERR(data))
2306                 return PTR_ERR(data);
2307
2308         return sprintf(buf, "%u\n", data->alarms);
2309 }
2310 static DEVICE_ATTR(alarms, 0444, show_alarms, NULL);
2311
2312 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
2313                           char *buf)
2314 {
2315         struct it87_data *data = it87_update_device(dev);
2316         int bitnr = to_sensor_dev_attr(attr)->index;
2317
2318         if (IS_ERR(data))
2319                 return PTR_ERR(data);
2320
2321         return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
2322 }
2323
2324 static ssize_t clear_intrusion(struct device *dev,
2325                                struct device_attribute *attr, const char *buf,
2326                                size_t count)
2327 {
2328         struct it87_data *data = dev_get_drvdata(dev);
2329         int err, config;
2330         long val;
2331
2332         if (kstrtol(buf, 10, &val) < 0 || val != 0)
2333                 return -EINVAL;
2334
2335         err = it87_lock(data);
2336         if (err)
2337                 return err;
2338
2339         config = data->read(data, IT87_REG_CONFIG);
2340         config |= BIT(5);
2341         data->write(data, IT87_REG_CONFIG, config);
2342         /* Invalidate cache to force re-read */
2343         data->valid = 0;
2344         it87_unlock(data);
2345         return count;
2346 }
2347
2348 static SENSOR_DEVICE_ATTR(in0_alarm, 0444, show_alarm, NULL, 8);
2349 static SENSOR_DEVICE_ATTR(in1_alarm, 0444, show_alarm, NULL, 9);
2350 static SENSOR_DEVICE_ATTR(in2_alarm, 0444, show_alarm, NULL, 10);
2351 static SENSOR_DEVICE_ATTR(in3_alarm, 0444, show_alarm, NULL, 11);
2352 static SENSOR_DEVICE_ATTR(in4_alarm, 0444, show_alarm, NULL, 12);
2353 static SENSOR_DEVICE_ATTR(in5_alarm, 0444, show_alarm, NULL, 13);
2354 static SENSOR_DEVICE_ATTR(in6_alarm, 0444, show_alarm, NULL, 14);
2355 static SENSOR_DEVICE_ATTR(in7_alarm, 0444, show_alarm, NULL, 15);
2356 static SENSOR_DEVICE_ATTR(fan1_alarm, 0444, show_alarm, NULL, 0);
2357 static SENSOR_DEVICE_ATTR(fan2_alarm, 0444, show_alarm, NULL, 1);
2358 static SENSOR_DEVICE_ATTR(fan3_alarm, 0444, show_alarm, NULL, 2);
2359 static SENSOR_DEVICE_ATTR(fan4_alarm, 0444, show_alarm, NULL, 3);
2360 static SENSOR_DEVICE_ATTR(fan5_alarm, 0444, show_alarm, NULL, 6);
2361 static SENSOR_DEVICE_ATTR(fan6_alarm, 0444, show_alarm, NULL, 7);
2362 static SENSOR_DEVICE_ATTR(temp1_alarm, 0444, show_alarm, NULL, 16);
2363 static SENSOR_DEVICE_ATTR(temp2_alarm, 0444, show_alarm, NULL, 17);
2364 static SENSOR_DEVICE_ATTR(temp3_alarm, 0444, show_alarm, NULL, 18);
2365 static SENSOR_DEVICE_ATTR(temp4_alarm, 0444, show_alarm, NULL, 19);
2366 static SENSOR_DEVICE_ATTR(temp5_alarm, 0444, show_alarm, NULL, 20);
2367 static SENSOR_DEVICE_ATTR(temp6_alarm, 0444, show_alarm, NULL, 21);
2368 static SENSOR_DEVICE_ATTR(intrusion0_alarm, 0644,
2369                           show_alarm, clear_intrusion, 4);
2370
2371 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
2372                          char *buf)
2373 {
2374         struct it87_data *data = it87_update_device(dev);
2375         int bitnr = to_sensor_dev_attr(attr)->index;
2376
2377         if (IS_ERR(data))
2378                 return PTR_ERR(data);
2379
2380         return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
2381 }
2382
2383 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
2384                         const char *buf, size_t count)
2385 {
2386         int bitnr = to_sensor_dev_attr(attr)->index;
2387         struct it87_data *data = dev_get_drvdata(dev);
2388         long val;
2389         int err;
2390
2391         if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
2392                 return -EINVAL;
2393
2394         err = it87_lock(data);
2395         if (err)
2396                 return err;
2397
2398         data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
2399         if (val)
2400                 data->beeps |= BIT(bitnr);
2401         else
2402                 data->beeps &= ~BIT(bitnr);
2403         data->write(data, IT87_REG_BEEP_ENABLE, data->beeps);
2404         it87_unlock(data);
2405         return count;
2406 }
2407
2408 static SENSOR_DEVICE_ATTR(in0_beep, 0644,
2409                           show_beep, set_beep, 1);
2410 static SENSOR_DEVICE_ATTR(in1_beep, 0444, show_beep, NULL, 1);
2411 static SENSOR_DEVICE_ATTR(in2_beep, 0444, show_beep, NULL, 1);
2412 static SENSOR_DEVICE_ATTR(in3_beep, 0444, show_beep, NULL, 1);
2413 static SENSOR_DEVICE_ATTR(in4_beep, 0444, show_beep, NULL, 1);
2414 static SENSOR_DEVICE_ATTR(in5_beep, 0444, show_beep, NULL, 1);
2415 static SENSOR_DEVICE_ATTR(in6_beep, 0444, show_beep, NULL, 1);
2416 static SENSOR_DEVICE_ATTR(in7_beep, 0444, show_beep, NULL, 1);
2417 /* fanX_beep writability is set later */
2418 static SENSOR_DEVICE_ATTR(fan1_beep, 0444, show_beep, set_beep, 0);
2419 static SENSOR_DEVICE_ATTR(fan2_beep, 0444, show_beep, set_beep, 0);
2420 static SENSOR_DEVICE_ATTR(fan3_beep, 0444, show_beep, set_beep, 0);
2421 static SENSOR_DEVICE_ATTR(fan4_beep, 0444, show_beep, set_beep, 0);
2422 static SENSOR_DEVICE_ATTR(fan5_beep, 0444, show_beep, set_beep, 0);
2423 static SENSOR_DEVICE_ATTR(fan6_beep, 0444, show_beep, set_beep, 0);
2424 static SENSOR_DEVICE_ATTR(temp1_beep, 0644,
2425                           show_beep, set_beep, 2);
2426 static SENSOR_DEVICE_ATTR(temp2_beep, 0444, show_beep, NULL, 2);
2427 static SENSOR_DEVICE_ATTR(temp3_beep, 0444, show_beep, NULL, 2);
2428 static SENSOR_DEVICE_ATTR(temp4_beep, 0444, show_beep, NULL, 2);
2429 static SENSOR_DEVICE_ATTR(temp5_beep, 0444, show_beep, NULL, 2);
2430 static SENSOR_DEVICE_ATTR(temp6_beep, 0444, show_beep, NULL, 2);
2431
2432 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
2433                             char *buf)
2434 {
2435         struct it87_data *data = dev_get_drvdata(dev);
2436
2437         return sprintf(buf, "%u\n", data->vrm);
2438 }
2439
2440 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
2441                              const char *buf, size_t count)
2442 {
2443         struct it87_data *data = dev_get_drvdata(dev);
2444         unsigned long val;
2445
2446         if (kstrtoul(buf, 10, &val) < 0)
2447                 return -EINVAL;
2448
2449         data->vrm = val;
2450
2451         return count;
2452 }
2453 static DEVICE_ATTR(vrm, 0644, show_vrm_reg, store_vrm_reg);
2454
2455 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
2456                             char *buf)
2457 {
2458         struct it87_data *data = it87_update_device(dev);
2459
2460         if (IS_ERR(data))
2461                 return PTR_ERR(data);
2462
2463         return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
2464 }
2465 static DEVICE_ATTR(cpu0_vid, 0444, show_vid_reg, NULL);
2466
2467 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
2468                           char *buf)
2469 {
2470         static const char * const labels[] = {
2471                 "+5V",
2472                 "5VSB",
2473                 "Vbat",
2474                 "AVCC",
2475         };
2476         static const char * const labels_it8721[] = {
2477                 "+3.3V",
2478                 "3VSB",
2479                 "Vbat",
2480                 "+3.3V",
2481         };
2482         struct it87_data *data = dev_get_drvdata(dev);
2483         int nr = to_sensor_dev_attr(attr)->index;
2484         const char *label;
2485
2486         if (has_vin3_5v(data) && nr == 0)
2487                 label = labels[0];
2488         else if (has_12mv_adc(data) || has_10_9mv_adc(data) ||
2489                  has_11mv_adc(data))
2490                 label = labels_it8721[nr];
2491         else
2492                 label = labels[nr];
2493
2494         return sprintf(buf, "%s\n", label);
2495 }
2496 static SENSOR_DEVICE_ATTR(in3_label, 0444, show_label, NULL, 0);
2497 static SENSOR_DEVICE_ATTR(in7_label, 0444, show_label, NULL, 1);
2498 static SENSOR_DEVICE_ATTR(in8_label, 0444, show_label, NULL, 2);
2499 /* AVCC3 */
2500 static SENSOR_DEVICE_ATTR(in9_label, 0444, show_label, NULL, 3);
2501
2502 static umode_t it87_in_is_visible(struct kobject *kobj,
2503                                   struct attribute *attr, int index)
2504 {
2505         struct device *dev = container_of(kobj, struct device, kobj);
2506         struct it87_data *data = dev_get_drvdata(dev);
2507         int i = index / 5;      /* voltage index */
2508         int a = index % 5;      /* attribute index */
2509
2510         if (index >= 40) {      /* in8 and higher only have input attributes */
2511                 i = index - 40 + 8;
2512                 a = 0;
2513         }
2514
2515         if (!(data->has_in & BIT(i)))
2516                 return 0;
2517
2518         if (a == 4 && !data->has_beep)
2519                 return 0;
2520
2521         return attr->mode;
2522 }
2523
2524 static struct attribute *it87_attributes_in[] = {
2525         &sensor_dev_attr_in0_input.dev_attr.attr,
2526         &sensor_dev_attr_in0_min.dev_attr.attr,
2527         &sensor_dev_attr_in0_max.dev_attr.attr,
2528         &sensor_dev_attr_in0_alarm.dev_attr.attr,
2529         &sensor_dev_attr_in0_beep.dev_attr.attr,        /* 4 */
2530
2531         &sensor_dev_attr_in1_input.dev_attr.attr,
2532         &sensor_dev_attr_in1_min.dev_attr.attr,
2533         &sensor_dev_attr_in1_max.dev_attr.attr,
2534         &sensor_dev_attr_in1_alarm.dev_attr.attr,
2535         &sensor_dev_attr_in1_beep.dev_attr.attr,        /* 9 */
2536
2537         &sensor_dev_attr_in2_input.dev_attr.attr,
2538         &sensor_dev_attr_in2_min.dev_attr.attr,
2539         &sensor_dev_attr_in2_max.dev_attr.attr,
2540         &sensor_dev_attr_in2_alarm.dev_attr.attr,
2541         &sensor_dev_attr_in2_beep.dev_attr.attr,        /* 14 */
2542
2543         &sensor_dev_attr_in3_input.dev_attr.attr,
2544         &sensor_dev_attr_in3_min.dev_attr.attr,
2545         &sensor_dev_attr_in3_max.dev_attr.attr,
2546         &sensor_dev_attr_in3_alarm.dev_attr.attr,
2547         &sensor_dev_attr_in3_beep.dev_attr.attr,        /* 19 */
2548
2549         &sensor_dev_attr_in4_input.dev_attr.attr,
2550         &sensor_dev_attr_in4_min.dev_attr.attr,
2551         &sensor_dev_attr_in4_max.dev_attr.attr,
2552         &sensor_dev_attr_in4_alarm.dev_attr.attr,
2553         &sensor_dev_attr_in4_beep.dev_attr.attr,        /* 24 */
2554
2555         &sensor_dev_attr_in5_input.dev_attr.attr,
2556         &sensor_dev_attr_in5_min.dev_attr.attr,
2557         &sensor_dev_attr_in5_max.dev_attr.attr,
2558         &sensor_dev_attr_in5_alarm.dev_attr.attr,
2559         &sensor_dev_attr_in5_beep.dev_attr.attr,        /* 29 */
2560
2561         &sensor_dev_attr_in6_input.dev_attr.attr,
2562         &sensor_dev_attr_in6_min.dev_attr.attr,
2563         &sensor_dev_attr_in6_max.dev_attr.attr,
2564         &sensor_dev_attr_in6_alarm.dev_attr.attr,
2565         &sensor_dev_attr_in6_beep.dev_attr.attr,        /* 34 */
2566
2567         &sensor_dev_attr_in7_input.dev_attr.attr,
2568         &sensor_dev_attr_in7_min.dev_attr.attr,
2569         &sensor_dev_attr_in7_max.dev_attr.attr,
2570         &sensor_dev_attr_in7_alarm.dev_attr.attr,
2571         &sensor_dev_attr_in7_beep.dev_attr.attr,        /* 39 */
2572
2573         &sensor_dev_attr_in8_input.dev_attr.attr,       /* 40 */
2574         &sensor_dev_attr_in9_input.dev_attr.attr,       /* 41 */
2575         &sensor_dev_attr_in10_input.dev_attr.attr,      /* 42 */
2576         &sensor_dev_attr_in11_input.dev_attr.attr,      /* 43 */
2577         &sensor_dev_attr_in12_input.dev_attr.attr,      /* 44 */
2578         NULL
2579 };
2580
2581 static const struct attribute_group it87_group_in = {
2582         .attrs = it87_attributes_in,
2583         .is_visible = it87_in_is_visible,
2584 };
2585
2586 static umode_t it87_temp_is_visible(struct kobject *kobj,
2587                                     struct attribute *attr, int index)
2588 {
2589         struct device *dev = container_of(kobj, struct device, kobj);
2590         struct it87_data *data = dev_get_drvdata(dev);
2591         int i = index / 7;      /* temperature index */
2592         int a = index % 7;      /* attribute index */
2593
2594         if (!(data->has_temp & BIT(i)))
2595                 return 0;
2596
2597         if (a && i >= data->num_temp_limit)
2598                 return 0;
2599
2600         if (a == 3) {
2601                 int type = get_temp_type(data, i);
2602
2603                 if (type == 0)
2604                         return 0;
2605                 if (has_bank_sel(data))
2606                         return 0444;
2607                 return attr->mode;
2608         }
2609
2610         if (a == 5 && i >= data->num_temp_offset)
2611                 return 0;
2612
2613         if (a == 6 && !data->has_beep)
2614                 return 0;
2615
2616         return attr->mode;
2617 }
2618
2619 static struct attribute *it87_attributes_temp[] = {
2620         &sensor_dev_attr_temp1_input.dev_attr.attr,
2621         &sensor_dev_attr_temp1_max.dev_attr.attr,
2622         &sensor_dev_attr_temp1_min.dev_attr.attr,
2623         &sensor_dev_attr_temp1_type.dev_attr.attr,      /* 3 */
2624         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2625         &sensor_dev_attr_temp1_offset.dev_attr.attr,    /* 5 */
2626         &sensor_dev_attr_temp1_beep.dev_attr.attr,      /* 6 */
2627
2628         &sensor_dev_attr_temp2_input.dev_attr.attr,     /* 7 */
2629         &sensor_dev_attr_temp2_max.dev_attr.attr,
2630         &sensor_dev_attr_temp2_min.dev_attr.attr,
2631         &sensor_dev_attr_temp2_type.dev_attr.attr,
2632         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2633         &sensor_dev_attr_temp2_offset.dev_attr.attr,
2634         &sensor_dev_attr_temp2_beep.dev_attr.attr,
2635
2636         &sensor_dev_attr_temp3_input.dev_attr.attr,     /* 14 */
2637         &sensor_dev_attr_temp3_max.dev_attr.attr,
2638         &sensor_dev_attr_temp3_min.dev_attr.attr,
2639         &sensor_dev_attr_temp3_type.dev_attr.attr,
2640         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2641         &sensor_dev_attr_temp3_offset.dev_attr.attr,
2642         &sensor_dev_attr_temp3_beep.dev_attr.attr,
2643
2644         &sensor_dev_attr_temp4_input.dev_attr.attr,     /* 21 */
2645         &sensor_dev_attr_temp4_max.dev_attr.attr,
2646         &sensor_dev_attr_temp4_min.dev_attr.attr,
2647         &sensor_dev_attr_temp4_type.dev_attr.attr,
2648         &sensor_dev_attr_temp4_alarm.dev_attr.attr,
2649         &sensor_dev_attr_temp4_offset.dev_attr.attr,
2650         &sensor_dev_attr_temp4_beep.dev_attr.attr,
2651
2652         &sensor_dev_attr_temp5_input.dev_attr.attr,
2653         &sensor_dev_attr_temp5_max.dev_attr.attr,
2654         &sensor_dev_attr_temp5_min.dev_attr.attr,
2655         &sensor_dev_attr_temp5_type.dev_attr.attr,
2656         &sensor_dev_attr_temp5_alarm.dev_attr.attr,
2657         &sensor_dev_attr_temp5_offset.dev_attr.attr,
2658         &sensor_dev_attr_temp5_beep.dev_attr.attr,
2659
2660         &sensor_dev_attr_temp6_input.dev_attr.attr,
2661         &sensor_dev_attr_temp6_max.dev_attr.attr,
2662         &sensor_dev_attr_temp6_min.dev_attr.attr,
2663         &sensor_dev_attr_temp6_type.dev_attr.attr,
2664         &sensor_dev_attr_temp6_alarm.dev_attr.attr,
2665         &sensor_dev_attr_temp6_offset.dev_attr.attr,
2666         &sensor_dev_attr_temp6_beep.dev_attr.attr,
2667         NULL
2668 };
2669
2670 static const struct attribute_group it87_group_temp = {
2671         .attrs = it87_attributes_temp,
2672         .is_visible = it87_temp_is_visible,
2673 };
2674
2675 static umode_t it87_is_visible(struct kobject *kobj,
2676                                struct attribute *attr, int index)
2677 {
2678         struct device *dev = container_of(kobj, struct device, kobj);
2679         struct it87_data *data = dev_get_drvdata(dev);
2680
2681         if ((index == 2 || index == 3) && !data->has_vid)
2682                 return 0;
2683
2684         if (index > 3 && !(data->in_internal & BIT(index - 4)))
2685                 return 0;
2686
2687         return attr->mode;
2688 }
2689
2690 static struct attribute *it87_attributes[] = {
2691         &dev_attr_alarms.attr,
2692         &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2693         &dev_attr_vrm.attr,                             /* 2 */
2694         &dev_attr_cpu0_vid.attr,                        /* 3 */
2695         &sensor_dev_attr_in3_label.dev_attr.attr,       /* 4 .. 7 */
2696         &sensor_dev_attr_in7_label.dev_attr.attr,
2697         &sensor_dev_attr_in8_label.dev_attr.attr,
2698         &sensor_dev_attr_in9_label.dev_attr.attr,
2699         NULL
2700 };
2701
2702 static const struct attribute_group it87_group = {
2703         .attrs = it87_attributes,
2704         .is_visible = it87_is_visible,
2705 };
2706
2707 static umode_t it87_fan_is_visible(struct kobject *kobj,
2708                                    struct attribute *attr, int index)
2709 {
2710         struct device *dev = container_of(kobj, struct device, kobj);
2711         struct it87_data *data = dev_get_drvdata(dev);
2712         int i = index / 5;      /* fan index */
2713         int a = index % 5;      /* attribute index */
2714
2715         if (index >= 15) {      /* fan 4..6 don't have divisor attributes */
2716                 i = (index - 15) / 4 + 3;
2717                 a = (index - 15) % 4;
2718         }
2719
2720         if (!(data->has_fan & BIT(i)))
2721                 return 0;
2722
2723         if (a == 3) {                           /* beep */
2724                 if (!data->has_beep)
2725                         return 0;
2726                 /* first fan beep attribute is writable */
2727                 if (i == __ffs(data->has_fan))
2728                         return attr->mode | 0200;
2729         }
2730
2731         if (a == 4 && has_16bit_fans(data))     /* divisor */
2732                 return 0;
2733
2734         return attr->mode;
2735 }
2736
2737 static struct attribute *it87_attributes_fan[] = {
2738         &sensor_dev_attr_fan1_input.dev_attr.attr,
2739         &sensor_dev_attr_fan1_min.dev_attr.attr,
2740         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2741         &sensor_dev_attr_fan1_beep.dev_attr.attr,       /* 3 */
2742         &sensor_dev_attr_fan1_div.dev_attr.attr,        /* 4 */
2743
2744         &sensor_dev_attr_fan2_input.dev_attr.attr,
2745         &sensor_dev_attr_fan2_min.dev_attr.attr,
2746         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2747         &sensor_dev_attr_fan2_beep.dev_attr.attr,
2748         &sensor_dev_attr_fan2_div.dev_attr.attr,        /* 9 */
2749
2750         &sensor_dev_attr_fan3_input.dev_attr.attr,
2751         &sensor_dev_attr_fan3_min.dev_attr.attr,
2752         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2753         &sensor_dev_attr_fan3_beep.dev_attr.attr,
2754         &sensor_dev_attr_fan3_div.dev_attr.attr,        /* 14 */
2755
2756         &sensor_dev_attr_fan4_input.dev_attr.attr,      /* 15 */
2757         &sensor_dev_attr_fan4_min.dev_attr.attr,
2758         &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2759         &sensor_dev_attr_fan4_beep.dev_attr.attr,
2760
2761         &sensor_dev_attr_fan5_input.dev_attr.attr,      /* 19 */
2762         &sensor_dev_attr_fan5_min.dev_attr.attr,
2763         &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2764         &sensor_dev_attr_fan5_beep.dev_attr.attr,
2765
2766         &sensor_dev_attr_fan6_input.dev_attr.attr,      /* 23 */
2767         &sensor_dev_attr_fan6_min.dev_attr.attr,
2768         &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2769         &sensor_dev_attr_fan6_beep.dev_attr.attr,
2770         NULL
2771 };
2772
2773 static const struct attribute_group it87_group_fan = {
2774         .attrs = it87_attributes_fan,
2775         .is_visible = it87_fan_is_visible,
2776 };
2777
2778 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2779                                    struct attribute *attr, int index)
2780 {
2781         struct device *dev = container_of(kobj, struct device, kobj);
2782         struct it87_data *data = dev_get_drvdata(dev);
2783         int i = index / 4;      /* pwm index */
2784         int a = index % 4;      /* attribute index */
2785
2786         if (!(data->has_pwm & BIT(i)))
2787                 return 0;
2788
2789         /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2790         if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2791                 return attr->mode | 0200;
2792
2793         /* pwm2_freq is writable if there are two pwm frequency selects */
2794         if (has_pwm_freq2(data) && i == 1 && a == 2)
2795                 return attr->mode | 0200;
2796
2797         return attr->mode;
2798 }
2799
2800 static struct attribute *it87_attributes_pwm[] = {
2801         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2802         &sensor_dev_attr_pwm1.dev_attr.attr,
2803         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2804         &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2805
2806         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2807         &sensor_dev_attr_pwm2.dev_attr.attr,
2808         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2809         &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2810
2811         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2812         &sensor_dev_attr_pwm3.dev_attr.attr,
2813         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2814         &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2815
2816         &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2817         &sensor_dev_attr_pwm4.dev_attr.attr,
2818         &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2819         &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2820
2821         &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2822         &sensor_dev_attr_pwm5.dev_attr.attr,
2823         &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2824         &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2825
2826         &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2827         &sensor_dev_attr_pwm6.dev_attr.attr,
2828         &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2829         &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2830
2831         NULL
2832 };
2833
2834 static const struct attribute_group it87_group_pwm = {
2835         .attrs = it87_attributes_pwm,
2836         .is_visible = it87_pwm_is_visible,
2837 };
2838
2839 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2840                                         struct attribute *attr, int index)
2841 {
2842         struct device *dev = container_of(kobj, struct device, kobj);
2843         struct it87_data *data = dev_get_drvdata(dev);
2844         int i = index / 11;     /* pwm index */
2845         int a = index % 11;     /* attribute index */
2846
2847         if (index >= 33) {      /* pwm 4..6 */
2848                 i = (index - 33) / 6 + 3;
2849                 a = (index - 33) % 6 + 4;
2850         }
2851
2852         if (!(data->has_pwm & BIT(i)))
2853                 return 0;
2854
2855         if (has_newer_autopwm(data)) {
2856                 if (a < 4)      /* no auto point pwm */
2857                         return 0;
2858                 if (a == 8)     /* no auto_point4 */
2859                         return 0;
2860         }
2861         if (has_old_autopwm(data)) {
2862                 if (a >= 9)     /* no pwm_auto_start, pwm_auto_slope */
2863                         return 0;
2864         }
2865
2866         return attr->mode;
2867 }
2868
2869 static struct attribute *it87_attributes_auto_pwm[] = {
2870         &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2871         &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2872         &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2873         &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2874         &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2875         &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2876         &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2877         &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2878         &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2879         &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2880         &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2881
2882         &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,    /* 11 */
2883         &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2884         &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2885         &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2886         &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2887         &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2888         &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2889         &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2890         &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2891         &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2892         &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2893
2894         &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,    /* 22 */
2895         &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2896         &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2897         &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2898         &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2899         &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2900         &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2901         &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2902         &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2903         &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2904         &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2905
2906         &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,   /* 33 */
2907         &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2908         &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2909         &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2910         &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2911         &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2912
2913         &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2914         &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2915         &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2916         &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2917         &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2918         &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2919
2920         &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2921         &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2922         &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2923         &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2924         &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2925         &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2926
2927         NULL,
2928 };
2929
2930 static const struct attribute_group it87_group_auto_pwm = {
2931         .attrs = it87_attributes_auto_pwm,
2932         .is_visible = it87_auto_pwm_is_visible,
2933 };
2934
2935 /* SuperIO detection - will change isa_address if a chip is found */
2936 static int __init it87_find(int sioaddr, unsigned short *address,
2937                             phys_addr_t *mmio_address,
2938                             struct it87_sio_data *sio_data)
2939 {
2940         const struct it87_devices *config;
2941         phys_addr_t base = 0;
2942         bool doexit = true;
2943         char mmio_str[32];
2944         u16 chip_type;
2945         int err;
2946
2947         err = superio_enter(sioaddr);
2948         if (err)
2949                 return err;
2950
2951         sio_data->sioaddr = sioaddr;
2952
2953         err = -ENODEV;
2954         chip_type = superio_inw(sioaddr, DEVID);
2955         if (chip_type == 0xffff)
2956                 goto exit;
2957
2958         if (force_id)
2959                 chip_type = force_id;
2960
2961         switch (chip_type) {
2962         case IT8705F_DEVID:
2963                 sio_data->type = it87;
2964                 break;
2965         case IT8712F_DEVID:
2966                 sio_data->type = it8712;
2967                 break;
2968         case IT8716F_DEVID:
2969         case IT8726F_DEVID:
2970                 sio_data->type = it8716;
2971                 break;
2972         case IT8718F_DEVID:
2973                 sio_data->type = it8718;
2974                 break;
2975         case IT8720F_DEVID:
2976                 sio_data->type = it8720;
2977                 break;
2978         case IT8721F_DEVID:
2979                 sio_data->type = it8721;
2980                 break;
2981         case IT8728F_DEVID:
2982                 sio_data->type = it8728;
2983                 break;
2984         case IT8732F_DEVID:
2985                 sio_data->type = it8732;
2986                 break;
2987         case IT8792E_DEVID:
2988                 sio_data->type = it8792;
2989                 /*
2990                  * Disabling configuration mode on IT8792E can result in system
2991                  * hang-ups and access failures to the Super-IO chip at the
2992                  * second SIO address. Never exit configuration mode on this
2993                  * chip to avoid the problem.
2994                  */
2995                 doexit = false;
2996                 break;
2997         case IT8771E_DEVID:
2998                 sio_data->type = it8771;
2999                 break;
3000         case IT8772E_DEVID:
3001                 sio_data->type = it8772;
3002                 break;
3003         case IT8781F_DEVID:
3004                 sio_data->type = it8781;
3005                 break;
3006         case IT8782F_DEVID:
3007                 sio_data->type = it8782;
3008                 break;
3009         case IT8783E_DEVID:
3010                 sio_data->type = it8783;
3011                 break;
3012         case IT8786E_DEVID:
3013                 sio_data->type = it8786;
3014                 break;
3015         case IT8790E_DEVID:
3016                 sio_data->type = it8790;
3017                 doexit = false;         /* See IT8792E comment above */
3018                 break;
3019         case IT8603E_DEVID:
3020         case IT8623E_DEVID:
3021                 sio_data->type = it8603;
3022                 break;
3023         case IT8607E_DEVID:
3024                 sio_data->type = it8607;
3025                 break;
3026         case IT8613E_DEVID:
3027                 sio_data->type = it8613;
3028                 break;
3029         case IT8620E_DEVID:
3030                 sio_data->type = it8620;
3031                 break;
3032         case IT8622E_DEVID:
3033                 sio_data->type = it8622;
3034                 break;
3035         case IT8625E_DEVID:
3036                 sio_data->type = it8625;
3037                 break;
3038         case IT8628E_DEVID:
3039                 sio_data->type = it8628;
3040                 break;
3041         case IT8655E_DEVID:
3042                 sio_data->type = it8655;
3043                 break;
3044         case IT8665E_DEVID:
3045                 sio_data->type = it8665;
3046                 break;
3047         case IT8686E_DEVID:
3048                 sio_data->type = it8686;
3049                 break;
3050         case 0xffff:    /* No device at all */
3051                 goto exit;
3052         default:
3053                 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
3054                 goto exit;
3055         }
3056
3057         superio_select(sioaddr, PME);
3058         if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
3059                 pr_info("Device not activated, skipping\n");
3060                 goto exit;
3061         }
3062
3063         *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
3064         if (*address == 0) {
3065                 pr_info("Base address not set, skipping\n");
3066                 goto exit;
3067         }
3068
3069         sio_data->doexit = doexit;
3070
3071         err = 0;
3072         sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
3073
3074         config = &it87_devices[sio_data->type];
3075
3076         if (has_mmio(config) && mmio) {
3077                 u8 reg;
3078
3079                 reg = superio_inb(sioaddr, IT87_EC_HWM_MIO_REG);
3080                 if (reg & BIT(5)) {
3081                         base = 0xf0000000 + ((reg & 0x0f) << 24);
3082                         base += (reg & 0xc0) << 14;
3083                 }
3084         }
3085         *mmio_address = base;
3086
3087         mmio_str[0] = '\0';
3088         if (base)
3089                 snprintf(mmio_str, sizeof(mmio_str), " [MMIO at %pa]", &base);
3090
3091         pr_info("Found IT%04x%s chip at 0x%x%s, revision %d\n", chip_type,
3092                 it87_devices[sio_data->type].suffix,
3093                 *address, mmio_str, sio_data->revision);
3094
3095         /* in7 (VSB or VCCH5V) is always internal on some chips */
3096         if (has_in7_internal(config))
3097                 sio_data->internal |= BIT(1);
3098
3099         /* in8 (Vbat) is always internal */
3100         sio_data->internal |= BIT(2);
3101
3102         /* in9 (AVCC3), always internal if supported */
3103         if (has_avcc3(config))
3104                 sio_data->internal |= BIT(3); /* in9 is AVCC */
3105         else
3106                 sio_data->skip_in |= BIT(9);
3107
3108         if (!has_four_pwm(config))
3109                 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
3110         else if (!has_five_pwm(config))
3111                 sio_data->skip_pwm |= BIT(4) | BIT(5);
3112         else if (!has_six_pwm(config))
3113                 sio_data->skip_pwm |= BIT(5);
3114
3115         if (!has_vid(config))
3116                 sio_data->skip_vid = 1;
3117
3118         /* Read GPIO config and VID value from LDN 7 (GPIO) */
3119         if (sio_data->type == it87) {
3120                 /* The IT8705F has a different LD number for GPIO */
3121                 superio_select(sioaddr, 5);
3122                 sio_data->beep_pin = superio_inb(sioaddr,
3123                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3124         } else if (sio_data->type == it8783) {
3125                 int reg25, reg27, reg2a, reg2c, regef;
3126
3127                 superio_select(sioaddr, GPIO);
3128
3129                 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3130                 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3131                 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3132                 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3133                 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
3134
3135                 /* Check if fan3 is there or not */
3136                 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
3137                         sio_data->skip_fan |= BIT(2);
3138                 if ((reg25 & BIT(4)) ||
3139                     (!(reg2a & BIT(1)) && (regef & BIT(0))))
3140                         sio_data->skip_pwm |= BIT(2);
3141
3142                 /* Check if fan2 is there or not */
3143                 if (reg27 & BIT(7))
3144                         sio_data->skip_fan |= BIT(1);
3145                 if (reg27 & BIT(3))
3146                         sio_data->skip_pwm |= BIT(1);
3147
3148                 /* VIN5 */
3149                 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
3150                         sio_data->skip_in |= BIT(5); /* No VIN5 */
3151
3152                 /* VIN6 */
3153                 if (reg27 & BIT(1))
3154                         sio_data->skip_in |= BIT(6); /* No VIN6 */
3155
3156                 /*
3157                  * VIN7
3158                  * Does not depend on bit 2 of Reg2C, contrary to datasheet.
3159                  */
3160                 if (reg27 & BIT(2)) {
3161                         /*
3162                          * The data sheet is a bit unclear regarding the
3163                          * internal voltage divider for VCCH5V. It says
3164                          * "This bit enables and switches VIN7 (pin 91) to the
3165                          * internal voltage divider for VCCH5V".
3166                          * This is different to other chips, where the internal
3167                          * voltage divider would connect VIN7 to an internal
3168                          * voltage source. Maybe that is the case here as well.
3169                          *
3170                          * Since we don't know for sure, re-route it if that is
3171                          * not the case, and ask the user to report if the
3172                          * resulting voltage is sane.
3173                          */
3174                         if (!(reg2c & BIT(1))) {
3175                                 reg2c |= BIT(1);
3176                                 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
3177                                              reg2c);
3178                                 pr_notice("Routing internal VCCH5V to in7.\n");
3179                         }
3180                         pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
3181                         pr_notice("Please report if it displays a reasonable voltage.\n");
3182                 }
3183
3184                 if (reg2c & BIT(0))
3185                         sio_data->internal |= BIT(0);
3186                 if (reg2c & BIT(1))
3187                         sio_data->internal |= BIT(1);
3188
3189                 sio_data->beep_pin = superio_inb(sioaddr,
3190                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3191         } else if (sio_data->type == it8603 || sio_data->type == it8607) {
3192                 int reg27, reg29;
3193
3194                 superio_select(sioaddr, GPIO);
3195
3196                 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3197
3198                 /* Check if fan3 is there or not */
3199                 if (reg27 & BIT(6))
3200                         sio_data->skip_pwm |= BIT(2);
3201                 if (reg27 & BIT(7))
3202                         sio_data->skip_fan |= BIT(2);
3203
3204                 /* Check if fan2 is there or not */
3205                 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3206                 if (reg29 & BIT(1))
3207                         sio_data->skip_pwm |= BIT(1);
3208                 if (reg29 & BIT(2))
3209                         sio_data->skip_fan |= BIT(1);
3210
3211                 switch (sio_data->type) {
3212                 case it8603:
3213                         sio_data->skip_in |= BIT(5); /* No VIN5 */
3214                         sio_data->skip_in |= BIT(6); /* No VIN6 */
3215                         break;
3216                 case it8607:
3217                         sio_data->skip_pwm |= BIT(0);/* No fan1 */
3218                         sio_data->skip_fan |= BIT(0);
3219                 default:
3220                         break;
3221                 }
3222
3223                 sio_data->beep_pin = superio_inb(sioaddr,
3224                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3225         } else if (sio_data->type == it8613) {
3226                 int reg27, reg29, reg2a;
3227
3228                 superio_select(sioaddr, GPIO);
3229
3230                 /* Check for pwm3, fan3, pwm5, fan5 */
3231                 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3232                 if (reg27 & BIT(1))
3233                         sio_data->skip_fan |= BIT(4);
3234                 if (reg27 & BIT(3))
3235                         sio_data->skip_pwm |= BIT(4);
3236                 if (reg27 & BIT(6))
3237                         sio_data->skip_pwm |= BIT(2);
3238                 if (reg27 & BIT(7))
3239                         sio_data->skip_fan |= BIT(2);
3240
3241                 /* Check for pwm2, fan2 */
3242                 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3243                 if (reg29 & BIT(1))
3244                         sio_data->skip_pwm |= BIT(1);
3245                 if (reg29 & BIT(2))
3246                         sio_data->skip_fan |= BIT(1);
3247
3248                 /* Check for pwm4, fan4 */
3249                 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3250                 if (!(reg2a & BIT(0)) || (reg29 & BIT(7))) {
3251                         sio_data->skip_fan |= BIT(3);
3252                         sio_data->skip_pwm |= BIT(3);
3253                 }
3254
3255                 sio_data->skip_pwm |= BIT(0); /* No pwm1 */
3256                 sio_data->skip_fan |= BIT(0); /* No fan1 */
3257                 sio_data->skip_in |= BIT(3);  /* No VIN3 */
3258                 sio_data->skip_in |= BIT(6);  /* No VIN6 */
3259
3260                 sio_data->beep_pin = superio_inb(sioaddr,
3261                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3262         } else if (sio_data->type == it8620 || sio_data->type == it8628 ||
3263                    sio_data->type == it8686) {
3264                 int reg;
3265
3266                 superio_select(sioaddr, GPIO);
3267
3268                 /* Check for pwm5 */
3269                 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3270                 if (reg & BIT(6))
3271                         sio_data->skip_pwm |= BIT(4);
3272
3273                 /* Check for fan4, fan5 */
3274                 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3275                 if (!(reg & BIT(5)))
3276                         sio_data->skip_fan |= BIT(3);
3277                 if (!(reg & BIT(4)))
3278                         sio_data->skip_fan |= BIT(4);
3279
3280                 /* Check for pwm3, fan3 */
3281                 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3282                 if (reg & BIT(6))
3283                         sio_data->skip_pwm |= BIT(2);
3284                 if (reg & BIT(7))
3285                         sio_data->skip_fan |= BIT(2);
3286
3287                 /* Check for pwm4 */
3288                 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
3289                 if (reg & BIT(2))
3290                         sio_data->skip_pwm |= BIT(3);
3291
3292                 /* Check for pwm2, fan2 */
3293                 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3294                 if (reg & BIT(1))
3295                         sio_data->skip_pwm |= BIT(1);
3296                 if (reg & BIT(2))
3297                         sio_data->skip_fan |= BIT(1);
3298                 /* Check for pwm6, fan6 */
3299                 if (!(reg & BIT(7))) {
3300                         sio_data->skip_pwm |= BIT(5);
3301                         sio_data->skip_fan |= BIT(5);
3302                 }
3303
3304                 /* Check if AVCC is on VIN3 */
3305                 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3306                 if (reg & BIT(0)) {
3307                         /* For it8686, the bit just enables AVCC3 */
3308                         if (sio_data->type != it8686)
3309                                 sio_data->internal |= BIT(0);
3310                 } else {
3311                         sio_data->internal &= ~BIT(3);
3312                         sio_data->skip_in |= BIT(9);
3313                 }
3314
3315                 sio_data->beep_pin = superio_inb(sioaddr,
3316                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3317         } else if (sio_data->type == it8622) {
3318                 int reg;
3319
3320                 superio_select(sioaddr, GPIO);
3321
3322                 /* Check for pwm4, fan4 */
3323                 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3324                 if (reg & BIT(6))
3325                         sio_data->skip_fan |= BIT(3);
3326                 if (reg & BIT(5))
3327                         sio_data->skip_pwm |= BIT(3);
3328
3329                 /* Check for pwm3, fan3, pwm5, fan5 */
3330                 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3331                 if (reg & BIT(6))
3332                         sio_data->skip_pwm |= BIT(2);
3333                 if (reg & BIT(7))
3334                         sio_data->skip_fan |= BIT(2);
3335                 if (reg & BIT(3))
3336                         sio_data->skip_pwm |= BIT(4);
3337                 if (reg & BIT(1))
3338                         sio_data->skip_fan |= BIT(4);
3339
3340                 /* Check for pwm2, fan2 */
3341                 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3342                 if (reg & BIT(1))
3343                         sio_data->skip_pwm |= BIT(1);
3344                 if (reg & BIT(2))
3345                         sio_data->skip_fan |= BIT(1);
3346
3347                 /* Check for AVCC */
3348                 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3349                 if (!(reg & BIT(0)))
3350                         sio_data->skip_in |= BIT(9);
3351
3352                 sio_data->beep_pin = superio_inb(sioaddr,
3353                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3354         } else if (sio_data->type == it8732) {
3355                 int reg;
3356
3357                 superio_select(sioaddr, GPIO);
3358
3359                 /* Check for pwm2, fan2 */
3360                 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3361                 if (reg & BIT(1))
3362                         sio_data->skip_pwm |= BIT(1);
3363                 if (reg & BIT(2))
3364                         sio_data->skip_fan |= BIT(1);
3365
3366                 /* Check for pwm3, fan3, fan4 */
3367                 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3368                 if (reg & BIT(6))
3369                         sio_data->skip_pwm |= BIT(2);
3370                 if (reg & BIT(7))
3371                         sio_data->skip_fan |= BIT(2);
3372                 if (reg & BIT(5))
3373                         sio_data->skip_fan |= BIT(3);
3374
3375                 /* Check if AVCC is on VIN3 */
3376                 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3377                 if (reg & BIT(0))
3378                         sio_data->internal |= BIT(0);
3379
3380                 sio_data->beep_pin = superio_inb(sioaddr,
3381                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3382         } else if (sio_data->type == it8655) {
3383                 int reg;
3384
3385                 superio_select(sioaddr, GPIO);
3386
3387                 /* Check for pwm2 */
3388                 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3389                 if (reg & BIT(1))
3390                         sio_data->skip_pwm |= BIT(1);
3391
3392                 /* Check for fan2 */
3393                 reg = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3394                 if (reg & BIT(4))
3395                         sio_data->skip_fan |= BIT(1);
3396
3397                 /* Check for pwm3, fan3 */
3398                 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3399                 if (reg & BIT(6))
3400                         sio_data->skip_pwm |= BIT(2);
3401                 if (reg & BIT(7))
3402                         sio_data->skip_fan |= BIT(2);
3403
3404                 sio_data->beep_pin = superio_inb(sioaddr,
3405                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3406         } else if (sio_data->type == it8665 || sio_data->type == it8625) {
3407                 int reg27, reg29, reg2d, regd3;
3408
3409                 superio_select(sioaddr, GPIO);
3410
3411                 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3412                 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3413                 reg2d = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3414                 regd3 = superio_inb(sioaddr, IT87_SIO_GPIO9_REG);
3415
3416                 /* Check for pwm2, fan2 */
3417                 if (reg29 & BIT(1))
3418                         sio_data->skip_pwm |= BIT(1);
3419                 /*
3420                  * Note: Table 6-1 in datasheet claims that FAN_TAC2
3421                  * would be enabled with 29h[2]=0.
3422                  */
3423                 if (reg2d & BIT(4))
3424                         sio_data->skip_fan |= BIT(1);
3425
3426                 /* Check for pwm3, fan3 */
3427                 if (reg27 & BIT(6))
3428                         sio_data->skip_pwm |= BIT(2);
3429                 if (reg27 & BIT(7))
3430                         sio_data->skip_fan |= BIT(2);
3431
3432                 /* Check for pwm4, fan4, pwm5, fan5 */
3433                 if (sio_data->type == it8625) {
3434                         int reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3435
3436                         if (reg25 & BIT(6))
3437                                 sio_data->skip_fan |= BIT(3);
3438                         if (reg25 & BIT(5))
3439                                 sio_data->skip_pwm |= BIT(3);
3440                         if (reg27 & BIT(3))
3441                                 sio_data->skip_pwm |= BIT(4);
3442                         if (reg27 & BIT(1))
3443                                 sio_data->skip_fan |= BIT(4);
3444                 } else {
3445                         int reg26 = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3446
3447                         if (regd3 & BIT(2))
3448                                 sio_data->skip_pwm |= BIT(3);
3449                         if (regd3 & BIT(3))
3450                                 sio_data->skip_fan |= BIT(3);
3451                         if (reg26 & BIT(5))
3452                                 sio_data->skip_pwm |= BIT(4);
3453                         if (reg26 & BIT(4))
3454                                 sio_data->skip_fan |= BIT(4);
3455                 }
3456
3457                 /* Check for pwm6, fan6 */
3458                 if (regd3 & BIT(0))
3459                         sio_data->skip_pwm |= BIT(5);
3460                 if (regd3 & BIT(1))
3461                         sio_data->skip_fan |= BIT(5);
3462
3463                 sio_data->beep_pin = superio_inb(sioaddr,
3464                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3465         } else {
3466                 int reg;
3467                 bool uart6;
3468
3469                 superio_select(sioaddr, GPIO);
3470
3471                 /* Check for fan4, fan5 */
3472                 if (has_five_fans(config)) {
3473                         reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3474                         switch (sio_data->type) {
3475                         case it8718:
3476                                 if (reg & BIT(5))
3477                                         sio_data->skip_fan |= BIT(3);
3478                                 if (reg & BIT(4))
3479                                         sio_data->skip_fan |= BIT(4);
3480                                 break;
3481                         case it8720:
3482                         case it8721:
3483                         case it8728:
3484                                 if (!(reg & BIT(5)))
3485                                         sio_data->skip_fan |= BIT(3);
3486                                 if (!(reg & BIT(4)))
3487                                         sio_data->skip_fan |= BIT(4);
3488                                 break;
3489                         default:
3490                                 break;
3491                         }
3492                 }
3493
3494                 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3495                 if (!sio_data->skip_vid) {
3496                         /* We need at least 4 VID pins */
3497                         if (reg & 0x0f) {
3498                                 pr_info("VID is disabled (pins used for GPIO)\n");
3499                                 sio_data->skip_vid = 1;
3500                         }
3501                 }
3502
3503                 /* Check if fan3 is there or not */
3504                 if (reg & BIT(6))
3505                         sio_data->skip_pwm |= BIT(2);
3506                 if (reg & BIT(7))
3507                         sio_data->skip_fan |= BIT(2);
3508
3509                 /* Check if fan2 is there or not */
3510                 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3511                 if (reg & BIT(1))
3512                         sio_data->skip_pwm |= BIT(1);
3513                 if (reg & BIT(2))
3514                         sio_data->skip_fan |= BIT(1);
3515
3516                 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
3517                     !(sio_data->skip_vid))
3518                         sio_data->vid_value = superio_inb(sioaddr,
3519                                                           IT87_SIO_VID_REG);
3520
3521                 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3522
3523                 uart6 = sio_data->type == it8782 && (reg & BIT(2));
3524
3525                 /*
3526                  * The IT8720F has no VIN7 pin, so VCCH should always be
3527                  * routed internally to VIN7 with an internal divider.
3528                  * Curiously, there still is a configuration bit to control
3529                  * this, which means it can be set incorrectly. And even
3530                  * more curiously, many boards out there are improperly
3531                  * configured, even though the IT8720F datasheet claims
3532                  * that the internal routing of VCCH to VIN7 is the default
3533                  * setting. So we force the internal routing in this case.
3534                  *
3535                  * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
3536                  * If UART6 is enabled, re-route VIN7 to the internal divider
3537                  * if that is not already the case.
3538                  */
3539                 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
3540                         reg |= BIT(1);
3541                         superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
3542                         pr_notice("Routing internal VCCH to in7\n");
3543                 }
3544                 if (reg & BIT(0))
3545                         sio_data->internal |= BIT(0);
3546                 if (reg & BIT(1))
3547                         sio_data->internal |= BIT(1);
3548
3549                 /*
3550                  * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
3551                  * While VIN7 can be routed to the internal voltage divider,
3552                  * VIN5 and VIN6 are not available if UART6 is enabled.
3553                  *
3554                  * Also, temp3 is not available if UART6 is enabled and TEMPIN3
3555                  * is the temperature source. Since we can not read the
3556                  * temperature source here, skip_temp is preliminary.
3557                  */
3558                 if (uart6) {
3559                         sio_data->skip_in |= BIT(5) | BIT(6);
3560                         sio_data->skip_temp |= BIT(2);
3561                 }
3562
3563                 sio_data->beep_pin = superio_inb(sioaddr,
3564                                                  IT87_SIO_BEEP_PIN_REG) & 0x3f;
3565         }
3566         if (sio_data->beep_pin)
3567                 pr_info("Beeping is supported\n");
3568
3569         if (config->smbus_bitmap && !base) {
3570                 u8 reg;
3571
3572                 superio_select(sioaddr, PME);
3573                 reg = superio_inb(sioaddr, IT87_SPECIAL_CFG_REG);
3574                 sio_data->ec_special_config = reg;
3575                 sio_data->smbus_bitmap = reg & config->smbus_bitmap;
3576         }
3577
3578 exit:
3579         superio_exit(sioaddr, doexit);
3580         return err;
3581 }
3582
3583 static void it87_init_regs(struct platform_device *pdev)
3584 {
3585         struct it87_data *data = platform_get_drvdata(pdev);
3586
3587         /* Initialize chip specific register pointers */
3588         switch (data->type) {
3589         case it8628:
3590         case it8686:
3591                 data->REG_FAN = IT87_REG_FAN;
3592                 data->REG_FANX = IT87_REG_FANX;
3593                 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3594                 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3595                 data->REG_PWM = IT87_REG_PWM;
3596                 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET_8686;
3597                 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW_8686;
3598                 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH_8686;
3599                 break;
3600         case it8625:
3601         case it8655:
3602         case it8665:
3603                 data->REG_FAN = IT87_REG_FAN_8665;
3604                 data->REG_FANX = IT87_REG_FANX_8665;
3605                 data->REG_FAN_MIN = IT87_REG_FAN_MIN_8665;
3606                 data->REG_FANX_MIN = IT87_REG_FANX_MIN_8665;
3607                 data->REG_PWM = IT87_REG_PWM_8665;
3608                 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3609                 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3610                 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3611                 break;
3612         case it8622:
3613                 data->REG_FAN = IT87_REG_FAN;
3614                 data->REG_FANX = IT87_REG_FANX;
3615                 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3616                 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3617                 data->REG_PWM = IT87_REG_PWM_8665;
3618                 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3619                 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3620                 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3621                 break;
3622         case it8613:
3623                 data->REG_FAN = IT87_REG_FAN;
3624                 data->REG_FANX = IT87_REG_FANX;
3625                 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3626                 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3627                 data->REG_PWM = IT87_REG_PWM_8665;
3628                 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3629                 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3630                 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3631                 break;
3632         default:
3633                 data->REG_FAN = IT87_REG_FAN;
3634                 data->REG_FANX = IT87_REG_FANX;
3635                 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3636                 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3637                 data->REG_PWM = IT87_REG_PWM;
3638                 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3639                 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3640                 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3641                 break;
3642         }
3643
3644         if (data->mmio) {
3645                 data->read = it87_mmio_read;
3646                 data->write = it87_mmio_write;
3647         } else if (has_bank_sel(data)) {
3648                 data->read = it87_io_read;
3649                 data->write = it87_io_write;
3650         } else {
3651                 data->read = _it87_io_read;
3652                 data->write = _it87_io_write;
3653         }
3654 }
3655
3656 /* Called when we have found a new IT87. */
3657 static void it87_init_device(struct platform_device *pdev)
3658 {
3659         struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
3660         struct it87_data *data = platform_get_drvdata(pdev);
3661         int tmp, i;
3662         u8 mask;
3663
3664         if (has_new_tempmap(data)) {
3665                 data->pwm_temp_map_shift = 3;
3666                 data->pwm_temp_map_mask = 0x07;
3667         } else {
3668                 data->pwm_temp_map_shift = 0;
3669                 data->pwm_temp_map_mask = 0x03;
3670         }
3671
3672         /*
3673          * For each PWM channel:
3674          * - If it is in automatic mode, setting to manual mode should set
3675          *   the fan to full speed by default.
3676          * - If it is in manual mode, we need a mapping to temperature
3677          *   channels to use when later setting to automatic mode later.
3678          *   Map to the first sensor by default (we are clueless.)
3679          * In both cases, the value can (and should) be changed by the user
3680          * prior to switching to a different mode.
3681          * Note that this is no longer needed for the IT8721F and later, as
3682          * these have separate registers for the temperature mapping and the
3683          * manual duty cycle.
3684          */
3685         for (i = 0; i < NUM_AUTO_PWM; i++) {
3686                 data->pwm_temp_map[i] = 0;
3687                 data->pwm_duty[i] = 0x7f;       /* Full speed */
3688                 data->auto_pwm[i][3] = 0x7f;    /* Full speed, hard-coded */
3689         }
3690
3691         /*
3692          * Some chips seem to have default value 0xff for all limit
3693          * registers. For low voltage limits it makes no sense and triggers
3694          * alarms, so change to 0 instead. For high temperature limits, it
3695          * means -1 degree C, which surprisingly doesn't trigger an alarm,
3696          * but is still confusing, so change to 127 degrees C.
3697          */
3698         for (i = 0; i < NUM_VIN_LIMIT; i++) {
3699                 tmp = data->read(data, IT87_REG_VIN_MIN(i));
3700                 if (tmp == 0xff)
3701                         data->write(data, IT87_REG_VIN_MIN(i), 0);
3702         }
3703         for (i = 0; i < data->num_temp_limit; i++) {
3704                 tmp = data->read(data, data->REG_TEMP_HIGH[i]);
3705                 if (tmp == 0xff)
3706                         data->write(data, data->REG_TEMP_HIGH[i], 127);
3707         }
3708
3709         /*
3710          * Temperature channels are not forcibly enabled, as they can be
3711          * set to two different sensor types and we can't guess which one
3712          * is correct for a given system. These channels can be enabled at
3713          * run-time through the temp{1-3}_type sysfs accessors if needed.
3714          */
3715
3716         /* Check if voltage monitors are reset manually or by some reason */
3717         tmp = data->read(data, IT87_REG_VIN_ENABLE);
3718         if ((tmp & 0xff) == 0) {
3719                 /* Enable all voltage monitors */
3720                 data->write(data, IT87_REG_VIN_ENABLE, 0xff);
3721         }
3722
3723         /* Check if tachometers are reset manually or by some reason */
3724         mask = 0x70 & ~(sio_data->skip_fan << 4);
3725         data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
3726         if ((data->fan_main_ctrl & mask) == 0) {
3727                 /* Enable all fan tachometers */
3728                 data->fan_main_ctrl |= mask;
3729                 data->write(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
3730         }
3731         data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
3732
3733         tmp = data->read(data, IT87_REG_FAN_16BIT);
3734
3735         /* Set tachometers to 16-bit mode if needed */
3736         if (has_fan16_config(data)) {
3737                 if (~tmp & 0x07 & data->has_fan) {
3738                         dev_dbg(&pdev->dev,
3739                                 "Setting fan1-3 to 16-bit mode\n");
3740                         data->write(data, IT87_REG_FAN_16BIT, tmp | 0x07);
3741                 }
3742         }
3743
3744         /* Check for additional fans */
3745         if (has_four_fans(data) && (tmp & BIT(4)))
3746                 data->has_fan |= BIT(3); /* fan4 enabled */
3747         if (has_five_fans(data) && (tmp & BIT(5)))
3748                 data->has_fan |= BIT(4); /* fan5 enabled */
3749         if (has_six_fans(data)) {
3750                 switch (data->type) {
3751                 case it8620:
3752                 case it8628:
3753                 case it8686:
3754                         if (tmp & BIT(2))
3755                                 data->has_fan |= BIT(5); /* fan6 enabled */
3756                         break;
3757                 case it8625:
3758                 case it8665:
3759                         tmp = data->read(data, IT87_REG_FAN_DIV);
3760                         if (tmp & BIT(3))
3761                                 data->has_fan |= BIT(5); /* fan6 enabled */
3762                         break;
3763                 default:
3764                         break;
3765                 }
3766         }
3767
3768         /* Fan input pins may be used for alternative functions */
3769         data->has_fan &= ~sio_data->skip_fan;
3770
3771         /* Check if pwm6 is enabled */
3772         if (has_six_pwm(data)) {
3773                 switch (data->type) {
3774                 case it8620:
3775                 case it8686:
3776                         tmp = data->read(data, IT87_REG_FAN_DIV);
3777                         if (!(tmp & BIT(3)))
3778                                 sio_data->skip_pwm |= BIT(5);
3779                         break;
3780                 default:
3781                         break;
3782                 }
3783         }
3784
3785         if (has_bank_sel(data)) {
3786                 for (i = 0; i < 3; i++)
3787                         data->temp_src[i] =
3788                                 data->read(data, IT87_REG_TEMP_SRC1[i]);
3789                 data->temp_src[3] = data->read(data, IT87_REG_TEMP_SRC2);
3790         }
3791
3792         /* Start monitoring */
3793         data->write(data, IT87_REG_CONFIG,
3794                     (data->read(data, IT87_REG_CONFIG) & 0x3e) |
3795                                         (update_vbat ? 0x41 : 0x01));
3796 }
3797
3798 /* Return 1 if and only if the PWM interface is safe to use */
3799 static int it87_check_pwm(struct device *dev)
3800 {
3801         struct it87_data *data = dev_get_drvdata(dev);
3802         /*
3803          * Some BIOSes fail to correctly configure the IT87 fans. All fans off
3804          * and polarity set to active low is sign that this is the case so we
3805          * disable pwm control to protect the user.
3806          */
3807         int tmp = data->read(data, IT87_REG_FAN_CTL);
3808
3809         if ((tmp & 0x87) == 0) {
3810                 if (fix_pwm_polarity) {
3811                         /*
3812                          * The user asks us to attempt a chip reconfiguration.
3813                          * This means switching to active high polarity and
3814                          * inverting all fan speed values.
3815                          */
3816                         int i;
3817                         u8 pwm[3];
3818
3819                         for (i = 0; i < ARRAY_SIZE(pwm); i++)
3820                                 pwm[i] = data->read(data,
3821                                                          data->REG_PWM[i]);
3822
3823                         /*
3824                          * If any fan is in automatic pwm mode, the polarity
3825                          * might be correct, as suspicious as it seems, so we
3826                          * better don't change anything (but still disable the
3827                          * PWM interface).
3828                          */
3829                         if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3830                                 dev_info(dev,
3831                                          "Reconfiguring PWM to active high polarity\n");
3832                                 data->write(data, IT87_REG_FAN_CTL, tmp | 0x87);
3833                                 for (i = 0; i < 3; i++)
3834                                         data->write(data, data->REG_PWM[i],
3835                                                     0x7f & ~pwm[i]);
3836                                 return 1;
3837                         }
3838
3839                         dev_info(dev,
3840                                  "PWM configuration is too broken to be fixed\n");
3841                 }
3842
3843                 dev_info(dev,
3844                          "Detected broken BIOS defaults, disabling PWM interface\n");
3845                 return 0;
3846         } else if (fix_pwm_polarity) {
3847                 dev_info(dev,
3848                          "PWM configuration looks sane, won't touch\n");
3849         }
3850
3851         return 1;
3852 }
3853
3854 static int it87_probe(struct platform_device *pdev)
3855 {
3856         struct it87_data *data;
3857         struct resource *res;
3858         struct device *dev = &pdev->dev;
3859         struct it87_sio_data *sio_data = dev_get_platdata(dev);
3860         int enable_pwm_interface;
3861         struct device *hwmon_dev;
3862         int err;
3863
3864         data = devm_kzalloc(dev, sizeof(struct it87_data), GFP_KERNEL);
3865         if (!data)
3866                 return -ENOMEM;
3867
3868         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3869         if (res) {
3870                 if (!devm_request_region(dev, res->start, IT87_EC_EXTENT,
3871                                          DRVNAME)) {
3872                         dev_err(dev, "Failed to request region %pR\n", res);
3873                         return -EBUSY;
3874                 }
3875         } else {
3876                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3877                 data->mmio = devm_ioremap_resource(dev, res);
3878                 if (IS_ERR(data->mmio))
3879                         return PTR_ERR(data->mmio);
3880         }
3881
3882         data->addr = res->start;
3883         data->type = sio_data->type;
3884         data->sioaddr = sio_data->sioaddr;
3885         data->smbus_bitmap = sio_data->smbus_bitmap;
3886         data->ec_special_config = sio_data->ec_special_config;
3887         data->doexit = sio_data->doexit;
3888         data->features = it87_devices[sio_data->type].features;
3889         data->num_temp_limit = it87_devices[sio_data->type].num_temp_limit;
3890         data->num_temp_offset = it87_devices[sio_data->type].num_temp_offset;
3891         data->pwm_num_temp_map = it87_devices[sio_data->type].num_temp_map;
3892         data->peci_mask = it87_devices[sio_data->type].peci_mask;
3893         data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3894
3895         /*
3896          * IT8705F Datasheet 0.4.1, 3h == Version G.
3897          * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3898          * These are the first revisions with 16-bit tachometer support.
3899          */
3900         switch (data->type) {
3901         case it87:
3902                 if (sio_data->revision >= 0x03) {
3903                         data->features &= ~FEAT_OLD_AUTOPWM;
3904                         data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3905                 }
3906                 break;
3907         case it8712:
3908                 if (sio_data->revision >= 0x08) {
3909                         data->features &= ~FEAT_OLD_AUTOPWM;
3910                         data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3911                                           FEAT_FIVE_FANS;
3912                 }
3913                 break;
3914         default:
3915                 break;
3916         }
3917
3918         platform_set_drvdata(pdev, data);
3919
3920         mutex_init(&data->update_lock);
3921
3922         /* Initialize register pointers */
3923         it87_init_regs(pdev);
3924
3925         err = smbus_disable(data);
3926         if (err)
3927                 return err;
3928
3929         /* Now, we do the remaining detection. */
3930         if ((data->read(data, IT87_REG_CONFIG) & 0x80) ||
3931             data->read(data, IT87_REG_CHIPID) != 0x90) {
3932                 smbus_enable(data);
3933                 return -ENODEV;
3934         }
3935
3936         /* Check PWM configuration */
3937         enable_pwm_interface = it87_check_pwm(dev);
3938
3939         /* Starting with IT8721F, we handle scaling of internal voltages */
3940         if (has_scaling(data)) {
3941                 if (sio_data->internal & BIT(0))
3942                         data->in_scaled |= BIT(3);      /* in3 is AVCC */
3943                 if (sio_data->internal & BIT(1))
3944                         data->in_scaled |= BIT(7);      /* in7 is VSB */
3945                 if (sio_data->internal & BIT(2))
3946                         data->in_scaled |= BIT(8);      /* in8 is Vbat */
3947                 if (sio_data->internal & BIT(3))
3948                         data->in_scaled |= BIT(9);      /* in9 is AVCC */
3949         } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3950                    sio_data->type == it8783) {
3951                 if (sio_data->internal & BIT(0))
3952                         data->in_scaled |= BIT(3);      /* in3 is VCC5V */
3953                 if (sio_data->internal & BIT(1))
3954                         data->in_scaled |= BIT(7);      /* in7 is VCCH5V */
3955         }
3956
3957         data->has_temp = 0x07;
3958         if (sio_data->skip_temp & BIT(2)) {
3959                 if (sio_data->type == it8782 &&
3960                     !(data->read(data, IT87_REG_TEMP_EXTRA) & 0x80))
3961                         data->has_temp &= ~BIT(2);
3962         }
3963
3964         data->in_internal = sio_data->internal;
3965         data->has_in = 0x3ff & ~sio_data->skip_in;
3966
3967         if (has_four_temp(data)) {
3968                 data->has_temp |= BIT(3);
3969         } else if (has_six_temp(data)) {
3970                 u8 reg = data->read(data, IT87_REG_TEMP456_ENABLE);
3971
3972                 /* Check for additional temperature sensors */
3973                 if ((reg & 0x03) >= 0x02)
3974                         data->has_temp |= BIT(3);
3975                 if (((reg >> 2) & 0x03) >= 0x02)
3976                         data->has_temp |= BIT(4);
3977                 if (((reg >> 4) & 0x03) >= 0x02)
3978                         data->has_temp |= BIT(5);
3979
3980                 /* Check for additional voltage sensors */
3981                 if ((reg & 0x03) == 0x01)
3982                         data->has_in |= BIT(10);
3983                 if (((reg >> 2) & 0x03) == 0x01)
3984                         data->has_in |= BIT(11);
3985                 if (((reg >> 4) & 0x03) == 0x01)
3986                         data->has_in |= BIT(12);
3987         }
3988
3989         data->has_beep = !!sio_data->beep_pin;
3990
3991         /* Initialize the IT87 chip */
3992         it87_init_device(pdev);
3993
3994         smbus_enable(data);
3995
3996         if (!sio_data->skip_vid) {
3997                 data->has_vid = true;
3998                 data->vrm = vid_which_vrm();
3999                 /* VID reading from Super-I/O config space if available */
4000                 data->vid = sio_data->vid_value;
4001         }
4002
4003         /* Prepare for sysfs hooks */
4004         data->groups[0] = &it87_group;
4005         data->groups[1] = &it87_group_in;
4006         data->groups[2] = &it87_group_temp;
4007         data->groups[3] = &it87_group_fan;
4008
4009         if (enable_pwm_interface) {
4010                 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
4011                 data->has_pwm &= ~sio_data->skip_pwm;
4012
4013                 data->groups[4] = &it87_group_pwm;
4014                 if (has_old_autopwm(data) || has_newer_autopwm(data))
4015                         data->groups[5] = &it87_group_auto_pwm;
4016         }
4017
4018         hwmon_dev = devm_hwmon_device_register_with_groups(dev,
4019                                         it87_devices[sio_data->type].name,
4020                                         data, data->groups);
4021         return PTR_ERR_OR_ZERO(hwmon_dev);
4022 }
4023
4024 static struct platform_driver it87_driver = {
4025         .driver = {
4026                 .name   = DRVNAME,
4027         },
4028         .probe  = it87_probe,
4029 };
4030
4031 static int __init it87_device_add(int index, unsigned short sio_address,
4032                                   phys_addr_t mmio_address,
4033                                   const struct it87_sio_data *sio_data)
4034 {
4035         struct platform_device *pdev;
4036         struct resource res = {
4037                 .name   = DRVNAME,
4038         };
4039         int err;
4040
4041         if (mmio_address) {
4042                 res.start = mmio_address;
4043                 res.end = mmio_address + 0x400 - 1;
4044                 res.flags = IORESOURCE_MEM;
4045         } else {
4046                 res.start = sio_address + IT87_EC_OFFSET;
4047                 res.end = sio_address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1;
4048                 res.flags = IORESOURCE_IO;
4049         }
4050
4051         err = acpi_check_resource_conflict(&res);
4052         if (err) {
4053                 if (!ignore_resource_conflict)
4054                         return err;
4055         }
4056
4057         pdev = platform_device_alloc(DRVNAME, sio_address);
4058         if (!pdev)
4059                 return -ENOMEM;
4060
4061         err = platform_device_add_resources(pdev, &res, 1);
4062         if (err) {
4063                 pr_err("Device resource addition failed (%d)\n", err);
4064                 goto exit_device_put;
4065         }
4066
4067         err = platform_device_add_data(pdev, sio_data,
4068                                        sizeof(struct it87_sio_data));
4069         if (err) {
4070                 pr_err("Platform data allocation failed\n");
4071                 goto exit_device_put;
4072         }
4073
4074         err = platform_device_add(pdev);
4075         if (err) {
4076                 pr_err("Device addition failed (%d)\n", err);
4077                 goto exit_device_put;
4078         }
4079
4080         it87_pdev[index] = pdev;
4081         return 0;
4082
4083 exit_device_put:
4084         platform_device_put(pdev);
4085         return err;
4086 }
4087
4088 struct it87_dmi_data {
4089         bool sio2_force_config; /* force sio2 into configuration mode   */
4090         u8 skip_pwm;            /* pwm channels to skip for this board  */
4091 };
4092
4093 /*
4094  * On various Gigabyte AM4 boards (AB350, AX370), the second Super-IO chip
4095  * (IT8792E) needs to be in configuration mode before accessing the first
4096  * due to a bug in IT8792E which otherwise results in LPC bus access errors.
4097  * This needs to be done before accessing the first Super-IO chip since
4098  * the second chip may have been accessed prior to loading this driver.
4099  *
4100  * The problem is also reported to affect IT8795E, which is used on X299 boards
4101  * and has the same chip ID as IT8792E (0x8733). It also appears to affect
4102  * systems with IT8790E, which is used on some Z97X-Gaming boards as well as
4103  * Z87X-OC.
4104  * DMI entries for those systems will be added as they become available and
4105  * as the problem is confirmed to affect those boards.
4106  */
4107 static struct it87_dmi_data gigabyte_sio2_force = {
4108         .sio2_force_config = true,
4109 };
4110
4111 /*
4112  * On the Shuttle SN68PT, FAN_CTL2 is apparently not
4113  * connected to a fan, but to something else. One user
4114  * has reported instant system power-off when changing
4115  * the PWM2 duty cycle, so we disable it.
4116  * I use the board name string as the trigger in case
4117  * the same board is ever used in other systems.
4118  */
4119 static struct it87_dmi_data nvidia_fn68pt = {
4120         .skip_pwm = BIT(1),
4121 };
4122
4123 static const struct dmi_system_id it87_dmi_table[] __initconst = {
4124         {
4125                 .matches = {
4126                         DMI_MATCH(DMI_SYS_VENDOR,
4127                                   "Gigabyte Technology Co., Ltd."),
4128                         DMI_MATCH(DMI_BOARD_NAME, "AB350"),
4129                 },
4130                 .driver_data = &gigabyte_sio2_force,
4131         },
4132         {
4133                 .matches = {
4134                         DMI_MATCH(DMI_SYS_VENDOR,
4135                                   "Gigabyte Technology Co., Ltd."),
4136                         DMI_MATCH(DMI_BOARD_NAME, "AX370"),
4137                 },
4138                 .driver_data = &gigabyte_sio2_force,
4139         },
4140         {
4141                 .matches = {
4142                         DMI_MATCH(DMI_SYS_VENDOR,
4143                                   "Gigabyte Technology Co., Ltd."),
4144                         DMI_MATCH(DMI_BOARD_NAME, "Z97X-Gaming G1"),
4145                 },
4146                 .driver_data = &gigabyte_sio2_force,
4147         },
4148         {
4149                 .matches = {
4150                         DMI_MATCH(DMI_BOARD_VENDOR, "nVIDIA"),
4151                         DMI_MATCH(DMI_BOARD_NAME, "FN68PT"),
4152                 },
4153                 .driver_data = &nvidia_fn68pt,
4154         },
4155         { }
4156 };
4157
4158 static int __init sm_it87_init(void)
4159 {
4160         const struct dmi_system_id *dmi = dmi_first_match(it87_dmi_table);
4161         struct it87_dmi_data *dmi_data = NULL;
4162         int sioaddr[2] = { REG_2E, REG_4E };
4163         struct it87_sio_data sio_data;
4164         unsigned short isa_address;
4165         phys_addr_t mmio_address;
4166         bool found = false;
4167         int i, err;
4168
4169         pr_info("it87 driver version %s\n", IT87_DRIVER_VERSION);
4170
4171         if (dmi)
4172                 dmi_data = dmi->driver_data;
4173
4174         err = platform_driver_register(&it87_driver);
4175         if (err)
4176                 return err;
4177
4178         if (dmi_data && dmi_data->sio2_force_config)
4179                 __superio_enter(REG_4E);
4180
4181         for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
4182                 memset(&sio_data, 0, sizeof(struct it87_sio_data));
4183                 isa_address = 0;
4184                 mmio_address = 0;
4185                 err = it87_find(sioaddr[i], &isa_address, &mmio_address,
4186                                 &sio_data);
4187                 if (err || isa_address == 0)
4188                         continue;
4189
4190                 if (dmi_data)
4191                         sio_data.skip_pwm |= dmi_data->skip_pwm;
4192                 err = it87_device_add(i, isa_address, mmio_address, &sio_data);
4193                 if (err)
4194                         goto exit_dev_unregister;
4195                 found = true;
4196         }
4197
4198         if (!found) {
4199                 err = -ENODEV;
4200                 goto exit_unregister;
4201         }
4202         return 0;
4203
4204 exit_dev_unregister:
4205         /* NULL check handled by platform_device_unregister */
4206         platform_device_unregister(it87_pdev[0]);
4207 exit_unregister:
4208         platform_driver_unregister(&it87_driver);
4209         return err;
4210 }
4211
4212 static void __exit sm_it87_exit(void)
4213 {
4214         /* NULL check handled by platform_device_unregister */
4215         platform_device_unregister(it87_pdev[1]);
4216         platform_device_unregister(it87_pdev[0]);
4217         platform_driver_unregister(&it87_driver);
4218 }
4219
4220 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
4221 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
4222 module_param(update_vbat, bool, 0000);
4223 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
4224 module_param(fix_pwm_polarity, bool, 0000);
4225 MODULE_PARM_DESC(fix_pwm_polarity,
4226                  "Force PWM polarity to active high (DANGEROUS)");
4227 MODULE_LICENSE("GPL");
4228
4229 module_init(sm_it87_init);
4230 module_exit(sm_it87_exit);