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_GPIO3_REG 0x27
168 #define IT87_SIO_GPIO5_REG 0x29
169 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
170 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
171 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
172 #define IT87_SIO_VID_REG 0xfc /* VID value */
173 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
175 /* Update battery voltage after every reading if true */
176 static bool update_vbat;
178 /* Not all BIOSes properly configure the PWM registers */
179 static bool fix_pwm_polarity;
181 /* Many IT87 constants specified below */
183 /* Length of ISA address segment */
184 #define IT87_EXTENT 8
186 /* Length of ISA address segment for Environmental Controller */
187 #define IT87_EC_EXTENT 2
189 /* Offset of EC registers from ISA base address */
190 #define IT87_EC_OFFSET 5
192 /* Where are the ISA address/data registers relative to the EC base address */
193 #define IT87_ADDR_REG_OFFSET 0
194 #define IT87_DATA_REG_OFFSET 1
196 /*----- The IT87 registers -----*/
198 #define IT87_REG_CONFIG 0x00
200 #define IT87_REG_ALARM1 0x01
201 #define IT87_REG_ALARM2 0x02
202 #define IT87_REG_ALARM3 0x03
205 * The IT8718F and IT8720F have the VID value in a different register, in
206 * Super-I/O configuration space.
208 #define IT87_REG_VID 0x0a
210 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
211 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
214 #define IT87_REG_FAN_DIV 0x0b
215 #define IT87_REG_FAN_16BIT 0x0c
217 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
219 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
220 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
221 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
222 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
223 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
225 #define IT87_REG_FAN_MAIN_CTRL 0x13
226 #define IT87_REG_FAN_CTL 0x14
227 #define IT87_REG_PWM(nr) (0x15 + (nr))
228 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
230 #define IT87_REG_VIN(nr) (0x20 + (nr))
231 #define IT87_REG_TEMP(nr) (0x29 + (nr))
233 #define IT87_REG_AVCC3 0x2f
235 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
236 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
237 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
238 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
240 #define IT87_REG_VIN_ENABLE 0x50
241 #define IT87_REG_TEMP_ENABLE 0x51
242 #define IT87_REG_TEMP_EXTRA 0x55
243 #define IT87_REG_BEEP_ENABLE 0x5c
245 #define IT87_REG_CHIPID 0x58
247 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
248 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
250 struct it87_devices {
255 const char * const suffix;
258 #define FEAT_12MV_ADC (1 << 0)
259 #define FEAT_NEWER_AUTOPWM (1 << 1)
260 #define FEAT_OLD_AUTOPWM (1 << 2)
261 #define FEAT_16BIT_FANS (1 << 3)
262 #define FEAT_TEMP_OFFSET (1 << 4)
263 #define FEAT_TEMP_PECI (1 << 5)
264 #define FEAT_TEMP_OLD_PECI (1 << 6)
265 #define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
266 #define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
267 #define FEAT_SIX_FANS (1 << 9) /* Supports six fans */
268 #define FEAT_VID (1 << 10) /* Set if chip supports VID */
269 #define FEAT_IN7_INTERNAL (1 << 11) /* Set if in7 is internal */
270 #define FEAT_AVCC3 (1 << 12) /* Chip supports in9/AVCC3 */
272 static const struct it87_devices it87_devices[] = {
275 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
280 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
281 /* may need to overwrite */
286 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
287 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
292 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
293 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
294 .old_peci_mask = 0x4,
299 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
300 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
301 .old_peci_mask = 0x4,
306 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
307 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
308 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL,
310 .old_peci_mask = 0x02, /* Actually reports PCH */
315 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
316 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
323 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
324 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
325 /* PECI: guesswork */
327 /* 16 bit fans (OHM) */
328 /* three fans, always 16 bit (guesswork) */
334 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
335 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
336 /* PECI (coreboot) */
337 /* 12mV ADC (HWSensors4, OHM) */
338 /* 16 bit fans (HWSensors4, OHM) */
339 /* three fans, always 16 bit (datasheet) */
345 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
346 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
347 .old_peci_mask = 0x4,
352 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
353 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
354 .old_peci_mask = 0x4,
359 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
360 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
361 .old_peci_mask = 0x4,
366 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
367 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
373 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
374 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL,
380 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
381 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
388 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
389 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
390 | FEAT_IN7_INTERNAL | FEAT_AVCC3,
396 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
397 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
398 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
399 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
400 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
401 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
402 ((data)->peci_mask & (1 << nr)))
403 #define has_temp_old_peci(data, nr) \
404 (((data)->features & FEAT_TEMP_OLD_PECI) && \
405 ((data)->old_peci_mask & (1 << nr)))
406 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
407 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
409 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
410 #define has_vid(data) ((data)->features & FEAT_VID)
411 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
412 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
414 struct it87_sio_data {
416 /* Values read from Super-I/O config space */
420 u8 internal; /* Internal sensors can be labeled */
421 /* Features skipped based on config or DMI */
430 * For each registered chip, we need to keep some data in memory.
431 * The structure is dynamically allocated.
434 struct device *hwmon_dev;
442 struct mutex update_lock;
443 char valid; /* !=0 if following fields are valid */
444 unsigned long last_updated; /* In jiffies */
446 u16 in_scaled; /* Internal voltage sensors are scaled */
447 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
448 u8 has_fan; /* Bitfield, fans enabled */
449 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
450 u8 has_temp; /* Bitfield, temp sensors enabled */
451 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
452 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
453 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
454 u8 fan_div[3]; /* Register encoding, shifted right */
455 u8 vid; /* Register encoding, combined */
457 u32 alarms; /* Register encoding, combined */
458 u8 beeps; /* Register encoding */
459 u8 fan_main_ctrl; /* Register value */
460 u8 fan_ctl; /* Register value */
463 * The following 3 arrays correspond to the same registers up to
464 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
465 * 7, and we want to preserve settings on mode changes, so we have
466 * to track all values separately.
467 * Starting with the IT8721F, the manual PWM duty cycles are stored
468 * in separate registers (8-bit values), so the separate tracking
469 * is no longer needed, but it is still done to keep the driver
472 u8 pwm_ctrl[3]; /* Register value */
473 u8 pwm_duty[3]; /* Manual PWM value set by user */
474 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
476 /* Automatic fan speed control registers */
477 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
478 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
481 static int adc_lsb(const struct it87_data *data, int nr)
483 int lsb = has_12mv_adc(data) ? 12 : 16;
484 if (data->in_scaled & (1 << nr))
489 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
491 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
492 return clamp_val(val, 0, 255);
495 static int in_from_reg(const struct it87_data *data, int nr, int val)
497 return val * adc_lsb(data, nr);
500 static inline u8 FAN_TO_REG(long rpm, int div)
504 rpm = clamp_val(rpm, 1, 1000000);
505 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
508 static inline u16 FAN16_TO_REG(long rpm)
512 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
515 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
516 1350000 / ((val) * (div)))
517 /* The divider is fixed to 2 in 16-bit mode */
518 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
519 1350000 / ((val) * 2))
521 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
522 ((val) + 500) / 1000), -128, 127))
523 #define TEMP_FROM_REG(val) ((val) * 1000)
525 static u8 pwm_to_reg(const struct it87_data *data, long val)
527 if (has_newer_autopwm(data))
533 static int pwm_from_reg(const struct it87_data *data, u8 reg)
535 if (has_newer_autopwm(data))
538 return (reg & 0x7f) << 1;
542 static int DIV_TO_REG(int val)
545 while (answer < 7 && (val >>= 1))
549 #define DIV_FROM_REG(val) (1 << (val))
551 static const unsigned int pwm_freq[8] = {
562 static int it87_probe(struct platform_device *pdev);
563 static int it87_remove(struct platform_device *pdev);
565 static int it87_read_value(struct it87_data *data, u8 reg);
566 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
567 static struct it87_data *it87_update_device(struct device *dev);
568 static int it87_check_pwm(struct device *dev);
569 static void it87_init_device(struct platform_device *pdev);
572 static struct platform_driver it87_driver = {
577 .remove = it87_remove,
580 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
583 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
585 int index = sattr->index;
587 struct it87_data *data = it87_update_device(dev);
588 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
591 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
592 const char *buf, size_t count)
594 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
596 int index = sattr->index;
598 struct it87_data *data = dev_get_drvdata(dev);
601 if (kstrtoul(buf, 10, &val) < 0)
604 mutex_lock(&data->update_lock);
605 data->in[nr][index] = in_to_reg(data, nr, val);
606 it87_write_value(data,
607 index == 1 ? IT87_REG_VIN_MIN(nr)
608 : IT87_REG_VIN_MAX(nr),
609 data->in[nr][index]);
610 mutex_unlock(&data->update_lock);
614 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
615 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
617 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
620 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
621 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
623 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
626 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
627 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
629 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
632 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
633 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
635 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
638 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
639 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
641 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
644 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
645 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
647 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
650 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
651 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
653 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
656 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
657 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
659 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
662 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
663 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
666 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
669 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
671 int index = sattr->index;
672 struct it87_data *data = it87_update_device(dev);
674 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
677 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
678 const char *buf, size_t count)
680 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
682 int index = sattr->index;
683 struct it87_data *data = dev_get_drvdata(dev);
687 if (kstrtol(buf, 10, &val) < 0)
690 mutex_lock(&data->update_lock);
695 reg = IT87_REG_TEMP_LOW(nr);
698 reg = IT87_REG_TEMP_HIGH(nr);
701 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
702 if (!(regval & 0x80)) {
704 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
707 reg = IT87_REG_TEMP_OFFSET[nr];
711 data->temp[nr][index] = TEMP_TO_REG(val);
712 it87_write_value(data, reg, data->temp[nr][index]);
713 mutex_unlock(&data->update_lock);
717 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
718 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
720 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
722 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
724 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
725 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
727 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
729 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
731 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
732 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
734 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
736 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
739 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
742 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
743 int nr = sensor_attr->index;
744 struct it87_data *data = it87_update_device(dev);
745 u8 reg = data->sensor; /* In case value is updated while used */
746 u8 extra = data->extra;
748 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
749 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
750 return sprintf(buf, "6\n"); /* Intel PECI */
752 return sprintf(buf, "3\n"); /* thermal diode */
754 return sprintf(buf, "4\n"); /* thermistor */
755 return sprintf(buf, "0\n"); /* disabled */
758 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
759 const char *buf, size_t count)
761 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
762 int nr = sensor_attr->index;
764 struct it87_data *data = dev_get_drvdata(dev);
768 if (kstrtol(buf, 10, &val) < 0)
771 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
774 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
776 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
777 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
779 if (val == 2) { /* backwards compatibility */
781 "Sensor type 2 is deprecated, please use 4 instead\n");
784 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
789 else if (has_temp_peci(data, nr) && val == 6)
790 reg |= (nr + 1) << 6;
791 else if (has_temp_old_peci(data, nr) && val == 6)
796 mutex_lock(&data->update_lock);
799 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
800 if (has_temp_old_peci(data, nr))
801 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
802 data->valid = 0; /* Force cache refresh */
803 mutex_unlock(&data->update_lock);
807 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
809 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
811 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
816 static int pwm_mode(const struct it87_data *data, int nr)
818 int ctrl = data->fan_main_ctrl & (1 << nr);
820 if (ctrl == 0 && data->type != it8603) /* Full speed */
822 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
824 else /* Manual mode */
828 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
831 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
833 int index = sattr->index;
835 struct it87_data *data = it87_update_device(dev);
837 speed = has_16bit_fans(data) ?
838 FAN16_FROM_REG(data->fan[nr][index]) :
839 FAN_FROM_REG(data->fan[nr][index],
840 DIV_FROM_REG(data->fan_div[nr]));
841 return sprintf(buf, "%d\n", speed);
844 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
847 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
848 int nr = sensor_attr->index;
850 struct it87_data *data = it87_update_device(dev);
851 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
853 static ssize_t show_pwm_enable(struct device *dev,
854 struct device_attribute *attr, char *buf)
856 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
857 int nr = sensor_attr->index;
859 struct it87_data *data = it87_update_device(dev);
860 return sprintf(buf, "%d\n", pwm_mode(data, nr));
862 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
865 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
866 int nr = sensor_attr->index;
868 struct it87_data *data = it87_update_device(dev);
869 return sprintf(buf, "%d\n",
870 pwm_from_reg(data, data->pwm_duty[nr]));
872 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
875 struct it87_data *data = it87_update_device(dev);
876 int index = (data->fan_ctl >> 4) & 0x07;
878 return sprintf(buf, "%u\n", pwm_freq[index]);
881 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
882 const char *buf, size_t count)
884 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
886 int index = sattr->index;
888 struct it87_data *data = dev_get_drvdata(dev);
892 if (kstrtol(buf, 10, &val) < 0)
895 mutex_lock(&data->update_lock);
897 if (has_16bit_fans(data)) {
898 data->fan[nr][index] = FAN16_TO_REG(val);
899 it87_write_value(data, IT87_REG_FAN_MIN[nr],
900 data->fan[nr][index] & 0xff);
901 it87_write_value(data, IT87_REG_FANX_MIN[nr],
902 data->fan[nr][index] >> 8);
904 reg = it87_read_value(data, IT87_REG_FAN_DIV);
907 data->fan_div[nr] = reg & 0x07;
910 data->fan_div[nr] = (reg >> 3) & 0x07;
913 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
916 data->fan[nr][index] =
917 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
918 it87_write_value(data, IT87_REG_FAN_MIN[nr],
919 data->fan[nr][index]);
922 mutex_unlock(&data->update_lock);
926 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
927 const char *buf, size_t count)
929 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
930 int nr = sensor_attr->index;
932 struct it87_data *data = dev_get_drvdata(dev);
937 if (kstrtoul(buf, 10, &val) < 0)
940 mutex_lock(&data->update_lock);
941 old = it87_read_value(data, IT87_REG_FAN_DIV);
943 /* Save fan min limit */
944 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
949 data->fan_div[nr] = DIV_TO_REG(val);
953 data->fan_div[nr] = 1;
955 data->fan_div[nr] = 3;
958 val |= (data->fan_div[0] & 0x07);
959 val |= (data->fan_div[1] & 0x07) << 3;
960 if (data->fan_div[2] == 3)
962 it87_write_value(data, IT87_REG_FAN_DIV, val);
964 /* Restore fan min limit */
965 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
966 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
968 mutex_unlock(&data->update_lock);
972 /* Returns 0 if OK, -EINVAL otherwise */
973 static int check_trip_points(struct device *dev, int nr)
975 const struct it87_data *data = dev_get_drvdata(dev);
978 if (has_old_autopwm(data)) {
979 for (i = 0; i < 3; i++) {
980 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
983 for (i = 0; i < 2; i++) {
984 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
991 "Inconsistent trip points, not switching to automatic mode\n");
992 dev_err(dev, "Adjust the trip points and try again\n");
997 static ssize_t set_pwm_enable(struct device *dev,
998 struct device_attribute *attr, const char *buf, size_t count)
1000 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1001 int nr = sensor_attr->index;
1003 struct it87_data *data = dev_get_drvdata(dev);
1006 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1009 /* Check trip points before switching to automatic mode */
1011 if (check_trip_points(dev, nr) < 0)
1015 /* IT8603E does not have on/off mode */
1016 if (val == 0 && data->type == it8603)
1019 mutex_lock(&data->update_lock);
1023 /* make sure the fan is on when in on/off mode */
1024 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1025 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
1026 /* set on/off mode */
1027 data->fan_main_ctrl &= ~(1 << nr);
1028 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1029 data->fan_main_ctrl);
1031 if (val == 1) /* Manual mode */
1032 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1033 data->pwm_temp_map[nr] :
1035 else /* Automatic mode */
1036 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1037 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1039 if (data->type != it8603) {
1040 /* set SmartGuardian mode */
1041 data->fan_main_ctrl |= (1 << nr);
1042 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1043 data->fan_main_ctrl);
1047 mutex_unlock(&data->update_lock);
1050 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1051 const char *buf, size_t count)
1053 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1054 int nr = sensor_attr->index;
1056 struct it87_data *data = dev_get_drvdata(dev);
1059 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1062 mutex_lock(&data->update_lock);
1063 if (has_newer_autopwm(data)) {
1065 * If we are in automatic mode, the PWM duty cycle register
1066 * is read-only so we can't write the value.
1068 if (data->pwm_ctrl[nr] & 0x80) {
1069 mutex_unlock(&data->update_lock);
1072 data->pwm_duty[nr] = pwm_to_reg(data, val);
1073 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1074 data->pwm_duty[nr]);
1076 data->pwm_duty[nr] = pwm_to_reg(data, val);
1078 * If we are in manual mode, write the duty cycle immediately;
1079 * otherwise, just store it for later use.
1081 if (!(data->pwm_ctrl[nr] & 0x80)) {
1082 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1083 it87_write_value(data, IT87_REG_PWM(nr),
1084 data->pwm_ctrl[nr]);
1087 mutex_unlock(&data->update_lock);
1090 static ssize_t set_pwm_freq(struct device *dev,
1091 struct device_attribute *attr, const char *buf, size_t count)
1093 struct it87_data *data = dev_get_drvdata(dev);
1097 if (kstrtoul(buf, 10, &val) < 0)
1100 /* Search for the nearest available frequency */
1101 for (i = 0; i < 7; i++) {
1102 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1106 mutex_lock(&data->update_lock);
1107 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1108 data->fan_ctl |= i << 4;
1109 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1110 mutex_unlock(&data->update_lock);
1114 static ssize_t show_pwm_temp_map(struct device *dev,
1115 struct device_attribute *attr, char *buf)
1117 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1118 int nr = sensor_attr->index;
1120 struct it87_data *data = it87_update_device(dev);
1123 if (data->pwm_temp_map[nr] < 3)
1124 map = 1 << data->pwm_temp_map[nr];
1126 map = 0; /* Should never happen */
1127 return sprintf(buf, "%d\n", map);
1129 static ssize_t set_pwm_temp_map(struct device *dev,
1130 struct device_attribute *attr, const char *buf, size_t count)
1132 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1133 int nr = sensor_attr->index;
1135 struct it87_data *data = dev_get_drvdata(dev);
1140 * This check can go away if we ever support automatic fan speed
1141 * control on newer chips.
1143 if (!has_old_autopwm(data)) {
1144 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1148 if (kstrtol(buf, 10, &val) < 0)
1165 mutex_lock(&data->update_lock);
1166 data->pwm_temp_map[nr] = reg;
1168 * If we are in automatic mode, write the temp mapping immediately;
1169 * otherwise, just store it for later use.
1171 if (data->pwm_ctrl[nr] & 0x80) {
1172 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1173 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1175 mutex_unlock(&data->update_lock);
1179 static ssize_t show_auto_pwm(struct device *dev,
1180 struct device_attribute *attr, char *buf)
1182 struct it87_data *data = it87_update_device(dev);
1183 struct sensor_device_attribute_2 *sensor_attr =
1184 to_sensor_dev_attr_2(attr);
1185 int nr = sensor_attr->nr;
1186 int point = sensor_attr->index;
1188 return sprintf(buf, "%d\n",
1189 pwm_from_reg(data, data->auto_pwm[nr][point]));
1192 static ssize_t set_auto_pwm(struct device *dev,
1193 struct device_attribute *attr, const char *buf, size_t count)
1195 struct it87_data *data = dev_get_drvdata(dev);
1196 struct sensor_device_attribute_2 *sensor_attr =
1197 to_sensor_dev_attr_2(attr);
1198 int nr = sensor_attr->nr;
1199 int point = sensor_attr->index;
1202 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1205 mutex_lock(&data->update_lock);
1206 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1207 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1208 data->auto_pwm[nr][point]);
1209 mutex_unlock(&data->update_lock);
1213 static ssize_t show_auto_temp(struct device *dev,
1214 struct device_attribute *attr, char *buf)
1216 struct it87_data *data = it87_update_device(dev);
1217 struct sensor_device_attribute_2 *sensor_attr =
1218 to_sensor_dev_attr_2(attr);
1219 int nr = sensor_attr->nr;
1220 int point = sensor_attr->index;
1222 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1225 static ssize_t set_auto_temp(struct device *dev,
1226 struct device_attribute *attr, const char *buf, size_t count)
1228 struct it87_data *data = dev_get_drvdata(dev);
1229 struct sensor_device_attribute_2 *sensor_attr =
1230 to_sensor_dev_attr_2(attr);
1231 int nr = sensor_attr->nr;
1232 int point = sensor_attr->index;
1235 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1238 mutex_lock(&data->update_lock);
1239 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1240 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1241 data->auto_temp[nr][point]);
1242 mutex_unlock(&data->update_lock);
1246 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1247 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1249 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1252 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1253 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1255 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1258 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1259 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1261 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1264 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1265 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1268 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1269 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1272 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1273 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1276 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1277 show_pwm_enable, set_pwm_enable, 0);
1278 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1279 static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1280 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1281 show_pwm_temp_map, set_pwm_temp_map, 0);
1282 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1283 show_auto_pwm, set_auto_pwm, 0, 0);
1284 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1285 show_auto_pwm, set_auto_pwm, 0, 1);
1286 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1287 show_auto_pwm, set_auto_pwm, 0, 2);
1288 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1289 show_auto_pwm, NULL, 0, 3);
1290 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1291 show_auto_temp, set_auto_temp, 0, 1);
1292 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1293 show_auto_temp, set_auto_temp, 0, 0);
1294 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1295 show_auto_temp, set_auto_temp, 0, 2);
1296 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1297 show_auto_temp, set_auto_temp, 0, 3);
1298 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1299 show_auto_temp, set_auto_temp, 0, 4);
1301 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1302 show_pwm_enable, set_pwm_enable, 1);
1303 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1304 static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1305 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1306 show_pwm_temp_map, set_pwm_temp_map, 1);
1307 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1308 show_auto_pwm, set_auto_pwm, 1, 0);
1309 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1310 show_auto_pwm, set_auto_pwm, 1, 1);
1311 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1312 show_auto_pwm, set_auto_pwm, 1, 2);
1313 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1314 show_auto_pwm, NULL, 1, 3);
1315 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1316 show_auto_temp, set_auto_temp, 1, 1);
1317 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1318 show_auto_temp, set_auto_temp, 1, 0);
1319 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1320 show_auto_temp, set_auto_temp, 1, 2);
1321 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1322 show_auto_temp, set_auto_temp, 1, 3);
1323 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1324 show_auto_temp, set_auto_temp, 1, 4);
1326 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1327 show_pwm_enable, set_pwm_enable, 2);
1328 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1329 static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1330 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1331 show_pwm_temp_map, set_pwm_temp_map, 2);
1332 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1333 show_auto_pwm, set_auto_pwm, 2, 0);
1334 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1335 show_auto_pwm, set_auto_pwm, 2, 1);
1336 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1337 show_auto_pwm, set_auto_pwm, 2, 2);
1338 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1339 show_auto_pwm, NULL, 2, 3);
1340 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1341 show_auto_temp, set_auto_temp, 2, 1);
1342 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1343 show_auto_temp, set_auto_temp, 2, 0);
1344 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1345 show_auto_temp, set_auto_temp, 2, 2);
1346 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1347 show_auto_temp, set_auto_temp, 2, 3);
1348 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1349 show_auto_temp, set_auto_temp, 2, 4);
1352 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1355 struct it87_data *data = it87_update_device(dev);
1356 return sprintf(buf, "%u\n", data->alarms);
1358 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1360 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1363 int bitnr = to_sensor_dev_attr(attr)->index;
1364 struct it87_data *data = it87_update_device(dev);
1365 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1368 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1369 *attr, const char *buf, size_t count)
1371 struct it87_data *data = dev_get_drvdata(dev);
1375 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1378 mutex_lock(&data->update_lock);
1379 config = it87_read_value(data, IT87_REG_CONFIG);
1384 it87_write_value(data, IT87_REG_CONFIG, config);
1385 /* Invalidate cache to force re-read */
1388 mutex_unlock(&data->update_lock);
1393 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1394 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1395 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1396 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1397 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1398 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1399 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1400 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1401 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1402 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1403 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1404 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1405 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1406 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1407 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1408 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1409 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1410 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1411 show_alarm, clear_intrusion, 4);
1413 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1416 int bitnr = to_sensor_dev_attr(attr)->index;
1417 struct it87_data *data = it87_update_device(dev);
1418 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1420 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1421 const char *buf, size_t count)
1423 int bitnr = to_sensor_dev_attr(attr)->index;
1424 struct it87_data *data = dev_get_drvdata(dev);
1427 if (kstrtol(buf, 10, &val) < 0
1428 || (val != 0 && val != 1))
1431 mutex_lock(&data->update_lock);
1432 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1434 data->beeps |= (1 << bitnr);
1436 data->beeps &= ~(1 << bitnr);
1437 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1438 mutex_unlock(&data->update_lock);
1442 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1443 show_beep, set_beep, 1);
1444 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1445 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1446 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1447 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1448 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1449 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1450 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1451 /* fanX_beep writability is set later */
1452 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1453 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1454 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1455 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1456 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1457 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1458 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1459 show_beep, set_beep, 2);
1460 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1461 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1463 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1466 struct it87_data *data = dev_get_drvdata(dev);
1467 return sprintf(buf, "%u\n", data->vrm);
1469 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1470 const char *buf, size_t count)
1472 struct it87_data *data = dev_get_drvdata(dev);
1475 if (kstrtoul(buf, 10, &val) < 0)
1482 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1484 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1487 struct it87_data *data = it87_update_device(dev);
1488 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1490 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1492 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1495 static const char * const labels[] = {
1500 static const char * const labels_it8721[] = {
1505 struct it87_data *data = dev_get_drvdata(dev);
1506 int nr = to_sensor_dev_attr(attr)->index;
1508 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1511 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1512 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1513 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1515 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1517 static ssize_t show_name(struct device *dev, struct device_attribute
1518 *devattr, char *buf)
1520 struct it87_data *data = dev_get_drvdata(dev);
1521 return sprintf(buf, "%s\n", data->name);
1523 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1525 static struct attribute *it87_attributes_in[10][5] = {
1527 &sensor_dev_attr_in0_input.dev_attr.attr,
1528 &sensor_dev_attr_in0_min.dev_attr.attr,
1529 &sensor_dev_attr_in0_max.dev_attr.attr,
1530 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1533 &sensor_dev_attr_in1_input.dev_attr.attr,
1534 &sensor_dev_attr_in1_min.dev_attr.attr,
1535 &sensor_dev_attr_in1_max.dev_attr.attr,
1536 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1539 &sensor_dev_attr_in2_input.dev_attr.attr,
1540 &sensor_dev_attr_in2_min.dev_attr.attr,
1541 &sensor_dev_attr_in2_max.dev_attr.attr,
1542 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1545 &sensor_dev_attr_in3_input.dev_attr.attr,
1546 &sensor_dev_attr_in3_min.dev_attr.attr,
1547 &sensor_dev_attr_in3_max.dev_attr.attr,
1548 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1551 &sensor_dev_attr_in4_input.dev_attr.attr,
1552 &sensor_dev_attr_in4_min.dev_attr.attr,
1553 &sensor_dev_attr_in4_max.dev_attr.attr,
1554 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1557 &sensor_dev_attr_in5_input.dev_attr.attr,
1558 &sensor_dev_attr_in5_min.dev_attr.attr,
1559 &sensor_dev_attr_in5_max.dev_attr.attr,
1560 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1563 &sensor_dev_attr_in6_input.dev_attr.attr,
1564 &sensor_dev_attr_in6_min.dev_attr.attr,
1565 &sensor_dev_attr_in6_max.dev_attr.attr,
1566 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1569 &sensor_dev_attr_in7_input.dev_attr.attr,
1570 &sensor_dev_attr_in7_min.dev_attr.attr,
1571 &sensor_dev_attr_in7_max.dev_attr.attr,
1572 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1575 &sensor_dev_attr_in8_input.dev_attr.attr,
1578 &sensor_dev_attr_in9_input.dev_attr.attr,
1582 static const struct attribute_group it87_group_in[10] = {
1583 { .attrs = it87_attributes_in[0] },
1584 { .attrs = it87_attributes_in[1] },
1585 { .attrs = it87_attributes_in[2] },
1586 { .attrs = it87_attributes_in[3] },
1587 { .attrs = it87_attributes_in[4] },
1588 { .attrs = it87_attributes_in[5] },
1589 { .attrs = it87_attributes_in[6] },
1590 { .attrs = it87_attributes_in[7] },
1591 { .attrs = it87_attributes_in[8] },
1592 { .attrs = it87_attributes_in[9] },
1595 static struct attribute *it87_attributes_temp[3][6] = {
1597 &sensor_dev_attr_temp1_input.dev_attr.attr,
1598 &sensor_dev_attr_temp1_max.dev_attr.attr,
1599 &sensor_dev_attr_temp1_min.dev_attr.attr,
1600 &sensor_dev_attr_temp1_type.dev_attr.attr,
1601 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1604 &sensor_dev_attr_temp2_input.dev_attr.attr,
1605 &sensor_dev_attr_temp2_max.dev_attr.attr,
1606 &sensor_dev_attr_temp2_min.dev_attr.attr,
1607 &sensor_dev_attr_temp2_type.dev_attr.attr,
1608 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1611 &sensor_dev_attr_temp3_input.dev_attr.attr,
1612 &sensor_dev_attr_temp3_max.dev_attr.attr,
1613 &sensor_dev_attr_temp3_min.dev_attr.attr,
1614 &sensor_dev_attr_temp3_type.dev_attr.attr,
1615 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1619 static const struct attribute_group it87_group_temp[3] = {
1620 { .attrs = it87_attributes_temp[0] },
1621 { .attrs = it87_attributes_temp[1] },
1622 { .attrs = it87_attributes_temp[2] },
1625 static struct attribute *it87_attributes_temp_offset[] = {
1626 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1627 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1628 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1631 static struct attribute *it87_attributes[] = {
1632 &dev_attr_alarms.attr,
1633 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1634 &dev_attr_name.attr,
1638 static const struct attribute_group it87_group = {
1639 .attrs = it87_attributes,
1642 static struct attribute *it87_attributes_in_beep[] = {
1643 &sensor_dev_attr_in0_beep.dev_attr.attr,
1644 &sensor_dev_attr_in1_beep.dev_attr.attr,
1645 &sensor_dev_attr_in2_beep.dev_attr.attr,
1646 &sensor_dev_attr_in3_beep.dev_attr.attr,
1647 &sensor_dev_attr_in4_beep.dev_attr.attr,
1648 &sensor_dev_attr_in5_beep.dev_attr.attr,
1649 &sensor_dev_attr_in6_beep.dev_attr.attr,
1650 &sensor_dev_attr_in7_beep.dev_attr.attr,
1655 static struct attribute *it87_attributes_temp_beep[] = {
1656 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1657 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1658 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1661 static struct attribute *it87_attributes_fan[6][3+1] = { {
1662 &sensor_dev_attr_fan1_input.dev_attr.attr,
1663 &sensor_dev_attr_fan1_min.dev_attr.attr,
1664 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1667 &sensor_dev_attr_fan2_input.dev_attr.attr,
1668 &sensor_dev_attr_fan2_min.dev_attr.attr,
1669 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1672 &sensor_dev_attr_fan3_input.dev_attr.attr,
1673 &sensor_dev_attr_fan3_min.dev_attr.attr,
1674 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1677 &sensor_dev_attr_fan4_input.dev_attr.attr,
1678 &sensor_dev_attr_fan4_min.dev_attr.attr,
1679 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1682 &sensor_dev_attr_fan5_input.dev_attr.attr,
1683 &sensor_dev_attr_fan5_min.dev_attr.attr,
1684 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1687 &sensor_dev_attr_fan6_input.dev_attr.attr,
1688 &sensor_dev_attr_fan6_min.dev_attr.attr,
1689 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1693 static const struct attribute_group it87_group_fan[6] = {
1694 { .attrs = it87_attributes_fan[0] },
1695 { .attrs = it87_attributes_fan[1] },
1696 { .attrs = it87_attributes_fan[2] },
1697 { .attrs = it87_attributes_fan[3] },
1698 { .attrs = it87_attributes_fan[4] },
1699 { .attrs = it87_attributes_fan[5] },
1702 static const struct attribute *it87_attributes_fan_div[] = {
1703 &sensor_dev_attr_fan1_div.dev_attr.attr,
1704 &sensor_dev_attr_fan2_div.dev_attr.attr,
1705 &sensor_dev_attr_fan3_div.dev_attr.attr,
1708 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1709 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1710 &sensor_dev_attr_pwm1.dev_attr.attr,
1711 &dev_attr_pwm1_freq.attr,
1712 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1715 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1716 &sensor_dev_attr_pwm2.dev_attr.attr,
1717 &dev_attr_pwm2_freq.attr,
1718 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1721 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1722 &sensor_dev_attr_pwm3.dev_attr.attr,
1723 &dev_attr_pwm3_freq.attr,
1724 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1728 static const struct attribute_group it87_group_pwm[3] = {
1729 { .attrs = it87_attributes_pwm[0] },
1730 { .attrs = it87_attributes_pwm[1] },
1731 { .attrs = it87_attributes_pwm[2] },
1734 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1735 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1736 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1737 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1738 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1739 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1740 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1741 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1742 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1743 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1746 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1747 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1748 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1749 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1750 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1751 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1752 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1753 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1754 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1757 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1758 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1759 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1760 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1761 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1762 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1763 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1764 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1765 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1769 static const struct attribute_group it87_group_autopwm[3] = {
1770 { .attrs = it87_attributes_autopwm[0] },
1771 { .attrs = it87_attributes_autopwm[1] },
1772 { .attrs = it87_attributes_autopwm[2] },
1775 static struct attribute *it87_attributes_fan_beep[] = {
1776 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1777 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1778 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1779 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1780 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1781 &sensor_dev_attr_fan6_beep.dev_attr.attr,
1784 static struct attribute *it87_attributes_vid[] = {
1786 &dev_attr_cpu0_vid.attr,
1790 static const struct attribute_group it87_group_vid = {
1791 .attrs = it87_attributes_vid,
1794 static struct attribute *it87_attributes_label[] = {
1795 &sensor_dev_attr_in3_label.dev_attr.attr,
1796 &sensor_dev_attr_in7_label.dev_attr.attr,
1797 &sensor_dev_attr_in8_label.dev_attr.attr,
1798 &sensor_dev_attr_in9_label.dev_attr.attr,
1802 static const struct attribute_group it87_group_label = {
1803 .attrs = it87_attributes_label,
1806 /* SuperIO detection - will change isa_address if a chip is found */
1807 static int __init it87_find(unsigned short *address,
1808 struct it87_sio_data *sio_data)
1812 const char *board_vendor, *board_name;
1814 err = superio_enter();
1819 chip_type = force_id ? force_id : superio_inw(DEVID);
1821 switch (chip_type) {
1823 sio_data->type = it87;
1826 sio_data->type = it8712;
1830 sio_data->type = it8716;
1833 sio_data->type = it8718;
1836 sio_data->type = it8720;
1839 sio_data->type = it8721;
1842 sio_data->type = it8728;
1845 sio_data->type = it8771;
1848 sio_data->type = it8772;
1851 sio_data->type = it8781;
1854 sio_data->type = it8782;
1857 sio_data->type = it8783;
1860 sio_data->type = it8786;
1864 sio_data->type = it8603;
1867 sio_data->type = it8620;
1870 sio_data->type = it8790;
1872 case 0xffff: /* No device at all */
1875 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1879 superio_select(PME);
1880 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1881 pr_info("Device not activated, skipping\n");
1885 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1886 if (*address == 0) {
1887 pr_info("Base address not set, skipping\n");
1892 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1893 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1894 it87_devices[sio_data->type].suffix,
1895 *address, sio_data->revision);
1897 /* in8 (Vbat) is always internal */
1898 sio_data->internal = (1 << 2);
1900 /* in7 (VSB or VCCH5V) is always internal on some chips */
1901 if (it87_devices[sio_data->type].features & FEAT_IN7_INTERNAL)
1902 sio_data->internal |= (1 << 1);
1904 /* in9 (AVCC3), always internal if supported */
1905 if (it87_devices[sio_data->type].features & FEAT_AVCC3)
1906 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1908 sio_data->skip_in |= (1 << 9);
1910 if (!(it87_devices[sio_data->type].features & FEAT_VID))
1911 sio_data->skip_vid = 1;
1913 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1914 if (sio_data->type == it87) {
1915 /* The IT8705F has a different LD number for GPIO */
1917 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1918 } else if (sio_data->type == it8783) {
1919 int reg25, reg27, reg2a, reg2c, regef;
1921 superio_select(GPIO);
1923 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1924 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1925 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1926 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1927 regef = superio_inb(IT87_SIO_SPI_REG);
1929 /* Check if fan3 is there or not */
1930 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
1931 sio_data->skip_fan |= (1 << 2);
1932 if ((reg25 & (1 << 4))
1933 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
1934 sio_data->skip_pwm |= (1 << 2);
1936 /* Check if fan2 is there or not */
1937 if (reg27 & (1 << 7))
1938 sio_data->skip_fan |= (1 << 1);
1939 if (reg27 & (1 << 3))
1940 sio_data->skip_pwm |= (1 << 1);
1943 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
1944 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1947 if (reg27 & (1 << 1))
1948 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1952 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1954 if (reg27 & (1 << 2)) {
1956 * The data sheet is a bit unclear regarding the
1957 * internal voltage divider for VCCH5V. It says
1958 * "This bit enables and switches VIN7 (pin 91) to the
1959 * internal voltage divider for VCCH5V".
1960 * This is different to other chips, where the internal
1961 * voltage divider would connect VIN7 to an internal
1962 * voltage source. Maybe that is the case here as well.
1964 * Since we don't know for sure, re-route it if that is
1965 * not the case, and ask the user to report if the
1966 * resulting voltage is sane.
1968 if (!(reg2c & (1 << 1))) {
1970 superio_outb(IT87_SIO_PINX2_REG, reg2c);
1971 pr_notice("Routing internal VCCH5V to in7.\n");
1973 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1974 pr_notice("Please report if it displays a reasonable voltage.\n");
1977 if (reg2c & (1 << 0))
1978 sio_data->internal |= (1 << 0);
1979 if (reg2c & (1 << 1))
1980 sio_data->internal |= (1 << 1);
1982 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1983 } else if (sio_data->type == it8603) {
1986 superio_select(GPIO);
1988 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1990 /* Check if fan3 is there or not */
1991 if (reg27 & (1 << 6))
1992 sio_data->skip_pwm |= (1 << 2);
1993 if (reg27 & (1 << 7))
1994 sio_data->skip_fan |= (1 << 2);
1996 /* Check if fan2 is there or not */
1997 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1998 if (reg29 & (1 << 1))
1999 sio_data->skip_pwm |= (1 << 1);
2000 if (reg29 & (1 << 2))
2001 sio_data->skip_fan |= (1 << 1);
2003 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2004 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2006 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2011 superio_select(GPIO);
2013 reg = superio_inb(IT87_SIO_GPIO3_REG);
2014 if (!sio_data->skip_vid) {
2015 /* We need at least 4 VID pins */
2017 pr_info("VID is disabled (pins used for GPIO)\n");
2018 sio_data->skip_vid = 1;
2022 /* Check if fan3 is there or not */
2024 sio_data->skip_pwm |= (1 << 2);
2026 sio_data->skip_fan |= (1 << 2);
2028 /* Check if fan2 is there or not */
2029 reg = superio_inb(IT87_SIO_GPIO5_REG);
2031 sio_data->skip_pwm |= (1 << 1);
2033 sio_data->skip_fan |= (1 << 1);
2035 if ((sio_data->type == it8718 || sio_data->type == it8720)
2036 && !(sio_data->skip_vid))
2037 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
2039 reg = superio_inb(IT87_SIO_PINX2_REG);
2041 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2044 * The IT8720F has no VIN7 pin, so VCCH should always be
2045 * routed internally to VIN7 with an internal divider.
2046 * Curiously, there still is a configuration bit to control
2047 * this, which means it can be set incorrectly. And even
2048 * more curiously, many boards out there are improperly
2049 * configured, even though the IT8720F datasheet claims
2050 * that the internal routing of VCCH to VIN7 is the default
2051 * setting. So we force the internal routing in this case.
2053 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2054 * If UART6 is enabled, re-route VIN7 to the internal divider
2055 * if that is not already the case.
2057 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
2059 superio_outb(IT87_SIO_PINX2_REG, reg);
2060 pr_notice("Routing internal VCCH to in7\n");
2063 sio_data->internal |= (1 << 0);
2065 sio_data->internal |= (1 << 1);
2068 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2069 * While VIN7 can be routed to the internal voltage divider,
2070 * VIN5 and VIN6 are not available if UART6 is enabled.
2072 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2073 * is the temperature source. Since we can not read the
2074 * temperature source here, skip_temp is preliminary.
2077 sio_data->skip_in |= (1 << 5) | (1 << 6);
2078 sio_data->skip_temp |= (1 << 2);
2081 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2083 if (sio_data->beep_pin)
2084 pr_info("Beeping is supported\n");
2086 /* Disable specific features based on DMI strings */
2087 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2088 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2089 if (board_vendor && board_name) {
2090 if (strcmp(board_vendor, "nVIDIA") == 0
2091 && strcmp(board_name, "FN68PT") == 0) {
2093 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2094 * connected to a fan, but to something else. One user
2095 * has reported instant system power-off when changing
2096 * the PWM2 duty cycle, so we disable it.
2097 * I use the board name string as the trigger in case
2098 * the same board is ever used in other systems.
2100 pr_info("Disabling pwm2 due to hardware constraints\n");
2101 sio_data->skip_pwm = (1 << 1);
2110 static void it87_remove_files(struct device *dev)
2112 struct it87_data *data = platform_get_drvdata(pdev);
2113 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2116 sysfs_remove_group(&dev->kobj, &it87_group);
2117 for (i = 0; i < 10; i++) {
2118 if (sio_data->skip_in & (1 << i))
2120 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2121 if (it87_attributes_in_beep[i])
2122 sysfs_remove_file(&dev->kobj,
2123 it87_attributes_in_beep[i]);
2125 for (i = 0; i < 3; i++) {
2126 if (!(data->has_temp & (1 << i)))
2128 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
2129 if (has_temp_offset(data))
2130 sysfs_remove_file(&dev->kobj,
2131 it87_attributes_temp_offset[i]);
2132 if (sio_data->beep_pin)
2133 sysfs_remove_file(&dev->kobj,
2134 it87_attributes_temp_beep[i]);
2136 for (i = 0; i < 6; i++) {
2137 if (!(data->has_fan & (1 << i)))
2139 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
2140 if (sio_data->beep_pin)
2141 sysfs_remove_file(&dev->kobj,
2142 it87_attributes_fan_beep[i]);
2143 if (i < 3 && !has_16bit_fans(data))
2144 sysfs_remove_file(&dev->kobj,
2145 it87_attributes_fan_div[i]);
2147 for (i = 0; i < 3; i++) {
2148 if (sio_data->skip_pwm & (1 << 0))
2150 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
2151 if (has_old_autopwm(data))
2152 sysfs_remove_group(&dev->kobj,
2153 &it87_group_autopwm[i]);
2155 if (!sio_data->skip_vid)
2156 sysfs_remove_group(&dev->kobj, &it87_group_vid);
2157 sysfs_remove_group(&dev->kobj, &it87_group_label);
2160 static int it87_probe(struct platform_device *pdev)
2162 struct it87_data *data;
2163 struct resource *res;
2164 struct device *dev = &pdev->dev;
2165 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2167 int enable_pwm_interface;
2168 int fan_beep_need_rw;
2170 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2171 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2173 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2174 (unsigned long)res->start,
2175 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2179 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2183 data->addr = res->start;
2184 data->type = sio_data->type;
2185 data->features = it87_devices[sio_data->type].features;
2186 data->peci_mask = it87_devices[sio_data->type].peci_mask;
2187 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
2188 data->name = it87_devices[sio_data->type].name;
2190 * IT8705F Datasheet 0.4.1, 3h == Version G.
2191 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2192 * These are the first revisions with 16-bit tachometer support.
2194 switch (data->type) {
2196 if (sio_data->revision >= 0x03) {
2197 data->features &= ~FEAT_OLD_AUTOPWM;
2198 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
2202 if (sio_data->revision >= 0x08) {
2203 data->features &= ~FEAT_OLD_AUTOPWM;
2204 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2212 /* Now, we do the remaining detection. */
2213 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2214 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2217 platform_set_drvdata(pdev, data);
2219 mutex_init(&data->update_lock);
2221 /* Check PWM configuration */
2222 enable_pwm_interface = it87_check_pwm(dev);
2224 /* Starting with IT8721F, we handle scaling of internal voltages */
2225 if (has_12mv_adc(data)) {
2226 if (sio_data->internal & (1 << 0))
2227 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2228 if (sio_data->internal & (1 << 1))
2229 data->in_scaled |= (1 << 7); /* in7 is VSB */
2230 if (sio_data->internal & (1 << 2))
2231 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2232 if (sio_data->internal & (1 << 3))
2233 data->in_scaled |= (1 << 9); /* in9 is AVCC */
2234 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2235 sio_data->type == it8783) {
2236 if (sio_data->internal & (1 << 0))
2237 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2238 if (sio_data->internal & (1 << 1))
2239 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
2242 data->has_temp = 0x07;
2243 if (sio_data->skip_temp & (1 << 2)) {
2244 if (sio_data->type == it8782
2245 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2246 data->has_temp &= ~(1 << 2);
2249 /* Initialize the IT87 chip */
2250 it87_init_device(pdev);
2252 /* Register sysfs hooks */
2253 err = sysfs_create_group(&dev->kobj, &it87_group);
2257 for (i = 0; i < 10; i++) {
2258 if (sio_data->skip_in & (1 << i))
2260 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2263 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2264 err = sysfs_create_file(&dev->kobj,
2265 it87_attributes_in_beep[i]);
2271 for (i = 0; i < 3; i++) {
2272 if (!(data->has_temp & (1 << i)))
2274 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2277 if (has_temp_offset(data)) {
2278 err = sysfs_create_file(&dev->kobj,
2279 it87_attributes_temp_offset[i]);
2283 if (sio_data->beep_pin) {
2284 err = sysfs_create_file(&dev->kobj,
2285 it87_attributes_temp_beep[i]);
2291 /* Do not create fan files for disabled fans */
2292 fan_beep_need_rw = 1;
2293 for (i = 0; i < 6; i++) {
2294 if (!(data->has_fan & (1 << i)))
2296 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2300 if (i < 3 && !has_16bit_fans(data)) {
2301 err = sysfs_create_file(&dev->kobj,
2302 it87_attributes_fan_div[i]);
2307 if (sio_data->beep_pin) {
2308 err = sysfs_create_file(&dev->kobj,
2309 it87_attributes_fan_beep[i]);
2312 if (!fan_beep_need_rw)
2316 * As we have a single beep enable bit for all fans,
2317 * only the first enabled fan has a writable attribute
2320 if (sysfs_chmod_file(&dev->kobj,
2321 it87_attributes_fan_beep[i],
2323 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2325 fan_beep_need_rw = 0;
2329 if (enable_pwm_interface) {
2330 for (i = 0; i < 3; i++) {
2331 if (sio_data->skip_pwm & (1 << i))
2333 err = sysfs_create_group(&dev->kobj,
2334 &it87_group_pwm[i]);
2338 if (!has_old_autopwm(data))
2340 err = sysfs_create_group(&dev->kobj,
2341 &it87_group_autopwm[i]);
2347 if (!sio_data->skip_vid) {
2348 data->vrm = vid_which_vrm();
2349 /* VID reading from Super-I/O config space if available */
2350 data->vid = sio_data->vid_value;
2351 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2356 /* Export labels for internal sensors */
2357 for (i = 0; i < 4; i++) {
2358 if (!(sio_data->internal & (1 << i)))
2360 err = sysfs_create_file(&dev->kobj,
2361 it87_attributes_label[i]);
2366 data->hwmon_dev = hwmon_device_register(dev);
2367 if (IS_ERR(data->hwmon_dev)) {
2368 err = PTR_ERR(data->hwmon_dev);
2375 it87_remove_files(dev);
2379 static int it87_remove(struct platform_device *pdev)
2381 struct it87_data *data = platform_get_drvdata(pdev);
2383 hwmon_device_unregister(data->hwmon_dev);
2384 it87_remove_files(&pdev->dev);
2390 * Must be called with data->update_lock held, except during initialization.
2391 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2392 * would slow down the IT87 access and should not be necessary.
2394 static int it87_read_value(struct it87_data *data, u8 reg)
2396 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2397 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2401 * Must be called with data->update_lock held, except during initialization.
2402 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2403 * would slow down the IT87 access and should not be necessary.
2405 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2407 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2408 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2411 /* Return 1 if and only if the PWM interface is safe to use */
2412 static int it87_check_pwm(struct device *dev)
2414 struct it87_data *data = dev_get_drvdata(dev);
2416 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2417 * and polarity set to active low is sign that this is the case so we
2418 * disable pwm control to protect the user.
2420 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2421 if ((tmp & 0x87) == 0) {
2422 if (fix_pwm_polarity) {
2424 * The user asks us to attempt a chip reconfiguration.
2425 * This means switching to active high polarity and
2426 * inverting all fan speed values.
2431 for (i = 0; i < 3; i++)
2432 pwm[i] = it87_read_value(data,
2436 * If any fan is in automatic pwm mode, the polarity
2437 * might be correct, as suspicious as it seems, so we
2438 * better don't change anything (but still disable the
2441 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2443 "Reconfiguring PWM to active high polarity\n");
2444 it87_write_value(data, IT87_REG_FAN_CTL,
2446 for (i = 0; i < 3; i++)
2447 it87_write_value(data,
2454 "PWM configuration is too broken to be fixed\n");
2458 "Detected broken BIOS defaults, disabling PWM interface\n");
2460 } else if (fix_pwm_polarity) {
2462 "PWM configuration looks sane, won't touch\n");
2468 /* Called when we have found a new IT87. */
2469 static void it87_init_device(struct platform_device *pdev)
2471 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2472 struct it87_data *data = platform_get_drvdata(pdev);
2477 * For each PWM channel:
2478 * - If it is in automatic mode, setting to manual mode should set
2479 * the fan to full speed by default.
2480 * - If it is in manual mode, we need a mapping to temperature
2481 * channels to use when later setting to automatic mode later.
2482 * Use a 1:1 mapping by default (we are clueless.)
2483 * In both cases, the value can (and should) be changed by the user
2484 * prior to switching to a different mode.
2485 * Note that this is no longer needed for the IT8721F and later, as
2486 * these have separate registers for the temperature mapping and the
2487 * manual duty cycle.
2489 for (i = 0; i < 3; i++) {
2490 data->pwm_temp_map[i] = i;
2491 data->pwm_duty[i] = 0x7f; /* Full speed */
2492 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2496 * Some chips seem to have default value 0xff for all limit
2497 * registers. For low voltage limits it makes no sense and triggers
2498 * alarms, so change to 0 instead. For high temperature limits, it
2499 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2500 * but is still confusing, so change to 127 degrees C.
2502 for (i = 0; i < 8; i++) {
2503 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2505 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2507 for (i = 0; i < 3; i++) {
2508 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2510 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2514 * Temperature channels are not forcibly enabled, as they can be
2515 * set to two different sensor types and we can't guess which one
2516 * is correct for a given system. These channels can be enabled at
2517 * run-time through the temp{1-3}_type sysfs accessors if needed.
2520 /* Check if voltage monitors are reset manually or by some reason */
2521 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2522 if ((tmp & 0xff) == 0) {
2523 /* Enable all voltage monitors */
2524 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2527 /* Check if tachometers are reset manually or by some reason */
2528 mask = 0x70 & ~(sio_data->skip_fan << 4);
2529 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2530 if ((data->fan_main_ctrl & mask) == 0) {
2531 /* Enable all fan tachometers */
2532 data->fan_main_ctrl |= mask;
2533 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2534 data->fan_main_ctrl);
2536 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2538 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2540 /* Set tachometers to 16-bit mode if needed */
2541 if (has_fan16_config(data)) {
2542 if (~tmp & 0x07 & data->has_fan) {
2544 "Setting fan1-3 to 16-bit mode\n");
2545 it87_write_value(data, IT87_REG_FAN_16BIT,
2550 /* Check for additional fans */
2551 if (has_five_fans(data)) {
2553 data->has_fan |= (1 << 3); /* fan4 enabled */
2555 data->has_fan |= (1 << 4); /* fan5 enabled */
2558 if (has_six_fans(data)) {
2560 data->has_fan |= (1 << 5); /* fan6 enabled */
2563 /* Fan input pins may be used for alternative functions */
2564 data->has_fan &= ~sio_data->skip_fan;
2566 /* Start monitoring */
2567 it87_write_value(data, IT87_REG_CONFIG,
2568 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2569 | (update_vbat ? 0x41 : 0x01));
2572 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2574 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2575 if (has_newer_autopwm(data)) {
2576 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2577 data->pwm_duty[nr] = it87_read_value(data,
2578 IT87_REG_PWM_DUTY(nr));
2580 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2581 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2582 else /* Manual mode */
2583 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2586 if (has_old_autopwm(data)) {
2589 for (i = 0; i < 5 ; i++)
2590 data->auto_temp[nr][i] = it87_read_value(data,
2591 IT87_REG_AUTO_TEMP(nr, i));
2592 for (i = 0; i < 3 ; i++)
2593 data->auto_pwm[nr][i] = it87_read_value(data,
2594 IT87_REG_AUTO_PWM(nr, i));
2598 static struct it87_data *it87_update_device(struct device *dev)
2600 struct it87_data *data = dev_get_drvdata(dev);
2603 mutex_lock(&data->update_lock);
2605 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2609 * Cleared after each update, so reenable. Value
2610 * returned by this read will be previous value
2612 it87_write_value(data, IT87_REG_CONFIG,
2613 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2615 for (i = 0; i <= 7; i++) {
2617 it87_read_value(data, IT87_REG_VIN(i));
2619 it87_read_value(data, IT87_REG_VIN_MIN(i));
2621 it87_read_value(data, IT87_REG_VIN_MAX(i));
2623 /* in8 (battery) has no limit registers */
2624 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2625 if (has_avcc3(data))
2626 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
2628 for (i = 0; i < 6; i++) {
2629 /* Skip disabled fans */
2630 if (!(data->has_fan & (1 << i)))
2634 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2635 data->fan[i][0] = it87_read_value(data,
2637 /* Add high byte if in 16-bit mode */
2638 if (has_16bit_fans(data)) {
2639 data->fan[i][0] |= it87_read_value(data,
2640 IT87_REG_FANX[i]) << 8;
2641 data->fan[i][1] |= it87_read_value(data,
2642 IT87_REG_FANX_MIN[i]) << 8;
2645 for (i = 0; i < 3; i++) {
2646 if (!(data->has_temp & (1 << i)))
2649 it87_read_value(data, IT87_REG_TEMP(i));
2651 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2653 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2654 if (has_temp_offset(data))
2656 it87_read_value(data,
2657 IT87_REG_TEMP_OFFSET[i]);
2660 /* Newer chips don't have clock dividers */
2661 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2662 i = it87_read_value(data, IT87_REG_FAN_DIV);
2663 data->fan_div[0] = i & 0x07;
2664 data->fan_div[1] = (i >> 3) & 0x07;
2665 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2669 it87_read_value(data, IT87_REG_ALARM1) |
2670 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2671 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2672 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2674 data->fan_main_ctrl = it87_read_value(data,
2675 IT87_REG_FAN_MAIN_CTRL);
2676 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2677 for (i = 0; i < 3; i++)
2678 it87_update_pwm_ctrl(data, i);
2680 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2681 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
2683 * The IT8705F does not have VID capability.
2684 * The IT8718F and later don't use IT87_REG_VID for the
2687 if (data->type == it8712 || data->type == it8716) {
2688 data->vid = it87_read_value(data, IT87_REG_VID);
2690 * The older IT8712F revisions had only 5 VID pins,
2691 * but we assume it is always safe to read 6 bits.
2695 data->last_updated = jiffies;
2699 mutex_unlock(&data->update_lock);
2704 static int __init it87_device_add(unsigned short address,
2705 const struct it87_sio_data *sio_data)
2707 struct resource res = {
2708 .start = address + IT87_EC_OFFSET,
2709 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2711 .flags = IORESOURCE_IO,
2715 err = acpi_check_resource_conflict(&res);
2719 pdev = platform_device_alloc(DRVNAME, address);
2722 pr_err("Device allocation failed\n");
2726 err = platform_device_add_resources(pdev, &res, 1);
2728 pr_err("Device resource addition failed (%d)\n", err);
2729 goto exit_device_put;
2732 err = platform_device_add_data(pdev, sio_data,
2733 sizeof(struct it87_sio_data));
2735 pr_err("Platform data allocation failed\n");
2736 goto exit_device_put;
2739 err = platform_device_add(pdev);
2741 pr_err("Device addition failed (%d)\n", err);
2742 goto exit_device_put;
2748 platform_device_put(pdev);
2753 static int __init sm_it87_init(void)
2756 unsigned short isa_address = 0;
2757 struct it87_sio_data sio_data;
2759 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2760 err = it87_find(&isa_address, &sio_data);
2763 err = platform_driver_register(&it87_driver);
2767 err = it87_device_add(isa_address, &sio_data);
2769 platform_driver_unregister(&it87_driver);
2776 static void __exit sm_it87_exit(void)
2778 platform_device_unregister(pdev);
2779 platform_driver_unregister(&it87_driver);
2783 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
2784 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2785 module_param(update_vbat, bool, 0);
2786 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2787 module_param(fix_pwm_polarity, bool, 0);
2788 MODULE_PARM_DESC(fix_pwm_polarity,
2789 "Force PWM polarity to active high (DANGEROUS)");
2790 MODULE_LICENSE("GPL");
2792 module_init(sm_it87_init);
2793 module_exit(sm_it87_exit);