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,
1162 data->REG_FAN_MIN[i]);
1163 data->fan[i][0] = data->read(data, data->REG_FAN[i]);
1164 /* Add high byte if in 16-bit mode */
1165 if (has_16bit_fans(data)) {
1166 data->fan[i][0] |= data->read(data,
1167 data->REG_FANX[i]) << 8;
1168 data->fan[i][1] |= data->read(data,
1169 data->REG_FANX_MIN[i]) << 8;
1172 for (i = 0; i < NUM_TEMP; i++) {
1173 if (!(data->has_temp & BIT(i)))
1176 data->read(data, IT87_REG_TEMP(i));
1178 if (i >= data->num_temp_limit)
1181 if (i < data->num_temp_offset)
1183 data->read(data, data->REG_TEMP_OFFSET[i]);
1186 data->read(data, data->REG_TEMP_LOW[i]);
1188 data->read(data, data->REG_TEMP_HIGH[i]);
1191 /* Newer chips don't have clock dividers */
1192 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1193 i = data->read(data, IT87_REG_FAN_DIV);
1194 data->fan_div[0] = i & 0x07;
1195 data->fan_div[1] = (i >> 3) & 0x07;
1196 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1200 data->read(data, IT87_REG_ALARM1) |
1201 (data->read(data, IT87_REG_ALARM2) << 8) |
1202 (data->read(data, IT87_REG_ALARM3) << 16);
1203 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
1205 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
1206 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL);
1207 for (i = 0; i < NUM_PWM; i++) {
1208 if (!(data->has_pwm & BIT(i)))
1210 it87_update_pwm_ctrl(data, i);
1213 data->sensor = data->read(data, IT87_REG_TEMP_ENABLE);
1214 data->extra = data->read(data, IT87_REG_TEMP_EXTRA);
1216 * The IT8705F does not have VID capability.
1217 * The IT8718F and later don't use IT87_REG_VID for the
1220 if (data->type == it8712 || data->type == it8716) {
1221 data->vid = data->read(data, IT87_REG_VID);
1223 * The older IT8712F revisions had only 5 VID pins,
1224 * but we assume it is always safe to read 6 bits.
1228 data->last_updated = jiffies;
1235 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
1238 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1239 struct it87_data *data = it87_update_device(dev);
1240 int index = sattr->index;
1244 return PTR_ERR(data);
1246 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
1249 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
1250 const char *buf, size_t count)
1252 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1253 struct it87_data *data = dev_get_drvdata(dev);
1254 int index = sattr->index;
1259 if (kstrtoul(buf, 10, &val) < 0)
1262 err = it87_lock(data);
1266 data->in[nr][index] = in_to_reg(data, nr, val);
1267 data->write(data, index == 1 ? IT87_REG_VIN_MIN(nr)
1268 : IT87_REG_VIN_MAX(nr),
1269 data->in[nr][index]);
1274 static SENSOR_DEVICE_ATTR_2(in0_input, 0444, show_in, NULL, 0, 0);
1275 static SENSOR_DEVICE_ATTR_2(in0_min, 0644, show_in, set_in, 0, 1);
1276 static SENSOR_DEVICE_ATTR_2(in0_max, 0644, show_in, set_in, 0, 2);
1278 static SENSOR_DEVICE_ATTR_2(in1_input, 0444, show_in, NULL, 1, 0);
1279 static SENSOR_DEVICE_ATTR_2(in1_min, 0644, show_in, set_in, 1, 1);
1280 static SENSOR_DEVICE_ATTR_2(in1_max, 0644, show_in, set_in, 1, 2);
1282 static SENSOR_DEVICE_ATTR_2(in2_input, 0444, show_in, NULL, 2, 0);
1283 static SENSOR_DEVICE_ATTR_2(in2_min, 0644, show_in, set_in, 2, 1);
1284 static SENSOR_DEVICE_ATTR_2(in2_max, 0644, show_in, set_in, 2, 2);
1286 static SENSOR_DEVICE_ATTR_2(in3_input, 0444, show_in, NULL, 3, 0);
1287 static SENSOR_DEVICE_ATTR_2(in3_min, 0644, show_in, set_in, 3, 1);
1288 static SENSOR_DEVICE_ATTR_2(in3_max, 0644, show_in, set_in, 3, 2);
1290 static SENSOR_DEVICE_ATTR_2(in4_input, 0444, show_in, NULL, 4, 0);
1291 static SENSOR_DEVICE_ATTR_2(in4_min, 0644, show_in, set_in, 4, 1);
1292 static SENSOR_DEVICE_ATTR_2(in4_max, 0644, show_in, set_in, 4, 2);
1294 static SENSOR_DEVICE_ATTR_2(in5_input, 0444, show_in, NULL, 5, 0);
1295 static SENSOR_DEVICE_ATTR_2(in5_min, 0644, show_in, set_in, 5, 1);
1296 static SENSOR_DEVICE_ATTR_2(in5_max, 0644, show_in, set_in, 5, 2);
1298 static SENSOR_DEVICE_ATTR_2(in6_input, 0444, show_in, NULL, 6, 0);
1299 static SENSOR_DEVICE_ATTR_2(in6_min, 0644, show_in, set_in, 6, 1);
1300 static SENSOR_DEVICE_ATTR_2(in6_max, 0644, show_in, set_in, 6, 2);
1302 static SENSOR_DEVICE_ATTR_2(in7_input, 0444, show_in, NULL, 7, 0);
1303 static SENSOR_DEVICE_ATTR_2(in7_min, 0644, show_in, set_in, 7, 1);
1304 static SENSOR_DEVICE_ATTR_2(in7_max, 0644, show_in, set_in, 7, 2);
1306 static SENSOR_DEVICE_ATTR_2(in8_input, 0444, show_in, NULL, 8, 0);
1307 static SENSOR_DEVICE_ATTR_2(in9_input, 0444, show_in, NULL, 9, 0);
1308 static SENSOR_DEVICE_ATTR_2(in10_input, 0444, show_in, NULL, 10, 0);
1309 static SENSOR_DEVICE_ATTR_2(in11_input, 0444, show_in, NULL, 11, 0);
1310 static SENSOR_DEVICE_ATTR_2(in12_input, 0444, show_in, NULL, 12, 0);
1312 /* Up to 6 temperatures */
1313 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
1316 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1318 int index = sattr->index;
1319 struct it87_data *data = it87_update_device(dev);
1322 return PTR_ERR(data);
1324 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
1327 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
1328 const char *buf, size_t count)
1330 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1332 int index = sattr->index;
1333 struct it87_data *data = dev_get_drvdata(dev);
1338 if (kstrtol(buf, 10, &val) < 0)
1341 err = it87_lock(data);
1348 reg = data->REG_TEMP_LOW[nr];
1351 reg = data->REG_TEMP_HIGH[nr];
1354 regval = data->read(data, IT87_REG_BEEP_ENABLE);
1355 if (!(regval & 0x80)) {
1357 data->write(data, IT87_REG_BEEP_ENABLE, regval);
1360 reg = data->REG_TEMP_OFFSET[nr];
1364 data->temp[nr][index] = TEMP_TO_REG(val);
1365 data->write(data, reg, data->temp[nr][index]);
1370 static SENSOR_DEVICE_ATTR_2(temp1_input, 0444, show_temp, NULL, 0, 0);
1371 static SENSOR_DEVICE_ATTR_2(temp1_min, 0644, show_temp, set_temp, 0, 1);
1372 static SENSOR_DEVICE_ATTR_2(temp1_max, 0644, show_temp, set_temp, 0, 2);
1373 static SENSOR_DEVICE_ATTR_2(temp1_offset, 0644, show_temp, set_temp, 0, 3);
1374 static SENSOR_DEVICE_ATTR_2(temp2_input, 0444, show_temp, NULL, 1, 0);
1375 static SENSOR_DEVICE_ATTR_2(temp2_min, 0644, show_temp, set_temp, 1, 1);
1376 static SENSOR_DEVICE_ATTR_2(temp2_max, 0644, show_temp, set_temp, 1, 2);
1377 static SENSOR_DEVICE_ATTR_2(temp2_offset, 0644, show_temp, set_temp, 1, 3);
1378 static SENSOR_DEVICE_ATTR_2(temp3_input, 0444, show_temp, NULL, 2, 0);
1379 static SENSOR_DEVICE_ATTR_2(temp3_min, 0644, show_temp, set_temp, 2, 1);
1380 static SENSOR_DEVICE_ATTR_2(temp3_max, 0644, show_temp, set_temp, 2, 2);
1381 static SENSOR_DEVICE_ATTR_2(temp3_offset, 0644, show_temp, set_temp, 2, 3);
1382 static SENSOR_DEVICE_ATTR_2(temp4_input, 0444, show_temp, NULL, 3, 0);
1383 static SENSOR_DEVICE_ATTR_2(temp4_min, 0644, show_temp, set_temp, 3, 1);
1384 static SENSOR_DEVICE_ATTR_2(temp4_max, 0644, show_temp, set_temp, 3, 2);
1385 static SENSOR_DEVICE_ATTR_2(temp4_offset, 0644, show_temp, set_temp, 3, 3);
1386 static SENSOR_DEVICE_ATTR_2(temp5_input, 0444, show_temp, NULL, 4, 0);
1387 static SENSOR_DEVICE_ATTR_2(temp5_min, 0644, show_temp, set_temp, 4, 1);
1388 static SENSOR_DEVICE_ATTR_2(temp5_max, 0644, show_temp, set_temp, 4, 2);
1389 static SENSOR_DEVICE_ATTR_2(temp5_offset, 0644, show_temp, set_temp, 4, 3);
1390 static SENSOR_DEVICE_ATTR_2(temp6_input, 0444, show_temp, NULL, 5, 0);
1391 static SENSOR_DEVICE_ATTR_2(temp6_min, 0644, show_temp, set_temp, 5, 1);
1392 static SENSOR_DEVICE_ATTR_2(temp6_max, 0644, show_temp, set_temp, 5, 2);
1393 static SENSOR_DEVICE_ATTR_2(temp6_offset, 0644, show_temp, set_temp, 5, 3);
1395 static const u8 temp_types_8686[NUM_TEMP][9] = {
1396 { 0, 8, 8, 8, 8, 8, 8, 8, 7 },
1397 { 0, 6, 8, 8, 6, 0, 0, 0, 7 },
1398 { 0, 6, 5, 8, 6, 0, 0, 0, 7 },
1399 { 4, 8, 8, 8, 8, 8, 8, 8, 7 },
1400 { 4, 6, 8, 8, 6, 0, 0, 0, 7 },
1401 { 4, 6, 5, 8, 6, 0, 0, 0, 7 },
1404 static int get_temp_type(struct it87_data *data, int index)
1407 int ttype, type = 0;
1409 if (has_bank_sel(data)) {
1412 src1 = (data->temp_src[index / 2] >> ((index % 2) * 4)) & 0x0f;
1414 switch (data->type) {
1417 type = temp_types_8686[index][src1];
1428 src2 = data->temp_src[3];
1431 type = (src2 & BIT(index)) ? 6 : 5;
1434 type = (src2 & BIT(index)) ? 4 : 6;
1437 type = (src2 & BIT(index)) ? 5 : 0;
1450 /* Dectect PECI vs. AMDTSI if possible */
1452 if ((has_temp_peci(data, index)) && data->type != it8721) {
1453 extra = data->read(data, 0x98); /* PCH/AMDTSI host status */
1458 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1460 /* Per chip special detection */
1461 switch (data->type) {
1463 if (!(reg & 0xc0) && index == 3)
1470 if (type || index >= 3)
1473 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1475 if ((has_temp_peci(data, index) && (reg >> 6 == index + 1)) ||
1476 (has_temp_old_peci(data, index) && (extra & 0x80)))
1477 type = ttype; /* Intel PECI or AMDTSI */
1478 if (reg & BIT(index))
1479 type = 3; /* thermal diode */
1480 else if (reg & BIT(index + 3))
1481 type = 4; /* thermistor */
1486 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1489 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1490 struct it87_data *data = it87_update_device(dev);
1494 return PTR_ERR(data);
1496 type = get_temp_type(data, sensor_attr->index);
1497 return sprintf(buf, "%d\n", type);
1500 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1501 const char *buf, size_t count)
1503 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1504 int nr = sensor_attr->index;
1506 struct it87_data *data = dev_get_drvdata(dev);
1511 if (kstrtol(buf, 10, &val) < 0)
1514 err = it87_lock(data);
1518 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1521 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1523 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1524 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1526 if (val == 2) { /* backwards compatibility */
1528 "Sensor type 2 is deprecated, please use 4 instead\n");
1531 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1536 else if (has_temp_peci(data, nr) && val == 6)
1537 reg |= (nr + 1) << 6;
1538 else if (has_temp_old_peci(data, nr) && val == 6)
1540 else if (val != 0) {
1546 data->extra = extra;
1547 data->write(data, IT87_REG_TEMP_ENABLE, data->sensor);
1548 if (has_temp_old_peci(data, nr))
1549 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1550 data->valid = 0; /* Force cache refresh */
1556 static SENSOR_DEVICE_ATTR(temp1_type, 0644, show_temp_type, set_temp_type, 0);
1557 static SENSOR_DEVICE_ATTR(temp2_type, 0644, show_temp_type, set_temp_type, 1);
1558 static SENSOR_DEVICE_ATTR(temp3_type, 0644, show_temp_type, set_temp_type, 2);
1559 static SENSOR_DEVICE_ATTR(temp4_type, 0644, show_temp_type, set_temp_type, 3);
1560 static SENSOR_DEVICE_ATTR(temp5_type, 0644, show_temp_type, set_temp_type, 4);
1561 static SENSOR_DEVICE_ATTR(temp6_type, 0644, show_temp_type, set_temp_type, 5);
1565 static int pwm_mode(const struct it87_data *data, int nr)
1567 if (has_fanctl_onoff(data) && nr < 3 &&
1568 !(data->fan_main_ctrl & BIT(nr)))
1569 return 0; /* Full speed */
1570 if (data->pwm_ctrl[nr] & 0x80)
1571 return 2; /* Automatic mode */
1572 if ((!has_fanctl_onoff(data) || nr >= 3) &&
1573 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1574 return 0; /* Full speed */
1576 return 1; /* Manual mode */
1579 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1582 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1584 int index = sattr->index;
1586 struct it87_data *data = it87_update_device(dev);
1589 return PTR_ERR(data);
1591 speed = has_16bit_fans(data) ?
1592 FAN16_FROM_REG(data->fan[nr][index]) :
1593 FAN_FROM_REG(data->fan[nr][index],
1594 DIV_FROM_REG(data->fan_div[nr]));
1595 return sprintf(buf, "%d\n", speed);
1598 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1601 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1602 struct it87_data *data = it87_update_device(dev);
1603 int nr = sensor_attr->index;
1606 return PTR_ERR(data);
1608 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1611 static ssize_t show_pwm_enable(struct device *dev,
1612 struct device_attribute *attr, char *buf)
1614 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1615 struct it87_data *data = it87_update_device(dev);
1616 int nr = sensor_attr->index;
1619 return PTR_ERR(data);
1621 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1624 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1627 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1628 struct it87_data *data = it87_update_device(dev);
1629 int nr = sensor_attr->index;
1632 return PTR_ERR(data);
1634 return sprintf(buf, "%d\n",
1635 pwm_from_reg(data, data->pwm_duty[nr]));
1638 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1641 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1642 struct it87_data *data = it87_update_device(dev);
1643 int nr = sensor_attr->index;
1648 return PTR_ERR(data);
1650 if (has_pwm_freq2(data) && nr == 1)
1651 index = (data->extra >> 4) & 0x07;
1653 index = (data->fan_ctl >> 4) & 0x07;
1655 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1657 return sprintf(buf, "%u\n", freq);
1660 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1661 const char *buf, size_t count)
1663 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1665 int index = sattr->index;
1667 struct it87_data *data = dev_get_drvdata(dev);
1672 if (kstrtol(buf, 10, &val) < 0)
1675 err = it87_lock(data);
1679 if (has_16bit_fans(data)) {
1680 data->fan[nr][index] = FAN16_TO_REG(val);
1681 data->write(data, data->REG_FAN_MIN[nr],
1682 data->fan[nr][index] & 0xff);
1683 data->write(data, data->REG_FANX_MIN[nr],
1684 data->fan[nr][index] >> 8);
1686 reg = data->read(data, IT87_REG_FAN_DIV);
1689 data->fan_div[nr] = reg & 0x07;
1692 data->fan_div[nr] = (reg >> 3) & 0x07;
1695 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1698 data->fan[nr][index] =
1699 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1700 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][index]);
1706 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1707 const char *buf, size_t count)
1709 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1710 struct it87_data *data = dev_get_drvdata(dev);
1711 int nr = sensor_attr->index;
1716 if (kstrtoul(buf, 10, &val) < 0)
1719 err = it87_lock(data);
1723 old = data->read(data, IT87_REG_FAN_DIV);
1725 /* Save fan min limit */
1726 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1731 data->fan_div[nr] = DIV_TO_REG(val);
1735 data->fan_div[nr] = 1;
1737 data->fan_div[nr] = 3;
1740 val |= (data->fan_div[0] & 0x07);
1741 val |= (data->fan_div[1] & 0x07) << 3;
1742 if (data->fan_div[2] == 3)
1744 data->write(data, IT87_REG_FAN_DIV, val);
1746 /* Restore fan min limit */
1747 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1748 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][1]);
1753 /* Returns 0 if OK, -EINVAL otherwise */
1754 static int check_trip_points(struct device *dev, int nr)
1756 const struct it87_data *data = dev_get_drvdata(dev);
1759 if (has_old_autopwm(data)) {
1760 for (i = 0; i < 3; i++) {
1761 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1764 for (i = 0; i < 2; i++) {
1765 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1768 } else if (has_newer_autopwm(data)) {
1769 for (i = 1; i < 3; i++) {
1770 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1777 "Inconsistent trip points, not switching to automatic mode\n");
1778 dev_err(dev, "Adjust the trip points and try again\n");
1783 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1784 const char *buf, size_t count)
1786 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1787 struct it87_data *data = dev_get_drvdata(dev);
1788 int nr = sensor_attr->index;
1792 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1795 /* Check trip points before switching to automatic mode */
1797 if (check_trip_points(dev, nr) < 0)
1801 err = it87_lock(data);
1805 it87_update_pwm_ctrl(data, nr);
1808 if (nr < 3 && has_fanctl_onoff(data)) {
1810 /* make sure the fan is on when in on/off mode */
1811 tmp = data->read(data, IT87_REG_FAN_CTL);
1812 data->write(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1813 /* set on/off mode */
1814 data->fan_main_ctrl &= ~BIT(nr);
1815 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1816 data->fan_main_ctrl);
1820 /* No on/off mode, set maximum pwm value */
1821 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1822 data->write(data, IT87_REG_PWM_DUTY[nr],
1823 data->pwm_duty[nr]);
1824 /* and set manual mode */
1825 if (has_newer_autopwm(data)) {
1826 ctrl = temp_map_to_reg(data, nr,
1827 data->pwm_temp_map[nr]);
1830 ctrl = data->pwm_duty[nr];
1832 data->pwm_ctrl[nr] = ctrl;
1833 data->write(data, data->REG_PWM[nr], ctrl);
1838 if (has_newer_autopwm(data)) {
1839 ctrl = temp_map_to_reg(data, nr,
1840 data->pwm_temp_map[nr]);
1846 ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1848 data->pwm_ctrl[nr] = ctrl;
1849 data->write(data, data->REG_PWM[nr], ctrl);
1851 if (has_fanctl_onoff(data) && nr < 3) {
1852 /* set SmartGuardian mode */
1853 data->fan_main_ctrl |= BIT(nr);
1854 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1855 data->fan_main_ctrl);
1862 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1863 const char *buf, size_t count)
1865 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1866 struct it87_data *data = dev_get_drvdata(dev);
1867 int nr = sensor_attr->index;
1871 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1874 err = it87_lock(data);
1878 it87_update_pwm_ctrl(data, nr);
1879 if (has_newer_autopwm(data)) {
1881 * If we are in automatic mode, the PWM duty cycle register
1882 * is read-only so we can't write the value.
1884 if (data->pwm_ctrl[nr] & 0x80) {
1888 data->pwm_duty[nr] = pwm_to_reg(data, val);
1889 data->write(data, IT87_REG_PWM_DUTY[nr],
1890 data->pwm_duty[nr]);
1892 data->pwm_duty[nr] = pwm_to_reg(data, val);
1894 * If we are in manual mode, write the duty cycle immediately;
1895 * otherwise, just store it for later use.
1897 if (!(data->pwm_ctrl[nr] & 0x80)) {
1898 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1899 data->write(data, data->REG_PWM[nr],
1900 data->pwm_ctrl[nr]);
1908 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1909 const char *buf, size_t count)
1911 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1912 struct it87_data *data = dev_get_drvdata(dev);
1913 int nr = sensor_attr->index;
1918 if (kstrtoul(buf, 10, &val) < 0)
1921 val = clamp_val(val, 0, 1000000);
1922 val *= has_newer_autopwm(data) ? 256 : 128;
1924 /* Search for the nearest available frequency */
1925 for (i = 0; i < 7; i++) {
1926 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1930 err = it87_lock(data);
1935 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL) & 0x8f;
1936 data->fan_ctl |= i << 4;
1937 data->write(data, IT87_REG_FAN_CTL, data->fan_ctl);
1939 data->extra = data->read(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1940 data->extra |= i << 4;
1941 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1947 static ssize_t show_pwm_temp_map(struct device *dev,
1948 struct device_attribute *attr, char *buf)
1950 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1951 struct it87_data *data = it87_update_device(dev);
1952 int nr = sensor_attr->index;
1955 return PTR_ERR(data);
1957 return sprintf(buf, "%d\n", data->pwm_temp_map[nr] + 1);
1960 static ssize_t set_pwm_temp_map(struct device *dev,
1961 struct device_attribute *attr, const char *buf,
1964 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1965 struct it87_data *data = dev_get_drvdata(dev);
1966 int nr = sensor_attr->index;
1971 if (kstrtoul(buf, 10, &val) < 0)
1974 if (!val || val > data->pwm_num_temp_map)
1979 err = it87_lock(data);
1983 it87_update_pwm_ctrl(data, nr);
1984 data->pwm_temp_map[nr] = map;
1986 * If we are in automatic mode, write the temp mapping immediately;
1987 * otherwise, just store it for later use.
1989 if (data->pwm_ctrl[nr] & 0x80) {
1990 data->pwm_ctrl[nr] = temp_map_to_reg(data, nr, map);
1991 data->write(data, data->REG_PWM[nr], data->pwm_ctrl[nr]);
1997 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
2000 struct it87_data *data = it87_update_device(dev);
2001 struct sensor_device_attribute_2 *sensor_attr =
2002 to_sensor_dev_attr_2(attr);
2003 int nr = sensor_attr->nr;
2004 int point = sensor_attr->index;
2007 return PTR_ERR(data);
2009 return sprintf(buf, "%d\n",
2010 pwm_from_reg(data, data->auto_pwm[nr][point]));
2013 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
2014 const char *buf, size_t count)
2016 struct it87_data *data = dev_get_drvdata(dev);
2017 struct sensor_device_attribute_2 *sensor_attr =
2018 to_sensor_dev_attr_2(attr);
2019 int nr = sensor_attr->nr;
2020 int point = sensor_attr->index;
2025 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
2028 err = it87_lock(data);
2032 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
2033 if (has_newer_autopwm(data))
2034 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
2036 regaddr = IT87_REG_AUTO_PWM(nr, point);
2037 data->write(data, regaddr, data->auto_pwm[nr][point]);
2042 static ssize_t show_auto_pwm_slope(struct device *dev,
2043 struct device_attribute *attr, char *buf)
2045 struct it87_data *data = it87_update_device(dev);
2046 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2047 int nr = sensor_attr->index;
2050 return PTR_ERR(data);
2052 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
2055 static ssize_t set_auto_pwm_slope(struct device *dev,
2056 struct device_attribute *attr,
2057 const char *buf, size_t count)
2059 struct it87_data *data = dev_get_drvdata(dev);
2060 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2061 int nr = sensor_attr->index;
2065 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
2068 err = it87_lock(data);
2072 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
2073 data->write(data, IT87_REG_AUTO_TEMP(nr, 4), data->auto_pwm[nr][1]);
2078 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2081 struct it87_data *data = it87_update_device(dev);
2082 struct sensor_device_attribute_2 *sensor_attr =
2083 to_sensor_dev_attr_2(attr);
2084 int nr = sensor_attr->nr;
2085 int point = sensor_attr->index;
2089 return PTR_ERR(data);
2091 if (has_old_autopwm(data) || point)
2092 reg = data->auto_temp[nr][point];
2094 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
2096 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
2099 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
2100 const char *buf, size_t count)
2102 struct it87_data *data = dev_get_drvdata(dev);
2103 struct sensor_device_attribute_2 *sensor_attr =
2104 to_sensor_dev_attr_2(attr);
2105 int nr = sensor_attr->nr;
2106 int point = sensor_attr->index;
2111 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
2114 err = it87_lock(data);
2118 if (has_newer_autopwm(data) && !point) {
2119 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
2120 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
2121 data->auto_temp[nr][0] = reg;
2122 data->write(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
2124 reg = TEMP_TO_REG(val);
2125 data->auto_temp[nr][point] = reg;
2126 if (has_newer_autopwm(data))
2128 data->write(data, IT87_REG_AUTO_TEMP(nr, point), reg);
2134 static SENSOR_DEVICE_ATTR_2(fan1_input, 0444, show_fan, NULL, 0, 0);
2135 static SENSOR_DEVICE_ATTR_2(fan1_min, 0644, show_fan, set_fan, 0, 1);
2136 static SENSOR_DEVICE_ATTR(fan1_div, 0644, show_fan_div, set_fan_div, 0);
2138 static SENSOR_DEVICE_ATTR_2(fan2_input, 0444, show_fan, NULL, 1, 0);
2139 static SENSOR_DEVICE_ATTR_2(fan2_min, 0644, show_fan, set_fan, 1, 1);
2140 static SENSOR_DEVICE_ATTR(fan2_div, 0644, show_fan_div, set_fan_div, 1);
2142 static SENSOR_DEVICE_ATTR_2(fan3_input, 0444, show_fan, NULL, 2, 0);
2143 static SENSOR_DEVICE_ATTR_2(fan3_min, 0644, show_fan, set_fan, 2, 1);
2144 static SENSOR_DEVICE_ATTR(fan3_div, 0644, show_fan_div, set_fan_div, 2);
2146 static SENSOR_DEVICE_ATTR_2(fan4_input, 0444, show_fan, NULL, 3, 0);
2147 static SENSOR_DEVICE_ATTR_2(fan4_min, 0644, show_fan, set_fan, 3, 1);
2149 static SENSOR_DEVICE_ATTR_2(fan5_input, 0444, show_fan, NULL, 4, 0);
2150 static SENSOR_DEVICE_ATTR_2(fan5_min, 0644, show_fan, set_fan, 4, 1);
2152 static SENSOR_DEVICE_ATTR_2(fan6_input, 0444, show_fan, NULL, 5, 0);
2153 static SENSOR_DEVICE_ATTR_2(fan6_min, 0644, show_fan, set_fan, 5, 1);
2155 static SENSOR_DEVICE_ATTR(pwm1_enable, 0644,
2156 show_pwm_enable, set_pwm_enable, 0);
2157 static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
2158 static SENSOR_DEVICE_ATTR(pwm1_freq, 0644, show_pwm_freq, set_pwm_freq, 0);
2159 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, 0444,
2160 show_pwm_temp_map, set_pwm_temp_map, 0);
2161 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, 0644,
2162 show_auto_pwm, set_auto_pwm, 0, 0);
2163 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, 0644,
2164 show_auto_pwm, set_auto_pwm, 0, 1);
2165 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, 0644,
2166 show_auto_pwm, set_auto_pwm, 0, 2);
2167 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, 0444,
2168 show_auto_pwm, NULL, 0, 3);
2169 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, 0644,
2170 show_auto_temp, set_auto_temp, 0, 1);
2171 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, 0644,
2172 show_auto_temp, set_auto_temp, 0, 0);
2173 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, 0644,
2174 show_auto_temp, set_auto_temp, 0, 2);
2175 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, 0644,
2176 show_auto_temp, set_auto_temp, 0, 3);
2177 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, 0644,
2178 show_auto_temp, set_auto_temp, 0, 4);
2179 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, 0644,
2180 show_auto_pwm, set_auto_pwm, 0, 0);
2181 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, 0644,
2182 show_auto_pwm_slope, set_auto_pwm_slope, 0);
2184 static SENSOR_DEVICE_ATTR(pwm2_enable, 0644,
2185 show_pwm_enable, set_pwm_enable, 1);
2186 static SENSOR_DEVICE_ATTR(pwm2, 0644, show_pwm, set_pwm, 1);
2187 static SENSOR_DEVICE_ATTR(pwm2_freq, 0444, show_pwm_freq, set_pwm_freq, 1);
2188 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, 0444,
2189 show_pwm_temp_map, set_pwm_temp_map, 1);
2190 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, 0644,
2191 show_auto_pwm, set_auto_pwm, 1, 0);
2192 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, 0644,
2193 show_auto_pwm, set_auto_pwm, 1, 1);
2194 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, 0644,
2195 show_auto_pwm, set_auto_pwm, 1, 2);
2196 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, 0444,
2197 show_auto_pwm, NULL, 1, 3);
2198 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, 0644,
2199 show_auto_temp, set_auto_temp, 1, 1);
2200 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, 0644,
2201 show_auto_temp, set_auto_temp, 1, 0);
2202 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, 0644,
2203 show_auto_temp, set_auto_temp, 1, 2);
2204 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, 0644,
2205 show_auto_temp, set_auto_temp, 1, 3);
2206 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, 0644,
2207 show_auto_temp, set_auto_temp, 1, 4);
2208 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, 0644,
2209 show_auto_pwm, set_auto_pwm, 1, 0);
2210 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, 0644,
2211 show_auto_pwm_slope, set_auto_pwm_slope, 1);
2213 static SENSOR_DEVICE_ATTR(pwm3_enable, 0644,
2214 show_pwm_enable, set_pwm_enable, 2);
2215 static SENSOR_DEVICE_ATTR(pwm3, 0644, show_pwm, set_pwm, 2);
2216 static SENSOR_DEVICE_ATTR(pwm3_freq, 0444, show_pwm_freq, NULL, 2);
2217 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, 0444,
2218 show_pwm_temp_map, set_pwm_temp_map, 2);
2219 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, 0644,
2220 show_auto_pwm, set_auto_pwm, 2, 0);
2221 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, 0644,
2222 show_auto_pwm, set_auto_pwm, 2, 1);
2223 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, 0644,
2224 show_auto_pwm, set_auto_pwm, 2, 2);
2225 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, 0444,
2226 show_auto_pwm, NULL, 2, 3);
2227 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, 0644,
2228 show_auto_temp, set_auto_temp, 2, 1);
2229 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, 0644,
2230 show_auto_temp, set_auto_temp, 2, 0);
2231 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, 0644,
2232 show_auto_temp, set_auto_temp, 2, 2);
2233 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, 0644,
2234 show_auto_temp, set_auto_temp, 2, 3);
2235 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, 0644,
2236 show_auto_temp, set_auto_temp, 2, 4);
2237 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, 0644,
2238 show_auto_pwm, set_auto_pwm, 2, 0);
2239 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, 0644,
2240 show_auto_pwm_slope, set_auto_pwm_slope, 2);
2242 static SENSOR_DEVICE_ATTR(pwm4_enable, 0644,
2243 show_pwm_enable, set_pwm_enable, 3);
2244 static SENSOR_DEVICE_ATTR(pwm4, 0644, show_pwm, set_pwm, 3);
2245 static SENSOR_DEVICE_ATTR(pwm4_freq, 0444, show_pwm_freq, NULL, 3);
2246 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, 0444,
2247 show_pwm_temp_map, set_pwm_temp_map, 3);
2248 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, 0644,
2249 show_auto_temp, set_auto_temp, 2, 1);
2250 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, 0644,
2251 show_auto_temp, set_auto_temp, 2, 0);
2252 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, 0644,
2253 show_auto_temp, set_auto_temp, 2, 2);
2254 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, 0644,
2255 show_auto_temp, set_auto_temp, 2, 3);
2256 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, 0644,
2257 show_auto_pwm, set_auto_pwm, 3, 0);
2258 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, 0644,
2259 show_auto_pwm_slope, set_auto_pwm_slope, 3);
2261 static SENSOR_DEVICE_ATTR(pwm5_enable, 0644,
2262 show_pwm_enable, set_pwm_enable, 4);
2263 static SENSOR_DEVICE_ATTR(pwm5, 0644, show_pwm, set_pwm, 4);
2264 static SENSOR_DEVICE_ATTR(pwm5_freq, 0444, show_pwm_freq, NULL, 4);
2265 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, 0444,
2266 show_pwm_temp_map, set_pwm_temp_map, 4);
2267 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, 0644,
2268 show_auto_temp, set_auto_temp, 2, 1);
2269 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, 0644,
2270 show_auto_temp, set_auto_temp, 2, 0);
2271 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, 0644,
2272 show_auto_temp, set_auto_temp, 2, 2);
2273 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, 0644,
2274 show_auto_temp, set_auto_temp, 2, 3);
2275 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, 0644,
2276 show_auto_pwm, set_auto_pwm, 4, 0);
2277 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, 0644,
2278 show_auto_pwm_slope, set_auto_pwm_slope, 4);
2280 static SENSOR_DEVICE_ATTR(pwm6_enable, 0644,
2281 show_pwm_enable, set_pwm_enable, 5);
2282 static SENSOR_DEVICE_ATTR(pwm6, 0644, show_pwm, set_pwm, 5);
2283 static SENSOR_DEVICE_ATTR(pwm6_freq, 0444, show_pwm_freq, NULL, 5);
2284 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, 0444,
2285 show_pwm_temp_map, set_pwm_temp_map, 5);
2286 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, 0644,
2287 show_auto_temp, set_auto_temp, 2, 1);
2288 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, 0644,
2289 show_auto_temp, set_auto_temp, 2, 0);
2290 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, 0644,
2291 show_auto_temp, set_auto_temp, 2, 2);
2292 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, 0644,
2293 show_auto_temp, set_auto_temp, 2, 3);
2294 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, 0644,
2295 show_auto_pwm, set_auto_pwm, 5, 0);
2296 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, 0644,
2297 show_auto_pwm_slope, set_auto_pwm_slope, 5);
2300 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
2303 struct it87_data *data = it87_update_device(dev);
2306 return PTR_ERR(data);
2308 return sprintf(buf, "%u\n", data->alarms);
2310 static DEVICE_ATTR(alarms, 0444, show_alarms, NULL);
2312 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
2315 struct it87_data *data = it87_update_device(dev);
2316 int bitnr = to_sensor_dev_attr(attr)->index;
2319 return PTR_ERR(data);
2321 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
2324 static ssize_t clear_intrusion(struct device *dev,
2325 struct device_attribute *attr, const char *buf,
2328 struct it87_data *data = dev_get_drvdata(dev);
2332 if (kstrtol(buf, 10, &val) < 0 || val != 0)
2335 err = it87_lock(data);
2339 config = data->read(data, IT87_REG_CONFIG);
2341 data->write(data, IT87_REG_CONFIG, config);
2342 /* Invalidate cache to force re-read */
2348 static SENSOR_DEVICE_ATTR(in0_alarm, 0444, show_alarm, NULL, 8);
2349 static SENSOR_DEVICE_ATTR(in1_alarm, 0444, show_alarm, NULL, 9);
2350 static SENSOR_DEVICE_ATTR(in2_alarm, 0444, show_alarm, NULL, 10);
2351 static SENSOR_DEVICE_ATTR(in3_alarm, 0444, show_alarm, NULL, 11);
2352 static SENSOR_DEVICE_ATTR(in4_alarm, 0444, show_alarm, NULL, 12);
2353 static SENSOR_DEVICE_ATTR(in5_alarm, 0444, show_alarm, NULL, 13);
2354 static SENSOR_DEVICE_ATTR(in6_alarm, 0444, show_alarm, NULL, 14);
2355 static SENSOR_DEVICE_ATTR(in7_alarm, 0444, show_alarm, NULL, 15);
2356 static SENSOR_DEVICE_ATTR(fan1_alarm, 0444, show_alarm, NULL, 0);
2357 static SENSOR_DEVICE_ATTR(fan2_alarm, 0444, show_alarm, NULL, 1);
2358 static SENSOR_DEVICE_ATTR(fan3_alarm, 0444, show_alarm, NULL, 2);
2359 static SENSOR_DEVICE_ATTR(fan4_alarm, 0444, show_alarm, NULL, 3);
2360 static SENSOR_DEVICE_ATTR(fan5_alarm, 0444, show_alarm, NULL, 6);
2361 static SENSOR_DEVICE_ATTR(fan6_alarm, 0444, show_alarm, NULL, 7);
2362 static SENSOR_DEVICE_ATTR(temp1_alarm, 0444, show_alarm, NULL, 16);
2363 static SENSOR_DEVICE_ATTR(temp2_alarm, 0444, show_alarm, NULL, 17);
2364 static SENSOR_DEVICE_ATTR(temp3_alarm, 0444, show_alarm, NULL, 18);
2365 static SENSOR_DEVICE_ATTR(temp4_alarm, 0444, show_alarm, NULL, 19);
2366 static SENSOR_DEVICE_ATTR(temp5_alarm, 0444, show_alarm, NULL, 20);
2367 static SENSOR_DEVICE_ATTR(temp6_alarm, 0444, show_alarm, NULL, 21);
2368 static SENSOR_DEVICE_ATTR(intrusion0_alarm, 0644,
2369 show_alarm, clear_intrusion, 4);
2371 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
2374 struct it87_data *data = it87_update_device(dev);
2375 int bitnr = to_sensor_dev_attr(attr)->index;
2378 return PTR_ERR(data);
2380 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
2383 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
2384 const char *buf, size_t count)
2386 int bitnr = to_sensor_dev_attr(attr)->index;
2387 struct it87_data *data = dev_get_drvdata(dev);
2391 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
2394 err = it87_lock(data);
2398 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
2400 data->beeps |= BIT(bitnr);
2402 data->beeps &= ~BIT(bitnr);
2403 data->write(data, IT87_REG_BEEP_ENABLE, data->beeps);
2408 static SENSOR_DEVICE_ATTR(in0_beep, 0644,
2409 show_beep, set_beep, 1);
2410 static SENSOR_DEVICE_ATTR(in1_beep, 0444, show_beep, NULL, 1);
2411 static SENSOR_DEVICE_ATTR(in2_beep, 0444, show_beep, NULL, 1);
2412 static SENSOR_DEVICE_ATTR(in3_beep, 0444, show_beep, NULL, 1);
2413 static SENSOR_DEVICE_ATTR(in4_beep, 0444, show_beep, NULL, 1);
2414 static SENSOR_DEVICE_ATTR(in5_beep, 0444, show_beep, NULL, 1);
2415 static SENSOR_DEVICE_ATTR(in6_beep, 0444, show_beep, NULL, 1);
2416 static SENSOR_DEVICE_ATTR(in7_beep, 0444, show_beep, NULL, 1);
2417 /* fanX_beep writability is set later */
2418 static SENSOR_DEVICE_ATTR(fan1_beep, 0444, show_beep, set_beep, 0);
2419 static SENSOR_DEVICE_ATTR(fan2_beep, 0444, show_beep, set_beep, 0);
2420 static SENSOR_DEVICE_ATTR(fan3_beep, 0444, show_beep, set_beep, 0);
2421 static SENSOR_DEVICE_ATTR(fan4_beep, 0444, show_beep, set_beep, 0);
2422 static SENSOR_DEVICE_ATTR(fan5_beep, 0444, show_beep, set_beep, 0);
2423 static SENSOR_DEVICE_ATTR(fan6_beep, 0444, show_beep, set_beep, 0);
2424 static SENSOR_DEVICE_ATTR(temp1_beep, 0644,
2425 show_beep, set_beep, 2);
2426 static SENSOR_DEVICE_ATTR(temp2_beep, 0444, show_beep, NULL, 2);
2427 static SENSOR_DEVICE_ATTR(temp3_beep, 0444, show_beep, NULL, 2);
2428 static SENSOR_DEVICE_ATTR(temp4_beep, 0444, show_beep, NULL, 2);
2429 static SENSOR_DEVICE_ATTR(temp5_beep, 0444, show_beep, NULL, 2);
2430 static SENSOR_DEVICE_ATTR(temp6_beep, 0444, show_beep, NULL, 2);
2432 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
2435 struct it87_data *data = dev_get_drvdata(dev);
2437 return sprintf(buf, "%u\n", data->vrm);
2440 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
2441 const char *buf, size_t count)
2443 struct it87_data *data = dev_get_drvdata(dev);
2446 if (kstrtoul(buf, 10, &val) < 0)
2453 static DEVICE_ATTR(vrm, 0644, show_vrm_reg, store_vrm_reg);
2455 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
2458 struct it87_data *data = it87_update_device(dev);
2461 return PTR_ERR(data);
2463 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
2465 static DEVICE_ATTR(cpu0_vid, 0444, show_vid_reg, NULL);
2467 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
2470 static const char * const labels[] = {
2476 static const char * const labels_it8721[] = {
2482 struct it87_data *data = dev_get_drvdata(dev);
2483 int nr = to_sensor_dev_attr(attr)->index;
2486 if (has_vin3_5v(data) && nr == 0)
2488 else if (has_12mv_adc(data) || has_10_9mv_adc(data) ||
2490 label = labels_it8721[nr];
2494 return sprintf(buf, "%s\n", label);
2496 static SENSOR_DEVICE_ATTR(in3_label, 0444, show_label, NULL, 0);
2497 static SENSOR_DEVICE_ATTR(in7_label, 0444, show_label, NULL, 1);
2498 static SENSOR_DEVICE_ATTR(in8_label, 0444, show_label, NULL, 2);
2500 static SENSOR_DEVICE_ATTR(in9_label, 0444, show_label, NULL, 3);
2502 static umode_t it87_in_is_visible(struct kobject *kobj,
2503 struct attribute *attr, int index)
2505 struct device *dev = container_of(kobj, struct device, kobj);
2506 struct it87_data *data = dev_get_drvdata(dev);
2507 int i = index / 5; /* voltage index */
2508 int a = index % 5; /* attribute index */
2510 if (index >= 40) { /* in8 and higher only have input attributes */
2515 if (!(data->has_in & BIT(i)))
2518 if (a == 4 && !data->has_beep)
2524 static struct attribute *it87_attributes_in[] = {
2525 &sensor_dev_attr_in0_input.dev_attr.attr,
2526 &sensor_dev_attr_in0_min.dev_attr.attr,
2527 &sensor_dev_attr_in0_max.dev_attr.attr,
2528 &sensor_dev_attr_in0_alarm.dev_attr.attr,
2529 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
2531 &sensor_dev_attr_in1_input.dev_attr.attr,
2532 &sensor_dev_attr_in1_min.dev_attr.attr,
2533 &sensor_dev_attr_in1_max.dev_attr.attr,
2534 &sensor_dev_attr_in1_alarm.dev_attr.attr,
2535 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
2537 &sensor_dev_attr_in2_input.dev_attr.attr,
2538 &sensor_dev_attr_in2_min.dev_attr.attr,
2539 &sensor_dev_attr_in2_max.dev_attr.attr,
2540 &sensor_dev_attr_in2_alarm.dev_attr.attr,
2541 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
2543 &sensor_dev_attr_in3_input.dev_attr.attr,
2544 &sensor_dev_attr_in3_min.dev_attr.attr,
2545 &sensor_dev_attr_in3_max.dev_attr.attr,
2546 &sensor_dev_attr_in3_alarm.dev_attr.attr,
2547 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2549 &sensor_dev_attr_in4_input.dev_attr.attr,
2550 &sensor_dev_attr_in4_min.dev_attr.attr,
2551 &sensor_dev_attr_in4_max.dev_attr.attr,
2552 &sensor_dev_attr_in4_alarm.dev_attr.attr,
2553 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2555 &sensor_dev_attr_in5_input.dev_attr.attr,
2556 &sensor_dev_attr_in5_min.dev_attr.attr,
2557 &sensor_dev_attr_in5_max.dev_attr.attr,
2558 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2559 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2561 &sensor_dev_attr_in6_input.dev_attr.attr,
2562 &sensor_dev_attr_in6_min.dev_attr.attr,
2563 &sensor_dev_attr_in6_max.dev_attr.attr,
2564 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2565 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2567 &sensor_dev_attr_in7_input.dev_attr.attr,
2568 &sensor_dev_attr_in7_min.dev_attr.attr,
2569 &sensor_dev_attr_in7_max.dev_attr.attr,
2570 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2571 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2573 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2574 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
2575 &sensor_dev_attr_in10_input.dev_attr.attr, /* 42 */
2576 &sensor_dev_attr_in11_input.dev_attr.attr, /* 43 */
2577 &sensor_dev_attr_in12_input.dev_attr.attr, /* 44 */
2581 static const struct attribute_group it87_group_in = {
2582 .attrs = it87_attributes_in,
2583 .is_visible = it87_in_is_visible,
2586 static umode_t it87_temp_is_visible(struct kobject *kobj,
2587 struct attribute *attr, int index)
2589 struct device *dev = container_of(kobj, struct device, kobj);
2590 struct it87_data *data = dev_get_drvdata(dev);
2591 int i = index / 7; /* temperature index */
2592 int a = index % 7; /* attribute index */
2594 if (!(data->has_temp & BIT(i)))
2597 if (a && i >= data->num_temp_limit)
2601 int type = get_temp_type(data, i);
2605 if (has_bank_sel(data))
2610 if (a == 5 && i >= data->num_temp_offset)
2613 if (a == 6 && !data->has_beep)
2619 static struct attribute *it87_attributes_temp[] = {
2620 &sensor_dev_attr_temp1_input.dev_attr.attr,
2621 &sensor_dev_attr_temp1_max.dev_attr.attr,
2622 &sensor_dev_attr_temp1_min.dev_attr.attr,
2623 &sensor_dev_attr_temp1_type.dev_attr.attr, /* 3 */
2624 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2625 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2626 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2628 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2629 &sensor_dev_attr_temp2_max.dev_attr.attr,
2630 &sensor_dev_attr_temp2_min.dev_attr.attr,
2631 &sensor_dev_attr_temp2_type.dev_attr.attr,
2632 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2633 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2634 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2636 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2637 &sensor_dev_attr_temp3_max.dev_attr.attr,
2638 &sensor_dev_attr_temp3_min.dev_attr.attr,
2639 &sensor_dev_attr_temp3_type.dev_attr.attr,
2640 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2641 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2642 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2644 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2645 &sensor_dev_attr_temp4_max.dev_attr.attr,
2646 &sensor_dev_attr_temp4_min.dev_attr.attr,
2647 &sensor_dev_attr_temp4_type.dev_attr.attr,
2648 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
2649 &sensor_dev_attr_temp4_offset.dev_attr.attr,
2650 &sensor_dev_attr_temp4_beep.dev_attr.attr,
2652 &sensor_dev_attr_temp5_input.dev_attr.attr,
2653 &sensor_dev_attr_temp5_max.dev_attr.attr,
2654 &sensor_dev_attr_temp5_min.dev_attr.attr,
2655 &sensor_dev_attr_temp5_type.dev_attr.attr,
2656 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
2657 &sensor_dev_attr_temp5_offset.dev_attr.attr,
2658 &sensor_dev_attr_temp5_beep.dev_attr.attr,
2660 &sensor_dev_attr_temp6_input.dev_attr.attr,
2661 &sensor_dev_attr_temp6_max.dev_attr.attr,
2662 &sensor_dev_attr_temp6_min.dev_attr.attr,
2663 &sensor_dev_attr_temp6_type.dev_attr.attr,
2664 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
2665 &sensor_dev_attr_temp6_offset.dev_attr.attr,
2666 &sensor_dev_attr_temp6_beep.dev_attr.attr,
2670 static const struct attribute_group it87_group_temp = {
2671 .attrs = it87_attributes_temp,
2672 .is_visible = it87_temp_is_visible,
2675 static umode_t it87_is_visible(struct kobject *kobj,
2676 struct attribute *attr, int index)
2678 struct device *dev = container_of(kobj, struct device, kobj);
2679 struct it87_data *data = dev_get_drvdata(dev);
2681 if ((index == 2 || index == 3) && !data->has_vid)
2684 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2690 static struct attribute *it87_attributes[] = {
2691 &dev_attr_alarms.attr,
2692 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2693 &dev_attr_vrm.attr, /* 2 */
2694 &dev_attr_cpu0_vid.attr, /* 3 */
2695 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2696 &sensor_dev_attr_in7_label.dev_attr.attr,
2697 &sensor_dev_attr_in8_label.dev_attr.attr,
2698 &sensor_dev_attr_in9_label.dev_attr.attr,
2702 static const struct attribute_group it87_group = {
2703 .attrs = it87_attributes,
2704 .is_visible = it87_is_visible,
2707 static umode_t it87_fan_is_visible(struct kobject *kobj,
2708 struct attribute *attr, int index)
2710 struct device *dev = container_of(kobj, struct device, kobj);
2711 struct it87_data *data = dev_get_drvdata(dev);
2712 int i = index / 5; /* fan index */
2713 int a = index % 5; /* attribute index */
2715 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2716 i = (index - 15) / 4 + 3;
2717 a = (index - 15) % 4;
2720 if (!(data->has_fan & BIT(i)))
2723 if (a == 3) { /* beep */
2724 if (!data->has_beep)
2726 /* first fan beep attribute is writable */
2727 if (i == __ffs(data->has_fan))
2728 return attr->mode | 0200;
2731 if (a == 4 && has_16bit_fans(data)) /* divisor */
2737 static struct attribute *it87_attributes_fan[] = {
2738 &sensor_dev_attr_fan1_input.dev_attr.attr,
2739 &sensor_dev_attr_fan1_min.dev_attr.attr,
2740 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2741 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2742 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2744 &sensor_dev_attr_fan2_input.dev_attr.attr,
2745 &sensor_dev_attr_fan2_min.dev_attr.attr,
2746 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2747 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2748 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2750 &sensor_dev_attr_fan3_input.dev_attr.attr,
2751 &sensor_dev_attr_fan3_min.dev_attr.attr,
2752 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2753 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2754 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2756 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2757 &sensor_dev_attr_fan4_min.dev_attr.attr,
2758 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2759 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2761 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2762 &sensor_dev_attr_fan5_min.dev_attr.attr,
2763 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2764 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2766 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2767 &sensor_dev_attr_fan6_min.dev_attr.attr,
2768 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2769 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2773 static const struct attribute_group it87_group_fan = {
2774 .attrs = it87_attributes_fan,
2775 .is_visible = it87_fan_is_visible,
2778 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2779 struct attribute *attr, int index)
2781 struct device *dev = container_of(kobj, struct device, kobj);
2782 struct it87_data *data = dev_get_drvdata(dev);
2783 int i = index / 4; /* pwm index */
2784 int a = index % 4; /* attribute index */
2786 if (!(data->has_pwm & BIT(i)))
2789 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2790 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2791 return attr->mode | 0200;
2793 /* pwm2_freq is writable if there are two pwm frequency selects */
2794 if (has_pwm_freq2(data) && i == 1 && a == 2)
2795 return attr->mode | 0200;
2800 static struct attribute *it87_attributes_pwm[] = {
2801 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2802 &sensor_dev_attr_pwm1.dev_attr.attr,
2803 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2804 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2806 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2807 &sensor_dev_attr_pwm2.dev_attr.attr,
2808 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2809 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2811 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2812 &sensor_dev_attr_pwm3.dev_attr.attr,
2813 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2814 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2816 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2817 &sensor_dev_attr_pwm4.dev_attr.attr,
2818 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2819 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2821 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2822 &sensor_dev_attr_pwm5.dev_attr.attr,
2823 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2824 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2826 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2827 &sensor_dev_attr_pwm6.dev_attr.attr,
2828 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2829 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2834 static const struct attribute_group it87_group_pwm = {
2835 .attrs = it87_attributes_pwm,
2836 .is_visible = it87_pwm_is_visible,
2839 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2840 struct attribute *attr, int index)
2842 struct device *dev = container_of(kobj, struct device, kobj);
2843 struct it87_data *data = dev_get_drvdata(dev);
2844 int i = index / 11; /* pwm index */
2845 int a = index % 11; /* attribute index */
2847 if (index >= 33) { /* pwm 4..6 */
2848 i = (index - 33) / 6 + 3;
2849 a = (index - 33) % 6 + 4;
2852 if (!(data->has_pwm & BIT(i)))
2855 if (has_newer_autopwm(data)) {
2856 if (a < 4) /* no auto point pwm */
2858 if (a == 8) /* no auto_point4 */
2861 if (has_old_autopwm(data)) {
2862 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2869 static struct attribute *it87_attributes_auto_pwm[] = {
2870 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2871 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2872 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2873 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2874 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2875 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2876 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2877 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2878 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2879 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2880 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2882 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2883 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2884 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2885 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2886 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2887 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2888 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2889 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2890 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2891 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2892 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2894 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2895 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2896 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2897 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2898 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2899 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2900 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2901 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2902 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2903 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2904 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2906 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2907 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2908 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2909 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2910 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2911 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2913 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2914 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2915 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2916 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2917 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2918 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2920 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2921 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2922 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2923 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2924 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2925 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2930 static const struct attribute_group it87_group_auto_pwm = {
2931 .attrs = it87_attributes_auto_pwm,
2932 .is_visible = it87_auto_pwm_is_visible,
2935 /* SuperIO detection - will change isa_address if a chip is found */
2936 static int __init it87_find(int sioaddr, unsigned short *address,
2937 phys_addr_t *mmio_address,
2938 struct it87_sio_data *sio_data)
2940 const struct it87_devices *config;
2941 phys_addr_t base = 0;
2947 err = superio_enter(sioaddr);
2951 sio_data->sioaddr = sioaddr;
2954 chip_type = superio_inw(sioaddr, DEVID);
2955 if (chip_type == 0xffff)
2959 chip_type = force_id;
2961 switch (chip_type) {
2963 sio_data->type = it87;
2966 sio_data->type = it8712;
2970 sio_data->type = it8716;
2973 sio_data->type = it8718;
2976 sio_data->type = it8720;
2979 sio_data->type = it8721;
2982 sio_data->type = it8728;
2985 sio_data->type = it8732;
2988 sio_data->type = it8792;
2990 * Disabling configuration mode on IT8792E can result in system
2991 * hang-ups and access failures to the Super-IO chip at the
2992 * second SIO address. Never exit configuration mode on this
2993 * chip to avoid the problem.
2998 sio_data->type = it8771;
3001 sio_data->type = it8772;
3004 sio_data->type = it8781;
3007 sio_data->type = it8782;
3010 sio_data->type = it8783;
3013 sio_data->type = it8786;
3016 sio_data->type = it8790;
3017 doexit = false; /* See IT8792E comment above */
3021 sio_data->type = it8603;
3024 sio_data->type = it8607;
3027 sio_data->type = it8613;
3030 sio_data->type = it8620;
3033 sio_data->type = it8622;
3036 sio_data->type = it8625;
3039 sio_data->type = it8628;
3042 sio_data->type = it8655;
3045 sio_data->type = it8665;
3048 sio_data->type = it8686;
3050 case 0xffff: /* No device at all */
3053 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
3057 superio_select(sioaddr, PME);
3058 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
3059 pr_info("Device not activated, skipping\n");
3063 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
3064 if (*address == 0) {
3065 pr_info("Base address not set, skipping\n");
3069 sio_data->doexit = doexit;
3072 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
3074 config = &it87_devices[sio_data->type];
3076 if (has_mmio(config) && mmio) {
3079 reg = superio_inb(sioaddr, IT87_EC_HWM_MIO_REG);
3081 base = 0xf0000000 + ((reg & 0x0f) << 24);
3082 base += (reg & 0xc0) << 14;
3085 *mmio_address = base;
3089 snprintf(mmio_str, sizeof(mmio_str), " [MMIO at %pa]", &base);
3091 pr_info("Found IT%04x%s chip at 0x%x%s, revision %d\n", chip_type,
3092 it87_devices[sio_data->type].suffix,
3093 *address, mmio_str, sio_data->revision);
3095 /* in7 (VSB or VCCH5V) is always internal on some chips */
3096 if (has_in7_internal(config))
3097 sio_data->internal |= BIT(1);
3099 /* in8 (Vbat) is always internal */
3100 sio_data->internal |= BIT(2);
3102 /* in9 (AVCC3), always internal if supported */
3103 if (has_avcc3(config))
3104 sio_data->internal |= BIT(3); /* in9 is AVCC */
3106 sio_data->skip_in |= BIT(9);
3108 if (!has_four_pwm(config))
3109 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
3110 else if (!has_five_pwm(config))
3111 sio_data->skip_pwm |= BIT(4) | BIT(5);
3112 else if (!has_six_pwm(config))
3113 sio_data->skip_pwm |= BIT(5);
3115 if (!has_vid(config))
3116 sio_data->skip_vid = 1;
3118 /* Read GPIO config and VID value from LDN 7 (GPIO) */
3119 if (sio_data->type == it87) {
3120 /* The IT8705F has a different LD number for GPIO */
3121 superio_select(sioaddr, 5);
3122 sio_data->beep_pin = superio_inb(sioaddr,
3123 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3124 } else if (sio_data->type == it8783) {
3125 int reg25, reg27, reg2a, reg2c, regef;
3127 superio_select(sioaddr, GPIO);
3129 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3130 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3131 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3132 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3133 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
3135 /* Check if fan3 is there or not */
3136 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
3137 sio_data->skip_fan |= BIT(2);
3138 if ((reg25 & BIT(4)) ||
3139 (!(reg2a & BIT(1)) && (regef & BIT(0))))
3140 sio_data->skip_pwm |= BIT(2);
3142 /* Check if fan2 is there or not */
3144 sio_data->skip_fan |= BIT(1);
3146 sio_data->skip_pwm |= BIT(1);
3149 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
3150 sio_data->skip_in |= BIT(5); /* No VIN5 */
3154 sio_data->skip_in |= BIT(6); /* No VIN6 */
3158 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
3160 if (reg27 & BIT(2)) {
3162 * The data sheet is a bit unclear regarding the
3163 * internal voltage divider for VCCH5V. It says
3164 * "This bit enables and switches VIN7 (pin 91) to the
3165 * internal voltage divider for VCCH5V".
3166 * This is different to other chips, where the internal
3167 * voltage divider would connect VIN7 to an internal
3168 * voltage source. Maybe that is the case here as well.
3170 * Since we don't know for sure, re-route it if that is
3171 * not the case, and ask the user to report if the
3172 * resulting voltage is sane.
3174 if (!(reg2c & BIT(1))) {
3176 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
3178 pr_notice("Routing internal VCCH5V to in7.\n");
3180 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
3181 pr_notice("Please report if it displays a reasonable voltage.\n");
3185 sio_data->internal |= BIT(0);
3187 sio_data->internal |= BIT(1);
3189 sio_data->beep_pin = superio_inb(sioaddr,
3190 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3191 } else if (sio_data->type == it8603 || sio_data->type == it8607) {
3194 superio_select(sioaddr, GPIO);
3196 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3198 /* Check if fan3 is there or not */
3200 sio_data->skip_pwm |= BIT(2);
3202 sio_data->skip_fan |= BIT(2);
3204 /* Check if fan2 is there or not */
3205 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3207 sio_data->skip_pwm |= BIT(1);
3209 sio_data->skip_fan |= BIT(1);
3211 switch (sio_data->type) {
3213 sio_data->skip_in |= BIT(5); /* No VIN5 */
3214 sio_data->skip_in |= BIT(6); /* No VIN6 */
3217 sio_data->skip_pwm |= BIT(0);/* No fan1 */
3218 sio_data->skip_fan |= BIT(0);
3223 sio_data->beep_pin = superio_inb(sioaddr,
3224 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3225 } else if (sio_data->type == it8613) {
3226 int reg27, reg29, reg2a;
3228 superio_select(sioaddr, GPIO);
3230 /* Check for pwm3, fan3, pwm5, fan5 */
3231 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3233 sio_data->skip_fan |= BIT(4);
3235 sio_data->skip_pwm |= BIT(4);
3237 sio_data->skip_pwm |= BIT(2);
3239 sio_data->skip_fan |= BIT(2);
3241 /* Check for pwm2, fan2 */
3242 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3244 sio_data->skip_pwm |= BIT(1);
3246 sio_data->skip_fan |= BIT(1);
3248 /* Check for pwm4, fan4 */
3249 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3250 if (!(reg2a & BIT(0)) || (reg29 & BIT(7))) {
3251 sio_data->skip_fan |= BIT(3);
3252 sio_data->skip_pwm |= BIT(3);
3255 sio_data->skip_pwm |= BIT(0); /* No pwm1 */
3256 sio_data->skip_fan |= BIT(0); /* No fan1 */
3257 sio_data->skip_in |= BIT(3); /* No VIN3 */
3258 sio_data->skip_in |= BIT(6); /* No VIN6 */
3260 sio_data->beep_pin = superio_inb(sioaddr,
3261 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3262 } else if (sio_data->type == it8620 || sio_data->type == it8628 ||
3263 sio_data->type == it8686) {
3266 superio_select(sioaddr, GPIO);
3268 /* Check for pwm5 */
3269 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3271 sio_data->skip_pwm |= BIT(4);
3273 /* Check for fan4, fan5 */
3274 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3275 if (!(reg & BIT(5)))
3276 sio_data->skip_fan |= BIT(3);
3277 if (!(reg & BIT(4)))
3278 sio_data->skip_fan |= BIT(4);
3280 /* Check for pwm3, fan3 */
3281 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3283 sio_data->skip_pwm |= BIT(2);
3285 sio_data->skip_fan |= BIT(2);
3287 /* Check for pwm4 */
3288 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
3290 sio_data->skip_pwm |= BIT(3);
3292 /* Check for pwm2, fan2 */
3293 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3295 sio_data->skip_pwm |= BIT(1);
3297 sio_data->skip_fan |= BIT(1);
3298 /* Check for pwm6, fan6 */
3299 if (!(reg & BIT(7))) {
3300 sio_data->skip_pwm |= BIT(5);
3301 sio_data->skip_fan |= BIT(5);
3304 /* Check if AVCC is on VIN3 */
3305 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3307 /* For it8686, the bit just enables AVCC3 */
3308 if (sio_data->type != it8686)
3309 sio_data->internal |= BIT(0);
3311 sio_data->internal &= ~BIT(3);
3312 sio_data->skip_in |= BIT(9);
3315 sio_data->beep_pin = superio_inb(sioaddr,
3316 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3317 } else if (sio_data->type == it8622) {
3320 superio_select(sioaddr, GPIO);
3322 /* Check for pwm4, fan4 */
3323 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3325 sio_data->skip_fan |= BIT(3);
3327 sio_data->skip_pwm |= BIT(3);
3329 /* Check for pwm3, fan3, pwm5, fan5 */
3330 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3332 sio_data->skip_pwm |= BIT(2);
3334 sio_data->skip_fan |= BIT(2);
3336 sio_data->skip_pwm |= BIT(4);
3338 sio_data->skip_fan |= BIT(4);
3340 /* Check for pwm2, fan2 */
3341 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3343 sio_data->skip_pwm |= BIT(1);
3345 sio_data->skip_fan |= BIT(1);
3347 /* Check for AVCC */
3348 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3349 if (!(reg & BIT(0)))
3350 sio_data->skip_in |= BIT(9);
3352 sio_data->beep_pin = superio_inb(sioaddr,
3353 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3354 } else if (sio_data->type == it8732) {
3357 superio_select(sioaddr, GPIO);
3359 /* Check for pwm2, fan2 */
3360 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3362 sio_data->skip_pwm |= BIT(1);
3364 sio_data->skip_fan |= BIT(1);
3366 /* Check for pwm3, fan3, fan4 */
3367 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3369 sio_data->skip_pwm |= BIT(2);
3371 sio_data->skip_fan |= BIT(2);
3373 sio_data->skip_fan |= BIT(3);
3375 /* Check if AVCC is on VIN3 */
3376 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3378 sio_data->internal |= BIT(0);
3380 sio_data->beep_pin = superio_inb(sioaddr,
3381 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3382 } else if (sio_data->type == it8655) {
3385 superio_select(sioaddr, GPIO);
3387 /* Check for pwm2 */
3388 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3390 sio_data->skip_pwm |= BIT(1);
3392 /* Check for fan2 */
3393 reg = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3395 sio_data->skip_fan |= BIT(1);
3397 /* Check for pwm3, fan3 */
3398 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3400 sio_data->skip_pwm |= BIT(2);
3402 sio_data->skip_fan |= BIT(2);
3404 sio_data->beep_pin = superio_inb(sioaddr,
3405 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3406 } else if (sio_data->type == it8665 || sio_data->type == it8625) {
3407 int reg27, reg29, reg2d, regd3;
3409 superio_select(sioaddr, GPIO);
3411 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3412 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3413 reg2d = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3414 regd3 = superio_inb(sioaddr, IT87_SIO_GPIO9_REG);
3416 /* Check for pwm2, fan2 */
3418 sio_data->skip_pwm |= BIT(1);
3420 * Note: Table 6-1 in datasheet claims that FAN_TAC2
3421 * would be enabled with 29h[2]=0.
3424 sio_data->skip_fan |= BIT(1);
3426 /* Check for pwm3, fan3 */
3428 sio_data->skip_pwm |= BIT(2);
3430 sio_data->skip_fan |= BIT(2);
3432 /* Check for pwm4, fan4, pwm5, fan5 */
3433 if (sio_data->type == it8625) {
3434 int reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3437 sio_data->skip_fan |= BIT(3);
3439 sio_data->skip_pwm |= BIT(3);
3441 sio_data->skip_pwm |= BIT(4);
3443 sio_data->skip_fan |= BIT(4);
3445 int reg26 = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3448 sio_data->skip_pwm |= BIT(3);
3450 sio_data->skip_fan |= BIT(3);
3452 sio_data->skip_pwm |= BIT(4);
3454 sio_data->skip_fan |= BIT(4);
3457 /* Check for pwm6, fan6 */
3459 sio_data->skip_pwm |= BIT(5);
3461 sio_data->skip_fan |= BIT(5);
3463 sio_data->beep_pin = superio_inb(sioaddr,
3464 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3469 superio_select(sioaddr, GPIO);
3471 /* Check for fan4, fan5 */
3472 if (has_five_fans(config)) {
3473 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3474 switch (sio_data->type) {
3477 sio_data->skip_fan |= BIT(3);
3479 sio_data->skip_fan |= BIT(4);
3484 if (!(reg & BIT(5)))
3485 sio_data->skip_fan |= BIT(3);
3486 if (!(reg & BIT(4)))
3487 sio_data->skip_fan |= BIT(4);
3494 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3495 if (!sio_data->skip_vid) {
3496 /* We need at least 4 VID pins */
3498 pr_info("VID is disabled (pins used for GPIO)\n");
3499 sio_data->skip_vid = 1;
3503 /* Check if fan3 is there or not */
3505 sio_data->skip_pwm |= BIT(2);
3507 sio_data->skip_fan |= BIT(2);
3509 /* Check if fan2 is there or not */
3510 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3512 sio_data->skip_pwm |= BIT(1);
3514 sio_data->skip_fan |= BIT(1);
3516 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
3517 !(sio_data->skip_vid))
3518 sio_data->vid_value = superio_inb(sioaddr,
3521 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3523 uart6 = sio_data->type == it8782 && (reg & BIT(2));
3526 * The IT8720F has no VIN7 pin, so VCCH should always be
3527 * routed internally to VIN7 with an internal divider.
3528 * Curiously, there still is a configuration bit to control
3529 * this, which means it can be set incorrectly. And even
3530 * more curiously, many boards out there are improperly
3531 * configured, even though the IT8720F datasheet claims
3532 * that the internal routing of VCCH to VIN7 is the default
3533 * setting. So we force the internal routing in this case.
3535 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
3536 * If UART6 is enabled, re-route VIN7 to the internal divider
3537 * if that is not already the case.
3539 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
3541 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
3542 pr_notice("Routing internal VCCH to in7\n");
3545 sio_data->internal |= BIT(0);
3547 sio_data->internal |= BIT(1);
3550 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
3551 * While VIN7 can be routed to the internal voltage divider,
3552 * VIN5 and VIN6 are not available if UART6 is enabled.
3554 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
3555 * is the temperature source. Since we can not read the
3556 * temperature source here, skip_temp is preliminary.
3559 sio_data->skip_in |= BIT(5) | BIT(6);
3560 sio_data->skip_temp |= BIT(2);
3563 sio_data->beep_pin = superio_inb(sioaddr,
3564 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3566 if (sio_data->beep_pin)
3567 pr_info("Beeping is supported\n");
3569 if (config->smbus_bitmap && !base) {
3572 superio_select(sioaddr, PME);
3573 reg = superio_inb(sioaddr, IT87_SPECIAL_CFG_REG);
3574 sio_data->ec_special_config = reg;
3575 sio_data->smbus_bitmap = reg & config->smbus_bitmap;
3579 superio_exit(sioaddr, doexit);
3583 static void it87_init_regs(struct platform_device *pdev)
3585 struct it87_data *data = platform_get_drvdata(pdev);
3587 /* Initialize chip specific register pointers */
3588 switch (data->type) {
3591 data->REG_FAN = IT87_REG_FAN;
3592 data->REG_FANX = IT87_REG_FANX;
3593 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3594 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3595 data->REG_PWM = IT87_REG_PWM;
3596 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET_8686;
3597 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW_8686;
3598 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH_8686;
3603 data->REG_FAN = IT87_REG_FAN_8665;
3604 data->REG_FANX = IT87_REG_FANX_8665;
3605 data->REG_FAN_MIN = IT87_REG_FAN_MIN_8665;
3606 data->REG_FANX_MIN = IT87_REG_FANX_MIN_8665;
3607 data->REG_PWM = IT87_REG_PWM_8665;
3608 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3609 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3610 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3613 data->REG_FAN = IT87_REG_FAN;
3614 data->REG_FANX = IT87_REG_FANX;
3615 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3616 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3617 data->REG_PWM = IT87_REG_PWM_8665;
3618 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3619 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3620 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3623 data->REG_FAN = IT87_REG_FAN;
3624 data->REG_FANX = IT87_REG_FANX;
3625 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3626 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3627 data->REG_PWM = IT87_REG_PWM_8665;
3628 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3629 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3630 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3633 data->REG_FAN = IT87_REG_FAN;
3634 data->REG_FANX = IT87_REG_FANX;
3635 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3636 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3637 data->REG_PWM = IT87_REG_PWM;
3638 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3639 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3640 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3645 data->read = it87_mmio_read;
3646 data->write = it87_mmio_write;
3647 } else if (has_bank_sel(data)) {
3648 data->read = it87_io_read;
3649 data->write = it87_io_write;
3651 data->read = _it87_io_read;
3652 data->write = _it87_io_write;
3656 /* Called when we have found a new IT87. */
3657 static void it87_init_device(struct platform_device *pdev)
3659 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
3660 struct it87_data *data = platform_get_drvdata(pdev);
3664 if (has_new_tempmap(data)) {
3665 data->pwm_temp_map_shift = 3;
3666 data->pwm_temp_map_mask = 0x07;
3668 data->pwm_temp_map_shift = 0;
3669 data->pwm_temp_map_mask = 0x03;
3673 * For each PWM channel:
3674 * - If it is in automatic mode, setting to manual mode should set
3675 * the fan to full speed by default.
3676 * - If it is in manual mode, we need a mapping to temperature
3677 * channels to use when later setting to automatic mode later.
3678 * Map to the first sensor by default (we are clueless.)
3679 * In both cases, the value can (and should) be changed by the user
3680 * prior to switching to a different mode.
3681 * Note that this is no longer needed for the IT8721F and later, as
3682 * these have separate registers for the temperature mapping and the
3683 * manual duty cycle.
3685 for (i = 0; i < NUM_AUTO_PWM; i++) {
3686 data->pwm_temp_map[i] = 0;
3687 data->pwm_duty[i] = 0x7f; /* Full speed */
3688 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
3692 * Some chips seem to have default value 0xff for all limit
3693 * registers. For low voltage limits it makes no sense and triggers
3694 * alarms, so change to 0 instead. For high temperature limits, it
3695 * means -1 degree C, which surprisingly doesn't trigger an alarm,
3696 * but is still confusing, so change to 127 degrees C.
3698 for (i = 0; i < NUM_VIN_LIMIT; i++) {
3699 tmp = data->read(data, IT87_REG_VIN_MIN(i));
3701 data->write(data, IT87_REG_VIN_MIN(i), 0);
3703 for (i = 0; i < data->num_temp_limit; i++) {
3704 tmp = data->read(data, data->REG_TEMP_HIGH[i]);
3706 data->write(data, data->REG_TEMP_HIGH[i], 127);
3710 * Temperature channels are not forcibly enabled, as they can be
3711 * set to two different sensor types and we can't guess which one
3712 * is correct for a given system. These channels can be enabled at
3713 * run-time through the temp{1-3}_type sysfs accessors if needed.
3716 /* Check if voltage monitors are reset manually or by some reason */
3717 tmp = data->read(data, IT87_REG_VIN_ENABLE);
3718 if ((tmp & 0xff) == 0) {
3719 /* Enable all voltage monitors */
3720 data->write(data, IT87_REG_VIN_ENABLE, 0xff);
3723 /* Check if tachometers are reset manually or by some reason */
3724 mask = 0x70 & ~(sio_data->skip_fan << 4);
3725 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
3726 if ((data->fan_main_ctrl & mask) == 0) {
3727 /* Enable all fan tachometers */
3728 data->fan_main_ctrl |= mask;
3729 data->write(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
3731 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
3733 tmp = data->read(data, IT87_REG_FAN_16BIT);
3735 /* Set tachometers to 16-bit mode if needed */
3736 if (has_fan16_config(data)) {
3737 if (~tmp & 0x07 & data->has_fan) {
3739 "Setting fan1-3 to 16-bit mode\n");
3740 data->write(data, IT87_REG_FAN_16BIT, tmp | 0x07);
3744 /* Check for additional fans */
3745 if (has_four_fans(data) && (tmp & BIT(4)))
3746 data->has_fan |= BIT(3); /* fan4 enabled */
3747 if (has_five_fans(data) && (tmp & BIT(5)))
3748 data->has_fan |= BIT(4); /* fan5 enabled */
3749 if (has_six_fans(data)) {
3750 switch (data->type) {
3755 data->has_fan |= BIT(5); /* fan6 enabled */
3759 tmp = data->read(data, IT87_REG_FAN_DIV);
3761 data->has_fan |= BIT(5); /* fan6 enabled */
3768 /* Fan input pins may be used for alternative functions */
3769 data->has_fan &= ~sio_data->skip_fan;
3771 /* Check if pwm6 is enabled */
3772 if (has_six_pwm(data)) {
3773 switch (data->type) {
3776 tmp = data->read(data, IT87_REG_FAN_DIV);
3777 if (!(tmp & BIT(3)))
3778 sio_data->skip_pwm |= BIT(5);
3785 if (has_bank_sel(data)) {
3786 for (i = 0; i < 3; i++)
3788 data->read(data, IT87_REG_TEMP_SRC1[i]);
3789 data->temp_src[3] = data->read(data, IT87_REG_TEMP_SRC2);
3792 /* Start monitoring */
3793 data->write(data, IT87_REG_CONFIG,
3794 (data->read(data, IT87_REG_CONFIG) & 0x3e) |
3795 (update_vbat ? 0x41 : 0x01));
3798 /* Return 1 if and only if the PWM interface is safe to use */
3799 static int it87_check_pwm(struct device *dev)
3801 struct it87_data *data = dev_get_drvdata(dev);
3803 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
3804 * and polarity set to active low is sign that this is the case so we
3805 * disable pwm control to protect the user.
3807 int tmp = data->read(data, IT87_REG_FAN_CTL);
3809 if ((tmp & 0x87) == 0) {
3810 if (fix_pwm_polarity) {
3812 * The user asks us to attempt a chip reconfiguration.
3813 * This means switching to active high polarity and
3814 * inverting all fan speed values.
3819 for (i = 0; i < ARRAY_SIZE(pwm); i++)
3820 pwm[i] = data->read(data,
3824 * If any fan is in automatic pwm mode, the polarity
3825 * might be correct, as suspicious as it seems, so we
3826 * better don't change anything (but still disable the
3829 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3831 "Reconfiguring PWM to active high polarity\n");
3832 data->write(data, IT87_REG_FAN_CTL, tmp | 0x87);
3833 for (i = 0; i < 3; i++)
3834 data->write(data, data->REG_PWM[i],
3840 "PWM configuration is too broken to be fixed\n");
3844 "Detected broken BIOS defaults, disabling PWM interface\n");
3846 } else if (fix_pwm_polarity) {
3848 "PWM configuration looks sane, won't touch\n");
3854 static int it87_probe(struct platform_device *pdev)
3856 struct it87_data *data;
3857 struct resource *res;
3858 struct device *dev = &pdev->dev;
3859 struct it87_sio_data *sio_data = dev_get_platdata(dev);
3860 int enable_pwm_interface;
3861 struct device *hwmon_dev;
3864 data = devm_kzalloc(dev, sizeof(struct it87_data), GFP_KERNEL);
3868 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3870 if (!devm_request_region(dev, res->start, IT87_EC_EXTENT,
3872 dev_err(dev, "Failed to request region %pR\n", res);
3876 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3877 data->mmio = devm_ioremap_resource(dev, res);
3878 if (IS_ERR(data->mmio))
3879 return PTR_ERR(data->mmio);
3882 data->addr = res->start;
3883 data->type = sio_data->type;
3884 data->sioaddr = sio_data->sioaddr;
3885 data->smbus_bitmap = sio_data->smbus_bitmap;
3886 data->ec_special_config = sio_data->ec_special_config;
3887 data->doexit = sio_data->doexit;
3888 data->features = it87_devices[sio_data->type].features;
3889 data->num_temp_limit = it87_devices[sio_data->type].num_temp_limit;
3890 data->num_temp_offset = it87_devices[sio_data->type].num_temp_offset;
3891 data->pwm_num_temp_map = it87_devices[sio_data->type].num_temp_map;
3892 data->peci_mask = it87_devices[sio_data->type].peci_mask;
3893 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3896 * IT8705F Datasheet 0.4.1, 3h == Version G.
3897 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3898 * These are the first revisions with 16-bit tachometer support.
3900 switch (data->type) {
3902 if (sio_data->revision >= 0x03) {
3903 data->features &= ~FEAT_OLD_AUTOPWM;
3904 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3908 if (sio_data->revision >= 0x08) {
3909 data->features &= ~FEAT_OLD_AUTOPWM;
3910 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3918 platform_set_drvdata(pdev, data);
3920 mutex_init(&data->update_lock);
3922 /* Initialize register pointers */
3923 it87_init_regs(pdev);
3925 err = smbus_disable(data);
3929 /* Now, we do the remaining detection. */
3930 if ((data->read(data, IT87_REG_CONFIG) & 0x80) ||
3931 data->read(data, IT87_REG_CHIPID) != 0x90) {
3936 /* Check PWM configuration */
3937 enable_pwm_interface = it87_check_pwm(dev);
3939 /* Starting with IT8721F, we handle scaling of internal voltages */
3940 if (has_scaling(data)) {
3941 if (sio_data->internal & BIT(0))
3942 data->in_scaled |= BIT(3); /* in3 is AVCC */
3943 if (sio_data->internal & BIT(1))
3944 data->in_scaled |= BIT(7); /* in7 is VSB */
3945 if (sio_data->internal & BIT(2))
3946 data->in_scaled |= BIT(8); /* in8 is Vbat */
3947 if (sio_data->internal & BIT(3))
3948 data->in_scaled |= BIT(9); /* in9 is AVCC */
3949 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3950 sio_data->type == it8783) {
3951 if (sio_data->internal & BIT(0))
3952 data->in_scaled |= BIT(3); /* in3 is VCC5V */
3953 if (sio_data->internal & BIT(1))
3954 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
3957 data->has_temp = 0x07;
3958 if (sio_data->skip_temp & BIT(2)) {
3959 if (sio_data->type == it8782 &&
3960 !(data->read(data, IT87_REG_TEMP_EXTRA) & 0x80))
3961 data->has_temp &= ~BIT(2);
3964 data->in_internal = sio_data->internal;
3965 data->has_in = 0x3ff & ~sio_data->skip_in;
3967 if (has_four_temp(data)) {
3968 data->has_temp |= BIT(3);
3969 } else if (has_six_temp(data)) {
3970 u8 reg = data->read(data, IT87_REG_TEMP456_ENABLE);
3972 /* Check for additional temperature sensors */
3973 if ((reg & 0x03) >= 0x02)
3974 data->has_temp |= BIT(3);
3975 if (((reg >> 2) & 0x03) >= 0x02)
3976 data->has_temp |= BIT(4);
3977 if (((reg >> 4) & 0x03) >= 0x02)
3978 data->has_temp |= BIT(5);
3980 /* Check for additional voltage sensors */
3981 if ((reg & 0x03) == 0x01)
3982 data->has_in |= BIT(10);
3983 if (((reg >> 2) & 0x03) == 0x01)
3984 data->has_in |= BIT(11);
3985 if (((reg >> 4) & 0x03) == 0x01)
3986 data->has_in |= BIT(12);
3989 data->has_beep = !!sio_data->beep_pin;
3991 /* Initialize the IT87 chip */
3992 it87_init_device(pdev);
3996 if (!sio_data->skip_vid) {
3997 data->has_vid = true;
3998 data->vrm = vid_which_vrm();
3999 /* VID reading from Super-I/O config space if available */
4000 data->vid = sio_data->vid_value;
4003 /* Prepare for sysfs hooks */
4004 data->groups[0] = &it87_group;
4005 data->groups[1] = &it87_group_in;
4006 data->groups[2] = &it87_group_temp;
4007 data->groups[3] = &it87_group_fan;
4009 if (enable_pwm_interface) {
4010 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
4011 data->has_pwm &= ~sio_data->skip_pwm;
4013 data->groups[4] = &it87_group_pwm;
4014 if (has_old_autopwm(data) || has_newer_autopwm(data))
4015 data->groups[5] = &it87_group_auto_pwm;
4018 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
4019 it87_devices[sio_data->type].name,
4020 data, data->groups);
4021 return PTR_ERR_OR_ZERO(hwmon_dev);
4024 static struct platform_driver it87_driver = {
4028 .probe = it87_probe,
4031 static int __init it87_device_add(int index, unsigned short sio_address,
4032 phys_addr_t mmio_address,
4033 const struct it87_sio_data *sio_data)
4035 struct platform_device *pdev;
4036 struct resource res = {
4042 res.start = mmio_address;
4043 res.end = mmio_address + 0x400 - 1;
4044 res.flags = IORESOURCE_MEM;
4046 res.start = sio_address + IT87_EC_OFFSET;
4047 res.end = sio_address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1;
4048 res.flags = IORESOURCE_IO;
4051 err = acpi_check_resource_conflict(&res);
4053 if (!ignore_resource_conflict)
4057 pdev = platform_device_alloc(DRVNAME, sio_address);
4061 err = platform_device_add_resources(pdev, &res, 1);
4063 pr_err("Device resource addition failed (%d)\n", err);
4064 goto exit_device_put;
4067 err = platform_device_add_data(pdev, sio_data,
4068 sizeof(struct it87_sio_data));
4070 pr_err("Platform data allocation failed\n");
4071 goto exit_device_put;
4074 err = platform_device_add(pdev);
4076 pr_err("Device addition failed (%d)\n", err);
4077 goto exit_device_put;
4080 it87_pdev[index] = pdev;
4084 platform_device_put(pdev);
4088 struct it87_dmi_data {
4089 bool sio2_force_config; /* force sio2 into configuration mode */
4090 u8 skip_pwm; /* pwm channels to skip for this board */
4094 * On various Gigabyte AM4 boards (AB350, AX370), the second Super-IO chip
4095 * (IT8792E) needs to be in configuration mode before accessing the first
4096 * due to a bug in IT8792E which otherwise results in LPC bus access errors.
4097 * This needs to be done before accessing the first Super-IO chip since
4098 * the second chip may have been accessed prior to loading this driver.
4100 * The problem is also reported to affect IT8795E, which is used on X299 boards
4101 * and has the same chip ID as IT8792E (0x8733). It also appears to affect
4102 * systems with IT8790E, which is used on some Z97X-Gaming boards as well as
4104 * DMI entries for those systems will be added as they become available and
4105 * as the problem is confirmed to affect those boards.
4107 static struct it87_dmi_data gigabyte_sio2_force = {
4108 .sio2_force_config = true,
4112 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
4113 * connected to a fan, but to something else. One user
4114 * has reported instant system power-off when changing
4115 * the PWM2 duty cycle, so we disable it.
4116 * I use the board name string as the trigger in case
4117 * the same board is ever used in other systems.
4119 static struct it87_dmi_data nvidia_fn68pt = {
4123 static const struct dmi_system_id it87_dmi_table[] __initconst = {
4126 DMI_MATCH(DMI_SYS_VENDOR,
4127 "Gigabyte Technology Co., Ltd."),
4128 DMI_MATCH(DMI_BOARD_NAME, "AB350"),
4130 .driver_data = &gigabyte_sio2_force,
4134 DMI_MATCH(DMI_SYS_VENDOR,
4135 "Gigabyte Technology Co., Ltd."),
4136 DMI_MATCH(DMI_BOARD_NAME, "AX370"),
4138 .driver_data = &gigabyte_sio2_force,
4142 DMI_MATCH(DMI_SYS_VENDOR,
4143 "Gigabyte Technology Co., Ltd."),
4144 DMI_MATCH(DMI_BOARD_NAME, "Z97X-Gaming G1"),
4146 .driver_data = &gigabyte_sio2_force,
4150 DMI_MATCH(DMI_BOARD_VENDOR, "nVIDIA"),
4151 DMI_MATCH(DMI_BOARD_NAME, "FN68PT"),
4153 .driver_data = &nvidia_fn68pt,
4158 static int __init sm_it87_init(void)
4160 const struct dmi_system_id *dmi = dmi_first_match(it87_dmi_table);
4161 struct it87_dmi_data *dmi_data = NULL;
4162 int sioaddr[2] = { REG_2E, REG_4E };
4163 struct it87_sio_data sio_data;
4164 unsigned short isa_address;
4165 phys_addr_t mmio_address;
4169 pr_info("it87 driver version %s\n", IT87_DRIVER_VERSION);
4172 dmi_data = dmi->driver_data;
4174 err = platform_driver_register(&it87_driver);
4178 if (dmi_data && dmi_data->sio2_force_config)
4179 __superio_enter(REG_4E);
4181 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
4182 memset(&sio_data, 0, sizeof(struct it87_sio_data));
4185 err = it87_find(sioaddr[i], &isa_address, &mmio_address,
4187 if (err || isa_address == 0)
4191 sio_data.skip_pwm |= dmi_data->skip_pwm;
4192 err = it87_device_add(i, isa_address, mmio_address, &sio_data);
4194 goto exit_dev_unregister;
4200 goto exit_unregister;
4204 exit_dev_unregister:
4205 /* NULL check handled by platform_device_unregister */
4206 platform_device_unregister(it87_pdev[0]);
4208 platform_driver_unregister(&it87_driver);
4212 static void __exit sm_it87_exit(void)
4214 /* NULL check handled by platform_device_unregister */
4215 platform_device_unregister(it87_pdev[1]);
4216 platform_device_unregister(it87_pdev[0]);
4217 platform_driver_unregister(&it87_driver);
4220 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
4221 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
4222 module_param(update_vbat, bool, 0000);
4223 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
4224 module_param(fix_pwm_polarity, bool, 0000);
4225 MODULE_PARM_DESC(fix_pwm_polarity,
4226 "Force PWM polarity to active high (DANGEROUS)");
4227 MODULE_LICENSE("GPL");
4229 module_init(sm_it87_init);
4230 module_exit(sm_it87_exit);