2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
13 * Supports: IT8603E Super I/O chip w/LPC interface
14 * IT8620E Super I/O chip w/LPC interface
15 * IT8623E Super I/O chip w/LPC interface
16 * IT8628E Super I/O chip w/LPC interface
17 * IT8705F Super I/O chip w/LPC interface
18 * IT8712F Super I/O chip w/LPC interface
19 * IT8716F Super I/O chip w/LPC interface
20 * IT8718F Super I/O chip w/LPC interface
21 * IT8720F Super I/O chip w/LPC interface
22 * IT8721F Super I/O chip w/LPC interface
23 * IT8726F Super I/O chip w/LPC interface
24 * IT8728F Super I/O chip w/LPC interface
25 * IT8732F Super I/O chip w/LPC interface
26 * IT8758E Super I/O chip w/LPC interface
27 * IT8771E Super I/O chip w/LPC interface
28 * IT8772E Super I/O chip w/LPC interface
29 * IT8781F Super I/O chip w/LPC interface
30 * IT8782F Super I/O chip w/LPC interface
31 * IT8783E/F Super I/O chip w/LPC interface
32 * IT8786E Super I/O chip w/LPC interface
33 * IT8790E Super I/O chip w/LPC interface
34 * Sis950 A clone of the IT8705F
36 * Copyright (C) 2001 Chris Gauthron
37 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
50 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52 #include <linux/bitops.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/jiffies.h>
57 #include <linux/platform_device.h>
58 #include <linux/hwmon.h>
59 #include <linux/hwmon-sysfs.h>
60 #include <linux/hwmon-vid.h>
61 #include <linux/err.h>
62 #include <linux/mutex.h>
63 #include <linux/sysfs.h>
64 #include <linux/string.h>
65 #include <linux/dmi.h>
66 #include <linux/acpi.h>
70 #define DRVNAME "it87"
72 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
73 it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
76 static unsigned short force_id;
77 module_param(force_id, ushort, 0);
78 MODULE_PARM_DESC(force_id, "Override the detected device ID");
80 static struct platform_device *it87_pdev[2];
82 #define REG_2E 0x2e /* The register to read/write */
83 #define REG_4E 0x4e /* Secondary register to read/write */
85 #define DEV 0x07 /* Register: Logical device select */
86 #define PME 0x04 /* The device with the fan registers in it */
88 /* The device with the IT8718F/IT8720F VID value in it */
91 #define DEVID 0x20 /* Register: Device ID */
92 #define DEVREV 0x22 /* Register: Device Revision */
94 static inline int superio_inb(int ioreg, int reg)
97 return inb(ioreg + 1);
100 static inline void superio_outb(int ioreg, int reg, int val)
103 outb(val, ioreg + 1);
106 static int superio_inw(int ioreg, int reg)
111 val = inb(ioreg + 1) << 8;
113 val |= inb(ioreg + 1);
117 static inline void superio_select(int ioreg, int ldn)
120 outb(ldn, ioreg + 1);
123 static inline int superio_enter(int ioreg)
126 * Try to reserve ioreg and ioreg + 1 for exclusive access.
128 if (!request_muxed_region(ioreg, 2, DRVNAME))
134 outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
138 static inline void superio_exit(int ioreg)
141 outb(0x02, ioreg + 1);
142 release_region(ioreg, 2);
145 /* Logical device 4 registers */
146 #define IT8712F_DEVID 0x8712
147 #define IT8705F_DEVID 0x8705
148 #define IT8716F_DEVID 0x8716
149 #define IT8718F_DEVID 0x8718
150 #define IT8720F_DEVID 0x8720
151 #define IT8721F_DEVID 0x8721
152 #define IT8726F_DEVID 0x8726
153 #define IT8728F_DEVID 0x8728
154 #define IT8732F_DEVID 0x8732
155 #define IT8771E_DEVID 0x8771
156 #define IT8772E_DEVID 0x8772
157 #define IT8781F_DEVID 0x8781
158 #define IT8782F_DEVID 0x8782
159 #define IT8783E_DEVID 0x8783
160 #define IT8786E_DEVID 0x8786
161 #define IT8790E_DEVID 0x8790
162 #define IT8603E_DEVID 0x8603
163 #define IT8620E_DEVID 0x8620
164 #define IT8623E_DEVID 0x8623
165 #define IT8628E_DEVID 0x8628
166 #define IT87_ACT_REG 0x30
167 #define IT87_BASE_REG 0x60
169 /* Logical device 7 registers (IT8712F and later) */
170 #define IT87_SIO_GPIO1_REG 0x25
171 #define IT87_SIO_GPIO2_REG 0x26
172 #define IT87_SIO_GPIO3_REG 0x27
173 #define IT87_SIO_GPIO4_REG 0x28
174 #define IT87_SIO_GPIO5_REG 0x29
175 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
176 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
177 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
178 #define IT87_SIO_VID_REG 0xfc /* VID value */
179 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
181 /* Update battery voltage after every reading if true */
182 static bool update_vbat;
184 /* Not all BIOSes properly configure the PWM registers */
185 static bool fix_pwm_polarity;
187 /* Many IT87 constants specified below */
189 /* Length of ISA address segment */
190 #define IT87_EXTENT 8
192 /* Length of ISA address segment for Environmental Controller */
193 #define IT87_EC_EXTENT 2
195 /* Offset of EC registers from ISA base address */
196 #define IT87_EC_OFFSET 5
198 /* Where are the ISA address/data registers relative to the EC base address */
199 #define IT87_ADDR_REG_OFFSET 0
200 #define IT87_DATA_REG_OFFSET 1
202 /*----- The IT87 registers -----*/
204 #define IT87_REG_CONFIG 0x00
206 #define IT87_REG_ALARM1 0x01
207 #define IT87_REG_ALARM2 0x02
208 #define IT87_REG_ALARM3 0x03
211 * The IT8718F and IT8720F have the VID value in a different register, in
212 * Super-I/O configuration space.
214 #define IT87_REG_VID 0x0a
216 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
217 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
220 #define IT87_REG_FAN_DIV 0x0b
221 #define IT87_REG_FAN_16BIT 0x0c
225 * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
226 * - up to 6 temp (1 to 6)
227 * - up to 6 fan (1 to 6)
230 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
231 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
232 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
233 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
234 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
236 #define IT87_REG_FAN_MAIN_CTRL 0x13
237 #define IT87_REG_FAN_CTL 0x14
238 static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
239 static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
241 static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
242 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
244 #define IT87_REG_TEMP(nr) (0x29 + (nr))
246 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
247 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
248 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
249 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
251 #define IT87_REG_VIN_ENABLE 0x50
252 #define IT87_REG_TEMP_ENABLE 0x51
253 #define IT87_REG_TEMP_EXTRA 0x55
254 #define IT87_REG_BEEP_ENABLE 0x5c
256 #define IT87_REG_CHIPID 0x58
258 static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
260 #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
261 #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
263 #define IT87_REG_TEMP456_ENABLE 0x77
265 #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
266 #define NUM_VIN_LIMIT 8
268 #define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
269 #define NUM_TEMP_LIMIT 3
270 #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
271 #define NUM_FAN_DIV 3
272 #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
273 #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
275 struct it87_devices {
277 const char * const suffix;
283 #define FEAT_12MV_ADC BIT(0)
284 #define FEAT_NEWER_AUTOPWM BIT(1)
285 #define FEAT_OLD_AUTOPWM BIT(2)
286 #define FEAT_16BIT_FANS BIT(3)
287 #define FEAT_TEMP_OFFSET BIT(4)
288 #define FEAT_TEMP_PECI BIT(5)
289 #define FEAT_TEMP_OLD_PECI BIT(6)
290 #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
291 #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
292 #define FEAT_VID BIT(9) /* Set if chip supports VID */
293 #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
294 #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
295 #define FEAT_10_9MV_ADC BIT(12)
296 #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
297 #define FEAT_SIX_PWM BIT(14) /* Chip supports 6 pwm chn */
298 #define FEAT_PWM_FREQ2 BIT(15) /* Separate pwm freq 2 */
299 #define FEAT_SIX_TEMP BIT(16) /* Up to 6 temp sensors */
300 #define FEAT_VIN3_5V BIT(17) /* VIN3 connected to +5V */
302 static const struct it87_devices it87_devices[] = {
306 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
311 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
312 /* may need to overwrite */
317 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
318 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
323 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
324 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
326 .old_peci_mask = 0x4,
331 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
332 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
334 .old_peci_mask = 0x4,
339 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
340 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
341 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
344 .old_peci_mask = 0x02, /* Actually reports PCH */
349 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
350 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
351 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
357 .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
358 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
359 | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
361 .old_peci_mask = 0x02, /* Actually reports PCH */
366 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
367 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
369 /* PECI: guesswork */
371 /* 16 bit fans (OHM) */
372 /* three fans, always 16 bit (guesswork) */
378 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
379 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
381 /* PECI (coreboot) */
382 /* 12mV ADC (HWSensors4, OHM) */
383 /* 16 bit fans (HWSensors4, OHM) */
384 /* three fans, always 16 bit (datasheet) */
390 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
391 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
392 .old_peci_mask = 0x4,
397 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
398 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
399 .old_peci_mask = 0x4,
404 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
405 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
406 .old_peci_mask = 0x4,
411 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
412 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
419 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
420 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
427 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
428 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
429 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
435 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
436 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
437 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
438 | FEAT_SIX_TEMP | FEAT_VIN3_5V,
444 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
445 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
446 | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
447 | FEAT_SIX_TEMP | FEAT_VIN3_5V,
452 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
453 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
454 #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
455 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
456 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
457 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
458 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
459 ((data)->peci_mask & BIT(nr)))
460 #define has_temp_old_peci(data, nr) \
461 (((data)->features & FEAT_TEMP_OLD_PECI) && \
462 ((data)->old_peci_mask & BIT(nr)))
463 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
464 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
466 #define has_vid(data) ((data)->features & FEAT_VID)
467 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
468 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
469 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
470 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
471 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
472 #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
473 #define has_vin3_5v(data) ((data)->features & FEAT_VIN3_5V)
475 struct it87_sio_data {
477 /* Values read from Super-I/O config space */
481 u8 internal; /* Internal sensors can be labeled */
482 /* Features skipped based on config or DMI */
491 * For each registered chip, we need to keep some data in memory.
492 * The structure is dynamically allocated.
495 const struct attribute_group *groups[7];
503 struct mutex update_lock;
504 char valid; /* !=0 if following fields are valid */
505 unsigned long last_updated; /* In jiffies */
507 u16 in_scaled; /* Internal voltage sensors are scaled */
508 u16 in_internal; /* Bitfield, internal sensors (for labels) */
509 u16 has_in; /* Bitfield, voltage sensors enabled */
510 u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
511 u8 has_fan; /* Bitfield, fans enabled */
512 u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
513 u8 has_temp; /* Bitfield, temp sensors enabled */
514 s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
515 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
516 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
517 u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
518 bool has_vid; /* True if VID supported */
519 u8 vid; /* Register encoding, combined */
521 u32 alarms; /* Register encoding, combined */
522 bool has_beep; /* true if beep supported */
523 u8 beeps; /* Register encoding */
524 u8 fan_main_ctrl; /* Register value */
525 u8 fan_ctl; /* Register value */
528 * The following 3 arrays correspond to the same registers up to
529 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
530 * 7, and we want to preserve settings on mode changes, so we have
531 * to track all values separately.
532 * Starting with the IT8721F, the manual PWM duty cycles are stored
533 * in separate registers (8-bit values), so the separate tracking
534 * is no longer needed, but it is still done to keep the driver
537 u8 has_pwm; /* Bitfield, pwm control enabled */
538 u8 pwm_ctrl[NUM_PWM]; /* Register value */
539 u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
540 u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
542 /* Automatic fan speed control registers */
543 u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
544 s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
547 static int adc_lsb(const struct it87_data *data, int nr)
551 if (has_12mv_adc(data))
553 else if (has_10_9mv_adc(data))
557 if (data->in_scaled & BIT(nr))
562 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
564 val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
565 return clamp_val(val, 0, 255);
568 static int in_from_reg(const struct it87_data *data, int nr, int val)
570 return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
573 static inline u8 FAN_TO_REG(long rpm, int div)
577 rpm = clamp_val(rpm, 1, 1000000);
578 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
581 static inline u16 FAN16_TO_REG(long rpm)
585 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
588 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
589 1350000 / ((val) * (div)))
590 /* The divider is fixed to 2 in 16-bit mode */
591 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
592 1350000 / ((val) * 2))
594 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
595 ((val) + 500) / 1000), -128, 127))
596 #define TEMP_FROM_REG(val) ((val) * 1000)
598 static u8 pwm_to_reg(const struct it87_data *data, long val)
600 if (has_newer_autopwm(data))
606 static int pwm_from_reg(const struct it87_data *data, u8 reg)
608 if (has_newer_autopwm(data))
611 return (reg & 0x7f) << 1;
614 static int DIV_TO_REG(int val)
618 while (answer < 7 && (val >>= 1))
623 #define DIV_FROM_REG(val) BIT(val)
626 * PWM base frequencies. The frequency has to be divided by either 128 or 256,
627 * depending on the chip type, to calculate the actual PWM frequency.
629 * Some of the chip datasheets suggest a base frequency of 51 kHz instead
630 * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
631 * of 200 Hz. Sometimes both PWM frequency select registers are affected,
632 * sometimes just one. It is unknown if this is a datasheet error or real,
633 * so this is ignored for now.
635 static const unsigned int pwm_freq[8] = {
647 * Must be called with data->update_lock held, except during initialization.
648 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
649 * would slow down the IT87 access and should not be necessary.
651 static int it87_read_value(struct it87_data *data, u8 reg)
653 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
654 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
658 * Must be called with data->update_lock held, except during initialization.
659 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
660 * would slow down the IT87 access and should not be necessary.
662 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
664 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
665 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
668 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
670 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
671 if (has_newer_autopwm(data)) {
672 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
673 data->pwm_duty[nr] = it87_read_value(data,
674 IT87_REG_PWM_DUTY[nr]);
676 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
677 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
678 else /* Manual mode */
679 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
682 if (has_old_autopwm(data)) {
685 for (i = 0; i < 5 ; i++)
686 data->auto_temp[nr][i] = it87_read_value(data,
687 IT87_REG_AUTO_TEMP(nr, i));
688 for (i = 0; i < 3 ; i++)
689 data->auto_pwm[nr][i] = it87_read_value(data,
690 IT87_REG_AUTO_PWM(nr, i));
691 } else if (has_newer_autopwm(data)) {
695 * 0: temperature hysteresis (base + 5)
696 * 1: fan off temperature (base + 0)
697 * 2: fan start temperature (base + 1)
698 * 3: fan max temperature (base + 2)
700 data->auto_temp[nr][0] =
701 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
703 for (i = 0; i < 3 ; i++)
704 data->auto_temp[nr][i + 1] =
705 it87_read_value(data,
706 IT87_REG_AUTO_TEMP(nr, i));
708 * 0: start pwm value (base + 3)
709 * 1: pwm slope (base + 4, 1/8th pwm)
711 data->auto_pwm[nr][0] =
712 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
713 data->auto_pwm[nr][1] =
714 it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
718 static struct it87_data *it87_update_device(struct device *dev)
720 struct it87_data *data = dev_get_drvdata(dev);
723 mutex_lock(&data->update_lock);
725 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
729 * Cleared after each update, so reenable. Value
730 * returned by this read will be previous value
732 it87_write_value(data, IT87_REG_CONFIG,
733 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
735 for (i = 0; i < NUM_VIN; i++) {
736 if (!(data->has_in & BIT(i)))
740 it87_read_value(data, IT87_REG_VIN[i]);
742 /* VBAT and AVCC don't have limit registers */
743 if (i >= NUM_VIN_LIMIT)
747 it87_read_value(data, IT87_REG_VIN_MIN(i));
749 it87_read_value(data, IT87_REG_VIN_MAX(i));
752 for (i = 0; i < NUM_FAN; i++) {
753 /* Skip disabled fans */
754 if (!(data->has_fan & BIT(i)))
758 it87_read_value(data, IT87_REG_FAN_MIN[i]);
759 data->fan[i][0] = it87_read_value(data,
761 /* Add high byte if in 16-bit mode */
762 if (has_16bit_fans(data)) {
763 data->fan[i][0] |= it87_read_value(data,
764 IT87_REG_FANX[i]) << 8;
765 data->fan[i][1] |= it87_read_value(data,
766 IT87_REG_FANX_MIN[i]) << 8;
769 for (i = 0; i < NUM_TEMP; i++) {
770 if (!(data->has_temp & BIT(i)))
773 it87_read_value(data, IT87_REG_TEMP(i));
775 if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
777 it87_read_value(data,
778 IT87_REG_TEMP_OFFSET[i]);
780 if (i >= NUM_TEMP_LIMIT)
784 it87_read_value(data, IT87_REG_TEMP_LOW(i));
786 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
789 /* Newer chips don't have clock dividers */
790 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
791 i = it87_read_value(data, IT87_REG_FAN_DIV);
792 data->fan_div[0] = i & 0x07;
793 data->fan_div[1] = (i >> 3) & 0x07;
794 data->fan_div[2] = (i & 0x40) ? 3 : 1;
798 it87_read_value(data, IT87_REG_ALARM1) |
799 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
800 (it87_read_value(data, IT87_REG_ALARM3) << 16);
801 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
803 data->fan_main_ctrl = it87_read_value(data,
804 IT87_REG_FAN_MAIN_CTRL);
805 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
806 for (i = 0; i < NUM_PWM; i++) {
807 if (!(data->has_pwm & BIT(i)))
809 it87_update_pwm_ctrl(data, i);
812 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
813 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
815 * The IT8705F does not have VID capability.
816 * The IT8718F and later don't use IT87_REG_VID for the
819 if (data->type == it8712 || data->type == it8716) {
820 data->vid = it87_read_value(data, IT87_REG_VID);
822 * The older IT8712F revisions had only 5 VID pins,
823 * but we assume it is always safe to read 6 bits.
827 data->last_updated = jiffies;
831 mutex_unlock(&data->update_lock);
836 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
839 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
840 struct it87_data *data = it87_update_device(dev);
841 int index = sattr->index;
844 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
847 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
848 const char *buf, size_t count)
850 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
851 struct it87_data *data = dev_get_drvdata(dev);
852 int index = sattr->index;
856 if (kstrtoul(buf, 10, &val) < 0)
859 mutex_lock(&data->update_lock);
860 data->in[nr][index] = in_to_reg(data, nr, val);
861 it87_write_value(data,
862 index == 1 ? IT87_REG_VIN_MIN(nr)
863 : IT87_REG_VIN_MAX(nr),
864 data->in[nr][index]);
865 mutex_unlock(&data->update_lock);
869 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
870 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
872 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
875 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
876 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
878 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
881 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
882 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
884 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
887 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
888 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
890 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
893 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
894 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
896 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
899 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
900 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
902 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
905 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
906 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
908 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
911 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
912 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
914 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
917 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
918 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
919 static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
920 static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
921 static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
923 /* Up to 6 temperatures */
924 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
927 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
929 int index = sattr->index;
930 struct it87_data *data = it87_update_device(dev);
932 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
935 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
936 const char *buf, size_t count)
938 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
940 int index = sattr->index;
941 struct it87_data *data = dev_get_drvdata(dev);
945 if (kstrtol(buf, 10, &val) < 0)
948 mutex_lock(&data->update_lock);
953 reg = IT87_REG_TEMP_LOW(nr);
956 reg = IT87_REG_TEMP_HIGH(nr);
959 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
960 if (!(regval & 0x80)) {
962 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
965 reg = IT87_REG_TEMP_OFFSET[nr];
969 data->temp[nr][index] = TEMP_TO_REG(val);
970 it87_write_value(data, reg, data->temp[nr][index]);
971 mutex_unlock(&data->update_lock);
975 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
976 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
978 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
980 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
982 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
983 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
985 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
987 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
989 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
990 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
992 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
994 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
996 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
997 static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
998 static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
1000 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
1003 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1004 int nr = sensor_attr->index;
1005 struct it87_data *data = it87_update_device(dev);
1006 u8 reg = data->sensor; /* In case value is updated while used */
1007 u8 extra = data->extra;
1009 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
1010 (has_temp_old_peci(data, nr) && (extra & 0x80)))
1011 return sprintf(buf, "6\n"); /* Intel PECI */
1012 if (reg & (1 << nr))
1013 return sprintf(buf, "3\n"); /* thermal diode */
1014 if (reg & (8 << nr))
1015 return sprintf(buf, "4\n"); /* thermistor */
1016 return sprintf(buf, "0\n"); /* disabled */
1019 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
1020 const char *buf, size_t count)
1022 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1023 int nr = sensor_attr->index;
1025 struct it87_data *data = dev_get_drvdata(dev);
1029 if (kstrtol(buf, 10, &val) < 0)
1032 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1035 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
1037 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
1038 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
1040 if (val == 2) { /* backwards compatibility */
1042 "Sensor type 2 is deprecated, please use 4 instead\n");
1045 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
1050 else if (has_temp_peci(data, nr) && val == 6)
1051 reg |= (nr + 1) << 6;
1052 else if (has_temp_old_peci(data, nr) && val == 6)
1057 mutex_lock(&data->update_lock);
1059 data->extra = extra;
1060 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
1061 if (has_temp_old_peci(data, nr))
1062 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1063 data->valid = 0; /* Force cache refresh */
1064 mutex_unlock(&data->update_lock);
1068 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
1070 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
1072 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
1077 static int pwm_mode(const struct it87_data *data, int nr)
1079 if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
1080 return 0; /* Full speed */
1081 if (data->pwm_ctrl[nr] & 0x80)
1082 return 2; /* Automatic mode */
1083 if ((data->type == it8603 || nr >= 3) &&
1084 data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
1085 return 0; /* Full speed */
1087 return 1; /* Manual mode */
1090 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
1093 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1095 int index = sattr->index;
1097 struct it87_data *data = it87_update_device(dev);
1099 speed = has_16bit_fans(data) ?
1100 FAN16_FROM_REG(data->fan[nr][index]) :
1101 FAN_FROM_REG(data->fan[nr][index],
1102 DIV_FROM_REG(data->fan_div[nr]));
1103 return sprintf(buf, "%d\n", speed);
1106 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
1109 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1110 struct it87_data *data = it87_update_device(dev);
1111 int nr = sensor_attr->index;
1113 return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
1116 static ssize_t show_pwm_enable(struct device *dev,
1117 struct device_attribute *attr, char *buf)
1119 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1120 struct it87_data *data = it87_update_device(dev);
1121 int nr = sensor_attr->index;
1123 return sprintf(buf, "%d\n", pwm_mode(data, nr));
1126 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1129 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1130 struct it87_data *data = it87_update_device(dev);
1131 int nr = sensor_attr->index;
1133 return sprintf(buf, "%d\n",
1134 pwm_from_reg(data, data->pwm_duty[nr]));
1137 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1140 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1141 struct it87_data *data = it87_update_device(dev);
1142 int nr = sensor_attr->index;
1146 if (has_pwm_freq2(data) && nr == 1)
1147 index = (data->extra >> 4) & 0x07;
1149 index = (data->fan_ctl >> 4) & 0x07;
1151 freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
1153 return sprintf(buf, "%u\n", freq);
1156 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
1157 const char *buf, size_t count)
1159 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1161 int index = sattr->index;
1163 struct it87_data *data = dev_get_drvdata(dev);
1167 if (kstrtol(buf, 10, &val) < 0)
1170 mutex_lock(&data->update_lock);
1172 if (has_16bit_fans(data)) {
1173 data->fan[nr][index] = FAN16_TO_REG(val);
1174 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1175 data->fan[nr][index] & 0xff);
1176 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1177 data->fan[nr][index] >> 8);
1179 reg = it87_read_value(data, IT87_REG_FAN_DIV);
1182 data->fan_div[nr] = reg & 0x07;
1185 data->fan_div[nr] = (reg >> 3) & 0x07;
1188 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
1191 data->fan[nr][index] =
1192 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
1193 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1194 data->fan[nr][index]);
1197 mutex_unlock(&data->update_lock);
1201 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
1202 const char *buf, size_t count)
1204 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1205 struct it87_data *data = dev_get_drvdata(dev);
1206 int nr = sensor_attr->index;
1211 if (kstrtoul(buf, 10, &val) < 0)
1214 mutex_lock(&data->update_lock);
1215 old = it87_read_value(data, IT87_REG_FAN_DIV);
1217 /* Save fan min limit */
1218 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
1223 data->fan_div[nr] = DIV_TO_REG(val);
1227 data->fan_div[nr] = 1;
1229 data->fan_div[nr] = 3;
1232 val |= (data->fan_div[0] & 0x07);
1233 val |= (data->fan_div[1] & 0x07) << 3;
1234 if (data->fan_div[2] == 3)
1236 it87_write_value(data, IT87_REG_FAN_DIV, val);
1238 /* Restore fan min limit */
1239 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1240 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
1242 mutex_unlock(&data->update_lock);
1246 /* Returns 0 if OK, -EINVAL otherwise */
1247 static int check_trip_points(struct device *dev, int nr)
1249 const struct it87_data *data = dev_get_drvdata(dev);
1252 if (has_old_autopwm(data)) {
1253 for (i = 0; i < 3; i++) {
1254 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1257 for (i = 0; i < 2; i++) {
1258 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1261 } else if (has_newer_autopwm(data)) {
1262 for (i = 1; i < 3; i++) {
1263 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1270 "Inconsistent trip points, not switching to automatic mode\n");
1271 dev_err(dev, "Adjust the trip points and try again\n");
1276 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1277 const char *buf, size_t count)
1279 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1280 struct it87_data *data = dev_get_drvdata(dev);
1281 int nr = sensor_attr->index;
1284 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1287 /* Check trip points before switching to automatic mode */
1289 if (check_trip_points(dev, nr) < 0)
1293 mutex_lock(&data->update_lock);
1296 if (nr < 3 && data->type != it8603) {
1298 /* make sure the fan is on when in on/off mode */
1299 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1300 it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
1301 /* set on/off mode */
1302 data->fan_main_ctrl &= ~BIT(nr);
1303 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1304 data->fan_main_ctrl);
1306 /* No on/off mode, set maximum pwm value */
1307 data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
1308 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1309 data->pwm_duty[nr]);
1310 /* and set manual mode */
1311 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1312 data->pwm_temp_map[nr] :
1314 it87_write_value(data, IT87_REG_PWM[nr],
1315 data->pwm_ctrl[nr]);
1318 if (val == 1) /* Manual mode */
1319 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1320 data->pwm_temp_map[nr] :
1322 else /* Automatic mode */
1323 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1324 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1326 if (data->type != it8603 && nr < 3) {
1327 /* set SmartGuardian mode */
1328 data->fan_main_ctrl |= BIT(nr);
1329 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1330 data->fan_main_ctrl);
1334 mutex_unlock(&data->update_lock);
1338 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1339 const char *buf, size_t count)
1341 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1342 struct it87_data *data = dev_get_drvdata(dev);
1343 int nr = sensor_attr->index;
1346 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1349 mutex_lock(&data->update_lock);
1350 if (has_newer_autopwm(data)) {
1352 * If we are in automatic mode, the PWM duty cycle register
1353 * is read-only so we can't write the value.
1355 if (data->pwm_ctrl[nr] & 0x80) {
1356 mutex_unlock(&data->update_lock);
1359 data->pwm_duty[nr] = pwm_to_reg(data, val);
1360 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1361 data->pwm_duty[nr]);
1363 data->pwm_duty[nr] = pwm_to_reg(data, val);
1365 * If we are in manual mode, write the duty cycle immediately;
1366 * otherwise, just store it for later use.
1368 if (!(data->pwm_ctrl[nr] & 0x80)) {
1369 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1370 it87_write_value(data, IT87_REG_PWM[nr],
1371 data->pwm_ctrl[nr]);
1374 mutex_unlock(&data->update_lock);
1378 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
1379 const char *buf, size_t count)
1381 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1382 struct it87_data *data = dev_get_drvdata(dev);
1383 int nr = sensor_attr->index;
1387 if (kstrtoul(buf, 10, &val) < 0)
1390 val = clamp_val(val, 0, 1000000);
1391 val *= has_newer_autopwm(data) ? 256 : 128;
1393 /* Search for the nearest available frequency */
1394 for (i = 0; i < 7; i++) {
1395 if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
1399 mutex_lock(&data->update_lock);
1401 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1402 data->fan_ctl |= i << 4;
1403 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1405 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1406 data->extra |= i << 4;
1407 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1409 mutex_unlock(&data->update_lock);
1414 static ssize_t show_pwm_temp_map(struct device *dev,
1415 struct device_attribute *attr, char *buf)
1417 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1418 struct it87_data *data = it87_update_device(dev);
1419 int nr = sensor_attr->index;
1422 map = data->pwm_temp_map[nr];
1424 map = 0; /* Should never happen */
1425 if (nr >= 3) /* pwm channels 3..6 map to temp4..6 */
1428 return sprintf(buf, "%d\n", (int)BIT(map));
1431 static ssize_t set_pwm_temp_map(struct device *dev,
1432 struct device_attribute *attr, const char *buf,
1435 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1436 struct it87_data *data = dev_get_drvdata(dev);
1437 int nr = sensor_attr->index;
1441 if (kstrtol(buf, 10, &val) < 0)
1461 mutex_lock(&data->update_lock);
1462 data->pwm_temp_map[nr] = reg;
1464 * If we are in automatic mode, write the temp mapping immediately;
1465 * otherwise, just store it for later use.
1467 if (data->pwm_ctrl[nr] & 0x80) {
1468 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1469 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1471 mutex_unlock(&data->update_lock);
1475 static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
1478 struct it87_data *data = it87_update_device(dev);
1479 struct sensor_device_attribute_2 *sensor_attr =
1480 to_sensor_dev_attr_2(attr);
1481 int nr = sensor_attr->nr;
1482 int point = sensor_attr->index;
1484 return sprintf(buf, "%d\n",
1485 pwm_from_reg(data, data->auto_pwm[nr][point]));
1488 static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
1489 const char *buf, size_t count)
1491 struct it87_data *data = dev_get_drvdata(dev);
1492 struct sensor_device_attribute_2 *sensor_attr =
1493 to_sensor_dev_attr_2(attr);
1494 int nr = sensor_attr->nr;
1495 int point = sensor_attr->index;
1499 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1502 mutex_lock(&data->update_lock);
1503 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1504 if (has_newer_autopwm(data))
1505 regaddr = IT87_REG_AUTO_TEMP(nr, 3);
1507 regaddr = IT87_REG_AUTO_PWM(nr, point);
1508 it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
1509 mutex_unlock(&data->update_lock);
1513 static ssize_t show_auto_pwm_slope(struct device *dev,
1514 struct device_attribute *attr, char *buf)
1516 struct it87_data *data = it87_update_device(dev);
1517 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1518 int nr = sensor_attr->index;
1520 return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
1523 static ssize_t set_auto_pwm_slope(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t count)
1527 struct it87_data *data = dev_get_drvdata(dev);
1528 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1529 int nr = sensor_attr->index;
1532 if (kstrtoul(buf, 10, &val) < 0 || val > 127)
1535 mutex_lock(&data->update_lock);
1536 data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
1537 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
1538 data->auto_pwm[nr][1]);
1539 mutex_unlock(&data->update_lock);
1543 static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
1546 struct it87_data *data = it87_update_device(dev);
1547 struct sensor_device_attribute_2 *sensor_attr =
1548 to_sensor_dev_attr_2(attr);
1549 int nr = sensor_attr->nr;
1550 int point = sensor_attr->index;
1553 if (has_old_autopwm(data) || point)
1554 reg = data->auto_temp[nr][point];
1556 reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
1558 return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
1561 static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
1562 const char *buf, size_t count)
1564 struct it87_data *data = dev_get_drvdata(dev);
1565 struct sensor_device_attribute_2 *sensor_attr =
1566 to_sensor_dev_attr_2(attr);
1567 int nr = sensor_attr->nr;
1568 int point = sensor_attr->index;
1572 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1575 mutex_lock(&data->update_lock);
1576 if (has_newer_autopwm(data) && !point) {
1577 reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
1578 reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
1579 data->auto_temp[nr][0] = reg;
1580 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
1582 reg = TEMP_TO_REG(val);
1583 data->auto_temp[nr][point] = reg;
1584 if (has_newer_autopwm(data))
1586 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
1588 mutex_unlock(&data->update_lock);
1592 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1593 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1595 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1598 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1599 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1601 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1604 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1605 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1607 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1610 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1611 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1614 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1615 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1618 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1619 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1622 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1623 show_pwm_enable, set_pwm_enable, 0);
1624 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1625 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1627 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
1628 show_pwm_temp_map, set_pwm_temp_map, 0);
1629 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1630 show_auto_pwm, set_auto_pwm, 0, 0);
1631 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1632 show_auto_pwm, set_auto_pwm, 0, 1);
1633 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1634 show_auto_pwm, set_auto_pwm, 0, 2);
1635 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1636 show_auto_pwm, NULL, 0, 3);
1637 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1638 show_auto_temp, set_auto_temp, 0, 1);
1639 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1640 show_auto_temp, set_auto_temp, 0, 0);
1641 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1642 show_auto_temp, set_auto_temp, 0, 2);
1643 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1644 show_auto_temp, set_auto_temp, 0, 3);
1645 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1646 show_auto_temp, set_auto_temp, 0, 4);
1647 static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
1648 show_auto_pwm, set_auto_pwm, 0, 0);
1649 static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
1650 show_auto_pwm_slope, set_auto_pwm_slope, 0);
1652 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1653 show_pwm_enable, set_pwm_enable, 1);
1654 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1655 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1656 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
1657 show_pwm_temp_map, set_pwm_temp_map, 1);
1658 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1659 show_auto_pwm, set_auto_pwm, 1, 0);
1660 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1661 show_auto_pwm, set_auto_pwm, 1, 1);
1662 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1663 show_auto_pwm, set_auto_pwm, 1, 2);
1664 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1665 show_auto_pwm, NULL, 1, 3);
1666 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1667 show_auto_temp, set_auto_temp, 1, 1);
1668 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1669 show_auto_temp, set_auto_temp, 1, 0);
1670 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1671 show_auto_temp, set_auto_temp, 1, 2);
1672 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1673 show_auto_temp, set_auto_temp, 1, 3);
1674 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1675 show_auto_temp, set_auto_temp, 1, 4);
1676 static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
1677 show_auto_pwm, set_auto_pwm, 1, 0);
1678 static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
1679 show_auto_pwm_slope, set_auto_pwm_slope, 1);
1681 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1682 show_pwm_enable, set_pwm_enable, 2);
1683 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1684 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1685 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
1686 show_pwm_temp_map, set_pwm_temp_map, 2);
1687 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1688 show_auto_pwm, set_auto_pwm, 2, 0);
1689 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1690 show_auto_pwm, set_auto_pwm, 2, 1);
1691 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1692 show_auto_pwm, set_auto_pwm, 2, 2);
1693 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1694 show_auto_pwm, NULL, 2, 3);
1695 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1696 show_auto_temp, set_auto_temp, 2, 1);
1697 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1698 show_auto_temp, set_auto_temp, 2, 0);
1699 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1700 show_auto_temp, set_auto_temp, 2, 2);
1701 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1702 show_auto_temp, set_auto_temp, 2, 3);
1703 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1704 show_auto_temp, set_auto_temp, 2, 4);
1705 static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
1706 show_auto_pwm, set_auto_pwm, 2, 0);
1707 static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
1708 show_auto_pwm_slope, set_auto_pwm_slope, 2);
1710 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1711 show_pwm_enable, set_pwm_enable, 3);
1712 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1713 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1714 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
1715 show_pwm_temp_map, set_pwm_temp_map, 3);
1716 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
1717 show_auto_temp, set_auto_temp, 2, 1);
1718 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1719 show_auto_temp, set_auto_temp, 2, 0);
1720 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
1721 show_auto_temp, set_auto_temp, 2, 2);
1722 static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
1723 show_auto_temp, set_auto_temp, 2, 3);
1724 static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
1725 show_auto_pwm, set_auto_pwm, 3, 0);
1726 static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
1727 show_auto_pwm_slope, set_auto_pwm_slope, 3);
1729 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1730 show_pwm_enable, set_pwm_enable, 4);
1731 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1732 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1733 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
1734 show_pwm_temp_map, set_pwm_temp_map, 4);
1735 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
1736 show_auto_temp, set_auto_temp, 2, 1);
1737 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1738 show_auto_temp, set_auto_temp, 2, 0);
1739 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
1740 show_auto_temp, set_auto_temp, 2, 2);
1741 static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
1742 show_auto_temp, set_auto_temp, 2, 3);
1743 static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
1744 show_auto_pwm, set_auto_pwm, 4, 0);
1745 static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
1746 show_auto_pwm_slope, set_auto_pwm_slope, 4);
1748 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1749 show_pwm_enable, set_pwm_enable, 5);
1750 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1751 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1752 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
1753 show_pwm_temp_map, set_pwm_temp_map, 5);
1754 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
1755 show_auto_temp, set_auto_temp, 2, 1);
1756 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1757 show_auto_temp, set_auto_temp, 2, 0);
1758 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
1759 show_auto_temp, set_auto_temp, 2, 2);
1760 static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
1761 show_auto_temp, set_auto_temp, 2, 3);
1762 static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
1763 show_auto_pwm, set_auto_pwm, 5, 0);
1764 static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
1765 show_auto_pwm_slope, set_auto_pwm_slope, 5);
1768 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1771 struct it87_data *data = it87_update_device(dev);
1773 return sprintf(buf, "%u\n", data->alarms);
1775 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1777 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1780 struct it87_data *data = it87_update_device(dev);
1781 int bitnr = to_sensor_dev_attr(attr)->index;
1783 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1786 static ssize_t clear_intrusion(struct device *dev,
1787 struct device_attribute *attr, const char *buf,
1790 struct it87_data *data = dev_get_drvdata(dev);
1794 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1797 mutex_lock(&data->update_lock);
1798 config = it87_read_value(data, IT87_REG_CONFIG);
1803 it87_write_value(data, IT87_REG_CONFIG, config);
1804 /* Invalidate cache to force re-read */
1807 mutex_unlock(&data->update_lock);
1812 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1813 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1814 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1815 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1816 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1817 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1818 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1819 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1820 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1821 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1822 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1823 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1824 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1825 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1826 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1827 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1828 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1829 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1830 show_alarm, clear_intrusion, 4);
1832 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1835 struct it87_data *data = it87_update_device(dev);
1836 int bitnr = to_sensor_dev_attr(attr)->index;
1838 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1841 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1842 const char *buf, size_t count)
1844 int bitnr = to_sensor_dev_attr(attr)->index;
1845 struct it87_data *data = dev_get_drvdata(dev);
1848 if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
1851 mutex_lock(&data->update_lock);
1852 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1854 data->beeps |= BIT(bitnr);
1856 data->beeps &= ~BIT(bitnr);
1857 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1858 mutex_unlock(&data->update_lock);
1862 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1863 show_beep, set_beep, 1);
1864 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1865 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1866 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1867 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1868 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1869 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1870 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1871 /* fanX_beep writability is set later */
1872 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1873 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1874 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1875 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1876 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1877 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1878 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1879 show_beep, set_beep, 2);
1880 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1881 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1883 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1886 struct it87_data *data = dev_get_drvdata(dev);
1888 return sprintf(buf, "%u\n", data->vrm);
1891 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1892 const char *buf, size_t count)
1894 struct it87_data *data = dev_get_drvdata(dev);
1897 if (kstrtoul(buf, 10, &val) < 0)
1904 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1906 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1909 struct it87_data *data = it87_update_device(dev);
1911 return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
1913 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1915 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1918 static const char * const labels[] = {
1923 static const char * const labels_it8721[] = {
1928 struct it87_data *data = dev_get_drvdata(dev);
1929 int nr = to_sensor_dev_attr(attr)->index;
1932 if (has_vin3_5v(data) && nr == 0)
1934 else if (has_12mv_adc(data) || has_10_9mv_adc(data))
1935 label = labels_it8721[nr];
1939 return sprintf(buf, "%s\n", label);
1941 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1942 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1943 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1945 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1947 static umode_t it87_in_is_visible(struct kobject *kobj,
1948 struct attribute *attr, int index)
1950 struct device *dev = container_of(kobj, struct device, kobj);
1951 struct it87_data *data = dev_get_drvdata(dev);
1952 int i = index / 5; /* voltage index */
1953 int a = index % 5; /* attribute index */
1955 if (index >= 40) { /* in8 and higher only have input attributes */
1960 if (!(data->has_in & BIT(i)))
1963 if (a == 4 && !data->has_beep)
1969 static struct attribute *it87_attributes_in[] = {
1970 &sensor_dev_attr_in0_input.dev_attr.attr,
1971 &sensor_dev_attr_in0_min.dev_attr.attr,
1972 &sensor_dev_attr_in0_max.dev_attr.attr,
1973 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1974 &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
1976 &sensor_dev_attr_in1_input.dev_attr.attr,
1977 &sensor_dev_attr_in1_min.dev_attr.attr,
1978 &sensor_dev_attr_in1_max.dev_attr.attr,
1979 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1980 &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
1982 &sensor_dev_attr_in2_input.dev_attr.attr,
1983 &sensor_dev_attr_in2_min.dev_attr.attr,
1984 &sensor_dev_attr_in2_max.dev_attr.attr,
1985 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1986 &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
1988 &sensor_dev_attr_in3_input.dev_attr.attr,
1989 &sensor_dev_attr_in3_min.dev_attr.attr,
1990 &sensor_dev_attr_in3_max.dev_attr.attr,
1991 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1992 &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
1994 &sensor_dev_attr_in4_input.dev_attr.attr,
1995 &sensor_dev_attr_in4_min.dev_attr.attr,
1996 &sensor_dev_attr_in4_max.dev_attr.attr,
1997 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1998 &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
2000 &sensor_dev_attr_in5_input.dev_attr.attr,
2001 &sensor_dev_attr_in5_min.dev_attr.attr,
2002 &sensor_dev_attr_in5_max.dev_attr.attr,
2003 &sensor_dev_attr_in5_alarm.dev_attr.attr,
2004 &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
2006 &sensor_dev_attr_in6_input.dev_attr.attr,
2007 &sensor_dev_attr_in6_min.dev_attr.attr,
2008 &sensor_dev_attr_in6_max.dev_attr.attr,
2009 &sensor_dev_attr_in6_alarm.dev_attr.attr,
2010 &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
2012 &sensor_dev_attr_in7_input.dev_attr.attr,
2013 &sensor_dev_attr_in7_min.dev_attr.attr,
2014 &sensor_dev_attr_in7_max.dev_attr.attr,
2015 &sensor_dev_attr_in7_alarm.dev_attr.attr,
2016 &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
2018 &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
2019 &sensor_dev_attr_in9_input.dev_attr.attr, /* 41 */
2020 &sensor_dev_attr_in10_input.dev_attr.attr, /* 41 */
2021 &sensor_dev_attr_in11_input.dev_attr.attr, /* 41 */
2022 &sensor_dev_attr_in12_input.dev_attr.attr, /* 41 */
2026 static const struct attribute_group it87_group_in = {
2027 .attrs = it87_attributes_in,
2028 .is_visible = it87_in_is_visible,
2031 static umode_t it87_temp_is_visible(struct kobject *kobj,
2032 struct attribute *attr, int index)
2034 struct device *dev = container_of(kobj, struct device, kobj);
2035 struct it87_data *data = dev_get_drvdata(dev);
2036 int i = index / 7; /* temperature index */
2037 int a = index % 7; /* attribute index */
2044 if (!(data->has_temp & BIT(i)))
2047 if (a == 5 && !has_temp_offset(data))
2050 if (a == 6 && !data->has_beep)
2056 static struct attribute *it87_attributes_temp[] = {
2057 &sensor_dev_attr_temp1_input.dev_attr.attr,
2058 &sensor_dev_attr_temp1_max.dev_attr.attr,
2059 &sensor_dev_attr_temp1_min.dev_attr.attr,
2060 &sensor_dev_attr_temp1_type.dev_attr.attr,
2061 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
2062 &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
2063 &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
2065 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
2066 &sensor_dev_attr_temp2_max.dev_attr.attr,
2067 &sensor_dev_attr_temp2_min.dev_attr.attr,
2068 &sensor_dev_attr_temp2_type.dev_attr.attr,
2069 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
2070 &sensor_dev_attr_temp2_offset.dev_attr.attr,
2071 &sensor_dev_attr_temp2_beep.dev_attr.attr,
2073 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
2074 &sensor_dev_attr_temp3_max.dev_attr.attr,
2075 &sensor_dev_attr_temp3_min.dev_attr.attr,
2076 &sensor_dev_attr_temp3_type.dev_attr.attr,
2077 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
2078 &sensor_dev_attr_temp3_offset.dev_attr.attr,
2079 &sensor_dev_attr_temp3_beep.dev_attr.attr,
2081 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
2082 &sensor_dev_attr_temp5_input.dev_attr.attr,
2083 &sensor_dev_attr_temp6_input.dev_attr.attr,
2087 static const struct attribute_group it87_group_temp = {
2088 .attrs = it87_attributes_temp,
2089 .is_visible = it87_temp_is_visible,
2092 static umode_t it87_is_visible(struct kobject *kobj,
2093 struct attribute *attr, int index)
2095 struct device *dev = container_of(kobj, struct device, kobj);
2096 struct it87_data *data = dev_get_drvdata(dev);
2098 if ((index == 2 || index == 3) && !data->has_vid)
2101 if (index > 3 && !(data->in_internal & BIT(index - 4)))
2107 static struct attribute *it87_attributes[] = {
2108 &dev_attr_alarms.attr,
2109 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
2110 &dev_attr_vrm.attr, /* 2 */
2111 &dev_attr_cpu0_vid.attr, /* 3 */
2112 &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
2113 &sensor_dev_attr_in7_label.dev_attr.attr,
2114 &sensor_dev_attr_in8_label.dev_attr.attr,
2115 &sensor_dev_attr_in9_label.dev_attr.attr,
2119 static const struct attribute_group it87_group = {
2120 .attrs = it87_attributes,
2121 .is_visible = it87_is_visible,
2124 static umode_t it87_fan_is_visible(struct kobject *kobj,
2125 struct attribute *attr, int index)
2127 struct device *dev = container_of(kobj, struct device, kobj);
2128 struct it87_data *data = dev_get_drvdata(dev);
2129 int i = index / 5; /* fan index */
2130 int a = index % 5; /* attribute index */
2132 if (index >= 15) { /* fan 4..6 don't have divisor attributes */
2133 i = (index - 15) / 4 + 3;
2134 a = (index - 15) % 4;
2137 if (!(data->has_fan & BIT(i)))
2140 if (a == 3) { /* beep */
2141 if (!data->has_beep)
2143 /* first fan beep attribute is writable */
2144 if (i == __ffs(data->has_fan))
2145 return attr->mode | S_IWUSR;
2148 if (a == 4 && has_16bit_fans(data)) /* divisor */
2154 static struct attribute *it87_attributes_fan[] = {
2155 &sensor_dev_attr_fan1_input.dev_attr.attr,
2156 &sensor_dev_attr_fan1_min.dev_attr.attr,
2157 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
2158 &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
2159 &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
2161 &sensor_dev_attr_fan2_input.dev_attr.attr,
2162 &sensor_dev_attr_fan2_min.dev_attr.attr,
2163 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
2164 &sensor_dev_attr_fan2_beep.dev_attr.attr,
2165 &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
2167 &sensor_dev_attr_fan3_input.dev_attr.attr,
2168 &sensor_dev_attr_fan3_min.dev_attr.attr,
2169 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
2170 &sensor_dev_attr_fan3_beep.dev_attr.attr,
2171 &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
2173 &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
2174 &sensor_dev_attr_fan4_min.dev_attr.attr,
2175 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
2176 &sensor_dev_attr_fan4_beep.dev_attr.attr,
2178 &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
2179 &sensor_dev_attr_fan5_min.dev_attr.attr,
2180 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
2181 &sensor_dev_attr_fan5_beep.dev_attr.attr,
2183 &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
2184 &sensor_dev_attr_fan6_min.dev_attr.attr,
2185 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
2186 &sensor_dev_attr_fan6_beep.dev_attr.attr,
2190 static const struct attribute_group it87_group_fan = {
2191 .attrs = it87_attributes_fan,
2192 .is_visible = it87_fan_is_visible,
2195 static umode_t it87_pwm_is_visible(struct kobject *kobj,
2196 struct attribute *attr, int index)
2198 struct device *dev = container_of(kobj, struct device, kobj);
2199 struct it87_data *data = dev_get_drvdata(dev);
2200 int i = index / 4; /* pwm index */
2201 int a = index % 4; /* attribute index */
2203 if (!(data->has_pwm & BIT(i)))
2206 /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
2207 if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
2208 return attr->mode | S_IWUSR;
2210 /* pwm2_freq is writable if there are two pwm frequency selects */
2211 if (has_pwm_freq2(data) && i == 1 && a == 2)
2212 return attr->mode | S_IWUSR;
2217 static struct attribute *it87_attributes_pwm[] = {
2218 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2219 &sensor_dev_attr_pwm1.dev_attr.attr,
2220 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2221 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
2223 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2224 &sensor_dev_attr_pwm2.dev_attr.attr,
2225 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2226 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
2228 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
2229 &sensor_dev_attr_pwm3.dev_attr.attr,
2230 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
2231 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
2233 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
2234 &sensor_dev_attr_pwm4.dev_attr.attr,
2235 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
2236 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
2238 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
2239 &sensor_dev_attr_pwm5.dev_attr.attr,
2240 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
2241 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
2243 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
2244 &sensor_dev_attr_pwm6.dev_attr.attr,
2245 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
2246 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
2251 static const struct attribute_group it87_group_pwm = {
2252 .attrs = it87_attributes_pwm,
2253 .is_visible = it87_pwm_is_visible,
2256 static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
2257 struct attribute *attr, int index)
2259 struct device *dev = container_of(kobj, struct device, kobj);
2260 struct it87_data *data = dev_get_drvdata(dev);
2261 int i = index / 11; /* pwm index */
2262 int a = index % 11; /* attribute index */
2264 if (index >= 33) { /* pwm 4..6 */
2265 i = (index - 33) / 6 + 3;
2266 a = (index - 33) % 6 + 4;
2269 if (!(data->has_pwm & BIT(i)))
2272 if (has_newer_autopwm(data)) {
2273 if (a < 4) /* no auto point pwm */
2275 if (a == 8) /* no auto_point4 */
2278 if (has_old_autopwm(data)) {
2279 if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
2286 static struct attribute *it87_attributes_auto_pwm[] = {
2287 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
2288 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
2289 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
2290 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
2291 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
2292 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
2293 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
2294 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
2295 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
2296 &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
2297 &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
2299 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
2300 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
2301 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
2302 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
2303 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
2304 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
2305 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
2306 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
2307 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
2308 &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
2309 &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
2311 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
2312 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
2313 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
2314 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
2315 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
2316 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
2317 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
2318 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
2319 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
2320 &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
2321 &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
2323 &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
2324 &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
2325 &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
2326 &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
2327 &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
2328 &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
2330 &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
2331 &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
2332 &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
2333 &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
2334 &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
2335 &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
2337 &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
2338 &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
2339 &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
2340 &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
2341 &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
2342 &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
2347 static const struct attribute_group it87_group_auto_pwm = {
2348 .attrs = it87_attributes_auto_pwm,
2349 .is_visible = it87_auto_pwm_is_visible,
2352 /* SuperIO detection - will change isa_address if a chip is found */
2353 static int __init it87_find(int sioaddr, unsigned short *address,
2354 struct it87_sio_data *sio_data)
2358 const char *board_vendor, *board_name;
2359 const struct it87_devices *config;
2361 err = superio_enter(sioaddr);
2366 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
2368 switch (chip_type) {
2370 sio_data->type = it87;
2373 sio_data->type = it8712;
2377 sio_data->type = it8716;
2380 sio_data->type = it8718;
2383 sio_data->type = it8720;
2386 sio_data->type = it8721;
2389 sio_data->type = it8728;
2392 sio_data->type = it8732;
2395 sio_data->type = it8771;
2398 sio_data->type = it8772;
2401 sio_data->type = it8781;
2404 sio_data->type = it8782;
2407 sio_data->type = it8783;
2410 sio_data->type = it8786;
2413 sio_data->type = it8790;
2417 sio_data->type = it8603;
2420 sio_data->type = it8620;
2423 sio_data->type = it8628;
2425 case 0xffff: /* No device at all */
2428 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
2432 superio_select(sioaddr, PME);
2433 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
2434 pr_info("Device not activated, skipping\n");
2438 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
2439 if (*address == 0) {
2440 pr_info("Base address not set, skipping\n");
2445 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
2446 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
2447 it87_devices[sio_data->type].suffix,
2448 *address, sio_data->revision);
2450 config = &it87_devices[sio_data->type];
2452 /* in7 (VSB or VCCH5V) is always internal on some chips */
2453 if (has_in7_internal(config))
2454 sio_data->internal |= BIT(1);
2456 /* in8 (Vbat) is always internal */
2457 sio_data->internal |= BIT(2);
2459 /* in9 (AVCC3), always internal if supported */
2460 if (has_avcc3(config))
2461 sio_data->internal |= BIT(3); /* in9 is AVCC */
2463 sio_data->skip_in |= BIT(9);
2465 if (!has_six_pwm(config))
2466 sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
2468 if (!has_vid(config))
2469 sio_data->skip_vid = 1;
2471 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2472 if (sio_data->type == it87) {
2473 /* The IT8705F has a different LD number for GPIO */
2474 superio_select(sioaddr, 5);
2475 sio_data->beep_pin = superio_inb(sioaddr,
2476 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2477 } else if (sio_data->type == it8783) {
2478 int reg25, reg27, reg2a, reg2c, regef;
2480 superio_select(sioaddr, GPIO);
2482 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2483 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2484 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2485 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2486 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
2488 /* Check if fan3 is there or not */
2489 if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
2490 sio_data->skip_fan |= BIT(2);
2491 if ((reg25 & BIT(4)) ||
2492 (!(reg2a & BIT(1)) && (regef & BIT(0))))
2493 sio_data->skip_pwm |= BIT(2);
2495 /* Check if fan2 is there or not */
2497 sio_data->skip_fan |= BIT(1);
2499 sio_data->skip_pwm |= BIT(1);
2502 if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
2503 sio_data->skip_in |= BIT(5); /* No VIN5 */
2507 sio_data->skip_in |= BIT(6); /* No VIN6 */
2511 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2513 if (reg27 & BIT(2)) {
2515 * The data sheet is a bit unclear regarding the
2516 * internal voltage divider for VCCH5V. It says
2517 * "This bit enables and switches VIN7 (pin 91) to the
2518 * internal voltage divider for VCCH5V".
2519 * This is different to other chips, where the internal
2520 * voltage divider would connect VIN7 to an internal
2521 * voltage source. Maybe that is the case here as well.
2523 * Since we don't know for sure, re-route it if that is
2524 * not the case, and ask the user to report if the
2525 * resulting voltage is sane.
2527 if (!(reg2c & BIT(1))) {
2529 superio_outb(sioaddr, IT87_SIO_PINX2_REG,
2531 pr_notice("Routing internal VCCH5V to in7.\n");
2533 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2534 pr_notice("Please report if it displays a reasonable voltage.\n");
2538 sio_data->internal |= BIT(0);
2540 sio_data->internal |= BIT(1);
2542 sio_data->beep_pin = superio_inb(sioaddr,
2543 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2544 } else if (sio_data->type == it8603) {
2547 superio_select(sioaddr, GPIO);
2549 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2551 /* Check if fan3 is there or not */
2553 sio_data->skip_pwm |= BIT(2);
2555 sio_data->skip_fan |= BIT(2);
2557 /* Check if fan2 is there or not */
2558 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2560 sio_data->skip_pwm |= BIT(1);
2562 sio_data->skip_fan |= BIT(1);
2564 sio_data->skip_in |= BIT(5); /* No VIN5 */
2565 sio_data->skip_in |= BIT(6); /* No VIN6 */
2567 sio_data->beep_pin = superio_inb(sioaddr,
2568 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2569 } else if (sio_data->type == it8620 || sio_data->type == it8628) {
2572 superio_select(sioaddr, GPIO);
2574 /* Check for pwm5 */
2575 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2577 sio_data->skip_pwm |= BIT(4);
2579 /* Check for fan4, fan5 */
2580 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2581 if (!(reg & BIT(5)))
2582 sio_data->skip_fan |= BIT(3);
2583 if (!(reg & BIT(4)))
2584 sio_data->skip_fan |= BIT(4);
2586 /* Check for pwm3, fan3 */
2587 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2589 sio_data->skip_pwm |= BIT(2);
2591 sio_data->skip_fan |= BIT(2);
2593 /* Check for pwm4 */
2594 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
2595 if (!(reg & BIT(2)))
2596 sio_data->skip_pwm |= BIT(3);
2598 /* Check for pwm2, fan2 */
2599 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2601 sio_data->skip_pwm |= BIT(1);
2603 sio_data->skip_fan |= BIT(1);
2604 /* Check for pwm6, fan6 */
2605 if (!(reg & BIT(7))) {
2606 sio_data->skip_pwm |= BIT(5);
2607 sio_data->skip_fan |= BIT(5);
2610 sio_data->beep_pin = superio_inb(sioaddr,
2611 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2616 superio_select(sioaddr, GPIO);
2618 /* Check for fan4, fan5 */
2619 if (has_five_fans(config)) {
2620 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2621 switch (sio_data->type) {
2624 sio_data->skip_fan |= BIT(3);
2626 sio_data->skip_fan |= BIT(4);
2631 if (!(reg & BIT(5)))
2632 sio_data->skip_fan |= BIT(3);
2633 if (!(reg & BIT(4)))
2634 sio_data->skip_fan |= BIT(4);
2641 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2642 if (!sio_data->skip_vid) {
2643 /* We need at least 4 VID pins */
2645 pr_info("VID is disabled (pins used for GPIO)\n");
2646 sio_data->skip_vid = 1;
2650 /* Check if fan3 is there or not */
2652 sio_data->skip_pwm |= BIT(2);
2654 sio_data->skip_fan |= BIT(2);
2656 /* Check if fan2 is there or not */
2657 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2659 sio_data->skip_pwm |= BIT(1);
2661 sio_data->skip_fan |= BIT(1);
2663 if ((sio_data->type == it8718 || sio_data->type == it8720) &&
2664 !(sio_data->skip_vid))
2665 sio_data->vid_value = superio_inb(sioaddr,
2668 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2670 uart6 = sio_data->type == it8782 && (reg & BIT(2));
2673 * The IT8720F has no VIN7 pin, so VCCH should always be
2674 * routed internally to VIN7 with an internal divider.
2675 * Curiously, there still is a configuration bit to control
2676 * this, which means it can be set incorrectly. And even
2677 * more curiously, many boards out there are improperly
2678 * configured, even though the IT8720F datasheet claims
2679 * that the internal routing of VCCH to VIN7 is the default
2680 * setting. So we force the internal routing in this case.
2682 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2683 * If UART6 is enabled, re-route VIN7 to the internal divider
2684 * if that is not already the case.
2686 if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
2688 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
2689 pr_notice("Routing internal VCCH to in7\n");
2692 sio_data->internal |= BIT(0);
2694 sio_data->internal |= BIT(1);
2697 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2698 * While VIN7 can be routed to the internal voltage divider,
2699 * VIN5 and VIN6 are not available if UART6 is enabled.
2701 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2702 * is the temperature source. Since we can not read the
2703 * temperature source here, skip_temp is preliminary.
2706 sio_data->skip_in |= BIT(5) | BIT(6);
2707 sio_data->skip_temp |= BIT(2);
2710 sio_data->beep_pin = superio_inb(sioaddr,
2711 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2713 if (sio_data->beep_pin)
2714 pr_info("Beeping is supported\n");
2716 /* Disable specific features based on DMI strings */
2717 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2718 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2719 if (board_vendor && board_name) {
2720 if (strcmp(board_vendor, "nVIDIA") == 0 &&
2721 strcmp(board_name, "FN68PT") == 0) {
2723 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2724 * connected to a fan, but to something else. One user
2725 * has reported instant system power-off when changing
2726 * the PWM2 duty cycle, so we disable it.
2727 * I use the board name string as the trigger in case
2728 * the same board is ever used in other systems.
2730 pr_info("Disabling pwm2 due to hardware constraints\n");
2731 sio_data->skip_pwm = BIT(1);
2736 superio_exit(sioaddr);
2740 /* Called when we have found a new IT87. */
2741 static void it87_init_device(struct platform_device *pdev)
2743 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2744 struct it87_data *data = platform_get_drvdata(pdev);
2749 * For each PWM channel:
2750 * - If it is in automatic mode, setting to manual mode should set
2751 * the fan to full speed by default.
2752 * - If it is in manual mode, we need a mapping to temperature
2753 * channels to use when later setting to automatic mode later.
2754 * Use a 1:1 mapping by default (we are clueless.)
2755 * In both cases, the value can (and should) be changed by the user
2756 * prior to switching to a different mode.
2757 * Note that this is no longer needed for the IT8721F and later, as
2758 * these have separate registers for the temperature mapping and the
2759 * manual duty cycle.
2761 for (i = 0; i < NUM_AUTO_PWM; i++) {
2762 data->pwm_temp_map[i] = i;
2763 data->pwm_duty[i] = 0x7f; /* Full speed */
2764 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2768 * Some chips seem to have default value 0xff for all limit
2769 * registers. For low voltage limits it makes no sense and triggers
2770 * alarms, so change to 0 instead. For high temperature limits, it
2771 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2772 * but is still confusing, so change to 127 degrees C.
2774 for (i = 0; i < NUM_VIN_LIMIT; i++) {
2775 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2777 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2779 for (i = 0; i < NUM_TEMP_LIMIT; i++) {
2780 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2782 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2786 * Temperature channels are not forcibly enabled, as they can be
2787 * set to two different sensor types and we can't guess which one
2788 * is correct for a given system. These channels can be enabled at
2789 * run-time through the temp{1-3}_type sysfs accessors if needed.
2792 /* Check if voltage monitors are reset manually or by some reason */
2793 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2794 if ((tmp & 0xff) == 0) {
2795 /* Enable all voltage monitors */
2796 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2799 /* Check if tachometers are reset manually or by some reason */
2800 mask = 0x70 & ~(sio_data->skip_fan << 4);
2801 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2802 if ((data->fan_main_ctrl & mask) == 0) {
2803 /* Enable all fan tachometers */
2804 data->fan_main_ctrl |= mask;
2805 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2806 data->fan_main_ctrl);
2808 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2810 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2812 /* Set tachometers to 16-bit mode if needed */
2813 if (has_fan16_config(data)) {
2814 if (~tmp & 0x07 & data->has_fan) {
2816 "Setting fan1-3 to 16-bit mode\n");
2817 it87_write_value(data, IT87_REG_FAN_16BIT,
2822 /* Check for additional fans */
2823 if (has_five_fans(data)) {
2825 data->has_fan |= BIT(3); /* fan4 enabled */
2827 data->has_fan |= BIT(4); /* fan5 enabled */
2828 if (has_six_fans(data) && (tmp & BIT(2)))
2829 data->has_fan |= BIT(5); /* fan6 enabled */
2832 /* Fan input pins may be used for alternative functions */
2833 data->has_fan &= ~sio_data->skip_fan;
2835 /* Check if pwm5, pwm6 are enabled */
2836 if (has_six_pwm(data)) {
2837 /* The following code may be IT8620E specific */
2838 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2839 if ((tmp & 0xc0) == 0xc0)
2840 sio_data->skip_pwm |= BIT(4);
2841 if (!(tmp & BIT(3)))
2842 sio_data->skip_pwm |= BIT(5);
2845 /* Start monitoring */
2846 it87_write_value(data, IT87_REG_CONFIG,
2847 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2848 | (update_vbat ? 0x41 : 0x01));
2851 /* Return 1 if and only if the PWM interface is safe to use */
2852 static int it87_check_pwm(struct device *dev)
2854 struct it87_data *data = dev_get_drvdata(dev);
2856 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2857 * and polarity set to active low is sign that this is the case so we
2858 * disable pwm control to protect the user.
2860 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2862 if ((tmp & 0x87) == 0) {
2863 if (fix_pwm_polarity) {
2865 * The user asks us to attempt a chip reconfiguration.
2866 * This means switching to active high polarity and
2867 * inverting all fan speed values.
2872 for (i = 0; i < ARRAY_SIZE(pwm); i++)
2873 pwm[i] = it87_read_value(data,
2877 * If any fan is in automatic pwm mode, the polarity
2878 * might be correct, as suspicious as it seems, so we
2879 * better don't change anything (but still disable the
2882 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2884 "Reconfiguring PWM to active high polarity\n");
2885 it87_write_value(data, IT87_REG_FAN_CTL,
2887 for (i = 0; i < 3; i++)
2888 it87_write_value(data,
2895 "PWM configuration is too broken to be fixed\n");
2899 "Detected broken BIOS defaults, disabling PWM interface\n");
2901 } else if (fix_pwm_polarity) {
2903 "PWM configuration looks sane, won't touch\n");
2909 static int it87_probe(struct platform_device *pdev)
2911 struct it87_data *data;
2912 struct resource *res;
2913 struct device *dev = &pdev->dev;
2914 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2915 int enable_pwm_interface;
2916 struct device *hwmon_dev;
2918 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2919 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2921 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2922 (unsigned long)res->start,
2923 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2927 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2931 data->addr = res->start;
2932 data->type = sio_data->type;
2933 data->features = it87_devices[sio_data->type].features;
2934 data->peci_mask = it87_devices[sio_data->type].peci_mask;
2935 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
2937 * IT8705F Datasheet 0.4.1, 3h == Version G.
2938 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2939 * These are the first revisions with 16-bit tachometer support.
2941 switch (data->type) {
2943 if (sio_data->revision >= 0x03) {
2944 data->features &= ~FEAT_OLD_AUTOPWM;
2945 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
2949 if (sio_data->revision >= 0x08) {
2950 data->features &= ~FEAT_OLD_AUTOPWM;
2951 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2959 /* Now, we do the remaining detection. */
2960 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
2961 it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2964 platform_set_drvdata(pdev, data);
2966 mutex_init(&data->update_lock);
2968 /* Check PWM configuration */
2969 enable_pwm_interface = it87_check_pwm(dev);
2971 /* Starting with IT8721F, we handle scaling of internal voltages */
2972 if (has_12mv_adc(data)) {
2973 if (sio_data->internal & BIT(0))
2974 data->in_scaled |= BIT(3); /* in3 is AVCC */
2975 if (sio_data->internal & BIT(1))
2976 data->in_scaled |= BIT(7); /* in7 is VSB */
2977 if (sio_data->internal & BIT(2))
2978 data->in_scaled |= BIT(8); /* in8 is Vbat */
2979 if (sio_data->internal & BIT(3))
2980 data->in_scaled |= BIT(9); /* in9 is AVCC */
2981 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2982 sio_data->type == it8783) {
2983 if (sio_data->internal & BIT(0))
2984 data->in_scaled |= BIT(3); /* in3 is VCC5V */
2985 if (sio_data->internal & BIT(1))
2986 data->in_scaled |= BIT(7); /* in7 is VCCH5V */
2989 data->has_temp = 0x07;
2990 if (sio_data->skip_temp & BIT(2)) {
2991 if (sio_data->type == it8782 &&
2992 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2993 data->has_temp &= ~BIT(2);
2996 data->in_internal = sio_data->internal;
2997 data->has_in = 0x3ff & ~sio_data->skip_in;
2999 if (has_six_temp(data)) {
3000 u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
3002 /* Check for additional temperature sensors */
3003 if ((reg & 0x03) >= 0x02)
3004 data->has_temp |= BIT(3);
3005 if (((reg >> 2) & 0x03) >= 0x02)
3006 data->has_temp |= BIT(4);
3007 if (((reg >> 4) & 0x03) >= 0x02)
3008 data->has_temp |= BIT(5);
3010 /* Check for additional voltage sensors */
3011 if ((reg & 0x03) == 0x01)
3012 data->has_in |= BIT(10);
3013 if (((reg >> 2) & 0x03) == 0x01)
3014 data->has_in |= BIT(11);
3015 if (((reg >> 4) & 0x03) == 0x01)
3016 data->has_in |= BIT(12);
3019 data->has_beep = !!sio_data->beep_pin;
3021 /* Initialize the IT87 chip */
3022 it87_init_device(pdev);
3024 if (!sio_data->skip_vid) {
3025 data->has_vid = true;
3026 data->vrm = vid_which_vrm();
3027 /* VID reading from Super-I/O config space if available */
3028 data->vid = sio_data->vid_value;
3031 /* Prepare for sysfs hooks */
3032 data->groups[0] = &it87_group;
3033 data->groups[1] = &it87_group_in;
3034 data->groups[2] = &it87_group_temp;
3035 data->groups[3] = &it87_group_fan;
3037 if (enable_pwm_interface) {
3038 data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
3039 data->has_pwm &= ~sio_data->skip_pwm;
3041 data->groups[4] = &it87_group_pwm;
3042 if (has_old_autopwm(data) || has_newer_autopwm(data))
3043 data->groups[5] = &it87_group_auto_pwm;
3046 hwmon_dev = devm_hwmon_device_register_with_groups(dev,
3047 it87_devices[sio_data->type].name,
3048 data, data->groups);
3049 return PTR_ERR_OR_ZERO(hwmon_dev);
3052 static struct platform_driver it87_driver = {
3056 .probe = it87_probe,
3059 static int __init it87_device_add(int index, unsigned short address,
3060 const struct it87_sio_data *sio_data)
3062 struct platform_device *pdev;
3063 struct resource res = {
3064 .start = address + IT87_EC_OFFSET,
3065 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
3067 .flags = IORESOURCE_IO,
3071 err = acpi_check_resource_conflict(&res);
3075 pdev = platform_device_alloc(DRVNAME, address);
3079 err = platform_device_add_resources(pdev, &res, 1);
3081 pr_err("Device resource addition failed (%d)\n", err);
3082 goto exit_device_put;
3085 err = platform_device_add_data(pdev, sio_data,
3086 sizeof(struct it87_sio_data));
3088 pr_err("Platform data allocation failed\n");
3089 goto exit_device_put;
3092 err = platform_device_add(pdev);
3094 pr_err("Device addition failed (%d)\n", err);
3095 goto exit_device_put;
3098 it87_pdev[index] = pdev;
3102 platform_device_put(pdev);
3106 static int __init sm_it87_init(void)
3108 int sioaddr[2] = { REG_2E, REG_4E };
3109 struct it87_sio_data sio_data;
3110 unsigned short isa_address;
3114 err = platform_driver_register(&it87_driver);
3118 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
3119 memset(&sio_data, 0, sizeof(struct it87_sio_data));
3121 err = it87_find(sioaddr[i], &isa_address, &sio_data);
3122 if (err || isa_address == 0)
3125 err = it87_device_add(i, isa_address, &sio_data);
3127 goto exit_dev_unregister;
3133 goto exit_unregister;
3137 exit_dev_unregister:
3138 /* NULL check handled by platform_device_unregister */
3139 platform_device_unregister(it87_pdev[0]);
3141 platform_driver_unregister(&it87_driver);
3145 static void __exit sm_it87_exit(void)
3147 /* NULL check handled by platform_device_unregister */
3148 platform_device_unregister(it87_pdev[1]);
3149 platform_device_unregister(it87_pdev[0]);
3150 platform_driver_unregister(&it87_driver);
3153 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
3154 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
3155 module_param(update_vbat, bool, 0);
3156 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
3157 module_param(fix_pwm_polarity, bool, 0);
3158 MODULE_PARM_DESC(fix_pwm_polarity,
3159 "Force PWM polarity to active high (DANGEROUS)");
3160 MODULE_LICENSE("GPL");
3162 module_init(sm_it87_init);
3163 module_exit(sm_it87_exit);