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