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>
64 #define DRVNAME "it87"
66 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8782,
69 static unsigned short force_id;
70 module_param(force_id, ushort, 0);
71 MODULE_PARM_DESC(force_id, "Override the detected device ID");
73 static struct platform_device *pdev;
75 #define REG 0x2e /* The register to read/write */
76 #define DEV 0x07 /* Register: Logical device select */
77 #define VAL 0x2f /* The value to read/write */
78 #define PME 0x04 /* The device with the fan registers in it */
80 /* The device with the IT8718F/IT8720F VID value in it */
83 #define DEVID 0x20 /* Register: Device ID */
84 #define DEVREV 0x22 /* Register: Device Revision */
86 static inline int superio_inb(int reg)
92 static inline void superio_outb(int reg, int val)
98 static int superio_inw(int reg)
108 static inline void superio_select(int ldn)
114 static inline int superio_enter(void)
117 * Try to reserve REG and REG + 1 for exclusive access.
119 if (!request_muxed_region(REG, 2, DRVNAME))
129 static inline void superio_exit(void)
133 release_region(REG, 2);
136 /* Logical device 4 registers */
137 #define IT8712F_DEVID 0x8712
138 #define IT8705F_DEVID 0x8705
139 #define IT8716F_DEVID 0x8716
140 #define IT8718F_DEVID 0x8718
141 #define IT8720F_DEVID 0x8720
142 #define IT8721F_DEVID 0x8721
143 #define IT8726F_DEVID 0x8726
144 #define IT8728F_DEVID 0x8728
145 #define IT8782F_DEVID 0x8782
146 #define IT8783E_DEVID 0x8783
147 #define IT87_ACT_REG 0x30
148 #define IT87_BASE_REG 0x60
150 /* Logical device 7 registers (IT8712F and later) */
151 #define IT87_SIO_GPIO1_REG 0x25
152 #define IT87_SIO_GPIO3_REG 0x27
153 #define IT87_SIO_GPIO5_REG 0x29
154 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
155 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
156 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
157 #define IT87_SIO_VID_REG 0xfc /* VID value */
158 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
160 /* Update battery voltage after every reading if true */
161 static bool update_vbat;
163 /* Not all BIOSes properly configure the PWM registers */
164 static bool fix_pwm_polarity;
166 /* Many IT87 constants specified below */
168 /* Length of ISA address segment */
169 #define IT87_EXTENT 8
171 /* Length of ISA address segment for Environmental Controller */
172 #define IT87_EC_EXTENT 2
174 /* Offset of EC registers from ISA base address */
175 #define IT87_EC_OFFSET 5
177 /* Where are the ISA address/data registers relative to the EC base address */
178 #define IT87_ADDR_REG_OFFSET 0
179 #define IT87_DATA_REG_OFFSET 1
181 /*----- The IT87 registers -----*/
183 #define IT87_REG_CONFIG 0x00
185 #define IT87_REG_ALARM1 0x01
186 #define IT87_REG_ALARM2 0x02
187 #define IT87_REG_ALARM3 0x03
190 * The IT8718F and IT8720F have the VID value in a different register, in
191 * Super-I/O configuration space.
193 #define IT87_REG_VID 0x0a
195 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
196 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
199 #define IT87_REG_FAN_DIV 0x0b
200 #define IT87_REG_FAN_16BIT 0x0c
202 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
204 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
205 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
206 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
207 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
208 #define IT87_REG_FAN_MAIN_CTRL 0x13
209 #define IT87_REG_FAN_CTL 0x14
210 #define IT87_REG_PWM(nr) (0x15 + (nr))
211 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
213 #define IT87_REG_VIN(nr) (0x20 + (nr))
214 #define IT87_REG_TEMP(nr) (0x29 + (nr))
216 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
217 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
218 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
219 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
221 #define IT87_REG_TEMP_OFFSET(nr) (0x55 + (1 << ((nr) - 1)))
223 #define IT87_REG_VIN_ENABLE 0x50
224 #define IT87_REG_TEMP_ENABLE 0x51
225 #define IT87_REG_TEMP_EXTRA 0x55
226 #define IT87_REG_BEEP_ENABLE 0x5c
228 #define IT87_REG_CHIPID 0x58
230 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
231 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
233 struct it87_devices {
234 const char * const name;
238 #define FEAT_12MV_ADC (1 << 0)
239 #define FEAT_NEWER_AUTOPWM (1 << 1)
240 #define FEAT_OLD_AUTOPWM (1 << 2)
241 #define FEAT_16BIT_FAN (1 << 3)
243 static const struct it87_devices it87_devices[] = {
246 .features = FEAT_OLD_AUTOPWM, /* may need to override */
250 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
254 .features = FEAT_16BIT_FAN,
258 .features = FEAT_16BIT_FAN,
262 .features = FEAT_16BIT_FAN,
266 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FAN,
270 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FAN,
274 .features = FEAT_16BIT_FAN,
278 .features = FEAT_16BIT_FAN,
282 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FAN)
283 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
284 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
285 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
287 struct it87_sio_data {
289 /* Values read from Super-I/O config space */
293 u8 internal; /* Internal sensors can be labeled */
294 /* Features skipped based on config or DMI */
303 * For each registered chip, we need to keep some data in memory.
304 * The structure is dynamically allocated.
307 struct device *hwmon_dev;
313 struct mutex update_lock;
314 char valid; /* !=0 if following fields are valid */
315 unsigned long last_updated; /* In jiffies */
317 u16 in_scaled; /* Internal voltage sensors are scaled */
318 u8 in[9][3]; /* [nr][0]=in, [1]=min, [2]=max */
319 u8 has_fan; /* Bitfield, fans enabled */
320 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
321 u8 has_temp; /* Bitfield, temp sensors enabled */
322 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
323 u8 sensor; /* Register value */
324 u8 fan_div[3]; /* Register encoding, shifted right */
325 u8 vid; /* Register encoding, combined */
327 u32 alarms; /* Register encoding, combined */
328 u8 beeps; /* Register encoding */
329 u8 fan_main_ctrl; /* Register value */
330 u8 fan_ctl; /* Register value */
333 * The following 3 arrays correspond to the same registers up to
334 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
335 * 7, and we want to preserve settings on mode changes, so we have
336 * to track all values separately.
337 * Starting with the IT8721F, the manual PWM duty cycles are stored
338 * in separate registers (8-bit values), so the separate tracking
339 * is no longer needed, but it is still done to keep the driver
342 u8 pwm_ctrl[3]; /* Register value */
343 u8 pwm_duty[3]; /* Manual PWM value set by user */
344 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
346 /* Automatic fan speed control registers */
347 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
348 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
351 static int adc_lsb(const struct it87_data *data, int nr)
353 int lsb = has_12mv_adc(data) ? 12 : 16;
354 if (data->in_scaled & (1 << nr))
359 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
361 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
362 return SENSORS_LIMIT(val, 0, 255);
365 static int in_from_reg(const struct it87_data *data, int nr, int val)
367 return val * adc_lsb(data, nr);
370 static inline u8 FAN_TO_REG(long rpm, int div)
374 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
375 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
379 static inline u16 FAN16_TO_REG(long rpm)
383 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
386 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
387 1350000 / ((val) * (div)))
388 /* The divider is fixed to 2 in 16-bit mode */
389 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
390 1350000 / ((val) * 2))
392 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
393 ((val) + 500) / 1000), -128, 127))
394 #define TEMP_FROM_REG(val) ((val) * 1000)
396 static u8 pwm_to_reg(const struct it87_data *data, long val)
398 if (has_newer_autopwm(data))
404 static int pwm_from_reg(const struct it87_data *data, u8 reg)
406 if (has_newer_autopwm(data))
409 return (reg & 0x7f) << 1;
413 static int DIV_TO_REG(int val)
416 while (answer < 7 && (val >>= 1))
420 #define DIV_FROM_REG(val) (1 << (val))
422 static const unsigned int pwm_freq[8] = {
433 static int it87_probe(struct platform_device *pdev);
434 static int __devexit it87_remove(struct platform_device *pdev);
436 static int it87_read_value(struct it87_data *data, u8 reg);
437 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
438 static struct it87_data *it87_update_device(struct device *dev);
439 static int it87_check_pwm(struct device *dev);
440 static void it87_init_device(struct platform_device *pdev);
443 static struct platform_driver it87_driver = {
445 .owner = THIS_MODULE,
449 .remove = __devexit_p(it87_remove),
452 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
455 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
457 int index = sattr->index;
459 struct it87_data *data = it87_update_device(dev);
460 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
463 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
464 const char *buf, size_t count)
466 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
468 int index = sattr->index;
470 struct it87_data *data = dev_get_drvdata(dev);
473 if (kstrtoul(buf, 10, &val) < 0)
476 mutex_lock(&data->update_lock);
477 data->in[nr][index] = in_to_reg(data, nr, val);
478 it87_write_value(data,
479 index == 1 ? IT87_REG_VIN_MIN(nr)
480 : IT87_REG_VIN_MAX(nr),
481 data->in[nr][index]);
482 mutex_unlock(&data->update_lock);
486 #define S_IRUGOWU (S_IRUGO | S_IWUSR)
488 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
489 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGOWU, show_in, set_in, 0, 1);
490 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGOWU, show_in, set_in, 0, 2);
492 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
493 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGOWU, show_in, set_in, 1, 1);
494 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGOWU, show_in, set_in, 1, 2);
496 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
497 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGOWU, show_in, set_in, 2, 1);
498 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGOWU, show_in, set_in, 2, 2);
500 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
501 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGOWU, show_in, set_in, 3, 1);
502 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGOWU, show_in, set_in, 3, 2);
504 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
505 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGOWU, show_in, set_in, 4, 1);
506 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGOWU, show_in, set_in, 4, 2);
508 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
509 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGOWU, show_in, set_in, 5, 1);
510 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGOWU, show_in, set_in, 5, 2);
512 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
513 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGOWU, show_in, set_in, 6, 1);
514 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGOWU, show_in, set_in, 6, 2);
516 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
517 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGOWU, show_in, set_in, 7, 1);
518 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGOWU, show_in, set_in, 7, 2);
520 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
523 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
526 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
528 int index = sattr->index;
529 struct it87_data *data = it87_update_device(dev);
531 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
534 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
537 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
539 int index = sattr->index;
540 struct it87_data *data = dev_get_drvdata(dev);
543 if (kstrtol(buf, 10, &val) < 0)
546 mutex_lock(&data->update_lock);
547 data->temp[nr][index] = TEMP_TO_REG(val);
548 it87_write_value(data,
549 index == 1 ? IT87_REG_TEMP_LOW(nr)
550 : index == 2 ? IT87_REG_TEMP_HIGH(nr)
551 : IT87_REG_TEMP_OFFSET(nr),
552 data->temp[nr][index]);
554 mutex_unlock(&data->update_lock);
558 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
559 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGOWU, show_temp, set_temp, 0, 1);
560 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGOWU, show_temp, set_temp, 0, 2);
561 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGOWU, show_temp, set_temp, 0, 3);
562 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
563 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGOWU, show_temp, set_temp, 1, 1);
564 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGOWU, show_temp, set_temp, 1, 2);
565 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGOWU, show_temp, set_temp, 1, 3);
566 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
567 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGOWU, show_temp, set_temp, 2, 1);
568 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGOWU, show_temp, set_temp, 2, 2);
569 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGOWU, show_temp, set_temp, 2, 3);
571 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
574 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
575 int nr = sensor_attr->index;
576 struct it87_data *data = it87_update_device(dev);
577 u8 reg = data->sensor; /* In case value is updated while used */
580 return sprintf(buf, "3\n"); /* thermal diode */
582 return sprintf(buf, "4\n"); /* thermistor */
583 return sprintf(buf, "0\n"); /* disabled */
586 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
587 const char *buf, size_t count)
589 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
590 int nr = sensor_attr->index;
592 struct it87_data *data = dev_get_drvdata(dev);
596 if (kstrtol(buf, 10, &val) < 0)
599 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
602 if (val == 2) { /* backwards compatibility */
604 "Sensor type 2 is deprecated, please use 4 instead\n");
607 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
615 mutex_lock(&data->update_lock);
617 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
618 data->valid = 0; /* Force cache refresh */
619 mutex_unlock(&data->update_lock);
623 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGOWU, show_type, set_type, 0);
624 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGOWU, show_type, set_type, 1);
625 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGOWU, show_type, set_type, 2);
629 static int pwm_mode(const struct it87_data *data, int nr)
631 int ctrl = data->fan_main_ctrl & (1 << nr);
633 if (ctrl == 0) /* Full speed */
635 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
637 else /* Manual mode */
641 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
644 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
646 int index = sattr->index;
648 struct it87_data *data = it87_update_device(dev);
650 speed = has_16bit_fans(data) ?
651 FAN16_FROM_REG(data->fan[nr][index]) :
652 FAN_FROM_REG(data->fan[nr][index],
653 DIV_FROM_REG(data->fan_div[nr]));
654 return sprintf(buf, "%d\n", speed);
657 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
660 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
661 int nr = sensor_attr->index;
663 struct it87_data *data = it87_update_device(dev);
664 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
666 static ssize_t show_pwm_enable(struct device *dev,
667 struct device_attribute *attr, char *buf)
669 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
670 int nr = sensor_attr->index;
672 struct it87_data *data = it87_update_device(dev);
673 return sprintf(buf, "%d\n", pwm_mode(data, nr));
675 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
678 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
679 int nr = sensor_attr->index;
681 struct it87_data *data = it87_update_device(dev);
682 return sprintf(buf, "%d\n",
683 pwm_from_reg(data, data->pwm_duty[nr]));
685 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
688 struct it87_data *data = it87_update_device(dev);
689 int index = (data->fan_ctl >> 4) & 0x07;
691 return sprintf(buf, "%u\n", pwm_freq[index]);
694 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
695 const char *buf, size_t count)
697 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
699 int index = sattr->index;
701 struct it87_data *data = dev_get_drvdata(dev);
705 if (kstrtol(buf, 10, &val) < 0)
708 mutex_lock(&data->update_lock);
710 if (!has_16bit_fans(data)) {
711 reg = it87_read_value(data, IT87_REG_FAN_DIV);
714 data->fan_div[nr] = reg & 0x07;
717 data->fan_div[nr] = (reg >> 3) & 0x07;
720 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
723 data->fan[nr][index] =
724 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
725 it87_write_value(data, IT87_REG_FAN_MIN[nr],
726 data->fan[nr][index]);
728 data->fan[nr][index] = FAN16_TO_REG(val);
729 it87_write_value(data, IT87_REG_FAN_MIN[nr],
730 data->fan[nr][index] & 0xff);
731 it87_write_value(data, IT87_REG_FANX_MIN[nr],
732 data->fan[nr][index] >> 8);
735 mutex_unlock(&data->update_lock);
739 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
740 const char *buf, size_t count)
742 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
743 int nr = sensor_attr->index;
745 struct it87_data *data = dev_get_drvdata(dev);
750 if (kstrtoul(buf, 10, &val) < 0)
753 mutex_lock(&data->update_lock);
754 old = it87_read_value(data, IT87_REG_FAN_DIV);
756 /* Save fan min limit */
757 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
762 data->fan_div[nr] = DIV_TO_REG(val);
766 data->fan_div[nr] = 1;
768 data->fan_div[nr] = 3;
771 val |= (data->fan_div[0] & 0x07);
772 val |= (data->fan_div[1] & 0x07) << 3;
773 if (data->fan_div[2] == 3)
775 it87_write_value(data, IT87_REG_FAN_DIV, val);
777 /* Restore fan min limit */
778 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
779 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
781 mutex_unlock(&data->update_lock);
785 /* Returns 0 if OK, -EINVAL otherwise */
786 static int check_trip_points(struct device *dev, int nr)
788 const struct it87_data *data = dev_get_drvdata(dev);
791 if (has_old_autopwm(data)) {
792 for (i = 0; i < 3; i++) {
793 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
796 for (i = 0; i < 2; i++) {
797 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
804 "Inconsistent trip points, not switching to automatic mode\n");
805 dev_err(dev, "Adjust the trip points and try again\n");
810 static ssize_t set_pwm_enable(struct device *dev,
811 struct device_attribute *attr, const char *buf, size_t count)
813 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
814 int nr = sensor_attr->index;
816 struct it87_data *data = dev_get_drvdata(dev);
819 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
822 /* Check trip points before switching to automatic mode */
824 if (check_trip_points(dev, nr) < 0)
828 mutex_lock(&data->update_lock);
832 /* make sure the fan is on when in on/off mode */
833 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
834 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
835 /* set on/off mode */
836 data->fan_main_ctrl &= ~(1 << nr);
837 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
838 data->fan_main_ctrl);
840 if (val == 1) /* Manual mode */
841 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
842 data->pwm_temp_map[nr] :
844 else /* Automatic mode */
845 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
846 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
847 /* set SmartGuardian mode */
848 data->fan_main_ctrl |= (1 << nr);
849 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
850 data->fan_main_ctrl);
853 mutex_unlock(&data->update_lock);
856 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
857 const char *buf, size_t count)
859 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
860 int nr = sensor_attr->index;
862 struct it87_data *data = dev_get_drvdata(dev);
865 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
868 mutex_lock(&data->update_lock);
869 if (has_newer_autopwm(data)) {
871 * If we are in automatic mode, the PWM duty cycle register
872 * is read-only so we can't write the value.
874 if (data->pwm_ctrl[nr] & 0x80) {
875 mutex_unlock(&data->update_lock);
878 data->pwm_duty[nr] = pwm_to_reg(data, val);
879 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
882 data->pwm_duty[nr] = pwm_to_reg(data, val);
884 * If we are in manual mode, write the duty cycle immediately;
885 * otherwise, just store it for later use.
887 if (!(data->pwm_ctrl[nr] & 0x80)) {
888 data->pwm_ctrl[nr] = data->pwm_duty[nr];
889 it87_write_value(data, IT87_REG_PWM(nr),
893 mutex_unlock(&data->update_lock);
896 static ssize_t set_pwm_freq(struct device *dev,
897 struct device_attribute *attr, const char *buf, size_t count)
899 struct it87_data *data = dev_get_drvdata(dev);
903 if (kstrtoul(buf, 10, &val) < 0)
906 /* Search for the nearest available frequency */
907 for (i = 0; i < 7; i++) {
908 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
912 mutex_lock(&data->update_lock);
913 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
914 data->fan_ctl |= i << 4;
915 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
916 mutex_unlock(&data->update_lock);
920 static ssize_t show_pwm_temp_map(struct device *dev,
921 struct device_attribute *attr, char *buf)
923 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
924 int nr = sensor_attr->index;
926 struct it87_data *data = it87_update_device(dev);
929 if (data->pwm_temp_map[nr] < 3)
930 map = 1 << data->pwm_temp_map[nr];
932 map = 0; /* Should never happen */
933 return sprintf(buf, "%d\n", map);
935 static ssize_t set_pwm_temp_map(struct device *dev,
936 struct device_attribute *attr, const char *buf, size_t count)
938 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
939 int nr = sensor_attr->index;
941 struct it87_data *data = dev_get_drvdata(dev);
946 * This check can go away if we ever support automatic fan speed
947 * control on newer chips.
949 if (!has_old_autopwm(data)) {
950 dev_notice(dev, "Mapping change disabled for safety reasons\n");
954 if (kstrtol(buf, 10, &val) < 0)
971 mutex_lock(&data->update_lock);
972 data->pwm_temp_map[nr] = reg;
974 * If we are in automatic mode, write the temp mapping immediately;
975 * otherwise, just store it for later use.
977 if (data->pwm_ctrl[nr] & 0x80) {
978 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
979 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
981 mutex_unlock(&data->update_lock);
985 static ssize_t show_auto_pwm(struct device *dev,
986 struct device_attribute *attr, char *buf)
988 struct it87_data *data = it87_update_device(dev);
989 struct sensor_device_attribute_2 *sensor_attr =
990 to_sensor_dev_attr_2(attr);
991 int nr = sensor_attr->nr;
992 int point = sensor_attr->index;
994 return sprintf(buf, "%d\n",
995 pwm_from_reg(data, data->auto_pwm[nr][point]));
998 static ssize_t set_auto_pwm(struct device *dev,
999 struct device_attribute *attr, const char *buf, size_t count)
1001 struct it87_data *data = dev_get_drvdata(dev);
1002 struct sensor_device_attribute_2 *sensor_attr =
1003 to_sensor_dev_attr_2(attr);
1004 int nr = sensor_attr->nr;
1005 int point = sensor_attr->index;
1008 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1011 mutex_lock(&data->update_lock);
1012 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1013 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1014 data->auto_pwm[nr][point]);
1015 mutex_unlock(&data->update_lock);
1019 static ssize_t show_auto_temp(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1022 struct it87_data *data = it87_update_device(dev);
1023 struct sensor_device_attribute_2 *sensor_attr =
1024 to_sensor_dev_attr_2(attr);
1025 int nr = sensor_attr->nr;
1026 int point = sensor_attr->index;
1028 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1031 static ssize_t set_auto_temp(struct device *dev,
1032 struct device_attribute *attr, const char *buf, size_t count)
1034 struct it87_data *data = dev_get_drvdata(dev);
1035 struct sensor_device_attribute_2 *sensor_attr =
1036 to_sensor_dev_attr_2(attr);
1037 int nr = sensor_attr->nr;
1038 int point = sensor_attr->index;
1041 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1044 mutex_lock(&data->update_lock);
1045 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1046 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1047 data->auto_temp[nr][point]);
1048 mutex_unlock(&data->update_lock);
1052 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1053 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGOWU, show_fan, set_fan, 0, 1);
1054 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGOWU, show_fan_div, set_fan_div, 0);
1056 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1057 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGOWU, show_fan, set_fan, 1, 1);
1058 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGOWU, show_fan_div, set_fan_div, 1);
1060 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1061 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGOWU, show_fan, set_fan, 2, 1);
1062 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGOWU, show_fan_div, set_fan_div, 2);
1064 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1065 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGOWU, show_fan, set_fan, 3, 1);
1067 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1068 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGOWU, show_fan, set_fan, 4, 1);
1070 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGOWU,
1071 show_pwm_enable, set_pwm_enable, 0);
1072 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGOWU, show_pwm, set_pwm, 0);
1073 static DEVICE_ATTR(pwm1_freq, S_IRUGOWU, show_pwm_freq, set_pwm_freq);
1074 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGOWU,
1075 show_pwm_temp_map, set_pwm_temp_map, 0);
1076 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGOWU,
1077 show_auto_pwm, set_auto_pwm, 0, 0);
1078 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGOWU,
1079 show_auto_pwm, set_auto_pwm, 0, 1);
1080 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGOWU,
1081 show_auto_pwm, set_auto_pwm, 0, 2);
1082 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1083 show_auto_pwm, NULL, 0, 3);
1084 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGOWU,
1085 show_auto_temp, set_auto_temp, 0, 1);
1086 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGOWU,
1087 show_auto_temp, set_auto_temp, 0, 0);
1088 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGOWU,
1089 show_auto_temp, set_auto_temp, 0, 2);
1090 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGOWU,
1091 show_auto_temp, set_auto_temp, 0, 3);
1092 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGOWU,
1093 show_auto_temp, set_auto_temp, 0, 4);
1095 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGOWU,
1096 show_pwm_enable, set_pwm_enable, 1);
1097 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGOWU, show_pwm, set_pwm, 1);
1098 static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1099 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGOWU,
1100 show_pwm_temp_map, set_pwm_temp_map, 1);
1101 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGOWU,
1102 show_auto_pwm, set_auto_pwm, 1, 0);
1103 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGOWU,
1104 show_auto_pwm, set_auto_pwm, 1, 1);
1105 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGOWU,
1106 show_auto_pwm, set_auto_pwm, 1, 2);
1107 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1108 show_auto_pwm, NULL, 1, 3);
1109 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGOWU,
1110 show_auto_temp, set_auto_temp, 1, 1);
1111 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGOWU,
1112 show_auto_temp, set_auto_temp, 1, 0);
1113 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGOWU,
1114 show_auto_temp, set_auto_temp, 1, 2);
1115 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGOWU,
1116 show_auto_temp, set_auto_temp, 1, 3);
1117 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGOWU,
1118 show_auto_temp, set_auto_temp, 1, 4);
1120 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGOWU,
1121 show_pwm_enable, set_pwm_enable, 2);
1122 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGOWU, show_pwm, set_pwm, 2);
1123 static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1124 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGOWU,
1125 show_pwm_temp_map, set_pwm_temp_map, 2);
1126 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGOWU,
1127 show_auto_pwm, set_auto_pwm, 2, 0);
1128 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGOWU,
1129 show_auto_pwm, set_auto_pwm, 2, 1);
1130 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGOWU,
1131 show_auto_pwm, set_auto_pwm, 2, 2);
1132 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1133 show_auto_pwm, NULL, 2, 3);
1134 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGOWU,
1135 show_auto_temp, set_auto_temp, 2, 1);
1136 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGOWU,
1137 show_auto_temp, set_auto_temp, 2, 0);
1138 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGOWU,
1139 show_auto_temp, set_auto_temp, 2, 2);
1140 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGOWU,
1141 show_auto_temp, set_auto_temp, 2, 3);
1142 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGOWU,
1143 show_auto_temp, set_auto_temp, 2, 4);
1146 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1149 struct it87_data *data = it87_update_device(dev);
1150 return sprintf(buf, "%u\n", data->alarms);
1152 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1154 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1157 int bitnr = to_sensor_dev_attr(attr)->index;
1158 struct it87_data *data = it87_update_device(dev);
1159 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1162 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1163 *attr, const char *buf, size_t count)
1165 struct it87_data *data = dev_get_drvdata(dev);
1169 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1172 mutex_lock(&data->update_lock);
1173 config = it87_read_value(data, IT87_REG_CONFIG);
1178 it87_write_value(data, IT87_REG_CONFIG, config);
1179 /* Invalidate cache to force re-read */
1182 mutex_unlock(&data->update_lock);
1187 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1188 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1189 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1190 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1191 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1192 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1193 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1194 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1195 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1196 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1197 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1198 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1199 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1200 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1201 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1202 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1203 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1204 show_alarm, clear_intrusion, 4);
1206 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1209 int bitnr = to_sensor_dev_attr(attr)->index;
1210 struct it87_data *data = it87_update_device(dev);
1211 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1213 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1214 const char *buf, size_t count)
1216 int bitnr = to_sensor_dev_attr(attr)->index;
1217 struct it87_data *data = dev_get_drvdata(dev);
1220 if (kstrtol(buf, 10, &val) < 0
1221 || (val != 0 && val != 1))
1224 mutex_lock(&data->update_lock);
1225 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1227 data->beeps |= (1 << bitnr);
1229 data->beeps &= ~(1 << bitnr);
1230 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1231 mutex_unlock(&data->update_lock);
1235 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1236 show_beep, set_beep, 1);
1237 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1238 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1239 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1240 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1241 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1242 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1243 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1244 /* fanX_beep writability is set later */
1245 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1246 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1247 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1248 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1249 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1250 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1251 show_beep, set_beep, 2);
1252 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1253 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1255 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1258 struct it87_data *data = dev_get_drvdata(dev);
1259 return sprintf(buf, "%u\n", data->vrm);
1261 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1262 const char *buf, size_t count)
1264 struct it87_data *data = dev_get_drvdata(dev);
1267 if (kstrtoul(buf, 10, &val) < 0)
1274 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1276 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1279 struct it87_data *data = it87_update_device(dev);
1280 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1282 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1284 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1287 static const char * const labels[] = {
1292 static const char * const labels_it8721[] = {
1297 struct it87_data *data = dev_get_drvdata(dev);
1298 int nr = to_sensor_dev_attr(attr)->index;
1300 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1303 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1304 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1305 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1307 static ssize_t show_name(struct device *dev, struct device_attribute
1308 *devattr, char *buf)
1310 struct it87_data *data = dev_get_drvdata(dev);
1311 return sprintf(buf, "%s\n", data->name);
1313 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1315 static struct attribute *it87_attributes_in[9][5] = {
1317 &sensor_dev_attr_in0_input.dev_attr.attr,
1318 &sensor_dev_attr_in0_min.dev_attr.attr,
1319 &sensor_dev_attr_in0_max.dev_attr.attr,
1320 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1323 &sensor_dev_attr_in1_input.dev_attr.attr,
1324 &sensor_dev_attr_in1_min.dev_attr.attr,
1325 &sensor_dev_attr_in1_max.dev_attr.attr,
1326 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1329 &sensor_dev_attr_in2_input.dev_attr.attr,
1330 &sensor_dev_attr_in2_min.dev_attr.attr,
1331 &sensor_dev_attr_in2_max.dev_attr.attr,
1332 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1335 &sensor_dev_attr_in3_input.dev_attr.attr,
1336 &sensor_dev_attr_in3_min.dev_attr.attr,
1337 &sensor_dev_attr_in3_max.dev_attr.attr,
1338 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1341 &sensor_dev_attr_in4_input.dev_attr.attr,
1342 &sensor_dev_attr_in4_min.dev_attr.attr,
1343 &sensor_dev_attr_in4_max.dev_attr.attr,
1344 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1347 &sensor_dev_attr_in5_input.dev_attr.attr,
1348 &sensor_dev_attr_in5_min.dev_attr.attr,
1349 &sensor_dev_attr_in5_max.dev_attr.attr,
1350 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1353 &sensor_dev_attr_in6_input.dev_attr.attr,
1354 &sensor_dev_attr_in6_min.dev_attr.attr,
1355 &sensor_dev_attr_in6_max.dev_attr.attr,
1356 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1359 &sensor_dev_attr_in7_input.dev_attr.attr,
1360 &sensor_dev_attr_in7_min.dev_attr.attr,
1361 &sensor_dev_attr_in7_max.dev_attr.attr,
1362 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1365 &sensor_dev_attr_in8_input.dev_attr.attr,
1369 static const struct attribute_group it87_group_in[9] = {
1370 { .attrs = it87_attributes_in[0] },
1371 { .attrs = it87_attributes_in[1] },
1372 { .attrs = it87_attributes_in[2] },
1373 { .attrs = it87_attributes_in[3] },
1374 { .attrs = it87_attributes_in[4] },
1375 { .attrs = it87_attributes_in[5] },
1376 { .attrs = it87_attributes_in[6] },
1377 { .attrs = it87_attributes_in[7] },
1378 { .attrs = it87_attributes_in[8] },
1381 static struct attribute *it87_attributes_temp[3][7] = {
1383 &sensor_dev_attr_temp1_input.dev_attr.attr,
1384 &sensor_dev_attr_temp1_max.dev_attr.attr,
1385 &sensor_dev_attr_temp1_min.dev_attr.attr,
1386 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1387 &sensor_dev_attr_temp1_type.dev_attr.attr,
1388 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1391 &sensor_dev_attr_temp2_input.dev_attr.attr,
1392 &sensor_dev_attr_temp2_max.dev_attr.attr,
1393 &sensor_dev_attr_temp2_min.dev_attr.attr,
1394 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1395 &sensor_dev_attr_temp2_type.dev_attr.attr,
1396 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1399 &sensor_dev_attr_temp3_input.dev_attr.attr,
1400 &sensor_dev_attr_temp3_max.dev_attr.attr,
1401 &sensor_dev_attr_temp3_min.dev_attr.attr,
1402 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1403 &sensor_dev_attr_temp3_type.dev_attr.attr,
1404 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1408 static const struct attribute_group it87_group_temp[3] = {
1409 { .attrs = it87_attributes_temp[0] },
1410 { .attrs = it87_attributes_temp[1] },
1411 { .attrs = it87_attributes_temp[2] },
1414 static struct attribute *it87_attributes[] = {
1415 &dev_attr_alarms.attr,
1416 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1417 &dev_attr_name.attr,
1421 static const struct attribute_group it87_group = {
1422 .attrs = it87_attributes,
1425 static struct attribute *it87_attributes_in_beep[] = {
1426 &sensor_dev_attr_in0_beep.dev_attr.attr,
1427 &sensor_dev_attr_in1_beep.dev_attr.attr,
1428 &sensor_dev_attr_in2_beep.dev_attr.attr,
1429 &sensor_dev_attr_in3_beep.dev_attr.attr,
1430 &sensor_dev_attr_in4_beep.dev_attr.attr,
1431 &sensor_dev_attr_in5_beep.dev_attr.attr,
1432 &sensor_dev_attr_in6_beep.dev_attr.attr,
1433 &sensor_dev_attr_in7_beep.dev_attr.attr,
1437 static struct attribute *it87_attributes_temp_beep[] = {
1438 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1439 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1440 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1443 static struct attribute *it87_attributes_fan[5][3+1] = { {
1444 &sensor_dev_attr_fan1_input.dev_attr.attr,
1445 &sensor_dev_attr_fan1_min.dev_attr.attr,
1446 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1449 &sensor_dev_attr_fan2_input.dev_attr.attr,
1450 &sensor_dev_attr_fan2_min.dev_attr.attr,
1451 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1454 &sensor_dev_attr_fan3_input.dev_attr.attr,
1455 &sensor_dev_attr_fan3_min.dev_attr.attr,
1456 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1459 &sensor_dev_attr_fan4_input.dev_attr.attr,
1460 &sensor_dev_attr_fan4_min.dev_attr.attr,
1461 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1464 &sensor_dev_attr_fan5_input.dev_attr.attr,
1465 &sensor_dev_attr_fan5_min.dev_attr.attr,
1466 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1470 static const struct attribute_group it87_group_fan[5] = {
1471 { .attrs = it87_attributes_fan[0] },
1472 { .attrs = it87_attributes_fan[1] },
1473 { .attrs = it87_attributes_fan[2] },
1474 { .attrs = it87_attributes_fan[3] },
1475 { .attrs = it87_attributes_fan[4] },
1478 static const struct attribute *it87_attributes_fan_div[] = {
1479 &sensor_dev_attr_fan1_div.dev_attr.attr,
1480 &sensor_dev_attr_fan2_div.dev_attr.attr,
1481 &sensor_dev_attr_fan3_div.dev_attr.attr,
1484 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1485 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1486 &sensor_dev_attr_pwm1.dev_attr.attr,
1487 &dev_attr_pwm1_freq.attr,
1488 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1491 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1492 &sensor_dev_attr_pwm2.dev_attr.attr,
1493 &dev_attr_pwm2_freq.attr,
1494 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1497 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1498 &sensor_dev_attr_pwm3.dev_attr.attr,
1499 &dev_attr_pwm3_freq.attr,
1500 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1504 static const struct attribute_group it87_group_pwm[3] = {
1505 { .attrs = it87_attributes_pwm[0] },
1506 { .attrs = it87_attributes_pwm[1] },
1507 { .attrs = it87_attributes_pwm[2] },
1510 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1511 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1512 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1513 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1514 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1515 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1516 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1517 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1518 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1519 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1522 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1523 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1524 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1525 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1526 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1527 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1528 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1529 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1530 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1533 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1534 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1535 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1536 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1537 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1538 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1539 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1540 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1541 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1545 static const struct attribute_group it87_group_autopwm[3] = {
1546 { .attrs = it87_attributes_autopwm[0] },
1547 { .attrs = it87_attributes_autopwm[1] },
1548 { .attrs = it87_attributes_autopwm[2] },
1551 static struct attribute *it87_attributes_fan_beep[] = {
1552 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1553 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1554 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1555 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1556 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1559 static struct attribute *it87_attributes_vid[] = {
1561 &dev_attr_cpu0_vid.attr,
1565 static const struct attribute_group it87_group_vid = {
1566 .attrs = it87_attributes_vid,
1569 static struct attribute *it87_attributes_label[] = {
1570 &sensor_dev_attr_in3_label.dev_attr.attr,
1571 &sensor_dev_attr_in7_label.dev_attr.attr,
1572 &sensor_dev_attr_in8_label.dev_attr.attr,
1576 static const struct attribute_group it87_group_label = {
1577 .attrs = it87_attributes_label,
1580 /* SuperIO detection - will change isa_address if a chip is found */
1581 static int __init it87_find(unsigned short *address,
1582 struct it87_sio_data *sio_data)
1586 const char *board_vendor, *board_name;
1588 err = superio_enter();
1593 chip_type = force_id ? force_id : superio_inw(DEVID);
1595 switch (chip_type) {
1597 sio_data->type = it87;
1600 sio_data->type = it8712;
1604 sio_data->type = it8716;
1607 sio_data->type = it8718;
1610 sio_data->type = it8720;
1613 sio_data->type = it8721;
1616 sio_data->type = it8728;
1619 sio_data->type = it8782;
1622 sio_data->type = it8783;
1624 case 0xffff: /* No device at all */
1627 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1631 superio_select(PME);
1632 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1633 pr_info("Device not activated, skipping\n");
1637 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1638 if (*address == 0) {
1639 pr_info("Base address not set, skipping\n");
1644 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1645 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1646 chip_type, *address, sio_data->revision);
1648 /* in8 (Vbat) is always internal */
1649 sio_data->internal = (1 << 2);
1651 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1652 if (sio_data->type == it87) {
1653 /* The IT8705F doesn't have VID pins at all */
1654 sio_data->skip_vid = 1;
1656 /* The IT8705F has a different LD number for GPIO */
1658 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1659 } else if (sio_data->type == it8783) {
1660 int reg25, reg27, reg2A, reg2C, regEF;
1662 sio_data->skip_vid = 1; /* No VID */
1664 superio_select(GPIO);
1666 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1667 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1668 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1669 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1670 regEF = superio_inb(IT87_SIO_SPI_REG);
1672 /* Check if fan3 is there or not */
1673 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1674 sio_data->skip_fan |= (1 << 2);
1675 if ((reg25 & (1 << 4))
1676 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1677 sio_data->skip_pwm |= (1 << 2);
1679 /* Check if fan2 is there or not */
1680 if (reg27 & (1 << 7))
1681 sio_data->skip_fan |= (1 << 1);
1682 if (reg27 & (1 << 3))
1683 sio_data->skip_pwm |= (1 << 1);
1686 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1687 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1690 if (reg27 & (1 << 1))
1691 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1695 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1697 if (reg27 & (1 << 2)) {
1699 * The data sheet is a bit unclear regarding the
1700 * internal voltage divider for VCCH5V. It says
1701 * "This bit enables and switches VIN7 (pin 91) to the
1702 * internal voltage divider for VCCH5V".
1703 * This is different to other chips, where the internal
1704 * voltage divider would connect VIN7 to an internal
1705 * voltage source. Maybe that is the case here as well.
1707 * Since we don't know for sure, re-route it if that is
1708 * not the case, and ask the user to report if the
1709 * resulting voltage is sane.
1711 if (!(reg2C & (1 << 1))) {
1713 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1714 pr_notice("Routing internal VCCH5V to in7.\n");
1716 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1717 pr_notice("Please report if it displays a reasonable voltage.\n");
1720 if (reg2C & (1 << 0))
1721 sio_data->internal |= (1 << 0);
1722 if (reg2C & (1 << 1))
1723 sio_data->internal |= (1 << 1);
1725 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1731 superio_select(GPIO);
1733 reg = superio_inb(IT87_SIO_GPIO3_REG);
1734 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1735 sio_data->type == it8782) {
1737 * IT8721F/IT8758E, and IT8782F don't have VID pins
1738 * at all, not sure about the IT8728F.
1740 sio_data->skip_vid = 1;
1742 /* We need at least 4 VID pins */
1744 pr_info("VID is disabled (pins used for GPIO)\n");
1745 sio_data->skip_vid = 1;
1749 /* Check if fan3 is there or not */
1751 sio_data->skip_pwm |= (1 << 2);
1753 sio_data->skip_fan |= (1 << 2);
1755 /* Check if fan2 is there or not */
1756 reg = superio_inb(IT87_SIO_GPIO5_REG);
1758 sio_data->skip_pwm |= (1 << 1);
1760 sio_data->skip_fan |= (1 << 1);
1762 if ((sio_data->type == it8718 || sio_data->type == it8720)
1763 && !(sio_data->skip_vid))
1764 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1766 reg = superio_inb(IT87_SIO_PINX2_REG);
1768 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1771 * The IT8720F has no VIN7 pin, so VCCH should always be
1772 * routed internally to VIN7 with an internal divider.
1773 * Curiously, there still is a configuration bit to control
1774 * this, which means it can be set incorrectly. And even
1775 * more curiously, many boards out there are improperly
1776 * configured, even though the IT8720F datasheet claims
1777 * that the internal routing of VCCH to VIN7 is the default
1778 * setting. So we force the internal routing in this case.
1780 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1781 * If UART6 is enabled, re-route VIN7 to the internal divider
1782 * if that is not already the case.
1784 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
1786 superio_outb(IT87_SIO_PINX2_REG, reg);
1787 pr_notice("Routing internal VCCH to in7\n");
1790 sio_data->internal |= (1 << 0);
1791 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1792 sio_data->type == it8728)
1793 sio_data->internal |= (1 << 1);
1796 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1797 * While VIN7 can be routed to the internal voltage divider,
1798 * VIN5 and VIN6 are not available if UART6 is enabled.
1800 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1801 * is the temperature source. Since we can not read the
1802 * temperature source here, skip_temp is preliminary.
1805 sio_data->skip_in |= (1 << 5) | (1 << 6);
1806 sio_data->skip_temp |= (1 << 2);
1809 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1811 if (sio_data->beep_pin)
1812 pr_info("Beeping is supported\n");
1814 /* Disable specific features based on DMI strings */
1815 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1816 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1817 if (board_vendor && board_name) {
1818 if (strcmp(board_vendor, "nVIDIA") == 0
1819 && strcmp(board_name, "FN68PT") == 0) {
1821 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1822 * connected to a fan, but to something else. One user
1823 * has reported instant system power-off when changing
1824 * the PWM2 duty cycle, so we disable it.
1825 * I use the board name string as the trigger in case
1826 * the same board is ever used in other systems.
1828 pr_info("Disabling pwm2 due to hardware constraints\n");
1829 sio_data->skip_pwm = (1 << 1);
1838 static void it87_remove_files(struct device *dev)
1840 struct it87_data *data = platform_get_drvdata(pdev);
1841 struct it87_sio_data *sio_data = dev->platform_data;
1844 sysfs_remove_group(&dev->kobj, &it87_group);
1845 for (i = 0; i < 9; i++) {
1846 if (sio_data->skip_in & (1 << i))
1848 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1849 if (it87_attributes_in_beep[i])
1850 sysfs_remove_file(&dev->kobj,
1851 it87_attributes_in_beep[i]);
1853 for (i = 0; i < 3; i++) {
1854 if (!(data->has_temp & (1 << i)))
1856 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1857 if (sio_data->beep_pin)
1858 sysfs_remove_file(&dev->kobj,
1859 it87_attributes_temp_beep[i]);
1861 for (i = 0; i < 5; i++) {
1862 if (!(data->has_fan & (1 << i)))
1864 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
1865 if (sio_data->beep_pin)
1866 sysfs_remove_file(&dev->kobj,
1867 it87_attributes_fan_beep[i]);
1868 if (i < 3 && !has_16bit_fans(data))
1869 sysfs_remove_file(&dev->kobj,
1870 it87_attributes_fan_div[i]);
1872 for (i = 0; i < 3; i++) {
1873 if (sio_data->skip_pwm & (1 << 0))
1875 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1876 if (has_old_autopwm(data))
1877 sysfs_remove_group(&dev->kobj,
1878 &it87_group_autopwm[i]);
1880 if (!sio_data->skip_vid)
1881 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1882 sysfs_remove_group(&dev->kobj, &it87_group_label);
1885 static int __devinit it87_probe(struct platform_device *pdev)
1887 struct it87_data *data;
1888 struct resource *res;
1889 struct device *dev = &pdev->dev;
1890 struct it87_sio_data *sio_data = dev->platform_data;
1892 int enable_pwm_interface;
1893 int fan_beep_need_rw;
1895 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1896 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1898 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1899 (unsigned long)res->start,
1900 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1904 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
1908 data->addr = res->start;
1909 data->type = sio_data->type;
1910 data->features = it87_devices[data->type].features;
1911 data->name = it87_devices[sio_data->type].name;
1913 * IT8705F Datasheet 0.4.1, 3h == Version G.
1914 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
1915 * These are the first revisions with 16-bit tachometer support.
1917 switch (data->type) {
1919 if (sio_data->revision >= 0x03) {
1920 data->features &= ~FEAT_OLD_AUTOPWM;
1921 data->features |= FEAT_16BIT_FAN;
1925 if (sio_data->revision >= 0x08) {
1926 data->features &= ~FEAT_OLD_AUTOPWM;
1927 data->features |= FEAT_16BIT_FAN;
1934 /* Now, we do the remaining detection. */
1935 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1936 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
1939 platform_set_drvdata(pdev, data);
1941 mutex_init(&data->update_lock);
1943 /* Check PWM configuration */
1944 enable_pwm_interface = it87_check_pwm(dev);
1946 /* Starting with IT8721F, we handle scaling of internal voltages */
1947 if (has_12mv_adc(data)) {
1948 if (sio_data->internal & (1 << 0))
1949 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1950 if (sio_data->internal & (1 << 1))
1951 data->in_scaled |= (1 << 7); /* in7 is VSB */
1952 if (sio_data->internal & (1 << 2))
1953 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1954 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
1955 if (sio_data->internal & (1 << 0))
1956 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1957 if (sio_data->internal & (1 << 1))
1958 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
1961 data->has_temp = 0x07;
1962 if (sio_data->skip_temp & (1 << 2)) {
1963 if (sio_data->type == it8782
1964 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
1965 data->has_temp &= ~(1 << 2);
1968 /* Initialize the IT87 chip */
1969 it87_init_device(pdev);
1971 /* Register sysfs hooks */
1972 err = sysfs_create_group(&dev->kobj, &it87_group);
1976 for (i = 0; i < 9; i++) {
1977 if (sio_data->skip_in & (1 << i))
1979 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
1982 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
1983 err = sysfs_create_file(&dev->kobj,
1984 it87_attributes_in_beep[i]);
1990 for (i = 0; i < 3; i++) {
1991 if (!(data->has_temp & (1 << i)))
1993 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
1996 if (sio_data->beep_pin) {
1997 err = sysfs_create_file(&dev->kobj,
1998 it87_attributes_temp_beep[i]);
2004 /* Do not create fan files for disabled fans */
2005 fan_beep_need_rw = 1;
2006 for (i = 0; i < 5; i++) {
2007 if (!(data->has_fan & (1 << i)))
2009 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2013 if (i < 3 && !has_16bit_fans(data)) {
2014 err = sysfs_create_file(&dev->kobj,
2015 it87_attributes_fan_div[i]);
2020 if (sio_data->beep_pin) {
2021 err = sysfs_create_file(&dev->kobj,
2022 it87_attributes_fan_beep[i]);
2025 if (!fan_beep_need_rw)
2029 * As we have a single beep enable bit for all fans,
2030 * only the first enabled fan has a writable attribute
2033 if (sysfs_chmod_file(&dev->kobj,
2034 it87_attributes_fan_beep[i],
2036 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2038 fan_beep_need_rw = 0;
2042 if (enable_pwm_interface) {
2043 for (i = 0; i < 3; i++) {
2044 if (sio_data->skip_pwm & (1 << i))
2046 err = sysfs_create_group(&dev->kobj,
2047 &it87_group_pwm[i]);
2051 if (!has_old_autopwm(data))
2053 err = sysfs_create_group(&dev->kobj,
2054 &it87_group_autopwm[i]);
2060 if (!sio_data->skip_vid) {
2061 data->vrm = vid_which_vrm();
2062 /* VID reading from Super-I/O config space if available */
2063 data->vid = sio_data->vid_value;
2064 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2069 /* Export labels for internal sensors */
2070 for (i = 0; i < 3; i++) {
2071 if (!(sio_data->internal & (1 << i)))
2073 err = sysfs_create_file(&dev->kobj,
2074 it87_attributes_label[i]);
2079 data->hwmon_dev = hwmon_device_register(dev);
2080 if (IS_ERR(data->hwmon_dev)) {
2081 err = PTR_ERR(data->hwmon_dev);
2088 it87_remove_files(dev);
2092 static int __devexit it87_remove(struct platform_device *pdev)
2094 struct it87_data *data = platform_get_drvdata(pdev);
2096 hwmon_device_unregister(data->hwmon_dev);
2097 it87_remove_files(&pdev->dev);
2103 * Must be called with data->update_lock held, except during initialization.
2104 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2105 * would slow down the IT87 access and should not be necessary.
2107 static int it87_read_value(struct it87_data *data, u8 reg)
2109 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2110 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2114 * Must be called with data->update_lock held, except during initialization.
2115 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2116 * would slow down the IT87 access and should not be necessary.
2118 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2120 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2121 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2124 /* Return 1 if and only if the PWM interface is safe to use */
2125 static int __devinit it87_check_pwm(struct device *dev)
2127 struct it87_data *data = dev_get_drvdata(dev);
2129 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2130 * and polarity set to active low is sign that this is the case so we
2131 * disable pwm control to protect the user.
2133 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2134 if ((tmp & 0x87) == 0) {
2135 if (fix_pwm_polarity) {
2137 * The user asks us to attempt a chip reconfiguration.
2138 * This means switching to active high polarity and
2139 * inverting all fan speed values.
2144 for (i = 0; i < 3; i++)
2145 pwm[i] = it87_read_value(data,
2149 * If any fan is in automatic pwm mode, the polarity
2150 * might be correct, as suspicious as it seems, so we
2151 * better don't change anything (but still disable the
2154 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2156 "Reconfiguring PWM to active high polarity\n");
2157 it87_write_value(data, IT87_REG_FAN_CTL,
2159 for (i = 0; i < 3; i++)
2160 it87_write_value(data,
2167 "PWM configuration is too broken to be fixed\n");
2171 "Detected broken BIOS defaults, disabling PWM interface\n");
2173 } else if (fix_pwm_polarity) {
2175 "PWM configuration looks sane, won't touch\n");
2181 /* Called when we have found a new IT87. */
2182 static void __devinit it87_init_device(struct platform_device *pdev)
2184 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2185 struct it87_data *data = platform_get_drvdata(pdev);
2190 * For each PWM channel:
2191 * - If it is in automatic mode, setting to manual mode should set
2192 * the fan to full speed by default.
2193 * - If it is in manual mode, we need a mapping to temperature
2194 * channels to use when later setting to automatic mode later.
2195 * Use a 1:1 mapping by default (we are clueless.)
2196 * In both cases, the value can (and should) be changed by the user
2197 * prior to switching to a different mode.
2198 * Note that this is no longer needed for the IT8721F and later, as
2199 * these have separate registers for the temperature mapping and the
2200 * manual duty cycle.
2202 for (i = 0; i < 3; i++) {
2203 data->pwm_temp_map[i] = i;
2204 data->pwm_duty[i] = 0x7f; /* Full speed */
2205 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2209 * Some chips seem to have default value 0xff for all limit
2210 * registers. For low voltage limits it makes no sense and triggers
2211 * alarms, so change to 0 instead. For high temperature limits, it
2212 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2213 * but is still confusing, so change to 127 degrees C.
2215 for (i = 0; i < 8; i++) {
2216 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2218 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2220 for (i = 0; i < 3; i++) {
2221 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2223 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2227 * Temperature channels are not forcibly enabled, as they can be
2228 * set to two different sensor types and we can't guess which one
2229 * is correct for a given system. These channels can be enabled at
2230 * run-time through the temp{1-3}_type sysfs accessors if needed.
2233 /* Check if voltage monitors are reset manually or by some reason */
2234 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2235 if ((tmp & 0xff) == 0) {
2236 /* Enable all voltage monitors */
2237 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2240 /* Check if tachometers are reset manually or by some reason */
2241 mask = 0x70 & ~(sio_data->skip_fan << 4);
2242 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2243 if ((data->fan_main_ctrl & mask) == 0) {
2244 /* Enable all fan tachometers */
2245 data->fan_main_ctrl |= mask;
2246 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2247 data->fan_main_ctrl);
2249 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2251 /* Set tachometers to 16-bit mode if needed */
2252 if (has_16bit_fans(data)) {
2253 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2254 if (~tmp & 0x07 & data->has_fan) {
2256 "Setting fan1-3 to 16-bit mode\n");
2257 it87_write_value(data, IT87_REG_FAN_16BIT,
2260 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2261 if (data->type != it87 && data->type != it8782 &&
2262 data->type != it8783) {
2264 data->has_fan |= (1 << 3); /* fan4 enabled */
2266 data->has_fan |= (1 << 4); /* fan5 enabled */
2270 /* Fan input pins may be used for alternative functions */
2271 data->has_fan &= ~sio_data->skip_fan;
2273 /* Start monitoring */
2274 it87_write_value(data, IT87_REG_CONFIG,
2275 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2276 | (update_vbat ? 0x41 : 0x01));
2279 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2281 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2282 if (has_newer_autopwm(data)) {
2283 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2284 data->pwm_duty[nr] = it87_read_value(data,
2285 IT87_REG_PWM_DUTY(nr));
2287 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2288 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2289 else /* Manual mode */
2290 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2293 if (has_old_autopwm(data)) {
2296 for (i = 0; i < 5 ; i++)
2297 data->auto_temp[nr][i] = it87_read_value(data,
2298 IT87_REG_AUTO_TEMP(nr, i));
2299 for (i = 0; i < 3 ; i++)
2300 data->auto_pwm[nr][i] = it87_read_value(data,
2301 IT87_REG_AUTO_PWM(nr, i));
2305 static struct it87_data *it87_update_device(struct device *dev)
2307 struct it87_data *data = dev_get_drvdata(dev);
2310 mutex_lock(&data->update_lock);
2312 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2316 * Cleared after each update, so reenable. Value
2317 * returned by this read will be previous value
2319 it87_write_value(data, IT87_REG_CONFIG,
2320 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2322 for (i = 0; i <= 7; i++) {
2324 it87_read_value(data, IT87_REG_VIN(i));
2326 it87_read_value(data, IT87_REG_VIN_MIN(i));
2328 it87_read_value(data, IT87_REG_VIN_MAX(i));
2330 /* in8 (battery) has no limit registers */
2331 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2333 for (i = 0; i < 5; i++) {
2334 /* Skip disabled fans */
2335 if (!(data->has_fan & (1 << i)))
2339 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2340 data->fan[i][0] = it87_read_value(data,
2342 /* Add high byte if in 16-bit mode */
2343 if (has_16bit_fans(data)) {
2344 data->fan[i][0] |= it87_read_value(data,
2345 IT87_REG_FANX[i]) << 8;
2346 data->fan[i][1] |= it87_read_value(data,
2347 IT87_REG_FANX_MIN[i]) << 8;
2350 for (i = 0; i < 3; i++) {
2351 if (!(data->has_temp & (1 << i)))
2354 it87_read_value(data, IT87_REG_TEMP(i));
2356 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2358 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2360 it87_read_value(data, IT87_REG_TEMP_OFFSET(i));
2363 /* Newer chips don't have clock dividers */
2364 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2365 i = it87_read_value(data, IT87_REG_FAN_DIV);
2366 data->fan_div[0] = i & 0x07;
2367 data->fan_div[1] = (i >> 3) & 0x07;
2368 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2372 it87_read_value(data, IT87_REG_ALARM1) |
2373 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2374 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2375 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2377 data->fan_main_ctrl = it87_read_value(data,
2378 IT87_REG_FAN_MAIN_CTRL);
2379 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2380 for (i = 0; i < 3; i++)
2381 it87_update_pwm_ctrl(data, i);
2383 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2385 * The IT8705F does not have VID capability.
2386 * The IT8718F and later don't use IT87_REG_VID for the
2389 if (data->type == it8712 || data->type == it8716) {
2390 data->vid = it87_read_value(data, IT87_REG_VID);
2392 * The older IT8712F revisions had only 5 VID pins,
2393 * but we assume it is always safe to read 6 bits.
2397 data->last_updated = jiffies;
2401 mutex_unlock(&data->update_lock);
2406 static int __init it87_device_add(unsigned short address,
2407 const struct it87_sio_data *sio_data)
2409 struct resource res = {
2410 .start = address + IT87_EC_OFFSET,
2411 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2413 .flags = IORESOURCE_IO,
2417 err = acpi_check_resource_conflict(&res);
2421 pdev = platform_device_alloc(DRVNAME, address);
2424 pr_err("Device allocation failed\n");
2428 err = platform_device_add_resources(pdev, &res, 1);
2430 pr_err("Device resource addition failed (%d)\n", err);
2431 goto exit_device_put;
2434 err = platform_device_add_data(pdev, sio_data,
2435 sizeof(struct it87_sio_data));
2437 pr_err("Platform data allocation failed\n");
2438 goto exit_device_put;
2441 err = platform_device_add(pdev);
2443 pr_err("Device addition failed (%d)\n", err);
2444 goto exit_device_put;
2450 platform_device_put(pdev);
2455 static int __init sm_it87_init(void)
2458 unsigned short isa_address = 0;
2459 struct it87_sio_data sio_data;
2461 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2462 err = it87_find(&isa_address, &sio_data);
2465 err = platform_driver_register(&it87_driver);
2469 err = it87_device_add(isa_address, &sio_data);
2471 platform_driver_unregister(&it87_driver);
2478 static void __exit sm_it87_exit(void)
2480 platform_device_unregister(pdev);
2481 platform_driver_unregister(&it87_driver);
2485 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <khali@linux-fr.org>");
2486 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2487 module_param(update_vbat, bool, 0);
2488 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2489 module_param(fix_pwm_polarity, bool, 0);
2490 MODULE_PARM_DESC(fix_pwm_polarity,
2491 "Force PWM polarity to active high (DANGEROUS)");
2492 MODULE_LICENSE("GPL");
2494 module_init(sm_it87_init);
2495 module_exit(sm_it87_exit);