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: IT8705F Super I/O chip w/LPC interface
14 * IT8712F Super I/O chip w/LPC interface
15 * IT8716F Super I/O chip w/LPC interface
16 * IT8718F Super I/O chip w/LPC interface
17 * IT8720F Super I/O chip w/LPC interface
18 * IT8721F Super I/O chip w/LPC interface
19 * IT8726F Super I/O chip w/LPC interface
20 * IT8728F Super I/O chip w/LPC interface
21 * IT8758E Super I/O chip w/LPC interface
22 * IT8782F Super I/O chip w/LPC interface
23 * IT8783E/F Super I/O chip w/LPC interface
24 * Sis950 A clone of the IT8705F
26 * Copyright (C) 2001 Chris Gauthron
27 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/slab.h>
49 #include <linux/jiffies.h>
50 #include <linux/platform_device.h>
51 #include <linux/hwmon.h>
52 #include <linux/hwmon-sysfs.h>
53 #include <linux/hwmon-vid.h>
54 #include <linux/err.h>
55 #include <linux/mutex.h>
56 #include <linux/sysfs.h>
57 #include <linux/string.h>
58 #include <linux/dmi.h>
59 #include <linux/acpi.h>
62 #define DRVNAME "it87"
64 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8782,
67 static unsigned short force_id;
68 module_param(force_id, ushort, 0);
69 MODULE_PARM_DESC(force_id, "Override the detected device ID");
71 static struct platform_device *pdev;
73 #define REG 0x2e /* The register to read/write */
74 #define DEV 0x07 /* Register: Logical device select */
75 #define VAL 0x2f /* The value to read/write */
76 #define PME 0x04 /* The device with the fan registers in it */
78 /* The device with the IT8718F/IT8720F VID value in it */
81 #define DEVID 0x20 /* Register: Device ID */
82 #define DEVREV 0x22 /* Register: Device Revision */
84 static inline int superio_inb(int reg)
90 static inline void superio_outb(int reg, int val)
96 static int superio_inw(int reg)
106 static inline void superio_select(int ldn)
112 static inline int superio_enter(void)
115 * Try to reserve REG and REG + 1 for exclusive access.
117 if (!request_muxed_region(REG, 2, DRVNAME))
127 static inline void superio_exit(void)
131 release_region(REG, 2);
134 /* Logical device 4 registers */
135 #define IT8712F_DEVID 0x8712
136 #define IT8705F_DEVID 0x8705
137 #define IT8716F_DEVID 0x8716
138 #define IT8718F_DEVID 0x8718
139 #define IT8720F_DEVID 0x8720
140 #define IT8721F_DEVID 0x8721
141 #define IT8726F_DEVID 0x8726
142 #define IT8728F_DEVID 0x8728
143 #define IT8782F_DEVID 0x8782
144 #define IT8783E_DEVID 0x8783
145 #define IT87_ACT_REG 0x30
146 #define IT87_BASE_REG 0x60
148 /* Logical device 7 registers (IT8712F and later) */
149 #define IT87_SIO_GPIO1_REG 0x25
150 #define IT87_SIO_GPIO3_REG 0x27
151 #define IT87_SIO_GPIO5_REG 0x29
152 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
153 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
154 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
155 #define IT87_SIO_VID_REG 0xfc /* VID value */
156 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
158 /* Update battery voltage after every reading if true */
159 static bool update_vbat;
161 /* Not all BIOSes properly configure the PWM registers */
162 static bool fix_pwm_polarity;
164 /* Many IT87 constants specified below */
166 /* Length of ISA address segment */
167 #define IT87_EXTENT 8
169 /* Length of ISA address segment for Environmental Controller */
170 #define IT87_EC_EXTENT 2
172 /* Offset of EC registers from ISA base address */
173 #define IT87_EC_OFFSET 5
175 /* Where are the ISA address/data registers relative to the EC base address */
176 #define IT87_ADDR_REG_OFFSET 0
177 #define IT87_DATA_REG_OFFSET 1
179 /*----- The IT87 registers -----*/
181 #define IT87_REG_CONFIG 0x00
183 #define IT87_REG_ALARM1 0x01
184 #define IT87_REG_ALARM2 0x02
185 #define IT87_REG_ALARM3 0x03
188 * The IT8718F and IT8720F have the VID value in a different register, in
189 * Super-I/O configuration space.
191 #define IT87_REG_VID 0x0a
193 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
194 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
197 #define IT87_REG_FAN_DIV 0x0b
198 #define IT87_REG_FAN_16BIT 0x0c
200 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
202 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
203 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
204 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
205 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
206 #define IT87_REG_FAN_MAIN_CTRL 0x13
207 #define IT87_REG_FAN_CTL 0x14
208 #define IT87_REG_PWM(nr) (0x15 + (nr))
209 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
211 #define IT87_REG_VIN(nr) (0x20 + (nr))
212 #define IT87_REG_TEMP(nr) (0x29 + (nr))
214 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
215 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
216 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
217 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
219 #define IT87_REG_VIN_ENABLE 0x50
220 #define IT87_REG_TEMP_ENABLE 0x51
221 #define IT87_REG_TEMP_EXTRA 0x55
222 #define IT87_REG_BEEP_ENABLE 0x5c
224 #define IT87_REG_CHIPID 0x58
226 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
227 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
230 struct it87_sio_data {
232 /* Values read from Super-I/O config space */
236 u8 internal; /* Internal sensors can be labeled */
237 /* Features skipped based on config or DMI */
246 * For each registered chip, we need to keep some data in memory.
247 * The structure is dynamically allocated.
250 struct device *hwmon_dev;
256 struct mutex update_lock;
257 char valid; /* !=0 if following fields are valid */
258 unsigned long last_updated; /* In jiffies */
260 u16 in_scaled; /* Internal voltage sensors are scaled */
261 u8 in[9][3]; /* [nr][0]=in, [1]=min, [2]=max */
262 u8 has_fan; /* Bitfield, fans enabled */
263 u16 fan[5]; /* Register values, possibly combined */
264 u16 fan_min[5]; /* Register values, possibly combined */
265 u8 has_temp; /* Bitfield, temp sensors enabled */
266 s8 temp[3][3]; /* [nr][0]=temp, [1]=min, [2]=max */
267 u8 sensor; /* Register value */
268 u8 fan_div[3]; /* Register encoding, shifted right */
269 u8 vid; /* Register encoding, combined */
271 u32 alarms; /* Register encoding, combined */
272 u8 beeps; /* Register encoding */
273 u8 fan_main_ctrl; /* Register value */
274 u8 fan_ctl; /* Register value */
277 * The following 3 arrays correspond to the same registers up to
278 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
279 * 7, and we want to preserve settings on mode changes, so we have
280 * to track all values separately.
281 * Starting with the IT8721F, the manual PWM duty cycles are stored
282 * in separate registers (8-bit values), so the separate tracking
283 * is no longer needed, but it is still done to keep the driver
286 u8 pwm_ctrl[3]; /* Register value */
287 u8 pwm_duty[3]; /* Manual PWM value set by user */
288 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
290 /* Automatic fan speed control registers */
291 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
292 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
295 static inline int has_12mv_adc(const struct it87_data *data)
298 * IT8721F and later have a 12 mV ADC, also with internal scaling
299 * on selected inputs.
301 return data->type == it8721
302 || data->type == it8728;
305 static inline int has_newer_autopwm(const struct it87_data *data)
308 * IT8721F and later have separate registers for the temperature
309 * mapping and the manual duty cycle.
311 return data->type == it8721
312 || data->type == it8728;
315 static int adc_lsb(const struct it87_data *data, int nr)
317 int lsb = has_12mv_adc(data) ? 12 : 16;
318 if (data->in_scaled & (1 << nr))
323 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
325 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
326 return SENSORS_LIMIT(val, 0, 255);
329 static int in_from_reg(const struct it87_data *data, int nr, int val)
331 return val * adc_lsb(data, nr);
334 static inline u8 FAN_TO_REG(long rpm, int div)
338 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
339 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
343 static inline u16 FAN16_TO_REG(long rpm)
347 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
350 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
351 1350000 / ((val) * (div)))
352 /* The divider is fixed to 2 in 16-bit mode */
353 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
354 1350000 / ((val) * 2))
356 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
357 ((val) + 500) / 1000), -128, 127))
358 #define TEMP_FROM_REG(val) ((val) * 1000)
360 static u8 pwm_to_reg(const struct it87_data *data, long val)
362 if (has_newer_autopwm(data))
368 static int pwm_from_reg(const struct it87_data *data, u8 reg)
370 if (has_newer_autopwm(data))
373 return (reg & 0x7f) << 1;
377 static int DIV_TO_REG(int val)
380 while (answer < 7 && (val >>= 1))
384 #define DIV_FROM_REG(val) (1 << (val))
386 static const unsigned int pwm_freq[8] = {
397 static inline int has_16bit_fans(const struct it87_data *data)
400 * IT8705F Datasheet 0.4.1, 3h == Version G.
401 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
402 * These are the first revisions with 16-bit tachometer support.
404 return (data->type == it87 && data->revision >= 0x03)
405 || (data->type == it8712 && data->revision >= 0x08)
406 || data->type == it8716
407 || data->type == it8718
408 || data->type == it8720
409 || data->type == it8721
410 || data->type == it8728
411 || data->type == it8782
412 || data->type == it8783;
415 static inline int has_old_autopwm(const struct it87_data *data)
418 * The old automatic fan speed control interface is implemented
419 * by IT8705F chips up to revision F and IT8712F chips up to
422 return (data->type == it87 && data->revision < 0x03)
423 || (data->type == it8712 && data->revision < 0x08);
426 static int it87_probe(struct platform_device *pdev);
427 static int it87_remove(struct platform_device *pdev);
429 static int it87_read_value(struct it87_data *data, u8 reg);
430 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
431 static struct it87_data *it87_update_device(struct device *dev);
432 static int it87_check_pwm(struct device *dev);
433 static void it87_init_device(struct platform_device *pdev);
436 static struct platform_driver it87_driver = {
438 .owner = THIS_MODULE,
442 .remove = it87_remove,
445 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
448 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
450 int index = sattr->index;
452 struct it87_data *data = it87_update_device(dev);
453 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
456 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
457 const char *buf, size_t count)
459 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
461 int index = sattr->index;
463 struct it87_data *data = dev_get_drvdata(dev);
466 if (kstrtoul(buf, 10, &val) < 0)
469 mutex_lock(&data->update_lock);
470 data->in[nr][index] = in_to_reg(data, nr, val);
471 it87_write_value(data,
472 index == 1 ? IT87_REG_VIN_MIN(nr)
473 : IT87_REG_VIN_MAX(nr),
474 data->in[nr][index]);
475 mutex_unlock(&data->update_lock);
479 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
480 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
482 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
485 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
486 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
488 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
491 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
492 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
494 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
497 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
498 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
500 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
503 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
504 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
506 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
509 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
510 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
512 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
515 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
516 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
518 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
521 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
522 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
524 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
527 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
530 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
533 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
535 int index = sattr->index;
536 struct it87_data *data = it87_update_device(dev);
538 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
541 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
544 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
546 int index = sattr->index;
547 struct it87_data *data = dev_get_drvdata(dev);
550 if (kstrtol(buf, 10, &val) < 0)
553 mutex_lock(&data->update_lock);
554 data->temp[nr][index] = TEMP_TO_REG(val);
555 it87_write_value(data,
556 index == 1 ? IT87_REG_TEMP_LOW(nr)
557 : IT87_REG_TEMP_HIGH(nr),
558 data->temp[nr][index]);
559 mutex_unlock(&data->update_lock);
563 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
564 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
566 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
568 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
569 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
571 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
573 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
574 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
576 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
579 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
582 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
583 int nr = sensor_attr->index;
584 struct it87_data *data = it87_update_device(dev);
585 u8 reg = data->sensor; /* In case value is updated while used */
588 return sprintf(buf, "3\n"); /* thermal diode */
590 return sprintf(buf, "4\n"); /* thermistor */
591 return sprintf(buf, "0\n"); /* disabled */
593 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
596 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
597 int nr = sensor_attr->index;
599 struct it87_data *data = dev_get_drvdata(dev);
603 if (kstrtol(buf, 10, &val) < 0)
606 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
609 if (val == 2) { /* backwards compatibility */
610 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
614 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
622 mutex_lock(&data->update_lock);
624 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
625 data->valid = 0; /* Force cache refresh */
626 mutex_unlock(&data->update_lock);
629 #define show_sensor_offset(offset) \
630 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
631 show_sensor, set_sensor, offset - 1);
633 show_sensor_offset(1);
634 show_sensor_offset(2);
635 show_sensor_offset(3);
639 static int pwm_mode(const struct it87_data *data, int nr)
641 int ctrl = data->fan_main_ctrl & (1 << nr);
643 if (ctrl == 0) /* Full speed */
645 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
647 else /* Manual mode */
651 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
654 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
655 int nr = sensor_attr->index;
657 struct it87_data *data = it87_update_device(dev);
658 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
659 DIV_FROM_REG(data->fan_div[nr])));
661 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
664 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
665 int nr = sensor_attr->index;
667 struct it87_data *data = it87_update_device(dev);
668 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
669 DIV_FROM_REG(data->fan_div[nr])));
671 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
674 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
675 int nr = sensor_attr->index;
677 struct it87_data *data = it87_update_device(dev);
678 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
680 static ssize_t show_pwm_enable(struct device *dev,
681 struct device_attribute *attr, char *buf)
683 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
684 int nr = sensor_attr->index;
686 struct it87_data *data = it87_update_device(dev);
687 return sprintf(buf, "%d\n", pwm_mode(data, nr));
689 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
692 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
693 int nr = sensor_attr->index;
695 struct it87_data *data = it87_update_device(dev);
696 return sprintf(buf, "%d\n",
697 pwm_from_reg(data, data->pwm_duty[nr]));
699 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
702 struct it87_data *data = it87_update_device(dev);
703 int index = (data->fan_ctl >> 4) & 0x07;
705 return sprintf(buf, "%u\n", pwm_freq[index]);
707 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
710 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
711 int nr = sensor_attr->index;
713 struct it87_data *data = dev_get_drvdata(dev);
717 if (kstrtol(buf, 10, &val) < 0)
720 mutex_lock(&data->update_lock);
721 reg = it87_read_value(data, IT87_REG_FAN_DIV);
724 data->fan_div[nr] = reg & 0x07;
727 data->fan_div[nr] = (reg >> 3) & 0x07;
730 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
734 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
735 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
736 mutex_unlock(&data->update_lock);
739 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
740 const char *buf, size_t count)
742 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
743 int nr = sensor_attr->index;
745 struct it87_data *data = dev_get_drvdata(dev);
750 if (kstrtoul(buf, 10, &val) < 0)
753 mutex_lock(&data->update_lock);
754 old = it87_read_value(data, IT87_REG_FAN_DIV);
756 /* Save fan min limit */
757 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
762 data->fan_div[nr] = DIV_TO_REG(val);
766 data->fan_div[nr] = 1;
768 data->fan_div[nr] = 3;
771 val |= (data->fan_div[0] & 0x07);
772 val |= (data->fan_div[1] & 0x07) << 3;
773 if (data->fan_div[2] == 3)
775 it87_write_value(data, IT87_REG_FAN_DIV, val);
777 /* Restore fan min limit */
778 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
779 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
781 mutex_unlock(&data->update_lock);
785 /* Returns 0 if OK, -EINVAL otherwise */
786 static int check_trip_points(struct device *dev, int nr)
788 const struct it87_data *data = dev_get_drvdata(dev);
791 if (has_old_autopwm(data)) {
792 for (i = 0; i < 3; i++) {
793 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
796 for (i = 0; i < 2; i++) {
797 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
803 dev_err(dev, "Inconsistent trip points, not switching to "
805 dev_err(dev, "Adjust the trip points and try again\n");
810 static ssize_t set_pwm_enable(struct device *dev,
811 struct device_attribute *attr, const char *buf, size_t count)
813 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
814 int nr = sensor_attr->index;
816 struct it87_data *data = dev_get_drvdata(dev);
819 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
822 /* Check trip points before switching to automatic mode */
824 if (check_trip_points(dev, nr) < 0)
828 mutex_lock(&data->update_lock);
832 /* make sure the fan is on when in on/off mode */
833 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
834 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
835 /* set on/off mode */
836 data->fan_main_ctrl &= ~(1 << nr);
837 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
838 data->fan_main_ctrl);
840 if (val == 1) /* Manual mode */
841 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
842 data->pwm_temp_map[nr] :
844 else /* Automatic mode */
845 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
846 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
847 /* set SmartGuardian mode */
848 data->fan_main_ctrl |= (1 << nr);
849 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
850 data->fan_main_ctrl);
853 mutex_unlock(&data->update_lock);
856 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
857 const char *buf, size_t count)
859 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
860 int nr = sensor_attr->index;
862 struct it87_data *data = dev_get_drvdata(dev);
865 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
868 mutex_lock(&data->update_lock);
869 if (has_newer_autopwm(data)) {
871 * If we are in automatic mode, the PWM duty cycle register
872 * is read-only so we can't write the value.
874 if (data->pwm_ctrl[nr] & 0x80) {
875 mutex_unlock(&data->update_lock);
878 data->pwm_duty[nr] = pwm_to_reg(data, val);
879 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
882 data->pwm_duty[nr] = pwm_to_reg(data, val);
884 * If we are in manual mode, write the duty cycle immediately;
885 * otherwise, just store it for later use.
887 if (!(data->pwm_ctrl[nr] & 0x80)) {
888 data->pwm_ctrl[nr] = data->pwm_duty[nr];
889 it87_write_value(data, IT87_REG_PWM(nr),
893 mutex_unlock(&data->update_lock);
896 static ssize_t set_pwm_freq(struct device *dev,
897 struct device_attribute *attr, const char *buf, size_t count)
899 struct it87_data *data = dev_get_drvdata(dev);
903 if (kstrtoul(buf, 10, &val) < 0)
906 /* Search for the nearest available frequency */
907 for (i = 0; i < 7; i++) {
908 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
912 mutex_lock(&data->update_lock);
913 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
914 data->fan_ctl |= i << 4;
915 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
916 mutex_unlock(&data->update_lock);
920 static ssize_t show_pwm_temp_map(struct device *dev,
921 struct device_attribute *attr, char *buf)
923 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
924 int nr = sensor_attr->index;
926 struct it87_data *data = it87_update_device(dev);
929 if (data->pwm_temp_map[nr] < 3)
930 map = 1 << data->pwm_temp_map[nr];
932 map = 0; /* Should never happen */
933 return sprintf(buf, "%d\n", map);
935 static ssize_t set_pwm_temp_map(struct device *dev,
936 struct device_attribute *attr, const char *buf, size_t count)
938 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
939 int nr = sensor_attr->index;
941 struct it87_data *data = dev_get_drvdata(dev);
946 * This check can go away if we ever support automatic fan speed
947 * control on newer chips.
949 if (!has_old_autopwm(data)) {
950 dev_notice(dev, "Mapping change disabled for safety reasons\n");
954 if (kstrtol(buf, 10, &val) < 0)
971 mutex_lock(&data->update_lock);
972 data->pwm_temp_map[nr] = reg;
974 * If we are in automatic mode, write the temp mapping immediately;
975 * otherwise, just store it for later use.
977 if (data->pwm_ctrl[nr] & 0x80) {
978 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
979 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
981 mutex_unlock(&data->update_lock);
985 static ssize_t show_auto_pwm(struct device *dev,
986 struct device_attribute *attr, char *buf)
988 struct it87_data *data = it87_update_device(dev);
989 struct sensor_device_attribute_2 *sensor_attr =
990 to_sensor_dev_attr_2(attr);
991 int nr = sensor_attr->nr;
992 int point = sensor_attr->index;
994 return sprintf(buf, "%d\n",
995 pwm_from_reg(data, data->auto_pwm[nr][point]));
998 static ssize_t set_auto_pwm(struct device *dev,
999 struct device_attribute *attr, const char *buf, size_t count)
1001 struct it87_data *data = dev_get_drvdata(dev);
1002 struct sensor_device_attribute_2 *sensor_attr =
1003 to_sensor_dev_attr_2(attr);
1004 int nr = sensor_attr->nr;
1005 int point = sensor_attr->index;
1008 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1011 mutex_lock(&data->update_lock);
1012 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1013 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1014 data->auto_pwm[nr][point]);
1015 mutex_unlock(&data->update_lock);
1019 static ssize_t show_auto_temp(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1022 struct it87_data *data = it87_update_device(dev);
1023 struct sensor_device_attribute_2 *sensor_attr =
1024 to_sensor_dev_attr_2(attr);
1025 int nr = sensor_attr->nr;
1026 int point = sensor_attr->index;
1028 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1031 static ssize_t set_auto_temp(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t count)
1034 struct it87_data *data = dev_get_drvdata(dev);
1035 struct sensor_device_attribute_2 *sensor_attr =
1036 to_sensor_dev_attr_2(attr);
1037 int nr = sensor_attr->nr;
1038 int point = sensor_attr->index;
1041 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1044 mutex_lock(&data->update_lock);
1045 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1046 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1047 data->auto_temp[nr][point]);
1048 mutex_unlock(&data->update_lock);
1052 #define show_fan_offset(offset) \
1053 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1054 show_fan, NULL, offset - 1); \
1055 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1056 show_fan_min, set_fan_min, offset - 1); \
1057 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1058 show_fan_div, set_fan_div, offset - 1);
1064 #define show_pwm_offset(offset) \
1065 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1066 show_pwm_enable, set_pwm_enable, offset - 1); \
1067 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1068 show_pwm, set_pwm, offset - 1); \
1069 static DEVICE_ATTR(pwm##offset##_freq, \
1070 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1071 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1072 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1073 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1075 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1076 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1078 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1079 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1081 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1082 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1084 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1085 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1086 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1087 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1089 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1090 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1092 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1093 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1095 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1096 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1098 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1099 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1106 /* A different set of callbacks for 16-bit fans */
1107 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1110 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1111 int nr = sensor_attr->index;
1112 struct it87_data *data = it87_update_device(dev);
1113 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1116 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1119 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1120 int nr = sensor_attr->index;
1121 struct it87_data *data = it87_update_device(dev);
1122 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1125 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1126 const char *buf, size_t count)
1128 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1129 int nr = sensor_attr->index;
1130 struct it87_data *data = dev_get_drvdata(dev);
1133 if (kstrtol(buf, 10, &val) < 0)
1136 mutex_lock(&data->update_lock);
1137 data->fan_min[nr] = FAN16_TO_REG(val);
1138 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1139 data->fan_min[nr] & 0xff);
1140 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1141 data->fan_min[nr] >> 8);
1142 mutex_unlock(&data->update_lock);
1147 * We want to use the same sysfs file names as 8-bit fans, but we need
1148 * different variable names, so we have to use SENSOR_ATTR instead of
1149 * SENSOR_DEVICE_ATTR.
1151 #define show_fan16_offset(offset) \
1152 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1153 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1154 show_fan16, NULL, offset - 1); \
1155 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1156 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1157 show_fan16_min, set_fan16_min, offset - 1)
1159 show_fan16_offset(1);
1160 show_fan16_offset(2);
1161 show_fan16_offset(3);
1162 show_fan16_offset(4);
1163 show_fan16_offset(5);
1166 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1169 struct it87_data *data = it87_update_device(dev);
1170 return sprintf(buf, "%u\n", data->alarms);
1172 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1174 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1177 int bitnr = to_sensor_dev_attr(attr)->index;
1178 struct it87_data *data = it87_update_device(dev);
1179 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1182 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1183 *attr, const char *buf, size_t count)
1185 struct it87_data *data = dev_get_drvdata(dev);
1189 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1192 mutex_lock(&data->update_lock);
1193 config = it87_read_value(data, IT87_REG_CONFIG);
1198 it87_write_value(data, IT87_REG_CONFIG, config);
1199 /* Invalidate cache to force re-read */
1202 mutex_unlock(&data->update_lock);
1207 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1208 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1209 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1210 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1211 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1212 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1213 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1214 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1215 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1216 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1217 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1218 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1219 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1220 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1221 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1222 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1223 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1224 show_alarm, clear_intrusion, 4);
1226 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1229 int bitnr = to_sensor_dev_attr(attr)->index;
1230 struct it87_data *data = it87_update_device(dev);
1231 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1233 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1234 const char *buf, size_t count)
1236 int bitnr = to_sensor_dev_attr(attr)->index;
1237 struct it87_data *data = dev_get_drvdata(dev);
1240 if (kstrtol(buf, 10, &val) < 0
1241 || (val != 0 && val != 1))
1244 mutex_lock(&data->update_lock);
1245 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1247 data->beeps |= (1 << bitnr);
1249 data->beeps &= ~(1 << bitnr);
1250 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1251 mutex_unlock(&data->update_lock);
1255 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1256 show_beep, set_beep, 1);
1257 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1258 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1259 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1260 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1261 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1262 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1263 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1264 /* fanX_beep writability is set later */
1265 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1266 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1267 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1268 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1269 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1270 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1271 show_beep, set_beep, 2);
1272 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1273 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1275 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1278 struct it87_data *data = dev_get_drvdata(dev);
1279 return sprintf(buf, "%u\n", data->vrm);
1281 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1282 const char *buf, size_t count)
1284 struct it87_data *data = dev_get_drvdata(dev);
1287 if (kstrtoul(buf, 10, &val) < 0)
1294 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1296 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1299 struct it87_data *data = it87_update_device(dev);
1300 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1302 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1304 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1307 static const char * const labels[] = {
1312 static const char * const labels_it8721[] = {
1317 struct it87_data *data = dev_get_drvdata(dev);
1318 int nr = to_sensor_dev_attr(attr)->index;
1320 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1323 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1324 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1325 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1327 static ssize_t show_name(struct device *dev, struct device_attribute
1328 *devattr, char *buf)
1330 struct it87_data *data = dev_get_drvdata(dev);
1331 return sprintf(buf, "%s\n", data->name);
1333 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1335 static struct attribute *it87_attributes_in[9][5] = {
1337 &sensor_dev_attr_in0_input.dev_attr.attr,
1338 &sensor_dev_attr_in0_min.dev_attr.attr,
1339 &sensor_dev_attr_in0_max.dev_attr.attr,
1340 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1343 &sensor_dev_attr_in1_input.dev_attr.attr,
1344 &sensor_dev_attr_in1_min.dev_attr.attr,
1345 &sensor_dev_attr_in1_max.dev_attr.attr,
1346 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1349 &sensor_dev_attr_in2_input.dev_attr.attr,
1350 &sensor_dev_attr_in2_min.dev_attr.attr,
1351 &sensor_dev_attr_in2_max.dev_attr.attr,
1352 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1355 &sensor_dev_attr_in3_input.dev_attr.attr,
1356 &sensor_dev_attr_in3_min.dev_attr.attr,
1357 &sensor_dev_attr_in3_max.dev_attr.attr,
1358 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1361 &sensor_dev_attr_in4_input.dev_attr.attr,
1362 &sensor_dev_attr_in4_min.dev_attr.attr,
1363 &sensor_dev_attr_in4_max.dev_attr.attr,
1364 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1367 &sensor_dev_attr_in5_input.dev_attr.attr,
1368 &sensor_dev_attr_in5_min.dev_attr.attr,
1369 &sensor_dev_attr_in5_max.dev_attr.attr,
1370 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1373 &sensor_dev_attr_in6_input.dev_attr.attr,
1374 &sensor_dev_attr_in6_min.dev_attr.attr,
1375 &sensor_dev_attr_in6_max.dev_attr.attr,
1376 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1379 &sensor_dev_attr_in7_input.dev_attr.attr,
1380 &sensor_dev_attr_in7_min.dev_attr.attr,
1381 &sensor_dev_attr_in7_max.dev_attr.attr,
1382 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1385 &sensor_dev_attr_in8_input.dev_attr.attr,
1389 static const struct attribute_group it87_group_in[9] = {
1390 { .attrs = it87_attributes_in[0] },
1391 { .attrs = it87_attributes_in[1] },
1392 { .attrs = it87_attributes_in[2] },
1393 { .attrs = it87_attributes_in[3] },
1394 { .attrs = it87_attributes_in[4] },
1395 { .attrs = it87_attributes_in[5] },
1396 { .attrs = it87_attributes_in[6] },
1397 { .attrs = it87_attributes_in[7] },
1398 { .attrs = it87_attributes_in[8] },
1401 static struct attribute *it87_attributes_temp[3][6] = {
1403 &sensor_dev_attr_temp1_input.dev_attr.attr,
1404 &sensor_dev_attr_temp1_max.dev_attr.attr,
1405 &sensor_dev_attr_temp1_min.dev_attr.attr,
1406 &sensor_dev_attr_temp1_type.dev_attr.attr,
1407 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1410 &sensor_dev_attr_temp2_input.dev_attr.attr,
1411 &sensor_dev_attr_temp2_max.dev_attr.attr,
1412 &sensor_dev_attr_temp2_min.dev_attr.attr,
1413 &sensor_dev_attr_temp2_type.dev_attr.attr,
1414 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1417 &sensor_dev_attr_temp3_input.dev_attr.attr,
1418 &sensor_dev_attr_temp3_max.dev_attr.attr,
1419 &sensor_dev_attr_temp3_min.dev_attr.attr,
1420 &sensor_dev_attr_temp3_type.dev_attr.attr,
1421 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1425 static const struct attribute_group it87_group_temp[3] = {
1426 { .attrs = it87_attributes_temp[0] },
1427 { .attrs = it87_attributes_temp[1] },
1428 { .attrs = it87_attributes_temp[2] },
1431 static struct attribute *it87_attributes[] = {
1432 &dev_attr_alarms.attr,
1433 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1434 &dev_attr_name.attr,
1438 static const struct attribute_group it87_group = {
1439 .attrs = it87_attributes,
1442 static struct attribute *it87_attributes_in_beep[] = {
1443 &sensor_dev_attr_in0_beep.dev_attr.attr,
1444 &sensor_dev_attr_in1_beep.dev_attr.attr,
1445 &sensor_dev_attr_in2_beep.dev_attr.attr,
1446 &sensor_dev_attr_in3_beep.dev_attr.attr,
1447 &sensor_dev_attr_in4_beep.dev_attr.attr,
1448 &sensor_dev_attr_in5_beep.dev_attr.attr,
1449 &sensor_dev_attr_in6_beep.dev_attr.attr,
1450 &sensor_dev_attr_in7_beep.dev_attr.attr,
1454 static struct attribute *it87_attributes_temp_beep[] = {
1455 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1456 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1457 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1460 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1461 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1462 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1463 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1466 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1467 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1468 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1471 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1472 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1473 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1476 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1477 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1478 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1481 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1482 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1483 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1487 static const struct attribute_group it87_group_fan16[5] = {
1488 { .attrs = it87_attributes_fan16[0] },
1489 { .attrs = it87_attributes_fan16[1] },
1490 { .attrs = it87_attributes_fan16[2] },
1491 { .attrs = it87_attributes_fan16[3] },
1492 { .attrs = it87_attributes_fan16[4] },
1495 static struct attribute *it87_attributes_fan[3][4+1] = { {
1496 &sensor_dev_attr_fan1_input.dev_attr.attr,
1497 &sensor_dev_attr_fan1_min.dev_attr.attr,
1498 &sensor_dev_attr_fan1_div.dev_attr.attr,
1499 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1502 &sensor_dev_attr_fan2_input.dev_attr.attr,
1503 &sensor_dev_attr_fan2_min.dev_attr.attr,
1504 &sensor_dev_attr_fan2_div.dev_attr.attr,
1505 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1508 &sensor_dev_attr_fan3_input.dev_attr.attr,
1509 &sensor_dev_attr_fan3_min.dev_attr.attr,
1510 &sensor_dev_attr_fan3_div.dev_attr.attr,
1511 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1515 static const struct attribute_group it87_group_fan[3] = {
1516 { .attrs = it87_attributes_fan[0] },
1517 { .attrs = it87_attributes_fan[1] },
1518 { .attrs = it87_attributes_fan[2] },
1521 static const struct attribute_group *
1522 it87_get_fan_group(const struct it87_data *data)
1524 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1527 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1528 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1529 &sensor_dev_attr_pwm1.dev_attr.attr,
1530 &dev_attr_pwm1_freq.attr,
1531 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1534 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1535 &sensor_dev_attr_pwm2.dev_attr.attr,
1536 &dev_attr_pwm2_freq.attr,
1537 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1540 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1541 &sensor_dev_attr_pwm3.dev_attr.attr,
1542 &dev_attr_pwm3_freq.attr,
1543 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1547 static const struct attribute_group it87_group_pwm[3] = {
1548 { .attrs = it87_attributes_pwm[0] },
1549 { .attrs = it87_attributes_pwm[1] },
1550 { .attrs = it87_attributes_pwm[2] },
1553 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1554 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1555 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1556 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1557 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1558 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1559 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1560 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1561 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1562 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1565 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1566 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1567 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1568 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1569 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1570 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1571 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1572 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1573 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1576 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1577 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1578 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1579 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1580 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1581 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1582 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1583 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1584 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1588 static const struct attribute_group it87_group_autopwm[3] = {
1589 { .attrs = it87_attributes_autopwm[0] },
1590 { .attrs = it87_attributes_autopwm[1] },
1591 { .attrs = it87_attributes_autopwm[2] },
1594 static struct attribute *it87_attributes_fan_beep[] = {
1595 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1596 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1597 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1598 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1599 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1602 static struct attribute *it87_attributes_vid[] = {
1604 &dev_attr_cpu0_vid.attr,
1608 static const struct attribute_group it87_group_vid = {
1609 .attrs = it87_attributes_vid,
1612 static struct attribute *it87_attributes_label[] = {
1613 &sensor_dev_attr_in3_label.dev_attr.attr,
1614 &sensor_dev_attr_in7_label.dev_attr.attr,
1615 &sensor_dev_attr_in8_label.dev_attr.attr,
1619 static const struct attribute_group it87_group_label = {
1620 .attrs = it87_attributes_label,
1623 /* SuperIO detection - will change isa_address if a chip is found */
1624 static int __init it87_find(unsigned short *address,
1625 struct it87_sio_data *sio_data)
1629 const char *board_vendor, *board_name;
1631 err = superio_enter();
1636 chip_type = force_id ? force_id : superio_inw(DEVID);
1638 switch (chip_type) {
1640 sio_data->type = it87;
1643 sio_data->type = it8712;
1647 sio_data->type = it8716;
1650 sio_data->type = it8718;
1653 sio_data->type = it8720;
1656 sio_data->type = it8721;
1659 sio_data->type = it8728;
1662 sio_data->type = it8782;
1665 sio_data->type = it8783;
1667 case 0xffff: /* No device at all */
1670 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1674 superio_select(PME);
1675 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1676 pr_info("Device not activated, skipping\n");
1680 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1681 if (*address == 0) {
1682 pr_info("Base address not set, skipping\n");
1687 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1688 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1689 chip_type, *address, sio_data->revision);
1691 /* in8 (Vbat) is always internal */
1692 sio_data->internal = (1 << 2);
1694 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1695 if (sio_data->type == it87) {
1696 /* The IT8705F doesn't have VID pins at all */
1697 sio_data->skip_vid = 1;
1699 /* The IT8705F has a different LD number for GPIO */
1701 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1702 } else if (sio_data->type == it8783) {
1703 int reg25, reg27, reg2A, reg2C, regEF;
1705 sio_data->skip_vid = 1; /* No VID */
1707 superio_select(GPIO);
1709 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1710 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1711 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1712 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1713 regEF = superio_inb(IT87_SIO_SPI_REG);
1715 /* Check if fan3 is there or not */
1716 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1717 sio_data->skip_fan |= (1 << 2);
1718 if ((reg25 & (1 << 4))
1719 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1720 sio_data->skip_pwm |= (1 << 2);
1722 /* Check if fan2 is there or not */
1723 if (reg27 & (1 << 7))
1724 sio_data->skip_fan |= (1 << 1);
1725 if (reg27 & (1 << 3))
1726 sio_data->skip_pwm |= (1 << 1);
1729 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1730 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1733 if (reg27 & (1 << 1))
1734 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1738 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1740 if (reg27 & (1 << 2)) {
1742 * The data sheet is a bit unclear regarding the
1743 * internal voltage divider for VCCH5V. It says
1744 * "This bit enables and switches VIN7 (pin 91) to the
1745 * internal voltage divider for VCCH5V".
1746 * This is different to other chips, where the internal
1747 * voltage divider would connect VIN7 to an internal
1748 * voltage source. Maybe that is the case here as well.
1750 * Since we don't know for sure, re-route it if that is
1751 * not the case, and ask the user to report if the
1752 * resulting voltage is sane.
1754 if (!(reg2C & (1 << 1))) {
1756 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1757 pr_notice("Routing internal VCCH5V to in7.\n");
1759 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1760 pr_notice("Please report if it displays a reasonable voltage.\n");
1763 if (reg2C & (1 << 0))
1764 sio_data->internal |= (1 << 0);
1765 if (reg2C & (1 << 1))
1766 sio_data->internal |= (1 << 1);
1768 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1774 superio_select(GPIO);
1776 reg = superio_inb(IT87_SIO_GPIO3_REG);
1777 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1778 sio_data->type == it8782) {
1780 * IT8721F/IT8758E, and IT8782F don't have VID pins
1781 * at all, not sure about the IT8728F.
1783 sio_data->skip_vid = 1;
1785 /* We need at least 4 VID pins */
1787 pr_info("VID is disabled (pins used for GPIO)\n");
1788 sio_data->skip_vid = 1;
1792 /* Check if fan3 is there or not */
1794 sio_data->skip_pwm |= (1 << 2);
1796 sio_data->skip_fan |= (1 << 2);
1798 /* Check if fan2 is there or not */
1799 reg = superio_inb(IT87_SIO_GPIO5_REG);
1801 sio_data->skip_pwm |= (1 << 1);
1803 sio_data->skip_fan |= (1 << 1);
1805 if ((sio_data->type == it8718 || sio_data->type == it8720)
1806 && !(sio_data->skip_vid))
1807 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1809 reg = superio_inb(IT87_SIO_PINX2_REG);
1811 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1814 * The IT8720F has no VIN7 pin, so VCCH should always be
1815 * routed internally to VIN7 with an internal divider.
1816 * Curiously, there still is a configuration bit to control
1817 * this, which means it can be set incorrectly. And even
1818 * more curiously, many boards out there are improperly
1819 * configured, even though the IT8720F datasheet claims
1820 * that the internal routing of VCCH to VIN7 is the default
1821 * setting. So we force the internal routing in this case.
1823 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1824 * If UART6 is enabled, re-route VIN7 to the internal divider
1825 * if that is not already the case.
1827 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
1829 superio_outb(IT87_SIO_PINX2_REG, reg);
1830 pr_notice("Routing internal VCCH to in7\n");
1833 sio_data->internal |= (1 << 0);
1834 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1835 sio_data->type == it8728)
1836 sio_data->internal |= (1 << 1);
1839 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1840 * While VIN7 can be routed to the internal voltage divider,
1841 * VIN5 and VIN6 are not available if UART6 is enabled.
1843 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1844 * is the temperature source. Since we can not read the
1845 * temperature source here, skip_temp is preliminary.
1848 sio_data->skip_in |= (1 << 5) | (1 << 6);
1849 sio_data->skip_temp |= (1 << 2);
1852 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1854 if (sio_data->beep_pin)
1855 pr_info("Beeping is supported\n");
1857 /* Disable specific features based on DMI strings */
1858 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1859 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1860 if (board_vendor && board_name) {
1861 if (strcmp(board_vendor, "nVIDIA") == 0
1862 && strcmp(board_name, "FN68PT") == 0) {
1864 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1865 * connected to a fan, but to something else. One user
1866 * has reported instant system power-off when changing
1867 * the PWM2 duty cycle, so we disable it.
1868 * I use the board name string as the trigger in case
1869 * the same board is ever used in other systems.
1871 pr_info("Disabling pwm2 due to hardware constraints\n");
1872 sio_data->skip_pwm = (1 << 1);
1881 static void it87_remove_files(struct device *dev)
1883 struct it87_data *data = platform_get_drvdata(pdev);
1884 struct it87_sio_data *sio_data = dev->platform_data;
1885 const struct attribute_group *fan_group = it87_get_fan_group(data);
1888 sysfs_remove_group(&dev->kobj, &it87_group);
1889 for (i = 0; i < 9; i++) {
1890 if (sio_data->skip_in & (1 << i))
1892 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1893 if (it87_attributes_in_beep[i])
1894 sysfs_remove_file(&dev->kobj,
1895 it87_attributes_in_beep[i]);
1897 for (i = 0; i < 3; i++) {
1898 if (!(data->has_temp & (1 << i)))
1900 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1901 if (sio_data->beep_pin)
1902 sysfs_remove_file(&dev->kobj,
1903 it87_attributes_temp_beep[i]);
1905 for (i = 0; i < 5; i++) {
1906 if (!(data->has_fan & (1 << i)))
1908 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1909 if (sio_data->beep_pin)
1910 sysfs_remove_file(&dev->kobj,
1911 it87_attributes_fan_beep[i]);
1913 for (i = 0; i < 3; i++) {
1914 if (sio_data->skip_pwm & (1 << 0))
1916 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1917 if (has_old_autopwm(data))
1918 sysfs_remove_group(&dev->kobj,
1919 &it87_group_autopwm[i]);
1921 if (!sio_data->skip_vid)
1922 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1923 sysfs_remove_group(&dev->kobj, &it87_group_label);
1926 static int it87_probe(struct platform_device *pdev)
1928 struct it87_data *data;
1929 struct resource *res;
1930 struct device *dev = &pdev->dev;
1931 struct it87_sio_data *sio_data = dev->platform_data;
1932 const struct attribute_group *fan_group;
1934 int enable_pwm_interface;
1935 int fan_beep_need_rw;
1936 static const char * const names[] = {
1948 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1949 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1951 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1952 (unsigned long)res->start,
1953 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1957 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
1961 data->addr = res->start;
1962 data->type = sio_data->type;
1963 data->revision = sio_data->revision;
1964 data->name = names[sio_data->type];
1966 /* Now, we do the remaining detection. */
1967 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1968 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
1971 platform_set_drvdata(pdev, data);
1973 mutex_init(&data->update_lock);
1975 /* Check PWM configuration */
1976 enable_pwm_interface = it87_check_pwm(dev);
1978 /* Starting with IT8721F, we handle scaling of internal voltages */
1979 if (has_12mv_adc(data)) {
1980 if (sio_data->internal & (1 << 0))
1981 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1982 if (sio_data->internal & (1 << 1))
1983 data->in_scaled |= (1 << 7); /* in7 is VSB */
1984 if (sio_data->internal & (1 << 2))
1985 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1986 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
1987 if (sio_data->internal & (1 << 0))
1988 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1989 if (sio_data->internal & (1 << 1))
1990 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
1993 data->has_temp = 0x07;
1994 if (sio_data->skip_temp & (1 << 2)) {
1995 if (sio_data->type == it8782
1996 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
1997 data->has_temp &= ~(1 << 2);
2000 /* Initialize the IT87 chip */
2001 it87_init_device(pdev);
2003 /* Register sysfs hooks */
2004 err = sysfs_create_group(&dev->kobj, &it87_group);
2008 for (i = 0; i < 9; i++) {
2009 if (sio_data->skip_in & (1 << i))
2011 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2014 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2015 err = sysfs_create_file(&dev->kobj,
2016 it87_attributes_in_beep[i]);
2022 for (i = 0; i < 3; i++) {
2023 if (!(data->has_temp & (1 << i)))
2025 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2028 if (sio_data->beep_pin) {
2029 err = sysfs_create_file(&dev->kobj,
2030 it87_attributes_temp_beep[i]);
2036 /* Do not create fan files for disabled fans */
2037 fan_group = it87_get_fan_group(data);
2038 fan_beep_need_rw = 1;
2039 for (i = 0; i < 5; i++) {
2040 if (!(data->has_fan & (1 << i)))
2042 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2046 if (sio_data->beep_pin) {
2047 err = sysfs_create_file(&dev->kobj,
2048 it87_attributes_fan_beep[i]);
2051 if (!fan_beep_need_rw)
2055 * As we have a single beep enable bit for all fans,
2056 * only the first enabled fan has a writable attribute
2059 if (sysfs_chmod_file(&dev->kobj,
2060 it87_attributes_fan_beep[i],
2062 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2064 fan_beep_need_rw = 0;
2068 if (enable_pwm_interface) {
2069 for (i = 0; i < 3; i++) {
2070 if (sio_data->skip_pwm & (1 << i))
2072 err = sysfs_create_group(&dev->kobj,
2073 &it87_group_pwm[i]);
2077 if (!has_old_autopwm(data))
2079 err = sysfs_create_group(&dev->kobj,
2080 &it87_group_autopwm[i]);
2086 if (!sio_data->skip_vid) {
2087 data->vrm = vid_which_vrm();
2088 /* VID reading from Super-I/O config space if available */
2089 data->vid = sio_data->vid_value;
2090 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2095 /* Export labels for internal sensors */
2096 for (i = 0; i < 3; i++) {
2097 if (!(sio_data->internal & (1 << i)))
2099 err = sysfs_create_file(&dev->kobj,
2100 it87_attributes_label[i]);
2105 data->hwmon_dev = hwmon_device_register(dev);
2106 if (IS_ERR(data->hwmon_dev)) {
2107 err = PTR_ERR(data->hwmon_dev);
2114 it87_remove_files(dev);
2118 static int it87_remove(struct platform_device *pdev)
2120 struct it87_data *data = platform_get_drvdata(pdev);
2122 hwmon_device_unregister(data->hwmon_dev);
2123 it87_remove_files(&pdev->dev);
2129 * Must be called with data->update_lock held, except during initialization.
2130 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2131 * would slow down the IT87 access and should not be necessary.
2133 static int it87_read_value(struct it87_data *data, u8 reg)
2135 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2136 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2140 * Must be called with data->update_lock held, except during initialization.
2141 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2142 * would slow down the IT87 access and should not be necessary.
2144 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2146 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2147 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2150 /* Return 1 if and only if the PWM interface is safe to use */
2151 static int it87_check_pwm(struct device *dev)
2153 struct it87_data *data = dev_get_drvdata(dev);
2155 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2156 * and polarity set to active low is sign that this is the case so we
2157 * disable pwm control to protect the user.
2159 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2160 if ((tmp & 0x87) == 0) {
2161 if (fix_pwm_polarity) {
2163 * The user asks us to attempt a chip reconfiguration.
2164 * This means switching to active high polarity and
2165 * inverting all fan speed values.
2170 for (i = 0; i < 3; i++)
2171 pwm[i] = it87_read_value(data,
2175 * If any fan is in automatic pwm mode, the polarity
2176 * might be correct, as suspicious as it seems, so we
2177 * better don't change anything (but still disable the
2180 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2181 dev_info(dev, "Reconfiguring PWM to "
2182 "active high polarity\n");
2183 it87_write_value(data, IT87_REG_FAN_CTL,
2185 for (i = 0; i < 3; i++)
2186 it87_write_value(data,
2192 dev_info(dev, "PWM configuration is "
2193 "too broken to be fixed\n");
2196 dev_info(dev, "Detected broken BIOS "
2197 "defaults, disabling PWM interface\n");
2199 } else if (fix_pwm_polarity) {
2200 dev_info(dev, "PWM configuration looks "
2201 "sane, won't touch\n");
2207 /* Called when we have found a new IT87. */
2208 static void it87_init_device(struct platform_device *pdev)
2210 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2211 struct it87_data *data = platform_get_drvdata(pdev);
2216 * For each PWM channel:
2217 * - If it is in automatic mode, setting to manual mode should set
2218 * the fan to full speed by default.
2219 * - If it is in manual mode, we need a mapping to temperature
2220 * channels to use when later setting to automatic mode later.
2221 * Use a 1:1 mapping by default (we are clueless.)
2222 * In both cases, the value can (and should) be changed by the user
2223 * prior to switching to a different mode.
2224 * Note that this is no longer needed for the IT8721F and later, as
2225 * these have separate registers for the temperature mapping and the
2226 * manual duty cycle.
2228 for (i = 0; i < 3; i++) {
2229 data->pwm_temp_map[i] = i;
2230 data->pwm_duty[i] = 0x7f; /* Full speed */
2231 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2235 * Some chips seem to have default value 0xff for all limit
2236 * registers. For low voltage limits it makes no sense and triggers
2237 * alarms, so change to 0 instead. For high temperature limits, it
2238 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2239 * but is still confusing, so change to 127 degrees C.
2241 for (i = 0; i < 8; i++) {
2242 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2244 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2246 for (i = 0; i < 3; i++) {
2247 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2249 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2253 * Temperature channels are not forcibly enabled, as they can be
2254 * set to two different sensor types and we can't guess which one
2255 * is correct for a given system. These channels can be enabled at
2256 * run-time through the temp{1-3}_type sysfs accessors if needed.
2259 /* Check if voltage monitors are reset manually or by some reason */
2260 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2261 if ((tmp & 0xff) == 0) {
2262 /* Enable all voltage monitors */
2263 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2266 /* Check if tachometers are reset manually or by some reason */
2267 mask = 0x70 & ~(sio_data->skip_fan << 4);
2268 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2269 if ((data->fan_main_ctrl & mask) == 0) {
2270 /* Enable all fan tachometers */
2271 data->fan_main_ctrl |= mask;
2272 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2273 data->fan_main_ctrl);
2275 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2277 /* Set tachometers to 16-bit mode if needed */
2278 if (has_16bit_fans(data)) {
2279 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2280 if (~tmp & 0x07 & data->has_fan) {
2282 "Setting fan1-3 to 16-bit mode\n");
2283 it87_write_value(data, IT87_REG_FAN_16BIT,
2286 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2287 if (data->type != it87 && data->type != it8782 &&
2288 data->type != it8783) {
2290 data->has_fan |= (1 << 3); /* fan4 enabled */
2292 data->has_fan |= (1 << 4); /* fan5 enabled */
2296 /* Fan input pins may be used for alternative functions */
2297 data->has_fan &= ~sio_data->skip_fan;
2299 /* Start monitoring */
2300 it87_write_value(data, IT87_REG_CONFIG,
2301 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2302 | (update_vbat ? 0x41 : 0x01));
2305 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2307 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2308 if (has_newer_autopwm(data)) {
2309 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2310 data->pwm_duty[nr] = it87_read_value(data,
2311 IT87_REG_PWM_DUTY(nr));
2313 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2314 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2315 else /* Manual mode */
2316 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2319 if (has_old_autopwm(data)) {
2322 for (i = 0; i < 5 ; i++)
2323 data->auto_temp[nr][i] = it87_read_value(data,
2324 IT87_REG_AUTO_TEMP(nr, i));
2325 for (i = 0; i < 3 ; i++)
2326 data->auto_pwm[nr][i] = it87_read_value(data,
2327 IT87_REG_AUTO_PWM(nr, i));
2331 static struct it87_data *it87_update_device(struct device *dev)
2333 struct it87_data *data = dev_get_drvdata(dev);
2336 mutex_lock(&data->update_lock);
2338 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2342 * Cleared after each update, so reenable. Value
2343 * returned by this read will be previous value
2345 it87_write_value(data, IT87_REG_CONFIG,
2346 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2348 for (i = 0; i <= 7; i++) {
2350 it87_read_value(data, IT87_REG_VIN(i));
2352 it87_read_value(data, IT87_REG_VIN_MIN(i));
2354 it87_read_value(data, IT87_REG_VIN_MAX(i));
2356 /* in8 (battery) has no limit registers */
2357 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2359 for (i = 0; i < 5; i++) {
2360 /* Skip disabled fans */
2361 if (!(data->has_fan & (1 << i)))
2365 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2366 data->fan[i] = it87_read_value(data,
2368 /* Add high byte if in 16-bit mode */
2369 if (has_16bit_fans(data)) {
2370 data->fan[i] |= it87_read_value(data,
2371 IT87_REG_FANX[i]) << 8;
2372 data->fan_min[i] |= it87_read_value(data,
2373 IT87_REG_FANX_MIN[i]) << 8;
2376 for (i = 0; i < 3; i++) {
2377 if (!(data->has_temp & (1 << i)))
2380 it87_read_value(data, IT87_REG_TEMP(i));
2382 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2384 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2387 /* Newer chips don't have clock dividers */
2388 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2389 i = it87_read_value(data, IT87_REG_FAN_DIV);
2390 data->fan_div[0] = i & 0x07;
2391 data->fan_div[1] = (i >> 3) & 0x07;
2392 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2396 it87_read_value(data, IT87_REG_ALARM1) |
2397 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2398 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2399 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2401 data->fan_main_ctrl = it87_read_value(data,
2402 IT87_REG_FAN_MAIN_CTRL);
2403 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2404 for (i = 0; i < 3; i++)
2405 it87_update_pwm_ctrl(data, i);
2407 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2409 * The IT8705F does not have VID capability.
2410 * The IT8718F and later don't use IT87_REG_VID for the
2413 if (data->type == it8712 || data->type == it8716) {
2414 data->vid = it87_read_value(data, IT87_REG_VID);
2416 * The older IT8712F revisions had only 5 VID pins,
2417 * but we assume it is always safe to read 6 bits.
2421 data->last_updated = jiffies;
2425 mutex_unlock(&data->update_lock);
2430 static int __init it87_device_add(unsigned short address,
2431 const struct it87_sio_data *sio_data)
2433 struct resource res = {
2434 .start = address + IT87_EC_OFFSET,
2435 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2437 .flags = IORESOURCE_IO,
2441 err = acpi_check_resource_conflict(&res);
2445 pdev = platform_device_alloc(DRVNAME, address);
2448 pr_err("Device allocation failed\n");
2452 err = platform_device_add_resources(pdev, &res, 1);
2454 pr_err("Device resource addition failed (%d)\n", err);
2455 goto exit_device_put;
2458 err = platform_device_add_data(pdev, sio_data,
2459 sizeof(struct it87_sio_data));
2461 pr_err("Platform data allocation failed\n");
2462 goto exit_device_put;
2465 err = platform_device_add(pdev);
2467 pr_err("Device addition failed (%d)\n", err);
2468 goto exit_device_put;
2474 platform_device_put(pdev);
2479 static int __init sm_it87_init(void)
2482 unsigned short isa_address = 0;
2483 struct it87_sio_data sio_data;
2485 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2486 err = it87_find(&isa_address, &sio_data);
2489 err = platform_driver_register(&it87_driver);
2493 err = it87_device_add(isa_address, &sio_data);
2495 platform_driver_unregister(&it87_driver);
2502 static void __exit sm_it87_exit(void)
2504 platform_device_unregister(pdev);
2505 platform_driver_unregister(&it87_driver);
2509 MODULE_AUTHOR("Chris Gauthron, "
2510 "Jean Delvare <khali@linux-fr.org>");
2511 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2512 module_param(update_vbat, bool, 0);
2513 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2514 module_param(fix_pwm_polarity, bool, 0);
2515 MODULE_PARM_DESC(fix_pwm_polarity,
2516 "Force PWM polarity to active high (DANGEROUS)");
2517 MODULE_LICENSE("GPL");
2519 module_init(sm_it87_init);
2520 module_exit(sm_it87_exit);