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: IT8603E Super I/O chip w/LPC interface
14 * IT8623E Super I/O chip w/LPC interface
15 * IT8705F Super I/O chip w/LPC interface
16 * IT8712F Super I/O chip w/LPC interface
17 * IT8716F Super I/O chip w/LPC interface
18 * IT8718F Super I/O chip w/LPC interface
19 * IT8720F Super I/O chip w/LPC interface
20 * IT8721F Super I/O chip w/LPC interface
21 * IT8726F Super I/O chip w/LPC interface
22 * IT8728F Super I/O chip w/LPC interface
23 * IT8758E Super I/O chip w/LPC interface
24 * IT8771E Super I/O chip w/LPC interface
25 * IT8772E Super I/O chip w/LPC interface
26 * IT8781F Super I/O chip w/LPC interface
27 * IT8782F Super I/O chip w/LPC interface
28 * IT8783E/F Super I/O chip w/LPC interface
29 * Sis950 A clone of the IT8705F
31 * Copyright (C) 2001 Chris Gauthron
32 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with this program; if not, write to the Free Software
46 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
49 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/jiffies.h>
55 #include <linux/platform_device.h>
56 #include <linux/hwmon.h>
57 #include <linux/hwmon-sysfs.h>
58 #include <linux/hwmon-vid.h>
59 #include <linux/err.h>
60 #include <linux/mutex.h>
61 #include <linux/sysfs.h>
62 #include <linux/string.h>
63 #include <linux/dmi.h>
64 #include <linux/acpi.h>
68 #define DRVNAME "it87"
70 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
71 it8772, it8781, it8782, it8783, it8603 };
73 static unsigned short force_id;
74 module_param(force_id, ushort, 0);
75 MODULE_PARM_DESC(force_id, "Override the detected device ID");
77 static struct platform_device *pdev;
79 #define REG 0x2e /* The register to read/write */
80 #define DEV 0x07 /* Register: Logical device select */
81 #define VAL 0x2f /* The value to read/write */
82 #define PME 0x04 /* The device with the fan registers in it */
84 /* The device with the IT8718F/IT8720F VID value in it */
87 #define DEVID 0x20 /* Register: Device ID */
88 #define DEVREV 0x22 /* Register: Device Revision */
90 static inline int superio_inb(int reg)
96 static inline void superio_outb(int reg, int val)
102 static int superio_inw(int reg)
112 static inline void superio_select(int ldn)
118 static inline int superio_enter(void)
121 * Try to reserve REG and REG + 1 for exclusive access.
123 if (!request_muxed_region(REG, 2, DRVNAME))
133 static inline void superio_exit(void)
137 release_region(REG, 2);
140 /* Logical device 4 registers */
141 #define IT8712F_DEVID 0x8712
142 #define IT8705F_DEVID 0x8705
143 #define IT8716F_DEVID 0x8716
144 #define IT8718F_DEVID 0x8718
145 #define IT8720F_DEVID 0x8720
146 #define IT8721F_DEVID 0x8721
147 #define IT8726F_DEVID 0x8726
148 #define IT8728F_DEVID 0x8728
149 #define IT8771E_DEVID 0x8771
150 #define IT8772E_DEVID 0x8772
151 #define IT8781F_DEVID 0x8781
152 #define IT8782F_DEVID 0x8782
153 #define IT8783E_DEVID 0x8783
154 #define IT8603E_DEVID 0x8603
155 #define IT8623E_DEVID 0x8623
156 #define IT87_ACT_REG 0x30
157 #define IT87_BASE_REG 0x60
159 /* Logical device 7 registers (IT8712F and later) */
160 #define IT87_SIO_GPIO1_REG 0x25
161 #define IT87_SIO_GPIO3_REG 0x27
162 #define IT87_SIO_GPIO5_REG 0x29
163 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
164 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
165 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
166 #define IT87_SIO_VID_REG 0xfc /* VID value */
167 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
169 /* Update battery voltage after every reading if true */
170 static bool update_vbat;
172 /* Not all BIOSes properly configure the PWM registers */
173 static bool fix_pwm_polarity;
175 /* Many IT87 constants specified below */
177 /* Length of ISA address segment */
178 #define IT87_EXTENT 8
180 /* Length of ISA address segment for Environmental Controller */
181 #define IT87_EC_EXTENT 2
183 /* Offset of EC registers from ISA base address */
184 #define IT87_EC_OFFSET 5
186 /* Where are the ISA address/data registers relative to the EC base address */
187 #define IT87_ADDR_REG_OFFSET 0
188 #define IT87_DATA_REG_OFFSET 1
190 /*----- The IT87 registers -----*/
192 #define IT87_REG_CONFIG 0x00
194 #define IT87_REG_ALARM1 0x01
195 #define IT87_REG_ALARM2 0x02
196 #define IT87_REG_ALARM3 0x03
199 * The IT8718F and IT8720F have the VID value in a different register, in
200 * Super-I/O configuration space.
202 #define IT87_REG_VID 0x0a
204 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
205 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
208 #define IT87_REG_FAN_DIV 0x0b
209 #define IT87_REG_FAN_16BIT 0x0c
211 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
213 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
214 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
215 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
216 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
217 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
219 #define IT87_REG_FAN_MAIN_CTRL 0x13
220 #define IT87_REG_FAN_CTL 0x14
221 #define IT87_REG_PWM(nr) (0x15 + (nr))
222 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
224 #define IT87_REG_VIN(nr) (0x20 + (nr))
225 #define IT87_REG_TEMP(nr) (0x29 + (nr))
227 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
228 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
229 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
230 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
232 #define IT87_REG_VIN_ENABLE 0x50
233 #define IT87_REG_TEMP_ENABLE 0x51
234 #define IT87_REG_TEMP_EXTRA 0x55
235 #define IT87_REG_BEEP_ENABLE 0x5c
237 #define IT87_REG_CHIPID 0x58
239 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
240 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
242 struct it87_devices {
249 #define FEAT_12MV_ADC (1 << 0)
250 #define FEAT_NEWER_AUTOPWM (1 << 1)
251 #define FEAT_OLD_AUTOPWM (1 << 2)
252 #define FEAT_16BIT_FANS (1 << 3)
253 #define FEAT_TEMP_OFFSET (1 << 4)
254 #define FEAT_TEMP_PECI (1 << 5)
255 #define FEAT_TEMP_OLD_PECI (1 << 6)
256 #define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
257 #define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
258 #define FEAT_VID (1 << 9) /* Set if chip supports VID */
260 static const struct it87_devices it87_devices[] = {
263 .features = FEAT_OLD_AUTOPWM | FEAT_FAN16_CONFIG,
264 /* may need to overwrite */
268 .features = FEAT_OLD_AUTOPWM | FEAT_FAN16_CONFIG | FEAT_VID,
269 /* may need to overwrite */
273 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
274 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
278 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
279 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
280 .old_peci_mask = 0x4,
284 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
285 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
286 .old_peci_mask = 0x4,
290 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
291 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
292 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
294 .old_peci_mask = 0x02, /* Actually reports PCH */
298 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
299 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS,
304 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
305 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
306 /* PECI: guesswork */
308 /* 16 bit fans (OHM) */
309 /* three fans, always 16 bit (guesswork) */
314 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
315 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
316 /* PECI (coreboot) */
317 /* 12mV ADC (HWSensors4, OHM) */
318 /* 16 bit fans (HWSensors4, OHM) */
319 /* three fans, always 16 bit (datasheet) */
324 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
325 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
326 .old_peci_mask = 0x4,
330 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
331 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
332 .old_peci_mask = 0x4,
336 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
337 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
338 .old_peci_mask = 0x4,
342 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
343 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
348 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
349 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
350 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
351 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
352 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
353 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
354 ((data)->peci_mask & (1 << nr)))
355 #define has_temp_old_peci(data, nr) \
356 (((data)->features & FEAT_TEMP_OLD_PECI) && \
357 ((data)->old_peci_mask & (1 << nr)))
358 #define has_fan16_config(data) (has_16bit_fans(data) && \
359 ((data)->features & FEAT_FAN16_CONFIG))
360 #define has_five_fans(data) ((data)->features & FEAT_FIVE_FANS)
361 #define has_vid(data) ((data)->features & FEAT_VID)
363 struct it87_sio_data {
365 /* Values read from Super-I/O config space */
369 u8 internal; /* Internal sensors can be labeled */
370 /* Features skipped based on config or DMI */
379 * For each registered chip, we need to keep some data in memory.
380 * The structure is dynamically allocated.
383 struct device *hwmon_dev;
391 struct mutex update_lock;
392 char valid; /* !=0 if following fields are valid */
393 unsigned long last_updated; /* In jiffies */
395 u16 in_scaled; /* Internal voltage sensors are scaled */
396 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
397 u8 has_fan; /* Bitfield, fans enabled */
398 u16 fan[5][2]; /* Register values, [nr][0]=fan, [1]=min */
399 u8 has_temp; /* Bitfield, temp sensors enabled */
400 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
401 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
402 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
403 u8 fan_div[3]; /* Register encoding, shifted right */
404 u8 vid; /* Register encoding, combined */
406 u32 alarms; /* Register encoding, combined */
407 u8 beeps; /* Register encoding */
408 u8 fan_main_ctrl; /* Register value */
409 u8 fan_ctl; /* Register value */
412 * The following 3 arrays correspond to the same registers up to
413 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
414 * 7, and we want to preserve settings on mode changes, so we have
415 * to track all values separately.
416 * Starting with the IT8721F, the manual PWM duty cycles are stored
417 * in separate registers (8-bit values), so the separate tracking
418 * is no longer needed, but it is still done to keep the driver
421 u8 pwm_ctrl[3]; /* Register value */
422 u8 pwm_duty[3]; /* Manual PWM value set by user */
423 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
425 /* Automatic fan speed control registers */
426 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
427 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
430 static int adc_lsb(const struct it87_data *data, int nr)
432 int lsb = has_12mv_adc(data) ? 12 : 16;
433 if (data->in_scaled & (1 << nr))
438 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
440 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
441 return clamp_val(val, 0, 255);
444 static int in_from_reg(const struct it87_data *data, int nr, int val)
446 return val * adc_lsb(data, nr);
449 static inline u8 FAN_TO_REG(long rpm, int div)
453 rpm = clamp_val(rpm, 1, 1000000);
454 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
457 static inline u16 FAN16_TO_REG(long rpm)
461 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
464 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
465 1350000 / ((val) * (div)))
466 /* The divider is fixed to 2 in 16-bit mode */
467 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
468 1350000 / ((val) * 2))
470 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
471 ((val) + 500) / 1000), -128, 127))
472 #define TEMP_FROM_REG(val) ((val) * 1000)
474 static u8 pwm_to_reg(const struct it87_data *data, long val)
476 if (has_newer_autopwm(data))
482 static int pwm_from_reg(const struct it87_data *data, u8 reg)
484 if (has_newer_autopwm(data))
487 return (reg & 0x7f) << 1;
491 static int DIV_TO_REG(int val)
494 while (answer < 7 && (val >>= 1))
498 #define DIV_FROM_REG(val) (1 << (val))
500 static const unsigned int pwm_freq[8] = {
511 static int it87_probe(struct platform_device *pdev);
512 static int it87_remove(struct platform_device *pdev);
514 static int it87_read_value(struct it87_data *data, u8 reg);
515 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
516 static struct it87_data *it87_update_device(struct device *dev);
517 static int it87_check_pwm(struct device *dev);
518 static void it87_init_device(struct platform_device *pdev);
521 static struct platform_driver it87_driver = {
526 .remove = it87_remove,
529 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
532 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
534 int index = sattr->index;
536 struct it87_data *data = it87_update_device(dev);
537 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
540 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
541 const char *buf, size_t count)
543 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
545 int index = sattr->index;
547 struct it87_data *data = dev_get_drvdata(dev);
550 if (kstrtoul(buf, 10, &val) < 0)
553 mutex_lock(&data->update_lock);
554 data->in[nr][index] = in_to_reg(data, nr, val);
555 it87_write_value(data,
556 index == 1 ? IT87_REG_VIN_MIN(nr)
557 : IT87_REG_VIN_MAX(nr),
558 data->in[nr][index]);
559 mutex_unlock(&data->update_lock);
563 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
564 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
566 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
569 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
570 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
572 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
575 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
576 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
578 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
581 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
582 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
584 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
587 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
588 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
590 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
593 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
594 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
596 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
599 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
600 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
602 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
605 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
606 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
608 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
611 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
612 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
615 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
618 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
620 int index = sattr->index;
621 struct it87_data *data = it87_update_device(dev);
623 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
626 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
629 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
631 int index = sattr->index;
632 struct it87_data *data = dev_get_drvdata(dev);
636 if (kstrtol(buf, 10, &val) < 0)
639 mutex_lock(&data->update_lock);
644 reg = IT87_REG_TEMP_LOW(nr);
647 reg = IT87_REG_TEMP_HIGH(nr);
650 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
651 if (!(regval & 0x80)) {
653 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
656 reg = IT87_REG_TEMP_OFFSET[nr];
660 data->temp[nr][index] = TEMP_TO_REG(val);
661 it87_write_value(data, reg, data->temp[nr][index]);
662 mutex_unlock(&data->update_lock);
666 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
667 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
669 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
671 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
673 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
674 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
676 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
678 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
680 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
681 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
683 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
685 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
688 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
691 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
692 int nr = sensor_attr->index;
693 struct it87_data *data = it87_update_device(dev);
694 u8 reg = data->sensor; /* In case value is updated while used */
695 u8 extra = data->extra;
697 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
698 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
699 return sprintf(buf, "6\n"); /* Intel PECI */
701 return sprintf(buf, "3\n"); /* thermal diode */
703 return sprintf(buf, "4\n"); /* thermistor */
704 return sprintf(buf, "0\n"); /* disabled */
707 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
708 const char *buf, size_t count)
710 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
711 int nr = sensor_attr->index;
713 struct it87_data *data = dev_get_drvdata(dev);
717 if (kstrtol(buf, 10, &val) < 0)
720 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
723 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
725 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
726 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
728 if (val == 2) { /* backwards compatibility */
730 "Sensor type 2 is deprecated, please use 4 instead\n");
733 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
738 else if (has_temp_peci(data, nr) && val == 6)
739 reg |= (nr + 1) << 6;
740 else if (has_temp_old_peci(data, nr) && val == 6)
745 mutex_lock(&data->update_lock);
748 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
749 if (has_temp_old_peci(data, nr))
750 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
751 data->valid = 0; /* Force cache refresh */
752 mutex_unlock(&data->update_lock);
756 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
758 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
760 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
765 static int pwm_mode(const struct it87_data *data, int nr)
767 int ctrl = data->fan_main_ctrl & (1 << nr);
769 if (ctrl == 0 && data->type != it8603) /* Full speed */
771 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
773 else /* Manual mode */
777 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
780 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
782 int index = sattr->index;
784 struct it87_data *data = it87_update_device(dev);
786 speed = has_16bit_fans(data) ?
787 FAN16_FROM_REG(data->fan[nr][index]) :
788 FAN_FROM_REG(data->fan[nr][index],
789 DIV_FROM_REG(data->fan_div[nr]));
790 return sprintf(buf, "%d\n", speed);
793 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
796 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
797 int nr = sensor_attr->index;
799 struct it87_data *data = it87_update_device(dev);
800 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
802 static ssize_t show_pwm_enable(struct device *dev,
803 struct device_attribute *attr, char *buf)
805 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
806 int nr = sensor_attr->index;
808 struct it87_data *data = it87_update_device(dev);
809 return sprintf(buf, "%d\n", pwm_mode(data, nr));
811 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
814 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
815 int nr = sensor_attr->index;
817 struct it87_data *data = it87_update_device(dev);
818 return sprintf(buf, "%d\n",
819 pwm_from_reg(data, data->pwm_duty[nr]));
821 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
824 struct it87_data *data = it87_update_device(dev);
825 int index = (data->fan_ctl >> 4) & 0x07;
827 return sprintf(buf, "%u\n", pwm_freq[index]);
830 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
831 const char *buf, size_t count)
833 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
835 int index = sattr->index;
837 struct it87_data *data = dev_get_drvdata(dev);
841 if (kstrtol(buf, 10, &val) < 0)
844 mutex_lock(&data->update_lock);
846 if (has_16bit_fans(data)) {
847 data->fan[nr][index] = FAN16_TO_REG(val);
848 it87_write_value(data, IT87_REG_FAN_MIN[nr],
849 data->fan[nr][index] & 0xff);
850 it87_write_value(data, IT87_REG_FANX_MIN[nr],
851 data->fan[nr][index] >> 8);
853 reg = it87_read_value(data, IT87_REG_FAN_DIV);
856 data->fan_div[nr] = reg & 0x07;
859 data->fan_div[nr] = (reg >> 3) & 0x07;
862 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
865 data->fan[nr][index] =
866 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
867 it87_write_value(data, IT87_REG_FAN_MIN[nr],
868 data->fan[nr][index]);
871 mutex_unlock(&data->update_lock);
875 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
876 const char *buf, size_t count)
878 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
879 int nr = sensor_attr->index;
881 struct it87_data *data = dev_get_drvdata(dev);
886 if (kstrtoul(buf, 10, &val) < 0)
889 mutex_lock(&data->update_lock);
890 old = it87_read_value(data, IT87_REG_FAN_DIV);
892 /* Save fan min limit */
893 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
898 data->fan_div[nr] = DIV_TO_REG(val);
902 data->fan_div[nr] = 1;
904 data->fan_div[nr] = 3;
907 val |= (data->fan_div[0] & 0x07);
908 val |= (data->fan_div[1] & 0x07) << 3;
909 if (data->fan_div[2] == 3)
911 it87_write_value(data, IT87_REG_FAN_DIV, val);
913 /* Restore fan min limit */
914 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
915 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
917 mutex_unlock(&data->update_lock);
921 /* Returns 0 if OK, -EINVAL otherwise */
922 static int check_trip_points(struct device *dev, int nr)
924 const struct it87_data *data = dev_get_drvdata(dev);
927 if (has_old_autopwm(data)) {
928 for (i = 0; i < 3; i++) {
929 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
932 for (i = 0; i < 2; i++) {
933 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
940 "Inconsistent trip points, not switching to automatic mode\n");
941 dev_err(dev, "Adjust the trip points and try again\n");
946 static ssize_t set_pwm_enable(struct device *dev,
947 struct device_attribute *attr, const char *buf, size_t count)
949 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
950 int nr = sensor_attr->index;
952 struct it87_data *data = dev_get_drvdata(dev);
955 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
958 /* Check trip points before switching to automatic mode */
960 if (check_trip_points(dev, nr) < 0)
964 /* IT8603E does not have on/off mode */
965 if (val == 0 && data->type == it8603)
968 mutex_lock(&data->update_lock);
972 /* make sure the fan is on when in on/off mode */
973 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
974 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
975 /* set on/off mode */
976 data->fan_main_ctrl &= ~(1 << nr);
977 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
978 data->fan_main_ctrl);
980 if (val == 1) /* Manual mode */
981 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
982 data->pwm_temp_map[nr] :
984 else /* Automatic mode */
985 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
986 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
988 if (data->type != it8603) {
989 /* set SmartGuardian mode */
990 data->fan_main_ctrl |= (1 << nr);
991 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
992 data->fan_main_ctrl);
996 mutex_unlock(&data->update_lock);
999 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1000 const char *buf, size_t count)
1002 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1003 int nr = sensor_attr->index;
1005 struct it87_data *data = dev_get_drvdata(dev);
1008 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1011 mutex_lock(&data->update_lock);
1012 if (has_newer_autopwm(data)) {
1014 * If we are in automatic mode, the PWM duty cycle register
1015 * is read-only so we can't write the value.
1017 if (data->pwm_ctrl[nr] & 0x80) {
1018 mutex_unlock(&data->update_lock);
1021 data->pwm_duty[nr] = pwm_to_reg(data, val);
1022 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1023 data->pwm_duty[nr]);
1025 data->pwm_duty[nr] = pwm_to_reg(data, val);
1027 * If we are in manual mode, write the duty cycle immediately;
1028 * otherwise, just store it for later use.
1030 if (!(data->pwm_ctrl[nr] & 0x80)) {
1031 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1032 it87_write_value(data, IT87_REG_PWM(nr),
1033 data->pwm_ctrl[nr]);
1036 mutex_unlock(&data->update_lock);
1039 static ssize_t set_pwm_freq(struct device *dev,
1040 struct device_attribute *attr, const char *buf, size_t count)
1042 struct it87_data *data = dev_get_drvdata(dev);
1046 if (kstrtoul(buf, 10, &val) < 0)
1049 /* Search for the nearest available frequency */
1050 for (i = 0; i < 7; i++) {
1051 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1055 mutex_lock(&data->update_lock);
1056 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1057 data->fan_ctl |= i << 4;
1058 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1059 mutex_unlock(&data->update_lock);
1063 static ssize_t show_pwm_temp_map(struct device *dev,
1064 struct device_attribute *attr, char *buf)
1066 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1067 int nr = sensor_attr->index;
1069 struct it87_data *data = it87_update_device(dev);
1072 if (data->pwm_temp_map[nr] < 3)
1073 map = 1 << data->pwm_temp_map[nr];
1075 map = 0; /* Should never happen */
1076 return sprintf(buf, "%d\n", map);
1078 static ssize_t set_pwm_temp_map(struct device *dev,
1079 struct device_attribute *attr, const char *buf, size_t count)
1081 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1082 int nr = sensor_attr->index;
1084 struct it87_data *data = dev_get_drvdata(dev);
1089 * This check can go away if we ever support automatic fan speed
1090 * control on newer chips.
1092 if (!has_old_autopwm(data)) {
1093 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1097 if (kstrtol(buf, 10, &val) < 0)
1114 mutex_lock(&data->update_lock);
1115 data->pwm_temp_map[nr] = reg;
1117 * If we are in automatic mode, write the temp mapping immediately;
1118 * otherwise, just store it for later use.
1120 if (data->pwm_ctrl[nr] & 0x80) {
1121 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1122 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1124 mutex_unlock(&data->update_lock);
1128 static ssize_t show_auto_pwm(struct device *dev,
1129 struct device_attribute *attr, char *buf)
1131 struct it87_data *data = it87_update_device(dev);
1132 struct sensor_device_attribute_2 *sensor_attr =
1133 to_sensor_dev_attr_2(attr);
1134 int nr = sensor_attr->nr;
1135 int point = sensor_attr->index;
1137 return sprintf(buf, "%d\n",
1138 pwm_from_reg(data, data->auto_pwm[nr][point]));
1141 static ssize_t set_auto_pwm(struct device *dev,
1142 struct device_attribute *attr, const char *buf, size_t count)
1144 struct it87_data *data = dev_get_drvdata(dev);
1145 struct sensor_device_attribute_2 *sensor_attr =
1146 to_sensor_dev_attr_2(attr);
1147 int nr = sensor_attr->nr;
1148 int point = sensor_attr->index;
1151 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1154 mutex_lock(&data->update_lock);
1155 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1156 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1157 data->auto_pwm[nr][point]);
1158 mutex_unlock(&data->update_lock);
1162 static ssize_t show_auto_temp(struct device *dev,
1163 struct device_attribute *attr, char *buf)
1165 struct it87_data *data = it87_update_device(dev);
1166 struct sensor_device_attribute_2 *sensor_attr =
1167 to_sensor_dev_attr_2(attr);
1168 int nr = sensor_attr->nr;
1169 int point = sensor_attr->index;
1171 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1174 static ssize_t set_auto_temp(struct device *dev,
1175 struct device_attribute *attr, const char *buf, size_t count)
1177 struct it87_data *data = dev_get_drvdata(dev);
1178 struct sensor_device_attribute_2 *sensor_attr =
1179 to_sensor_dev_attr_2(attr);
1180 int nr = sensor_attr->nr;
1181 int point = sensor_attr->index;
1184 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1187 mutex_lock(&data->update_lock);
1188 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1189 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1190 data->auto_temp[nr][point]);
1191 mutex_unlock(&data->update_lock);
1195 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1196 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1198 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1201 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1202 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1204 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1207 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1208 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1210 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1213 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1214 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1217 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1218 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1221 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1222 show_pwm_enable, set_pwm_enable, 0);
1223 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1224 static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1225 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1226 show_pwm_temp_map, set_pwm_temp_map, 0);
1227 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1228 show_auto_pwm, set_auto_pwm, 0, 0);
1229 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1230 show_auto_pwm, set_auto_pwm, 0, 1);
1231 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1232 show_auto_pwm, set_auto_pwm, 0, 2);
1233 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1234 show_auto_pwm, NULL, 0, 3);
1235 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1236 show_auto_temp, set_auto_temp, 0, 1);
1237 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1238 show_auto_temp, set_auto_temp, 0, 0);
1239 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1240 show_auto_temp, set_auto_temp, 0, 2);
1241 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1242 show_auto_temp, set_auto_temp, 0, 3);
1243 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1244 show_auto_temp, set_auto_temp, 0, 4);
1246 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1247 show_pwm_enable, set_pwm_enable, 1);
1248 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1249 static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1250 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1251 show_pwm_temp_map, set_pwm_temp_map, 1);
1252 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1253 show_auto_pwm, set_auto_pwm, 1, 0);
1254 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1255 show_auto_pwm, set_auto_pwm, 1, 1);
1256 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1257 show_auto_pwm, set_auto_pwm, 1, 2);
1258 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1259 show_auto_pwm, NULL, 1, 3);
1260 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1261 show_auto_temp, set_auto_temp, 1, 1);
1262 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1263 show_auto_temp, set_auto_temp, 1, 0);
1264 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1265 show_auto_temp, set_auto_temp, 1, 2);
1266 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1267 show_auto_temp, set_auto_temp, 1, 3);
1268 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1269 show_auto_temp, set_auto_temp, 1, 4);
1271 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1272 show_pwm_enable, set_pwm_enable, 2);
1273 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1274 static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1275 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1276 show_pwm_temp_map, set_pwm_temp_map, 2);
1277 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1278 show_auto_pwm, set_auto_pwm, 2, 0);
1279 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1280 show_auto_pwm, set_auto_pwm, 2, 1);
1281 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1282 show_auto_pwm, set_auto_pwm, 2, 2);
1283 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1284 show_auto_pwm, NULL, 2, 3);
1285 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1286 show_auto_temp, set_auto_temp, 2, 1);
1287 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1288 show_auto_temp, set_auto_temp, 2, 0);
1289 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1290 show_auto_temp, set_auto_temp, 2, 2);
1291 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1292 show_auto_temp, set_auto_temp, 2, 3);
1293 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1294 show_auto_temp, set_auto_temp, 2, 4);
1297 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1300 struct it87_data *data = it87_update_device(dev);
1301 return sprintf(buf, "%u\n", data->alarms);
1303 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1305 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1308 int bitnr = to_sensor_dev_attr(attr)->index;
1309 struct it87_data *data = it87_update_device(dev);
1310 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1313 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1314 *attr, const char *buf, size_t count)
1316 struct it87_data *data = dev_get_drvdata(dev);
1320 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1323 mutex_lock(&data->update_lock);
1324 config = it87_read_value(data, IT87_REG_CONFIG);
1329 it87_write_value(data, IT87_REG_CONFIG, config);
1330 /* Invalidate cache to force re-read */
1333 mutex_unlock(&data->update_lock);
1338 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1339 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1340 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1341 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1342 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1343 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1344 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1345 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1346 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1347 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1348 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1349 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1350 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1351 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1352 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1353 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1354 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1355 show_alarm, clear_intrusion, 4);
1357 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1360 int bitnr = to_sensor_dev_attr(attr)->index;
1361 struct it87_data *data = it87_update_device(dev);
1362 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1364 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1365 const char *buf, size_t count)
1367 int bitnr = to_sensor_dev_attr(attr)->index;
1368 struct it87_data *data = dev_get_drvdata(dev);
1371 if (kstrtol(buf, 10, &val) < 0
1372 || (val != 0 && val != 1))
1375 mutex_lock(&data->update_lock);
1376 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1378 data->beeps |= (1 << bitnr);
1380 data->beeps &= ~(1 << bitnr);
1381 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1382 mutex_unlock(&data->update_lock);
1386 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1387 show_beep, set_beep, 1);
1388 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1389 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1390 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1391 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1392 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1393 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1394 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1395 /* fanX_beep writability is set later */
1396 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1397 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1398 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1399 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1400 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1401 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1402 show_beep, set_beep, 2);
1403 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1404 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1406 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1409 struct it87_data *data = dev_get_drvdata(dev);
1410 return sprintf(buf, "%u\n", data->vrm);
1412 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1413 const char *buf, size_t count)
1415 struct it87_data *data = dev_get_drvdata(dev);
1418 if (kstrtoul(buf, 10, &val) < 0)
1425 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1427 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1430 struct it87_data *data = it87_update_device(dev);
1431 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1433 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1435 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1438 static const char * const labels[] = {
1443 static const char * const labels_it8721[] = {
1448 struct it87_data *data = dev_get_drvdata(dev);
1449 int nr = to_sensor_dev_attr(attr)->index;
1451 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1454 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1455 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1456 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1457 /* special AVCC3 IT8603E in9 */
1458 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1460 static ssize_t show_name(struct device *dev, struct device_attribute
1461 *devattr, char *buf)
1463 struct it87_data *data = dev_get_drvdata(dev);
1464 return sprintf(buf, "%s\n", data->name);
1466 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1468 static struct attribute *it87_attributes_in[10][5] = {
1470 &sensor_dev_attr_in0_input.dev_attr.attr,
1471 &sensor_dev_attr_in0_min.dev_attr.attr,
1472 &sensor_dev_attr_in0_max.dev_attr.attr,
1473 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1476 &sensor_dev_attr_in1_input.dev_attr.attr,
1477 &sensor_dev_attr_in1_min.dev_attr.attr,
1478 &sensor_dev_attr_in1_max.dev_attr.attr,
1479 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1482 &sensor_dev_attr_in2_input.dev_attr.attr,
1483 &sensor_dev_attr_in2_min.dev_attr.attr,
1484 &sensor_dev_attr_in2_max.dev_attr.attr,
1485 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1488 &sensor_dev_attr_in3_input.dev_attr.attr,
1489 &sensor_dev_attr_in3_min.dev_attr.attr,
1490 &sensor_dev_attr_in3_max.dev_attr.attr,
1491 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1494 &sensor_dev_attr_in4_input.dev_attr.attr,
1495 &sensor_dev_attr_in4_min.dev_attr.attr,
1496 &sensor_dev_attr_in4_max.dev_attr.attr,
1497 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1500 &sensor_dev_attr_in5_input.dev_attr.attr,
1501 &sensor_dev_attr_in5_min.dev_attr.attr,
1502 &sensor_dev_attr_in5_max.dev_attr.attr,
1503 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1506 &sensor_dev_attr_in6_input.dev_attr.attr,
1507 &sensor_dev_attr_in6_min.dev_attr.attr,
1508 &sensor_dev_attr_in6_max.dev_attr.attr,
1509 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1512 &sensor_dev_attr_in7_input.dev_attr.attr,
1513 &sensor_dev_attr_in7_min.dev_attr.attr,
1514 &sensor_dev_attr_in7_max.dev_attr.attr,
1515 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1518 &sensor_dev_attr_in8_input.dev_attr.attr,
1521 &sensor_dev_attr_in9_input.dev_attr.attr,
1525 static const struct attribute_group it87_group_in[10] = {
1526 { .attrs = it87_attributes_in[0] },
1527 { .attrs = it87_attributes_in[1] },
1528 { .attrs = it87_attributes_in[2] },
1529 { .attrs = it87_attributes_in[3] },
1530 { .attrs = it87_attributes_in[4] },
1531 { .attrs = it87_attributes_in[5] },
1532 { .attrs = it87_attributes_in[6] },
1533 { .attrs = it87_attributes_in[7] },
1534 { .attrs = it87_attributes_in[8] },
1535 { .attrs = it87_attributes_in[9] },
1538 static struct attribute *it87_attributes_temp[3][6] = {
1540 &sensor_dev_attr_temp1_input.dev_attr.attr,
1541 &sensor_dev_attr_temp1_max.dev_attr.attr,
1542 &sensor_dev_attr_temp1_min.dev_attr.attr,
1543 &sensor_dev_attr_temp1_type.dev_attr.attr,
1544 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1547 &sensor_dev_attr_temp2_input.dev_attr.attr,
1548 &sensor_dev_attr_temp2_max.dev_attr.attr,
1549 &sensor_dev_attr_temp2_min.dev_attr.attr,
1550 &sensor_dev_attr_temp2_type.dev_attr.attr,
1551 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1554 &sensor_dev_attr_temp3_input.dev_attr.attr,
1555 &sensor_dev_attr_temp3_max.dev_attr.attr,
1556 &sensor_dev_attr_temp3_min.dev_attr.attr,
1557 &sensor_dev_attr_temp3_type.dev_attr.attr,
1558 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1562 static const struct attribute_group it87_group_temp[3] = {
1563 { .attrs = it87_attributes_temp[0] },
1564 { .attrs = it87_attributes_temp[1] },
1565 { .attrs = it87_attributes_temp[2] },
1568 static struct attribute *it87_attributes_temp_offset[] = {
1569 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1570 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1571 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1574 static struct attribute *it87_attributes[] = {
1575 &dev_attr_alarms.attr,
1576 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1577 &dev_attr_name.attr,
1581 static const struct attribute_group it87_group = {
1582 .attrs = it87_attributes,
1585 static struct attribute *it87_attributes_in_beep[] = {
1586 &sensor_dev_attr_in0_beep.dev_attr.attr,
1587 &sensor_dev_attr_in1_beep.dev_attr.attr,
1588 &sensor_dev_attr_in2_beep.dev_attr.attr,
1589 &sensor_dev_attr_in3_beep.dev_attr.attr,
1590 &sensor_dev_attr_in4_beep.dev_attr.attr,
1591 &sensor_dev_attr_in5_beep.dev_attr.attr,
1592 &sensor_dev_attr_in6_beep.dev_attr.attr,
1593 &sensor_dev_attr_in7_beep.dev_attr.attr,
1598 static struct attribute *it87_attributes_temp_beep[] = {
1599 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1600 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1601 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1604 static struct attribute *it87_attributes_fan[5][3+1] = { {
1605 &sensor_dev_attr_fan1_input.dev_attr.attr,
1606 &sensor_dev_attr_fan1_min.dev_attr.attr,
1607 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1610 &sensor_dev_attr_fan2_input.dev_attr.attr,
1611 &sensor_dev_attr_fan2_min.dev_attr.attr,
1612 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1615 &sensor_dev_attr_fan3_input.dev_attr.attr,
1616 &sensor_dev_attr_fan3_min.dev_attr.attr,
1617 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1620 &sensor_dev_attr_fan4_input.dev_attr.attr,
1621 &sensor_dev_attr_fan4_min.dev_attr.attr,
1622 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1625 &sensor_dev_attr_fan5_input.dev_attr.attr,
1626 &sensor_dev_attr_fan5_min.dev_attr.attr,
1627 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1631 static const struct attribute_group it87_group_fan[5] = {
1632 { .attrs = it87_attributes_fan[0] },
1633 { .attrs = it87_attributes_fan[1] },
1634 { .attrs = it87_attributes_fan[2] },
1635 { .attrs = it87_attributes_fan[3] },
1636 { .attrs = it87_attributes_fan[4] },
1639 static const struct attribute *it87_attributes_fan_div[] = {
1640 &sensor_dev_attr_fan1_div.dev_attr.attr,
1641 &sensor_dev_attr_fan2_div.dev_attr.attr,
1642 &sensor_dev_attr_fan3_div.dev_attr.attr,
1645 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1646 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1647 &sensor_dev_attr_pwm1.dev_attr.attr,
1648 &dev_attr_pwm1_freq.attr,
1649 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1652 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1653 &sensor_dev_attr_pwm2.dev_attr.attr,
1654 &dev_attr_pwm2_freq.attr,
1655 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1658 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1659 &sensor_dev_attr_pwm3.dev_attr.attr,
1660 &dev_attr_pwm3_freq.attr,
1661 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1665 static const struct attribute_group it87_group_pwm[3] = {
1666 { .attrs = it87_attributes_pwm[0] },
1667 { .attrs = it87_attributes_pwm[1] },
1668 { .attrs = it87_attributes_pwm[2] },
1671 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1672 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1673 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1674 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1675 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1676 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1677 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1678 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1679 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1680 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1683 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1684 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1685 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1686 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1687 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1688 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1689 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1690 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1691 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1694 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1695 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1696 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1697 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1698 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1699 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1700 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1701 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1702 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1706 static const struct attribute_group it87_group_autopwm[3] = {
1707 { .attrs = it87_attributes_autopwm[0] },
1708 { .attrs = it87_attributes_autopwm[1] },
1709 { .attrs = it87_attributes_autopwm[2] },
1712 static struct attribute *it87_attributes_fan_beep[] = {
1713 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1714 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1715 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1716 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1717 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1720 static struct attribute *it87_attributes_vid[] = {
1722 &dev_attr_cpu0_vid.attr,
1726 static const struct attribute_group it87_group_vid = {
1727 .attrs = it87_attributes_vid,
1730 static struct attribute *it87_attributes_label[] = {
1731 &sensor_dev_attr_in3_label.dev_attr.attr,
1732 &sensor_dev_attr_in7_label.dev_attr.attr,
1733 &sensor_dev_attr_in8_label.dev_attr.attr,
1734 &sensor_dev_attr_in9_label.dev_attr.attr,
1738 static const struct attribute_group it87_group_label = {
1739 .attrs = it87_attributes_label,
1742 /* SuperIO detection - will change isa_address if a chip is found */
1743 static int __init it87_find(unsigned short *address,
1744 struct it87_sio_data *sio_data)
1748 const char *board_vendor, *board_name;
1750 err = superio_enter();
1755 chip_type = force_id ? force_id : superio_inw(DEVID);
1757 switch (chip_type) {
1759 sio_data->type = it87;
1762 sio_data->type = it8712;
1766 sio_data->type = it8716;
1769 sio_data->type = it8718;
1772 sio_data->type = it8720;
1775 sio_data->type = it8721;
1778 sio_data->type = it8728;
1781 sio_data->type = it8771;
1784 sio_data->type = it8772;
1787 sio_data->type = it8781;
1790 sio_data->type = it8782;
1793 sio_data->type = it8783;
1797 sio_data->type = it8603;
1799 case 0xffff: /* No device at all */
1802 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1806 superio_select(PME);
1807 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1808 pr_info("Device not activated, skipping\n");
1812 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1813 if (*address == 0) {
1814 pr_info("Base address not set, skipping\n");
1819 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1820 pr_info("Found IT%04x%c chip at 0x%x, revision %d\n", chip_type,
1821 chip_type == 0x8771 || chip_type == 0x8772 ||
1822 chip_type == 0x8603 ? 'E' : 'F', *address,
1823 sio_data->revision);
1825 /* in8 (Vbat) is always internal */
1826 sio_data->internal = (1 << 2);
1827 /* Only the IT8603E has in9 */
1828 if (sio_data->type != it8603)
1829 sio_data->skip_in |= (1 << 9);
1831 if (!(it87_devices[sio_data->type].features & FEAT_VID))
1832 sio_data->skip_vid = 1;
1834 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1835 if (sio_data->type == it87) {
1836 /* The IT8705F has a different LD number for GPIO */
1838 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1839 } else if (sio_data->type == it8783) {
1840 int reg25, reg27, reg2a, reg2c, regef;
1842 superio_select(GPIO);
1844 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1845 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1846 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1847 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1848 regef = superio_inb(IT87_SIO_SPI_REG);
1850 /* Check if fan3 is there or not */
1851 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
1852 sio_data->skip_fan |= (1 << 2);
1853 if ((reg25 & (1 << 4))
1854 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
1855 sio_data->skip_pwm |= (1 << 2);
1857 /* Check if fan2 is there or not */
1858 if (reg27 & (1 << 7))
1859 sio_data->skip_fan |= (1 << 1);
1860 if (reg27 & (1 << 3))
1861 sio_data->skip_pwm |= (1 << 1);
1864 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
1865 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1868 if (reg27 & (1 << 1))
1869 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1873 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1875 if (reg27 & (1 << 2)) {
1877 * The data sheet is a bit unclear regarding the
1878 * internal voltage divider for VCCH5V. It says
1879 * "This bit enables and switches VIN7 (pin 91) to the
1880 * internal voltage divider for VCCH5V".
1881 * This is different to other chips, where the internal
1882 * voltage divider would connect VIN7 to an internal
1883 * voltage source. Maybe that is the case here as well.
1885 * Since we don't know for sure, re-route it if that is
1886 * not the case, and ask the user to report if the
1887 * resulting voltage is sane.
1889 if (!(reg2c & (1 << 1))) {
1891 superio_outb(IT87_SIO_PINX2_REG, reg2c);
1892 pr_notice("Routing internal VCCH5V to in7.\n");
1894 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1895 pr_notice("Please report if it displays a reasonable voltage.\n");
1898 if (reg2c & (1 << 0))
1899 sio_data->internal |= (1 << 0);
1900 if (reg2c & (1 << 1))
1901 sio_data->internal |= (1 << 1);
1903 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1904 } else if (sio_data->type == it8603) {
1907 superio_select(GPIO);
1909 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1911 /* Check if fan3 is there or not */
1912 if (reg27 & (1 << 6))
1913 sio_data->skip_pwm |= (1 << 2);
1914 if (reg27 & (1 << 7))
1915 sio_data->skip_fan |= (1 << 2);
1917 /* Check if fan2 is there or not */
1918 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1919 if (reg29 & (1 << 1))
1920 sio_data->skip_pwm |= (1 << 1);
1921 if (reg29 & (1 << 2))
1922 sio_data->skip_fan |= (1 << 1);
1924 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1925 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1928 sio_data->skip_pwm |= (1 << 3);
1929 sio_data->skip_fan |= (1 << 3);
1931 sio_data->internal |= (1 << 1); /* in7 is VSB */
1932 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1934 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1939 superio_select(GPIO);
1941 reg = superio_inb(IT87_SIO_GPIO3_REG);
1942 if (!sio_data->skip_vid) {
1943 /* We need at least 4 VID pins */
1945 pr_info("VID is disabled (pins used for GPIO)\n");
1946 sio_data->skip_vid = 1;
1950 /* Check if fan3 is there or not */
1952 sio_data->skip_pwm |= (1 << 2);
1954 sio_data->skip_fan |= (1 << 2);
1956 /* Check if fan2 is there or not */
1957 reg = superio_inb(IT87_SIO_GPIO5_REG);
1959 sio_data->skip_pwm |= (1 << 1);
1961 sio_data->skip_fan |= (1 << 1);
1963 if (!sio_data->skip_vid)
1964 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1966 reg = superio_inb(IT87_SIO_PINX2_REG);
1968 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
1971 * The IT8720F has no VIN7 pin, so VCCH should always be
1972 * routed internally to VIN7 with an internal divider.
1973 * Curiously, there still is a configuration bit to control
1974 * this, which means it can be set incorrectly. And even
1975 * more curiously, many boards out there are improperly
1976 * configured, even though the IT8720F datasheet claims
1977 * that the internal routing of VCCH to VIN7 is the default
1978 * setting. So we force the internal routing in this case.
1980 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
1981 * If UART6 is enabled, re-route VIN7 to the internal divider
1982 * if that is not already the case.
1984 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
1986 superio_outb(IT87_SIO_PINX2_REG, reg);
1987 pr_notice("Routing internal VCCH to in7\n");
1990 sio_data->internal |= (1 << 0);
1991 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1992 sio_data->type == it8728 ||
1993 sio_data->type == it8771 ||
1994 sio_data->type == it8772)
1995 sio_data->internal |= (1 << 1);
1998 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
1999 * While VIN7 can be routed to the internal voltage divider,
2000 * VIN5 and VIN6 are not available if UART6 is enabled.
2002 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2003 * is the temperature source. Since we can not read the
2004 * temperature source here, skip_temp is preliminary.
2007 sio_data->skip_in |= (1 << 5) | (1 << 6);
2008 sio_data->skip_temp |= (1 << 2);
2011 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2013 if (sio_data->beep_pin)
2014 pr_info("Beeping is supported\n");
2016 /* Disable specific features based on DMI strings */
2017 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2018 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2019 if (board_vendor && board_name) {
2020 if (strcmp(board_vendor, "nVIDIA") == 0
2021 && strcmp(board_name, "FN68PT") == 0) {
2023 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2024 * connected to a fan, but to something else. One user
2025 * has reported instant system power-off when changing
2026 * the PWM2 duty cycle, so we disable it.
2027 * I use the board name string as the trigger in case
2028 * the same board is ever used in other systems.
2030 pr_info("Disabling pwm2 due to hardware constraints\n");
2031 sio_data->skip_pwm = (1 << 1);
2040 static void it87_remove_files(struct device *dev)
2042 struct it87_data *data = platform_get_drvdata(pdev);
2043 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2046 sysfs_remove_group(&dev->kobj, &it87_group);
2047 for (i = 0; i < 10; i++) {
2048 if (sio_data->skip_in & (1 << i))
2050 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2051 if (it87_attributes_in_beep[i])
2052 sysfs_remove_file(&dev->kobj,
2053 it87_attributes_in_beep[i]);
2055 for (i = 0; i < 3; i++) {
2056 if (!(data->has_temp & (1 << i)))
2058 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
2059 if (has_temp_offset(data))
2060 sysfs_remove_file(&dev->kobj,
2061 it87_attributes_temp_offset[i]);
2062 if (sio_data->beep_pin)
2063 sysfs_remove_file(&dev->kobj,
2064 it87_attributes_temp_beep[i]);
2066 for (i = 0; i < 5; i++) {
2067 if (!(data->has_fan & (1 << i)))
2069 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
2070 if (sio_data->beep_pin)
2071 sysfs_remove_file(&dev->kobj,
2072 it87_attributes_fan_beep[i]);
2073 if (i < 3 && !has_16bit_fans(data))
2074 sysfs_remove_file(&dev->kobj,
2075 it87_attributes_fan_div[i]);
2077 for (i = 0; i < 3; i++) {
2078 if (sio_data->skip_pwm & (1 << 0))
2080 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
2081 if (has_old_autopwm(data))
2082 sysfs_remove_group(&dev->kobj,
2083 &it87_group_autopwm[i]);
2085 if (!sio_data->skip_vid)
2086 sysfs_remove_group(&dev->kobj, &it87_group_vid);
2087 sysfs_remove_group(&dev->kobj, &it87_group_label);
2090 static int it87_probe(struct platform_device *pdev)
2092 struct it87_data *data;
2093 struct resource *res;
2094 struct device *dev = &pdev->dev;
2095 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2097 int enable_pwm_interface;
2098 int fan_beep_need_rw;
2100 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2101 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2103 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2104 (unsigned long)res->start,
2105 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2109 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2113 data->addr = res->start;
2114 data->type = sio_data->type;
2115 data->features = it87_devices[sio_data->type].features;
2116 data->peci_mask = it87_devices[sio_data->type].peci_mask;
2117 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
2118 data->name = it87_devices[sio_data->type].name;
2120 * IT8705F Datasheet 0.4.1, 3h == Version G.
2121 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2122 * These are the first revisions with 16-bit tachometer support.
2124 switch (data->type) {
2126 if (sio_data->revision >= 0x03) {
2127 data->features &= ~FEAT_OLD_AUTOPWM;
2128 data->features |= FEAT_16BIT_FANS;
2132 if (sio_data->revision >= 0x08) {
2133 data->features &= ~FEAT_OLD_AUTOPWM;
2134 data->features |= FEAT_16BIT_FANS | FEAT_FIVE_FANS;
2141 /* Now, we do the remaining detection. */
2142 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2143 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2146 platform_set_drvdata(pdev, data);
2148 mutex_init(&data->update_lock);
2150 /* Check PWM configuration */
2151 enable_pwm_interface = it87_check_pwm(dev);
2153 /* Starting with IT8721F, we handle scaling of internal voltages */
2154 if (has_12mv_adc(data)) {
2155 if (sio_data->internal & (1 << 0))
2156 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2157 if (sio_data->internal & (1 << 1))
2158 data->in_scaled |= (1 << 7); /* in7 is VSB */
2159 if (sio_data->internal & (1 << 2))
2160 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2161 if (sio_data->internal & (1 << 3))
2162 data->in_scaled |= (1 << 9); /* in9 is AVCC */
2163 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2164 sio_data->type == it8783) {
2165 if (sio_data->internal & (1 << 0))
2166 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2167 if (sio_data->internal & (1 << 1))
2168 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
2171 data->has_temp = 0x07;
2172 if (sio_data->skip_temp & (1 << 2)) {
2173 if (sio_data->type == it8782
2174 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2175 data->has_temp &= ~(1 << 2);
2178 /* Initialize the IT87 chip */
2179 it87_init_device(pdev);
2181 /* Register sysfs hooks */
2182 err = sysfs_create_group(&dev->kobj, &it87_group);
2186 for (i = 0; i < 10; i++) {
2187 if (sio_data->skip_in & (1 << i))
2189 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2192 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2193 err = sysfs_create_file(&dev->kobj,
2194 it87_attributes_in_beep[i]);
2200 for (i = 0; i < 3; i++) {
2201 if (!(data->has_temp & (1 << i)))
2203 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2206 if (has_temp_offset(data)) {
2207 err = sysfs_create_file(&dev->kobj,
2208 it87_attributes_temp_offset[i]);
2212 if (sio_data->beep_pin) {
2213 err = sysfs_create_file(&dev->kobj,
2214 it87_attributes_temp_beep[i]);
2220 /* Do not create fan files for disabled fans */
2221 fan_beep_need_rw = 1;
2222 for (i = 0; i < 5; i++) {
2223 if (!(data->has_fan & (1 << i)))
2225 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2229 if (i < 3 && !has_16bit_fans(data)) {
2230 err = sysfs_create_file(&dev->kobj,
2231 it87_attributes_fan_div[i]);
2236 if (sio_data->beep_pin) {
2237 err = sysfs_create_file(&dev->kobj,
2238 it87_attributes_fan_beep[i]);
2241 if (!fan_beep_need_rw)
2245 * As we have a single beep enable bit for all fans,
2246 * only the first enabled fan has a writable attribute
2249 if (sysfs_chmod_file(&dev->kobj,
2250 it87_attributes_fan_beep[i],
2252 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2254 fan_beep_need_rw = 0;
2258 if (enable_pwm_interface) {
2259 for (i = 0; i < 3; i++) {
2260 if (sio_data->skip_pwm & (1 << i))
2262 err = sysfs_create_group(&dev->kobj,
2263 &it87_group_pwm[i]);
2267 if (!has_old_autopwm(data))
2269 err = sysfs_create_group(&dev->kobj,
2270 &it87_group_autopwm[i]);
2276 if (!sio_data->skip_vid) {
2277 data->vrm = vid_which_vrm();
2278 /* VID reading from Super-I/O config space if available */
2279 data->vid = sio_data->vid_value;
2280 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2285 /* Export labels for internal sensors */
2286 for (i = 0; i < 4; i++) {
2287 if (!(sio_data->internal & (1 << i)))
2289 err = sysfs_create_file(&dev->kobj,
2290 it87_attributes_label[i]);
2295 data->hwmon_dev = hwmon_device_register(dev);
2296 if (IS_ERR(data->hwmon_dev)) {
2297 err = PTR_ERR(data->hwmon_dev);
2304 it87_remove_files(dev);
2308 static int it87_remove(struct platform_device *pdev)
2310 struct it87_data *data = platform_get_drvdata(pdev);
2312 hwmon_device_unregister(data->hwmon_dev);
2313 it87_remove_files(&pdev->dev);
2319 * Must be called with data->update_lock held, except during initialization.
2320 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2321 * would slow down the IT87 access and should not be necessary.
2323 static int it87_read_value(struct it87_data *data, u8 reg)
2325 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2326 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2330 * Must be called with data->update_lock held, except during initialization.
2331 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2332 * would slow down the IT87 access and should not be necessary.
2334 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2336 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2337 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2340 /* Return 1 if and only if the PWM interface is safe to use */
2341 static int it87_check_pwm(struct device *dev)
2343 struct it87_data *data = dev_get_drvdata(dev);
2345 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2346 * and polarity set to active low is sign that this is the case so we
2347 * disable pwm control to protect the user.
2349 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2350 if ((tmp & 0x87) == 0) {
2351 if (fix_pwm_polarity) {
2353 * The user asks us to attempt a chip reconfiguration.
2354 * This means switching to active high polarity and
2355 * inverting all fan speed values.
2360 for (i = 0; i < 3; i++)
2361 pwm[i] = it87_read_value(data,
2365 * If any fan is in automatic pwm mode, the polarity
2366 * might be correct, as suspicious as it seems, so we
2367 * better don't change anything (but still disable the
2370 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2372 "Reconfiguring PWM to active high polarity\n");
2373 it87_write_value(data, IT87_REG_FAN_CTL,
2375 for (i = 0; i < 3; i++)
2376 it87_write_value(data,
2383 "PWM configuration is too broken to be fixed\n");
2387 "Detected broken BIOS defaults, disabling PWM interface\n");
2389 } else if (fix_pwm_polarity) {
2391 "PWM configuration looks sane, won't touch\n");
2397 /* Called when we have found a new IT87. */
2398 static void it87_init_device(struct platform_device *pdev)
2400 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2401 struct it87_data *data = platform_get_drvdata(pdev);
2406 * For each PWM channel:
2407 * - If it is in automatic mode, setting to manual mode should set
2408 * the fan to full speed by default.
2409 * - If it is in manual mode, we need a mapping to temperature
2410 * channels to use when later setting to automatic mode later.
2411 * Use a 1:1 mapping by default (we are clueless.)
2412 * In both cases, the value can (and should) be changed by the user
2413 * prior to switching to a different mode.
2414 * Note that this is no longer needed for the IT8721F and later, as
2415 * these have separate registers for the temperature mapping and the
2416 * manual duty cycle.
2418 for (i = 0; i < 3; i++) {
2419 data->pwm_temp_map[i] = i;
2420 data->pwm_duty[i] = 0x7f; /* Full speed */
2421 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2425 * Some chips seem to have default value 0xff for all limit
2426 * registers. For low voltage limits it makes no sense and triggers
2427 * alarms, so change to 0 instead. For high temperature limits, it
2428 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2429 * but is still confusing, so change to 127 degrees C.
2431 for (i = 0; i < 8; i++) {
2432 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2434 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2436 for (i = 0; i < 3; i++) {
2437 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2439 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2443 * Temperature channels are not forcibly enabled, as they can be
2444 * set to two different sensor types and we can't guess which one
2445 * is correct for a given system. These channels can be enabled at
2446 * run-time through the temp{1-3}_type sysfs accessors if needed.
2449 /* Check if voltage monitors are reset manually or by some reason */
2450 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2451 if ((tmp & 0xff) == 0) {
2452 /* Enable all voltage monitors */
2453 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2456 /* Check if tachometers are reset manually or by some reason */
2457 mask = 0x70 & ~(sio_data->skip_fan << 4);
2458 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2459 if ((data->fan_main_ctrl & mask) == 0) {
2460 /* Enable all fan tachometers */
2461 data->fan_main_ctrl |= mask;
2462 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2463 data->fan_main_ctrl);
2465 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2467 /* Set tachometers to 16-bit mode if needed. */
2468 if (has_fan16_config(data)) {
2469 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2470 if (~tmp & 0x07 & data->has_fan) {
2472 "Setting fan1-3 to 16-bit mode\n");
2473 it87_write_value(data, IT87_REG_FAN_16BIT,
2478 /* Check for additional fans */
2479 if (has_five_fans(data)) {
2480 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2482 data->has_fan |= (1 << 3); /* fan4 enabled */
2484 data->has_fan |= (1 << 4); /* fan5 enabled */
2487 /* Fan input pins may be used for alternative functions */
2488 data->has_fan &= ~sio_data->skip_fan;
2490 /* Start monitoring */
2491 it87_write_value(data, IT87_REG_CONFIG,
2492 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2493 | (update_vbat ? 0x41 : 0x01));
2496 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2498 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2499 if (has_newer_autopwm(data)) {
2500 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2501 data->pwm_duty[nr] = it87_read_value(data,
2502 IT87_REG_PWM_DUTY(nr));
2504 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2505 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2506 else /* Manual mode */
2507 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2510 if (has_old_autopwm(data)) {
2513 for (i = 0; i < 5 ; i++)
2514 data->auto_temp[nr][i] = it87_read_value(data,
2515 IT87_REG_AUTO_TEMP(nr, i));
2516 for (i = 0; i < 3 ; i++)
2517 data->auto_pwm[nr][i] = it87_read_value(data,
2518 IT87_REG_AUTO_PWM(nr, i));
2522 static struct it87_data *it87_update_device(struct device *dev)
2524 struct it87_data *data = dev_get_drvdata(dev);
2527 mutex_lock(&data->update_lock);
2529 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2533 * Cleared after each update, so reenable. Value
2534 * returned by this read will be previous value
2536 it87_write_value(data, IT87_REG_CONFIG,
2537 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2539 for (i = 0; i <= 7; i++) {
2541 it87_read_value(data, IT87_REG_VIN(i));
2543 it87_read_value(data, IT87_REG_VIN_MIN(i));
2545 it87_read_value(data, IT87_REG_VIN_MAX(i));
2547 /* in8 (battery) has no limit registers */
2548 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2549 if (data->type == it8603)
2550 data->in[9][0] = it87_read_value(data, 0x2f);
2552 for (i = 0; i < 5; i++) {
2553 /* Skip disabled fans */
2554 if (!(data->has_fan & (1 << i)))
2558 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2559 data->fan[i][0] = it87_read_value(data,
2561 /* Add high byte if in 16-bit mode */
2562 if (has_16bit_fans(data)) {
2563 data->fan[i][0] |= it87_read_value(data,
2564 IT87_REG_FANX[i]) << 8;
2565 data->fan[i][1] |= it87_read_value(data,
2566 IT87_REG_FANX_MIN[i]) << 8;
2569 for (i = 0; i < 3; i++) {
2570 if (!(data->has_temp & (1 << i)))
2573 it87_read_value(data, IT87_REG_TEMP(i));
2575 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2577 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2578 if (has_temp_offset(data))
2580 it87_read_value(data,
2581 IT87_REG_TEMP_OFFSET[i]);
2584 /* Newer chips don't have clock dividers */
2585 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2586 i = it87_read_value(data, IT87_REG_FAN_DIV);
2587 data->fan_div[0] = i & 0x07;
2588 data->fan_div[1] = (i >> 3) & 0x07;
2589 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2593 it87_read_value(data, IT87_REG_ALARM1) |
2594 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2595 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2596 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2598 data->fan_main_ctrl = it87_read_value(data,
2599 IT87_REG_FAN_MAIN_CTRL);
2600 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2601 for (i = 0; i < 3; i++)
2602 it87_update_pwm_ctrl(data, i);
2604 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2605 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
2607 * The IT8705F does not have VID capability.
2608 * The IT8718F and later don't use IT87_REG_VID for the
2611 if (data->type == it8712 || data->type == it8716) {
2612 data->vid = it87_read_value(data, IT87_REG_VID);
2614 * The older IT8712F revisions had only 5 VID pins,
2615 * but we assume it is always safe to read 6 bits.
2619 data->last_updated = jiffies;
2623 mutex_unlock(&data->update_lock);
2628 static int __init it87_device_add(unsigned short address,
2629 const struct it87_sio_data *sio_data)
2631 struct resource res = {
2632 .start = address + IT87_EC_OFFSET,
2633 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2635 .flags = IORESOURCE_IO,
2639 err = acpi_check_resource_conflict(&res);
2643 pdev = platform_device_alloc(DRVNAME, address);
2646 pr_err("Device allocation failed\n");
2650 err = platform_device_add_resources(pdev, &res, 1);
2652 pr_err("Device resource addition failed (%d)\n", err);
2653 goto exit_device_put;
2656 err = platform_device_add_data(pdev, sio_data,
2657 sizeof(struct it87_sio_data));
2659 pr_err("Platform data allocation failed\n");
2660 goto exit_device_put;
2663 err = platform_device_add(pdev);
2665 pr_err("Device addition failed (%d)\n", err);
2666 goto exit_device_put;
2672 platform_device_put(pdev);
2677 static int __init sm_it87_init(void)
2680 unsigned short isa_address = 0;
2681 struct it87_sio_data sio_data;
2683 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2684 err = it87_find(&isa_address, &sio_data);
2687 err = platform_driver_register(&it87_driver);
2691 err = it87_device_add(isa_address, &sio_data);
2693 platform_driver_unregister(&it87_driver);
2700 static void __exit sm_it87_exit(void)
2702 platform_device_unregister(pdev);
2703 platform_driver_unregister(&it87_driver);
2707 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
2708 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2709 module_param(update_vbat, bool, 0);
2710 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2711 module_param(fix_pwm_polarity, bool, 0);
2712 MODULE_PARM_DESC(fix_pwm_polarity,
2713 "Force PWM polarity to active high (DANGEROUS)");
2714 MODULE_LICENSE("GPL");
2716 module_init(sm_it87_init);
2717 module_exit(sm_it87_exit);