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>
78 #ifndef IT87_DRIVER_VERSION
79 #define IT87_DRIVER_VERSION "<not provided>"
82 #define DRVNAME "it87"
84 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
85 it8771, it8772, it8781, it8782, it8783, it8786, it8790,
86 it8792, it8603, it8607, it8613, it8620, it8622, it8625, it8628,
87 it8655, it8665, it8686 };
89 static unsigned short force_id;
90 module_param(force_id, ushort, 0000);
91 MODULE_PARM_DESC(force_id, "Override the detected device ID");
93 static bool ignore_resource_conflict;
94 module_param(ignore_resource_conflict, bool, 0000);
95 MODULE_PARM_DESC(ignore_resource_conflict, "Ignore ACPI resource conflict");
98 module_param(mmio, bool, 0000);
99 MODULE_PARM_DESC(mmio, "Use MMIO if available");
101 static struct platform_device *it87_pdev[2];
103 #define REG_2E 0x2e /* The register to read/write */
104 #define REG_4E 0x4e /* Secondary register to read/write */
106 #define DEV 0x07 /* Register: Logical device select */
107 #define PME 0x04 /* The device with the fan registers in it */
109 /* The device with the IT8718F/IT8720F VID value in it */
112 #define DEVID 0x20 /* Register: Device ID */
113 #define DEVREV 0x22 /* Register: Device Revision */
115 static inline void __superio_enter(int ioreg)
120 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
123 static inline int superio_inb(int ioreg, int reg)
128 val = inb(ioreg + 1);
133 static inline void superio_outb(int ioreg, int reg, int val)
136 outb(val, ioreg + 1);
139 static int superio_inw(int ioreg, int reg)
141 return (superio_inb(ioreg, reg) << 8) | superio_inb(ioreg, reg + 1);
144 static inline void superio_select(int ioreg, int ldn)
147 outb(ldn, ioreg + 1);
150 static inline int superio_enter(int ioreg)
153 * Try to reserve ioreg and ioreg + 1 for exclusive access.
155 if (!request_muxed_region(ioreg, 2, DRVNAME))
158 __superio_enter(ioreg);
162 static inline void superio_exit(int ioreg, bool doexit)
166 outb(0x02, ioreg + 1);
168 release_region(ioreg, 2);
171 /* Logical device 4 registers */
172 #define IT8712F_DEVID 0x8712
173 #define IT8705F_DEVID 0x8705
174 #define IT8716F_DEVID 0x8716
175 #define IT8718F_DEVID 0x8718
176 #define IT8720F_DEVID 0x8720
177 #define IT8721F_DEVID 0x8721
178 #define IT8726F_DEVID 0x8726
179 #define IT8728F_DEVID 0x8728
180 #define IT8732F_DEVID 0x8732
181 #define IT8792E_DEVID 0x8733
182 #define IT8771E_DEVID 0x8771
183 #define IT8772E_DEVID 0x8772
184 #define IT8781F_DEVID 0x8781
185 #define IT8782F_DEVID 0x8782
186 #define IT8783E_DEVID 0x8783
187 #define IT8786E_DEVID 0x8786
188 #define IT8790E_DEVID 0x8790
189 #define IT8603E_DEVID 0x8603
190 #define IT8607E_DEVID 0x8607
191 #define IT8613E_DEVID 0x8613
192 #define IT8620E_DEVID 0x8620
193 #define IT8622E_DEVID 0x8622
194 #define IT8623E_DEVID 0x8623
195 #define IT8625E_DEVID 0x8625
196 #define IT8628E_DEVID 0x8628
197 #define IT8655E_DEVID 0x8655
198 #define IT8665E_DEVID 0x8665
199 #define IT8686E_DEVID 0x8686
201 /* Logical device 4 (Environmental Monitor) registers */
202 #define IT87_ACT_REG 0x30
203 #define IT87_BASE_REG 0x60
204 #define IT87_SPECIAL_CFG_REG 0xf3 /* special configuration register */
206 /* Global configuration registers (IT8712F and later) */
207 #define IT87_EC_HWM_MIO_REG 0x24 /* MMIO configuration register */
208 #define IT87_SIO_GPIO1_REG 0x25
209 #define IT87_SIO_GPIO2_REG 0x26
210 #define IT87_SIO_GPIO3_REG 0x27
211 #define IT87_SIO_GPIO4_REG 0x28
212 #define IT87_SIO_GPIO5_REG 0x29
213 #define IT87_SIO_GPIO9_REG 0xd3
214 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
215 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
216 #define IT87_SIO_PINX4_REG 0x2d /* Pin selection */
218 /* Logical device 7 (GPIO) registers (IT8712F and later) */
219 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
220 #define IT87_SIO_VID_REG 0xfc /* VID value */
221 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
223 /* Update battery voltage after every reading if true */
224 static bool update_vbat;
226 /* Not all BIOSes properly configure the PWM registers */
227 static bool fix_pwm_polarity;
229 /* Many IT87 constants specified below */
231 /* Length of ISA address segment */
232 #define IT87_EXTENT 8
234 /* Length of ISA address segment for Environmental Controller */
235 #define IT87_EC_EXTENT 2
237 /* Offset of EC registers from ISA base address */
238 #define IT87_EC_OFFSET 5
240 /* Where are the ISA address/data registers relative to the EC base address */
241 #define IT87_ADDR_REG_OFFSET 0
242 #define IT87_DATA_REG_OFFSET 1
244 /*----- The IT87 registers -----*/
246 #define IT87_REG_CONFIG 0x00
248 #define IT87_REG_ALARM1 0x01
249 #define IT87_REG_ALARM2 0x02
250 #define IT87_REG_ALARM3 0x03
252 #define IT87_REG_BANK 0x06
255 * The IT8718F and IT8720F have the VID value in a different register, in
256 * Super-I/O configuration space.
258 #define IT87_REG_VID 0x0a
260 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
261 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
264 #define IT87_REG_FAN_DIV 0x0b
265 #define IT87_REG_FAN_16BIT 0x0c
269 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
270 * - up to 6 temp (1 to 6)
271 * - up to 6 fan (1 to 6)
274 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
275 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
276 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
277 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
279 static const u8 IT87_REG_FAN_8665[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x93 };
280 static const u8 IT87_REG_FAN_MIN_8665[] = {
281 0x10, 0x11, 0x12, 0x84, 0x86, 0xb2 };
282 static const u8 IT87_REG_FANX_8665[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x94 };
283 static const u8 IT87_REG_FANX_MIN_8665[] = {
284 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0xb3 };
286 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59, 0x5a, 0x90, 0x91 };
288 static const u8 IT87_REG_TEMP_OFFSET_8686[] = {
289 0x56, 0x57, 0x59, 0x90, 0x91, 0x92 };
291 #define IT87_REG_FAN_MAIN_CTRL 0x13
292 #define IT87_REG_FAN_CTL 0x14
294 static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
295 static const u8 IT87_REG_PWM_8665[] = { 0x15, 0x16, 0x17, 0x1e, 0x1f, 0x92 };
297 static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
299 static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
300 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
302 #define IT87_REG_TEMP(nr) (0x29 + (nr))
304 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
305 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
307 static const u8 IT87_REG_TEMP_HIGH[] = { 0x40, 0x42, 0x44, 0x46, 0xb4, 0xb6 };
308 static const u8 IT87_REG_TEMP_LOW[] = { 0x41, 0x43, 0x45, 0x47, 0xb5, 0xb7 };
310 static const u8 IT87_REG_TEMP_HIGH_8686[] = {
311 0x40, 0x42, 0x44, 0xb4, 0xb6, 0xb8 };
312 static const u8 IT87_REG_TEMP_LOW_8686[] = {
313 0x41, 0x43, 0x45, 0xb5, 0xb7, 0xb9 };
315 #define IT87_REG_VIN_ENABLE 0x50
316 #define IT87_REG_TEMP_ENABLE 0x51
317 #define IT87_REG_TEMP_EXTRA 0x55
318 #define IT87_REG_BEEP_ENABLE 0x5c
320 #define IT87_REG_CHIPID 0x58
322 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
324 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
325 #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
327 #define IT87_REG_TEMP456_ENABLE 0x77
329 static const u16 IT87_REG_TEMP_SRC1[] = { 0x21d, 0x21e, 0x21f };
330 #define IT87_REG_TEMP_SRC2 0x23d
332 #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
333 #define NUM_VIN_LIMIT 8
335 #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
336 #define NUM_FAN_DIV 3
337 #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
338 #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
340 struct it87_devices {
342 const char * const suffix;
346 u8 num_temp_map; /* Number of temperature sources for pwm */
349 u8 smbus_bitmap; /* SMBus enable bits in extra config register */
350 u8 ec_special_config;
353 #define FEAT_12MV_ADC BIT(0)
354 #define FEAT_NEWER_AUTOPWM BIT(1)
355 #define FEAT_OLD_AUTOPWM BIT(2)
356 #define FEAT_16BIT_FANS BIT(3)
357 #define FEAT_TEMP_PECI BIT(5)
358 #define FEAT_TEMP_OLD_PECI BIT(6)
359 #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
360 #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
361 #define FEAT_VID BIT(9) /* Set if chip supports VID */
362 #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
363 #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
364 #define FEAT_10_9MV_ADC BIT(12)
365 #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
366 #define FEAT_FIVE_PWM BIT(14) /* Chip supports 5 pwm chn */
367 #define FEAT_SIX_PWM BIT(15) /* Chip supports 6 pwm chn */
368 #define FEAT_PWM_FREQ2 BIT(16) /* Separate pwm freq 2 */
369 #define FEAT_SIX_TEMP BIT(17) /* Up to 6 temp sensors */
370 #define FEAT_VIN3_5V BIT(18) /* VIN3 connected to +5V */
371 #define FEAT_FOUR_FANS BIT(19) /* Supports four fans */
372 #define FEAT_FOUR_PWM BIT(20) /* Supports four fan controls */
373 #define FEAT_BANK_SEL BIT(21) /* Chip has multi-bank support */
374 #define FEAT_SCALING BIT(22) /* Internal voltage scaling */
375 #define FEAT_FANCTL_ONOFF BIT(23) /* chip has FAN_CTL ON/OFF */
376 #define FEAT_11MV_ADC BIT(24)
377 #define FEAT_NEW_TEMPMAP BIT(25) /* new temp input selection */
378 #define FEAT_MMIO BIT(26) /* Chip supports MMIO */
379 #define FEAT_FOUR_TEMP BIT(27)
381 static const struct it87_devices it87_devices[] = {
385 .features = FEAT_OLD_AUTOPWM | FEAT_FANCTL_ONOFF,
386 /* may need to overwrite */
388 .num_temp_offset = 0,
394 .features = FEAT_OLD_AUTOPWM | FEAT_VID | FEAT_FANCTL_ONOFF,
395 /* may need to overwrite */
397 .num_temp_offset = 0,
403 .features = FEAT_16BIT_FANS | FEAT_VID
404 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2
407 .num_temp_offset = 3,
413 .features = FEAT_16BIT_FANS | FEAT_VID
414 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
415 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
417 .num_temp_offset = 3,
419 .old_peci_mask = 0x4,
424 .features = FEAT_16BIT_FANS | FEAT_VID
425 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
426 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
428 .num_temp_offset = 3,
430 .old_peci_mask = 0x4,
435 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
436 | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
437 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
438 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
440 .num_temp_offset = 3,
443 .old_peci_mask = 0x02, /* Actually reports PCH */
448 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
449 | FEAT_TEMP_PECI | FEAT_FIVE_FANS
450 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_SCALING
453 .num_temp_offset = 3,
460 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
461 | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
462 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_FOUR_FANS
463 | FEAT_FOUR_PWM | FEAT_FANCTL_ONOFF,
465 .num_temp_offset = 3,
468 .old_peci_mask = 0x02, /* Actually reports PCH */
473 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
474 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
475 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
476 /* PECI: guesswork */
478 /* 16 bit fans (OHM) */
479 /* three fans, always 16 bit (guesswork) */
481 .num_temp_offset = 3,
488 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
489 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
490 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
491 /* PECI (coreboot) */
492 /* 12mV ADC (HWSensors4, OHM) */
493 /* 16 bit fans (HWSensors4, OHM) */
494 /* three fans, always 16 bit (datasheet) */
496 .num_temp_offset = 3,
503 .features = FEAT_16BIT_FANS
504 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
507 .num_temp_offset = 3,
509 .old_peci_mask = 0x4,
514 .features = FEAT_16BIT_FANS
515 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
518 .num_temp_offset = 3,
520 .old_peci_mask = 0x4,
525 .features = FEAT_16BIT_FANS
526 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
529 .num_temp_offset = 3,
531 .old_peci_mask = 0x4,
536 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
537 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
538 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
540 .num_temp_offset = 3,
547 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
548 | FEAT_16BIT_FANS | FEAT_TEMP_PECI
549 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
551 .num_temp_offset = 3,
558 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
559 | FEAT_16BIT_FANS | FEAT_TEMP_PECI
560 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
562 .num_temp_offset = 3,
569 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
570 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
571 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
573 .num_temp_offset = 3,
580 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
581 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL | FEAT_NEW_TEMPMAP
582 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING
585 .num_temp_offset = 3,
592 .features = FEAT_NEWER_AUTOPWM | FEAT_11MV_ADC | FEAT_16BIT_FANS
593 | FEAT_TEMP_PECI | FEAT_FIVE_FANS
594 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
595 | FEAT_AVCC3 | FEAT_SCALING | FEAT_NEW_TEMPMAP,
597 .num_temp_offset = 6,
604 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
605 | FEAT_TEMP_PECI | FEAT_SIX_FANS
606 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
607 | FEAT_SIX_TEMP | FEAT_VIN3_5V | FEAT_SCALING
610 .num_temp_offset = 3,
617 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
618 | FEAT_TEMP_PECI | FEAT_FIVE_FANS | FEAT_FOUR_TEMP
619 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
620 | FEAT_AVCC3 | FEAT_VIN3_5V | FEAT_SCALING,
622 .num_temp_offset = 3,
625 .smbus_bitmap = BIT(1) | BIT(2),
630 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
631 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP
632 | FEAT_11MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
633 | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_SCALING,
635 .num_temp_offset = 6,
637 .smbus_bitmap = BIT(1) | BIT(2),
642 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
643 | FEAT_TEMP_PECI | FEAT_SIX_FANS
644 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
645 | FEAT_SIX_TEMP | FEAT_SCALING | FEAT_AVCC3
648 .num_temp_offset = 3,
655 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
656 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
657 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_BANK_SEL
660 .num_temp_offset = 6,
662 .smbus_bitmap = BIT(2),
667 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
668 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
669 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
670 | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_MMIO,
672 .num_temp_offset = 6,
674 .smbus_bitmap = BIT(2),
679 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
680 | FEAT_SIX_FANS | FEAT_NEW_TEMPMAP
681 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
682 | FEAT_SIX_TEMP | FEAT_BANK_SEL | FEAT_SCALING | FEAT_AVCC3,
684 .num_temp_offset = 6,
686 .smbus_bitmap = BIT(1) | BIT(2),
690 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
691 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
692 #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
693 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
694 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
695 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
696 ((data)->peci_mask & BIT(nr)))
697 #define has_temp_old_peci(data, nr) \
698 (((data)->features & FEAT_TEMP_OLD_PECI) && \
699 ((data)->old_peci_mask & BIT(nr)))
700 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
701 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
703 #define has_vid(data) ((data)->features & FEAT_VID)
704 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
705 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
706 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
707 #define has_five_pwm(data) ((data)->features & (FEAT_FIVE_PWM \
709 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
710 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
711 #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
712 #define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
713 #define has_four_fans(data) ((data)->features & (FEAT_FOUR_FANS | \
716 #define has_four_pwm(data) ((data)->features & (FEAT_FOUR_PWM | \
719 #define has_bank_sel(data) ((data)->features & FEAT_BANK_SEL)
720 #define has_scaling(data) ((data)->features & FEAT_SCALING)
721 #define has_fanctl_onoff(data) ((data)->features & FEAT_FANCTL_ONOFF)
722 #define has_11mv_adc(data) ((data)->features & FEAT_11MV_ADC)
723 #define has_new_tempmap(data) ((data)->features & FEAT_NEW_TEMPMAP)
724 #define has_mmio(data) ((data)->features & FEAT_MMIO)
725 #define has_four_temp(data) ((data)->features & FEAT_FOUR_TEMP)
727 struct it87_sio_data {
731 /* Values read from Super-I/O config space */
735 u8 internal; /* Internal sensors can be labeled */
736 /* Features skipped based on config or DMI */
743 u8 ec_special_config;
747 * For each registered chip, we need to keep some data in memory.
748 * The structure is dynamically allocated.
751 const struct attribute_group *groups[7];
757 u8 smbus_bitmap; /* !=0 if SMBus needs to be disabled */
758 u8 ec_special_config; /* EC special config register restore value */
759 u8 sioaddr; /* SIO port address */
760 bool doexit; /* true if exit from sio config is ok */
762 void __iomem *mmio; /* Remapped MMIO address if available */
763 int (*read)(struct it87_data *, u16);
764 void (*write)(struct it87_data *, u16, u8);
768 const u8 *REG_FAN_MIN;
769 const u8 *REG_FANX_MIN;
773 const u8 *REG_TEMP_OFFSET;
774 const u8 *REG_TEMP_LOW;
775 const u8 *REG_TEMP_HIGH;
779 struct mutex update_lock;
780 char valid; /* !=0 if following fields are valid */
781 unsigned long last_updated; /* In jiffies */
783 u16 in_scaled; /* Internal voltage sensors are scaled */
784 u16 in_internal; /* Bitfield, internal sensors (for labels) */
785 u16 has_in; /* Bitfield, voltage sensors enabled */
786 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
787 u8 has_fan; /* Bitfield, fans enabled */
788 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
789 u8 has_temp; /* Bitfield, temp sensors enabled */
790 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
791 u8 num_temp_limit; /* Number of temperature limit registers */
792 u8 num_temp_offset; /* Number of temperature offset registers */
793 u8 temp_src[4]; /* Up to 4 temperature source registers */
794 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
795 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
796 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
797 bool has_vid; /* True if VID supported */
798 u8 vid; /* Register encoding, combined */
800 u32 alarms; /* Register encoding, combined */
801 bool has_beep; /* true if beep supported */
802 u8 beeps; /* Register encoding */
803 u8 fan_main_ctrl; /* Register value */
804 u8 fan_ctl; /* Register value */
807 * The following 3 arrays correspond to the same registers up to
808 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
809 * 7, and we want to preserve settings on mode changes, so we have
810 * to track all values separately.
811 * Starting with the IT8721F, the manual PWM duty cycles are stored
812 * in separate registers (8-bit values), so the separate tracking
813 * is no longer needed, but it is still done to keep the driver
816 u8 has_pwm; /* Bitfield, pwm control enabled */
817 u8 pwm_ctrl[NUM_PWM]; /* Register value */
818 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
819 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
820 u8 pwm_temp_map_mask; /* 0x03 for old, 0x07 for new temp map */
821 u8 pwm_temp_map_shift; /* 0 for old, 3 for new temp map */
822 u8 pwm_num_temp_map; /* from config data, 3..7 depending on chip */
824 /* Automatic fan speed control registers */
825 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
826 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
829 static int adc_lsb(const struct it87_data *data, int nr)
833 if (has_12mv_adc(data))
835 else if (has_10_9mv_adc(data))
837 else if (has_11mv_adc(data))
841 if (data->in_scaled & BIT(nr))
846 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
848 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
849 return clamp_val(val, 0, 255);
852 static int in_from_reg(const struct it87_data *data, int nr, int val)
854 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
857 static inline u8 FAN_TO_REG(long rpm, int div)
861 rpm = clamp_val(rpm, 1, 1000000);
862 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
865 static inline u16 FAN16_TO_REG(long rpm)
869 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
872 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
873 1350000 / ((val) * (div)))
874 /* The divider is fixed to 2 in 16-bit mode */
875 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
876 1350000 / ((val) * 2))
878 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
879 ((val) + 500) / 1000), -128, 127))
880 #define TEMP_FROM_REG(val) ((val) * 1000)
882 static u8 pwm_to_reg(const struct it87_data *data, long val)
884 if (has_newer_autopwm(data))
890 static int pwm_from_reg(const struct it87_data *data, u8 reg)
892 if (has_newer_autopwm(data))
895 return (reg & 0x7f) << 1;
898 static int DIV_TO_REG(int val)
902 while (answer < 7 && (val >>= 1))
907 #define DIV_FROM_REG(val) BIT(val)
909 static u8 temp_map_from_reg(const struct it87_data *data, u8 reg)
913 map = (reg >> data->pwm_temp_map_shift) & data->pwm_temp_map_mask;
914 if (map >= data->pwm_num_temp_map) /* map is 0-based */
920 static u8 temp_map_to_reg(const struct it87_data *data, int nr, u8 map)
922 u8 ctrl = data->pwm_ctrl[nr];
924 return (ctrl & ~(data->pwm_temp_map_mask << data->pwm_temp_map_shift)) |
925 (map << data->pwm_temp_map_shift);
929 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
930 * depending on the chip type, to calculate the actual PWM frequency.
932 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
933 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
934 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
935 * sometimes just one. It is unknown if this is a datasheet error or real,
936 * so this is ignored for now.
938 static const unsigned int pwm_freq[8] = {
949 static int smbus_disable(struct it87_data *data)
953 if (data->smbus_bitmap) {
954 err = superio_enter(data->sioaddr);
957 superio_select(data->sioaddr, PME);
958 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
959 data->ec_special_config & ~data->smbus_bitmap);
960 superio_exit(data->sioaddr, data->doexit);
965 static int smbus_enable(struct it87_data *data)
969 if (data->smbus_bitmap) {
970 err = superio_enter(data->sioaddr);
974 superio_select(data->sioaddr, PME);
975 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
976 data->ec_special_config);
977 superio_exit(data->sioaddr, data->doexit);
982 static int _it87_io_read(struct it87_data *data, u16 reg)
984 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
985 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
988 static void _it87_io_write(struct it87_data *data, u16 reg, u8 value)
990 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
991 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
994 static u8 it87_io_set_bank(struct it87_data *data, u8 bank)
998 if (has_bank_sel(data)) {
999 u8 breg = _it87_io_read(data, IT87_REG_BANK);
1002 if (bank != _bank) {
1004 breg |= (bank << 5);
1005 _it87_io_write(data, IT87_REG_BANK, breg);
1012 * Must be called with data->update_lock held, except during initialization.
1013 * Must be called with SMBus accesses disabled.
1014 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1015 * would slow down the IT87 access and should not be necessary.
1017 static int it87_io_read(struct it87_data *data, u16 reg)
1022 bank = it87_io_set_bank(data, reg >> 8);
1023 val = _it87_io_read(data, reg & 0xff);
1024 it87_io_set_bank(data, bank);
1030 * Must be called with data->update_lock held, except during initialization.
1031 * Must be called with SMBus accesses disabled
1032 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1033 * would slow down the IT87 access and should not be necessary.
1035 static void it87_io_write(struct it87_data *data, u16 reg, u8 value)
1039 bank = it87_io_set_bank(data, reg >> 8);
1040 _it87_io_write(data, reg & 0xff, value);
1041 it87_io_set_bank(data, bank);
1044 static int it87_mmio_read(struct it87_data *data, u16 reg)
1046 return readb(data->mmio + reg);
1049 static void it87_mmio_write(struct it87_data *data, u16 reg, u8 value)
1051 writeb(value, data->mmio + reg);
1054 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1058 ctrl = data->read(data, data->REG_PWM[nr]);
1059 data->pwm_ctrl[nr] = ctrl;
1060 if (has_newer_autopwm(data)) {
1061 data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1062 data->pwm_duty[nr] = data->read(data, IT87_REG_PWM_DUTY[nr]);
1064 if (ctrl & 0x80) /* Automatic mode */
1065 data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1066 else /* Manual mode */
1067 data->pwm_duty[nr] = ctrl & 0x7f;
1070 if (has_old_autopwm(data)) {
1073 for (i = 0; i < 5 ; i++)
1074 data->auto_temp[nr][i] = data->read(data,
1075 IT87_REG_AUTO_TEMP(nr, i));
1076 for (i = 0; i < 3 ; i++)
1077 data->auto_pwm[nr][i] = data->read(data,
1078 IT87_REG_AUTO_PWM(nr, i));
1079 } else if (has_newer_autopwm(data)) {
1083 * 0: temperature hysteresis (base + 5)
1084 * 1: fan off temperature (base + 0)
1085 * 2: fan start temperature (base + 1)
1086 * 3: fan max temperature (base + 2)
1088 data->auto_temp[nr][0] =
1089 data->read(data, IT87_REG_AUTO_TEMP(nr, 5));
1091 for (i = 0; i < 3 ; i++)
1092 data->auto_temp[nr][i + 1] =
1093 data->read(data, IT87_REG_AUTO_TEMP(nr, i));
1095 * 0: start pwm value (base + 3)
1096 * 1: pwm slope (base + 4, 1/8th pwm)
1098 data->auto_pwm[nr][0] =
1099 data->read(data, IT87_REG_AUTO_TEMP(nr, 3));
1100 data->auto_pwm[nr][1] =
1101 data->read(data, IT87_REG_AUTO_TEMP(nr, 4));
1105 static int it87_lock(struct it87_data *data)
1109 mutex_lock(&data->update_lock);
1110 err = smbus_disable(data);
1112 mutex_unlock(&data->update_lock);
1116 static void it87_unlock(struct it87_data *data)
1119 mutex_unlock(&data->update_lock);
1122 static struct it87_data *it87_update_device(struct device *dev)
1124 struct it87_data *data = dev_get_drvdata(dev);
1128 err = it87_lock(data);
1130 return ERR_PTR(err);
1132 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
1136 * Cleared after each update, so reenable. Value
1137 * returned by this read will be previous value
1139 data->write(data, IT87_REG_CONFIG,
1140 data->read(data, IT87_REG_CONFIG) | 0x40);
1142 for (i = 0; i < NUM_VIN; i++) {
1143 if (!(data->has_in & BIT(i)))
1146 data->in[i][0] = data->read(data, IT87_REG_VIN[i]);
1148 /* VBAT and AVCC don't have limit registers */
1149 if (i >= NUM_VIN_LIMIT)
1152 data->in[i][1] = data->read(data, IT87_REG_VIN_MIN(i));
1153 data->in[i][2] = data->read(data, IT87_REG_VIN_MAX(i));
1156 for (i = 0; i < NUM_FAN; i++) {
1157 /* Skip disabled fans */
1158 if (!(data->has_fan & BIT(i)))
1161 data->fan[i][1] = data->read(data, data->REG_FAN_MIN[i]);
1162 data->fan[i][0] = data->read(data, data->REG_FAN[i]);
1163 /* Add high byte if in 16-bit mode */
1164 if (has_16bit_fans(data)) {
1165 data->fan[i][0] |= data->read(data,
1166 data->REG_FANX[i]) << 8;
1167 data->fan[i][1] |= data->read(data,
1168 data->REG_FANX_MIN[i]) << 8;
1171 for (i = 0; i < NUM_TEMP; i++) {
1172 if (!(data->has_temp & BIT(i)))
1175 data->read(data, IT87_REG_TEMP(i));
1177 if (i >= data->num_temp_limit)
1180 if (i < data->num_temp_offset)
1182 data->read(data, data->REG_TEMP_OFFSET[i]);
1185 data->read(data, data->REG_TEMP_LOW[i]);
1187 data->read(data, data->REG_TEMP_HIGH[i]);
1190 /* Newer chips don't have clock dividers */
1191 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1192 i = data->read(data, IT87_REG_FAN_DIV);
1193 data->fan_div[0] = i & 0x07;
1194 data->fan_div[1] = (i >> 3) & 0x07;
1195 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1199 data->read(data, IT87_REG_ALARM1) |
1200 (data->read(data, IT87_REG_ALARM2) << 8) |
1201 (data->read(data, IT87_REG_ALARM3) << 16);
1202 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
1204 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
1205 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL);
1206 for (i = 0; i < NUM_PWM; i++) {
1207 if (!(data->has_pwm & BIT(i)))
1209 it87_update_pwm_ctrl(data, i);
1212 data->sensor = data->read(data, IT87_REG_TEMP_ENABLE);
1213 data->extra = data->read(data, IT87_REG_TEMP_EXTRA);
1215 * The IT8705F does not have VID capability.
1216 * The IT8718F and later don't use IT87_REG_VID for the
1219 if (data->type == it8712 || data->type == it8716) {
1220 data->vid = data->read(data, IT87_REG_VID);
1222 * The older IT8712F revisions had only 5 VID pins,
1223 * but we assume it is always safe to read 6 bits.
1227 data->last_updated = jiffies;
1234 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
1237 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1238 struct it87_data *data = it87_update_device(dev);
1239 int index = sattr->index;
1243 return PTR_ERR(data);
1245 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
1248 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
1249 const char *buf, size_t count)
1251 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1252 struct it87_data *data = dev_get_drvdata(dev);
1253 int index = sattr->index;
1258 if (kstrtoul(buf, 10, &val) < 0)
1261 err = it87_lock(data);
1265 data->in[nr][index] = in_to_reg(data, nr, val);
1266 data->write(data, index == 1 ? IT87_REG_VIN_MIN(nr)
1267 : IT87_REG_VIN_MAX(nr),
1268 data->in[nr][index]);
1273 static SENSOR_DEVICE_ATTR_2(in0_input, 0444, show_in, NULL, 0, 0);
1274 static SENSOR_DEVICE_ATTR_2(in0_min, 0644, show_in, set_in, 0, 1);
1275 static SENSOR_DEVICE_ATTR_2(in0_max, 0644, show_in, set_in, 0, 2);
1277 static SENSOR_DEVICE_ATTR_2(in1_input, 0444, show_in, NULL, 1, 0);
1278 static SENSOR_DEVICE_ATTR_2(in1_min, 0644, show_in, set_in, 1, 1);
1279 static SENSOR_DEVICE_ATTR_2(in1_max, 0644, show_in, set_in, 1, 2);
1281 static SENSOR_DEVICE_ATTR_2(in2_input, 0444, show_in, NULL, 2, 0);
1282 static SENSOR_DEVICE_ATTR_2(in2_min, 0644, show_in, set_in, 2, 1);
1283 static SENSOR_DEVICE_ATTR_2(in2_max, 0644, show_in, set_in, 2, 2);
1285 static SENSOR_DEVICE_ATTR_2(in3_input, 0444, show_in, NULL, 3, 0);
1286 static SENSOR_DEVICE_ATTR_2(in3_min, 0644, show_in, set_in, 3, 1);
1287 static SENSOR_DEVICE_ATTR_2(in3_max, 0644, show_in, set_in, 3, 2);
1289 static SENSOR_DEVICE_ATTR_2(in4_input, 0444, show_in, NULL, 4, 0);
1290 static SENSOR_DEVICE_ATTR_2(in4_min, 0644, show_in, set_in, 4, 1);
1291 static SENSOR_DEVICE_ATTR_2(in4_max, 0644, show_in, set_in, 4, 2);
1293 static SENSOR_DEVICE_ATTR_2(in5_input, 0444, show_in, NULL, 5, 0);
1294 static SENSOR_DEVICE_ATTR_2(in5_min, 0644, show_in, set_in, 5, 1);
1295 static SENSOR_DEVICE_ATTR_2(in5_max, 0644, show_in, set_in, 5, 2);
1297 static SENSOR_DEVICE_ATTR_2(in6_input, 0444, show_in, NULL, 6, 0);
1298 static SENSOR_DEVICE_ATTR_2(in6_min, 0644, show_in, set_in, 6, 1);
1299 static SENSOR_DEVICE_ATTR_2(in6_max, 0644, show_in, set_in, 6, 2);
1301 static SENSOR_DEVICE_ATTR_2(in7_input, 0444, show_in, NULL, 7, 0);
1302 static SENSOR_DEVICE_ATTR_2(in7_min, 0644, show_in, set_in, 7, 1);
1303 static SENSOR_DEVICE_ATTR_2(in7_max, 0644, show_in, set_in, 7, 2);
1305 static SENSOR_DEVICE_ATTR_2(in8_input, 0444, show_in, NULL, 8, 0);
1306 static SENSOR_DEVICE_ATTR_2(in9_input, 0444, show_in, NULL, 9, 0);
1307 static SENSOR_DEVICE_ATTR_2(in10_input, 0444, show_in, NULL, 10, 0);
1308 static SENSOR_DEVICE_ATTR_2(in11_input, 0444, show_in, NULL, 11, 0);
1309 static SENSOR_DEVICE_ATTR_2(in12_input, 0444, show_in, NULL, 12, 0);
1311 /* Up to 6 temperatures */
1312 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
1315 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1317 int index = sattr->index;
1318 struct it87_data *data = it87_update_device(dev);
1321 return PTR_ERR(data);
1323 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
1326 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
1327 const char *buf, size_t count)
1329 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1331 int index = sattr->index;
1332 struct it87_data *data = dev_get_drvdata(dev);
1337 if (kstrtol(buf, 10, &val) < 0)
1340 err = it87_lock(data);
1347 reg = data->REG_TEMP_LOW[nr];
1350 reg = data->REG_TEMP_HIGH[nr];
1353 regval = data->read(data, IT87_REG_BEEP_ENABLE);
1354 if (!(regval & 0x80)) {
1356 data->write(data, IT87_REG_BEEP_ENABLE, regval);
1359 reg = data->REG_TEMP_OFFSET[nr];
1363 data->temp[nr][index] = TEMP_TO_REG(val);
1364 data->write(data, reg, data->temp[nr][index]);
1369 static SENSOR_DEVICE_ATTR_2(temp1_input, 0444, show_temp, NULL, 0, 0);
1370 static SENSOR_DEVICE_ATTR_2(temp1_min, 0644, show_temp, set_temp, 0, 1);
1371 static SENSOR_DEVICE_ATTR_2(temp1_max, 0644, show_temp, set_temp, 0, 2);
1372 static SENSOR_DEVICE_ATTR_2(temp1_offset, 0644, show_temp, set_temp, 0, 3);
1373 static SENSOR_DEVICE_ATTR_2(temp2_input, 0444, show_temp, NULL, 1, 0);
1374 static SENSOR_DEVICE_ATTR_2(temp2_min, 0644, show_temp, set_temp, 1, 1);
1375 static SENSOR_DEVICE_ATTR_2(temp2_max, 0644, show_temp, set_temp, 1, 2);
1376 static SENSOR_DEVICE_ATTR_2(temp2_offset, 0644, show_temp, set_temp, 1, 3);
1377 static SENSOR_DEVICE_ATTR_2(temp3_input, 0444, show_temp, NULL, 2, 0);
1378 static SENSOR_DEVICE_ATTR_2(temp3_min, 0644, show_temp, set_temp, 2, 1);
1379 static SENSOR_DEVICE_ATTR_2(temp3_max, 0644, show_temp, set_temp, 2, 2);
1380 static SENSOR_DEVICE_ATTR_2(temp3_offset, 0644, show_temp, set_temp, 2, 3);
1381 static SENSOR_DEVICE_ATTR_2(temp4_input, 0444, show_temp, NULL, 3, 0);
1382 static SENSOR_DEVICE_ATTR_2(temp4_min, 0644, show_temp, set_temp, 3, 1);
1383 static SENSOR_DEVICE_ATTR_2(temp4_max, 0644, show_temp, set_temp, 3, 2);
1384 static SENSOR_DEVICE_ATTR_2(temp4_offset, 0644, show_temp, set_temp, 3, 3);
1385 static SENSOR_DEVICE_ATTR_2(temp5_input, 0444, show_temp, NULL, 4, 0);
1386 static SENSOR_DEVICE_ATTR_2(temp5_min, 0644, show_temp, set_temp, 4, 1);
1387 static SENSOR_DEVICE_ATTR_2(temp5_max, 0644, show_temp, set_temp, 4, 2);
1388 static SENSOR_DEVICE_ATTR_2(temp5_offset, 0644, show_temp, set_temp, 4, 3);
1389 static SENSOR_DEVICE_ATTR_2(temp6_input, 0444, show_temp, NULL, 5, 0);
1390 static SENSOR_DEVICE_ATTR_2(temp6_min, 0644, show_temp, set_temp, 5, 1);
1391 static SENSOR_DEVICE_ATTR_2(temp6_max, 0644, show_temp, set_temp, 5, 2);
1392 static SENSOR_DEVICE_ATTR_2(temp6_offset, 0644, show_temp, set_temp, 5, 3);
1394 static const u8 temp_types_8686[NUM_TEMP][9] = {
1395 { 0, 8, 8, 8, 8, 8, 8, 8, 7 },
1396 { 0, 6, 8, 8, 6, 0, 0, 0, 7 },
1397 { 0, 6, 5, 8, 6, 0, 0, 0, 7 },
1398 { 4, 8, 8, 8, 8, 8, 8, 8, 7 },
1399 { 4, 6, 8, 8, 6, 0, 0, 0, 7 },
1400 { 4, 6, 5, 8, 6, 0, 0, 0, 7 },
1403 static int get_temp_type(struct it87_data *data, int index)
1406 int ttype, type = 0;
1408 if (has_bank_sel(data)) {
1411 src1 = (data->temp_src[index / 2] >> ((index % 2) * 4)) & 0x0f;
1413 switch (data->type) {
1416 type = temp_types_8686[index][src1];
1427 src2 = data->temp_src[3];
1430 type = (src2 & BIT(index)) ? 6 : 5;
1433 type = (src2 & BIT(index)) ? 4 : 6;
1436 type = (src2 & BIT(index)) ? 5 : 0;
1449 /* Dectect PECI vs. AMDTSI if possible */
1451 if ((has_temp_peci(data, index)) && data->type != it8721) {
1452 extra = data->read(data, 0x98); /* PCH/AMDTSI host status */
1457 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1459 /* Per chip special detection */
1460 switch (data->type) {
1462 if (!(reg & 0xc0) && index == 3)
1469 if (type || index >= 3)
1472 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1474 if ((has_temp_peci(data, index) && (reg >> 6 == index + 1)) ||
1475 (has_temp_old_peci(data, index) && (extra & 0x80)))
1476 type = ttype; /* Intel PECI or AMDTSI */
1477 if (reg & BIT(index))
1478 type = 3; /* thermal diode */
1479 else if (reg & BIT(index + 3))
1480 type = 4; /* thermistor */
1485 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1488 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1489 struct it87_data *data = it87_update_device(dev);
1493 return PTR_ERR(data);
1495 type = get_temp_type(data, sensor_attr->index);
1496 return sprintf(buf, "%d\n", type);
1499 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1500 const char *buf, size_t count)
1502 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1503 int nr = sensor_attr->index;
1505 struct it87_data *data = dev_get_drvdata(dev);
1510 if (kstrtol(buf, 10, &val) < 0)
1513 err = it87_lock(data);
1517 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1520 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1522 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1523 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1525 if (val == 2) { /* backwards compatibility */
1527 "Sensor type 2 is deprecated, please use 4 instead\n");
1530 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1535 else if (has_temp_peci(data, nr) && val == 6)
1536 reg |= (nr + 1) << 6;
1537 else if (has_temp_old_peci(data, nr) && val == 6)
1539 else if (val != 0) {
1545 data->extra = extra;
1546 data->write(data, IT87_REG_TEMP_ENABLE, data->sensor);
1547 if (has_temp_old_peci(data, nr))
1548 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1549 data->valid = 0; /* Force cache refresh */
1555 static SENSOR_DEVICE_ATTR(temp1_type, 0644, show_temp_type, set_temp_type, 0);
1556 static SENSOR_DEVICE_ATTR(temp2_type, 0644, show_temp_type, set_temp_type, 1);
1557 static SENSOR_DEVICE_ATTR(temp3_type, 0644, show_temp_type, set_temp_type, 2);
1558 static SENSOR_DEVICE_ATTR(temp4_type, 0644, show_temp_type, set_temp_type, 3);
1559 static SENSOR_DEVICE_ATTR(temp5_type, 0644, show_temp_type, set_temp_type, 4);
1560 static SENSOR_DEVICE_ATTR(temp6_type, 0644, show_temp_type, set_temp_type, 5);
1564 static int pwm_mode(const struct it87_data *data, int nr)
1566 if (has_fanctl_onoff(data) && nr < 3 &&
1567 !(data->fan_main_ctrl & BIT(nr)))
1568 return 0; /* Full speed */
1569 if (data->pwm_ctrl[nr] & 0x80)
1570 return 2; /* Automatic mode */
1571 if ((!has_fanctl_onoff(data) || nr >= 3) &&
1572 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1573 return 0; /* Full speed */
1575 return 1; /* Manual mode */
1578 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1581 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1583 int index = sattr->index;
1585 struct it87_data *data = it87_update_device(dev);
1588 return PTR_ERR(data);
1590 speed = has_16bit_fans(data) ?
1591 FAN16_FROM_REG(data->fan[nr][index]) :
1592 FAN_FROM_REG(data->fan[nr][index],
1593 DIV_FROM_REG(data->fan_div[nr]));
1594 return sprintf(buf, "%d\n", speed);
1597 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1600 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1601 struct it87_data *data = it87_update_device(dev);
1602 int nr = sensor_attr->index;
1605 return PTR_ERR(data);
1607 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1610 static ssize_t show_pwm_enable(struct device *dev,
1611 struct device_attribute *attr, char *buf)
1613 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1614 struct it87_data *data = it87_update_device(dev);
1615 int nr = sensor_attr->index;
1618 return PTR_ERR(data);
1620 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1623 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1626 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1627 struct it87_data *data = it87_update_device(dev);
1628 int nr = sensor_attr->index;
1631 return PTR_ERR(data);
1633 return sprintf(buf, "%d\n",
1634 pwm_from_reg(data, data->pwm_duty[nr]));
1637 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1640 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1641 struct it87_data *data = it87_update_device(dev);
1642 int nr = sensor_attr->index;
1647 return PTR_ERR(data);
1649 if (has_pwm_freq2(data) && nr == 1)
1650 index = (data->extra >> 4) & 0x07;
1652 index = (data->fan_ctl >> 4) & 0x07;
1654 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1656 return sprintf(buf, "%u\n", freq);
1659 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1660 const char *buf, size_t count)
1662 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1664 int index = sattr->index;
1666 struct it87_data *data = dev_get_drvdata(dev);
1671 if (kstrtol(buf, 10, &val) < 0)
1674 err = it87_lock(data);
1678 if (has_16bit_fans(data)) {
1679 data->fan[nr][index] = FAN16_TO_REG(val);
1680 data->write(data, data->REG_FAN_MIN[nr],
1681 data->fan[nr][index] & 0xff);
1682 data->write(data, data->REG_FANX_MIN[nr],
1683 data->fan[nr][index] >> 8);
1685 reg = data->read(data, IT87_REG_FAN_DIV);
1688 data->fan_div[nr] = reg & 0x07;
1691 data->fan_div[nr] = (reg >> 3) & 0x07;
1694 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1697 data->fan[nr][index] =
1698 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1699 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][index]);
1705 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1706 const char *buf, size_t count)
1708 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1709 struct it87_data *data = dev_get_drvdata(dev);
1710 int nr = sensor_attr->index;
1715 if (kstrtoul(buf, 10, &val) < 0)
1718 err = it87_lock(data);
1722 old = data->read(data, IT87_REG_FAN_DIV);
1724 /* Save fan min limit */
1725 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1730 data->fan_div[nr] = DIV_TO_REG(val);
1734 data->fan_div[nr] = 1;
1736 data->fan_div[nr] = 3;
1739 val |= (data->fan_div[0] & 0x07);
1740 val |= (data->fan_div[1] & 0x07) << 3;
1741 if (data->fan_div[2] == 3)
1743 data->write(data, IT87_REG_FAN_DIV, val);
1745 /* Restore fan min limit */
1746 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1747 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][1]);
1752 /* Returns 0 if OK, -EINVAL otherwise */
1753 static int check_trip_points(struct device *dev, int nr)
1755 const struct it87_data *data = dev_get_drvdata(dev);
1758 if (has_old_autopwm(data)) {
1759 for (i = 0; i < 3; i++) {
1760 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1763 for (i = 0; i < 2; i++) {
1764 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1767 } else if (has_newer_autopwm(data)) {
1768 for (i = 1; i < 3; i++) {
1769 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1776 "Inconsistent trip points, not switching to automatic mode\n");
1777 dev_err(dev, "Adjust the trip points and try again\n");
1782 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1783 const char *buf, size_t count)
1785 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1786 struct it87_data *data = dev_get_drvdata(dev);
1787 int nr = sensor_attr->index;
1791 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1794 /* Check trip points before switching to automatic mode */
1796 if (check_trip_points(dev, nr) < 0)
1800 err = it87_lock(data);
1804 it87_update_pwm_ctrl(data, nr);
1807 if (nr < 3 && has_fanctl_onoff(data)) {
1809 /* make sure the fan is on when in on/off mode */
1810 tmp = data->read(data, IT87_REG_FAN_CTL);
1811 data->write(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1812 /* set on/off mode */
1813 data->fan_main_ctrl &= ~BIT(nr);
1814 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1815 data->fan_main_ctrl);
1819 /* No on/off mode, set maximum pwm value */
1820 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1821 data->write(data, IT87_REG_PWM_DUTY[nr],
1822 data->pwm_duty[nr]);
1823 /* and set manual mode */
1824 if (has_newer_autopwm(data)) {
1825 ctrl = temp_map_to_reg(data, nr,
1826 data->pwm_temp_map[nr]);
1829 ctrl = data->pwm_duty[nr];
1831 data->pwm_ctrl[nr] = ctrl;
1832 data->write(data, data->REG_PWM[nr], ctrl);
1837 if (has_newer_autopwm(data)) {
1838 ctrl = temp_map_to_reg(data, nr,
1839 data->pwm_temp_map[nr]);
1845 ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1847 data->pwm_ctrl[nr] = ctrl;
1848 data->write(data, data->REG_PWM[nr], ctrl);
1850 if (has_fanctl_onoff(data) && nr < 3) {
1851 /* set SmartGuardian mode */
1852 data->fan_main_ctrl |= BIT(nr);
1853 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1854 data->fan_main_ctrl);
1861 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1862 const char *buf, size_t count)
1864 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1865 struct it87_data *data = dev_get_drvdata(dev);
1866 int nr = sensor_attr->index;
1870 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1873 err = it87_lock(data);
1877 it87_update_pwm_ctrl(data, nr);
1878 if (has_newer_autopwm(data)) {
1880 * If we are in automatic mode, the PWM duty cycle register
1881 * is read-only so we can't write the value.
1883 if (data->pwm_ctrl[nr] & 0x80) {
1887 data->pwm_duty[nr] = pwm_to_reg(data, val);
1888 data->write(data, IT87_REG_PWM_DUTY[nr],
1889 data->pwm_duty[nr]);
1891 data->pwm_duty[nr] = pwm_to_reg(data, val);
1893 * If we are in manual mode, write the duty cycle immediately;
1894 * otherwise, just store it for later use.
1896 if (!(data->pwm_ctrl[nr] & 0x80)) {
1897 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1898 data->write(data, data->REG_PWM[nr],
1899 data->pwm_ctrl[nr]);
1907 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1908 const char *buf, size_t count)
1910 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1911 struct it87_data *data = dev_get_drvdata(dev);
1912 int nr = sensor_attr->index;
1917 if (kstrtoul(buf, 10, &val) < 0)
1920 val = clamp_val(val, 0, 1000000);
1921 val *= has_newer_autopwm(data) ? 256 : 128;
1923 /* Search for the nearest available frequency */
1924 for (i = 0; i < 7; i++) {
1925 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1929 err = it87_lock(data);
1934 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL) & 0x8f;
1935 data->fan_ctl |= i << 4;
1936 data->write(data, IT87_REG_FAN_CTL, data->fan_ctl);
1938 data->extra = data->read(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1939 data->extra |= i << 4;
1940 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1946 static ssize_t show_pwm_temp_map(struct device *dev,
1947 struct device_attribute *attr, char *buf)
1949 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1950 struct it87_data *data = it87_update_device(dev);
1951 int nr = sensor_attr->index;
1954 return PTR_ERR(data);
1956 return sprintf(buf, "%d\n", data->pwm_temp_map[nr] + 1);
1959 static ssize_t set_pwm_temp_map(struct device *dev,
1960 struct device_attribute *attr, const char *buf,
1963 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1964 struct it87_data *data = dev_get_drvdata(dev);
1965 int nr = sensor_attr->index;
1970 if (kstrtoul(buf, 10, &val) < 0)
1973 if (!val || val > data->pwm_num_temp_map)
1978 err = it87_lock(data);
1982 it87_update_pwm_ctrl(data, nr);
1983 data->pwm_temp_map[nr] = map;
1985 * If we are in automatic mode, write the temp mapping immediately;
1986 * otherwise, just store it for later use.
1988 if (data->pwm_ctrl[nr] & 0x80) {
1989 data->pwm_ctrl[nr] = temp_map_to_reg(data, nr, map);
1990 data->write(data, data->REG_PWM[nr], data->pwm_ctrl[nr]);
1996 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1999 struct it87_data *data = it87_update_device(dev);
2000 struct sensor_device_attribute_2 *sensor_attr =
2001 to_sensor_dev_attr_2(attr);
2002 int nr = sensor_attr->nr;
2003 int point = sensor_attr->index;
2006 return PTR_ERR(data);
2008 return sprintf(buf, "%d\n",
2009 pwm_from_reg(data, data->auto_pwm[nr][point]));
2012 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
2013 const char *buf, size_t count)
2015 struct it87_data *data = dev_get_drvdata(dev);
2016 struct sensor_device_attribute_2 *sensor_attr =
2017 to_sensor_dev_attr_2(attr);
2018 int nr = sensor_attr->nr;
2019 int point = sensor_attr->index;
2024 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
2027 err = it87_lock(data);
2031 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
2032 if (has_newer_autopwm(data))
2033 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
2035 regaddr = IT87_REG_AUTO_PWM(nr, point);
2036 data->write(data, regaddr, data->auto_pwm[nr][point]);
2041 static ssize_t show_auto_pwm_slope(struct device *dev,
2042 struct device_attribute *attr, char *buf)
2044 struct it87_data *data = it87_update_device(dev);
2045 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2046 int nr = sensor_attr->index;
2049 return PTR_ERR(data);
2051 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
2054 static ssize_t set_auto_pwm_slope(struct device *dev,
2055 struct device_attribute *attr,
2056 const char *buf, size_t count)
2058 struct it87_data *data = dev_get_drvdata(dev);
2059 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2060 int nr = sensor_attr->index;
2064 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
2067 err = it87_lock(data);
2071 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
2072 data->write(data, IT87_REG_AUTO_TEMP(nr, 4), data->auto_pwm[nr][1]);
2077 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2080 struct it87_data *data = it87_update_device(dev);
2081 struct sensor_device_attribute_2 *sensor_attr =
2082 to_sensor_dev_attr_2(attr);
2083 int nr = sensor_attr->nr;
2084 int point = sensor_attr->index;
2088 return PTR_ERR(data);
2090 if (has_old_autopwm(data) || point)
2091 reg = data->auto_temp[nr][point];
2093 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
2095 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
2098 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
2099 const char *buf, size_t count)
2101 struct it87_data *data = dev_get_drvdata(dev);
2102 struct sensor_device_attribute_2 *sensor_attr =
2103 to_sensor_dev_attr_2(attr);
2104 int nr = sensor_attr->nr;
2105 int point = sensor_attr->index;
2110 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
2113 err = it87_lock(data);
2117 if (has_newer_autopwm(data) && !point) {
2118 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
2119 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
2120 data->auto_temp[nr][0] = reg;
2121 data->write(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
2123 reg = TEMP_TO_REG(val);
2124 data->auto_temp[nr][point] = reg;
2125 if (has_newer_autopwm(data))
2127 data->write(data, IT87_REG_AUTO_TEMP(nr, point), reg);
2133 static SENSOR_DEVICE_ATTR_2(fan1_input, 0444, show_fan, NULL, 0, 0);
2134 static SENSOR_DEVICE_ATTR_2(fan1_min, 0644, show_fan, set_fan, 0, 1);
2135 static SENSOR_DEVICE_ATTR(fan1_div, 0644, show_fan_div, set_fan_div, 0);
2137 static SENSOR_DEVICE_ATTR_2(fan2_input, 0444, show_fan, NULL, 1, 0);
2138 static SENSOR_DEVICE_ATTR_2(fan2_min, 0644, show_fan, set_fan, 1, 1);
2139 static SENSOR_DEVICE_ATTR(fan2_div, 0644, show_fan_div, set_fan_div, 1);
2141 static SENSOR_DEVICE_ATTR_2(fan3_input, 0444, show_fan, NULL, 2, 0);
2142 static SENSOR_DEVICE_ATTR_2(fan3_min, 0644, show_fan, set_fan, 2, 1);
2143 static SENSOR_DEVICE_ATTR(fan3_div, 0644, show_fan_div, set_fan_div, 2);
2145 static SENSOR_DEVICE_ATTR_2(fan4_input, 0444, show_fan, NULL, 3, 0);
2146 static SENSOR_DEVICE_ATTR_2(fan4_min, 0644, show_fan, set_fan, 3, 1);
2148 static SENSOR_DEVICE_ATTR_2(fan5_input, 0444, show_fan, NULL, 4, 0);
2149 static SENSOR_DEVICE_ATTR_2(fan5_min, 0644, show_fan, set_fan, 4, 1);
2151 static SENSOR_DEVICE_ATTR_2(fan6_input, 0444, show_fan, NULL, 5, 0);
2152 static SENSOR_DEVICE_ATTR_2(fan6_min, 0644, show_fan, set_fan, 5, 1);
2154 static SENSOR_DEVICE_ATTR(pwm1_enable, 0644,
2155 show_pwm_enable, set_pwm_enable, 0);
2156 static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
2157 static SENSOR_DEVICE_ATTR(pwm1_freq, 0644, show_pwm_freq, set_pwm_freq, 0);
2158 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, 0444,
2159 show_pwm_temp_map, set_pwm_temp_map, 0);
2160 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, 0644,
2161 show_auto_pwm, set_auto_pwm, 0, 0);
2162 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, 0644,
2163 show_auto_pwm, set_auto_pwm, 0, 1);
2164 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, 0644,
2165 show_auto_pwm, set_auto_pwm, 0, 2);
2166 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, 0444,
2167 show_auto_pwm, NULL, 0, 3);
2168 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, 0644,
2169 show_auto_temp, set_auto_temp, 0, 1);
2170 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, 0644,
2171 show_auto_temp, set_auto_temp, 0, 0);
2172 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, 0644,
2173 show_auto_temp, set_auto_temp, 0, 2);
2174 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, 0644,
2175 show_auto_temp, set_auto_temp, 0, 3);
2176 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, 0644,
2177 show_auto_temp, set_auto_temp, 0, 4);
2178 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, 0644,
2179 show_auto_pwm, set_auto_pwm, 0, 0);
2180 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, 0644,
2181 show_auto_pwm_slope, set_auto_pwm_slope, 0);
2183 static SENSOR_DEVICE_ATTR(pwm2_enable, 0644,
2184 show_pwm_enable, set_pwm_enable, 1);
2185 static SENSOR_DEVICE_ATTR(pwm2, 0644, show_pwm, set_pwm, 1);
2186 static SENSOR_DEVICE_ATTR(pwm2_freq, 0444, show_pwm_freq, set_pwm_freq, 1);
2187 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, 0444,
2188 show_pwm_temp_map, set_pwm_temp_map, 1);
2189 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, 0644,
2190 show_auto_pwm, set_auto_pwm, 1, 0);
2191 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, 0644,
2192 show_auto_pwm, set_auto_pwm, 1, 1);
2193 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, 0644,
2194 show_auto_pwm, set_auto_pwm, 1, 2);
2195 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, 0444,
2196 show_auto_pwm, NULL, 1, 3);
2197 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, 0644,
2198 show_auto_temp, set_auto_temp, 1, 1);
2199 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, 0644,
2200 show_auto_temp, set_auto_temp, 1, 0);
2201 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, 0644,
2202 show_auto_temp, set_auto_temp, 1, 2);
2203 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, 0644,
2204 show_auto_temp, set_auto_temp, 1, 3);
2205 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, 0644,
2206 show_auto_temp, set_auto_temp, 1, 4);
2207 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, 0644,
2208 show_auto_pwm, set_auto_pwm, 1, 0);
2209 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, 0644,
2210 show_auto_pwm_slope, set_auto_pwm_slope, 1);
2212 static SENSOR_DEVICE_ATTR(pwm3_enable, 0644,
2213 show_pwm_enable, set_pwm_enable, 2);
2214 static SENSOR_DEVICE_ATTR(pwm3, 0644, show_pwm, set_pwm, 2);
2215 static SENSOR_DEVICE_ATTR(pwm3_freq, 0444, show_pwm_freq, NULL, 2);
2216 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, 0444,
2217 show_pwm_temp_map, set_pwm_temp_map, 2);
2218 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, 0644,
2219 show_auto_pwm, set_auto_pwm, 2, 0);
2220 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, 0644,
2221 show_auto_pwm, set_auto_pwm, 2, 1);
2222 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, 0644,
2223 show_auto_pwm, set_auto_pwm, 2, 2);
2224 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, 0444,
2225 show_auto_pwm, NULL, 2, 3);
2226 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, 0644,
2227 show_auto_temp, set_auto_temp, 2, 1);
2228 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, 0644,
2229 show_auto_temp, set_auto_temp, 2, 0);
2230 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, 0644,
2231 show_auto_temp, set_auto_temp, 2, 2);
2232 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, 0644,
2233 show_auto_temp, set_auto_temp, 2, 3);
2234 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, 0644,
2235 show_auto_temp, set_auto_temp, 2, 4);
2236 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, 0644,
2237 show_auto_pwm, set_auto_pwm, 2, 0);
2238 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, 0644,
2239 show_auto_pwm_slope, set_auto_pwm_slope, 2);
2241 static SENSOR_DEVICE_ATTR(pwm4_enable, 0644,
2242 show_pwm_enable, set_pwm_enable, 3);
2243 static SENSOR_DEVICE_ATTR(pwm4, 0644, show_pwm, set_pwm, 3);
2244 static SENSOR_DEVICE_ATTR(pwm4_freq, 0444, show_pwm_freq, NULL, 3);
2245 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, 0444,
2246 show_pwm_temp_map, set_pwm_temp_map, 3);
2247 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, 0644,
2248 show_auto_temp, set_auto_temp, 2, 1);
2249 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, 0644,
2250 show_auto_temp, set_auto_temp, 2, 0);
2251 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, 0644,
2252 show_auto_temp, set_auto_temp, 2, 2);
2253 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, 0644,
2254 show_auto_temp, set_auto_temp, 2, 3);
2255 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, 0644,
2256 show_auto_pwm, set_auto_pwm, 3, 0);
2257 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, 0644,
2258 show_auto_pwm_slope, set_auto_pwm_slope, 3);
2260 static SENSOR_DEVICE_ATTR(pwm5_enable, 0644,
2261 show_pwm_enable, set_pwm_enable, 4);
2262 static SENSOR_DEVICE_ATTR(pwm5, 0644, show_pwm, set_pwm, 4);
2263 static SENSOR_DEVICE_ATTR(pwm5_freq, 0444, show_pwm_freq, NULL, 4);
2264 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, 0444,
2265 show_pwm_temp_map, set_pwm_temp_map, 4);
2266 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, 0644,
2267 show_auto_temp, set_auto_temp, 2, 1);
2268 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, 0644,
2269 show_auto_temp, set_auto_temp, 2, 0);
2270 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, 0644,
2271 show_auto_temp, set_auto_temp, 2, 2);
2272 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, 0644,
2273 show_auto_temp, set_auto_temp, 2, 3);
2274 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, 0644,
2275 show_auto_pwm, set_auto_pwm, 4, 0);
2276 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, 0644,
2277 show_auto_pwm_slope, set_auto_pwm_slope, 4);
2279 static SENSOR_DEVICE_ATTR(pwm6_enable, 0644,
2280 show_pwm_enable, set_pwm_enable, 5);
2281 static SENSOR_DEVICE_ATTR(pwm6, 0644, show_pwm, set_pwm, 5);
2282 static SENSOR_DEVICE_ATTR(pwm6_freq, 0444, show_pwm_freq, NULL, 5);
2283 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, 0444,
2284 show_pwm_temp_map, set_pwm_temp_map, 5);
2285 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, 0644,
2286 show_auto_temp, set_auto_temp, 2, 1);
2287 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, 0644,
2288 show_auto_temp, set_auto_temp, 2, 0);
2289 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, 0644,
2290 show_auto_temp, set_auto_temp, 2, 2);
2291 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, 0644,
2292 show_auto_temp, set_auto_temp, 2, 3);
2293 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, 0644,
2294 show_auto_pwm, set_auto_pwm, 5, 0);
2295 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, 0644,
2296 show_auto_pwm_slope, set_auto_pwm_slope, 5);
2299 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
2302 struct it87_data *data = it87_update_device(dev);
2305 return PTR_ERR(data);
2307 return sprintf(buf, "%u\n", data->alarms);
2309 static DEVICE_ATTR(alarms, 0444, show_alarms, NULL);
2311 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
2314 struct it87_data *data = it87_update_device(dev);
2315 int bitnr = to_sensor_dev_attr(attr)->index;
2318 return PTR_ERR(data);
2320 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
2323 static ssize_t clear_intrusion(struct device *dev,
2324 struct device_attribute *attr, const char *buf,
2327 struct it87_data *data = dev_get_drvdata(dev);
2331 if (kstrtol(buf, 10, &val) < 0 || val != 0)
2334 err = it87_lock(data);
2338 config = data->read(data, IT87_REG_CONFIG);
2340 data->write(data, IT87_REG_CONFIG, config);
2341 /* Invalidate cache to force re-read */
2347 static SENSOR_DEVICE_ATTR(in0_alarm, 0444, show_alarm, NULL, 8);
2348 static SENSOR_DEVICE_ATTR(in1_alarm, 0444, show_alarm, NULL, 9);
2349 static SENSOR_DEVICE_ATTR(in2_alarm, 0444, show_alarm, NULL, 10);
2350 static SENSOR_DEVICE_ATTR(in3_alarm, 0444, show_alarm, NULL, 11);
2351 static SENSOR_DEVICE_ATTR(in4_alarm, 0444, show_alarm, NULL, 12);
2352 static SENSOR_DEVICE_ATTR(in5_alarm, 0444, show_alarm, NULL, 13);
2353 static SENSOR_DEVICE_ATTR(in6_alarm, 0444, show_alarm, NULL, 14);
2354 static SENSOR_DEVICE_ATTR(in7_alarm, 0444, show_alarm, NULL, 15);
2355 static SENSOR_DEVICE_ATTR(fan1_alarm, 0444, show_alarm, NULL, 0);
2356 static SENSOR_DEVICE_ATTR(fan2_alarm, 0444, show_alarm, NULL, 1);
2357 static SENSOR_DEVICE_ATTR(fan3_alarm, 0444, show_alarm, NULL, 2);
2358 static SENSOR_DEVICE_ATTR(fan4_alarm, 0444, show_alarm, NULL, 3);
2359 static SENSOR_DEVICE_ATTR(fan5_alarm, 0444, show_alarm, NULL, 6);
2360 static SENSOR_DEVICE_ATTR(fan6_alarm, 0444, show_alarm, NULL, 7);
2361 static SENSOR_DEVICE_ATTR(temp1_alarm, 0444, show_alarm, NULL, 16);
2362 static SENSOR_DEVICE_ATTR(temp2_alarm, 0444, show_alarm, NULL, 17);
2363 static SENSOR_DEVICE_ATTR(temp3_alarm, 0444, show_alarm, NULL, 18);
2364 static SENSOR_DEVICE_ATTR(temp4_alarm, 0444, show_alarm, NULL, 19);
2365 static SENSOR_DEVICE_ATTR(temp5_alarm, 0444, show_alarm, NULL, 20);
2366 static SENSOR_DEVICE_ATTR(temp6_alarm, 0444, show_alarm, NULL, 21);
2367 static SENSOR_DEVICE_ATTR(intrusion0_alarm, 0644,
2368 show_alarm, clear_intrusion, 4);
2370 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
2373 struct it87_data *data = it87_update_device(dev);
2374 int bitnr = to_sensor_dev_attr(attr)->index;
2377 return PTR_ERR(data);
2379 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
2382 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
2383 const char *buf, size_t count)
2385 int bitnr = to_sensor_dev_attr(attr)->index;
2386 struct it87_data *data = dev_get_drvdata(dev);
2390 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
2393 err = it87_lock(data);
2397 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
2399 data->beeps |= BIT(bitnr);
2401 data->beeps &= ~BIT(bitnr);
2402 data->write(data, IT87_REG_BEEP_ENABLE, data->beeps);
2407 static SENSOR_DEVICE_ATTR(in0_beep, 0644,
2408 show_beep, set_beep, 1);
2409 static SENSOR_DEVICE_ATTR(in1_beep, 0444, show_beep, NULL, 1);
2410 static SENSOR_DEVICE_ATTR(in2_beep, 0444, show_beep, NULL, 1);
2411 static SENSOR_DEVICE_ATTR(in3_beep, 0444, show_beep, NULL, 1);
2412 static SENSOR_DEVICE_ATTR(in4_beep, 0444, show_beep, NULL, 1);
2413 static SENSOR_DEVICE_ATTR(in5_beep, 0444, show_beep, NULL, 1);
2414 static SENSOR_DEVICE_ATTR(in6_beep, 0444, show_beep, NULL, 1);
2415 static SENSOR_DEVICE_ATTR(in7_beep, 0444, show_beep, NULL, 1);
2416 /* fanX_beep writability is set later */
2417 static SENSOR_DEVICE_ATTR(fan1_beep, 0444, show_beep, set_beep, 0);
2418 static SENSOR_DEVICE_ATTR(fan2_beep, 0444, show_beep, set_beep, 0);
2419 static SENSOR_DEVICE_ATTR(fan3_beep, 0444, show_beep, set_beep, 0);
2420 static SENSOR_DEVICE_ATTR(fan4_beep, 0444, show_beep, set_beep, 0);
2421 static SENSOR_DEVICE_ATTR(fan5_beep, 0444, show_beep, set_beep, 0);
2422 static SENSOR_DEVICE_ATTR(fan6_beep, 0444, show_beep, set_beep, 0);
2423 static SENSOR_DEVICE_ATTR(temp1_beep, 0644,
2424 show_beep, set_beep, 2);
2425 static SENSOR_DEVICE_ATTR(temp2_beep, 0444, show_beep, NULL, 2);
2426 static SENSOR_DEVICE_ATTR(temp3_beep, 0444, show_beep, NULL, 2);
2427 static SENSOR_DEVICE_ATTR(temp4_beep, 0444, show_beep, NULL, 2);
2428 static SENSOR_DEVICE_ATTR(temp5_beep, 0444, show_beep, NULL, 2);
2429 static SENSOR_DEVICE_ATTR(temp6_beep, 0444, show_beep, NULL, 2);
2431 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
2434 struct it87_data *data = dev_get_drvdata(dev);
2436 return sprintf(buf, "%u\n", data->vrm);
2439 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
2440 const char *buf, size_t count)
2442 struct it87_data *data = dev_get_drvdata(dev);
2445 if (kstrtoul(buf, 10, &val) < 0)
2452 static DEVICE_ATTR(vrm, 0644, show_vrm_reg, store_vrm_reg);
2454 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
2457 struct it87_data *data = it87_update_device(dev);
2460 return PTR_ERR(data);
2462 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
2464 static DEVICE_ATTR(cpu0_vid, 0444, show_vid_reg, NULL);
2466 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
2469 static const char * const labels[] = {
2475 static const char * const labels_it8721[] = {
2481 struct it87_data *data = dev_get_drvdata(dev);
2482 int nr = to_sensor_dev_attr(attr)->index;
2485 if (has_vin3_5v(data) && nr == 0)
2487 else if (has_12mv_adc(data) || has_10_9mv_adc(data) ||
2489 label = labels_it8721[nr];
2493 return sprintf(buf, "%s\n", label);
2495 static SENSOR_DEVICE_ATTR(in3_label, 0444, show_label, NULL, 0);
2496 static SENSOR_DEVICE_ATTR(in7_label, 0444, show_label, NULL, 1);
2497 static SENSOR_DEVICE_ATTR(in8_label, 0444, show_label, NULL, 2);
2499 static SENSOR_DEVICE_ATTR(in9_label, 0444, show_label, NULL, 3);
2501 static umode_t it87_in_is_visible(struct kobject *kobj,
2502 struct attribute *attr, int index)
2504 struct device *dev = container_of(kobj, struct device, kobj);
2505 struct it87_data *data = dev_get_drvdata(dev);
2506 int i = index / 5; /* voltage index */
2507 int a = index % 5; /* attribute index */
2509 if (index >= 40) { /* in8 and higher only have input attributes */
2514 if (!(data->has_in & BIT(i)))
2517 if (a == 4 && !data->has_beep)
2523 static struct attribute *it87_attributes_in[] = {
2524 &sensor_dev_attr_in0_input.dev_attr.attr,
2525 &sensor_dev_attr_in0_min.dev_attr.attr,
2526 &sensor_dev_attr_in0_max.dev_attr.attr,
2527 &sensor_dev_attr_in0_alarm.dev_attr.attr,
2528 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
2530 &sensor_dev_attr_in1_input.dev_attr.attr,
2531 &sensor_dev_attr_in1_min.dev_attr.attr,
2532 &sensor_dev_attr_in1_max.dev_attr.attr,
2533 &sensor_dev_attr_in1_alarm.dev_attr.attr,
2534 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
2536 &sensor_dev_attr_in2_input.dev_attr.attr,
2537 &sensor_dev_attr_in2_min.dev_attr.attr,
2538 &sensor_dev_attr_in2_max.dev_attr.attr,
2539 &sensor_dev_attr_in2_alarm.dev_attr.attr,
2540 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
2542 &sensor_dev_attr_in3_input.dev_attr.attr,
2543 &sensor_dev_attr_in3_min.dev_attr.attr,
2544 &sensor_dev_attr_in3_max.dev_attr.attr,
2545 &sensor_dev_attr_in3_alarm.dev_attr.attr,
2546 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2548 &sensor_dev_attr_in4_input.dev_attr.attr,
2549 &sensor_dev_attr_in4_min.dev_attr.attr,
2550 &sensor_dev_attr_in4_max.dev_attr.attr,
2551 &sensor_dev_attr_in4_alarm.dev_attr.attr,
2552 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2554 &sensor_dev_attr_in5_input.dev_attr.attr,
2555 &sensor_dev_attr_in5_min.dev_attr.attr,
2556 &sensor_dev_attr_in5_max.dev_attr.attr,
2557 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2558 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2560 &sensor_dev_attr_in6_input.dev_attr.attr,
2561 &sensor_dev_attr_in6_min.dev_attr.attr,
2562 &sensor_dev_attr_in6_max.dev_attr.attr,
2563 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2564 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2566 &sensor_dev_attr_in7_input.dev_attr.attr,
2567 &sensor_dev_attr_in7_min.dev_attr.attr,
2568 &sensor_dev_attr_in7_max.dev_attr.attr,
2569 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2570 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2572 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2573 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
2574 &sensor_dev_attr_in10_input.dev_attr.attr, /* 42 */
2575 &sensor_dev_attr_in11_input.dev_attr.attr, /* 43 */
2576 &sensor_dev_attr_in12_input.dev_attr.attr, /* 44 */
2580 static const struct attribute_group it87_group_in = {
2581 .attrs = it87_attributes_in,
2582 .is_visible = it87_in_is_visible,
2585 static umode_t it87_temp_is_visible(struct kobject *kobj,
2586 struct attribute *attr, int index)
2588 struct device *dev = container_of(kobj, struct device, kobj);
2589 struct it87_data *data = dev_get_drvdata(dev);
2590 int i = index / 7; /* temperature index */
2591 int a = index % 7; /* attribute index */
2593 if (!(data->has_temp & BIT(i)))
2596 if (a && i >= data->num_temp_limit)
2600 int type = get_temp_type(data, i);
2604 if (has_bank_sel(data))
2609 if (a == 5 && i >= data->num_temp_offset)
2612 if (a == 6 && !data->has_beep)
2618 static struct attribute *it87_attributes_temp[] = {
2619 &sensor_dev_attr_temp1_input.dev_attr.attr,
2620 &sensor_dev_attr_temp1_max.dev_attr.attr,
2621 &sensor_dev_attr_temp1_min.dev_attr.attr,
2622 &sensor_dev_attr_temp1_type.dev_attr.attr, /* 3 */
2623 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2624 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2625 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2627 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2628 &sensor_dev_attr_temp2_max.dev_attr.attr,
2629 &sensor_dev_attr_temp2_min.dev_attr.attr,
2630 &sensor_dev_attr_temp2_type.dev_attr.attr,
2631 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2632 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2633 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2635 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2636 &sensor_dev_attr_temp3_max.dev_attr.attr,
2637 &sensor_dev_attr_temp3_min.dev_attr.attr,
2638 &sensor_dev_attr_temp3_type.dev_attr.attr,
2639 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2640 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2641 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2643 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2644 &sensor_dev_attr_temp4_max.dev_attr.attr,
2645 &sensor_dev_attr_temp4_min.dev_attr.attr,
2646 &sensor_dev_attr_temp4_type.dev_attr.attr,
2647 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
2648 &sensor_dev_attr_temp4_offset.dev_attr.attr,
2649 &sensor_dev_attr_temp4_beep.dev_attr.attr,
2651 &sensor_dev_attr_temp5_input.dev_attr.attr,
2652 &sensor_dev_attr_temp5_max.dev_attr.attr,
2653 &sensor_dev_attr_temp5_min.dev_attr.attr,
2654 &sensor_dev_attr_temp5_type.dev_attr.attr,
2655 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
2656 &sensor_dev_attr_temp5_offset.dev_attr.attr,
2657 &sensor_dev_attr_temp5_beep.dev_attr.attr,
2659 &sensor_dev_attr_temp6_input.dev_attr.attr,
2660 &sensor_dev_attr_temp6_max.dev_attr.attr,
2661 &sensor_dev_attr_temp6_min.dev_attr.attr,
2662 &sensor_dev_attr_temp6_type.dev_attr.attr,
2663 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
2664 &sensor_dev_attr_temp6_offset.dev_attr.attr,
2665 &sensor_dev_attr_temp6_beep.dev_attr.attr,
2669 static const struct attribute_group it87_group_temp = {
2670 .attrs = it87_attributes_temp,
2671 .is_visible = it87_temp_is_visible,
2674 static umode_t it87_is_visible(struct kobject *kobj,
2675 struct attribute *attr, int index)
2677 struct device *dev = container_of(kobj, struct device, kobj);
2678 struct it87_data *data = dev_get_drvdata(dev);
2680 if ((index == 2 || index == 3) && !data->has_vid)
2683 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2689 static struct attribute *it87_attributes[] = {
2690 &dev_attr_alarms.attr,
2691 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2692 &dev_attr_vrm.attr, /* 2 */
2693 &dev_attr_cpu0_vid.attr, /* 3 */
2694 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2695 &sensor_dev_attr_in7_label.dev_attr.attr,
2696 &sensor_dev_attr_in8_label.dev_attr.attr,
2697 &sensor_dev_attr_in9_label.dev_attr.attr,
2701 static const struct attribute_group it87_group = {
2702 .attrs = it87_attributes,
2703 .is_visible = it87_is_visible,
2706 static umode_t it87_fan_is_visible(struct kobject *kobj,
2707 struct attribute *attr, int index)
2709 struct device *dev = container_of(kobj, struct device, kobj);
2710 struct it87_data *data = dev_get_drvdata(dev);
2711 int i = index / 5; /* fan index */
2712 int a = index % 5; /* attribute index */
2714 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2715 i = (index - 15) / 4 + 3;
2716 a = (index - 15) % 4;
2719 if (!(data->has_fan & BIT(i)))
2722 if (a == 3) { /* beep */
2723 if (!data->has_beep)
2725 /* first fan beep attribute is writable */
2726 if (i == __ffs(data->has_fan))
2727 return attr->mode | 0200;
2730 if (a == 4 && has_16bit_fans(data)) /* divisor */
2736 static struct attribute *it87_attributes_fan[] = {
2737 &sensor_dev_attr_fan1_input.dev_attr.attr,
2738 &sensor_dev_attr_fan1_min.dev_attr.attr,
2739 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2740 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2741 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2743 &sensor_dev_attr_fan2_input.dev_attr.attr,
2744 &sensor_dev_attr_fan2_min.dev_attr.attr,
2745 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2746 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2747 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2749 &sensor_dev_attr_fan3_input.dev_attr.attr,
2750 &sensor_dev_attr_fan3_min.dev_attr.attr,
2751 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2752 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2753 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2755 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2756 &sensor_dev_attr_fan4_min.dev_attr.attr,
2757 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2758 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2760 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2761 &sensor_dev_attr_fan5_min.dev_attr.attr,
2762 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2763 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2765 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2766 &sensor_dev_attr_fan6_min.dev_attr.attr,
2767 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2768 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2772 static const struct attribute_group it87_group_fan = {
2773 .attrs = it87_attributes_fan,
2774 .is_visible = it87_fan_is_visible,
2777 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2778 struct attribute *attr, int index)
2780 struct device *dev = container_of(kobj, struct device, kobj);
2781 struct it87_data *data = dev_get_drvdata(dev);
2782 int i = index / 4; /* pwm index */
2783 int a = index % 4; /* attribute index */
2785 if (!(data->has_pwm & BIT(i)))
2788 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2789 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2790 return attr->mode | 0200;
2792 /* pwm2_freq is writable if there are two pwm frequency selects */
2793 if (has_pwm_freq2(data) && i == 1 && a == 2)
2794 return attr->mode | 0200;
2799 static struct attribute *it87_attributes_pwm[] = {
2800 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2801 &sensor_dev_attr_pwm1.dev_attr.attr,
2802 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2803 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2805 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2806 &sensor_dev_attr_pwm2.dev_attr.attr,
2807 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2808 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2810 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2811 &sensor_dev_attr_pwm3.dev_attr.attr,
2812 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2813 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2815 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2816 &sensor_dev_attr_pwm4.dev_attr.attr,
2817 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2818 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2820 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2821 &sensor_dev_attr_pwm5.dev_attr.attr,
2822 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2823 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2825 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2826 &sensor_dev_attr_pwm6.dev_attr.attr,
2827 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2828 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2833 static const struct attribute_group it87_group_pwm = {
2834 .attrs = it87_attributes_pwm,
2835 .is_visible = it87_pwm_is_visible,
2838 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2839 struct attribute *attr, int index)
2841 struct device *dev = container_of(kobj, struct device, kobj);
2842 struct it87_data *data = dev_get_drvdata(dev);
2843 int i = index / 11; /* pwm index */
2844 int a = index % 11; /* attribute index */
2846 if (index >= 33) { /* pwm 4..6 */
2847 i = (index - 33) / 6 + 3;
2848 a = (index - 33) % 6 + 4;
2851 if (!(data->has_pwm & BIT(i)))
2854 if (has_newer_autopwm(data)) {
2855 if (a < 4) /* no auto point pwm */
2857 if (a == 8) /* no auto_point4 */
2860 if (has_old_autopwm(data)) {
2861 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2868 static struct attribute *it87_attributes_auto_pwm[] = {
2869 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2870 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2871 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2872 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2873 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2874 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2875 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2876 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2877 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2878 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2879 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2881 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2882 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2883 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2884 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2885 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2886 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2887 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2888 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2889 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2890 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2891 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2893 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2894 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2895 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2896 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2897 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2898 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2899 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2900 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2901 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2902 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2903 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2905 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2906 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2907 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2908 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2909 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2910 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2912 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2913 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2914 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2915 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2916 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2917 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2919 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2920 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2921 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2922 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2923 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2924 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2929 static const struct attribute_group it87_group_auto_pwm = {
2930 .attrs = it87_attributes_auto_pwm,
2931 .is_visible = it87_auto_pwm_is_visible,
2934 /* SuperIO detection - will change isa_address if a chip is found */
2935 static int __init it87_find(int sioaddr, unsigned short *address,
2936 phys_addr_t *mmio_address, struct it87_sio_data *sio_data)
2938 const struct it87_devices *config;
2939 phys_addr_t base = 0;
2945 err = superio_enter(sioaddr);
2949 sio_data->sioaddr = sioaddr;
2952 chip_type = superio_inw(sioaddr, DEVID);
2953 if (chip_type == 0xffff)
2957 chip_type = force_id;
2959 switch (chip_type) {
2961 sio_data->type = it87;
2964 sio_data->type = it8712;
2968 sio_data->type = it8716;
2971 sio_data->type = it8718;
2974 sio_data->type = it8720;
2977 sio_data->type = it8721;
2980 sio_data->type = it8728;
2983 sio_data->type = it8732;
2986 sio_data->type = it8792;
2988 * Disabling configuration mode on IT8792E can result in system
2989 * hang-ups and access failures to the Super-IO chip at the
2990 * second SIO address. Never exit configuration mode on this
2991 * chip to avoid the problem.
2996 sio_data->type = it8771;
2999 sio_data->type = it8772;
3002 sio_data->type = it8781;
3005 sio_data->type = it8782;
3008 sio_data->type = it8783;
3011 sio_data->type = it8786;
3014 sio_data->type = it8790;
3015 doexit = false; /* See IT8792E comment above */
3019 sio_data->type = it8603;
3022 sio_data->type = it8607;
3025 sio_data->type = it8613;
3028 sio_data->type = it8620;
3031 sio_data->type = it8622;
3034 sio_data->type = it8625;
3037 sio_data->type = it8628;
3040 sio_data->type = it8655;
3043 sio_data->type = it8665;
3046 sio_data->type = it8686;
3048 case 0xffff: /* No device at all */
3051 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
3055 superio_select(sioaddr, PME);
3056 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
3057 pr_info("Device not activated, skipping\n");
3061 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
3062 if (*address == 0) {
3063 pr_info("Base address not set, skipping\n");
3067 sio_data->doexit = doexit;
3070 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
3072 config = &it87_devices[sio_data->type];
3074 if (has_mmio(config) && mmio) {
3077 reg = superio_inb(sioaddr, IT87_EC_HWM_MIO_REG);
3079 base = 0xf0000000 + ((reg & 0x0f) << 24);
3080 base += (reg & 0xc0) << 14;
3083 *mmio_address = base;
3087 snprintf(mmio_str, sizeof(mmio_str), " [MMIO at %pa]", &base);
3089 pr_info("Found IT%04x%s chip at 0x%x%s, revision %d\n", chip_type,
3090 it87_devices[sio_data->type].suffix,
3091 *address, mmio_str, sio_data->revision);
3093 /* in7 (VSB or VCCH5V) is always internal on some chips */
3094 if (has_in7_internal(config))
3095 sio_data->internal |= BIT(1);
3097 /* in8 (Vbat) is always internal */
3098 sio_data->internal |= BIT(2);
3100 /* in9 (AVCC3), always internal if supported */
3101 if (has_avcc3(config))
3102 sio_data->internal |= BIT(3); /* in9 is AVCC */
3104 sio_data->skip_in |= BIT(9);
3106 if (!has_four_pwm(config))
3107 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
3108 else if (!has_five_pwm(config))
3109 sio_data->skip_pwm |= BIT(4) | BIT(5);
3110 else if (!has_six_pwm(config))
3111 sio_data->skip_pwm |= BIT(5);
3113 if (!has_vid(config))
3114 sio_data->skip_vid = 1;
3116 /* Read GPIO config and VID value from LDN 7 (GPIO) */
3117 if (sio_data->type == it87) {
3118 /* The IT8705F has a different LD number for GPIO */
3119 superio_select(sioaddr, 5);
3120 sio_data->beep_pin = superio_inb(sioaddr,
3121 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3122 } else if (sio_data->type == it8783) {
3123 int reg25, reg27, reg2a, reg2c, regef;
3125 superio_select(sioaddr, GPIO);
3127 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3128 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3129 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3130 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3131 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
3133 /* Check if fan3 is there or not */
3134 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
3135 sio_data->skip_fan |= BIT(2);
3136 if ((reg25 & BIT(4)) ||
3137 (!(reg2a & BIT(1)) && (regef & BIT(0))))
3138 sio_data->skip_pwm |= BIT(2);
3140 /* Check if fan2 is there or not */
3142 sio_data->skip_fan |= BIT(1);
3144 sio_data->skip_pwm |= BIT(1);
3147 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
3148 sio_data->skip_in |= BIT(5); /* No VIN5 */
3152 sio_data->skip_in |= BIT(6); /* No VIN6 */
3156 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
3158 if (reg27 & BIT(2)) {
3160 * The data sheet is a bit unclear regarding the
3161 * internal voltage divider for VCCH5V. It says
3162 * "This bit enables and switches VIN7 (pin 91) to the
3163 * internal voltage divider for VCCH5V".
3164 * This is different to other chips, where the internal
3165 * voltage divider would connect VIN7 to an internal
3166 * voltage source. Maybe that is the case here as well.
3168 * Since we don't know for sure, re-route it if that is
3169 * not the case, and ask the user to report if the
3170 * resulting voltage is sane.
3172 if (!(reg2c & BIT(1))) {
3174 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
3176 pr_notice("Routing internal VCCH5V to in7.\n");
3178 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
3179 pr_notice("Please report if it displays a reasonable voltage.\n");
3183 sio_data->internal |= BIT(0);
3185 sio_data->internal |= BIT(1);
3187 sio_data->beep_pin = superio_inb(sioaddr,
3188 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3189 } else if (sio_data->type == it8603 || sio_data->type == it8607) {
3192 superio_select(sioaddr, GPIO);
3194 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3196 /* Check if fan3 is there or not */
3198 sio_data->skip_pwm |= BIT(2);
3200 sio_data->skip_fan |= BIT(2);
3202 /* Check if fan2 is there or not */
3203 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3205 sio_data->skip_pwm |= BIT(1);
3207 sio_data->skip_fan |= BIT(1);
3209 switch (sio_data->type) {
3211 sio_data->skip_in |= BIT(5); /* No VIN5 */
3212 sio_data->skip_in |= BIT(6); /* No VIN6 */
3215 sio_data->skip_pwm |= BIT(0);/* No fan1 */
3216 sio_data->skip_fan |= BIT(0);
3221 sio_data->beep_pin = superio_inb(sioaddr,
3222 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3223 } else if (sio_data->type == it8613) {
3224 int reg27, reg29, reg2a;
3226 superio_select(sioaddr, GPIO);
3228 /* Check for pwm3, fan3, pwm5, fan5 */
3229 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3231 sio_data->skip_fan |= BIT(4);
3233 sio_data->skip_pwm |= BIT(4);
3235 sio_data->skip_pwm |= BIT(2);
3237 sio_data->skip_fan |= BIT(2);
3239 /* Check for pwm2, fan2 */
3240 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3242 sio_data->skip_pwm |= BIT(1);
3244 sio_data->skip_fan |= BIT(1);
3246 /* Check for pwm4, fan4 */
3247 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3248 if (!(reg2a & BIT(0)) || (reg29 & BIT(7))) {
3249 sio_data->skip_fan |= BIT(3);
3250 sio_data->skip_pwm |= BIT(3);
3253 sio_data->skip_pwm |= BIT(0); /* No pwm1 */
3254 sio_data->skip_fan |= BIT(0); /* No fan1 */
3255 sio_data->skip_in |= BIT(3); /* No VIN3 */
3256 sio_data->skip_in |= BIT(6); /* No VIN6 */
3258 sio_data->beep_pin = superio_inb(sioaddr,
3259 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3260 } else if (sio_data->type == it8620 || sio_data->type == it8628 ||
3261 sio_data->type == it8686) {
3264 superio_select(sioaddr, GPIO);
3266 /* Check for pwm5 */
3267 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3269 sio_data->skip_pwm |= BIT(4);
3271 /* Check for fan4, fan5 */
3272 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3273 if (!(reg & BIT(5)))
3274 sio_data->skip_fan |= BIT(3);
3275 if (!(reg & BIT(4)))
3276 sio_data->skip_fan |= BIT(4);
3278 /* Check for pwm3, fan3 */
3279 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3281 sio_data->skip_pwm |= BIT(2);
3283 sio_data->skip_fan |= BIT(2);
3285 /* Check for pwm4 */
3286 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
3288 sio_data->skip_pwm |= BIT(3);
3290 /* Check for pwm2, fan2 */
3291 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3293 sio_data->skip_pwm |= BIT(1);
3295 sio_data->skip_fan |= BIT(1);
3296 /* Check for pwm6, fan6 */
3297 if (!(reg & BIT(7))) {
3298 sio_data->skip_pwm |= BIT(5);
3299 sio_data->skip_fan |= BIT(5);
3302 /* Check if AVCC is on VIN3 */
3303 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3305 /* For it8686, the bit just enables AVCC3 */
3306 if (sio_data->type != it8686)
3307 sio_data->internal |= BIT(0);
3309 sio_data->internal &= ~BIT(3);
3310 sio_data->skip_in |= BIT(9);
3313 sio_data->beep_pin = superio_inb(sioaddr,
3314 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3315 } else if (sio_data->type == it8622) {
3318 superio_select(sioaddr, GPIO);
3320 /* Check for pwm4, fan4 */
3321 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3323 sio_data->skip_fan |= BIT(3);
3325 sio_data->skip_pwm |= BIT(3);
3327 /* Check for pwm3, fan3, pwm5, fan5 */
3328 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3330 sio_data->skip_pwm |= BIT(2);
3332 sio_data->skip_fan |= BIT(2);
3334 sio_data->skip_pwm |= BIT(4);
3336 sio_data->skip_fan |= BIT(4);
3338 /* Check for pwm2, fan2 */
3339 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3341 sio_data->skip_pwm |= BIT(1);
3343 sio_data->skip_fan |= BIT(1);
3345 /* Check for AVCC */
3346 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3347 if (!(reg & BIT(0)))
3348 sio_data->skip_in |= BIT(9);
3350 sio_data->beep_pin = superio_inb(sioaddr,
3351 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3352 } else if (sio_data->type == it8732) {
3355 superio_select(sioaddr, GPIO);
3357 /* Check for pwm2, fan2 */
3358 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3360 sio_data->skip_pwm |= BIT(1);
3362 sio_data->skip_fan |= BIT(1);
3364 /* Check for pwm3, fan3, fan4 */
3365 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3367 sio_data->skip_pwm |= BIT(2);
3369 sio_data->skip_fan |= BIT(2);
3371 sio_data->skip_fan |= BIT(3);
3373 /* Check if AVCC is on VIN3 */
3374 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3376 sio_data->internal |= BIT(0);
3378 sio_data->beep_pin = superio_inb(sioaddr,
3379 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3380 } else if (sio_data->type == it8655) {
3383 superio_select(sioaddr, GPIO);
3385 /* Check for pwm2 */
3386 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3388 sio_data->skip_pwm |= BIT(1);
3390 /* Check for fan2 */
3391 reg = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3393 sio_data->skip_fan |= BIT(1);
3395 /* Check for pwm3, fan3 */
3396 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3398 sio_data->skip_pwm |= BIT(2);
3400 sio_data->skip_fan |= BIT(2);
3402 sio_data->beep_pin = superio_inb(sioaddr,
3403 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3404 } else if (sio_data->type == it8665 || sio_data->type == it8625) {
3405 int reg27, reg29, reg2d, regd3;
3407 superio_select(sioaddr, GPIO);
3409 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3410 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3411 reg2d = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3412 regd3 = superio_inb(sioaddr, IT87_SIO_GPIO9_REG);
3414 /* Check for pwm2, fan2 */
3416 sio_data->skip_pwm |= BIT(1);
3418 * Note: Table 6-1 in datasheet claims that FAN_TAC2
3419 * would be enabled with 29h[2]=0.
3422 sio_data->skip_fan |= BIT(1);
3424 /* Check for pwm3, fan3 */
3426 sio_data->skip_pwm |= BIT(2);
3428 sio_data->skip_fan |= BIT(2);
3430 /* Check for pwm4, fan4, pwm5, fan5 */
3431 if (sio_data->type == it8625) {
3432 int reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3435 sio_data->skip_fan |= BIT(3);
3437 sio_data->skip_pwm |= BIT(3);
3439 sio_data->skip_pwm |= BIT(4);
3441 sio_data->skip_fan |= BIT(4);
3443 int reg26 = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3446 sio_data->skip_pwm |= BIT(3);
3448 sio_data->skip_fan |= BIT(3);
3450 sio_data->skip_pwm |= BIT(4);
3452 sio_data->skip_fan |= BIT(4);
3455 /* Check for pwm6, fan6 */
3457 sio_data->skip_pwm |= BIT(5);
3459 sio_data->skip_fan |= BIT(5);
3461 sio_data->beep_pin = superio_inb(sioaddr,
3462 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3467 superio_select(sioaddr, GPIO);
3469 /* Check for fan4, fan5 */
3470 if (has_five_fans(config)) {
3471 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3472 switch (sio_data->type) {
3475 sio_data->skip_fan |= BIT(3);
3477 sio_data->skip_fan |= BIT(4);
3482 if (!(reg & BIT(5)))
3483 sio_data->skip_fan |= BIT(3);
3484 if (!(reg & BIT(4)))
3485 sio_data->skip_fan |= BIT(4);
3492 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3493 if (!sio_data->skip_vid) {
3494 /* We need at least 4 VID pins */
3496 pr_info("VID is disabled (pins used for GPIO)\n");
3497 sio_data->skip_vid = 1;
3501 /* Check if fan3 is there or not */
3503 sio_data->skip_pwm |= BIT(2);
3505 sio_data->skip_fan |= BIT(2);
3507 /* Check if fan2 is there or not */
3508 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3510 sio_data->skip_pwm |= BIT(1);
3512 sio_data->skip_fan |= BIT(1);
3514 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
3515 !(sio_data->skip_vid))
3516 sio_data->vid_value = superio_inb(sioaddr,
3519 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3521 uart6 = sio_data->type == it8782 && (reg & BIT(2));
3524 * The IT8720F has no VIN7 pin, so VCCH should always be
3525 * routed internally to VIN7 with an internal divider.
3526 * Curiously, there still is a configuration bit to control
3527 * this, which means it can be set incorrectly. And even
3528 * more curiously, many boards out there are improperly
3529 * configured, even though the IT8720F datasheet claims
3530 * that the internal routing of VCCH to VIN7 is the default
3531 * setting. So we force the internal routing in this case.
3533 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
3534 * If UART6 is enabled, re-route VIN7 to the internal divider
3535 * if that is not already the case.
3537 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
3539 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
3540 pr_notice("Routing internal VCCH to in7\n");
3543 sio_data->internal |= BIT(0);
3545 sio_data->internal |= BIT(1);
3548 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
3549 * While VIN7 can be routed to the internal voltage divider,
3550 * VIN5 and VIN6 are not available if UART6 is enabled.
3552 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
3553 * is the temperature source. Since we can not read the
3554 * temperature source here, skip_temp is preliminary.
3557 sio_data->skip_in |= BIT(5) | BIT(6);
3558 sio_data->skip_temp |= BIT(2);
3561 sio_data->beep_pin = superio_inb(sioaddr,
3562 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3564 if (sio_data->beep_pin)
3565 pr_info("Beeping is supported\n");
3567 if (config->smbus_bitmap && !base) {
3570 superio_select(sioaddr, PME);
3571 reg = superio_inb(sioaddr, IT87_SPECIAL_CFG_REG);
3572 sio_data->ec_special_config = reg;
3573 sio_data->smbus_bitmap = reg & config->smbus_bitmap;
3577 superio_exit(sioaddr, doexit);
3581 static void it87_init_regs(struct platform_device *pdev)
3583 struct it87_data *data = platform_get_drvdata(pdev);
3585 /* Initialize chip specific register pointers */
3586 switch (data->type) {
3589 data->REG_FAN = IT87_REG_FAN;
3590 data->REG_FANX = IT87_REG_FANX;
3591 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3592 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3593 data->REG_PWM = IT87_REG_PWM;
3594 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET_8686;
3595 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW_8686;
3596 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH_8686;
3601 data->REG_FAN = IT87_REG_FAN_8665;
3602 data->REG_FANX = IT87_REG_FANX_8665;
3603 data->REG_FAN_MIN = IT87_REG_FAN_MIN_8665;
3604 data->REG_FANX_MIN = IT87_REG_FANX_MIN_8665;
3605 data->REG_PWM = IT87_REG_PWM_8665;
3606 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3607 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3608 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3611 data->REG_FAN = IT87_REG_FAN;
3612 data->REG_FANX = IT87_REG_FANX;
3613 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3614 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3615 data->REG_PWM = IT87_REG_PWM_8665;
3616 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3617 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3618 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3621 data->REG_FAN = IT87_REG_FAN;
3622 data->REG_FANX = IT87_REG_FANX;
3623 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3624 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
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;
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;
3643 data->read = it87_mmio_read;
3644 data->write = it87_mmio_write;
3645 } else if (has_bank_sel(data)) {
3646 data->read = it87_io_read;
3647 data->write = it87_io_write;
3649 data->read = _it87_io_read;
3650 data->write = _it87_io_write;
3654 /* Called when we have found a new IT87. */
3655 static void it87_init_device(struct platform_device *pdev)
3657 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
3658 struct it87_data *data = platform_get_drvdata(pdev);
3662 if (has_new_tempmap(data)) {
3663 data->pwm_temp_map_shift = 3;
3664 data->pwm_temp_map_mask = 0x07;
3666 data->pwm_temp_map_shift = 0;
3667 data->pwm_temp_map_mask = 0x03;
3671 * For each PWM channel:
3672 * - If it is in automatic mode, setting to manual mode should set
3673 * the fan to full speed by default.
3674 * - If it is in manual mode, we need a mapping to temperature
3675 * channels to use when later setting to automatic mode later.
3676 * Map to the first sensor by default (we are clueless.)
3677 * In both cases, the value can (and should) be changed by the user
3678 * prior to switching to a different mode.
3679 * Note that this is no longer needed for the IT8721F and later, as
3680 * these have separate registers for the temperature mapping and the
3681 * manual duty cycle.
3683 for (i = 0; i < NUM_AUTO_PWM; i++) {
3684 data->pwm_temp_map[i] = 0;
3685 data->pwm_duty[i] = 0x7f; /* Full speed */
3686 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
3690 * Some chips seem to have default value 0xff for all limit
3691 * registers. For low voltage limits it makes no sense and triggers
3692 * alarms, so change to 0 instead. For high temperature limits, it
3693 * means -1 degree C, which surprisingly doesn't trigger an alarm,
3694 * but is still confusing, so change to 127 degrees C.
3696 for (i = 0; i < NUM_VIN_LIMIT; i++) {
3697 tmp = data->read(data, IT87_REG_VIN_MIN(i));
3699 data->write(data, IT87_REG_VIN_MIN(i), 0);
3701 for (i = 0; i < data->num_temp_limit; i++) {
3702 tmp = data->read(data, data->REG_TEMP_HIGH[i]);
3704 data->write(data, data->REG_TEMP_HIGH[i], 127);
3708 * Temperature channels are not forcibly enabled, as they can be
3709 * set to two different sensor types and we can't guess which one
3710 * is correct for a given system. These channels can be enabled at
3711 * run-time through the temp{1-3}_type sysfs accessors if needed.
3714 /* Check if voltage monitors are reset manually or by some reason */
3715 tmp = data->read(data, IT87_REG_VIN_ENABLE);
3716 if ((tmp & 0xff) == 0) {
3717 /* Enable all voltage monitors */
3718 data->write(data, IT87_REG_VIN_ENABLE, 0xff);
3721 /* Check if tachometers are reset manually or by some reason */
3722 mask = 0x70 & ~(sio_data->skip_fan << 4);
3723 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
3724 if ((data->fan_main_ctrl & mask) == 0) {
3725 /* Enable all fan tachometers */
3726 data->fan_main_ctrl |= mask;
3727 data->write(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
3729 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
3731 tmp = data->read(data, IT87_REG_FAN_16BIT);
3733 /* Set tachometers to 16-bit mode if needed */
3734 if (has_fan16_config(data)) {
3735 if (~tmp & 0x07 & data->has_fan) {
3737 "Setting fan1-3 to 16-bit mode\n");
3738 data->write(data, IT87_REG_FAN_16BIT, tmp | 0x07);
3742 /* Check for additional fans */
3743 if (has_four_fans(data) && (tmp & BIT(4)))
3744 data->has_fan |= BIT(3); /* fan4 enabled */
3745 if (has_five_fans(data) && (tmp & BIT(5)))
3746 data->has_fan |= BIT(4); /* fan5 enabled */
3747 if (has_six_fans(data)) {
3748 switch (data->type) {
3753 data->has_fan |= BIT(5); /* fan6 enabled */
3757 tmp = data->read(data, IT87_REG_FAN_DIV);
3759 data->has_fan |= BIT(5); /* fan6 enabled */
3766 /* Fan input pins may be used for alternative functions */
3767 data->has_fan &= ~sio_data->skip_fan;
3769 /* Check if pwm6 is enabled */
3770 if (has_six_pwm(data)) {
3771 switch (data->type) {
3774 tmp = data->read(data, IT87_REG_FAN_DIV);
3775 if (!(tmp & BIT(3)))
3776 sio_data->skip_pwm |= BIT(5);
3783 if (has_bank_sel(data)) {
3784 for (i = 0; i < 3; i++)
3786 data->read(data, IT87_REG_TEMP_SRC1[i]);
3787 data->temp_src[3] = data->read(data, IT87_REG_TEMP_SRC2);
3790 /* Start monitoring */
3791 data->write(data, IT87_REG_CONFIG,
3792 (data->read(data, IT87_REG_CONFIG) & 0x3e) |
3793 (update_vbat ? 0x41 : 0x01));
3796 /* Return 1 if and only if the PWM interface is safe to use */
3797 static int it87_check_pwm(struct device *dev)
3799 struct it87_data *data = dev_get_drvdata(dev);
3801 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
3802 * and polarity set to active low is sign that this is the case so we
3803 * disable pwm control to protect the user.
3805 int tmp = data->read(data, IT87_REG_FAN_CTL);
3807 if ((tmp & 0x87) == 0) {
3808 if (fix_pwm_polarity) {
3810 * The user asks us to attempt a chip reconfiguration.
3811 * This means switching to active high polarity and
3812 * inverting all fan speed values.
3817 for (i = 0; i < ARRAY_SIZE(pwm); i++)
3818 pwm[i] = data->read(data,
3822 * If any fan is in automatic pwm mode, the polarity
3823 * might be correct, as suspicious as it seems, so we
3824 * better don't change anything (but still disable the
3827 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3829 "Reconfiguring PWM to active high polarity\n");
3830 data->write(data, IT87_REG_FAN_CTL, tmp | 0x87);
3831 for (i = 0; i < 3; i++)
3832 data->write(data, data->REG_PWM[i],
3838 "PWM configuration is too broken to be fixed\n");
3842 "Detected broken BIOS defaults, disabling PWM interface\n");
3844 } else if (fix_pwm_polarity) {
3846 "PWM configuration looks sane, won't touch\n");
3852 static int it87_probe(struct platform_device *pdev)
3854 struct it87_data *data;
3855 struct resource *res;
3856 struct device *dev = &pdev->dev;
3857 struct it87_sio_data *sio_data = dev_get_platdata(dev);
3858 int enable_pwm_interface;
3859 struct device *hwmon_dev;
3862 data = devm_kzalloc(dev, sizeof(struct it87_data), GFP_KERNEL);
3866 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3868 if (!devm_request_region(dev, res->start, IT87_EC_EXTENT,
3870 dev_err(dev, "Failed to request region %pR\n", res);
3874 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3875 data->mmio = devm_ioremap_resource(dev, res);
3876 if (IS_ERR(data->mmio))
3877 return PTR_ERR(data->mmio);
3880 data->addr = res->start;
3881 data->type = sio_data->type;
3882 data->sioaddr = sio_data->sioaddr;
3883 data->smbus_bitmap = sio_data->smbus_bitmap;
3884 data->ec_special_config = sio_data->ec_special_config;
3885 data->doexit = sio_data->doexit;
3886 data->features = it87_devices[sio_data->type].features;
3887 data->num_temp_limit = it87_devices[sio_data->type].num_temp_limit;
3888 data->num_temp_offset = it87_devices[sio_data->type].num_temp_offset;
3889 data->pwm_num_temp_map = it87_devices[sio_data->type].num_temp_map;
3890 data->peci_mask = it87_devices[sio_data->type].peci_mask;
3891 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3894 * IT8705F Datasheet 0.4.1, 3h == Version G.
3895 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3896 * These are the first revisions with 16-bit tachometer support.
3898 switch (data->type) {
3900 if (sio_data->revision >= 0x03) {
3901 data->features &= ~FEAT_OLD_AUTOPWM;
3902 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3906 if (sio_data->revision >= 0x08) {
3907 data->features &= ~FEAT_OLD_AUTOPWM;
3908 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3916 platform_set_drvdata(pdev, data);
3918 mutex_init(&data->update_lock);
3920 /* Initialize register pointers */
3921 it87_init_regs(pdev);
3923 err = smbus_disable(data);
3927 /* Now, we do the remaining detection. */
3928 if ((data->read(data, IT87_REG_CONFIG) & 0x80) ||
3929 data->read(data, IT87_REG_CHIPID) != 0x90) {
3934 /* Check PWM configuration */
3935 enable_pwm_interface = it87_check_pwm(dev);
3937 /* Starting with IT8721F, we handle scaling of internal voltages */
3938 if (has_scaling(data)) {
3939 if (sio_data->internal & BIT(0))
3940 data->in_scaled |= BIT(3); /* in3 is AVCC */
3941 if (sio_data->internal & BIT(1))
3942 data->in_scaled |= BIT(7); /* in7 is VSB */
3943 if (sio_data->internal & BIT(2))
3944 data->in_scaled |= BIT(8); /* in8 is Vbat */
3945 if (sio_data->internal & BIT(3))
3946 data->in_scaled |= BIT(9); /* in9 is AVCC */
3947 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3948 sio_data->type == it8783) {
3949 if (sio_data->internal & BIT(0))
3950 data->in_scaled |= BIT(3); /* in3 is VCC5V */
3951 if (sio_data->internal & BIT(1))
3952 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
3955 data->has_temp = 0x07;
3956 if (sio_data->skip_temp & BIT(2)) {
3957 if (sio_data->type == it8782 &&
3958 !(data->read(data, IT87_REG_TEMP_EXTRA) & 0x80))
3959 data->has_temp &= ~BIT(2);
3962 data->in_internal = sio_data->internal;
3963 data->has_in = 0x3ff & ~sio_data->skip_in;
3965 if (has_four_temp(data)) {
3966 data->has_temp |= BIT(3);
3967 } else if (has_six_temp(data)) {
3968 u8 reg = data->read(data, IT87_REG_TEMP456_ENABLE);
3970 /* Check for additional temperature sensors */
3971 if ((reg & 0x03) >= 0x02)
3972 data->has_temp |= BIT(3);
3973 if (((reg >> 2) & 0x03) >= 0x02)
3974 data->has_temp |= BIT(4);
3975 if (((reg >> 4) & 0x03) >= 0x02)
3976 data->has_temp |= BIT(5);
3978 /* Check for additional voltage sensors */
3979 if ((reg & 0x03) == 0x01)
3980 data->has_in |= BIT(10);
3981 if (((reg >> 2) & 0x03) == 0x01)
3982 data->has_in |= BIT(11);
3983 if (((reg >> 4) & 0x03) == 0x01)
3984 data->has_in |= BIT(12);
3987 data->has_beep = !!sio_data->beep_pin;
3989 /* Initialize the IT87 chip */
3990 it87_init_device(pdev);
3994 if (!sio_data->skip_vid) {
3995 data->has_vid = true;
3996 data->vrm = vid_which_vrm();
3997 /* VID reading from Super-I/O config space if available */
3998 data->vid = sio_data->vid_value;
4001 /* Prepare for sysfs hooks */
4002 data->groups[0] = &it87_group;
4003 data->groups[1] = &it87_group_in;
4004 data->groups[2] = &it87_group_temp;
4005 data->groups[3] = &it87_group_fan;
4007 if (enable_pwm_interface) {
4008 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
4009 data->has_pwm &= ~sio_data->skip_pwm;
4011 data->groups[4] = &it87_group_pwm;
4012 if (has_old_autopwm(data) || has_newer_autopwm(data))
4013 data->groups[5] = &it87_group_auto_pwm;
4016 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
4017 it87_devices[sio_data->type].name,
4018 data, data->groups);
4019 return PTR_ERR_OR_ZERO(hwmon_dev);
4022 static struct platform_driver it87_driver = {
4026 .probe = it87_probe,
4029 static int __init it87_device_add(int index, unsigned short sio_address,
4030 phys_addr_t mmio_address,
4031 const struct it87_sio_data *sio_data)
4033 struct platform_device *pdev;
4034 struct resource res = {
4040 res.start = mmio_address;
4041 res.end = mmio_address + 0x400 - 1;
4042 res.flags = IORESOURCE_MEM;
4044 res.start = sio_address + IT87_EC_OFFSET;
4045 res.end = sio_address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1;
4046 res.flags = IORESOURCE_IO;
4049 err = acpi_check_resource_conflict(&res);
4051 if (!ignore_resource_conflict)
4055 pdev = platform_device_alloc(DRVNAME, sio_address);
4059 err = platform_device_add_resources(pdev, &res, 1);
4061 pr_err("Device resource addition failed (%d)\n", err);
4062 goto exit_device_put;
4065 err = platform_device_add_data(pdev, sio_data,
4066 sizeof(struct it87_sio_data));
4068 pr_err("Platform data allocation failed\n");
4069 goto exit_device_put;
4072 err = platform_device_add(pdev);
4074 pr_err("Device addition failed (%d)\n", err);
4075 goto exit_device_put;
4078 it87_pdev[index] = pdev;
4082 platform_device_put(pdev);
4086 struct it87_dmi_data {
4087 bool sio2_force_config; /* force sio2 into configuration mode */
4088 u8 skip_pwm; /* pwm channels to skip for this board */
4092 * On various Gigabyte AM4 boards (AB350, AX370), the second Super-IO chip
4093 * (IT8792E) needs to be in configuration mode before accessing the first
4094 * due to a bug in IT8792E which otherwise results in LPC bus access errors.
4095 * This needs to be done before accessing the first Super-IO chip since
4096 * the second chip may have been accessed prior to loading this driver.
4098 * The problem is also reported to affect IT8795E, which is used on X299 boards
4099 * and has the same chip ID as IT8792E (0x8733). It also appears to affect
4100 * systems with IT8790E, which is used on some Z97X-Gaming boards as well as
4102 * DMI entries for those systems will be added as they become available and
4103 * as the problem is confirmed to affect those boards.
4105 static struct it87_dmi_data gigabyte_sio2_force = {
4106 .sio2_force_config = true,
4110 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
4111 * connected to a fan, but to something else. One user
4112 * has reported instant system power-off when changing
4113 * the PWM2 duty cycle, so we disable it.
4114 * I use the board name string as the trigger in case
4115 * the same board is ever used in other systems.
4117 static struct it87_dmi_data nvidia_fn68pt = {
4121 static const struct dmi_system_id it87_dmi_table[] __initconst = {
4124 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4125 DMI_MATCH(DMI_BOARD_NAME, "AB350"),
4127 .driver_data = &gigabyte_sio2_force,
4131 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4132 DMI_MATCH(DMI_BOARD_NAME, "AX370"),
4134 .driver_data = &gigabyte_sio2_force,
4138 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4139 DMI_MATCH(DMI_BOARD_NAME, "Z97X-Gaming G1"),
4141 .driver_data = &gigabyte_sio2_force,
4145 DMI_MATCH(DMI_BOARD_VENDOR, "nVIDIA"),
4146 DMI_MATCH(DMI_BOARD_NAME, "FN68PT"),
4148 .driver_data = &nvidia_fn68pt,
4153 static int __init sm_it87_init(void)
4155 const struct dmi_system_id *dmi = dmi_first_match(it87_dmi_table);
4156 struct it87_dmi_data *dmi_data = NULL;
4157 int sioaddr[2] = { REG_2E, REG_4E };
4158 struct it87_sio_data sio_data;
4159 unsigned short isa_address;
4160 phys_addr_t mmio_address;
4164 pr_info("it87 driver version %s\n", IT87_DRIVER_VERSION);
4167 dmi_data = dmi->driver_data;
4169 err = platform_driver_register(&it87_driver);
4173 if (dmi_data && dmi_data->sio2_force_config)
4174 __superio_enter(REG_4E);
4176 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
4177 memset(&sio_data, 0, sizeof(struct it87_sio_data));
4180 err = it87_find(sioaddr[i], &isa_address, &mmio_address,
4182 if (err || isa_address == 0)
4186 sio_data.skip_pwm |= dmi_data->skip_pwm;
4187 err = it87_device_add(i, isa_address, mmio_address, &sio_data);
4189 goto exit_dev_unregister;
4195 goto exit_unregister;
4199 exit_dev_unregister:
4200 /* NULL check handled by platform_device_unregister */
4201 platform_device_unregister(it87_pdev[0]);
4203 platform_driver_unregister(&it87_driver);
4207 static void __exit sm_it87_exit(void)
4209 /* NULL check handled by platform_device_unregister */
4210 platform_device_unregister(it87_pdev[1]);
4211 platform_device_unregister(it87_pdev[0]);
4212 platform_driver_unregister(&it87_driver);
4215 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
4216 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
4217 module_param(update_vbat, bool, 0000);
4218 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
4219 module_param(fix_pwm_polarity, bool, 0000);
4220 MODULE_PARM_DESC(fix_pwm_polarity,
4221 "Force PWM polarity to active high (DANGEROUS)");
4222 MODULE_LICENSE("GPL");
4224 module_init(sm_it87_init);
4225 module_exit(sm_it87_exit);