2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
13 * Supports: IT8705F Super I/O chip w/LPC interface
14 * IT8712F Super I/O chip w/LPC interface
15 * IT8716F Super I/O chip w/LPC interface
16 * IT8718F Super I/O chip w/LPC interface
17 * IT8720F Super I/O chip w/LPC interface
18 * IT8721F Super I/O chip w/LPC interface
19 * IT8726F Super I/O chip w/LPC interface
20 * IT8728F Super I/O chip w/LPC interface
21 * IT8758E Super I/O chip w/LPC interface
22 * IT8783E/F Super I/O chip w/LPC interface
23 * Sis950 A clone of the IT8705F
25 * Copyright (C) 2001 Chris Gauthron
26 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/slab.h>
48 #include <linux/jiffies.h>
49 #include <linux/platform_device.h>
50 #include <linux/hwmon.h>
51 #include <linux/hwmon-sysfs.h>
52 #include <linux/hwmon-vid.h>
53 #include <linux/err.h>
54 #include <linux/mutex.h>
55 #include <linux/sysfs.h>
56 #include <linux/string.h>
57 #include <linux/dmi.h>
58 #include <linux/acpi.h>
62 #define kstrtol strict_strtol
65 #define kstrtoul strict_strtoul
68 #define DRVNAME "it87"
70 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8783 };
72 static unsigned short force_id;
73 module_param(force_id, ushort, 0);
74 MODULE_PARM_DESC(force_id, "Override the detected device ID");
76 static struct platform_device *pdev;
78 #define REG 0x2e /* The register to read/write */
79 #define DEV 0x07 /* Register: Logical device select */
80 #define VAL 0x2f /* The value to read/write */
81 #define PME 0x04 /* The device with the fan registers in it */
83 /* The device with the IT8718F/IT8720F VID value in it */
86 #define DEVID 0x20 /* Register: Device ID */
87 #define DEVREV 0x22 /* Register: Device Revision */
89 static inline int superio_inb(int reg)
95 static inline void superio_outb(int reg, int val)
101 static int superio_inw(int reg)
111 static inline void superio_select(int ldn)
117 static inline int superio_enter(void)
120 * Try to reserve REG and REG + 1 for exclusive access.
122 #ifdef request_muxed_region
123 if (!request_muxed_region(REG, 2, DRVNAME))
134 static inline void superio_exit(void)
138 #ifdef request_muxed_region
139 release_region(REG, 2);
143 /* Logical device 4 registers */
144 #define IT8712F_DEVID 0x8712
145 #define IT8705F_DEVID 0x8705
146 #define IT8716F_DEVID 0x8716
147 #define IT8718F_DEVID 0x8718
148 #define IT8720F_DEVID 0x8720
149 #define IT8721F_DEVID 0x8721
150 #define IT8726F_DEVID 0x8726
151 #define IT8728F_DEVID 0x8728
152 #define IT8783E_DEVID 0x8783
153 #define IT87_ACT_REG 0x30
154 #define IT87_BASE_REG 0x60
156 /* Logical device 7 registers (IT8712F and later) */
157 #define IT87_SIO_GPIO1_REG 0x25
158 #define IT87_SIO_GPIO3_REG 0x27
159 #define IT87_SIO_GPIO5_REG 0x29
160 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
161 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
162 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
163 #define IT87_SIO_VID_REG 0xfc /* VID value */
164 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
166 /* Update battery voltage after every reading if true */
167 static bool update_vbat;
169 /* Not all BIOSes properly configure the PWM registers */
170 static bool fix_pwm_polarity;
172 /* Many IT87 constants specified below */
174 /* Length of ISA address segment */
175 #define IT87_EXTENT 8
177 /* Length of ISA address segment for Environmental Controller */
178 #define IT87_EC_EXTENT 2
180 /* Offset of EC registers from ISA base address */
181 #define IT87_EC_OFFSET 5
183 /* Where are the ISA address/data registers relative to the EC base address */
184 #define IT87_ADDR_REG_OFFSET 0
185 #define IT87_DATA_REG_OFFSET 1
187 /*----- The IT87 registers -----*/
189 #define IT87_REG_CONFIG 0x00
191 #define IT87_REG_ALARM1 0x01
192 #define IT87_REG_ALARM2 0x02
193 #define IT87_REG_ALARM3 0x03
196 * The IT8718F and IT8720F have the VID value in a different register, in
197 * Super-I/O configuration space.
199 #define IT87_REG_VID 0x0a
201 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
202 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
205 #define IT87_REG_FAN_DIV 0x0b
206 #define IT87_REG_FAN_16BIT 0x0c
208 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
210 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
211 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
212 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
213 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
214 #define IT87_REG_FAN_MAIN_CTRL 0x13
215 #define IT87_REG_FAN_CTL 0x14
216 #define IT87_REG_PWM(nr) (0x15 + (nr))
217 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
219 #define IT87_REG_VIN(nr) (0x20 + (nr))
220 #define IT87_REG_TEMP(nr) (0x29 + (nr))
222 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
223 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
224 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
225 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
227 #define IT87_REG_VIN_ENABLE 0x50
228 #define IT87_REG_TEMP_ENABLE 0x51
229 #define IT87_REG_BEEP_ENABLE 0x5c
231 #define IT87_REG_CHIPID 0x58
233 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
234 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
237 struct it87_sio_data {
239 /* Values read from Super-I/O config space */
243 u8 internal; /* Internal sensors can be labeled */
244 /* Features skipped based on config or DMI */
251 * For each registered chip, we need to keep some data in memory.
252 * The structure is dynamically allocated.
255 struct device *hwmon_dev;
261 struct mutex update_lock;
262 char valid; /* !=0 if following fields are valid */
263 unsigned long last_updated; /* In jiffies */
265 u16 in_scaled; /* Internal voltage sensors are scaled */
266 u8 in[9]; /* Register value */
267 u8 in_max[8]; /* Register value */
268 u8 in_min[8]; /* Register value */
269 u8 has_fan; /* Bitfield, fans enabled */
270 u16 fan[5]; /* Register values, possibly combined */
271 u16 fan_min[5]; /* Register values, possibly combined */
272 s8 temp[3]; /* Register value */
273 s8 temp_high[3]; /* Register value */
274 s8 temp_low[3]; /* Register value */
275 u8 sensor; /* Register value */
276 u8 fan_div[3]; /* Register encoding, shifted right */
277 u8 vid; /* Register encoding, combined */
279 u32 alarms; /* Register encoding, combined */
280 u8 beeps; /* Register encoding */
281 u8 fan_main_ctrl; /* Register value */
282 u8 fan_ctl; /* Register value */
285 * The following 3 arrays correspond to the same registers up to
286 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
287 * 7, and we want to preserve settings on mode changes, so we have
288 * to track all values separately.
289 * Starting with the IT8721F, the manual PWM duty cycles are stored
290 * in separate registers (8-bit values), so the separate tracking
291 * is no longer needed, but it is still done to keep the driver
294 u8 pwm_ctrl[3]; /* Register value */
295 u8 pwm_duty[3]; /* Manual PWM value set by user */
296 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
298 /* Automatic fan speed control registers */
299 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
300 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
303 static inline int has_12mv_adc(const struct it87_data *data)
306 * IT8721F and later have a 12 mV ADC, also with internal scaling
307 * on selected inputs.
309 return data->type == it8721
310 || data->type == it8728;
313 static inline int has_newer_autopwm(const struct it87_data *data)
316 * IT8721F and later have separate registers for the temperature
317 * mapping and the manual duty cycle.
319 return data->type == it8721
320 || data->type == it8728;
323 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
327 if (has_12mv_adc(data)) {
328 if (data->in_scaled & (1 << nr))
333 if (data->in_scaled & (1 << nr))
339 val = DIV_ROUND_CLOSEST(val, lsb);
340 return SENSORS_LIMIT(val, 0, 255);
343 static int in_from_reg(const struct it87_data *data, int nr, int val)
345 if (has_12mv_adc(data)) {
346 if (data->in_scaled & (1 << nr))
351 if (data->in_scaled & (1 << nr))
358 static inline u8 FAN_TO_REG(long rpm, int div)
362 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
363 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
367 static inline u16 FAN16_TO_REG(long rpm)
371 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
374 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
375 1350000 / ((val) * (div)))
376 /* The divider is fixed to 2 in 16-bit mode */
377 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
378 1350000 / ((val) * 2))
380 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
381 ((val) + 500) / 1000), -128, 127))
382 #define TEMP_FROM_REG(val) ((val) * 1000)
384 static u8 pwm_to_reg(const struct it87_data *data, long val)
386 if (has_newer_autopwm(data))
392 static int pwm_from_reg(const struct it87_data *data, u8 reg)
394 if (has_newer_autopwm(data))
397 return (reg & 0x7f) << 1;
401 static int DIV_TO_REG(int val)
404 while (answer < 7 && (val >>= 1))
408 #define DIV_FROM_REG(val) (1 << (val))
410 static const unsigned int pwm_freq[8] = {
421 static inline int has_16bit_fans(const struct it87_data *data)
424 * IT8705F Datasheet 0.4.1, 3h == Version G.
425 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
426 * These are the first revisions with 16-bit tachometer support.
428 return (data->type == it87 && data->revision >= 0x03)
429 || (data->type == it8712 && data->revision >= 0x08)
430 || data->type == it8716
431 || data->type == it8718
432 || data->type == it8720
433 || data->type == it8721
434 || data->type == it8728
435 || data->type == it8783;
438 static inline int has_old_autopwm(const struct it87_data *data)
441 * The old automatic fan speed control interface is implemented
442 * by IT8705F chips up to revision F and IT8712F chips up to
445 return (data->type == it87 && data->revision < 0x03)
446 || (data->type == it8712 && data->revision < 0x08);
449 static int it87_probe(struct platform_device *pdev);
450 static int __devexit it87_remove(struct platform_device *pdev);
452 static int it87_read_value(struct it87_data *data, u8 reg);
453 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
454 static struct it87_data *it87_update_device(struct device *dev);
455 static int it87_check_pwm(struct device *dev);
456 static void it87_init_device(struct platform_device *pdev);
459 static struct platform_driver it87_driver = {
461 .owner = THIS_MODULE,
465 .remove = __devexit_p(it87_remove),
468 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
471 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
472 int nr = sensor_attr->index;
474 struct it87_data *data = it87_update_device(dev);
475 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
478 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
481 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
482 int nr = sensor_attr->index;
484 struct it87_data *data = it87_update_device(dev);
485 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
488 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
491 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
492 int nr = sensor_attr->index;
494 struct it87_data *data = it87_update_device(dev);
495 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
498 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
499 const char *buf, size_t count)
501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
504 struct it87_data *data = dev_get_drvdata(dev);
507 if (kstrtoul(buf, 10, &val) < 0)
510 mutex_lock(&data->update_lock);
511 data->in_min[nr] = in_to_reg(data, nr, val);
512 it87_write_value(data, IT87_REG_VIN_MIN(nr),
514 mutex_unlock(&data->update_lock);
517 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
518 const char *buf, size_t count)
520 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
521 int nr = sensor_attr->index;
523 struct it87_data *data = dev_get_drvdata(dev);
526 if (kstrtoul(buf, 10, &val) < 0)
529 mutex_lock(&data->update_lock);
530 data->in_max[nr] = in_to_reg(data, nr, val);
531 it87_write_value(data, IT87_REG_VIN_MAX(nr),
533 mutex_unlock(&data->update_lock);
537 #define show_in_offset(offset) \
538 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
539 show_in, NULL, offset);
541 #define limit_in_offset(offset) \
542 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
543 show_in_min, set_in_min, offset); \
544 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
545 show_in_max, set_in_max, offset);
566 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
569 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
570 int nr = sensor_attr->index;
572 struct it87_data *data = it87_update_device(dev);
573 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
575 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
578 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
579 int nr = sensor_attr->index;
581 struct it87_data *data = it87_update_device(dev);
582 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
584 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
587 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
588 int nr = sensor_attr->index;
590 struct it87_data *data = it87_update_device(dev);
591 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
593 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
594 const char *buf, size_t count)
596 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
597 int nr = sensor_attr->index;
599 struct it87_data *data = dev_get_drvdata(dev);
602 if (kstrtol(buf, 10, &val) < 0)
605 mutex_lock(&data->update_lock);
606 data->temp_high[nr] = TEMP_TO_REG(val);
607 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
608 mutex_unlock(&data->update_lock);
611 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
612 const char *buf, size_t count)
614 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
615 int nr = sensor_attr->index;
617 struct it87_data *data = dev_get_drvdata(dev);
620 if (kstrtol(buf, 10, &val) < 0)
623 mutex_lock(&data->update_lock);
624 data->temp_low[nr] = TEMP_TO_REG(val);
625 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
626 mutex_unlock(&data->update_lock);
629 #define show_temp_offset(offset) \
630 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
631 show_temp, NULL, offset - 1); \
632 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
633 show_temp_max, set_temp_max, offset - 1); \
634 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
635 show_temp_min, set_temp_min, offset - 1);
641 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
644 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
645 int nr = sensor_attr->index;
646 struct it87_data *data = it87_update_device(dev);
647 u8 reg = data->sensor; /* In case value is updated while used */
650 return sprintf(buf, "3\n"); /* thermal diode */
652 return sprintf(buf, "4\n"); /* thermistor */
653 return sprintf(buf, "0\n"); /* disabled */
655 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
658 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
659 int nr = sensor_attr->index;
661 struct it87_data *data = dev_get_drvdata(dev);
665 if (kstrtol(buf, 10, &val) < 0)
668 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
671 if (val == 2) { /* backwards compatibility */
672 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
676 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
684 mutex_lock(&data->update_lock);
686 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
687 data->valid = 0; /* Force cache refresh */
688 mutex_unlock(&data->update_lock);
691 #define show_sensor_offset(offset) \
692 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
693 show_sensor, set_sensor, offset - 1);
695 show_sensor_offset(1);
696 show_sensor_offset(2);
697 show_sensor_offset(3);
701 static int pwm_mode(const struct it87_data *data, int nr)
703 int ctrl = data->fan_main_ctrl & (1 << nr);
705 if (ctrl == 0) /* Full speed */
707 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
709 else /* Manual mode */
713 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
716 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
717 int nr = sensor_attr->index;
719 struct it87_data *data = it87_update_device(dev);
720 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
721 DIV_FROM_REG(data->fan_div[nr])));
723 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
726 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
727 int nr = sensor_attr->index;
729 struct it87_data *data = it87_update_device(dev);
730 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
731 DIV_FROM_REG(data->fan_div[nr])));
733 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
736 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
737 int nr = sensor_attr->index;
739 struct it87_data *data = it87_update_device(dev);
740 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
742 static ssize_t show_pwm_enable(struct device *dev,
743 struct device_attribute *attr, char *buf)
745 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
746 int nr = sensor_attr->index;
748 struct it87_data *data = it87_update_device(dev);
749 return sprintf(buf, "%d\n", pwm_mode(data, nr));
751 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
754 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
755 int nr = sensor_attr->index;
757 struct it87_data *data = it87_update_device(dev);
758 return sprintf(buf, "%d\n",
759 pwm_from_reg(data, data->pwm_duty[nr]));
761 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
764 struct it87_data *data = it87_update_device(dev);
765 int index = (data->fan_ctl >> 4) & 0x07;
767 return sprintf(buf, "%u\n", pwm_freq[index]);
769 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count)
772 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
773 int nr = sensor_attr->index;
775 struct it87_data *data = dev_get_drvdata(dev);
779 if (kstrtol(buf, 10, &val) < 0)
782 mutex_lock(&data->update_lock);
783 reg = it87_read_value(data, IT87_REG_FAN_DIV);
786 data->fan_div[nr] = reg & 0x07;
789 data->fan_div[nr] = (reg >> 3) & 0x07;
792 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
796 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
797 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
798 mutex_unlock(&data->update_lock);
801 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
802 const char *buf, size_t count)
804 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
805 int nr = sensor_attr->index;
807 struct it87_data *data = dev_get_drvdata(dev);
812 if (kstrtoul(buf, 10, &val) < 0)
815 mutex_lock(&data->update_lock);
816 old = it87_read_value(data, IT87_REG_FAN_DIV);
818 /* Save fan min limit */
819 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
824 data->fan_div[nr] = DIV_TO_REG(val);
828 data->fan_div[nr] = 1;
830 data->fan_div[nr] = 3;
833 val |= (data->fan_div[0] & 0x07);
834 val |= (data->fan_div[1] & 0x07) << 3;
835 if (data->fan_div[2] == 3)
837 it87_write_value(data, IT87_REG_FAN_DIV, val);
839 /* Restore fan min limit */
840 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
841 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
843 mutex_unlock(&data->update_lock);
847 /* Returns 0 if OK, -EINVAL otherwise */
848 static int check_trip_points(struct device *dev, int nr)
850 const struct it87_data *data = dev_get_drvdata(dev);
853 if (has_old_autopwm(data)) {
854 for (i = 0; i < 3; i++) {
855 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
858 for (i = 0; i < 2; i++) {
859 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
865 dev_err(dev, "Inconsistent trip points, not switching to "
867 dev_err(dev, "Adjust the trip points and try again\n");
872 static ssize_t set_pwm_enable(struct device *dev,
873 struct device_attribute *attr, const char *buf, size_t count)
875 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
876 int nr = sensor_attr->index;
878 struct it87_data *data = dev_get_drvdata(dev);
881 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
884 /* Check trip points before switching to automatic mode */
886 if (check_trip_points(dev, nr) < 0)
890 mutex_lock(&data->update_lock);
894 /* make sure the fan is on when in on/off mode */
895 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
896 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
897 /* set on/off mode */
898 data->fan_main_ctrl &= ~(1 << nr);
899 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
900 data->fan_main_ctrl);
902 if (val == 1) /* Manual mode */
903 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
904 data->pwm_temp_map[nr] :
906 else /* Automatic mode */
907 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
908 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
909 /* set SmartGuardian mode */
910 data->fan_main_ctrl |= (1 << nr);
911 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
912 data->fan_main_ctrl);
915 mutex_unlock(&data->update_lock);
918 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
919 const char *buf, size_t count)
921 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
922 int nr = sensor_attr->index;
924 struct it87_data *data = dev_get_drvdata(dev);
927 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
930 mutex_lock(&data->update_lock);
931 if (has_newer_autopwm(data)) {
933 * If we are in automatic mode, the PWM duty cycle register
934 * is read-only so we can't write the value.
936 if (data->pwm_ctrl[nr] & 0x80) {
937 mutex_unlock(&data->update_lock);
940 data->pwm_duty[nr] = pwm_to_reg(data, val);
941 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
944 data->pwm_duty[nr] = pwm_to_reg(data, val);
946 * If we are in manual mode, write the duty cycle immediately;
947 * otherwise, just store it for later use.
949 if (!(data->pwm_ctrl[nr] & 0x80)) {
950 data->pwm_ctrl[nr] = data->pwm_duty[nr];
951 it87_write_value(data, IT87_REG_PWM(nr),
955 mutex_unlock(&data->update_lock);
958 static ssize_t set_pwm_freq(struct device *dev,
959 struct device_attribute *attr, const char *buf, size_t count)
961 struct it87_data *data = dev_get_drvdata(dev);
965 if (kstrtoul(buf, 10, &val) < 0)
968 /* Search for the nearest available frequency */
969 for (i = 0; i < 7; i++) {
970 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
974 mutex_lock(&data->update_lock);
975 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
976 data->fan_ctl |= i << 4;
977 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
978 mutex_unlock(&data->update_lock);
982 static ssize_t show_pwm_temp_map(struct device *dev,
983 struct device_attribute *attr, char *buf)
985 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
986 int nr = sensor_attr->index;
988 struct it87_data *data = it87_update_device(dev);
991 if (data->pwm_temp_map[nr] < 3)
992 map = 1 << data->pwm_temp_map[nr];
994 map = 0; /* Should never happen */
995 return sprintf(buf, "%d\n", map);
997 static ssize_t set_pwm_temp_map(struct device *dev,
998 struct device_attribute *attr, const char *buf, size_t count)
1000 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1001 int nr = sensor_attr->index;
1003 struct it87_data *data = dev_get_drvdata(dev);
1008 * This check can go away if we ever support automatic fan speed
1009 * control on newer chips.
1011 if (!has_old_autopwm(data)) {
1012 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1016 if (kstrtol(buf, 10, &val) < 0)
1033 mutex_lock(&data->update_lock);
1034 data->pwm_temp_map[nr] = reg;
1036 * If we are in automatic mode, write the temp mapping immediately;
1037 * otherwise, just store it for later use.
1039 if (data->pwm_ctrl[nr] & 0x80) {
1040 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1041 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1043 mutex_unlock(&data->update_lock);
1047 static ssize_t show_auto_pwm(struct device *dev,
1048 struct device_attribute *attr, char *buf)
1050 struct it87_data *data = it87_update_device(dev);
1051 struct sensor_device_attribute_2 *sensor_attr =
1052 to_sensor_dev_attr_2(attr);
1053 int nr = sensor_attr->nr;
1054 int point = sensor_attr->index;
1056 return sprintf(buf, "%d\n",
1057 pwm_from_reg(data, data->auto_pwm[nr][point]));
1060 static ssize_t set_auto_pwm(struct device *dev,
1061 struct device_attribute *attr, const char *buf, size_t count)
1063 struct it87_data *data = dev_get_drvdata(dev);
1064 struct sensor_device_attribute_2 *sensor_attr =
1065 to_sensor_dev_attr_2(attr);
1066 int nr = sensor_attr->nr;
1067 int point = sensor_attr->index;
1070 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1073 mutex_lock(&data->update_lock);
1074 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1075 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1076 data->auto_pwm[nr][point]);
1077 mutex_unlock(&data->update_lock);
1081 static ssize_t show_auto_temp(struct device *dev,
1082 struct device_attribute *attr, char *buf)
1084 struct it87_data *data = it87_update_device(dev);
1085 struct sensor_device_attribute_2 *sensor_attr =
1086 to_sensor_dev_attr_2(attr);
1087 int nr = sensor_attr->nr;
1088 int point = sensor_attr->index;
1090 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1093 static ssize_t set_auto_temp(struct device *dev,
1094 struct device_attribute *attr, const char *buf, size_t count)
1096 struct it87_data *data = dev_get_drvdata(dev);
1097 struct sensor_device_attribute_2 *sensor_attr =
1098 to_sensor_dev_attr_2(attr);
1099 int nr = sensor_attr->nr;
1100 int point = sensor_attr->index;
1103 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1106 mutex_lock(&data->update_lock);
1107 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1108 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1109 data->auto_temp[nr][point]);
1110 mutex_unlock(&data->update_lock);
1114 #define show_fan_offset(offset) \
1115 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1116 show_fan, NULL, offset - 1); \
1117 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1118 show_fan_min, set_fan_min, offset - 1); \
1119 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1120 show_fan_div, set_fan_div, offset - 1);
1126 #define show_pwm_offset(offset) \
1127 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1128 show_pwm_enable, set_pwm_enable, offset - 1); \
1129 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1130 show_pwm, set_pwm, offset - 1); \
1131 static DEVICE_ATTR(pwm##offset##_freq, \
1132 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1133 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1134 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1135 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1137 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1138 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1140 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1141 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1143 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1144 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1146 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1147 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1148 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1149 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1151 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1152 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1154 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1155 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1157 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1158 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1160 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1161 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1168 /* A different set of callbacks for 16-bit fans */
1169 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1172 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1173 int nr = sensor_attr->index;
1174 struct it87_data *data = it87_update_device(dev);
1175 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1178 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1181 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1182 int nr = sensor_attr->index;
1183 struct it87_data *data = it87_update_device(dev);
1184 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1187 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1188 const char *buf, size_t count)
1190 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1191 int nr = sensor_attr->index;
1192 struct it87_data *data = dev_get_drvdata(dev);
1195 if (kstrtol(buf, 10, &val) < 0)
1198 mutex_lock(&data->update_lock);
1199 data->fan_min[nr] = FAN16_TO_REG(val);
1200 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1201 data->fan_min[nr] & 0xff);
1202 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1203 data->fan_min[nr] >> 8);
1204 mutex_unlock(&data->update_lock);
1209 * We want to use the same sysfs file names as 8-bit fans, but we need
1210 * different variable names, so we have to use SENSOR_ATTR instead of
1211 * SENSOR_DEVICE_ATTR.
1213 #define show_fan16_offset(offset) \
1214 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1215 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1216 show_fan16, NULL, offset - 1); \
1217 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1218 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1219 show_fan16_min, set_fan16_min, offset - 1)
1221 show_fan16_offset(1);
1222 show_fan16_offset(2);
1223 show_fan16_offset(3);
1224 show_fan16_offset(4);
1225 show_fan16_offset(5);
1228 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1231 struct it87_data *data = it87_update_device(dev);
1232 return sprintf(buf, "%u\n", data->alarms);
1234 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1236 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1239 int bitnr = to_sensor_dev_attr(attr)->index;
1240 struct it87_data *data = it87_update_device(dev);
1241 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1244 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1245 *attr, const char *buf, size_t count)
1247 struct it87_data *data = dev_get_drvdata(dev);
1251 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1254 mutex_lock(&data->update_lock);
1255 config = it87_read_value(data, IT87_REG_CONFIG);
1260 it87_write_value(data, IT87_REG_CONFIG, config);
1261 /* Invalidate cache to force re-read */
1264 mutex_unlock(&data->update_lock);
1269 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1270 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1271 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1272 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1273 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1274 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1275 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1276 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1277 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1278 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1279 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1280 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1281 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1282 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1283 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1284 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1285 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1286 show_alarm, clear_intrusion, 4);
1288 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1291 int bitnr = to_sensor_dev_attr(attr)->index;
1292 struct it87_data *data = it87_update_device(dev);
1293 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1295 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1296 const char *buf, size_t count)
1298 int bitnr = to_sensor_dev_attr(attr)->index;
1299 struct it87_data *data = dev_get_drvdata(dev);
1302 if (kstrtol(buf, 10, &val) < 0
1303 || (val != 0 && val != 1))
1306 mutex_lock(&data->update_lock);
1307 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1309 data->beeps |= (1 << bitnr);
1311 data->beeps &= ~(1 << bitnr);
1312 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1313 mutex_unlock(&data->update_lock);
1317 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1318 show_beep, set_beep, 1);
1319 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1320 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1321 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1322 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1323 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1324 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1325 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1326 /* fanX_beep writability is set later */
1327 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1328 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1329 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1330 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1331 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1332 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1333 show_beep, set_beep, 2);
1334 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1335 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1337 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1340 struct it87_data *data = dev_get_drvdata(dev);
1341 return sprintf(buf, "%u\n", data->vrm);
1343 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1344 const char *buf, size_t count)
1346 struct it87_data *data = dev_get_drvdata(dev);
1349 if (kstrtoul(buf, 10, &val) < 0)
1356 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1358 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1361 struct it87_data *data = it87_update_device(dev);
1362 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1364 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1366 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1369 static const char * const labels[] = {
1374 static const char * const labels_it8721[] = {
1379 struct it87_data *data = dev_get_drvdata(dev);
1380 int nr = to_sensor_dev_attr(attr)->index;
1382 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1385 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1386 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1387 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1389 static ssize_t show_name(struct device *dev, struct device_attribute
1390 *devattr, char *buf)
1392 struct it87_data *data = dev_get_drvdata(dev);
1393 return sprintf(buf, "%s\n", data->name);
1395 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1397 static struct attribute *it87_attributes[] = {
1398 &sensor_dev_attr_in0_input.dev_attr.attr,
1399 &sensor_dev_attr_in1_input.dev_attr.attr,
1400 &sensor_dev_attr_in2_input.dev_attr.attr,
1401 &sensor_dev_attr_in3_input.dev_attr.attr,
1402 &sensor_dev_attr_in4_input.dev_attr.attr,
1403 &sensor_dev_attr_in5_input.dev_attr.attr,
1404 &sensor_dev_attr_in6_input.dev_attr.attr,
1405 &sensor_dev_attr_in7_input.dev_attr.attr,
1406 &sensor_dev_attr_in8_input.dev_attr.attr,
1407 &sensor_dev_attr_in0_min.dev_attr.attr,
1408 &sensor_dev_attr_in1_min.dev_attr.attr,
1409 &sensor_dev_attr_in2_min.dev_attr.attr,
1410 &sensor_dev_attr_in3_min.dev_attr.attr,
1411 &sensor_dev_attr_in4_min.dev_attr.attr,
1412 &sensor_dev_attr_in5_min.dev_attr.attr,
1413 &sensor_dev_attr_in6_min.dev_attr.attr,
1414 &sensor_dev_attr_in7_min.dev_attr.attr,
1415 &sensor_dev_attr_in0_max.dev_attr.attr,
1416 &sensor_dev_attr_in1_max.dev_attr.attr,
1417 &sensor_dev_attr_in2_max.dev_attr.attr,
1418 &sensor_dev_attr_in3_max.dev_attr.attr,
1419 &sensor_dev_attr_in4_max.dev_attr.attr,
1420 &sensor_dev_attr_in5_max.dev_attr.attr,
1421 &sensor_dev_attr_in6_max.dev_attr.attr,
1422 &sensor_dev_attr_in7_max.dev_attr.attr,
1423 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1424 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1425 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1426 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1427 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1428 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1429 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1430 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1432 &sensor_dev_attr_temp1_input.dev_attr.attr,
1433 &sensor_dev_attr_temp2_input.dev_attr.attr,
1434 &sensor_dev_attr_temp3_input.dev_attr.attr,
1435 &sensor_dev_attr_temp1_max.dev_attr.attr,
1436 &sensor_dev_attr_temp2_max.dev_attr.attr,
1437 &sensor_dev_attr_temp3_max.dev_attr.attr,
1438 &sensor_dev_attr_temp1_min.dev_attr.attr,
1439 &sensor_dev_attr_temp2_min.dev_attr.attr,
1440 &sensor_dev_attr_temp3_min.dev_attr.attr,
1441 &sensor_dev_attr_temp1_type.dev_attr.attr,
1442 &sensor_dev_attr_temp2_type.dev_attr.attr,
1443 &sensor_dev_attr_temp3_type.dev_attr.attr,
1444 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1445 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1446 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1448 &dev_attr_alarms.attr,
1449 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1450 &dev_attr_name.attr,
1454 static const struct attribute_group it87_group = {
1455 .attrs = it87_attributes,
1458 static struct attribute *it87_attributes_beep[] = {
1459 &sensor_dev_attr_in0_beep.dev_attr.attr,
1460 &sensor_dev_attr_in1_beep.dev_attr.attr,
1461 &sensor_dev_attr_in2_beep.dev_attr.attr,
1462 &sensor_dev_attr_in3_beep.dev_attr.attr,
1463 &sensor_dev_attr_in4_beep.dev_attr.attr,
1464 &sensor_dev_attr_in5_beep.dev_attr.attr,
1465 &sensor_dev_attr_in6_beep.dev_attr.attr,
1466 &sensor_dev_attr_in7_beep.dev_attr.attr,
1468 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1469 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1470 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1474 static const struct attribute_group it87_group_beep = {
1475 .attrs = it87_attributes_beep,
1478 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1479 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1480 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1481 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1484 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1485 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1486 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1489 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1490 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1491 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1494 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1495 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1496 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1499 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1500 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1501 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1505 static const struct attribute_group it87_group_fan16[5] = {
1506 { .attrs = it87_attributes_fan16[0] },
1507 { .attrs = it87_attributes_fan16[1] },
1508 { .attrs = it87_attributes_fan16[2] },
1509 { .attrs = it87_attributes_fan16[3] },
1510 { .attrs = it87_attributes_fan16[4] },
1513 static struct attribute *it87_attributes_fan[3][4+1] = { {
1514 &sensor_dev_attr_fan1_input.dev_attr.attr,
1515 &sensor_dev_attr_fan1_min.dev_attr.attr,
1516 &sensor_dev_attr_fan1_div.dev_attr.attr,
1517 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1520 &sensor_dev_attr_fan2_input.dev_attr.attr,
1521 &sensor_dev_attr_fan2_min.dev_attr.attr,
1522 &sensor_dev_attr_fan2_div.dev_attr.attr,
1523 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1526 &sensor_dev_attr_fan3_input.dev_attr.attr,
1527 &sensor_dev_attr_fan3_min.dev_attr.attr,
1528 &sensor_dev_attr_fan3_div.dev_attr.attr,
1529 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1533 static const struct attribute_group it87_group_fan[3] = {
1534 { .attrs = it87_attributes_fan[0] },
1535 { .attrs = it87_attributes_fan[1] },
1536 { .attrs = it87_attributes_fan[2] },
1539 static const struct attribute_group *
1540 it87_get_fan_group(const struct it87_data *data)
1542 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1545 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1546 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1547 &sensor_dev_attr_pwm1.dev_attr.attr,
1548 &dev_attr_pwm1_freq.attr,
1549 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1552 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1553 &sensor_dev_attr_pwm2.dev_attr.attr,
1554 &dev_attr_pwm2_freq.attr,
1555 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1558 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1559 &sensor_dev_attr_pwm3.dev_attr.attr,
1560 &dev_attr_pwm3_freq.attr,
1561 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1565 static const struct attribute_group it87_group_pwm[3] = {
1566 { .attrs = it87_attributes_pwm[0] },
1567 { .attrs = it87_attributes_pwm[1] },
1568 { .attrs = it87_attributes_pwm[2] },
1571 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1572 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1573 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1574 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1575 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1576 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1577 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1578 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1579 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1580 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1583 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1584 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1585 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1586 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1587 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1588 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1589 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1590 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1591 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1594 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1595 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1596 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1597 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1598 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1599 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1600 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1601 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1602 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1606 static const struct attribute_group it87_group_autopwm[3] = {
1607 { .attrs = it87_attributes_autopwm[0] },
1608 { .attrs = it87_attributes_autopwm[1] },
1609 { .attrs = it87_attributes_autopwm[2] },
1612 static struct attribute *it87_attributes_fan_beep[] = {
1613 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1614 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1615 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1616 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1617 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1620 static struct attribute *it87_attributes_vid[] = {
1622 &dev_attr_cpu0_vid.attr,
1626 static const struct attribute_group it87_group_vid = {
1627 .attrs = it87_attributes_vid,
1630 static struct attribute *it87_attributes_label[] = {
1631 &sensor_dev_attr_in3_label.dev_attr.attr,
1632 &sensor_dev_attr_in7_label.dev_attr.attr,
1633 &sensor_dev_attr_in8_label.dev_attr.attr,
1637 static const struct attribute_group it87_group_label = {
1638 .attrs = it87_attributes_label,
1641 /* SuperIO detection - will change isa_address if a chip is found */
1642 static int __init it87_find(unsigned short *address,
1643 struct it87_sio_data *sio_data)
1647 const char *board_vendor, *board_name;
1649 err = superio_enter();
1654 chip_type = force_id ? force_id : superio_inw(DEVID);
1656 switch (chip_type) {
1658 sio_data->type = it87;
1661 sio_data->type = it8712;
1665 sio_data->type = it8716;
1668 sio_data->type = it8718;
1671 sio_data->type = it8720;
1674 sio_data->type = it8721;
1677 sio_data->type = it8728;
1680 sio_data->type = it8783;
1682 case 0xffff: /* No device at all */
1685 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1689 superio_select(PME);
1690 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1691 pr_info("Device not activated, skipping\n");
1695 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1696 if (*address == 0) {
1697 pr_info("Base address not set, skipping\n");
1702 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1703 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1704 chip_type, *address, sio_data->revision);
1706 /* in8 (Vbat) is always internal */
1707 sio_data->internal = (1 << 2);
1709 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1710 if (sio_data->type == it87) {
1711 /* The IT8705F doesn't have VID pins at all */
1712 sio_data->skip_vid = 1;
1714 /* The IT8705F has a different LD number for GPIO */
1716 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1717 } else if (sio_data->type == it8783) {
1718 int reg25, reg27, reg2A, reg2C, regEF;
1720 sio_data->skip_vid = 1; /* No VID */
1722 superio_select(GPIO);
1724 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1725 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1726 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1727 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1728 regEF = superio_inb(IT87_SIO_SPI_REG);
1730 /* Check if fan3 is there or not */
1731 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1732 sio_data->skip_fan |= (1 << 2);
1733 if ((reg25 & (1 << 4))
1734 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1735 sio_data->skip_pwm |= (1 << 2);
1737 /* Check if fan2 is there or not */
1738 if (reg27 & (1 << 7))
1739 sio_data->skip_fan |= (1 << 1);
1740 if (reg27 & (1 << 3))
1741 sio_data->skip_pwm |= (1 << 1);
1744 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1748 if ((reg27 & (1 << 1)) || (reg2C & (1 << 2)))
1752 if ((reg27 & (1 << 2)) || (reg2C & (1 << 2))) {
1754 * If the external VIN7 pin is disabled, route it to the
1755 * internal VCCH5V if that is not already done.
1757 if (!(reg2C & (1 << 1))) {
1759 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1760 pr_notice("Routing internal VCCH to in7\n");
1764 if (reg2C & (1 << 0))
1765 sio_data->internal |= (1 << 0);
1766 if (reg2C & (1 << 1))
1767 sio_data->internal |= (1 << 1);
1769 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1774 superio_select(GPIO);
1776 reg = superio_inb(IT87_SIO_GPIO3_REG);
1777 if (sio_data->type == it8721 || sio_data->type == it8728) {
1779 * The IT8721F/IT8758E don't have VID pins at all,
1780 * not sure about the IT8728F.
1782 sio_data->skip_vid = 1;
1784 /* We need at least 4 VID pins */
1786 pr_info("VID is disabled (pins used for GPIO)\n");
1787 sio_data->skip_vid = 1;
1791 /* Check if fan3 is there or not */
1793 sio_data->skip_pwm |= (1 << 2);
1795 sio_data->skip_fan |= (1 << 2);
1797 /* Check if fan2 is there or not */
1798 reg = superio_inb(IT87_SIO_GPIO5_REG);
1800 sio_data->skip_pwm |= (1 << 1);
1802 sio_data->skip_fan |= (1 << 1);
1804 if ((sio_data->type == it8718 || sio_data->type == it8720)
1805 && !(sio_data->skip_vid))
1806 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1808 reg = superio_inb(IT87_SIO_PINX2_REG);
1810 * The IT8720F has no VIN7 pin, so VCCH should always be
1811 * routed internally to VIN7 with an internal divider.
1812 * Curiously, there still is a configuration bit to control
1813 * this, which means it can be set incorrectly. And even
1814 * more curiously, many boards out there are improperly
1815 * configured, even though the IT8720F datasheet claims
1816 * that the internal routing of VCCH to VIN7 is the default
1817 * setting. So we force the internal routing in this case.
1819 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1821 superio_outb(IT87_SIO_PINX2_REG, reg);
1822 pr_notice("Routing internal VCCH to in7\n");
1825 sio_data->internal |= (1 << 0);
1826 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1827 sio_data->type == it8728)
1828 sio_data->internal |= (1 << 1);
1830 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1832 if (sio_data->beep_pin)
1833 pr_info("Beeping is supported\n");
1835 /* Disable specific features based on DMI strings */
1836 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1837 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1838 if (board_vendor && board_name) {
1839 if (strcmp(board_vendor, "nVIDIA") == 0
1840 && strcmp(board_name, "FN68PT") == 0) {
1842 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1843 * connected to a fan, but to something else. One user
1844 * has reported instant system power-off when changing
1845 * the PWM2 duty cycle, so we disable it.
1846 * I use the board name string as the trigger in case
1847 * the same board is ever used in other systems.
1849 pr_info("Disabling pwm2 due to hardware constraints\n");
1850 sio_data->skip_pwm = (1 << 1);
1859 static void it87_remove_files(struct device *dev)
1861 struct it87_data *data = platform_get_drvdata(pdev);
1862 struct it87_sio_data *sio_data = dev->platform_data;
1863 const struct attribute_group *fan_group = it87_get_fan_group(data);
1866 sysfs_remove_group(&dev->kobj, &it87_group);
1867 if (sio_data->beep_pin)
1868 sysfs_remove_group(&dev->kobj, &it87_group_beep);
1869 for (i = 0; i < 5; i++) {
1870 if (!(data->has_fan & (1 << i)))
1872 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1873 if (sio_data->beep_pin)
1874 sysfs_remove_file(&dev->kobj,
1875 it87_attributes_fan_beep[i]);
1877 for (i = 0; i < 3; i++) {
1878 if (sio_data->skip_pwm & (1 << 0))
1880 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1881 if (has_old_autopwm(data))
1882 sysfs_remove_group(&dev->kobj,
1883 &it87_group_autopwm[i]);
1885 if (!sio_data->skip_vid)
1886 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1887 sysfs_remove_group(&dev->kobj, &it87_group_label);
1890 static int __devinit it87_probe(struct platform_device *pdev)
1892 struct it87_data *data;
1893 struct resource *res;
1894 struct device *dev = &pdev->dev;
1895 struct it87_sio_data *sio_data = dev->platform_data;
1896 const struct attribute_group *fan_group;
1898 int enable_pwm_interface;
1899 int fan_beep_need_rw;
1900 static const char * const names[] = {
1911 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1912 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1913 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1914 (unsigned long)res->start,
1915 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1920 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1926 data->addr = res->start;
1927 data->type = sio_data->type;
1928 data->revision = sio_data->revision;
1929 data->name = names[sio_data->type];
1931 /* Now, we do the remaining detection. */
1932 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1933 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1938 platform_set_drvdata(pdev, data);
1940 mutex_init(&data->update_lock);
1942 /* Check PWM configuration */
1943 enable_pwm_interface = it87_check_pwm(dev);
1945 /* Starting with IT8721F, we handle scaling of internal voltages */
1946 if (has_12mv_adc(data)) {
1947 if (sio_data->internal & (1 << 0))
1948 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1949 if (sio_data->internal & (1 << 1))
1950 data->in_scaled |= (1 << 7); /* in7 is VSB */
1951 if (sio_data->internal & (1 << 2))
1952 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1953 } else if (sio_data->type == it8783) {
1954 if (sio_data->internal & (1 << 0))
1955 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1956 if (sio_data->internal & (1 << 1))
1957 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
1960 /* Initialize the IT87 chip */
1961 it87_init_device(pdev);
1963 /* Register sysfs hooks */
1964 err = sysfs_create_group(&dev->kobj, &it87_group);
1968 if (sio_data->beep_pin) {
1969 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1974 /* Do not create fan files for disabled fans */
1975 fan_group = it87_get_fan_group(data);
1976 fan_beep_need_rw = 1;
1977 for (i = 0; i < 5; i++) {
1978 if (!(data->has_fan & (1 << i)))
1980 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1984 if (sio_data->beep_pin) {
1985 err = sysfs_create_file(&dev->kobj,
1986 it87_attributes_fan_beep[i]);
1989 if (!fan_beep_need_rw)
1993 * As we have a single beep enable bit for all fans,
1994 * only the first enabled fan has a writable attribute
1997 if (sysfs_chmod_file(&dev->kobj,
1998 it87_attributes_fan_beep[i],
2000 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2002 fan_beep_need_rw = 0;
2006 if (enable_pwm_interface) {
2007 for (i = 0; i < 3; i++) {
2008 if (sio_data->skip_pwm & (1 << i))
2010 err = sysfs_create_group(&dev->kobj,
2011 &it87_group_pwm[i]);
2015 if (!has_old_autopwm(data))
2017 err = sysfs_create_group(&dev->kobj,
2018 &it87_group_autopwm[i]);
2024 if (!sio_data->skip_vid) {
2025 data->vrm = vid_which_vrm();
2026 /* VID reading from Super-I/O config space if available */
2027 data->vid = sio_data->vid_value;
2028 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2033 /* Export labels for internal sensors */
2034 for (i = 0; i < 3; i++) {
2035 if (!(sio_data->internal & (1 << i)))
2037 err = sysfs_create_file(&dev->kobj,
2038 it87_attributes_label[i]);
2043 data->hwmon_dev = hwmon_device_register(dev);
2044 if (IS_ERR(data->hwmon_dev)) {
2045 err = PTR_ERR(data->hwmon_dev);
2052 it87_remove_files(dev);
2054 platform_set_drvdata(pdev, NULL);
2057 release_region(res->start, IT87_EC_EXTENT);
2062 static int __devexit it87_remove(struct platform_device *pdev)
2064 struct it87_data *data = platform_get_drvdata(pdev);
2066 hwmon_device_unregister(data->hwmon_dev);
2067 it87_remove_files(&pdev->dev);
2069 release_region(data->addr, IT87_EC_EXTENT);
2070 platform_set_drvdata(pdev, NULL);
2077 * Must be called with data->update_lock held, except during initialization.
2078 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2079 * would slow down the IT87 access and should not be necessary.
2081 static int it87_read_value(struct it87_data *data, u8 reg)
2083 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2084 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2088 * Must be called with data->update_lock held, except during initialization.
2089 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2090 * would slow down the IT87 access and should not be necessary.
2092 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2094 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2095 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2098 /* Return 1 if and only if the PWM interface is safe to use */
2099 static int __devinit it87_check_pwm(struct device *dev)
2101 struct it87_data *data = dev_get_drvdata(dev);
2103 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2104 * and polarity set to active low is sign that this is the case so we
2105 * disable pwm control to protect the user.
2107 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2108 if ((tmp & 0x87) == 0) {
2109 if (fix_pwm_polarity) {
2111 * The user asks us to attempt a chip reconfiguration.
2112 * This means switching to active high polarity and
2113 * inverting all fan speed values.
2118 for (i = 0; i < 3; i++)
2119 pwm[i] = it87_read_value(data,
2123 * If any fan is in automatic pwm mode, the polarity
2124 * might be correct, as suspicious as it seems, so we
2125 * better don't change anything (but still disable the
2128 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2129 dev_info(dev, "Reconfiguring PWM to "
2130 "active high polarity\n");
2131 it87_write_value(data, IT87_REG_FAN_CTL,
2133 for (i = 0; i < 3; i++)
2134 it87_write_value(data,
2140 dev_info(dev, "PWM configuration is "
2141 "too broken to be fixed\n");
2144 dev_info(dev, "Detected broken BIOS "
2145 "defaults, disabling PWM interface\n");
2147 } else if (fix_pwm_polarity) {
2148 dev_info(dev, "PWM configuration looks "
2149 "sane, won't touch\n");
2155 /* Called when we have found a new IT87. */
2156 static void __devinit it87_init_device(struct platform_device *pdev)
2158 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2159 struct it87_data *data = platform_get_drvdata(pdev);
2164 * For each PWM channel:
2165 * - If it is in automatic mode, setting to manual mode should set
2166 * the fan to full speed by default.
2167 * - If it is in manual mode, we need a mapping to temperature
2168 * channels to use when later setting to automatic mode later.
2169 * Use a 1:1 mapping by default (we are clueless.)
2170 * In both cases, the value can (and should) be changed by the user
2171 * prior to switching to a different mode.
2172 * Note that this is no longer needed for the IT8721F and later, as
2173 * these have separate registers for the temperature mapping and the
2174 * manual duty cycle.
2176 for (i = 0; i < 3; i++) {
2177 data->pwm_temp_map[i] = i;
2178 data->pwm_duty[i] = 0x7f; /* Full speed */
2179 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2183 * Some chips seem to have default value 0xff for all limit
2184 * registers. For low voltage limits it makes no sense and triggers
2185 * alarms, so change to 0 instead. For high temperature limits, it
2186 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2187 * but is still confusing, so change to 127 degrees C.
2189 for (i = 0; i < 8; i++) {
2190 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2192 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2194 for (i = 0; i < 3; i++) {
2195 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2197 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2201 * Temperature channels are not forcibly enabled, as they can be
2202 * set to two different sensor types and we can't guess which one
2203 * is correct for a given system. These channels can be enabled at
2204 * run-time through the temp{1-3}_type sysfs accessors if needed.
2207 /* Check if voltage monitors are reset manually or by some reason */
2208 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2209 if ((tmp & 0xff) == 0) {
2210 /* Enable all voltage monitors */
2211 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2214 /* Check if tachometers are reset manually or by some reason */
2215 mask = 0x70 & ~(sio_data->skip_fan << 4);
2216 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2217 if ((data->fan_main_ctrl & mask) == 0) {
2218 /* Enable all fan tachometers */
2219 data->fan_main_ctrl |= mask;
2220 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2221 data->fan_main_ctrl);
2223 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2225 /* Set tachometers to 16-bit mode if needed */
2226 if (has_16bit_fans(data)) {
2227 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2228 if (~tmp & 0x07 & data->has_fan) {
2230 "Setting fan1-3 to 16-bit mode\n");
2231 it87_write_value(data, IT87_REG_FAN_16BIT,
2234 /* IT8705F only supports three fans. */
2235 if (data->type != it87) {
2237 data->has_fan |= (1 << 3); /* fan4 enabled */
2239 data->has_fan |= (1 << 4); /* fan5 enabled */
2243 /* Fan input pins may be used for alternative functions */
2244 data->has_fan &= ~sio_data->skip_fan;
2246 /* Start monitoring */
2247 it87_write_value(data, IT87_REG_CONFIG,
2248 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
2249 | (update_vbat ? 0x41 : 0x01));
2252 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2254 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2255 if (has_newer_autopwm(data)) {
2256 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2257 data->pwm_duty[nr] = it87_read_value(data,
2258 IT87_REG_PWM_DUTY(nr));
2260 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2261 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2262 else /* Manual mode */
2263 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2266 if (has_old_autopwm(data)) {
2269 for (i = 0; i < 5 ; i++)
2270 data->auto_temp[nr][i] = it87_read_value(data,
2271 IT87_REG_AUTO_TEMP(nr, i));
2272 for (i = 0; i < 3 ; i++)
2273 data->auto_pwm[nr][i] = it87_read_value(data,
2274 IT87_REG_AUTO_PWM(nr, i));
2278 static struct it87_data *it87_update_device(struct device *dev)
2280 struct it87_data *data = dev_get_drvdata(dev);
2283 mutex_lock(&data->update_lock);
2285 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2289 * Cleared after each update, so reenable. Value
2290 * returned by this read will be previous value
2292 it87_write_value(data, IT87_REG_CONFIG,
2293 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2295 for (i = 0; i <= 7; i++) {
2297 it87_read_value(data, IT87_REG_VIN(i));
2299 it87_read_value(data, IT87_REG_VIN_MIN(i));
2301 it87_read_value(data, IT87_REG_VIN_MAX(i));
2303 /* in8 (battery) has no limit registers */
2304 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
2306 for (i = 0; i < 5; i++) {
2307 /* Skip disabled fans */
2308 if (!(data->has_fan & (1 << i)))
2312 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2313 data->fan[i] = it87_read_value(data,
2315 /* Add high byte if in 16-bit mode */
2316 if (has_16bit_fans(data)) {
2317 data->fan[i] |= it87_read_value(data,
2318 IT87_REG_FANX[i]) << 8;
2319 data->fan_min[i] |= it87_read_value(data,
2320 IT87_REG_FANX_MIN[i]) << 8;
2323 for (i = 0; i < 3; i++) {
2325 it87_read_value(data, IT87_REG_TEMP(i));
2326 data->temp_high[i] =
2327 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2329 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2332 /* Newer chips don't have clock dividers */
2333 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2334 i = it87_read_value(data, IT87_REG_FAN_DIV);
2335 data->fan_div[0] = i & 0x07;
2336 data->fan_div[1] = (i >> 3) & 0x07;
2337 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2341 it87_read_value(data, IT87_REG_ALARM1) |
2342 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2343 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2344 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2346 data->fan_main_ctrl = it87_read_value(data,
2347 IT87_REG_FAN_MAIN_CTRL);
2348 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2349 for (i = 0; i < 3; i++)
2350 it87_update_pwm_ctrl(data, i);
2352 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2354 * The IT8705F does not have VID capability.
2355 * The IT8718F and later don't use IT87_REG_VID for the
2358 if (data->type == it8712 || data->type == it8716) {
2359 data->vid = it87_read_value(data, IT87_REG_VID);
2361 * The older IT8712F revisions had only 5 VID pins,
2362 * but we assume it is always safe to read 6 bits.
2366 data->last_updated = jiffies;
2370 mutex_unlock(&data->update_lock);
2375 static int __init it87_device_add(unsigned short address,
2376 const struct it87_sio_data *sio_data)
2378 struct resource res = {
2379 .start = address + IT87_EC_OFFSET,
2380 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2382 .flags = IORESOURCE_IO,
2386 err = acpi_check_resource_conflict(&res);
2390 pdev = platform_device_alloc(DRVNAME, address);
2393 pr_err("Device allocation failed\n");
2397 err = platform_device_add_resources(pdev, &res, 1);
2399 pr_err("Device resource addition failed (%d)\n", err);
2400 goto exit_device_put;
2403 err = platform_device_add_data(pdev, sio_data,
2404 sizeof(struct it87_sio_data));
2406 pr_err("Platform data allocation failed\n");
2407 goto exit_device_put;
2410 err = platform_device_add(pdev);
2412 pr_err("Device addition failed (%d)\n", err);
2413 goto exit_device_put;
2419 platform_device_put(pdev);
2424 static int __init sm_it87_init(void)
2427 unsigned short isa_address = 0;
2428 struct it87_sio_data sio_data;
2430 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2431 err = it87_find(&isa_address, &sio_data);
2434 err = platform_driver_register(&it87_driver);
2438 err = it87_device_add(isa_address, &sio_data);
2440 platform_driver_unregister(&it87_driver);
2447 static void __exit sm_it87_exit(void)
2449 platform_device_unregister(pdev);
2450 platform_driver_unregister(&it87_driver);
2454 MODULE_AUTHOR("Chris Gauthron, "
2455 "Jean Delvare <khali@linux-fr.org>");
2456 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2457 module_param(update_vbat, bool, 0);
2458 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2459 module_param(fix_pwm_polarity, bool, 0);
2460 MODULE_PARM_DESC(fix_pwm_polarity,
2461 "Force PWM polarity to active high (DANGEROUS)");
2462 MODULE_LICENSE("GPL");
2464 module_init(sm_it87_init);
2465 module_exit(sm_it87_exit);