2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
13 * Supports: IT8603E Super I/O chip w/LPC interface
14 * IT8607E Super I/O chip w/LPC interface
15 * IT8620E Super I/O chip w/LPC interface
16 * IT8622E Super I/O chip w/LPC interface
17 * IT8623E Super I/O chip w/LPC interface
18 * IT8628E Super I/O chip w/LPC interface
19 * IT8686E Super I/O chip w/LPC interface
20 * IT8705F Super I/O chip w/LPC interface
21 * IT8712F Super I/O chip w/LPC interface
22 * IT8716F Super I/O chip w/LPC interface
23 * IT8718F Super I/O chip w/LPC interface
24 * IT8720F Super I/O chip w/LPC interface
25 * IT8721F Super I/O chip w/LPC interface
26 * IT8726F Super I/O chip w/LPC interface
27 * IT8728F Super I/O chip w/LPC interface
28 * IT8732F Super I/O chip w/LPC interface
29 * IT8758E Super I/O chip w/LPC interface
30 * IT8771E Super I/O chip w/LPC interface
31 * IT8772E Super I/O chip w/LPC interface
32 * IT8781F Super I/O chip w/LPC interface
33 * IT8782F Super I/O chip w/LPC interface
34 * IT8783E/F Super I/O chip w/LPC interface
35 * IT8786E Super I/O chip w/LPC interface
36 * IT8790E Super I/O chip w/LPC interface
37 * IT8792E Super I/O chip w/LPC interface
38 * Sis950 A clone of the IT8705F
40 * Copyright (C) 2001 Chris Gauthron
41 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
43 * This program is free software; you can redistribute it and/or modify
44 * it under the terms of the GNU General Public License as published by
45 * the Free Software Foundation; either version 2 of the License, or
46 * (at your option) any later version.
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
54 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56 #include <linux/bitops.h>
57 #include <linux/module.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #include <linux/jiffies.h>
61 #include <linux/platform_device.h>
62 #include <linux/hwmon.h>
63 #include <linux/hwmon-sysfs.h>
64 #include <linux/hwmon-vid.h>
65 #include <linux/err.h>
66 #include <linux/mutex.h>
67 #include <linux/sysfs.h>
68 #include <linux/string.h>
69 #include <linux/dmi.h>
70 #include <linux/acpi.h>
74 #define DRVNAME "it87"
76 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
77 it8771, it8772, it8781, it8782, it8783, it8786, it8790,
78 it8792, it8603, it8607, it8620, it8622, it8628, it8686 };
80 static unsigned short force_id;
81 module_param(force_id, ushort, 0);
82 MODULE_PARM_DESC(force_id, "Override the detected device ID");
84 static struct platform_device *it87_pdev[2];
86 #define REG_2E 0x2e /* The register to read/write */
87 #define REG_4E 0x4e /* Secondary register to read/write */
89 #define DEV 0x07 /* Register: Logical device select */
90 #define PME 0x04 /* The device with the fan registers in it */
92 /* The device with the IT8718F/IT8720F VID value in it */
95 #define DEVID 0x20 /* Register: Device ID */
96 #define DEVREV 0x22 /* Register: Device Revision */
98 static inline int superio_inb(int ioreg, int reg)
101 return inb(ioreg + 1);
104 static inline void superio_outb(int ioreg, int reg, int val)
107 outb(val, ioreg + 1);
110 static int superio_inw(int ioreg, int reg)
115 val = inb(ioreg + 1) << 8;
117 val |= inb(ioreg + 1);
121 static inline void superio_select(int ioreg, int ldn)
124 outb(ldn, ioreg + 1);
127 static inline int superio_enter(int ioreg)
130 * Try to reserve ioreg and ioreg + 1 for exclusive access.
132 if (!request_muxed_region(ioreg, 2, DRVNAME))
138 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
142 static inline void superio_exit(int ioreg)
145 outb(0x02, ioreg + 1);
146 release_region(ioreg, 2);
149 /* Logical device 4 registers */
150 #define IT8712F_DEVID 0x8712
151 #define IT8705F_DEVID 0x8705
152 #define IT8716F_DEVID 0x8716
153 #define IT8718F_DEVID 0x8718
154 #define IT8720F_DEVID 0x8720
155 #define IT8721F_DEVID 0x8721
156 #define IT8726F_DEVID 0x8726
157 #define IT8728F_DEVID 0x8728
158 #define IT8732F_DEVID 0x8732
159 #define IT8792E_DEVID 0x8733
160 #define IT8771E_DEVID 0x8771
161 #define IT8772E_DEVID 0x8772
162 #define IT8781F_DEVID 0x8781
163 #define IT8782F_DEVID 0x8782
164 #define IT8783E_DEVID 0x8783
165 #define IT8786E_DEVID 0x8786
166 #define IT8790E_DEVID 0x8790
167 #define IT8603E_DEVID 0x8603
168 #define IT8607E_DEVID 0x8607
169 #define IT8620E_DEVID 0x8620
170 #define IT8622E_DEVID 0x8622
171 #define IT8623E_DEVID 0x8623
172 #define IT8628E_DEVID 0x8628
173 #define IT8686E_DEVID 0x8686
174 #define IT87_ACT_REG 0x30
175 #define IT87_BASE_REG 0x60
177 /* Logical device 7 registers (IT8712F and later) */
178 #define IT87_SIO_GPIO1_REG 0x25
179 #define IT87_SIO_GPIO2_REG 0x26
180 #define IT87_SIO_GPIO3_REG 0x27
181 #define IT87_SIO_GPIO4_REG 0x28
182 #define IT87_SIO_GPIO5_REG 0x29
183 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
184 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
185 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
186 #define IT87_SIO_VID_REG 0xfc /* VID value */
187 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
189 /* Update battery voltage after every reading if true */
190 static bool update_vbat;
192 /* Not all BIOSes properly configure the PWM registers */
193 static bool fix_pwm_polarity;
195 /* Many IT87 constants specified below */
197 /* Length of ISA address segment */
198 #define IT87_EXTENT 8
200 /* Length of ISA address segment for Environmental Controller */
201 #define IT87_EC_EXTENT 2
203 /* Offset of EC registers from ISA base address */
204 #define IT87_EC_OFFSET 5
206 /* Where are the ISA address/data registers relative to the EC base address */
207 #define IT87_ADDR_REG_OFFSET 0
208 #define IT87_DATA_REG_OFFSET 1
210 /*----- The IT87 registers -----*/
212 #define IT87_REG_CONFIG 0x00
214 #define IT87_REG_ALARM1 0x01
215 #define IT87_REG_ALARM2 0x02
216 #define IT87_REG_ALARM3 0x03
218 #define IT87_REG_BANK 0x06
221 * The IT8718F and IT8720F have the VID value in a different register, in
222 * Super-I/O configuration space.
224 #define IT87_REG_VID 0x0a
226 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
227 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
230 #define IT87_REG_FAN_DIV 0x0b
231 #define IT87_REG_FAN_16BIT 0x0c
235 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
236 * - up to 6 temp (1 to 6)
237 * - up to 6 fan (1 to 6)
240 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
241 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
242 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
243 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
244 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
246 #define IT87_REG_FAN_MAIN_CTRL 0x13
247 #define IT87_REG_FAN_CTL 0x14
248 static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
249 static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
251 static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
252 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
254 #define IT87_REG_TEMP(nr) (0x29 + (nr))
256 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
257 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
258 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
259 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
261 #define IT87_REG_VIN_ENABLE 0x50
262 #define IT87_REG_TEMP_ENABLE 0x51
263 #define IT87_REG_TEMP_EXTRA 0x55
264 #define IT87_REG_BEEP_ENABLE 0x5c
266 #define IT87_REG_CHIPID 0x58
268 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
270 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
271 #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
273 #define IT87_REG_TEMP456_ENABLE 0x77
275 #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
276 #define NUM_VIN_LIMIT 8
278 #define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
279 #define NUM_TEMP_LIMIT 3
280 #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
281 #define NUM_FAN_DIV 3
282 #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
283 #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
285 struct it87_devices {
287 const char * const suffix;
293 #define FEAT_12MV_ADC BIT(0)
294 #define FEAT_NEWER_AUTOPWM BIT(1)
295 #define FEAT_OLD_AUTOPWM BIT(2)
296 #define FEAT_16BIT_FANS BIT(3)
297 #define FEAT_TEMP_OFFSET BIT(4)
298 #define FEAT_TEMP_PECI BIT(5)
299 #define FEAT_TEMP_OLD_PECI BIT(6)
300 #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
301 #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
302 #define FEAT_VID BIT(9) /* Set if chip supports VID */
303 #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
304 #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
305 #define FEAT_10_9MV_ADC BIT(12)
306 #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
307 #define FEAT_FIVE_PWM BIT(14) /* Chip supports 5 pwm chn */
308 #define FEAT_SIX_PWM BIT(15) /* Chip supports 6 pwm chn */
309 #define FEAT_PWM_FREQ2 BIT(16) /* Separate pwm freq 2 */
310 #define FEAT_SIX_TEMP BIT(17) /* Up to 6 temp sensors */
311 #define FEAT_VIN3_5V BIT(18) /* VIN3 connected to +5V */
312 #define FEAT_FOUR_FANS BIT(19) /* Supports four fans */
313 #define FEAT_FOUR_PWM BIT(20) /* Supports four fan controls */
314 #define FEAT_BANK_SEL BIT(21) /* Chip has multi-bank support */
315 #define FEAT_SCALING BIT(22) /* Internal voltage scaling */
317 static const struct it87_devices it87_devices[] = {
321 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
326 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
327 /* may need to overwrite */
332 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
333 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
338 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
339 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
341 .old_peci_mask = 0x4,
346 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
347 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
349 .old_peci_mask = 0x4,
354 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
355 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
356 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
357 | FEAT_PWM_FREQ2 | FEAT_SCALING,
359 .old_peci_mask = 0x02, /* Actually reports PCH */
364 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
365 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
366 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2 | FEAT_SCALING,
372 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
373 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
374 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL | FEAT_FOUR_FANS
377 .old_peci_mask = 0x02, /* Actually reports PCH */
382 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
383 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
384 | FEAT_PWM_FREQ2 | FEAT_SCALING,
385 /* PECI: guesswork */
387 /* 16 bit fans (OHM) */
388 /* three fans, always 16 bit (guesswork) */
394 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
395 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
396 | FEAT_PWM_FREQ2 | FEAT_SCALING,
397 /* PECI (coreboot) */
398 /* 12mV ADC (HWSensors4, OHM) */
399 /* 16 bit fans (HWSensors4, OHM) */
400 /* three fans, always 16 bit (datasheet) */
406 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
407 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
408 .old_peci_mask = 0x4,
413 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
414 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
415 .old_peci_mask = 0x4,
420 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
421 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
422 .old_peci_mask = 0x4,
427 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
428 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
435 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
436 | FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI
437 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
443 .features = FEAT_NEWER_AUTOPWM | FEAT_10_9MV_ADC | FEAT_SCALING
444 | FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI
445 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
451 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
452 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
453 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
459 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
460 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
461 | FEAT_AVCC3 | FEAT_PWM_FREQ2 | FEAT_SCALING,
467 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
468 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
469 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
470 | FEAT_SIX_TEMP | FEAT_VIN3_5V | FEAT_SCALING,
476 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
477 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
478 | FEAT_FIVE_PWM | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2
479 | FEAT_AVCC3 | FEAT_VIN3_5V | FEAT_SCALING,
485 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
486 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
487 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
488 | FEAT_SIX_TEMP | FEAT_SCALING,
494 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
495 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
496 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
497 | FEAT_SIX_TEMP | FEAT_BANK_SEL | FEAT_SCALING,
502 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
503 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
504 #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
505 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
506 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
507 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
508 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
509 ((data)->peci_mask & BIT(nr)))
510 #define has_temp_old_peci(data, nr) \
511 (((data)->features & FEAT_TEMP_OLD_PECI) && \
512 ((data)->old_peci_mask & BIT(nr)))
513 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
514 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
516 #define has_vid(data) ((data)->features & FEAT_VID)
517 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
518 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
519 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
520 #define has_five_pwm(data) ((data)->features & (FEAT_FIVE_PWM \
522 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
523 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
524 #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
525 #define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
526 #define has_four_fans(data) ((data)->features & (FEAT_FOUR_FANS | \
529 #define has_four_pwm(data) ((data)->features & (FEAT_FOUR_PWM | \
532 #define has_bank_sel(data) ((data)->features & FEAT_BANK_SEL)
533 #define has_scaling(data) ((data)->features & FEAT_SCALING)
535 struct it87_sio_data {
537 /* Values read from Super-I/O config space */
541 u8 internal; /* Internal sensors can be labeled */
542 /* Features skipped based on config or DMI */
551 * For each registered chip, we need to keep some data in memory.
552 * The structure is dynamically allocated.
555 const struct attribute_group *groups[7];
564 struct mutex update_lock;
565 char valid; /* !=0 if following fields are valid */
566 unsigned long last_updated; /* In jiffies */
568 u16 in_scaled; /* Internal voltage sensors are scaled */
569 u16 in_internal; /* Bitfield, internal sensors (for labels) */
570 u16 has_in; /* Bitfield, voltage sensors enabled */
571 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
572 u8 has_fan; /* Bitfield, fans enabled */
573 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
574 u8 has_temp; /* Bitfield, temp sensors enabled */
575 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
576 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
577 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
578 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
579 bool has_vid; /* True if VID supported */
580 u8 vid; /* Register encoding, combined */
582 u32 alarms; /* Register encoding, combined */
583 bool has_beep; /* true if beep supported */
584 u8 beeps; /* Register encoding */
585 u8 fan_main_ctrl; /* Register value */
586 u8 fan_ctl; /* Register value */
589 * The following 3 arrays correspond to the same registers up to
590 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
591 * 7, and we want to preserve settings on mode changes, so we have
592 * to track all values separately.
593 * Starting with the IT8721F, the manual PWM duty cycles are stored
594 * in separate registers (8-bit values), so the separate tracking
595 * is no longer needed, but it is still done to keep the driver
598 u8 has_pwm; /* Bitfield, pwm control enabled */
599 u8 pwm_ctrl[NUM_PWM]; /* Register value */
600 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
601 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
603 /* Automatic fan speed control registers */
604 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
605 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
608 static int adc_lsb(const struct it87_data *data, int nr)
612 if (has_12mv_adc(data))
614 else if (has_10_9mv_adc(data))
618 if (data->in_scaled & BIT(nr))
623 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
625 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
626 return clamp_val(val, 0, 255);
629 static int in_from_reg(const struct it87_data *data, int nr, int val)
631 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
634 static inline u8 FAN_TO_REG(long rpm, int div)
638 rpm = clamp_val(rpm, 1, 1000000);
639 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
642 static inline u16 FAN16_TO_REG(long rpm)
646 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
649 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
650 1350000 / ((val) * (div)))
651 /* The divider is fixed to 2 in 16-bit mode */
652 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
653 1350000 / ((val) * 2))
655 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
656 ((val) + 500) / 1000), -128, 127))
657 #define TEMP_FROM_REG(val) ((val) * 1000)
659 static u8 pwm_to_reg(const struct it87_data *data, long val)
661 if (has_newer_autopwm(data))
667 static int pwm_from_reg(const struct it87_data *data, u8 reg)
669 if (has_newer_autopwm(data))
672 return (reg & 0x7f) << 1;
675 static int DIV_TO_REG(int val)
679 while (answer < 7 && (val >>= 1))
684 #define DIV_FROM_REG(val) BIT(val)
687 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
688 * depending on the chip type, to calculate the actual PWM frequency.
690 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
691 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
692 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
693 * sometimes just one. It is unknown if this is a datasheet error or real,
694 * so this is ignored for now.
696 static const unsigned int pwm_freq[8] = {
707 static int _it87_read_value(struct it87_data *data, u8 reg)
709 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
710 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
713 static void _it87_write_value(struct it87_data *data, u8 reg, u8 value)
715 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
716 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
719 static void it87_set_bank(struct it87_data *data, u8 bank)
721 if (has_bank_sel(data) && bank != data->bank) {
722 u8 breg = _it87_read_value(data, IT87_REG_BANK);
727 _it87_write_value(data, IT87_REG_BANK, breg);
732 * Must be called with data->update_lock held, except during initialization.
733 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
734 * would slow down the IT87 access and should not be necessary.
736 static int it87_read_value(struct it87_data *data, u16 reg)
738 it87_set_bank(data, reg >> 8);
739 return _it87_read_value(data, reg & 0xff);
743 * Must be called with data->update_lock held, except during initialization.
744 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
745 * would slow down the IT87 access and should not be necessary.
747 static void it87_write_value(struct it87_data *data, u16 reg, u8 value)
749 it87_set_bank(data, reg >> 8);
750 _it87_write_value(data, reg & 0xff, value);
753 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
755 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
756 if (has_newer_autopwm(data)) {
757 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
758 data->pwm_duty[nr] = it87_read_value(data,
759 IT87_REG_PWM_DUTY[nr]);
761 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
762 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
763 else /* Manual mode */
764 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
767 if (has_old_autopwm(data)) {
770 for (i = 0; i < 5 ; i++)
771 data->auto_temp[nr][i] = it87_read_value(data,
772 IT87_REG_AUTO_TEMP(nr, i));
773 for (i = 0; i < 3 ; i++)
774 data->auto_pwm[nr][i] = it87_read_value(data,
775 IT87_REG_AUTO_PWM(nr, i));
776 } else if (has_newer_autopwm(data)) {
780 * 0: temperature hysteresis (base + 5)
781 * 1: fan off temperature (base + 0)
782 * 2: fan start temperature (base + 1)
783 * 3: fan max temperature (base + 2)
785 data->auto_temp[nr][0] =
786 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
788 for (i = 0; i < 3 ; i++)
789 data->auto_temp[nr][i + 1] =
790 it87_read_value(data,
791 IT87_REG_AUTO_TEMP(nr, i));
793 * 0: start pwm value (base + 3)
794 * 1: pwm slope (base + 4, 1/8th pwm)
796 data->auto_pwm[nr][0] =
797 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
798 data->auto_pwm[nr][1] =
799 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
803 static struct it87_data *it87_update_device(struct device *dev)
805 struct it87_data *data = dev_get_drvdata(dev);
808 mutex_lock(&data->update_lock);
810 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
814 * Cleared after each update, so reenable. Value
815 * returned by this read will be previous value
817 it87_write_value(data, IT87_REG_CONFIG,
818 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
820 for (i = 0; i < NUM_VIN; i++) {
821 if (!(data->has_in & BIT(i)))
825 it87_read_value(data, IT87_REG_VIN[i]);
827 /* VBAT and AVCC don't have limit registers */
828 if (i >= NUM_VIN_LIMIT)
832 it87_read_value(data, IT87_REG_VIN_MIN(i));
834 it87_read_value(data, IT87_REG_VIN_MAX(i));
837 for (i = 0; i < NUM_FAN; i++) {
838 /* Skip disabled fans */
839 if (!(data->has_fan & BIT(i)))
843 it87_read_value(data, IT87_REG_FAN_MIN[i]);
844 data->fan[i][0] = it87_read_value(data,
846 /* Add high byte if in 16-bit mode */
847 if (has_16bit_fans(data)) {
848 data->fan[i][0] |= it87_read_value(data,
849 IT87_REG_FANX[i]) << 8;
850 data->fan[i][1] |= it87_read_value(data,
851 IT87_REG_FANX_MIN[i]) << 8;
854 for (i = 0; i < NUM_TEMP; i++) {
855 if (!(data->has_temp & BIT(i)))
858 it87_read_value(data, IT87_REG_TEMP(i));
860 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
862 it87_read_value(data,
863 IT87_REG_TEMP_OFFSET[i]);
865 if (i >= NUM_TEMP_LIMIT)
869 it87_read_value(data, IT87_REG_TEMP_LOW(i));
871 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
874 /* Newer chips don't have clock dividers */
875 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
876 i = it87_read_value(data, IT87_REG_FAN_DIV);
877 data->fan_div[0] = i & 0x07;
878 data->fan_div[1] = (i >> 3) & 0x07;
879 data->fan_div[2] = (i & 0x40) ? 3 : 1;
883 it87_read_value(data, IT87_REG_ALARM1) |
884 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
885 (it87_read_value(data, IT87_REG_ALARM3) << 16);
886 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
888 data->fan_main_ctrl = it87_read_value(data,
889 IT87_REG_FAN_MAIN_CTRL);
890 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
891 for (i = 0; i < NUM_PWM; i++) {
892 if (!(data->has_pwm & BIT(i)))
894 it87_update_pwm_ctrl(data, i);
897 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
898 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
900 * The IT8705F does not have VID capability.
901 * The IT8718F and later don't use IT87_REG_VID for the
904 if (data->type == it8712 || data->type == it8716) {
905 data->vid = it87_read_value(data, IT87_REG_VID);
907 * The older IT8712F revisions had only 5 VID pins,
908 * but we assume it is always safe to read 6 bits.
912 data->last_updated = jiffies;
916 mutex_unlock(&data->update_lock);
921 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
924 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
925 struct it87_data *data = it87_update_device(dev);
926 int index = sattr->index;
929 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
932 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
933 const char *buf, size_t count)
935 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
936 struct it87_data *data = dev_get_drvdata(dev);
937 int index = sattr->index;
941 if (kstrtoul(buf, 10, &val) < 0)
944 mutex_lock(&data->update_lock);
945 data->in[nr][index] = in_to_reg(data, nr, val);
946 it87_write_value(data,
947 index == 1 ? IT87_REG_VIN_MIN(nr)
948 : IT87_REG_VIN_MAX(nr),
949 data->in[nr][index]);
950 mutex_unlock(&data->update_lock);
954 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
955 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
957 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
960 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
961 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
963 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
966 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
967 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
969 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
972 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
973 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
975 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
978 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
979 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
981 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
984 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
985 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
987 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
990 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
991 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
993 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
996 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
997 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
999 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
1002 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
1003 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
1004 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
1005 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
1006 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
1008 /* Up to 6 temperatures */
1009 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
1012 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1014 int index = sattr->index;
1015 struct it87_data *data = it87_update_device(dev);
1017 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
1020 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
1021 const char *buf, size_t count)
1023 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1025 int index = sattr->index;
1026 struct it87_data *data = dev_get_drvdata(dev);
1030 if (kstrtol(buf, 10, &val) < 0)
1033 mutex_lock(&data->update_lock);
1038 reg = IT87_REG_TEMP_LOW(nr);
1041 reg = IT87_REG_TEMP_HIGH(nr);
1044 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1045 if (!(regval & 0x80)) {
1047 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
1050 reg = IT87_REG_TEMP_OFFSET[nr];
1054 data->temp[nr][index] = TEMP_TO_REG(val);
1055 it87_write_value(data, reg, data->temp[nr][index]);
1056 mutex_unlock(&data->update_lock);
1060 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
1061 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1063 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1065 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
1067 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
1068 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1070 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1072 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
1074 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
1075 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
1077 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
1079 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
1081 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
1082 static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
1083 static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
1085 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1088 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1089 int nr = sensor_attr->index;
1090 struct it87_data *data = it87_update_device(dev);
1091 u8 reg = data->sensor; /* In case value is updated while used */
1092 u8 extra = data->extra;
1094 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
1095 (has_temp_old_peci(data, nr) && (extra & 0x80)))
1096 return sprintf(buf, "6\n"); /* Intel PECI */
1097 if (reg & (1 << nr))
1098 return sprintf(buf, "3\n"); /* thermal diode */
1099 if (reg & (8 << nr))
1100 return sprintf(buf, "4\n"); /* thermistor */
1101 return sprintf(buf, "0\n"); /* disabled */
1104 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1105 const char *buf, size_t count)
1107 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1108 int nr = sensor_attr->index;
1110 struct it87_data *data = dev_get_drvdata(dev);
1114 if (kstrtol(buf, 10, &val) < 0)
1117 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1120 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1122 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1123 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1125 if (val == 2) { /* backwards compatibility */
1127 "Sensor type 2 is deprecated, please use 4 instead\n");
1130 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1135 else if (has_temp_peci(data, nr) && val == 6)
1136 reg |= (nr + 1) << 6;
1137 else if (has_temp_old_peci(data, nr) && val == 6)
1142 mutex_lock(&data->update_lock);
1144 data->extra = extra;
1145 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
1146 if (has_temp_old_peci(data, nr))
1147 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1148 data->valid = 0; /* Force cache refresh */
1149 mutex_unlock(&data->update_lock);
1153 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1155 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1157 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1162 static int pwm_mode(const struct it87_data *data, int nr)
1164 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1165 return 0; /* Full speed */
1166 if (data->pwm_ctrl[nr] & 0x80)
1167 return 2; /* Automatic mode */
1168 if ((data->type == it8603 || nr >= 3) &&
1169 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1170 return 0; /* Full speed */
1172 return 1; /* Manual mode */
1175 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1178 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1180 int index = sattr->index;
1182 struct it87_data *data = it87_update_device(dev);
1184 speed = has_16bit_fans(data) ?
1185 FAN16_FROM_REG(data->fan[nr][index]) :
1186 FAN_FROM_REG(data->fan[nr][index],
1187 DIV_FROM_REG(data->fan_div[nr]));
1188 return sprintf(buf, "%d\n", speed);
1191 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1194 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1195 struct it87_data *data = it87_update_device(dev);
1196 int nr = sensor_attr->index;
1198 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1201 static ssize_t show_pwm_enable(struct device *dev,
1202 struct device_attribute *attr, char *buf)
1204 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1205 struct it87_data *data = it87_update_device(dev);
1206 int nr = sensor_attr->index;
1208 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1211 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1214 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1215 struct it87_data *data = it87_update_device(dev);
1216 int nr = sensor_attr->index;
1218 return sprintf(buf, "%d\n",
1219 pwm_from_reg(data, data->pwm_duty[nr]));
1222 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1225 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1226 struct it87_data *data = it87_update_device(dev);
1227 int nr = sensor_attr->index;
1231 if (has_pwm_freq2(data) && nr == 1)
1232 index = (data->extra >> 4) & 0x07;
1234 index = (data->fan_ctl >> 4) & 0x07;
1236 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1238 return sprintf(buf, "%u\n", freq);
1241 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1242 const char *buf, size_t count)
1244 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1246 int index = sattr->index;
1248 struct it87_data *data = dev_get_drvdata(dev);
1252 if (kstrtol(buf, 10, &val) < 0)
1255 mutex_lock(&data->update_lock);
1257 if (has_16bit_fans(data)) {
1258 data->fan[nr][index] = FAN16_TO_REG(val);
1259 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1260 data->fan[nr][index] & 0xff);
1261 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1262 data->fan[nr][index] >> 8);
1264 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1267 data->fan_div[nr] = reg & 0x07;
1270 data->fan_div[nr] = (reg >> 3) & 0x07;
1273 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1276 data->fan[nr][index] =
1277 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1278 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1279 data->fan[nr][index]);
1282 mutex_unlock(&data->update_lock);
1286 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1287 const char *buf, size_t count)
1289 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1290 struct it87_data *data = dev_get_drvdata(dev);
1291 int nr = sensor_attr->index;
1296 if (kstrtoul(buf, 10, &val) < 0)
1299 mutex_lock(&data->update_lock);
1300 old = it87_read_value(data, IT87_REG_FAN_DIV);
1302 /* Save fan min limit */
1303 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1308 data->fan_div[nr] = DIV_TO_REG(val);
1312 data->fan_div[nr] = 1;
1314 data->fan_div[nr] = 3;
1317 val |= (data->fan_div[0] & 0x07);
1318 val |= (data->fan_div[1] & 0x07) << 3;
1319 if (data->fan_div[2] == 3)
1321 it87_write_value(data, IT87_REG_FAN_DIV, val);
1323 /* Restore fan min limit */
1324 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1325 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
1327 mutex_unlock(&data->update_lock);
1331 /* Returns 0 if OK, -EINVAL otherwise */
1332 static int check_trip_points(struct device *dev, int nr)
1334 const struct it87_data *data = dev_get_drvdata(dev);
1337 if (has_old_autopwm(data)) {
1338 for (i = 0; i < 3; i++) {
1339 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1342 for (i = 0; i < 2; i++) {
1343 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1346 } else if (has_newer_autopwm(data)) {
1347 for (i = 1; i < 3; i++) {
1348 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1355 "Inconsistent trip points, not switching to automatic mode\n");
1356 dev_err(dev, "Adjust the trip points and try again\n");
1361 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1362 const char *buf, size_t count)
1364 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1365 struct it87_data *data = dev_get_drvdata(dev);
1366 int nr = sensor_attr->index;
1369 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1372 /* Check trip points before switching to automatic mode */
1374 if (check_trip_points(dev, nr) < 0)
1378 mutex_lock(&data->update_lock);
1381 if (nr < 3 && data->type != it8603) {
1383 /* make sure the fan is on when in on/off mode */
1384 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1385 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1386 /* set on/off mode */
1387 data->fan_main_ctrl &= ~BIT(nr);
1388 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1389 data->fan_main_ctrl);
1393 /* No on/off mode, set maximum pwm value */
1394 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1395 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1396 data->pwm_duty[nr]);
1397 /* and set manual mode */
1398 if (has_newer_autopwm(data)) {
1399 ctrl = (data->pwm_ctrl[nr] & 0x7c) |
1400 data->pwm_temp_map[nr];
1402 ctrl = data->pwm_duty[nr];
1404 data->pwm_ctrl[nr] = ctrl;
1405 it87_write_value(data, IT87_REG_PWM[nr], ctrl);
1410 if (has_newer_autopwm(data)) {
1411 ctrl = (data->pwm_ctrl[nr] & 0x7c) |
1412 data->pwm_temp_map[nr];
1416 ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
1418 data->pwm_ctrl[nr] = ctrl;
1419 it87_write_value(data, IT87_REG_PWM[nr], ctrl);
1421 if (data->type != it8603 && nr < 3) {
1422 /* set SmartGuardian mode */
1423 data->fan_main_ctrl |= BIT(nr);
1424 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1425 data->fan_main_ctrl);
1429 mutex_unlock(&data->update_lock);
1433 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1434 const char *buf, size_t count)
1436 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1437 struct it87_data *data = dev_get_drvdata(dev);
1438 int nr = sensor_attr->index;
1441 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1444 mutex_lock(&data->update_lock);
1445 it87_update_pwm_ctrl(data, nr);
1446 if (has_newer_autopwm(data)) {
1448 * If we are in automatic mode, the PWM duty cycle register
1449 * is read-only so we can't write the value.
1451 if (data->pwm_ctrl[nr] & 0x80) {
1452 mutex_unlock(&data->update_lock);
1455 data->pwm_duty[nr] = pwm_to_reg(data, val);
1456 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1457 data->pwm_duty[nr]);
1459 data->pwm_duty[nr] = pwm_to_reg(data, val);
1461 * If we are in manual mode, write the duty cycle immediately;
1462 * otherwise, just store it for later use.
1464 if (!(data->pwm_ctrl[nr] & 0x80)) {
1465 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1466 it87_write_value(data, IT87_REG_PWM[nr],
1467 data->pwm_ctrl[nr]);
1470 mutex_unlock(&data->update_lock);
1474 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1475 const char *buf, size_t count)
1477 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1478 struct it87_data *data = dev_get_drvdata(dev);
1479 int nr = sensor_attr->index;
1483 if (kstrtoul(buf, 10, &val) < 0)
1486 val = clamp_val(val, 0, 1000000);
1487 val *= has_newer_autopwm(data) ? 256 : 128;
1489 /* Search for the nearest available frequency */
1490 for (i = 0; i < 7; i++) {
1491 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1495 mutex_lock(&data->update_lock);
1497 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1498 data->fan_ctl |= i << 4;
1499 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1501 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1502 data->extra |= i << 4;
1503 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1505 mutex_unlock(&data->update_lock);
1510 static ssize_t show_pwm_temp_map(struct device *dev,
1511 struct device_attribute *attr, char *buf)
1513 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1514 struct it87_data *data = it87_update_device(dev);
1515 int nr = sensor_attr->index;
1518 map = data->pwm_temp_map[nr];
1520 map = 0; /* Should never happen */
1521 if (nr >= 3) /* pwm channels 3..6 map to temp4..6 */
1524 return sprintf(buf, "%d\n", (int)BIT(map));
1527 static ssize_t set_pwm_temp_map(struct device *dev,
1528 struct device_attribute *attr, const char *buf,
1531 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1532 struct it87_data *data = dev_get_drvdata(dev);
1533 int nr = sensor_attr->index;
1537 if (kstrtol(buf, 10, &val) < 0)
1557 mutex_lock(&data->update_lock);
1558 it87_update_pwm_ctrl(data, nr);
1559 data->pwm_temp_map[nr] = reg;
1561 * If we are in automatic mode, write the temp mapping immediately;
1562 * otherwise, just store it for later use.
1564 if (data->pwm_ctrl[nr] & 0x80) {
1565 data->pwm_ctrl[nr] = (data->pwm_ctrl[nr] & 0xfc) |
1566 data->pwm_temp_map[nr];
1567 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1569 mutex_unlock(&data->update_lock);
1573 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1576 struct it87_data *data = it87_update_device(dev);
1577 struct sensor_device_attribute_2 *sensor_attr =
1578 to_sensor_dev_attr_2(attr);
1579 int nr = sensor_attr->nr;
1580 int point = sensor_attr->index;
1582 return sprintf(buf, "%d\n",
1583 pwm_from_reg(data, data->auto_pwm[nr][point]));
1586 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1587 const char *buf, size_t count)
1589 struct it87_data *data = dev_get_drvdata(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;
1597 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1600 mutex_lock(&data->update_lock);
1601 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1602 if (has_newer_autopwm(data))
1603 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1605 regaddr = IT87_REG_AUTO_PWM(nr, point);
1606 it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1607 mutex_unlock(&data->update_lock);
1611 static ssize_t show_auto_pwm_slope(struct device *dev,
1612 struct device_attribute *attr, char *buf)
1614 struct it87_data *data = it87_update_device(dev);
1615 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1616 int nr = sensor_attr->index;
1618 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1621 static ssize_t set_auto_pwm_slope(struct device *dev,
1622 struct device_attribute *attr,
1623 const char *buf, size_t count)
1625 struct it87_data *data = dev_get_drvdata(dev);
1626 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1627 int nr = sensor_attr->index;
1630 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1633 mutex_lock(&data->update_lock);
1634 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1635 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1636 data->auto_pwm[nr][1]);
1637 mutex_unlock(&data->update_lock);
1641 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1644 struct it87_data *data = it87_update_device(dev);
1645 struct sensor_device_attribute_2 *sensor_attr =
1646 to_sensor_dev_attr_2(attr);
1647 int nr = sensor_attr->nr;
1648 int point = sensor_attr->index;
1651 if (has_old_autopwm(data) || point)
1652 reg = data->auto_temp[nr][point];
1654 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1656 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
1659 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1660 const char *buf, size_t count)
1662 struct it87_data *data = dev_get_drvdata(dev);
1663 struct sensor_device_attribute_2 *sensor_attr =
1664 to_sensor_dev_attr_2(attr);
1665 int nr = sensor_attr->nr;
1666 int point = sensor_attr->index;
1670 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1673 mutex_lock(&data->update_lock);
1674 if (has_newer_autopwm(data) && !point) {
1675 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1676 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1677 data->auto_temp[nr][0] = reg;
1678 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1680 reg = TEMP_TO_REG(val);
1681 data->auto_temp[nr][point] = reg;
1682 if (has_newer_autopwm(data))
1684 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1686 mutex_unlock(&data->update_lock);
1690 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1691 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1693 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1696 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1697 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1699 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1702 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1703 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1705 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1708 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1709 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1712 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1713 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1716 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1717 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1720 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1721 show_pwm_enable, set_pwm_enable, 0);
1722 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1723 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1725 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
1726 show_pwm_temp_map, set_pwm_temp_map, 0);
1727 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1728 show_auto_pwm, set_auto_pwm, 0, 0);
1729 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1730 show_auto_pwm, set_auto_pwm, 0, 1);
1731 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1732 show_auto_pwm, set_auto_pwm, 0, 2);
1733 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1734 show_auto_pwm, NULL, 0, 3);
1735 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1736 show_auto_temp, set_auto_temp, 0, 1);
1737 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1738 show_auto_temp, set_auto_temp, 0, 0);
1739 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1740 show_auto_temp, set_auto_temp, 0, 2);
1741 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1742 show_auto_temp, set_auto_temp, 0, 3);
1743 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1744 show_auto_temp, set_auto_temp, 0, 4);
1745 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1746 show_auto_pwm, set_auto_pwm, 0, 0);
1747 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1748 show_auto_pwm_slope, set_auto_pwm_slope, 0);
1750 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1751 show_pwm_enable, set_pwm_enable, 1);
1752 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1753 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1754 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
1755 show_pwm_temp_map, set_pwm_temp_map, 1);
1756 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1757 show_auto_pwm, set_auto_pwm, 1, 0);
1758 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1759 show_auto_pwm, set_auto_pwm, 1, 1);
1760 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1761 show_auto_pwm, set_auto_pwm, 1, 2);
1762 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1763 show_auto_pwm, NULL, 1, 3);
1764 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1765 show_auto_temp, set_auto_temp, 1, 1);
1766 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1767 show_auto_temp, set_auto_temp, 1, 0);
1768 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1769 show_auto_temp, set_auto_temp, 1, 2);
1770 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1771 show_auto_temp, set_auto_temp, 1, 3);
1772 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1773 show_auto_temp, set_auto_temp, 1, 4);
1774 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1775 show_auto_pwm, set_auto_pwm, 1, 0);
1776 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1777 show_auto_pwm_slope, set_auto_pwm_slope, 1);
1779 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1780 show_pwm_enable, set_pwm_enable, 2);
1781 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1782 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1783 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
1784 show_pwm_temp_map, set_pwm_temp_map, 2);
1785 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1786 show_auto_pwm, set_auto_pwm, 2, 0);
1787 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1788 show_auto_pwm, set_auto_pwm, 2, 1);
1789 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1790 show_auto_pwm, set_auto_pwm, 2, 2);
1791 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1792 show_auto_pwm, NULL, 2, 3);
1793 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1794 show_auto_temp, set_auto_temp, 2, 1);
1795 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1796 show_auto_temp, set_auto_temp, 2, 0);
1797 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1798 show_auto_temp, set_auto_temp, 2, 2);
1799 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1800 show_auto_temp, set_auto_temp, 2, 3);
1801 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1802 show_auto_temp, set_auto_temp, 2, 4);
1803 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1804 show_auto_pwm, set_auto_pwm, 2, 0);
1805 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1806 show_auto_pwm_slope, set_auto_pwm_slope, 2);
1808 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1809 show_pwm_enable, set_pwm_enable, 3);
1810 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1811 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1812 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
1813 show_pwm_temp_map, set_pwm_temp_map, 3);
1814 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1815 show_auto_temp, set_auto_temp, 2, 1);
1816 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1817 show_auto_temp, set_auto_temp, 2, 0);
1818 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1819 show_auto_temp, set_auto_temp, 2, 2);
1820 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1821 show_auto_temp, set_auto_temp, 2, 3);
1822 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1823 show_auto_pwm, set_auto_pwm, 3, 0);
1824 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1825 show_auto_pwm_slope, set_auto_pwm_slope, 3);
1827 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1828 show_pwm_enable, set_pwm_enable, 4);
1829 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1830 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1831 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
1832 show_pwm_temp_map, set_pwm_temp_map, 4);
1833 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1834 show_auto_temp, set_auto_temp, 2, 1);
1835 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1836 show_auto_temp, set_auto_temp, 2, 0);
1837 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1838 show_auto_temp, set_auto_temp, 2, 2);
1839 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1840 show_auto_temp, set_auto_temp, 2, 3);
1841 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1842 show_auto_pwm, set_auto_pwm, 4, 0);
1843 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1844 show_auto_pwm_slope, set_auto_pwm_slope, 4);
1846 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1847 show_pwm_enable, set_pwm_enable, 5);
1848 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1849 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1850 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
1851 show_pwm_temp_map, set_pwm_temp_map, 5);
1852 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1853 show_auto_temp, set_auto_temp, 2, 1);
1854 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1855 show_auto_temp, set_auto_temp, 2, 0);
1856 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1857 show_auto_temp, set_auto_temp, 2, 2);
1858 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1859 show_auto_temp, set_auto_temp, 2, 3);
1860 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1861 show_auto_pwm, set_auto_pwm, 5, 0);
1862 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1863 show_auto_pwm_slope, set_auto_pwm_slope, 5);
1866 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1869 struct it87_data *data = it87_update_device(dev);
1871 return sprintf(buf, "%u\n", data->alarms);
1873 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1875 static ssize_t show_alarm(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->alarms >> bitnr) & 1);
1884 static ssize_t clear_intrusion(struct device *dev,
1885 struct device_attribute *attr, const char *buf,
1888 struct it87_data *data = dev_get_drvdata(dev);
1892 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1895 mutex_lock(&data->update_lock);
1896 config = it87_read_value(data, IT87_REG_CONFIG);
1901 it87_write_value(data, IT87_REG_CONFIG, config);
1902 /* Invalidate cache to force re-read */
1905 mutex_unlock(&data->update_lock);
1910 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1911 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1912 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1913 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1914 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1915 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1916 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1917 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1918 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1919 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1920 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1921 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1922 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1923 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1924 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1925 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1926 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1927 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1928 show_alarm, clear_intrusion, 4);
1930 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1933 struct it87_data *data = it87_update_device(dev);
1934 int bitnr = to_sensor_dev_attr(attr)->index;
1936 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1939 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1940 const char *buf, size_t count)
1942 int bitnr = to_sensor_dev_attr(attr)->index;
1943 struct it87_data *data = dev_get_drvdata(dev);
1946 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
1949 mutex_lock(&data->update_lock);
1950 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1952 data->beeps |= BIT(bitnr);
1954 data->beeps &= ~BIT(bitnr);
1955 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1956 mutex_unlock(&data->update_lock);
1960 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1961 show_beep, set_beep, 1);
1962 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1963 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1964 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1965 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1966 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1967 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1968 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1969 /* fanX_beep writability is set later */
1970 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1971 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1972 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1973 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1974 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1975 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1976 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1977 show_beep, set_beep, 2);
1978 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1979 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1981 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1984 struct it87_data *data = dev_get_drvdata(dev);
1986 return sprintf(buf, "%u\n", data->vrm);
1989 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1990 const char *buf, size_t count)
1992 struct it87_data *data = dev_get_drvdata(dev);
1995 if (kstrtoul(buf, 10, &val) < 0)
2002 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
2004 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
2007 struct it87_data *data = it87_update_device(dev);
2009 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
2011 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
2013 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
2016 static const char * const labels[] = {
2022 static const char * const labels_it8721[] = {
2028 struct it87_data *data = dev_get_drvdata(dev);
2029 int nr = to_sensor_dev_attr(attr)->index;
2032 if (has_vin3_5v(data) && nr == 0)
2034 else if (has_12mv_adc(data) || has_10_9mv_adc(data))
2035 label = labels_it8721[nr];
2039 return sprintf(buf, "%s\n", label);
2041 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
2042 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
2043 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
2045 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 3);
2047 static umode_t it87_in_is_visible(struct kobject *kobj,
2048 struct attribute *attr, int index)
2050 struct device *dev = container_of(kobj, struct device, kobj);
2051 struct it87_data *data = dev_get_drvdata(dev);
2052 int i = index / 5; /* voltage index */
2053 int a = index % 5; /* attribute index */
2055 if (index >= 40) { /* in8 and higher only have input attributes */
2060 if (!(data->has_in & BIT(i)))
2063 if (a == 4 && !data->has_beep)
2069 static struct attribute *it87_attributes_in[] = {
2070 &sensor_dev_attr_in0_input.dev_attr.attr,
2071 &sensor_dev_attr_in0_min.dev_attr.attr,
2072 &sensor_dev_attr_in0_max.dev_attr.attr,
2073 &sensor_dev_attr_in0_alarm.dev_attr.attr,
2074 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
2076 &sensor_dev_attr_in1_input.dev_attr.attr,
2077 &sensor_dev_attr_in1_min.dev_attr.attr,
2078 &sensor_dev_attr_in1_max.dev_attr.attr,
2079 &sensor_dev_attr_in1_alarm.dev_attr.attr,
2080 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
2082 &sensor_dev_attr_in2_input.dev_attr.attr,
2083 &sensor_dev_attr_in2_min.dev_attr.attr,
2084 &sensor_dev_attr_in2_max.dev_attr.attr,
2085 &sensor_dev_attr_in2_alarm.dev_attr.attr,
2086 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
2088 &sensor_dev_attr_in3_input.dev_attr.attr,
2089 &sensor_dev_attr_in3_min.dev_attr.attr,
2090 &sensor_dev_attr_in3_max.dev_attr.attr,
2091 &sensor_dev_attr_in3_alarm.dev_attr.attr,
2092 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
2094 &sensor_dev_attr_in4_input.dev_attr.attr,
2095 &sensor_dev_attr_in4_min.dev_attr.attr,
2096 &sensor_dev_attr_in4_max.dev_attr.attr,
2097 &sensor_dev_attr_in4_alarm.dev_attr.attr,
2098 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2100 &sensor_dev_attr_in5_input.dev_attr.attr,
2101 &sensor_dev_attr_in5_min.dev_attr.attr,
2102 &sensor_dev_attr_in5_max.dev_attr.attr,
2103 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2104 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2106 &sensor_dev_attr_in6_input.dev_attr.attr,
2107 &sensor_dev_attr_in6_min.dev_attr.attr,
2108 &sensor_dev_attr_in6_max.dev_attr.attr,
2109 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2110 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2112 &sensor_dev_attr_in7_input.dev_attr.attr,
2113 &sensor_dev_attr_in7_min.dev_attr.attr,
2114 &sensor_dev_attr_in7_max.dev_attr.attr,
2115 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2116 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2118 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2119 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
2120 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
2121 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
2122 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
2126 static const struct attribute_group it87_group_in = {
2127 .attrs = it87_attributes_in,
2128 .is_visible = it87_in_is_visible,
2131 static umode_t it87_temp_is_visible(struct kobject *kobj,
2132 struct attribute *attr, int index)
2134 struct device *dev = container_of(kobj, struct device, kobj);
2135 struct it87_data *data = dev_get_drvdata(dev);
2136 int i = index / 7; /* temperature index */
2137 int a = index % 7; /* attribute index */
2144 if (!(data->has_temp & BIT(i)))
2147 if (a == 5 && !has_temp_offset(data))
2150 if (a == 6 && !data->has_beep)
2156 static struct attribute *it87_attributes_temp[] = {
2157 &sensor_dev_attr_temp1_input.dev_attr.attr,
2158 &sensor_dev_attr_temp1_max.dev_attr.attr,
2159 &sensor_dev_attr_temp1_min.dev_attr.attr,
2160 &sensor_dev_attr_temp1_type.dev_attr.attr,
2161 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2162 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2163 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2165 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2166 &sensor_dev_attr_temp2_max.dev_attr.attr,
2167 &sensor_dev_attr_temp2_min.dev_attr.attr,
2168 &sensor_dev_attr_temp2_type.dev_attr.attr,
2169 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2170 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2171 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2173 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2174 &sensor_dev_attr_temp3_max.dev_attr.attr,
2175 &sensor_dev_attr_temp3_min.dev_attr.attr,
2176 &sensor_dev_attr_temp3_type.dev_attr.attr,
2177 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2178 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2179 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2181 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2182 &sensor_dev_attr_temp5_input.dev_attr.attr,
2183 &sensor_dev_attr_temp6_input.dev_attr.attr,
2187 static const struct attribute_group it87_group_temp = {
2188 .attrs = it87_attributes_temp,
2189 .is_visible = it87_temp_is_visible,
2192 static umode_t it87_is_visible(struct kobject *kobj,
2193 struct attribute *attr, int index)
2195 struct device *dev = container_of(kobj, struct device, kobj);
2196 struct it87_data *data = dev_get_drvdata(dev);
2198 if ((index == 2 || index == 3) && !data->has_vid)
2201 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2207 static struct attribute *it87_attributes[] = {
2208 &dev_attr_alarms.attr,
2209 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2210 &dev_attr_vrm.attr, /* 2 */
2211 &dev_attr_cpu0_vid.attr, /* 3 */
2212 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2213 &sensor_dev_attr_in7_label.dev_attr.attr,
2214 &sensor_dev_attr_in8_label.dev_attr.attr,
2215 &sensor_dev_attr_in9_label.dev_attr.attr,
2219 static const struct attribute_group it87_group = {
2220 .attrs = it87_attributes,
2221 .is_visible = it87_is_visible,
2224 static umode_t it87_fan_is_visible(struct kobject *kobj,
2225 struct attribute *attr, int index)
2227 struct device *dev = container_of(kobj, struct device, kobj);
2228 struct it87_data *data = dev_get_drvdata(dev);
2229 int i = index / 5; /* fan index */
2230 int a = index % 5; /* attribute index */
2232 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2233 i = (index - 15) / 4 + 3;
2234 a = (index - 15) % 4;
2237 if (!(data->has_fan & BIT(i)))
2240 if (a == 3) { /* beep */
2241 if (!data->has_beep)
2243 /* first fan beep attribute is writable */
2244 if (i == __ffs(data->has_fan))
2245 return attr->mode | S_IWUSR;
2248 if (a == 4 && has_16bit_fans(data)) /* divisor */
2254 static struct attribute *it87_attributes_fan[] = {
2255 &sensor_dev_attr_fan1_input.dev_attr.attr,
2256 &sensor_dev_attr_fan1_min.dev_attr.attr,
2257 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2258 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2259 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2261 &sensor_dev_attr_fan2_input.dev_attr.attr,
2262 &sensor_dev_attr_fan2_min.dev_attr.attr,
2263 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2264 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2265 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2267 &sensor_dev_attr_fan3_input.dev_attr.attr,
2268 &sensor_dev_attr_fan3_min.dev_attr.attr,
2269 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2270 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2271 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2273 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2274 &sensor_dev_attr_fan4_min.dev_attr.attr,
2275 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2276 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2278 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2279 &sensor_dev_attr_fan5_min.dev_attr.attr,
2280 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2281 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2283 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2284 &sensor_dev_attr_fan6_min.dev_attr.attr,
2285 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2286 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2290 static const struct attribute_group it87_group_fan = {
2291 .attrs = it87_attributes_fan,
2292 .is_visible = it87_fan_is_visible,
2295 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2296 struct attribute *attr, int index)
2298 struct device *dev = container_of(kobj, struct device, kobj);
2299 struct it87_data *data = dev_get_drvdata(dev);
2300 int i = index / 4; /* pwm index */
2301 int a = index % 4; /* attribute index */
2303 if (!(data->has_pwm & BIT(i)))
2306 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2307 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2308 return attr->mode | S_IWUSR;
2310 /* pwm2_freq is writable if there are two pwm frequency selects */
2311 if (has_pwm_freq2(data) && i == 1 && a == 2)
2312 return attr->mode | S_IWUSR;
2317 static struct attribute *it87_attributes_pwm[] = {
2318 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2319 &sensor_dev_attr_pwm1.dev_attr.attr,
2320 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2321 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2323 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2324 &sensor_dev_attr_pwm2.dev_attr.attr,
2325 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2326 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2328 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2329 &sensor_dev_attr_pwm3.dev_attr.attr,
2330 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2331 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2333 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2334 &sensor_dev_attr_pwm4.dev_attr.attr,
2335 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2336 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2338 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2339 &sensor_dev_attr_pwm5.dev_attr.attr,
2340 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2341 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2343 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2344 &sensor_dev_attr_pwm6.dev_attr.attr,
2345 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2346 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2351 static const struct attribute_group it87_group_pwm = {
2352 .attrs = it87_attributes_pwm,
2353 .is_visible = it87_pwm_is_visible,
2356 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2357 struct attribute *attr, int index)
2359 struct device *dev = container_of(kobj, struct device, kobj);
2360 struct it87_data *data = dev_get_drvdata(dev);
2361 int i = index / 11; /* pwm index */
2362 int a = index % 11; /* attribute index */
2364 if (index >= 33) { /* pwm 4..6 */
2365 i = (index - 33) / 6 + 3;
2366 a = (index - 33) % 6 + 4;
2369 if (!(data->has_pwm & BIT(i)))
2372 if (has_newer_autopwm(data)) {
2373 if (a < 4) /* no auto point pwm */
2375 if (a == 8) /* no auto_point4 */
2378 if (has_old_autopwm(data)) {
2379 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2386 static struct attribute *it87_attributes_auto_pwm[] = {
2387 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2388 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2389 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2390 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2391 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2392 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2393 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2394 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2395 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2396 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2397 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2399 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2400 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2401 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2402 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2403 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2404 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2405 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2406 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2407 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2408 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2409 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2411 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2412 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2413 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2414 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2415 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2416 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2417 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2418 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2419 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2420 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2421 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2423 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2424 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2425 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2426 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2427 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2428 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2430 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2431 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2432 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2433 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2434 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2435 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2437 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2438 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2439 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2440 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2441 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2442 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2447 static const struct attribute_group it87_group_auto_pwm = {
2448 .attrs = it87_attributes_auto_pwm,
2449 .is_visible = it87_auto_pwm_is_visible,
2452 /* SuperIO detection - will change isa_address if a chip is found */
2453 static int __init it87_find(int sioaddr, unsigned short *address,
2454 struct it87_sio_data *sio_data)
2458 const char *board_vendor, *board_name;
2459 const struct it87_devices *config;
2461 err = superio_enter(sioaddr);
2466 chip_type = superio_inw(sioaddr, DEVID);
2467 if (chip_type == 0xffff)
2471 chip_type = force_id;
2473 switch (chip_type) {
2475 sio_data->type = it87;
2478 sio_data->type = it8712;
2482 sio_data->type = it8716;
2485 sio_data->type = it8718;
2488 sio_data->type = it8720;
2491 sio_data->type = it8721;
2494 sio_data->type = it8728;
2497 sio_data->type = it8732;
2500 sio_data->type = it8792;
2503 sio_data->type = it8771;
2506 sio_data->type = it8772;
2509 sio_data->type = it8781;
2512 sio_data->type = it8782;
2515 sio_data->type = it8783;
2518 sio_data->type = it8786;
2521 sio_data->type = it8790;
2525 sio_data->type = it8603;
2528 sio_data->type = it8607;
2531 sio_data->type = it8620;
2534 sio_data->type = it8622;
2537 sio_data->type = it8628;
2540 sio_data->type = it8686;
2542 case 0xffff: /* No device at all */
2545 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
2549 superio_select(sioaddr, PME);
2550 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
2551 pr_info("Device not activated, skipping\n");
2555 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
2556 if (*address == 0) {
2557 pr_info("Base address not set, skipping\n");
2562 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
2563 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2564 it87_devices[sio_data->type].suffix,
2565 *address, sio_data->revision);
2567 config = &it87_devices[sio_data->type];
2569 /* in7 (VSB or VCCH5V) is always internal on some chips */
2570 if (has_in7_internal(config))
2571 sio_data->internal |= BIT(1);
2573 /* in8 (Vbat) is always internal */
2574 sio_data->internal |= BIT(2);
2576 /* in9 (AVCC3), always internal if supported */
2577 if (has_avcc3(config))
2578 sio_data->internal |= BIT(3); /* in9 is AVCC */
2580 sio_data->skip_in |= BIT(9);
2582 if (!has_four_pwm(config))
2583 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
2584 else if (!has_five_pwm(config))
2585 sio_data->skip_pwm |= BIT(4) | BIT(5);
2586 else if (!has_six_pwm(config))
2587 sio_data->skip_pwm |= BIT(5);
2589 if (!has_vid(config))
2590 sio_data->skip_vid = 1;
2592 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2593 if (sio_data->type == it87) {
2594 /* The IT8705F has a different LD number for GPIO */
2595 superio_select(sioaddr, 5);
2596 sio_data->beep_pin = superio_inb(sioaddr,
2597 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2598 } else if (sio_data->type == it8783) {
2599 int reg25, reg27, reg2a, reg2c, regef;
2601 superio_select(sioaddr, GPIO);
2603 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2604 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2605 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2606 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2607 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
2609 /* Check if fan3 is there or not */
2610 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2611 sio_data->skip_fan |= BIT(2);
2612 if ((reg25 & BIT(4)) ||
2613 (!(reg2a & BIT(1)) && (regef & BIT(0))))
2614 sio_data->skip_pwm |= BIT(2);
2616 /* Check if fan2 is there or not */
2618 sio_data->skip_fan |= BIT(1);
2620 sio_data->skip_pwm |= BIT(1);
2623 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2624 sio_data->skip_in |= BIT(5); /* No VIN5 */
2628 sio_data->skip_in |= BIT(6); /* No VIN6 */
2632 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2634 if (reg27 & BIT(2)) {
2636 * The data sheet is a bit unclear regarding the
2637 * internal voltage divider for VCCH5V. It says
2638 * "This bit enables and switches VIN7 (pin 91) to the
2639 * internal voltage divider for VCCH5V".
2640 * This is different to other chips, where the internal
2641 * voltage divider would connect VIN7 to an internal
2642 * voltage source. Maybe that is the case here as well.
2644 * Since we don't know for sure, re-route it if that is
2645 * not the case, and ask the user to report if the
2646 * resulting voltage is sane.
2648 if (!(reg2c & BIT(1))) {
2650 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2652 pr_notice("Routing internal VCCH5V to in7.\n");
2654 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2655 pr_notice("Please report if it displays a reasonable voltage.\n");
2659 sio_data->internal |= BIT(0);
2661 sio_data->internal |= BIT(1);
2663 sio_data->beep_pin = superio_inb(sioaddr,
2664 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2665 } else if (sio_data->type == it8603 || sio_data->type == it8607) {
2668 superio_select(sioaddr, GPIO);
2670 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2672 /* Check if fan3 is there or not */
2674 sio_data->skip_pwm |= BIT(2);
2676 sio_data->skip_fan |= BIT(2);
2678 /* Check if fan2 is there or not */
2679 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2681 sio_data->skip_pwm |= BIT(1);
2683 sio_data->skip_fan |= BIT(1);
2685 if (sio_data->type == it8603) {
2686 sio_data->skip_in |= BIT(5); /* No VIN5 */
2687 sio_data->skip_in |= BIT(6); /* No VIN6 */
2690 sio_data->beep_pin = superio_inb(sioaddr,
2691 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2692 } else if (sio_data->type == it8620 || sio_data->type == it8628 ||
2693 sio_data->type == it8686) {
2696 superio_select(sioaddr, GPIO);
2698 /* Check for pwm5 */
2699 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2701 sio_data->skip_pwm |= BIT(4);
2703 /* Check for fan4, fan5 */
2704 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2705 if (!(reg & BIT(5)))
2706 sio_data->skip_fan |= BIT(3);
2707 if (!(reg & BIT(4)))
2708 sio_data->skip_fan |= BIT(4);
2710 /* Check for pwm3, fan3 */
2711 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2713 sio_data->skip_pwm |= BIT(2);
2715 sio_data->skip_fan |= BIT(2);
2717 /* Check for pwm4 */
2718 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
2720 sio_data->skip_pwm |= BIT(3);
2722 /* Check for pwm2, fan2 */
2723 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2725 sio_data->skip_pwm |= BIT(1);
2727 sio_data->skip_fan |= BIT(1);
2728 /* Check for pwm6, fan6 */
2729 if (!(reg & BIT(7))) {
2730 sio_data->skip_pwm |= BIT(5);
2731 sio_data->skip_fan |= BIT(5);
2734 /* Check if AVCC is on VIN3 */
2735 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2737 sio_data->internal |= BIT(0);
2739 sio_data->skip_in |= BIT(9);
2741 sio_data->beep_pin = superio_inb(sioaddr,
2742 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2743 } else if (sio_data->type == it8622) {
2746 superio_select(sioaddr, GPIO);
2748 /* Check for pwm4, fan4 */
2749 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2751 sio_data->skip_fan |= BIT(3);
2753 sio_data->skip_pwm |= BIT(3);
2755 /* Check for pwm3, fan3, pwm5, fan5 */
2756 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2758 sio_data->skip_pwm |= BIT(2);
2760 sio_data->skip_fan |= BIT(2);
2762 sio_data->skip_pwm |= BIT(4);
2764 sio_data->skip_fan |= BIT(4);
2766 /* Check for pwm2, fan2 */
2767 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2769 sio_data->skip_pwm |= BIT(1);
2771 sio_data->skip_fan |= BIT(1);
2773 /* Check for AVCC */
2774 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2775 if (!(reg & BIT(0)))
2776 sio_data->skip_in |= BIT(9);
2778 sio_data->beep_pin = superio_inb(sioaddr,
2779 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2780 } else if (sio_data->type == it8732) {
2783 superio_select(sioaddr, GPIO);
2785 /* Check for pwm2, fan2 */
2786 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2788 sio_data->skip_pwm |= BIT(1);
2790 sio_data->skip_fan |= BIT(1);
2792 /* Check for pwm3, fan3, fan4 */
2793 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2795 sio_data->skip_pwm |= BIT(2);
2797 sio_data->skip_fan |= BIT(2);
2799 sio_data->skip_fan |= BIT(3);
2801 /* Check if AVCC is on VIN3 */
2802 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2804 sio_data->internal |= BIT(0);
2806 sio_data->beep_pin = superio_inb(sioaddr,
2807 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2812 superio_select(sioaddr, GPIO);
2814 /* Check for fan4, fan5 */
2815 if (has_five_fans(config)) {
2816 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2817 switch (sio_data->type) {
2820 sio_data->skip_fan |= BIT(3);
2822 sio_data->skip_fan |= BIT(4);
2827 if (!(reg & BIT(5)))
2828 sio_data->skip_fan |= BIT(3);
2829 if (!(reg & BIT(4)))
2830 sio_data->skip_fan |= BIT(4);
2837 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2838 if (!sio_data->skip_vid) {
2839 /* We need at least 4 VID pins */
2841 pr_info("VID is disabled (pins used for GPIO)\n");
2842 sio_data->skip_vid = 1;
2846 /* Check if fan3 is there or not */
2848 sio_data->skip_pwm |= BIT(2);
2850 sio_data->skip_fan |= BIT(2);
2852 /* Check if fan2 is there or not */
2853 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2855 sio_data->skip_pwm |= BIT(1);
2857 sio_data->skip_fan |= BIT(1);
2859 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2860 !(sio_data->skip_vid))
2861 sio_data->vid_value = superio_inb(sioaddr,
2864 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2866 uart6 = sio_data->type == it8782 && (reg & BIT(2));
2869 * The IT8720F has no VIN7 pin, so VCCH should always be
2870 * routed internally to VIN7 with an internal divider.
2871 * Curiously, there still is a configuration bit to control
2872 * this, which means it can be set incorrectly. And even
2873 * more curiously, many boards out there are improperly
2874 * configured, even though the IT8720F datasheet claims
2875 * that the internal routing of VCCH to VIN7 is the default
2876 * setting. So we force the internal routing in this case.
2878 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2879 * If UART6 is enabled, re-route VIN7 to the internal divider
2880 * if that is not already the case.
2882 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2884 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
2885 pr_notice("Routing internal VCCH to in7\n");
2888 sio_data->internal |= BIT(0);
2890 sio_data->internal |= BIT(1);
2893 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2894 * While VIN7 can be routed to the internal voltage divider,
2895 * VIN5 and VIN6 are not available if UART6 is enabled.
2897 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2898 * is the temperature source. Since we can not read the
2899 * temperature source here, skip_temp is preliminary.
2902 sio_data->skip_in |= BIT(5) | BIT(6);
2903 sio_data->skip_temp |= BIT(2);
2906 sio_data->beep_pin = superio_inb(sioaddr,
2907 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2909 if (sio_data->beep_pin)
2910 pr_info("Beeping is supported\n");
2912 /* Disable specific features based on DMI strings */
2913 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2914 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2915 if (board_vendor && board_name) {
2916 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2917 strcmp(board_name, "FN68PT") == 0) {
2919 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2920 * connected to a fan, but to something else. One user
2921 * has reported instant system power-off when changing
2922 * the PWM2 duty cycle, so we disable it.
2923 * I use the board name string as the trigger in case
2924 * the same board is ever used in other systems.
2926 pr_info("Disabling pwm2 due to hardware constraints\n");
2927 sio_data->skip_pwm = BIT(1);
2932 superio_exit(sioaddr);
2936 /* Called when we have found a new IT87. */
2937 static void it87_init_device(struct platform_device *pdev)
2939 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2940 struct it87_data *data = platform_get_drvdata(pdev);
2945 * For each PWM channel:
2946 * - If it is in automatic mode, setting to manual mode should set
2947 * the fan to full speed by default.
2948 * - If it is in manual mode, we need a mapping to temperature
2949 * channels to use when later setting to automatic mode later.
2950 * Use a 1:1 mapping by default (we are clueless.)
2951 * In both cases, the value can (and should) be changed by the user
2952 * prior to switching to a different mode.
2953 * Note that this is no longer needed for the IT8721F and later, as
2954 * these have separate registers for the temperature mapping and the
2955 * manual duty cycle.
2957 for (i = 0; i < NUM_AUTO_PWM; i++) {
2958 data->pwm_temp_map[i] = i;
2959 data->pwm_duty[i] = 0x7f; /* Full speed */
2960 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2964 * Some chips seem to have default value 0xff for all limit
2965 * registers. For low voltage limits it makes no sense and triggers
2966 * alarms, so change to 0 instead. For high temperature limits, it
2967 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2968 * but is still confusing, so change to 127 degrees C.
2970 for (i = 0; i < NUM_VIN_LIMIT; i++) {
2971 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2973 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2975 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
2976 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2978 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2982 * Temperature channels are not forcibly enabled, as they can be
2983 * set to two different sensor types and we can't guess which one
2984 * is correct for a given system. These channels can be enabled at
2985 * run-time through the temp{1-3}_type sysfs accessors if needed.
2988 /* Check if voltage monitors are reset manually or by some reason */
2989 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2990 if ((tmp & 0xff) == 0) {
2991 /* Enable all voltage monitors */
2992 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2995 /* Check if tachometers are reset manually or by some reason */
2996 mask = 0x70 & ~(sio_data->skip_fan << 4);
2997 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2998 if ((data->fan_main_ctrl & mask) == 0) {
2999 /* Enable all fan tachometers */
3000 data->fan_main_ctrl |= mask;
3001 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
3002 data->fan_main_ctrl);
3004 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
3006 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
3008 /* Set tachometers to 16-bit mode if needed */
3009 if (has_fan16_config(data)) {
3010 if (~tmp & 0x07 & data->has_fan) {
3012 "Setting fan1-3 to 16-bit mode\n");
3013 it87_write_value(data, IT87_REG_FAN_16BIT,
3018 /* Check for additional fans */
3019 if (has_four_fans(data) && (tmp & BIT(4)))
3020 data->has_fan |= BIT(3); /* fan4 enabled */
3021 if (has_five_fans(data) && (tmp & BIT(5)))
3022 data->has_fan |= BIT(4); /* fan5 enabled */
3023 if (!has_fan16_config(data) && has_six_fans(data) && (tmp & BIT(2)))
3024 data->has_fan |= BIT(5); /* fan6 enabled */
3026 /* Fan input pins may be used for alternative functions */
3027 data->has_fan &= ~sio_data->skip_fan;
3029 /* Check if pwm5, pwm6 are enabled */
3030 if (has_six_pwm(data)) {
3031 /* The following code may be IT8620E specific */
3032 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
3033 if ((tmp & 0xc0) == 0xc0)
3034 sio_data->skip_pwm |= BIT(4);
3035 if (!(tmp & BIT(3)))
3036 sio_data->skip_pwm |= BIT(5);
3039 /* Start monitoring */
3040 it87_write_value(data, IT87_REG_CONFIG,
3041 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
3042 | (update_vbat ? 0x41 : 0x01));
3045 /* Return 1 if and only if the PWM interface is safe to use */
3046 static int it87_check_pwm(struct device *dev)
3048 struct it87_data *data = dev_get_drvdata(dev);
3050 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
3051 * and polarity set to active low is sign that this is the case so we
3052 * disable pwm control to protect the user.
3054 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
3056 if ((tmp & 0x87) == 0) {
3057 if (fix_pwm_polarity) {
3059 * The user asks us to attempt a chip reconfiguration.
3060 * This means switching to active high polarity and
3061 * inverting all fan speed values.
3066 for (i = 0; i < ARRAY_SIZE(pwm); i++)
3067 pwm[i] = it87_read_value(data,
3071 * If any fan is in automatic pwm mode, the polarity
3072 * might be correct, as suspicious as it seems, so we
3073 * better don't change anything (but still disable the
3076 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
3078 "Reconfiguring PWM to active high polarity\n");
3079 it87_write_value(data, IT87_REG_FAN_CTL,
3081 for (i = 0; i < 3; i++)
3082 it87_write_value(data,
3089 "PWM configuration is too broken to be fixed\n");
3093 "Detected broken BIOS defaults, disabling PWM interface\n");
3095 } else if (fix_pwm_polarity) {
3097 "PWM configuration looks sane, won't touch\n");
3103 static int it87_probe(struct platform_device *pdev)
3105 struct it87_data *data;
3106 struct resource *res;
3107 struct device *dev = &pdev->dev;
3108 struct it87_sio_data *sio_data = dev_get_platdata(dev);
3109 int enable_pwm_interface;
3110 struct device *hwmon_dev;
3112 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
3113 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
3115 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
3116 (unsigned long)res->start,
3117 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
3121 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
3125 data->addr = res->start;
3126 data->type = sio_data->type;
3127 data->features = it87_devices[sio_data->type].features;
3128 data->peci_mask = it87_devices[sio_data->type].peci_mask;
3129 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
3133 * IT8705F Datasheet 0.4.1, 3h == Version G.
3134 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
3135 * These are the first revisions with 16-bit tachometer support.
3137 switch (data->type) {
3139 if (sio_data->revision >= 0x03) {
3140 data->features &= ~FEAT_OLD_AUTOPWM;
3141 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
3145 if (sio_data->revision >= 0x08) {
3146 data->features &= ~FEAT_OLD_AUTOPWM;
3147 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
3155 /* Now, we do the remaining detection. */
3156 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
3157 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
3160 platform_set_drvdata(pdev, data);
3162 mutex_init(&data->update_lock);
3164 /* Check PWM configuration */
3165 enable_pwm_interface = it87_check_pwm(dev);
3167 /* Starting with IT8721F, we handle scaling of internal voltages */
3168 if (has_scaling(data)) {
3169 if (sio_data->internal & BIT(0))
3170 data->in_scaled |= BIT(3); /* in3 is AVCC */
3171 if (sio_data->internal & BIT(1))
3172 data->in_scaled |= BIT(7); /* in7 is VSB */
3173 if (sio_data->internal & BIT(2))
3174 data->in_scaled |= BIT(8); /* in8 is Vbat */
3175 if (sio_data->internal & BIT(3))
3176 data->in_scaled |= BIT(9); /* in9 is AVCC */
3177 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
3178 sio_data->type == it8783) {
3179 if (sio_data->internal & BIT(0))
3180 data->in_scaled |= BIT(3); /* in3 is VCC5V */
3181 if (sio_data->internal & BIT(1))
3182 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
3185 data->has_temp = 0x07;
3186 if (sio_data->skip_temp & BIT(2)) {
3187 if (sio_data->type == it8782 &&
3188 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
3189 data->has_temp &= ~BIT(2);
3192 data->in_internal = sio_data->internal;
3193 data->has_in = 0x3ff & ~sio_data->skip_in;
3195 if (has_six_temp(data)) {
3196 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
3198 /* Check for additional temperature sensors */
3199 if ((reg & 0x03) >= 0x02)
3200 data->has_temp |= BIT(3);
3201 if (((reg >> 2) & 0x03) >= 0x02)
3202 data->has_temp |= BIT(4);
3203 if (((reg >> 4) & 0x03) >= 0x02)
3204 data->has_temp |= BIT(5);
3206 /* Check for additional voltage sensors */
3207 if ((reg & 0x03) == 0x01)
3208 data->has_in |= BIT(10);
3209 if (((reg >> 2) & 0x03) == 0x01)
3210 data->has_in |= BIT(11);
3211 if (((reg >> 4) & 0x03) == 0x01)
3212 data->has_in |= BIT(12);
3215 data->has_beep = !!sio_data->beep_pin;
3217 /* Initialize the IT87 chip */
3218 it87_init_device(pdev);
3220 if (!sio_data->skip_vid) {
3221 data->has_vid = true;
3222 data->vrm = vid_which_vrm();
3223 /* VID reading from Super-I/O config space if available */
3224 data->vid = sio_data->vid_value;
3227 /* Prepare for sysfs hooks */
3228 data->groups[0] = &it87_group;
3229 data->groups[1] = &it87_group_in;
3230 data->groups[2] = &it87_group_temp;
3231 data->groups[3] = &it87_group_fan;
3233 if (enable_pwm_interface) {
3234 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
3235 data->has_pwm &= ~sio_data->skip_pwm;
3237 data->groups[4] = &it87_group_pwm;
3238 if (has_old_autopwm(data) || has_newer_autopwm(data))
3239 data->groups[5] = &it87_group_auto_pwm;
3242 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3243 it87_devices[sio_data->type].name,
3244 data, data->groups);
3245 return PTR_ERR_OR_ZERO(hwmon_dev);
3248 static struct platform_driver it87_driver = {
3252 .probe = it87_probe,
3255 static int __init it87_device_add(int index, unsigned short address,
3256 const struct it87_sio_data *sio_data)
3258 struct platform_device *pdev;
3259 struct resource res = {
3260 .start = address + IT87_EC_OFFSET,
3261 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
3263 .flags = IORESOURCE_IO,
3267 err = acpi_check_resource_conflict(&res);
3271 pdev = platform_device_alloc(DRVNAME, address);
3275 err = platform_device_add_resources(pdev, &res, 1);
3277 pr_err("Device resource addition failed (%d)\n", err);
3278 goto exit_device_put;
3281 err = platform_device_add_data(pdev, sio_data,
3282 sizeof(struct it87_sio_data));
3284 pr_err("Platform data allocation failed\n");
3285 goto exit_device_put;
3288 err = platform_device_add(pdev);
3290 pr_err("Device addition failed (%d)\n", err);
3291 goto exit_device_put;
3294 it87_pdev[index] = pdev;
3298 platform_device_put(pdev);
3302 static int __init sm_it87_init(void)
3304 int sioaddr[2] = { REG_2E, REG_4E };
3305 struct it87_sio_data sio_data;
3306 unsigned short isa_address;
3310 err = platform_driver_register(&it87_driver);
3314 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3315 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3317 err = it87_find(sioaddr[i], &isa_address, &sio_data);
3318 if (err || isa_address == 0)
3321 err = it87_device_add(i, isa_address, &sio_data);
3323 goto exit_dev_unregister;
3329 goto exit_unregister;
3333 exit_dev_unregister:
3334 /* NULL check handled by platform_device_unregister */
3335 platform_device_unregister(it87_pdev[0]);
3337 platform_driver_unregister(&it87_driver);
3341 static void __exit sm_it87_exit(void)
3343 /* NULL check handled by platform_device_unregister */
3344 platform_device_unregister(it87_pdev[1]);
3345 platform_device_unregister(it87_pdev[0]);
3346 platform_driver_unregister(&it87_driver);
3349 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
3350 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
3351 module_param(update_vbat, bool, 0);
3352 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3353 module_param(fix_pwm_polarity, bool, 0);
3354 MODULE_PARM_DESC(fix_pwm_polarity,
3355 "Force PWM polarity to active high (DANGEROUS)");
3356 MODULE_LICENSE("GPL");
3358 module_init(sm_it87_init);
3359 module_exit(sm_it87_exit);