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