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 * IT8620E Super I/O chip w/LPC interface
16 * IT8622E Super I/O chip w/LPC interface
17 * IT8623E Super I/O chip w/LPC interface
18 * IT8628E Super I/O chip w/LPC interface
19 * IT8665E Super I/O chip w/LPC interface
20 * IT8686E Super I/O chip w/LPC interface
21 * IT8705F Super I/O chip w/LPC interface
22 * IT8712F Super I/O chip w/LPC interface
23 * IT8716F Super I/O chip w/LPC interface
24 * IT8718F Super I/O chip w/LPC interface
25 * IT8720F Super I/O chip w/LPC interface
26 * IT8721F Super I/O chip w/LPC interface
27 * IT8726F Super I/O chip w/LPC interface
28 * IT8728F Super I/O chip w/LPC interface
29 * IT8732F Super I/O chip w/LPC interface
30 * IT8758E Super I/O chip w/LPC interface
31 * IT8771E Super I/O chip w/LPC interface
32 * IT8772E Super I/O chip w/LPC interface
33 * IT8781F Super I/O chip w/LPC interface
34 * IT8782F Super I/O chip w/LPC interface
35 * IT8783E/F Super I/O chip w/LPC interface
36 * IT8786E Super I/O chip w/LPC interface
37 * IT8790E Super I/O chip w/LPC interface
38 * IT8792E Super I/O chip w/LPC interface
39 * Sis950 A clone of the IT8705F
41 * Copyright (C) 2001 Chris Gauthron
42 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
44 * This program is free software; you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation; either version 2 of the License, or
47 * (at your option) any later version.
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
55 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
57 #include <linux/bitops.h>
58 #include <linux/module.h>
59 #include <linux/init.h>
60 #include <linux/slab.h>
61 #include <linux/jiffies.h>
62 #include <linux/platform_device.h>
63 #include <linux/hwmon.h>
64 #include <linux/hwmon-sysfs.h>
65 #include <linux/hwmon-vid.h>
66 #include <linux/err.h>
67 #include <linux/mutex.h>
68 #include <linux/sysfs.h>
69 #include <linux/string.h>
70 #include <linux/dmi.h>
71 #include <linux/acpi.h>
75 #define DRVNAME "it87"
77 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
78 it8771, it8772, it8781, it8782, it8783, it8786, it8790,
79 it8792, it8603, it8607, it8620, it8622, it8628, it8665,
82 static unsigned short force_id;
83 module_param(force_id, ushort, 0);
84 MODULE_PARM_DESC(force_id, "Override the detected device ID");
86 static struct platform_device *it87_pdev[2];
88 #define REG_2E 0x2e /* The register to read/write */
89 #define REG_4E 0x4e /* Secondary register to read/write */
91 #define DEV 0x07 /* Register: Logical device select */
92 #define PME 0x04 /* The device with the fan registers in it */
94 /* The device with the IT8718F/IT8720F VID value in it */
97 #define DEVID 0x20 /* Register: Device ID */
98 #define DEVREV 0x22 /* Register: Device Revision */
100 static inline int superio_inb(int ioreg, int reg)
103 return inb(ioreg + 1);
106 static inline void superio_outb(int ioreg, int reg, int val)
109 outb(val, ioreg + 1);
112 static int superio_inw(int ioreg, int reg)
117 val = inb(ioreg + 1) << 8;
119 val |= inb(ioreg + 1);
123 static inline void superio_select(int ioreg, int ldn)
126 outb(ldn, ioreg + 1);
129 static inline int superio_enter(int ioreg)
132 * Try to reserve ioreg and ioreg + 1 for exclusive access.
134 if (!request_muxed_region(ioreg, 2, DRVNAME))
140 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
144 static inline void superio_exit(int ioreg)
147 outb(0x02, ioreg + 1);
148 release_region(ioreg, 2);
151 /* Logical device 4 registers */
152 #define IT8712F_DEVID 0x8712
153 #define IT8705F_DEVID 0x8705
154 #define IT8716F_DEVID 0x8716
155 #define IT8718F_DEVID 0x8718
156 #define IT8720F_DEVID 0x8720
157 #define IT8721F_DEVID 0x8721
158 #define IT8726F_DEVID 0x8726
159 #define IT8728F_DEVID 0x8728
160 #define IT8732F_DEVID 0x8732
161 #define IT8792E_DEVID 0x8733
162 #define IT8771E_DEVID 0x8771
163 #define IT8772E_DEVID 0x8772
164 #define IT8781F_DEVID 0x8781
165 #define IT8782F_DEVID 0x8782
166 #define IT8783E_DEVID 0x8783
167 #define IT8786E_DEVID 0x8786
168 #define IT8790E_DEVID 0x8790
169 #define IT8603E_DEVID 0x8603
170 #define IT8607E_DEVID 0x8607
171 #define IT8620E_DEVID 0x8620
172 #define IT8622E_DEVID 0x8622
173 #define IT8623E_DEVID 0x8623
174 #define IT8628E_DEVID 0x8628
175 #define IT8665E_DEVID 0x8665
176 #define IT8686E_DEVID 0x8686
177 #define IT87_ACT_REG 0x30
178 #define IT87_BASE_REG 0x60
180 /* Logical device 7 registers (IT8712F and later) */
181 #define IT87_SIO_GPIO1_REG 0x25
182 #define IT87_SIO_GPIO2_REG 0x26
183 #define IT87_SIO_GPIO3_REG 0x27
184 #define IT87_SIO_GPIO4_REG 0x28
185 #define IT87_SIO_GPIO5_REG 0x29
186 #define IT87_SIO_GPIO9_REG 0xd3
187 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
188 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
189 #define IT87_SIO_PINX4_REG 0x2d /* Pin selection */
190 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
191 #define IT87_SIO_VID_REG 0xfc /* VID value */
192 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
194 /* Update battery voltage after every reading if true */
195 static bool update_vbat;
197 /* Not all BIOSes properly configure the PWM registers */
198 static bool fix_pwm_polarity;
200 /* Many IT87 constants specified below */
202 /* Length of ISA address segment */
203 #define IT87_EXTENT 8
205 /* Length of ISA address segment for Environmental Controller */
206 #define IT87_EC_EXTENT 2
208 /* Offset of EC registers from ISA base address */
209 #define IT87_EC_OFFSET 5
211 /* Where are the ISA address/data registers relative to the EC base address */
212 #define IT87_ADDR_REG_OFFSET 0
213 #define IT87_DATA_REG_OFFSET 1
215 /*----- The IT87 registers -----*/
217 #define IT87_REG_CONFIG 0x00
219 #define IT87_REG_ALARM1 0x01
220 #define IT87_REG_ALARM2 0x02
221 #define IT87_REG_ALARM3 0x03
223 #define IT87_REG_BANK 0x06
226 * The IT8718F and IT8720F have the VID value in a different register, in
227 * Super-I/O configuration space.
229 #define IT87_REG_VID 0x0a
231 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
232 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
235 #define IT87_REG_FAN_DIV 0x0b
236 #define IT87_REG_FAN_16BIT 0x0c
240 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
241 * - up to 6 temp (1 to 6)
242 * - up to 6 fan (1 to 6)
245 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
246 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
247 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
248 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
249 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
251 #define IT87_REG_FAN_MAIN_CTRL 0x13
252 #define IT87_REG_FAN_CTL 0x14
253 static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
254 static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
256 static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
257 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
259 #define IT87_REG_TEMP(nr) (0x29 + (nr))
261 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
262 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
263 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
264 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
266 #define IT87_REG_VIN_ENABLE 0x50
267 #define IT87_REG_TEMP_ENABLE 0x51
268 #define IT87_REG_TEMP_EXTRA 0x55
269 #define IT87_REG_BEEP_ENABLE 0x5c
271 #define IT87_REG_CHIPID 0x58
273 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
275 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
276 #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
278 #define IT87_REG_TEMP456_ENABLE 0x77
280 #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
281 #define NUM_VIN_LIMIT 8
283 #define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
284 #define NUM_TEMP_LIMIT 3
285 #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
286 #define NUM_FAN_DIV 3
287 #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
288 #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
290 struct it87_devices {
292 const char * const suffix;
298 #define FEAT_12MV_ADC BIT(0)
299 #define FEAT_NEWER_AUTOPWM BIT(1)
300 #define FEAT_OLD_AUTOPWM BIT(2)
301 #define FEAT_16BIT_FANS BIT(3)
302 #define FEAT_TEMP_OFFSET BIT(4)
303 #define FEAT_TEMP_PECI BIT(5)
304 #define FEAT_TEMP_OLD_PECI BIT(6)
305 #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
306 #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
307 #define FEAT_VID BIT(9) /* Set if chip supports VID */
308 #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
309 #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
310 #define FEAT_10_9MV_ADC BIT(12)
311 #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
312 #define FEAT_FIVE_PWM BIT(14) /* Chip supports 5 pwm chn */
313 #define FEAT_SIX_PWM BIT(15) /* Chip supports 6 pwm chn */
314 #define FEAT_PWM_FREQ2 BIT(16) /* Separate pwm freq 2 */
315 #define FEAT_SIX_TEMP BIT(17) /* Up to 6 temp sensors */
316 #define FEAT_VIN3_5V BIT(18) /* VIN3 connected to +5V */
317 #define FEAT_FOUR_FANS BIT(19) /* Supports four fans */
318 #define FEAT_FOUR_PWM BIT(20) /* Supports four fan controls */
319 #define FEAT_BANK_SEL BIT(21) /* Chip has multi-bank support */
320 #define FEAT_SCALING BIT(22) /* Internal voltage scaling */
322 static const struct it87_devices it87_devices[] = {
326 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
331 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
332 /* may need to overwrite */
337 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
338 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
343 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
344 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
346 .old_peci_mask = 0x4,
351 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
352 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
354 .old_peci_mask = 0x4,
359 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
360 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
361 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
362 | FEAT_PWM_FREQ2 | FEAT_SCALING,
364 .old_peci_mask = 0x02, /* Actually reports PCH */
369 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
370 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
371 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_SCALING,
377 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
378 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
379 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_FOUR_FANS
382 .old_peci_mask = 0x02, /* Actually reports PCH */
387 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
388 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
389 | FEAT_PWM_FREQ2 | FEAT_SCALING,
390 /* PECI: guesswork */
392 /* 16 bit fans (OHM) */
393 /* three fans, always 16 bit (guesswork) */
399 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
400 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
401 | FEAT_PWM_FREQ2 | FEAT_SCALING,
402 /* PECI (coreboot) */
403 /* 12mV ADC (HWSensors4, OHM) */
404 /* 16 bit fans (HWSensors4, OHM) */
405 /* three fans, always 16 bit (datasheet) */
411 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
412 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
413 .old_peci_mask = 0x4,
418 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
419 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
420 .old_peci_mask = 0x4,
425 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
426 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
427 .old_peci_mask = 0x4,
432 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
433 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
440 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
441 | FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI
442 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
448 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
449 | FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI
450 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
456 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
457 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
458 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
464 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
465 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
466 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
472 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
473 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
474 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
475 | FEAT_SIX_TEMP | FEAT_VIN3_5V | FEAT_SCALING,
481 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
482 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
483 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
484 | FEAT_AVCC3 | FEAT_VIN3_5V | FEAT_SCALING,
490 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
491 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
492 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
493 | FEAT_SIX_TEMP | FEAT_SCALING,
499 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
500 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
501 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_SIX_FANS
502 | FEAT_SIX_PWM | FEAT_BANK_SEL,
508 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
509 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
510 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
511 | FEAT_SIX_TEMP | FEAT_BANK_SEL | FEAT_SCALING,
516 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
517 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
518 #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
519 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
520 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
521 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
522 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
523 ((data)->peci_mask & BIT(nr)))
524 #define has_temp_old_peci(data, nr) \
525 (((data)->features & FEAT_TEMP_OLD_PECI) && \
526 ((data)->old_peci_mask & BIT(nr)))
527 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
528 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
530 #define has_vid(data) ((data)->features & FEAT_VID)
531 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
532 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
533 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
534 #define has_five_pwm(data) ((data)->features & (FEAT_FIVE_PWM \
536 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
537 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
538 #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
539 #define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
540 #define has_four_fans(data) ((data)->features & (FEAT_FOUR_FANS | \
543 #define has_four_pwm(data) ((data)->features & (FEAT_FOUR_PWM | \
546 #define has_bank_sel(data) ((data)->features & FEAT_BANK_SEL)
547 #define has_scaling(data) ((data)->features & FEAT_SCALING)
549 struct it87_sio_data {
551 /* Values read from Super-I/O config space */
555 u8 internal; /* Internal sensors can be labeled */
556 /* Features skipped based on config or DMI */
565 * For each registered chip, we need to keep some data in memory.
566 * The structure is dynamically allocated.
569 const struct attribute_group *groups[7];
578 struct mutex update_lock;
579 char valid; /* !=0 if following fields are valid */
580 unsigned long last_updated; /* In jiffies */
582 u16 in_scaled; /* Internal voltage sensors are scaled */
583 u16 in_internal; /* Bitfield, internal sensors (for labels) */
584 u16 has_in; /* Bitfield, voltage sensors enabled */
585 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
586 u8 has_fan; /* Bitfield, fans enabled */
587 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
588 u8 has_temp; /* Bitfield, temp sensors enabled */
589 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
590 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
591 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
592 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
593 bool has_vid; /* True if VID supported */
594 u8 vid; /* Register encoding, combined */
596 u32 alarms; /* Register encoding, combined */
597 bool has_beep; /* true if beep supported */
598 u8 beeps; /* Register encoding */
599 u8 fan_main_ctrl; /* Register value */
600 u8 fan_ctl; /* Register value */
603 * The following 3 arrays correspond to the same registers up to
604 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
605 * 7, and we want to preserve settings on mode changes, so we have
606 * to track all values separately.
607 * Starting with the IT8721F, the manual PWM duty cycles are stored
608 * in separate registers (8-bit values), so the separate tracking
609 * is no longer needed, but it is still done to keep the driver
612 u8 has_pwm; /* Bitfield, pwm control enabled */
613 u8 pwm_ctrl[NUM_PWM]; /* Register value */
614 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
615 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
617 /* Automatic fan speed control registers */
618 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
619 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
622 static int adc_lsb(const struct it87_data *data, int nr)
626 if (has_12mv_adc(data))
628 else if (has_10_9mv_adc(data))
632 if (data->in_scaled & BIT(nr))
637 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
639 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
640 return clamp_val(val, 0, 255);
643 static int in_from_reg(const struct it87_data *data, int nr, int val)
645 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
648 static inline u8 FAN_TO_REG(long rpm, int div)
652 rpm = clamp_val(rpm, 1, 1000000);
653 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
656 static inline u16 FAN16_TO_REG(long rpm)
660 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
663 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
664 1350000 / ((val) * (div)))
665 /* The divider is fixed to 2 in 16-bit mode */
666 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
667 1350000 / ((val) * 2))
669 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
670 ((val) + 500) / 1000), -128, 127))
671 #define TEMP_FROM_REG(val) ((val) * 1000)
673 static u8 pwm_to_reg(const struct it87_data *data, long val)
675 if (has_newer_autopwm(data))
681 static int pwm_from_reg(const struct it87_data *data, u8 reg)
683 if (has_newer_autopwm(data))
686 return (reg & 0x7f) << 1;
689 static int DIV_TO_REG(int val)
693 while (answer < 7 && (val >>= 1))
698 #define DIV_FROM_REG(val) BIT(val)
701 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
702 * depending on the chip type, to calculate the actual PWM frequency.
704 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
705 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
706 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
707 * sometimes just one. It is unknown if this is a datasheet error or real,
708 * so this is ignored for now.
710 static const unsigned int pwm_freq[8] = {
721 static int _it87_read_value(struct it87_data *data, u8 reg)
723 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
724 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
727 static void _it87_write_value(struct it87_data *data, u8 reg, u8 value)
729 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
730 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
733 static void it87_set_bank(struct it87_data *data, u8 bank)
735 if (has_bank_sel(data) && bank != data->bank) {
736 u8 breg = _it87_read_value(data, IT87_REG_BANK);
741 _it87_write_value(data, IT87_REG_BANK, breg);
746 * Must be called with data->update_lock held, except during initialization.
747 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
748 * would slow down the IT87 access and should not be necessary.
750 static int it87_read_value(struct it87_data *data, u16 reg)
752 it87_set_bank(data, reg >> 8);
753 return _it87_read_value(data, reg & 0xff);
757 * Must be called with data->update_lock held, except during initialization.
758 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
759 * would slow down the IT87 access and should not be necessary.
761 static void it87_write_value(struct it87_data *data, u16 reg, u8 value)
763 it87_set_bank(data, reg >> 8);
764 _it87_write_value(data, reg & 0xff, value);
767 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
769 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
770 if (has_newer_autopwm(data)) {
771 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
772 data->pwm_duty[nr] = it87_read_value(data,
773 IT87_REG_PWM_DUTY[nr]);
775 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
776 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
777 else /* Manual mode */
778 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
781 if (has_old_autopwm(data)) {
784 for (i = 0; i < 5 ; i++)
785 data->auto_temp[nr][i] = it87_read_value(data,
786 IT87_REG_AUTO_TEMP(nr, i));
787 for (i = 0; i < 3 ; i++)
788 data->auto_pwm[nr][i] = it87_read_value(data,
789 IT87_REG_AUTO_PWM(nr, i));
790 } else if (has_newer_autopwm(data)) {
794 * 0: temperature hysteresis (base + 5)
795 * 1: fan off temperature (base + 0)
796 * 2: fan start temperature (base + 1)
797 * 3: fan max temperature (base + 2)
799 data->auto_temp[nr][0] =
800 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
802 for (i = 0; i < 3 ; i++)
803 data->auto_temp[nr][i + 1] =
804 it87_read_value(data,
805 IT87_REG_AUTO_TEMP(nr, i));
807 * 0: start pwm value (base + 3)
808 * 1: pwm slope (base + 4, 1/8th pwm)
810 data->auto_pwm[nr][0] =
811 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
812 data->auto_pwm[nr][1] =
813 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
817 static struct it87_data *it87_update_device(struct device *dev)
819 struct it87_data *data = dev_get_drvdata(dev);
822 mutex_lock(&data->update_lock);
824 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
828 * Cleared after each update, so reenable. Value
829 * returned by this read will be previous value
831 it87_write_value(data, IT87_REG_CONFIG,
832 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
834 for (i = 0; i < NUM_VIN; i++) {
835 if (!(data->has_in & BIT(i)))
839 it87_read_value(data, IT87_REG_VIN[i]);
841 /* VBAT and AVCC don't have limit registers */
842 if (i >= NUM_VIN_LIMIT)
846 it87_read_value(data, IT87_REG_VIN_MIN(i));
848 it87_read_value(data, IT87_REG_VIN_MAX(i));
851 for (i = 0; i < NUM_FAN; i++) {
852 /* Skip disabled fans */
853 if (!(data->has_fan & BIT(i)))
857 it87_read_value(data, IT87_REG_FAN_MIN[i]);
858 data->fan[i][0] = it87_read_value(data,
860 /* Add high byte if in 16-bit mode */
861 if (has_16bit_fans(data)) {
862 data->fan[i][0] |= it87_read_value(data,
863 IT87_REG_FANX[i]) << 8;
864 data->fan[i][1] |= it87_read_value(data,
865 IT87_REG_FANX_MIN[i]) << 8;
868 for (i = 0; i < NUM_TEMP; i++) {
869 if (!(data->has_temp & BIT(i)))
872 it87_read_value(data, IT87_REG_TEMP(i));
874 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
876 it87_read_value(data,
877 IT87_REG_TEMP_OFFSET[i]);
879 if (i >= NUM_TEMP_LIMIT)
883 it87_read_value(data, IT87_REG_TEMP_LOW(i));
885 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
888 /* Newer chips don't have clock dividers */
889 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
890 i = it87_read_value(data, IT87_REG_FAN_DIV);
891 data->fan_div[0] = i & 0x07;
892 data->fan_div[1] = (i >> 3) & 0x07;
893 data->fan_div[2] = (i & 0x40) ? 3 : 1;
897 it87_read_value(data, IT87_REG_ALARM1) |
898 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
899 (it87_read_value(data, IT87_REG_ALARM3) << 16);
900 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
902 data->fan_main_ctrl = it87_read_value(data,
903 IT87_REG_FAN_MAIN_CTRL);
904 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
905 for (i = 0; i < NUM_PWM; i++) {
906 if (!(data->has_pwm & BIT(i)))
908 it87_update_pwm_ctrl(data, i);
911 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
912 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
914 * The IT8705F does not have VID capability.
915 * The IT8718F and later don't use IT87_REG_VID for the
918 if (data->type == it8712 || data->type == it8716) {
919 data->vid = it87_read_value(data, IT87_REG_VID);
921 * The older IT8712F revisions had only 5 VID pins,
922 * but we assume it is always safe to read 6 bits.
926 data->last_updated = jiffies;
930 mutex_unlock(&data->update_lock);
935 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
938 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
939 struct it87_data *data = it87_update_device(dev);
940 int index = sattr->index;
943 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
946 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
947 const char *buf, size_t count)
949 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
950 struct it87_data *data = dev_get_drvdata(dev);
951 int index = sattr->index;
955 if (kstrtoul(buf, 10, &val) < 0)
958 mutex_lock(&data->update_lock);
959 data->in[nr][index] = in_to_reg(data, nr, val);
960 it87_write_value(data,
961 index == 1 ? IT87_REG_VIN_MIN(nr)
962 : IT87_REG_VIN_MAX(nr),
963 data->in[nr][index]);
964 mutex_unlock(&data->update_lock);
968 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
969 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
971 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
974 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
975 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
977 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
980 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
981 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
983 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
986 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
987 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
989 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
992 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
993 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
995 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
998 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
999 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
1001 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
1004 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
1005 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
1007 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
1010 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
1011 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
1013 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
1016 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
1017 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
1018 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
1019 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
1020 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
1022 /* Up to 6 temperatures */
1023 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
1026 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1028 int index = sattr->index;
1029 struct it87_data *data = it87_update_device(dev);
1031 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
1034 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
1035 const char *buf, size_t count)
1037 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1039 int index = sattr->index;
1040 struct it87_data *data = dev_get_drvdata(dev);
1044 if (kstrtol(buf, 10, &val) < 0)
1047 mutex_lock(&data->update_lock);
1052 reg = IT87_REG_TEMP_LOW(nr);
1055 reg = IT87_REG_TEMP_HIGH(nr);
1058 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1059 if (!(regval & 0x80)) {
1061 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
1064 reg = IT87_REG_TEMP_OFFSET[nr];
1068 data->temp[nr][index] = TEMP_TO_REG(val);
1069 it87_write_value(data, reg, data->temp[nr][index]);
1070 mutex_unlock(&data->update_lock);
1074 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
1075 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1077 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1079 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
1081 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
1082 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1084 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1086 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1088 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
1089 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1091 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1093 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1095 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
1096 static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
1097 static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
1099 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1102 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1103 int nr = sensor_attr->index;
1104 struct it87_data *data = it87_update_device(dev);
1105 u8 reg = data->sensor; /* In case value is updated while used */
1106 u8 extra = data->extra;
1108 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
1109 (has_temp_old_peci(data, nr) && (extra & 0x80)))
1110 return sprintf(buf, "6\n"); /* Intel PECI */
1111 if (reg & (1 << nr))
1112 return sprintf(buf, "3\n"); /* thermal diode */
1113 if (reg & (8 << nr))
1114 return sprintf(buf, "4\n"); /* thermistor */
1115 return sprintf(buf, "0\n"); /* disabled */
1118 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1119 const char *buf, size_t count)
1121 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1122 int nr = sensor_attr->index;
1124 struct it87_data *data = dev_get_drvdata(dev);
1128 if (kstrtol(buf, 10, &val) < 0)
1131 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1134 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1136 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1137 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1139 if (val == 2) { /* backwards compatibility */
1141 "Sensor type 2 is deprecated, please use 4 instead\n");
1144 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1149 else if (has_temp_peci(data, nr) && val == 6)
1150 reg |= (nr + 1) << 6;
1151 else if (has_temp_old_peci(data, nr) && val == 6)
1156 mutex_lock(&data->update_lock);
1158 data->extra = extra;
1159 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
1160 if (has_temp_old_peci(data, nr))
1161 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1162 data->valid = 0; /* Force cache refresh */
1163 mutex_unlock(&data->update_lock);
1167 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1169 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1171 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1176 static int pwm_mode(const struct it87_data *data, int nr)
1178 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1179 return 0; /* Full speed */
1180 if (data->pwm_ctrl[nr] & 0x80)
1181 return 2; /* Automatic mode */
1182 if ((data->type == it8603 || nr >= 3) &&
1183 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1184 return 0; /* Full speed */
1186 return 1; /* Manual mode */
1189 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1192 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1194 int index = sattr->index;
1196 struct it87_data *data = it87_update_device(dev);
1198 speed = has_16bit_fans(data) ?
1199 FAN16_FROM_REG(data->fan[nr][index]) :
1200 FAN_FROM_REG(data->fan[nr][index],
1201 DIV_FROM_REG(data->fan_div[nr]));
1202 return sprintf(buf, "%d\n", speed);
1205 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1208 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1209 struct it87_data *data = it87_update_device(dev);
1210 int nr = sensor_attr->index;
1212 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1215 static ssize_t show_pwm_enable(struct device *dev,
1216 struct device_attribute *attr, char *buf)
1218 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1219 struct it87_data *data = it87_update_device(dev);
1220 int nr = sensor_attr->index;
1222 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1225 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1228 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1229 struct it87_data *data = it87_update_device(dev);
1230 int nr = sensor_attr->index;
1232 return sprintf(buf, "%d\n",
1233 pwm_from_reg(data, data->pwm_duty[nr]));
1236 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1239 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1240 struct it87_data *data = it87_update_device(dev);
1241 int nr = sensor_attr->index;
1245 if (has_pwm_freq2(data) && nr == 1)
1246 index = (data->extra >> 4) & 0x07;
1248 index = (data->fan_ctl >> 4) & 0x07;
1250 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1252 return sprintf(buf, "%u\n", freq);
1255 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1256 const char *buf, size_t count)
1258 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1260 int index = sattr->index;
1262 struct it87_data *data = dev_get_drvdata(dev);
1266 if (kstrtol(buf, 10, &val) < 0)
1269 mutex_lock(&data->update_lock);
1271 if (has_16bit_fans(data)) {
1272 data->fan[nr][index] = FAN16_TO_REG(val);
1273 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1274 data->fan[nr][index] & 0xff);
1275 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1276 data->fan[nr][index] >> 8);
1278 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1281 data->fan_div[nr] = reg & 0x07;
1284 data->fan_div[nr] = (reg >> 3) & 0x07;
1287 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1290 data->fan[nr][index] =
1291 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1292 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1293 data->fan[nr][index]);
1296 mutex_unlock(&data->update_lock);
1300 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1301 const char *buf, size_t count)
1303 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1304 struct it87_data *data = dev_get_drvdata(dev);
1305 int nr = sensor_attr->index;
1310 if (kstrtoul(buf, 10, &val) < 0)
1313 mutex_lock(&data->update_lock);
1314 old = it87_read_value(data, IT87_REG_FAN_DIV);
1316 /* Save fan min limit */
1317 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1322 data->fan_div[nr] = DIV_TO_REG(val);
1326 data->fan_div[nr] = 1;
1328 data->fan_div[nr] = 3;
1331 val |= (data->fan_div[0] & 0x07);
1332 val |= (data->fan_div[1] & 0x07) << 3;
1333 if (data->fan_div[2] == 3)
1335 it87_write_value(data, IT87_REG_FAN_DIV, val);
1337 /* Restore fan min limit */
1338 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1339 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
1341 mutex_unlock(&data->update_lock);
1345 /* Returns 0 if OK, -EINVAL otherwise */
1346 static int check_trip_points(struct device *dev, int nr)
1348 const struct it87_data *data = dev_get_drvdata(dev);
1351 if (has_old_autopwm(data)) {
1352 for (i = 0; i < 3; i++) {
1353 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1356 for (i = 0; i < 2; i++) {
1357 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1360 } else if (has_newer_autopwm(data)) {
1361 for (i = 1; i < 3; i++) {
1362 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1369 "Inconsistent trip points, not switching to automatic mode\n");
1370 dev_err(dev, "Adjust the trip points and try again\n");
1375 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1376 const char *buf, size_t count)
1378 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1379 struct it87_data *data = dev_get_drvdata(dev);
1380 int nr = sensor_attr->index;
1383 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1386 /* Check trip points before switching to automatic mode */
1388 if (check_trip_points(dev, nr) < 0)
1392 mutex_lock(&data->update_lock);
1395 if (nr < 3 && data->type != it8603) {
1397 /* make sure the fan is on when in on/off mode */
1398 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1399 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1400 /* set on/off mode */
1401 data->fan_main_ctrl &= ~BIT(nr);
1402 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1403 data->fan_main_ctrl);
1407 /* No on/off mode, set maximum pwm value */
1408 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1409 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1410 data->pwm_duty[nr]);
1411 /* and set manual mode */
1412 if (has_newer_autopwm(data)) {
1413 ctrl = (data->pwm_ctrl[nr] & 0x7c) |
1414 data->pwm_temp_map[nr];
1416 ctrl = data->pwm_duty[nr];
1418 data->pwm_ctrl[nr] = ctrl;
1419 it87_write_value(data, IT87_REG_PWM[nr], ctrl);
1424 if (has_newer_autopwm(data)) {
1425 ctrl = (data->pwm_ctrl[nr] & 0x7c) |
1426 data->pwm_temp_map[nr];
1430 ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1432 data->pwm_ctrl[nr] = ctrl;
1433 it87_write_value(data, IT87_REG_PWM[nr], ctrl);
1435 if (data->type != it8603 && nr < 3) {
1436 /* set SmartGuardian mode */
1437 data->fan_main_ctrl |= BIT(nr);
1438 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1439 data->fan_main_ctrl);
1443 mutex_unlock(&data->update_lock);
1447 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1448 const char *buf, size_t count)
1450 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1451 struct it87_data *data = dev_get_drvdata(dev);
1452 int nr = sensor_attr->index;
1455 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1458 mutex_lock(&data->update_lock);
1459 it87_update_pwm_ctrl(data, nr);
1460 if (has_newer_autopwm(data)) {
1462 * If we are in automatic mode, the PWM duty cycle register
1463 * is read-only so we can't write the value.
1465 if (data->pwm_ctrl[nr] & 0x80) {
1466 mutex_unlock(&data->update_lock);
1469 data->pwm_duty[nr] = pwm_to_reg(data, val);
1470 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1471 data->pwm_duty[nr]);
1473 data->pwm_duty[nr] = pwm_to_reg(data, val);
1475 * If we are in manual mode, write the duty cycle immediately;
1476 * otherwise, just store it for later use.
1478 if (!(data->pwm_ctrl[nr] & 0x80)) {
1479 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1480 it87_write_value(data, IT87_REG_PWM[nr],
1481 data->pwm_ctrl[nr]);
1484 mutex_unlock(&data->update_lock);
1488 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1489 const char *buf, size_t count)
1491 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1492 struct it87_data *data = dev_get_drvdata(dev);
1493 int nr = sensor_attr->index;
1497 if (kstrtoul(buf, 10, &val) < 0)
1500 val = clamp_val(val, 0, 1000000);
1501 val *= has_newer_autopwm(data) ? 256 : 128;
1503 /* Search for the nearest available frequency */
1504 for (i = 0; i < 7; i++) {
1505 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1509 mutex_lock(&data->update_lock);
1511 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1512 data->fan_ctl |= i << 4;
1513 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1515 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1516 data->extra |= i << 4;
1517 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1519 mutex_unlock(&data->update_lock);
1524 static ssize_t show_pwm_temp_map(struct device *dev,
1525 struct device_attribute *attr, char *buf)
1527 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1528 struct it87_data *data = it87_update_device(dev);
1529 int nr = sensor_attr->index;
1532 map = data->pwm_temp_map[nr];
1534 map = 0; /* Should never happen */
1535 if (nr >= 3) /* pwm channels 3..6 map to temp4..6 */
1538 return sprintf(buf, "%d\n", (int)BIT(map));
1541 static ssize_t set_pwm_temp_map(struct device *dev,
1542 struct device_attribute *attr, const char *buf,
1545 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1546 struct it87_data *data = dev_get_drvdata(dev);
1547 int nr = sensor_attr->index;
1551 if (kstrtol(buf, 10, &val) < 0)
1571 mutex_lock(&data->update_lock);
1572 it87_update_pwm_ctrl(data, nr);
1573 data->pwm_temp_map[nr] = reg;
1575 * If we are in automatic mode, write the temp mapping immediately;
1576 * otherwise, just store it for later use.
1578 if (data->pwm_ctrl[nr] & 0x80) {
1579 data->pwm_ctrl[nr] = (data->pwm_ctrl[nr] & 0xfc) |
1580 data->pwm_temp_map[nr];
1581 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1583 mutex_unlock(&data->update_lock);
1587 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1590 struct it87_data *data = it87_update_device(dev);
1591 struct sensor_device_attribute_2 *sensor_attr =
1592 to_sensor_dev_attr_2(attr);
1593 int nr = sensor_attr->nr;
1594 int point = sensor_attr->index;
1596 return sprintf(buf, "%d\n",
1597 pwm_from_reg(data, data->auto_pwm[nr][point]));
1600 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1601 const char *buf, size_t count)
1603 struct it87_data *data = dev_get_drvdata(dev);
1604 struct sensor_device_attribute_2 *sensor_attr =
1605 to_sensor_dev_attr_2(attr);
1606 int nr = sensor_attr->nr;
1607 int point = sensor_attr->index;
1611 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1614 mutex_lock(&data->update_lock);
1615 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1616 if (has_newer_autopwm(data))
1617 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1619 regaddr = IT87_REG_AUTO_PWM(nr, point);
1620 it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1621 mutex_unlock(&data->update_lock);
1625 static ssize_t show_auto_pwm_slope(struct device *dev,
1626 struct device_attribute *attr, char *buf)
1628 struct it87_data *data = it87_update_device(dev);
1629 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1630 int nr = sensor_attr->index;
1632 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1635 static ssize_t set_auto_pwm_slope(struct device *dev,
1636 struct device_attribute *attr,
1637 const char *buf, size_t count)
1639 struct it87_data *data = dev_get_drvdata(dev);
1640 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1641 int nr = sensor_attr->index;
1644 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1647 mutex_lock(&data->update_lock);
1648 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1649 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1650 data->auto_pwm[nr][1]);
1651 mutex_unlock(&data->update_lock);
1655 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1658 struct it87_data *data = it87_update_device(dev);
1659 struct sensor_device_attribute_2 *sensor_attr =
1660 to_sensor_dev_attr_2(attr);
1661 int nr = sensor_attr->nr;
1662 int point = sensor_attr->index;
1665 if (has_old_autopwm(data) || point)
1666 reg = data->auto_temp[nr][point];
1668 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1670 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
1673 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1674 const char *buf, size_t count)
1676 struct it87_data *data = dev_get_drvdata(dev);
1677 struct sensor_device_attribute_2 *sensor_attr =
1678 to_sensor_dev_attr_2(attr);
1679 int nr = sensor_attr->nr;
1680 int point = sensor_attr->index;
1684 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1687 mutex_lock(&data->update_lock);
1688 if (has_newer_autopwm(data) && !point) {
1689 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1690 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1691 data->auto_temp[nr][0] = reg;
1692 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1694 reg = TEMP_TO_REG(val);
1695 data->auto_temp[nr][point] = reg;
1696 if (has_newer_autopwm(data))
1698 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1700 mutex_unlock(&data->update_lock);
1704 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1705 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1707 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1710 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1711 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1713 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1716 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1717 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1719 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1722 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1723 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1726 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1727 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1730 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1731 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1734 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1735 show_pwm_enable, set_pwm_enable, 0);
1736 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1737 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1739 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
1740 show_pwm_temp_map, set_pwm_temp_map, 0);
1741 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1742 show_auto_pwm, set_auto_pwm, 0, 0);
1743 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1744 show_auto_pwm, set_auto_pwm, 0, 1);
1745 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1746 show_auto_pwm, set_auto_pwm, 0, 2);
1747 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1748 show_auto_pwm, NULL, 0, 3);
1749 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1750 show_auto_temp, set_auto_temp, 0, 1);
1751 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1752 show_auto_temp, set_auto_temp, 0, 0);
1753 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1754 show_auto_temp, set_auto_temp, 0, 2);
1755 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1756 show_auto_temp, set_auto_temp, 0, 3);
1757 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1758 show_auto_temp, set_auto_temp, 0, 4);
1759 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1760 show_auto_pwm, set_auto_pwm, 0, 0);
1761 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1762 show_auto_pwm_slope, set_auto_pwm_slope, 0);
1764 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1765 show_pwm_enable, set_pwm_enable, 1);
1766 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1767 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1768 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
1769 show_pwm_temp_map, set_pwm_temp_map, 1);
1770 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1771 show_auto_pwm, set_auto_pwm, 1, 0);
1772 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1773 show_auto_pwm, set_auto_pwm, 1, 1);
1774 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1775 show_auto_pwm, set_auto_pwm, 1, 2);
1776 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1777 show_auto_pwm, NULL, 1, 3);
1778 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1779 show_auto_temp, set_auto_temp, 1, 1);
1780 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1781 show_auto_temp, set_auto_temp, 1, 0);
1782 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1783 show_auto_temp, set_auto_temp, 1, 2);
1784 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1785 show_auto_temp, set_auto_temp, 1, 3);
1786 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1787 show_auto_temp, set_auto_temp, 1, 4);
1788 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1789 show_auto_pwm, set_auto_pwm, 1, 0);
1790 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1791 show_auto_pwm_slope, set_auto_pwm_slope, 1);
1793 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1794 show_pwm_enable, set_pwm_enable, 2);
1795 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1796 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1797 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
1798 show_pwm_temp_map, set_pwm_temp_map, 2);
1799 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1800 show_auto_pwm, set_auto_pwm, 2, 0);
1801 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1802 show_auto_pwm, set_auto_pwm, 2, 1);
1803 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1804 show_auto_pwm, set_auto_pwm, 2, 2);
1805 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1806 show_auto_pwm, NULL, 2, 3);
1807 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1808 show_auto_temp, set_auto_temp, 2, 1);
1809 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1810 show_auto_temp, set_auto_temp, 2, 0);
1811 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1812 show_auto_temp, set_auto_temp, 2, 2);
1813 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1814 show_auto_temp, set_auto_temp, 2, 3);
1815 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1816 show_auto_temp, set_auto_temp, 2, 4);
1817 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1818 show_auto_pwm, set_auto_pwm, 2, 0);
1819 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1820 show_auto_pwm_slope, set_auto_pwm_slope, 2);
1822 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1823 show_pwm_enable, set_pwm_enable, 3);
1824 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1825 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1826 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
1827 show_pwm_temp_map, set_pwm_temp_map, 3);
1828 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1829 show_auto_temp, set_auto_temp, 2, 1);
1830 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1831 show_auto_temp, set_auto_temp, 2, 0);
1832 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1833 show_auto_temp, set_auto_temp, 2, 2);
1834 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1835 show_auto_temp, set_auto_temp, 2, 3);
1836 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1837 show_auto_pwm, set_auto_pwm, 3, 0);
1838 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1839 show_auto_pwm_slope, set_auto_pwm_slope, 3);
1841 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1842 show_pwm_enable, set_pwm_enable, 4);
1843 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1844 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1845 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
1846 show_pwm_temp_map, set_pwm_temp_map, 4);
1847 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1848 show_auto_temp, set_auto_temp, 2, 1);
1849 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1850 show_auto_temp, set_auto_temp, 2, 0);
1851 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1852 show_auto_temp, set_auto_temp, 2, 2);
1853 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1854 show_auto_temp, set_auto_temp, 2, 3);
1855 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1856 show_auto_pwm, set_auto_pwm, 4, 0);
1857 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1858 show_auto_pwm_slope, set_auto_pwm_slope, 4);
1860 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1861 show_pwm_enable, set_pwm_enable, 5);
1862 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1863 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1864 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
1865 show_pwm_temp_map, set_pwm_temp_map, 5);
1866 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1867 show_auto_temp, set_auto_temp, 2, 1);
1868 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1869 show_auto_temp, set_auto_temp, 2, 0);
1870 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1871 show_auto_temp, set_auto_temp, 2, 2);
1872 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1873 show_auto_temp, set_auto_temp, 2, 3);
1874 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1875 show_auto_pwm, set_auto_pwm, 5, 0);
1876 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1877 show_auto_pwm_slope, set_auto_pwm_slope, 5);
1880 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1883 struct it87_data *data = it87_update_device(dev);
1885 return sprintf(buf, "%u\n", data->alarms);
1887 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1889 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1892 struct it87_data *data = it87_update_device(dev);
1893 int bitnr = to_sensor_dev_attr(attr)->index;
1895 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1898 static ssize_t clear_intrusion(struct device *dev,
1899 struct device_attribute *attr, const char *buf,
1902 struct it87_data *data = dev_get_drvdata(dev);
1906 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1909 mutex_lock(&data->update_lock);
1910 config = it87_read_value(data, IT87_REG_CONFIG);
1915 it87_write_value(data, IT87_REG_CONFIG, config);
1916 /* Invalidate cache to force re-read */
1919 mutex_unlock(&data->update_lock);
1924 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1925 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1926 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1927 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1928 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1929 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1930 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1931 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1932 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1933 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1934 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1935 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1936 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1937 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1938 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1939 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1940 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1941 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1942 show_alarm, clear_intrusion, 4);
1944 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1947 struct it87_data *data = it87_update_device(dev);
1948 int bitnr = to_sensor_dev_attr(attr)->index;
1950 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1953 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1954 const char *buf, size_t count)
1956 int bitnr = to_sensor_dev_attr(attr)->index;
1957 struct it87_data *data = dev_get_drvdata(dev);
1960 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
1963 mutex_lock(&data->update_lock);
1964 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1966 data->beeps |= BIT(bitnr);
1968 data->beeps &= ~BIT(bitnr);
1969 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1970 mutex_unlock(&data->update_lock);
1974 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1975 show_beep, set_beep, 1);
1976 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1977 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1978 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1979 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1980 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1981 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1982 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1983 /* fanX_beep writability is set later */
1984 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1985 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1986 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1987 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1988 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1989 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1990 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1991 show_beep, set_beep, 2);
1992 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1993 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1995 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1998 struct it87_data *data = dev_get_drvdata(dev);
2000 return sprintf(buf, "%u\n", data->vrm);
2003 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
2004 const char *buf, size_t count)
2006 struct it87_data *data = dev_get_drvdata(dev);
2009 if (kstrtoul(buf, 10, &val) < 0)
2016 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
2018 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
2021 struct it87_data *data = it87_update_device(dev);
2023 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
2025 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
2027 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
2030 static const char * const labels[] = {
2036 static const char * const labels_it8721[] = {
2042 struct it87_data *data = dev_get_drvdata(dev);
2043 int nr = to_sensor_dev_attr(attr)->index;
2046 if (has_vin3_5v(data) && nr == 0)
2048 else if (has_12mv_adc(data) || has_10_9mv_adc(data))
2049 label = labels_it8721[nr];
2053 return sprintf(buf, "%s\n", label);
2055 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
2056 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
2057 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
2059 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 3);
2061 static umode_t it87_in_is_visible(struct kobject *kobj,
2062 struct attribute *attr, int index)
2064 struct device *dev = container_of(kobj, struct device, kobj);
2065 struct it87_data *data = dev_get_drvdata(dev);
2066 int i = index / 5; /* voltage index */
2067 int a = index % 5; /* attribute index */
2069 if (index >= 40) { /* in8 and higher only have input attributes */
2074 if (!(data->has_in & BIT(i)))
2077 if (a == 4 && !data->has_beep)
2083 static struct attribute *it87_attributes_in[] = {
2084 &sensor_dev_attr_in0_input.dev_attr.attr,
2085 &sensor_dev_attr_in0_min.dev_attr.attr,
2086 &sensor_dev_attr_in0_max.dev_attr.attr,
2087 &sensor_dev_attr_in0_alarm.dev_attr.attr,
2088 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
2090 &sensor_dev_attr_in1_input.dev_attr.attr,
2091 &sensor_dev_attr_in1_min.dev_attr.attr,
2092 &sensor_dev_attr_in1_max.dev_attr.attr,
2093 &sensor_dev_attr_in1_alarm.dev_attr.attr,
2094 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
2096 &sensor_dev_attr_in2_input.dev_attr.attr,
2097 &sensor_dev_attr_in2_min.dev_attr.attr,
2098 &sensor_dev_attr_in2_max.dev_attr.attr,
2099 &sensor_dev_attr_in2_alarm.dev_attr.attr,
2100 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
2102 &sensor_dev_attr_in3_input.dev_attr.attr,
2103 &sensor_dev_attr_in3_min.dev_attr.attr,
2104 &sensor_dev_attr_in3_max.dev_attr.attr,
2105 &sensor_dev_attr_in3_alarm.dev_attr.attr,
2106 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2108 &sensor_dev_attr_in4_input.dev_attr.attr,
2109 &sensor_dev_attr_in4_min.dev_attr.attr,
2110 &sensor_dev_attr_in4_max.dev_attr.attr,
2111 &sensor_dev_attr_in4_alarm.dev_attr.attr,
2112 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2114 &sensor_dev_attr_in5_input.dev_attr.attr,
2115 &sensor_dev_attr_in5_min.dev_attr.attr,
2116 &sensor_dev_attr_in5_max.dev_attr.attr,
2117 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2118 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2120 &sensor_dev_attr_in6_input.dev_attr.attr,
2121 &sensor_dev_attr_in6_min.dev_attr.attr,
2122 &sensor_dev_attr_in6_max.dev_attr.attr,
2123 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2124 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2126 &sensor_dev_attr_in7_input.dev_attr.attr,
2127 &sensor_dev_attr_in7_min.dev_attr.attr,
2128 &sensor_dev_attr_in7_max.dev_attr.attr,
2129 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2130 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2132 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2133 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
2134 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
2135 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
2136 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
2140 static const struct attribute_group it87_group_in = {
2141 .attrs = it87_attributes_in,
2142 .is_visible = it87_in_is_visible,
2145 static umode_t it87_temp_is_visible(struct kobject *kobj,
2146 struct attribute *attr, int index)
2148 struct device *dev = container_of(kobj, struct device, kobj);
2149 struct it87_data *data = dev_get_drvdata(dev);
2150 int i = index / 7; /* temperature index */
2151 int a = index % 7; /* attribute index */
2158 if (!(data->has_temp & BIT(i)))
2161 if (a == 5 && !has_temp_offset(data))
2164 if (a == 6 && !data->has_beep)
2170 static struct attribute *it87_attributes_temp[] = {
2171 &sensor_dev_attr_temp1_input.dev_attr.attr,
2172 &sensor_dev_attr_temp1_max.dev_attr.attr,
2173 &sensor_dev_attr_temp1_min.dev_attr.attr,
2174 &sensor_dev_attr_temp1_type.dev_attr.attr,
2175 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2176 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2177 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2179 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2180 &sensor_dev_attr_temp2_max.dev_attr.attr,
2181 &sensor_dev_attr_temp2_min.dev_attr.attr,
2182 &sensor_dev_attr_temp2_type.dev_attr.attr,
2183 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2184 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2185 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2187 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2188 &sensor_dev_attr_temp3_max.dev_attr.attr,
2189 &sensor_dev_attr_temp3_min.dev_attr.attr,
2190 &sensor_dev_attr_temp3_type.dev_attr.attr,
2191 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2192 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2193 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2195 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2196 &sensor_dev_attr_temp5_input.dev_attr.attr,
2197 &sensor_dev_attr_temp6_input.dev_attr.attr,
2201 static const struct attribute_group it87_group_temp = {
2202 .attrs = it87_attributes_temp,
2203 .is_visible = it87_temp_is_visible,
2206 static umode_t it87_is_visible(struct kobject *kobj,
2207 struct attribute *attr, int index)
2209 struct device *dev = container_of(kobj, struct device, kobj);
2210 struct it87_data *data = dev_get_drvdata(dev);
2212 if ((index == 2 || index == 3) && !data->has_vid)
2215 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2221 static struct attribute *it87_attributes[] = {
2222 &dev_attr_alarms.attr,
2223 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2224 &dev_attr_vrm.attr, /* 2 */
2225 &dev_attr_cpu0_vid.attr, /* 3 */
2226 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2227 &sensor_dev_attr_in7_label.dev_attr.attr,
2228 &sensor_dev_attr_in8_label.dev_attr.attr,
2229 &sensor_dev_attr_in9_label.dev_attr.attr,
2233 static const struct attribute_group it87_group = {
2234 .attrs = it87_attributes,
2235 .is_visible = it87_is_visible,
2238 static umode_t it87_fan_is_visible(struct kobject *kobj,
2239 struct attribute *attr, int index)
2241 struct device *dev = container_of(kobj, struct device, kobj);
2242 struct it87_data *data = dev_get_drvdata(dev);
2243 int i = index / 5; /* fan index */
2244 int a = index % 5; /* attribute index */
2246 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2247 i = (index - 15) / 4 + 3;
2248 a = (index - 15) % 4;
2251 if (!(data->has_fan & BIT(i)))
2254 if (a == 3) { /* beep */
2255 if (!data->has_beep)
2257 /* first fan beep attribute is writable */
2258 if (i == __ffs(data->has_fan))
2259 return attr->mode | S_IWUSR;
2262 if (a == 4 && has_16bit_fans(data)) /* divisor */
2268 static struct attribute *it87_attributes_fan[] = {
2269 &sensor_dev_attr_fan1_input.dev_attr.attr,
2270 &sensor_dev_attr_fan1_min.dev_attr.attr,
2271 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2272 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2273 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2275 &sensor_dev_attr_fan2_input.dev_attr.attr,
2276 &sensor_dev_attr_fan2_min.dev_attr.attr,
2277 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2278 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2279 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2281 &sensor_dev_attr_fan3_input.dev_attr.attr,
2282 &sensor_dev_attr_fan3_min.dev_attr.attr,
2283 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2284 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2285 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2287 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2288 &sensor_dev_attr_fan4_min.dev_attr.attr,
2289 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2290 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2292 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2293 &sensor_dev_attr_fan5_min.dev_attr.attr,
2294 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2295 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2297 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2298 &sensor_dev_attr_fan6_min.dev_attr.attr,
2299 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2300 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2304 static const struct attribute_group it87_group_fan = {
2305 .attrs = it87_attributes_fan,
2306 .is_visible = it87_fan_is_visible,
2309 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2310 struct attribute *attr, int index)
2312 struct device *dev = container_of(kobj, struct device, kobj);
2313 struct it87_data *data = dev_get_drvdata(dev);
2314 int i = index / 4; /* pwm index */
2315 int a = index % 4; /* attribute index */
2317 if (!(data->has_pwm & BIT(i)))
2320 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2321 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2322 return attr->mode | S_IWUSR;
2324 /* pwm2_freq is writable if there are two pwm frequency selects */
2325 if (has_pwm_freq2(data) && i == 1 && a == 2)
2326 return attr->mode | S_IWUSR;
2331 static struct attribute *it87_attributes_pwm[] = {
2332 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2333 &sensor_dev_attr_pwm1.dev_attr.attr,
2334 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2335 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2337 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2338 &sensor_dev_attr_pwm2.dev_attr.attr,
2339 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2340 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2342 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2343 &sensor_dev_attr_pwm3.dev_attr.attr,
2344 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2345 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2347 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2348 &sensor_dev_attr_pwm4.dev_attr.attr,
2349 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2350 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2352 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2353 &sensor_dev_attr_pwm5.dev_attr.attr,
2354 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2355 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2357 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2358 &sensor_dev_attr_pwm6.dev_attr.attr,
2359 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2360 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2365 static const struct attribute_group it87_group_pwm = {
2366 .attrs = it87_attributes_pwm,
2367 .is_visible = it87_pwm_is_visible,
2370 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2371 struct attribute *attr, int index)
2373 struct device *dev = container_of(kobj, struct device, kobj);
2374 struct it87_data *data = dev_get_drvdata(dev);
2375 int i = index / 11; /* pwm index */
2376 int a = index % 11; /* attribute index */
2378 if (index >= 33) { /* pwm 4..6 */
2379 i = (index - 33) / 6 + 3;
2380 a = (index - 33) % 6 + 4;
2383 if (!(data->has_pwm & BIT(i)))
2386 if (has_newer_autopwm(data)) {
2387 if (a < 4) /* no auto point pwm */
2389 if (a == 8) /* no auto_point4 */
2392 if (has_old_autopwm(data)) {
2393 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2400 static struct attribute *it87_attributes_auto_pwm[] = {
2401 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2402 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2403 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2404 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2405 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2406 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2407 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2408 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2409 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2410 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2411 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2413 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2414 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2415 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2416 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2417 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2418 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2419 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2420 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2421 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2422 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2423 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2425 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2426 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2427 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2428 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2429 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2430 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2431 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2432 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2433 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2434 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2435 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2437 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2438 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2439 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2440 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2441 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2442 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2444 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2445 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2446 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2447 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2448 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2449 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2451 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2452 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2453 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2454 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2455 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2456 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2461 static const struct attribute_group it87_group_auto_pwm = {
2462 .attrs = it87_attributes_auto_pwm,
2463 .is_visible = it87_auto_pwm_is_visible,
2466 /* SuperIO detection - will change isa_address if a chip is found */
2467 static int __init it87_find(int sioaddr, unsigned short *address,
2468 struct it87_sio_data *sio_data)
2472 const char *board_vendor, *board_name;
2473 const struct it87_devices *config;
2475 err = superio_enter(sioaddr);
2480 chip_type = superio_inw(sioaddr, DEVID);
2481 if (chip_type == 0xffff)
2485 chip_type = force_id;
2487 switch (chip_type) {
2489 sio_data->type = it87;
2492 sio_data->type = it8712;
2496 sio_data->type = it8716;
2499 sio_data->type = it8718;
2502 sio_data->type = it8720;
2505 sio_data->type = it8721;
2508 sio_data->type = it8728;
2511 sio_data->type = it8732;
2514 sio_data->type = it8792;
2517 sio_data->type = it8771;
2520 sio_data->type = it8772;
2523 sio_data->type = it8781;
2526 sio_data->type = it8782;
2529 sio_data->type = it8783;
2532 sio_data->type = it8786;
2535 sio_data->type = it8790;
2539 sio_data->type = it8603;
2542 sio_data->type = it8607;
2545 sio_data->type = it8620;
2548 sio_data->type = it8622;
2551 sio_data->type = it8628;
2554 sio_data->type = it8665;
2557 sio_data->type = it8686;
2559 case 0xffff: /* No device at all */
2562 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
2566 superio_select(sioaddr, PME);
2567 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
2568 pr_info("Device not activated, skipping\n");
2572 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
2573 if (*address == 0) {
2574 pr_info("Base address not set, skipping\n");
2579 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
2580 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2581 it87_devices[sio_data->type].suffix,
2582 *address, sio_data->revision);
2584 config = &it87_devices[sio_data->type];
2586 /* in7 (VSB or VCCH5V) is always internal on some chips */
2587 if (has_in7_internal(config))
2588 sio_data->internal |= BIT(1);
2590 /* in8 (Vbat) is always internal */
2591 sio_data->internal |= BIT(2);
2593 /* in9 (AVCC3), always internal if supported */
2594 if (has_avcc3(config))
2595 sio_data->internal |= BIT(3); /* in9 is AVCC */
2597 sio_data->skip_in |= BIT(9);
2599 if (!has_four_pwm(config))
2600 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
2601 else if (!has_five_pwm(config))
2602 sio_data->skip_pwm |= BIT(4) | BIT(5);
2603 else if (!has_six_pwm(config))
2604 sio_data->skip_pwm |= BIT(5);
2606 if (!has_vid(config))
2607 sio_data->skip_vid = 1;
2609 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2610 if (sio_data->type == it87) {
2611 /* The IT8705F has a different LD number for GPIO */
2612 superio_select(sioaddr, 5);
2613 sio_data->beep_pin = superio_inb(sioaddr,
2614 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2615 } else if (sio_data->type == it8783) {
2616 int reg25, reg27, reg2a, reg2c, regef;
2618 superio_select(sioaddr, GPIO);
2620 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2621 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2622 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2623 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2624 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
2626 /* Check if fan3 is there or not */
2627 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2628 sio_data->skip_fan |= BIT(2);
2629 if ((reg25 & BIT(4)) ||
2630 (!(reg2a & BIT(1)) && (regef & BIT(0))))
2631 sio_data->skip_pwm |= BIT(2);
2633 /* Check if fan2 is there or not */
2635 sio_data->skip_fan |= BIT(1);
2637 sio_data->skip_pwm |= BIT(1);
2640 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2641 sio_data->skip_in |= BIT(5); /* No VIN5 */
2645 sio_data->skip_in |= BIT(6); /* No VIN6 */
2649 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2651 if (reg27 & BIT(2)) {
2653 * The data sheet is a bit unclear regarding the
2654 * internal voltage divider for VCCH5V. It says
2655 * "This bit enables and switches VIN7 (pin 91) to the
2656 * internal voltage divider for VCCH5V".
2657 * This is different to other chips, where the internal
2658 * voltage divider would connect VIN7 to an internal
2659 * voltage source. Maybe that is the case here as well.
2661 * Since we don't know for sure, re-route it if that is
2662 * not the case, and ask the user to report if the
2663 * resulting voltage is sane.
2665 if (!(reg2c & BIT(1))) {
2667 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2669 pr_notice("Routing internal VCCH5V to in7.\n");
2671 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2672 pr_notice("Please report if it displays a reasonable voltage.\n");
2676 sio_data->internal |= BIT(0);
2678 sio_data->internal |= BIT(1);
2680 sio_data->beep_pin = superio_inb(sioaddr,
2681 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2682 } else if (sio_data->type == it8603 || sio_data->type == it8607) {
2685 superio_select(sioaddr, GPIO);
2687 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2689 /* Check if fan3 is there or not */
2691 sio_data->skip_pwm |= BIT(2);
2693 sio_data->skip_fan |= BIT(2);
2695 /* Check if fan2 is there or not */
2696 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2698 sio_data->skip_pwm |= BIT(1);
2700 sio_data->skip_fan |= BIT(1);
2702 if (sio_data->type == it8603) {
2703 sio_data->skip_in |= BIT(5); /* No VIN5 */
2704 sio_data->skip_in |= BIT(6); /* No VIN6 */
2707 sio_data->beep_pin = superio_inb(sioaddr,
2708 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2709 } else if (sio_data->type == it8620 || sio_data->type == it8628 ||
2710 sio_data->type == it8686) {
2713 superio_select(sioaddr, GPIO);
2715 /* Check for pwm5 */
2716 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2718 sio_data->skip_pwm |= BIT(4);
2720 /* Check for fan4, fan5 */
2721 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2722 if (!(reg & BIT(5)))
2723 sio_data->skip_fan |= BIT(3);
2724 if (!(reg & BIT(4)))
2725 sio_data->skip_fan |= BIT(4);
2727 /* Check for pwm3, fan3 */
2728 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2730 sio_data->skip_pwm |= BIT(2);
2732 sio_data->skip_fan |= BIT(2);
2734 /* Check for pwm4 */
2735 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
2737 sio_data->skip_pwm |= BIT(3);
2739 /* Check for pwm2, fan2 */
2740 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2742 sio_data->skip_pwm |= BIT(1);
2744 sio_data->skip_fan |= BIT(1);
2745 /* Check for pwm6, fan6 */
2746 if (!(reg & BIT(7))) {
2747 sio_data->skip_pwm |= BIT(5);
2748 sio_data->skip_fan |= BIT(5);
2751 /* Check if AVCC is on VIN3 */
2752 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2754 sio_data->internal |= BIT(0);
2756 sio_data->skip_in |= BIT(9);
2758 sio_data->beep_pin = superio_inb(sioaddr,
2759 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2760 } else if (sio_data->type == it8622) {
2763 superio_select(sioaddr, GPIO);
2765 /* Check for pwm4, fan4 */
2766 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2768 sio_data->skip_fan |= BIT(3);
2770 sio_data->skip_pwm |= BIT(3);
2772 /* Check for pwm3, fan3, pwm5, fan5 */
2773 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2775 sio_data->skip_pwm |= BIT(2);
2777 sio_data->skip_fan |= BIT(2);
2779 sio_data->skip_pwm |= BIT(4);
2781 sio_data->skip_fan |= BIT(4);
2783 /* Check for pwm2, fan2 */
2784 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2786 sio_data->skip_pwm |= BIT(1);
2788 sio_data->skip_fan |= BIT(1);
2790 /* Check for AVCC */
2791 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2792 if (!(reg & BIT(0)))
2793 sio_data->skip_in |= BIT(9);
2795 sio_data->beep_pin = superio_inb(sioaddr,
2796 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2797 } else if (sio_data->type == it8732) {
2800 superio_select(sioaddr, GPIO);
2802 /* Check for pwm2, fan2 */
2803 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2805 sio_data->skip_pwm |= BIT(1);
2807 sio_data->skip_fan |= BIT(1);
2809 /* Check for pwm3, fan3, fan4 */
2810 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2812 sio_data->skip_pwm |= BIT(2);
2814 sio_data->skip_fan |= BIT(2);
2816 sio_data->skip_fan |= BIT(3);
2818 /* Check if AVCC is on VIN3 */
2819 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2821 sio_data->internal |= BIT(0);
2823 sio_data->beep_pin = superio_inb(sioaddr,
2824 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2825 } else if (sio_data->type == it8665) {
2828 superio_select(sioaddr, GPIO);
2830 /* Check for pwm2 */
2831 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2833 sio_data->skip_pwm |= BIT(1);
2835 /* Check for fan2 */
2836 reg = superio_inb(sioaddr, IT87_SIO_PINX4_REG);
2838 sio_data->skip_fan |= BIT(1);
2840 /* Check for pwm3, fan3 */
2841 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2843 sio_data->skip_pwm |= BIT(2);
2845 sio_data->skip_fan |= BIT(2);
2847 /* Check for pwm5, fan5 */
2848 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2850 sio_data->skip_pwm |= BIT(4);
2851 if (!(reg & BIT(4)))
2852 sio_data->skip_fan |= BIT(4);
2854 /* Check for pwm4, fan4, pwm6, fan6 */
2855 reg = superio_inb(sioaddr, IT87_SIO_GPIO9_REG);
2857 sio_data->skip_pwm |= BIT(3);
2859 sio_data->skip_fan |= BIT(3);
2861 sio_data->skip_pwm |= BIT(5);
2863 sio_data->skip_fan |= BIT(5);
2865 sio_data->beep_pin = superio_inb(sioaddr,
2866 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2871 superio_select(sioaddr, GPIO);
2873 /* Check for fan4, fan5 */
2874 if (has_five_fans(config)) {
2875 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2876 switch (sio_data->type) {
2879 sio_data->skip_fan |= BIT(3);
2881 sio_data->skip_fan |= BIT(4);
2886 if (!(reg & BIT(5)))
2887 sio_data->skip_fan |= BIT(3);
2888 if (!(reg & BIT(4)))
2889 sio_data->skip_fan |= BIT(4);
2896 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2897 if (!sio_data->skip_vid) {
2898 /* We need at least 4 VID pins */
2900 pr_info("VID is disabled (pins used for GPIO)\n");
2901 sio_data->skip_vid = 1;
2905 /* Check if fan3 is there or not */
2907 sio_data->skip_pwm |= BIT(2);
2909 sio_data->skip_fan |= BIT(2);
2911 /* Check if fan2 is there or not */
2912 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2914 sio_data->skip_pwm |= BIT(1);
2916 sio_data->skip_fan |= BIT(1);
2918 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2919 !(sio_data->skip_vid))
2920 sio_data->vid_value = superio_inb(sioaddr,
2923 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2925 uart6 = sio_data->type == it8782 && (reg & BIT(2));
2928 * The IT8720F has no VIN7 pin, so VCCH should always be
2929 * routed internally to VIN7 with an internal divider.
2930 * Curiously, there still is a configuration bit to control
2931 * this, which means it can be set incorrectly. And even
2932 * more curiously, many boards out there are improperly
2933 * configured, even though the IT8720F datasheet claims
2934 * that the internal routing of VCCH to VIN7 is the default
2935 * setting. So we force the internal routing in this case.
2937 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2938 * If UART6 is enabled, re-route VIN7 to the internal divider
2939 * if that is not already the case.
2941 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2943 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
2944 pr_notice("Routing internal VCCH to in7\n");
2947 sio_data->internal |= BIT(0);
2949 sio_data->internal |= BIT(1);
2952 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2953 * While VIN7 can be routed to the internal voltage divider,
2954 * VIN5 and VIN6 are not available if UART6 is enabled.
2956 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2957 * is the temperature source. Since we can not read the
2958 * temperature source here, skip_temp is preliminary.
2961 sio_data->skip_in |= BIT(5) | BIT(6);
2962 sio_data->skip_temp |= BIT(2);
2965 sio_data->beep_pin = superio_inb(sioaddr,
2966 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2968 if (sio_data->beep_pin)
2969 pr_info("Beeping is supported\n");
2971 /* Disable specific features based on DMI strings */
2972 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2973 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2974 if (board_vendor && board_name) {
2975 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2976 strcmp(board_name, "FN68PT") == 0) {
2978 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2979 * connected to a fan, but to something else. One user
2980 * has reported instant system power-off when changing
2981 * the PWM2 duty cycle, so we disable it.
2982 * I use the board name string as the trigger in case
2983 * the same board is ever used in other systems.
2985 pr_info("Disabling pwm2 due to hardware constraints\n");
2986 sio_data->skip_pwm = BIT(1);
2991 superio_exit(sioaddr);
2995 /* Called when we have found a new IT87. */
2996 static void it87_init_device(struct platform_device *pdev)
2998 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2999 struct it87_data *data = platform_get_drvdata(pdev);
3004 * For each PWM channel:
3005 * - If it is in automatic mode, setting to manual mode should set
3006 * the fan to full speed by default.
3007 * - If it is in manual mode, we need a mapping to temperature
3008 * channels to use when later setting to automatic mode later.
3009 * Use a 1:1 mapping by default (we are clueless.)
3010 * In both cases, the value can (and should) be changed by the user
3011 * prior to switching to a different mode.
3012 * Note that this is no longer needed for the IT8721F and later, as
3013 * these have separate registers for the temperature mapping and the
3014 * manual duty cycle.
3016 for (i = 0; i < NUM_AUTO_PWM; i++) {
3017 data->pwm_temp_map[i] = i;
3018 data->pwm_duty[i] = 0x7f; /* Full speed */
3019 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
3023 * Some chips seem to have default value 0xff for all limit
3024 * registers. For low voltage limits it makes no sense and triggers
3025 * alarms, so change to 0 instead. For high temperature limits, it
3026 * means -1 degree C, which surprisingly doesn't trigger an alarm,
3027 * but is still confusing, so change to 127 degrees C.
3029 for (i = 0; i < NUM_VIN_LIMIT; i++) {
3030 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
3032 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
3034 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
3035 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
3037 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
3041 * Temperature channels are not forcibly enabled, as they can be
3042 * set to two different sensor types and we can't guess which one
3043 * is correct for a given system. These channels can be enabled at
3044 * run-time through the temp{1-3}_type sysfs accessors if needed.
3047 /* Check if voltage monitors are reset manually or by some reason */
3048 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
3049 if ((tmp & 0xff) == 0) {
3050 /* Enable all voltage monitors */
3051 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
3054 /* Check if tachometers are reset manually or by some reason */
3055 mask = 0x70 & ~(sio_data->skip_fan << 4);
3056 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
3057 if ((data->fan_main_ctrl & mask) == 0) {
3058 /* Enable all fan tachometers */
3059 data->fan_main_ctrl |= mask;
3060 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
3061 data->fan_main_ctrl);
3063 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
3065 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
3067 /* Set tachometers to 16-bit mode if needed */
3068 if (has_fan16_config(data)) {
3069 if (~tmp & 0x07 & data->has_fan) {
3071 "Setting fan1-3 to 16-bit mode\n");
3072 it87_write_value(data, IT87_REG_FAN_16BIT,
3077 /* Check for additional fans */
3078 if (has_four_fans(data) && (tmp & BIT(4)))
3079 data->has_fan |= BIT(3); /* fan4 enabled */
3080 if (has_five_fans(data) && (tmp & BIT(5)))
3081 data->has_fan |= BIT(4); /* fan5 enabled */
3082 if (has_six_fans(data)) {
3083 switch (data->type) {
3088 data->has_fan |= BIT(5); /* fan6 enabled */
3091 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
3093 data->has_fan |= BIT(5); /* fan6 enabled */
3100 /* Fan input pins may be used for alternative functions */
3101 data->has_fan &= ~sio_data->skip_fan;
3103 /* Check if pwm6 is enabled */
3104 if (has_six_pwm(data)) {
3105 switch (data->type) {
3108 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
3109 if (!(tmp & BIT(3)))
3110 sio_data->skip_pwm |= BIT(5);
3117 /* Start monitoring */
3118 it87_write_value(data, IT87_REG_CONFIG,
3119 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
3120 | (update_vbat ? 0x41 : 0x01));
3123 /* Return 1 if and only if the PWM interface is safe to use */
3124 static int it87_check_pwm(struct device *dev)
3126 struct it87_data *data = dev_get_drvdata(dev);
3128 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
3129 * and polarity set to active low is sign that this is the case so we
3130 * disable pwm control to protect the user.
3132 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
3134 if ((tmp & 0x87) == 0) {
3135 if (fix_pwm_polarity) {
3137 * The user asks us to attempt a chip reconfiguration.
3138 * This means switching to active high polarity and
3139 * inverting all fan speed values.
3144 for (i = 0; i < ARRAY_SIZE(pwm); i++)
3145 pwm[i] = it87_read_value(data,
3149 * If any fan is in automatic pwm mode, the polarity
3150 * might be correct, as suspicious as it seems, so we
3151 * better don't change anything (but still disable the
3154 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3156 "Reconfiguring PWM to active high polarity\n");
3157 it87_write_value(data, IT87_REG_FAN_CTL,
3159 for (i = 0; i < 3; i++)
3160 it87_write_value(data,
3167 "PWM configuration is too broken to be fixed\n");
3171 "Detected broken BIOS defaults, disabling PWM interface\n");
3173 } else if (fix_pwm_polarity) {
3175 "PWM configuration looks sane, won't touch\n");
3181 static int it87_probe(struct platform_device *pdev)
3183 struct it87_data *data;
3184 struct resource *res;
3185 struct device *dev = &pdev->dev;
3186 struct it87_sio_data *sio_data = dev_get_platdata(dev);
3187 int enable_pwm_interface;
3188 struct device *hwmon_dev;
3190 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3191 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
3193 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
3194 (unsigned long)res->start,
3195 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
3199 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
3203 data->addr = res->start;
3204 data->type = sio_data->type;
3205 data->features = it87_devices[sio_data->type].features;
3206 data->peci_mask = it87_devices[sio_data->type].peci_mask;
3207 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3211 * IT8705F Datasheet 0.4.1, 3h == Version G.
3212 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3213 * These are the first revisions with 16-bit tachometer support.
3215 switch (data->type) {
3217 if (sio_data->revision >= 0x03) {
3218 data->features &= ~FEAT_OLD_AUTOPWM;
3219 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3223 if (sio_data->revision >= 0x08) {
3224 data->features &= ~FEAT_OLD_AUTOPWM;
3225 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3233 /* Now, we do the remaining detection. */
3234 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
3235 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
3238 platform_set_drvdata(pdev, data);
3240 mutex_init(&data->update_lock);
3242 /* Check PWM configuration */
3243 enable_pwm_interface = it87_check_pwm(dev);
3245 /* Starting with IT8721F, we handle scaling of internal voltages */
3246 if (has_scaling(data)) {
3247 if (sio_data->internal & BIT(0))
3248 data->in_scaled |= BIT(3); /* in3 is AVCC */
3249 if (sio_data->internal & BIT(1))
3250 data->in_scaled |= BIT(7); /* in7 is VSB */
3251 if (sio_data->internal & BIT(2))
3252 data->in_scaled |= BIT(8); /* in8 is Vbat */
3253 if (sio_data->internal & BIT(3))
3254 data->in_scaled |= BIT(9); /* in9 is AVCC */
3255 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3256 sio_data->type == it8783) {
3257 if (sio_data->internal & BIT(0))
3258 data->in_scaled |= BIT(3); /* in3 is VCC5V */
3259 if (sio_data->internal & BIT(1))
3260 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
3263 data->has_temp = 0x07;
3264 if (sio_data->skip_temp & BIT(2)) {
3265 if (sio_data->type == it8782 &&
3266 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
3267 data->has_temp &= ~BIT(2);
3270 data->in_internal = sio_data->internal;
3271 data->has_in = 0x3ff & ~sio_data->skip_in;
3273 if (has_six_temp(data)) {
3274 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
3276 /* Check for additional temperature sensors */
3277 if ((reg & 0x03) >= 0x02)
3278 data->has_temp |= BIT(3);
3279 if (((reg >> 2) & 0x03) >= 0x02)
3280 data->has_temp |= BIT(4);
3281 if (((reg >> 4) & 0x03) >= 0x02)
3282 data->has_temp |= BIT(5);
3284 /* Check for additional voltage sensors */
3285 if ((reg & 0x03) == 0x01)
3286 data->has_in |= BIT(10);
3287 if (((reg >> 2) & 0x03) == 0x01)
3288 data->has_in |= BIT(11);
3289 if (((reg >> 4) & 0x03) == 0x01)
3290 data->has_in |= BIT(12);
3293 data->has_beep = !!sio_data->beep_pin;
3295 /* Initialize the IT87 chip */
3296 it87_init_device(pdev);
3298 if (!sio_data->skip_vid) {
3299 data->has_vid = true;
3300 data->vrm = vid_which_vrm();
3301 /* VID reading from Super-I/O config space if available */
3302 data->vid = sio_data->vid_value;
3305 /* Prepare for sysfs hooks */
3306 data->groups[0] = &it87_group;
3307 data->groups[1] = &it87_group_in;
3308 data->groups[2] = &it87_group_temp;
3309 data->groups[3] = &it87_group_fan;
3311 if (enable_pwm_interface) {
3312 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
3313 data->has_pwm &= ~sio_data->skip_pwm;
3315 data->groups[4] = &it87_group_pwm;
3316 if (has_old_autopwm(data) || has_newer_autopwm(data))
3317 data->groups[5] = &it87_group_auto_pwm;
3320 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3321 it87_devices[sio_data->type].name,
3322 data, data->groups);
3323 return PTR_ERR_OR_ZERO(hwmon_dev);
3326 static struct platform_driver it87_driver = {
3330 .probe = it87_probe,
3333 static int __init it87_device_add(int index, unsigned short address,
3334 const struct it87_sio_data *sio_data)
3336 struct platform_device *pdev;
3337 struct resource res = {
3338 .start = address + IT87_EC_OFFSET,
3339 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
3341 .flags = IORESOURCE_IO,
3345 err = acpi_check_resource_conflict(&res);
3349 pdev = platform_device_alloc(DRVNAME, address);
3353 err = platform_device_add_resources(pdev, &res, 1);
3355 pr_err("Device resource addition failed (%d)\n", err);
3356 goto exit_device_put;
3359 err = platform_device_add_data(pdev, sio_data,
3360 sizeof(struct it87_sio_data));
3362 pr_err("Platform data allocation failed\n");
3363 goto exit_device_put;
3366 err = platform_device_add(pdev);
3368 pr_err("Device addition failed (%d)\n", err);
3369 goto exit_device_put;
3372 it87_pdev[index] = pdev;
3376 platform_device_put(pdev);
3380 static int __init sm_it87_init(void)
3382 int sioaddr[2] = { REG_2E, REG_4E };
3383 struct it87_sio_data sio_data;
3384 unsigned short isa_address;
3388 err = platform_driver_register(&it87_driver);
3392 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3393 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3395 err = it87_find(sioaddr[i], &isa_address, &sio_data);
3396 if (err || isa_address == 0)
3399 err = it87_device_add(i, isa_address, &sio_data);
3401 goto exit_dev_unregister;
3407 goto exit_unregister;
3411 exit_dev_unregister:
3412 /* NULL check handled by platform_device_unregister */
3413 platform_device_unregister(it87_pdev[0]);
3415 platform_driver_unregister(&it87_driver);
3419 static void __exit sm_it87_exit(void)
3421 /* NULL check handled by platform_device_unregister */
3422 platform_device_unregister(it87_pdev[1]);
3423 platform_device_unregister(it87_pdev[0]);
3424 platform_driver_unregister(&it87_driver);
3427 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
3428 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
3429 module_param(update_vbat, bool, 0);
3430 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3431 module_param(fix_pwm_polarity, bool, 0);
3432 MODULE_PARM_DESC(fix_pwm_polarity,
3433 "Force PWM polarity to active high (DANGEROUS)");
3434 MODULE_LICENSE("GPL");
3436 module_init(sm_it87_init);
3437 module_exit(sm_it87_exit);