2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
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
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
13 * Supports: IT8603E Super I/O chip w/LPC interface
14 * IT8607E Super I/O chip w/LPC interface
15 * IT8613E Super I/O chip w/LPC interface
16 * IT8620E Super I/O chip w/LPC interface
17 * IT8622E Super I/O chip w/LPC interface
18 * IT8623E Super I/O chip w/LPC interface
19 * IT8625E Super I/O chip w/LPC interface
20 * IT8628E Super I/O chip w/LPC interface
21 * IT8655E Super I/O chip w/LPC interface
22 * IT8665E Super I/O chip w/LPC interface
23 * IT8686E Super I/O chip w/LPC interface
24 * IT8705F Super I/O chip w/LPC interface
25 * IT8712F Super I/O chip w/LPC interface
26 * IT8716F Super I/O chip w/LPC interface
27 * IT8718F Super I/O chip w/LPC interface
28 * IT8720F Super I/O chip w/LPC interface
29 * IT8721F Super I/O chip w/LPC interface
30 * IT8726F Super I/O chip w/LPC interface
31 * IT8728F Super I/O chip w/LPC interface
32 * IT8732F Super I/O chip w/LPC interface
33 * IT8758E Super I/O chip w/LPC interface
34 * IT8771E Super I/O chip w/LPC interface
35 * IT8772E Super I/O chip w/LPC interface
36 * IT8781F Super I/O chip w/LPC interface
37 * IT8782F Super I/O chip w/LPC interface
38 * IT8783E/F Super I/O chip w/LPC interface
39 * IT8786E Super I/O chip w/LPC interface
40 * IT8790E Super I/O chip w/LPC interface
41 * IT8792E Super I/O chip w/LPC interface
42 * Sis950 A clone of the IT8705F
44 * Copyright (C) 2001 Chris Gauthron
45 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License as published by
49 * the Free Software Foundation; either version 2 of the License, or
50 * (at your option) any later version.
52 * This program is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 * GNU General Public License for more details.
58 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
60 #include <linux/bitops.h>
61 #include <linux/module.h>
62 #include <linux/init.h>
63 #include <linux/slab.h>
64 #include <linux/jiffies.h>
65 #include <linux/platform_device.h>
66 #include <linux/hwmon.h>
67 #include <linux/hwmon-sysfs.h>
68 #include <linux/hwmon-vid.h>
69 #include <linux/err.h>
70 #include <linux/mutex.h>
71 #include <linux/sysfs.h>
72 #include <linux/string.h>
73 #include <linux/dmi.h>
74 #include <linux/acpi.h>
79 #define DRVNAME "it87"
81 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
82 it8771, it8772, it8781, it8782, it8783, it8786, it8790,
83 it8792, it8603, it8607, it8613, it8620, it8622, it8625, it8628,
84 it8655, it8665, it8686 };
86 static unsigned short force_id;
87 module_param(force_id, ushort, 0);
88 MODULE_PARM_DESC(force_id, "Override the detected device ID");
90 static bool ignore_resource_conflict;
91 module_param(ignore_resource_conflict, bool, 0);
92 MODULE_PARM_DESC(ignore_resource_conflict, "Ignore ACPI resource conflict");
95 module_param(mmio, bool, 0);
96 MODULE_PARM_DESC(mmio, "Use MMIO if available");
98 static struct platform_device *it87_pdev[2];
100 #define REG_2E 0x2e /* The register to read/write */
101 #define REG_4E 0x4e /* Secondary register to read/write */
103 #define DEV 0x07 /* Register: Logical device select */
104 #define PME 0x04 /* The device with the fan registers in it */
106 /* The device with the IT8718F/IT8720F VID value in it */
109 #define DEVID 0x20 /* Register: Device ID */
110 #define DEVREV 0x22 /* Register: Device Revision */
112 static inline void __superio_enter(int ioreg)
117 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
120 static inline int superio_inb(int ioreg, int reg)
125 val = inb(ioreg + 1);
130 static inline void superio_outb(int ioreg, int reg, int val)
133 outb(val, ioreg + 1);
136 static int superio_inw(int ioreg, int reg)
138 return (superio_inb(ioreg, reg) << 8) | superio_inb(ioreg, reg + 1);
141 static inline void superio_select(int ioreg, int ldn)
144 outb(ldn, ioreg + 1);
147 static inline int superio_enter(int ioreg)
150 * Try to reserve ioreg and ioreg + 1 for exclusive access.
152 if (!request_muxed_region(ioreg, 2, DRVNAME))
155 __superio_enter(ioreg);
159 static inline void superio_exit(int ioreg, bool doexit)
163 outb(0x02, ioreg + 1);
165 release_region(ioreg, 2);
168 /* Logical device 4 registers */
169 #define IT8712F_DEVID 0x8712
170 #define IT8705F_DEVID 0x8705
171 #define IT8716F_DEVID 0x8716
172 #define IT8718F_DEVID 0x8718
173 #define IT8720F_DEVID 0x8720
174 #define IT8721F_DEVID 0x8721
175 #define IT8726F_DEVID 0x8726
176 #define IT8728F_DEVID 0x8728
177 #define IT8732F_DEVID 0x8732
178 #define IT8792E_DEVID 0x8733
179 #define IT8771E_DEVID 0x8771
180 #define IT8772E_DEVID 0x8772
181 #define IT8781F_DEVID 0x8781
182 #define IT8782F_DEVID 0x8782
183 #define IT8783E_DEVID 0x8783
184 #define IT8786E_DEVID 0x8786
185 #define IT8790E_DEVID 0x8790
186 #define IT8603E_DEVID 0x8603
187 #define IT8607E_DEVID 0x8607
188 #define IT8613E_DEVID 0x8613
189 #define IT8620E_DEVID 0x8620
190 #define IT8622E_DEVID 0x8622
191 #define IT8623E_DEVID 0x8623
192 #define IT8625E_DEVID 0x8625
193 #define IT8628E_DEVID 0x8628
194 #define IT8655E_DEVID 0x8655
195 #define IT8665E_DEVID 0x8665
196 #define IT8686E_DEVID 0x8686
198 /* Logical device 4 (Environmental Monitor) registers */
199 #define IT87_ACT_REG 0x30
200 #define IT87_BASE_REG 0x60
201 #define IT87_SPECIAL_CFG_REG 0xf3 /* special configuration register */
203 /* Global configuration registers (IT8712F and later) */
204 #define IT87_EC_HWM_MIO_REG 0x24 /* MMIO configuration register */
205 #define IT87_SIO_GPIO1_REG 0x25
206 #define IT87_SIO_GPIO2_REG 0x26
207 #define IT87_SIO_GPIO3_REG 0x27
208 #define IT87_SIO_GPIO4_REG 0x28
209 #define IT87_SIO_GPIO5_REG 0x29
210 #define IT87_SIO_GPIO9_REG 0xd3
211 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
212 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
213 #define IT87_SIO_PINX4_REG 0x2d /* Pin selection */
215 /* Logical device 7 (GPIO) registers (IT8712F and later) */
216 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
217 #define IT87_SIO_VID_REG 0xfc /* VID value */
218 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
220 /* Update battery voltage after every reading if true */
221 static bool update_vbat;
223 /* Not all BIOSes properly configure the PWM registers */
224 static bool fix_pwm_polarity;
226 /* Many IT87 constants specified below */
228 /* Length of ISA address segment */
229 #define IT87_EXTENT 8
231 /* Length of ISA address segment for Environmental Controller */
232 #define IT87_EC_EXTENT 2
234 /* Offset of EC registers from ISA base address */
235 #define IT87_EC_OFFSET 5
237 /* Where are the ISA address/data registers relative to the EC base address */
238 #define IT87_ADDR_REG_OFFSET 0
239 #define IT87_DATA_REG_OFFSET 1
241 /*----- The IT87 registers -----*/
243 #define IT87_REG_CONFIG 0x00
245 #define IT87_REG_ALARM1 0x01
246 #define IT87_REG_ALARM2 0x02
247 #define IT87_REG_ALARM3 0x03
249 #define IT87_REG_BANK 0x06
252 * The IT8718F and IT8720F have the VID value in a different register, in
253 * Super-I/O configuration space.
255 #define IT87_REG_VID 0x0a
257 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
258 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
261 #define IT87_REG_FAN_DIV 0x0b
262 #define IT87_REG_FAN_16BIT 0x0c
266 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
267 * - up to 6 temp (1 to 6)
268 * - up to 6 fan (1 to 6)
271 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
272 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
273 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
274 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
276 static const u8 IT87_REG_FAN_8665[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x93 };
277 static const u8 IT87_REG_FAN_MIN_8665[] =
278 { 0x10, 0x11, 0x12, 0x84, 0x86, 0xb2 };
279 static const u8 IT87_REG_FANX_8665[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x94 };
280 static const u8 IT87_REG_FANX_MIN_8665[] =
281 { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0xb3 };
283 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59, 0x5a, 0x90, 0x91 };
285 static const u8 IT87_REG_TEMP_OFFSET_8686[] = { 0x56, 0x57, 0x59, 0x90, 0x91, 0x92 };
287 #define IT87_REG_FAN_MAIN_CTRL 0x13
288 #define IT87_REG_FAN_CTL 0x14
290 static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
291 static const u8 IT87_REG_PWM_8665[] = { 0x15, 0x16, 0x17, 0x1e, 0x1f, 0x92 };
293 static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
295 static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
296 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
298 #define IT87_REG_TEMP(nr) (0x29 + (nr))
300 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
301 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
303 static const u8 IT87_REG_TEMP_HIGH[] = { 0x40, 0x42, 0x44, 0x46, 0xb4, 0xb6 };
304 static const u8 IT87_REG_TEMP_LOW[] = { 0x41, 0x43, 0x45, 0x47, 0xb5, 0xb7 };
306 static const u8 IT87_REG_TEMP_HIGH_8686[] =
307 { 0x40, 0x42, 0x44, 0xb4, 0xb6, 0xb8 };
308 static const u8 IT87_REG_TEMP_LOW_8686[] =
309 { 0x41, 0x43, 0x45, 0xb5, 0xb7, 0xb9 };
311 #define IT87_REG_VIN_ENABLE 0x50
312 #define IT87_REG_TEMP_ENABLE 0x51
313 #define IT87_REG_TEMP_EXTRA 0x55
314 #define IT87_REG_BEEP_ENABLE 0x5c
316 #define IT87_REG_CHIPID 0x58
318 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
320 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
321 #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
323 #define IT87_REG_TEMP456_ENABLE 0x77
325 static const u16 IT87_REG_TEMP_SRC1[] = { 0x21d, 0x21e, 0x21f };
326 #define IT87_REG_TEMP_SRC2 0x23d
328 #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
329 #define NUM_VIN_LIMIT 8
331 #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
332 #define NUM_FAN_DIV 3
333 #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
334 #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
336 struct it87_devices {
338 const char * const suffix;
342 u8 num_temp_map; /* Number of temperature sources for pwm */
345 u8 smbus_bitmap; /* SMBus enable bits in extra config register */
346 u8 ec_special_config;
349 #define FEAT_12MV_ADC BIT(0)
350 #define FEAT_NEWER_AUTOPWM BIT(1)
351 #define FEAT_OLD_AUTOPWM BIT(2)
352 #define FEAT_16BIT_FANS BIT(3)
353 #define FEAT_TEMP_PECI BIT(5)
354 #define FEAT_TEMP_OLD_PECI BIT(6)
355 #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
356 #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
357 #define FEAT_VID BIT(9) /* Set if chip supports VID */
358 #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
359 #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
360 #define FEAT_10_9MV_ADC BIT(12)
361 #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
362 #define FEAT_FIVE_PWM BIT(14) /* Chip supports 5 pwm chn */
363 #define FEAT_SIX_PWM BIT(15) /* Chip supports 6 pwm chn */
364 #define FEAT_PWM_FREQ2 BIT(16) /* Separate pwm freq 2 */
365 #define FEAT_SIX_TEMP BIT(17) /* Up to 6 temp sensors */
366 #define FEAT_VIN3_5V BIT(18) /* VIN3 connected to +5V */
367 #define FEAT_FOUR_FANS BIT(19) /* Supports four fans */
368 #define FEAT_FOUR_PWM BIT(20) /* Supports four fan controls */
369 #define FEAT_BANK_SEL BIT(21) /* Chip has multi-bank support */
370 #define FEAT_SCALING BIT(22) /* Internal voltage scaling */
371 #define FEAT_FANCTL_ONOFF BIT(23) /* chip has FAN_CTL ON/OFF */
372 #define FEAT_11MV_ADC BIT(24)
373 #define FEAT_NEW_TEMPMAP BIT(25) /* new temp input selection */
374 #define FEAT_MMIO BIT(26) /* Chip supports MMIO */
376 static const struct it87_devices it87_devices[] = {
380 .features = FEAT_OLD_AUTOPWM | FEAT_FANCTL_ONOFF,
381 /* may need to overwrite */
383 .num_temp_offset = 0,
389 .features = FEAT_OLD_AUTOPWM | FEAT_VID | FEAT_FANCTL_ONOFF,
390 /* may need to overwrite */
392 .num_temp_offset = 0,
398 .features = FEAT_16BIT_FANS | FEAT_VID
399 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2
402 .num_temp_offset = 3,
408 .features = FEAT_16BIT_FANS | FEAT_VID
409 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
410 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
412 .num_temp_offset = 3,
414 .old_peci_mask = 0x4,
419 .features = FEAT_16BIT_FANS | FEAT_VID
420 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
421 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
423 .num_temp_offset = 3,
425 .old_peci_mask = 0x4,
430 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
431 | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
432 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
433 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
435 .num_temp_offset = 3,
438 .old_peci_mask = 0x02, /* Actually reports PCH */
443 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
444 | FEAT_TEMP_PECI | FEAT_FIVE_FANS
445 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_SCALING
448 .num_temp_offset = 3,
455 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
456 | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
457 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_FOUR_FANS
458 | FEAT_FOUR_PWM | FEAT_FANCTL_ONOFF,
460 .num_temp_offset = 3,
463 .old_peci_mask = 0x02, /* Actually reports PCH */
468 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
469 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
470 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
471 /* PECI: guesswork */
473 /* 16 bit fans (OHM) */
474 /* three fans, always 16 bit (guesswork) */
476 .num_temp_offset = 3,
483 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
484 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
485 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
486 /* PECI (coreboot) */
487 /* 12mV ADC (HWSensors4, OHM) */
488 /* 16 bit fans (HWSensors4, OHM) */
489 /* three fans, always 16 bit (datasheet) */
491 .num_temp_offset = 3,
498 .features = FEAT_16BIT_FANS
499 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
502 .num_temp_offset = 3,
504 .old_peci_mask = 0x4,
509 .features = FEAT_16BIT_FANS
510 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
513 .num_temp_offset = 3,
515 .old_peci_mask = 0x4,
520 .features = FEAT_16BIT_FANS
521 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
524 .num_temp_offset = 3,
526 .old_peci_mask = 0x4,
531 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
532 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
533 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
535 .num_temp_offset = 3,
542 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
543 | FEAT_16BIT_FANS | FEAT_TEMP_PECI
544 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
546 .num_temp_offset = 3,
553 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
554 | FEAT_16BIT_FANS | FEAT_TEMP_PECI
555 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
557 .num_temp_offset = 3,
564 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
565 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
566 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
568 .num_temp_offset = 3,
575 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
576 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL | FEAT_NEW_TEMPMAP
577 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING
580 .num_temp_offset = 3,
587 .features = FEAT_NEWER_AUTOPWM | FEAT_11MV_ADC | FEAT_16BIT_FANS
588 | FEAT_TEMP_PECI | FEAT_FIVE_FANS
589 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
590 | FEAT_AVCC3 | FEAT_SCALING | FEAT_NEW_TEMPMAP,
592 .num_temp_offset = 6,
599 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
600 | FEAT_TEMP_PECI | FEAT_SIX_FANS
601 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
602 | FEAT_SIX_TEMP | FEAT_VIN3_5V | FEAT_SCALING
605 .num_temp_offset = 3,
612 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
613 | FEAT_TEMP_PECI | FEAT_FIVE_FANS
614 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
615 | FEAT_AVCC3 | FEAT_VIN3_5V | FEAT_SCALING,
617 .num_temp_offset = 3,
624 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
625 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP
626 | FEAT_11MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
627 | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_SCALING,
629 .num_temp_offset = 6,
631 .smbus_bitmap = BIT(1) | BIT(2),
636 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
637 | FEAT_TEMP_PECI | FEAT_SIX_FANS
638 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
639 | FEAT_SIX_TEMP | FEAT_SCALING | FEAT_AVCC3
642 .num_temp_offset = 3,
649 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
650 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
651 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_BANK_SEL,
653 .num_temp_offset = 6,
655 .smbus_bitmap = BIT(2),
660 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
661 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
662 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
663 | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_MMIO,
665 .num_temp_offset = 6,
667 .smbus_bitmap = BIT(2),
672 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
673 | FEAT_SIX_FANS | FEAT_NEW_TEMPMAP
674 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
675 | FEAT_SIX_TEMP | FEAT_BANK_SEL | FEAT_SCALING | FEAT_AVCC3,
677 .num_temp_offset = 6,
679 .smbus_bitmap = BIT(1) | BIT(2),
683 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
684 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
685 #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
686 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
687 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
688 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
689 ((data)->peci_mask & BIT(nr)))
690 #define has_temp_old_peci(data, nr) \
691 (((data)->features & FEAT_TEMP_OLD_PECI) && \
692 ((data)->old_peci_mask & BIT(nr)))
693 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
694 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
696 #define has_vid(data) ((data)->features & FEAT_VID)
697 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
698 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
699 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
700 #define has_five_pwm(data) ((data)->features & (FEAT_FIVE_PWM \
702 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
703 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
704 #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
705 #define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
706 #define has_four_fans(data) ((data)->features & (FEAT_FOUR_FANS | \
709 #define has_four_pwm(data) ((data)->features & (FEAT_FOUR_PWM | \
712 #define has_bank_sel(data) ((data)->features & FEAT_BANK_SEL)
713 #define has_scaling(data) ((data)->features & FEAT_SCALING)
714 #define has_fanctl_onoff(data) ((data)->features & FEAT_FANCTL_ONOFF)
715 #define has_11mv_adc(data) ((data)->features & FEAT_11MV_ADC)
716 #define has_new_tempmap(data) ((data)->features & FEAT_NEW_TEMPMAP)
717 #define has_mmio(data) ((data)->features & FEAT_MMIO)
719 struct it87_sio_data {
723 /* Values read from Super-I/O config space */
727 u8 internal; /* Internal sensors can be labeled */
728 /* Features skipped based on config or DMI */
735 u8 ec_special_config;
739 * For each registered chip, we need to keep some data in memory.
740 * The structure is dynamically allocated.
743 const struct attribute_group *groups[7];
749 u8 smbus_bitmap; /* !=0 if SMBus needs to be disabled */
750 u8 ec_special_config; /* EC special config register restore value */
751 u8 sioaddr; /* SIO port address */
752 bool doexit; /* true if exit from sio config is ok */
754 void __iomem *mmio; /* Remapped MMIO address if available */
755 int (*read)(struct it87_data *, u16);
756 void (*write)(struct it87_data *, u16, u8);
760 const u8 *REG_FAN_MIN;
761 const u8 *REG_FANX_MIN;
765 const u8 *REG_TEMP_OFFSET;
766 const u8 *REG_TEMP_LOW;
767 const u8 *REG_TEMP_HIGH;
771 struct mutex update_lock;
772 char valid; /* !=0 if following fields are valid */
773 unsigned long last_updated; /* In jiffies */
775 u16 in_scaled; /* Internal voltage sensors are scaled */
776 u16 in_internal; /* Bitfield, internal sensors (for labels) */
777 u16 has_in; /* Bitfield, voltage sensors enabled */
778 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
779 u8 has_fan; /* Bitfield, fans enabled */
780 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
781 u8 has_temp; /* Bitfield, temp sensors enabled */
782 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
783 u8 num_temp_limit; /* Number of temperature limit registers */
784 u8 num_temp_offset; /* Number of temperature offset registers */
785 u8 temp_src[4]; /* Up to 4 temperature source registers */
786 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
787 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
788 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
789 bool has_vid; /* True if VID supported */
790 u8 vid; /* Register encoding, combined */
792 u32 alarms; /* Register encoding, combined */
793 bool has_beep; /* true if beep supported */
794 u8 beeps; /* Register encoding */
795 u8 fan_main_ctrl; /* Register value */
796 u8 fan_ctl; /* Register value */
799 * The following 3 arrays correspond to the same registers up to
800 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
801 * 7, and we want to preserve settings on mode changes, so we have
802 * to track all values separately.
803 * Starting with the IT8721F, the manual PWM duty cycles are stored
804 * in separate registers (8-bit values), so the separate tracking
805 * is no longer needed, but it is still done to keep the driver
808 u8 has_pwm; /* Bitfield, pwm control enabled */
809 u8 pwm_ctrl[NUM_PWM]; /* Register value */
810 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
811 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
812 u8 pwm_temp_map_mask; /* 0x03 for old, 0x07 for new temp map */
813 u8 pwm_temp_map_shift; /* 0 for old, 3 for new temp map */
814 u8 pwm_num_temp_map; /* from config data, 3..7 depending on chip */
816 /* Automatic fan speed control registers */
817 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
818 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
821 static int adc_lsb(const struct it87_data *data, int nr)
825 if (has_12mv_adc(data))
827 else if (has_10_9mv_adc(data))
829 else if (has_11mv_adc(data))
833 if (data->in_scaled & BIT(nr))
838 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
840 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
841 return clamp_val(val, 0, 255);
844 static int in_from_reg(const struct it87_data *data, int nr, int val)
846 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
849 static inline u8 FAN_TO_REG(long rpm, int div)
853 rpm = clamp_val(rpm, 1, 1000000);
854 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
857 static inline u16 FAN16_TO_REG(long rpm)
861 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
864 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
865 1350000 / ((val) * (div)))
866 /* The divider is fixed to 2 in 16-bit mode */
867 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
868 1350000 / ((val) * 2))
870 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
871 ((val) + 500) / 1000), -128, 127))
872 #define TEMP_FROM_REG(val) ((val) * 1000)
874 static u8 pwm_to_reg(const struct it87_data *data, long val)
876 if (has_newer_autopwm(data))
882 static int pwm_from_reg(const struct it87_data *data, u8 reg)
884 if (has_newer_autopwm(data))
887 return (reg & 0x7f) << 1;
890 static int DIV_TO_REG(int val)
894 while (answer < 7 && (val >>= 1))
899 #define DIV_FROM_REG(val) BIT(val)
901 static u8 temp_map_from_reg(const struct it87_data *data, u8 reg)
905 map = (reg >> data->pwm_temp_map_shift) & data->pwm_temp_map_mask;
906 if (map >= data->pwm_num_temp_map) /* map is 0-based */
912 static u8 temp_map_to_reg(const struct it87_data *data, int nr, u8 map)
914 u8 ctrl = data->pwm_ctrl[nr];
916 return (ctrl & ~(data->pwm_temp_map_mask << data->pwm_temp_map_shift)) |
917 (map << data->pwm_temp_map_shift);
921 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
922 * depending on the chip type, to calculate the actual PWM frequency.
924 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
925 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
926 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
927 * sometimes just one. It is unknown if this is a datasheet error or real,
928 * so this is ignored for now.
930 static const unsigned int pwm_freq[8] = {
941 static int smbus_disable(struct it87_data *data)
945 if (data->smbus_bitmap) {
946 err = superio_enter(data->sioaddr);
949 superio_select(data->sioaddr, PME);
950 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
951 data->ec_special_config & ~data->smbus_bitmap);
952 superio_exit(data->sioaddr, data->doexit);
957 static int smbus_enable(struct it87_data *data)
961 if (data->smbus_bitmap) {
962 err = superio_enter(data->sioaddr);
966 superio_select(data->sioaddr, PME);
967 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
968 data->ec_special_config);
969 superio_exit(data->sioaddr, data->doexit);
974 static int _it87_io_read(struct it87_data *data, u16 reg)
976 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
977 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
980 static void _it87_io_write(struct it87_data *data, u16 reg, u8 value)
982 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
983 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
986 static u8 it87_io_set_bank(struct it87_data *data, u8 bank)
990 if (has_bank_sel(data)) {
991 u8 breg = _it87_io_read(data, IT87_REG_BANK);
997 _it87_io_write(data, IT87_REG_BANK, breg);
1004 * Must be called with data->update_lock held, except during initialization.
1005 * Must be called with SMBus accesses disabled.
1006 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1007 * would slow down the IT87 access and should not be necessary.
1009 static int it87_io_read(struct it87_data *data, u16 reg)
1014 bank = it87_io_set_bank(data, reg >> 8);
1015 val = _it87_io_read(data, reg & 0xff);
1016 it87_io_set_bank(data, bank);
1022 * Must be called with data->update_lock held, except during initialization.
1023 * Must be called with SMBus accesses disabled
1024 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1025 * would slow down the IT87 access and should not be necessary.
1027 static void it87_io_write(struct it87_data *data, u16 reg, u8 value)
1031 bank = it87_io_set_bank(data, reg >> 8);
1032 _it87_io_write(data, reg & 0xff, value);
1033 it87_io_set_bank(data, bank);
1036 static int it87_mmio_read(struct it87_data *data, u16 reg)
1038 return readb(data->mmio + reg);
1041 static void it87_mmio_write(struct it87_data *data, u16 reg, u8 value)
1043 writeb(value, data->mmio + reg);
1046 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1050 ctrl = data->read(data, data->REG_PWM[nr]);
1051 data->pwm_ctrl[nr] = ctrl;
1052 if (has_newer_autopwm(data)) {
1053 data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1054 data->pwm_duty[nr] = data->read(data, IT87_REG_PWM_DUTY[nr]);
1056 if (ctrl & 0x80) /* Automatic mode */
1057 data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1058 else /* Manual mode */
1059 data->pwm_duty[nr] = ctrl & 0x7f;
1062 if (has_old_autopwm(data)) {
1065 for (i = 0; i < 5 ; i++)
1066 data->auto_temp[nr][i] = data->read(data,
1067 IT87_REG_AUTO_TEMP(nr, i));
1068 for (i = 0; i < 3 ; i++)
1069 data->auto_pwm[nr][i] = data->read(data,
1070 IT87_REG_AUTO_PWM(nr, i));
1071 } else if (has_newer_autopwm(data)) {
1075 * 0: temperature hysteresis (base + 5)
1076 * 1: fan off temperature (base + 0)
1077 * 2: fan start temperature (base + 1)
1078 * 3: fan max temperature (base + 2)
1080 data->auto_temp[nr][0] =
1081 data->read(data, IT87_REG_AUTO_TEMP(nr, 5));
1083 for (i = 0; i < 3 ; i++)
1084 data->auto_temp[nr][i + 1] =
1085 data->read(data, IT87_REG_AUTO_TEMP(nr, i));
1087 * 0: start pwm value (base + 3)
1088 * 1: pwm slope (base + 4, 1/8th pwm)
1090 data->auto_pwm[nr][0] =
1091 data->read(data, IT87_REG_AUTO_TEMP(nr, 3));
1092 data->auto_pwm[nr][1] =
1093 data->read(data, IT87_REG_AUTO_TEMP(nr, 4));
1097 static int it87_lock(struct it87_data *data)
1101 mutex_lock(&data->update_lock);
1102 err = smbus_disable(data);
1104 mutex_unlock(&data->update_lock);
1108 static void it87_unlock(struct it87_data *data)
1111 mutex_unlock(&data->update_lock);
1114 static struct it87_data *it87_update_device(struct device *dev)
1116 struct it87_data *data = dev_get_drvdata(dev);
1120 err = it87_lock(data);
1122 return ERR_PTR(err);
1124 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
1128 * Cleared after each update, so reenable. Value
1129 * returned by this read will be previous value
1131 data->write(data, IT87_REG_CONFIG,
1132 data->read(data, IT87_REG_CONFIG) | 0x40);
1134 for (i = 0; i < NUM_VIN; i++) {
1135 if (!(data->has_in & BIT(i)))
1138 data->in[i][0] = data->read(data, IT87_REG_VIN[i]);
1140 /* VBAT and AVCC don't have limit registers */
1141 if (i >= NUM_VIN_LIMIT)
1144 data->in[i][1] = data->read(data, IT87_REG_VIN_MIN(i));
1145 data->in[i][2] = data->read(data, IT87_REG_VIN_MAX(i));
1148 for (i = 0; i < NUM_FAN; i++) {
1149 /* Skip disabled fans */
1150 if (!(data->has_fan & BIT(i)))
1153 data->fan[i][1] = data->read(data, data->REG_FAN_MIN[i]);
1154 data->fan[i][0] = data->read(data, data->REG_FAN[i]);
1155 /* Add high byte if in 16-bit mode */
1156 if (has_16bit_fans(data)) {
1157 data->fan[i][0] |= data->read(data,
1158 data->REG_FANX[i]) << 8;
1159 data->fan[i][1] |= data->read(data,
1160 data->REG_FANX_MIN[i]) << 8;
1163 for (i = 0; i < NUM_TEMP; i++) {
1164 if (!(data->has_temp & BIT(i)))
1167 data->read(data, IT87_REG_TEMP(i));
1169 if (i >= data->num_temp_limit)
1172 if (i < data->num_temp_offset)
1174 data->read(data, data->REG_TEMP_OFFSET[i]);
1177 data->read(data, data->REG_TEMP_LOW[i]);
1179 data->read(data, data->REG_TEMP_HIGH[i]);
1182 /* Newer chips don't have clock dividers */
1183 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1184 i = data->read(data, IT87_REG_FAN_DIV);
1185 data->fan_div[0] = i & 0x07;
1186 data->fan_div[1] = (i >> 3) & 0x07;
1187 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1191 data->read(data, IT87_REG_ALARM1) |
1192 (data->read(data, IT87_REG_ALARM2) << 8) |
1193 (data->read(data, IT87_REG_ALARM3) << 16);
1194 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
1196 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
1197 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL);
1198 for (i = 0; i < NUM_PWM; i++) {
1199 if (!(data->has_pwm & BIT(i)))
1201 it87_update_pwm_ctrl(data, i);
1204 data->sensor = data->read(data, IT87_REG_TEMP_ENABLE);
1205 data->extra = data->read(data, IT87_REG_TEMP_EXTRA);
1207 * The IT8705F does not have VID capability.
1208 * The IT8718F and later don't use IT87_REG_VID for the
1211 if (data->type == it8712 || data->type == it8716) {
1212 data->vid = data->read(data, IT87_REG_VID);
1214 * The older IT8712F revisions had only 5 VID pins,
1215 * but we assume it is always safe to read 6 bits.
1219 data->last_updated = jiffies;
1226 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
1229 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1230 struct it87_data *data = it87_update_device(dev);
1231 int index = sattr->index;
1235 return PTR_ERR(data);
1237 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
1240 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
1241 const char *buf, size_t count)
1243 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1244 struct it87_data *data = dev_get_drvdata(dev);
1245 int index = sattr->index;
1250 if (kstrtoul(buf, 10, &val) < 0)
1253 err = it87_lock(data);
1257 data->in[nr][index] = in_to_reg(data, nr, val);
1258 data->write(data, index == 1 ? IT87_REG_VIN_MIN(nr)
1259 : IT87_REG_VIN_MAX(nr),
1260 data->in[nr][index]);
1265 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
1266 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
1268 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
1271 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
1272 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
1274 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
1277 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
1278 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
1280 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
1283 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
1284 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
1286 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
1289 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
1290 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
1292 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
1295 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
1296 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
1298 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
1301 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
1302 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
1304 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
1307 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
1308 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
1310 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
1313 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
1314 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
1315 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
1316 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
1317 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
1319 /* Up to 6 temperatures */
1320 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
1323 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1325 int index = sattr->index;
1326 struct it87_data *data = it87_update_device(dev);
1329 return PTR_ERR(data);
1331 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
1334 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
1335 const char *buf, size_t count)
1337 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1339 int index = sattr->index;
1340 struct it87_data *data = dev_get_drvdata(dev);
1345 if (kstrtol(buf, 10, &val) < 0)
1348 err = it87_lock(data);
1355 reg = data->REG_TEMP_LOW[nr];
1358 reg = data->REG_TEMP_HIGH[nr];
1361 regval = data->read(data, IT87_REG_BEEP_ENABLE);
1362 if (!(regval & 0x80)) {
1364 data->write(data, IT87_REG_BEEP_ENABLE, regval);
1367 reg = data->REG_TEMP_OFFSET[nr];
1371 data->temp[nr][index] = TEMP_TO_REG(val);
1372 data->write(data, reg, data->temp[nr][index]);
1377 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
1378 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1380 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1382 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
1384 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
1385 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1387 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1389 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1391 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
1392 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1394 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1396 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1398 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
1399 static SENSOR_DEVICE_ATTR_2(temp4_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1401 static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1403 static SENSOR_DEVICE_ATTR_2(temp4_offset, S_IRUGO | S_IWUSR, show_temp,
1405 static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
1406 static SENSOR_DEVICE_ATTR_2(temp5_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1408 static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1410 static SENSOR_DEVICE_ATTR_2(temp5_offset, S_IRUGO | S_IWUSR, show_temp,
1412 static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
1413 static SENSOR_DEVICE_ATTR_2(temp6_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1415 static SENSOR_DEVICE_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1417 static SENSOR_DEVICE_ATTR_2(temp6_offset, S_IRUGO | S_IWUSR, show_temp,
1420 static const u8 temp_types_8686[NUM_TEMP][9] = {
1421 { 0, 8, 8, 8, 8, 8, 8, 8, 7 },
1422 { 0, 6, 8, 8, 6, 0, 0, 0, 7 },
1423 { 0, 6, 5, 8, 6, 0, 0, 0, 7 },
1424 { 4, 8, 8, 8, 8, 8, 8, 8, 7 },
1425 { 4, 6, 8, 8, 6, 0, 0, 0, 7 },
1426 { 4, 6, 5, 8, 6, 0, 0, 0, 7 },
1429 static int get_temp_type(struct it87_data *data, int index)
1434 if (has_bank_sel(data)) {
1437 src1 = (data->temp_src[index / 2] >> ((index % 2) * 4)) & 0x0f;
1439 switch (data->type) {
1442 type = temp_types_8686[index][src1];
1453 src2 = data->temp_src[3];
1456 type = (src2 & BIT(index)) ? 6 : 5;
1459 type = (src2 & BIT(index)) ? 4 : 6;
1462 type = (src2 & BIT(index)) ? 5 : 0;
1472 if (type || index >= 3)
1475 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1476 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1478 if ((has_temp_peci(data, index) && (reg >> 6 == index + 1)) ||
1479 (has_temp_old_peci(data, index) && (extra & 0x80)))
1480 type = 6; /* Intel PECI */
1481 if (reg & BIT(index))
1482 type = 3; /* thermal diode */
1483 else if (reg & BIT(index + 3))
1484 type = 4; /* thermistor */
1489 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1492 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1493 struct it87_data *data = it87_update_device(dev);
1497 return PTR_ERR(data);
1499 type = get_temp_type(data, sensor_attr->index);
1500 return sprintf(buf, "%d\n", type);
1503 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1504 const char *buf, size_t count)
1506 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1507 int nr = sensor_attr->index;
1509 struct it87_data *data = dev_get_drvdata(dev);
1514 if (kstrtol(buf, 10, &val) < 0)
1517 err = it87_lock(data);
1521 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1524 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1526 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1527 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1529 if (val == 2) { /* backwards compatibility */
1531 "Sensor type 2 is deprecated, please use 4 instead\n");
1534 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1539 else if (has_temp_peci(data, nr) && val == 6)
1540 reg |= (nr + 1) << 6;
1541 else if (has_temp_old_peci(data, nr) && val == 6)
1543 else if (val != 0) {
1549 data->extra = extra;
1550 data->write(data, IT87_REG_TEMP_ENABLE, data->sensor);
1551 if (has_temp_old_peci(data, nr))
1552 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1553 data->valid = 0; /* Force cache refresh */
1559 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1561 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1563 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1565 static SENSOR_DEVICE_ATTR(temp4_type, S_IRUGO | S_IWUSR, show_temp_type,
1567 static SENSOR_DEVICE_ATTR(temp5_type, S_IRUGO | S_IWUSR, show_temp_type,
1569 static SENSOR_DEVICE_ATTR(temp6_type, S_IRUGO | S_IWUSR, show_temp_type,
1574 static int pwm_mode(const struct it87_data *data, int nr)
1576 if (has_fanctl_onoff(data) && nr < 3 &&
1577 !(data->fan_main_ctrl & BIT(nr)))
1578 return 0; /* Full speed */
1579 if (data->pwm_ctrl[nr] & 0x80)
1580 return 2; /* Automatic mode */
1581 if ((!has_fanctl_onoff(data) || nr >= 3) &&
1582 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1583 return 0; /* Full speed */
1585 return 1; /* Manual mode */
1588 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1591 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1593 int index = sattr->index;
1595 struct it87_data *data = it87_update_device(dev);
1598 return PTR_ERR(data);
1600 speed = has_16bit_fans(data) ?
1601 FAN16_FROM_REG(data->fan[nr][index]) :
1602 FAN_FROM_REG(data->fan[nr][index],
1603 DIV_FROM_REG(data->fan_div[nr]));
1604 return sprintf(buf, "%d\n", speed);
1607 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1610 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1611 struct it87_data *data = it87_update_device(dev);
1612 int nr = sensor_attr->index;
1615 return PTR_ERR(data);
1617 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1620 static ssize_t show_pwm_enable(struct device *dev,
1621 struct device_attribute *attr, char *buf)
1623 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1624 struct it87_data *data = it87_update_device(dev);
1625 int nr = sensor_attr->index;
1628 return PTR_ERR(data);
1630 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1633 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1636 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1637 struct it87_data *data = it87_update_device(dev);
1638 int nr = sensor_attr->index;
1641 return PTR_ERR(data);
1643 return sprintf(buf, "%d\n",
1644 pwm_from_reg(data, data->pwm_duty[nr]));
1647 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1650 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1651 struct it87_data *data = it87_update_device(dev);
1652 int nr = sensor_attr->index;
1657 return PTR_ERR(data);
1659 if (has_pwm_freq2(data) && nr == 1)
1660 index = (data->extra >> 4) & 0x07;
1662 index = (data->fan_ctl >> 4) & 0x07;
1664 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1666 return sprintf(buf, "%u\n", freq);
1669 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1670 const char *buf, size_t count)
1672 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1674 int index = sattr->index;
1676 struct it87_data *data = dev_get_drvdata(dev);
1681 if (kstrtol(buf, 10, &val) < 0)
1684 err = it87_lock(data);
1688 if (has_16bit_fans(data)) {
1689 data->fan[nr][index] = FAN16_TO_REG(val);
1690 data->write(data, data->REG_FAN_MIN[nr],
1691 data->fan[nr][index] & 0xff);
1692 data->write(data, data->REG_FANX_MIN[nr],
1693 data->fan[nr][index] >> 8);
1695 reg = data->read(data, IT87_REG_FAN_DIV);
1698 data->fan_div[nr] = reg & 0x07;
1701 data->fan_div[nr] = (reg >> 3) & 0x07;
1704 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1707 data->fan[nr][index] =
1708 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1709 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][index]);
1715 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1716 const char *buf, size_t count)
1718 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1719 struct it87_data *data = dev_get_drvdata(dev);
1720 int nr = sensor_attr->index;
1725 if (kstrtoul(buf, 10, &val) < 0)
1728 err = it87_lock(data);
1732 old = data->read(data, IT87_REG_FAN_DIV);
1734 /* Save fan min limit */
1735 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1740 data->fan_div[nr] = DIV_TO_REG(val);
1744 data->fan_div[nr] = 1;
1746 data->fan_div[nr] = 3;
1749 val |= (data->fan_div[0] & 0x07);
1750 val |= (data->fan_div[1] & 0x07) << 3;
1751 if (data->fan_div[2] == 3)
1753 data->write(data, IT87_REG_FAN_DIV, val);
1755 /* Restore fan min limit */
1756 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1757 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][1]);
1762 /* Returns 0 if OK, -EINVAL otherwise */
1763 static int check_trip_points(struct device *dev, int nr)
1765 const struct it87_data *data = dev_get_drvdata(dev);
1768 if (has_old_autopwm(data)) {
1769 for (i = 0; i < 3; i++) {
1770 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1773 for (i = 0; i < 2; i++) {
1774 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1777 } else if (has_newer_autopwm(data)) {
1778 for (i = 1; i < 3; i++) {
1779 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1786 "Inconsistent trip points, not switching to automatic mode\n");
1787 dev_err(dev, "Adjust the trip points and try again\n");
1792 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1793 const char *buf, size_t count)
1795 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1796 struct it87_data *data = dev_get_drvdata(dev);
1797 int nr = sensor_attr->index;
1801 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1804 /* Check trip points before switching to automatic mode */
1806 if (check_trip_points(dev, nr) < 0)
1810 err = it87_lock(data);
1814 it87_update_pwm_ctrl(data, nr);
1817 if (nr < 3 && has_fanctl_onoff(data)) {
1819 /* make sure the fan is on when in on/off mode */
1820 tmp = data->read(data, IT87_REG_FAN_CTL);
1821 data->write(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1822 /* set on/off mode */
1823 data->fan_main_ctrl &= ~BIT(nr);
1824 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1825 data->fan_main_ctrl);
1829 /* No on/off mode, set maximum pwm value */
1830 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1831 data->write(data, IT87_REG_PWM_DUTY[nr],
1832 data->pwm_duty[nr]);
1833 /* and set manual mode */
1834 if (has_newer_autopwm(data)) {
1835 ctrl = temp_map_to_reg(data, nr,
1836 data->pwm_temp_map[nr]);
1839 ctrl = data->pwm_duty[nr];
1841 data->pwm_ctrl[nr] = ctrl;
1842 data->write(data, data->REG_PWM[nr], ctrl);
1847 if (has_newer_autopwm(data)) {
1848 ctrl = temp_map_to_reg(data, nr,
1849 data->pwm_temp_map[nr]);
1855 ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1857 data->pwm_ctrl[nr] = ctrl;
1858 data->write(data, data->REG_PWM[nr], ctrl);
1860 if (has_fanctl_onoff(data) && nr < 3) {
1861 /* set SmartGuardian mode */
1862 data->fan_main_ctrl |= BIT(nr);
1863 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1864 data->fan_main_ctrl);
1871 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1872 const char *buf, size_t count)
1874 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1875 struct it87_data *data = dev_get_drvdata(dev);
1876 int nr = sensor_attr->index;
1880 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1883 err = it87_lock(data);
1887 it87_update_pwm_ctrl(data, nr);
1888 if (has_newer_autopwm(data)) {
1890 * If we are in automatic mode, the PWM duty cycle register
1891 * is read-only so we can't write the value.
1893 if (data->pwm_ctrl[nr] & 0x80) {
1897 data->pwm_duty[nr] = pwm_to_reg(data, val);
1898 data->write(data, IT87_REG_PWM_DUTY[nr],
1899 data->pwm_duty[nr]);
1901 data->pwm_duty[nr] = pwm_to_reg(data, val);
1903 * If we are in manual mode, write the duty cycle immediately;
1904 * otherwise, just store it for later use.
1906 if (!(data->pwm_ctrl[nr] & 0x80)) {
1907 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1908 data->write(data, data->REG_PWM[nr],
1909 data->pwm_ctrl[nr]);
1917 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1918 const char *buf, size_t count)
1920 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1921 struct it87_data *data = dev_get_drvdata(dev);
1922 int nr = sensor_attr->index;
1927 if (kstrtoul(buf, 10, &val) < 0)
1930 val = clamp_val(val, 0, 1000000);
1931 val *= has_newer_autopwm(data) ? 256 : 128;
1933 /* Search for the nearest available frequency */
1934 for (i = 0; i < 7; i++) {
1935 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1939 err = it87_lock(data);
1944 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL) & 0x8f;
1945 data->fan_ctl |= i << 4;
1946 data->write(data, IT87_REG_FAN_CTL, data->fan_ctl);
1948 data->extra = data->read(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1949 data->extra |= i << 4;
1950 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1956 static ssize_t show_pwm_temp_map(struct device *dev,
1957 struct device_attribute *attr, char *buf)
1959 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1960 struct it87_data *data = it87_update_device(dev);
1961 int nr = sensor_attr->index;
1964 return PTR_ERR(data);
1966 return sprintf(buf, "%d\n", data->pwm_temp_map[nr] + 1);
1969 static ssize_t set_pwm_temp_map(struct device *dev,
1970 struct device_attribute *attr, const char *buf,
1973 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1974 struct it87_data *data = dev_get_drvdata(dev);
1975 int nr = sensor_attr->index;
1980 if (kstrtoul(buf, 10, &val) < 0)
1983 if (!val || val > data->pwm_num_temp_map)
1988 err = it87_lock(data);
1992 it87_update_pwm_ctrl(data, nr);
1993 data->pwm_temp_map[nr] = map;
1995 * If we are in automatic mode, write the temp mapping immediately;
1996 * otherwise, just store it for later use.
1998 if (data->pwm_ctrl[nr] & 0x80) {
1999 data->pwm_ctrl[nr] = temp_map_to_reg(data, nr, map);
2000 data->write(data, data->REG_PWM[nr], data->pwm_ctrl[nr]);
2006 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
2009 struct it87_data *data = it87_update_device(dev);
2010 struct sensor_device_attribute_2 *sensor_attr =
2011 to_sensor_dev_attr_2(attr);
2012 int nr = sensor_attr->nr;
2013 int point = sensor_attr->index;
2016 return PTR_ERR(data);
2018 return sprintf(buf, "%d\n",
2019 pwm_from_reg(data, data->auto_pwm[nr][point]));
2022 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
2023 const char *buf, size_t count)
2025 struct it87_data *data = dev_get_drvdata(dev);
2026 struct sensor_device_attribute_2 *sensor_attr =
2027 to_sensor_dev_attr_2(attr);
2028 int nr = sensor_attr->nr;
2029 int point = sensor_attr->index;
2034 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
2037 err = it87_lock(data);
2041 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
2042 if (has_newer_autopwm(data))
2043 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
2045 regaddr = IT87_REG_AUTO_PWM(nr, point);
2046 data->write(data, regaddr, data->auto_pwm[nr][point]);
2051 static ssize_t show_auto_pwm_slope(struct device *dev,
2052 struct device_attribute *attr, char *buf)
2054 struct it87_data *data = it87_update_device(dev);
2055 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2056 int nr = sensor_attr->index;
2059 return PTR_ERR(data);
2061 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
2064 static ssize_t set_auto_pwm_slope(struct device *dev,
2065 struct device_attribute *attr,
2066 const char *buf, size_t count)
2068 struct it87_data *data = dev_get_drvdata(dev);
2069 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2070 int nr = sensor_attr->index;
2074 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
2077 err = it87_lock(data);
2081 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
2082 data->write(data, IT87_REG_AUTO_TEMP(nr, 4), data->auto_pwm[nr][1]);
2087 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2090 struct it87_data *data = it87_update_device(dev);
2091 struct sensor_device_attribute_2 *sensor_attr =
2092 to_sensor_dev_attr_2(attr);
2093 int nr = sensor_attr->nr;
2094 int point = sensor_attr->index;
2098 return PTR_ERR(data);
2100 if (has_old_autopwm(data) || point)
2101 reg = data->auto_temp[nr][point];
2103 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
2105 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
2108 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
2109 const char *buf, size_t count)
2111 struct it87_data *data = dev_get_drvdata(dev);
2112 struct sensor_device_attribute_2 *sensor_attr =
2113 to_sensor_dev_attr_2(attr);
2114 int nr = sensor_attr->nr;
2115 int point = sensor_attr->index;
2120 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
2123 err = it87_lock(data);
2127 if (has_newer_autopwm(data) && !point) {
2128 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
2129 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
2130 data->auto_temp[nr][0] = reg;
2131 data->write(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
2133 reg = TEMP_TO_REG(val);
2134 data->auto_temp[nr][point] = reg;
2135 if (has_newer_autopwm(data))
2137 data->write(data, IT87_REG_AUTO_TEMP(nr, point), reg);
2143 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
2144 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
2146 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
2149 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
2150 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
2152 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
2155 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
2156 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
2158 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
2161 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
2162 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
2165 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
2166 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
2169 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
2170 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
2173 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
2174 show_pwm_enable, set_pwm_enable, 0);
2175 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
2176 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
2178 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
2179 show_pwm_temp_map, set_pwm_temp_map, 0);
2180 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
2181 show_auto_pwm, set_auto_pwm, 0, 0);
2182 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
2183 show_auto_pwm, set_auto_pwm, 0, 1);
2184 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
2185 show_auto_pwm, set_auto_pwm, 0, 2);
2186 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
2187 show_auto_pwm, NULL, 0, 3);
2188 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
2189 show_auto_temp, set_auto_temp, 0, 1);
2190 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
2191 show_auto_temp, set_auto_temp, 0, 0);
2192 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
2193 show_auto_temp, set_auto_temp, 0, 2);
2194 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
2195 show_auto_temp, set_auto_temp, 0, 3);
2196 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
2197 show_auto_temp, set_auto_temp, 0, 4);
2198 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
2199 show_auto_pwm, set_auto_pwm, 0, 0);
2200 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
2201 show_auto_pwm_slope, set_auto_pwm_slope, 0);
2203 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
2204 show_pwm_enable, set_pwm_enable, 1);
2205 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
2206 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
2207 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
2208 show_pwm_temp_map, set_pwm_temp_map, 1);
2209 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
2210 show_auto_pwm, set_auto_pwm, 1, 0);
2211 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
2212 show_auto_pwm, set_auto_pwm, 1, 1);
2213 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
2214 show_auto_pwm, set_auto_pwm, 1, 2);
2215 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
2216 show_auto_pwm, NULL, 1, 3);
2217 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
2218 show_auto_temp, set_auto_temp, 1, 1);
2219 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
2220 show_auto_temp, set_auto_temp, 1, 0);
2221 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
2222 show_auto_temp, set_auto_temp, 1, 2);
2223 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
2224 show_auto_temp, set_auto_temp, 1, 3);
2225 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
2226 show_auto_temp, set_auto_temp, 1, 4);
2227 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
2228 show_auto_pwm, set_auto_pwm, 1, 0);
2229 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
2230 show_auto_pwm_slope, set_auto_pwm_slope, 1);
2232 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
2233 show_pwm_enable, set_pwm_enable, 2);
2234 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
2235 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
2236 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
2237 show_pwm_temp_map, set_pwm_temp_map, 2);
2238 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
2239 show_auto_pwm, set_auto_pwm, 2, 0);
2240 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
2241 show_auto_pwm, set_auto_pwm, 2, 1);
2242 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
2243 show_auto_pwm, set_auto_pwm, 2, 2);
2244 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
2245 show_auto_pwm, NULL, 2, 3);
2246 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
2247 show_auto_temp, set_auto_temp, 2, 1);
2248 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
2249 show_auto_temp, set_auto_temp, 2, 0);
2250 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
2251 show_auto_temp, set_auto_temp, 2, 2);
2252 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
2253 show_auto_temp, set_auto_temp, 2, 3);
2254 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
2255 show_auto_temp, set_auto_temp, 2, 4);
2256 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
2257 show_auto_pwm, set_auto_pwm, 2, 0);
2258 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
2259 show_auto_pwm_slope, set_auto_pwm_slope, 2);
2261 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
2262 show_pwm_enable, set_pwm_enable, 3);
2263 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
2264 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
2265 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
2266 show_pwm_temp_map, set_pwm_temp_map, 3);
2267 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
2268 show_auto_temp, set_auto_temp, 2, 1);
2269 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
2270 show_auto_temp, set_auto_temp, 2, 0);
2271 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
2272 show_auto_temp, set_auto_temp, 2, 2);
2273 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
2274 show_auto_temp, set_auto_temp, 2, 3);
2275 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
2276 show_auto_pwm, set_auto_pwm, 3, 0);
2277 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
2278 show_auto_pwm_slope, set_auto_pwm_slope, 3);
2280 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
2281 show_pwm_enable, set_pwm_enable, 4);
2282 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
2283 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
2284 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
2285 show_pwm_temp_map, set_pwm_temp_map, 4);
2286 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
2287 show_auto_temp, set_auto_temp, 2, 1);
2288 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
2289 show_auto_temp, set_auto_temp, 2, 0);
2290 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
2291 show_auto_temp, set_auto_temp, 2, 2);
2292 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
2293 show_auto_temp, set_auto_temp, 2, 3);
2294 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
2295 show_auto_pwm, set_auto_pwm, 4, 0);
2296 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
2297 show_auto_pwm_slope, set_auto_pwm_slope, 4);
2299 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
2300 show_pwm_enable, set_pwm_enable, 5);
2301 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
2302 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
2303 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
2304 show_pwm_temp_map, set_pwm_temp_map, 5);
2305 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
2306 show_auto_temp, set_auto_temp, 2, 1);
2307 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
2308 show_auto_temp, set_auto_temp, 2, 0);
2309 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
2310 show_auto_temp, set_auto_temp, 2, 2);
2311 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
2312 show_auto_temp, set_auto_temp, 2, 3);
2313 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
2314 show_auto_pwm, set_auto_pwm, 5, 0);
2315 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
2316 show_auto_pwm_slope, set_auto_pwm_slope, 5);
2319 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
2322 struct it87_data *data = it87_update_device(dev);
2325 return PTR_ERR(data);
2327 return sprintf(buf, "%u\n", data->alarms);
2329 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
2331 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
2334 struct it87_data *data = it87_update_device(dev);
2335 int bitnr = to_sensor_dev_attr(attr)->index;
2338 return PTR_ERR(data);
2340 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
2343 static ssize_t clear_intrusion(struct device *dev,
2344 struct device_attribute *attr, const char *buf,
2347 struct it87_data *data = dev_get_drvdata(dev);
2351 if (kstrtol(buf, 10, &val) < 0 || val != 0)
2354 err = it87_lock(data);
2358 config = data->read(data, IT87_REG_CONFIG);
2360 data->write(data, IT87_REG_CONFIG, config);
2361 /* Invalidate cache to force re-read */
2367 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
2368 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
2369 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
2370 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
2371 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
2372 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
2373 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
2374 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
2375 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
2376 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
2377 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
2378 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
2379 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
2380 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
2381 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
2382 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
2383 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
2384 static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 19);
2385 static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 20);
2386 static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 21);
2387 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
2388 show_alarm, clear_intrusion, 4);
2390 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
2393 struct it87_data *data = it87_update_device(dev);
2394 int bitnr = to_sensor_dev_attr(attr)->index;
2397 return PTR_ERR(data);
2399 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
2402 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
2403 const char *buf, size_t count)
2405 int bitnr = to_sensor_dev_attr(attr)->index;
2406 struct it87_data *data = dev_get_drvdata(dev);
2410 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
2413 err = it87_lock(data);
2417 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
2419 data->beeps |= BIT(bitnr);
2421 data->beeps &= ~BIT(bitnr);
2422 data->write(data, IT87_REG_BEEP_ENABLE, data->beeps);
2427 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
2428 show_beep, set_beep, 1);
2429 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
2430 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
2431 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
2432 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
2433 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
2434 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
2435 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
2436 /* fanX_beep writability is set later */
2437 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
2438 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
2439 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
2440 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
2441 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
2442 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
2443 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
2444 show_beep, set_beep, 2);
2445 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
2446 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
2447 static SENSOR_DEVICE_ATTR(temp4_beep, S_IRUGO, show_beep, NULL, 2);
2448 static SENSOR_DEVICE_ATTR(temp5_beep, S_IRUGO, show_beep, NULL, 2);
2449 static SENSOR_DEVICE_ATTR(temp6_beep, S_IRUGO, show_beep, NULL, 2);
2451 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
2454 struct it87_data *data = dev_get_drvdata(dev);
2456 return sprintf(buf, "%u\n", data->vrm);
2459 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
2460 const char *buf, size_t count)
2462 struct it87_data *data = dev_get_drvdata(dev);
2465 if (kstrtoul(buf, 10, &val) < 0)
2472 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
2474 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
2477 struct it87_data *data = it87_update_device(dev);
2480 return PTR_ERR(data);
2482 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
2484 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
2486 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
2489 static const char * const labels[] = {
2495 static const char * const labels_it8721[] = {
2501 struct it87_data *data = dev_get_drvdata(dev);
2502 int nr = to_sensor_dev_attr(attr)->index;
2505 if (has_vin3_5v(data) && nr == 0)
2507 else if (has_12mv_adc(data) || has_10_9mv_adc(data) ||
2509 label = labels_it8721[nr];
2513 return sprintf(buf, "%s\n", label);
2515 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
2516 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
2517 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
2519 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 3);
2521 static umode_t it87_in_is_visible(struct kobject *kobj,
2522 struct attribute *attr, int index)
2524 struct device *dev = container_of(kobj, struct device, kobj);
2525 struct it87_data *data = dev_get_drvdata(dev);
2526 int i = index / 5; /* voltage index */
2527 int a = index % 5; /* attribute index */
2529 if (index >= 40) { /* in8 and higher only have input attributes */
2534 if (!(data->has_in & BIT(i)))
2537 if (a == 4 && !data->has_beep)
2543 static struct attribute *it87_attributes_in[] = {
2544 &sensor_dev_attr_in0_input.dev_attr.attr,
2545 &sensor_dev_attr_in0_min.dev_attr.attr,
2546 &sensor_dev_attr_in0_max.dev_attr.attr,
2547 &sensor_dev_attr_in0_alarm.dev_attr.attr,
2548 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
2550 &sensor_dev_attr_in1_input.dev_attr.attr,
2551 &sensor_dev_attr_in1_min.dev_attr.attr,
2552 &sensor_dev_attr_in1_max.dev_attr.attr,
2553 &sensor_dev_attr_in1_alarm.dev_attr.attr,
2554 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
2556 &sensor_dev_attr_in2_input.dev_attr.attr,
2557 &sensor_dev_attr_in2_min.dev_attr.attr,
2558 &sensor_dev_attr_in2_max.dev_attr.attr,
2559 &sensor_dev_attr_in2_alarm.dev_attr.attr,
2560 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
2562 &sensor_dev_attr_in3_input.dev_attr.attr,
2563 &sensor_dev_attr_in3_min.dev_attr.attr,
2564 &sensor_dev_attr_in3_max.dev_attr.attr,
2565 &sensor_dev_attr_in3_alarm.dev_attr.attr,
2566 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2568 &sensor_dev_attr_in4_input.dev_attr.attr,
2569 &sensor_dev_attr_in4_min.dev_attr.attr,
2570 &sensor_dev_attr_in4_max.dev_attr.attr,
2571 &sensor_dev_attr_in4_alarm.dev_attr.attr,
2572 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2574 &sensor_dev_attr_in5_input.dev_attr.attr,
2575 &sensor_dev_attr_in5_min.dev_attr.attr,
2576 &sensor_dev_attr_in5_max.dev_attr.attr,
2577 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2578 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2580 &sensor_dev_attr_in6_input.dev_attr.attr,
2581 &sensor_dev_attr_in6_min.dev_attr.attr,
2582 &sensor_dev_attr_in6_max.dev_attr.attr,
2583 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2584 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2586 &sensor_dev_attr_in7_input.dev_attr.attr,
2587 &sensor_dev_attr_in7_min.dev_attr.attr,
2588 &sensor_dev_attr_in7_max.dev_attr.attr,
2589 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2590 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2592 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2593 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
2594 &sensor_dev_attr_in10_input.dev_attr.attr, /* 42 */
2595 &sensor_dev_attr_in11_input.dev_attr.attr, /* 43 */
2596 &sensor_dev_attr_in12_input.dev_attr.attr, /* 44 */
2600 static const struct attribute_group it87_group_in = {
2601 .attrs = it87_attributes_in,
2602 .is_visible = it87_in_is_visible,
2605 static umode_t it87_temp_is_visible(struct kobject *kobj,
2606 struct attribute *attr, int index)
2608 struct device *dev = container_of(kobj, struct device, kobj);
2609 struct it87_data *data = dev_get_drvdata(dev);
2610 int i = index / 7; /* temperature index */
2611 int a = index % 7; /* attribute index */
2613 if (!(data->has_temp & BIT(i)))
2616 if (a && i >= data->num_temp_limit)
2620 int type = get_temp_type(data, i);
2624 if (has_bank_sel(data))
2629 if (a == 5 && i >= data->num_temp_offset)
2632 if (a == 6 && !data->has_beep)
2638 static struct attribute *it87_attributes_temp[] = {
2639 &sensor_dev_attr_temp1_input.dev_attr.attr,
2640 &sensor_dev_attr_temp1_max.dev_attr.attr,
2641 &sensor_dev_attr_temp1_min.dev_attr.attr,
2642 &sensor_dev_attr_temp1_type.dev_attr.attr, /* 3 */
2643 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2644 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2645 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2647 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2648 &sensor_dev_attr_temp2_max.dev_attr.attr,
2649 &sensor_dev_attr_temp2_min.dev_attr.attr,
2650 &sensor_dev_attr_temp2_type.dev_attr.attr,
2651 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2652 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2653 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2655 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2656 &sensor_dev_attr_temp3_max.dev_attr.attr,
2657 &sensor_dev_attr_temp3_min.dev_attr.attr,
2658 &sensor_dev_attr_temp3_type.dev_attr.attr,
2659 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2660 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2661 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2663 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2664 &sensor_dev_attr_temp4_max.dev_attr.attr,
2665 &sensor_dev_attr_temp4_min.dev_attr.attr,
2666 &sensor_dev_attr_temp4_type.dev_attr.attr,
2667 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
2668 &sensor_dev_attr_temp4_offset.dev_attr.attr,
2669 &sensor_dev_attr_temp4_beep.dev_attr.attr,
2671 &sensor_dev_attr_temp5_input.dev_attr.attr,
2672 &sensor_dev_attr_temp5_max.dev_attr.attr,
2673 &sensor_dev_attr_temp5_min.dev_attr.attr,
2674 &sensor_dev_attr_temp5_type.dev_attr.attr,
2675 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
2676 &sensor_dev_attr_temp5_offset.dev_attr.attr,
2677 &sensor_dev_attr_temp5_beep.dev_attr.attr,
2679 &sensor_dev_attr_temp6_input.dev_attr.attr,
2680 &sensor_dev_attr_temp6_max.dev_attr.attr,
2681 &sensor_dev_attr_temp6_min.dev_attr.attr,
2682 &sensor_dev_attr_temp6_type.dev_attr.attr,
2683 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
2684 &sensor_dev_attr_temp6_offset.dev_attr.attr,
2685 &sensor_dev_attr_temp6_beep.dev_attr.attr,
2689 static const struct attribute_group it87_group_temp = {
2690 .attrs = it87_attributes_temp,
2691 .is_visible = it87_temp_is_visible,
2694 static umode_t it87_is_visible(struct kobject *kobj,
2695 struct attribute *attr, int index)
2697 struct device *dev = container_of(kobj, struct device, kobj);
2698 struct it87_data *data = dev_get_drvdata(dev);
2700 if ((index == 2 || index == 3) && !data->has_vid)
2703 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2709 static struct attribute *it87_attributes[] = {
2710 &dev_attr_alarms.attr,
2711 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2712 &dev_attr_vrm.attr, /* 2 */
2713 &dev_attr_cpu0_vid.attr, /* 3 */
2714 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2715 &sensor_dev_attr_in7_label.dev_attr.attr,
2716 &sensor_dev_attr_in8_label.dev_attr.attr,
2717 &sensor_dev_attr_in9_label.dev_attr.attr,
2721 static const struct attribute_group it87_group = {
2722 .attrs = it87_attributes,
2723 .is_visible = it87_is_visible,
2726 static umode_t it87_fan_is_visible(struct kobject *kobj,
2727 struct attribute *attr, int index)
2729 struct device *dev = container_of(kobj, struct device, kobj);
2730 struct it87_data *data = dev_get_drvdata(dev);
2731 int i = index / 5; /* fan index */
2732 int a = index % 5; /* attribute index */
2734 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2735 i = (index - 15) / 4 + 3;
2736 a = (index - 15) % 4;
2739 if (!(data->has_fan & BIT(i)))
2742 if (a == 3) { /* beep */
2743 if (!data->has_beep)
2745 /* first fan beep attribute is writable */
2746 if (i == __ffs(data->has_fan))
2747 return attr->mode | S_IWUSR;
2750 if (a == 4 && has_16bit_fans(data)) /* divisor */
2756 static struct attribute *it87_attributes_fan[] = {
2757 &sensor_dev_attr_fan1_input.dev_attr.attr,
2758 &sensor_dev_attr_fan1_min.dev_attr.attr,
2759 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2760 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2761 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2763 &sensor_dev_attr_fan2_input.dev_attr.attr,
2764 &sensor_dev_attr_fan2_min.dev_attr.attr,
2765 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2766 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2767 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2769 &sensor_dev_attr_fan3_input.dev_attr.attr,
2770 &sensor_dev_attr_fan3_min.dev_attr.attr,
2771 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2772 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2773 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2775 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2776 &sensor_dev_attr_fan4_min.dev_attr.attr,
2777 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2778 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2780 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2781 &sensor_dev_attr_fan5_min.dev_attr.attr,
2782 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2783 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2785 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2786 &sensor_dev_attr_fan6_min.dev_attr.attr,
2787 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2788 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2792 static const struct attribute_group it87_group_fan = {
2793 .attrs = it87_attributes_fan,
2794 .is_visible = it87_fan_is_visible,
2797 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2798 struct attribute *attr, int index)
2800 struct device *dev = container_of(kobj, struct device, kobj);
2801 struct it87_data *data = dev_get_drvdata(dev);
2802 int i = index / 4; /* pwm index */
2803 int a = index % 4; /* attribute index */
2805 if (!(data->has_pwm & BIT(i)))
2808 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2809 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2810 return attr->mode | S_IWUSR;
2812 /* pwm2_freq is writable if there are two pwm frequency selects */
2813 if (has_pwm_freq2(data) && i == 1 && a == 2)
2814 return attr->mode | S_IWUSR;
2819 static struct attribute *it87_attributes_pwm[] = {
2820 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2821 &sensor_dev_attr_pwm1.dev_attr.attr,
2822 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2823 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2825 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2826 &sensor_dev_attr_pwm2.dev_attr.attr,
2827 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2828 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2830 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2831 &sensor_dev_attr_pwm3.dev_attr.attr,
2832 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2833 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2835 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2836 &sensor_dev_attr_pwm4.dev_attr.attr,
2837 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2838 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2840 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2841 &sensor_dev_attr_pwm5.dev_attr.attr,
2842 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2843 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2845 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2846 &sensor_dev_attr_pwm6.dev_attr.attr,
2847 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2848 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2853 static const struct attribute_group it87_group_pwm = {
2854 .attrs = it87_attributes_pwm,
2855 .is_visible = it87_pwm_is_visible,
2858 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2859 struct attribute *attr, int index)
2861 struct device *dev = container_of(kobj, struct device, kobj);
2862 struct it87_data *data = dev_get_drvdata(dev);
2863 int i = index / 11; /* pwm index */
2864 int a = index % 11; /* attribute index */
2866 if (index >= 33) { /* pwm 4..6 */
2867 i = (index - 33) / 6 + 3;
2868 a = (index - 33) % 6 + 4;
2871 if (!(data->has_pwm & BIT(i)))
2874 if (has_newer_autopwm(data)) {
2875 if (a < 4) /* no auto point pwm */
2877 if (a == 8) /* no auto_point4 */
2880 if (has_old_autopwm(data)) {
2881 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2888 static struct attribute *it87_attributes_auto_pwm[] = {
2889 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2890 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2891 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2892 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2893 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2894 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2895 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2896 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2897 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2898 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2899 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2901 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2902 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2903 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2904 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2905 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2906 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2907 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2908 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2909 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2910 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2911 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2913 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2914 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2915 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2916 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2917 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2918 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2919 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2920 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2921 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2922 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2923 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2925 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2926 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2927 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2928 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2929 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2930 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2932 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2933 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2934 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2935 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2936 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2937 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2939 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2940 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2941 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2942 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2943 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2944 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2949 static const struct attribute_group it87_group_auto_pwm = {
2950 .attrs = it87_attributes_auto_pwm,
2951 .is_visible = it87_auto_pwm_is_visible,
2954 /* SuperIO detection - will change isa_address if a chip is found */
2955 static int __init it87_find(int sioaddr, unsigned short *address,
2956 phys_addr_t *mmio_address, struct it87_sio_data *sio_data)
2958 const struct it87_devices *config;
2959 phys_addr_t base = 0;
2965 err = superio_enter(sioaddr);
2969 sio_data->sioaddr = sioaddr;
2972 chip_type = superio_inw(sioaddr, DEVID);
2973 if (chip_type == 0xffff)
2977 chip_type = force_id;
2979 switch (chip_type) {
2981 sio_data->type = it87;
2984 sio_data->type = it8712;
2988 sio_data->type = it8716;
2991 sio_data->type = it8718;
2994 sio_data->type = it8720;
2997 sio_data->type = it8721;
3000 sio_data->type = it8728;
3003 sio_data->type = it8732;
3006 sio_data->type = it8792;
3008 * Disabling configuration mode on IT8792E can result in system
3009 * hang-ups and access failures to the Super-IO chip at the
3010 * second SIO address. Never exit configuration mode on this
3011 * chip to avoid the problem.
3016 sio_data->type = it8771;
3019 sio_data->type = it8772;
3022 sio_data->type = it8781;
3025 sio_data->type = it8782;
3028 sio_data->type = it8783;
3031 sio_data->type = it8786;
3034 sio_data->type = it8790;
3035 doexit = false; /* See IT8792E comment above */
3039 sio_data->type = it8603;
3042 sio_data->type = it8607;
3045 sio_data->type = it8613;
3048 sio_data->type = it8620;
3051 sio_data->type = it8622;
3054 sio_data->type = it8625;
3057 sio_data->type = it8628;
3060 sio_data->type = it8655;
3063 sio_data->type = it8665;
3066 sio_data->type = it8686;
3068 case 0xffff: /* No device at all */
3071 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
3075 superio_select(sioaddr, PME);
3076 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
3077 pr_info("Device not activated, skipping\n");
3081 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
3082 if (*address == 0) {
3083 pr_info("Base address not set, skipping\n");
3087 sio_data->doexit = doexit;
3090 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
3092 config = &it87_devices[sio_data->type];
3094 if (has_mmio(config) && mmio) {
3097 reg = superio_inb(sioaddr, IT87_EC_HWM_MIO_REG);
3099 base = 0xf0000000 + ((reg & 0x0f) << 24);
3100 base += (reg & 0xc0) << 14;
3103 *mmio_address = base;
3107 snprintf(mmio_str, sizeof(mmio_str), " [MMIO at %pa]", &base);
3109 pr_info("Found IT%04x%s chip at 0x%x%s, revision %d\n", chip_type,
3110 it87_devices[sio_data->type].suffix,
3111 *address, mmio_str, sio_data->revision);
3113 /* in7 (VSB or VCCH5V) is always internal on some chips */
3114 if (has_in7_internal(config))
3115 sio_data->internal |= BIT(1);
3117 /* in8 (Vbat) is always internal */
3118 sio_data->internal |= BIT(2);
3120 /* in9 (AVCC3), always internal if supported */
3121 if (has_avcc3(config))
3122 sio_data->internal |= BIT(3); /* in9 is AVCC */
3124 sio_data->skip_in |= BIT(9);
3126 if (!has_four_pwm(config))
3127 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
3128 else if (!has_five_pwm(config))
3129 sio_data->skip_pwm |= BIT(4) | BIT(5);
3130 else if (!has_six_pwm(config))
3131 sio_data->skip_pwm |= BIT(5);
3133 if (!has_vid(config))
3134 sio_data->skip_vid = 1;
3136 /* Read GPIO config and VID value from LDN 7 (GPIO) */
3137 if (sio_data->type == it87) {
3138 /* The IT8705F has a different LD number for GPIO */
3139 superio_select(sioaddr, 5);
3140 sio_data->beep_pin = superio_inb(sioaddr,
3141 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3142 } else if (sio_data->type == it8783) {
3143 int reg25, reg27, reg2a, reg2c, regef;
3145 superio_select(sioaddr, GPIO);
3147 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3148 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3149 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3150 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3151 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
3153 /* Check if fan3 is there or not */
3154 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
3155 sio_data->skip_fan |= BIT(2);
3156 if ((reg25 & BIT(4)) ||
3157 (!(reg2a & BIT(1)) && (regef & BIT(0))))
3158 sio_data->skip_pwm |= BIT(2);
3160 /* Check if fan2 is there or not */
3162 sio_data->skip_fan |= BIT(1);
3164 sio_data->skip_pwm |= BIT(1);
3167 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
3168 sio_data->skip_in |= BIT(5); /* No VIN5 */
3172 sio_data->skip_in |= BIT(6); /* No VIN6 */
3176 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
3178 if (reg27 & BIT(2)) {
3180 * The data sheet is a bit unclear regarding the
3181 * internal voltage divider for VCCH5V. It says
3182 * "This bit enables and switches VIN7 (pin 91) to the
3183 * internal voltage divider for VCCH5V".
3184 * This is different to other chips, where the internal
3185 * voltage divider would connect VIN7 to an internal
3186 * voltage source. Maybe that is the case here as well.
3188 * Since we don't know for sure, re-route it if that is
3189 * not the case, and ask the user to report if the
3190 * resulting voltage is sane.
3192 if (!(reg2c & BIT(1))) {
3194 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
3196 pr_notice("Routing internal VCCH5V to in7.\n");
3198 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
3199 pr_notice("Please report if it displays a reasonable voltage.\n");
3203 sio_data->internal |= BIT(0);
3205 sio_data->internal |= BIT(1);
3207 sio_data->beep_pin = superio_inb(sioaddr,
3208 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3209 } else if (sio_data->type == it8603 || sio_data->type == it8607) {
3212 superio_select(sioaddr, GPIO);
3214 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3216 /* Check if fan3 is there or not */
3218 sio_data->skip_pwm |= BIT(2);
3220 sio_data->skip_fan |= BIT(2);
3222 /* Check if fan2 is there or not */
3223 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3225 sio_data->skip_pwm |= BIT(1);
3227 sio_data->skip_fan |= BIT(1);
3229 switch (sio_data->type) {
3231 sio_data->skip_in |= BIT(5); /* No VIN5 */
3232 sio_data->skip_in |= BIT(6); /* No VIN6 */
3235 sio_data->skip_pwm |= BIT(0);/* No fan1 */
3236 sio_data->skip_fan |= BIT(0);
3241 sio_data->beep_pin = superio_inb(sioaddr,
3242 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3243 } else if (sio_data->type == it8613) {
3244 int reg27, reg29, reg2a;
3246 superio_select(sioaddr, GPIO);
3248 /* Check for pwm3, fan3, pwm5, fan5 */
3249 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3251 sio_data->skip_fan |= BIT(4);
3253 sio_data->skip_pwm |= BIT(4);
3255 sio_data->skip_pwm |= BIT(2);
3257 sio_data->skip_fan |= BIT(2);
3259 /* Check for pwm2, fan2 */
3260 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3262 sio_data->skip_pwm |= BIT(1);
3264 sio_data->skip_fan |= BIT(1);
3266 /* Check for pwm4, fan4 */
3267 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3268 if (!(reg2a & BIT(0)) || (reg29 & BIT(7))) {
3269 sio_data->skip_fan |= BIT(3);
3270 sio_data->skip_pwm |= BIT(3);
3273 sio_data->skip_pwm |= BIT(0); /* No pwm1 */
3274 sio_data->skip_fan |= BIT(0); /* No fan1 */
3275 sio_data->skip_in |= BIT(3); /* No VIN3 */
3276 sio_data->skip_in |= BIT(6); /* No VIN6 */
3278 sio_data->beep_pin = superio_inb(sioaddr,
3279 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3280 } else if (sio_data->type == it8620 || sio_data->type == it8628 ||
3281 sio_data->type == it8686) {
3284 superio_select(sioaddr, GPIO);
3286 /* Check for pwm5 */
3287 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3289 sio_data->skip_pwm |= BIT(4);
3291 /* Check for fan4, fan5 */
3292 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3293 if (!(reg & BIT(5)))
3294 sio_data->skip_fan |= BIT(3);
3295 if (!(reg & BIT(4)))
3296 sio_data->skip_fan |= BIT(4);
3298 /* Check for pwm3, fan3 */
3299 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3301 sio_data->skip_pwm |= BIT(2);
3303 sio_data->skip_fan |= BIT(2);
3305 /* Check for pwm4 */
3306 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
3308 sio_data->skip_pwm |= BIT(3);
3310 /* Check for pwm2, fan2 */
3311 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3313 sio_data->skip_pwm |= BIT(1);
3315 sio_data->skip_fan |= BIT(1);
3316 /* Check for pwm6, fan6 */
3317 if (!(reg & BIT(7))) {
3318 sio_data->skip_pwm |= BIT(5);
3319 sio_data->skip_fan |= BIT(5);
3322 /* Check if AVCC is on VIN3 */
3323 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3325 /* For it8686, the bit just enables AVCC3 */
3326 if (sio_data->type != it8686)
3327 sio_data->internal |= BIT(0);
3329 sio_data->internal &= ~BIT(3);
3330 sio_data->skip_in |= BIT(9);
3333 sio_data->beep_pin = superio_inb(sioaddr,
3334 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3335 } else if (sio_data->type == it8622) {
3338 superio_select(sioaddr, GPIO);
3340 /* Check for pwm4, fan4 */
3341 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3343 sio_data->skip_fan |= BIT(3);
3345 sio_data->skip_pwm |= BIT(3);
3347 /* Check for pwm3, fan3, pwm5, fan5 */
3348 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3350 sio_data->skip_pwm |= BIT(2);
3352 sio_data->skip_fan |= BIT(2);
3354 sio_data->skip_pwm |= BIT(4);
3356 sio_data->skip_fan |= BIT(4);
3358 /* Check for pwm2, fan2 */
3359 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3361 sio_data->skip_pwm |= BIT(1);
3363 sio_data->skip_fan |= BIT(1);
3365 /* Check for AVCC */
3366 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3367 if (!(reg & BIT(0)))
3368 sio_data->skip_in |= BIT(9);
3370 sio_data->beep_pin = superio_inb(sioaddr,
3371 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3372 } else if (sio_data->type == it8732) {
3375 superio_select(sioaddr, GPIO);
3377 /* Check for pwm2, fan2 */
3378 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3380 sio_data->skip_pwm |= BIT(1);
3382 sio_data->skip_fan |= BIT(1);
3384 /* Check for pwm3, fan3, fan4 */
3385 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3387 sio_data->skip_pwm |= BIT(2);
3389 sio_data->skip_fan |= BIT(2);
3391 sio_data->skip_fan |= BIT(3);
3393 /* Check if AVCC is on VIN3 */
3394 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3396 sio_data->internal |= BIT(0);
3398 sio_data->beep_pin = superio_inb(sioaddr,
3399 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3400 } else if (sio_data->type == it8655) {
3403 superio_select(sioaddr, GPIO);
3405 /* Check for pwm2 */
3406 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3408 sio_data->skip_pwm |= BIT(1);
3410 /* Check for fan2 */
3411 reg = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3413 sio_data->skip_fan |= BIT(1);
3415 /* Check for pwm3, fan3 */
3416 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3418 sio_data->skip_pwm |= BIT(2);
3420 sio_data->skip_fan |= BIT(2);
3422 sio_data->beep_pin = superio_inb(sioaddr,
3423 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3424 } else if (sio_data->type == it8665 || sio_data->type == it8625) {
3425 int reg27, reg29, reg2d, regd3;
3427 superio_select(sioaddr, GPIO);
3429 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3430 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3431 reg2d = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3432 regd3 = superio_inb(sioaddr, IT87_SIO_GPIO9_REG);
3434 /* Check for pwm2, fan2 */
3436 sio_data->skip_pwm |= BIT(1);
3438 * Note: Table 6-1 in datasheet claims that FAN_TAC2
3439 * would be enabled with 29h[2]=0.
3442 sio_data->skip_fan |= BIT(1);
3444 /* Check for pwm3, fan3 */
3446 sio_data->skip_pwm |= BIT(2);
3448 sio_data->skip_fan |= BIT(2);
3450 /* Check for pwm4, fan4, pwm5, fan5 */
3451 if (sio_data->type == it8625) {
3452 int reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3455 sio_data->skip_fan |= BIT(3);
3457 sio_data->skip_pwm |= BIT(3);
3459 sio_data->skip_pwm |= BIT(4);
3461 sio_data->skip_fan |= BIT(4);
3463 int reg26 = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3466 sio_data->skip_pwm |= BIT(3);
3468 sio_data->skip_fan |= BIT(3);
3470 sio_data->skip_pwm |= BIT(4);
3472 sio_data->skip_fan |= BIT(4);
3475 /* Check for pwm6, fan6 */
3477 sio_data->skip_pwm |= BIT(5);
3479 sio_data->skip_fan |= BIT(5);
3481 sio_data->beep_pin = superio_inb(sioaddr,
3482 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3487 superio_select(sioaddr, GPIO);
3489 /* Check for fan4, fan5 */
3490 if (has_five_fans(config)) {
3491 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3492 switch (sio_data->type) {
3495 sio_data->skip_fan |= BIT(3);
3497 sio_data->skip_fan |= BIT(4);
3502 if (!(reg & BIT(5)))
3503 sio_data->skip_fan |= BIT(3);
3504 if (!(reg & BIT(4)))
3505 sio_data->skip_fan |= BIT(4);
3512 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3513 if (!sio_data->skip_vid) {
3514 /* We need at least 4 VID pins */
3516 pr_info("VID is disabled (pins used for GPIO)\n");
3517 sio_data->skip_vid = 1;
3521 /* Check if fan3 is there or not */
3523 sio_data->skip_pwm |= BIT(2);
3525 sio_data->skip_fan |= BIT(2);
3527 /* Check if fan2 is there or not */
3528 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3530 sio_data->skip_pwm |= BIT(1);
3532 sio_data->skip_fan |= BIT(1);
3534 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
3535 !(sio_data->skip_vid))
3536 sio_data->vid_value = superio_inb(sioaddr,
3539 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3541 uart6 = sio_data->type == it8782 && (reg & BIT(2));
3544 * The IT8720F has no VIN7 pin, so VCCH should always be
3545 * routed internally to VIN7 with an internal divider.
3546 * Curiously, there still is a configuration bit to control
3547 * this, which means it can be set incorrectly. And even
3548 * more curiously, many boards out there are improperly
3549 * configured, even though the IT8720F datasheet claims
3550 * that the internal routing of VCCH to VIN7 is the default
3551 * setting. So we force the internal routing in this case.
3553 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
3554 * If UART6 is enabled, re-route VIN7 to the internal divider
3555 * if that is not already the case.
3557 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
3559 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
3560 pr_notice("Routing internal VCCH to in7\n");
3563 sio_data->internal |= BIT(0);
3565 sio_data->internal |= BIT(1);
3568 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
3569 * While VIN7 can be routed to the internal voltage divider,
3570 * VIN5 and VIN6 are not available if UART6 is enabled.
3572 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
3573 * is the temperature source. Since we can not read the
3574 * temperature source here, skip_temp is preliminary.
3577 sio_data->skip_in |= BIT(5) | BIT(6);
3578 sio_data->skip_temp |= BIT(2);
3581 sio_data->beep_pin = superio_inb(sioaddr,
3582 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3584 if (sio_data->beep_pin)
3585 pr_info("Beeping is supported\n");
3587 if (config->smbus_bitmap && !base) {
3590 superio_select(sioaddr, PME);
3591 reg = superio_inb(sioaddr, IT87_SPECIAL_CFG_REG);
3592 sio_data->ec_special_config = reg;
3593 sio_data->smbus_bitmap = reg & config->smbus_bitmap;
3597 superio_exit(sioaddr, doexit);
3601 static void it87_init_regs(struct platform_device *pdev)
3603 struct it87_data *data = platform_get_drvdata(pdev);
3605 /* Initialize chip specific register pointers */
3606 switch (data->type) {
3609 data->REG_FAN = IT87_REG_FAN;
3610 data->REG_FANX = IT87_REG_FANX;
3611 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3612 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3613 data->REG_PWM = IT87_REG_PWM;
3614 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET_8686;
3615 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW_8686;
3616 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH_8686;
3621 data->REG_FAN = IT87_REG_FAN_8665;
3622 data->REG_FANX = IT87_REG_FANX_8665;
3623 data->REG_FAN_MIN = IT87_REG_FAN_MIN_8665;
3624 data->REG_FANX_MIN = IT87_REG_FANX_MIN_8665;
3625 data->REG_PWM = IT87_REG_PWM_8665;
3626 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3627 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3628 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3631 data->REG_FAN = IT87_REG_FAN;
3632 data->REG_FANX = IT87_REG_FANX;
3633 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3634 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3635 data->REG_PWM = IT87_REG_PWM_8665;
3636 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3637 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3638 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3641 data->REG_FAN = IT87_REG_FAN;
3642 data->REG_FANX = IT87_REG_FANX;
3643 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3644 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3645 data->REG_PWM = IT87_REG_PWM_8665;
3646 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3647 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3648 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3651 data->REG_FAN = IT87_REG_FAN;
3652 data->REG_FANX = IT87_REG_FANX;
3653 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3654 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3655 data->REG_PWM = IT87_REG_PWM;
3656 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3657 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3658 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3663 data->read = it87_mmio_read;
3664 data->write = it87_mmio_write;
3665 } else if (has_bank_sel(data)) {
3666 data->read = it87_io_read;
3667 data->write = it87_io_write;
3669 data->read = _it87_io_read;
3670 data->write = _it87_io_write;
3674 /* Called when we have found a new IT87. */
3675 static void it87_init_device(struct platform_device *pdev)
3677 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
3678 struct it87_data *data = platform_get_drvdata(pdev);
3682 if (has_new_tempmap(data)) {
3683 data->pwm_temp_map_shift = 3;
3684 data->pwm_temp_map_mask = 0x07;
3686 data->pwm_temp_map_shift = 0;
3687 data->pwm_temp_map_mask = 0x03;
3691 * For each PWM channel:
3692 * - If it is in automatic mode, setting to manual mode should set
3693 * the fan to full speed by default.
3694 * - If it is in manual mode, we need a mapping to temperature
3695 * channels to use when later setting to automatic mode later.
3696 * Map to the first sensor by default (we are clueless.)
3697 * In both cases, the value can (and should) be changed by the user
3698 * prior to switching to a different mode.
3699 * Note that this is no longer needed for the IT8721F and later, as
3700 * these have separate registers for the temperature mapping and the
3701 * manual duty cycle.
3703 for (i = 0; i < NUM_AUTO_PWM; i++) {
3704 data->pwm_temp_map[i] = 0;
3705 data->pwm_duty[i] = 0x7f; /* Full speed */
3706 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
3710 * Some chips seem to have default value 0xff for all limit
3711 * registers. For low voltage limits it makes no sense and triggers
3712 * alarms, so change to 0 instead. For high temperature limits, it
3713 * means -1 degree C, which surprisingly doesn't trigger an alarm,
3714 * but is still confusing, so change to 127 degrees C.
3716 for (i = 0; i < NUM_VIN_LIMIT; i++) {
3717 tmp = data->read(data, IT87_REG_VIN_MIN(i));
3719 data->write(data, IT87_REG_VIN_MIN(i), 0);
3721 for (i = 0; i < data->num_temp_limit; i++) {
3722 tmp = data->read(data, data->REG_TEMP_HIGH[i]);
3724 data->write(data, data->REG_TEMP_HIGH[i], 127);
3728 * Temperature channels are not forcibly enabled, as they can be
3729 * set to two different sensor types and we can't guess which one
3730 * is correct for a given system. These channels can be enabled at
3731 * run-time through the temp{1-3}_type sysfs accessors if needed.
3734 /* Check if voltage monitors are reset manually or by some reason */
3735 tmp = data->read(data, IT87_REG_VIN_ENABLE);
3736 if ((tmp & 0xff) == 0) {
3737 /* Enable all voltage monitors */
3738 data->write(data, IT87_REG_VIN_ENABLE, 0xff);
3741 /* Check if tachometers are reset manually or by some reason */
3742 mask = 0x70 & ~(sio_data->skip_fan << 4);
3743 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
3744 if ((data->fan_main_ctrl & mask) == 0) {
3745 /* Enable all fan tachometers */
3746 data->fan_main_ctrl |= mask;
3747 data->write(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
3749 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
3751 tmp = data->read(data, IT87_REG_FAN_16BIT);
3753 /* Set tachometers to 16-bit mode if needed */
3754 if (has_fan16_config(data)) {
3755 if (~tmp & 0x07 & data->has_fan) {
3757 "Setting fan1-3 to 16-bit mode\n");
3758 data->write(data, IT87_REG_FAN_16BIT, tmp | 0x07);
3762 /* Check for additional fans */
3763 if (has_four_fans(data) && (tmp & BIT(4)))
3764 data->has_fan |= BIT(3); /* fan4 enabled */
3765 if (has_five_fans(data) && (tmp & BIT(5)))
3766 data->has_fan |= BIT(4); /* fan5 enabled */
3767 if (has_six_fans(data)) {
3768 switch (data->type) {
3773 data->has_fan |= BIT(5); /* fan6 enabled */
3777 tmp = data->read(data, IT87_REG_FAN_DIV);
3779 data->has_fan |= BIT(5); /* fan6 enabled */
3786 /* Fan input pins may be used for alternative functions */
3787 data->has_fan &= ~sio_data->skip_fan;
3789 /* Check if pwm6 is enabled */
3790 if (has_six_pwm(data)) {
3791 switch (data->type) {
3794 tmp = data->read(data, IT87_REG_FAN_DIV);
3795 if (!(tmp & BIT(3)))
3796 sio_data->skip_pwm |= BIT(5);
3803 if (has_bank_sel(data)) {
3804 for (i = 0; i < 3; i++)
3806 data->read(data, IT87_REG_TEMP_SRC1[i]);
3807 data->temp_src[3] = data->read(data, IT87_REG_TEMP_SRC2);
3810 /* Start monitoring */
3811 data->write(data, IT87_REG_CONFIG,
3812 (data->read(data, IT87_REG_CONFIG) & 0x3e) |
3813 (update_vbat ? 0x41 : 0x01));
3816 /* Return 1 if and only if the PWM interface is safe to use */
3817 static int it87_check_pwm(struct device *dev)
3819 struct it87_data *data = dev_get_drvdata(dev);
3821 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
3822 * and polarity set to active low is sign that this is the case so we
3823 * disable pwm control to protect the user.
3825 int tmp = data->read(data, IT87_REG_FAN_CTL);
3827 if ((tmp & 0x87) == 0) {
3828 if (fix_pwm_polarity) {
3830 * The user asks us to attempt a chip reconfiguration.
3831 * This means switching to active high polarity and
3832 * inverting all fan speed values.
3837 for (i = 0; i < ARRAY_SIZE(pwm); i++)
3838 pwm[i] = data->read(data,
3842 * If any fan is in automatic pwm mode, the polarity
3843 * might be correct, as suspicious as it seems, so we
3844 * better don't change anything (but still disable the
3847 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3849 "Reconfiguring PWM to active high polarity\n");
3850 data->write(data, IT87_REG_FAN_CTL, tmp | 0x87);
3851 for (i = 0; i < 3; i++)
3852 data->write(data, data->REG_PWM[i],
3858 "PWM configuration is too broken to be fixed\n");
3862 "Detected broken BIOS defaults, disabling PWM interface\n");
3864 } else if (fix_pwm_polarity) {
3866 "PWM configuration looks sane, won't touch\n");
3872 static int it87_probe(struct platform_device *pdev)
3874 struct it87_data *data;
3875 struct resource *res;
3876 struct device *dev = &pdev->dev;
3877 struct it87_sio_data *sio_data = dev_get_platdata(dev);
3878 int enable_pwm_interface;
3879 struct device *hwmon_dev;
3882 data = devm_kzalloc(dev, sizeof(struct it87_data), GFP_KERNEL);
3886 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3889 if (res->flags & IORESOURCE_IO) {
3890 if (!devm_request_region(dev, res->start, IT87_EC_EXTENT,
3892 dev_err(dev, "Failed to request region %pR\n", res);
3896 data->mmio = devm_ioremap_resource(dev, res);
3897 if (IS_ERR(data->mmio))
3898 return PTR_ERR(data->mmio);
3901 data->addr = res->start;
3902 data->type = sio_data->type;
3903 data->sioaddr = sio_data->sioaddr;
3904 data->smbus_bitmap = sio_data->smbus_bitmap;
3905 data->ec_special_config = sio_data->ec_special_config;
3906 data->doexit = sio_data->doexit;
3907 data->features = it87_devices[sio_data->type].features;
3908 data->num_temp_limit = it87_devices[sio_data->type].num_temp_limit;
3909 data->num_temp_offset = it87_devices[sio_data->type].num_temp_offset;
3910 data->pwm_num_temp_map = it87_devices[sio_data->type].num_temp_map;
3911 data->peci_mask = it87_devices[sio_data->type].peci_mask;
3912 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3915 * IT8705F Datasheet 0.4.1, 3h == Version G.
3916 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3917 * These are the first revisions with 16-bit tachometer support.
3919 switch (data->type) {
3921 if (sio_data->revision >= 0x03) {
3922 data->features &= ~FEAT_OLD_AUTOPWM;
3923 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3927 if (sio_data->revision >= 0x08) {
3928 data->features &= ~FEAT_OLD_AUTOPWM;
3929 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3937 platform_set_drvdata(pdev, data);
3939 mutex_init(&data->update_lock);
3941 /* Initialize register pointers */
3942 it87_init_regs(pdev);
3944 err = smbus_disable(data);
3948 /* Now, we do the remaining detection. */
3949 if ((data->read(data, IT87_REG_CONFIG) & 0x80) ||
3950 data->read(data, IT87_REG_CHIPID) != 0x90) {
3955 /* Check PWM configuration */
3956 enable_pwm_interface = it87_check_pwm(dev);
3958 /* Starting with IT8721F, we handle scaling of internal voltages */
3959 if (has_scaling(data)) {
3960 if (sio_data->internal & BIT(0))
3961 data->in_scaled |= BIT(3); /* in3 is AVCC */
3962 if (sio_data->internal & BIT(1))
3963 data->in_scaled |= BIT(7); /* in7 is VSB */
3964 if (sio_data->internal & BIT(2))
3965 data->in_scaled |= BIT(8); /* in8 is Vbat */
3966 if (sio_data->internal & BIT(3))
3967 data->in_scaled |= BIT(9); /* in9 is AVCC */
3968 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3969 sio_data->type == it8783) {
3970 if (sio_data->internal & BIT(0))
3971 data->in_scaled |= BIT(3); /* in3 is VCC5V */
3972 if (sio_data->internal & BIT(1))
3973 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
3976 data->has_temp = 0x07;
3977 if (sio_data->skip_temp & BIT(2)) {
3978 if (sio_data->type == it8782 &&
3979 !(data->read(data, IT87_REG_TEMP_EXTRA) & 0x80))
3980 data->has_temp &= ~BIT(2);
3983 data->in_internal = sio_data->internal;
3984 data->has_in = 0x3ff & ~sio_data->skip_in;
3986 if (has_six_temp(data)) {
3987 u8 reg = data->read(data, IT87_REG_TEMP456_ENABLE);
3989 /* Check for additional temperature sensors */
3990 if ((reg & 0x03) >= 0x02)
3991 data->has_temp |= BIT(3);
3992 if (((reg >> 2) & 0x03) >= 0x02)
3993 data->has_temp |= BIT(4);
3994 if (((reg >> 4) & 0x03) >= 0x02)
3995 data->has_temp |= BIT(5);
3997 /* Check for additional voltage sensors */
3998 if ((reg & 0x03) == 0x01)
3999 data->has_in |= BIT(10);
4000 if (((reg >> 2) & 0x03) == 0x01)
4001 data->has_in |= BIT(11);
4002 if (((reg >> 4) & 0x03) == 0x01)
4003 data->has_in |= BIT(12);
4006 data->has_beep = !!sio_data->beep_pin;
4008 /* Initialize the IT87 chip */
4009 it87_init_device(pdev);
4013 if (!sio_data->skip_vid) {
4014 data->has_vid = true;
4015 data->vrm = vid_which_vrm();
4016 /* VID reading from Super-I/O config space if available */
4017 data->vid = sio_data->vid_value;
4020 /* Prepare for sysfs hooks */
4021 data->groups[0] = &it87_group;
4022 data->groups[1] = &it87_group_in;
4023 data->groups[2] = &it87_group_temp;
4024 data->groups[3] = &it87_group_fan;
4026 if (enable_pwm_interface) {
4027 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
4028 data->has_pwm &= ~sio_data->skip_pwm;
4030 data->groups[4] = &it87_group_pwm;
4031 if (has_old_autopwm(data) || has_newer_autopwm(data))
4032 data->groups[5] = &it87_group_auto_pwm;
4035 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
4036 it87_devices[sio_data->type].name,
4037 data, data->groups);
4038 return PTR_ERR_OR_ZERO(hwmon_dev);
4041 static struct platform_driver it87_driver = {
4045 .probe = it87_probe,
4048 static int __init it87_device_add(int index, unsigned short sio_address,
4049 phys_addr_t mmio_address,
4050 const struct it87_sio_data *sio_data)
4052 struct platform_device *pdev;
4053 struct resource res = {
4059 res.start = mmio_address;
4060 res.end = mmio_address + 0x400 - 1;
4061 res.flags = IORESOURCE_MEM;
4063 res.start = sio_address + IT87_EC_OFFSET;
4064 res.end = sio_address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1;
4065 res.flags = IORESOURCE_IO;
4068 err = acpi_check_resource_conflict(&res);
4070 if (!ignore_resource_conflict)
4074 pdev = platform_device_alloc(DRVNAME, sio_address);
4078 err = platform_device_add_resources(pdev, &res, 1);
4080 pr_err("Device resource addition failed (%d)\n", err);
4081 goto exit_device_put;
4084 err = platform_device_add_data(pdev, sio_data,
4085 sizeof(struct it87_sio_data));
4087 pr_err("Platform data allocation failed\n");
4088 goto exit_device_put;
4091 err = platform_device_add(pdev);
4093 pr_err("Device addition failed (%d)\n", err);
4094 goto exit_device_put;
4097 it87_pdev[index] = pdev;
4101 platform_device_put(pdev);
4105 struct it87_dmi_data {
4106 bool sio2_force_config; /* force sio2 into configuration mode */
4107 u8 skip_pwm; /* pwm channels to skip for this board */
4111 * On various Gigabyte AM4 boards (AB350, AX370), the second Super-IO chip
4112 * (IT8792E) needs to be in configuration mode before accessing the first
4113 * due to a bug in IT8792E which otherwise results in LPC bus access errors.
4114 * This needs to be done before accessing the first Super-IO chip since
4115 * the second chip may have been accessed prior to loading this driver.
4117 * The problem is also reported to affect IT8795E, which is used on X299 boards
4118 * and has the same chip ID as IT8792E (0x8733). It also appears to affect
4119 * systems with IT8790E, which is used on some Z97X-Gaming boards as well as
4121 * DMI entries for those systems will be added as they become available and
4122 * as the problem is confirmed to affect those boards.
4124 static struct it87_dmi_data gigabyte_sio2_force = {
4125 .sio2_force_config = true,
4129 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
4130 * connected to a fan, but to something else. One user
4131 * has reported instant system power-off when changing
4132 * the PWM2 duty cycle, so we disable it.
4133 * I use the board name string as the trigger in case
4134 * the same board is ever used in other systems.
4136 static struct it87_dmi_data nvidia_fn68pt = {
4140 static const struct dmi_system_id it87_dmi_table[] __initconst = {
4143 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4144 DMI_MATCH(DMI_BOARD_NAME, "AB350"),
4146 .driver_data = &gigabyte_sio2_force,
4150 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4151 DMI_MATCH(DMI_BOARD_NAME, "AX370"),
4153 .driver_data = &gigabyte_sio2_force,
4157 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4158 DMI_MATCH(DMI_BOARD_NAME, "Z97X-Gaming G1"),
4160 .driver_data = &gigabyte_sio2_force,
4164 DMI_MATCH(DMI_BOARD_VENDOR, "nVIDIA"),
4165 DMI_MATCH(DMI_BOARD_NAME, "FN68PT"),
4167 .driver_data = &nvidia_fn68pt,
4172 static int __init sm_it87_init(void)
4174 const struct dmi_system_id *dmi = dmi_first_match(it87_dmi_table);
4175 struct it87_dmi_data *dmi_data = NULL;
4176 int sioaddr[2] = { REG_2E, REG_4E };
4177 struct it87_sio_data sio_data;
4178 unsigned short isa_address;
4179 phys_addr_t mmio_address;
4183 pr_info("it87 driver version %s\n", IT87_DRIVER_VERSION);
4186 dmi_data = dmi->driver_data;
4188 err = platform_driver_register(&it87_driver);
4192 if (dmi_data && dmi_data->sio2_force_config)
4193 __superio_enter(REG_4E);
4195 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
4196 memset(&sio_data, 0, sizeof(struct it87_sio_data));
4199 err = it87_find(sioaddr[i], &isa_address, &mmio_address,
4201 if (err || isa_address == 0)
4205 sio_data.skip_pwm |= dmi_data->skip_pwm;
4206 err = it87_device_add(i, isa_address, mmio_address, &sio_data);
4208 goto exit_dev_unregister;
4214 goto exit_unregister;
4218 exit_dev_unregister:
4219 /* NULL check handled by platform_device_unregister */
4220 platform_device_unregister(it87_pdev[0]);
4222 platform_driver_unregister(&it87_driver);
4226 static void __exit sm_it87_exit(void)
4228 /* NULL check handled by platform_device_unregister */
4229 platform_device_unregister(it87_pdev[1]);
4230 platform_device_unregister(it87_pdev[0]);
4231 platform_driver_unregister(&it87_driver);
4234 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
4235 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
4236 module_param(update_vbat, bool, 0);
4237 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
4238 module_param(fix_pwm_polarity, bool, 0);
4239 MODULE_PARM_DESC(fix_pwm_polarity,
4240 "Force PWM polarity to active high (DANGEROUS)");
4241 MODULE_LICENSE("GPL");
4243 module_init(sm_it87_init);
4244 module_exit(sm_it87_exit);