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 * IT8783E/F Super I/O chip w/LPC interface
23 * Sis950 A clone of the IT8705F
25 * Copyright (C) 2001 Chris Gauthron
26 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/slab.h>
48 #include <linux/jiffies.h>
49 #include <linux/platform_device.h>
50 #include <linux/hwmon.h>
51 #include <linux/hwmon-sysfs.h>
52 #include <linux/hwmon-vid.h>
53 #include <linux/err.h>
54 #include <linux/mutex.h>
55 #include <linux/sysfs.h>
56 #include <linux/string.h>
57 #include <linux/dmi.h>
58 #include <linux/acpi.h>
61 #define DRVNAME "it87"
63 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8783 };
65 static unsigned short force_id;
66 module_param(force_id, ushort, 0);
67 MODULE_PARM_DESC(force_id, "Override the detected device ID");
69 static struct platform_device *pdev;
71 #define REG 0x2e /* The register to read/write */
72 #define DEV 0x07 /* Register: Logical device select */
73 #define VAL 0x2f /* The value to read/write */
74 #define PME 0x04 /* The device with the fan registers in it */
76 /* The device with the IT8718F/IT8720F VID value in it */
79 #define DEVID 0x20 /* Register: Device ID */
80 #define DEVREV 0x22 /* Register: Device Revision */
82 static inline int superio_inb(int reg)
88 static inline void superio_outb(int reg, int val)
94 static int superio_inw(int reg)
104 static inline void superio_select(int ldn)
110 static inline int superio_enter(void)
113 * Try to reserve REG and REG + 1 for exclusive access.
115 if (!request_muxed_region(REG, 2, DRVNAME))
125 static inline void superio_exit(void)
129 release_region(REG, 2);
132 /* Logical device 4 registers */
133 #define IT8712F_DEVID 0x8712
134 #define IT8705F_DEVID 0x8705
135 #define IT8716F_DEVID 0x8716
136 #define IT8718F_DEVID 0x8718
137 #define IT8720F_DEVID 0x8720
138 #define IT8721F_DEVID 0x8721
139 #define IT8726F_DEVID 0x8726
140 #define IT8728F_DEVID 0x8728
141 #define IT8783E_DEVID 0x8783
142 #define IT87_ACT_REG 0x30
143 #define IT87_BASE_REG 0x60
145 /* Logical device 7 registers (IT8712F and later) */
146 #define IT87_SIO_GPIO1_REG 0x25
147 #define IT87_SIO_GPIO3_REG 0x27
148 #define IT87_SIO_GPIO5_REG 0x29
149 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
150 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
151 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
152 #define IT87_SIO_VID_REG 0xfc /* VID value */
153 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
155 /* Update battery voltage after every reading if true */
156 static bool update_vbat;
158 /* Not all BIOSes properly configure the PWM registers */
159 static bool fix_pwm_polarity;
161 /* Many IT87 constants specified below */
163 /* Length of ISA address segment */
164 #define IT87_EXTENT 8
166 /* Length of ISA address segment for Environmental Controller */
167 #define IT87_EC_EXTENT 2
169 /* Offset of EC registers from ISA base address */
170 #define IT87_EC_OFFSET 5
172 /* Where are the ISA address/data registers relative to the EC base address */
173 #define IT87_ADDR_REG_OFFSET 0
174 #define IT87_DATA_REG_OFFSET 1
176 /*----- The IT87 registers -----*/
178 #define IT87_REG_CONFIG 0x00
180 #define IT87_REG_ALARM1 0x01
181 #define IT87_REG_ALARM2 0x02
182 #define IT87_REG_ALARM3 0x03
185 * The IT8718F and IT8720F have the VID value in a different register, in
186 * Super-I/O configuration space.
188 #define IT87_REG_VID 0x0a
190 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
191 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
194 #define IT87_REG_FAN_DIV 0x0b
195 #define IT87_REG_FAN_16BIT 0x0c
197 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
199 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
200 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
201 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
202 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
203 #define IT87_REG_FAN_MAIN_CTRL 0x13
204 #define IT87_REG_FAN_CTL 0x14
205 #define IT87_REG_PWM(nr) (0x15 + (nr))
206 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
208 #define IT87_REG_VIN(nr) (0x20 + (nr))
209 #define IT87_REG_TEMP(nr) (0x29 + (nr))
211 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
212 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
213 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
214 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
216 #define IT87_REG_VIN_ENABLE 0x50
217 #define IT87_REG_TEMP_ENABLE 0x51
218 #define IT87_REG_BEEP_ENABLE 0x5c
220 #define IT87_REG_CHIPID 0x58
222 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
223 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
226 struct it87_sio_data {
228 /* Values read from Super-I/O config space */
232 u8 internal; /* Internal sensors can be labeled */
233 /* Features skipped based on config or DMI */
240 * For each registered chip, we need to keep some data in memory.
241 * The structure is dynamically allocated.
244 struct device *hwmon_dev;
250 struct mutex update_lock;
251 char valid; /* !=0 if following fields are valid */
252 unsigned long last_updated; /* In jiffies */
254 u16 in_scaled; /* Internal voltage sensors are scaled */
255 u8 in[9]; /* Register value */
256 u8 in_max[8]; /* Register value */
257 u8 in_min[8]; /* Register value */
258 u8 has_fan; /* Bitfield, fans enabled */
259 u16 fan[5]; /* Register values, possibly combined */
260 u16 fan_min[5]; /* Register values, possibly combined */
261 s8 temp[3]; /* Register value */
262 s8 temp_high[3]; /* Register value */
263 s8 temp_low[3]; /* Register value */
264 u8 sensor; /* Register value */
265 u8 fan_div[3]; /* Register encoding, shifted right */
266 u8 vid; /* Register encoding, combined */
268 u32 alarms; /* Register encoding, combined */
269 u8 beeps; /* Register encoding */
270 u8 fan_main_ctrl; /* Register value */
271 u8 fan_ctl; /* Register value */
274 * The following 3 arrays correspond to the same registers up to
275 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
276 * 7, and we want to preserve settings on mode changes, so we have
277 * to track all values separately.
278 * Starting with the IT8721F, the manual PWM duty cycles are stored
279 * in separate registers (8-bit values), so the separate tracking
280 * is no longer needed, but it is still done to keep the driver
283 u8 pwm_ctrl[3]; /* Register value */
284 u8 pwm_duty[3]; /* Manual PWM value set by user */
285 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
287 /* Automatic fan speed control registers */
288 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
289 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
292 static inline int has_12mv_adc(const struct it87_data *data)
295 * IT8721F and later have a 12 mV ADC, also with internal scaling
296 * on selected inputs.
298 return data->type == it8721
299 || data->type == it8728;
302 static inline int has_newer_autopwm(const struct it87_data *data)
305 * IT8721F and later have separate registers for the temperature
306 * mapping and the manual duty cycle.
308 return data->type == it8721
309 || data->type == it8728;
312 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
316 if (has_12mv_adc(data)) {
317 if (data->in_scaled & (1 << nr))
322 if (data->in_scaled & (1 << nr))
328 val = DIV_ROUND_CLOSEST(val, lsb);
329 return SENSORS_LIMIT(val, 0, 255);
332 static int in_from_reg(const struct it87_data *data, int nr, int val)
334 if (has_12mv_adc(data)) {
335 if (data->in_scaled & (1 << nr))
340 if (data->in_scaled & (1 << 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 == it8783;
427 static inline int has_old_autopwm(const struct it87_data *data)
430 * The old automatic fan speed control interface is implemented
431 * by IT8705F chips up to revision F and IT8712F chips up to
434 return (data->type == it87 && data->revision < 0x03)
435 || (data->type == it8712 && data->revision < 0x08);
438 static int it87_probe(struct platform_device *pdev);
439 static int __devexit it87_remove(struct platform_device *pdev);
441 static int it87_read_value(struct it87_data *data, u8 reg);
442 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
443 static struct it87_data *it87_update_device(struct device *dev);
444 static int it87_check_pwm(struct device *dev);
445 static void it87_init_device(struct platform_device *pdev);
448 static struct platform_driver it87_driver = {
450 .owner = THIS_MODULE,
454 .remove = __devexit_p(it87_remove),
457 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
460 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
461 int nr = sensor_attr->index;
463 struct it87_data *data = it87_update_device(dev);
464 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
467 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
470 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
471 int nr = sensor_attr->index;
473 struct it87_data *data = it87_update_device(dev);
474 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
477 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
480 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
481 int nr = sensor_attr->index;
483 struct it87_data *data = it87_update_device(dev);
484 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
487 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
488 const char *buf, size_t count)
490 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
491 int nr = sensor_attr->index;
493 struct it87_data *data = dev_get_drvdata(dev);
496 if (kstrtoul(buf, 10, &val) < 0)
499 mutex_lock(&data->update_lock);
500 data->in_min[nr] = in_to_reg(data, nr, val);
501 it87_write_value(data, IT87_REG_VIN_MIN(nr),
503 mutex_unlock(&data->update_lock);
506 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
507 const char *buf, size_t count)
509 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
510 int nr = sensor_attr->index;
512 struct it87_data *data = dev_get_drvdata(dev);
515 if (kstrtoul(buf, 10, &val) < 0)
518 mutex_lock(&data->update_lock);
519 data->in_max[nr] = in_to_reg(data, nr, val);
520 it87_write_value(data, IT87_REG_VIN_MAX(nr),
522 mutex_unlock(&data->update_lock);
526 #define show_in_offset(offset) \
527 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
528 show_in, NULL, offset);
530 #define limit_in_offset(offset) \
531 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
532 show_in_min, set_in_min, offset); \
533 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
534 show_in_max, set_in_max, offset);
555 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
558 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
559 int nr = sensor_attr->index;
561 struct it87_data *data = it87_update_device(dev);
562 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
564 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
570 struct it87_data *data = it87_update_device(dev);
571 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
573 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
576 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
577 int nr = sensor_attr->index;
579 struct it87_data *data = it87_update_device(dev);
580 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
582 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
583 const char *buf, size_t count)
585 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
586 int nr = sensor_attr->index;
588 struct it87_data *data = dev_get_drvdata(dev);
591 if (kstrtol(buf, 10, &val) < 0)
594 mutex_lock(&data->update_lock);
595 data->temp_high[nr] = TEMP_TO_REG(val);
596 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
597 mutex_unlock(&data->update_lock);
600 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
601 const char *buf, size_t count)
603 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
604 int nr = sensor_attr->index;
606 struct it87_data *data = dev_get_drvdata(dev);
609 if (kstrtol(buf, 10, &val) < 0)
612 mutex_lock(&data->update_lock);
613 data->temp_low[nr] = TEMP_TO_REG(val);
614 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
615 mutex_unlock(&data->update_lock);
618 #define show_temp_offset(offset) \
619 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
620 show_temp, NULL, offset - 1); \
621 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
622 show_temp_max, set_temp_max, offset - 1); \
623 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
624 show_temp_min, set_temp_min, offset - 1);
630 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
633 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
634 int nr = sensor_attr->index;
635 struct it87_data *data = it87_update_device(dev);
636 u8 reg = data->sensor; /* In case value is updated while used */
639 return sprintf(buf, "3\n"); /* thermal diode */
641 return sprintf(buf, "4\n"); /* thermistor */
642 return sprintf(buf, "0\n"); /* disabled */
644 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
645 const char *buf, size_t count)
647 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
648 int nr = sensor_attr->index;
650 struct it87_data *data = dev_get_drvdata(dev);
654 if (kstrtol(buf, 10, &val) < 0)
657 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
660 if (val == 2) { /* backwards compatibility */
661 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
665 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
673 mutex_lock(&data->update_lock);
675 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
676 data->valid = 0; /* Force cache refresh */
677 mutex_unlock(&data->update_lock);
680 #define show_sensor_offset(offset) \
681 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
682 show_sensor, set_sensor, offset - 1);
684 show_sensor_offset(1);
685 show_sensor_offset(2);
686 show_sensor_offset(3);
690 static int pwm_mode(const struct it87_data *data, int nr)
692 int ctrl = data->fan_main_ctrl & (1 << nr);
694 if (ctrl == 0) /* Full speed */
696 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
698 else /* Manual mode */
702 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
705 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
706 int nr = sensor_attr->index;
708 struct it87_data *data = it87_update_device(dev);
709 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
710 DIV_FROM_REG(data->fan_div[nr])));
712 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
715 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
716 int nr = sensor_attr->index;
718 struct it87_data *data = it87_update_device(dev);
719 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
720 DIV_FROM_REG(data->fan_div[nr])));
722 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
725 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
726 int nr = sensor_attr->index;
728 struct it87_data *data = it87_update_device(dev);
729 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
731 static ssize_t show_pwm_enable(struct device *dev,
732 struct device_attribute *attr, char *buf)
734 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
735 int nr = sensor_attr->index;
737 struct it87_data *data = it87_update_device(dev);
738 return sprintf(buf, "%d\n", pwm_mode(data, nr));
740 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
743 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
744 int nr = sensor_attr->index;
746 struct it87_data *data = it87_update_device(dev);
747 return sprintf(buf, "%d\n",
748 pwm_from_reg(data, data->pwm_duty[nr]));
750 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
753 struct it87_data *data = it87_update_device(dev);
754 int index = (data->fan_ctl >> 4) & 0x07;
756 return sprintf(buf, "%u\n", pwm_freq[index]);
758 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
759 const char *buf, size_t count)
761 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
762 int nr = sensor_attr->index;
764 struct it87_data *data = dev_get_drvdata(dev);
768 if (kstrtol(buf, 10, &val) < 0)
771 mutex_lock(&data->update_lock);
772 reg = it87_read_value(data, IT87_REG_FAN_DIV);
775 data->fan_div[nr] = reg & 0x07;
778 data->fan_div[nr] = (reg >> 3) & 0x07;
781 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
785 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
786 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
787 mutex_unlock(&data->update_lock);
790 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
791 const char *buf, size_t count)
793 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
794 int nr = sensor_attr->index;
796 struct it87_data *data = dev_get_drvdata(dev);
801 if (kstrtoul(buf, 10, &val) < 0)
804 mutex_lock(&data->update_lock);
805 old = it87_read_value(data, IT87_REG_FAN_DIV);
807 /* Save fan min limit */
808 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
813 data->fan_div[nr] = DIV_TO_REG(val);
817 data->fan_div[nr] = 1;
819 data->fan_div[nr] = 3;
822 val |= (data->fan_div[0] & 0x07);
823 val |= (data->fan_div[1] & 0x07) << 3;
824 if (data->fan_div[2] == 3)
826 it87_write_value(data, IT87_REG_FAN_DIV, val);
828 /* Restore fan min limit */
829 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
830 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
832 mutex_unlock(&data->update_lock);
836 /* Returns 0 if OK, -EINVAL otherwise */
837 static int check_trip_points(struct device *dev, int nr)
839 const struct it87_data *data = dev_get_drvdata(dev);
842 if (has_old_autopwm(data)) {
843 for (i = 0; i < 3; i++) {
844 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
847 for (i = 0; i < 2; i++) {
848 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
854 dev_err(dev, "Inconsistent trip points, not switching to "
856 dev_err(dev, "Adjust the trip points and try again\n");
861 static ssize_t set_pwm_enable(struct device *dev,
862 struct device_attribute *attr, const char *buf, size_t count)
864 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
865 int nr = sensor_attr->index;
867 struct it87_data *data = dev_get_drvdata(dev);
870 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
873 /* Check trip points before switching to automatic mode */
875 if (check_trip_points(dev, nr) < 0)
879 mutex_lock(&data->update_lock);
883 /* make sure the fan is on when in on/off mode */
884 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
885 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
886 /* set on/off mode */
887 data->fan_main_ctrl &= ~(1 << nr);
888 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
889 data->fan_main_ctrl);
891 if (val == 1) /* Manual mode */
892 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
893 data->pwm_temp_map[nr] :
895 else /* Automatic mode */
896 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
897 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
898 /* set SmartGuardian mode */
899 data->fan_main_ctrl |= (1 << nr);
900 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
901 data->fan_main_ctrl);
904 mutex_unlock(&data->update_lock);
907 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
908 const char *buf, size_t count)
910 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
911 int nr = sensor_attr->index;
913 struct it87_data *data = dev_get_drvdata(dev);
916 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
919 mutex_lock(&data->update_lock);
920 if (has_newer_autopwm(data)) {
922 * If we are in automatic mode, the PWM duty cycle register
923 * is read-only so we can't write the value.
925 if (data->pwm_ctrl[nr] & 0x80) {
926 mutex_unlock(&data->update_lock);
929 data->pwm_duty[nr] = pwm_to_reg(data, val);
930 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
933 data->pwm_duty[nr] = pwm_to_reg(data, val);
935 * If we are in manual mode, write the duty cycle immediately;
936 * otherwise, just store it for later use.
938 if (!(data->pwm_ctrl[nr] & 0x80)) {
939 data->pwm_ctrl[nr] = data->pwm_duty[nr];
940 it87_write_value(data, IT87_REG_PWM(nr),
944 mutex_unlock(&data->update_lock);
947 static ssize_t set_pwm_freq(struct device *dev,
948 struct device_attribute *attr, const char *buf, size_t count)
950 struct it87_data *data = dev_get_drvdata(dev);
954 if (kstrtoul(buf, 10, &val) < 0)
957 /* Search for the nearest available frequency */
958 for (i = 0; i < 7; i++) {
959 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
963 mutex_lock(&data->update_lock);
964 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
965 data->fan_ctl |= i << 4;
966 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
967 mutex_unlock(&data->update_lock);
971 static ssize_t show_pwm_temp_map(struct device *dev,
972 struct device_attribute *attr, char *buf)
974 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
975 int nr = sensor_attr->index;
977 struct it87_data *data = it87_update_device(dev);
980 if (data->pwm_temp_map[nr] < 3)
981 map = 1 << data->pwm_temp_map[nr];
983 map = 0; /* Should never happen */
984 return sprintf(buf, "%d\n", map);
986 static ssize_t set_pwm_temp_map(struct device *dev,
987 struct device_attribute *attr, const char *buf, size_t count)
989 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
990 int nr = sensor_attr->index;
992 struct it87_data *data = dev_get_drvdata(dev);
997 * This check can go away if we ever support automatic fan speed
998 * control on newer chips.
1000 if (!has_old_autopwm(data)) {
1001 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1005 if (kstrtol(buf, 10, &val) < 0)
1022 mutex_lock(&data->update_lock);
1023 data->pwm_temp_map[nr] = reg;
1025 * If we are in automatic mode, write the temp mapping immediately;
1026 * otherwise, just store it for later use.
1028 if (data->pwm_ctrl[nr] & 0x80) {
1029 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1030 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1032 mutex_unlock(&data->update_lock);
1036 static ssize_t show_auto_pwm(struct device *dev,
1037 struct device_attribute *attr, char *buf)
1039 struct it87_data *data = it87_update_device(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;
1045 return sprintf(buf, "%d\n",
1046 pwm_from_reg(data, data->auto_pwm[nr][point]));
1049 static ssize_t set_auto_pwm(struct device *dev,
1050 struct device_attribute *attr, const char *buf, size_t count)
1052 struct it87_data *data = dev_get_drvdata(dev);
1053 struct sensor_device_attribute_2 *sensor_attr =
1054 to_sensor_dev_attr_2(attr);
1055 int nr = sensor_attr->nr;
1056 int point = sensor_attr->index;
1059 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1062 mutex_lock(&data->update_lock);
1063 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1064 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1065 data->auto_pwm[nr][point]);
1066 mutex_unlock(&data->update_lock);
1070 static ssize_t show_auto_temp(struct device *dev,
1071 struct device_attribute *attr, char *buf)
1073 struct it87_data *data = it87_update_device(dev);
1074 struct sensor_device_attribute_2 *sensor_attr =
1075 to_sensor_dev_attr_2(attr);
1076 int nr = sensor_attr->nr;
1077 int point = sensor_attr->index;
1079 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1082 static ssize_t set_auto_temp(struct device *dev,
1083 struct device_attribute *attr, const char *buf, size_t count)
1085 struct it87_data *data = dev_get_drvdata(dev);
1086 struct sensor_device_attribute_2 *sensor_attr =
1087 to_sensor_dev_attr_2(attr);
1088 int nr = sensor_attr->nr;
1089 int point = sensor_attr->index;
1092 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1095 mutex_lock(&data->update_lock);
1096 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1097 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1098 data->auto_temp[nr][point]);
1099 mutex_unlock(&data->update_lock);
1103 #define show_fan_offset(offset) \
1104 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1105 show_fan, NULL, offset - 1); \
1106 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1107 show_fan_min, set_fan_min, offset - 1); \
1108 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1109 show_fan_div, set_fan_div, offset - 1);
1115 #define show_pwm_offset(offset) \
1116 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1117 show_pwm_enable, set_pwm_enable, offset - 1); \
1118 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1119 show_pwm, set_pwm, offset - 1); \
1120 static DEVICE_ATTR(pwm##offset##_freq, \
1121 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1122 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1123 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1124 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1126 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1127 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1129 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1130 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1132 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1133 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1135 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1136 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1137 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1138 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1140 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1141 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1143 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1144 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1146 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1147 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1149 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1150 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1157 /* A different set of callbacks for 16-bit fans */
1158 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1161 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1162 int nr = sensor_attr->index;
1163 struct it87_data *data = it87_update_device(dev);
1164 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1167 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1170 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1171 int nr = sensor_attr->index;
1172 struct it87_data *data = it87_update_device(dev);
1173 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1176 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1177 const char *buf, size_t count)
1179 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1180 int nr = sensor_attr->index;
1181 struct it87_data *data = dev_get_drvdata(dev);
1184 if (kstrtol(buf, 10, &val) < 0)
1187 mutex_lock(&data->update_lock);
1188 data->fan_min[nr] = FAN16_TO_REG(val);
1189 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1190 data->fan_min[nr] & 0xff);
1191 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1192 data->fan_min[nr] >> 8);
1193 mutex_unlock(&data->update_lock);
1198 * We want to use the same sysfs file names as 8-bit fans, but we need
1199 * different variable names, so we have to use SENSOR_ATTR instead of
1200 * SENSOR_DEVICE_ATTR.
1202 #define show_fan16_offset(offset) \
1203 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1204 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1205 show_fan16, NULL, offset - 1); \
1206 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1207 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1208 show_fan16_min, set_fan16_min, offset - 1)
1210 show_fan16_offset(1);
1211 show_fan16_offset(2);
1212 show_fan16_offset(3);
1213 show_fan16_offset(4);
1214 show_fan16_offset(5);
1217 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1220 struct it87_data *data = it87_update_device(dev);
1221 return sprintf(buf, "%u\n", data->alarms);
1223 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1225 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1228 int bitnr = to_sensor_dev_attr(attr)->index;
1229 struct it87_data *data = it87_update_device(dev);
1230 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1233 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1234 *attr, const char *buf, size_t count)
1236 struct it87_data *data = dev_get_drvdata(dev);
1240 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1243 mutex_lock(&data->update_lock);
1244 config = it87_read_value(data, IT87_REG_CONFIG);
1249 it87_write_value(data, IT87_REG_CONFIG, config);
1250 /* Invalidate cache to force re-read */
1253 mutex_unlock(&data->update_lock);
1258 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1259 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1260 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1261 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1262 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1263 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1264 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1265 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1266 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1267 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1268 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1269 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1270 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1271 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1272 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1273 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1274 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1275 show_alarm, clear_intrusion, 4);
1277 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1280 int bitnr = to_sensor_dev_attr(attr)->index;
1281 struct it87_data *data = it87_update_device(dev);
1282 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1284 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1285 const char *buf, size_t count)
1287 int bitnr = to_sensor_dev_attr(attr)->index;
1288 struct it87_data *data = dev_get_drvdata(dev);
1291 if (kstrtol(buf, 10, &val) < 0
1292 || (val != 0 && val != 1))
1295 mutex_lock(&data->update_lock);
1296 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1298 data->beeps |= (1 << bitnr);
1300 data->beeps &= ~(1 << bitnr);
1301 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1302 mutex_unlock(&data->update_lock);
1306 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1307 show_beep, set_beep, 1);
1308 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1309 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1310 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1311 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1312 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1313 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1314 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1315 /* fanX_beep writability is set later */
1316 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1317 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1318 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1319 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1320 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1321 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1322 show_beep, set_beep, 2);
1323 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1324 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1326 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1329 struct it87_data *data = dev_get_drvdata(dev);
1330 return sprintf(buf, "%u\n", data->vrm);
1332 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1333 const char *buf, size_t count)
1335 struct it87_data *data = dev_get_drvdata(dev);
1338 if (kstrtoul(buf, 10, &val) < 0)
1345 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1347 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1350 struct it87_data *data = it87_update_device(dev);
1351 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1353 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1355 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1358 static const char * const labels[] = {
1363 static const char * const labels_it8721[] = {
1368 struct it87_data *data = dev_get_drvdata(dev);
1369 int nr = to_sensor_dev_attr(attr)->index;
1371 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1374 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1375 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1376 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1378 static ssize_t show_name(struct device *dev, struct device_attribute
1379 *devattr, char *buf)
1381 struct it87_data *data = dev_get_drvdata(dev);
1382 return sprintf(buf, "%s\n", data->name);
1384 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1386 static struct attribute *it87_attributes[] = {
1387 &sensor_dev_attr_in0_input.dev_attr.attr,
1388 &sensor_dev_attr_in1_input.dev_attr.attr,
1389 &sensor_dev_attr_in2_input.dev_attr.attr,
1390 &sensor_dev_attr_in3_input.dev_attr.attr,
1391 &sensor_dev_attr_in4_input.dev_attr.attr,
1392 &sensor_dev_attr_in5_input.dev_attr.attr,
1393 &sensor_dev_attr_in6_input.dev_attr.attr,
1394 &sensor_dev_attr_in7_input.dev_attr.attr,
1395 &sensor_dev_attr_in8_input.dev_attr.attr,
1396 &sensor_dev_attr_in0_min.dev_attr.attr,
1397 &sensor_dev_attr_in1_min.dev_attr.attr,
1398 &sensor_dev_attr_in2_min.dev_attr.attr,
1399 &sensor_dev_attr_in3_min.dev_attr.attr,
1400 &sensor_dev_attr_in4_min.dev_attr.attr,
1401 &sensor_dev_attr_in5_min.dev_attr.attr,
1402 &sensor_dev_attr_in6_min.dev_attr.attr,
1403 &sensor_dev_attr_in7_min.dev_attr.attr,
1404 &sensor_dev_attr_in0_max.dev_attr.attr,
1405 &sensor_dev_attr_in1_max.dev_attr.attr,
1406 &sensor_dev_attr_in2_max.dev_attr.attr,
1407 &sensor_dev_attr_in3_max.dev_attr.attr,
1408 &sensor_dev_attr_in4_max.dev_attr.attr,
1409 &sensor_dev_attr_in5_max.dev_attr.attr,
1410 &sensor_dev_attr_in6_max.dev_attr.attr,
1411 &sensor_dev_attr_in7_max.dev_attr.attr,
1412 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1413 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1414 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1415 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1416 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1417 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1418 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1419 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1421 &sensor_dev_attr_temp1_input.dev_attr.attr,
1422 &sensor_dev_attr_temp2_input.dev_attr.attr,
1423 &sensor_dev_attr_temp3_input.dev_attr.attr,
1424 &sensor_dev_attr_temp1_max.dev_attr.attr,
1425 &sensor_dev_attr_temp2_max.dev_attr.attr,
1426 &sensor_dev_attr_temp3_max.dev_attr.attr,
1427 &sensor_dev_attr_temp1_min.dev_attr.attr,
1428 &sensor_dev_attr_temp2_min.dev_attr.attr,
1429 &sensor_dev_attr_temp3_min.dev_attr.attr,
1430 &sensor_dev_attr_temp1_type.dev_attr.attr,
1431 &sensor_dev_attr_temp2_type.dev_attr.attr,
1432 &sensor_dev_attr_temp3_type.dev_attr.attr,
1433 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1434 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1435 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1437 &dev_attr_alarms.attr,
1438 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1439 &dev_attr_name.attr,
1443 static const struct attribute_group it87_group = {
1444 .attrs = it87_attributes,
1447 static struct attribute *it87_attributes_beep[] = {
1448 &sensor_dev_attr_in0_beep.dev_attr.attr,
1449 &sensor_dev_attr_in1_beep.dev_attr.attr,
1450 &sensor_dev_attr_in2_beep.dev_attr.attr,
1451 &sensor_dev_attr_in3_beep.dev_attr.attr,
1452 &sensor_dev_attr_in4_beep.dev_attr.attr,
1453 &sensor_dev_attr_in5_beep.dev_attr.attr,
1454 &sensor_dev_attr_in6_beep.dev_attr.attr,
1455 &sensor_dev_attr_in7_beep.dev_attr.attr,
1457 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1458 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1459 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1463 static const struct attribute_group it87_group_beep = {
1464 .attrs = it87_attributes_beep,
1467 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1468 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1469 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1470 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1473 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1474 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1475 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1478 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1479 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1480 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1483 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1484 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1485 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1488 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1489 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1490 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1494 static const struct attribute_group it87_group_fan16[5] = {
1495 { .attrs = it87_attributes_fan16[0] },
1496 { .attrs = it87_attributes_fan16[1] },
1497 { .attrs = it87_attributes_fan16[2] },
1498 { .attrs = it87_attributes_fan16[3] },
1499 { .attrs = it87_attributes_fan16[4] },
1502 static struct attribute *it87_attributes_fan[3][4+1] = { {
1503 &sensor_dev_attr_fan1_input.dev_attr.attr,
1504 &sensor_dev_attr_fan1_min.dev_attr.attr,
1505 &sensor_dev_attr_fan1_div.dev_attr.attr,
1506 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1509 &sensor_dev_attr_fan2_input.dev_attr.attr,
1510 &sensor_dev_attr_fan2_min.dev_attr.attr,
1511 &sensor_dev_attr_fan2_div.dev_attr.attr,
1512 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1515 &sensor_dev_attr_fan3_input.dev_attr.attr,
1516 &sensor_dev_attr_fan3_min.dev_attr.attr,
1517 &sensor_dev_attr_fan3_div.dev_attr.attr,
1518 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1522 static const struct attribute_group it87_group_fan[3] = {
1523 { .attrs = it87_attributes_fan[0] },
1524 { .attrs = it87_attributes_fan[1] },
1525 { .attrs = it87_attributes_fan[2] },
1528 static const struct attribute_group *
1529 it87_get_fan_group(const struct it87_data *data)
1531 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1534 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1535 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1536 &sensor_dev_attr_pwm1.dev_attr.attr,
1537 &dev_attr_pwm1_freq.attr,
1538 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1541 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1542 &sensor_dev_attr_pwm2.dev_attr.attr,
1543 &dev_attr_pwm2_freq.attr,
1544 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1547 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1548 &sensor_dev_attr_pwm3.dev_attr.attr,
1549 &dev_attr_pwm3_freq.attr,
1550 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1554 static const struct attribute_group it87_group_pwm[3] = {
1555 { .attrs = it87_attributes_pwm[0] },
1556 { .attrs = it87_attributes_pwm[1] },
1557 { .attrs = it87_attributes_pwm[2] },
1560 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1561 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1562 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1563 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1564 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1565 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1566 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1567 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1568 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1569 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1572 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1573 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1574 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1575 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1576 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1577 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1578 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1579 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1580 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1583 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1584 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1585 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1586 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1587 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1588 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1589 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1590 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1591 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1595 static const struct attribute_group it87_group_autopwm[3] = {
1596 { .attrs = it87_attributes_autopwm[0] },
1597 { .attrs = it87_attributes_autopwm[1] },
1598 { .attrs = it87_attributes_autopwm[2] },
1601 static struct attribute *it87_attributes_fan_beep[] = {
1602 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1603 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1604 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1605 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1606 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1609 static struct attribute *it87_attributes_vid[] = {
1611 &dev_attr_cpu0_vid.attr,
1615 static const struct attribute_group it87_group_vid = {
1616 .attrs = it87_attributes_vid,
1619 static struct attribute *it87_attributes_label[] = {
1620 &sensor_dev_attr_in3_label.dev_attr.attr,
1621 &sensor_dev_attr_in7_label.dev_attr.attr,
1622 &sensor_dev_attr_in8_label.dev_attr.attr,
1626 static const struct attribute_group it87_group_label = {
1627 .attrs = it87_attributes_label,
1630 /* SuperIO detection - will change isa_address if a chip is found */
1631 static int __init it87_find(unsigned short *address,
1632 struct it87_sio_data *sio_data)
1636 const char *board_vendor, *board_name;
1638 err = superio_enter();
1643 chip_type = force_id ? force_id : superio_inw(DEVID);
1645 switch (chip_type) {
1647 sio_data->type = it87;
1650 sio_data->type = it8712;
1654 sio_data->type = it8716;
1657 sio_data->type = it8718;
1660 sio_data->type = it8720;
1663 sio_data->type = it8721;
1666 sio_data->type = it8728;
1669 sio_data->type = it8783;
1671 case 0xffff: /* No device at all */
1674 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1678 superio_select(PME);
1679 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1680 pr_info("Device not activated, skipping\n");
1684 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1685 if (*address == 0) {
1686 pr_info("Base address not set, skipping\n");
1691 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1692 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1693 chip_type, *address, sio_data->revision);
1695 /* in8 (Vbat) is always internal */
1696 sio_data->internal = (1 << 2);
1698 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1699 if (sio_data->type == it87) {
1700 /* The IT8705F doesn't have VID pins at all */
1701 sio_data->skip_vid = 1;
1703 /* The IT8705F has a different LD number for GPIO */
1705 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1706 } else if (sio_data->type == it8783) {
1707 int reg25, reg27, reg2A, reg2C, regEF;
1709 sio_data->skip_vid = 1; /* No VID */
1711 superio_select(GPIO);
1713 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1714 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1715 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1716 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1717 regEF = superio_inb(IT87_SIO_SPI_REG);
1719 /* Check if fan3 is there or not */
1720 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1721 sio_data->skip_fan |= (1 << 2);
1722 if ((reg25 & (1 << 4))
1723 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1724 sio_data->skip_pwm |= (1 << 2);
1726 /* Check if fan2 is there or not */
1727 if (reg27 & (1 << 7))
1728 sio_data->skip_fan |= (1 << 1);
1729 if (reg27 & (1 << 3))
1730 sio_data->skip_pwm |= (1 << 1);
1733 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1737 if ((reg27 & (1 << 1)) || (reg2C & (1 << 2)))
1741 if ((reg27 & (1 << 2)) || (reg2C & (1 << 2))) {
1743 * If the external VIN7 pin is disabled, route it to the
1744 * internal VCCH5V if that is not already done.
1746 if (!(reg2C & (1 << 1))) {
1748 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1749 pr_notice("Routing internal VCCH to in7\n");
1753 if (reg2C & (1 << 0))
1754 sio_data->internal |= (1 << 0);
1755 if (reg2C & (1 << 1))
1756 sio_data->internal |= (1 << 1);
1758 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1763 superio_select(GPIO);
1765 reg = superio_inb(IT87_SIO_GPIO3_REG);
1766 if (sio_data->type == it8721 || sio_data->type == it8728) {
1768 * The IT8721F/IT8758E/IT8783E/F don't have VID pins
1769 * at all, not sure about the IT8728F.
1771 sio_data->skip_vid = 1;
1773 /* We need at least 4 VID pins */
1775 pr_info("VID is disabled (pins used for GPIO)\n");
1776 sio_data->skip_vid = 1;
1780 /* Check if fan3 is there or not */
1782 sio_data->skip_pwm |= (1 << 2);
1784 sio_data->skip_fan |= (1 << 2);
1786 /* Check if fan2 is there or not */
1787 reg = superio_inb(IT87_SIO_GPIO5_REG);
1789 sio_data->skip_pwm |= (1 << 1);
1791 sio_data->skip_fan |= (1 << 1);
1793 if ((sio_data->type == it8718 || sio_data->type == it8720)
1794 && !(sio_data->skip_vid))
1795 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1797 reg = superio_inb(IT87_SIO_PINX2_REG);
1799 * The IT8720F has no VIN7 pin, so VCCH should always be
1800 * routed internally to VIN7 with an internal divider.
1801 * Curiously, there still is a configuration bit to control
1802 * this, which means it can be set incorrectly. And even
1803 * more curiously, many boards out there are improperly
1804 * configured, even though the IT8720F datasheet claims
1805 * that the internal routing of VCCH to VIN7 is the default
1806 * setting. So we force the internal routing in this case.
1808 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1810 superio_outb(IT87_SIO_PINX2_REG, reg);
1811 pr_notice("Routing internal VCCH to in7\n");
1814 sio_data->internal |= (1 << 0);
1815 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1816 sio_data->type == it8728)
1817 sio_data->internal |= (1 << 1);
1819 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1821 if (sio_data->beep_pin)
1822 pr_info("Beeping is supported\n");
1824 /* Disable specific features based on DMI strings */
1825 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1826 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1827 if (board_vendor && board_name) {
1828 if (strcmp(board_vendor, "nVIDIA") == 0
1829 && strcmp(board_name, "FN68PT") == 0) {
1831 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1832 * connected to a fan, but to something else. One user
1833 * has reported instant system power-off when changing
1834 * the PWM2 duty cycle, so we disable it.
1835 * I use the board name string as the trigger in case
1836 * the same board is ever used in other systems.
1838 pr_info("Disabling pwm2 due to hardware constraints\n");
1839 sio_data->skip_pwm = (1 << 1);
1848 static void it87_remove_files(struct device *dev)
1850 struct it87_data *data = platform_get_drvdata(pdev);
1851 struct it87_sio_data *sio_data = dev->platform_data;
1852 const struct attribute_group *fan_group = it87_get_fan_group(data);
1855 sysfs_remove_group(&dev->kobj, &it87_group);
1856 if (sio_data->beep_pin)
1857 sysfs_remove_group(&dev->kobj, &it87_group_beep);
1858 for (i = 0; i < 5; i++) {
1859 if (!(data->has_fan & (1 << i)))
1861 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1862 if (sio_data->beep_pin)
1863 sysfs_remove_file(&dev->kobj,
1864 it87_attributes_fan_beep[i]);
1866 for (i = 0; i < 3; i++) {
1867 if (sio_data->skip_pwm & (1 << 0))
1869 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1870 if (has_old_autopwm(data))
1871 sysfs_remove_group(&dev->kobj,
1872 &it87_group_autopwm[i]);
1874 if (!sio_data->skip_vid)
1875 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1876 sysfs_remove_group(&dev->kobj, &it87_group_label);
1879 static int __devinit it87_probe(struct platform_device *pdev)
1881 struct it87_data *data;
1882 struct resource *res;
1883 struct device *dev = &pdev->dev;
1884 struct it87_sio_data *sio_data = dev->platform_data;
1885 const struct attribute_group *fan_group;
1887 int enable_pwm_interface;
1888 int fan_beep_need_rw;
1889 static const char * const names[] = {
1899 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1900 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1901 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1902 (unsigned long)res->start,
1903 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1908 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1914 data->addr = res->start;
1915 data->type = sio_data->type;
1916 data->revision = sio_data->revision;
1917 data->name = names[sio_data->type];
1919 /* Now, we do the remaining detection. */
1920 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1921 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1926 platform_set_drvdata(pdev, data);
1928 mutex_init(&data->update_lock);
1930 /* Check PWM configuration */
1931 enable_pwm_interface = it87_check_pwm(dev);
1933 /* Starting with IT8721F, we handle scaling of internal voltages */
1934 if (has_12mv_adc(data)) {
1935 if (sio_data->internal & (1 << 0))
1936 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1937 if (sio_data->internal & (1 << 1))
1938 data->in_scaled |= (1 << 7); /* in7 is VSB */
1939 if (sio_data->internal & (1 << 2))
1940 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1941 } else if (sio_data->type == it8783) {
1942 if (sio_data->internal & (1 << 0))
1943 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1944 if (sio_data->internal & (1 << 1))
1945 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
1948 /* Initialize the IT87 chip */
1949 it87_init_device(pdev);
1951 /* Register sysfs hooks */
1952 err = sysfs_create_group(&dev->kobj, &it87_group);
1956 if (sio_data->beep_pin) {
1957 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1962 /* Do not create fan files for disabled fans */
1963 fan_group = it87_get_fan_group(data);
1964 fan_beep_need_rw = 1;
1965 for (i = 0; i < 5; i++) {
1966 if (!(data->has_fan & (1 << i)))
1968 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1972 if (sio_data->beep_pin) {
1973 err = sysfs_create_file(&dev->kobj,
1974 it87_attributes_fan_beep[i]);
1977 if (!fan_beep_need_rw)
1981 * As we have a single beep enable bit for all fans,
1982 * only the first enabled fan has a writable attribute
1985 if (sysfs_chmod_file(&dev->kobj,
1986 it87_attributes_fan_beep[i],
1988 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1990 fan_beep_need_rw = 0;
1994 if (enable_pwm_interface) {
1995 for (i = 0; i < 3; i++) {
1996 if (sio_data->skip_pwm & (1 << i))
1998 err = sysfs_create_group(&dev->kobj,
1999 &it87_group_pwm[i]);
2003 if (!has_old_autopwm(data))
2005 err = sysfs_create_group(&dev->kobj,
2006 &it87_group_autopwm[i]);
2012 if (!sio_data->skip_vid) {
2013 data->vrm = vid_which_vrm();
2014 /* VID reading from Super-I/O config space if available */
2015 data->vid = sio_data->vid_value;
2016 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2021 /* Export labels for internal sensors */
2022 for (i = 0; i < 3; i++) {
2023 if (!(sio_data->internal & (1 << i)))
2025 err = sysfs_create_file(&dev->kobj,
2026 it87_attributes_label[i]);
2031 data->hwmon_dev = hwmon_device_register(dev);
2032 if (IS_ERR(data->hwmon_dev)) {
2033 err = PTR_ERR(data->hwmon_dev);
2040 it87_remove_files(dev);
2042 platform_set_drvdata(pdev, NULL);
2045 release_region(res->start, IT87_EC_EXTENT);
2050 static int __devexit it87_remove(struct platform_device *pdev)
2052 struct it87_data *data = platform_get_drvdata(pdev);
2054 hwmon_device_unregister(data->hwmon_dev);
2055 it87_remove_files(&pdev->dev);
2057 release_region(data->addr, IT87_EC_EXTENT);
2058 platform_set_drvdata(pdev, NULL);
2065 * Must be called with data->update_lock held, except during initialization.
2066 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2067 * would slow down the IT87 access and should not be necessary.
2069 static int it87_read_value(struct it87_data *data, u8 reg)
2071 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2072 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2076 * Must be called with data->update_lock held, except during initialization.
2077 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2078 * would slow down the IT87 access and should not be necessary.
2080 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2082 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2083 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2086 /* Return 1 if and only if the PWM interface is safe to use */
2087 static int __devinit it87_check_pwm(struct device *dev)
2089 struct it87_data *data = dev_get_drvdata(dev);
2091 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2092 * and polarity set to active low is sign that this is the case so we
2093 * disable pwm control to protect the user.
2095 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2096 if ((tmp & 0x87) == 0) {
2097 if (fix_pwm_polarity) {
2099 * The user asks us to attempt a chip reconfiguration.
2100 * This means switching to active high polarity and
2101 * inverting all fan speed values.
2106 for (i = 0; i < 3; i++)
2107 pwm[i] = it87_read_value(data,
2111 * If any fan is in automatic pwm mode, the polarity
2112 * might be correct, as suspicious as it seems, so we
2113 * better don't change anything (but still disable the
2116 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2117 dev_info(dev, "Reconfiguring PWM to "
2118 "active high polarity\n");
2119 it87_write_value(data, IT87_REG_FAN_CTL,
2121 for (i = 0; i < 3; i++)
2122 it87_write_value(data,
2128 dev_info(dev, "PWM configuration is "
2129 "too broken to be fixed\n");
2132 dev_info(dev, "Detected broken BIOS "
2133 "defaults, disabling PWM interface\n");
2135 } else if (fix_pwm_polarity) {
2136 dev_info(dev, "PWM configuration looks "
2137 "sane, won't touch\n");
2143 /* Called when we have found a new IT87. */
2144 static void __devinit it87_init_device(struct platform_device *pdev)
2146 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2147 struct it87_data *data = platform_get_drvdata(pdev);
2152 * For each PWM channel:
2153 * - If it is in automatic mode, setting to manual mode should set
2154 * the fan to full speed by default.
2155 * - If it is in manual mode, we need a mapping to temperature
2156 * channels to use when later setting to automatic mode later.
2157 * Use a 1:1 mapping by default (we are clueless.)
2158 * In both cases, the value can (and should) be changed by the user
2159 * prior to switching to a different mode.
2160 * Note that this is no longer needed for the IT8721F and later, as
2161 * these have separate registers for the temperature mapping and the
2162 * manual duty cycle.
2164 for (i = 0; i < 3; i++) {
2165 data->pwm_temp_map[i] = i;
2166 data->pwm_duty[i] = 0x7f; /* Full speed */
2167 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2171 * Some chips seem to have default value 0xff for all limit
2172 * registers. For low voltage limits it makes no sense and triggers
2173 * alarms, so change to 0 instead. For high temperature limits, it
2174 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2175 * but is still confusing, so change to 127 degrees C.
2177 for (i = 0; i < 8; i++) {
2178 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2180 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2182 for (i = 0; i < 3; i++) {
2183 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2185 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2189 * Temperature channels are not forcibly enabled, as they can be
2190 * set to two different sensor types and we can't guess which one
2191 * is correct for a given system. These channels can be enabled at
2192 * run-time through the temp{1-3}_type sysfs accessors if needed.
2195 /* Check if voltage monitors are reset manually or by some reason */
2196 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2197 if ((tmp & 0xff) == 0) {
2198 /* Enable all voltage monitors */
2199 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2202 /* Check if tachometers are reset manually or by some reason */
2203 mask = 0x70 & ~(sio_data->skip_fan << 4);
2204 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2205 if ((data->fan_main_ctrl & mask) == 0) {
2206 /* Enable all fan tachometers */
2207 data->fan_main_ctrl |= mask;
2208 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2209 data->fan_main_ctrl);
2211 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2213 /* Set tachometers to 16-bit mode if needed */
2214 if (has_16bit_fans(data)) {
2215 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2216 if (~tmp & 0x07 & data->has_fan) {
2218 "Setting fan1-3 to 16-bit mode\n");
2219 it87_write_value(data, IT87_REG_FAN_16BIT,
2222 /* IT8705F only supports three fans. */
2223 if (data->type != it87) {
2225 data->has_fan |= (1 << 3); /* fan4 enabled */
2227 data->has_fan |= (1 << 4); /* fan5 enabled */
2231 /* Fan input pins may be used for alternative functions */
2232 data->has_fan &= ~sio_data->skip_fan;
2234 /* Start monitoring */
2235 it87_write_value(data, IT87_REG_CONFIG,
2236 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
2237 | (update_vbat ? 0x41 : 0x01));
2240 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2242 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2243 if (has_newer_autopwm(data)) {
2244 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2245 data->pwm_duty[nr] = it87_read_value(data,
2246 IT87_REG_PWM_DUTY(nr));
2248 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2249 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2250 else /* Manual mode */
2251 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2254 if (has_old_autopwm(data)) {
2257 for (i = 0; i < 5 ; i++)
2258 data->auto_temp[nr][i] = it87_read_value(data,
2259 IT87_REG_AUTO_TEMP(nr, i));
2260 for (i = 0; i < 3 ; i++)
2261 data->auto_pwm[nr][i] = it87_read_value(data,
2262 IT87_REG_AUTO_PWM(nr, i));
2266 static struct it87_data *it87_update_device(struct device *dev)
2268 struct it87_data *data = dev_get_drvdata(dev);
2271 mutex_lock(&data->update_lock);
2273 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2277 * Cleared after each update, so reenable. Value
2278 * returned by this read will be previous value
2280 it87_write_value(data, IT87_REG_CONFIG,
2281 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2283 for (i = 0; i <= 7; i++) {
2285 it87_read_value(data, IT87_REG_VIN(i));
2287 it87_read_value(data, IT87_REG_VIN_MIN(i));
2289 it87_read_value(data, IT87_REG_VIN_MAX(i));
2291 /* in8 (battery) has no limit registers */
2292 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
2294 for (i = 0; i < 5; i++) {
2295 /* Skip disabled fans */
2296 if (!(data->has_fan & (1 << i)))
2300 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2301 data->fan[i] = it87_read_value(data,
2303 /* Add high byte if in 16-bit mode */
2304 if (has_16bit_fans(data)) {
2305 data->fan[i] |= it87_read_value(data,
2306 IT87_REG_FANX[i]) << 8;
2307 data->fan_min[i] |= it87_read_value(data,
2308 IT87_REG_FANX_MIN[i]) << 8;
2311 for (i = 0; i < 3; i++) {
2313 it87_read_value(data, IT87_REG_TEMP(i));
2314 data->temp_high[i] =
2315 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2317 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2320 /* Newer chips don't have clock dividers */
2321 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2322 i = it87_read_value(data, IT87_REG_FAN_DIV);
2323 data->fan_div[0] = i & 0x07;
2324 data->fan_div[1] = (i >> 3) & 0x07;
2325 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2329 it87_read_value(data, IT87_REG_ALARM1) |
2330 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2331 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2332 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2334 data->fan_main_ctrl = it87_read_value(data,
2335 IT87_REG_FAN_MAIN_CTRL);
2336 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2337 for (i = 0; i < 3; i++)
2338 it87_update_pwm_ctrl(data, i);
2340 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2342 * The IT8705F does not have VID capability.
2343 * The IT8718F and later don't use IT87_REG_VID for the
2346 if (data->type == it8712 || data->type == it8716) {
2347 data->vid = it87_read_value(data, IT87_REG_VID);
2349 * The older IT8712F revisions had only 5 VID pins,
2350 * but we assume it is always safe to read 6 bits.
2354 data->last_updated = jiffies;
2358 mutex_unlock(&data->update_lock);
2363 static int __init it87_device_add(unsigned short address,
2364 const struct it87_sio_data *sio_data)
2366 struct resource res = {
2367 .start = address + IT87_EC_OFFSET,
2368 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2370 .flags = IORESOURCE_IO,
2374 err = acpi_check_resource_conflict(&res);
2378 pdev = platform_device_alloc(DRVNAME, address);
2381 pr_err("Device allocation failed\n");
2385 err = platform_device_add_resources(pdev, &res, 1);
2387 pr_err("Device resource addition failed (%d)\n", err);
2388 goto exit_device_put;
2391 err = platform_device_add_data(pdev, sio_data,
2392 sizeof(struct it87_sio_data));
2394 pr_err("Platform data allocation failed\n");
2395 goto exit_device_put;
2398 err = platform_device_add(pdev);
2400 pr_err("Device addition failed (%d)\n", err);
2401 goto exit_device_put;
2407 platform_device_put(pdev);
2412 static int __init sm_it87_init(void)
2415 unsigned short isa_address = 0;
2416 struct it87_sio_data sio_data;
2418 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2419 err = it87_find(&isa_address, &sio_data);
2422 err = platform_driver_register(&it87_driver);
2426 err = it87_device_add(isa_address, &sio_data);
2428 platform_driver_unregister(&it87_driver);
2435 static void __exit sm_it87_exit(void)
2437 platform_device_unregister(pdev);
2438 platform_driver_unregister(&it87_driver);
2442 MODULE_AUTHOR("Chris Gauthron, "
2443 "Jean Delvare <khali@linux-fr.org>");
2444 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2445 module_param(update_vbat, bool, 0);
2446 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2447 module_param(fix_pwm_polarity, bool, 0);
2448 MODULE_PARM_DESC(fix_pwm_polarity,
2449 "Force PWM polarity to active high (DANGEROUS)");
2450 MODULE_LICENSE("GPL");
2452 module_init(sm_it87_init);
2453 module_exit(sm_it87_exit);