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 * IT8620E Super I/O chip w/LPC interface
15 * IT8622E Super I/O chip w/LPC interface
16 * IT8623E Super I/O chip w/LPC interface
17 * IT8628E Super I/O chip w/LPC interface
18 * IT8705F Super I/O chip w/LPC interface
19 * IT8712F Super I/O chip w/LPC interface
20 * IT8716F Super I/O chip w/LPC interface
21 * IT8718F Super I/O chip w/LPC interface
22 * IT8720F Super I/O chip w/LPC interface
23 * IT8721F Super I/O chip w/LPC interface
24 * IT8726F Super I/O chip w/LPC interface
25 * IT8728F Super I/O chip w/LPC interface
26 * IT8732F Super I/O chip w/LPC interface
27 * IT8758E Super I/O chip w/LPC interface
28 * IT8771E Super I/O chip w/LPC interface
29 * IT8772E Super I/O chip w/LPC interface
30 * IT8781F Super I/O chip w/LPC interface
31 * IT8782F Super I/O chip w/LPC interface
32 * IT8783E/F Super I/O chip w/LPC interface
33 * IT8786E Super I/O chip w/LPC interface
34 * IT8790E Super I/O chip w/LPC interface
35 * IT8792E Super I/O chip w/LPC interface
36 * Sis950 A clone of the IT8705F
38 * Copyright (C) 2001 Chris Gauthron
39 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
41 * This program is free software; you can redistribute it and/or modify
42 * it under the terms of the GNU General Public License as published by
43 * the Free Software Foundation; either version 2 of the License, or
44 * (at your option) any later version.
46 * This program is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 * GNU General Public License for more details.
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <linux/bitops.h>
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/slab.h>
58 #include <linux/jiffies.h>
59 #include <linux/platform_device.h>
60 #include <linux/hwmon.h>
61 #include <linux/hwmon-sysfs.h>
62 #include <linux/hwmon-vid.h>
63 #include <linux/err.h>
64 #include <linux/mutex.h>
65 #include <linux/sysfs.h>
66 #include <linux/string.h>
67 #include <linux/dmi.h>
68 #include <linux/acpi.h>
71 #define DRVNAME "it87"
73 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
74 it8771, it8772, it8781, it8782, it8783, it8786, it8790,
75 it8792, it8603, it8620, it8622, it8628 };
77 static unsigned short force_id;
78 module_param(force_id, ushort, 0);
79 MODULE_PARM_DESC(force_id, "Override the detected device ID");
81 static struct platform_device *it87_pdev[2];
83 #define REG_2E 0x2e /* The register to read/write */
84 #define REG_4E 0x4e /* Secondary register to read/write */
86 #define DEV 0x07 /* Register: Logical device select */
87 #define PME 0x04 /* The device with the fan registers in it */
89 /* The device with the IT8718F/IT8720F VID value in it */
92 #define DEVID 0x20 /* Register: Device ID */
93 #define DEVREV 0x22 /* Register: Device Revision */
95 static inline int superio_inb(int ioreg, int reg)
98 return inb(ioreg + 1);
101 static inline void superio_outb(int ioreg, int reg, int val)
104 outb(val, ioreg + 1);
107 static int superio_inw(int ioreg, int reg)
112 val = inb(ioreg + 1) << 8;
114 val |= inb(ioreg + 1);
118 static inline void superio_select(int ioreg, int ldn)
121 outb(ldn, ioreg + 1);
124 static inline int superio_enter(int ioreg)
127 * Try to reserve ioreg and ioreg + 1 for exclusive access.
129 if (!request_muxed_region(ioreg, 2, DRVNAME))
135 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
139 static inline void superio_exit(int ioreg)
142 outb(0x02, ioreg + 1);
143 release_region(ioreg, 2);
146 /* Logical device 4 registers */
147 #define IT8712F_DEVID 0x8712
148 #define IT8705F_DEVID 0x8705
149 #define IT8716F_DEVID 0x8716
150 #define IT8718F_DEVID 0x8718
151 #define IT8720F_DEVID 0x8720
152 #define IT8721F_DEVID 0x8721
153 #define IT8726F_DEVID 0x8726
154 #define IT8728F_DEVID 0x8728
155 #define IT8732F_DEVID 0x8732
156 #define IT8792E_DEVID 0x8733
157 #define IT8771E_DEVID 0x8771
158 #define IT8772E_DEVID 0x8772
159 #define IT8781F_DEVID 0x8781
160 #define IT8782F_DEVID 0x8782
161 #define IT8783E_DEVID 0x8783
162 #define IT8786E_DEVID 0x8786
163 #define IT8790E_DEVID 0x8790
164 #define IT8603E_DEVID 0x8603
165 #define IT8620E_DEVID 0x8620
166 #define IT8622E_DEVID 0x8622
167 #define IT8623E_DEVID 0x8623
168 #define IT8628E_DEVID 0x8628
169 #define IT87_ACT_REG 0x30
170 #define IT87_BASE_REG 0x60
172 /* Logical device 7 registers (IT8712F and later) */
173 #define IT87_SIO_GPIO1_REG 0x25
174 #define IT87_SIO_GPIO2_REG 0x26
175 #define IT87_SIO_GPIO3_REG 0x27
176 #define IT87_SIO_GPIO4_REG 0x28
177 #define IT87_SIO_GPIO5_REG 0x29
178 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
179 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
180 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
181 #define IT87_SIO_VID_REG 0xfc /* VID value */
182 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
184 /* Update battery voltage after every reading if true */
185 static bool update_vbat;
187 /* Not all BIOSes properly configure the PWM registers */
188 static bool fix_pwm_polarity;
190 /* Many IT87 constants specified below */
192 /* Length of ISA address segment */
193 #define IT87_EXTENT 8
195 /* Length of ISA address segment for Environmental Controller */
196 #define IT87_EC_EXTENT 2
198 /* Offset of EC registers from ISA base address */
199 #define IT87_EC_OFFSET 5
201 /* Where are the ISA address/data registers relative to the EC base address */
202 #define IT87_ADDR_REG_OFFSET 0
203 #define IT87_DATA_REG_OFFSET 1
205 /*----- The IT87 registers -----*/
207 #define IT87_REG_CONFIG 0x00
209 #define IT87_REG_ALARM1 0x01
210 #define IT87_REG_ALARM2 0x02
211 #define IT87_REG_ALARM3 0x03
214 * The IT8718F and IT8720F have the VID value in a different register, in
215 * Super-I/O configuration space.
217 #define IT87_REG_VID 0x0a
219 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
220 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
223 #define IT87_REG_FAN_DIV 0x0b
224 #define IT87_REG_FAN_16BIT 0x0c
228 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
229 * - up to 6 temp (1 to 6)
230 * - up to 6 fan (1 to 6)
233 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
234 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
235 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
236 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
237 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
239 #define IT87_REG_FAN_MAIN_CTRL 0x13
240 #define IT87_REG_FAN_CTL 0x14
241 static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
242 static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
244 static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
245 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
247 #define IT87_REG_TEMP(nr) (0x29 + (nr))
249 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
250 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
251 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
252 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
254 #define IT87_REG_VIN_ENABLE 0x50
255 #define IT87_REG_TEMP_ENABLE 0x51
256 #define IT87_REG_TEMP_EXTRA 0x55
257 #define IT87_REG_BEEP_ENABLE 0x5c
259 #define IT87_REG_CHIPID 0x58
261 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
263 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
264 #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
266 #define IT87_REG_TEMP456_ENABLE 0x77
268 #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
269 #define NUM_VIN_LIMIT 8
271 #define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
272 #define NUM_TEMP_LIMIT 3
273 #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
274 #define NUM_FAN_DIV 3
275 #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
276 #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
278 struct it87_devices {
280 const char * const suffix;
286 #define FEAT_12MV_ADC BIT(0)
287 #define FEAT_NEWER_AUTOPWM BIT(1)
288 #define FEAT_OLD_AUTOPWM BIT(2)
289 #define FEAT_16BIT_FANS BIT(3)
290 #define FEAT_TEMP_OFFSET BIT(4)
291 #define FEAT_TEMP_PECI BIT(5)
292 #define FEAT_TEMP_OLD_PECI BIT(6)
293 #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
294 #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
295 #define FEAT_VID BIT(9) /* Set if chip supports VID */
296 #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
297 #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
298 #define FEAT_10_9MV_ADC BIT(12)
299 #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
300 #define FEAT_FIVE_PWM BIT(14) /* Chip supports 5 pwm chn */
301 #define FEAT_SIX_PWM BIT(15) /* Chip supports 6 pwm chn */
302 #define FEAT_PWM_FREQ2 BIT(16) /* Separate pwm freq 2 */
303 #define FEAT_SIX_TEMP BIT(17) /* Up to 6 temp sensors */
304 #define FEAT_VIN3_5V BIT(18) /* VIN3 connected to +5V */
306 static const struct it87_devices it87_devices[] = {
310 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
315 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
316 /* may need to overwrite */
321 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
322 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
327 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
328 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
330 .old_peci_mask = 0x4,
335 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
336 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
338 .old_peci_mask = 0x4,
343 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
344 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
345 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
348 .old_peci_mask = 0x02, /* Actually reports PCH */
353 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
354 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
355 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
361 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
362 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
363 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
365 .old_peci_mask = 0x02, /* Actually reports PCH */
370 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
371 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
373 /* PECI: guesswork */
375 /* 16 bit fans (OHM) */
376 /* three fans, always 16 bit (guesswork) */
382 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
383 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
385 /* PECI (coreboot) */
386 /* 12mV ADC (HWSensors4, OHM) */
387 /* 16 bit fans (HWSensors4, OHM) */
388 /* three fans, always 16 bit (datasheet) */
394 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
395 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
396 .old_peci_mask = 0x4,
401 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
402 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
403 .old_peci_mask = 0x4,
408 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
409 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
410 .old_peci_mask = 0x4,
415 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
416 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
423 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
424 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
431 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
432 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
433 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
435 .old_peci_mask = 0x02, /* Actually reports PCH */
440 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
441 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
442 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
448 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
449 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
450 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
451 | FEAT_SIX_TEMP | FEAT_VIN3_5V,
457 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
458 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
459 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
460 | FEAT_AVCC3 | FEAT_VIN3_5V,
466 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
467 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
468 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
469 | FEAT_SIX_TEMP | FEAT_VIN3_5V,
474 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
475 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
476 #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
477 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
478 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
479 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
480 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
481 ((data)->peci_mask & BIT(nr)))
482 #define has_temp_old_peci(data, nr) \
483 (((data)->features & FEAT_TEMP_OLD_PECI) && \
484 ((data)->old_peci_mask & BIT(nr)))
485 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
486 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
488 #define has_vid(data) ((data)->features & FEAT_VID)
489 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
490 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
491 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
492 #define has_five_pwm(data) ((data)->features & (FEAT_FIVE_PWM \
494 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
495 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
496 #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
497 #define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
498 #define has_scaling(data) ((data)->features & (FEAT_12MV_ADC | \
501 struct it87_sio_data {
504 /* Values read from Super-I/O config space */
508 u8 internal; /* Internal sensors can be labeled */
509 bool need_in7_reroute;
510 /* Features skipped based on config or DMI */
519 * For each registered chip, we need to keep some data in memory.
520 * The structure is dynamically allocated.
523 const struct attribute_group *groups[7];
532 struct mutex update_lock;
533 char valid; /* !=0 if following fields are valid */
534 unsigned long last_updated; /* In jiffies */
536 u16 in_scaled; /* Internal voltage sensors are scaled */
537 u16 in_internal; /* Bitfield, internal sensors (for labels) */
538 u16 has_in; /* Bitfield, voltage sensors enabled */
539 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
540 bool need_in7_reroute;
541 u8 has_fan; /* Bitfield, fans enabled */
542 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
543 u8 has_temp; /* Bitfield, temp sensors enabled */
544 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
545 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
546 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
547 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
548 bool has_vid; /* True if VID supported */
549 u8 vid; /* Register encoding, combined */
551 u32 alarms; /* Register encoding, combined */
552 bool has_beep; /* true if beep supported */
553 u8 beeps; /* Register encoding */
554 u8 fan_main_ctrl; /* Register value */
555 u8 fan_ctl; /* Register value */
558 * The following 3 arrays correspond to the same registers up to
559 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
560 * 7, and we want to preserve settings on mode changes, so we have
561 * to track all values separately.
562 * Starting with the IT8721F, the manual PWM duty cycles are stored
563 * in separate registers (8-bit values), so the separate tracking
564 * is no longer needed, but it is still done to keep the driver
567 u8 has_pwm; /* Bitfield, pwm control enabled */
568 u8 pwm_ctrl[NUM_PWM]; /* Register value */
569 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
570 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
572 /* Automatic fan speed control registers */
573 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
574 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
577 static int adc_lsb(const struct it87_data *data, int nr)
581 if (has_12mv_adc(data))
583 else if (has_10_9mv_adc(data))
587 if (data->in_scaled & BIT(nr))
592 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
594 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
595 return clamp_val(val, 0, 255);
598 static int in_from_reg(const struct it87_data *data, int nr, int val)
600 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
603 static inline u8 FAN_TO_REG(long rpm, int div)
607 rpm = clamp_val(rpm, 1, 1000000);
608 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
611 static inline u16 FAN16_TO_REG(long rpm)
615 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
618 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
619 1350000 / ((val) * (div)))
620 /* The divider is fixed to 2 in 16-bit mode */
621 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
622 1350000 / ((val) * 2))
624 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
625 ((val) + 500) / 1000), -128, 127))
626 #define TEMP_FROM_REG(val) ((val) * 1000)
628 static u8 pwm_to_reg(const struct it87_data *data, long val)
630 if (has_newer_autopwm(data))
636 static int pwm_from_reg(const struct it87_data *data, u8 reg)
638 if (has_newer_autopwm(data))
641 return (reg & 0x7f) << 1;
644 static int DIV_TO_REG(int val)
648 while (answer < 7 && (val >>= 1))
653 #define DIV_FROM_REG(val) BIT(val)
656 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
657 * depending on the chip type, to calculate the actual PWM frequency.
659 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
660 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
661 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
662 * sometimes just one. It is unknown if this is a datasheet error or real,
663 * so this is ignored for now.
665 static const unsigned int pwm_freq[8] = {
677 * Must be called with data->update_lock held, except during initialization.
678 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
679 * would slow down the IT87 access and should not be necessary.
681 static int it87_read_value(struct it87_data *data, u8 reg)
683 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
684 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
688 * Must be called with data->update_lock held, except during initialization.
689 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
690 * would slow down the IT87 access and should not be necessary.
692 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
694 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
695 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
698 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
700 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
701 if (has_newer_autopwm(data)) {
702 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
703 data->pwm_duty[nr] = it87_read_value(data,
704 IT87_REG_PWM_DUTY[nr]);
706 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
707 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
708 else /* Manual mode */
709 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
712 if (has_old_autopwm(data)) {
715 for (i = 0; i < 5 ; i++)
716 data->auto_temp[nr][i] = it87_read_value(data,
717 IT87_REG_AUTO_TEMP(nr, i));
718 for (i = 0; i < 3 ; i++)
719 data->auto_pwm[nr][i] = it87_read_value(data,
720 IT87_REG_AUTO_PWM(nr, i));
721 } else if (has_newer_autopwm(data)) {
725 * 0: temperature hysteresis (base + 5)
726 * 1: fan off temperature (base + 0)
727 * 2: fan start temperature (base + 1)
728 * 3: fan max temperature (base + 2)
730 data->auto_temp[nr][0] =
731 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
733 for (i = 0; i < 3 ; i++)
734 data->auto_temp[nr][i + 1] =
735 it87_read_value(data,
736 IT87_REG_AUTO_TEMP(nr, i));
738 * 0: start pwm value (base + 3)
739 * 1: pwm slope (base + 4, 1/8th pwm)
741 data->auto_pwm[nr][0] =
742 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
743 data->auto_pwm[nr][1] =
744 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
748 static struct it87_data *it87_update_device(struct device *dev)
750 struct it87_data *data = dev_get_drvdata(dev);
753 mutex_lock(&data->update_lock);
755 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
759 * Cleared after each update, so reenable. Value
760 * returned by this read will be previous value
762 it87_write_value(data, IT87_REG_CONFIG,
763 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
765 for (i = 0; i < NUM_VIN; i++) {
766 if (!(data->has_in & BIT(i)))
770 it87_read_value(data, IT87_REG_VIN[i]);
772 /* VBAT and AVCC don't have limit registers */
773 if (i >= NUM_VIN_LIMIT)
777 it87_read_value(data, IT87_REG_VIN_MIN(i));
779 it87_read_value(data, IT87_REG_VIN_MAX(i));
782 for (i = 0; i < NUM_FAN; i++) {
783 /* Skip disabled fans */
784 if (!(data->has_fan & BIT(i)))
788 it87_read_value(data, IT87_REG_FAN_MIN[i]);
789 data->fan[i][0] = it87_read_value(data,
791 /* Add high byte if in 16-bit mode */
792 if (has_16bit_fans(data)) {
793 data->fan[i][0] |= it87_read_value(data,
794 IT87_REG_FANX[i]) << 8;
795 data->fan[i][1] |= it87_read_value(data,
796 IT87_REG_FANX_MIN[i]) << 8;
799 for (i = 0; i < NUM_TEMP; i++) {
800 if (!(data->has_temp & BIT(i)))
803 it87_read_value(data, IT87_REG_TEMP(i));
805 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
807 it87_read_value(data,
808 IT87_REG_TEMP_OFFSET[i]);
810 if (i >= NUM_TEMP_LIMIT)
814 it87_read_value(data, IT87_REG_TEMP_LOW(i));
816 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
819 /* Newer chips don't have clock dividers */
820 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
821 i = it87_read_value(data, IT87_REG_FAN_DIV);
822 data->fan_div[0] = i & 0x07;
823 data->fan_div[1] = (i >> 3) & 0x07;
824 data->fan_div[2] = (i & 0x40) ? 3 : 1;
828 it87_read_value(data, IT87_REG_ALARM1) |
829 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
830 (it87_read_value(data, IT87_REG_ALARM3) << 16);
831 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
833 data->fan_main_ctrl = it87_read_value(data,
834 IT87_REG_FAN_MAIN_CTRL);
835 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
836 for (i = 0; i < NUM_PWM; i++) {
837 if (!(data->has_pwm & BIT(i)))
839 it87_update_pwm_ctrl(data, i);
842 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
843 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
845 * The IT8705F does not have VID capability.
846 * The IT8718F and later don't use IT87_REG_VID for the
849 if (data->type == it8712 || data->type == it8716) {
850 data->vid = it87_read_value(data, IT87_REG_VID);
852 * The older IT8712F revisions had only 5 VID pins,
853 * but we assume it is always safe to read 6 bits.
857 data->last_updated = jiffies;
861 mutex_unlock(&data->update_lock);
866 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
869 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
870 struct it87_data *data = it87_update_device(dev);
871 int index = sattr->index;
874 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
877 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
878 const char *buf, size_t count)
880 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
881 struct it87_data *data = dev_get_drvdata(dev);
882 int index = sattr->index;
886 if (kstrtoul(buf, 10, &val) < 0)
889 mutex_lock(&data->update_lock);
890 data->in[nr][index] = in_to_reg(data, nr, val);
891 it87_write_value(data,
892 index == 1 ? IT87_REG_VIN_MIN(nr)
893 : IT87_REG_VIN_MAX(nr),
894 data->in[nr][index]);
895 mutex_unlock(&data->update_lock);
899 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
900 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
902 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
905 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
906 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
908 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
911 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
912 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
914 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
917 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
918 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
920 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
923 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
924 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
926 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
929 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
930 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
932 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
935 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
936 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
938 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
941 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
942 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
944 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
947 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
948 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
949 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
950 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
951 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
953 /* Up to 6 temperatures */
954 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
957 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
959 int index = sattr->index;
960 struct it87_data *data = it87_update_device(dev);
962 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
965 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
966 const char *buf, size_t count)
968 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
970 int index = sattr->index;
971 struct it87_data *data = dev_get_drvdata(dev);
975 if (kstrtol(buf, 10, &val) < 0)
978 mutex_lock(&data->update_lock);
983 reg = IT87_REG_TEMP_LOW(nr);
986 reg = IT87_REG_TEMP_HIGH(nr);
989 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
990 if (!(regval & 0x80)) {
992 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
995 reg = IT87_REG_TEMP_OFFSET[nr];
999 data->temp[nr][index] = TEMP_TO_REG(val);
1000 it87_write_value(data, reg, data->temp[nr][index]);
1001 mutex_unlock(&data->update_lock);
1005 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
1006 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1008 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1010 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
1012 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
1013 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1015 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1017 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1019 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
1020 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1022 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1024 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1026 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
1027 static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
1028 static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
1030 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1033 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1034 int nr = sensor_attr->index;
1035 struct it87_data *data = it87_update_device(dev);
1036 u8 reg = data->sensor; /* In case value is updated while used */
1037 u8 extra = data->extra;
1039 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
1040 (has_temp_old_peci(data, nr) && (extra & 0x80)))
1041 return sprintf(buf, "6\n"); /* Intel PECI */
1042 if (reg & (1 << nr))
1043 return sprintf(buf, "3\n"); /* thermal diode */
1044 if (reg & (8 << nr))
1045 return sprintf(buf, "4\n"); /* thermistor */
1046 return sprintf(buf, "0\n"); /* disabled */
1049 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1050 const char *buf, size_t count)
1052 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1053 int nr = sensor_attr->index;
1055 struct it87_data *data = dev_get_drvdata(dev);
1059 if (kstrtol(buf, 10, &val) < 0)
1062 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1065 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1067 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1068 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1070 if (val == 2) { /* backwards compatibility */
1072 "Sensor type 2 is deprecated, please use 4 instead\n");
1075 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1080 else if (has_temp_peci(data, nr) && val == 6)
1081 reg |= (nr + 1) << 6;
1082 else if (has_temp_old_peci(data, nr) && val == 6)
1087 mutex_lock(&data->update_lock);
1089 data->extra = extra;
1090 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
1091 if (has_temp_old_peci(data, nr))
1092 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1093 data->valid = 0; /* Force cache refresh */
1094 mutex_unlock(&data->update_lock);
1098 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1100 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1102 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1107 static int pwm_mode(const struct it87_data *data, int nr)
1109 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1110 return 0; /* Full speed */
1111 if (data->pwm_ctrl[nr] & 0x80)
1112 return 2; /* Automatic mode */
1113 if ((data->type == it8603 || nr >= 3) &&
1114 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1115 return 0; /* Full speed */
1117 return 1; /* Manual mode */
1120 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1123 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1125 int index = sattr->index;
1127 struct it87_data *data = it87_update_device(dev);
1129 speed = has_16bit_fans(data) ?
1130 FAN16_FROM_REG(data->fan[nr][index]) :
1131 FAN_FROM_REG(data->fan[nr][index],
1132 DIV_FROM_REG(data->fan_div[nr]));
1133 return sprintf(buf, "%d\n", speed);
1136 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1139 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1140 struct it87_data *data = it87_update_device(dev);
1141 int nr = sensor_attr->index;
1143 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1146 static ssize_t show_pwm_enable(struct device *dev,
1147 struct device_attribute *attr, char *buf)
1149 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1150 struct it87_data *data = it87_update_device(dev);
1151 int nr = sensor_attr->index;
1153 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1156 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1159 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1160 struct it87_data *data = it87_update_device(dev);
1161 int nr = sensor_attr->index;
1163 return sprintf(buf, "%d\n",
1164 pwm_from_reg(data, data->pwm_duty[nr]));
1167 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1170 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1171 struct it87_data *data = it87_update_device(dev);
1172 int nr = sensor_attr->index;
1176 if (has_pwm_freq2(data) && nr == 1)
1177 index = (data->extra >> 4) & 0x07;
1179 index = (data->fan_ctl >> 4) & 0x07;
1181 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1183 return sprintf(buf, "%u\n", freq);
1186 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1187 const char *buf, size_t count)
1189 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1191 int index = sattr->index;
1193 struct it87_data *data = dev_get_drvdata(dev);
1197 if (kstrtol(buf, 10, &val) < 0)
1200 mutex_lock(&data->update_lock);
1202 if (has_16bit_fans(data)) {
1203 data->fan[nr][index] = FAN16_TO_REG(val);
1204 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1205 data->fan[nr][index] & 0xff);
1206 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1207 data->fan[nr][index] >> 8);
1209 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1212 data->fan_div[nr] = reg & 0x07;
1215 data->fan_div[nr] = (reg >> 3) & 0x07;
1218 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1221 data->fan[nr][index] =
1222 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1223 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1224 data->fan[nr][index]);
1227 mutex_unlock(&data->update_lock);
1231 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1232 const char *buf, size_t count)
1234 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1235 struct it87_data *data = dev_get_drvdata(dev);
1236 int nr = sensor_attr->index;
1241 if (kstrtoul(buf, 10, &val) < 0)
1244 mutex_lock(&data->update_lock);
1245 old = it87_read_value(data, IT87_REG_FAN_DIV);
1247 /* Save fan min limit */
1248 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1253 data->fan_div[nr] = DIV_TO_REG(val);
1257 data->fan_div[nr] = 1;
1259 data->fan_div[nr] = 3;
1262 val |= (data->fan_div[0] & 0x07);
1263 val |= (data->fan_div[1] & 0x07) << 3;
1264 if (data->fan_div[2] == 3)
1266 it87_write_value(data, IT87_REG_FAN_DIV, val);
1268 /* Restore fan min limit */
1269 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1270 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
1272 mutex_unlock(&data->update_lock);
1276 /* Returns 0 if OK, -EINVAL otherwise */
1277 static int check_trip_points(struct device *dev, int nr)
1279 const struct it87_data *data = dev_get_drvdata(dev);
1282 if (has_old_autopwm(data)) {
1283 for (i = 0; i < 3; i++) {
1284 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1287 for (i = 0; i < 2; i++) {
1288 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1291 } else if (has_newer_autopwm(data)) {
1292 for (i = 1; i < 3; i++) {
1293 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1300 "Inconsistent trip points, not switching to automatic mode\n");
1301 dev_err(dev, "Adjust the trip points and try again\n");
1306 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1307 const char *buf, size_t count)
1309 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1310 struct it87_data *data = dev_get_drvdata(dev);
1311 int nr = sensor_attr->index;
1314 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1317 /* Check trip points before switching to automatic mode */
1319 if (check_trip_points(dev, nr) < 0)
1323 mutex_lock(&data->update_lock);
1326 if (nr < 3 && data->type != it8603) {
1328 /* make sure the fan is on when in on/off mode */
1329 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1330 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1331 /* set on/off mode */
1332 data->fan_main_ctrl &= ~BIT(nr);
1333 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1334 data->fan_main_ctrl);
1338 /* No on/off mode, set maximum pwm value */
1339 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1340 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1341 data->pwm_duty[nr]);
1342 /* and set manual mode */
1343 if (has_newer_autopwm(data)) {
1344 ctrl = (data->pwm_ctrl[nr] & 0x7c) |
1345 data->pwm_temp_map[nr];
1347 ctrl = data->pwm_duty[nr];
1349 data->pwm_ctrl[nr] = ctrl;
1350 it87_write_value(data, IT87_REG_PWM[nr], ctrl);
1355 if (has_newer_autopwm(data)) {
1356 ctrl = (data->pwm_ctrl[nr] & 0x7c) |
1357 data->pwm_temp_map[nr];
1361 ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1363 data->pwm_ctrl[nr] = ctrl;
1364 it87_write_value(data, IT87_REG_PWM[nr], ctrl);
1366 if (data->type != it8603 && nr < 3) {
1367 /* set SmartGuardian mode */
1368 data->fan_main_ctrl |= BIT(nr);
1369 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1370 data->fan_main_ctrl);
1374 mutex_unlock(&data->update_lock);
1378 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1379 const char *buf, size_t count)
1381 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1382 struct it87_data *data = dev_get_drvdata(dev);
1383 int nr = sensor_attr->index;
1386 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1389 mutex_lock(&data->update_lock);
1390 it87_update_pwm_ctrl(data, nr);
1391 if (has_newer_autopwm(data)) {
1393 * If we are in automatic mode, the PWM duty cycle register
1394 * is read-only so we can't write the value.
1396 if (data->pwm_ctrl[nr] & 0x80) {
1397 mutex_unlock(&data->update_lock);
1400 data->pwm_duty[nr] = pwm_to_reg(data, val);
1401 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1402 data->pwm_duty[nr]);
1404 data->pwm_duty[nr] = pwm_to_reg(data, val);
1406 * If we are in manual mode, write the duty cycle immediately;
1407 * otherwise, just store it for later use.
1409 if (!(data->pwm_ctrl[nr] & 0x80)) {
1410 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1411 it87_write_value(data, IT87_REG_PWM[nr],
1412 data->pwm_ctrl[nr]);
1415 mutex_unlock(&data->update_lock);
1419 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1420 const char *buf, size_t count)
1422 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1423 struct it87_data *data = dev_get_drvdata(dev);
1424 int nr = sensor_attr->index;
1428 if (kstrtoul(buf, 10, &val) < 0)
1431 val = clamp_val(val, 0, 1000000);
1432 val *= has_newer_autopwm(data) ? 256 : 128;
1434 /* Search for the nearest available frequency */
1435 for (i = 0; i < 7; i++) {
1436 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1440 mutex_lock(&data->update_lock);
1442 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1443 data->fan_ctl |= i << 4;
1444 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1446 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1447 data->extra |= i << 4;
1448 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1450 mutex_unlock(&data->update_lock);
1455 static ssize_t show_pwm_temp_map(struct device *dev,
1456 struct device_attribute *attr, char *buf)
1458 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1459 struct it87_data *data = it87_update_device(dev);
1460 int nr = sensor_attr->index;
1463 map = data->pwm_temp_map[nr];
1465 map = 0; /* Should never happen */
1466 if (nr >= 3) /* pwm channels 3..6 map to temp4..6 */
1469 return sprintf(buf, "%d\n", (int)BIT(map));
1472 static ssize_t set_pwm_temp_map(struct device *dev,
1473 struct device_attribute *attr, const char *buf,
1476 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1477 struct it87_data *data = dev_get_drvdata(dev);
1478 int nr = sensor_attr->index;
1482 if (kstrtol(buf, 10, &val) < 0)
1502 mutex_lock(&data->update_lock);
1503 it87_update_pwm_ctrl(data, nr);
1504 data->pwm_temp_map[nr] = reg;
1506 * If we are in automatic mode, write the temp mapping immediately;
1507 * otherwise, just store it for later use.
1509 if (data->pwm_ctrl[nr] & 0x80) {
1510 data->pwm_ctrl[nr] = (data->pwm_ctrl[nr] & 0xfc) |
1511 data->pwm_temp_map[nr];
1512 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1514 mutex_unlock(&data->update_lock);
1518 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1521 struct it87_data *data = it87_update_device(dev);
1522 struct sensor_device_attribute_2 *sensor_attr =
1523 to_sensor_dev_attr_2(attr);
1524 int nr = sensor_attr->nr;
1525 int point = sensor_attr->index;
1527 return sprintf(buf, "%d\n",
1528 pwm_from_reg(data, data->auto_pwm[nr][point]));
1531 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1532 const char *buf, size_t count)
1534 struct it87_data *data = dev_get_drvdata(dev);
1535 struct sensor_device_attribute_2 *sensor_attr =
1536 to_sensor_dev_attr_2(attr);
1537 int nr = sensor_attr->nr;
1538 int point = sensor_attr->index;
1542 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1545 mutex_lock(&data->update_lock);
1546 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1547 if (has_newer_autopwm(data))
1548 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1550 regaddr = IT87_REG_AUTO_PWM(nr, point);
1551 it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1552 mutex_unlock(&data->update_lock);
1556 static ssize_t show_auto_pwm_slope(struct device *dev,
1557 struct device_attribute *attr, char *buf)
1559 struct it87_data *data = it87_update_device(dev);
1560 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1561 int nr = sensor_attr->index;
1563 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1566 static ssize_t set_auto_pwm_slope(struct device *dev,
1567 struct device_attribute *attr,
1568 const char *buf, size_t count)
1570 struct it87_data *data = dev_get_drvdata(dev);
1571 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1572 int nr = sensor_attr->index;
1575 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1578 mutex_lock(&data->update_lock);
1579 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1580 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1581 data->auto_pwm[nr][1]);
1582 mutex_unlock(&data->update_lock);
1586 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1589 struct it87_data *data = it87_update_device(dev);
1590 struct sensor_device_attribute_2 *sensor_attr =
1591 to_sensor_dev_attr_2(attr);
1592 int nr = sensor_attr->nr;
1593 int point = sensor_attr->index;
1596 if (has_old_autopwm(data) || point)
1597 reg = data->auto_temp[nr][point];
1599 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1601 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
1604 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1605 const char *buf, size_t count)
1607 struct it87_data *data = dev_get_drvdata(dev);
1608 struct sensor_device_attribute_2 *sensor_attr =
1609 to_sensor_dev_attr_2(attr);
1610 int nr = sensor_attr->nr;
1611 int point = sensor_attr->index;
1615 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1618 mutex_lock(&data->update_lock);
1619 if (has_newer_autopwm(data) && !point) {
1620 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1621 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1622 data->auto_temp[nr][0] = reg;
1623 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1625 reg = TEMP_TO_REG(val);
1626 data->auto_temp[nr][point] = reg;
1627 if (has_newer_autopwm(data))
1629 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1631 mutex_unlock(&data->update_lock);
1635 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1636 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1638 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1641 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1642 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1644 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1647 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1648 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1650 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1653 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1654 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1657 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1658 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1661 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1662 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1665 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1666 show_pwm_enable, set_pwm_enable, 0);
1667 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1668 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1670 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
1671 show_pwm_temp_map, set_pwm_temp_map, 0);
1672 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1673 show_auto_pwm, set_auto_pwm, 0, 0);
1674 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1675 show_auto_pwm, set_auto_pwm, 0, 1);
1676 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1677 show_auto_pwm, set_auto_pwm, 0, 2);
1678 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1679 show_auto_pwm, NULL, 0, 3);
1680 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1681 show_auto_temp, set_auto_temp, 0, 1);
1682 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1683 show_auto_temp, set_auto_temp, 0, 0);
1684 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1685 show_auto_temp, set_auto_temp, 0, 2);
1686 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1687 show_auto_temp, set_auto_temp, 0, 3);
1688 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1689 show_auto_temp, set_auto_temp, 0, 4);
1690 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1691 show_auto_pwm, set_auto_pwm, 0, 0);
1692 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1693 show_auto_pwm_slope, set_auto_pwm_slope, 0);
1695 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1696 show_pwm_enable, set_pwm_enable, 1);
1697 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1698 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1699 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
1700 show_pwm_temp_map, set_pwm_temp_map, 1);
1701 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1702 show_auto_pwm, set_auto_pwm, 1, 0);
1703 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1704 show_auto_pwm, set_auto_pwm, 1, 1);
1705 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1706 show_auto_pwm, set_auto_pwm, 1, 2);
1707 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1708 show_auto_pwm, NULL, 1, 3);
1709 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1710 show_auto_temp, set_auto_temp, 1, 1);
1711 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1712 show_auto_temp, set_auto_temp, 1, 0);
1713 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1714 show_auto_temp, set_auto_temp, 1, 2);
1715 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1716 show_auto_temp, set_auto_temp, 1, 3);
1717 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1718 show_auto_temp, set_auto_temp, 1, 4);
1719 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1720 show_auto_pwm, set_auto_pwm, 1, 0);
1721 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1722 show_auto_pwm_slope, set_auto_pwm_slope, 1);
1724 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1725 show_pwm_enable, set_pwm_enable, 2);
1726 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1727 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1728 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
1729 show_pwm_temp_map, set_pwm_temp_map, 2);
1730 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1731 show_auto_pwm, set_auto_pwm, 2, 0);
1732 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1733 show_auto_pwm, set_auto_pwm, 2, 1);
1734 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1735 show_auto_pwm, set_auto_pwm, 2, 2);
1736 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1737 show_auto_pwm, NULL, 2, 3);
1738 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1739 show_auto_temp, set_auto_temp, 2, 1);
1740 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1741 show_auto_temp, set_auto_temp, 2, 0);
1742 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1743 show_auto_temp, set_auto_temp, 2, 2);
1744 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1745 show_auto_temp, set_auto_temp, 2, 3);
1746 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1747 show_auto_temp, set_auto_temp, 2, 4);
1748 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1749 show_auto_pwm, set_auto_pwm, 2, 0);
1750 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1751 show_auto_pwm_slope, set_auto_pwm_slope, 2);
1753 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1754 show_pwm_enable, set_pwm_enable, 3);
1755 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1756 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1757 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
1758 show_pwm_temp_map, set_pwm_temp_map, 3);
1759 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1760 show_auto_temp, set_auto_temp, 2, 1);
1761 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1762 show_auto_temp, set_auto_temp, 2, 0);
1763 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1764 show_auto_temp, set_auto_temp, 2, 2);
1765 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1766 show_auto_temp, set_auto_temp, 2, 3);
1767 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1768 show_auto_pwm, set_auto_pwm, 3, 0);
1769 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1770 show_auto_pwm_slope, set_auto_pwm_slope, 3);
1772 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1773 show_pwm_enable, set_pwm_enable, 4);
1774 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1775 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1776 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
1777 show_pwm_temp_map, set_pwm_temp_map, 4);
1778 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1779 show_auto_temp, set_auto_temp, 2, 1);
1780 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1781 show_auto_temp, set_auto_temp, 2, 0);
1782 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1783 show_auto_temp, set_auto_temp, 2, 2);
1784 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1785 show_auto_temp, set_auto_temp, 2, 3);
1786 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1787 show_auto_pwm, set_auto_pwm, 4, 0);
1788 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1789 show_auto_pwm_slope, set_auto_pwm_slope, 4);
1791 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1792 show_pwm_enable, set_pwm_enable, 5);
1793 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1794 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1795 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
1796 show_pwm_temp_map, set_pwm_temp_map, 5);
1797 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1798 show_auto_temp, set_auto_temp, 2, 1);
1799 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1800 show_auto_temp, set_auto_temp, 2, 0);
1801 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1802 show_auto_temp, set_auto_temp, 2, 2);
1803 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1804 show_auto_temp, set_auto_temp, 2, 3);
1805 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1806 show_auto_pwm, set_auto_pwm, 5, 0);
1807 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1808 show_auto_pwm_slope, set_auto_pwm_slope, 5);
1811 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
1814 struct it87_data *data = it87_update_device(dev);
1816 return sprintf(buf, "%u\n", data->alarms);
1818 static DEVICE_ATTR_RO(alarms);
1820 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1823 struct it87_data *data = it87_update_device(dev);
1824 int bitnr = to_sensor_dev_attr(attr)->index;
1826 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1829 static ssize_t clear_intrusion(struct device *dev,
1830 struct device_attribute *attr, const char *buf,
1833 struct it87_data *data = dev_get_drvdata(dev);
1837 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1840 mutex_lock(&data->update_lock);
1841 config = it87_read_value(data, IT87_REG_CONFIG);
1846 it87_write_value(data, IT87_REG_CONFIG, config);
1847 /* Invalidate cache to force re-read */
1850 mutex_unlock(&data->update_lock);
1855 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1856 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1857 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1858 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1859 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1860 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1861 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1862 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1863 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1864 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1865 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1866 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1867 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1868 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1869 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1870 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1871 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1872 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1873 show_alarm, clear_intrusion, 4);
1875 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1878 struct it87_data *data = it87_update_device(dev);
1879 int bitnr = to_sensor_dev_attr(attr)->index;
1881 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1884 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1885 const char *buf, size_t count)
1887 int bitnr = to_sensor_dev_attr(attr)->index;
1888 struct it87_data *data = dev_get_drvdata(dev);
1891 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
1894 mutex_lock(&data->update_lock);
1895 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1897 data->beeps |= BIT(bitnr);
1899 data->beeps &= ~BIT(bitnr);
1900 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1901 mutex_unlock(&data->update_lock);
1905 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1906 show_beep, set_beep, 1);
1907 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1908 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1909 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1910 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1911 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1912 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1913 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1914 /* fanX_beep writability is set later */
1915 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1916 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1917 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1918 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1919 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1920 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1921 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1922 show_beep, set_beep, 2);
1923 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1924 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1926 static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
1929 struct it87_data *data = dev_get_drvdata(dev);
1931 return sprintf(buf, "%u\n", data->vrm);
1934 static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
1935 const char *buf, size_t count)
1937 struct it87_data *data = dev_get_drvdata(dev);
1940 if (kstrtoul(buf, 10, &val) < 0)
1947 static DEVICE_ATTR_RW(vrm);
1949 static ssize_t cpu0_vid_show(struct device *dev,
1950 struct device_attribute *attr, char *buf)
1952 struct it87_data *data = it87_update_device(dev);
1954 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
1956 static DEVICE_ATTR_RO(cpu0_vid);
1958 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1961 static const char * const labels[] = {
1967 static const char * const labels_it8721[] = {
1973 struct it87_data *data = dev_get_drvdata(dev);
1974 int nr = to_sensor_dev_attr(attr)->index;
1977 if (has_vin3_5v(data) && nr == 0)
1979 else if (has_12mv_adc(data) || has_10_9mv_adc(data))
1980 label = labels_it8721[nr];
1984 return sprintf(buf, "%s\n", label);
1986 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1987 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1988 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1990 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 3);
1992 static umode_t it87_in_is_visible(struct kobject *kobj,
1993 struct attribute *attr, int index)
1995 struct device *dev = container_of(kobj, struct device, kobj);
1996 struct it87_data *data = dev_get_drvdata(dev);
1997 int i = index / 5; /* voltage index */
1998 int a = index % 5; /* attribute index */
2000 if (index >= 40) { /* in8 and higher only have input attributes */
2005 if (!(data->has_in & BIT(i)))
2008 if (a == 4 && !data->has_beep)
2014 static struct attribute *it87_attributes_in[] = {
2015 &sensor_dev_attr_in0_input.dev_attr.attr,
2016 &sensor_dev_attr_in0_min.dev_attr.attr,
2017 &sensor_dev_attr_in0_max.dev_attr.attr,
2018 &sensor_dev_attr_in0_alarm.dev_attr.attr,
2019 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
2021 &sensor_dev_attr_in1_input.dev_attr.attr,
2022 &sensor_dev_attr_in1_min.dev_attr.attr,
2023 &sensor_dev_attr_in1_max.dev_attr.attr,
2024 &sensor_dev_attr_in1_alarm.dev_attr.attr,
2025 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
2027 &sensor_dev_attr_in2_input.dev_attr.attr,
2028 &sensor_dev_attr_in2_min.dev_attr.attr,
2029 &sensor_dev_attr_in2_max.dev_attr.attr,
2030 &sensor_dev_attr_in2_alarm.dev_attr.attr,
2031 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
2033 &sensor_dev_attr_in3_input.dev_attr.attr,
2034 &sensor_dev_attr_in3_min.dev_attr.attr,
2035 &sensor_dev_attr_in3_max.dev_attr.attr,
2036 &sensor_dev_attr_in3_alarm.dev_attr.attr,
2037 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2039 &sensor_dev_attr_in4_input.dev_attr.attr,
2040 &sensor_dev_attr_in4_min.dev_attr.attr,
2041 &sensor_dev_attr_in4_max.dev_attr.attr,
2042 &sensor_dev_attr_in4_alarm.dev_attr.attr,
2043 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2045 &sensor_dev_attr_in5_input.dev_attr.attr,
2046 &sensor_dev_attr_in5_min.dev_attr.attr,
2047 &sensor_dev_attr_in5_max.dev_attr.attr,
2048 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2049 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2051 &sensor_dev_attr_in6_input.dev_attr.attr,
2052 &sensor_dev_attr_in6_min.dev_attr.attr,
2053 &sensor_dev_attr_in6_max.dev_attr.attr,
2054 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2055 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2057 &sensor_dev_attr_in7_input.dev_attr.attr,
2058 &sensor_dev_attr_in7_min.dev_attr.attr,
2059 &sensor_dev_attr_in7_max.dev_attr.attr,
2060 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2061 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2063 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2064 &sensor_dev_attr_in9_input.dev_attr.attr,
2065 &sensor_dev_attr_in10_input.dev_attr.attr,
2066 &sensor_dev_attr_in11_input.dev_attr.attr,
2067 &sensor_dev_attr_in12_input.dev_attr.attr,
2071 static const struct attribute_group it87_group_in = {
2072 .attrs = it87_attributes_in,
2073 .is_visible = it87_in_is_visible,
2076 static umode_t it87_temp_is_visible(struct kobject *kobj,
2077 struct attribute *attr, int index)
2079 struct device *dev = container_of(kobj, struct device, kobj);
2080 struct it87_data *data = dev_get_drvdata(dev);
2081 int i = index / 7; /* temperature index */
2082 int a = index % 7; /* attribute index */
2089 if (!(data->has_temp & BIT(i)))
2092 if (a == 5 && !has_temp_offset(data))
2095 if (a == 6 && !data->has_beep)
2101 static struct attribute *it87_attributes_temp[] = {
2102 &sensor_dev_attr_temp1_input.dev_attr.attr,
2103 &sensor_dev_attr_temp1_max.dev_attr.attr,
2104 &sensor_dev_attr_temp1_min.dev_attr.attr,
2105 &sensor_dev_attr_temp1_type.dev_attr.attr,
2106 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2107 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2108 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2110 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2111 &sensor_dev_attr_temp2_max.dev_attr.attr,
2112 &sensor_dev_attr_temp2_min.dev_attr.attr,
2113 &sensor_dev_attr_temp2_type.dev_attr.attr,
2114 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2115 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2116 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2118 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2119 &sensor_dev_attr_temp3_max.dev_attr.attr,
2120 &sensor_dev_attr_temp3_min.dev_attr.attr,
2121 &sensor_dev_attr_temp3_type.dev_attr.attr,
2122 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2123 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2124 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2126 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2127 &sensor_dev_attr_temp5_input.dev_attr.attr,
2128 &sensor_dev_attr_temp6_input.dev_attr.attr,
2132 static const struct attribute_group it87_group_temp = {
2133 .attrs = it87_attributes_temp,
2134 .is_visible = it87_temp_is_visible,
2137 static umode_t it87_is_visible(struct kobject *kobj,
2138 struct attribute *attr, int index)
2140 struct device *dev = container_of(kobj, struct device, kobj);
2141 struct it87_data *data = dev_get_drvdata(dev);
2143 if ((index == 2 || index == 3) && !data->has_vid)
2146 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2152 static struct attribute *it87_attributes[] = {
2153 &dev_attr_alarms.attr,
2154 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2155 &dev_attr_vrm.attr, /* 2 */
2156 &dev_attr_cpu0_vid.attr, /* 3 */
2157 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2158 &sensor_dev_attr_in7_label.dev_attr.attr,
2159 &sensor_dev_attr_in8_label.dev_attr.attr,
2160 &sensor_dev_attr_in9_label.dev_attr.attr,
2164 static const struct attribute_group it87_group = {
2165 .attrs = it87_attributes,
2166 .is_visible = it87_is_visible,
2169 static umode_t it87_fan_is_visible(struct kobject *kobj,
2170 struct attribute *attr, int index)
2172 struct device *dev = container_of(kobj, struct device, kobj);
2173 struct it87_data *data = dev_get_drvdata(dev);
2174 int i = index / 5; /* fan index */
2175 int a = index % 5; /* attribute index */
2177 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2178 i = (index - 15) / 4 + 3;
2179 a = (index - 15) % 4;
2182 if (!(data->has_fan & BIT(i)))
2185 if (a == 3) { /* beep */
2186 if (!data->has_beep)
2188 /* first fan beep attribute is writable */
2189 if (i == __ffs(data->has_fan))
2190 return attr->mode | S_IWUSR;
2193 if (a == 4 && has_16bit_fans(data)) /* divisor */
2199 static struct attribute *it87_attributes_fan[] = {
2200 &sensor_dev_attr_fan1_input.dev_attr.attr,
2201 &sensor_dev_attr_fan1_min.dev_attr.attr,
2202 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2203 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2204 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2206 &sensor_dev_attr_fan2_input.dev_attr.attr,
2207 &sensor_dev_attr_fan2_min.dev_attr.attr,
2208 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2209 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2210 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2212 &sensor_dev_attr_fan3_input.dev_attr.attr,
2213 &sensor_dev_attr_fan3_min.dev_attr.attr,
2214 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2215 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2216 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2218 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2219 &sensor_dev_attr_fan4_min.dev_attr.attr,
2220 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2221 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2223 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2224 &sensor_dev_attr_fan5_min.dev_attr.attr,
2225 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2226 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2228 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2229 &sensor_dev_attr_fan6_min.dev_attr.attr,
2230 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2231 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2235 static const struct attribute_group it87_group_fan = {
2236 .attrs = it87_attributes_fan,
2237 .is_visible = it87_fan_is_visible,
2240 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2241 struct attribute *attr, int index)
2243 struct device *dev = container_of(kobj, struct device, kobj);
2244 struct it87_data *data = dev_get_drvdata(dev);
2245 int i = index / 4; /* pwm index */
2246 int a = index % 4; /* attribute index */
2248 if (!(data->has_pwm & BIT(i)))
2251 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2252 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2253 return attr->mode | S_IWUSR;
2255 /* pwm2_freq is writable if there are two pwm frequency selects */
2256 if (has_pwm_freq2(data) && i == 1 && a == 2)
2257 return attr->mode | S_IWUSR;
2262 static struct attribute *it87_attributes_pwm[] = {
2263 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2264 &sensor_dev_attr_pwm1.dev_attr.attr,
2265 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2266 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2268 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2269 &sensor_dev_attr_pwm2.dev_attr.attr,
2270 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2271 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2273 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2274 &sensor_dev_attr_pwm3.dev_attr.attr,
2275 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2276 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2278 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2279 &sensor_dev_attr_pwm4.dev_attr.attr,
2280 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2281 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2283 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2284 &sensor_dev_attr_pwm5.dev_attr.attr,
2285 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2286 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2288 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2289 &sensor_dev_attr_pwm6.dev_attr.attr,
2290 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2291 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2296 static const struct attribute_group it87_group_pwm = {
2297 .attrs = it87_attributes_pwm,
2298 .is_visible = it87_pwm_is_visible,
2301 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2302 struct attribute *attr, int index)
2304 struct device *dev = container_of(kobj, struct device, kobj);
2305 struct it87_data *data = dev_get_drvdata(dev);
2306 int i = index / 11; /* pwm index */
2307 int a = index % 11; /* attribute index */
2309 if (index >= 33) { /* pwm 4..6 */
2310 i = (index - 33) / 6 + 3;
2311 a = (index - 33) % 6 + 4;
2314 if (!(data->has_pwm & BIT(i)))
2317 if (has_newer_autopwm(data)) {
2318 if (a < 4) /* no auto point pwm */
2320 if (a == 8) /* no auto_point4 */
2323 if (has_old_autopwm(data)) {
2324 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2331 static struct attribute *it87_attributes_auto_pwm[] = {
2332 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2333 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2334 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2335 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2336 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2337 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2338 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2339 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2340 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2341 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2342 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2344 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2345 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2346 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2347 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2348 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2349 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2350 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2351 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2352 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2353 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2354 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2356 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2357 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2358 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2359 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2360 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2361 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2362 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2363 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2364 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2365 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2366 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2368 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2369 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2370 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2371 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2372 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2373 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2375 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2376 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2377 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2378 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2379 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2380 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2382 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2383 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2384 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2385 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2386 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2387 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2392 static const struct attribute_group it87_group_auto_pwm = {
2393 .attrs = it87_attributes_auto_pwm,
2394 .is_visible = it87_auto_pwm_is_visible,
2397 /* SuperIO detection - will change isa_address if a chip is found */
2398 static int __init it87_find(int sioaddr, unsigned short *address,
2399 struct it87_sio_data *sio_data)
2403 const char *board_vendor, *board_name;
2404 const struct it87_devices *config;
2406 err = superio_enter(sioaddr);
2411 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
2413 switch (chip_type) {
2415 sio_data->type = it87;
2418 sio_data->type = it8712;
2422 sio_data->type = it8716;
2425 sio_data->type = it8718;
2428 sio_data->type = it8720;
2431 sio_data->type = it8721;
2434 sio_data->type = it8728;
2437 sio_data->type = it8732;
2440 sio_data->type = it8792;
2443 sio_data->type = it8771;
2446 sio_data->type = it8772;
2449 sio_data->type = it8781;
2452 sio_data->type = it8782;
2455 sio_data->type = it8783;
2458 sio_data->type = it8786;
2461 sio_data->type = it8790;
2465 sio_data->type = it8603;
2468 sio_data->type = it8620;
2471 sio_data->type = it8622;
2474 sio_data->type = it8628;
2476 case 0xffff: /* No device at all */
2479 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
2483 superio_select(sioaddr, PME);
2484 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
2485 pr_info("Device not activated, skipping\n");
2489 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
2490 if (*address == 0) {
2491 pr_info("Base address not set, skipping\n");
2496 sio_data->sioaddr = sioaddr;
2497 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
2498 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2499 it87_devices[sio_data->type].suffix,
2500 *address, sio_data->revision);
2502 config = &it87_devices[sio_data->type];
2504 /* in7 (VSB or VCCH5V) is always internal on some chips */
2505 if (has_in7_internal(config))
2506 sio_data->internal |= BIT(1);
2508 /* in8 (Vbat) is always internal */
2509 sio_data->internal |= BIT(2);
2511 /* in9 (AVCC3), always internal if supported */
2512 if (has_avcc3(config))
2513 sio_data->internal |= BIT(3); /* in9 is AVCC */
2515 sio_data->skip_in |= BIT(9);
2517 if (!has_five_pwm(config))
2518 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
2519 else if (!has_six_pwm(config))
2520 sio_data->skip_pwm |= BIT(5);
2522 if (!has_vid(config))
2523 sio_data->skip_vid = 1;
2525 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2526 if (sio_data->type == it87) {
2527 /* The IT8705F has a different LD number for GPIO */
2528 superio_select(sioaddr, 5);
2529 sio_data->beep_pin = superio_inb(sioaddr,
2530 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2531 } else if (sio_data->type == it8783) {
2532 int reg25, reg27, reg2a, reg2c, regef;
2534 superio_select(sioaddr, GPIO);
2536 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2537 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2538 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2539 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2540 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
2542 /* Check if fan3 is there or not */
2543 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2544 sio_data->skip_fan |= BIT(2);
2545 if ((reg25 & BIT(4)) ||
2546 (!(reg2a & BIT(1)) && (regef & BIT(0))))
2547 sio_data->skip_pwm |= BIT(2);
2549 /* Check if fan2 is there or not */
2551 sio_data->skip_fan |= BIT(1);
2553 sio_data->skip_pwm |= BIT(1);
2556 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2557 sio_data->skip_in |= BIT(5); /* No VIN5 */
2561 sio_data->skip_in |= BIT(6); /* No VIN6 */
2565 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2567 if (reg27 & BIT(2)) {
2569 * The data sheet is a bit unclear regarding the
2570 * internal voltage divider for VCCH5V. It says
2571 * "This bit enables and switches VIN7 (pin 91) to the
2572 * internal voltage divider for VCCH5V".
2573 * This is different to other chips, where the internal
2574 * voltage divider would connect VIN7 to an internal
2575 * voltage source. Maybe that is the case here as well.
2577 * Since we don't know for sure, re-route it if that is
2578 * not the case, and ask the user to report if the
2579 * resulting voltage is sane.
2581 if (!(reg2c & BIT(1))) {
2583 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2585 sio_data->need_in7_reroute = true;
2586 pr_notice("Routing internal VCCH5V to in7.\n");
2588 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2589 pr_notice("Please report if it displays a reasonable voltage.\n");
2593 sio_data->internal |= BIT(0);
2595 sio_data->internal |= BIT(1);
2597 sio_data->beep_pin = superio_inb(sioaddr,
2598 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2599 } else if (sio_data->type == it8603) {
2602 superio_select(sioaddr, GPIO);
2604 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2606 /* Check if fan3 is there or not */
2608 sio_data->skip_pwm |= BIT(2);
2610 sio_data->skip_fan |= BIT(2);
2612 /* Check if fan2 is there or not */
2613 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2615 sio_data->skip_pwm |= BIT(1);
2617 sio_data->skip_fan |= BIT(1);
2619 sio_data->skip_in |= BIT(5); /* No VIN5 */
2620 sio_data->skip_in |= BIT(6); /* No VIN6 */
2622 sio_data->beep_pin = superio_inb(sioaddr,
2623 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2624 } else if (sio_data->type == it8620 || sio_data->type == it8628) {
2627 superio_select(sioaddr, GPIO);
2629 /* Check for pwm5 */
2630 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2632 sio_data->skip_pwm |= BIT(4);
2634 /* Check for fan4, fan5 */
2635 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2636 if (!(reg & BIT(5)))
2637 sio_data->skip_fan |= BIT(3);
2638 if (!(reg & BIT(4)))
2639 sio_data->skip_fan |= BIT(4);
2641 /* Check for pwm3, fan3 */
2642 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2644 sio_data->skip_pwm |= BIT(2);
2646 sio_data->skip_fan |= BIT(2);
2648 /* Check for pwm4 */
2649 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
2651 sio_data->skip_pwm |= BIT(3);
2653 /* Check for pwm2, fan2 */
2654 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2656 sio_data->skip_pwm |= BIT(1);
2658 sio_data->skip_fan |= BIT(1);
2659 /* Check for pwm6, fan6 */
2660 if (!(reg & BIT(7))) {
2661 sio_data->skip_pwm |= BIT(5);
2662 sio_data->skip_fan |= BIT(5);
2665 /* Check if AVCC is on VIN3 */
2666 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2668 sio_data->internal |= BIT(0);
2670 sio_data->skip_in |= BIT(9);
2672 sio_data->beep_pin = superio_inb(sioaddr,
2673 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2674 } else if (sio_data->type == it8622) {
2677 superio_select(sioaddr, GPIO);
2679 /* Check for pwm4, fan4 */
2680 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2682 sio_data->skip_fan |= BIT(3);
2684 sio_data->skip_pwm |= BIT(3);
2686 /* Check for pwm3, fan3, pwm5, fan5 */
2687 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2689 sio_data->skip_pwm |= BIT(2);
2691 sio_data->skip_fan |= BIT(2);
2693 sio_data->skip_pwm |= BIT(4);
2695 sio_data->skip_fan |= BIT(4);
2697 /* Check for pwm2, fan2 */
2698 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2700 sio_data->skip_pwm |= BIT(1);
2702 sio_data->skip_fan |= BIT(1);
2704 /* Check for AVCC */
2705 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2706 if (!(reg & BIT(0)))
2707 sio_data->skip_in |= BIT(9);
2709 sio_data->beep_pin = superio_inb(sioaddr,
2710 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2715 superio_select(sioaddr, GPIO);
2717 /* Check for fan4, fan5 */
2718 if (has_five_fans(config)) {
2719 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2720 switch (sio_data->type) {
2723 sio_data->skip_fan |= BIT(3);
2725 sio_data->skip_fan |= BIT(4);
2730 if (!(reg & BIT(5)))
2731 sio_data->skip_fan |= BIT(3);
2732 if (!(reg & BIT(4)))
2733 sio_data->skip_fan |= BIT(4);
2740 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2741 if (!sio_data->skip_vid) {
2742 /* We need at least 4 VID pins */
2744 pr_info("VID is disabled (pins used for GPIO)\n");
2745 sio_data->skip_vid = 1;
2749 /* Check if fan3 is there or not */
2751 sio_data->skip_pwm |= BIT(2);
2753 sio_data->skip_fan |= BIT(2);
2755 /* Check if fan2 is there or not */
2756 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2758 sio_data->skip_pwm |= BIT(1);
2760 sio_data->skip_fan |= BIT(1);
2762 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2763 !(sio_data->skip_vid))
2764 sio_data->vid_value = superio_inb(sioaddr,
2767 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2769 uart6 = sio_data->type == it8782 && (reg & BIT(2));
2772 * The IT8720F has no VIN7 pin, so VCCH5V should always be
2773 * routed internally to VIN7 with an internal divider.
2774 * Curiously, there still is a configuration bit to control
2775 * this, which means it can be set incorrectly. And even
2776 * more curiously, many boards out there are improperly
2777 * configured, even though the IT8720F datasheet claims
2778 * that the internal routing of VCCH5V to VIN7 is the default
2779 * setting. So we force the internal routing in this case.
2781 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2782 * If UART6 is enabled, re-route VIN7 to the internal divider
2783 * if that is not already the case.
2785 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2787 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
2788 sio_data->need_in7_reroute = true;
2789 pr_notice("Routing internal VCCH5V to in7\n");
2792 sio_data->internal |= BIT(0);
2794 sio_data->internal |= BIT(1);
2797 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2798 * While VIN7 can be routed to the internal voltage divider,
2799 * VIN5 and VIN6 are not available if UART6 is enabled.
2801 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2802 * is the temperature source. Since we can not read the
2803 * temperature source here, skip_temp is preliminary.
2806 sio_data->skip_in |= BIT(5) | BIT(6);
2807 sio_data->skip_temp |= BIT(2);
2810 sio_data->beep_pin = superio_inb(sioaddr,
2811 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2813 if (sio_data->beep_pin)
2814 pr_info("Beeping is supported\n");
2816 /* Disable specific features based on DMI strings */
2817 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2818 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2819 if (board_vendor && board_name) {
2820 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2821 strcmp(board_name, "FN68PT") == 0) {
2823 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2824 * connected to a fan, but to something else. One user
2825 * has reported instant system power-off when changing
2826 * the PWM2 duty cycle, so we disable it.
2827 * I use the board name string as the trigger in case
2828 * the same board is ever used in other systems.
2830 pr_info("Disabling pwm2 due to hardware constraints\n");
2831 sio_data->skip_pwm = BIT(1);
2836 superio_exit(sioaddr);
2841 * Some chips seem to have default value 0xff for all limit
2842 * registers. For low voltage limits it makes no sense and triggers
2843 * alarms, so change to 0 instead. For high temperature limits, it
2844 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2845 * but is still confusing, so change to 127 degrees C.
2847 static void it87_check_limit_regs(struct it87_data *data)
2851 for (i = 0; i < NUM_VIN_LIMIT; i++) {
2852 reg = it87_read_value(data, IT87_REG_VIN_MIN(i));
2854 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2856 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
2857 reg = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2859 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2863 /* Check if voltage monitors are reset manually or by some reason */
2864 static void it87_check_voltage_monitors_reset(struct it87_data *data)
2868 reg = it87_read_value(data, IT87_REG_VIN_ENABLE);
2869 if ((reg & 0xff) == 0) {
2870 /* Enable all voltage monitors */
2871 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2875 /* Check if tachometers are reset manually or by some reason */
2876 static void it87_check_tachometers_reset(struct platform_device *pdev)
2878 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2879 struct it87_data *data = platform_get_drvdata(pdev);
2880 u8 mask, fan_main_ctrl;
2882 mask = 0x70 & ~(sio_data->skip_fan << 4);
2883 fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2884 if ((fan_main_ctrl & mask) == 0) {
2885 /* Enable all fan tachometers */
2886 fan_main_ctrl |= mask;
2887 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2892 /* Set tachometers to 16-bit mode if needed */
2893 static void it87_check_tachometers_16bit_mode(struct platform_device *pdev)
2895 struct it87_data *data = platform_get_drvdata(pdev);
2898 if (!has_fan16_config(data))
2901 reg = it87_read_value(data, IT87_REG_FAN_16BIT);
2902 if (~reg & 0x07 & data->has_fan) {
2904 "Setting fan1-3 to 16-bit mode\n");
2905 it87_write_value(data, IT87_REG_FAN_16BIT,
2910 static void it87_start_monitoring(struct it87_data *data)
2912 it87_write_value(data, IT87_REG_CONFIG,
2913 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2914 | (update_vbat ? 0x41 : 0x01));
2917 /* Called when we have found a new IT87. */
2918 static void it87_init_device(struct platform_device *pdev)
2920 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2921 struct it87_data *data = platform_get_drvdata(pdev);
2925 * For each PWM channel:
2926 * - If it is in automatic mode, setting to manual mode should set
2927 * the fan to full speed by default.
2928 * - If it is in manual mode, we need a mapping to temperature
2929 * channels to use when later setting to automatic mode later.
2930 * Use a 1:1 mapping by default (we are clueless.)
2931 * In both cases, the value can (and should) be changed by the user
2932 * prior to switching to a different mode.
2933 * Note that this is no longer needed for the IT8721F and later, as
2934 * these have separate registers for the temperature mapping and the
2935 * manual duty cycle.
2937 for (i = 0; i < NUM_AUTO_PWM; i++) {
2938 data->pwm_temp_map[i] = i;
2939 data->pwm_duty[i] = 0x7f; /* Full speed */
2940 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2943 it87_check_limit_regs(data);
2946 * Temperature channels are not forcibly enabled, as they can be
2947 * set to two different sensor types and we can't guess which one
2948 * is correct for a given system. These channels can be enabled at
2949 * run-time through the temp{1-3}_type sysfs accessors if needed.
2952 it87_check_voltage_monitors_reset(data);
2954 it87_check_tachometers_reset(pdev);
2956 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2957 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2959 it87_check_tachometers_16bit_mode(pdev);
2961 /* Check for additional fans */
2962 if (has_five_fans(data)) {
2963 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2966 data->has_fan |= BIT(3); /* fan4 enabled */
2968 data->has_fan |= BIT(4); /* fan5 enabled */
2969 if (has_six_fans(data) && (tmp & BIT(2)))
2970 data->has_fan |= BIT(5); /* fan6 enabled */
2973 /* Fan input pins may be used for alternative functions */
2974 data->has_fan &= ~sio_data->skip_fan;
2976 /* Check if pwm5, pwm6 are enabled */
2977 if (has_six_pwm(data)) {
2978 /* The following code may be IT8620E specific */
2979 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2980 if ((tmp & 0xc0) == 0xc0)
2981 sio_data->skip_pwm |= BIT(4);
2982 if (!(tmp & BIT(3)))
2983 sio_data->skip_pwm |= BIT(5);
2986 it87_start_monitoring(data);
2989 /* Return 1 if and only if the PWM interface is safe to use */
2990 static int it87_check_pwm(struct device *dev)
2992 struct it87_data *data = dev_get_drvdata(dev);
2994 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2995 * and polarity set to active low is sign that this is the case so we
2996 * disable pwm control to protect the user.
2998 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
3000 if ((tmp & 0x87) == 0) {
3001 if (fix_pwm_polarity) {
3003 * The user asks us to attempt a chip reconfiguration.
3004 * This means switching to active high polarity and
3005 * inverting all fan speed values.
3010 for (i = 0; i < ARRAY_SIZE(pwm); i++)
3011 pwm[i] = it87_read_value(data,
3015 * If any fan is in automatic pwm mode, the polarity
3016 * might be correct, as suspicious as it seems, so we
3017 * better don't change anything (but still disable the
3020 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3022 "Reconfiguring PWM to active high polarity\n");
3023 it87_write_value(data, IT87_REG_FAN_CTL,
3025 for (i = 0; i < 3; i++)
3026 it87_write_value(data,
3033 "PWM configuration is too broken to be fixed\n");
3037 } else if (fix_pwm_polarity) {
3039 "PWM configuration looks sane, won't touch\n");
3045 static int it87_probe(struct platform_device *pdev)
3047 struct it87_data *data;
3048 struct resource *res;
3049 struct device *dev = &pdev->dev;
3050 struct it87_sio_data *sio_data = dev_get_platdata(dev);
3051 int enable_pwm_interface;
3052 struct device *hwmon_dev;
3054 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3055 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
3057 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
3058 (unsigned long)res->start,
3059 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
3063 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
3067 data->addr = res->start;
3068 data->sioaddr = sio_data->sioaddr;
3069 data->type = sio_data->type;
3070 data->features = it87_devices[sio_data->type].features;
3071 data->peci_mask = it87_devices[sio_data->type].peci_mask;
3072 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3074 * IT8705F Datasheet 0.4.1, 3h == Version G.
3075 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3076 * These are the first revisions with 16-bit tachometer support.
3078 switch (data->type) {
3080 if (sio_data->revision >= 0x03) {
3081 data->features &= ~FEAT_OLD_AUTOPWM;
3082 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3086 if (sio_data->revision >= 0x08) {
3087 data->features &= ~FEAT_OLD_AUTOPWM;
3088 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3096 /* Now, we do the remaining detection. */
3097 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
3098 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
3101 platform_set_drvdata(pdev, data);
3103 mutex_init(&data->update_lock);
3105 /* Check PWM configuration */
3106 enable_pwm_interface = it87_check_pwm(dev);
3107 if (!enable_pwm_interface)
3109 "Detected broken BIOS defaults, disabling PWM interface\n");
3111 /* Starting with IT8721F, we handle scaling of internal voltages */
3112 if (has_scaling(data)) {
3113 if (sio_data->internal & BIT(0))
3114 data->in_scaled |= BIT(3); /* in3 is AVCC */
3115 if (sio_data->internal & BIT(1))
3116 data->in_scaled |= BIT(7); /* in7 is VSB */
3117 if (sio_data->internal & BIT(2))
3118 data->in_scaled |= BIT(8); /* in8 is Vbat */
3119 if (sio_data->internal & BIT(3))
3120 data->in_scaled |= BIT(9); /* in9 is AVCC */
3121 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3122 sio_data->type == it8783) {
3123 if (sio_data->internal & BIT(0))
3124 data->in_scaled |= BIT(3); /* in3 is VCC5V */
3125 if (sio_data->internal & BIT(1))
3126 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
3129 data->has_temp = 0x07;
3130 if (sio_data->skip_temp & BIT(2)) {
3131 if (sio_data->type == it8782 &&
3132 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
3133 data->has_temp &= ~BIT(2);
3136 data->in_internal = sio_data->internal;
3137 data->need_in7_reroute = sio_data->need_in7_reroute;
3138 data->has_in = 0x3ff & ~sio_data->skip_in;
3140 if (has_six_temp(data)) {
3141 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
3143 /* Check for additional temperature sensors */
3144 if ((reg & 0x03) >= 0x02)
3145 data->has_temp |= BIT(3);
3146 if (((reg >> 2) & 0x03) >= 0x02)
3147 data->has_temp |= BIT(4);
3148 if (((reg >> 4) & 0x03) >= 0x02)
3149 data->has_temp |= BIT(5);
3151 /* Check for additional voltage sensors */
3152 if ((reg & 0x03) == 0x01)
3153 data->has_in |= BIT(10);
3154 if (((reg >> 2) & 0x03) == 0x01)
3155 data->has_in |= BIT(11);
3156 if (((reg >> 4) & 0x03) == 0x01)
3157 data->has_in |= BIT(12);
3160 data->has_beep = !!sio_data->beep_pin;
3162 /* Initialize the IT87 chip */
3163 it87_init_device(pdev);
3165 if (!sio_data->skip_vid) {
3166 data->has_vid = true;
3167 data->vrm = vid_which_vrm();
3168 /* VID reading from Super-I/O config space if available */
3169 data->vid = sio_data->vid_value;
3172 /* Prepare for sysfs hooks */
3173 data->groups[0] = &it87_group;
3174 data->groups[1] = &it87_group_in;
3175 data->groups[2] = &it87_group_temp;
3176 data->groups[3] = &it87_group_fan;
3178 if (enable_pwm_interface) {
3179 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
3180 data->has_pwm &= ~sio_data->skip_pwm;
3182 data->groups[4] = &it87_group_pwm;
3183 if (has_old_autopwm(data) || has_newer_autopwm(data))
3184 data->groups[5] = &it87_group_auto_pwm;
3187 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3188 it87_devices[sio_data->type].name,
3189 data, data->groups);
3190 return PTR_ERR_OR_ZERO(hwmon_dev);
3193 static void __maybe_unused it87_resume_sio(struct platform_device *pdev)
3195 struct it87_data *data = dev_get_drvdata(&pdev->dev);
3199 if (!data->need_in7_reroute)
3202 err = superio_enter(data->sioaddr);
3204 dev_warn(&pdev->dev,
3205 "Unable to enter Super I/O to reroute in7 (%d)",
3210 superio_select(data->sioaddr, GPIO);
3212 reg2c = superio_inb(data->sioaddr, IT87_SIO_PINX2_REG);
3213 if (!(reg2c & BIT(1))) {
3215 "Routing internal VCCH5V to in7 again");
3218 superio_outb(data->sioaddr, IT87_SIO_PINX2_REG,
3222 superio_exit(data->sioaddr);
3225 static int __maybe_unused it87_resume(struct device *dev)
3227 struct platform_device *pdev = to_platform_device(dev);
3228 struct it87_data *data = dev_get_drvdata(dev);
3230 it87_resume_sio(pdev);
3232 mutex_lock(&data->update_lock);
3234 it87_check_pwm(dev);
3235 it87_check_limit_regs(data);
3236 it87_check_voltage_monitors_reset(data);
3237 it87_check_tachometers_reset(pdev);
3238 it87_check_tachometers_16bit_mode(pdev);
3240 it87_start_monitoring(data);
3245 mutex_unlock(&data->update_lock);
3247 it87_update_device(dev);
3252 static SIMPLE_DEV_PM_OPS(it87_dev_pm_ops, NULL, it87_resume);
3254 static struct platform_driver it87_driver = {
3257 .pm = &it87_dev_pm_ops,
3259 .probe = it87_probe,
3262 static int __init it87_device_add(int index, unsigned short address,
3263 const struct it87_sio_data *sio_data)
3265 struct platform_device *pdev;
3266 struct resource res = {
3267 .start = address + IT87_EC_OFFSET,
3268 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
3270 .flags = IORESOURCE_IO,
3274 err = acpi_check_resource_conflict(&res);
3278 pdev = platform_device_alloc(DRVNAME, address);
3282 err = platform_device_add_resources(pdev, &res, 1);
3284 pr_err("Device resource addition failed (%d)\n", err);
3285 goto exit_device_put;
3288 err = platform_device_add_data(pdev, sio_data,
3289 sizeof(struct it87_sio_data));
3291 pr_err("Platform data allocation failed\n");
3292 goto exit_device_put;
3295 err = platform_device_add(pdev);
3297 pr_err("Device addition failed (%d)\n", err);
3298 goto exit_device_put;
3301 it87_pdev[index] = pdev;
3305 platform_device_put(pdev);
3309 static int __init sm_it87_init(void)
3311 int sioaddr[2] = { REG_2E, REG_4E };
3312 struct it87_sio_data sio_data;
3313 unsigned short isa_address[2];
3317 err = platform_driver_register(&it87_driver);
3321 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3322 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3324 err = it87_find(sioaddr[i], &isa_address[i], &sio_data);
3325 if (err || isa_address[i] == 0)
3328 * Don't register second chip if its ISA address matches
3329 * the first chip's ISA address.
3331 if (i && isa_address[i] == isa_address[0])
3334 err = it87_device_add(i, isa_address[i], &sio_data);
3336 goto exit_dev_unregister;
3341 * IT8705F may respond on both SIO addresses.
3342 * Stop probing after finding one.
3344 if (sio_data.type == it87)
3350 goto exit_unregister;
3354 exit_dev_unregister:
3355 /* NULL check handled by platform_device_unregister */
3356 platform_device_unregister(it87_pdev[0]);
3358 platform_driver_unregister(&it87_driver);
3362 static void __exit sm_it87_exit(void)
3364 /* NULL check handled by platform_device_unregister */
3365 platform_device_unregister(it87_pdev[1]);
3366 platform_device_unregister(it87_pdev[0]);
3367 platform_driver_unregister(&it87_driver);
3370 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
3371 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
3372 module_param(update_vbat, bool, 0);
3373 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3374 module_param(fix_pwm_polarity, bool, 0);
3375 MODULE_PARM_DESC(fix_pwm_polarity,
3376 "Force PWM polarity to active high (DANGEROUS)");
3377 MODULE_LICENSE("GPL");
3379 module_init(sm_it87_init);
3380 module_exit(sm_it87_exit);