2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
13 * Supports: IT8705F Super I/O chip w/LPC interface
14 * IT8712F Super I/O chip w/LPC interface
15 * IT8716F Super I/O chip w/LPC interface
16 * IT8718F Super I/O chip w/LPC interface
17 * IT8720F Super I/O chip w/LPC interface
18 * IT8721F Super I/O chip w/LPC interface
19 * IT8726F Super I/O chip w/LPC interface
20 * IT8728F Super I/O chip w/LPC interface
21 * IT8758E Super I/O chip w/LPC interface
22 * IT8782F Super I/O chip w/LPC interface
23 * IT8771E Super I/O chip w/LPC interface
24 * IT8772E Super I/O chip w/LPC interface
25 * IT8781F Super I/O chip w/LPC interface
26 * IT8782F Super I/O chip w/LPC interface
27 * IT8783E/F Super I/O chip w/LPC interface
28 * Sis950 A clone of the IT8705F
30 * Copyright (C) 2001 Chris Gauthron
31 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/jiffies.h>
54 #include <linux/platform_device.h>
55 #include <linux/hwmon.h>
56 #include <linux/hwmon-sysfs.h>
57 #include <linux/hwmon-vid.h>
58 #include <linux/err.h>
59 #include <linux/mutex.h>
60 #include <linux/sysfs.h>
61 #include <linux/string.h>
62 #include <linux/dmi.h>
63 #include <linux/acpi.h>
68 #define DRVNAME "it87"
70 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
71 it8772, it8781, it8782, it8783 };
73 static unsigned short force_id;
74 module_param(force_id, ushort, 0);
75 MODULE_PARM_DESC(force_id, "Override the detected device ID");
77 static struct platform_device *pdev;
79 #define REG 0x2e /* The register to read/write */
80 #define DEV 0x07 /* Register: Logical device select */
81 #define VAL 0x2f /* The value to read/write */
82 #define PME 0x04 /* The device with the fan registers in it */
84 /* The device with the IT8718F/IT8720F VID value in it */
87 #define DEVID 0x20 /* Register: Device ID */
88 #define DEVREV 0x22 /* Register: Device Revision */
90 static inline int superio_inb(int reg)
96 static inline void superio_outb(int reg, int val)
102 static int superio_inw(int reg)
112 static inline void superio_select(int ldn)
118 static inline int superio_enter(void)
121 * Try to reserve REG and REG + 1 for exclusive access.
123 if (!request_muxed_region(REG, 2, DRVNAME))
133 static inline void superio_exit(void)
137 release_region(REG, 2);
140 /* Logical device 4 registers */
141 #define IT8712F_DEVID 0x8712
142 #define IT8705F_DEVID 0x8705
143 #define IT8716F_DEVID 0x8716
144 #define IT8718F_DEVID 0x8718
145 #define IT8720F_DEVID 0x8720
146 #define IT8721F_DEVID 0x8721
147 #define IT8726F_DEVID 0x8726
148 #define IT8728F_DEVID 0x8728
149 #define IT8771E_DEVID 0x8771
150 #define IT8772E_DEVID 0x8772
151 #define IT8781F_DEVID 0x8781
152 #define IT8782F_DEVID 0x8782
153 #define IT8783E_DEVID 0x8783
154 #define IT87_ACT_REG 0x30
155 #define IT87_BASE_REG 0x60
157 /* Logical device 7 registers (IT8712F and later) */
158 #define IT87_SIO_GPIO1_REG 0x25
159 #define IT87_SIO_GPIO3_REG 0x27
160 #define IT87_SIO_GPIO5_REG 0x29
161 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
162 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
163 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
164 #define IT87_SIO_VID_REG 0xfc /* VID value */
165 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
167 /* Update battery voltage after every reading if true */
168 static bool update_vbat;
170 /* Not all BIOSes properly configure the PWM registers */
171 static bool fix_pwm_polarity;
173 /* Many IT87 constants specified below */
175 /* Length of ISA address segment */
176 #define IT87_EXTENT 8
178 /* Length of ISA address segment for Environmental Controller */
179 #define IT87_EC_EXTENT 2
181 /* Offset of EC registers from ISA base address */
182 #define IT87_EC_OFFSET 5
184 /* Where are the ISA address/data registers relative to the EC base address */
185 #define IT87_ADDR_REG_OFFSET 0
186 #define IT87_DATA_REG_OFFSET 1
188 /*----- The IT87 registers -----*/
190 #define IT87_REG_CONFIG 0x00
192 #define IT87_REG_ALARM1 0x01
193 #define IT87_REG_ALARM2 0x02
194 #define IT87_REG_ALARM3 0x03
197 * The IT8718F and IT8720F have the VID value in a different register, in
198 * Super-I/O configuration space.
200 #define IT87_REG_VID 0x0a
202 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
203 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
206 #define IT87_REG_FAN_DIV 0x0b
207 #define IT87_REG_FAN_16BIT 0x0c
209 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
211 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
212 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
213 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
214 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
215 #define IT87_REG_FAN_MAIN_CTRL 0x13
216 #define IT87_REG_FAN_CTL 0x14
217 #define IT87_REG_PWM(nr) (0x15 + (nr))
218 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
220 #define IT87_REG_VIN(nr) (0x20 + (nr))
221 #define IT87_REG_TEMP(nr) (0x29 + (nr))
223 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
224 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
225 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
226 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
228 #define IT87_REG_VIN_ENABLE 0x50
229 #define IT87_REG_TEMP_ENABLE 0x51
230 #define IT87_REG_TEMP_EXTRA 0x55
231 #define IT87_REG_BEEP_ENABLE 0x5c
233 #define IT87_REG_CHIPID 0x58
235 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
236 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
239 struct it87_sio_data {
241 /* Values read from Super-I/O config space */
245 u8 internal; /* Internal sensors can be labeled */
246 /* Features skipped based on config or DMI */
255 * For each registered chip, we need to keep some data in memory.
256 * The structure is dynamically allocated.
259 struct device *hwmon_dev;
265 struct mutex update_lock;
266 char valid; /* !=0 if following fields are valid */
267 unsigned long last_updated; /* In jiffies */
269 u16 in_scaled; /* Internal voltage sensors are scaled */
270 u8 in[9]; /* Register value */
271 u8 in_max[8]; /* Register value */
272 u8 in_min[8]; /* Register value */
273 u8 has_fan; /* Bitfield, fans enabled */
274 u16 fan[5]; /* Register values, possibly combined */
275 u16 fan_min[5]; /* Register values, possibly combined */
276 u8 has_temp; /* Bitfield, temp sensors enabled */
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 int adc_lsb(const struct it87_data *data, int nr)
335 int lsb = has_12mv_adc(data) ? 12 : 16;
336 if (data->in_scaled & (1 << nr))
341 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
343 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
344 return SENSORS_LIMIT(val, 0, 255);
347 static int in_from_reg(const struct it87_data *data, int nr, int val)
349 return val * adc_lsb(data, nr);
352 static inline u8 FAN_TO_REG(long rpm, int div)
356 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
357 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
361 static inline u16 FAN16_TO_REG(long rpm)
365 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
368 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
369 1350000 / ((val) * (div)))
370 /* The divider is fixed to 2 in 16-bit mode */
371 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
372 1350000 / ((val) * 2))
374 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
375 ((val) + 500) / 1000), -128, 127))
376 #define TEMP_FROM_REG(val) ((val) * 1000)
378 static u8 pwm_to_reg(const struct it87_data *data, long val)
380 if (has_newer_autopwm(data))
386 static int pwm_from_reg(const struct it87_data *data, u8 reg)
388 if (has_newer_autopwm(data))
391 return (reg & 0x7f) << 1;
395 static int DIV_TO_REG(int val)
398 while (answer < 7 && (val >>= 1))
402 #define DIV_FROM_REG(val) (1 << (val))
404 static const unsigned int pwm_freq[8] = {
415 static inline int has_16bit_fans(const struct it87_data *data)
418 * IT8705F Datasheet 0.4.1, 3h == Version G.
419 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
420 * These are the first revisions with 16-bit tachometer support.
422 return (data->type == it87 && data->revision >= 0x03)
423 || (data->type == it8712 && data->revision >= 0x08)
424 || data->type == it8716
425 || data->type == it8718
426 || data->type == it8720
427 || data->type == it8721
428 || data->type == it8728
429 || data->type == it8771
430 || data->type == it8772
431 || data->type == it8781
432 || data->type == it8782
433 || data->type == it8783;
436 static inline int has_old_autopwm(const struct it87_data *data)
439 * The old automatic fan speed control interface is implemented
440 * by IT8705F chips up to revision F and IT8712F chips up to
443 return (data->type == it87 && data->revision < 0x03)
444 || (data->type == it8712 && data->revision < 0x08);
447 static int it87_probe(struct platform_device *pdev);
448 static int __devexit it87_remove(struct platform_device *pdev);
450 static int it87_read_value(struct it87_data *data, u8 reg);
451 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
452 static struct it87_data *it87_update_device(struct device *dev);
453 static int it87_check_pwm(struct device *dev);
454 static void it87_init_device(struct platform_device *pdev);
457 static struct platform_driver it87_driver = {
459 .owner = THIS_MODULE,
463 .remove = __devexit_p(it87_remove),
466 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
469 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
470 int nr = sensor_attr->index;
472 struct it87_data *data = it87_update_device(dev);
473 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
476 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
479 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
480 int nr = sensor_attr->index;
482 struct it87_data *data = it87_update_device(dev);
483 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
486 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
489 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
490 int nr = sensor_attr->index;
492 struct it87_data *data = it87_update_device(dev);
493 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
496 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
497 const char *buf, size_t count)
499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
502 struct it87_data *data = dev_get_drvdata(dev);
505 if (kstrtoul(buf, 10, &val) < 0)
508 mutex_lock(&data->update_lock);
509 data->in_min[nr] = in_to_reg(data, nr, val);
510 it87_write_value(data, IT87_REG_VIN_MIN(nr),
512 mutex_unlock(&data->update_lock);
515 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
516 const char *buf, size_t count)
518 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
519 int nr = sensor_attr->index;
521 struct it87_data *data = dev_get_drvdata(dev);
524 if (kstrtoul(buf, 10, &val) < 0)
527 mutex_lock(&data->update_lock);
528 data->in_max[nr] = in_to_reg(data, nr, val);
529 it87_write_value(data, IT87_REG_VIN_MAX(nr),
531 mutex_unlock(&data->update_lock);
535 #define show_in_offset(offset) \
536 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
537 show_in, NULL, offset);
539 #define limit_in_offset(offset) \
540 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
541 show_in_min, set_in_min, offset); \
542 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
543 show_in_max, set_in_max, offset);
564 static ssize_t show_temp(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[nr]));
573 static ssize_t show_temp_max(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_high[nr]));
582 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
585 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
586 int nr = sensor_attr->index;
588 struct it87_data *data = it87_update_device(dev);
589 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
591 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
592 const char *buf, size_t count)
594 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
595 int nr = sensor_attr->index;
597 struct it87_data *data = dev_get_drvdata(dev);
600 if (kstrtol(buf, 10, &val) < 0)
603 mutex_lock(&data->update_lock);
604 data->temp_high[nr] = TEMP_TO_REG(val);
605 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
606 mutex_unlock(&data->update_lock);
609 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
610 const char *buf, size_t count)
612 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
613 int nr = sensor_attr->index;
615 struct it87_data *data = dev_get_drvdata(dev);
618 if (kstrtol(buf, 10, &val) < 0)
621 mutex_lock(&data->update_lock);
622 data->temp_low[nr] = TEMP_TO_REG(val);
623 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
624 mutex_unlock(&data->update_lock);
627 #define show_temp_offset(offset) \
628 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
629 show_temp, NULL, offset - 1); \
630 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
631 show_temp_max, set_temp_max, offset - 1); \
632 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
633 show_temp_min, set_temp_min, offset - 1);
639 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
642 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
643 int nr = sensor_attr->index;
644 struct it87_data *data = it87_update_device(dev);
645 u8 reg = data->sensor; /* In case value is updated while used */
648 return sprintf(buf, "3\n"); /* thermal diode */
650 return sprintf(buf, "4\n"); /* thermistor */
651 return sprintf(buf, "0\n"); /* disabled */
653 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
654 const char *buf, size_t count)
656 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
657 int nr = sensor_attr->index;
659 struct it87_data *data = dev_get_drvdata(dev);
663 if (kstrtol(buf, 10, &val) < 0)
666 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
669 if (val == 2) { /* backwards compatibility */
670 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
674 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
682 mutex_lock(&data->update_lock);
684 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
685 data->valid = 0; /* Force cache refresh */
686 mutex_unlock(&data->update_lock);
689 #define show_sensor_offset(offset) \
690 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
691 show_sensor, set_sensor, offset - 1);
693 show_sensor_offset(1);
694 show_sensor_offset(2);
695 show_sensor_offset(3);
699 static int pwm_mode(const struct it87_data *data, int nr)
701 int ctrl = data->fan_main_ctrl & (1 << nr);
703 if (ctrl == 0) /* Full speed */
705 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
707 else /* Manual mode */
711 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
714 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
715 int nr = sensor_attr->index;
717 struct it87_data *data = it87_update_device(dev);
718 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
719 DIV_FROM_REG(data->fan_div[nr])));
721 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
724 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
725 int nr = sensor_attr->index;
727 struct it87_data *data = it87_update_device(dev);
728 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
729 DIV_FROM_REG(data->fan_div[nr])));
731 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
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", DIV_FROM_REG(data->fan_div[nr]));
740 static ssize_t show_pwm_enable(struct device *dev,
741 struct device_attribute *attr, char *buf)
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", pwm_mode(data, nr));
749 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
752 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
753 int nr = sensor_attr->index;
755 struct it87_data *data = it87_update_device(dev);
756 return sprintf(buf, "%d\n",
757 pwm_from_reg(data, data->pwm_duty[nr]));
759 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
762 struct it87_data *data = it87_update_device(dev);
763 int index = (data->fan_ctl >> 4) & 0x07;
765 return sprintf(buf, "%u\n", pwm_freq[index]);
767 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
768 const char *buf, size_t count)
770 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
771 int nr = sensor_attr->index;
773 struct it87_data *data = dev_get_drvdata(dev);
777 if (kstrtol(buf, 10, &val) < 0)
780 mutex_lock(&data->update_lock);
781 reg = it87_read_value(data, IT87_REG_FAN_DIV);
784 data->fan_div[nr] = reg & 0x07;
787 data->fan_div[nr] = (reg >> 3) & 0x07;
790 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
794 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
795 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
796 mutex_unlock(&data->update_lock);
799 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
800 const char *buf, size_t count)
802 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
803 int nr = sensor_attr->index;
805 struct it87_data *data = dev_get_drvdata(dev);
810 if (kstrtoul(buf, 10, &val) < 0)
813 mutex_lock(&data->update_lock);
814 old = it87_read_value(data, IT87_REG_FAN_DIV);
816 /* Save fan min limit */
817 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
822 data->fan_div[nr] = DIV_TO_REG(val);
826 data->fan_div[nr] = 1;
828 data->fan_div[nr] = 3;
831 val |= (data->fan_div[0] & 0x07);
832 val |= (data->fan_div[1] & 0x07) << 3;
833 if (data->fan_div[2] == 3)
835 it87_write_value(data, IT87_REG_FAN_DIV, val);
837 /* Restore fan min limit */
838 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
839 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
841 mutex_unlock(&data->update_lock);
845 /* Returns 0 if OK, -EINVAL otherwise */
846 static int check_trip_points(struct device *dev, int nr)
848 const struct it87_data *data = dev_get_drvdata(dev);
851 if (has_old_autopwm(data)) {
852 for (i = 0; i < 3; i++) {
853 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
856 for (i = 0; i < 2; i++) {
857 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
863 dev_err(dev, "Inconsistent trip points, not switching to "
865 dev_err(dev, "Adjust the trip points and try again\n");
870 static ssize_t set_pwm_enable(struct device *dev,
871 struct device_attribute *attr, const char *buf, size_t count)
873 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
874 int nr = sensor_attr->index;
876 struct it87_data *data = dev_get_drvdata(dev);
879 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
882 /* Check trip points before switching to automatic mode */
884 if (check_trip_points(dev, nr) < 0)
888 mutex_lock(&data->update_lock);
892 /* make sure the fan is on when in on/off mode */
893 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
894 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
895 /* set on/off mode */
896 data->fan_main_ctrl &= ~(1 << nr);
897 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
898 data->fan_main_ctrl);
900 if (val == 1) /* Manual mode */
901 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
902 data->pwm_temp_map[nr] :
904 else /* Automatic mode */
905 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
906 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
907 /* set SmartGuardian mode */
908 data->fan_main_ctrl |= (1 << nr);
909 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
910 data->fan_main_ctrl);
913 mutex_unlock(&data->update_lock);
916 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
917 const char *buf, size_t count)
919 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
920 int nr = sensor_attr->index;
922 struct it87_data *data = dev_get_drvdata(dev);
925 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
928 mutex_lock(&data->update_lock);
929 if (has_newer_autopwm(data)) {
931 * If we are in automatic mode, the PWM duty cycle register
932 * is read-only so we can't write the value.
934 if (data->pwm_ctrl[nr] & 0x80) {
935 mutex_unlock(&data->update_lock);
938 data->pwm_duty[nr] = pwm_to_reg(data, val);
939 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
942 data->pwm_duty[nr] = pwm_to_reg(data, val);
944 * If we are in manual mode, write the duty cycle immediately;
945 * otherwise, just store it for later use.
947 if (!(data->pwm_ctrl[nr] & 0x80)) {
948 data->pwm_ctrl[nr] = data->pwm_duty[nr];
949 it87_write_value(data, IT87_REG_PWM(nr),
953 mutex_unlock(&data->update_lock);
956 static ssize_t set_pwm_freq(struct device *dev,
957 struct device_attribute *attr, const char *buf, size_t count)
959 struct it87_data *data = dev_get_drvdata(dev);
963 if (kstrtoul(buf, 10, &val) < 0)
966 /* Search for the nearest available frequency */
967 for (i = 0; i < 7; i++) {
968 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
972 mutex_lock(&data->update_lock);
973 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
974 data->fan_ctl |= i << 4;
975 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
976 mutex_unlock(&data->update_lock);
980 static ssize_t show_pwm_temp_map(struct device *dev,
981 struct device_attribute *attr, char *buf)
983 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
984 int nr = sensor_attr->index;
986 struct it87_data *data = it87_update_device(dev);
989 if (data->pwm_temp_map[nr] < 3)
990 map = 1 << data->pwm_temp_map[nr];
992 map = 0; /* Should never happen */
993 return sprintf(buf, "%d\n", map);
995 static ssize_t set_pwm_temp_map(struct device *dev,
996 struct device_attribute *attr, const char *buf, size_t count)
998 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
999 int nr = sensor_attr->index;
1001 struct it87_data *data = dev_get_drvdata(dev);
1006 * This check can go away if we ever support automatic fan speed
1007 * control on newer chips.
1009 if (!has_old_autopwm(data)) {
1010 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1014 if (kstrtol(buf, 10, &val) < 0)
1031 mutex_lock(&data->update_lock);
1032 data->pwm_temp_map[nr] = reg;
1034 * If we are in automatic mode, write the temp mapping immediately;
1035 * otherwise, just store it for later use.
1037 if (data->pwm_ctrl[nr] & 0x80) {
1038 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1039 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1041 mutex_unlock(&data->update_lock);
1045 static ssize_t show_auto_pwm(struct device *dev,
1046 struct device_attribute *attr, char *buf)
1048 struct it87_data *data = it87_update_device(dev);
1049 struct sensor_device_attribute_2 *sensor_attr =
1050 to_sensor_dev_attr_2(attr);
1051 int nr = sensor_attr->nr;
1052 int point = sensor_attr->index;
1054 return sprintf(buf, "%d\n",
1055 pwm_from_reg(data, data->auto_pwm[nr][point]));
1058 static ssize_t set_auto_pwm(struct device *dev,
1059 struct device_attribute *attr, const char *buf, size_t count)
1061 struct it87_data *data = dev_get_drvdata(dev);
1062 struct sensor_device_attribute_2 *sensor_attr =
1063 to_sensor_dev_attr_2(attr);
1064 int nr = sensor_attr->nr;
1065 int point = sensor_attr->index;
1068 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1071 mutex_lock(&data->update_lock);
1072 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1073 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1074 data->auto_pwm[nr][point]);
1075 mutex_unlock(&data->update_lock);
1079 static ssize_t show_auto_temp(struct device *dev,
1080 struct device_attribute *attr, char *buf)
1082 struct it87_data *data = it87_update_device(dev);
1083 struct sensor_device_attribute_2 *sensor_attr =
1084 to_sensor_dev_attr_2(attr);
1085 int nr = sensor_attr->nr;
1086 int point = sensor_attr->index;
1088 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1091 static ssize_t set_auto_temp(struct device *dev,
1092 struct device_attribute *attr, const char *buf, size_t count)
1094 struct it87_data *data = dev_get_drvdata(dev);
1095 struct sensor_device_attribute_2 *sensor_attr =
1096 to_sensor_dev_attr_2(attr);
1097 int nr = sensor_attr->nr;
1098 int point = sensor_attr->index;
1101 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1104 mutex_lock(&data->update_lock);
1105 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1106 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1107 data->auto_temp[nr][point]);
1108 mutex_unlock(&data->update_lock);
1112 #define show_fan_offset(offset) \
1113 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1114 show_fan, NULL, offset - 1); \
1115 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1116 show_fan_min, set_fan_min, offset - 1); \
1117 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1118 show_fan_div, set_fan_div, offset - 1);
1124 #define show_pwm_offset(offset) \
1125 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1126 show_pwm_enable, set_pwm_enable, offset - 1); \
1127 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1128 show_pwm, set_pwm, offset - 1); \
1129 static DEVICE_ATTR(pwm##offset##_freq, \
1130 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1131 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1132 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1133 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1135 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1136 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1138 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1139 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1141 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1142 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1144 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1145 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1146 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1147 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1149 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1150 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1152 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1153 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1155 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1156 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1158 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1159 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1166 /* A different set of callbacks for 16-bit fans */
1167 static ssize_t show_fan16(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[nr]));
1176 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1179 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1180 int nr = sensor_attr->index;
1181 struct it87_data *data = it87_update_device(dev);
1182 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1185 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1186 const char *buf, size_t count)
1188 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1189 int nr = sensor_attr->index;
1190 struct it87_data *data = dev_get_drvdata(dev);
1193 if (kstrtol(buf, 10, &val) < 0)
1196 mutex_lock(&data->update_lock);
1197 data->fan_min[nr] = FAN16_TO_REG(val);
1198 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1199 data->fan_min[nr] & 0xff);
1200 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1201 data->fan_min[nr] >> 8);
1202 mutex_unlock(&data->update_lock);
1207 * We want to use the same sysfs file names as 8-bit fans, but we need
1208 * different variable names, so we have to use SENSOR_ATTR instead of
1209 * SENSOR_DEVICE_ATTR.
1211 #define show_fan16_offset(offset) \
1212 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1213 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1214 show_fan16, NULL, offset - 1); \
1215 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1216 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1217 show_fan16_min, set_fan16_min, offset - 1)
1219 show_fan16_offset(1);
1220 show_fan16_offset(2);
1221 show_fan16_offset(3);
1222 show_fan16_offset(4);
1223 show_fan16_offset(5);
1226 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1229 struct it87_data *data = it87_update_device(dev);
1230 return sprintf(buf, "%u\n", data->alarms);
1232 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1234 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1237 int bitnr = to_sensor_dev_attr(attr)->index;
1238 struct it87_data *data = it87_update_device(dev);
1239 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1242 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1243 *attr, const char *buf, size_t count)
1245 struct it87_data *data = dev_get_drvdata(dev);
1249 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1252 mutex_lock(&data->update_lock);
1253 config = it87_read_value(data, IT87_REG_CONFIG);
1258 it87_write_value(data, IT87_REG_CONFIG, config);
1259 /* Invalidate cache to force re-read */
1262 mutex_unlock(&data->update_lock);
1267 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1268 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1269 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1270 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1271 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1272 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1273 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1274 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1275 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1276 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1277 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1278 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1279 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1280 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1281 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1282 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1283 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1284 show_alarm, clear_intrusion, 4);
1286 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1289 int bitnr = to_sensor_dev_attr(attr)->index;
1290 struct it87_data *data = it87_update_device(dev);
1291 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1293 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1294 const char *buf, size_t count)
1296 int bitnr = to_sensor_dev_attr(attr)->index;
1297 struct it87_data *data = dev_get_drvdata(dev);
1300 if (kstrtol(buf, 10, &val) < 0
1301 || (val != 0 && val != 1))
1304 mutex_lock(&data->update_lock);
1305 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1307 data->beeps |= (1 << bitnr);
1309 data->beeps &= ~(1 << bitnr);
1310 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1311 mutex_unlock(&data->update_lock);
1315 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1316 show_beep, set_beep, 1);
1317 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1318 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1319 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1320 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1321 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1322 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1323 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1324 /* fanX_beep writability is set later */
1325 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1326 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1327 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1328 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1329 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1330 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1331 show_beep, set_beep, 2);
1332 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1333 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1335 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1338 struct it87_data *data = dev_get_drvdata(dev);
1339 return sprintf(buf, "%u\n", data->vrm);
1341 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1342 const char *buf, size_t count)
1344 struct it87_data *data = dev_get_drvdata(dev);
1347 if (kstrtoul(buf, 10, &val) < 0)
1354 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1356 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1359 struct it87_data *data = it87_update_device(dev);
1360 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1362 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1364 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1367 static const char * const labels[] = {
1372 static const char * const labels_it8721[] = {
1377 struct it87_data *data = dev_get_drvdata(dev);
1378 int nr = to_sensor_dev_attr(attr)->index;
1380 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1383 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1384 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1385 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1387 static ssize_t show_name(struct device *dev, struct device_attribute
1388 *devattr, char *buf)
1390 struct it87_data *data = dev_get_drvdata(dev);
1391 return sprintf(buf, "%s\n", data->name);
1393 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1395 static struct attribute *it87_attributes_in[9][5] = {
1397 &sensor_dev_attr_in0_input.dev_attr.attr,
1398 &sensor_dev_attr_in0_min.dev_attr.attr,
1399 &sensor_dev_attr_in0_max.dev_attr.attr,
1400 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1403 &sensor_dev_attr_in1_input.dev_attr.attr,
1404 &sensor_dev_attr_in1_min.dev_attr.attr,
1405 &sensor_dev_attr_in1_max.dev_attr.attr,
1406 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1409 &sensor_dev_attr_in2_input.dev_attr.attr,
1410 &sensor_dev_attr_in2_min.dev_attr.attr,
1411 &sensor_dev_attr_in2_max.dev_attr.attr,
1412 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1415 &sensor_dev_attr_in3_input.dev_attr.attr,
1416 &sensor_dev_attr_in3_min.dev_attr.attr,
1417 &sensor_dev_attr_in3_max.dev_attr.attr,
1418 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1421 &sensor_dev_attr_in4_input.dev_attr.attr,
1422 &sensor_dev_attr_in4_min.dev_attr.attr,
1423 &sensor_dev_attr_in4_max.dev_attr.attr,
1424 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1427 &sensor_dev_attr_in5_input.dev_attr.attr,
1428 &sensor_dev_attr_in5_min.dev_attr.attr,
1429 &sensor_dev_attr_in5_max.dev_attr.attr,
1430 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1433 &sensor_dev_attr_in6_input.dev_attr.attr,
1434 &sensor_dev_attr_in6_min.dev_attr.attr,
1435 &sensor_dev_attr_in6_max.dev_attr.attr,
1436 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1439 &sensor_dev_attr_in7_input.dev_attr.attr,
1440 &sensor_dev_attr_in7_min.dev_attr.attr,
1441 &sensor_dev_attr_in7_max.dev_attr.attr,
1442 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1445 &sensor_dev_attr_in8_input.dev_attr.attr,
1449 static const struct attribute_group it87_group_in[9] = {
1450 { .attrs = it87_attributes_in[0] },
1451 { .attrs = it87_attributes_in[1] },
1452 { .attrs = it87_attributes_in[2] },
1453 { .attrs = it87_attributes_in[3] },
1454 { .attrs = it87_attributes_in[4] },
1455 { .attrs = it87_attributes_in[5] },
1456 { .attrs = it87_attributes_in[6] },
1457 { .attrs = it87_attributes_in[7] },
1458 { .attrs = it87_attributes_in[8] },
1461 static struct attribute *it87_attributes_temp[3][6] = {
1463 &sensor_dev_attr_temp1_input.dev_attr.attr,
1464 &sensor_dev_attr_temp1_max.dev_attr.attr,
1465 &sensor_dev_attr_temp1_min.dev_attr.attr,
1466 &sensor_dev_attr_temp1_type.dev_attr.attr,
1467 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1470 &sensor_dev_attr_temp2_input.dev_attr.attr,
1471 &sensor_dev_attr_temp2_max.dev_attr.attr,
1472 &sensor_dev_attr_temp2_min.dev_attr.attr,
1473 &sensor_dev_attr_temp2_type.dev_attr.attr,
1474 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1477 &sensor_dev_attr_temp3_input.dev_attr.attr,
1478 &sensor_dev_attr_temp3_max.dev_attr.attr,
1479 &sensor_dev_attr_temp3_min.dev_attr.attr,
1480 &sensor_dev_attr_temp3_type.dev_attr.attr,
1481 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1485 static const struct attribute_group it87_group_temp[3] = {
1486 { .attrs = it87_attributes_temp[0] },
1487 { .attrs = it87_attributes_temp[1] },
1488 { .attrs = it87_attributes_temp[2] },
1491 static struct attribute *it87_attributes[] = {
1492 &dev_attr_alarms.attr,
1493 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1494 &dev_attr_name.attr,
1498 static const struct attribute_group it87_group = {
1499 .attrs = it87_attributes,
1502 static struct attribute *it87_attributes_in_beep[] = {
1503 &sensor_dev_attr_in0_beep.dev_attr.attr,
1504 &sensor_dev_attr_in1_beep.dev_attr.attr,
1505 &sensor_dev_attr_in2_beep.dev_attr.attr,
1506 &sensor_dev_attr_in3_beep.dev_attr.attr,
1507 &sensor_dev_attr_in4_beep.dev_attr.attr,
1508 &sensor_dev_attr_in5_beep.dev_attr.attr,
1509 &sensor_dev_attr_in6_beep.dev_attr.attr,
1510 &sensor_dev_attr_in7_beep.dev_attr.attr,
1514 static struct attribute *it87_attributes_temp_beep[] = {
1515 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1516 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1517 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1520 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1521 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1522 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1523 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1526 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1527 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1528 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1531 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1532 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1533 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1536 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1537 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1538 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1541 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1542 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1543 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1547 static const struct attribute_group it87_group_fan16[5] = {
1548 { .attrs = it87_attributes_fan16[0] },
1549 { .attrs = it87_attributes_fan16[1] },
1550 { .attrs = it87_attributes_fan16[2] },
1551 { .attrs = it87_attributes_fan16[3] },
1552 { .attrs = it87_attributes_fan16[4] },
1555 static struct attribute *it87_attributes_fan[3][4+1] = { {
1556 &sensor_dev_attr_fan1_input.dev_attr.attr,
1557 &sensor_dev_attr_fan1_min.dev_attr.attr,
1558 &sensor_dev_attr_fan1_div.dev_attr.attr,
1559 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1562 &sensor_dev_attr_fan2_input.dev_attr.attr,
1563 &sensor_dev_attr_fan2_min.dev_attr.attr,
1564 &sensor_dev_attr_fan2_div.dev_attr.attr,
1565 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1568 &sensor_dev_attr_fan3_input.dev_attr.attr,
1569 &sensor_dev_attr_fan3_min.dev_attr.attr,
1570 &sensor_dev_attr_fan3_div.dev_attr.attr,
1571 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1575 static const struct attribute_group it87_group_fan[3] = {
1576 { .attrs = it87_attributes_fan[0] },
1577 { .attrs = it87_attributes_fan[1] },
1578 { .attrs = it87_attributes_fan[2] },
1581 static const struct attribute_group *
1582 it87_get_fan_group(const struct it87_data *data)
1584 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1587 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1588 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1589 &sensor_dev_attr_pwm1.dev_attr.attr,
1590 &dev_attr_pwm1_freq.attr,
1591 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1594 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1595 &sensor_dev_attr_pwm2.dev_attr.attr,
1596 &dev_attr_pwm2_freq.attr,
1597 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1600 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1601 &sensor_dev_attr_pwm3.dev_attr.attr,
1602 &dev_attr_pwm3_freq.attr,
1603 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1607 static const struct attribute_group it87_group_pwm[3] = {
1608 { .attrs = it87_attributes_pwm[0] },
1609 { .attrs = it87_attributes_pwm[1] },
1610 { .attrs = it87_attributes_pwm[2] },
1613 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1614 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1615 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1616 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1617 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1618 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1619 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1620 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1621 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1622 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1625 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1626 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1627 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1628 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1629 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1630 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1631 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1632 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1633 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1636 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1637 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1638 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1639 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1640 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1641 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1642 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1643 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1644 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1648 static const struct attribute_group it87_group_autopwm[3] = {
1649 { .attrs = it87_attributes_autopwm[0] },
1650 { .attrs = it87_attributes_autopwm[1] },
1651 { .attrs = it87_attributes_autopwm[2] },
1654 static struct attribute *it87_attributes_fan_beep[] = {
1655 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1656 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1657 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1658 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1659 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1662 static struct attribute *it87_attributes_vid[] = {
1664 &dev_attr_cpu0_vid.attr,
1668 static const struct attribute_group it87_group_vid = {
1669 .attrs = it87_attributes_vid,
1672 static struct attribute *it87_attributes_label[] = {
1673 &sensor_dev_attr_in3_label.dev_attr.attr,
1674 &sensor_dev_attr_in7_label.dev_attr.attr,
1675 &sensor_dev_attr_in8_label.dev_attr.attr,
1679 static const struct attribute_group it87_group_label = {
1680 .attrs = it87_attributes_label,
1683 /* SuperIO detection - will change isa_address if a chip is found */
1684 static int __init it87_find(unsigned short *address,
1685 struct it87_sio_data *sio_data)
1689 const char *board_vendor, *board_name;
1691 err = superio_enter();
1696 chip_type = force_id ? force_id : superio_inw(DEVID);
1698 switch (chip_type) {
1700 sio_data->type = it87;
1703 sio_data->type = it8712;
1707 sio_data->type = it8716;
1710 sio_data->type = it8718;
1713 sio_data->type = it8720;
1716 sio_data->type = it8721;
1719 sio_data->type = it8728;
1722 sio_data->type = it8771;
1725 sio_data->type = it8772;
1728 sio_data->type = it8781;
1731 sio_data->type = it8782;
1734 sio_data->type = it8783;
1736 case 0xffff: /* No device at all */
1739 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1743 superio_select(PME);
1744 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1745 pr_info("Device not activated, skipping\n");
1749 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1750 if (*address == 0) {
1751 pr_info("Base address not set, skipping\n");
1756 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1757 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1758 chip_type, *address, sio_data->revision);
1760 /* in8 (Vbat) is always internal */
1761 sio_data->internal = (1 << 2);
1763 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1764 if (sio_data->type == it87) {
1765 /* The IT8705F doesn't have VID pins at all */
1766 sio_data->skip_vid = 1;
1768 /* The IT8705F has a different LD number for GPIO */
1770 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1771 } else if (sio_data->type == it8783) {
1772 int reg25, reg27, reg2A, reg2C, regEF;
1775 sio_data->skip_vid = 1; /* No VID */
1777 superio_select(GPIO);
1779 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1780 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1781 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1782 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1783 regEF = superio_inb(IT87_SIO_SPI_REG);
1785 uart6 = reg2C & (1 << 2);
1787 /* Check if fan3 is there or not */
1788 if ((reg27 & (1 << 0)) || !uart6)
1789 sio_data->skip_fan |= (1 << 2);
1790 if ((reg25 & (1 << 4))
1791 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1792 sio_data->skip_pwm |= (1 << 2);
1794 /* Check if fan2 is there or not */
1795 if (reg27 & (1 << 7))
1796 sio_data->skip_fan |= (1 << 1);
1797 if (reg27 & (1 << 3))
1798 sio_data->skip_pwm |= (1 << 1);
1801 if ((reg27 & (1 << 0)) || uart6)
1802 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1805 if ((reg27 & (1 << 1)) || uart6)
1806 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1810 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1812 if (reg27 & (1 << 2)) {
1814 * The data sheet is a bit uncear regarding the internal
1815 * voltage divider for VCCH5V. It says
1816 * "This bit enables and switches VIN7 (pin91) to the
1817 * internal voltage divider for VCCH5V".
1818 * This is different to other chips, where the internal
1819 * voltage divider would connect VIN7 to an internal
1820 * voltage source. Maybe that is the case here as well,
1821 * but at least for now follow the data sheet and assume
1822 * that there is no VIN7 if pin 91 is not configured as
1825 sio_data->skip_in |= (1 << 7);
1829 * No temp3 if UART6 is enabled and the temp3 source is TEMPIN3.
1830 * We can not read the temp3 source here, so skip_temp is
1834 sio_data->skip_temp |= (1 << 2);
1836 if (reg2C & (1 << 0))
1837 sio_data->internal |= (1 << 0);
1838 if (reg2C & (1 << 1))
1839 sio_data->internal |= (1 << 1);
1841 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1847 superio_select(GPIO);
1849 reg = superio_inb(IT87_SIO_GPIO3_REG);
1850 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1851 sio_data->type == it8771 || sio_data->type == it8772 ||
1852 sio_data->type == it8781 || sio_data->type == it8782) {
1854 * IT8721F, IT8758E, and IT8782F don't have VID pins
1855 * at all, not sure about the IT8728F.
1857 sio_data->skip_vid = 1;
1859 /* We need at least 4 VID pins */
1861 pr_info("VID is disabled (pins used for GPIO)\n");
1862 sio_data->skip_vid = 1;
1866 /* Check if fan3 is there or not */
1868 sio_data->skip_pwm |= (1 << 2);
1870 sio_data->skip_fan |= (1 << 2);
1872 /* Check if fan2 is there or not */
1873 reg = superio_inb(IT87_SIO_GPIO5_REG);
1875 sio_data->skip_pwm |= (1 << 1);
1877 sio_data->skip_fan |= (1 << 1);
1879 if ((sio_data->type == it8718 || sio_data->type == it8720)
1880 && !(sio_data->skip_vid))
1881 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1883 reg = superio_inb(IT87_SIO_PINX2_REG);
1885 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1888 * The IT8720F has no VIN7 pin, so VCCH should always be
1889 * routed internally to VIN7 with an internal divider.
1890 * Curiously, there still is a configuration bit to control
1891 * this, which means it can be set incorrectly. And even
1892 * more curiously, many boards out there are improperly
1893 * configured, even though the IT8720F datasheet claims
1894 * that the internal routing of VCCH to VIN7 is the default
1895 * setting. So we force the internal routing in this case.
1897 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1898 * If UART6 is enabled, re-route VIN7 to the internal divider.
1900 if ((sio_data->type == it8720 && !(reg & (1 << 1))) || uart6) {
1902 superio_outb(IT87_SIO_PINX2_REG, reg);
1903 pr_notice("Routing internal VCCH to in7\n");
1906 sio_data->internal |= (1 << 0);
1907 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1908 sio_data->type == it8728 || sio_data->type == it8771 ||
1909 sio_data->type == it8772 )
1910 sio_data->internal |= (1 << 1);
1913 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1914 * While VIN7 can be routed to the internal voltage divider,
1915 * VIN5 and VIN6 are not available if UART6 is enabled.
1917 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1918 * is the temperature source. Since we can not read the
1919 * temperature source here, skip_temp is preliminary.
1922 sio_data->skip_in |= (1 << 5) | (1 << 6);
1923 sio_data->skip_temp |= (1 << 2);
1926 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1928 if (sio_data->beep_pin)
1929 pr_info("Beeping is supported\n");
1931 /* Disable specific features based on DMI strings */
1932 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1933 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1934 if (board_vendor && board_name) {
1935 if (strcmp(board_vendor, "nVIDIA") == 0
1936 && strcmp(board_name, "FN68PT") == 0) {
1938 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1939 * connected to a fan, but to something else. One user
1940 * has reported instant system power-off when changing
1941 * the PWM2 duty cycle, so we disable it.
1942 * I use the board name string as the trigger in case
1943 * the same board is ever used in other systems.
1945 pr_info("Disabling pwm2 due to hardware constraints\n");
1946 sio_data->skip_pwm = (1 << 1);
1955 static void it87_remove_files(struct device *dev)
1957 struct it87_data *data = platform_get_drvdata(pdev);
1958 struct it87_sio_data *sio_data = dev->platform_data;
1959 const struct attribute_group *fan_group = it87_get_fan_group(data);
1962 sysfs_remove_group(&dev->kobj, &it87_group);
1963 for (i = 0; i < 9; i++) {
1964 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1965 if (it87_attributes_in_beep[i])
1966 sysfs_remove_file(&dev->kobj,
1967 it87_attributes_in_beep[i]);
1969 for (i = 0; i < 3; i++) {
1970 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1971 sysfs_remove_file(&dev->kobj,
1972 it87_attributes_temp_beep[i]);
1974 for (i = 0; i < 5; i++) {
1975 if (!(data->has_fan & (1 << i)))
1977 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1978 if (sio_data->beep_pin)
1979 sysfs_remove_file(&dev->kobj,
1980 it87_attributes_fan_beep[i]);
1982 for (i = 0; i < 3; i++) {
1983 if (sio_data->skip_pwm & (1 << 0))
1985 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1986 if (has_old_autopwm(data))
1987 sysfs_remove_group(&dev->kobj,
1988 &it87_group_autopwm[i]);
1990 if (!sio_data->skip_vid)
1991 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1992 sysfs_remove_group(&dev->kobj, &it87_group_label);
1995 static int __devinit it87_probe(struct platform_device *pdev)
1997 struct it87_data *data;
1998 struct resource *res;
1999 struct device *dev = &pdev->dev;
2000 struct it87_sio_data *sio_data = dev->platform_data;
2001 const struct attribute_group *fan_group;
2003 int enable_pwm_interface;
2004 int fan_beep_need_rw;
2005 static const char * const names[] = {
2020 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2021 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
2022 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2023 (unsigned long)res->start,
2024 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2029 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2035 data->addr = res->start;
2036 data->type = sio_data->type;
2037 data->revision = sio_data->revision;
2038 data->name = names[sio_data->type];
2040 /* Now, we do the remaining detection. */
2041 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2042 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
2047 platform_set_drvdata(pdev, data);
2049 mutex_init(&data->update_lock);
2051 /* Check PWM configuration */
2052 enable_pwm_interface = it87_check_pwm(dev);
2054 /* Starting with IT8721F, we handle scaling of internal voltages */
2055 if (has_12mv_adc(data)) {
2056 if (sio_data->internal & (1 << 0))
2057 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2058 if (sio_data->internal & (1 << 1))
2059 data->in_scaled |= (1 << 7); /* in7 is VSB */
2060 if (sio_data->internal & (1 << 2))
2061 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2062 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2063 sio_data->type == it8783) {
2064 if (sio_data->internal & (1 << 0))
2065 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2066 if (sio_data->internal & (1 << 1))
2067 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
2070 data->has_temp = 0x07;
2071 if (sio_data->skip_temp &&
2072 !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2073 data->has_temp &= ~sio_data->skip_temp;
2075 /* Initialize the IT87 chip */
2076 it87_init_device(pdev);
2078 /* Register sysfs hooks */
2079 for (i = 0; i < 9; i++) {
2080 if (sio_data->skip_in & (1 << i))
2082 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2085 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2086 err = sysfs_create_file(&dev->kobj,
2087 it87_attributes_in_beep[i]);
2093 for (i = 0; i < 3; i++) {
2094 if (!(data->has_temp & (1 << i)))
2096 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2099 if (sio_data->beep_pin) {
2100 err = sysfs_create_file(&dev->kobj,
2101 it87_attributes_temp_beep[i]);
2107 err = sysfs_create_group(&dev->kobj, &it87_group);
2111 /* Do not create fan files for disabled fans */
2112 fan_group = it87_get_fan_group(data);
2113 fan_beep_need_rw = 1;
2114 for (i = 0; i < 5; i++) {
2115 if (!(data->has_fan & (1 << i)))
2117 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2121 if (sio_data->beep_pin) {
2122 err = sysfs_create_file(&dev->kobj,
2123 it87_attributes_fan_beep[i]);
2126 if (!fan_beep_need_rw)
2130 * As we have a single beep enable bit for all fans,
2131 * only the first enabled fan has a writable attribute
2134 if (sysfs_chmod_file(&dev->kobj,
2135 it87_attributes_fan_beep[i],
2137 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2139 fan_beep_need_rw = 0;
2143 if (enable_pwm_interface) {
2144 for (i = 0; i < 3; i++) {
2145 if (sio_data->skip_pwm & (1 << i))
2147 err = sysfs_create_group(&dev->kobj,
2148 &it87_group_pwm[i]);
2152 if (!has_old_autopwm(data))
2154 err = sysfs_create_group(&dev->kobj,
2155 &it87_group_autopwm[i]);
2161 if (!sio_data->skip_vid) {
2162 data->vrm = vid_which_vrm();
2163 /* VID reading from Super-I/O config space if available */
2164 data->vid = sio_data->vid_value;
2165 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2170 /* Export labels for internal sensors */
2171 for (i = 0; i < 3; i++) {
2172 if (!(sio_data->internal & (1 << i)))
2174 err = sysfs_create_file(&dev->kobj,
2175 it87_attributes_label[i]);
2180 data->hwmon_dev = hwmon_device_register(dev);
2181 if (IS_ERR(data->hwmon_dev)) {
2182 err = PTR_ERR(data->hwmon_dev);
2189 it87_remove_files(dev);
2191 release_region(res->start, IT87_EC_EXTENT);
2196 static int __devexit it87_remove(struct platform_device *pdev)
2198 struct it87_data *data = platform_get_drvdata(pdev);
2200 hwmon_device_unregister(data->hwmon_dev);
2201 it87_remove_files(&pdev->dev);
2203 release_region(data->addr, IT87_EC_EXTENT);
2208 * Must be called with data->update_lock held, except during initialization.
2209 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2210 * would slow down the IT87 access and should not be necessary.
2212 static int it87_read_value(struct it87_data *data, u8 reg)
2214 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2215 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2219 * Must be called with data->update_lock held, except during initialization.
2220 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2221 * would slow down the IT87 access and should not be necessary.
2223 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2225 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2226 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2229 /* Return 1 if and only if the PWM interface is safe to use */
2230 static int __devinit it87_check_pwm(struct device *dev)
2232 struct it87_data *data = dev_get_drvdata(dev);
2234 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2235 * and polarity set to active low is sign that this is the case so we
2236 * disable pwm control to protect the user.
2238 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2239 if ((tmp & 0x87) == 0) {
2240 if (fix_pwm_polarity) {
2242 * The user asks us to attempt a chip reconfiguration.
2243 * This means switching to active high polarity and
2244 * inverting all fan speed values.
2249 for (i = 0; i < 3; i++)
2250 pwm[i] = it87_read_value(data,
2254 * If any fan is in automatic pwm mode, the polarity
2255 * might be correct, as suspicious as it seems, so we
2256 * better don't change anything (but still disable the
2259 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2260 dev_info(dev, "Reconfiguring PWM to "
2261 "active high polarity\n");
2262 it87_write_value(data, IT87_REG_FAN_CTL,
2264 for (i = 0; i < 3; i++)
2265 it87_write_value(data,
2271 dev_info(dev, "PWM configuration is "
2272 "too broken to be fixed\n");
2275 dev_info(dev, "Detected broken BIOS "
2276 "defaults, disabling PWM interface\n");
2278 } else if (fix_pwm_polarity) {
2279 dev_info(dev, "PWM configuration looks "
2280 "sane, won't touch\n");
2286 /* Called when we have found a new IT87. */
2287 static void __devinit it87_init_device(struct platform_device *pdev)
2289 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2290 struct it87_data *data = platform_get_drvdata(pdev);
2295 * For each PWM channel:
2296 * - If it is in automatic mode, setting to manual mode should set
2297 * the fan to full speed by default.
2298 * - If it is in manual mode, we need a mapping to temperature
2299 * channels to use when later setting to automatic mode later.
2300 * Use a 1:1 mapping by default (we are clueless.)
2301 * In both cases, the value can (and should) be changed by the user
2302 * prior to switching to a different mode.
2303 * Note that this is no longer needed for the IT8721F and later, as
2304 * these have separate registers for the temperature mapping and the
2305 * manual duty cycle.
2307 for (i = 0; i < 3; i++) {
2308 data->pwm_temp_map[i] = i;
2309 data->pwm_duty[i] = 0x7f; /* Full speed */
2310 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2314 * Some chips seem to have default value 0xff for all limit
2315 * registers. For low voltage limits it makes no sense and triggers
2316 * alarms, so change to 0 instead. For high temperature limits, it
2317 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2318 * but is still confusing, so change to 127 degrees C.
2320 for (i = 0; i < 8; i++) {
2321 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2323 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2325 for (i = 0; i < 3; i++) {
2326 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2328 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2332 * Temperature channels are not forcibly enabled, as they can be
2333 * set to two different sensor types and we can't guess which one
2334 * is correct for a given system. These channels can be enabled at
2335 * run-time through the temp{1-3}_type sysfs accessors if needed.
2338 /* Check if voltage monitors are reset manually or by some reason */
2339 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2340 if ((tmp & 0xff) == 0) {
2341 /* Enable all voltage monitors */
2342 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2345 /* Check if tachometers are reset manually or by some reason */
2346 mask = 0x70 & ~(sio_data->skip_fan << 4);
2347 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2348 if ((data->fan_main_ctrl & mask) == 0) {
2349 /* Enable all fan tachometers */
2350 data->fan_main_ctrl |= mask;
2351 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2352 data->fan_main_ctrl);
2354 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2356 /* Set tachometers to 16-bit mode if needed */
2357 if (has_16bit_fans(data)) {
2358 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2359 if (~tmp & 0x07 & data->has_fan) {
2361 "Setting fan1-3 to 16-bit mode\n");
2362 it87_write_value(data, IT87_REG_FAN_16BIT,
2365 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2366 if (data->type != it87 && data->type != it8781 &&
2367 data->type != it8782 && data->type != it8783) {
2369 data->has_fan |= (1 << 3); /* fan4 enabled */
2371 data->has_fan |= (1 << 4); /* fan5 enabled */
2375 /* Fan input pins may be used for alternative functions */
2376 data->has_fan &= ~sio_data->skip_fan;
2378 /* Start monitoring */
2379 it87_write_value(data, IT87_REG_CONFIG,
2380 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
2381 | (update_vbat ? 0x41 : 0x01));
2384 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2386 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2387 if (has_newer_autopwm(data)) {
2388 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2389 data->pwm_duty[nr] = it87_read_value(data,
2390 IT87_REG_PWM_DUTY(nr));
2392 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2393 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2394 else /* Manual mode */
2395 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2398 if (has_old_autopwm(data)) {
2401 for (i = 0; i < 5 ; i++)
2402 data->auto_temp[nr][i] = it87_read_value(data,
2403 IT87_REG_AUTO_TEMP(nr, i));
2404 for (i = 0; i < 3 ; i++)
2405 data->auto_pwm[nr][i] = it87_read_value(data,
2406 IT87_REG_AUTO_PWM(nr, i));
2410 static struct it87_data *it87_update_device(struct device *dev)
2412 struct it87_data *data = dev_get_drvdata(dev);
2415 mutex_lock(&data->update_lock);
2417 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2421 * Cleared after each update, so reenable. Value
2422 * returned by this read will be previous value
2424 it87_write_value(data, IT87_REG_CONFIG,
2425 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2427 for (i = 0; i <= 7; i++) {
2429 it87_read_value(data, IT87_REG_VIN(i));
2431 it87_read_value(data, IT87_REG_VIN_MIN(i));
2433 it87_read_value(data, IT87_REG_VIN_MAX(i));
2435 /* in8 (battery) has no limit registers */
2436 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
2438 for (i = 0; i < 5; i++) {
2439 /* Skip disabled fans */
2440 if (!(data->has_fan & (1 << i)))
2444 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2445 data->fan[i] = it87_read_value(data,
2447 /* Add high byte if in 16-bit mode */
2448 if (has_16bit_fans(data)) {
2449 data->fan[i] |= it87_read_value(data,
2450 IT87_REG_FANX[i]) << 8;
2451 data->fan_min[i] |= it87_read_value(data,
2452 IT87_REG_FANX_MIN[i]) << 8;
2455 for (i = 0; i < 3; i++) {
2456 if (!(data->has_temp & (1 << i)))
2459 it87_read_value(data, IT87_REG_TEMP(i));
2460 data->temp_high[i] =
2461 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2463 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2466 /* Newer chips don't have clock dividers */
2467 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2468 i = it87_read_value(data, IT87_REG_FAN_DIV);
2469 data->fan_div[0] = i & 0x07;
2470 data->fan_div[1] = (i >> 3) & 0x07;
2471 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2475 it87_read_value(data, IT87_REG_ALARM1) |
2476 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2477 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2478 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2480 data->fan_main_ctrl = it87_read_value(data,
2481 IT87_REG_FAN_MAIN_CTRL);
2482 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2483 for (i = 0; i < 3; i++)
2484 it87_update_pwm_ctrl(data, i);
2486 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2488 * The IT8705F does not have VID capability.
2489 * The IT8718F and later don't use IT87_REG_VID for the
2492 if (data->type == it8712 || data->type == it8716) {
2493 data->vid = it87_read_value(data, IT87_REG_VID);
2495 * The older IT8712F revisions had only 5 VID pins,
2496 * but we assume it is always safe to read 6 bits.
2500 data->last_updated = jiffies;
2504 mutex_unlock(&data->update_lock);
2509 static int __init it87_device_add(unsigned short address,
2510 const struct it87_sio_data *sio_data)
2512 struct resource res = {
2513 .start = address + IT87_EC_OFFSET,
2514 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2516 .flags = IORESOURCE_IO,
2520 err = acpi_check_resource_conflict(&res);
2524 pdev = platform_device_alloc(DRVNAME, address);
2527 pr_err("Device allocation failed\n");
2531 err = platform_device_add_resources(pdev, &res, 1);
2533 pr_err("Device resource addition failed (%d)\n", err);
2534 goto exit_device_put;
2537 err = platform_device_add_data(pdev, sio_data,
2538 sizeof(struct it87_sio_data));
2540 pr_err("Platform data allocation failed\n");
2541 goto exit_device_put;
2544 err = platform_device_add(pdev);
2546 pr_err("Device addition failed (%d)\n", err);
2547 goto exit_device_put;
2553 platform_device_put(pdev);
2558 static int __init sm_it87_init(void)
2561 unsigned short isa_address = 0;
2562 struct it87_sio_data sio_data;
2564 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2565 err = it87_find(&isa_address, &sio_data);
2568 err = platform_driver_register(&it87_driver);
2572 err = it87_device_add(isa_address, &sio_data);
2574 platform_driver_unregister(&it87_driver);
2581 static void __exit sm_it87_exit(void)
2583 platform_device_unregister(pdev);
2584 platform_driver_unregister(&it87_driver);
2588 MODULE_AUTHOR("Chris Gauthron, "
2589 "Jean Delvare <khali@linux-fr.org>");
2590 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2591 module_param(update_vbat, bool, 0);
2592 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2593 module_param(fix_pwm_polarity, bool, 0);
2594 MODULE_PARM_DESC(fix_pwm_polarity,
2595 "Force PWM polarity to active high (DANGEROUS)");
2596 MODULE_LICENSE("GPL");
2598 module_init(sm_it87_init);
2599 module_exit(sm_it87_exit);