2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
13 * Supports: IT8705F Super I/O chip w/LPC interface
14 * IT8712F Super I/O chip w/LPC interface
15 * IT8716F Super I/O chip w/LPC interface
16 * IT8718F Super I/O chip w/LPC interface
17 * IT8720F Super I/O chip w/LPC interface
18 * IT8721F Super I/O chip w/LPC interface
19 * IT8726F Super I/O chip w/LPC interface
20 * IT8728F Super I/O chip w/LPC interface
21 * IT8758E Super I/O chip w/LPC interface
22 * IT8782F Super I/O chip w/LPC interface
23 * IT8783E/F Super I/O chip w/LPC interface
24 * Sis950 A clone of the IT8705F
26 * Copyright (C) 2001 Chris Gauthron
27 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
44 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/slab.h>
49 #include <linux/jiffies.h>
50 #include <linux/platform_device.h>
51 #include <linux/hwmon.h>
52 #include <linux/hwmon-sysfs.h>
53 #include <linux/hwmon-vid.h>
54 #include <linux/err.h>
55 #include <linux/mutex.h>
56 #include <linux/sysfs.h>
57 #include <linux/string.h>
58 #include <linux/dmi.h>
59 #include <linux/acpi.h>
61 #include <linux/version.h>
63 #ifndef request_muxed_region
64 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 28)
65 #define request_muxed_region(start,n,name) __request_region(&ioport_resource, (start), (n), (name))
67 #define IORESOURCE_MUXED 0x00400000
68 #define request_muxed_region(start,n,name) __request_region(&ioport_resource, (start), (n), (name), IORESOURCE_MUXED)
72 /* Red Hat EL5 includes backports of these functions, so we can't redefine
74 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
75 #if !(defined RHEL_MAJOR && RHEL_MAJOR == 5 && RHEL_MINOR >= 5)
76 static inline int strict_strtoul(const char *cp, unsigned int base,
79 *res = simple_strtoul(cp, NULL, base);
83 static inline int strict_strtol(const char *cp, unsigned int base, long *res)
85 *res = simple_strtol(cp, NULL, base);
91 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
92 #define kstrtoul strict_strtoul
93 #define kstrtol strict_strtol
96 #define DRVNAME "it87"
98 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8782,
101 static unsigned short force_id;
102 module_param(force_id, ushort, 0);
103 MODULE_PARM_DESC(force_id, "Override the detected device ID");
105 static struct platform_device *pdev;
107 #define REG 0x2e /* The register to read/write */
108 #define DEV 0x07 /* Register: Logical device select */
109 #define VAL 0x2f /* The value to read/write */
110 #define PME 0x04 /* The device with the fan registers in it */
112 /* The device with the IT8718F/IT8720F VID value in it */
115 #define DEVID 0x20 /* Register: Device ID */
116 #define DEVREV 0x22 /* Register: Device Revision */
118 static inline int superio_inb(int reg)
124 static inline void superio_outb(int reg, int val)
130 static int superio_inw(int reg)
140 static inline void superio_select(int ldn)
146 static inline int superio_enter(void)
149 * Try to reserve REG and REG + 1 for exclusive access.
151 if (!request_muxed_region(REG, 2, DRVNAME))
161 static inline void superio_exit(void)
165 release_region(REG, 2);
168 /* Logical device 4 registers */
169 #define IT8712F_DEVID 0x8712
170 #define IT8705F_DEVID 0x8705
171 #define IT8716F_DEVID 0x8716
172 #define IT8718F_DEVID 0x8718
173 #define IT8720F_DEVID 0x8720
174 #define IT8721F_DEVID 0x8721
175 #define IT8726F_DEVID 0x8726
176 #define IT8728F_DEVID 0x8728
177 #define IT8782F_DEVID 0x8782
178 #define IT8783E_DEVID 0x8783
179 #define IT87_ACT_REG 0x30
180 #define IT87_BASE_REG 0x60
182 /* Logical device 7 registers (IT8712F and later) */
183 #define IT87_SIO_GPIO1_REG 0x25
184 #define IT87_SIO_GPIO3_REG 0x27
185 #define IT87_SIO_GPIO5_REG 0x29
186 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
187 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
188 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
189 #define IT87_SIO_VID_REG 0xfc /* VID value */
190 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
192 /* Update battery voltage after every reading if true */
193 static bool update_vbat;
195 /* Not all BIOSes properly configure the PWM registers */
196 static bool fix_pwm_polarity;
198 /* Many IT87 constants specified below */
200 /* Length of ISA address segment */
201 #define IT87_EXTENT 8
203 /* Length of ISA address segment for Environmental Controller */
204 #define IT87_EC_EXTENT 2
206 /* Offset of EC registers from ISA base address */
207 #define IT87_EC_OFFSET 5
209 /* Where are the ISA address/data registers relative to the EC base address */
210 #define IT87_ADDR_REG_OFFSET 0
211 #define IT87_DATA_REG_OFFSET 1
213 /*----- The IT87 registers -----*/
215 #define IT87_REG_CONFIG 0x00
217 #define IT87_REG_ALARM1 0x01
218 #define IT87_REG_ALARM2 0x02
219 #define IT87_REG_ALARM3 0x03
222 * The IT8718F and IT8720F have the VID value in a different register, in
223 * Super-I/O configuration space.
225 #define IT87_REG_VID 0x0a
227 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
228 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
231 #define IT87_REG_FAN_DIV 0x0b
232 #define IT87_REG_FAN_16BIT 0x0c
234 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
236 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
237 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
238 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
239 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
240 #define IT87_REG_FAN_MAIN_CTRL 0x13
241 #define IT87_REG_FAN_CTL 0x14
242 #define IT87_REG_PWM(nr) (0x15 + (nr))
243 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
245 #define IT87_REG_VIN(nr) (0x20 + (nr))
246 #define IT87_REG_TEMP(nr) (0x29 + (nr))
248 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
249 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
250 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
251 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
253 #define IT87_REG_VIN_ENABLE 0x50
254 #define IT87_REG_TEMP_ENABLE 0x51
255 #define IT87_REG_TEMP_EXTRA 0x55
256 #define IT87_REG_BEEP_ENABLE 0x5c
258 #define IT87_REG_CHIPID 0x58
260 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
261 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
264 struct it87_sio_data {
266 /* Values read from Super-I/O config space */
270 u8 internal; /* Internal sensors can be labeled */
271 /* Features skipped based on config or DMI */
280 * For each registered chip, we need to keep some data in memory.
281 * The structure is dynamically allocated.
284 struct device *hwmon_dev;
290 struct mutex update_lock;
291 char valid; /* !=0 if following fields are valid */
292 unsigned long last_updated; /* In jiffies */
294 u16 in_scaled; /* Internal voltage sensors are scaled */
295 u8 in[9]; /* Register value */
296 u8 in_max[8]; /* Register value */
297 u8 in_min[8]; /* Register value */
298 u8 has_fan; /* Bitfield, fans enabled */
299 u16 fan[5]; /* Register values, possibly combined */
300 u16 fan_min[5]; /* Register values, possibly combined */
301 u8 has_temp; /* Bitfield, temp sensors enabled */
302 s8 temp[3]; /* Register value */
303 s8 temp_high[3]; /* Register value */
304 s8 temp_low[3]; /* Register value */
305 u8 sensor; /* Register value */
306 u8 fan_div[3]; /* Register encoding, shifted right */
307 u8 vid; /* Register encoding, combined */
309 u32 alarms; /* Register encoding, combined */
310 u8 beeps; /* Register encoding */
311 u8 fan_main_ctrl; /* Register value */
312 u8 fan_ctl; /* Register value */
315 * The following 3 arrays correspond to the same registers up to
316 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
317 * 7, and we want to preserve settings on mode changes, so we have
318 * to track all values separately.
319 * Starting with the IT8721F, the manual PWM duty cycles are stored
320 * in separate registers (8-bit values), so the separate tracking
321 * is no longer needed, but it is still done to keep the driver
324 u8 pwm_ctrl[3]; /* Register value */
325 u8 pwm_duty[3]; /* Manual PWM value set by user */
326 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
328 /* Automatic fan speed control registers */
329 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
330 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
333 static inline int has_12mv_adc(const struct it87_data *data)
336 * IT8721F and later have a 12 mV ADC, also with internal scaling
337 * on selected inputs.
339 return data->type == it8721
340 || data->type == it8728;
343 static inline int has_newer_autopwm(const struct it87_data *data)
346 * IT8721F and later have separate registers for the temperature
347 * mapping and the manual duty cycle.
349 return data->type == it8721
350 || data->type == it8728;
353 static int adc_lsb(const struct it87_data *data, int nr)
355 int lsb = has_12mv_adc(data) ? 12 : 16;
356 if (data->in_scaled & (1 << nr))
361 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
363 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
364 return SENSORS_LIMIT(val, 0, 255);
367 static int in_from_reg(const struct it87_data *data, int nr, int val)
369 return val * adc_lsb(data, nr);
372 static inline u8 FAN_TO_REG(long rpm, int div)
376 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
377 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
381 static inline u16 FAN16_TO_REG(long rpm)
385 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
388 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
389 1350000 / ((val) * (div)))
390 /* The divider is fixed to 2 in 16-bit mode */
391 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
392 1350000 / ((val) * 2))
394 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
395 ((val) + 500) / 1000), -128, 127))
396 #define TEMP_FROM_REG(val) ((val) * 1000)
398 static u8 pwm_to_reg(const struct it87_data *data, long val)
400 if (has_newer_autopwm(data))
406 static int pwm_from_reg(const struct it87_data *data, u8 reg)
408 if (has_newer_autopwm(data))
411 return (reg & 0x7f) << 1;
415 static int DIV_TO_REG(int val)
418 while (answer < 7 && (val >>= 1))
422 #define DIV_FROM_REG(val) (1 << (val))
424 static const unsigned int pwm_freq[8] = {
435 static inline int has_16bit_fans(const struct it87_data *data)
438 * IT8705F Datasheet 0.4.1, 3h == Version G.
439 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
440 * These are the first revisions with 16-bit tachometer support.
442 return (data->type == it87 && data->revision >= 0x03)
443 || (data->type == it8712 && data->revision >= 0x08)
444 || data->type == it8716
445 || data->type == it8718
446 || data->type == it8720
447 || data->type == it8721
448 || data->type == it8728
449 || data->type == it8782
450 || data->type == it8783;
453 static inline int has_old_autopwm(const struct it87_data *data)
456 * The old automatic fan speed control interface is implemented
457 * by IT8705F chips up to revision F and IT8712F chips up to
460 return (data->type == it87 && data->revision < 0x03)
461 || (data->type == it8712 && data->revision < 0x08);
464 static int it87_probe(struct platform_device *pdev);
465 static int __devexit it87_remove(struct platform_device *pdev);
467 static int it87_read_value(struct it87_data *data, u8 reg);
468 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
469 static struct it87_data *it87_update_device(struct device *dev);
470 static int it87_check_pwm(struct device *dev);
471 static void it87_init_device(struct platform_device *pdev);
474 static struct platform_driver it87_driver = {
476 .owner = THIS_MODULE,
480 .remove = __devexit_p(it87_remove),
483 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
486 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
487 int nr = sensor_attr->index;
489 struct it87_data *data = it87_update_device(dev);
490 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
493 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
496 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
497 int nr = sensor_attr->index;
499 struct it87_data *data = it87_update_device(dev);
500 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
503 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
506 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
507 int nr = sensor_attr->index;
509 struct it87_data *data = it87_update_device(dev);
510 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
513 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
514 const char *buf, size_t count)
516 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
517 int nr = sensor_attr->index;
519 struct it87_data *data = dev_get_drvdata(dev);
522 if (kstrtoul(buf, 10, &val) < 0)
525 mutex_lock(&data->update_lock);
526 data->in_min[nr] = in_to_reg(data, nr, val);
527 it87_write_value(data, IT87_REG_VIN_MIN(nr),
529 mutex_unlock(&data->update_lock);
532 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
533 const char *buf, size_t count)
535 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
536 int nr = sensor_attr->index;
538 struct it87_data *data = dev_get_drvdata(dev);
541 if (kstrtoul(buf, 10, &val) < 0)
544 mutex_lock(&data->update_lock);
545 data->in_max[nr] = in_to_reg(data, nr, val);
546 it87_write_value(data, IT87_REG_VIN_MAX(nr),
548 mutex_unlock(&data->update_lock);
552 #define show_in_offset(offset) \
553 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
554 show_in, NULL, offset);
556 #define limit_in_offset(offset) \
557 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
558 show_in_min, set_in_min, offset); \
559 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
560 show_in_max, set_in_max, offset);
581 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
584 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
585 int nr = sensor_attr->index;
587 struct it87_data *data = it87_update_device(dev);
588 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
590 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
593 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
594 int nr = sensor_attr->index;
596 struct it87_data *data = it87_update_device(dev);
597 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
599 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
602 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
603 int nr = sensor_attr->index;
605 struct it87_data *data = it87_update_device(dev);
606 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
608 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
611 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
612 int nr = sensor_attr->index;
614 struct it87_data *data = dev_get_drvdata(dev);
617 if (kstrtol(buf, 10, &val) < 0)
620 mutex_lock(&data->update_lock);
621 data->temp_high[nr] = TEMP_TO_REG(val);
622 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
623 mutex_unlock(&data->update_lock);
626 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
629 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
630 int nr = sensor_attr->index;
632 struct it87_data *data = dev_get_drvdata(dev);
635 if (kstrtol(buf, 10, &val) < 0)
638 mutex_lock(&data->update_lock);
639 data->temp_low[nr] = TEMP_TO_REG(val);
640 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
641 mutex_unlock(&data->update_lock);
644 #define show_temp_offset(offset) \
645 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
646 show_temp, NULL, offset - 1); \
647 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
648 show_temp_max, set_temp_max, offset - 1); \
649 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
650 show_temp_min, set_temp_min, offset - 1);
656 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
659 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
660 int nr = sensor_attr->index;
661 struct it87_data *data = it87_update_device(dev);
662 u8 reg = data->sensor; /* In case value is updated while used */
665 return sprintf(buf, "3\n"); /* thermal diode */
667 return sprintf(buf, "4\n"); /* thermistor */
668 return sprintf(buf, "0\n"); /* disabled */
670 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
671 const char *buf, size_t count)
673 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
674 int nr = sensor_attr->index;
676 struct it87_data *data = dev_get_drvdata(dev);
680 if (kstrtol(buf, 10, &val) < 0)
683 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
686 if (val == 2) { /* backwards compatibility */
687 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
691 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
699 mutex_lock(&data->update_lock);
701 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
702 data->valid = 0; /* Force cache refresh */
703 mutex_unlock(&data->update_lock);
706 #define show_sensor_offset(offset) \
707 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
708 show_sensor, set_sensor, offset - 1);
710 show_sensor_offset(1);
711 show_sensor_offset(2);
712 show_sensor_offset(3);
716 static int pwm_mode(const struct it87_data *data, int nr)
718 int ctrl = data->fan_main_ctrl & (1 << nr);
720 if (ctrl == 0) /* Full speed */
722 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
724 else /* Manual mode */
728 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
731 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
732 int nr = sensor_attr->index;
734 struct it87_data *data = it87_update_device(dev);
735 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
736 DIV_FROM_REG(data->fan_div[nr])));
738 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
741 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
742 int nr = sensor_attr->index;
744 struct it87_data *data = it87_update_device(dev);
745 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
746 DIV_FROM_REG(data->fan_div[nr])));
748 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
751 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
752 int nr = sensor_attr->index;
754 struct it87_data *data = it87_update_device(dev);
755 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
757 static ssize_t show_pwm_enable(struct device *dev,
758 struct device_attribute *attr, char *buf)
760 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
761 int nr = sensor_attr->index;
763 struct it87_data *data = it87_update_device(dev);
764 return sprintf(buf, "%d\n", pwm_mode(data, nr));
766 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
769 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
770 int nr = sensor_attr->index;
772 struct it87_data *data = it87_update_device(dev);
773 return sprintf(buf, "%d\n",
774 pwm_from_reg(data, data->pwm_duty[nr]));
776 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
779 struct it87_data *data = it87_update_device(dev);
780 int index = (data->fan_ctl >> 4) & 0x07;
782 return sprintf(buf, "%u\n", pwm_freq[index]);
784 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
785 const char *buf, size_t count)
787 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
788 int nr = sensor_attr->index;
790 struct it87_data *data = dev_get_drvdata(dev);
794 if (kstrtol(buf, 10, &val) < 0)
797 mutex_lock(&data->update_lock);
798 reg = it87_read_value(data, IT87_REG_FAN_DIV);
801 data->fan_div[nr] = reg & 0x07;
804 data->fan_div[nr] = (reg >> 3) & 0x07;
807 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
811 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
812 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
813 mutex_unlock(&data->update_lock);
816 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
817 const char *buf, size_t count)
819 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
820 int nr = sensor_attr->index;
822 struct it87_data *data = dev_get_drvdata(dev);
827 if (kstrtoul(buf, 10, &val) < 0)
830 mutex_lock(&data->update_lock);
831 old = it87_read_value(data, IT87_REG_FAN_DIV);
833 /* Save fan min limit */
834 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
839 data->fan_div[nr] = DIV_TO_REG(val);
843 data->fan_div[nr] = 1;
845 data->fan_div[nr] = 3;
848 val |= (data->fan_div[0] & 0x07);
849 val |= (data->fan_div[1] & 0x07) << 3;
850 if (data->fan_div[2] == 3)
852 it87_write_value(data, IT87_REG_FAN_DIV, val);
854 /* Restore fan min limit */
855 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
856 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
858 mutex_unlock(&data->update_lock);
862 /* Returns 0 if OK, -EINVAL otherwise */
863 static int check_trip_points(struct device *dev, int nr)
865 const struct it87_data *data = dev_get_drvdata(dev);
868 if (has_old_autopwm(data)) {
869 for (i = 0; i < 3; i++) {
870 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
873 for (i = 0; i < 2; i++) {
874 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
880 dev_err(dev, "Inconsistent trip points, not switching to "
882 dev_err(dev, "Adjust the trip points and try again\n");
887 static ssize_t set_pwm_enable(struct device *dev,
888 struct device_attribute *attr, const char *buf, size_t count)
890 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
891 int nr = sensor_attr->index;
893 struct it87_data *data = dev_get_drvdata(dev);
896 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
899 /* Check trip points before switching to automatic mode */
901 if (check_trip_points(dev, nr) < 0)
905 mutex_lock(&data->update_lock);
909 /* make sure the fan is on when in on/off mode */
910 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
911 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
912 /* set on/off mode */
913 data->fan_main_ctrl &= ~(1 << nr);
914 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
915 data->fan_main_ctrl);
917 if (val == 1) /* Manual mode */
918 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
919 data->pwm_temp_map[nr] :
921 else /* Automatic mode */
922 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
923 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
924 /* set SmartGuardian mode */
925 data->fan_main_ctrl |= (1 << nr);
926 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
927 data->fan_main_ctrl);
930 mutex_unlock(&data->update_lock);
933 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
934 const char *buf, size_t count)
936 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
937 int nr = sensor_attr->index;
939 struct it87_data *data = dev_get_drvdata(dev);
942 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
945 mutex_lock(&data->update_lock);
946 if (has_newer_autopwm(data)) {
948 * If we are in automatic mode, the PWM duty cycle register
949 * is read-only so we can't write the value.
951 if (data->pwm_ctrl[nr] & 0x80) {
952 mutex_unlock(&data->update_lock);
955 data->pwm_duty[nr] = pwm_to_reg(data, val);
956 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
959 data->pwm_duty[nr] = pwm_to_reg(data, val);
961 * If we are in manual mode, write the duty cycle immediately;
962 * otherwise, just store it for later use.
964 if (!(data->pwm_ctrl[nr] & 0x80)) {
965 data->pwm_ctrl[nr] = data->pwm_duty[nr];
966 it87_write_value(data, IT87_REG_PWM(nr),
970 mutex_unlock(&data->update_lock);
973 static ssize_t set_pwm_freq(struct device *dev,
974 struct device_attribute *attr, const char *buf, size_t count)
976 struct it87_data *data = dev_get_drvdata(dev);
980 if (kstrtoul(buf, 10, &val) < 0)
983 /* Search for the nearest available frequency */
984 for (i = 0; i < 7; i++) {
985 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
989 mutex_lock(&data->update_lock);
990 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
991 data->fan_ctl |= i << 4;
992 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
993 mutex_unlock(&data->update_lock);
997 static ssize_t show_pwm_temp_map(struct device *dev,
998 struct device_attribute *attr, char *buf)
1000 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1001 int nr = sensor_attr->index;
1003 struct it87_data *data = it87_update_device(dev);
1006 if (data->pwm_temp_map[nr] < 3)
1007 map = 1 << data->pwm_temp_map[nr];
1009 map = 0; /* Should never happen */
1010 return sprintf(buf, "%d\n", map);
1012 static ssize_t set_pwm_temp_map(struct device *dev,
1013 struct device_attribute *attr, const char *buf, size_t count)
1015 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1016 int nr = sensor_attr->index;
1018 struct it87_data *data = dev_get_drvdata(dev);
1023 * This check can go away if we ever support automatic fan speed
1024 * control on newer chips.
1026 if (!has_old_autopwm(data)) {
1027 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1031 if (kstrtol(buf, 10, &val) < 0)
1048 mutex_lock(&data->update_lock);
1049 data->pwm_temp_map[nr] = reg;
1051 * If we are in automatic mode, write the temp mapping immediately;
1052 * otherwise, just store it for later use.
1054 if (data->pwm_ctrl[nr] & 0x80) {
1055 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1056 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1058 mutex_unlock(&data->update_lock);
1062 static ssize_t show_auto_pwm(struct device *dev,
1063 struct device_attribute *attr, char *buf)
1065 struct it87_data *data = it87_update_device(dev);
1066 struct sensor_device_attribute_2 *sensor_attr =
1067 to_sensor_dev_attr_2(attr);
1068 int nr = sensor_attr->nr;
1069 int point = sensor_attr->index;
1071 return sprintf(buf, "%d\n",
1072 pwm_from_reg(data, data->auto_pwm[nr][point]));
1075 static ssize_t set_auto_pwm(struct device *dev,
1076 struct device_attribute *attr, const char *buf, size_t count)
1078 struct it87_data *data = dev_get_drvdata(dev);
1079 struct sensor_device_attribute_2 *sensor_attr =
1080 to_sensor_dev_attr_2(attr);
1081 int nr = sensor_attr->nr;
1082 int point = sensor_attr->index;
1085 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1088 mutex_lock(&data->update_lock);
1089 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1090 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1091 data->auto_pwm[nr][point]);
1092 mutex_unlock(&data->update_lock);
1096 static ssize_t show_auto_temp(struct device *dev,
1097 struct device_attribute *attr, char *buf)
1099 struct it87_data *data = it87_update_device(dev);
1100 struct sensor_device_attribute_2 *sensor_attr =
1101 to_sensor_dev_attr_2(attr);
1102 int nr = sensor_attr->nr;
1103 int point = sensor_attr->index;
1105 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1108 static ssize_t set_auto_temp(struct device *dev,
1109 struct device_attribute *attr, const char *buf, size_t count)
1111 struct it87_data *data = dev_get_drvdata(dev);
1112 struct sensor_device_attribute_2 *sensor_attr =
1113 to_sensor_dev_attr_2(attr);
1114 int nr = sensor_attr->nr;
1115 int point = sensor_attr->index;
1118 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1121 mutex_lock(&data->update_lock);
1122 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1123 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1124 data->auto_temp[nr][point]);
1125 mutex_unlock(&data->update_lock);
1129 #define show_fan_offset(offset) \
1130 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1131 show_fan, NULL, offset - 1); \
1132 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1133 show_fan_min, set_fan_min, offset - 1); \
1134 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1135 show_fan_div, set_fan_div, offset - 1);
1141 #define show_pwm_offset(offset) \
1142 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1143 show_pwm_enable, set_pwm_enable, offset - 1); \
1144 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1145 show_pwm, set_pwm, offset - 1); \
1146 static DEVICE_ATTR(pwm##offset##_freq, \
1147 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1148 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1149 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1150 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1152 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1153 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1155 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1156 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1158 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1159 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1161 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1162 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1163 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1164 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1166 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1167 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1169 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1170 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1172 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1173 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1175 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1176 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1183 /* A different set of callbacks for 16-bit fans */
1184 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1187 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1188 int nr = sensor_attr->index;
1189 struct it87_data *data = it87_update_device(dev);
1190 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1193 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1196 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1197 int nr = sensor_attr->index;
1198 struct it87_data *data = it87_update_device(dev);
1199 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1202 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1203 const char *buf, size_t count)
1205 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1206 int nr = sensor_attr->index;
1207 struct it87_data *data = dev_get_drvdata(dev);
1210 if (kstrtol(buf, 10, &val) < 0)
1213 mutex_lock(&data->update_lock);
1214 data->fan_min[nr] = FAN16_TO_REG(val);
1215 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1216 data->fan_min[nr] & 0xff);
1217 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1218 data->fan_min[nr] >> 8);
1219 mutex_unlock(&data->update_lock);
1224 * We want to use the same sysfs file names as 8-bit fans, but we need
1225 * different variable names, so we have to use SENSOR_ATTR instead of
1226 * SENSOR_DEVICE_ATTR.
1228 #define show_fan16_offset(offset) \
1229 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1230 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1231 show_fan16, NULL, offset - 1); \
1232 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1233 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1234 show_fan16_min, set_fan16_min, offset - 1)
1236 show_fan16_offset(1);
1237 show_fan16_offset(2);
1238 show_fan16_offset(3);
1239 show_fan16_offset(4);
1240 show_fan16_offset(5);
1243 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1246 struct it87_data *data = it87_update_device(dev);
1247 return sprintf(buf, "%u\n", data->alarms);
1249 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1251 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1254 int bitnr = to_sensor_dev_attr(attr)->index;
1255 struct it87_data *data = it87_update_device(dev);
1256 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1259 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1260 *attr, const char *buf, size_t count)
1262 struct it87_data *data = dev_get_drvdata(dev);
1266 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1269 mutex_lock(&data->update_lock);
1270 config = it87_read_value(data, IT87_REG_CONFIG);
1275 it87_write_value(data, IT87_REG_CONFIG, config);
1276 /* Invalidate cache to force re-read */
1279 mutex_unlock(&data->update_lock);
1284 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1285 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1286 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1287 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1288 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1289 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1290 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1291 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1292 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1293 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1294 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1295 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1296 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1297 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1298 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1299 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1300 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1301 show_alarm, clear_intrusion, 4);
1303 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1306 int bitnr = to_sensor_dev_attr(attr)->index;
1307 struct it87_data *data = it87_update_device(dev);
1308 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1310 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1311 const char *buf, size_t count)
1313 int bitnr = to_sensor_dev_attr(attr)->index;
1314 struct it87_data *data = dev_get_drvdata(dev);
1317 if (kstrtol(buf, 10, &val) < 0
1318 || (val != 0 && val != 1))
1321 mutex_lock(&data->update_lock);
1322 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1324 data->beeps |= (1 << bitnr);
1326 data->beeps &= ~(1 << bitnr);
1327 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1328 mutex_unlock(&data->update_lock);
1332 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1333 show_beep, set_beep, 1);
1334 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1335 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1336 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1337 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1338 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1339 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1340 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1341 /* fanX_beep writability is set later */
1342 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1343 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1344 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1345 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1346 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1347 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1348 show_beep, set_beep, 2);
1349 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1350 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1352 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1355 struct it87_data *data = dev_get_drvdata(dev);
1356 return sprintf(buf, "%u\n", data->vrm);
1358 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1359 const char *buf, size_t count)
1361 struct it87_data *data = dev_get_drvdata(dev);
1364 if (kstrtoul(buf, 10, &val) < 0)
1371 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1373 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1376 struct it87_data *data = it87_update_device(dev);
1377 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1379 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1381 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1384 static const char * const labels[] = {
1389 static const char * const labels_it8721[] = {
1394 struct it87_data *data = dev_get_drvdata(dev);
1395 int nr = to_sensor_dev_attr(attr)->index;
1397 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1400 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1401 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1402 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1404 static ssize_t show_name(struct device *dev, struct device_attribute
1405 *devattr, char *buf)
1407 struct it87_data *data = dev_get_drvdata(dev);
1408 return sprintf(buf, "%s\n", data->name);
1410 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1412 static struct attribute *it87_attributes_in[9][5] = {
1414 &sensor_dev_attr_in0_input.dev_attr.attr,
1415 &sensor_dev_attr_in0_min.dev_attr.attr,
1416 &sensor_dev_attr_in0_max.dev_attr.attr,
1417 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1420 &sensor_dev_attr_in1_input.dev_attr.attr,
1421 &sensor_dev_attr_in1_min.dev_attr.attr,
1422 &sensor_dev_attr_in1_max.dev_attr.attr,
1423 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1426 &sensor_dev_attr_in2_input.dev_attr.attr,
1427 &sensor_dev_attr_in2_min.dev_attr.attr,
1428 &sensor_dev_attr_in2_max.dev_attr.attr,
1429 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1432 &sensor_dev_attr_in3_input.dev_attr.attr,
1433 &sensor_dev_attr_in3_min.dev_attr.attr,
1434 &sensor_dev_attr_in3_max.dev_attr.attr,
1435 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1438 &sensor_dev_attr_in4_input.dev_attr.attr,
1439 &sensor_dev_attr_in4_min.dev_attr.attr,
1440 &sensor_dev_attr_in4_max.dev_attr.attr,
1441 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1444 &sensor_dev_attr_in5_input.dev_attr.attr,
1445 &sensor_dev_attr_in5_min.dev_attr.attr,
1446 &sensor_dev_attr_in5_max.dev_attr.attr,
1447 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1450 &sensor_dev_attr_in6_input.dev_attr.attr,
1451 &sensor_dev_attr_in6_min.dev_attr.attr,
1452 &sensor_dev_attr_in6_max.dev_attr.attr,
1453 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1456 &sensor_dev_attr_in7_input.dev_attr.attr,
1457 &sensor_dev_attr_in7_min.dev_attr.attr,
1458 &sensor_dev_attr_in7_max.dev_attr.attr,
1459 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1462 &sensor_dev_attr_in8_input.dev_attr.attr,
1466 static const struct attribute_group it87_group_in[9] = {
1467 { .attrs = it87_attributes_in[0] },
1468 { .attrs = it87_attributes_in[1] },
1469 { .attrs = it87_attributes_in[2] },
1470 { .attrs = it87_attributes_in[3] },
1471 { .attrs = it87_attributes_in[4] },
1472 { .attrs = it87_attributes_in[5] },
1473 { .attrs = it87_attributes_in[6] },
1474 { .attrs = it87_attributes_in[7] },
1475 { .attrs = it87_attributes_in[8] },
1478 static struct attribute *it87_attributes_temp[3][6] = {
1480 &sensor_dev_attr_temp1_input.dev_attr.attr,
1481 &sensor_dev_attr_temp1_max.dev_attr.attr,
1482 &sensor_dev_attr_temp1_min.dev_attr.attr,
1483 &sensor_dev_attr_temp1_type.dev_attr.attr,
1484 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1487 &sensor_dev_attr_temp2_input.dev_attr.attr,
1488 &sensor_dev_attr_temp2_max.dev_attr.attr,
1489 &sensor_dev_attr_temp2_min.dev_attr.attr,
1490 &sensor_dev_attr_temp2_type.dev_attr.attr,
1491 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1494 &sensor_dev_attr_temp3_input.dev_attr.attr,
1495 &sensor_dev_attr_temp3_max.dev_attr.attr,
1496 &sensor_dev_attr_temp3_min.dev_attr.attr,
1497 &sensor_dev_attr_temp3_type.dev_attr.attr,
1498 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1502 static const struct attribute_group it87_group_temp[3] = {
1503 { .attrs = it87_attributes_temp[0] },
1504 { .attrs = it87_attributes_temp[1] },
1505 { .attrs = it87_attributes_temp[2] },
1508 static struct attribute *it87_attributes[] = {
1509 &dev_attr_alarms.attr,
1510 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1511 &dev_attr_name.attr,
1515 static const struct attribute_group it87_group = {
1516 .attrs = it87_attributes,
1519 static struct attribute *it87_attributes_in_beep[] = {
1520 &sensor_dev_attr_in0_beep.dev_attr.attr,
1521 &sensor_dev_attr_in1_beep.dev_attr.attr,
1522 &sensor_dev_attr_in2_beep.dev_attr.attr,
1523 &sensor_dev_attr_in3_beep.dev_attr.attr,
1524 &sensor_dev_attr_in4_beep.dev_attr.attr,
1525 &sensor_dev_attr_in5_beep.dev_attr.attr,
1526 &sensor_dev_attr_in6_beep.dev_attr.attr,
1527 &sensor_dev_attr_in7_beep.dev_attr.attr,
1531 static struct attribute *it87_attributes_temp_beep[] = {
1532 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1533 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1534 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1537 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1538 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1539 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1540 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1543 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1544 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1545 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1548 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1549 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1550 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1553 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1554 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1555 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1558 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1559 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1560 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1564 static const struct attribute_group it87_group_fan16[5] = {
1565 { .attrs = it87_attributes_fan16[0] },
1566 { .attrs = it87_attributes_fan16[1] },
1567 { .attrs = it87_attributes_fan16[2] },
1568 { .attrs = it87_attributes_fan16[3] },
1569 { .attrs = it87_attributes_fan16[4] },
1572 static struct attribute *it87_attributes_fan[3][4+1] = { {
1573 &sensor_dev_attr_fan1_input.dev_attr.attr,
1574 &sensor_dev_attr_fan1_min.dev_attr.attr,
1575 &sensor_dev_attr_fan1_div.dev_attr.attr,
1576 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1579 &sensor_dev_attr_fan2_input.dev_attr.attr,
1580 &sensor_dev_attr_fan2_min.dev_attr.attr,
1581 &sensor_dev_attr_fan2_div.dev_attr.attr,
1582 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1585 &sensor_dev_attr_fan3_input.dev_attr.attr,
1586 &sensor_dev_attr_fan3_min.dev_attr.attr,
1587 &sensor_dev_attr_fan3_div.dev_attr.attr,
1588 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1592 static const struct attribute_group it87_group_fan[3] = {
1593 { .attrs = it87_attributes_fan[0] },
1594 { .attrs = it87_attributes_fan[1] },
1595 { .attrs = it87_attributes_fan[2] },
1598 static const struct attribute_group *
1599 it87_get_fan_group(const struct it87_data *data)
1601 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1604 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1605 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1606 &sensor_dev_attr_pwm1.dev_attr.attr,
1607 &dev_attr_pwm1_freq.attr,
1608 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1611 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1612 &sensor_dev_attr_pwm2.dev_attr.attr,
1613 &dev_attr_pwm2_freq.attr,
1614 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1617 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1618 &sensor_dev_attr_pwm3.dev_attr.attr,
1619 &dev_attr_pwm3_freq.attr,
1620 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1624 static const struct attribute_group it87_group_pwm[3] = {
1625 { .attrs = it87_attributes_pwm[0] },
1626 { .attrs = it87_attributes_pwm[1] },
1627 { .attrs = it87_attributes_pwm[2] },
1630 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1631 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1632 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1633 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1634 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1635 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1636 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1637 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1638 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1639 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1642 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1643 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1644 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1645 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1646 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1647 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1648 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1649 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1650 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1653 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1654 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1655 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1656 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1657 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1658 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1659 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1660 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1661 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1665 static const struct attribute_group it87_group_autopwm[3] = {
1666 { .attrs = it87_attributes_autopwm[0] },
1667 { .attrs = it87_attributes_autopwm[1] },
1668 { .attrs = it87_attributes_autopwm[2] },
1671 static struct attribute *it87_attributes_fan_beep[] = {
1672 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1673 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1674 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1675 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1676 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1679 static struct attribute *it87_attributes_vid[] = {
1681 &dev_attr_cpu0_vid.attr,
1685 static const struct attribute_group it87_group_vid = {
1686 .attrs = it87_attributes_vid,
1689 static struct attribute *it87_attributes_label[] = {
1690 &sensor_dev_attr_in3_label.dev_attr.attr,
1691 &sensor_dev_attr_in7_label.dev_attr.attr,
1692 &sensor_dev_attr_in8_label.dev_attr.attr,
1696 static const struct attribute_group it87_group_label = {
1697 .attrs = it87_attributes_label,
1700 /* SuperIO detection - will change isa_address if a chip is found */
1701 static int __init it87_find(unsigned short *address,
1702 struct it87_sio_data *sio_data)
1706 const char *board_vendor, *board_name;
1708 err = superio_enter();
1713 chip_type = force_id ? force_id : superio_inw(DEVID);
1715 switch (chip_type) {
1717 sio_data->type = it87;
1720 sio_data->type = it8712;
1724 sio_data->type = it8716;
1727 sio_data->type = it8718;
1730 sio_data->type = it8720;
1733 sio_data->type = it8721;
1736 sio_data->type = it8728;
1739 sio_data->type = it8782;
1742 sio_data->type = it8783;
1744 case 0xffff: /* No device at all */
1747 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1751 superio_select(PME);
1752 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1753 pr_info("Device not activated, skipping\n");
1757 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1758 if (*address == 0) {
1759 pr_info("Base address not set, skipping\n");
1764 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1765 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1766 chip_type, *address, sio_data->revision);
1768 /* in8 (Vbat) is always internal */
1769 sio_data->internal = (1 << 2);
1771 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1772 if (sio_data->type == it87) {
1773 /* The IT8705F doesn't have VID pins at all */
1774 sio_data->skip_vid = 1;
1776 /* The IT8705F has a different LD number for GPIO */
1778 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1779 } else if (sio_data->type == it8783) {
1780 int reg25, reg27, reg2A, reg2C, regEF;
1782 sio_data->skip_vid = 1; /* No VID */
1784 superio_select(GPIO);
1786 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1787 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1788 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1789 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1790 regEF = superio_inb(IT87_SIO_SPI_REG);
1792 /* Check if fan3 is there or not */
1793 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1794 sio_data->skip_fan |= (1 << 2);
1795 if ((reg25 & (1 << 4))
1796 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1797 sio_data->skip_pwm |= (1 << 2);
1799 /* Check if fan2 is there or not */
1800 if (reg27 & (1 << 7))
1801 sio_data->skip_fan |= (1 << 1);
1802 if (reg27 & (1 << 3))
1803 sio_data->skip_pwm |= (1 << 1);
1806 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1807 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1810 if (reg27 & (1 << 1))
1811 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1815 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1817 if (reg27 & (1 << 2)) {
1819 * The data sheet is a bit unclear regarding the
1820 * internal voltage divider for VCCH5V. It says
1821 * "This bit enables and switches VIN7 (pin 91) to the
1822 * internal voltage divider for VCCH5V".
1823 * This is different to other chips, where the internal
1824 * voltage divider would connect VIN7 to an internal
1825 * voltage source. Maybe that is the case here as well.
1827 * Since we don't know for sure, re-route it if that is
1828 * not the case, and ask the user to report if the
1829 * resulting voltage is sane.
1831 if (!(reg2C & (1 << 1))) {
1833 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1834 pr_notice("Routing internal VCCH5V to in7.\n");
1836 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1837 pr_notice("Please report if it displays a reasonable voltage.\n");
1840 if (reg2C & (1 << 0))
1841 sio_data->internal |= (1 << 0);
1842 if (reg2C & (1 << 1))
1843 sio_data->internal |= (1 << 1);
1845 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1851 superio_select(GPIO);
1853 reg = superio_inb(IT87_SIO_GPIO3_REG);
1854 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1855 sio_data->type == it8782) {
1857 * IT8721F/IT8758E, and IT8782F don't have VID pins
1858 * at all, not sure about the IT8728F.
1860 sio_data->skip_vid = 1;
1862 /* We need at least 4 VID pins */
1864 pr_info("VID is disabled (pins used for GPIO)\n");
1865 sio_data->skip_vid = 1;
1869 /* Check if fan3 is there or not */
1871 sio_data->skip_pwm |= (1 << 2);
1873 sio_data->skip_fan |= (1 << 2);
1875 /* Check if fan2 is there or not */
1876 reg = superio_inb(IT87_SIO_GPIO5_REG);
1878 sio_data->skip_pwm |= (1 << 1);
1880 sio_data->skip_fan |= (1 << 1);
1882 if ((sio_data->type == it8718 || sio_data->type == it8720)
1883 && !(sio_data->skip_vid))
1884 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1886 reg = superio_inb(IT87_SIO_PINX2_REG);
1888 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1891 * The IT8720F has no VIN7 pin, so VCCH should always be
1892 * routed internally to VIN7 with an internal divider.
1893 * Curiously, there still is a configuration bit to control
1894 * this, which means it can be set incorrectly. And even
1895 * more curiously, many boards out there are improperly
1896 * configured, even though the IT8720F datasheet claims
1897 * that the internal routing of VCCH to VIN7 is the default
1898 * setting. So we force the internal routing in this case.
1900 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1901 * If UART6 is enabled, re-route VIN7 to the internal divider
1902 * if that is not already the case.
1904 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
1906 superio_outb(IT87_SIO_PINX2_REG, reg);
1907 pr_notice("Routing internal VCCH to in7\n");
1910 sio_data->internal |= (1 << 0);
1911 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1912 sio_data->type == it8728)
1913 sio_data->internal |= (1 << 1);
1916 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1917 * While VIN7 can be routed to the internal voltage divider,
1918 * VIN5 and VIN6 are not available if UART6 is enabled.
1920 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1921 * is the temperature source. Since we can not read the
1922 * temperature source here, skip_temp is preliminary.
1925 sio_data->skip_in |= (1 << 5) | (1 << 6);
1926 sio_data->skip_temp |= (1 << 2);
1929 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1931 if (sio_data->beep_pin)
1932 pr_info("Beeping is supported\n");
1934 /* Disable specific features based on DMI strings */
1935 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1936 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1937 if (board_vendor && board_name) {
1938 if (strcmp(board_vendor, "nVIDIA") == 0
1939 && strcmp(board_name, "FN68PT") == 0) {
1941 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1942 * connected to a fan, but to something else. One user
1943 * has reported instant system power-off when changing
1944 * the PWM2 duty cycle, so we disable it.
1945 * I use the board name string as the trigger in case
1946 * the same board is ever used in other systems.
1948 pr_info("Disabling pwm2 due to hardware constraints\n");
1949 sio_data->skip_pwm = (1 << 1);
1958 static void it87_remove_files(struct device *dev)
1960 struct it87_data *data = platform_get_drvdata(pdev);
1961 struct it87_sio_data *sio_data = dev->platform_data;
1962 const struct attribute_group *fan_group = it87_get_fan_group(data);
1965 sysfs_remove_group(&dev->kobj, &it87_group);
1966 for (i = 0; i < 9; i++) {
1967 if (sio_data->skip_in & (1 << i))
1969 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1970 if (it87_attributes_in_beep[i])
1971 sysfs_remove_file(&dev->kobj,
1972 it87_attributes_in_beep[i]);
1974 for (i = 0; i < 3; i++) {
1975 if (!(data->has_temp & (1 << i)))
1977 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1978 if (sio_data->beep_pin)
1979 sysfs_remove_file(&dev->kobj,
1980 it87_attributes_temp_beep[i]);
1982 for (i = 0; i < 5; i++) {
1983 if (!(data->has_fan & (1 << i)))
1985 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1986 if (sio_data->beep_pin)
1987 sysfs_remove_file(&dev->kobj,
1988 it87_attributes_fan_beep[i]);
1990 for (i = 0; i < 3; i++) {
1991 if (sio_data->skip_pwm & (1 << 0))
1993 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1994 if (has_old_autopwm(data))
1995 sysfs_remove_group(&dev->kobj,
1996 &it87_group_autopwm[i]);
1998 if (!sio_data->skip_vid)
1999 sysfs_remove_group(&dev->kobj, &it87_group_vid);
2000 sysfs_remove_group(&dev->kobj, &it87_group_label);
2003 static int __devinit it87_probe(struct platform_device *pdev)
2005 struct it87_data *data;
2006 struct resource *res;
2007 struct device *dev = &pdev->dev;
2008 struct it87_sio_data *sio_data = dev->platform_data;
2009 const struct attribute_group *fan_group;
2011 int enable_pwm_interface;
2012 int fan_beep_need_rw;
2013 static const char * const names[] = {
2025 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2026 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2028 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2029 (unsigned long)res->start,
2030 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2034 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2038 data->addr = res->start;
2039 data->type = sio_data->type;
2040 data->revision = sio_data->revision;
2041 data->name = names[sio_data->type];
2043 /* Now, we do the remaining detection. */
2044 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2045 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2048 platform_set_drvdata(pdev, data);
2050 mutex_init(&data->update_lock);
2052 /* Check PWM configuration */
2053 enable_pwm_interface = it87_check_pwm(dev);
2055 /* Starting with IT8721F, we handle scaling of internal voltages */
2056 if (has_12mv_adc(data)) {
2057 if (sio_data->internal & (1 << 0))
2058 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2059 if (sio_data->internal & (1 << 1))
2060 data->in_scaled |= (1 << 7); /* in7 is VSB */
2061 if (sio_data->internal & (1 << 2))
2062 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2063 } else if (sio_data->type == it8782 || 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 & (1 << 2)) {
2072 if (sio_data->type == it8782
2073 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2074 data->has_temp &= ~(1 << 2);
2077 /* Initialize the IT87 chip */
2078 it87_init_device(pdev);
2080 /* Register sysfs hooks */
2081 err = sysfs_create_group(&dev->kobj, &it87_group);
2085 for (i = 0; i < 9; i++) {
2086 if (sio_data->skip_in & (1 << i))
2088 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2091 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2092 err = sysfs_create_file(&dev->kobj,
2093 it87_attributes_in_beep[i]);
2099 for (i = 0; i < 3; i++) {
2100 if (!(data->has_temp & (1 << i)))
2102 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2105 if (sio_data->beep_pin) {
2106 err = sysfs_create_file(&dev->kobj,
2107 it87_attributes_temp_beep[i]);
2113 /* Do not create fan files for disabled fans */
2114 fan_group = it87_get_fan_group(data);
2115 fan_beep_need_rw = 1;
2116 for (i = 0; i < 5; i++) {
2117 if (!(data->has_fan & (1 << i)))
2119 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
2123 if (sio_data->beep_pin) {
2124 err = sysfs_create_file(&dev->kobj,
2125 it87_attributes_fan_beep[i]);
2128 if (!fan_beep_need_rw)
2132 * As we have a single beep enable bit for all fans,
2133 * only the first enabled fan has a writable attribute
2136 if (sysfs_chmod_file(&dev->kobj,
2137 it87_attributes_fan_beep[i],
2139 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2141 fan_beep_need_rw = 0;
2145 if (enable_pwm_interface) {
2146 for (i = 0; i < 3; i++) {
2147 if (sio_data->skip_pwm & (1 << i))
2149 err = sysfs_create_group(&dev->kobj,
2150 &it87_group_pwm[i]);
2154 if (!has_old_autopwm(data))
2156 err = sysfs_create_group(&dev->kobj,
2157 &it87_group_autopwm[i]);
2163 if (!sio_data->skip_vid) {
2164 data->vrm = vid_which_vrm();
2165 /* VID reading from Super-I/O config space if available */
2166 data->vid = sio_data->vid_value;
2167 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2172 /* Export labels for internal sensors */
2173 for (i = 0; i < 3; i++) {
2174 if (!(sio_data->internal & (1 << i)))
2176 err = sysfs_create_file(&dev->kobj,
2177 it87_attributes_label[i]);
2182 data->hwmon_dev = hwmon_device_register(dev);
2183 if (IS_ERR(data->hwmon_dev)) {
2184 err = PTR_ERR(data->hwmon_dev);
2191 it87_remove_files(dev);
2195 static int __devexit it87_remove(struct platform_device *pdev)
2197 struct it87_data *data = platform_get_drvdata(pdev);
2199 hwmon_device_unregister(data->hwmon_dev);
2200 it87_remove_files(&pdev->dev);
2206 * Must be called with data->update_lock held, except during initialization.
2207 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2208 * would slow down the IT87 access and should not be necessary.
2210 static int it87_read_value(struct it87_data *data, u8 reg)
2212 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2213 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2217 * Must be called with data->update_lock held, except during initialization.
2218 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2219 * would slow down the IT87 access and should not be necessary.
2221 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2223 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2224 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2227 /* Return 1 if and only if the PWM interface is safe to use */
2228 static int __devinit it87_check_pwm(struct device *dev)
2230 struct it87_data *data = dev_get_drvdata(dev);
2232 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2233 * and polarity set to active low is sign that this is the case so we
2234 * disable pwm control to protect the user.
2236 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2237 if ((tmp & 0x87) == 0) {
2238 if (fix_pwm_polarity) {
2240 * The user asks us to attempt a chip reconfiguration.
2241 * This means switching to active high polarity and
2242 * inverting all fan speed values.
2247 for (i = 0; i < 3; i++)
2248 pwm[i] = it87_read_value(data,
2252 * If any fan is in automatic pwm mode, the polarity
2253 * might be correct, as suspicious as it seems, so we
2254 * better don't change anything (but still disable the
2257 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2258 dev_info(dev, "Reconfiguring PWM to "
2259 "active high polarity\n");
2260 it87_write_value(data, IT87_REG_FAN_CTL,
2262 for (i = 0; i < 3; i++)
2263 it87_write_value(data,
2269 dev_info(dev, "PWM configuration is "
2270 "too broken to be fixed\n");
2273 dev_info(dev, "Detected broken BIOS "
2274 "defaults, disabling PWM interface\n");
2276 } else if (fix_pwm_polarity) {
2277 dev_info(dev, "PWM configuration looks "
2278 "sane, won't touch\n");
2284 /* Called when we have found a new IT87. */
2285 static void __devinit it87_init_device(struct platform_device *pdev)
2287 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2288 struct it87_data *data = platform_get_drvdata(pdev);
2293 * For each PWM channel:
2294 * - If it is in automatic mode, setting to manual mode should set
2295 * the fan to full speed by default.
2296 * - If it is in manual mode, we need a mapping to temperature
2297 * channels to use when later setting to automatic mode later.
2298 * Use a 1:1 mapping by default (we are clueless.)
2299 * In both cases, the value can (and should) be changed by the user
2300 * prior to switching to a different mode.
2301 * Note that this is no longer needed for the IT8721F and later, as
2302 * these have separate registers for the temperature mapping and the
2303 * manual duty cycle.
2305 for (i = 0; i < 3; i++) {
2306 data->pwm_temp_map[i] = i;
2307 data->pwm_duty[i] = 0x7f; /* Full speed */
2308 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2312 * Some chips seem to have default value 0xff for all limit
2313 * registers. For low voltage limits it makes no sense and triggers
2314 * alarms, so change to 0 instead. For high temperature limits, it
2315 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2316 * but is still confusing, so change to 127 degrees C.
2318 for (i = 0; i < 8; i++) {
2319 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2321 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2323 for (i = 0; i < 3; i++) {
2324 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2326 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2330 * Temperature channels are not forcibly enabled, as they can be
2331 * set to two different sensor types and we can't guess which one
2332 * is correct for a given system. These channels can be enabled at
2333 * run-time through the temp{1-3}_type sysfs accessors if needed.
2336 /* Check if voltage monitors are reset manually or by some reason */
2337 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2338 if ((tmp & 0xff) == 0) {
2339 /* Enable all voltage monitors */
2340 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2343 /* Check if tachometers are reset manually or by some reason */
2344 mask = 0x70 & ~(sio_data->skip_fan << 4);
2345 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2346 if ((data->fan_main_ctrl & mask) == 0) {
2347 /* Enable all fan tachometers */
2348 data->fan_main_ctrl |= mask;
2349 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2350 data->fan_main_ctrl);
2352 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2354 /* Set tachometers to 16-bit mode if needed */
2355 if (has_16bit_fans(data)) {
2356 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2357 if (~tmp & 0x07 & data->has_fan) {
2359 "Setting fan1-3 to 16-bit mode\n");
2360 it87_write_value(data, IT87_REG_FAN_16BIT,
2363 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2364 if (data->type != it87 && data->type != it8782 &&
2365 data->type != it8783) {
2367 data->has_fan |= (1 << 3); /* fan4 enabled */
2369 data->has_fan |= (1 << 4); /* fan5 enabled */
2373 /* Fan input pins may be used for alternative functions */
2374 data->has_fan &= ~sio_data->skip_fan;
2376 /* Start monitoring */
2377 it87_write_value(data, IT87_REG_CONFIG,
2378 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
2379 | (update_vbat ? 0x41 : 0x01));
2382 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2384 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2385 if (has_newer_autopwm(data)) {
2386 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2387 data->pwm_duty[nr] = it87_read_value(data,
2388 IT87_REG_PWM_DUTY(nr));
2390 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2391 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2392 else /* Manual mode */
2393 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2396 if (has_old_autopwm(data)) {
2399 for (i = 0; i < 5 ; i++)
2400 data->auto_temp[nr][i] = it87_read_value(data,
2401 IT87_REG_AUTO_TEMP(nr, i));
2402 for (i = 0; i < 3 ; i++)
2403 data->auto_pwm[nr][i] = it87_read_value(data,
2404 IT87_REG_AUTO_PWM(nr, i));
2408 static struct it87_data *it87_update_device(struct device *dev)
2410 struct it87_data *data = dev_get_drvdata(dev);
2413 mutex_lock(&data->update_lock);
2415 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2419 * Cleared after each update, so reenable. Value
2420 * returned by this read will be previous value
2422 it87_write_value(data, IT87_REG_CONFIG,
2423 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2425 for (i = 0; i <= 7; i++) {
2427 it87_read_value(data, IT87_REG_VIN(i));
2429 it87_read_value(data, IT87_REG_VIN_MIN(i));
2431 it87_read_value(data, IT87_REG_VIN_MAX(i));
2433 /* in8 (battery) has no limit registers */
2434 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
2436 for (i = 0; i < 5; i++) {
2437 /* Skip disabled fans */
2438 if (!(data->has_fan & (1 << i)))
2442 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2443 data->fan[i] = it87_read_value(data,
2445 /* Add high byte if in 16-bit mode */
2446 if (has_16bit_fans(data)) {
2447 data->fan[i] |= it87_read_value(data,
2448 IT87_REG_FANX[i]) << 8;
2449 data->fan_min[i] |= it87_read_value(data,
2450 IT87_REG_FANX_MIN[i]) << 8;
2453 for (i = 0; i < 3; i++) {
2454 if (!(data->has_temp & (1 << i)))
2457 it87_read_value(data, IT87_REG_TEMP(i));
2458 data->temp_high[i] =
2459 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2461 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2464 /* Newer chips don't have clock dividers */
2465 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2466 i = it87_read_value(data, IT87_REG_FAN_DIV);
2467 data->fan_div[0] = i & 0x07;
2468 data->fan_div[1] = (i >> 3) & 0x07;
2469 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2473 it87_read_value(data, IT87_REG_ALARM1) |
2474 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2475 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2476 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2478 data->fan_main_ctrl = it87_read_value(data,
2479 IT87_REG_FAN_MAIN_CTRL);
2480 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2481 for (i = 0; i < 3; i++)
2482 it87_update_pwm_ctrl(data, i);
2484 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2486 * The IT8705F does not have VID capability.
2487 * The IT8718F and later don't use IT87_REG_VID for the
2490 if (data->type == it8712 || data->type == it8716) {
2491 data->vid = it87_read_value(data, IT87_REG_VID);
2493 * The older IT8712F revisions had only 5 VID pins,
2494 * but we assume it is always safe to read 6 bits.
2498 data->last_updated = jiffies;
2502 mutex_unlock(&data->update_lock);
2507 static int __init it87_device_add(unsigned short address,
2508 const struct it87_sio_data *sio_data)
2510 struct resource res = {
2511 .start = address + IT87_EC_OFFSET,
2512 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2514 .flags = IORESOURCE_IO,
2518 err = acpi_check_resource_conflict(&res);
2522 pdev = platform_device_alloc(DRVNAME, address);
2525 pr_err("Device allocation failed\n");
2529 err = platform_device_add_resources(pdev, &res, 1);
2531 pr_err("Device resource addition failed (%d)\n", err);
2532 goto exit_device_put;
2535 err = platform_device_add_data(pdev, sio_data,
2536 sizeof(struct it87_sio_data));
2538 pr_err("Platform data allocation failed\n");
2539 goto exit_device_put;
2542 err = platform_device_add(pdev);
2544 pr_err("Device addition failed (%d)\n", err);
2545 goto exit_device_put;
2551 platform_device_put(pdev);
2556 static int __init sm_it87_init(void)
2559 unsigned short isa_address = 0;
2560 struct it87_sio_data sio_data;
2562 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2563 err = it87_find(&isa_address, &sio_data);
2566 err = platform_driver_register(&it87_driver);
2570 err = it87_device_add(isa_address, &sio_data);
2572 platform_driver_unregister(&it87_driver);
2579 static void __exit sm_it87_exit(void)
2581 platform_device_unregister(pdev);
2582 platform_driver_unregister(&it87_driver);
2586 MODULE_AUTHOR("Chris Gauthron, "
2587 "Jean Delvare <khali@linux-fr.org>");
2588 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2589 module_param(update_vbat, bool, 0);
2590 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2591 module_param(fix_pwm_polarity, bool, 0);
2592 MODULE_PARM_DESC(fix_pwm_polarity,
2593 "Force PWM polarity to active high (DANGEROUS)");
2594 MODULE_LICENSE("GPL");
2596 module_init(sm_it87_init);
2597 module_exit(sm_it87_exit);