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 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
210 #define IT87_REG_FAN_MAIN_CTRL 0x13
211 #define IT87_REG_FAN_CTL 0x14
212 #define IT87_REG_PWM(nr) (0x15 + (nr))
213 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
215 #define IT87_REG_VIN(nr) (0x20 + (nr))
216 #define IT87_REG_TEMP(nr) (0x29 + (nr))
218 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
219 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
220 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
221 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
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);
544 if (kstrtol(buf, 10, &val) < 0)
547 mutex_lock(&data->update_lock);
548 data->temp[nr][index] = TEMP_TO_REG(val);
550 reg = it87_read_value(data, IT87_REG_BEEP_ENABLE);
552 it87_write_value(data, IT87_REG_BEEP_ENABLE, reg);
554 it87_write_value(data,
555 index == 1 ? IT87_REG_TEMP_LOW(nr)
556 : index == 2 ? IT87_REG_TEMP_HIGH(nr)
557 : IT87_REG_TEMP_OFFSET[nr],
558 data->temp[nr][index]);
560 mutex_unlock(&data->update_lock);
564 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
565 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGOWU, show_temp, set_temp, 0, 1);
566 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGOWU, show_temp, set_temp, 0, 2);
567 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGOWU, show_temp, set_temp, 0, 3);
568 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
569 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGOWU, show_temp, set_temp, 1, 1);
570 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGOWU, show_temp, set_temp, 1, 2);
571 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGOWU, show_temp, set_temp, 1, 3);
572 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
573 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGOWU, show_temp, set_temp, 2, 1);
574 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGOWU, show_temp, set_temp, 2, 2);
575 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGOWU, show_temp, set_temp, 2, 3);
577 static ssize_t show_type(struct device *dev, struct device_attribute *attr,
580 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
581 int nr = sensor_attr->index;
582 struct it87_data *data = it87_update_device(dev);
583 u8 reg = data->sensor; /* In case value is updated while used */
586 return sprintf(buf, "3\n"); /* thermal diode */
588 return sprintf(buf, "4\n"); /* thermistor */
589 return sprintf(buf, "0\n"); /* disabled */
592 static ssize_t set_type(struct device *dev, struct device_attribute *attr,
593 const char *buf, size_t count)
595 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
596 int nr = sensor_attr->index;
598 struct it87_data *data = dev_get_drvdata(dev);
602 if (kstrtol(buf, 10, &val) < 0)
605 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
608 if (val == 2) { /* backwards compatibility */
610 "Sensor type 2 is deprecated, please use 4 instead\n");
613 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
621 mutex_lock(&data->update_lock);
623 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
624 data->valid = 0; /* Force cache refresh */
625 mutex_unlock(&data->update_lock);
629 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGOWU, show_type, set_type, 0);
630 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGOWU, show_type, set_type, 1);
631 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGOWU, show_type, set_type, 2);
635 static int pwm_mode(const struct it87_data *data, int nr)
637 int ctrl = data->fan_main_ctrl & (1 << nr);
639 if (ctrl == 0) /* Full speed */
641 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
643 else /* Manual mode */
647 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
650 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
652 int index = sattr->index;
654 struct it87_data *data = it87_update_device(dev);
656 speed = has_16bit_fans(data) ?
657 FAN16_FROM_REG(data->fan[nr][index]) :
658 FAN_FROM_REG(data->fan[nr][index],
659 DIV_FROM_REG(data->fan_div[nr]));
660 return sprintf(buf, "%d\n", speed);
663 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
666 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
667 int nr = sensor_attr->index;
669 struct it87_data *data = it87_update_device(dev);
670 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
672 static ssize_t show_pwm_enable(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
676 int nr = sensor_attr->index;
678 struct it87_data *data = it87_update_device(dev);
679 return sprintf(buf, "%d\n", pwm_mode(data, nr));
681 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
684 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
685 int nr = sensor_attr->index;
687 struct it87_data *data = it87_update_device(dev);
688 return sprintf(buf, "%d\n",
689 pwm_from_reg(data, data->pwm_duty[nr]));
691 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
694 struct it87_data *data = it87_update_device(dev);
695 int index = (data->fan_ctl >> 4) & 0x07;
697 return sprintf(buf, "%u\n", pwm_freq[index]);
700 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
701 const char *buf, size_t count)
703 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
705 int index = sattr->index;
707 struct it87_data *data = dev_get_drvdata(dev);
711 if (kstrtol(buf, 10, &val) < 0)
714 mutex_lock(&data->update_lock);
716 if (!has_16bit_fans(data)) {
717 reg = it87_read_value(data, IT87_REG_FAN_DIV);
720 data->fan_div[nr] = reg & 0x07;
723 data->fan_div[nr] = (reg >> 3) & 0x07;
726 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
729 data->fan[nr][index] =
730 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
731 it87_write_value(data, IT87_REG_FAN_MIN[nr],
732 data->fan[nr][index]);
734 data->fan[nr][index] = FAN16_TO_REG(val);
735 it87_write_value(data, IT87_REG_FAN_MIN[nr],
736 data->fan[nr][index] & 0xff);
737 it87_write_value(data, IT87_REG_FANX_MIN[nr],
738 data->fan[nr][index] >> 8);
741 mutex_unlock(&data->update_lock);
745 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
746 const char *buf, size_t count)
748 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
749 int nr = sensor_attr->index;
751 struct it87_data *data = dev_get_drvdata(dev);
756 if (kstrtoul(buf, 10, &val) < 0)
759 mutex_lock(&data->update_lock);
760 old = it87_read_value(data, IT87_REG_FAN_DIV);
762 /* Save fan min limit */
763 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
768 data->fan_div[nr] = DIV_TO_REG(val);
772 data->fan_div[nr] = 1;
774 data->fan_div[nr] = 3;
777 val |= (data->fan_div[0] & 0x07);
778 val |= (data->fan_div[1] & 0x07) << 3;
779 if (data->fan_div[2] == 3)
781 it87_write_value(data, IT87_REG_FAN_DIV, val);
783 /* Restore fan min limit */
784 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
785 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
787 mutex_unlock(&data->update_lock);
791 /* Returns 0 if OK, -EINVAL otherwise */
792 static int check_trip_points(struct device *dev, int nr)
794 const struct it87_data *data = dev_get_drvdata(dev);
797 if (has_old_autopwm(data)) {
798 for (i = 0; i < 3; i++) {
799 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
802 for (i = 0; i < 2; i++) {
803 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
810 "Inconsistent trip points, not switching to automatic mode\n");
811 dev_err(dev, "Adjust the trip points and try again\n");
816 static ssize_t set_pwm_enable(struct device *dev,
817 struct device_attribute *attr, const char *buf, size_t count)
819 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
820 int nr = sensor_attr->index;
822 struct it87_data *data = dev_get_drvdata(dev);
825 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
828 /* Check trip points before switching to automatic mode */
830 if (check_trip_points(dev, nr) < 0)
834 mutex_lock(&data->update_lock);
838 /* make sure the fan is on when in on/off mode */
839 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
840 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
841 /* set on/off mode */
842 data->fan_main_ctrl &= ~(1 << nr);
843 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
844 data->fan_main_ctrl);
846 if (val == 1) /* Manual mode */
847 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
848 data->pwm_temp_map[nr] :
850 else /* Automatic mode */
851 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
852 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
853 /* set SmartGuardian mode */
854 data->fan_main_ctrl |= (1 << nr);
855 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
856 data->fan_main_ctrl);
859 mutex_unlock(&data->update_lock);
862 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
863 const char *buf, size_t count)
865 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
866 int nr = sensor_attr->index;
868 struct it87_data *data = dev_get_drvdata(dev);
871 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
874 mutex_lock(&data->update_lock);
875 if (has_newer_autopwm(data)) {
877 * If we are in automatic mode, the PWM duty cycle register
878 * is read-only so we can't write the value.
880 if (data->pwm_ctrl[nr] & 0x80) {
881 mutex_unlock(&data->update_lock);
884 data->pwm_duty[nr] = pwm_to_reg(data, val);
885 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
888 data->pwm_duty[nr] = pwm_to_reg(data, val);
890 * If we are in manual mode, write the duty cycle immediately;
891 * otherwise, just store it for later use.
893 if (!(data->pwm_ctrl[nr] & 0x80)) {
894 data->pwm_ctrl[nr] = data->pwm_duty[nr];
895 it87_write_value(data, IT87_REG_PWM(nr),
899 mutex_unlock(&data->update_lock);
902 static ssize_t set_pwm_freq(struct device *dev,
903 struct device_attribute *attr, const char *buf, size_t count)
905 struct it87_data *data = dev_get_drvdata(dev);
909 if (kstrtoul(buf, 10, &val) < 0)
912 /* Search for the nearest available frequency */
913 for (i = 0; i < 7; i++) {
914 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
918 mutex_lock(&data->update_lock);
919 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
920 data->fan_ctl |= i << 4;
921 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
922 mutex_unlock(&data->update_lock);
926 static ssize_t show_pwm_temp_map(struct device *dev,
927 struct device_attribute *attr, char *buf)
929 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
930 int nr = sensor_attr->index;
932 struct it87_data *data = it87_update_device(dev);
935 if (data->pwm_temp_map[nr] < 3)
936 map = 1 << data->pwm_temp_map[nr];
938 map = 0; /* Should never happen */
939 return sprintf(buf, "%d\n", map);
941 static ssize_t set_pwm_temp_map(struct device *dev,
942 struct device_attribute *attr, const char *buf, size_t count)
944 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
945 int nr = sensor_attr->index;
947 struct it87_data *data = dev_get_drvdata(dev);
952 * This check can go away if we ever support automatic fan speed
953 * control on newer chips.
955 if (!has_old_autopwm(data)) {
956 dev_notice(dev, "Mapping change disabled for safety reasons\n");
960 if (kstrtol(buf, 10, &val) < 0)
977 mutex_lock(&data->update_lock);
978 data->pwm_temp_map[nr] = reg;
980 * If we are in automatic mode, write the temp mapping immediately;
981 * otherwise, just store it for later use.
983 if (data->pwm_ctrl[nr] & 0x80) {
984 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
985 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
987 mutex_unlock(&data->update_lock);
991 static ssize_t show_auto_pwm(struct device *dev,
992 struct device_attribute *attr, char *buf)
994 struct it87_data *data = it87_update_device(dev);
995 struct sensor_device_attribute_2 *sensor_attr =
996 to_sensor_dev_attr_2(attr);
997 int nr = sensor_attr->nr;
998 int point = sensor_attr->index;
1000 return sprintf(buf, "%d\n",
1001 pwm_from_reg(data, data->auto_pwm[nr][point]));
1004 static ssize_t set_auto_pwm(struct device *dev,
1005 struct device_attribute *attr, const char *buf, size_t count)
1007 struct it87_data *data = dev_get_drvdata(dev);
1008 struct sensor_device_attribute_2 *sensor_attr =
1009 to_sensor_dev_attr_2(attr);
1010 int nr = sensor_attr->nr;
1011 int point = sensor_attr->index;
1014 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1017 mutex_lock(&data->update_lock);
1018 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1019 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1020 data->auto_pwm[nr][point]);
1021 mutex_unlock(&data->update_lock);
1025 static ssize_t show_auto_temp(struct device *dev,
1026 struct device_attribute *attr, char *buf)
1028 struct it87_data *data = it87_update_device(dev);
1029 struct sensor_device_attribute_2 *sensor_attr =
1030 to_sensor_dev_attr_2(attr);
1031 int nr = sensor_attr->nr;
1032 int point = sensor_attr->index;
1034 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1037 static ssize_t set_auto_temp(struct device *dev,
1038 struct device_attribute *attr, const char *buf, size_t count)
1040 struct it87_data *data = dev_get_drvdata(dev);
1041 struct sensor_device_attribute_2 *sensor_attr =
1042 to_sensor_dev_attr_2(attr);
1043 int nr = sensor_attr->nr;
1044 int point = sensor_attr->index;
1047 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1050 mutex_lock(&data->update_lock);
1051 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1052 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1053 data->auto_temp[nr][point]);
1054 mutex_unlock(&data->update_lock);
1058 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1059 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGOWU, show_fan, set_fan, 0, 1);
1060 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGOWU, show_fan_div, set_fan_div, 0);
1062 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1063 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGOWU, show_fan, set_fan, 1, 1);
1064 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGOWU, show_fan_div, set_fan_div, 1);
1066 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1067 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGOWU, show_fan, set_fan, 2, 1);
1068 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGOWU, show_fan_div, set_fan_div, 2);
1070 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1071 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGOWU, show_fan, set_fan, 3, 1);
1073 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1074 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGOWU, show_fan, set_fan, 4, 1);
1076 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGOWU,
1077 show_pwm_enable, set_pwm_enable, 0);
1078 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGOWU, show_pwm, set_pwm, 0);
1079 static DEVICE_ATTR(pwm1_freq, S_IRUGOWU, show_pwm_freq, set_pwm_freq);
1080 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGOWU,
1081 show_pwm_temp_map, set_pwm_temp_map, 0);
1082 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGOWU,
1083 show_auto_pwm, set_auto_pwm, 0, 0);
1084 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGOWU,
1085 show_auto_pwm, set_auto_pwm, 0, 1);
1086 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGOWU,
1087 show_auto_pwm, set_auto_pwm, 0, 2);
1088 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1089 show_auto_pwm, NULL, 0, 3);
1090 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGOWU,
1091 show_auto_temp, set_auto_temp, 0, 1);
1092 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGOWU,
1093 show_auto_temp, set_auto_temp, 0, 0);
1094 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGOWU,
1095 show_auto_temp, set_auto_temp, 0, 2);
1096 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGOWU,
1097 show_auto_temp, set_auto_temp, 0, 3);
1098 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGOWU,
1099 show_auto_temp, set_auto_temp, 0, 4);
1101 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGOWU,
1102 show_pwm_enable, set_pwm_enable, 1);
1103 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGOWU, show_pwm, set_pwm, 1);
1104 static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1105 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGOWU,
1106 show_pwm_temp_map, set_pwm_temp_map, 1);
1107 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGOWU,
1108 show_auto_pwm, set_auto_pwm, 1, 0);
1109 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGOWU,
1110 show_auto_pwm, set_auto_pwm, 1, 1);
1111 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGOWU,
1112 show_auto_pwm, set_auto_pwm, 1, 2);
1113 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1114 show_auto_pwm, NULL, 1, 3);
1115 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGOWU,
1116 show_auto_temp, set_auto_temp, 1, 1);
1117 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGOWU,
1118 show_auto_temp, set_auto_temp, 1, 0);
1119 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGOWU,
1120 show_auto_temp, set_auto_temp, 1, 2);
1121 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGOWU,
1122 show_auto_temp, set_auto_temp, 1, 3);
1123 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGOWU,
1124 show_auto_temp, set_auto_temp, 1, 4);
1126 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGOWU,
1127 show_pwm_enable, set_pwm_enable, 2);
1128 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGOWU, show_pwm, set_pwm, 2);
1129 static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1130 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGOWU,
1131 show_pwm_temp_map, set_pwm_temp_map, 2);
1132 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGOWU,
1133 show_auto_pwm, set_auto_pwm, 2, 0);
1134 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGOWU,
1135 show_auto_pwm, set_auto_pwm, 2, 1);
1136 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGOWU,
1137 show_auto_pwm, set_auto_pwm, 2, 2);
1138 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1139 show_auto_pwm, NULL, 2, 3);
1140 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGOWU,
1141 show_auto_temp, set_auto_temp, 2, 1);
1142 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGOWU,
1143 show_auto_temp, set_auto_temp, 2, 0);
1144 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGOWU,
1145 show_auto_temp, set_auto_temp, 2, 2);
1146 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGOWU,
1147 show_auto_temp, set_auto_temp, 2, 3);
1148 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGOWU,
1149 show_auto_temp, set_auto_temp, 2, 4);
1152 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1155 struct it87_data *data = it87_update_device(dev);
1156 return sprintf(buf, "%u\n", data->alarms);
1158 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1160 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1163 int bitnr = to_sensor_dev_attr(attr)->index;
1164 struct it87_data *data = it87_update_device(dev);
1165 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1168 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1169 *attr, const char *buf, size_t count)
1171 struct it87_data *data = dev_get_drvdata(dev);
1175 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1178 mutex_lock(&data->update_lock);
1179 config = it87_read_value(data, IT87_REG_CONFIG);
1184 it87_write_value(data, IT87_REG_CONFIG, config);
1185 /* Invalidate cache to force re-read */
1188 mutex_unlock(&data->update_lock);
1193 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1194 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1195 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1196 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1197 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1198 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1199 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1200 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1201 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1202 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1203 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1204 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1205 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1206 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1207 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1208 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1209 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1210 show_alarm, clear_intrusion, 4);
1212 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1215 int bitnr = to_sensor_dev_attr(attr)->index;
1216 struct it87_data *data = it87_update_device(dev);
1217 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1219 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1220 const char *buf, size_t count)
1222 int bitnr = to_sensor_dev_attr(attr)->index;
1223 struct it87_data *data = dev_get_drvdata(dev);
1226 if (kstrtol(buf, 10, &val) < 0
1227 || (val != 0 && val != 1))
1230 mutex_lock(&data->update_lock);
1231 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1233 data->beeps |= (1 << bitnr);
1235 data->beeps &= ~(1 << bitnr);
1236 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1237 mutex_unlock(&data->update_lock);
1241 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1242 show_beep, set_beep, 1);
1243 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1244 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1245 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1246 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1247 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1248 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1249 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1250 /* fanX_beep writability is set later */
1251 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1252 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1253 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1254 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1255 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1256 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1257 show_beep, set_beep, 2);
1258 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1259 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1261 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1264 struct it87_data *data = dev_get_drvdata(dev);
1265 return sprintf(buf, "%u\n", data->vrm);
1267 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1268 const char *buf, size_t count)
1270 struct it87_data *data = dev_get_drvdata(dev);
1273 if (kstrtoul(buf, 10, &val) < 0)
1280 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1282 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1285 struct it87_data *data = it87_update_device(dev);
1286 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1288 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1290 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1293 static const char * const labels[] = {
1298 static const char * const labels_it8721[] = {
1303 struct it87_data *data = dev_get_drvdata(dev);
1304 int nr = to_sensor_dev_attr(attr)->index;
1306 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1309 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1310 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1311 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1313 static ssize_t show_name(struct device *dev, struct device_attribute
1314 *devattr, char *buf)
1316 struct it87_data *data = dev_get_drvdata(dev);
1317 return sprintf(buf, "%s\n", data->name);
1319 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1321 static struct attribute *it87_attributes_in[9][5] = {
1323 &sensor_dev_attr_in0_input.dev_attr.attr,
1324 &sensor_dev_attr_in0_min.dev_attr.attr,
1325 &sensor_dev_attr_in0_max.dev_attr.attr,
1326 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1329 &sensor_dev_attr_in1_input.dev_attr.attr,
1330 &sensor_dev_attr_in1_min.dev_attr.attr,
1331 &sensor_dev_attr_in1_max.dev_attr.attr,
1332 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1335 &sensor_dev_attr_in2_input.dev_attr.attr,
1336 &sensor_dev_attr_in2_min.dev_attr.attr,
1337 &sensor_dev_attr_in2_max.dev_attr.attr,
1338 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1341 &sensor_dev_attr_in3_input.dev_attr.attr,
1342 &sensor_dev_attr_in3_min.dev_attr.attr,
1343 &sensor_dev_attr_in3_max.dev_attr.attr,
1344 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1347 &sensor_dev_attr_in4_input.dev_attr.attr,
1348 &sensor_dev_attr_in4_min.dev_attr.attr,
1349 &sensor_dev_attr_in4_max.dev_attr.attr,
1350 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1353 &sensor_dev_attr_in5_input.dev_attr.attr,
1354 &sensor_dev_attr_in5_min.dev_attr.attr,
1355 &sensor_dev_attr_in5_max.dev_attr.attr,
1356 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1359 &sensor_dev_attr_in6_input.dev_attr.attr,
1360 &sensor_dev_attr_in6_min.dev_attr.attr,
1361 &sensor_dev_attr_in6_max.dev_attr.attr,
1362 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1365 &sensor_dev_attr_in7_input.dev_attr.attr,
1366 &sensor_dev_attr_in7_min.dev_attr.attr,
1367 &sensor_dev_attr_in7_max.dev_attr.attr,
1368 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1371 &sensor_dev_attr_in8_input.dev_attr.attr,
1375 static const struct attribute_group it87_group_in[9] = {
1376 { .attrs = it87_attributes_in[0] },
1377 { .attrs = it87_attributes_in[1] },
1378 { .attrs = it87_attributes_in[2] },
1379 { .attrs = it87_attributes_in[3] },
1380 { .attrs = it87_attributes_in[4] },
1381 { .attrs = it87_attributes_in[5] },
1382 { .attrs = it87_attributes_in[6] },
1383 { .attrs = it87_attributes_in[7] },
1384 { .attrs = it87_attributes_in[8] },
1387 static struct attribute *it87_attributes_temp[3][7] = {
1389 &sensor_dev_attr_temp1_input.dev_attr.attr,
1390 &sensor_dev_attr_temp1_max.dev_attr.attr,
1391 &sensor_dev_attr_temp1_min.dev_attr.attr,
1392 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1393 &sensor_dev_attr_temp1_type.dev_attr.attr,
1394 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1397 &sensor_dev_attr_temp2_input.dev_attr.attr,
1398 &sensor_dev_attr_temp2_max.dev_attr.attr,
1399 &sensor_dev_attr_temp2_min.dev_attr.attr,
1400 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1401 &sensor_dev_attr_temp2_type.dev_attr.attr,
1402 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1405 &sensor_dev_attr_temp3_input.dev_attr.attr,
1406 &sensor_dev_attr_temp3_max.dev_attr.attr,
1407 &sensor_dev_attr_temp3_min.dev_attr.attr,
1408 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1409 &sensor_dev_attr_temp3_type.dev_attr.attr,
1410 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1414 static const struct attribute_group it87_group_temp[3] = {
1415 { .attrs = it87_attributes_temp[0] },
1416 { .attrs = it87_attributes_temp[1] },
1417 { .attrs = it87_attributes_temp[2] },
1420 static struct attribute *it87_attributes[] = {
1421 &dev_attr_alarms.attr,
1422 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1423 &dev_attr_name.attr,
1427 static const struct attribute_group it87_group = {
1428 .attrs = it87_attributes,
1431 static struct attribute *it87_attributes_in_beep[] = {
1432 &sensor_dev_attr_in0_beep.dev_attr.attr,
1433 &sensor_dev_attr_in1_beep.dev_attr.attr,
1434 &sensor_dev_attr_in2_beep.dev_attr.attr,
1435 &sensor_dev_attr_in3_beep.dev_attr.attr,
1436 &sensor_dev_attr_in4_beep.dev_attr.attr,
1437 &sensor_dev_attr_in5_beep.dev_attr.attr,
1438 &sensor_dev_attr_in6_beep.dev_attr.attr,
1439 &sensor_dev_attr_in7_beep.dev_attr.attr,
1443 static struct attribute *it87_attributes_temp_beep[] = {
1444 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1445 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1446 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1449 static struct attribute *it87_attributes_fan[5][3+1] = { {
1450 &sensor_dev_attr_fan1_input.dev_attr.attr,
1451 &sensor_dev_attr_fan1_min.dev_attr.attr,
1452 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1455 &sensor_dev_attr_fan2_input.dev_attr.attr,
1456 &sensor_dev_attr_fan2_min.dev_attr.attr,
1457 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1460 &sensor_dev_attr_fan3_input.dev_attr.attr,
1461 &sensor_dev_attr_fan3_min.dev_attr.attr,
1462 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1465 &sensor_dev_attr_fan4_input.dev_attr.attr,
1466 &sensor_dev_attr_fan4_min.dev_attr.attr,
1467 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1470 &sensor_dev_attr_fan5_input.dev_attr.attr,
1471 &sensor_dev_attr_fan5_min.dev_attr.attr,
1472 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1476 static const struct attribute_group it87_group_fan[5] = {
1477 { .attrs = it87_attributes_fan[0] },
1478 { .attrs = it87_attributes_fan[1] },
1479 { .attrs = it87_attributes_fan[2] },
1480 { .attrs = it87_attributes_fan[3] },
1481 { .attrs = it87_attributes_fan[4] },
1484 static const struct attribute *it87_attributes_fan_div[] = {
1485 &sensor_dev_attr_fan1_div.dev_attr.attr,
1486 &sensor_dev_attr_fan2_div.dev_attr.attr,
1487 &sensor_dev_attr_fan3_div.dev_attr.attr,
1490 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1491 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1492 &sensor_dev_attr_pwm1.dev_attr.attr,
1493 &dev_attr_pwm1_freq.attr,
1494 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1497 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1498 &sensor_dev_attr_pwm2.dev_attr.attr,
1499 &dev_attr_pwm2_freq.attr,
1500 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1503 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1504 &sensor_dev_attr_pwm3.dev_attr.attr,
1505 &dev_attr_pwm3_freq.attr,
1506 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1510 static const struct attribute_group it87_group_pwm[3] = {
1511 { .attrs = it87_attributes_pwm[0] },
1512 { .attrs = it87_attributes_pwm[1] },
1513 { .attrs = it87_attributes_pwm[2] },
1516 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1517 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1518 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1519 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1520 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1521 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1522 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1523 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1524 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1525 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1528 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1529 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1530 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1531 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1532 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1533 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1534 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1535 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1536 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1539 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1540 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1541 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1542 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1543 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1544 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1545 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1546 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1547 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1551 static const struct attribute_group it87_group_autopwm[3] = {
1552 { .attrs = it87_attributes_autopwm[0] },
1553 { .attrs = it87_attributes_autopwm[1] },
1554 { .attrs = it87_attributes_autopwm[2] },
1557 static struct attribute *it87_attributes_fan_beep[] = {
1558 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1559 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1560 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1561 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1562 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1565 static struct attribute *it87_attributes_vid[] = {
1567 &dev_attr_cpu0_vid.attr,
1571 static const struct attribute_group it87_group_vid = {
1572 .attrs = it87_attributes_vid,
1575 static struct attribute *it87_attributes_label[] = {
1576 &sensor_dev_attr_in3_label.dev_attr.attr,
1577 &sensor_dev_attr_in7_label.dev_attr.attr,
1578 &sensor_dev_attr_in8_label.dev_attr.attr,
1582 static const struct attribute_group it87_group_label = {
1583 .attrs = it87_attributes_label,
1586 /* SuperIO detection - will change isa_address if a chip is found */
1587 static int __init it87_find(unsigned short *address,
1588 struct it87_sio_data *sio_data)
1592 const char *board_vendor, *board_name;
1594 err = superio_enter();
1599 chip_type = force_id ? force_id : superio_inw(DEVID);
1601 switch (chip_type) {
1603 sio_data->type = it87;
1606 sio_data->type = it8712;
1610 sio_data->type = it8716;
1613 sio_data->type = it8718;
1616 sio_data->type = it8720;
1619 sio_data->type = it8721;
1622 sio_data->type = it8728;
1625 sio_data->type = it8782;
1628 sio_data->type = it8783;
1630 case 0xffff: /* No device at all */
1633 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1637 superio_select(PME);
1638 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1639 pr_info("Device not activated, skipping\n");
1643 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1644 if (*address == 0) {
1645 pr_info("Base address not set, skipping\n");
1650 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1651 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1652 chip_type, *address, sio_data->revision);
1654 /* in8 (Vbat) is always internal */
1655 sio_data->internal = (1 << 2);
1657 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1658 if (sio_data->type == it87) {
1659 /* The IT8705F doesn't have VID pins at all */
1660 sio_data->skip_vid = 1;
1662 /* The IT8705F has a different LD number for GPIO */
1664 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1665 } else if (sio_data->type == it8783) {
1666 int reg25, reg27, reg2A, reg2C, regEF;
1668 sio_data->skip_vid = 1; /* No VID */
1670 superio_select(GPIO);
1672 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1673 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1674 reg2A = superio_inb(IT87_SIO_PINX1_REG);
1675 reg2C = superio_inb(IT87_SIO_PINX2_REG);
1676 regEF = superio_inb(IT87_SIO_SPI_REG);
1678 /* Check if fan3 is there or not */
1679 if ((reg27 & (1 << 0)) || !(reg2C & (1 << 2)))
1680 sio_data->skip_fan |= (1 << 2);
1681 if ((reg25 & (1 << 4))
1682 || (!(reg2A & (1 << 1)) && (regEF & (1 << 0))))
1683 sio_data->skip_pwm |= (1 << 2);
1685 /* Check if fan2 is there or not */
1686 if (reg27 & (1 << 7))
1687 sio_data->skip_fan |= (1 << 1);
1688 if (reg27 & (1 << 3))
1689 sio_data->skip_pwm |= (1 << 1);
1692 if ((reg27 & (1 << 0)) || (reg2C & (1 << 2)))
1693 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1696 if (reg27 & (1 << 1))
1697 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1701 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1703 if (reg27 & (1 << 2)) {
1705 * The data sheet is a bit unclear regarding the
1706 * internal voltage divider for VCCH5V. It says
1707 * "This bit enables and switches VIN7 (pin 91) to the
1708 * internal voltage divider for VCCH5V".
1709 * This is different to other chips, where the internal
1710 * voltage divider would connect VIN7 to an internal
1711 * voltage source. Maybe that is the case here as well.
1713 * Since we don't know for sure, re-route it if that is
1714 * not the case, and ask the user to report if the
1715 * resulting voltage is sane.
1717 if (!(reg2C & (1 << 1))) {
1719 superio_outb(IT87_SIO_PINX2_REG, reg2C);
1720 pr_notice("Routing internal VCCH5V to in7.\n");
1722 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1723 pr_notice("Please report if it displays a reasonable voltage.\n");
1726 if (reg2C & (1 << 0))
1727 sio_data->internal |= (1 << 0);
1728 if (reg2C & (1 << 1))
1729 sio_data->internal |= (1 << 1);
1731 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1737 superio_select(GPIO);
1739 reg = superio_inb(IT87_SIO_GPIO3_REG);
1740 if (sio_data->type == it8721 || sio_data->type == it8728 ||
1741 sio_data->type == it8782) {
1743 * IT8721F/IT8758E, and IT8782F don't have VID pins
1744 * at all, not sure about the IT8728F.
1746 sio_data->skip_vid = 1;
1748 /* We need at least 4 VID pins */
1750 pr_info("VID is disabled (pins used for GPIO)\n");
1751 sio_data->skip_vid = 1;
1755 /* Check if fan3 is there or not */
1757 sio_data->skip_pwm |= (1 << 2);
1759 sio_data->skip_fan |= (1 << 2);
1761 /* Check if fan2 is there or not */
1762 reg = superio_inb(IT87_SIO_GPIO5_REG);
1764 sio_data->skip_pwm |= (1 << 1);
1766 sio_data->skip_fan |= (1 << 1);
1768 if ((sio_data->type == it8718 || sio_data->type == it8720)
1769 && !(sio_data->skip_vid))
1770 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1772 reg = superio_inb(IT87_SIO_PINX2_REG);
1774 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1777 * The IT8720F has no VIN7 pin, so VCCH should always be
1778 * routed internally to VIN7 with an internal divider.
1779 * Curiously, there still is a configuration bit to control
1780 * this, which means it can be set incorrectly. And even
1781 * more curiously, many boards out there are improperly
1782 * configured, even though the IT8720F datasheet claims
1783 * that the internal routing of VCCH to VIN7 is the default
1784 * setting. So we force the internal routing in this case.
1786 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1787 * If UART6 is enabled, re-route VIN7 to the internal divider
1788 * if that is not already the case.
1790 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
1792 superio_outb(IT87_SIO_PINX2_REG, reg);
1793 pr_notice("Routing internal VCCH to in7\n");
1796 sio_data->internal |= (1 << 0);
1797 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1798 sio_data->type == it8728)
1799 sio_data->internal |= (1 << 1);
1802 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1803 * While VIN7 can be routed to the internal voltage divider,
1804 * VIN5 and VIN6 are not available if UART6 is enabled.
1806 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
1807 * is the temperature source. Since we can not read the
1808 * temperature source here, skip_temp is preliminary.
1811 sio_data->skip_in |= (1 << 5) | (1 << 6);
1812 sio_data->skip_temp |= (1 << 2);
1815 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1817 if (sio_data->beep_pin)
1818 pr_info("Beeping is supported\n");
1820 /* Disable specific features based on DMI strings */
1821 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1822 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1823 if (board_vendor && board_name) {
1824 if (strcmp(board_vendor, "nVIDIA") == 0
1825 && strcmp(board_name, "FN68PT") == 0) {
1827 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1828 * connected to a fan, but to something else. One user
1829 * has reported instant system power-off when changing
1830 * the PWM2 duty cycle, so we disable it.
1831 * I use the board name string as the trigger in case
1832 * the same board is ever used in other systems.
1834 pr_info("Disabling pwm2 due to hardware constraints\n");
1835 sio_data->skip_pwm = (1 << 1);
1844 static void it87_remove_files(struct device *dev)
1846 struct it87_data *data = platform_get_drvdata(pdev);
1847 struct it87_sio_data *sio_data = dev->platform_data;
1850 sysfs_remove_group(&dev->kobj, &it87_group);
1851 for (i = 0; i < 9; i++) {
1852 if (sio_data->skip_in & (1 << i))
1854 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
1855 if (it87_attributes_in_beep[i])
1856 sysfs_remove_file(&dev->kobj,
1857 it87_attributes_in_beep[i]);
1859 for (i = 0; i < 3; i++) {
1860 if (!(data->has_temp & (1 << i)))
1862 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
1863 if (sio_data->beep_pin)
1864 sysfs_remove_file(&dev->kobj,
1865 it87_attributes_temp_beep[i]);
1867 for (i = 0; i < 5; i++) {
1868 if (!(data->has_fan & (1 << i)))
1870 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
1871 if (sio_data->beep_pin)
1872 sysfs_remove_file(&dev->kobj,
1873 it87_attributes_fan_beep[i]);
1874 if (i < 3 && !has_16bit_fans(data))
1875 sysfs_remove_file(&dev->kobj,
1876 it87_attributes_fan_div[i]);
1878 for (i = 0; i < 3; i++) {
1879 if (sio_data->skip_pwm & (1 << 0))
1881 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1882 if (has_old_autopwm(data))
1883 sysfs_remove_group(&dev->kobj,
1884 &it87_group_autopwm[i]);
1886 if (!sio_data->skip_vid)
1887 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1888 sysfs_remove_group(&dev->kobj, &it87_group_label);
1891 static int __devinit it87_probe(struct platform_device *pdev)
1893 struct it87_data *data;
1894 struct resource *res;
1895 struct device *dev = &pdev->dev;
1896 struct it87_sio_data *sio_data = dev->platform_data;
1898 int enable_pwm_interface;
1899 int fan_beep_need_rw;
1901 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1902 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
1904 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1905 (unsigned long)res->start,
1906 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1910 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
1914 data->addr = res->start;
1915 data->type = sio_data->type;
1916 data->features = it87_devices[data->type].features;
1917 data->name = it87_devices[sio_data->type].name;
1919 * IT8705F Datasheet 0.4.1, 3h == Version G.
1920 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
1921 * These are the first revisions with 16-bit tachometer support.
1923 switch (data->type) {
1925 if (sio_data->revision >= 0x03) {
1926 data->features &= ~FEAT_OLD_AUTOPWM;
1927 data->features |= FEAT_16BIT_FAN;
1931 if (sio_data->revision >= 0x08) {
1932 data->features &= ~FEAT_OLD_AUTOPWM;
1933 data->features |= FEAT_16BIT_FAN;
1940 /* Now, we do the remaining detection. */
1941 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1942 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
1945 platform_set_drvdata(pdev, data);
1947 mutex_init(&data->update_lock);
1949 /* Check PWM configuration */
1950 enable_pwm_interface = it87_check_pwm(dev);
1952 /* Starting with IT8721F, we handle scaling of internal voltages */
1953 if (has_12mv_adc(data)) {
1954 if (sio_data->internal & (1 << 0))
1955 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1956 if (sio_data->internal & (1 << 1))
1957 data->in_scaled |= (1 << 7); /* in7 is VSB */
1958 if (sio_data->internal & (1 << 2))
1959 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1960 } else if (sio_data->type == it8782 || sio_data->type == it8783) {
1961 if (sio_data->internal & (1 << 0))
1962 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
1963 if (sio_data->internal & (1 << 1))
1964 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
1967 data->has_temp = 0x07;
1968 if (sio_data->skip_temp & (1 << 2)) {
1969 if (sio_data->type == it8782
1970 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
1971 data->has_temp &= ~(1 << 2);
1974 /* Initialize the IT87 chip */
1975 it87_init_device(pdev);
1977 /* Register sysfs hooks */
1978 err = sysfs_create_group(&dev->kobj, &it87_group);
1982 for (i = 0; i < 9; i++) {
1983 if (sio_data->skip_in & (1 << i))
1985 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
1988 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
1989 err = sysfs_create_file(&dev->kobj,
1990 it87_attributes_in_beep[i]);
1996 for (i = 0; i < 3; i++) {
1997 if (!(data->has_temp & (1 << i)))
1999 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2002 if (sio_data->beep_pin) {
2003 err = sysfs_create_file(&dev->kobj,
2004 it87_attributes_temp_beep[i]);
2010 /* Do not create fan files for disabled fans */
2011 fan_beep_need_rw = 1;
2012 for (i = 0; i < 5; i++) {
2013 if (!(data->has_fan & (1 << i)))
2015 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2019 if (i < 3 && !has_16bit_fans(data)) {
2020 err = sysfs_create_file(&dev->kobj,
2021 it87_attributes_fan_div[i]);
2026 if (sio_data->beep_pin) {
2027 err = sysfs_create_file(&dev->kobj,
2028 it87_attributes_fan_beep[i]);
2031 if (!fan_beep_need_rw)
2035 * As we have a single beep enable bit for all fans,
2036 * only the first enabled fan has a writable attribute
2039 if (sysfs_chmod_file(&dev->kobj,
2040 it87_attributes_fan_beep[i],
2042 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2044 fan_beep_need_rw = 0;
2048 if (enable_pwm_interface) {
2049 for (i = 0; i < 3; i++) {
2050 if (sio_data->skip_pwm & (1 << i))
2052 err = sysfs_create_group(&dev->kobj,
2053 &it87_group_pwm[i]);
2057 if (!has_old_autopwm(data))
2059 err = sysfs_create_group(&dev->kobj,
2060 &it87_group_autopwm[i]);
2066 if (!sio_data->skip_vid) {
2067 data->vrm = vid_which_vrm();
2068 /* VID reading from Super-I/O config space if available */
2069 data->vid = sio_data->vid_value;
2070 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2075 /* Export labels for internal sensors */
2076 for (i = 0; i < 3; i++) {
2077 if (!(sio_data->internal & (1 << i)))
2079 err = sysfs_create_file(&dev->kobj,
2080 it87_attributes_label[i]);
2085 data->hwmon_dev = hwmon_device_register(dev);
2086 if (IS_ERR(data->hwmon_dev)) {
2087 err = PTR_ERR(data->hwmon_dev);
2094 it87_remove_files(dev);
2098 static int __devexit it87_remove(struct platform_device *pdev)
2100 struct it87_data *data = platform_get_drvdata(pdev);
2102 hwmon_device_unregister(data->hwmon_dev);
2103 it87_remove_files(&pdev->dev);
2109 * Must be called with data->update_lock held, except during initialization.
2110 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2111 * would slow down the IT87 access and should not be necessary.
2113 static int it87_read_value(struct it87_data *data, u8 reg)
2115 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2116 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2120 * Must be called with data->update_lock held, except during initialization.
2121 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2122 * would slow down the IT87 access and should not be necessary.
2124 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2126 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2127 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2130 /* Return 1 if and only if the PWM interface is safe to use */
2131 static int __devinit it87_check_pwm(struct device *dev)
2133 struct it87_data *data = dev_get_drvdata(dev);
2135 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2136 * and polarity set to active low is sign that this is the case so we
2137 * disable pwm control to protect the user.
2139 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2140 if ((tmp & 0x87) == 0) {
2141 if (fix_pwm_polarity) {
2143 * The user asks us to attempt a chip reconfiguration.
2144 * This means switching to active high polarity and
2145 * inverting all fan speed values.
2150 for (i = 0; i < 3; i++)
2151 pwm[i] = it87_read_value(data,
2155 * If any fan is in automatic pwm mode, the polarity
2156 * might be correct, as suspicious as it seems, so we
2157 * better don't change anything (but still disable the
2160 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2162 "Reconfiguring PWM to active high polarity\n");
2163 it87_write_value(data, IT87_REG_FAN_CTL,
2165 for (i = 0; i < 3; i++)
2166 it87_write_value(data,
2173 "PWM configuration is too broken to be fixed\n");
2177 "Detected broken BIOS defaults, disabling PWM interface\n");
2179 } else if (fix_pwm_polarity) {
2181 "PWM configuration looks sane, won't touch\n");
2187 /* Called when we have found a new IT87. */
2188 static void __devinit it87_init_device(struct platform_device *pdev)
2190 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2191 struct it87_data *data = platform_get_drvdata(pdev);
2196 * For each PWM channel:
2197 * - If it is in automatic mode, setting to manual mode should set
2198 * the fan to full speed by default.
2199 * - If it is in manual mode, we need a mapping to temperature
2200 * channels to use when later setting to automatic mode later.
2201 * Use a 1:1 mapping by default (we are clueless.)
2202 * In both cases, the value can (and should) be changed by the user
2203 * prior to switching to a different mode.
2204 * Note that this is no longer needed for the IT8721F and later, as
2205 * these have separate registers for the temperature mapping and the
2206 * manual duty cycle.
2208 for (i = 0; i < 3; i++) {
2209 data->pwm_temp_map[i] = i;
2210 data->pwm_duty[i] = 0x7f; /* Full speed */
2211 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2215 * Some chips seem to have default value 0xff for all limit
2216 * registers. For low voltage limits it makes no sense and triggers
2217 * alarms, so change to 0 instead. For high temperature limits, it
2218 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2219 * but is still confusing, so change to 127 degrees C.
2221 for (i = 0; i < 8; i++) {
2222 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2224 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2226 for (i = 0; i < 3; i++) {
2227 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2229 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2233 * Temperature channels are not forcibly enabled, as they can be
2234 * set to two different sensor types and we can't guess which one
2235 * is correct for a given system. These channels can be enabled at
2236 * run-time through the temp{1-3}_type sysfs accessors if needed.
2239 /* Check if voltage monitors are reset manually or by some reason */
2240 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2241 if ((tmp & 0xff) == 0) {
2242 /* Enable all voltage monitors */
2243 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2246 /* Check if tachometers are reset manually or by some reason */
2247 mask = 0x70 & ~(sio_data->skip_fan << 4);
2248 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2249 if ((data->fan_main_ctrl & mask) == 0) {
2250 /* Enable all fan tachometers */
2251 data->fan_main_ctrl |= mask;
2252 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2253 data->fan_main_ctrl);
2255 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2257 /* Set tachometers to 16-bit mode if needed */
2258 if (has_16bit_fans(data)) {
2259 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2260 if (~tmp & 0x07 & data->has_fan) {
2262 "Setting fan1-3 to 16-bit mode\n");
2263 it87_write_value(data, IT87_REG_FAN_16BIT,
2266 /* IT8705F, IT8782F, and IT8783E/F only support three fans. */
2267 if (data->type != it87 && data->type != it8782 &&
2268 data->type != it8783) {
2270 data->has_fan |= (1 << 3); /* fan4 enabled */
2272 data->has_fan |= (1 << 4); /* fan5 enabled */
2276 /* Fan input pins may be used for alternative functions */
2277 data->has_fan &= ~sio_data->skip_fan;
2279 /* Start monitoring */
2280 it87_write_value(data, IT87_REG_CONFIG,
2281 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2282 | (update_vbat ? 0x41 : 0x01));
2285 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2287 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2288 if (has_newer_autopwm(data)) {
2289 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2290 data->pwm_duty[nr] = it87_read_value(data,
2291 IT87_REG_PWM_DUTY(nr));
2293 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2294 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2295 else /* Manual mode */
2296 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2299 if (has_old_autopwm(data)) {
2302 for (i = 0; i < 5 ; i++)
2303 data->auto_temp[nr][i] = it87_read_value(data,
2304 IT87_REG_AUTO_TEMP(nr, i));
2305 for (i = 0; i < 3 ; i++)
2306 data->auto_pwm[nr][i] = it87_read_value(data,
2307 IT87_REG_AUTO_PWM(nr, i));
2311 static struct it87_data *it87_update_device(struct device *dev)
2313 struct it87_data *data = dev_get_drvdata(dev);
2316 mutex_lock(&data->update_lock);
2318 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2322 * Cleared after each update, so reenable. Value
2323 * returned by this read will be previous value
2325 it87_write_value(data, IT87_REG_CONFIG,
2326 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2328 for (i = 0; i <= 7; i++) {
2330 it87_read_value(data, IT87_REG_VIN(i));
2332 it87_read_value(data, IT87_REG_VIN_MIN(i));
2334 it87_read_value(data, IT87_REG_VIN_MAX(i));
2336 /* in8 (battery) has no limit registers */
2337 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2339 for (i = 0; i < 5; i++) {
2340 /* Skip disabled fans */
2341 if (!(data->has_fan & (1 << i)))
2345 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2346 data->fan[i][0] = it87_read_value(data,
2348 /* Add high byte if in 16-bit mode */
2349 if (has_16bit_fans(data)) {
2350 data->fan[i][0] |= it87_read_value(data,
2351 IT87_REG_FANX[i]) << 8;
2352 data->fan[i][1] |= it87_read_value(data,
2353 IT87_REG_FANX_MIN[i]) << 8;
2356 for (i = 0; i < 3; i++) {
2357 if (!(data->has_temp & (1 << i)))
2360 it87_read_value(data, IT87_REG_TEMP(i));
2362 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2364 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2366 it87_read_value(data, IT87_REG_TEMP_OFFSET[i]);
2369 /* Newer chips don't have clock dividers */
2370 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2371 i = it87_read_value(data, IT87_REG_FAN_DIV);
2372 data->fan_div[0] = i & 0x07;
2373 data->fan_div[1] = (i >> 3) & 0x07;
2374 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2378 it87_read_value(data, IT87_REG_ALARM1) |
2379 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2380 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2381 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2383 data->fan_main_ctrl = it87_read_value(data,
2384 IT87_REG_FAN_MAIN_CTRL);
2385 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2386 for (i = 0; i < 3; i++)
2387 it87_update_pwm_ctrl(data, i);
2389 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2391 * The IT8705F does not have VID capability.
2392 * The IT8718F and later don't use IT87_REG_VID for the
2395 if (data->type == it8712 || data->type == it8716) {
2396 data->vid = it87_read_value(data, IT87_REG_VID);
2398 * The older IT8712F revisions had only 5 VID pins,
2399 * but we assume it is always safe to read 6 bits.
2403 data->last_updated = jiffies;
2407 mutex_unlock(&data->update_lock);
2412 static int __init it87_device_add(unsigned short address,
2413 const struct it87_sio_data *sio_data)
2415 struct resource res = {
2416 .start = address + IT87_EC_OFFSET,
2417 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2419 .flags = IORESOURCE_IO,
2423 err = acpi_check_resource_conflict(&res);
2427 pdev = platform_device_alloc(DRVNAME, address);
2430 pr_err("Device allocation failed\n");
2434 err = platform_device_add_resources(pdev, &res, 1);
2436 pr_err("Device resource addition failed (%d)\n", err);
2437 goto exit_device_put;
2440 err = platform_device_add_data(pdev, sio_data,
2441 sizeof(struct it87_sio_data));
2443 pr_err("Platform data allocation failed\n");
2444 goto exit_device_put;
2447 err = platform_device_add(pdev);
2449 pr_err("Device addition failed (%d)\n", err);
2450 goto exit_device_put;
2456 platform_device_put(pdev);
2461 static int __init sm_it87_init(void)
2464 unsigned short isa_address = 0;
2465 struct it87_sio_data sio_data;
2467 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2468 err = it87_find(&isa_address, &sio_data);
2471 err = platform_driver_register(&it87_driver);
2475 err = it87_device_add(isa_address, &sio_data);
2477 platform_driver_unregister(&it87_driver);
2484 static void __exit sm_it87_exit(void)
2486 platform_device_unregister(pdev);
2487 platform_driver_unregister(&it87_driver);
2491 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <khali@linux-fr.org>");
2492 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2493 module_param(update_vbat, bool, 0);
2494 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2495 module_param(fix_pwm_polarity, bool, 0);
2496 MODULE_PARM_DESC(fix_pwm_polarity,
2497 "Force PWM polarity to active high (DANGEROUS)");
2498 MODULE_LICENSE("GPL");
2500 module_init(sm_it87_init);
2501 module_exit(sm_it87_exit);