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 * IT8771E Super I/O chip w/LPC interface
23 * IT8772E Super I/O chip w/LPC interface
24 * IT8783E/F Super I/O chip w/LPC interface
25 * Sis950 A clone of the IT8705F
27 * Copyright (C) 2001 Chris Gauthron
28 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/slab.h>
50 #include <linux/jiffies.h>
51 #include <linux/platform_device.h>
52 #include <linux/hwmon.h>
53 #include <linux/hwmon-sysfs.h>
54 #include <linux/hwmon-vid.h>
55 #include <linux/err.h>
56 #include <linux/mutex.h>
57 #include <linux/sysfs.h>
58 #include <linux/string.h>
59 #include <linux/dmi.h>
60 #include <linux/acpi.h>
64 #define kstrtol strict_strtol
67 #define kstrtoul strict_strtoul
70 #define DRVNAME "it87"
72 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
75 static unsigned short force_id;
76 module_param(force_id, ushort, 0);
77 MODULE_PARM_DESC(force_id, "Override the detected device ID");
79 static struct platform_device *pdev;
81 #define REG 0x2e /* The register to read/write */
82 #define DEV 0x07 /* Register: Logical device select */
83 #define VAL 0x2f /* The value to read/write */
84 #define PME 0x04 /* The device with the fan registers in it */
86 /* The device with the IT8718F/IT8720F VID value in it */
89 #define DEVID 0x20 /* Register: Device ID */
90 #define DEVREV 0x22 /* Register: Device Revision */
92 static inline int superio_inb(int reg)
98 static inline void superio_outb(int reg, int val)
104 static int superio_inw(int reg)
114 static inline void superio_select(int ldn)
120 static inline int superio_enter(void)
123 * Try to reserve REG and REG + 1 for exclusive access.
125 #ifdef request_muxed_region
126 if (!request_muxed_region(REG, 2, DRVNAME))
137 static inline void superio_exit(void)
141 #ifdef request_muxed_region
142 release_region(REG, 2);
146 /* Logical device 4 registers */
147 #define IT8712F_DEVID 0x8712
148 #define IT8705F_DEVID 0x8705
149 #define IT8716F_DEVID 0x8716
150 #define IT8718F_DEVID 0x8718
151 #define IT8720F_DEVID 0x8720
152 #define IT8721F_DEVID 0x8721
153 #define IT8726F_DEVID 0x8726
154 #define IT8728F_DEVID 0x8728
155 #define IT8771E_DEVID 0x8771
156 #define IT8772E_DEVID 0x8772
157 #define IT8783E_DEVID 0x8783
158 #define IT87_ACT_REG 0x30
159 #define IT87_BASE_REG 0x60
161 /* Logical device 7 registers (IT8712F and later) */
162 #define IT87_SIO_GPIO1_REG 0x25
163 #define IT87_SIO_GPIO3_REG 0x27
164 #define IT87_SIO_GPIO5_REG 0x29
165 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
166 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
167 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
168 #define IT87_SIO_VID_REG 0xfc /* VID value */
169 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
171 /* Update battery voltage after every reading if true */
172 static bool update_vbat;
174 /* Not all BIOSes properly configure the PWM registers */
175 static bool fix_pwm_polarity;
177 /* Many IT87 constants specified below */
179 /* Length of ISA address segment */
180 #define IT87_EXTENT 8
182 /* Length of ISA address segment for Environmental Controller */
183 #define IT87_EC_EXTENT 2
185 /* Offset of EC registers from ISA base address */
186 #define IT87_EC_OFFSET 5
188 /* Where are the ISA address/data registers relative to the EC base address */
189 #define IT87_ADDR_REG_OFFSET 0
190 #define IT87_DATA_REG_OFFSET 1
192 /*----- The IT87 registers -----*/
194 #define IT87_REG_CONFIG 0x00
196 #define IT87_REG_ALARM1 0x01
197 #define IT87_REG_ALARM2 0x02
198 #define IT87_REG_ALARM3 0x03
201 * The IT8718F and IT8720F have the VID value in a different register, in
202 * Super-I/O configuration space.
204 #define IT87_REG_VID 0x0a
206 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
207 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
210 #define IT87_REG_FAN_DIV 0x0b
211 #define IT87_REG_FAN_16BIT 0x0c
213 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
215 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
216 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
217 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
218 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
219 #define IT87_REG_FAN_MAIN_CTRL 0x13
220 #define IT87_REG_FAN_CTL 0x14
221 #define IT87_REG_PWM(nr) (0x15 + (nr))
222 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
224 #define IT87_REG_VIN(nr) (0x20 + (nr))
225 #define IT87_REG_TEMP(nr) (0x29 + (nr))
227 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
228 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
229 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
230 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
232 #define IT87_REG_VIN_ENABLE 0x50
233 #define IT87_REG_TEMP_ENABLE 0x51
234 #define IT87_REG_BEEP_ENABLE 0x5c
236 #define IT87_REG_CHIPID 0x58
238 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
239 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
242 struct it87_sio_data {
244 /* Values read from Super-I/O config space */
248 u8 internal; /* Internal sensors can be labeled */
249 /* Features skipped based on config or DMI */
256 * For each registered chip, we need to keep some data in memory.
257 * The structure is dynamically allocated.
260 struct device *hwmon_dev;
266 struct mutex update_lock;
267 char valid; /* !=0 if following fields are valid */
268 unsigned long last_updated; /* In jiffies */
270 u16 in_scaled; /* Internal voltage sensors are scaled */
271 u8 in[9]; /* Register value */
272 u8 in_max[8]; /* Register value */
273 u8 in_min[8]; /* Register value */
274 u8 has_fan; /* Bitfield, fans enabled */
275 u16 fan[5]; /* Register values, possibly combined */
276 u16 fan_min[5]; /* Register values, possibly combined */
277 s8 temp[3]; /* Register value */
278 s8 temp_high[3]; /* Register value */
279 s8 temp_low[3]; /* Register value */
280 u8 sensor; /* Register value */
281 u8 fan_div[3]; /* Register encoding, shifted right */
282 u8 vid; /* Register encoding, combined */
284 u32 alarms; /* Register encoding, combined */
285 u8 beeps; /* Register encoding */
286 u8 fan_main_ctrl; /* Register value */
287 u8 fan_ctl; /* Register value */
290 * The following 3 arrays correspond to the same registers up to
291 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
292 * 7, and we want to preserve settings on mode changes, so we have
293 * to track all values separately.
294 * Starting with the IT8721F, the manual PWM duty cycles are stored
295 * in separate registers (8-bit values), so the separate tracking
296 * is no longer needed, but it is still done to keep the driver
299 u8 pwm_ctrl[3]; /* Register value */
300 u8 pwm_duty[3]; /* Manual PWM value set by user */
301 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
303 /* Automatic fan speed control registers */
304 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
305 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
308 static inline int has_12mv_adc(const struct it87_data *data)
311 * IT8721F and later have a 12 mV ADC, also with internal scaling
312 * on selected inputs.
314 return data->type == it8721
315 || data->type == it8728
316 || data->type == it8771
317 || data->type == it8772;
320 static inline int has_newer_autopwm(const struct it87_data *data)
323 * IT8721F and later have separate registers for the temperature
324 * mapping and the manual duty cycle.
325 * [ Assume that also applies to IT8771 / IT8772 ]
327 return data->type == it8721
328 || data->type == it8728
329 || data->type == it8771
330 || data->type == it8772;
333 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
337 if (has_12mv_adc(data)) {
338 if (data->in_scaled & (1 << nr))
343 if (data->in_scaled & (1 << nr))
349 val = DIV_ROUND_CLOSEST(val, lsb);
350 return SENSORS_LIMIT(val, 0, 255);
353 static int in_from_reg(const struct it87_data *data, int nr, int val)
355 if (has_12mv_adc(data)) {
356 if (data->in_scaled & (1 << nr))
361 if (data->in_scaled & (1 << nr))
368 static inline u8 FAN_TO_REG(long rpm, int div)
372 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
373 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
377 static inline u16 FAN16_TO_REG(long rpm)
381 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
384 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
385 1350000 / ((val) * (div)))
386 /* The divider is fixed to 2 in 16-bit mode */
387 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
388 1350000 / ((val) * 2))
390 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
391 ((val) + 500) / 1000), -128, 127))
392 #define TEMP_FROM_REG(val) ((val) * 1000)
394 static u8 pwm_to_reg(const struct it87_data *data, long val)
396 if (has_newer_autopwm(data))
402 static int pwm_from_reg(const struct it87_data *data, u8 reg)
404 if (has_newer_autopwm(data))
407 return (reg & 0x7f) << 1;
411 static int DIV_TO_REG(int val)
414 while (answer < 7 && (val >>= 1))
418 #define DIV_FROM_REG(val) (1 << (val))
420 static const unsigned int pwm_freq[8] = {
431 static inline int has_16bit_fans(const struct it87_data *data)
434 * IT8705F Datasheet 0.4.1, 3h == Version G.
435 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
436 * These are the first revisions with 16-bit tachometer support.
438 return (data->type == it87 && data->revision >= 0x03)
439 || (data->type == it8712 && data->revision >= 0x08)
440 || data->type == it8716
441 || data->type == it8718
442 || data->type == it8720
443 || data->type == it8721
444 || data->type == it8728
445 || data->type == it8771
446 || data->type == it8772
447 || data->type == it8783;
450 static inline int has_old_autopwm(const struct it87_data *data)
453 * The old automatic fan speed control interface is implemented
454 * by IT8705F chips up to revision F and IT8712F chips up to
457 return (data->type == it87 && data->revision < 0x03)
458 || (data->type == it8712 && data->revision < 0x08);
461 static int it87_probe(struct platform_device *pdev);
462 static int __devexit it87_remove(struct platform_device *pdev);
464 static int it87_read_value(struct it87_data *data, u8 reg);
465 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
466 static struct it87_data *it87_update_device(struct device *dev);
467 static int it87_check_pwm(struct device *dev);
468 static void it87_init_device(struct platform_device *pdev);
471 static struct platform_driver it87_driver = {
473 .owner = THIS_MODULE,
477 .remove = __devexit_p(it87_remove),
480 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
483 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
484 int nr = sensor_attr->index;
486 struct it87_data *data = it87_update_device(dev);
487 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
490 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
493 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
494 int nr = sensor_attr->index;
496 struct it87_data *data = it87_update_device(dev);
497 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
500 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
503 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
504 int nr = sensor_attr->index;
506 struct it87_data *data = it87_update_device(dev);
507 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
510 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
511 const char *buf, size_t count)
513 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
514 int nr = sensor_attr->index;
516 struct it87_data *data = dev_get_drvdata(dev);
519 if (kstrtoul(buf, 10, &val) < 0)
522 mutex_lock(&data->update_lock);
523 data->in_min[nr] = in_to_reg(data, nr, val);
524 it87_write_value(data, IT87_REG_VIN_MIN(nr),
526 mutex_unlock(&data->update_lock);
529 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
530 const char *buf, size_t count)
532 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
533 int nr = sensor_attr->index;
535 struct it87_data *data = dev_get_drvdata(dev);
538 if (kstrtoul(buf, 10, &val) < 0)
541 mutex_lock(&data->update_lock);
542 data->in_max[nr] = in_to_reg(data, nr, val);
543 it87_write_value(data, IT87_REG_VIN_MAX(nr),
545 mutex_unlock(&data->update_lock);
549 #define show_in_offset(offset) \
550 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
551 show_in, NULL, offset);
553 #define limit_in_offset(offset) \
554 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
555 show_in_min, set_in_min, offset); \
556 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
557 show_in_max, set_in_max, offset);
578 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
581 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
582 int nr = sensor_attr->index;
584 struct it87_data *data = it87_update_device(dev);
585 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
587 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
590 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
591 int nr = sensor_attr->index;
593 struct it87_data *data = it87_update_device(dev);
594 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
596 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
599 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
600 int nr = sensor_attr->index;
602 struct it87_data *data = it87_update_device(dev);
603 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
605 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
606 const char *buf, size_t count)
608 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
609 int nr = sensor_attr->index;
611 struct it87_data *data = dev_get_drvdata(dev);
614 if (kstrtol(buf, 10, &val) < 0)
617 mutex_lock(&data->update_lock);
618 data->temp_high[nr] = TEMP_TO_REG(val);
619 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
620 mutex_unlock(&data->update_lock);
623 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
624 const char *buf, size_t count)
626 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
627 int nr = sensor_attr->index;
629 struct it87_data *data = dev_get_drvdata(dev);
632 if (kstrtol(buf, 10, &val) < 0)
635 mutex_lock(&data->update_lock);
636 data->temp_low[nr] = TEMP_TO_REG(val);
637 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
638 mutex_unlock(&data->update_lock);
641 #define show_temp_offset(offset) \
642 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
643 show_temp, NULL, offset - 1); \
644 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
645 show_temp_max, set_temp_max, offset - 1); \
646 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
647 show_temp_min, set_temp_min, offset - 1);
653 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
656 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
657 int nr = sensor_attr->index;
658 struct it87_data *data = it87_update_device(dev);
659 u8 reg = data->sensor; /* In case value is updated while used */
662 return sprintf(buf, "3\n"); /* thermal diode */
664 return sprintf(buf, "4\n"); /* thermistor */
665 return sprintf(buf, "0\n"); /* disabled */
667 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
668 const char *buf, size_t count)
670 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
671 int nr = sensor_attr->index;
673 struct it87_data *data = dev_get_drvdata(dev);
677 if (kstrtol(buf, 10, &val) < 0)
680 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
683 if (val == 2) { /* backwards compatibility */
684 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
688 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
696 mutex_lock(&data->update_lock);
698 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
699 data->valid = 0; /* Force cache refresh */
700 mutex_unlock(&data->update_lock);
703 #define show_sensor_offset(offset) \
704 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
705 show_sensor, set_sensor, offset - 1);
707 show_sensor_offset(1);
708 show_sensor_offset(2);
709 show_sensor_offset(3);
713 static int pwm_mode(const struct it87_data *data, int nr)
715 int ctrl = data->fan_main_ctrl & (1 << nr);
717 if (ctrl == 0) /* Full speed */
719 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
721 else /* Manual mode */
725 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
728 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
729 int nr = sensor_attr->index;
731 struct it87_data *data = it87_update_device(dev);
732 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
733 DIV_FROM_REG(data->fan_div[nr])));
735 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
738 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
739 int nr = sensor_attr->index;
741 struct it87_data *data = it87_update_device(dev);
742 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
743 DIV_FROM_REG(data->fan_div[nr])));
745 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
748 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
749 int nr = sensor_attr->index;
751 struct it87_data *data = it87_update_device(dev);
752 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
754 static ssize_t show_pwm_enable(struct device *dev,
755 struct device_attribute *attr, char *buf)
757 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
758 int nr = sensor_attr->index;
760 struct it87_data *data = it87_update_device(dev);
761 return sprintf(buf, "%d\n", pwm_mode(data, nr));
763 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
766 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
767 int nr = sensor_attr->index;
769 struct it87_data *data = it87_update_device(dev);
770 return sprintf(buf, "%d\n",
771 pwm_from_reg(data, data->pwm_duty[nr]));
773 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
776 struct it87_data *data = it87_update_device(dev);
777 int index = (data->fan_ctl >> 4) & 0x07;
779 return sprintf(buf, "%u\n", pwm_freq[index]);
781 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
782 const char *buf, size_t count)
784 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
785 int nr = sensor_attr->index;
787 struct it87_data *data = dev_get_drvdata(dev);
791 if (kstrtol(buf, 10, &val) < 0)
794 mutex_lock(&data->update_lock);
795 reg = it87_read_value(data, IT87_REG_FAN_DIV);
798 data->fan_div[nr] = reg & 0x07;
801 data->fan_div[nr] = (reg >> 3) & 0x07;
804 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
808 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
809 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
810 mutex_unlock(&data->update_lock);
813 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
814 const char *buf, size_t count)
816 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
817 int nr = sensor_attr->index;
819 struct it87_data *data = dev_get_drvdata(dev);
824 if (kstrtoul(buf, 10, &val) < 0)
827 mutex_lock(&data->update_lock);
828 old = it87_read_value(data, IT87_REG_FAN_DIV);
830 /* Save fan min limit */
831 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
836 data->fan_div[nr] = DIV_TO_REG(val);
840 data->fan_div[nr] = 1;
842 data->fan_div[nr] = 3;
845 val |= (data->fan_div[0] & 0x07);
846 val |= (data->fan_div[1] & 0x07) << 3;
847 if (data->fan_div[2] == 3)
849 it87_write_value(data, IT87_REG_FAN_DIV, val);
851 /* Restore fan min limit */
852 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
853 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
855 mutex_unlock(&data->update_lock);
859 /* Returns 0 if OK, -EINVAL otherwise */
860 static int check_trip_points(struct device *dev, int nr)
862 const struct it87_data *data = dev_get_drvdata(dev);
865 if (has_old_autopwm(data)) {
866 for (i = 0; i < 3; i++) {
867 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
870 for (i = 0; i < 2; i++) {
871 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
877 dev_err(dev, "Inconsistent trip points, not switching to "
879 dev_err(dev, "Adjust the trip points and try again\n");
884 static ssize_t set_pwm_enable(struct device *dev,
885 struct device_attribute *attr, const char *buf, size_t count)
887 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
888 int nr = sensor_attr->index;
890 struct it87_data *data = dev_get_drvdata(dev);
893 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
896 /* Check trip points before switching to automatic mode */
898 if (check_trip_points(dev, nr) < 0)
902 mutex_lock(&data->update_lock);
906 /* make sure the fan is on when in on/off mode */
907 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
908 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
909 /* set on/off mode */
910 data->fan_main_ctrl &= ~(1 << nr);
911 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
912 data->fan_main_ctrl);
914 if (val == 1) /* Manual mode */
915 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
916 data->pwm_temp_map[nr] :
918 else /* Automatic mode */
919 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
920 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
921 /* set SmartGuardian mode */
922 data->fan_main_ctrl |= (1 << nr);
923 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
924 data->fan_main_ctrl);
927 mutex_unlock(&data->update_lock);
930 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
931 const char *buf, size_t count)
933 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
934 int nr = sensor_attr->index;
936 struct it87_data *data = dev_get_drvdata(dev);
939 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
942 mutex_lock(&data->update_lock);
943 if (has_newer_autopwm(data)) {
945 * If we are in automatic mode, the PWM duty cycle register
946 * is read-only so we can't write the value.
948 if (data->pwm_ctrl[nr] & 0x80) {
949 mutex_unlock(&data->update_lock);
952 data->pwm_duty[nr] = pwm_to_reg(data, val);
953 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
956 data->pwm_duty[nr] = pwm_to_reg(data, val);
958 * If we are in manual mode, write the duty cycle immediately;
959 * otherwise, just store it for later use.
961 if (!(data->pwm_ctrl[nr] & 0x80)) {
962 data->pwm_ctrl[nr] = data->pwm_duty[nr];
963 it87_write_value(data, IT87_REG_PWM(nr),
967 mutex_unlock(&data->update_lock);
970 static ssize_t set_pwm_freq(struct device *dev,
971 struct device_attribute *attr, const char *buf, size_t count)
973 struct it87_data *data = dev_get_drvdata(dev);
977 if (kstrtoul(buf, 10, &val) < 0)
980 /* Search for the nearest available frequency */
981 for (i = 0; i < 7; i++) {
982 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
986 mutex_lock(&data->update_lock);
987 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
988 data->fan_ctl |= i << 4;
989 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
990 mutex_unlock(&data->update_lock);
994 static ssize_t show_pwm_temp_map(struct device *dev,
995 struct device_attribute *attr, char *buf)
997 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
998 int nr = sensor_attr->index;
1000 struct it87_data *data = it87_update_device(dev);
1003 if (data->pwm_temp_map[nr] < 3)
1004 map = 1 << data->pwm_temp_map[nr];
1006 map = 0; /* Should never happen */
1007 return sprintf(buf, "%d\n", map);
1009 static ssize_t set_pwm_temp_map(struct device *dev,
1010 struct device_attribute *attr, const char *buf, size_t count)
1012 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1013 int nr = sensor_attr->index;
1015 struct it87_data *data = dev_get_drvdata(dev);
1020 * This check can go away if we ever support automatic fan speed
1021 * control on newer chips.
1023 if (!has_old_autopwm(data)) {
1024 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1028 if (kstrtol(buf, 10, &val) < 0)
1045 mutex_lock(&data->update_lock);
1046 data->pwm_temp_map[nr] = reg;
1048 * If we are in automatic mode, write the temp mapping immediately;
1049 * otherwise, just store it for later use.
1051 if (data->pwm_ctrl[nr] & 0x80) {
1052 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1053 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1055 mutex_unlock(&data->update_lock);
1059 static ssize_t show_auto_pwm(struct device *dev,
1060 struct device_attribute *attr, char *buf)
1062 struct it87_data *data = it87_update_device(dev);
1063 struct sensor_device_attribute_2 *sensor_attr =
1064 to_sensor_dev_attr_2(attr);
1065 int nr = sensor_attr->nr;
1066 int point = sensor_attr->index;
1068 return sprintf(buf, "%d\n",
1069 pwm_from_reg(data, data->auto_pwm[nr][point]));
1072 static ssize_t set_auto_pwm(struct device *dev,
1073 struct device_attribute *attr, const char *buf, size_t count)
1075 struct it87_data *data = dev_get_drvdata(dev);
1076 struct sensor_device_attribute_2 *sensor_attr =
1077 to_sensor_dev_attr_2(attr);
1078 int nr = sensor_attr->nr;
1079 int point = sensor_attr->index;
1082 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1085 mutex_lock(&data->update_lock);
1086 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1087 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1088 data->auto_pwm[nr][point]);
1089 mutex_unlock(&data->update_lock);
1093 static ssize_t show_auto_temp(struct device *dev,
1094 struct device_attribute *attr, char *buf)
1096 struct it87_data *data = it87_update_device(dev);
1097 struct sensor_device_attribute_2 *sensor_attr =
1098 to_sensor_dev_attr_2(attr);
1099 int nr = sensor_attr->nr;
1100 int point = sensor_attr->index;
1102 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1105 static ssize_t set_auto_temp(struct device *dev,
1106 struct device_attribute *attr, const char *buf, size_t count)
1108 struct it87_data *data = dev_get_drvdata(dev);
1109 struct sensor_device_attribute_2 *sensor_attr =
1110 to_sensor_dev_attr_2(attr);
1111 int nr = sensor_attr->nr;
1112 int point = sensor_attr->index;
1115 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1118 mutex_lock(&data->update_lock);
1119 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1120 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1121 data->auto_temp[nr][point]);
1122 mutex_unlock(&data->update_lock);
1126 #define show_fan_offset(offset) \
1127 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1128 show_fan, NULL, offset - 1); \
1129 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1130 show_fan_min, set_fan_min, offset - 1); \
1131 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1132 show_fan_div, set_fan_div, offset - 1);
1138 #define show_pwm_offset(offset) \
1139 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1140 show_pwm_enable, set_pwm_enable, offset - 1); \
1141 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1142 show_pwm, set_pwm, offset - 1); \
1143 static DEVICE_ATTR(pwm##offset##_freq, \
1144 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1145 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1146 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1147 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1149 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1150 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1152 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1153 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1155 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1156 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1158 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1159 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1160 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1161 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1163 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1164 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1166 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1167 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1169 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1170 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1172 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1173 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1180 /* A different set of callbacks for 16-bit fans */
1181 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1184 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1185 int nr = sensor_attr->index;
1186 struct it87_data *data = it87_update_device(dev);
1187 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1190 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1193 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1194 int nr = sensor_attr->index;
1195 struct it87_data *data = it87_update_device(dev);
1196 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1199 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1200 const char *buf, size_t count)
1202 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1203 int nr = sensor_attr->index;
1204 struct it87_data *data = dev_get_drvdata(dev);
1207 if (kstrtol(buf, 10, &val) < 0)
1210 mutex_lock(&data->update_lock);
1211 data->fan_min[nr] = FAN16_TO_REG(val);
1212 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1213 data->fan_min[nr] & 0xff);
1214 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1215 data->fan_min[nr] >> 8);
1216 mutex_unlock(&data->update_lock);
1221 * We want to use the same sysfs file names as 8-bit fans, but we need
1222 * different variable names, so we have to use SENSOR_ATTR instead of
1223 * SENSOR_DEVICE_ATTR.
1225 #define show_fan16_offset(offset) \
1226 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1227 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1228 show_fan16, NULL, offset - 1); \
1229 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1230 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1231 show_fan16_min, set_fan16_min, offset - 1)
1233 show_fan16_offset(1);
1234 show_fan16_offset(2);
1235 show_fan16_offset(3);
1236 show_fan16_offset(4);
1237 show_fan16_offset(5);
1240 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1243 struct it87_data *data = it87_update_device(dev);
1244 return sprintf(buf, "%u\n", data->alarms);
1246 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1248 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1251 int bitnr = to_sensor_dev_attr(attr)->index;
1252 struct it87_data *data = it87_update_device(dev);
1253 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1256 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1257 *attr, const char *buf, size_t count)
1259 struct it87_data *data = dev_get_drvdata(dev);
1263 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1266 mutex_lock(&data->update_lock);
1267 config = it87_read_value(data, IT87_REG_CONFIG);
1272 it87_write_value(data, IT87_REG_CONFIG, config);
1273 /* Invalidate cache to force re-read */
1276 mutex_unlock(&data->update_lock);
1281 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1282 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1283 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1284 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1285 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1286 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1287 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1288 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1289 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1290 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1291 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1292 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1293 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1294 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1295 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1296 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1297 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1298 show_alarm, clear_intrusion, 4);
1300 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1303 int bitnr = to_sensor_dev_attr(attr)->index;
1304 struct it87_data *data = it87_update_device(dev);
1305 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1307 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1308 const char *buf, size_t count)
1310 int bitnr = to_sensor_dev_attr(attr)->index;
1311 struct it87_data *data = dev_get_drvdata(dev);
1314 if (kstrtol(buf, 10, &val) < 0
1315 || (val != 0 && val != 1))
1318 mutex_lock(&data->update_lock);
1319 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1321 data->beeps |= (1 << bitnr);
1323 data->beeps &= ~(1 << bitnr);
1324 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1325 mutex_unlock(&data->update_lock);
1329 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1330 show_beep, set_beep, 1);
1331 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1332 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1333 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1334 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1335 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1336 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1337 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1338 /* fanX_beep writability is set later */
1339 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1340 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1341 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1342 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1343 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1344 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1345 show_beep, set_beep, 2);
1346 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1347 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1349 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1352 struct it87_data *data = dev_get_drvdata(dev);
1353 return sprintf(buf, "%u\n", data->vrm);
1355 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1356 const char *buf, size_t count)
1358 struct it87_data *data = dev_get_drvdata(dev);
1361 if (kstrtoul(buf, 10, &val) < 0)
1368 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1370 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1373 struct it87_data *data = it87_update_device(dev);
1374 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1376 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1378 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1381 static const char * const labels[] = {
1386 static const char * const labels_it8721[] = {
1391 struct it87_data *data = dev_get_drvdata(dev);
1392 int nr = to_sensor_dev_attr(attr)->index;
1394 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1397 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1398 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1399 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1401 static ssize_t show_name(struct device *dev, struct device_attribute
1402 *devattr, char *buf)
1404 struct it87_data *data = dev_get_drvdata(dev);
1405 return sprintf(buf, "%s\n", data->name);
1407 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1409 static struct attribute *it87_attributes[] = {
1410 &sensor_dev_attr_in0_input.dev_attr.attr,
1411 &sensor_dev_attr_in1_input.dev_attr.attr,
1412 &sensor_dev_attr_in2_input.dev_attr.attr,
1413 &sensor_dev_attr_in3_input.dev_attr.attr,
1414 &sensor_dev_attr_in4_input.dev_attr.attr,
1415 &sensor_dev_attr_in5_input.dev_attr.attr,
1416 &sensor_dev_attr_in6_input.dev_attr.attr,
1417 &sensor_dev_attr_in7_input.dev_attr.attr,
1418 &sensor_dev_attr_in8_input.dev_attr.attr,
1419 &sensor_dev_attr_in0_min.dev_attr.attr,
1420 &sensor_dev_attr_in1_min.dev_attr.attr,
1421 &sensor_dev_attr_in2_min.dev_attr.attr,
1422 &sensor_dev_attr_in3_min.dev_attr.attr,
1423 &sensor_dev_attr_in4_min.dev_attr.attr,
1424 &sensor_dev_attr_in5_min.dev_attr.attr,
1425 &sensor_dev_attr_in6_min.dev_attr.attr,
1426 &sensor_dev_attr_in7_min.dev_attr.attr,
1427 &sensor_dev_attr_in0_max.dev_attr.attr,
1428 &sensor_dev_attr_in1_max.dev_attr.attr,
1429 &sensor_dev_attr_in2_max.dev_attr.attr,
1430 &sensor_dev_attr_in3_max.dev_attr.attr,
1431 &sensor_dev_attr_in4_max.dev_attr.attr,
1432 &sensor_dev_attr_in5_max.dev_attr.attr,
1433 &sensor_dev_attr_in6_max.dev_attr.attr,
1434 &sensor_dev_attr_in7_max.dev_attr.attr,
1435 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1436 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1437 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1438 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1439 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1440 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1441 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1442 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1444 &sensor_dev_attr_temp1_input.dev_attr.attr,
1445 &sensor_dev_attr_temp2_input.dev_attr.attr,
1446 &sensor_dev_attr_temp3_input.dev_attr.attr,
1447 &sensor_dev_attr_temp1_max.dev_attr.attr,
1448 &sensor_dev_attr_temp2_max.dev_attr.attr,
1449 &sensor_dev_attr_temp3_max.dev_attr.attr,
1450 &sensor_dev_attr_temp1_min.dev_attr.attr,
1451 &sensor_dev_attr_temp2_min.dev_attr.attr,
1452 &sensor_dev_attr_temp3_min.dev_attr.attr,
1453 &sensor_dev_attr_temp1_type.dev_attr.attr,
1454 &sensor_dev_attr_temp2_type.dev_attr.attr,
1455 &sensor_dev_attr_temp3_type.dev_attr.attr,
1456 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1457 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1458 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1460 &dev_attr_alarms.attr,
1461 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1462 &dev_attr_name.attr,
1466 static const struct attribute_group it87_group = {
1467 .attrs = it87_attributes,
1470 static struct attribute *it87_attributes_beep[] = {
1471 &sensor_dev_attr_in0_beep.dev_attr.attr,
1472 &sensor_dev_attr_in1_beep.dev_attr.attr,
1473 &sensor_dev_attr_in2_beep.dev_attr.attr,
1474 &sensor_dev_attr_in3_beep.dev_attr.attr,
1475 &sensor_dev_attr_in4_beep.dev_attr.attr,
1476 &sensor_dev_attr_in5_beep.dev_attr.attr,
1477 &sensor_dev_attr_in6_beep.dev_attr.attr,
1478 &sensor_dev_attr_in7_beep.dev_attr.attr,
1480 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1481 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1482 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1486 static const struct attribute_group it87_group_beep = {
1487 .attrs = it87_attributes_beep,
1490 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1491 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1492 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1493 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1496 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1497 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1498 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1501 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1502 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1503 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1506 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1507 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1508 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1511 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1512 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1513 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1517 static const struct attribute_group it87_group_fan16[5] = {
1518 { .attrs = it87_attributes_fan16[0] },
1519 { .attrs = it87_attributes_fan16[1] },
1520 { .attrs = it87_attributes_fan16[2] },
1521 { .attrs = it87_attributes_fan16[3] },
1522 { .attrs = it87_attributes_fan16[4] },
1525 static struct attribute *it87_attributes_fan[3][4+1] = { {
1526 &sensor_dev_attr_fan1_input.dev_attr.attr,
1527 &sensor_dev_attr_fan1_min.dev_attr.attr,
1528 &sensor_dev_attr_fan1_div.dev_attr.attr,
1529 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1532 &sensor_dev_attr_fan2_input.dev_attr.attr,
1533 &sensor_dev_attr_fan2_min.dev_attr.attr,
1534 &sensor_dev_attr_fan2_div.dev_attr.attr,
1535 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1538 &sensor_dev_attr_fan3_input.dev_attr.attr,
1539 &sensor_dev_attr_fan3_min.dev_attr.attr,
1540 &sensor_dev_attr_fan3_div.dev_attr.attr,
1541 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1545 static const struct attribute_group it87_group_fan[3] = {
1546 { .attrs = it87_attributes_fan[0] },
1547 { .attrs = it87_attributes_fan[1] },
1548 { .attrs = it87_attributes_fan[2] },
1551 static const struct attribute_group *
1552 it87_get_fan_group(const struct it87_data *data)
1554 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1557 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1558 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1559 &sensor_dev_attr_pwm1.dev_attr.attr,
1560 &dev_attr_pwm1_freq.attr,
1561 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1564 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1565 &sensor_dev_attr_pwm2.dev_attr.attr,
1566 &dev_attr_pwm2_freq.attr,
1567 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1570 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1571 &sensor_dev_attr_pwm3.dev_attr.attr,
1572 &dev_attr_pwm3_freq.attr,
1573 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1577 static const struct attribute_group it87_group_pwm[3] = {
1578 { .attrs = it87_attributes_pwm[0] },
1579 { .attrs = it87_attributes_pwm[1] },
1580 { .attrs = it87_attributes_pwm[2] },
1583 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1584 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1585 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1586 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1587 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1588 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1589 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1590 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1591 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1592 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1595 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1596 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1597 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1598 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1599 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1600 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1601 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1602 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1603 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1606 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1607 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1608 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1609 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1610 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1611 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1612 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1613 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1614 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1618 static const struct attribute_group it87_group_autopwm[3] = {
1619 { .attrs = it87_attributes_autopwm[0] },
1620 { .attrs = it87_attributes_autopwm[1] },
1621 { .attrs = it87_attributes_autopwm[2] },
1624 static struct attribute *it87_attributes_fan_beep[] = {
1625 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1626 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1627 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1628 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1629 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1632 static struct attribute *it87_attributes_vid[] = {
1634 &dev_attr_cpu0_vid.attr,
1638 static const struct attribute_group it87_group_vid = {
1639 .attrs = it87_attributes_vid,
1642 static struct attribute *it87_attributes_label[] = {
1643 &sensor_dev_attr_in3_label.dev_attr.attr,
1644 &sensor_dev_attr_in7_label.dev_attr.attr,
1645 &sensor_dev_attr_in8_label.dev_attr.attr,
1649 static const struct attribute_group it87_group_label = {
1650 .attrs = it87_attributes_label,
1653 /* SuperIO detection - will change isa_address if a chip is found */
1654 static int __init it87_find(unsigned short *address,
1655 struct it87_sio_data *sio_data)
1659 const char *board_vendor, *board_name;
1661 err = superio_enter();
1666 chip_type = force_id ? force_id : superio_inw(DEVID);
1668 switch (chip_type) {
1670 sio_data->type = it87;
1673 sio_data->type = it8712;
1677 sio_data->type = it8716;
1680 sio_data->type = it8718;
1683 sio_data->type = it8720;
1686 sio_data->type = it8721;
1689 sio_data->type = it8728;
1692 sio_data->type = it8771;
1695 sio_data->type = it8772;
1698 sio_data->type = it8783;
1700 case 0xffff: /* No device at all */
1703 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1707 superio_select(PME);
1708 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1709 pr_info("Device not activated, skipping\n");
1713 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1714 if (*address == 0) {
1715 pr_info("Base address not set, skipping\n");
1720 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1721 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1722 chip_type, *address, sio_data->revision);
1724 /* in8 (Vbat) is always internal */
1725 sio_data->internal = (1 << 2);
1727 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1728 if (sio_data->type == it87) {
1729 /* The IT8705F doesn't have VID pins at all */
1730 sio_data->skip_vid = 1;
1732 /* The IT8705F has a different LD number for GPIO */
1734 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1735 } else if (sio_data->type == it8783) {
1736 int reg25, reg27, reg2A, reg2C, regEF;
1738 sio_data->skip_vid = 1; /* No VID */
1740 superio_select(GPIO);
1742 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1743 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1744 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1745 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1746 regEF = superio_inb(IT87_SIO_SPI_REG);
1748 /* Check if fan3 is there or not */
1749 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1750 sio_data->skip_fan |= (1 << 2);
1751 if ((reg25 & (1 << 4))
1752 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1753 sio_data->skip_pwm |= (1 << 2);
1755 /* Check if fan2 is there or not */
1756 if (reg27 & (1 << 7))
1757 sio_data->skip_fan |= (1 << 1);
1758 if (reg27 & (1 << 3))
1759 sio_data->skip_pwm |= (1 << 1);
1762 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1766 if ((reg27 & (1 << 1)) || (reg2C & (1 << 2)))
1770 if ((reg27 & (1 << 2)) || (reg2C & (1 << 2))) {
1772 * If the external VIN7 pin is disabled, route it to the
1773 * internal VCCH5V if that is not already done.
1775 if (!(reg2C & (1 << 1))) {
1777 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1778 pr_notice("Routing internal VCCH to in7\n");
1782 if (reg2C & (1 << 0))
1783 sio_data->internal |= (1 << 0);
1784 if (reg2C & (1 << 1))
1785 sio_data->internal |= (1 << 1);
1787 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1792 superio_select(GPIO);
1794 reg = superio_inb(IT87_SIO_GPIO3_REG);
1795 if (sio_data->type == it8721 || sio_data->type == it8728
1796 || sio_data->type == it8771 || sio_data->type == it8772) {
1798 * The IT8721F/IT8758E don't have VID pins at all,
1799 * not sure about the IT8728F, IT8771E, IT8772E.
1801 sio_data->skip_vid = 1;
1803 /* We need at least 4 VID pins */
1805 pr_info("VID is disabled (pins used for GPIO)\n");
1806 sio_data->skip_vid = 1;
1810 /* Check if fan3 is there or not */
1812 sio_data->skip_pwm |= (1 << 2);
1814 sio_data->skip_fan |= (1 << 2);
1816 /* Check if fan2 is there or not */
1817 reg = superio_inb(IT87_SIO_GPIO5_REG);
1819 sio_data->skip_pwm |= (1 << 1);
1821 sio_data->skip_fan |= (1 << 1);
1823 if ((sio_data->type == it8718 || sio_data->type == it8720)
1824 && !(sio_data->skip_vid))
1825 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1827 reg = superio_inb(IT87_SIO_PINX2_REG);
1829 * The IT8720F has no VIN7 pin, so VCCH should always be
1830 * routed internally to VIN7 with an internal divider.
1831 * Curiously, there still is a configuration bit to control
1832 * this, which means it can be set incorrectly. And even
1833 * more curiously, many boards out there are improperly
1834 * configured, even though the IT8720F datasheet claims
1835 * that the internal routing of VCCH to VIN7 is the default
1836 * setting. So we force the internal routing in this case.
1838 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1840 superio_outb(IT87_SIO_PINX2_REG, reg);
1841 pr_notice("Routing internal VCCH to in7\n");
1844 sio_data->internal |= (1 << 0);
1845 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1846 sio_data->type == it8728 || sio_data->type == it8771 ||
1847 sio_data->type == it8772 )
1848 sio_data->internal |= (1 << 1);
1850 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1852 if (sio_data->beep_pin)
1853 pr_info("Beeping is supported\n");
1855 /* Disable specific features based on DMI strings */
1856 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1857 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1858 if (board_vendor && board_name) {
1859 if (strcmp(board_vendor, "nVIDIA") == 0
1860 && strcmp(board_name, "FN68PT") == 0) {
1862 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1863 * connected to a fan, but to something else. One user
1864 * has reported instant system power-off when changing
1865 * the PWM2 duty cycle, so we disable it.
1866 * I use the board name string as the trigger in case
1867 * the same board is ever used in other systems.
1869 pr_info("Disabling pwm2 due to hardware constraints\n");
1870 sio_data->skip_pwm = (1 << 1);
1879 static void it87_remove_files(struct device *dev)
1881 struct it87_data *data = platform_get_drvdata(pdev);
1882 struct it87_sio_data *sio_data = dev->platform_data;
1883 const struct attribute_group *fan_group = it87_get_fan_group(data);
1886 sysfs_remove_group(&dev->kobj, &it87_group);
1887 if (sio_data->beep_pin)
1888 sysfs_remove_group(&dev->kobj, &it87_group_beep);
1889 for (i = 0; i < 5; i++) {
1890 if (!(data->has_fan & (1 << i)))
1892 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1893 if (sio_data->beep_pin)
1894 sysfs_remove_file(&dev->kobj,
1895 it87_attributes_fan_beep[i]);
1897 for (i = 0; i < 3; i++) {
1898 if (sio_data->skip_pwm & (1 << 0))
1900 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1901 if (has_old_autopwm(data))
1902 sysfs_remove_group(&dev->kobj,
1903 &it87_group_autopwm[i]);
1905 if (!sio_data->skip_vid)
1906 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1907 sysfs_remove_group(&dev->kobj, &it87_group_label);
1910 static int __devinit it87_probe(struct platform_device *pdev)
1912 struct it87_data *data;
1913 struct resource *res;
1914 struct device *dev = &pdev->dev;
1915 struct it87_sio_data *sio_data = dev->platform_data;
1916 const struct attribute_group *fan_group;
1918 int enable_pwm_interface;
1919 int fan_beep_need_rw;
1920 static const char * const names[] = {
1933 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1934 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1935 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1936 (unsigned long)res->start,
1937 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1942 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1948 data->addr = res->start;
1949 data->type = sio_data->type;
1950 data->revision = sio_data->revision;
1951 data->name = names[sio_data->type];
1953 /* Now, we do the remaining detection. */
1954 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1955 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1960 platform_set_drvdata(pdev, data);
1962 mutex_init(&data->update_lock);
1964 /* Check PWM configuration */
1965 enable_pwm_interface = it87_check_pwm(dev);
1967 /* Starting with IT8721F, we handle scaling of internal voltages */
1968 if (has_12mv_adc(data)) {
1969 if (sio_data->internal & (1 << 0))
1970 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1971 if (sio_data->internal & (1 << 1))
1972 data->in_scaled |= (1 << 7); /* in7 is VSB */
1973 if (sio_data->internal & (1 << 2))
1974 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1975 } else if (sio_data->type == it8783) {
1976 if (sio_data->internal & (1 << 0))
1977 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1978 if (sio_data->internal & (1 << 1))
1979 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
1982 /* Initialize the IT87 chip */
1983 it87_init_device(pdev);
1985 /* Register sysfs hooks */
1986 err = sysfs_create_group(&dev->kobj, &it87_group);
1990 if (sio_data->beep_pin) {
1991 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1996 /* Do not create fan files for disabled fans */
1997 fan_group = it87_get_fan_group(data);
1998 fan_beep_need_rw = 1;
1999 for (i = 0; i < 5; i++) {
2000 if (!(data->has_fan & (1 << i)))
2002 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2006 if (sio_data->beep_pin) {
2007 err = sysfs_create_file(&dev->kobj,
2008 it87_attributes_fan_beep[i]);
2011 if (!fan_beep_need_rw)
2015 * As we have a single beep enable bit for all fans,
2016 * only the first enabled fan has a writable attribute
2019 if (sysfs_chmod_file(&dev->kobj,
2020 it87_attributes_fan_beep[i],
2022 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2024 fan_beep_need_rw = 0;
2028 if (enable_pwm_interface) {
2029 for (i = 0; i < 3; i++) {
2030 if (sio_data->skip_pwm & (1 << i))
2032 err = sysfs_create_group(&dev->kobj,
2033 &it87_group_pwm[i]);
2037 if (!has_old_autopwm(data))
2039 err = sysfs_create_group(&dev->kobj,
2040 &it87_group_autopwm[i]);
2046 if (!sio_data->skip_vid) {
2047 data->vrm = vid_which_vrm();
2048 /* VID reading from Super-I/O config space if available */
2049 data->vid = sio_data->vid_value;
2050 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2055 /* Export labels for internal sensors */
2056 for (i = 0; i < 3; i++) {
2057 if (!(sio_data->internal & (1 << i)))
2059 err = sysfs_create_file(&dev->kobj,
2060 it87_attributes_label[i]);
2065 data->hwmon_dev = hwmon_device_register(dev);
2066 if (IS_ERR(data->hwmon_dev)) {
2067 err = PTR_ERR(data->hwmon_dev);
2074 it87_remove_files(dev);
2076 platform_set_drvdata(pdev, NULL);
2079 release_region(res->start, IT87_EC_EXTENT);
2084 static int __devexit it87_remove(struct platform_device *pdev)
2086 struct it87_data *data = platform_get_drvdata(pdev);
2088 hwmon_device_unregister(data->hwmon_dev);
2089 it87_remove_files(&pdev->dev);
2091 release_region(data->addr, IT87_EC_EXTENT);
2092 platform_set_drvdata(pdev, NULL);
2099 * Must be called with data->update_lock held, except during initialization.
2100 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2101 * would slow down the IT87 access and should not be necessary.
2103 static int it87_read_value(struct it87_data *data, u8 reg)
2105 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2106 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2110 * Must be called with data->update_lock held, except during initialization.
2111 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2112 * would slow down the IT87 access and should not be necessary.
2114 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2116 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2117 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2120 /* Return 1 if and only if the PWM interface is safe to use */
2121 static int __devinit it87_check_pwm(struct device *dev)
2123 struct it87_data *data = dev_get_drvdata(dev);
2125 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2126 * and polarity set to active low is sign that this is the case so we
2127 * disable pwm control to protect the user.
2129 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2130 if ((tmp & 0x87) == 0) {
2131 if (fix_pwm_polarity) {
2133 * The user asks us to attempt a chip reconfiguration.
2134 * This means switching to active high polarity and
2135 * inverting all fan speed values.
2140 for (i = 0; i < 3; i++)
2141 pwm[i] = it87_read_value(data,
2145 * If any fan is in automatic pwm mode, the polarity
2146 * might be correct, as suspicious as it seems, so we
2147 * better don't change anything (but still disable the
2150 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2151 dev_info(dev, "Reconfiguring PWM to "
2152 "active high polarity\n");
2153 it87_write_value(data, IT87_REG_FAN_CTL,
2155 for (i = 0; i < 3; i++)
2156 it87_write_value(data,
2162 dev_info(dev, "PWM configuration is "
2163 "too broken to be fixed\n");
2166 dev_info(dev, "Detected broken BIOS "
2167 "defaults, disabling PWM interface\n");
2169 } else if (fix_pwm_polarity) {
2170 dev_info(dev, "PWM configuration looks "
2171 "sane, won't touch\n");
2177 /* Called when we have found a new IT87. */
2178 static void __devinit it87_init_device(struct platform_device *pdev)
2180 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2181 struct it87_data *data = platform_get_drvdata(pdev);
2186 * For each PWM channel:
2187 * - If it is in automatic mode, setting to manual mode should set
2188 * the fan to full speed by default.
2189 * - If it is in manual mode, we need a mapping to temperature
2190 * channels to use when later setting to automatic mode later.
2191 * Use a 1:1 mapping by default (we are clueless.)
2192 * In both cases, the value can (and should) be changed by the user
2193 * prior to switching to a different mode.
2194 * Note that this is no longer needed for the IT8721F and later, as
2195 * these have separate registers for the temperature mapping and the
2196 * manual duty cycle.
2198 for (i = 0; i < 3; i++) {
2199 data->pwm_temp_map[i] = i;
2200 data->pwm_duty[i] = 0x7f; /* Full speed */
2201 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2205 * Some chips seem to have default value 0xff for all limit
2206 * registers. For low voltage limits it makes no sense and triggers
2207 * alarms, so change to 0 instead. For high temperature limits, it
2208 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2209 * but is still confusing, so change to 127 degrees C.
2211 for (i = 0; i < 8; i++) {
2212 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2214 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2216 for (i = 0; i < 3; i++) {
2217 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2219 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2223 * Temperature channels are not forcibly enabled, as they can be
2224 * set to two different sensor types and we can't guess which one
2225 * is correct for a given system. These channels can be enabled at
2226 * run-time through the temp{1-3}_type sysfs accessors if needed.
2229 /* Check if voltage monitors are reset manually or by some reason */
2230 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2231 if ((tmp & 0xff) == 0) {
2232 /* Enable all voltage monitors */
2233 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2236 /* Check if tachometers are reset manually or by some reason */
2237 mask = 0x70 & ~(sio_data->skip_fan << 4);
2238 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2239 if ((data->fan_main_ctrl & mask) == 0) {
2240 /* Enable all fan tachometers */
2241 data->fan_main_ctrl |= mask;
2242 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2243 data->fan_main_ctrl);
2245 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2247 /* Set tachometers to 16-bit mode if needed */
2248 if (has_16bit_fans(data)) {
2249 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2250 if (~tmp & 0x07 & data->has_fan) {
2252 "Setting fan1-3 to 16-bit mode\n");
2253 it87_write_value(data, IT87_REG_FAN_16BIT,
2256 /* IT8705F only supports three fans. */
2257 if (data->type != it87) {
2259 data->has_fan |= (1 << 3); /* fan4 enabled */
2261 data->has_fan |= (1 << 4); /* fan5 enabled */
2265 /* Fan input pins may be used for alternative functions */
2266 data->has_fan &= ~sio_data->skip_fan;
2268 /* Start monitoring */
2269 it87_write_value(data, IT87_REG_CONFIG,
2270 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
2271 | (update_vbat ? 0x41 : 0x01));
2274 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2276 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2277 if (has_newer_autopwm(data)) {
2278 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2279 data->pwm_duty[nr] = it87_read_value(data,
2280 IT87_REG_PWM_DUTY(nr));
2282 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2283 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2284 else /* Manual mode */
2285 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2288 if (has_old_autopwm(data)) {
2291 for (i = 0; i < 5 ; i++)
2292 data->auto_temp[nr][i] = it87_read_value(data,
2293 IT87_REG_AUTO_TEMP(nr, i));
2294 for (i = 0; i < 3 ; i++)
2295 data->auto_pwm[nr][i] = it87_read_value(data,
2296 IT87_REG_AUTO_PWM(nr, i));
2300 static struct it87_data *it87_update_device(struct device *dev)
2302 struct it87_data *data = dev_get_drvdata(dev);
2305 mutex_lock(&data->update_lock);
2307 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2311 * Cleared after each update, so reenable. Value
2312 * returned by this read will be previous value
2314 it87_write_value(data, IT87_REG_CONFIG,
2315 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2317 for (i = 0; i <= 7; i++) {
2319 it87_read_value(data, IT87_REG_VIN(i));
2321 it87_read_value(data, IT87_REG_VIN_MIN(i));
2323 it87_read_value(data, IT87_REG_VIN_MAX(i));
2325 /* in8 (battery) has no limit registers */
2326 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
2328 for (i = 0; i < 5; i++) {
2329 /* Skip disabled fans */
2330 if (!(data->has_fan & (1 << i)))
2334 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2335 data->fan[i] = it87_read_value(data,
2337 /* Add high byte if in 16-bit mode */
2338 if (has_16bit_fans(data)) {
2339 data->fan[i] |= it87_read_value(data,
2340 IT87_REG_FANX[i]) << 8;
2341 data->fan_min[i] |= it87_read_value(data,
2342 IT87_REG_FANX_MIN[i]) << 8;
2345 for (i = 0; i < 3; i++) {
2347 it87_read_value(data, IT87_REG_TEMP(i));
2348 data->temp_high[i] =
2349 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2351 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2354 /* Newer chips don't have clock dividers */
2355 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2356 i = it87_read_value(data, IT87_REG_FAN_DIV);
2357 data->fan_div[0] = i & 0x07;
2358 data->fan_div[1] = (i >> 3) & 0x07;
2359 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2363 it87_read_value(data, IT87_REG_ALARM1) |
2364 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2365 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2366 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2368 data->fan_main_ctrl = it87_read_value(data,
2369 IT87_REG_FAN_MAIN_CTRL);
2370 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2371 for (i = 0; i < 3; i++)
2372 it87_update_pwm_ctrl(data, i);
2374 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2376 * The IT8705F does not have VID capability.
2377 * The IT8718F and later don't use IT87_REG_VID for the
2380 if (data->type == it8712 || data->type == it8716) {
2381 data->vid = it87_read_value(data, IT87_REG_VID);
2383 * The older IT8712F revisions had only 5 VID pins,
2384 * but we assume it is always safe to read 6 bits.
2388 data->last_updated = jiffies;
2392 mutex_unlock(&data->update_lock);
2397 static int __init it87_device_add(unsigned short address,
2398 const struct it87_sio_data *sio_data)
2400 struct resource res = {
2401 .start = address + IT87_EC_OFFSET,
2402 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2404 .flags = IORESOURCE_IO,
2408 err = acpi_check_resource_conflict(&res);
2412 pdev = platform_device_alloc(DRVNAME, address);
2415 pr_err("Device allocation failed\n");
2419 err = platform_device_add_resources(pdev, &res, 1);
2421 pr_err("Device resource addition failed (%d)\n", err);
2422 goto exit_device_put;
2425 err = platform_device_add_data(pdev, sio_data,
2426 sizeof(struct it87_sio_data));
2428 pr_err("Platform data allocation failed\n");
2429 goto exit_device_put;
2432 err = platform_device_add(pdev);
2434 pr_err("Device addition failed (%d)\n", err);
2435 goto exit_device_put;
2441 platform_device_put(pdev);
2446 static int __init sm_it87_init(void)
2449 unsigned short isa_address = 0;
2450 struct it87_sio_data sio_data;
2452 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2453 err = it87_find(&isa_address, &sio_data);
2456 err = platform_driver_register(&it87_driver);
2460 err = it87_device_add(isa_address, &sio_data);
2462 platform_driver_unregister(&it87_driver);
2469 static void __exit sm_it87_exit(void)
2471 platform_device_unregister(pdev);
2472 platform_driver_unregister(&it87_driver);
2476 MODULE_AUTHOR("Chris Gauthron, "
2477 "Jean Delvare <khali@linux-fr.org>");
2478 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2479 module_param(update_vbat, bool, 0);
2480 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2481 module_param(fix_pwm_polarity, bool, 0);
2482 MODULE_PARM_DESC(fix_pwm_polarity,
2483 "Force PWM polarity to active high (DANGEROUS)");
2484 MODULE_LICENSE("GPL");
2486 module_init(sm_it87_init);
2487 module_exit(sm_it87_exit);