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 * IT8705F Super I/O chip w/LPC interface
17 * IT8712F Super I/O chip w/LPC interface
18 * IT8716F Super I/O chip w/LPC interface
19 * IT8718F Super I/O chip w/LPC interface
20 * IT8720F Super I/O chip w/LPC interface
21 * IT8721F Super I/O chip w/LPC interface
22 * IT8726F Super I/O chip w/LPC interface
23 * IT8728F Super I/O chip w/LPC interface
24 * IT8758E Super I/O chip w/LPC interface
25 * IT8771E Super I/O chip w/LPC interface
26 * IT8772E Super I/O chip w/LPC interface
27 * IT8781F Super I/O chip w/LPC interface
28 * IT8782F Super I/O chip w/LPC interface
29 * IT8783E/F Super I/O chip w/LPC interface
30 * IT8786E Super I/O chip w/LPC interface
31 * IT8790E Super I/O chip w/LPC interface
32 * Sis950 A clone of the IT8705F
34 * Copyright (C) 2001 Chris Gauthron
35 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/jiffies.h>
58 #include <linux/platform_device.h>
59 #include <linux/hwmon.h>
60 #include <linux/hwmon-sysfs.h>
61 #include <linux/hwmon-vid.h>
62 #include <linux/err.h>
63 #include <linux/mutex.h>
64 #include <linux/sysfs.h>
65 #include <linux/string.h>
66 #include <linux/dmi.h>
67 #include <linux/acpi.h>
71 #define DRVNAME "it87"
73 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
74 it8772, it8781, it8782, it8783, it8786, it8790, it8603, it8620 };
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 *pdev;
82 #define REG 0x2e /* The register to read/write */
83 #define DEV 0x07 /* Register: Logical device select */
84 #define VAL 0x2f /* The value to read/write */
85 #define PME 0x04 /* The device with the fan registers in it */
87 /* The device with the IT8718F/IT8720F VID value in it */
90 #define DEVID 0x20 /* Register: Device ID */
91 #define DEVREV 0x22 /* Register: Device Revision */
93 static inline int superio_inb(int reg)
99 static inline void superio_outb(int reg, int val)
105 static int superio_inw(int reg)
115 static inline void superio_select(int ldn)
121 static inline int superio_enter(void)
124 * Try to reserve REG and REG + 1 for exclusive access.
126 if (!request_muxed_region(REG, 2, DRVNAME))
136 static inline void superio_exit(void)
140 release_region(REG, 2);
143 /* Logical device 4 registers */
144 #define IT8712F_DEVID 0x8712
145 #define IT8705F_DEVID 0x8705
146 #define IT8716F_DEVID 0x8716
147 #define IT8718F_DEVID 0x8718
148 #define IT8720F_DEVID 0x8720
149 #define IT8721F_DEVID 0x8721
150 #define IT8726F_DEVID 0x8726
151 #define IT8728F_DEVID 0x8728
152 #define IT8771E_DEVID 0x8771
153 #define IT8772E_DEVID 0x8772
154 #define IT8781F_DEVID 0x8781
155 #define IT8782F_DEVID 0x8782
156 #define IT8783E_DEVID 0x8783
157 #define IT8786E_DEVID 0x8786
158 #define IT8603E_DEVID 0x8603
159 #define IT8620E_DEVID 0x8620
160 #define IT8623E_DEVID 0x8623
161 #define IT8790E_DEVID 0x8790
162 #define IT87_ACT_REG 0x30
163 #define IT87_BASE_REG 0x60
165 /* Logical device 7 registers (IT8712F and later) */
166 #define IT87_SIO_GPIO1_REG 0x25
167 #define IT87_SIO_GPIO2_REG 0x26
168 #define IT87_SIO_GPIO3_REG 0x27
169 #define IT87_SIO_GPIO4_REG 0x28
170 #define IT87_SIO_GPIO5_REG 0x29
171 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
172 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
173 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
174 #define IT87_SIO_VID_REG 0xfc /* VID value */
175 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
177 /* Update battery voltage after every reading if true */
178 static bool update_vbat;
180 /* Not all BIOSes properly configure the PWM registers */
181 static bool fix_pwm_polarity;
183 /* Many IT87 constants specified below */
185 /* Length of ISA address segment */
186 #define IT87_EXTENT 8
188 /* Length of ISA address segment for Environmental Controller */
189 #define IT87_EC_EXTENT 2
191 /* Offset of EC registers from ISA base address */
192 #define IT87_EC_OFFSET 5
194 /* Where are the ISA address/data registers relative to the EC base address */
195 #define IT87_ADDR_REG_OFFSET 0
196 #define IT87_DATA_REG_OFFSET 1
198 /*----- The IT87 registers -----*/
200 #define IT87_REG_CONFIG 0x00
202 #define IT87_REG_ALARM1 0x01
203 #define IT87_REG_ALARM2 0x02
204 #define IT87_REG_ALARM3 0x03
207 * The IT8718F and IT8720F have the VID value in a different register, in
208 * Super-I/O configuration space.
210 #define IT87_REG_VID 0x0a
212 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
213 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
216 #define IT87_REG_FAN_DIV 0x0b
217 #define IT87_REG_FAN_16BIT 0x0c
219 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
221 static const u8 IT87_REG_FAN[] = {0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c};
222 static const u8 IT87_REG_FAN_MIN[] = {0x10, 0x11, 0x12, 0x84, 0x86, 0x4e};
223 static const u8 IT87_REG_FANX[] = {0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d};
224 static const u8 IT87_REG_FANX_MIN[] = {0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f};
225 static const u8 IT87_REG_TEMP_OFFSET[] = {0x56, 0x57, 0x59};
227 #define IT87_REG_FAN_MAIN_CTRL 0x13
228 #define IT87_REG_FAN_CTL 0x14
229 static const u8 IT87_REG_PWM[] = {0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf};
230 static const u8 IT87_REG_PWM_DUTY[] = {0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab};
232 #define IT87_REG_VIN(nr) (0x20 + (nr))
233 #define IT87_REG_TEMP(nr) (0x29 + (nr))
235 #define IT87_REG_AVCC3 0x2f
237 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
238 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
239 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
240 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
242 #define IT87_REG_VIN_ENABLE 0x50
243 #define IT87_REG_TEMP_ENABLE 0x51
244 #define IT87_REG_TEMP_EXTRA 0x55
245 #define IT87_REG_BEEP_ENABLE 0x5c
247 #define IT87_REG_CHIPID 0x58
249 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
250 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
252 struct it87_devices {
257 const char * const suffix;
260 #define FEAT_12MV_ADC (1 << 0)
261 #define FEAT_NEWER_AUTOPWM (1 << 1)
262 #define FEAT_OLD_AUTOPWM (1 << 2)
263 #define FEAT_16BIT_FANS (1 << 3)
264 #define FEAT_TEMP_OFFSET (1 << 4)
265 #define FEAT_TEMP_PECI (1 << 5)
266 #define FEAT_TEMP_OLD_PECI (1 << 6)
267 #define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
268 #define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
269 #define FEAT_SIX_FANS (1 << 9) /* Supports six fans */
270 #define FEAT_VID (1 << 10) /* Set if chip supports VID */
271 #define FEAT_IN7_INTERNAL (1 << 11) /* Set if in7 is internal */
272 #define FEAT_AVCC3 (1 << 12) /* Chip supports in9/AVCC3 */
273 #define FEAT_SIX_PWM (1 << 13) /* Chip supports 6 pwm chn */
274 #define FEAT_PWM_FREQ2 (1 << 14) /* Separate pwm freq 2 */
276 static const struct it87_devices it87_devices[] = {
279 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
284 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
285 /* may need to overwrite */
290 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
291 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
296 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
297 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
299 .old_peci_mask = 0x4,
304 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
305 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
307 .old_peci_mask = 0x4,
312 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
313 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
314 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
317 .old_peci_mask = 0x02, /* Actually reports PCH */
322 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
323 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
324 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
330 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
331 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
333 /* PECI: guesswork */
335 /* 16 bit fans (OHM) */
336 /* three fans, always 16 bit (guesswork) */
342 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
343 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
345 /* PECI (coreboot) */
346 /* 12mV ADC (HWSensors4, OHM) */
347 /* 16 bit fans (HWSensors4, OHM) */
348 /* three fans, always 16 bit (datasheet) */
354 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
355 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
356 .old_peci_mask = 0x4,
361 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
362 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
363 .old_peci_mask = 0x4,
368 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
369 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
370 .old_peci_mask = 0x4,
375 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
376 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
383 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
384 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
391 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
392 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
393 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
399 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
400 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
401 | FEAT_IN7_INTERNAL | FEAT_AVCC3 | FEAT_SIX_PWM
408 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
409 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
410 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
411 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
412 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
413 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
414 ((data)->peci_mask & (1 << nr)))
415 #define has_temp_old_peci(data, nr) \
416 (((data)->features & FEAT_TEMP_OLD_PECI) && \
417 ((data)->old_peci_mask & (1 << nr)))
418 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
419 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
421 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
422 #define has_vid(data) ((data)->features & FEAT_VID)
423 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
424 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
425 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
426 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
428 struct it87_sio_data {
430 /* Values read from Super-I/O config space */
434 u8 internal; /* Internal sensors can be labeled */
435 /* Features skipped based on config or DMI */
444 * For each registered chip, we need to keep some data in memory.
445 * The structure is dynamically allocated.
448 struct device *hwmon_dev;
456 struct mutex update_lock;
457 char valid; /* !=0 if following fields are valid */
458 unsigned long last_updated; /* In jiffies */
460 u16 in_scaled; /* Internal voltage sensors are scaled */
461 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
462 u8 has_fan; /* Bitfield, fans enabled */
463 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
464 u8 has_temp; /* Bitfield, temp sensors enabled */
465 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
466 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
467 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
468 u8 fan_div[3]; /* Register encoding, shifted right */
469 u8 vid; /* Register encoding, combined */
471 u32 alarms; /* Register encoding, combined */
472 u8 beeps; /* Register encoding */
473 u8 fan_main_ctrl; /* Register value */
474 u8 fan_ctl; /* Register value */
477 * The following 3 arrays correspond to the same registers up to
478 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
479 * 7, and we want to preserve settings on mode changes, so we have
480 * to track all values separately.
481 * Starting with the IT8721F, the manual PWM duty cycles are stored
482 * in separate registers (8-bit values), so the separate tracking
483 * is no longer needed, but it is still done to keep the driver
486 u8 pwm_ctrl[6]; /* Register value */
487 u8 pwm_duty[6]; /* Manual PWM value set by user */
488 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
490 /* Automatic fan speed control registers */
491 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
492 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
495 static int adc_lsb(const struct it87_data *data, int nr)
497 int lsb = has_12mv_adc(data) ? 12 : 16;
498 if (data->in_scaled & (1 << nr))
503 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
505 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
506 return clamp_val(val, 0, 255);
509 static int in_from_reg(const struct it87_data *data, int nr, int val)
511 return val * adc_lsb(data, nr);
514 static inline u8 FAN_TO_REG(long rpm, int div)
518 rpm = clamp_val(rpm, 1, 1000000);
519 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
522 static inline u16 FAN16_TO_REG(long rpm)
526 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
529 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
530 1350000 / ((val) * (div)))
531 /* The divider is fixed to 2 in 16-bit mode */
532 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
533 1350000 / ((val) * 2))
535 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
536 ((val) + 500) / 1000), -128, 127))
537 #define TEMP_FROM_REG(val) ((val) * 1000)
539 static u8 pwm_to_reg(const struct it87_data *data, long val)
541 if (has_newer_autopwm(data))
547 static int pwm_from_reg(const struct it87_data *data, u8 reg)
549 if (has_newer_autopwm(data))
552 return (reg & 0x7f) << 1;
556 static int DIV_TO_REG(int val)
559 while (answer < 7 && (val >>= 1))
563 #define DIV_FROM_REG(val) (1 << (val))
565 static const unsigned int pwm_freq[8] = {
576 static int it87_probe(struct platform_device *pdev);
577 static int it87_remove(struct platform_device *pdev);
579 static int it87_read_value(struct it87_data *data, u8 reg);
580 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
581 static struct it87_data *it87_update_device(struct device *dev);
582 static int it87_check_pwm(struct device *dev);
583 static void it87_init_device(struct platform_device *pdev);
586 static struct platform_driver it87_driver = {
591 .remove = it87_remove,
594 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
597 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
599 int index = sattr->index;
601 struct it87_data *data = it87_update_device(dev);
602 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
605 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
608 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
610 int index = sattr->index;
612 struct it87_data *data = dev_get_drvdata(dev);
615 if (kstrtoul(buf, 10, &val) < 0)
618 mutex_lock(&data->update_lock);
619 data->in[nr][index] = in_to_reg(data, nr, val);
620 it87_write_value(data,
621 index == 1 ? IT87_REG_VIN_MIN(nr)
622 : IT87_REG_VIN_MAX(nr),
623 data->in[nr][index]);
624 mutex_unlock(&data->update_lock);
628 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
629 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
631 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
634 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
635 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
637 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
640 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
641 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
643 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
646 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
647 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
649 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
652 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
653 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
655 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
658 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
659 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
661 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
664 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
665 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
667 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
670 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
671 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
673 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
676 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
677 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
680 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
683 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
685 int index = sattr->index;
686 struct it87_data *data = it87_update_device(dev);
688 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
691 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
692 const char *buf, size_t count)
694 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
696 int index = sattr->index;
697 struct it87_data *data = dev_get_drvdata(dev);
701 if (kstrtol(buf, 10, &val) < 0)
704 mutex_lock(&data->update_lock);
709 reg = IT87_REG_TEMP_LOW(nr);
712 reg = IT87_REG_TEMP_HIGH(nr);
715 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
716 if (!(regval & 0x80)) {
718 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
721 reg = IT87_REG_TEMP_OFFSET[nr];
725 data->temp[nr][index] = TEMP_TO_REG(val);
726 it87_write_value(data, reg, data->temp[nr][index]);
727 mutex_unlock(&data->update_lock);
731 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
732 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
734 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
736 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
738 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
739 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
741 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
743 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
745 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
746 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
748 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
750 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
753 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
756 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
757 int nr = sensor_attr->index;
758 struct it87_data *data = it87_update_device(dev);
759 u8 reg = data->sensor; /* In case value is updated while used */
760 u8 extra = data->extra;
762 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
763 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
764 return sprintf(buf, "6\n"); /* Intel PECI */
766 return sprintf(buf, "3\n"); /* thermal diode */
768 return sprintf(buf, "4\n"); /* thermistor */
769 return sprintf(buf, "0\n"); /* disabled */
772 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
775 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
776 int nr = sensor_attr->index;
778 struct it87_data *data = dev_get_drvdata(dev);
782 if (kstrtol(buf, 10, &val) < 0)
785 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
788 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
790 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
791 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
793 if (val == 2) { /* backwards compatibility */
795 "Sensor type 2 is deprecated, please use 4 instead\n");
798 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
803 else if (has_temp_peci(data, nr) && val == 6)
804 reg |= (nr + 1) << 6;
805 else if (has_temp_old_peci(data, nr) && val == 6)
810 mutex_lock(&data->update_lock);
813 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
814 if (has_temp_old_peci(data, nr))
815 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
816 data->valid = 0; /* Force cache refresh */
817 mutex_unlock(&data->update_lock);
821 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
823 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
825 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
830 static int pwm_mode(const struct it87_data *data, int nr)
832 int ctrl = data->fan_main_ctrl & (1 << nr);
834 if (ctrl == 0 && data->type != it8603) /* Full speed */
836 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
838 else /* Manual mode */
842 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
845 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
847 int index = sattr->index;
849 struct it87_data *data = it87_update_device(dev);
851 speed = has_16bit_fans(data) ?
852 FAN16_FROM_REG(data->fan[nr][index]) :
853 FAN_FROM_REG(data->fan[nr][index],
854 DIV_FROM_REG(data->fan_div[nr]));
855 return sprintf(buf, "%d\n", speed);
858 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
861 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
862 int nr = sensor_attr->index;
864 struct it87_data *data = it87_update_device(dev);
865 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
867 static ssize_t show_pwm_enable(struct device *dev,
868 struct device_attribute *attr, char *buf)
870 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
871 int nr = sensor_attr->index;
873 struct it87_data *data = it87_update_device(dev);
874 return sprintf(buf, "%d\n", pwm_mode(data, nr));
876 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
879 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
880 int nr = sensor_attr->index;
882 struct it87_data *data = it87_update_device(dev);
883 return sprintf(buf, "%d\n",
884 pwm_from_reg(data, data->pwm_duty[nr]));
886 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
889 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
890 struct it87_data *data = it87_update_device(dev);
891 int nr = sensor_attr->index;
895 if (has_pwm_freq2(data) && nr == 1)
896 index = (data->extra >> 4) & 0x07;
898 index = (data->fan_ctl >> 4) & 0x07;
900 freq = pwm_freq[index] / has_newer_autopwm(data) ? 256 : 128;
902 return sprintf(buf, "%u\n", freq);
905 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
906 const char *buf, size_t count)
908 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
910 int index = sattr->index;
912 struct it87_data *data = dev_get_drvdata(dev);
916 if (kstrtol(buf, 10, &val) < 0)
919 mutex_lock(&data->update_lock);
921 if (has_16bit_fans(data)) {
922 data->fan[nr][index] = FAN16_TO_REG(val);
923 it87_write_value(data, IT87_REG_FAN_MIN[nr],
924 data->fan[nr][index] & 0xff);
925 it87_write_value(data, IT87_REG_FANX_MIN[nr],
926 data->fan[nr][index] >> 8);
928 reg = it87_read_value(data, IT87_REG_FAN_DIV);
931 data->fan_div[nr] = reg & 0x07;
934 data->fan_div[nr] = (reg >> 3) & 0x07;
937 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
940 data->fan[nr][index] =
941 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
942 it87_write_value(data, IT87_REG_FAN_MIN[nr],
943 data->fan[nr][index]);
946 mutex_unlock(&data->update_lock);
950 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
951 const char *buf, size_t count)
953 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
954 int nr = sensor_attr->index;
956 struct it87_data *data = dev_get_drvdata(dev);
961 if (kstrtoul(buf, 10, &val) < 0)
964 mutex_lock(&data->update_lock);
965 old = it87_read_value(data, IT87_REG_FAN_DIV);
967 /* Save fan min limit */
968 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
973 data->fan_div[nr] = DIV_TO_REG(val);
977 data->fan_div[nr] = 1;
979 data->fan_div[nr] = 3;
982 val |= (data->fan_div[0] & 0x07);
983 val |= (data->fan_div[1] & 0x07) << 3;
984 if (data->fan_div[2] == 3)
986 it87_write_value(data, IT87_REG_FAN_DIV, val);
988 /* Restore fan min limit */
989 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
990 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
992 mutex_unlock(&data->update_lock);
996 /* Returns 0 if OK, -EINVAL otherwise */
997 static int check_trip_points(struct device *dev, int nr)
999 const struct it87_data *data = dev_get_drvdata(dev);
1002 if (has_old_autopwm(data)) {
1003 for (i = 0; i < 3; i++) {
1004 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1007 for (i = 0; i < 2; i++) {
1008 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1015 "Inconsistent trip points, not switching to automatic mode\n");
1016 dev_err(dev, "Adjust the trip points and try again\n");
1021 static ssize_t set_pwm_enable(struct device *dev,
1022 struct device_attribute *attr, const char *buf, size_t count)
1024 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1025 int nr = sensor_attr->index;
1027 struct it87_data *data = dev_get_drvdata(dev);
1030 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1033 /* Check trip points before switching to automatic mode */
1035 if (check_trip_points(dev, nr) < 0)
1039 /* IT8603E does not have on/off mode */
1040 if (val == 0 && data->type == it8603)
1043 mutex_lock(&data->update_lock);
1047 /* make sure the fan is on when in on/off mode */
1048 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1049 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
1050 /* set on/off mode */
1051 data->fan_main_ctrl &= ~(1 << nr);
1052 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1053 data->fan_main_ctrl);
1055 if (val == 1) /* Manual mode */
1056 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1057 data->pwm_temp_map[nr] :
1059 else /* Automatic mode */
1060 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1061 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1063 if (data->type != it8603) {
1064 /* set SmartGuardian mode */
1065 data->fan_main_ctrl |= (1 << nr);
1066 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1067 data->fan_main_ctrl);
1071 mutex_unlock(&data->update_lock);
1074 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1075 const char *buf, size_t count)
1077 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1078 int nr = sensor_attr->index;
1080 struct it87_data *data = dev_get_drvdata(dev);
1083 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1086 mutex_lock(&data->update_lock);
1087 if (has_newer_autopwm(data)) {
1089 * If we are in automatic mode, the PWM duty cycle register
1090 * is read-only so we can't write the value.
1092 if (data->pwm_ctrl[nr] & 0x80) {
1093 mutex_unlock(&data->update_lock);
1096 data->pwm_duty[nr] = pwm_to_reg(data, val);
1097 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1098 data->pwm_duty[nr]);
1100 data->pwm_duty[nr] = pwm_to_reg(data, val);
1102 * If we are in manual mode, write the duty cycle immediately;
1103 * otherwise, just store it for later use.
1105 if (!(data->pwm_ctrl[nr] & 0x80)) {
1106 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1107 it87_write_value(data, IT87_REG_PWM[nr],
1108 data->pwm_ctrl[nr]);
1111 mutex_unlock(&data->update_lock);
1114 static ssize_t set_pwm_freq(struct device *dev,
1115 struct device_attribute *attr, const char *buf, size_t count)
1117 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1118 struct it87_data *data = dev_get_drvdata(dev);
1119 int nr = sensor_attr->index;
1123 if (kstrtoul(buf, 10, &val) < 0)
1126 val = clamp_val(val, 0, 1000000);
1127 val *= has_newer_autopwm(data) ? 256 : 128;
1129 /* Search for the nearest available frequency */
1130 for (i = 0; i < 7; i++) {
1131 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1135 mutex_lock(&data->update_lock);
1137 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1138 data->fan_ctl |= i << 4;
1139 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1141 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1142 data->extra |= i << 4;
1143 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1145 mutex_unlock(&data->update_lock);
1149 static ssize_t show_pwm_temp_map(struct device *dev,
1150 struct device_attribute *attr, char *buf)
1152 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1153 int nr = sensor_attr->index;
1155 struct it87_data *data = it87_update_device(dev);
1158 if (data->pwm_temp_map[nr] < 3)
1159 map = 1 << data->pwm_temp_map[nr];
1161 map = 0; /* Should never happen */
1162 return sprintf(buf, "%d\n", map);
1164 static ssize_t set_pwm_temp_map(struct device *dev,
1165 struct device_attribute *attr, const char *buf, size_t count)
1167 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1168 int nr = sensor_attr->index;
1170 struct it87_data *data = dev_get_drvdata(dev);
1175 * This check can go away if we ever support automatic fan speed
1176 * control on newer chips.
1178 if (!has_old_autopwm(data)) {
1179 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1183 if (kstrtol(buf, 10, &val) < 0)
1200 mutex_lock(&data->update_lock);
1201 data->pwm_temp_map[nr] = reg;
1203 * If we are in automatic mode, write the temp mapping immediately;
1204 * otherwise, just store it for later use.
1206 if (data->pwm_ctrl[nr] & 0x80) {
1207 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1208 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1210 mutex_unlock(&data->update_lock);
1214 static ssize_t show_auto_pwm(struct device *dev,
1215 struct device_attribute *attr, char *buf)
1217 struct it87_data *data = it87_update_device(dev);
1218 struct sensor_device_attribute_2 *sensor_attr =
1219 to_sensor_dev_attr_2(attr);
1220 int nr = sensor_attr->nr;
1221 int point = sensor_attr->index;
1223 return sprintf(buf, "%d\n",
1224 pwm_from_reg(data, data->auto_pwm[nr][point]));
1227 static ssize_t set_auto_pwm(struct device *dev,
1228 struct device_attribute *attr, const char *buf, size_t count)
1230 struct it87_data *data = dev_get_drvdata(dev);
1231 struct sensor_device_attribute_2 *sensor_attr =
1232 to_sensor_dev_attr_2(attr);
1233 int nr = sensor_attr->nr;
1234 int point = sensor_attr->index;
1237 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1240 mutex_lock(&data->update_lock);
1241 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1242 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1243 data->auto_pwm[nr][point]);
1244 mutex_unlock(&data->update_lock);
1248 static ssize_t show_auto_temp(struct device *dev,
1249 struct device_attribute *attr, char *buf)
1251 struct it87_data *data = it87_update_device(dev);
1252 struct sensor_device_attribute_2 *sensor_attr =
1253 to_sensor_dev_attr_2(attr);
1254 int nr = sensor_attr->nr;
1255 int point = sensor_attr->index;
1257 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1260 static ssize_t set_auto_temp(struct device *dev,
1261 struct device_attribute *attr, const char *buf, size_t count)
1263 struct it87_data *data = dev_get_drvdata(dev);
1264 struct sensor_device_attribute_2 *sensor_attr =
1265 to_sensor_dev_attr_2(attr);
1266 int nr = sensor_attr->nr;
1267 int point = sensor_attr->index;
1270 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1273 mutex_lock(&data->update_lock);
1274 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1275 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1276 data->auto_temp[nr][point]);
1277 mutex_unlock(&data->update_lock);
1281 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1282 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1284 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1287 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1288 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1290 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1293 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1294 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1296 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1299 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1300 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1303 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1304 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1307 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1308 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1311 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1312 show_pwm_enable, set_pwm_enable, 0);
1313 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1314 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1316 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1317 show_pwm_temp_map, set_pwm_temp_map, 0);
1318 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1319 show_auto_pwm, set_auto_pwm, 0, 0);
1320 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1321 show_auto_pwm, set_auto_pwm, 0, 1);
1322 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1323 show_auto_pwm, set_auto_pwm, 0, 2);
1324 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1325 show_auto_pwm, NULL, 0, 3);
1326 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1327 show_auto_temp, set_auto_temp, 0, 1);
1328 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1329 show_auto_temp, set_auto_temp, 0, 0);
1330 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1331 show_auto_temp, set_auto_temp, 0, 2);
1332 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1333 show_auto_temp, set_auto_temp, 0, 3);
1334 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1335 show_auto_temp, set_auto_temp, 0, 4);
1337 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1338 show_pwm_enable, set_pwm_enable, 1);
1339 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1340 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1341 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1342 show_pwm_temp_map, set_pwm_temp_map, 1);
1343 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1344 show_auto_pwm, set_auto_pwm, 1, 0);
1345 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1346 show_auto_pwm, set_auto_pwm, 1, 1);
1347 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1348 show_auto_pwm, set_auto_pwm, 1, 2);
1349 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1350 show_auto_pwm, NULL, 1, 3);
1351 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1352 show_auto_temp, set_auto_temp, 1, 1);
1353 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1354 show_auto_temp, set_auto_temp, 1, 0);
1355 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1356 show_auto_temp, set_auto_temp, 1, 2);
1357 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1358 show_auto_temp, set_auto_temp, 1, 3);
1359 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1360 show_auto_temp, set_auto_temp, 1, 4);
1362 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1363 show_pwm_enable, set_pwm_enable, 2);
1364 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1365 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1366 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1367 show_pwm_temp_map, set_pwm_temp_map, 2);
1368 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1369 show_auto_pwm, set_auto_pwm, 2, 0);
1370 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1371 show_auto_pwm, set_auto_pwm, 2, 1);
1372 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1373 show_auto_pwm, set_auto_pwm, 2, 2);
1374 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1375 show_auto_pwm, NULL, 2, 3);
1376 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1377 show_auto_temp, set_auto_temp, 2, 1);
1378 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1379 show_auto_temp, set_auto_temp, 2, 0);
1380 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1381 show_auto_temp, set_auto_temp, 2, 2);
1382 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1383 show_auto_temp, set_auto_temp, 2, 3);
1384 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1385 show_auto_temp, set_auto_temp, 2, 4);
1387 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1388 show_pwm_enable, set_pwm_enable, 3);
1389 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1390 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1391 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO | S_IWUSR,
1392 show_pwm_temp_map, set_pwm_temp_map, 3);
1394 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1395 show_pwm_enable, set_pwm_enable, 4);
1396 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1397 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1398 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO | S_IWUSR,
1399 show_pwm_temp_map, set_pwm_temp_map, 4);
1401 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1402 show_pwm_enable, set_pwm_enable, 5);
1403 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1404 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1405 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO | S_IWUSR,
1406 show_pwm_temp_map, set_pwm_temp_map, 5);
1409 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1412 struct it87_data *data = it87_update_device(dev);
1413 return sprintf(buf, "%u\n", data->alarms);
1415 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1417 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1420 int bitnr = to_sensor_dev_attr(attr)->index;
1421 struct it87_data *data = it87_update_device(dev);
1422 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1425 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1426 *attr, const char *buf, size_t count)
1428 struct it87_data *data = dev_get_drvdata(dev);
1432 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1435 mutex_lock(&data->update_lock);
1436 config = it87_read_value(data, IT87_REG_CONFIG);
1441 it87_write_value(data, IT87_REG_CONFIG, config);
1442 /* Invalidate cache to force re-read */
1445 mutex_unlock(&data->update_lock);
1450 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1451 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1452 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1453 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1454 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1455 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1456 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1457 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1458 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1459 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1460 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1461 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1462 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1463 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1464 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1465 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1466 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1467 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1468 show_alarm, clear_intrusion, 4);
1470 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1473 int bitnr = to_sensor_dev_attr(attr)->index;
1474 struct it87_data *data = it87_update_device(dev);
1475 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1477 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1478 const char *buf, size_t count)
1480 int bitnr = to_sensor_dev_attr(attr)->index;
1481 struct it87_data *data = dev_get_drvdata(dev);
1484 if (kstrtol(buf, 10, &val) < 0
1485 || (val != 0 && val != 1))
1488 mutex_lock(&data->update_lock);
1489 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1491 data->beeps |= (1 << bitnr);
1493 data->beeps &= ~(1 << bitnr);
1494 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1495 mutex_unlock(&data->update_lock);
1499 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1500 show_beep, set_beep, 1);
1501 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1502 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1503 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1504 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1505 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1506 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1507 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1508 /* fanX_beep writability is set later */
1509 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1510 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1511 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1512 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1513 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1514 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1515 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1516 show_beep, set_beep, 2);
1517 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1518 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1520 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1523 struct it87_data *data = dev_get_drvdata(dev);
1524 return sprintf(buf, "%u\n", data->vrm);
1526 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1527 const char *buf, size_t count)
1529 struct it87_data *data = dev_get_drvdata(dev);
1532 if (kstrtoul(buf, 10, &val) < 0)
1539 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1541 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1544 struct it87_data *data = it87_update_device(dev);
1545 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1547 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1549 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1552 static const char * const labels[] = {
1557 static const char * const labels_it8721[] = {
1562 struct it87_data *data = dev_get_drvdata(dev);
1563 int nr = to_sensor_dev_attr(attr)->index;
1565 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1568 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1569 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1570 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1572 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1574 static ssize_t show_name(struct device *dev, struct device_attribute
1575 *devattr, char *buf)
1577 struct it87_data *data = dev_get_drvdata(dev);
1578 return sprintf(buf, "%s\n", data->name);
1580 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1582 static struct attribute *it87_attributes_in[10][5] = {
1584 &sensor_dev_attr_in0_input.dev_attr.attr,
1585 &sensor_dev_attr_in0_min.dev_attr.attr,
1586 &sensor_dev_attr_in0_max.dev_attr.attr,
1587 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1590 &sensor_dev_attr_in1_input.dev_attr.attr,
1591 &sensor_dev_attr_in1_min.dev_attr.attr,
1592 &sensor_dev_attr_in1_max.dev_attr.attr,
1593 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1596 &sensor_dev_attr_in2_input.dev_attr.attr,
1597 &sensor_dev_attr_in2_min.dev_attr.attr,
1598 &sensor_dev_attr_in2_max.dev_attr.attr,
1599 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1602 &sensor_dev_attr_in3_input.dev_attr.attr,
1603 &sensor_dev_attr_in3_min.dev_attr.attr,
1604 &sensor_dev_attr_in3_max.dev_attr.attr,
1605 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1608 &sensor_dev_attr_in4_input.dev_attr.attr,
1609 &sensor_dev_attr_in4_min.dev_attr.attr,
1610 &sensor_dev_attr_in4_max.dev_attr.attr,
1611 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1614 &sensor_dev_attr_in5_input.dev_attr.attr,
1615 &sensor_dev_attr_in5_min.dev_attr.attr,
1616 &sensor_dev_attr_in5_max.dev_attr.attr,
1617 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1620 &sensor_dev_attr_in6_input.dev_attr.attr,
1621 &sensor_dev_attr_in6_min.dev_attr.attr,
1622 &sensor_dev_attr_in6_max.dev_attr.attr,
1623 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1626 &sensor_dev_attr_in7_input.dev_attr.attr,
1627 &sensor_dev_attr_in7_min.dev_attr.attr,
1628 &sensor_dev_attr_in7_max.dev_attr.attr,
1629 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1632 &sensor_dev_attr_in8_input.dev_attr.attr,
1635 &sensor_dev_attr_in9_input.dev_attr.attr,
1639 static const struct attribute_group it87_group_in[10] = {
1640 { .attrs = it87_attributes_in[0] },
1641 { .attrs = it87_attributes_in[1] },
1642 { .attrs = it87_attributes_in[2] },
1643 { .attrs = it87_attributes_in[3] },
1644 { .attrs = it87_attributes_in[4] },
1645 { .attrs = it87_attributes_in[5] },
1646 { .attrs = it87_attributes_in[6] },
1647 { .attrs = it87_attributes_in[7] },
1648 { .attrs = it87_attributes_in[8] },
1649 { .attrs = it87_attributes_in[9] },
1652 static struct attribute *it87_attributes_temp[3][6] = {
1654 &sensor_dev_attr_temp1_input.dev_attr.attr,
1655 &sensor_dev_attr_temp1_max.dev_attr.attr,
1656 &sensor_dev_attr_temp1_min.dev_attr.attr,
1657 &sensor_dev_attr_temp1_type.dev_attr.attr,
1658 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1661 &sensor_dev_attr_temp2_input.dev_attr.attr,
1662 &sensor_dev_attr_temp2_max.dev_attr.attr,
1663 &sensor_dev_attr_temp2_min.dev_attr.attr,
1664 &sensor_dev_attr_temp2_type.dev_attr.attr,
1665 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1668 &sensor_dev_attr_temp3_input.dev_attr.attr,
1669 &sensor_dev_attr_temp3_max.dev_attr.attr,
1670 &sensor_dev_attr_temp3_min.dev_attr.attr,
1671 &sensor_dev_attr_temp3_type.dev_attr.attr,
1672 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1676 static const struct attribute_group it87_group_temp[3] = {
1677 { .attrs = it87_attributes_temp[0] },
1678 { .attrs = it87_attributes_temp[1] },
1679 { .attrs = it87_attributes_temp[2] },
1682 static struct attribute *it87_attributes_temp_offset[] = {
1683 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1684 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1685 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1688 static struct attribute *it87_attributes[] = {
1689 &dev_attr_alarms.attr,
1690 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1691 &dev_attr_name.attr,
1695 static const struct attribute_group it87_group = {
1696 .attrs = it87_attributes,
1699 static struct attribute *it87_attributes_in_beep[] = {
1700 &sensor_dev_attr_in0_beep.dev_attr.attr,
1701 &sensor_dev_attr_in1_beep.dev_attr.attr,
1702 &sensor_dev_attr_in2_beep.dev_attr.attr,
1703 &sensor_dev_attr_in3_beep.dev_attr.attr,
1704 &sensor_dev_attr_in4_beep.dev_attr.attr,
1705 &sensor_dev_attr_in5_beep.dev_attr.attr,
1706 &sensor_dev_attr_in6_beep.dev_attr.attr,
1707 &sensor_dev_attr_in7_beep.dev_attr.attr,
1712 static struct attribute *it87_attributes_temp_beep[] = {
1713 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1714 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1715 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1718 static struct attribute *it87_attributes_fan[6][3+1] = { {
1719 &sensor_dev_attr_fan1_input.dev_attr.attr,
1720 &sensor_dev_attr_fan1_min.dev_attr.attr,
1721 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1724 &sensor_dev_attr_fan2_input.dev_attr.attr,
1725 &sensor_dev_attr_fan2_min.dev_attr.attr,
1726 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1729 &sensor_dev_attr_fan3_input.dev_attr.attr,
1730 &sensor_dev_attr_fan3_min.dev_attr.attr,
1731 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1734 &sensor_dev_attr_fan4_input.dev_attr.attr,
1735 &sensor_dev_attr_fan4_min.dev_attr.attr,
1736 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1739 &sensor_dev_attr_fan5_input.dev_attr.attr,
1740 &sensor_dev_attr_fan5_min.dev_attr.attr,
1741 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1744 &sensor_dev_attr_fan6_input.dev_attr.attr,
1745 &sensor_dev_attr_fan6_min.dev_attr.attr,
1746 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1750 static const struct attribute_group it87_group_fan[6] = {
1751 { .attrs = it87_attributes_fan[0] },
1752 { .attrs = it87_attributes_fan[1] },
1753 { .attrs = it87_attributes_fan[2] },
1754 { .attrs = it87_attributes_fan[3] },
1755 { .attrs = it87_attributes_fan[4] },
1756 { .attrs = it87_attributes_fan[5] },
1759 static const struct attribute *it87_attributes_fan_div[] = {
1760 &sensor_dev_attr_fan1_div.dev_attr.attr,
1761 &sensor_dev_attr_fan2_div.dev_attr.attr,
1762 &sensor_dev_attr_fan3_div.dev_attr.attr,
1765 static struct attribute *it87_attributes_pwm[6][4+1] = { {
1766 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1767 &sensor_dev_attr_pwm1.dev_attr.attr,
1768 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1769 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1772 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1773 &sensor_dev_attr_pwm2.dev_attr.attr,
1774 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1775 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1778 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1779 &sensor_dev_attr_pwm3.dev_attr.attr,
1780 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1781 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1784 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1785 &sensor_dev_attr_pwm4.dev_attr.attr,
1786 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
1787 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1790 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1791 &sensor_dev_attr_pwm5.dev_attr.attr,
1792 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1793 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
1796 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1797 &sensor_dev_attr_pwm6.dev_attr.attr,
1798 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1799 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
1803 static umode_t pwm_attribute_mode(struct kobject *kobj, struct attribute *attr,
1806 struct device *dev = container_of(kobj, struct device, kobj);
1807 struct it87_data *data = dev_get_drvdata(dev);
1809 if (has_pwm_freq2(data) && index == 2)
1810 return attr->mode | S_IWUSR;
1815 static const struct attribute_group it87_group_pwm[6] = {
1816 { .attrs = it87_attributes_pwm[0] },
1817 { .attrs = it87_attributes_pwm[1],
1818 .is_visible = pwm_attribute_mode, },
1819 { .attrs = it87_attributes_pwm[2] },
1820 { .attrs = it87_attributes_pwm[3] },
1821 { .attrs = it87_attributes_pwm[4] },
1822 { .attrs = it87_attributes_pwm[5] },
1825 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1826 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1827 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1828 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1829 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1830 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1831 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1832 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1833 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1834 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1837 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1838 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1839 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1840 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1841 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1842 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1843 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1844 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1845 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1848 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1849 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1850 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1851 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1852 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1853 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1854 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1855 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1856 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1860 static const struct attribute_group it87_group_autopwm[3] = {
1861 { .attrs = it87_attributes_autopwm[0] },
1862 { .attrs = it87_attributes_autopwm[1] },
1863 { .attrs = it87_attributes_autopwm[2] },
1866 static struct attribute *it87_attributes_fan_beep[] = {
1867 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1868 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1869 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1870 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1871 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1872 &sensor_dev_attr_fan6_beep.dev_attr.attr,
1875 static struct attribute *it87_attributes_vid[] = {
1877 &dev_attr_cpu0_vid.attr,
1881 static const struct attribute_group it87_group_vid = {
1882 .attrs = it87_attributes_vid,
1885 static struct attribute *it87_attributes_label[] = {
1886 &sensor_dev_attr_in3_label.dev_attr.attr,
1887 &sensor_dev_attr_in7_label.dev_attr.attr,
1888 &sensor_dev_attr_in8_label.dev_attr.attr,
1889 &sensor_dev_attr_in9_label.dev_attr.attr,
1893 static const struct attribute_group it87_group_label = {
1894 .attrs = it87_attributes_label,
1897 /* SuperIO detection - will change isa_address if a chip is found */
1898 static int __init it87_find(unsigned short *address,
1899 struct it87_sio_data *sio_data)
1903 const char *board_vendor, *board_name;
1905 err = superio_enter();
1910 chip_type = force_id ? force_id : superio_inw(DEVID);
1912 switch (chip_type) {
1914 sio_data->type = it87;
1917 sio_data->type = it8712;
1921 sio_data->type = it8716;
1924 sio_data->type = it8718;
1927 sio_data->type = it8720;
1930 sio_data->type = it8721;
1933 sio_data->type = it8728;
1936 sio_data->type = it8771;
1939 sio_data->type = it8772;
1942 sio_data->type = it8781;
1945 sio_data->type = it8782;
1948 sio_data->type = it8783;
1951 sio_data->type = it8786;
1955 sio_data->type = it8603;
1958 sio_data->type = it8620;
1961 sio_data->type = it8790;
1963 case 0xffff: /* No device at all */
1966 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1970 superio_select(PME);
1971 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1972 pr_info("Device not activated, skipping\n");
1976 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1977 if (*address == 0) {
1978 pr_info("Base address not set, skipping\n");
1983 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1984 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1985 it87_devices[sio_data->type].suffix,
1986 *address, sio_data->revision);
1988 /* in8 (Vbat) is always internal */
1989 sio_data->internal = (1 << 2);
1991 /* in7 (VSB or VCCH5V) is always internal on some chips */
1992 if (it87_devices[sio_data->type].features & FEAT_IN7_INTERNAL)
1993 sio_data->internal |= (1 << 1);
1995 /* in9 (AVCC3), always internal if supported */
1996 if (it87_devices[sio_data->type].features & FEAT_AVCC3)
1997 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1999 sio_data->skip_in |= (1 << 9);
2001 if (!(it87_devices[sio_data->type].features & FEAT_SIX_PWM))
2002 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2004 if (!(it87_devices[sio_data->type].features & FEAT_VID))
2005 sio_data->skip_vid = 1;
2007 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2008 if (sio_data->type == it87) {
2009 /* The IT8705F has a different LD number for GPIO */
2011 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2012 } else if (sio_data->type == it8783) {
2013 int reg25, reg27, reg2a, reg2c, regef;
2015 superio_select(GPIO);
2017 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
2018 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
2019 reg2a = superio_inb(IT87_SIO_PINX1_REG);
2020 reg2c = superio_inb(IT87_SIO_PINX2_REG);
2021 regef = superio_inb(IT87_SIO_SPI_REG);
2023 /* Check if fan3 is there or not */
2024 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
2025 sio_data->skip_fan |= (1 << 2);
2026 if ((reg25 & (1 << 4))
2027 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
2028 sio_data->skip_pwm |= (1 << 2);
2030 /* Check if fan2 is there or not */
2031 if (reg27 & (1 << 7))
2032 sio_data->skip_fan |= (1 << 1);
2033 if (reg27 & (1 << 3))
2034 sio_data->skip_pwm |= (1 << 1);
2037 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
2038 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2041 if (reg27 & (1 << 1))
2042 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2046 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2048 if (reg27 & (1 << 2)) {
2050 * The data sheet is a bit unclear regarding the
2051 * internal voltage divider for VCCH5V. It says
2052 * "This bit enables and switches VIN7 (pin 91) to the
2053 * internal voltage divider for VCCH5V".
2054 * This is different to other chips, where the internal
2055 * voltage divider would connect VIN7 to an internal
2056 * voltage source. Maybe that is the case here as well.
2058 * Since we don't know for sure, re-route it if that is
2059 * not the case, and ask the user to report if the
2060 * resulting voltage is sane.
2062 if (!(reg2c & (1 << 1))) {
2064 superio_outb(IT87_SIO_PINX2_REG, reg2c);
2065 pr_notice("Routing internal VCCH5V to in7.\n");
2067 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2068 pr_notice("Please report if it displays a reasonable voltage.\n");
2071 if (reg2c & (1 << 0))
2072 sio_data->internal |= (1 << 0);
2073 if (reg2c & (1 << 1))
2074 sio_data->internal |= (1 << 1);
2076 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2077 } else if (sio_data->type == it8603) {
2080 superio_select(GPIO);
2082 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
2084 /* Check if fan3 is there or not */
2085 if (reg27 & (1 << 6))
2086 sio_data->skip_pwm |= (1 << 2);
2087 if (reg27 & (1 << 7))
2088 sio_data->skip_fan |= (1 << 2);
2090 /* Check if fan2 is there or not */
2091 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
2092 if (reg29 & (1 << 1))
2093 sio_data->skip_pwm |= (1 << 1);
2094 if (reg29 & (1 << 2))
2095 sio_data->skip_fan |= (1 << 1);
2097 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2098 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2100 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2101 } else if (sio_data->type == it8620) {
2104 superio_select(GPIO);
2106 /* Check for pwm5 */
2107 reg = superio_inb(IT87_SIO_GPIO1_REG);
2109 sio_data->skip_pwm |= (1 << 4);
2111 /* Check for fan4, fan5 */
2112 reg = superio_inb(IT87_SIO_GPIO2_REG);
2113 if (!(reg & (1 << 5)))
2114 sio_data->skip_fan |= (1 << 3);
2115 if (!(reg & (1 << 4)))
2116 sio_data->skip_fan |= (1 << 4);
2118 /* Check for pwm3, fan3 */
2119 reg = superio_inb(IT87_SIO_GPIO3_REG);
2121 sio_data->skip_pwm |= (1 << 2);
2123 sio_data->skip_fan |= (1 << 2);
2125 /* Check for pwm4 */
2126 reg = superio_inb(IT87_SIO_GPIO4_REG);
2127 if (!(reg & (1 << 4)))
2128 sio_data->skip_pwm |= (1 << 3);
2130 /* Check for pwm2, fan2 */
2131 reg = superio_inb(IT87_SIO_GPIO5_REG);
2133 sio_data->skip_pwm |= (1 << 1);
2135 sio_data->skip_fan |= (1 << 1);
2136 /* Check for pwm6, fan6 */
2137 if (!(reg & (1 << 7))) {
2138 sio_data->skip_pwm |= (1 << 5);
2139 sio_data->skip_fan |= (1 << 5);
2142 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2147 superio_select(GPIO);
2149 reg = superio_inb(IT87_SIO_GPIO3_REG);
2150 if (!sio_data->skip_vid) {
2151 /* We need at least 4 VID pins */
2153 pr_info("VID is disabled (pins used for GPIO)\n");
2154 sio_data->skip_vid = 1;
2158 /* Check if fan3 is there or not */
2160 sio_data->skip_pwm |= (1 << 2);
2162 sio_data->skip_fan |= (1 << 2);
2164 /* Check if fan2 is there or not */
2165 reg = superio_inb(IT87_SIO_GPIO5_REG);
2167 sio_data->skip_pwm |= (1 << 1);
2169 sio_data->skip_fan |= (1 << 1);
2171 if ((sio_data->type == it8718 || sio_data->type == it8720)
2172 && !(sio_data->skip_vid))
2173 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
2175 reg = superio_inb(IT87_SIO_PINX2_REG);
2177 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2180 * The IT8720F has no VIN7 pin, so VCCH should always be
2181 * routed internally to VIN7 with an internal divider.
2182 * Curiously, there still is a configuration bit to control
2183 * this, which means it can be set incorrectly. And even
2184 * more curiously, many boards out there are improperly
2185 * configured, even though the IT8720F datasheet claims
2186 * that the internal routing of VCCH to VIN7 is the default
2187 * setting. So we force the internal routing in this case.
2189 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2190 * If UART6 is enabled, re-route VIN7 to the internal divider
2191 * if that is not already the case.
2193 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
2195 superio_outb(IT87_SIO_PINX2_REG, reg);
2196 pr_notice("Routing internal VCCH to in7\n");
2199 sio_data->internal |= (1 << 0);
2201 sio_data->internal |= (1 << 1);
2204 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2205 * While VIN7 can be routed to the internal voltage divider,
2206 * VIN5 and VIN6 are not available if UART6 is enabled.
2208 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2209 * is the temperature source. Since we can not read the
2210 * temperature source here, skip_temp is preliminary.
2213 sio_data->skip_in |= (1 << 5) | (1 << 6);
2214 sio_data->skip_temp |= (1 << 2);
2217 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2219 if (sio_data->beep_pin)
2220 pr_info("Beeping is supported\n");
2222 /* Disable specific features based on DMI strings */
2223 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2224 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2225 if (board_vendor && board_name) {
2226 if (strcmp(board_vendor, "nVIDIA") == 0
2227 && strcmp(board_name, "FN68PT") == 0) {
2229 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2230 * connected to a fan, but to something else. One user
2231 * has reported instant system power-off when changing
2232 * the PWM2 duty cycle, so we disable it.
2233 * I use the board name string as the trigger in case
2234 * the same board is ever used in other systems.
2236 pr_info("Disabling pwm2 due to hardware constraints\n");
2237 sio_data->skip_pwm = (1 << 1);
2246 static void it87_remove_files(struct device *dev)
2248 struct it87_data *data = platform_get_drvdata(pdev);
2249 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2252 sysfs_remove_group(&dev->kobj, &it87_group);
2253 for (i = 0; i < 10; i++) {
2254 if (sio_data->skip_in & (1 << i))
2256 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2257 if (it87_attributes_in_beep[i])
2258 sysfs_remove_file(&dev->kobj,
2259 it87_attributes_in_beep[i]);
2261 for (i = 0; i < 3; i++) {
2262 if (!(data->has_temp & (1 << i)))
2264 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
2265 if (has_temp_offset(data))
2266 sysfs_remove_file(&dev->kobj,
2267 it87_attributes_temp_offset[i]);
2268 if (sio_data->beep_pin)
2269 sysfs_remove_file(&dev->kobj,
2270 it87_attributes_temp_beep[i]);
2272 for (i = 0; i < 6; i++) {
2273 if (!(data->has_fan & (1 << i)))
2275 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
2276 if (sio_data->beep_pin)
2277 sysfs_remove_file(&dev->kobj,
2278 it87_attributes_fan_beep[i]);
2279 if (i < 3 && !has_16bit_fans(data))
2280 sysfs_remove_file(&dev->kobj,
2281 it87_attributes_fan_div[i]);
2283 for (i = 0; i < 6; i++) {
2284 if (sio_data->skip_pwm & (1 << i))
2286 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
2287 if (has_old_autopwm(data))
2288 sysfs_remove_group(&dev->kobj,
2289 &it87_group_autopwm[i]);
2291 if (!sio_data->skip_vid)
2292 sysfs_remove_group(&dev->kobj, &it87_group_vid);
2293 sysfs_remove_group(&dev->kobj, &it87_group_label);
2296 static int it87_probe(struct platform_device *pdev)
2298 struct it87_data *data;
2299 struct resource *res;
2300 struct device *dev = &pdev->dev;
2301 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2303 int enable_pwm_interface;
2304 int fan_beep_need_rw;
2306 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2307 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2309 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2310 (unsigned long)res->start,
2311 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2315 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2319 data->addr = res->start;
2320 data->type = sio_data->type;
2321 data->features = it87_devices[sio_data->type].features;
2322 data->peci_mask = it87_devices[sio_data->type].peci_mask;
2323 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
2324 data->name = it87_devices[sio_data->type].name;
2326 * IT8705F Datasheet 0.4.1, 3h == Version G.
2327 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2328 * These are the first revisions with 16-bit tachometer support.
2330 switch (data->type) {
2332 if (sio_data->revision >= 0x03) {
2333 data->features &= ~FEAT_OLD_AUTOPWM;
2334 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
2338 if (sio_data->revision >= 0x08) {
2339 data->features &= ~FEAT_OLD_AUTOPWM;
2340 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2348 /* Now, we do the remaining detection. */
2349 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2350 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2353 platform_set_drvdata(pdev, data);
2355 mutex_init(&data->update_lock);
2357 /* Check PWM configuration */
2358 enable_pwm_interface = it87_check_pwm(dev);
2360 /* Starting with IT8721F, we handle scaling of internal voltages */
2361 if (has_12mv_adc(data)) {
2362 if (sio_data->internal & (1 << 0))
2363 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2364 if (sio_data->internal & (1 << 1))
2365 data->in_scaled |= (1 << 7); /* in7 is VSB */
2366 if (sio_data->internal & (1 << 2))
2367 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2368 if (sio_data->internal & (1 << 3))
2369 data->in_scaled |= (1 << 9); /* in9 is AVCC */
2370 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2371 sio_data->type == it8783) {
2372 if (sio_data->internal & (1 << 0))
2373 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2374 if (sio_data->internal & (1 << 1))
2375 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
2378 data->has_temp = 0x07;
2379 if (sio_data->skip_temp & (1 << 2)) {
2380 if (sio_data->type == it8782
2381 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2382 data->has_temp &= ~(1 << 2);
2385 /* Initialize the IT87 chip */
2386 it87_init_device(pdev);
2388 /* Register sysfs hooks */
2389 err = sysfs_create_group(&dev->kobj, &it87_group);
2393 for (i = 0; i < 10; i++) {
2394 if (sio_data->skip_in & (1 << i))
2396 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2399 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2400 err = sysfs_create_file(&dev->kobj,
2401 it87_attributes_in_beep[i]);
2407 for (i = 0; i < 3; i++) {
2408 if (!(data->has_temp & (1 << i)))
2410 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2413 if (has_temp_offset(data)) {
2414 err = sysfs_create_file(&dev->kobj,
2415 it87_attributes_temp_offset[i]);
2419 if (sio_data->beep_pin) {
2420 err = sysfs_create_file(&dev->kobj,
2421 it87_attributes_temp_beep[i]);
2427 /* Do not create fan files for disabled fans */
2428 fan_beep_need_rw = 1;
2429 for (i = 0; i < 6; i++) {
2430 if (!(data->has_fan & (1 << i)))
2432 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2436 if (i < 3 && !has_16bit_fans(data)) {
2437 err = sysfs_create_file(&dev->kobj,
2438 it87_attributes_fan_div[i]);
2443 if (sio_data->beep_pin) {
2444 err = sysfs_create_file(&dev->kobj,
2445 it87_attributes_fan_beep[i]);
2448 if (!fan_beep_need_rw)
2452 * As we have a single beep enable bit for all fans,
2453 * only the first enabled fan has a writable attribute
2456 if (sysfs_chmod_file(&dev->kobj,
2457 it87_attributes_fan_beep[i],
2459 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2461 fan_beep_need_rw = 0;
2465 if (enable_pwm_interface) {
2466 for (i = 0; i < 6; i++) {
2467 if (sio_data->skip_pwm & (1 << i))
2469 err = sysfs_create_group(&dev->kobj,
2470 &it87_group_pwm[i]);
2474 if (!has_old_autopwm(data))
2476 err = sysfs_create_group(&dev->kobj,
2477 &it87_group_autopwm[i]);
2483 if (!sio_data->skip_vid) {
2484 data->vrm = vid_which_vrm();
2485 /* VID reading from Super-I/O config space if available */
2486 data->vid = sio_data->vid_value;
2487 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2492 /* Export labels for internal sensors */
2493 for (i = 0; i < 4; i++) {
2494 if (!(sio_data->internal & (1 << i)))
2496 err = sysfs_create_file(&dev->kobj,
2497 it87_attributes_label[i]);
2502 data->hwmon_dev = hwmon_device_register(dev);
2503 if (IS_ERR(data->hwmon_dev)) {
2504 err = PTR_ERR(data->hwmon_dev);
2511 it87_remove_files(dev);
2515 static int it87_remove(struct platform_device *pdev)
2517 struct it87_data *data = platform_get_drvdata(pdev);
2519 hwmon_device_unregister(data->hwmon_dev);
2520 it87_remove_files(&pdev->dev);
2526 * Must be called with data->update_lock held, except during initialization.
2527 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2528 * would slow down the IT87 access and should not be necessary.
2530 static int it87_read_value(struct it87_data *data, u8 reg)
2532 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2533 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2537 * Must be called with data->update_lock held, except during initialization.
2538 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2539 * would slow down the IT87 access and should not be necessary.
2541 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2543 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2544 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2547 /* Return 1 if and only if the PWM interface is safe to use */
2548 static int it87_check_pwm(struct device *dev)
2550 struct it87_data *data = dev_get_drvdata(dev);
2552 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2553 * and polarity set to active low is sign that this is the case so we
2554 * disable pwm control to protect the user.
2556 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2557 if ((tmp & 0x87) == 0) {
2558 if (fix_pwm_polarity) {
2560 * The user asks us to attempt a chip reconfiguration.
2561 * This means switching to active high polarity and
2562 * inverting all fan speed values.
2567 for (i = 0; i < 3; i++)
2568 pwm[i] = it87_read_value(data,
2572 * If any fan is in automatic pwm mode, the polarity
2573 * might be correct, as suspicious as it seems, so we
2574 * better don't change anything (but still disable the
2577 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2579 "Reconfiguring PWM to active high polarity\n");
2580 it87_write_value(data, IT87_REG_FAN_CTL,
2582 for (i = 0; i < 3; i++)
2583 it87_write_value(data,
2590 "PWM configuration is too broken to be fixed\n");
2594 "Detected broken BIOS defaults, disabling PWM interface\n");
2596 } else if (fix_pwm_polarity) {
2598 "PWM configuration looks sane, won't touch\n");
2604 /* Called when we have found a new IT87. */
2605 static void it87_init_device(struct platform_device *pdev)
2607 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2608 struct it87_data *data = platform_get_drvdata(pdev);
2613 * For each PWM channel:
2614 * - If it is in automatic mode, setting to manual mode should set
2615 * the fan to full speed by default.
2616 * - If it is in manual mode, we need a mapping to temperature
2617 * channels to use when later setting to automatic mode later.
2618 * Use a 1:1 mapping by default (we are clueless.)
2619 * In both cases, the value can (and should) be changed by the user
2620 * prior to switching to a different mode.
2621 * Note that this is no longer needed for the IT8721F and later, as
2622 * these have separate registers for the temperature mapping and the
2623 * manual duty cycle.
2625 for (i = 0; i < 3; i++) {
2626 data->pwm_temp_map[i] = i;
2627 data->pwm_duty[i] = 0x7f; /* Full speed */
2628 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2632 * Some chips seem to have default value 0xff for all limit
2633 * registers. For low voltage limits it makes no sense and triggers
2634 * alarms, so change to 0 instead. For high temperature limits, it
2635 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2636 * but is still confusing, so change to 127 degrees C.
2638 for (i = 0; i < 8; i++) {
2639 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2641 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2643 for (i = 0; i < 3; i++) {
2644 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2646 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2650 * Temperature channels are not forcibly enabled, as they can be
2651 * set to two different sensor types and we can't guess which one
2652 * is correct for a given system. These channels can be enabled at
2653 * run-time through the temp{1-3}_type sysfs accessors if needed.
2656 /* Check if voltage monitors are reset manually or by some reason */
2657 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2658 if ((tmp & 0xff) == 0) {
2659 /* Enable all voltage monitors */
2660 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2663 /* Check if tachometers are reset manually or by some reason */
2664 mask = 0x70 & ~(sio_data->skip_fan << 4);
2665 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2666 if ((data->fan_main_ctrl & mask) == 0) {
2667 /* Enable all fan tachometers */
2668 data->fan_main_ctrl |= mask;
2669 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2670 data->fan_main_ctrl);
2672 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2674 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2676 /* Set tachometers to 16-bit mode if needed */
2677 if (has_fan16_config(data)) {
2678 if (~tmp & 0x07 & data->has_fan) {
2680 "Setting fan1-3 to 16-bit mode\n");
2681 it87_write_value(data, IT87_REG_FAN_16BIT,
2686 /* Check for additional fans */
2687 if (has_five_fans(data)) {
2689 data->has_fan |= (1 << 3); /* fan4 enabled */
2691 data->has_fan |= (1 << 4); /* fan5 enabled */
2694 if (has_six_fans(data)) {
2696 data->has_fan |= (1 << 5); /* fan6 enabled */
2699 /* Fan input pins may be used for alternative functions */
2700 data->has_fan &= ~sio_data->skip_fan;
2702 /* Check if pwm5, pwm6 are enabled */
2703 if (has_six_pwm(data)) {
2704 /* The following code may be IT8620E specific */
2705 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2706 if ((tmp & 0xc0) == 0xc0)
2707 sio_data->skip_pwm |= (1 << 4);
2708 if (!(tmp & (1 << 3)))
2709 sio_data->skip_pwm |= (1 << 5);
2712 /* Start monitoring */
2713 it87_write_value(data, IT87_REG_CONFIG,
2714 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2715 | (update_vbat ? 0x41 : 0x01));
2718 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2720 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
2721 if (has_newer_autopwm(data)) {
2722 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2723 data->pwm_duty[nr] = it87_read_value(data,
2724 IT87_REG_PWM_DUTY[nr]);
2726 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2727 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2728 else /* Manual mode */
2729 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2732 if (has_old_autopwm(data)) {
2735 for (i = 0; i < 5 ; i++)
2736 data->auto_temp[nr][i] = it87_read_value(data,
2737 IT87_REG_AUTO_TEMP(nr, i));
2738 for (i = 0; i < 3 ; i++)
2739 data->auto_pwm[nr][i] = it87_read_value(data,
2740 IT87_REG_AUTO_PWM(nr, i));
2744 static struct it87_data *it87_update_device(struct device *dev)
2746 struct it87_data *data = dev_get_drvdata(dev);
2749 mutex_lock(&data->update_lock);
2751 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2755 * Cleared after each update, so reenable. Value
2756 * returned by this read will be previous value
2758 it87_write_value(data, IT87_REG_CONFIG,
2759 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2761 for (i = 0; i <= 7; i++) {
2763 it87_read_value(data, IT87_REG_VIN(i));
2765 it87_read_value(data, IT87_REG_VIN_MIN(i));
2767 it87_read_value(data, IT87_REG_VIN_MAX(i));
2769 /* in8 (battery) has no limit registers */
2770 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2771 if (has_avcc3(data))
2772 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
2774 for (i = 0; i < 6; i++) {
2775 /* Skip disabled fans */
2776 if (!(data->has_fan & (1 << i)))
2780 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2781 data->fan[i][0] = it87_read_value(data,
2783 /* Add high byte if in 16-bit mode */
2784 if (has_16bit_fans(data)) {
2785 data->fan[i][0] |= it87_read_value(data,
2786 IT87_REG_FANX[i]) << 8;
2787 data->fan[i][1] |= it87_read_value(data,
2788 IT87_REG_FANX_MIN[i]) << 8;
2791 for (i = 0; i < 3; i++) {
2792 if (!(data->has_temp & (1 << i)))
2795 it87_read_value(data, IT87_REG_TEMP(i));
2797 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2799 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2800 if (has_temp_offset(data))
2802 it87_read_value(data,
2803 IT87_REG_TEMP_OFFSET[i]);
2806 /* Newer chips don't have clock dividers */
2807 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2808 i = it87_read_value(data, IT87_REG_FAN_DIV);
2809 data->fan_div[0] = i & 0x07;
2810 data->fan_div[1] = (i >> 3) & 0x07;
2811 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2815 it87_read_value(data, IT87_REG_ALARM1) |
2816 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2817 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2818 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2820 data->fan_main_ctrl = it87_read_value(data,
2821 IT87_REG_FAN_MAIN_CTRL);
2822 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2823 for (i = 0; i < 3; i++)
2824 it87_update_pwm_ctrl(data, i);
2826 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2827 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
2829 * The IT8705F does not have VID capability.
2830 * The IT8718F and later don't use IT87_REG_VID for the
2833 if (data->type == it8712 || data->type == it8716) {
2834 data->vid = it87_read_value(data, IT87_REG_VID);
2836 * The older IT8712F revisions had only 5 VID pins,
2837 * but we assume it is always safe to read 6 bits.
2841 data->last_updated = jiffies;
2845 mutex_unlock(&data->update_lock);
2850 static int __init it87_device_add(unsigned short address,
2851 const struct it87_sio_data *sio_data)
2853 struct resource res = {
2854 .start = address + IT87_EC_OFFSET,
2855 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2857 .flags = IORESOURCE_IO,
2861 err = acpi_check_resource_conflict(&res);
2865 pdev = platform_device_alloc(DRVNAME, address);
2868 pr_err("Device allocation failed\n");
2872 err = platform_device_add_resources(pdev, &res, 1);
2874 pr_err("Device resource addition failed (%d)\n", err);
2875 goto exit_device_put;
2878 err = platform_device_add_data(pdev, sio_data,
2879 sizeof(struct it87_sio_data));
2881 pr_err("Platform data allocation failed\n");
2882 goto exit_device_put;
2885 err = platform_device_add(pdev);
2887 pr_err("Device addition failed (%d)\n", err);
2888 goto exit_device_put;
2894 platform_device_put(pdev);
2899 static int __init sm_it87_init(void)
2902 unsigned short isa_address = 0;
2903 struct it87_sio_data sio_data;
2905 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2906 err = it87_find(&isa_address, &sio_data);
2909 err = platform_driver_register(&it87_driver);
2913 err = it87_device_add(isa_address, &sio_data);
2915 platform_driver_unregister(&it87_driver);
2922 static void __exit sm_it87_exit(void)
2924 platform_device_unregister(pdev);
2925 platform_driver_unregister(&it87_driver);
2929 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
2930 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2931 module_param(update_vbat, bool, 0);
2932 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2933 module_param(fix_pwm_polarity, bool, 0);
2934 MODULE_PARM_DESC(fix_pwm_polarity,
2935 "Force PWM polarity to active high (DANGEROUS)");
2936 MODULE_LICENSE("GPL");
2938 module_init(sm_it87_init);
2939 module_exit(sm_it87_exit);