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[] = { 0x56, 0x57, 0x59, 0x90, 0x91, 0x92 };
290 #define IT87_REG_FAN_MAIN_CTRL 0x13
291 #define IT87_REG_FAN_CTL 0x14
293 static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
294 static const u8 IT87_REG_PWM_8665[] = { 0x15, 0x16, 0x17, 0x1e, 0x1f, 0x92 };
296 static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
298 static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
299 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
301 #define IT87_REG_TEMP(nr) (0x29 + (nr))
303 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
304 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
306 static const u8 IT87_REG_TEMP_HIGH[] = { 0x40, 0x42, 0x44, 0x46, 0xb4, 0xb6 };
307 static const u8 IT87_REG_TEMP_LOW[] = { 0x41, 0x43, 0x45, 0x47, 0xb5, 0xb7 };
309 static const u8 IT87_REG_TEMP_HIGH_8686[] =
310 { 0x40, 0x42, 0x44, 0xb4, 0xb6, 0xb8 };
311 static const u8 IT87_REG_TEMP_LOW_8686[] =
312 { 0x41, 0x43, 0x45, 0xb5, 0xb7, 0xb9 };
314 #define IT87_REG_VIN_ENABLE 0x50
315 #define IT87_REG_TEMP_ENABLE 0x51
316 #define IT87_REG_TEMP_EXTRA 0x55
317 #define IT87_REG_BEEP_ENABLE 0x5c
319 #define IT87_REG_CHIPID 0x58
321 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
323 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
324 #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
326 #define IT87_REG_TEMP456_ENABLE 0x77
328 static const u16 IT87_REG_TEMP_SRC1[] = { 0x21d, 0x21e, 0x21f };
329 #define IT87_REG_TEMP_SRC2 0x23d
331 #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
332 #define NUM_VIN_LIMIT 8
334 #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
335 #define NUM_FAN_DIV 3
336 #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
337 #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
339 struct it87_devices {
341 const char * const suffix;
345 u8 num_temp_map; /* Number of temperature sources for pwm */
348 u8 smbus_bitmap; /* SMBus enable bits in extra config register */
349 u8 ec_special_config;
352 #define FEAT_12MV_ADC BIT(0)
353 #define FEAT_NEWER_AUTOPWM BIT(1)
354 #define FEAT_OLD_AUTOPWM BIT(2)
355 #define FEAT_16BIT_FANS BIT(3)
356 #define FEAT_TEMP_PECI BIT(5)
357 #define FEAT_TEMP_OLD_PECI BIT(6)
358 #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
359 #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
360 #define FEAT_VID BIT(9) /* Set if chip supports VID */
361 #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
362 #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
363 #define FEAT_10_9MV_ADC BIT(12)
364 #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
365 #define FEAT_FIVE_PWM BIT(14) /* Chip supports 5 pwm chn */
366 #define FEAT_SIX_PWM BIT(15) /* Chip supports 6 pwm chn */
367 #define FEAT_PWM_FREQ2 BIT(16) /* Separate pwm freq 2 */
368 #define FEAT_SIX_TEMP BIT(17) /* Up to 6 temp sensors */
369 #define FEAT_VIN3_5V BIT(18) /* VIN3 connected to +5V */
370 #define FEAT_FOUR_FANS BIT(19) /* Supports four fans */
371 #define FEAT_FOUR_PWM BIT(20) /* Supports four fan controls */
372 #define FEAT_BANK_SEL BIT(21) /* Chip has multi-bank support */
373 #define FEAT_SCALING BIT(22) /* Internal voltage scaling */
374 #define FEAT_FANCTL_ONOFF BIT(23) /* chip has FAN_CTL ON/OFF */
375 #define FEAT_11MV_ADC BIT(24)
376 #define FEAT_NEW_TEMPMAP BIT(25) /* new temp input selection */
377 #define FEAT_MMIO BIT(26) /* Chip supports MMIO */
378 #define FEAT_FOUR_TEMP BIT(27)
380 static const struct it87_devices it87_devices[] = {
384 .features = FEAT_OLD_AUTOPWM | FEAT_FANCTL_ONOFF,
385 /* may need to overwrite */
387 .num_temp_offset = 0,
393 .features = FEAT_OLD_AUTOPWM | FEAT_VID | FEAT_FANCTL_ONOFF,
394 /* may need to overwrite */
396 .num_temp_offset = 0,
402 .features = FEAT_16BIT_FANS | FEAT_VID
403 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2
406 .num_temp_offset = 3,
412 .features = FEAT_16BIT_FANS | FEAT_VID
413 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
414 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
416 .num_temp_offset = 3,
418 .old_peci_mask = 0x4,
423 .features = FEAT_16BIT_FANS | FEAT_VID
424 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
425 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
427 .num_temp_offset = 3,
429 .old_peci_mask = 0x4,
434 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
435 | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
436 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
437 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
439 .num_temp_offset = 3,
442 .old_peci_mask = 0x02, /* Actually reports PCH */
447 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
448 | FEAT_TEMP_PECI | FEAT_FIVE_FANS
449 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_SCALING
452 .num_temp_offset = 3,
459 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
460 | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
461 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_FOUR_FANS
462 | FEAT_FOUR_PWM | FEAT_FANCTL_ONOFF,
464 .num_temp_offset = 3,
467 .old_peci_mask = 0x02, /* Actually reports PCH */
472 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
473 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
474 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
475 /* PECI: guesswork */
477 /* 16 bit fans (OHM) */
478 /* three fans, always 16 bit (guesswork) */
480 .num_temp_offset = 3,
487 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
488 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
489 | FEAT_PWM_FREQ2 | FEAT_SCALING | FEAT_FANCTL_ONOFF,
490 /* PECI (coreboot) */
491 /* 12mV ADC (HWSensors4, OHM) */
492 /* 16 bit fans (HWSensors4, OHM) */
493 /* three fans, always 16 bit (datasheet) */
495 .num_temp_offset = 3,
502 .features = FEAT_16BIT_FANS
503 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
506 .num_temp_offset = 3,
508 .old_peci_mask = 0x4,
513 .features = FEAT_16BIT_FANS
514 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
517 .num_temp_offset = 3,
519 .old_peci_mask = 0x4,
524 .features = FEAT_16BIT_FANS
525 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2
528 .num_temp_offset = 3,
530 .old_peci_mask = 0x4,
535 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
536 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
537 | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
539 .num_temp_offset = 3,
546 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
547 | FEAT_16BIT_FANS | FEAT_TEMP_PECI
548 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
550 .num_temp_offset = 3,
557 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
558 | FEAT_16BIT_FANS | FEAT_TEMP_PECI
559 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_FANCTL_ONOFF,
561 .num_temp_offset = 3,
568 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
569 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
570 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
572 .num_temp_offset = 3,
579 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
580 | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL | FEAT_NEW_TEMPMAP
581 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING
584 .num_temp_offset = 3,
591 .features = FEAT_NEWER_AUTOPWM | FEAT_11MV_ADC | FEAT_16BIT_FANS
592 | FEAT_TEMP_PECI | FEAT_FIVE_FANS
593 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
594 | FEAT_AVCC3 | FEAT_SCALING | FEAT_NEW_TEMPMAP,
596 .num_temp_offset = 6,
603 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
604 | FEAT_TEMP_PECI | FEAT_SIX_FANS
605 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
606 | FEAT_SIX_TEMP | FEAT_VIN3_5V | FEAT_SCALING
609 .num_temp_offset = 3,
616 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
617 | FEAT_TEMP_PECI | FEAT_FIVE_FANS | FEAT_FOUR_TEMP
618 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
619 | FEAT_AVCC3 | FEAT_VIN3_5V | FEAT_SCALING,
621 .num_temp_offset = 3,
624 .smbus_bitmap = BIT(1) | BIT(2),
629 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
630 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP
631 | FEAT_11MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
632 | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_SCALING,
634 .num_temp_offset = 6,
636 .smbus_bitmap = BIT(1) | BIT(2),
641 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
642 | FEAT_TEMP_PECI | FEAT_SIX_FANS
643 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
644 | FEAT_SIX_TEMP | FEAT_SCALING | FEAT_AVCC3
647 .num_temp_offset = 3,
654 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
655 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
656 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_BANK_SEL
659 .num_temp_offset = 6,
661 .smbus_bitmap = BIT(2),
666 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
667 | FEAT_AVCC3 | FEAT_NEW_TEMPMAP | FEAT_SCALING
668 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
669 | FEAT_SIX_PWM | FEAT_BANK_SEL | FEAT_MMIO,
671 .num_temp_offset = 6,
673 .smbus_bitmap = BIT(2),
678 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
679 | FEAT_SIX_FANS | FEAT_NEW_TEMPMAP
680 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
681 | FEAT_SIX_TEMP | FEAT_BANK_SEL | FEAT_SCALING | FEAT_AVCC3,
683 .num_temp_offset = 6,
685 .smbus_bitmap = BIT(1) | BIT(2),
689 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
690 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
691 #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
692 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
693 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
694 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
695 ((data)->peci_mask & BIT(nr)))
696 #define has_temp_old_peci(data, nr) \
697 (((data)->features & FEAT_TEMP_OLD_PECI) && \
698 ((data)->old_peci_mask & BIT(nr)))
699 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
700 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
702 #define has_vid(data) ((data)->features & FEAT_VID)
703 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
704 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
705 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
706 #define has_five_pwm(data) ((data)->features & (FEAT_FIVE_PWM \
708 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
709 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
710 #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
711 #define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
712 #define has_four_fans(data) ((data)->features & (FEAT_FOUR_FANS | \
715 #define has_four_pwm(data) ((data)->features & (FEAT_FOUR_PWM | \
718 #define has_bank_sel(data) ((data)->features & FEAT_BANK_SEL)
719 #define has_scaling(data) ((data)->features & FEAT_SCALING)
720 #define has_fanctl_onoff(data) ((data)->features & FEAT_FANCTL_ONOFF)
721 #define has_11mv_adc(data) ((data)->features & FEAT_11MV_ADC)
722 #define has_new_tempmap(data) ((data)->features & FEAT_NEW_TEMPMAP)
723 #define has_mmio(data) ((data)->features & FEAT_MMIO)
724 #define has_four_temp(data) ((data)->features & FEAT_FOUR_TEMP)
726 struct it87_sio_data {
730 /* Values read from Super-I/O config space */
734 u8 internal; /* Internal sensors can be labeled */
735 /* Features skipped based on config or DMI */
742 u8 ec_special_config;
746 * For each registered chip, we need to keep some data in memory.
747 * The structure is dynamically allocated.
750 const struct attribute_group *groups[7];
756 u8 smbus_bitmap; /* !=0 if SMBus needs to be disabled */
757 u8 ec_special_config; /* EC special config register restore value */
758 u8 sioaddr; /* SIO port address */
759 bool doexit; /* true if exit from sio config is ok */
761 void __iomem *mmio; /* Remapped MMIO address if available */
762 int (*read)(struct it87_data *, u16);
763 void (*write)(struct it87_data *, u16, u8);
767 const u8 *REG_FAN_MIN;
768 const u8 *REG_FANX_MIN;
772 const u8 *REG_TEMP_OFFSET;
773 const u8 *REG_TEMP_LOW;
774 const u8 *REG_TEMP_HIGH;
778 struct mutex update_lock;
779 char valid; /* !=0 if following fields are valid */
780 unsigned long last_updated; /* In jiffies */
782 u16 in_scaled; /* Internal voltage sensors are scaled */
783 u16 in_internal; /* Bitfield, internal sensors (for labels) */
784 u16 has_in; /* Bitfield, voltage sensors enabled */
785 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
786 u8 has_fan; /* Bitfield, fans enabled */
787 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
788 u8 has_temp; /* Bitfield, temp sensors enabled */
789 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
790 u8 num_temp_limit; /* Number of temperature limit registers */
791 u8 num_temp_offset; /* Number of temperature offset registers */
792 u8 temp_src[4]; /* Up to 4 temperature source registers */
793 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
794 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
795 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
796 bool has_vid; /* True if VID supported */
797 u8 vid; /* Register encoding, combined */
799 u32 alarms; /* Register encoding, combined */
800 bool has_beep; /* true if beep supported */
801 u8 beeps; /* Register encoding */
802 u8 fan_main_ctrl; /* Register value */
803 u8 fan_ctl; /* Register value */
806 * The following 3 arrays correspond to the same registers up to
807 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
808 * 7, and we want to preserve settings on mode changes, so we have
809 * to track all values separately.
810 * Starting with the IT8721F, the manual PWM duty cycles are stored
811 * in separate registers (8-bit values), so the separate tracking
812 * is no longer needed, but it is still done to keep the driver
815 u8 has_pwm; /* Bitfield, pwm control enabled */
816 u8 pwm_ctrl[NUM_PWM]; /* Register value */
817 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
818 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
819 u8 pwm_temp_map_mask; /* 0x03 for old, 0x07 for new temp map */
820 u8 pwm_temp_map_shift; /* 0 for old, 3 for new temp map */
821 u8 pwm_num_temp_map; /* from config data, 3..7 depending on chip */
823 /* Automatic fan speed control registers */
824 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
825 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
828 static int adc_lsb(const struct it87_data *data, int nr)
832 if (has_12mv_adc(data))
834 else if (has_10_9mv_adc(data))
836 else if (has_11mv_adc(data))
840 if (data->in_scaled & BIT(nr))
845 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
847 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
848 return clamp_val(val, 0, 255);
851 static int in_from_reg(const struct it87_data *data, int nr, int val)
853 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
856 static inline u8 FAN_TO_REG(long rpm, int div)
860 rpm = clamp_val(rpm, 1, 1000000);
861 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
864 static inline u16 FAN16_TO_REG(long rpm)
868 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
871 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
872 1350000 / ((val) * (div)))
873 /* The divider is fixed to 2 in 16-bit mode */
874 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
875 1350000 / ((val) * 2))
877 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
878 ((val) + 500) / 1000), -128, 127))
879 #define TEMP_FROM_REG(val) ((val) * 1000)
881 static u8 pwm_to_reg(const struct it87_data *data, long val)
883 if (has_newer_autopwm(data))
889 static int pwm_from_reg(const struct it87_data *data, u8 reg)
891 if (has_newer_autopwm(data))
894 return (reg & 0x7f) << 1;
897 static int DIV_TO_REG(int val)
901 while (answer < 7 && (val >>= 1))
906 #define DIV_FROM_REG(val) BIT(val)
908 static u8 temp_map_from_reg(const struct it87_data *data, u8 reg)
912 map = (reg >> data->pwm_temp_map_shift) & data->pwm_temp_map_mask;
913 if (map >= data->pwm_num_temp_map) /* map is 0-based */
919 static u8 temp_map_to_reg(const struct it87_data *data, int nr, u8 map)
921 u8 ctrl = data->pwm_ctrl[nr];
923 return (ctrl & ~(data->pwm_temp_map_mask << data->pwm_temp_map_shift)) |
924 (map << data->pwm_temp_map_shift);
928 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
929 * depending on the chip type, to calculate the actual PWM frequency.
931 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
932 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
933 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
934 * sometimes just one. It is unknown if this is a datasheet error or real,
935 * so this is ignored for now.
937 static const unsigned int pwm_freq[8] = {
948 static int smbus_disable(struct it87_data *data)
952 if (data->smbus_bitmap) {
953 err = superio_enter(data->sioaddr);
956 superio_select(data->sioaddr, PME);
957 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
958 data->ec_special_config & ~data->smbus_bitmap);
959 superio_exit(data->sioaddr, data->doexit);
964 static int smbus_enable(struct it87_data *data)
968 if (data->smbus_bitmap) {
969 err = superio_enter(data->sioaddr);
973 superio_select(data->sioaddr, PME);
974 superio_outb(data->sioaddr, IT87_SPECIAL_CFG_REG,
975 data->ec_special_config);
976 superio_exit(data->sioaddr, data->doexit);
981 static int _it87_io_read(struct it87_data *data, u16 reg)
983 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
984 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
987 static void _it87_io_write(struct it87_data *data, u16 reg, u8 value)
989 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
990 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
993 static u8 it87_io_set_bank(struct it87_data *data, u8 bank)
997 if (has_bank_sel(data)) {
998 u8 breg = _it87_io_read(data, IT87_REG_BANK);
1001 if (bank != _bank) {
1003 breg |= (bank << 5);
1004 _it87_io_write(data, IT87_REG_BANK, breg);
1011 * Must be called with data->update_lock held, except during initialization.
1012 * Must be called with SMBus accesses disabled.
1013 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1014 * would slow down the IT87 access and should not be necessary.
1016 static int it87_io_read(struct it87_data *data, u16 reg)
1021 bank = it87_io_set_bank(data, reg >> 8);
1022 val = _it87_io_read(data, reg & 0xff);
1023 it87_io_set_bank(data, bank);
1029 * Must be called with data->update_lock held, except during initialization.
1030 * Must be called with SMBus accesses disabled
1031 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1032 * would slow down the IT87 access and should not be necessary.
1034 static void it87_io_write(struct it87_data *data, u16 reg, u8 value)
1038 bank = it87_io_set_bank(data, reg >> 8);
1039 _it87_io_write(data, reg & 0xff, value);
1040 it87_io_set_bank(data, bank);
1043 static int it87_mmio_read(struct it87_data *data, u16 reg)
1045 return readb(data->mmio + reg);
1048 static void it87_mmio_write(struct it87_data *data, u16 reg, u8 value)
1050 writeb(value, data->mmio + reg);
1053 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1057 ctrl = data->read(data, data->REG_PWM[nr]);
1058 data->pwm_ctrl[nr] = ctrl;
1059 if (has_newer_autopwm(data)) {
1060 data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1061 data->pwm_duty[nr] = data->read(data, IT87_REG_PWM_DUTY[nr]);
1063 if (ctrl & 0x80) /* Automatic mode */
1064 data->pwm_temp_map[nr] = temp_map_from_reg(data, ctrl);
1065 else /* Manual mode */
1066 data->pwm_duty[nr] = ctrl & 0x7f;
1069 if (has_old_autopwm(data)) {
1072 for (i = 0; i < 5 ; i++)
1073 data->auto_temp[nr][i] = data->read(data,
1074 IT87_REG_AUTO_TEMP(nr, i));
1075 for (i = 0; i < 3 ; i++)
1076 data->auto_pwm[nr][i] = data->read(data,
1077 IT87_REG_AUTO_PWM(nr, i));
1078 } else if (has_newer_autopwm(data)) {
1082 * 0: temperature hysteresis (base + 5)
1083 * 1: fan off temperature (base + 0)
1084 * 2: fan start temperature (base + 1)
1085 * 3: fan max temperature (base + 2)
1087 data->auto_temp[nr][0] =
1088 data->read(data, IT87_REG_AUTO_TEMP(nr, 5));
1090 for (i = 0; i < 3 ; i++)
1091 data->auto_temp[nr][i + 1] =
1092 data->read(data, IT87_REG_AUTO_TEMP(nr, i));
1094 * 0: start pwm value (base + 3)
1095 * 1: pwm slope (base + 4, 1/8th pwm)
1097 data->auto_pwm[nr][0] =
1098 data->read(data, IT87_REG_AUTO_TEMP(nr, 3));
1099 data->auto_pwm[nr][1] =
1100 data->read(data, IT87_REG_AUTO_TEMP(nr, 4));
1104 static int it87_lock(struct it87_data *data)
1108 mutex_lock(&data->update_lock);
1109 err = smbus_disable(data);
1111 mutex_unlock(&data->update_lock);
1115 static void it87_unlock(struct it87_data *data)
1118 mutex_unlock(&data->update_lock);
1121 static struct it87_data *it87_update_device(struct device *dev)
1123 struct it87_data *data = dev_get_drvdata(dev);
1127 err = it87_lock(data);
1129 return ERR_PTR(err);
1131 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
1135 * Cleared after each update, so reenable. Value
1136 * returned by this read will be previous value
1138 data->write(data, IT87_REG_CONFIG,
1139 data->read(data, IT87_REG_CONFIG) | 0x40);
1141 for (i = 0; i < NUM_VIN; i++) {
1142 if (!(data->has_in & BIT(i)))
1145 data->in[i][0] = data->read(data, IT87_REG_VIN[i]);
1147 /* VBAT and AVCC don't have limit registers */
1148 if (i >= NUM_VIN_LIMIT)
1151 data->in[i][1] = data->read(data, IT87_REG_VIN_MIN(i));
1152 data->in[i][2] = data->read(data, IT87_REG_VIN_MAX(i));
1155 for (i = 0; i < NUM_FAN; i++) {
1156 /* Skip disabled fans */
1157 if (!(data->has_fan & BIT(i)))
1160 data->fan[i][1] = data->read(data, data->REG_FAN_MIN[i]);
1161 data->fan[i][0] = data->read(data, data->REG_FAN[i]);
1162 /* Add high byte if in 16-bit mode */
1163 if (has_16bit_fans(data)) {
1164 data->fan[i][0] |= data->read(data,
1165 data->REG_FANX[i]) << 8;
1166 data->fan[i][1] |= data->read(data,
1167 data->REG_FANX_MIN[i]) << 8;
1170 for (i = 0; i < NUM_TEMP; i++) {
1171 if (!(data->has_temp & BIT(i)))
1174 data->read(data, IT87_REG_TEMP(i));
1176 if (i >= data->num_temp_limit)
1179 if (i < data->num_temp_offset)
1181 data->read(data, data->REG_TEMP_OFFSET[i]);
1184 data->read(data, data->REG_TEMP_LOW[i]);
1186 data->read(data, data->REG_TEMP_HIGH[i]);
1189 /* Newer chips don't have clock dividers */
1190 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1191 i = data->read(data, IT87_REG_FAN_DIV);
1192 data->fan_div[0] = i & 0x07;
1193 data->fan_div[1] = (i >> 3) & 0x07;
1194 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1198 data->read(data, IT87_REG_ALARM1) |
1199 (data->read(data, IT87_REG_ALARM2) << 8) |
1200 (data->read(data, IT87_REG_ALARM3) << 16);
1201 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
1203 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
1204 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL);
1205 for (i = 0; i < NUM_PWM; i++) {
1206 if (!(data->has_pwm & BIT(i)))
1208 it87_update_pwm_ctrl(data, i);
1211 data->sensor = data->read(data, IT87_REG_TEMP_ENABLE);
1212 data->extra = data->read(data, IT87_REG_TEMP_EXTRA);
1214 * The IT8705F does not have VID capability.
1215 * The IT8718F and later don't use IT87_REG_VID for the
1218 if (data->type == it8712 || data->type == it8716) {
1219 data->vid = data->read(data, IT87_REG_VID);
1221 * The older IT8712F revisions had only 5 VID pins,
1222 * but we assume it is always safe to read 6 bits.
1226 data->last_updated = jiffies;
1233 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
1236 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1237 struct it87_data *data = it87_update_device(dev);
1238 int index = sattr->index;
1242 return PTR_ERR(data);
1244 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
1247 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
1248 const char *buf, size_t count)
1250 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1251 struct it87_data *data = dev_get_drvdata(dev);
1252 int index = sattr->index;
1257 if (kstrtoul(buf, 10, &val) < 0)
1260 err = it87_lock(data);
1264 data->in[nr][index] = in_to_reg(data, nr, val);
1265 data->write(data, index == 1 ? IT87_REG_VIN_MIN(nr)
1266 : IT87_REG_VIN_MAX(nr),
1267 data->in[nr][index]);
1272 static SENSOR_DEVICE_ATTR_2(in0_input, 0444, show_in, NULL, 0, 0);
1273 static SENSOR_DEVICE_ATTR_2(in0_min, 0644, show_in, set_in, 0, 1);
1274 static SENSOR_DEVICE_ATTR_2(in0_max, 0644, show_in, set_in, 0, 2);
1276 static SENSOR_DEVICE_ATTR_2(in1_input, 0444, show_in, NULL, 1, 0);
1277 static SENSOR_DEVICE_ATTR_2(in1_min, 0644, show_in, set_in, 1, 1);
1278 static SENSOR_DEVICE_ATTR_2(in1_max, 0644, show_in, set_in, 1, 2);
1280 static SENSOR_DEVICE_ATTR_2(in2_input, 0444, show_in, NULL, 2, 0);
1281 static SENSOR_DEVICE_ATTR_2(in2_min, 0644, show_in, set_in, 2, 1);
1282 static SENSOR_DEVICE_ATTR_2(in2_max, 0644, show_in, set_in, 2, 2);
1284 static SENSOR_DEVICE_ATTR_2(in3_input, 0444, show_in, NULL, 3, 0);
1285 static SENSOR_DEVICE_ATTR_2(in3_min, 0644, show_in, set_in, 3, 1);
1286 static SENSOR_DEVICE_ATTR_2(in3_max, 0644, show_in, set_in, 3, 2);
1288 static SENSOR_DEVICE_ATTR_2(in4_input, 0444, show_in, NULL, 4, 0);
1289 static SENSOR_DEVICE_ATTR_2(in4_min, 0644, show_in, set_in, 4, 1);
1290 static SENSOR_DEVICE_ATTR_2(in4_max, 0644, show_in, set_in, 4, 2);
1292 static SENSOR_DEVICE_ATTR_2(in5_input, 0444, show_in, NULL, 5, 0);
1293 static SENSOR_DEVICE_ATTR_2(in5_min, 0644, show_in, set_in, 5, 1);
1294 static SENSOR_DEVICE_ATTR_2(in5_max, 0644, show_in, set_in, 5, 2);
1296 static SENSOR_DEVICE_ATTR_2(in6_input, 0444, show_in, NULL, 6, 0);
1297 static SENSOR_DEVICE_ATTR_2(in6_min, 0644, show_in, set_in, 6, 1);
1298 static SENSOR_DEVICE_ATTR_2(in6_max, 0644, show_in, set_in, 6, 2);
1300 static SENSOR_DEVICE_ATTR_2(in7_input, 0444, show_in, NULL, 7, 0);
1301 static SENSOR_DEVICE_ATTR_2(in7_min, 0644, show_in, set_in, 7, 1);
1302 static SENSOR_DEVICE_ATTR_2(in7_max, 0644, show_in, set_in, 7, 2);
1304 static SENSOR_DEVICE_ATTR_2(in8_input, 0444, show_in, NULL, 8, 0);
1305 static SENSOR_DEVICE_ATTR_2(in9_input, 0444, show_in, NULL, 9, 0);
1306 static SENSOR_DEVICE_ATTR_2(in10_input, 0444, show_in, NULL, 10, 0);
1307 static SENSOR_DEVICE_ATTR_2(in11_input, 0444, show_in, NULL, 11, 0);
1308 static SENSOR_DEVICE_ATTR_2(in12_input, 0444, show_in, NULL, 12, 0);
1310 /* Up to 6 temperatures */
1311 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
1314 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1316 int index = sattr->index;
1317 struct it87_data *data = it87_update_device(dev);
1320 return PTR_ERR(data);
1322 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
1325 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
1326 const char *buf, size_t count)
1328 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1330 int index = sattr->index;
1331 struct it87_data *data = dev_get_drvdata(dev);
1336 if (kstrtol(buf, 10, &val) < 0)
1339 err = it87_lock(data);
1346 reg = data->REG_TEMP_LOW[nr];
1349 reg = data->REG_TEMP_HIGH[nr];
1352 regval = data->read(data, IT87_REG_BEEP_ENABLE);
1353 if (!(regval & 0x80)) {
1355 data->write(data, IT87_REG_BEEP_ENABLE, regval);
1358 reg = data->REG_TEMP_OFFSET[nr];
1362 data->temp[nr][index] = TEMP_TO_REG(val);
1363 data->write(data, reg, data->temp[nr][index]);
1368 static SENSOR_DEVICE_ATTR_2(temp1_input, 0444, show_temp, NULL, 0, 0);
1369 static SENSOR_DEVICE_ATTR_2(temp1_min, 0644, show_temp, set_temp, 0, 1);
1370 static SENSOR_DEVICE_ATTR_2(temp1_max, 0644, show_temp, set_temp, 0, 2);
1371 static SENSOR_DEVICE_ATTR_2(temp1_offset, 0644, show_temp, set_temp, 0, 3);
1372 static SENSOR_DEVICE_ATTR_2(temp2_input, 0444, show_temp, NULL, 1, 0);
1373 static SENSOR_DEVICE_ATTR_2(temp2_min, 0644, show_temp, set_temp, 1, 1);
1374 static SENSOR_DEVICE_ATTR_2(temp2_max, 0644, show_temp, set_temp, 1, 2);
1375 static SENSOR_DEVICE_ATTR_2(temp2_offset, 0644, show_temp, set_temp, 1, 3);
1376 static SENSOR_DEVICE_ATTR_2(temp3_input, 0444, show_temp, NULL, 2, 0);
1377 static SENSOR_DEVICE_ATTR_2(temp3_min, 0644, show_temp, set_temp, 2, 1);
1378 static SENSOR_DEVICE_ATTR_2(temp3_max, 0644, show_temp, set_temp, 2, 2);
1379 static SENSOR_DEVICE_ATTR_2(temp3_offset, 0644, show_temp, set_temp, 2, 3);
1380 static SENSOR_DEVICE_ATTR_2(temp4_input, 0444, show_temp, NULL, 3, 0);
1381 static SENSOR_DEVICE_ATTR_2(temp4_min, 0644, show_temp, set_temp, 3, 1);
1382 static SENSOR_DEVICE_ATTR_2(temp4_max, 0644, show_temp, set_temp, 3, 2);
1383 static SENSOR_DEVICE_ATTR_2(temp4_offset, 0644, show_temp, set_temp, 3, 3);
1384 static SENSOR_DEVICE_ATTR_2(temp5_input, 0444, show_temp, NULL, 4, 0);
1385 static SENSOR_DEVICE_ATTR_2(temp5_min, 0644, show_temp, set_temp, 4, 1);
1386 static SENSOR_DEVICE_ATTR_2(temp5_max, 0644, show_temp, set_temp, 4, 2);
1387 static SENSOR_DEVICE_ATTR_2(temp5_offset, 0644, show_temp, set_temp, 4, 3);
1388 static SENSOR_DEVICE_ATTR_2(temp6_input, 0444, show_temp, NULL, 5, 0);
1389 static SENSOR_DEVICE_ATTR_2(temp6_min, 0644, show_temp, set_temp, 5, 1);
1390 static SENSOR_DEVICE_ATTR_2(temp6_max, 0644, show_temp, set_temp, 5, 2);
1391 static SENSOR_DEVICE_ATTR_2(temp6_offset, 0644, show_temp, set_temp, 5, 3);
1393 static const u8 temp_types_8686[NUM_TEMP][9] = {
1394 { 0, 8, 8, 8, 8, 8, 8, 8, 7 },
1395 { 0, 6, 8, 8, 6, 0, 0, 0, 7 },
1396 { 0, 6, 5, 8, 6, 0, 0, 0, 7 },
1397 { 4, 8, 8, 8, 8, 8, 8, 8, 7 },
1398 { 4, 6, 8, 8, 6, 0, 0, 0, 7 },
1399 { 4, 6, 5, 8, 6, 0, 0, 0, 7 },
1402 static int get_temp_type(struct it87_data *data, int index)
1405 int ttype, type = 0;
1407 if (has_bank_sel(data)) {
1410 src1 = (data->temp_src[index / 2] >> ((index % 2) * 4)) & 0x0f;
1412 switch (data->type) {
1415 type = temp_types_8686[index][src1];
1426 src2 = data->temp_src[3];
1429 type = (src2 & BIT(index)) ? 6 : 5;
1432 type = (src2 & BIT(index)) ? 4 : 6;
1435 type = (src2 & BIT(index)) ? 5 : 0;
1448 /* Dectect PECI vs. AMDTSI if possible */
1450 if ((has_temp_peci(data, index)) && data->type != it8721) {
1451 extra = data->read(data, 0x98); /* PCH/AMDTSI host status */
1456 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1458 /* Per chip special detection */
1459 switch (data->type) {
1461 if (!(reg & 0xc0) && index == 3)
1468 if (type || index >= 3)
1471 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1473 if ((has_temp_peci(data, index) && (reg >> 6 == index + 1)) ||
1474 (has_temp_old_peci(data, index) && (extra & 0x80)))
1475 type = ttype; /* Intel PECI or AMDTSI */
1476 if (reg & BIT(index))
1477 type = 3; /* thermal diode */
1478 else if (reg & BIT(index + 3))
1479 type = 4; /* thermistor */
1484 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1487 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1488 struct it87_data *data = it87_update_device(dev);
1492 return PTR_ERR(data);
1494 type = get_temp_type(data, sensor_attr->index);
1495 return sprintf(buf, "%d\n", type);
1498 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1499 const char *buf, size_t count)
1501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1502 int nr = sensor_attr->index;
1504 struct it87_data *data = dev_get_drvdata(dev);
1509 if (kstrtol(buf, 10, &val) < 0)
1512 err = it87_lock(data);
1516 reg = data->read(data, IT87_REG_TEMP_ENABLE);
1519 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1521 extra = data->read(data, IT87_REG_TEMP_EXTRA);
1522 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1524 if (val == 2) { /* backwards compatibility */
1526 "Sensor type 2 is deprecated, please use 4 instead\n");
1529 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1534 else if (has_temp_peci(data, nr) && val == 6)
1535 reg |= (nr + 1) << 6;
1536 else if (has_temp_old_peci(data, nr) && val == 6)
1538 else if (val != 0) {
1544 data->extra = extra;
1545 data->write(data, IT87_REG_TEMP_ENABLE, data->sensor);
1546 if (has_temp_old_peci(data, nr))
1547 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1548 data->valid = 0; /* Force cache refresh */
1554 static SENSOR_DEVICE_ATTR(temp1_type, 0644, show_temp_type, set_temp_type, 0);
1555 static SENSOR_DEVICE_ATTR(temp2_type, 0644, show_temp_type, set_temp_type, 1);
1556 static SENSOR_DEVICE_ATTR(temp3_type, 0644, show_temp_type, set_temp_type, 2);
1557 static SENSOR_DEVICE_ATTR(temp4_type, 0644, show_temp_type, set_temp_type, 3);
1558 static SENSOR_DEVICE_ATTR(temp5_type, 0644, show_temp_type, set_temp_type, 4);
1559 static SENSOR_DEVICE_ATTR(temp6_type, 0644, show_temp_type, set_temp_type, 5);
1563 static int pwm_mode(const struct it87_data *data, int nr)
1565 if (has_fanctl_onoff(data) && nr < 3 &&
1566 !(data->fan_main_ctrl & BIT(nr)))
1567 return 0; /* Full speed */
1568 if (data->pwm_ctrl[nr] & 0x80)
1569 return 2; /* Automatic mode */
1570 if ((!has_fanctl_onoff(data) || nr >= 3) &&
1571 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1572 return 0; /* Full speed */
1574 return 1; /* Manual mode */
1577 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1580 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1582 int index = sattr->index;
1584 struct it87_data *data = it87_update_device(dev);
1587 return PTR_ERR(data);
1589 speed = has_16bit_fans(data) ?
1590 FAN16_FROM_REG(data->fan[nr][index]) :
1591 FAN_FROM_REG(data->fan[nr][index],
1592 DIV_FROM_REG(data->fan_div[nr]));
1593 return sprintf(buf, "%d\n", speed);
1596 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1599 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1600 struct it87_data *data = it87_update_device(dev);
1601 int nr = sensor_attr->index;
1604 return PTR_ERR(data);
1606 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1609 static ssize_t show_pwm_enable(struct device *dev,
1610 struct device_attribute *attr, char *buf)
1612 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1613 struct it87_data *data = it87_update_device(dev);
1614 int nr = sensor_attr->index;
1617 return PTR_ERR(data);
1619 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1622 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1625 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1626 struct it87_data *data = it87_update_device(dev);
1627 int nr = sensor_attr->index;
1630 return PTR_ERR(data);
1632 return sprintf(buf, "%d\n",
1633 pwm_from_reg(data, data->pwm_duty[nr]));
1636 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1639 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1640 struct it87_data *data = it87_update_device(dev);
1641 int nr = sensor_attr->index;
1646 return PTR_ERR(data);
1648 if (has_pwm_freq2(data) && nr == 1)
1649 index = (data->extra >> 4) & 0x07;
1651 index = (data->fan_ctl >> 4) & 0x07;
1653 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1655 return sprintf(buf, "%u\n", freq);
1658 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1659 const char *buf, size_t count)
1661 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1663 int index = sattr->index;
1665 struct it87_data *data = dev_get_drvdata(dev);
1670 if (kstrtol(buf, 10, &val) < 0)
1673 err = it87_lock(data);
1677 if (has_16bit_fans(data)) {
1678 data->fan[nr][index] = FAN16_TO_REG(val);
1679 data->write(data, data->REG_FAN_MIN[nr],
1680 data->fan[nr][index] & 0xff);
1681 data->write(data, data->REG_FANX_MIN[nr],
1682 data->fan[nr][index] >> 8);
1684 reg = data->read(data, IT87_REG_FAN_DIV);
1687 data->fan_div[nr] = reg & 0x07;
1690 data->fan_div[nr] = (reg >> 3) & 0x07;
1693 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1696 data->fan[nr][index] =
1697 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1698 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][index]);
1704 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1705 const char *buf, size_t count)
1707 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1708 struct it87_data *data = dev_get_drvdata(dev);
1709 int nr = sensor_attr->index;
1714 if (kstrtoul(buf, 10, &val) < 0)
1717 err = it87_lock(data);
1721 old = data->read(data, IT87_REG_FAN_DIV);
1723 /* Save fan min limit */
1724 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1729 data->fan_div[nr] = DIV_TO_REG(val);
1733 data->fan_div[nr] = 1;
1735 data->fan_div[nr] = 3;
1738 val |= (data->fan_div[0] & 0x07);
1739 val |= (data->fan_div[1] & 0x07) << 3;
1740 if (data->fan_div[2] == 3)
1742 data->write(data, IT87_REG_FAN_DIV, val);
1744 /* Restore fan min limit */
1745 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1746 data->write(data, data->REG_FAN_MIN[nr], data->fan[nr][1]);
1751 /* Returns 0 if OK, -EINVAL otherwise */
1752 static int check_trip_points(struct device *dev, int nr)
1754 const struct it87_data *data = dev_get_drvdata(dev);
1757 if (has_old_autopwm(data)) {
1758 for (i = 0; i < 3; i++) {
1759 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1762 for (i = 0; i < 2; i++) {
1763 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1766 } else if (has_newer_autopwm(data)) {
1767 for (i = 1; i < 3; i++) {
1768 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1775 "Inconsistent trip points, not switching to automatic mode\n");
1776 dev_err(dev, "Adjust the trip points and try again\n");
1781 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1782 const char *buf, size_t count)
1784 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1785 struct it87_data *data = dev_get_drvdata(dev);
1786 int nr = sensor_attr->index;
1790 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1793 /* Check trip points before switching to automatic mode */
1795 if (check_trip_points(dev, nr) < 0)
1799 err = it87_lock(data);
1803 it87_update_pwm_ctrl(data, nr);
1806 if (nr < 3 && has_fanctl_onoff(data)) {
1808 /* make sure the fan is on when in on/off mode */
1809 tmp = data->read(data, IT87_REG_FAN_CTL);
1810 data->write(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1811 /* set on/off mode */
1812 data->fan_main_ctrl &= ~BIT(nr);
1813 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1814 data->fan_main_ctrl);
1818 /* No on/off mode, set maximum pwm value */
1819 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1820 data->write(data, IT87_REG_PWM_DUTY[nr],
1821 data->pwm_duty[nr]);
1822 /* and set manual mode */
1823 if (has_newer_autopwm(data)) {
1824 ctrl = temp_map_to_reg(data, nr,
1825 data->pwm_temp_map[nr]);
1828 ctrl = data->pwm_duty[nr];
1830 data->pwm_ctrl[nr] = ctrl;
1831 data->write(data, data->REG_PWM[nr], ctrl);
1836 if (has_newer_autopwm(data)) {
1837 ctrl = temp_map_to_reg(data, nr,
1838 data->pwm_temp_map[nr]);
1844 ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1846 data->pwm_ctrl[nr] = ctrl;
1847 data->write(data, data->REG_PWM[nr], ctrl);
1849 if (has_fanctl_onoff(data) && nr < 3) {
1850 /* set SmartGuardian mode */
1851 data->fan_main_ctrl |= BIT(nr);
1852 data->write(data, IT87_REG_FAN_MAIN_CTRL,
1853 data->fan_main_ctrl);
1860 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1861 const char *buf, size_t count)
1863 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1864 struct it87_data *data = dev_get_drvdata(dev);
1865 int nr = sensor_attr->index;
1869 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1872 err = it87_lock(data);
1876 it87_update_pwm_ctrl(data, nr);
1877 if (has_newer_autopwm(data)) {
1879 * If we are in automatic mode, the PWM duty cycle register
1880 * is read-only so we can't write the value.
1882 if (data->pwm_ctrl[nr] & 0x80) {
1886 data->pwm_duty[nr] = pwm_to_reg(data, val);
1887 data->write(data, IT87_REG_PWM_DUTY[nr],
1888 data->pwm_duty[nr]);
1890 data->pwm_duty[nr] = pwm_to_reg(data, val);
1892 * If we are in manual mode, write the duty cycle immediately;
1893 * otherwise, just store it for later use.
1895 if (!(data->pwm_ctrl[nr] & 0x80)) {
1896 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1897 data->write(data, data->REG_PWM[nr],
1898 data->pwm_ctrl[nr]);
1906 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1907 const char *buf, size_t count)
1909 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1910 struct it87_data *data = dev_get_drvdata(dev);
1911 int nr = sensor_attr->index;
1916 if (kstrtoul(buf, 10, &val) < 0)
1919 val = clamp_val(val, 0, 1000000);
1920 val *= has_newer_autopwm(data) ? 256 : 128;
1922 /* Search for the nearest available frequency */
1923 for (i = 0; i < 7; i++) {
1924 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1928 err = it87_lock(data);
1933 data->fan_ctl = data->read(data, IT87_REG_FAN_CTL) & 0x8f;
1934 data->fan_ctl |= i << 4;
1935 data->write(data, IT87_REG_FAN_CTL, data->fan_ctl);
1937 data->extra = data->read(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1938 data->extra |= i << 4;
1939 data->write(data, IT87_REG_TEMP_EXTRA, data->extra);
1945 static ssize_t show_pwm_temp_map(struct device *dev,
1946 struct device_attribute *attr, char *buf)
1948 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1949 struct it87_data *data = it87_update_device(dev);
1950 int nr = sensor_attr->index;
1953 return PTR_ERR(data);
1955 return sprintf(buf, "%d\n", data->pwm_temp_map[nr] + 1);
1958 static ssize_t set_pwm_temp_map(struct device *dev,
1959 struct device_attribute *attr, const char *buf,
1962 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1963 struct it87_data *data = dev_get_drvdata(dev);
1964 int nr = sensor_attr->index;
1969 if (kstrtoul(buf, 10, &val) < 0)
1972 if (!val || val > data->pwm_num_temp_map)
1977 err = it87_lock(data);
1981 it87_update_pwm_ctrl(data, nr);
1982 data->pwm_temp_map[nr] = map;
1984 * If we are in automatic mode, write the temp mapping immediately;
1985 * otherwise, just store it for later use.
1987 if (data->pwm_ctrl[nr] & 0x80) {
1988 data->pwm_ctrl[nr] = temp_map_to_reg(data, nr, map);
1989 data->write(data, data->REG_PWM[nr], data->pwm_ctrl[nr]);
1995 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1998 struct it87_data *data = it87_update_device(dev);
1999 struct sensor_device_attribute_2 *sensor_attr =
2000 to_sensor_dev_attr_2(attr);
2001 int nr = sensor_attr->nr;
2002 int point = sensor_attr->index;
2005 return PTR_ERR(data);
2007 return sprintf(buf, "%d\n",
2008 pwm_from_reg(data, data->auto_pwm[nr][point]));
2011 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
2012 const char *buf, size_t count)
2014 struct it87_data *data = dev_get_drvdata(dev);
2015 struct sensor_device_attribute_2 *sensor_attr =
2016 to_sensor_dev_attr_2(attr);
2017 int nr = sensor_attr->nr;
2018 int point = sensor_attr->index;
2023 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
2026 err = it87_lock(data);
2030 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
2031 if (has_newer_autopwm(data))
2032 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
2034 regaddr = IT87_REG_AUTO_PWM(nr, point);
2035 data->write(data, regaddr, data->auto_pwm[nr][point]);
2040 static ssize_t show_auto_pwm_slope(struct device *dev,
2041 struct device_attribute *attr, char *buf)
2043 struct it87_data *data = it87_update_device(dev);
2044 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2045 int nr = sensor_attr->index;
2048 return PTR_ERR(data);
2050 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
2053 static ssize_t set_auto_pwm_slope(struct device *dev,
2054 struct device_attribute *attr,
2055 const char *buf, size_t count)
2057 struct it87_data *data = dev_get_drvdata(dev);
2058 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
2059 int nr = sensor_attr->index;
2063 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
2066 err = it87_lock(data);
2070 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
2071 data->write(data, IT87_REG_AUTO_TEMP(nr, 4), data->auto_pwm[nr][1]);
2076 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
2079 struct it87_data *data = it87_update_device(dev);
2080 struct sensor_device_attribute_2 *sensor_attr =
2081 to_sensor_dev_attr_2(attr);
2082 int nr = sensor_attr->nr;
2083 int point = sensor_attr->index;
2087 return PTR_ERR(data);
2089 if (has_old_autopwm(data) || point)
2090 reg = data->auto_temp[nr][point];
2092 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
2094 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
2097 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
2098 const char *buf, size_t count)
2100 struct it87_data *data = dev_get_drvdata(dev);
2101 struct sensor_device_attribute_2 *sensor_attr =
2102 to_sensor_dev_attr_2(attr);
2103 int nr = sensor_attr->nr;
2104 int point = sensor_attr->index;
2109 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
2112 err = it87_lock(data);
2116 if (has_newer_autopwm(data) && !point) {
2117 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
2118 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
2119 data->auto_temp[nr][0] = reg;
2120 data->write(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
2122 reg = TEMP_TO_REG(val);
2123 data->auto_temp[nr][point] = reg;
2124 if (has_newer_autopwm(data))
2126 data->write(data, IT87_REG_AUTO_TEMP(nr, point), reg);
2132 static SENSOR_DEVICE_ATTR_2(fan1_input, 0444, show_fan, NULL, 0, 0);
2133 static SENSOR_DEVICE_ATTR_2(fan1_min, 0644, show_fan, set_fan, 0, 1);
2134 static SENSOR_DEVICE_ATTR(fan1_div, 0644, show_fan_div, set_fan_div, 0);
2136 static SENSOR_DEVICE_ATTR_2(fan2_input, 0444, show_fan, NULL, 1, 0);
2137 static SENSOR_DEVICE_ATTR_2(fan2_min, 0644, show_fan, set_fan, 1, 1);
2138 static SENSOR_DEVICE_ATTR(fan2_div, 0644, show_fan_div, set_fan_div, 1);
2140 static SENSOR_DEVICE_ATTR_2(fan3_input, 0444, show_fan, NULL, 2, 0);
2141 static SENSOR_DEVICE_ATTR_2(fan3_min, 0644, show_fan, set_fan, 2, 1);
2142 static SENSOR_DEVICE_ATTR(fan3_div, 0644, show_fan_div, set_fan_div, 2);
2144 static SENSOR_DEVICE_ATTR_2(fan4_input, 0444, show_fan, NULL, 3, 0);
2145 static SENSOR_DEVICE_ATTR_2(fan4_min, 0644, show_fan, set_fan, 3, 1);
2147 static SENSOR_DEVICE_ATTR_2(fan5_input, 0444, show_fan, NULL, 4, 0);
2148 static SENSOR_DEVICE_ATTR_2(fan5_min, 0644, show_fan, set_fan, 4, 1);
2150 static SENSOR_DEVICE_ATTR_2(fan6_input, 0444, show_fan, NULL, 5, 0);
2151 static SENSOR_DEVICE_ATTR_2(fan6_min, 0644, show_fan, set_fan, 5, 1);
2153 static SENSOR_DEVICE_ATTR(pwm1_enable, 0644,
2154 show_pwm_enable, set_pwm_enable, 0);
2155 static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
2156 static SENSOR_DEVICE_ATTR(pwm1_freq, 0644, show_pwm_freq, set_pwm_freq, 0);
2157 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, 0444,
2158 show_pwm_temp_map, set_pwm_temp_map, 0);
2159 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, 0644,
2160 show_auto_pwm, set_auto_pwm, 0, 0);
2161 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, 0644,
2162 show_auto_pwm, set_auto_pwm, 0, 1);
2163 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, 0644,
2164 show_auto_pwm, set_auto_pwm, 0, 2);
2165 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, 0444,
2166 show_auto_pwm, NULL, 0, 3);
2167 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, 0644,
2168 show_auto_temp, set_auto_temp, 0, 1);
2169 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, 0644,
2170 show_auto_temp, set_auto_temp, 0, 0);
2171 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, 0644,
2172 show_auto_temp, set_auto_temp, 0, 2);
2173 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, 0644,
2174 show_auto_temp, set_auto_temp, 0, 3);
2175 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, 0644,
2176 show_auto_temp, set_auto_temp, 0, 4);
2177 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, 0644,
2178 show_auto_pwm, set_auto_pwm, 0, 0);
2179 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, 0644,
2180 show_auto_pwm_slope, set_auto_pwm_slope, 0);
2182 static SENSOR_DEVICE_ATTR(pwm2_enable, 0644,
2183 show_pwm_enable, set_pwm_enable, 1);
2184 static SENSOR_DEVICE_ATTR(pwm2, 0644, show_pwm, set_pwm, 1);
2185 static SENSOR_DEVICE_ATTR(pwm2_freq, 0444, show_pwm_freq, set_pwm_freq, 1);
2186 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, 0444,
2187 show_pwm_temp_map, set_pwm_temp_map, 1);
2188 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, 0644,
2189 show_auto_pwm, set_auto_pwm, 1, 0);
2190 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, 0644,
2191 show_auto_pwm, set_auto_pwm, 1, 1);
2192 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, 0644,
2193 show_auto_pwm, set_auto_pwm, 1, 2);
2194 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, 0444,
2195 show_auto_pwm, NULL, 1, 3);
2196 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, 0644,
2197 show_auto_temp, set_auto_temp, 1, 1);
2198 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, 0644,
2199 show_auto_temp, set_auto_temp, 1, 0);
2200 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, 0644,
2201 show_auto_temp, set_auto_temp, 1, 2);
2202 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, 0644,
2203 show_auto_temp, set_auto_temp, 1, 3);
2204 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, 0644,
2205 show_auto_temp, set_auto_temp, 1, 4);
2206 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, 0644,
2207 show_auto_pwm, set_auto_pwm, 1, 0);
2208 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, 0644,
2209 show_auto_pwm_slope, set_auto_pwm_slope, 1);
2211 static SENSOR_DEVICE_ATTR(pwm3_enable, 0644,
2212 show_pwm_enable, set_pwm_enable, 2);
2213 static SENSOR_DEVICE_ATTR(pwm3, 0644, show_pwm, set_pwm, 2);
2214 static SENSOR_DEVICE_ATTR(pwm3_freq, 0444, show_pwm_freq, NULL, 2);
2215 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, 0444,
2216 show_pwm_temp_map, set_pwm_temp_map, 2);
2217 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, 0644,
2218 show_auto_pwm, set_auto_pwm, 2, 0);
2219 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, 0644,
2220 show_auto_pwm, set_auto_pwm, 2, 1);
2221 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, 0644,
2222 show_auto_pwm, set_auto_pwm, 2, 2);
2223 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, 0444,
2224 show_auto_pwm, NULL, 2, 3);
2225 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, 0644,
2226 show_auto_temp, set_auto_temp, 2, 1);
2227 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, 0644,
2228 show_auto_temp, set_auto_temp, 2, 0);
2229 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, 0644,
2230 show_auto_temp, set_auto_temp, 2, 2);
2231 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, 0644,
2232 show_auto_temp, set_auto_temp, 2, 3);
2233 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, 0644,
2234 show_auto_temp, set_auto_temp, 2, 4);
2235 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, 0644,
2236 show_auto_pwm, set_auto_pwm, 2, 0);
2237 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, 0644,
2238 show_auto_pwm_slope, set_auto_pwm_slope, 2);
2240 static SENSOR_DEVICE_ATTR(pwm4_enable, 0644,
2241 show_pwm_enable, set_pwm_enable, 3);
2242 static SENSOR_DEVICE_ATTR(pwm4, 0644, show_pwm, set_pwm, 3);
2243 static SENSOR_DEVICE_ATTR(pwm4_freq, 0444, show_pwm_freq, NULL, 3);
2244 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, 0444,
2245 show_pwm_temp_map, set_pwm_temp_map, 3);
2246 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, 0644,
2247 show_auto_temp, set_auto_temp, 2, 1);
2248 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, 0644,
2249 show_auto_temp, set_auto_temp, 2, 0);
2250 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, 0644,
2251 show_auto_temp, set_auto_temp, 2, 2);
2252 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, 0644,
2253 show_auto_temp, set_auto_temp, 2, 3);
2254 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, 0644,
2255 show_auto_pwm, set_auto_pwm, 3, 0);
2256 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, 0644,
2257 show_auto_pwm_slope, set_auto_pwm_slope, 3);
2259 static SENSOR_DEVICE_ATTR(pwm5_enable, 0644,
2260 show_pwm_enable, set_pwm_enable, 4);
2261 static SENSOR_DEVICE_ATTR(pwm5, 0644, show_pwm, set_pwm, 4);
2262 static SENSOR_DEVICE_ATTR(pwm5_freq, 0444, show_pwm_freq, NULL, 4);
2263 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, 0444,
2264 show_pwm_temp_map, set_pwm_temp_map, 4);
2265 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, 0644,
2266 show_auto_temp, set_auto_temp, 2, 1);
2267 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, 0644,
2268 show_auto_temp, set_auto_temp, 2, 0);
2269 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, 0644,
2270 show_auto_temp, set_auto_temp, 2, 2);
2271 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, 0644,
2272 show_auto_temp, set_auto_temp, 2, 3);
2273 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, 0644,
2274 show_auto_pwm, set_auto_pwm, 4, 0);
2275 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, 0644,
2276 show_auto_pwm_slope, set_auto_pwm_slope, 4);
2278 static SENSOR_DEVICE_ATTR(pwm6_enable, 0644,
2279 show_pwm_enable, set_pwm_enable, 5);
2280 static SENSOR_DEVICE_ATTR(pwm6, 0644, show_pwm, set_pwm, 5);
2281 static SENSOR_DEVICE_ATTR(pwm6_freq, 0444, show_pwm_freq, NULL, 5);
2282 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, 0444,
2283 show_pwm_temp_map, set_pwm_temp_map, 5);
2284 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, 0644,
2285 show_auto_temp, set_auto_temp, 2, 1);
2286 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, 0644,
2287 show_auto_temp, set_auto_temp, 2, 0);
2288 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, 0644,
2289 show_auto_temp, set_auto_temp, 2, 2);
2290 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, 0644,
2291 show_auto_temp, set_auto_temp, 2, 3);
2292 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, 0644,
2293 show_auto_pwm, set_auto_pwm, 5, 0);
2294 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, 0644,
2295 show_auto_pwm_slope, set_auto_pwm_slope, 5);
2298 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
2301 struct it87_data *data = it87_update_device(dev);
2304 return PTR_ERR(data);
2306 return sprintf(buf, "%u\n", data->alarms);
2308 static DEVICE_ATTR(alarms, 0444, show_alarms, NULL);
2310 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
2313 struct it87_data *data = it87_update_device(dev);
2314 int bitnr = to_sensor_dev_attr(attr)->index;
2317 return PTR_ERR(data);
2319 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
2322 static ssize_t clear_intrusion(struct device *dev,
2323 struct device_attribute *attr, const char *buf,
2326 struct it87_data *data = dev_get_drvdata(dev);
2330 if (kstrtol(buf, 10, &val) < 0 || val != 0)
2333 err = it87_lock(data);
2337 config = data->read(data, IT87_REG_CONFIG);
2339 data->write(data, IT87_REG_CONFIG, config);
2340 /* Invalidate cache to force re-read */
2346 static SENSOR_DEVICE_ATTR(in0_alarm, 0444, show_alarm, NULL, 8);
2347 static SENSOR_DEVICE_ATTR(in1_alarm, 0444, show_alarm, NULL, 9);
2348 static SENSOR_DEVICE_ATTR(in2_alarm, 0444, show_alarm, NULL, 10);
2349 static SENSOR_DEVICE_ATTR(in3_alarm, 0444, show_alarm, NULL, 11);
2350 static SENSOR_DEVICE_ATTR(in4_alarm, 0444, show_alarm, NULL, 12);
2351 static SENSOR_DEVICE_ATTR(in5_alarm, 0444, show_alarm, NULL, 13);
2352 static SENSOR_DEVICE_ATTR(in6_alarm, 0444, show_alarm, NULL, 14);
2353 static SENSOR_DEVICE_ATTR(in7_alarm, 0444, show_alarm, NULL, 15);
2354 static SENSOR_DEVICE_ATTR(fan1_alarm, 0444, show_alarm, NULL, 0);
2355 static SENSOR_DEVICE_ATTR(fan2_alarm, 0444, show_alarm, NULL, 1);
2356 static SENSOR_DEVICE_ATTR(fan3_alarm, 0444, show_alarm, NULL, 2);
2357 static SENSOR_DEVICE_ATTR(fan4_alarm, 0444, show_alarm, NULL, 3);
2358 static SENSOR_DEVICE_ATTR(fan5_alarm, 0444, show_alarm, NULL, 6);
2359 static SENSOR_DEVICE_ATTR(fan6_alarm, 0444, show_alarm, NULL, 7);
2360 static SENSOR_DEVICE_ATTR(temp1_alarm, 0444, show_alarm, NULL, 16);
2361 static SENSOR_DEVICE_ATTR(temp2_alarm, 0444, show_alarm, NULL, 17);
2362 static SENSOR_DEVICE_ATTR(temp3_alarm, 0444, show_alarm, NULL, 18);
2363 static SENSOR_DEVICE_ATTR(temp4_alarm, 0444, show_alarm, NULL, 19);
2364 static SENSOR_DEVICE_ATTR(temp5_alarm, 0444, show_alarm, NULL, 20);
2365 static SENSOR_DEVICE_ATTR(temp6_alarm, 0444, show_alarm, NULL, 21);
2366 static SENSOR_DEVICE_ATTR(intrusion0_alarm, 0644,
2367 show_alarm, clear_intrusion, 4);
2369 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
2372 struct it87_data *data = it87_update_device(dev);
2373 int bitnr = to_sensor_dev_attr(attr)->index;
2376 return PTR_ERR(data);
2378 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
2381 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
2382 const char *buf, size_t count)
2384 int bitnr = to_sensor_dev_attr(attr)->index;
2385 struct it87_data *data = dev_get_drvdata(dev);
2389 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
2392 err = it87_lock(data);
2396 data->beeps = data->read(data, IT87_REG_BEEP_ENABLE);
2398 data->beeps |= BIT(bitnr);
2400 data->beeps &= ~BIT(bitnr);
2401 data->write(data, IT87_REG_BEEP_ENABLE, data->beeps);
2406 static SENSOR_DEVICE_ATTR(in0_beep, 0644,
2407 show_beep, set_beep, 1);
2408 static SENSOR_DEVICE_ATTR(in1_beep, 0444, show_beep, NULL, 1);
2409 static SENSOR_DEVICE_ATTR(in2_beep, 0444, show_beep, NULL, 1);
2410 static SENSOR_DEVICE_ATTR(in3_beep, 0444, show_beep, NULL, 1);
2411 static SENSOR_DEVICE_ATTR(in4_beep, 0444, show_beep, NULL, 1);
2412 static SENSOR_DEVICE_ATTR(in5_beep, 0444, show_beep, NULL, 1);
2413 static SENSOR_DEVICE_ATTR(in6_beep, 0444, show_beep, NULL, 1);
2414 static SENSOR_DEVICE_ATTR(in7_beep, 0444, show_beep, NULL, 1);
2415 /* fanX_beep writability is set later */
2416 static SENSOR_DEVICE_ATTR(fan1_beep, 0444, show_beep, set_beep, 0);
2417 static SENSOR_DEVICE_ATTR(fan2_beep, 0444, show_beep, set_beep, 0);
2418 static SENSOR_DEVICE_ATTR(fan3_beep, 0444, show_beep, set_beep, 0);
2419 static SENSOR_DEVICE_ATTR(fan4_beep, 0444, show_beep, set_beep, 0);
2420 static SENSOR_DEVICE_ATTR(fan5_beep, 0444, show_beep, set_beep, 0);
2421 static SENSOR_DEVICE_ATTR(fan6_beep, 0444, show_beep, set_beep, 0);
2422 static SENSOR_DEVICE_ATTR(temp1_beep, 0644,
2423 show_beep, set_beep, 2);
2424 static SENSOR_DEVICE_ATTR(temp2_beep, 0444, show_beep, NULL, 2);
2425 static SENSOR_DEVICE_ATTR(temp3_beep, 0444, show_beep, NULL, 2);
2426 static SENSOR_DEVICE_ATTR(temp4_beep, 0444, show_beep, NULL, 2);
2427 static SENSOR_DEVICE_ATTR(temp5_beep, 0444, show_beep, NULL, 2);
2428 static SENSOR_DEVICE_ATTR(temp6_beep, 0444, show_beep, NULL, 2);
2430 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
2433 struct it87_data *data = dev_get_drvdata(dev);
2435 return sprintf(buf, "%u\n", data->vrm);
2438 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
2439 const char *buf, size_t count)
2441 struct it87_data *data = dev_get_drvdata(dev);
2444 if (kstrtoul(buf, 10, &val) < 0)
2451 static DEVICE_ATTR(vrm, 0644, show_vrm_reg, store_vrm_reg);
2453 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
2456 struct it87_data *data = it87_update_device(dev);
2459 return PTR_ERR(data);
2461 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
2463 static DEVICE_ATTR(cpu0_vid, 0444, show_vid_reg, NULL);
2465 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
2468 static const char * const labels[] = {
2474 static const char * const labels_it8721[] = {
2480 struct it87_data *data = dev_get_drvdata(dev);
2481 int nr = to_sensor_dev_attr(attr)->index;
2484 if (has_vin3_5v(data) && nr == 0)
2486 else if (has_12mv_adc(data) || has_10_9mv_adc(data) ||
2488 label = labels_it8721[nr];
2492 return sprintf(buf, "%s\n", label);
2494 static SENSOR_DEVICE_ATTR(in3_label, 0444, show_label, NULL, 0);
2495 static SENSOR_DEVICE_ATTR(in7_label, 0444, show_label, NULL, 1);
2496 static SENSOR_DEVICE_ATTR(in8_label, 0444, show_label, NULL, 2);
2498 static SENSOR_DEVICE_ATTR(in9_label, 0444, show_label, NULL, 3);
2500 static umode_t it87_in_is_visible(struct kobject *kobj,
2501 struct attribute *attr, int index)
2503 struct device *dev = container_of(kobj, struct device, kobj);
2504 struct it87_data *data = dev_get_drvdata(dev);
2505 int i = index / 5; /* voltage index */
2506 int a = index % 5; /* attribute index */
2508 if (index >= 40) { /* in8 and higher only have input attributes */
2513 if (!(data->has_in & BIT(i)))
2516 if (a == 4 && !data->has_beep)
2522 static struct attribute *it87_attributes_in[] = {
2523 &sensor_dev_attr_in0_input.dev_attr.attr,
2524 &sensor_dev_attr_in0_min.dev_attr.attr,
2525 &sensor_dev_attr_in0_max.dev_attr.attr,
2526 &sensor_dev_attr_in0_alarm.dev_attr.attr,
2527 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
2529 &sensor_dev_attr_in1_input.dev_attr.attr,
2530 &sensor_dev_attr_in1_min.dev_attr.attr,
2531 &sensor_dev_attr_in1_max.dev_attr.attr,
2532 &sensor_dev_attr_in1_alarm.dev_attr.attr,
2533 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
2535 &sensor_dev_attr_in2_input.dev_attr.attr,
2536 &sensor_dev_attr_in2_min.dev_attr.attr,
2537 &sensor_dev_attr_in2_max.dev_attr.attr,
2538 &sensor_dev_attr_in2_alarm.dev_attr.attr,
2539 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
2541 &sensor_dev_attr_in3_input.dev_attr.attr,
2542 &sensor_dev_attr_in3_min.dev_attr.attr,
2543 &sensor_dev_attr_in3_max.dev_attr.attr,
2544 &sensor_dev_attr_in3_alarm.dev_attr.attr,
2545 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2547 &sensor_dev_attr_in4_input.dev_attr.attr,
2548 &sensor_dev_attr_in4_min.dev_attr.attr,
2549 &sensor_dev_attr_in4_max.dev_attr.attr,
2550 &sensor_dev_attr_in4_alarm.dev_attr.attr,
2551 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2553 &sensor_dev_attr_in5_input.dev_attr.attr,
2554 &sensor_dev_attr_in5_min.dev_attr.attr,
2555 &sensor_dev_attr_in5_max.dev_attr.attr,
2556 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2557 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2559 &sensor_dev_attr_in6_input.dev_attr.attr,
2560 &sensor_dev_attr_in6_min.dev_attr.attr,
2561 &sensor_dev_attr_in6_max.dev_attr.attr,
2562 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2563 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2565 &sensor_dev_attr_in7_input.dev_attr.attr,
2566 &sensor_dev_attr_in7_min.dev_attr.attr,
2567 &sensor_dev_attr_in7_max.dev_attr.attr,
2568 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2569 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2571 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2572 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
2573 &sensor_dev_attr_in10_input.dev_attr.attr, /* 42 */
2574 &sensor_dev_attr_in11_input.dev_attr.attr, /* 43 */
2575 &sensor_dev_attr_in12_input.dev_attr.attr, /* 44 */
2579 static const struct attribute_group it87_group_in = {
2580 .attrs = it87_attributes_in,
2581 .is_visible = it87_in_is_visible,
2584 static umode_t it87_temp_is_visible(struct kobject *kobj,
2585 struct attribute *attr, int index)
2587 struct device *dev = container_of(kobj, struct device, kobj);
2588 struct it87_data *data = dev_get_drvdata(dev);
2589 int i = index / 7; /* temperature index */
2590 int a = index % 7; /* attribute index */
2592 if (!(data->has_temp & BIT(i)))
2595 if (a && i >= data->num_temp_limit)
2599 int type = get_temp_type(data, i);
2603 if (has_bank_sel(data))
2608 if (a == 5 && i >= data->num_temp_offset)
2611 if (a == 6 && !data->has_beep)
2617 static struct attribute *it87_attributes_temp[] = {
2618 &sensor_dev_attr_temp1_input.dev_attr.attr,
2619 &sensor_dev_attr_temp1_max.dev_attr.attr,
2620 &sensor_dev_attr_temp1_min.dev_attr.attr,
2621 &sensor_dev_attr_temp1_type.dev_attr.attr, /* 3 */
2622 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2623 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2624 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2626 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2627 &sensor_dev_attr_temp2_max.dev_attr.attr,
2628 &sensor_dev_attr_temp2_min.dev_attr.attr,
2629 &sensor_dev_attr_temp2_type.dev_attr.attr,
2630 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2631 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2632 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2634 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2635 &sensor_dev_attr_temp3_max.dev_attr.attr,
2636 &sensor_dev_attr_temp3_min.dev_attr.attr,
2637 &sensor_dev_attr_temp3_type.dev_attr.attr,
2638 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2639 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2640 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2642 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2643 &sensor_dev_attr_temp4_max.dev_attr.attr,
2644 &sensor_dev_attr_temp4_min.dev_attr.attr,
2645 &sensor_dev_attr_temp4_type.dev_attr.attr,
2646 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
2647 &sensor_dev_attr_temp4_offset.dev_attr.attr,
2648 &sensor_dev_attr_temp4_beep.dev_attr.attr,
2650 &sensor_dev_attr_temp5_input.dev_attr.attr,
2651 &sensor_dev_attr_temp5_max.dev_attr.attr,
2652 &sensor_dev_attr_temp5_min.dev_attr.attr,
2653 &sensor_dev_attr_temp5_type.dev_attr.attr,
2654 &sensor_dev_attr_temp5_alarm.dev_attr.attr,
2655 &sensor_dev_attr_temp5_offset.dev_attr.attr,
2656 &sensor_dev_attr_temp5_beep.dev_attr.attr,
2658 &sensor_dev_attr_temp6_input.dev_attr.attr,
2659 &sensor_dev_attr_temp6_max.dev_attr.attr,
2660 &sensor_dev_attr_temp6_min.dev_attr.attr,
2661 &sensor_dev_attr_temp6_type.dev_attr.attr,
2662 &sensor_dev_attr_temp6_alarm.dev_attr.attr,
2663 &sensor_dev_attr_temp6_offset.dev_attr.attr,
2664 &sensor_dev_attr_temp6_beep.dev_attr.attr,
2668 static const struct attribute_group it87_group_temp = {
2669 .attrs = it87_attributes_temp,
2670 .is_visible = it87_temp_is_visible,
2673 static umode_t it87_is_visible(struct kobject *kobj,
2674 struct attribute *attr, int index)
2676 struct device *dev = container_of(kobj, struct device, kobj);
2677 struct it87_data *data = dev_get_drvdata(dev);
2679 if ((index == 2 || index == 3) && !data->has_vid)
2682 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2688 static struct attribute *it87_attributes[] = {
2689 &dev_attr_alarms.attr,
2690 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2691 &dev_attr_vrm.attr, /* 2 */
2692 &dev_attr_cpu0_vid.attr, /* 3 */
2693 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2694 &sensor_dev_attr_in7_label.dev_attr.attr,
2695 &sensor_dev_attr_in8_label.dev_attr.attr,
2696 &sensor_dev_attr_in9_label.dev_attr.attr,
2700 static const struct attribute_group it87_group = {
2701 .attrs = it87_attributes,
2702 .is_visible = it87_is_visible,
2705 static umode_t it87_fan_is_visible(struct kobject *kobj,
2706 struct attribute *attr, int index)
2708 struct device *dev = container_of(kobj, struct device, kobj);
2709 struct it87_data *data = dev_get_drvdata(dev);
2710 int i = index / 5; /* fan index */
2711 int a = index % 5; /* attribute index */
2713 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2714 i = (index - 15) / 4 + 3;
2715 a = (index - 15) % 4;
2718 if (!(data->has_fan & BIT(i)))
2721 if (a == 3) { /* beep */
2722 if (!data->has_beep)
2724 /* first fan beep attribute is writable */
2725 if (i == __ffs(data->has_fan))
2726 return attr->mode | 0200;
2729 if (a == 4 && has_16bit_fans(data)) /* divisor */
2735 static struct attribute *it87_attributes_fan[] = {
2736 &sensor_dev_attr_fan1_input.dev_attr.attr,
2737 &sensor_dev_attr_fan1_min.dev_attr.attr,
2738 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2739 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2740 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2742 &sensor_dev_attr_fan2_input.dev_attr.attr,
2743 &sensor_dev_attr_fan2_min.dev_attr.attr,
2744 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2745 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2746 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2748 &sensor_dev_attr_fan3_input.dev_attr.attr,
2749 &sensor_dev_attr_fan3_min.dev_attr.attr,
2750 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2751 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2752 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2754 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2755 &sensor_dev_attr_fan4_min.dev_attr.attr,
2756 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2757 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2759 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2760 &sensor_dev_attr_fan5_min.dev_attr.attr,
2761 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2762 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2764 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2765 &sensor_dev_attr_fan6_min.dev_attr.attr,
2766 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2767 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2771 static const struct attribute_group it87_group_fan = {
2772 .attrs = it87_attributes_fan,
2773 .is_visible = it87_fan_is_visible,
2776 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2777 struct attribute *attr, int index)
2779 struct device *dev = container_of(kobj, struct device, kobj);
2780 struct it87_data *data = dev_get_drvdata(dev);
2781 int i = index / 4; /* pwm index */
2782 int a = index % 4; /* attribute index */
2784 if (!(data->has_pwm & BIT(i)))
2787 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2788 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2789 return attr->mode | 0200;
2791 /* pwm2_freq is writable if there are two pwm frequency selects */
2792 if (has_pwm_freq2(data) && i == 1 && a == 2)
2793 return attr->mode | 0200;
2798 static struct attribute *it87_attributes_pwm[] = {
2799 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2800 &sensor_dev_attr_pwm1.dev_attr.attr,
2801 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2802 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2804 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2805 &sensor_dev_attr_pwm2.dev_attr.attr,
2806 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2807 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2809 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2810 &sensor_dev_attr_pwm3.dev_attr.attr,
2811 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2812 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2814 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2815 &sensor_dev_attr_pwm4.dev_attr.attr,
2816 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2817 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2819 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2820 &sensor_dev_attr_pwm5.dev_attr.attr,
2821 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2822 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2824 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2825 &sensor_dev_attr_pwm6.dev_attr.attr,
2826 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2827 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2832 static const struct attribute_group it87_group_pwm = {
2833 .attrs = it87_attributes_pwm,
2834 .is_visible = it87_pwm_is_visible,
2837 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2838 struct attribute *attr, int index)
2840 struct device *dev = container_of(kobj, struct device, kobj);
2841 struct it87_data *data = dev_get_drvdata(dev);
2842 int i = index / 11; /* pwm index */
2843 int a = index % 11; /* attribute index */
2845 if (index >= 33) { /* pwm 4..6 */
2846 i = (index - 33) / 6 + 3;
2847 a = (index - 33) % 6 + 4;
2850 if (!(data->has_pwm & BIT(i)))
2853 if (has_newer_autopwm(data)) {
2854 if (a < 4) /* no auto point pwm */
2856 if (a == 8) /* no auto_point4 */
2859 if (has_old_autopwm(data)) {
2860 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2867 static struct attribute *it87_attributes_auto_pwm[] = {
2868 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2869 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2870 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2871 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2872 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2873 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2874 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2875 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2876 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2877 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2878 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2880 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2881 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2882 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2883 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2884 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2885 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2886 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2887 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2888 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2889 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2890 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2892 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2893 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2894 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2895 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2896 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2897 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2898 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2899 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2900 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2901 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2902 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2904 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2905 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2906 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2907 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2908 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2909 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2911 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2912 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2913 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2914 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2915 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2916 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2918 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2919 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2920 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2921 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2922 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2923 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2928 static const struct attribute_group it87_group_auto_pwm = {
2929 .attrs = it87_attributes_auto_pwm,
2930 .is_visible = it87_auto_pwm_is_visible,
2933 /* SuperIO detection - will change isa_address if a chip is found */
2934 static int __init it87_find(int sioaddr, unsigned short *address,
2935 phys_addr_t *mmio_address, struct it87_sio_data *sio_data)
2937 const struct it87_devices *config;
2938 phys_addr_t base = 0;
2944 err = superio_enter(sioaddr);
2948 sio_data->sioaddr = sioaddr;
2951 chip_type = superio_inw(sioaddr, DEVID);
2952 if (chip_type == 0xffff)
2956 chip_type = force_id;
2958 switch (chip_type) {
2960 sio_data->type = it87;
2963 sio_data->type = it8712;
2967 sio_data->type = it8716;
2970 sio_data->type = it8718;
2973 sio_data->type = it8720;
2976 sio_data->type = it8721;
2979 sio_data->type = it8728;
2982 sio_data->type = it8732;
2985 sio_data->type = it8792;
2987 * Disabling configuration mode on IT8792E can result in system
2988 * hang-ups and access failures to the Super-IO chip at the
2989 * second SIO address. Never exit configuration mode on this
2990 * chip to avoid the problem.
2995 sio_data->type = it8771;
2998 sio_data->type = it8772;
3001 sio_data->type = it8781;
3004 sio_data->type = it8782;
3007 sio_data->type = it8783;
3010 sio_data->type = it8786;
3013 sio_data->type = it8790;
3014 doexit = false; /* See IT8792E comment above */
3018 sio_data->type = it8603;
3021 sio_data->type = it8607;
3024 sio_data->type = it8613;
3027 sio_data->type = it8620;
3030 sio_data->type = it8622;
3033 sio_data->type = it8625;
3036 sio_data->type = it8628;
3039 sio_data->type = it8655;
3042 sio_data->type = it8665;
3045 sio_data->type = it8686;
3047 case 0xffff: /* No device at all */
3050 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
3054 superio_select(sioaddr, PME);
3055 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
3056 pr_info("Device not activated, skipping\n");
3060 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
3061 if (*address == 0) {
3062 pr_info("Base address not set, skipping\n");
3066 sio_data->doexit = doexit;
3069 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
3071 config = &it87_devices[sio_data->type];
3073 if (has_mmio(config) && mmio) {
3076 reg = superio_inb(sioaddr, IT87_EC_HWM_MIO_REG);
3078 base = 0xf0000000 + ((reg & 0x0f) << 24);
3079 base += (reg & 0xc0) << 14;
3082 *mmio_address = base;
3086 snprintf(mmio_str, sizeof(mmio_str), " [MMIO at %pa]", &base);
3088 pr_info("Found IT%04x%s chip at 0x%x%s, revision %d\n", chip_type,
3089 it87_devices[sio_data->type].suffix,
3090 *address, mmio_str, sio_data->revision);
3092 /* in7 (VSB or VCCH5V) is always internal on some chips */
3093 if (has_in7_internal(config))
3094 sio_data->internal |= BIT(1);
3096 /* in8 (Vbat) is always internal */
3097 sio_data->internal |= BIT(2);
3099 /* in9 (AVCC3), always internal if supported */
3100 if (has_avcc3(config))
3101 sio_data->internal |= BIT(3); /* in9 is AVCC */
3103 sio_data->skip_in |= BIT(9);
3105 if (!has_four_pwm(config))
3106 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
3107 else if (!has_five_pwm(config))
3108 sio_data->skip_pwm |= BIT(4) | BIT(5);
3109 else if (!has_six_pwm(config))
3110 sio_data->skip_pwm |= BIT(5);
3112 if (!has_vid(config))
3113 sio_data->skip_vid = 1;
3115 /* Read GPIO config and VID value from LDN 7 (GPIO) */
3116 if (sio_data->type == it87) {
3117 /* The IT8705F has a different LD number for GPIO */
3118 superio_select(sioaddr, 5);
3119 sio_data->beep_pin = superio_inb(sioaddr,
3120 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3121 } else if (sio_data->type == it8783) {
3122 int reg25, reg27, reg2a, reg2c, regef;
3124 superio_select(sioaddr, GPIO);
3126 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3127 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3128 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3129 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3130 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
3132 /* Check if fan3 is there or not */
3133 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
3134 sio_data->skip_fan |= BIT(2);
3135 if ((reg25 & BIT(4)) ||
3136 (!(reg2a & BIT(1)) && (regef & BIT(0))))
3137 sio_data->skip_pwm |= BIT(2);
3139 /* Check if fan2 is there or not */
3141 sio_data->skip_fan |= BIT(1);
3143 sio_data->skip_pwm |= BIT(1);
3146 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
3147 sio_data->skip_in |= BIT(5); /* No VIN5 */
3151 sio_data->skip_in |= BIT(6); /* No VIN6 */
3155 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
3157 if (reg27 & BIT(2)) {
3159 * The data sheet is a bit unclear regarding the
3160 * internal voltage divider for VCCH5V. It says
3161 * "This bit enables and switches VIN7 (pin 91) to the
3162 * internal voltage divider for VCCH5V".
3163 * This is different to other chips, where the internal
3164 * voltage divider would connect VIN7 to an internal
3165 * voltage source. Maybe that is the case here as well.
3167 * Since we don't know for sure, re-route it if that is
3168 * not the case, and ask the user to report if the
3169 * resulting voltage is sane.
3171 if (!(reg2c & BIT(1))) {
3173 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
3175 pr_notice("Routing internal VCCH5V to in7.\n");
3177 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
3178 pr_notice("Please report if it displays a reasonable voltage.\n");
3182 sio_data->internal |= BIT(0);
3184 sio_data->internal |= BIT(1);
3186 sio_data->beep_pin = superio_inb(sioaddr,
3187 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3188 } else if (sio_data->type == it8603 || sio_data->type == it8607) {
3191 superio_select(sioaddr, GPIO);
3193 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3195 /* Check if fan3 is there or not */
3197 sio_data->skip_pwm |= BIT(2);
3199 sio_data->skip_fan |= BIT(2);
3201 /* Check if fan2 is there or not */
3202 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3204 sio_data->skip_pwm |= BIT(1);
3206 sio_data->skip_fan |= BIT(1);
3208 switch (sio_data->type) {
3210 sio_data->skip_in |= BIT(5); /* No VIN5 */
3211 sio_data->skip_in |= BIT(6); /* No VIN6 */
3214 sio_data->skip_pwm |= BIT(0);/* No fan1 */
3215 sio_data->skip_fan |= BIT(0);
3220 sio_data->beep_pin = superio_inb(sioaddr,
3221 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3222 } else if (sio_data->type == it8613) {
3223 int reg27, reg29, reg2a;
3225 superio_select(sioaddr, GPIO);
3227 /* Check for pwm3, fan3, pwm5, fan5 */
3228 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3230 sio_data->skip_fan |= BIT(4);
3232 sio_data->skip_pwm |= BIT(4);
3234 sio_data->skip_pwm |= BIT(2);
3236 sio_data->skip_fan |= BIT(2);
3238 /* Check for pwm2, fan2 */
3239 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3241 sio_data->skip_pwm |= BIT(1);
3243 sio_data->skip_fan |= BIT(1);
3245 /* Check for pwm4, fan4 */
3246 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
3247 if (!(reg2a & BIT(0)) || (reg29 & BIT(7))) {
3248 sio_data->skip_fan |= BIT(3);
3249 sio_data->skip_pwm |= BIT(3);
3252 sio_data->skip_pwm |= BIT(0); /* No pwm1 */
3253 sio_data->skip_fan |= BIT(0); /* No fan1 */
3254 sio_data->skip_in |= BIT(3); /* No VIN3 */
3255 sio_data->skip_in |= BIT(6); /* No VIN6 */
3257 sio_data->beep_pin = superio_inb(sioaddr,
3258 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3259 } else if (sio_data->type == it8620 || sio_data->type == it8628 ||
3260 sio_data->type == it8686) {
3263 superio_select(sioaddr, GPIO);
3265 /* Check for pwm5 */
3266 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3268 sio_data->skip_pwm |= BIT(4);
3270 /* Check for fan4, fan5 */
3271 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3272 if (!(reg & BIT(5)))
3273 sio_data->skip_fan |= BIT(3);
3274 if (!(reg & BIT(4)))
3275 sio_data->skip_fan |= BIT(4);
3277 /* Check for pwm3, fan3 */
3278 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3280 sio_data->skip_pwm |= BIT(2);
3282 sio_data->skip_fan |= BIT(2);
3284 /* Check for pwm4 */
3285 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
3287 sio_data->skip_pwm |= BIT(3);
3289 /* Check for pwm2, fan2 */
3290 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3292 sio_data->skip_pwm |= BIT(1);
3294 sio_data->skip_fan |= BIT(1);
3295 /* Check for pwm6, fan6 */
3296 if (!(reg & BIT(7))) {
3297 sio_data->skip_pwm |= BIT(5);
3298 sio_data->skip_fan |= BIT(5);
3301 /* Check if AVCC is on VIN3 */
3302 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3304 /* For it8686, the bit just enables AVCC3 */
3305 if (sio_data->type != it8686)
3306 sio_data->internal |= BIT(0);
3308 sio_data->internal &= ~BIT(3);
3309 sio_data->skip_in |= BIT(9);
3312 sio_data->beep_pin = superio_inb(sioaddr,
3313 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3314 } else if (sio_data->type == it8622) {
3317 superio_select(sioaddr, GPIO);
3319 /* Check for pwm4, fan4 */
3320 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3322 sio_data->skip_fan |= BIT(3);
3324 sio_data->skip_pwm |= BIT(3);
3326 /* Check for pwm3, fan3, pwm5, fan5 */
3327 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3329 sio_data->skip_pwm |= BIT(2);
3331 sio_data->skip_fan |= BIT(2);
3333 sio_data->skip_pwm |= BIT(4);
3335 sio_data->skip_fan |= BIT(4);
3337 /* Check for pwm2, fan2 */
3338 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3340 sio_data->skip_pwm |= BIT(1);
3342 sio_data->skip_fan |= BIT(1);
3344 /* Check for AVCC */
3345 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3346 if (!(reg & BIT(0)))
3347 sio_data->skip_in |= BIT(9);
3349 sio_data->beep_pin = superio_inb(sioaddr,
3350 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3351 } else if (sio_data->type == it8732) {
3354 superio_select(sioaddr, GPIO);
3356 /* Check for pwm2, fan2 */
3357 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3359 sio_data->skip_pwm |= BIT(1);
3361 sio_data->skip_fan |= BIT(1);
3363 /* Check for pwm3, fan3, fan4 */
3364 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3366 sio_data->skip_pwm |= BIT(2);
3368 sio_data->skip_fan |= BIT(2);
3370 sio_data->skip_fan |= BIT(3);
3372 /* Check if AVCC is on VIN3 */
3373 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3375 sio_data->internal |= BIT(0);
3377 sio_data->beep_pin = superio_inb(sioaddr,
3378 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3379 } else if (sio_data->type == it8655) {
3382 superio_select(sioaddr, GPIO);
3384 /* Check for pwm2 */
3385 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3387 sio_data->skip_pwm |= BIT(1);
3389 /* Check for fan2 */
3390 reg = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3392 sio_data->skip_fan |= BIT(1);
3394 /* Check for pwm3, fan3 */
3395 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3397 sio_data->skip_pwm |= BIT(2);
3399 sio_data->skip_fan |= BIT(2);
3401 sio_data->beep_pin = superio_inb(sioaddr,
3402 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3403 } else if (sio_data->type == it8665 || sio_data->type == it8625) {
3404 int reg27, reg29, reg2d, regd3;
3406 superio_select(sioaddr, GPIO);
3408 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3409 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3410 reg2d = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
3411 regd3 = superio_inb(sioaddr, IT87_SIO_GPIO9_REG);
3413 /* Check for pwm2, fan2 */
3415 sio_data->skip_pwm |= BIT(1);
3417 * Note: Table 6-1 in datasheet claims that FAN_TAC2
3418 * would be enabled with 29h[2]=0.
3421 sio_data->skip_fan |= BIT(1);
3423 /* Check for pwm3, fan3 */
3425 sio_data->skip_pwm |= BIT(2);
3427 sio_data->skip_fan |= BIT(2);
3429 /* Check for pwm4, fan4, pwm5, fan5 */
3430 if (sio_data->type == it8625) {
3431 int reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
3434 sio_data->skip_fan |= BIT(3);
3436 sio_data->skip_pwm |= BIT(3);
3438 sio_data->skip_pwm |= BIT(4);
3440 sio_data->skip_fan |= BIT(4);
3442 int reg26 = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3445 sio_data->skip_pwm |= BIT(3);
3447 sio_data->skip_fan |= BIT(3);
3449 sio_data->skip_pwm |= BIT(4);
3451 sio_data->skip_fan |= BIT(4);
3454 /* Check for pwm6, fan6 */
3456 sio_data->skip_pwm |= BIT(5);
3458 sio_data->skip_fan |= BIT(5);
3460 sio_data->beep_pin = superio_inb(sioaddr,
3461 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3466 superio_select(sioaddr, GPIO);
3468 /* Check for fan4, fan5 */
3469 if (has_five_fans(config)) {
3470 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
3471 switch (sio_data->type) {
3474 sio_data->skip_fan |= BIT(3);
3476 sio_data->skip_fan |= BIT(4);
3481 if (!(reg & BIT(5)))
3482 sio_data->skip_fan |= BIT(3);
3483 if (!(reg & BIT(4)))
3484 sio_data->skip_fan |= BIT(4);
3491 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
3492 if (!sio_data->skip_vid) {
3493 /* We need at least 4 VID pins */
3495 pr_info("VID is disabled (pins used for GPIO)\n");
3496 sio_data->skip_vid = 1;
3500 /* Check if fan3 is there or not */
3502 sio_data->skip_pwm |= BIT(2);
3504 sio_data->skip_fan |= BIT(2);
3506 /* Check if fan2 is there or not */
3507 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
3509 sio_data->skip_pwm |= BIT(1);
3511 sio_data->skip_fan |= BIT(1);
3513 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
3514 !(sio_data->skip_vid))
3515 sio_data->vid_value = superio_inb(sioaddr,
3518 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
3520 uart6 = sio_data->type == it8782 && (reg & BIT(2));
3523 * The IT8720F has no VIN7 pin, so VCCH should always be
3524 * routed internally to VIN7 with an internal divider.
3525 * Curiously, there still is a configuration bit to control
3526 * this, which means it can be set incorrectly. And even
3527 * more curiously, many boards out there are improperly
3528 * configured, even though the IT8720F datasheet claims
3529 * that the internal routing of VCCH to VIN7 is the default
3530 * setting. So we force the internal routing in this case.
3532 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
3533 * If UART6 is enabled, re-route VIN7 to the internal divider
3534 * if that is not already the case.
3536 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
3538 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
3539 pr_notice("Routing internal VCCH to in7\n");
3542 sio_data->internal |= BIT(0);
3544 sio_data->internal |= BIT(1);
3547 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
3548 * While VIN7 can be routed to the internal voltage divider,
3549 * VIN5 and VIN6 are not available if UART6 is enabled.
3551 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
3552 * is the temperature source. Since we can not read the
3553 * temperature source here, skip_temp is preliminary.
3556 sio_data->skip_in |= BIT(5) | BIT(6);
3557 sio_data->skip_temp |= BIT(2);
3560 sio_data->beep_pin = superio_inb(sioaddr,
3561 IT87_SIO_BEEP_PIN_REG) & 0x3f;
3563 if (sio_data->beep_pin)
3564 pr_info("Beeping is supported\n");
3566 if (config->smbus_bitmap && !base) {
3569 superio_select(sioaddr, PME);
3570 reg = superio_inb(sioaddr, IT87_SPECIAL_CFG_REG);
3571 sio_data->ec_special_config = reg;
3572 sio_data->smbus_bitmap = reg & config->smbus_bitmap;
3576 superio_exit(sioaddr, doexit);
3580 static void it87_init_regs(struct platform_device *pdev)
3582 struct it87_data *data = platform_get_drvdata(pdev);
3584 /* Initialize chip specific register pointers */
3585 switch (data->type) {
3588 data->REG_FAN = IT87_REG_FAN;
3589 data->REG_FANX = IT87_REG_FANX;
3590 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3591 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3592 data->REG_PWM = IT87_REG_PWM;
3593 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET_8686;
3594 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW_8686;
3595 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH_8686;
3600 data->REG_FAN = IT87_REG_FAN_8665;
3601 data->REG_FANX = IT87_REG_FANX_8665;
3602 data->REG_FAN_MIN = IT87_REG_FAN_MIN_8665;
3603 data->REG_FANX_MIN = IT87_REG_FANX_MIN_8665;
3604 data->REG_PWM = IT87_REG_PWM_8665;
3605 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3606 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3607 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3610 data->REG_FAN = IT87_REG_FAN;
3611 data->REG_FANX = IT87_REG_FANX;
3612 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3613 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3614 data->REG_PWM = IT87_REG_PWM_8665;
3615 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3616 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3617 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3620 data->REG_FAN = IT87_REG_FAN;
3621 data->REG_FANX = IT87_REG_FANX;
3622 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3623 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3624 data->REG_PWM = IT87_REG_PWM_8665;
3625 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3626 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3627 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3630 data->REG_FAN = IT87_REG_FAN;
3631 data->REG_FANX = IT87_REG_FANX;
3632 data->REG_FAN_MIN = IT87_REG_FAN_MIN;
3633 data->REG_FANX_MIN = IT87_REG_FANX_MIN;
3634 data->REG_PWM = IT87_REG_PWM;
3635 data->REG_TEMP_OFFSET = IT87_REG_TEMP_OFFSET;
3636 data->REG_TEMP_LOW = IT87_REG_TEMP_LOW;
3637 data->REG_TEMP_HIGH = IT87_REG_TEMP_HIGH;
3642 data->read = it87_mmio_read;
3643 data->write = it87_mmio_write;
3644 } else if (has_bank_sel(data)) {
3645 data->read = it87_io_read;
3646 data->write = it87_io_write;
3648 data->read = _it87_io_read;
3649 data->write = _it87_io_write;
3653 /* Called when we have found a new IT87. */
3654 static void it87_init_device(struct platform_device *pdev)
3656 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
3657 struct it87_data *data = platform_get_drvdata(pdev);
3661 if (has_new_tempmap(data)) {
3662 data->pwm_temp_map_shift = 3;
3663 data->pwm_temp_map_mask = 0x07;
3665 data->pwm_temp_map_shift = 0;
3666 data->pwm_temp_map_mask = 0x03;
3670 * For each PWM channel:
3671 * - If it is in automatic mode, setting to manual mode should set
3672 * the fan to full speed by default.
3673 * - If it is in manual mode, we need a mapping to temperature
3674 * channels to use when later setting to automatic mode later.
3675 * Map to the first sensor by default (we are clueless.)
3676 * In both cases, the value can (and should) be changed by the user
3677 * prior to switching to a different mode.
3678 * Note that this is no longer needed for the IT8721F and later, as
3679 * these have separate registers for the temperature mapping and the
3680 * manual duty cycle.
3682 for (i = 0; i < NUM_AUTO_PWM; i++) {
3683 data->pwm_temp_map[i] = 0;
3684 data->pwm_duty[i] = 0x7f; /* Full speed */
3685 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
3689 * Some chips seem to have default value 0xff for all limit
3690 * registers. For low voltage limits it makes no sense and triggers
3691 * alarms, so change to 0 instead. For high temperature limits, it
3692 * means -1 degree C, which surprisingly doesn't trigger an alarm,
3693 * but is still confusing, so change to 127 degrees C.
3695 for (i = 0; i < NUM_VIN_LIMIT; i++) {
3696 tmp = data->read(data, IT87_REG_VIN_MIN(i));
3698 data->write(data, IT87_REG_VIN_MIN(i), 0);
3700 for (i = 0; i < data->num_temp_limit; i++) {
3701 tmp = data->read(data, data->REG_TEMP_HIGH[i]);
3703 data->write(data, data->REG_TEMP_HIGH[i], 127);
3707 * Temperature channels are not forcibly enabled, as they can be
3708 * set to two different sensor types and we can't guess which one
3709 * is correct for a given system. These channels can be enabled at
3710 * run-time through the temp{1-3}_type sysfs accessors if needed.
3713 /* Check if voltage monitors are reset manually or by some reason */
3714 tmp = data->read(data, IT87_REG_VIN_ENABLE);
3715 if ((tmp & 0xff) == 0) {
3716 /* Enable all voltage monitors */
3717 data->write(data, IT87_REG_VIN_ENABLE, 0xff);
3720 /* Check if tachometers are reset manually or by some reason */
3721 mask = 0x70 & ~(sio_data->skip_fan << 4);
3722 data->fan_main_ctrl = data->read(data, IT87_REG_FAN_MAIN_CTRL);
3723 if ((data->fan_main_ctrl & mask) == 0) {
3724 /* Enable all fan tachometers */
3725 data->fan_main_ctrl |= mask;
3726 data->write(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
3728 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
3730 tmp = data->read(data, IT87_REG_FAN_16BIT);
3732 /* Set tachometers to 16-bit mode if needed */
3733 if (has_fan16_config(data)) {
3734 if (~tmp & 0x07 & data->has_fan) {
3736 "Setting fan1-3 to 16-bit mode\n");
3737 data->write(data, IT87_REG_FAN_16BIT, tmp | 0x07);
3741 /* Check for additional fans */
3742 if (has_four_fans(data) && (tmp & BIT(4)))
3743 data->has_fan |= BIT(3); /* fan4 enabled */
3744 if (has_five_fans(data) && (tmp & BIT(5)))
3745 data->has_fan |= BIT(4); /* fan5 enabled */
3746 if (has_six_fans(data)) {
3747 switch (data->type) {
3752 data->has_fan |= BIT(5); /* fan6 enabled */
3756 tmp = data->read(data, IT87_REG_FAN_DIV);
3758 data->has_fan |= BIT(5); /* fan6 enabled */
3765 /* Fan input pins may be used for alternative functions */
3766 data->has_fan &= ~sio_data->skip_fan;
3768 /* Check if pwm6 is enabled */
3769 if (has_six_pwm(data)) {
3770 switch (data->type) {
3773 tmp = data->read(data, IT87_REG_FAN_DIV);
3774 if (!(tmp & BIT(3)))
3775 sio_data->skip_pwm |= BIT(5);
3782 if (has_bank_sel(data)) {
3783 for (i = 0; i < 3; i++)
3785 data->read(data, IT87_REG_TEMP_SRC1[i]);
3786 data->temp_src[3] = data->read(data, IT87_REG_TEMP_SRC2);
3789 /* Start monitoring */
3790 data->write(data, IT87_REG_CONFIG,
3791 (data->read(data, IT87_REG_CONFIG) & 0x3e) |
3792 (update_vbat ? 0x41 : 0x01));
3795 /* Return 1 if and only if the PWM interface is safe to use */
3796 static int it87_check_pwm(struct device *dev)
3798 struct it87_data *data = dev_get_drvdata(dev);
3800 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
3801 * and polarity set to active low is sign that this is the case so we
3802 * disable pwm control to protect the user.
3804 int tmp = data->read(data, IT87_REG_FAN_CTL);
3806 if ((tmp & 0x87) == 0) {
3807 if (fix_pwm_polarity) {
3809 * The user asks us to attempt a chip reconfiguration.
3810 * This means switching to active high polarity and
3811 * inverting all fan speed values.
3816 for (i = 0; i < ARRAY_SIZE(pwm); i++)
3817 pwm[i] = data->read(data,
3821 * If any fan is in automatic pwm mode, the polarity
3822 * might be correct, as suspicious as it seems, so we
3823 * better don't change anything (but still disable the
3826 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3828 "Reconfiguring PWM to active high polarity\n");
3829 data->write(data, IT87_REG_FAN_CTL, tmp | 0x87);
3830 for (i = 0; i < 3; i++)
3831 data->write(data, data->REG_PWM[i],
3837 "PWM configuration is too broken to be fixed\n");
3841 "Detected broken BIOS defaults, disabling PWM interface\n");
3843 } else if (fix_pwm_polarity) {
3845 "PWM configuration looks sane, won't touch\n");
3851 static int it87_probe(struct platform_device *pdev)
3853 struct it87_data *data;
3854 struct resource *res;
3855 struct device *dev = &pdev->dev;
3856 struct it87_sio_data *sio_data = dev_get_platdata(dev);
3857 int enable_pwm_interface;
3858 struct device *hwmon_dev;
3861 data = devm_kzalloc(dev, sizeof(struct it87_data), GFP_KERNEL);
3865 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3867 if (!devm_request_region(dev, res->start, IT87_EC_EXTENT,
3869 dev_err(dev, "Failed to request region %pR\n", res);
3873 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3874 data->mmio = devm_ioremap_resource(dev, res);
3875 if (IS_ERR(data->mmio))
3876 return PTR_ERR(data->mmio);
3879 data->addr = res->start;
3880 data->type = sio_data->type;
3881 data->sioaddr = sio_data->sioaddr;
3882 data->smbus_bitmap = sio_data->smbus_bitmap;
3883 data->ec_special_config = sio_data->ec_special_config;
3884 data->doexit = sio_data->doexit;
3885 data->features = it87_devices[sio_data->type].features;
3886 data->num_temp_limit = it87_devices[sio_data->type].num_temp_limit;
3887 data->num_temp_offset = it87_devices[sio_data->type].num_temp_offset;
3888 data->pwm_num_temp_map = it87_devices[sio_data->type].num_temp_map;
3889 data->peci_mask = it87_devices[sio_data->type].peci_mask;
3890 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3893 * IT8705F Datasheet 0.4.1, 3h == Version G.
3894 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3895 * These are the first revisions with 16-bit tachometer support.
3897 switch (data->type) {
3899 if (sio_data->revision >= 0x03) {
3900 data->features &= ~FEAT_OLD_AUTOPWM;
3901 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3905 if (sio_data->revision >= 0x08) {
3906 data->features &= ~FEAT_OLD_AUTOPWM;
3907 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3915 platform_set_drvdata(pdev, data);
3917 mutex_init(&data->update_lock);
3919 /* Initialize register pointers */
3920 it87_init_regs(pdev);
3922 err = smbus_disable(data);
3926 /* Now, we do the remaining detection. */
3927 if ((data->read(data, IT87_REG_CONFIG) & 0x80) ||
3928 data->read(data, IT87_REG_CHIPID) != 0x90) {
3933 /* Check PWM configuration */
3934 enable_pwm_interface = it87_check_pwm(dev);
3936 /* Starting with IT8721F, we handle scaling of internal voltages */
3937 if (has_scaling(data)) {
3938 if (sio_data->internal & BIT(0))
3939 data->in_scaled |= BIT(3); /* in3 is AVCC */
3940 if (sio_data->internal & BIT(1))
3941 data->in_scaled |= BIT(7); /* in7 is VSB */
3942 if (sio_data->internal & BIT(2))
3943 data->in_scaled |= BIT(8); /* in8 is Vbat */
3944 if (sio_data->internal & BIT(3))
3945 data->in_scaled |= BIT(9); /* in9 is AVCC */
3946 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3947 sio_data->type == it8783) {
3948 if (sio_data->internal & BIT(0))
3949 data->in_scaled |= BIT(3); /* in3 is VCC5V */
3950 if (sio_data->internal & BIT(1))
3951 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
3954 data->has_temp = 0x07;
3955 if (sio_data->skip_temp & BIT(2)) {
3956 if (sio_data->type == it8782 &&
3957 !(data->read(data, IT87_REG_TEMP_EXTRA) & 0x80))
3958 data->has_temp &= ~BIT(2);
3961 data->in_internal = sio_data->internal;
3962 data->has_in = 0x3ff & ~sio_data->skip_in;
3964 if (has_four_temp(data)) {
3965 data->has_temp |= BIT(3);
3966 } else if (has_six_temp(data)) {
3967 u8 reg = data->read(data, IT87_REG_TEMP456_ENABLE);
3969 /* Check for additional temperature sensors */
3970 if ((reg & 0x03) >= 0x02)
3971 data->has_temp |= BIT(3);
3972 if (((reg >> 2) & 0x03) >= 0x02)
3973 data->has_temp |= BIT(4);
3974 if (((reg >> 4) & 0x03) >= 0x02)
3975 data->has_temp |= BIT(5);
3977 /* Check for additional voltage sensors */
3978 if ((reg & 0x03) == 0x01)
3979 data->has_in |= BIT(10);
3980 if (((reg >> 2) & 0x03) == 0x01)
3981 data->has_in |= BIT(11);
3982 if (((reg >> 4) & 0x03) == 0x01)
3983 data->has_in |= BIT(12);
3986 data->has_beep = !!sio_data->beep_pin;
3988 /* Initialize the IT87 chip */
3989 it87_init_device(pdev);
3993 if (!sio_data->skip_vid) {
3994 data->has_vid = true;
3995 data->vrm = vid_which_vrm();
3996 /* VID reading from Super-I/O config space if available */
3997 data->vid = sio_data->vid_value;
4000 /* Prepare for sysfs hooks */
4001 data->groups[0] = &it87_group;
4002 data->groups[1] = &it87_group_in;
4003 data->groups[2] = &it87_group_temp;
4004 data->groups[3] = &it87_group_fan;
4006 if (enable_pwm_interface) {
4007 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
4008 data->has_pwm &= ~sio_data->skip_pwm;
4010 data->groups[4] = &it87_group_pwm;
4011 if (has_old_autopwm(data) || has_newer_autopwm(data))
4012 data->groups[5] = &it87_group_auto_pwm;
4015 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
4016 it87_devices[sio_data->type].name,
4017 data, data->groups);
4018 return PTR_ERR_OR_ZERO(hwmon_dev);
4021 static struct platform_driver it87_driver = {
4025 .probe = it87_probe,
4028 static int __init it87_device_add(int index, unsigned short sio_address,
4029 phys_addr_t mmio_address,
4030 const struct it87_sio_data *sio_data)
4032 struct platform_device *pdev;
4033 struct resource res = {
4039 res.start = mmio_address;
4040 res.end = mmio_address + 0x400 - 1;
4041 res.flags = IORESOURCE_MEM;
4043 res.start = sio_address + IT87_EC_OFFSET;
4044 res.end = sio_address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1;
4045 res.flags = IORESOURCE_IO;
4048 err = acpi_check_resource_conflict(&res);
4050 if (!ignore_resource_conflict)
4054 pdev = platform_device_alloc(DRVNAME, sio_address);
4058 err = platform_device_add_resources(pdev, &res, 1);
4060 pr_err("Device resource addition failed (%d)\n", err);
4061 goto exit_device_put;
4064 err = platform_device_add_data(pdev, sio_data,
4065 sizeof(struct it87_sio_data));
4067 pr_err("Platform data allocation failed\n");
4068 goto exit_device_put;
4071 err = platform_device_add(pdev);
4073 pr_err("Device addition failed (%d)\n", err);
4074 goto exit_device_put;
4077 it87_pdev[index] = pdev;
4081 platform_device_put(pdev);
4085 struct it87_dmi_data {
4086 bool sio2_force_config; /* force sio2 into configuration mode */
4087 u8 skip_pwm; /* pwm channels to skip for this board */
4091 * On various Gigabyte AM4 boards (AB350, AX370), the second Super-IO chip
4092 * (IT8792E) needs to be in configuration mode before accessing the first
4093 * due to a bug in IT8792E which otherwise results in LPC bus access errors.
4094 * This needs to be done before accessing the first Super-IO chip since
4095 * the second chip may have been accessed prior to loading this driver.
4097 * The problem is also reported to affect IT8795E, which is used on X299 boards
4098 * and has the same chip ID as IT8792E (0x8733). It also appears to affect
4099 * systems with IT8790E, which is used on some Z97X-Gaming boards as well as
4101 * DMI entries for those systems will be added as they become available and
4102 * as the problem is confirmed to affect those boards.
4104 static struct it87_dmi_data gigabyte_sio2_force = {
4105 .sio2_force_config = true,
4109 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
4110 * connected to a fan, but to something else. One user
4111 * has reported instant system power-off when changing
4112 * the PWM2 duty cycle, so we disable it.
4113 * I use the board name string as the trigger in case
4114 * the same board is ever used in other systems.
4116 static struct it87_dmi_data nvidia_fn68pt = {
4120 static const struct dmi_system_id it87_dmi_table[] __initconst = {
4123 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4124 DMI_MATCH(DMI_BOARD_NAME, "AB350"),
4126 .driver_data = &gigabyte_sio2_force,
4130 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4131 DMI_MATCH(DMI_BOARD_NAME, "AX370"),
4133 .driver_data = &gigabyte_sio2_force,
4137 DMI_MATCH(DMI_SYS_VENDOR, "Gigabyte Technology Co., Ltd."),
4138 DMI_MATCH(DMI_BOARD_NAME, "Z97X-Gaming G1"),
4140 .driver_data = &gigabyte_sio2_force,
4144 DMI_MATCH(DMI_BOARD_VENDOR, "nVIDIA"),
4145 DMI_MATCH(DMI_BOARD_NAME, "FN68PT"),
4147 .driver_data = &nvidia_fn68pt,
4152 static int __init sm_it87_init(void)
4154 const struct dmi_system_id *dmi = dmi_first_match(it87_dmi_table);
4155 struct it87_dmi_data *dmi_data = NULL;
4156 int sioaddr[2] = { REG_2E, REG_4E };
4157 struct it87_sio_data sio_data;
4158 unsigned short isa_address;
4159 phys_addr_t mmio_address;
4163 pr_info("it87 driver version %s\n", IT87_DRIVER_VERSION);
4166 dmi_data = dmi->driver_data;
4168 err = platform_driver_register(&it87_driver);
4172 if (dmi_data && dmi_data->sio2_force_config)
4173 __superio_enter(REG_4E);
4175 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
4176 memset(&sio_data, 0, sizeof(struct it87_sio_data));
4179 err = it87_find(sioaddr[i], &isa_address, &mmio_address,
4181 if (err || isa_address == 0)
4185 sio_data.skip_pwm |= dmi_data->skip_pwm;
4186 err = it87_device_add(i, isa_address, mmio_address, &sio_data);
4188 goto exit_dev_unregister;
4194 goto exit_unregister;
4198 exit_dev_unregister:
4199 /* NULL check handled by platform_device_unregister */
4200 platform_device_unregister(it87_pdev[0]);
4202 platform_driver_unregister(&it87_driver);
4206 static void __exit sm_it87_exit(void)
4208 /* NULL check handled by platform_device_unregister */
4209 platform_device_unregister(it87_pdev[1]);
4210 platform_device_unregister(it87_pdev[0]);
4211 platform_driver_unregister(&it87_driver);
4214 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
4215 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
4216 module_param(update_vbat, bool, 0000);
4217 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
4218 module_param(fix_pwm_polarity, bool, 0000);
4219 MODULE_PARM_DESC(fix_pwm_polarity,
4220 "Force PWM polarity to active high (DANGEROUS)");
4221 MODULE_LICENSE("GPL");
4223 module_init(sm_it87_init);
4224 module_exit(sm_it87_exit);