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 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
208 #define IT87_REG_FAN_MAIN_CTRL 0x13
209 #define IT87_REG_FAN_CTL 0x14
210 #define IT87_REG_PWM(nr) (0x15 + (nr))
211 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
213 #define IT87_REG_VIN(nr) (0x20 + (nr))
214 #define IT87_REG_TEMP(nr) (0x29 + (nr))
216 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
217 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
218 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
219 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
221 #define IT87_REG_VIN_ENABLE 0x50
222 #define IT87_REG_TEMP_ENABLE 0x51
223 #define IT87_REG_TEMP_EXTRA 0x55
224 #define IT87_REG_BEEP_ENABLE 0x5c
226 #define IT87_REG_CHIPID 0x58
228 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
229 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
232 struct it87_sio_data {
234 /* Values read from Super-I/O config space */
238 u8 internal; /* Internal sensors can be labeled */
239 /* Features skipped based on config or DMI */
248 * For each registered chip, we need to keep some data in memory.
249 * The structure is dynamically allocated.
252 struct device *hwmon_dev;
258 struct mutex update_lock;
259 char valid; /* !=0 if following fields are valid */
260 unsigned long last_updated; /* In jiffies */
262 u16 in_scaled; /* Internal voltage sensors are scaled */
263 u8 in[9][3]; /* [nr][0]=in, [1]=min, [2]=max */
264 u8 has_fan; /* Bitfield, fans enabled */
265 u16 fan[5]; /* Register values, possibly combined */
266 u16 fan_min[5]; /* Register values, possibly combined */
267 u8 has_temp; /* Bitfield, temp sensors enabled */
268 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
269 u8 sensor; /* Register value */
270 u8 fan_div[3]; /* Register encoding, shifted right */
271 u8 vid; /* Register encoding, combined */
273 u32 alarms; /* Register encoding, combined */
274 u8 beeps; /* Register encoding */
275 u8 fan_main_ctrl; /* Register value */
276 u8 fan_ctl; /* Register value */
279 * The following 3 arrays correspond to the same registers up to
280 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
281 * 7, and we want to preserve settings on mode changes, so we have
282 * to track all values separately.
283 * Starting with the IT8721F, the manual PWM duty cycles are stored
284 * in separate registers (8-bit values), so the separate tracking
285 * is no longer needed, but it is still done to keep the driver
288 u8 pwm_ctrl[3]; /* Register value */
289 u8 pwm_duty[3]; /* Manual PWM value set by user */
290 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
292 /* Automatic fan speed control registers */
293 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
294 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
297 static inline int has_12mv_adc(const struct it87_data *data)
300 * IT8721F and later have a 12 mV ADC, also with internal scaling
301 * on selected inputs.
303 return data->type == it8721
304 || data->type == it8728;
307 static inline int has_newer_autopwm(const struct it87_data *data)
310 * IT8721F and later have separate registers for the temperature
311 * mapping and the manual duty cycle.
313 return data->type == it8721
314 || data->type == it8728;
317 static inline int has_temp_offset(const struct it87_data *data)
319 return data->type == it8716
320 || data->type == it8718
321 || data->type == it8720
322 || data->type == it8721
323 || data->type == it8728
324 || data->type == it8782
325 || data->type == it8783;
328 static int adc_lsb(const struct it87_data *data, int nr)
330 int lsb = has_12mv_adc(data) ? 12 : 16;
331 if (data->in_scaled & (1 << nr))
336 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
338 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
339 return SENSORS_LIMIT(val, 0, 255);
342 static int in_from_reg(const struct it87_data *data, int nr, int val)
344 return val * adc_lsb(data, nr);
347 static inline u8 FAN_TO_REG(long rpm, int div)
351 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
352 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
356 static inline u16 FAN16_TO_REG(long rpm)
360 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
363 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
364 1350000 / ((val) * (div)))
365 /* The divider is fixed to 2 in 16-bit mode */
366 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
367 1350000 / ((val) * 2))
369 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
370 ((val) + 500) / 1000), -128, 127))
371 #define TEMP_FROM_REG(val) ((val) * 1000)
373 static u8 pwm_to_reg(const struct it87_data *data, long val)
375 if (has_newer_autopwm(data))
381 static int pwm_from_reg(const struct it87_data *data, u8 reg)
383 if (has_newer_autopwm(data))
386 return (reg & 0x7f) << 1;
390 static int DIV_TO_REG(int val)
393 while (answer < 7 && (val >>= 1))
397 #define DIV_FROM_REG(val) (1 << (val))
399 static const unsigned int pwm_freq[8] = {
410 static inline int has_16bit_fans(const struct it87_data *data)
413 * IT8705F Datasheet 0.4.1, 3h == Version G.
414 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
415 * These are the first revisions with 16-bit tachometer support.
417 return (data->type == it87 && data->revision >= 0x03)
418 || (data->type == it8712 && data->revision >= 0x08)
419 || data->type == it8716
420 || data->type == it8718
421 || data->type == it8720
422 || data->type == it8721
423 || data->type == it8728
424 || data->type == it8782
425 || data->type == it8783;
428 static inline int has_old_autopwm(const struct it87_data *data)
431 * The old automatic fan speed control interface is implemented
432 * by IT8705F chips up to revision F and IT8712F chips up to
435 return (data->type == it87 && data->revision < 0x03)
436 || (data->type == it8712 && data->revision < 0x08);
439 static int it87_probe(struct platform_device *pdev);
440 static int it87_remove(struct platform_device *pdev);
442 static int it87_read_value(struct it87_data *data, u8 reg);
443 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
444 static struct it87_data *it87_update_device(struct device *dev);
445 static int it87_check_pwm(struct device *dev);
446 static void it87_init_device(struct platform_device *pdev);
449 static struct platform_driver it87_driver = {
451 .owner = THIS_MODULE,
455 .remove = it87_remove,
458 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
461 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
463 int index = sattr->index;
465 struct it87_data *data = it87_update_device(dev);
466 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
469 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
470 const char *buf, size_t count)
472 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
474 int index = sattr->index;
476 struct it87_data *data = dev_get_drvdata(dev);
479 if (kstrtoul(buf, 10, &val) < 0)
482 mutex_lock(&data->update_lock);
483 data->in[nr][index] = in_to_reg(data, nr, val);
484 it87_write_value(data,
485 index == 1 ? IT87_REG_VIN_MIN(nr)
486 : IT87_REG_VIN_MAX(nr),
487 data->in[nr][index]);
488 mutex_unlock(&data->update_lock);
492 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
493 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
495 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
498 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
499 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
501 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
504 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
505 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
507 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
510 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
511 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
513 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
516 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
517 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
519 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
522 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
523 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
525 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
528 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
529 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
531 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
534 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
535 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
537 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
540 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
543 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
546 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
548 int index = sattr->index;
549 struct it87_data *data = it87_update_device(dev);
551 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
554 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
555 const char *buf, size_t count)
557 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
559 int index = sattr->index;
560 struct it87_data *data = dev_get_drvdata(dev);
564 if (kstrtol(buf, 10, &val) < 0)
567 mutex_lock(&data->update_lock);
572 reg = IT87_REG_TEMP_LOW(nr);
575 reg = IT87_REG_TEMP_HIGH(nr);
578 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
579 if (!(regval & 0x80)) {
581 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
584 reg = IT87_REG_TEMP_OFFSET[nr];
588 data->temp[nr][index] = TEMP_TO_REG(val);
589 it87_write_value(data, reg, data->temp[nr][index]);
590 mutex_unlock(&data->update_lock);
594 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
595 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
597 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
599 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
601 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
602 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
604 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
606 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
608 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
609 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
611 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
613 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
616 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
619 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
620 int nr = sensor_attr->index;
621 struct it87_data *data = it87_update_device(dev);
622 u8 reg = data->sensor; /* In case value is updated while used */
625 return sprintf(buf, "3\n"); /* thermal diode */
627 return sprintf(buf, "4\n"); /* thermistor */
628 return sprintf(buf, "0\n"); /* disabled */
631 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
632 const char *buf, size_t count)
634 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
635 int nr = sensor_attr->index;
637 struct it87_data *data = dev_get_drvdata(dev);
641 if (kstrtol(buf, 10, &val) < 0)
644 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
647 if (val == 2) { /* backwards compatibility */
648 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
652 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
660 mutex_lock(&data->update_lock);
662 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
663 data->valid = 0; /* Force cache refresh */
664 mutex_unlock(&data->update_lock);
668 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
670 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
672 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
677 static int pwm_mode(const struct it87_data *data, int nr)
679 int ctrl = data->fan_main_ctrl & (1 << nr);
681 if (ctrl == 0) /* Full speed */
683 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
685 else /* Manual mode */
689 static ssize_t show_fan(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", FAN_FROM_REG(data->fan[nr],
697 DIV_FROM_REG(data->fan_div[nr])));
699 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
702 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
703 int nr = sensor_attr->index;
705 struct it87_data *data = it87_update_device(dev);
706 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
707 DIV_FROM_REG(data->fan_div[nr])));
709 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
712 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
713 int nr = sensor_attr->index;
715 struct it87_data *data = it87_update_device(dev);
716 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
718 static ssize_t show_pwm_enable(struct device *dev,
719 struct device_attribute *attr, char *buf)
721 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
722 int nr = sensor_attr->index;
724 struct it87_data *data = it87_update_device(dev);
725 return sprintf(buf, "%d\n", pwm_mode(data, nr));
727 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
730 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
731 int nr = sensor_attr->index;
733 struct it87_data *data = it87_update_device(dev);
734 return sprintf(buf, "%d\n",
735 pwm_from_reg(data, data->pwm_duty[nr]));
737 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
740 struct it87_data *data = it87_update_device(dev);
741 int index = (data->fan_ctl >> 4) & 0x07;
743 return sprintf(buf, "%u\n", pwm_freq[index]);
745 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
746 const char *buf, size_t count)
748 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
749 int nr = sensor_attr->index;
751 struct it87_data *data = dev_get_drvdata(dev);
755 if (kstrtol(buf, 10, &val) < 0)
758 mutex_lock(&data->update_lock);
759 reg = it87_read_value(data, IT87_REG_FAN_DIV);
762 data->fan_div[nr] = reg & 0x07;
765 data->fan_div[nr] = (reg >> 3) & 0x07;
768 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
772 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
773 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
774 mutex_unlock(&data->update_lock);
777 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
780 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
781 int nr = sensor_attr->index;
783 struct it87_data *data = dev_get_drvdata(dev);
788 if (kstrtoul(buf, 10, &val) < 0)
791 mutex_lock(&data->update_lock);
792 old = it87_read_value(data, IT87_REG_FAN_DIV);
794 /* Save fan min limit */
795 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
800 data->fan_div[nr] = DIV_TO_REG(val);
804 data->fan_div[nr] = 1;
806 data->fan_div[nr] = 3;
809 val |= (data->fan_div[0] & 0x07);
810 val |= (data->fan_div[1] & 0x07) << 3;
811 if (data->fan_div[2] == 3)
813 it87_write_value(data, IT87_REG_FAN_DIV, val);
815 /* Restore fan min limit */
816 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
817 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
819 mutex_unlock(&data->update_lock);
823 /* Returns 0 if OK, -EINVAL otherwise */
824 static int check_trip_points(struct device *dev, int nr)
826 const struct it87_data *data = dev_get_drvdata(dev);
829 if (has_old_autopwm(data)) {
830 for (i = 0; i < 3; i++) {
831 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
834 for (i = 0; i < 2; i++) {
835 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
841 dev_err(dev, "Inconsistent trip points, not switching to "
843 dev_err(dev, "Adjust the trip points and try again\n");
848 static ssize_t set_pwm_enable(struct device *dev,
849 struct device_attribute *attr, const char *buf, size_t count)
851 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
852 int nr = sensor_attr->index;
854 struct it87_data *data = dev_get_drvdata(dev);
857 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
860 /* Check trip points before switching to automatic mode */
862 if (check_trip_points(dev, nr) < 0)
866 mutex_lock(&data->update_lock);
870 /* make sure the fan is on when in on/off mode */
871 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
872 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
873 /* set on/off mode */
874 data->fan_main_ctrl &= ~(1 << nr);
875 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
876 data->fan_main_ctrl);
878 if (val == 1) /* Manual mode */
879 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
880 data->pwm_temp_map[nr] :
882 else /* Automatic mode */
883 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
884 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
885 /* set SmartGuardian mode */
886 data->fan_main_ctrl |= (1 << nr);
887 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
888 data->fan_main_ctrl);
891 mutex_unlock(&data->update_lock);
894 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
895 const char *buf, size_t count)
897 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
898 int nr = sensor_attr->index;
900 struct it87_data *data = dev_get_drvdata(dev);
903 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
906 mutex_lock(&data->update_lock);
907 if (has_newer_autopwm(data)) {
909 * If we are in automatic mode, the PWM duty cycle register
910 * is read-only so we can't write the value.
912 if (data->pwm_ctrl[nr] & 0x80) {
913 mutex_unlock(&data->update_lock);
916 data->pwm_duty[nr] = pwm_to_reg(data, val);
917 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
920 data->pwm_duty[nr] = pwm_to_reg(data, val);
922 * If we are in manual mode, write the duty cycle immediately;
923 * otherwise, just store it for later use.
925 if (!(data->pwm_ctrl[nr] & 0x80)) {
926 data->pwm_ctrl[nr] = data->pwm_duty[nr];
927 it87_write_value(data, IT87_REG_PWM(nr),
931 mutex_unlock(&data->update_lock);
934 static ssize_t set_pwm_freq(struct device *dev,
935 struct device_attribute *attr, const char *buf, size_t count)
937 struct it87_data *data = dev_get_drvdata(dev);
941 if (kstrtoul(buf, 10, &val) < 0)
944 /* Search for the nearest available frequency */
945 for (i = 0; i < 7; i++) {
946 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
950 mutex_lock(&data->update_lock);
951 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
952 data->fan_ctl |= i << 4;
953 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
954 mutex_unlock(&data->update_lock);
958 static ssize_t show_pwm_temp_map(struct device *dev,
959 struct device_attribute *attr, char *buf)
961 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
962 int nr = sensor_attr->index;
964 struct it87_data *data = it87_update_device(dev);
967 if (data->pwm_temp_map[nr] < 3)
968 map = 1 << data->pwm_temp_map[nr];
970 map = 0; /* Should never happen */
971 return sprintf(buf, "%d\n", map);
973 static ssize_t set_pwm_temp_map(struct device *dev,
974 struct device_attribute *attr, const char *buf, size_t count)
976 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
977 int nr = sensor_attr->index;
979 struct it87_data *data = dev_get_drvdata(dev);
984 * This check can go away if we ever support automatic fan speed
985 * control on newer chips.
987 if (!has_old_autopwm(data)) {
988 dev_notice(dev, "Mapping change disabled for safety reasons\n");
992 if (kstrtol(buf, 10, &val) < 0)
1009 mutex_lock(&data->update_lock);
1010 data->pwm_temp_map[nr] = reg;
1012 * If we are in automatic mode, write the temp mapping immediately;
1013 * otherwise, just store it for later use.
1015 if (data->pwm_ctrl[nr] & 0x80) {
1016 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1017 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1019 mutex_unlock(&data->update_lock);
1023 static ssize_t show_auto_pwm(struct device *dev,
1024 struct device_attribute *attr, char *buf)
1026 struct it87_data *data = it87_update_device(dev);
1027 struct sensor_device_attribute_2 *sensor_attr =
1028 to_sensor_dev_attr_2(attr);
1029 int nr = sensor_attr->nr;
1030 int point = sensor_attr->index;
1032 return sprintf(buf, "%d\n",
1033 pwm_from_reg(data, data->auto_pwm[nr][point]));
1036 static ssize_t set_auto_pwm(struct device *dev,
1037 struct device_attribute *attr, const char *buf, size_t count)
1039 struct it87_data *data = dev_get_drvdata(dev);
1040 struct sensor_device_attribute_2 *sensor_attr =
1041 to_sensor_dev_attr_2(attr);
1042 int nr = sensor_attr->nr;
1043 int point = sensor_attr->index;
1046 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1049 mutex_lock(&data->update_lock);
1050 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1051 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1052 data->auto_pwm[nr][point]);
1053 mutex_unlock(&data->update_lock);
1057 static ssize_t show_auto_temp(struct device *dev,
1058 struct device_attribute *attr, char *buf)
1060 struct it87_data *data = it87_update_device(dev);
1061 struct sensor_device_attribute_2 *sensor_attr =
1062 to_sensor_dev_attr_2(attr);
1063 int nr = sensor_attr->nr;
1064 int point = sensor_attr->index;
1066 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1069 static ssize_t set_auto_temp(struct device *dev,
1070 struct device_attribute *attr, const char *buf, size_t count)
1072 struct it87_data *data = dev_get_drvdata(dev);
1073 struct sensor_device_attribute_2 *sensor_attr =
1074 to_sensor_dev_attr_2(attr);
1075 int nr = sensor_attr->nr;
1076 int point = sensor_attr->index;
1079 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1082 mutex_lock(&data->update_lock);
1083 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1084 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1085 data->auto_temp[nr][point]);
1086 mutex_unlock(&data->update_lock);
1090 #define show_fan_offset(offset) \
1091 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1092 show_fan, NULL, offset - 1); \
1093 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1094 show_fan_min, set_fan_min, offset - 1); \
1095 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1096 show_fan_div, set_fan_div, offset - 1);
1102 #define show_pwm_offset(offset) \
1103 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1104 show_pwm_enable, set_pwm_enable, offset - 1); \
1105 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1106 show_pwm, set_pwm, offset - 1); \
1107 static DEVICE_ATTR(pwm##offset##_freq, \
1108 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1109 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1110 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1111 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1113 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1114 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1116 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1117 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1119 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1120 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1122 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1123 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1124 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1125 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1127 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1128 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1130 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1131 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1133 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1134 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1136 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1137 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1144 /* A different set of callbacks for 16-bit fans */
1145 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1148 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1149 int nr = sensor_attr->index;
1150 struct it87_data *data = it87_update_device(dev);
1151 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1154 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1157 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1158 int nr = sensor_attr->index;
1159 struct it87_data *data = it87_update_device(dev);
1160 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1163 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1164 const char *buf, size_t count)
1166 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1167 int nr = sensor_attr->index;
1168 struct it87_data *data = dev_get_drvdata(dev);
1171 if (kstrtol(buf, 10, &val) < 0)
1174 mutex_lock(&data->update_lock);
1175 data->fan_min[nr] = FAN16_TO_REG(val);
1176 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1177 data->fan_min[nr] & 0xff);
1178 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1179 data->fan_min[nr] >> 8);
1180 mutex_unlock(&data->update_lock);
1185 * We want to use the same sysfs file names as 8-bit fans, but we need
1186 * different variable names, so we have to use SENSOR_ATTR instead of
1187 * SENSOR_DEVICE_ATTR.
1189 #define show_fan16_offset(offset) \
1190 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1191 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1192 show_fan16, NULL, offset - 1); \
1193 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1194 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1195 show_fan16_min, set_fan16_min, offset - 1)
1197 show_fan16_offset(1);
1198 show_fan16_offset(2);
1199 show_fan16_offset(3);
1200 show_fan16_offset(4);
1201 show_fan16_offset(5);
1204 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1207 struct it87_data *data = it87_update_device(dev);
1208 return sprintf(buf, "%u\n", data->alarms);
1210 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1212 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1215 int bitnr = to_sensor_dev_attr(attr)->index;
1216 struct it87_data *data = it87_update_device(dev);
1217 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1220 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1221 *attr, const char *buf, size_t count)
1223 struct it87_data *data = dev_get_drvdata(dev);
1227 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1230 mutex_lock(&data->update_lock);
1231 config = it87_read_value(data, IT87_REG_CONFIG);
1236 it87_write_value(data, IT87_REG_CONFIG, config);
1237 /* Invalidate cache to force re-read */
1240 mutex_unlock(&data->update_lock);
1245 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1246 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1247 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1248 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1249 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1250 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1251 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1252 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1253 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1254 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1255 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1256 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1257 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1258 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1259 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1260 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1261 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1262 show_alarm, clear_intrusion, 4);
1264 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1267 int bitnr = to_sensor_dev_attr(attr)->index;
1268 struct it87_data *data = it87_update_device(dev);
1269 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1271 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1272 const char *buf, size_t count)
1274 int bitnr = to_sensor_dev_attr(attr)->index;
1275 struct it87_data *data = dev_get_drvdata(dev);
1278 if (kstrtol(buf, 10, &val) < 0
1279 || (val != 0 && val != 1))
1282 mutex_lock(&data->update_lock);
1283 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1285 data->beeps |= (1 << bitnr);
1287 data->beeps &= ~(1 << bitnr);
1288 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1289 mutex_unlock(&data->update_lock);
1293 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1294 show_beep, set_beep, 1);
1295 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1296 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1297 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1298 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1299 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1300 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1301 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1302 /* fanX_beep writability is set later */
1303 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1304 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1305 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1306 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1307 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1308 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1309 show_beep, set_beep, 2);
1310 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1311 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1313 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1316 struct it87_data *data = dev_get_drvdata(dev);
1317 return sprintf(buf, "%u\n", data->vrm);
1319 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1320 const char *buf, size_t count)
1322 struct it87_data *data = dev_get_drvdata(dev);
1325 if (kstrtoul(buf, 10, &val) < 0)
1332 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1334 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1337 struct it87_data *data = it87_update_device(dev);
1338 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1340 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1342 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1345 static const char * const labels[] = {
1350 static const char * const labels_it8721[] = {
1355 struct it87_data *data = dev_get_drvdata(dev);
1356 int nr = to_sensor_dev_attr(attr)->index;
1358 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1361 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1362 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1363 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1365 static ssize_t show_name(struct device *dev, struct device_attribute
1366 *devattr, char *buf)
1368 struct it87_data *data = dev_get_drvdata(dev);
1369 return sprintf(buf, "%s\n", data->name);
1371 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1373 static struct attribute *it87_attributes_in[9][5] = {
1375 &sensor_dev_attr_in0_input.dev_attr.attr,
1376 &sensor_dev_attr_in0_min.dev_attr.attr,
1377 &sensor_dev_attr_in0_max.dev_attr.attr,
1378 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1381 &sensor_dev_attr_in1_input.dev_attr.attr,
1382 &sensor_dev_attr_in1_min.dev_attr.attr,
1383 &sensor_dev_attr_in1_max.dev_attr.attr,
1384 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1387 &sensor_dev_attr_in2_input.dev_attr.attr,
1388 &sensor_dev_attr_in2_min.dev_attr.attr,
1389 &sensor_dev_attr_in2_max.dev_attr.attr,
1390 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1393 &sensor_dev_attr_in3_input.dev_attr.attr,
1394 &sensor_dev_attr_in3_min.dev_attr.attr,
1395 &sensor_dev_attr_in3_max.dev_attr.attr,
1396 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1399 &sensor_dev_attr_in4_input.dev_attr.attr,
1400 &sensor_dev_attr_in4_min.dev_attr.attr,
1401 &sensor_dev_attr_in4_max.dev_attr.attr,
1402 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1405 &sensor_dev_attr_in5_input.dev_attr.attr,
1406 &sensor_dev_attr_in5_min.dev_attr.attr,
1407 &sensor_dev_attr_in5_max.dev_attr.attr,
1408 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1411 &sensor_dev_attr_in6_input.dev_attr.attr,
1412 &sensor_dev_attr_in6_min.dev_attr.attr,
1413 &sensor_dev_attr_in6_max.dev_attr.attr,
1414 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1417 &sensor_dev_attr_in7_input.dev_attr.attr,
1418 &sensor_dev_attr_in7_min.dev_attr.attr,
1419 &sensor_dev_attr_in7_max.dev_attr.attr,
1420 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1423 &sensor_dev_attr_in8_input.dev_attr.attr,
1427 static const struct attribute_group it87_group_in[9] = {
1428 { .attrs = it87_attributes_in[0] },
1429 { .attrs = it87_attributes_in[1] },
1430 { .attrs = it87_attributes_in[2] },
1431 { .attrs = it87_attributes_in[3] },
1432 { .attrs = it87_attributes_in[4] },
1433 { .attrs = it87_attributes_in[5] },
1434 { .attrs = it87_attributes_in[6] },
1435 { .attrs = it87_attributes_in[7] },
1436 { .attrs = it87_attributes_in[8] },
1439 static struct attribute *it87_attributes_temp[3][6] = {
1441 &sensor_dev_attr_temp1_input.dev_attr.attr,
1442 &sensor_dev_attr_temp1_max.dev_attr.attr,
1443 &sensor_dev_attr_temp1_min.dev_attr.attr,
1444 &sensor_dev_attr_temp1_type.dev_attr.attr,
1445 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1448 &sensor_dev_attr_temp2_input.dev_attr.attr,
1449 &sensor_dev_attr_temp2_max.dev_attr.attr,
1450 &sensor_dev_attr_temp2_min.dev_attr.attr,
1451 &sensor_dev_attr_temp2_type.dev_attr.attr,
1452 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1455 &sensor_dev_attr_temp3_input.dev_attr.attr,
1456 &sensor_dev_attr_temp3_max.dev_attr.attr,
1457 &sensor_dev_attr_temp3_min.dev_attr.attr,
1458 &sensor_dev_attr_temp3_type.dev_attr.attr,
1459 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1463 static const struct attribute_group it87_group_temp[3] = {
1464 { .attrs = it87_attributes_temp[0] },
1465 { .attrs = it87_attributes_temp[1] },
1466 { .attrs = it87_attributes_temp[2] },
1469 static struct attribute *it87_attributes_temp_offset[] = {
1470 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1471 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1472 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1475 static struct attribute *it87_attributes[] = {
1476 &dev_attr_alarms.attr,
1477 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1478 &dev_attr_name.attr,
1482 static const struct attribute_group it87_group = {
1483 .attrs = it87_attributes,
1486 static struct attribute *it87_attributes_in_beep[] = {
1487 &sensor_dev_attr_in0_beep.dev_attr.attr,
1488 &sensor_dev_attr_in1_beep.dev_attr.attr,
1489 &sensor_dev_attr_in2_beep.dev_attr.attr,
1490 &sensor_dev_attr_in3_beep.dev_attr.attr,
1491 &sensor_dev_attr_in4_beep.dev_attr.attr,
1492 &sensor_dev_attr_in5_beep.dev_attr.attr,
1493 &sensor_dev_attr_in6_beep.dev_attr.attr,
1494 &sensor_dev_attr_in7_beep.dev_attr.attr,
1498 static struct attribute *it87_attributes_temp_beep[] = {
1499 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1500 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1501 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1504 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1505 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1506 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1507 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1510 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1511 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1512 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1515 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1516 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1517 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1520 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1521 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1522 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1525 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1526 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1527 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1531 static const struct attribute_group it87_group_fan16[5] = {
1532 { .attrs = it87_attributes_fan16[0] },
1533 { .attrs = it87_attributes_fan16[1] },
1534 { .attrs = it87_attributes_fan16[2] },
1535 { .attrs = it87_attributes_fan16[3] },
1536 { .attrs = it87_attributes_fan16[4] },
1539 static struct attribute *it87_attributes_fan[3][4+1] = { {
1540 &sensor_dev_attr_fan1_input.dev_attr.attr,
1541 &sensor_dev_attr_fan1_min.dev_attr.attr,
1542 &sensor_dev_attr_fan1_div.dev_attr.attr,
1543 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1546 &sensor_dev_attr_fan2_input.dev_attr.attr,
1547 &sensor_dev_attr_fan2_min.dev_attr.attr,
1548 &sensor_dev_attr_fan2_div.dev_attr.attr,
1549 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1552 &sensor_dev_attr_fan3_input.dev_attr.attr,
1553 &sensor_dev_attr_fan3_min.dev_attr.attr,
1554 &sensor_dev_attr_fan3_div.dev_attr.attr,
1555 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1559 static const struct attribute_group it87_group_fan[3] = {
1560 { .attrs = it87_attributes_fan[0] },
1561 { .attrs = it87_attributes_fan[1] },
1562 { .attrs = it87_attributes_fan[2] },
1565 static const struct attribute_group *
1566 it87_get_fan_group(const struct it87_data *data)
1568 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1571 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1572 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1573 &sensor_dev_attr_pwm1.dev_attr.attr,
1574 &dev_attr_pwm1_freq.attr,
1575 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1578 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1579 &sensor_dev_attr_pwm2.dev_attr.attr,
1580 &dev_attr_pwm2_freq.attr,
1581 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1584 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1585 &sensor_dev_attr_pwm3.dev_attr.attr,
1586 &dev_attr_pwm3_freq.attr,
1587 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1591 static const struct attribute_group it87_group_pwm[3] = {
1592 { .attrs = it87_attributes_pwm[0] },
1593 { .attrs = it87_attributes_pwm[1] },
1594 { .attrs = it87_attributes_pwm[2] },
1597 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1598 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1599 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1600 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1601 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1602 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1603 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1604 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1605 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1606 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1609 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1610 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1611 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1612 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1613 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1614 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1615 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1616 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1617 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1620 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1621 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1622 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1623 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1624 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1625 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1626 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1627 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1628 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1632 static const struct attribute_group it87_group_autopwm[3] = {
1633 { .attrs = it87_attributes_autopwm[0] },
1634 { .attrs = it87_attributes_autopwm[1] },
1635 { .attrs = it87_attributes_autopwm[2] },
1638 static struct attribute *it87_attributes_fan_beep[] = {
1639 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1640 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1641 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1642 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1643 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1646 static struct attribute *it87_attributes_vid[] = {
1648 &dev_attr_cpu0_vid.attr,
1652 static const struct attribute_group it87_group_vid = {
1653 .attrs = it87_attributes_vid,
1656 static struct attribute *it87_attributes_label[] = {
1657 &sensor_dev_attr_in3_label.dev_attr.attr,
1658 &sensor_dev_attr_in7_label.dev_attr.attr,
1659 &sensor_dev_attr_in8_label.dev_attr.attr,
1663 static const struct attribute_group it87_group_label = {
1664 .attrs = it87_attributes_label,
1667 /* SuperIO detection - will change isa_address if a chip is found */
1668 static int __init it87_find(unsigned short *address,
1669 struct it87_sio_data *sio_data)
1673 const char *board_vendor, *board_name;
1675 err = superio_enter();
1680 chip_type = force_id ? force_id : superio_inw(DEVID);
1682 switch (chip_type) {
1684 sio_data->type = it87;
1687 sio_data->type = it8712;
1691 sio_data->type = it8716;
1694 sio_data->type = it8718;
1697 sio_data->type = it8720;
1700 sio_data->type = it8721;
1703 sio_data->type = it8728;
1706 sio_data->type = it8782;
1709 sio_data->type = it8783;
1711 case 0xffff: /* No device at all */
1714 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1718 superio_select(PME);
1719 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1720 pr_info("Device not activated, skipping\n");
1724 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1725 if (*address == 0) {
1726 pr_info("Base address not set, skipping\n");
1731 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1732 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1733 chip_type, *address, sio_data->revision);
1735 /* in8 (Vbat) is always internal */
1736 sio_data->internal = (1 << 2);
1738 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1739 if (sio_data->type == it87) {
1740 /* The IT8705F doesn't have VID pins at all */
1741 sio_data->skip_vid = 1;
1743 /* The IT8705F has a different LD number for GPIO */
1745 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1746 } else if (sio_data->type == it8783) {
1747 int reg25, reg27, reg2A, reg2C, regEF;
1749 sio_data->skip_vid = 1; /* No VID */
1751 superio_select(GPIO);
1753 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1754 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1755 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1756 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1757 regEF = superio_inb(IT87_SIO_SPI_REG);
1759 /* Check if fan3 is there or not */
1760 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1761 sio_data->skip_fan |= (1 << 2);
1762 if ((reg25 & (1 << 4))
1763 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1764 sio_data->skip_pwm |= (1 << 2);
1766 /* Check if fan2 is there or not */
1767 if (reg27 & (1 << 7))
1768 sio_data->skip_fan |= (1 << 1);
1769 if (reg27 & (1 << 3))
1770 sio_data->skip_pwm |= (1 << 1);
1773 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1774 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1777 if (reg27 & (1 << 1))
1778 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1782 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1784 if (reg27 & (1 << 2)) {
1786 * The data sheet is a bit unclear regarding the
1787 * internal voltage divider for VCCH5V. It says
1788 * "This bit enables and switches VIN7 (pin 91) to the
1789 * internal voltage divider for VCCH5V".
1790 * This is different to other chips, where the internal
1791 * voltage divider would connect VIN7 to an internal
1792 * voltage source. Maybe that is the case here as well.
1794 * Since we don't know for sure, re-route it if that is
1795 * not the case, and ask the user to report if the
1796 * resulting voltage is sane.
1798 if (!(reg2C & (1 << 1))) {
1800 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1801 pr_notice("Routing internal VCCH5V to in7.\n");
1803 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1804 pr_notice("Please report if it displays a reasonable voltage.\n");
1807 if (reg2C & (1 << 0))
1808 sio_data->internal |= (1 << 0);
1809 if (reg2C & (1 << 1))
1810 sio_data->internal |= (1 << 1);
1812 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1818 superio_select(GPIO);
1820 reg = superio_inb(IT87_SIO_GPIO3_REG);
1821 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1822 sio_data->type == it8782) {
1824 * IT8721F/IT8758E, and IT8782F don't have VID pins
1825 * at all, not sure about the IT8728F.
1827 sio_data->skip_vid = 1;
1829 /* We need at least 4 VID pins */
1831 pr_info("VID is disabled (pins used for GPIO)\n");
1832 sio_data->skip_vid = 1;
1836 /* Check if fan3 is there or not */
1838 sio_data->skip_pwm |= (1 << 2);
1840 sio_data->skip_fan |= (1 << 2);
1842 /* Check if fan2 is there or not */
1843 reg = superio_inb(IT87_SIO_GPIO5_REG);
1845 sio_data->skip_pwm |= (1 << 1);
1847 sio_data->skip_fan |= (1 << 1);
1849 if ((sio_data->type == it8718 || sio_data->type == it8720)
1850 && !(sio_data->skip_vid))
1851 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1853 reg = superio_inb(IT87_SIO_PINX2_REG);
1855 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1858 * The IT8720F has no VIN7 pin, so VCCH should always be
1859 * routed internally to VIN7 with an internal divider.
1860 * Curiously, there still is a configuration bit to control
1861 * this, which means it can be set incorrectly. And even
1862 * more curiously, many boards out there are improperly
1863 * configured, even though the IT8720F datasheet claims
1864 * that the internal routing of VCCH to VIN7 is the default
1865 * setting. So we force the internal routing in this case.
1867 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1868 * If UART6 is enabled, re-route VIN7 to the internal divider
1869 * if that is not already the case.
1871 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
1873 superio_outb(IT87_SIO_PINX2_REG, reg);
1874 pr_notice("Routing internal VCCH to in7\n");
1877 sio_data->internal |= (1 << 0);
1878 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1879 sio_data->type == it8728)
1880 sio_data->internal |= (1 << 1);
1883 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1884 * While VIN7 can be routed to the internal voltage divider,
1885 * VIN5 and VIN6 are not available if UART6 is enabled.
1887 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1888 * is the temperature source. Since we can not read the
1889 * temperature source here, skip_temp is preliminary.
1892 sio_data->skip_in |= (1 << 5) | (1 << 6);
1893 sio_data->skip_temp |= (1 << 2);
1896 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1898 if (sio_data->beep_pin)
1899 pr_info("Beeping is supported\n");
1901 /* Disable specific features based on DMI strings */
1902 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1903 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1904 if (board_vendor && board_name) {
1905 if (strcmp(board_vendor, "nVIDIA") == 0
1906 && strcmp(board_name, "FN68PT") == 0) {
1908 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1909 * connected to a fan, but to something else. One user
1910 * has reported instant system power-off when changing
1911 * the PWM2 duty cycle, so we disable it.
1912 * I use the board name string as the trigger in case
1913 * the same board is ever used in other systems.
1915 pr_info("Disabling pwm2 due to hardware constraints\n");
1916 sio_data->skip_pwm = (1 << 1);
1925 static void it87_remove_files(struct device *dev)
1927 struct it87_data *data = platform_get_drvdata(pdev);
1928 struct it87_sio_data *sio_data = dev->platform_data;
1929 const struct attribute_group *fan_group = it87_get_fan_group(data);
1932 sysfs_remove_group(&dev->kobj, &it87_group);
1933 for (i = 0; i < 9; i++) {
1934 if (sio_data->skip_in & (1 << i))
1936 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1937 if (it87_attributes_in_beep[i])
1938 sysfs_remove_file(&dev->kobj,
1939 it87_attributes_in_beep[i]);
1941 for (i = 0; i < 3; i++) {
1942 if (!(data->has_temp & (1 << i)))
1944 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1945 if (has_temp_offset(data))
1946 sysfs_remove_file(&dev->kobj,
1947 it87_attributes_temp_offset[i]);
1948 if (sio_data->beep_pin)
1949 sysfs_remove_file(&dev->kobj,
1950 it87_attributes_temp_beep[i]);
1952 for (i = 0; i < 5; i++) {
1953 if (!(data->has_fan & (1 << i)))
1955 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1956 if (sio_data->beep_pin)
1957 sysfs_remove_file(&dev->kobj,
1958 it87_attributes_fan_beep[i]);
1960 for (i = 0; i < 3; i++) {
1961 if (sio_data->skip_pwm & (1 << 0))
1963 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1964 if (has_old_autopwm(data))
1965 sysfs_remove_group(&dev->kobj,
1966 &it87_group_autopwm[i]);
1968 if (!sio_data->skip_vid)
1969 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1970 sysfs_remove_group(&dev->kobj, &it87_group_label);
1973 static int it87_probe(struct platform_device *pdev)
1975 struct it87_data *data;
1976 struct resource *res;
1977 struct device *dev = &pdev->dev;
1978 struct it87_sio_data *sio_data = dev->platform_data;
1979 const struct attribute_group *fan_group;
1981 int enable_pwm_interface;
1982 int fan_beep_need_rw;
1983 static const char * const names[] = {
1995 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1996 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1998 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1999 (unsigned long)res->start,
2000 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2004 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2008 data->addr = res->start;
2009 data->type = sio_data->type;
2010 data->revision = sio_data->revision;
2011 data->name = names[sio_data->type];
2013 /* Now, we do the remaining detection. */
2014 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2015 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2018 platform_set_drvdata(pdev, data);
2020 mutex_init(&data->update_lock);
2022 /* Check PWM configuration */
2023 enable_pwm_interface = it87_check_pwm(dev);
2025 /* Starting with IT8721F, we handle scaling of internal voltages */
2026 if (has_12mv_adc(data)) {
2027 if (sio_data->internal & (1 << 0))
2028 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2029 if (sio_data->internal & (1 << 1))
2030 data->in_scaled |= (1 << 7); /* in7 is VSB */
2031 if (sio_data->internal & (1 << 2))
2032 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2033 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
2034 if (sio_data->internal & (1 << 0))
2035 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2036 if (sio_data->internal & (1 << 1))
2037 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
2040 data->has_temp = 0x07;
2041 if (sio_data->skip_temp & (1 << 2)) {
2042 if (sio_data->type == it8782
2043 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2044 data->has_temp &= ~(1 << 2);
2047 /* Initialize the IT87 chip */
2048 it87_init_device(pdev);
2050 /* Register sysfs hooks */
2051 err = sysfs_create_group(&dev->kobj, &it87_group);
2055 for (i = 0; i < 9; i++) {
2056 if (sio_data->skip_in & (1 << i))
2058 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2061 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2062 err = sysfs_create_file(&dev->kobj,
2063 it87_attributes_in_beep[i]);
2069 for (i = 0; i < 3; i++) {
2070 if (!(data->has_temp & (1 << i)))
2072 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2075 if (has_temp_offset(data)) {
2076 err = sysfs_create_file(&dev->kobj,
2077 it87_attributes_temp_offset[i]);
2081 if (sio_data->beep_pin) {
2082 err = sysfs_create_file(&dev->kobj,
2083 it87_attributes_temp_beep[i]);
2089 /* Do not create fan files for disabled fans */
2090 fan_group = it87_get_fan_group(data);
2091 fan_beep_need_rw = 1;
2092 for (i = 0; i < 5; i++) {
2093 if (!(data->has_fan & (1 << i)))
2095 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2099 if (sio_data->beep_pin) {
2100 err = sysfs_create_file(&dev->kobj,
2101 it87_attributes_fan_beep[i]);
2104 if (!fan_beep_need_rw)
2108 * As we have a single beep enable bit for all fans,
2109 * only the first enabled fan has a writable attribute
2112 if (sysfs_chmod_file(&dev->kobj,
2113 it87_attributes_fan_beep[i],
2115 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2117 fan_beep_need_rw = 0;
2121 if (enable_pwm_interface) {
2122 for (i = 0; i < 3; i++) {
2123 if (sio_data->skip_pwm & (1 << i))
2125 err = sysfs_create_group(&dev->kobj,
2126 &it87_group_pwm[i]);
2130 if (!has_old_autopwm(data))
2132 err = sysfs_create_group(&dev->kobj,
2133 &it87_group_autopwm[i]);
2139 if (!sio_data->skip_vid) {
2140 data->vrm = vid_which_vrm();
2141 /* VID reading from Super-I/O config space if available */
2142 data->vid = sio_data->vid_value;
2143 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2148 /* Export labels for internal sensors */
2149 for (i = 0; i < 3; i++) {
2150 if (!(sio_data->internal & (1 << i)))
2152 err = sysfs_create_file(&dev->kobj,
2153 it87_attributes_label[i]);
2158 data->hwmon_dev = hwmon_device_register(dev);
2159 if (IS_ERR(data->hwmon_dev)) {
2160 err = PTR_ERR(data->hwmon_dev);
2167 it87_remove_files(dev);
2171 static int it87_remove(struct platform_device *pdev)
2173 struct it87_data *data = platform_get_drvdata(pdev);
2175 hwmon_device_unregister(data->hwmon_dev);
2176 it87_remove_files(&pdev->dev);
2182 * Must be called with data->update_lock held, except during initialization.
2183 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2184 * would slow down the IT87 access and should not be necessary.
2186 static int it87_read_value(struct it87_data *data, u8 reg)
2188 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2189 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2193 * Must be called with data->update_lock held, except during initialization.
2194 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2195 * would slow down the IT87 access and should not be necessary.
2197 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2199 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2200 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2203 /* Return 1 if and only if the PWM interface is safe to use */
2204 static int it87_check_pwm(struct device *dev)
2206 struct it87_data *data = dev_get_drvdata(dev);
2208 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2209 * and polarity set to active low is sign that this is the case so we
2210 * disable pwm control to protect the user.
2212 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2213 if ((tmp & 0x87) == 0) {
2214 if (fix_pwm_polarity) {
2216 * The user asks us to attempt a chip reconfiguration.
2217 * This means switching to active high polarity and
2218 * inverting all fan speed values.
2223 for (i = 0; i < 3; i++)
2224 pwm[i] = it87_read_value(data,
2228 * If any fan is in automatic pwm mode, the polarity
2229 * might be correct, as suspicious as it seems, so we
2230 * better don't change anything (but still disable the
2233 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2234 dev_info(dev, "Reconfiguring PWM to "
2235 "active high polarity\n");
2236 it87_write_value(data, IT87_REG_FAN_CTL,
2238 for (i = 0; i < 3; i++)
2239 it87_write_value(data,
2245 dev_info(dev, "PWM configuration is "
2246 "too broken to be fixed\n");
2249 dev_info(dev, "Detected broken BIOS "
2250 "defaults, disabling PWM interface\n");
2252 } else if (fix_pwm_polarity) {
2253 dev_info(dev, "PWM configuration looks "
2254 "sane, won't touch\n");
2260 /* Called when we have found a new IT87. */
2261 static void it87_init_device(struct platform_device *pdev)
2263 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2264 struct it87_data *data = platform_get_drvdata(pdev);
2269 * For each PWM channel:
2270 * - If it is in automatic mode, setting to manual mode should set
2271 * the fan to full speed by default.
2272 * - If it is in manual mode, we need a mapping to temperature
2273 * channels to use when later setting to automatic mode later.
2274 * Use a 1:1 mapping by default (we are clueless.)
2275 * In both cases, the value can (and should) be changed by the user
2276 * prior to switching to a different mode.
2277 * Note that this is no longer needed for the IT8721F and later, as
2278 * these have separate registers for the temperature mapping and the
2279 * manual duty cycle.
2281 for (i = 0; i < 3; i++) {
2282 data->pwm_temp_map[i] = i;
2283 data->pwm_duty[i] = 0x7f; /* Full speed */
2284 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2288 * Some chips seem to have default value 0xff for all limit
2289 * registers. For low voltage limits it makes no sense and triggers
2290 * alarms, so change to 0 instead. For high temperature limits, it
2291 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2292 * but is still confusing, so change to 127 degrees C.
2294 for (i = 0; i < 8; i++) {
2295 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2297 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2299 for (i = 0; i < 3; i++) {
2300 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2302 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2306 * Temperature channels are not forcibly enabled, as they can be
2307 * set to two different sensor types and we can't guess which one
2308 * is correct for a given system. These channels can be enabled at
2309 * run-time through the temp{1-3}_type sysfs accessors if needed.
2312 /* Check if voltage monitors are reset manually or by some reason */
2313 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2314 if ((tmp & 0xff) == 0) {
2315 /* Enable all voltage monitors */
2316 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2319 /* Check if tachometers are reset manually or by some reason */
2320 mask = 0x70 & ~(sio_data->skip_fan << 4);
2321 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2322 if ((data->fan_main_ctrl & mask) == 0) {
2323 /* Enable all fan tachometers */
2324 data->fan_main_ctrl |= mask;
2325 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2326 data->fan_main_ctrl);
2328 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2330 /* Set tachometers to 16-bit mode if needed */
2331 if (has_16bit_fans(data)) {
2332 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2333 if (~tmp & 0x07 & data->has_fan) {
2335 "Setting fan1-3 to 16-bit mode\n");
2336 it87_write_value(data, IT87_REG_FAN_16BIT,
2339 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2340 if (data->type != it87 && data->type != it8782 &&
2341 data->type != it8783) {
2343 data->has_fan |= (1 << 3); /* fan4 enabled */
2345 data->has_fan |= (1 << 4); /* fan5 enabled */
2349 /* Fan input pins may be used for alternative functions */
2350 data->has_fan &= ~sio_data->skip_fan;
2352 /* Start monitoring */
2353 it87_write_value(data, IT87_REG_CONFIG,
2354 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2355 | (update_vbat ? 0x41 : 0x01));
2358 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2360 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2361 if (has_newer_autopwm(data)) {
2362 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2363 data->pwm_duty[nr] = it87_read_value(data,
2364 IT87_REG_PWM_DUTY(nr));
2366 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2367 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2368 else /* Manual mode */
2369 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2372 if (has_old_autopwm(data)) {
2375 for (i = 0; i < 5 ; i++)
2376 data->auto_temp[nr][i] = it87_read_value(data,
2377 IT87_REG_AUTO_TEMP(nr, i));
2378 for (i = 0; i < 3 ; i++)
2379 data->auto_pwm[nr][i] = it87_read_value(data,
2380 IT87_REG_AUTO_PWM(nr, i));
2384 static struct it87_data *it87_update_device(struct device *dev)
2386 struct it87_data *data = dev_get_drvdata(dev);
2389 mutex_lock(&data->update_lock);
2391 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2395 * Cleared after each update, so reenable. Value
2396 * returned by this read will be previous value
2398 it87_write_value(data, IT87_REG_CONFIG,
2399 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2401 for (i = 0; i <= 7; i++) {
2403 it87_read_value(data, IT87_REG_VIN(i));
2405 it87_read_value(data, IT87_REG_VIN_MIN(i));
2407 it87_read_value(data, IT87_REG_VIN_MAX(i));
2409 /* in8 (battery) has no limit registers */
2410 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2412 for (i = 0; i < 5; i++) {
2413 /* Skip disabled fans */
2414 if (!(data->has_fan & (1 << i)))
2418 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2419 data->fan[i] = it87_read_value(data,
2421 /* Add high byte if in 16-bit mode */
2422 if (has_16bit_fans(data)) {
2423 data->fan[i] |= it87_read_value(data,
2424 IT87_REG_FANX[i]) << 8;
2425 data->fan_min[i] |= it87_read_value(data,
2426 IT87_REG_FANX_MIN[i]) << 8;
2429 for (i = 0; i < 3; i++) {
2430 if (!(data->has_temp & (1 << i)))
2433 it87_read_value(data, IT87_REG_TEMP(i));
2435 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2437 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2438 if (has_temp_offset(data))
2440 it87_read_value(data,
2441 IT87_REG_TEMP_OFFSET[i]);
2444 /* Newer chips don't have clock dividers */
2445 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2446 i = it87_read_value(data, IT87_REG_FAN_DIV);
2447 data->fan_div[0] = i & 0x07;
2448 data->fan_div[1] = (i >> 3) & 0x07;
2449 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2453 it87_read_value(data, IT87_REG_ALARM1) |
2454 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2455 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2456 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2458 data->fan_main_ctrl = it87_read_value(data,
2459 IT87_REG_FAN_MAIN_CTRL);
2460 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2461 for (i = 0; i < 3; i++)
2462 it87_update_pwm_ctrl(data, i);
2464 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2466 * The IT8705F does not have VID capability.
2467 * The IT8718F and later don't use IT87_REG_VID for the
2470 if (data->type == it8712 || data->type == it8716) {
2471 data->vid = it87_read_value(data, IT87_REG_VID);
2473 * The older IT8712F revisions had only 5 VID pins,
2474 * but we assume it is always safe to read 6 bits.
2478 data->last_updated = jiffies;
2482 mutex_unlock(&data->update_lock);
2487 static int __init it87_device_add(unsigned short address,
2488 const struct it87_sio_data *sio_data)
2490 struct resource res = {
2491 .start = address + IT87_EC_OFFSET,
2492 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2494 .flags = IORESOURCE_IO,
2498 err = acpi_check_resource_conflict(&res);
2502 pdev = platform_device_alloc(DRVNAME, address);
2505 pr_err("Device allocation failed\n");
2509 err = platform_device_add_resources(pdev, &res, 1);
2511 pr_err("Device resource addition failed (%d)\n", err);
2512 goto exit_device_put;
2515 err = platform_device_add_data(pdev, sio_data,
2516 sizeof(struct it87_sio_data));
2518 pr_err("Platform data allocation failed\n");
2519 goto exit_device_put;
2522 err = platform_device_add(pdev);
2524 pr_err("Device addition failed (%d)\n", err);
2525 goto exit_device_put;
2531 platform_device_put(pdev);
2536 static int __init sm_it87_init(void)
2539 unsigned short isa_address = 0;
2540 struct it87_sio_data sio_data;
2542 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2543 err = it87_find(&isa_address, &sio_data);
2546 err = platform_driver_register(&it87_driver);
2550 err = it87_device_add(isa_address, &sio_data);
2552 platform_driver_unregister(&it87_driver);
2559 static void __exit sm_it87_exit(void)
2561 platform_device_unregister(pdev);
2562 platform_driver_unregister(&it87_driver);
2566 MODULE_AUTHOR("Chris Gauthron, "
2567 "Jean Delvare <khali@linux-fr.org>");
2568 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2569 module_param(update_vbat, bool, 0);
2570 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2571 module_param(fix_pwm_polarity, bool, 0);
2572 MODULE_PARM_DESC(fix_pwm_polarity,
2573 "Force PWM polarity to active high (DANGEROUS)");
2574 MODULE_LICENSE("GPL");
2576 module_init(sm_it87_init);
2577 module_exit(sm_it87_exit);