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 * IT8620E Super I/O chip w/LPC interface
15 * IT8623E Super I/O chip w/LPC interface
16 * IT8705F Super I/O chip w/LPC interface
17 * IT8712F Super I/O chip w/LPC interface
18 * IT8716F Super I/O chip w/LPC interface
19 * IT8718F Super I/O chip w/LPC interface
20 * IT8720F Super I/O chip w/LPC interface
21 * IT8721F Super I/O chip w/LPC interface
22 * IT8726F Super I/O chip w/LPC interface
23 * IT8728F Super I/O chip w/LPC interface
24 * IT8758E Super I/O chip w/LPC interface
25 * IT8771E Super I/O chip w/LPC interface
26 * IT8772E Super I/O chip w/LPC interface
27 * IT8781F Super I/O chip w/LPC interface
28 * IT8782F Super I/O chip w/LPC interface
29 * IT8783E/F Super I/O chip w/LPC interface
30 * IT8786E Super I/O chip w/LPC interface
31 * IT8790E Super I/O chip w/LPC interface
32 * Sis950 A clone of the IT8705F
34 * Copyright (C) 2001 Chris Gauthron
35 * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/jiffies.h>
58 #include <linux/platform_device.h>
59 #include <linux/hwmon.h>
60 #include <linux/hwmon-sysfs.h>
61 #include <linux/hwmon-vid.h>
62 #include <linux/err.h>
63 #include <linux/mutex.h>
64 #include <linux/sysfs.h>
65 #include <linux/string.h>
66 #include <linux/dmi.h>
67 #include <linux/acpi.h>
71 #define DRVNAME "it87"
73 enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8771,
74 it8772, it8781, it8782, it8783, it8786, it8603, it8620, it8790 };
76 static unsigned short force_id;
77 module_param(force_id, ushort, 0);
78 MODULE_PARM_DESC(force_id, "Override the detected device ID");
80 static struct platform_device *pdev;
82 #define REG 0x2e /* The register to read/write */
83 #define DEV 0x07 /* Register: Logical device select */
84 #define VAL 0x2f /* The value to read/write */
85 #define PME 0x04 /* The device with the fan registers in it */
87 /* The device with the IT8718F/IT8720F VID value in it */
90 #define DEVID 0x20 /* Register: Device ID */
91 #define DEVREV 0x22 /* Register: Device Revision */
93 static inline int superio_inb(int reg)
99 static inline void superio_outb(int reg, int val)
105 static int superio_inw(int reg)
115 static inline void superio_select(int ldn)
121 static inline int superio_enter(void)
124 * Try to reserve REG and REG + 1 for exclusive access.
126 if (!request_muxed_region(REG, 2, DRVNAME))
136 static inline void superio_exit(void)
140 release_region(REG, 2);
143 /* Logical device 4 registers */
144 #define IT8712F_DEVID 0x8712
145 #define IT8705F_DEVID 0x8705
146 #define IT8716F_DEVID 0x8716
147 #define IT8718F_DEVID 0x8718
148 #define IT8720F_DEVID 0x8720
149 #define IT8721F_DEVID 0x8721
150 #define IT8726F_DEVID 0x8726
151 #define IT8728F_DEVID 0x8728
152 #define IT8771E_DEVID 0x8771
153 #define IT8772E_DEVID 0x8772
154 #define IT8781F_DEVID 0x8781
155 #define IT8782F_DEVID 0x8782
156 #define IT8783E_DEVID 0x8783
157 #define IT8786E_DEVID 0x8786
158 #define IT8603E_DEVID 0x8603
159 #define IT8620E_DEVID 0x8620
160 #define IT8623E_DEVID 0x8623
161 #define IT8790E_DEVID 0x8790
162 #define IT87_ACT_REG 0x30
163 #define IT87_BASE_REG 0x60
165 /* Logical device 7 registers (IT8712F and later) */
166 #define IT87_SIO_GPIO1_REG 0x25
167 #define IT87_SIO_GPIO3_REG 0x27
168 #define IT87_SIO_GPIO5_REG 0x29
169 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
170 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
171 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
172 #define IT87_SIO_VID_REG 0xfc /* VID value */
173 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
175 /* Update battery voltage after every reading if true */
176 static bool update_vbat;
178 /* Not all BIOSes properly configure the PWM registers */
179 static bool fix_pwm_polarity;
181 /* Many IT87 constants specified below */
183 /* Length of ISA address segment */
184 #define IT87_EXTENT 8
186 /* Length of ISA address segment for Environmental Controller */
187 #define IT87_EC_EXTENT 2
189 /* Offset of EC registers from ISA base address */
190 #define IT87_EC_OFFSET 5
192 /* Where are the ISA address/data registers relative to the EC base address */
193 #define IT87_ADDR_REG_OFFSET 0
194 #define IT87_DATA_REG_OFFSET 1
196 /*----- The IT87 registers -----*/
198 #define IT87_REG_CONFIG 0x00
200 #define IT87_REG_ALARM1 0x01
201 #define IT87_REG_ALARM2 0x02
202 #define IT87_REG_ALARM3 0x03
205 * The IT8718F and IT8720F have the VID value in a different register, in
206 * Super-I/O configuration space.
208 #define IT87_REG_VID 0x0a
210 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
211 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
214 #define IT87_REG_FAN_DIV 0x0b
215 #define IT87_REG_FAN_16BIT 0x0c
217 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
219 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
220 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
221 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
222 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
223 static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
225 #define IT87_REG_FAN_MAIN_CTRL 0x13
226 #define IT87_REG_FAN_CTL 0x14
227 #define IT87_REG_PWM(nr) (0x15 + (nr))
228 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
230 #define IT87_REG_VIN(nr) (0x20 + (nr))
231 #define IT87_REG_TEMP(nr) (0x29 + (nr))
233 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
234 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
235 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
236 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
238 #define IT87_REG_VIN_ENABLE 0x50
239 #define IT87_REG_TEMP_ENABLE 0x51
240 #define IT87_REG_TEMP_EXTRA 0x55
241 #define IT87_REG_BEEP_ENABLE 0x5c
243 #define IT87_REG_CHIPID 0x58
245 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
246 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
248 struct it87_devices {
255 #define FEAT_12MV_ADC (1 << 0)
256 #define FEAT_NEWER_AUTOPWM (1 << 1)
257 #define FEAT_OLD_AUTOPWM (1 << 2)
258 #define FEAT_16BIT_FANS (1 << 3)
259 #define FEAT_TEMP_OFFSET (1 << 4)
260 #define FEAT_TEMP_PECI (1 << 5)
261 #define FEAT_TEMP_OLD_PECI (1 << 6)
262 #define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
263 #define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
264 #define FEAT_SIX_FANS (1 << 9) /* Supports six fans */
265 #define FEAT_VID (1 << 10) /* Set if chip supports VID */
267 static const struct it87_devices it87_devices[] = {
270 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
274 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
275 /* may need to overwrite */
279 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
280 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
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_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
291 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
292 .old_peci_mask = 0x4,
296 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
297 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
298 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS,
300 .old_peci_mask = 0x02, /* Actually reports PCH */
304 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
305 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS,
310 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
311 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
312 /* PECI: guesswork */
314 /* 16 bit fans (OHM) */
315 /* three fans, always 16 bit (guesswork) */
320 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
321 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
322 /* PECI (coreboot) */
323 /* 12mV ADC (HWSensors4, OHM) */
324 /* 16 bit fans (HWSensors4, OHM) */
325 /* three fans, always 16 bit (datasheet) */
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_16BIT_FANS | FEAT_TEMP_OFFSET
343 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG,
344 .old_peci_mask = 0x4,
348 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
349 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
354 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
355 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
360 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
361 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS,
366 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
367 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI,
372 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
373 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
374 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
375 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
376 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
377 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
378 ((data)->peci_mask & (1 << nr)))
379 #define has_temp_old_peci(data, nr) \
380 (((data)->features & FEAT_TEMP_OLD_PECI) && \
381 ((data)->old_peci_mask & (1 << nr)))
382 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
383 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
385 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
386 #define has_vid(data) ((data)->features & FEAT_VID)
388 struct it87_sio_data {
390 /* Values read from Super-I/O config space */
394 u8 internal; /* Internal sensors can be labeled */
395 /* Features skipped based on config or DMI */
404 * For each registered chip, we need to keep some data in memory.
405 * The structure is dynamically allocated.
408 struct device *hwmon_dev;
416 struct mutex update_lock;
417 char valid; /* !=0 if following fields are valid */
418 unsigned long last_updated; /* In jiffies */
420 u16 in_scaled; /* Internal voltage sensors are scaled */
421 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
422 u8 has_fan; /* Bitfield, fans enabled */
423 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
424 u8 has_temp; /* Bitfield, temp sensors enabled */
425 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
426 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
427 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
428 u8 fan_div[3]; /* Register encoding, shifted right */
429 u8 vid; /* Register encoding, combined */
431 u32 alarms; /* Register encoding, combined */
432 u8 beeps; /* Register encoding */
433 u8 fan_main_ctrl; /* Register value */
434 u8 fan_ctl; /* Register value */
437 * The following 3 arrays correspond to the same registers up to
438 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
439 * 7, and we want to preserve settings on mode changes, so we have
440 * to track all values separately.
441 * Starting with the IT8721F, the manual PWM duty cycles are stored
442 * in separate registers (8-bit values), so the separate tracking
443 * is no longer needed, but it is still done to keep the driver
446 u8 pwm_ctrl[3]; /* Register value */
447 u8 pwm_duty[3]; /* Manual PWM value set by user */
448 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
450 /* Automatic fan speed control registers */
451 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
452 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
455 static int adc_lsb(const struct it87_data *data, int nr)
457 int lsb = has_12mv_adc(data) ? 12 : 16;
458 if (data->in_scaled & (1 << nr))
463 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
465 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
466 return clamp_val(val, 0, 255);
469 static int in_from_reg(const struct it87_data *data, int nr, int val)
471 return val * adc_lsb(data, nr);
474 static inline u8 FAN_TO_REG(long rpm, int div)
478 rpm = clamp_val(rpm, 1, 1000000);
479 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
482 static inline u16 FAN16_TO_REG(long rpm)
486 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
489 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
490 1350000 / ((val) * (div)))
491 /* The divider is fixed to 2 in 16-bit mode */
492 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
493 1350000 / ((val) * 2))
495 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
496 ((val) + 500) / 1000), -128, 127))
497 #define TEMP_FROM_REG(val) ((val) * 1000)
499 static u8 pwm_to_reg(const struct it87_data *data, long val)
501 if (has_newer_autopwm(data))
507 static int pwm_from_reg(const struct it87_data *data, u8 reg)
509 if (has_newer_autopwm(data))
512 return (reg & 0x7f) << 1;
516 static int DIV_TO_REG(int val)
519 while (answer < 7 && (val >>= 1))
523 #define DIV_FROM_REG(val) (1 << (val))
525 static const unsigned int pwm_freq[8] = {
536 static int it87_probe(struct platform_device *pdev);
537 static int it87_remove(struct platform_device *pdev);
539 static int it87_read_value(struct it87_data *data, u8 reg);
540 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
541 static struct it87_data *it87_update_device(struct device *dev);
542 static int it87_check_pwm(struct device *dev);
543 static void it87_init_device(struct platform_device *pdev);
546 static struct platform_driver it87_driver = {
551 .remove = it87_remove,
554 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
557 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
559 int index = sattr->index;
561 struct it87_data *data = it87_update_device(dev);
562 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
565 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
566 const char *buf, size_t count)
568 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
570 int index = sattr->index;
572 struct it87_data *data = dev_get_drvdata(dev);
575 if (kstrtoul(buf, 10, &val) < 0)
578 mutex_lock(&data->update_lock);
579 data->in[nr][index] = in_to_reg(data, nr, val);
580 it87_write_value(data,
581 index == 1 ? IT87_REG_VIN_MIN(nr)
582 : IT87_REG_VIN_MAX(nr),
583 data->in[nr][index]);
584 mutex_unlock(&data->update_lock);
588 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
589 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
591 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
594 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
595 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
597 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
600 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
601 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
603 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
606 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
607 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
609 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
612 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
613 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
615 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
618 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
619 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
621 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
624 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
625 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
627 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
630 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
631 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
633 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
636 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
637 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
640 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
643 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
645 int index = sattr->index;
646 struct it87_data *data = it87_update_device(dev);
648 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
651 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
652 const char *buf, size_t count)
654 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
656 int index = sattr->index;
657 struct it87_data *data = dev_get_drvdata(dev);
661 if (kstrtol(buf, 10, &val) < 0)
664 mutex_lock(&data->update_lock);
669 reg = IT87_REG_TEMP_LOW(nr);
672 reg = IT87_REG_TEMP_HIGH(nr);
675 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
676 if (!(regval & 0x80)) {
678 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
681 reg = IT87_REG_TEMP_OFFSET[nr];
685 data->temp[nr][index] = TEMP_TO_REG(val);
686 it87_write_value(data, reg, data->temp[nr][index]);
687 mutex_unlock(&data->update_lock);
691 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
692 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
694 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
696 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
698 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
699 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
701 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
703 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
705 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
706 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
708 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
710 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
713 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
716 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
717 int nr = sensor_attr->index;
718 struct it87_data *data = it87_update_device(dev);
719 u8 reg = data->sensor; /* In case value is updated while used */
720 u8 extra = data->extra;
722 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
723 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
724 return sprintf(buf, "6\n"); /* Intel PECI */
726 return sprintf(buf, "3\n"); /* thermal diode */
728 return sprintf(buf, "4\n"); /* thermistor */
729 return sprintf(buf, "0\n"); /* disabled */
732 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
733 const char *buf, size_t count)
735 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
736 int nr = sensor_attr->index;
738 struct it87_data *data = dev_get_drvdata(dev);
742 if (kstrtol(buf, 10, &val) < 0)
745 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
748 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
750 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
751 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
753 if (val == 2) { /* backwards compatibility */
755 "Sensor type 2 is deprecated, please use 4 instead\n");
758 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
763 else if (has_temp_peci(data, nr) && val == 6)
764 reg |= (nr + 1) << 6;
765 else if (has_temp_old_peci(data, nr) && val == 6)
770 mutex_lock(&data->update_lock);
773 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
774 if (has_temp_old_peci(data, nr))
775 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
776 data->valid = 0; /* Force cache refresh */
777 mutex_unlock(&data->update_lock);
781 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
783 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
785 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
790 static int pwm_mode(const struct it87_data *data, int nr)
792 int ctrl = data->fan_main_ctrl & (1 << nr);
794 if (ctrl == 0 && data->type != it8603) /* Full speed */
796 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
798 else /* Manual mode */
802 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
805 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
807 int index = sattr->index;
809 struct it87_data *data = it87_update_device(dev);
811 speed = has_16bit_fans(data) ?
812 FAN16_FROM_REG(data->fan[nr][index]) :
813 FAN_FROM_REG(data->fan[nr][index],
814 DIV_FROM_REG(data->fan_div[nr]));
815 return sprintf(buf, "%d\n", speed);
818 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
821 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
822 int nr = sensor_attr->index;
824 struct it87_data *data = it87_update_device(dev);
825 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
827 static ssize_t show_pwm_enable(struct device *dev,
828 struct device_attribute *attr, char *buf)
830 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
831 int nr = sensor_attr->index;
833 struct it87_data *data = it87_update_device(dev);
834 return sprintf(buf, "%d\n", pwm_mode(data, nr));
836 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
839 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
840 int nr = sensor_attr->index;
842 struct it87_data *data = it87_update_device(dev);
843 return sprintf(buf, "%d\n",
844 pwm_from_reg(data, data->pwm_duty[nr]));
846 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
849 struct it87_data *data = it87_update_device(dev);
850 int index = (data->fan_ctl >> 4) & 0x07;
852 return sprintf(buf, "%u\n", pwm_freq[index]);
855 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
856 const char *buf, size_t count)
858 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
860 int index = sattr->index;
862 struct it87_data *data = dev_get_drvdata(dev);
866 if (kstrtol(buf, 10, &val) < 0)
869 mutex_lock(&data->update_lock);
871 if (has_16bit_fans(data)) {
872 data->fan[nr][index] = FAN16_TO_REG(val);
873 it87_write_value(data, IT87_REG_FAN_MIN[nr],
874 data->fan[nr][index] & 0xff);
875 it87_write_value(data, IT87_REG_FANX_MIN[nr],
876 data->fan[nr][index] >> 8);
878 reg = it87_read_value(data, IT87_REG_FAN_DIV);
881 data->fan_div[nr] = reg & 0x07;
884 data->fan_div[nr] = (reg >> 3) & 0x07;
887 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
890 data->fan[nr][index] =
891 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
892 it87_write_value(data, IT87_REG_FAN_MIN[nr],
893 data->fan[nr][index]);
896 mutex_unlock(&data->update_lock);
900 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
901 const char *buf, size_t count)
903 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
904 int nr = sensor_attr->index;
906 struct it87_data *data = dev_get_drvdata(dev);
911 if (kstrtoul(buf, 10, &val) < 0)
914 mutex_lock(&data->update_lock);
915 old = it87_read_value(data, IT87_REG_FAN_DIV);
917 /* Save fan min limit */
918 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
923 data->fan_div[nr] = DIV_TO_REG(val);
927 data->fan_div[nr] = 1;
929 data->fan_div[nr] = 3;
932 val |= (data->fan_div[0] & 0x07);
933 val |= (data->fan_div[1] & 0x07) << 3;
934 if (data->fan_div[2] == 3)
936 it87_write_value(data, IT87_REG_FAN_DIV, val);
938 /* Restore fan min limit */
939 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
940 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
942 mutex_unlock(&data->update_lock);
946 /* Returns 0 if OK, -EINVAL otherwise */
947 static int check_trip_points(struct device *dev, int nr)
949 const struct it87_data *data = dev_get_drvdata(dev);
952 if (has_old_autopwm(data)) {
953 for (i = 0; i < 3; i++) {
954 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
957 for (i = 0; i < 2; i++) {
958 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
965 "Inconsistent trip points, not switching to automatic mode\n");
966 dev_err(dev, "Adjust the trip points and try again\n");
971 static ssize_t set_pwm_enable(struct device *dev,
972 struct device_attribute *attr, const char *buf, size_t count)
974 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
975 int nr = sensor_attr->index;
977 struct it87_data *data = dev_get_drvdata(dev);
980 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
983 /* Check trip points before switching to automatic mode */
985 if (check_trip_points(dev, nr) < 0)
989 /* IT8603E does not have on/off mode */
990 if (val == 0 && data->type == it8603)
993 mutex_lock(&data->update_lock);
997 /* make sure the fan is on when in on/off mode */
998 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
999 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
1000 /* set on/off mode */
1001 data->fan_main_ctrl &= ~(1 << nr);
1002 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1003 data->fan_main_ctrl);
1005 if (val == 1) /* Manual mode */
1006 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1007 data->pwm_temp_map[nr] :
1009 else /* Automatic mode */
1010 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1011 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1013 if (data->type != it8603) {
1014 /* set SmartGuardian mode */
1015 data->fan_main_ctrl |= (1 << nr);
1016 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1017 data->fan_main_ctrl);
1021 mutex_unlock(&data->update_lock);
1024 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1025 const char *buf, size_t count)
1027 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1028 int nr = sensor_attr->index;
1030 struct it87_data *data = dev_get_drvdata(dev);
1033 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1036 mutex_lock(&data->update_lock);
1037 if (has_newer_autopwm(data)) {
1039 * If we are in automatic mode, the PWM duty cycle register
1040 * is read-only so we can't write the value.
1042 if (data->pwm_ctrl[nr] & 0x80) {
1043 mutex_unlock(&data->update_lock);
1046 data->pwm_duty[nr] = pwm_to_reg(data, val);
1047 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
1048 data->pwm_duty[nr]);
1050 data->pwm_duty[nr] = pwm_to_reg(data, val);
1052 * If we are in manual mode, write the duty cycle immediately;
1053 * otherwise, just store it for later use.
1055 if (!(data->pwm_ctrl[nr] & 0x80)) {
1056 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1057 it87_write_value(data, IT87_REG_PWM(nr),
1058 data->pwm_ctrl[nr]);
1061 mutex_unlock(&data->update_lock);
1064 static ssize_t set_pwm_freq(struct device *dev,
1065 struct device_attribute *attr, const char *buf, size_t count)
1067 struct it87_data *data = dev_get_drvdata(dev);
1071 if (kstrtoul(buf, 10, &val) < 0)
1074 /* Search for the nearest available frequency */
1075 for (i = 0; i < 7; i++) {
1076 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1080 mutex_lock(&data->update_lock);
1081 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1082 data->fan_ctl |= i << 4;
1083 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1084 mutex_unlock(&data->update_lock);
1088 static ssize_t show_pwm_temp_map(struct device *dev,
1089 struct device_attribute *attr, char *buf)
1091 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1092 int nr = sensor_attr->index;
1094 struct it87_data *data = it87_update_device(dev);
1097 if (data->pwm_temp_map[nr] < 3)
1098 map = 1 << data->pwm_temp_map[nr];
1100 map = 0; /* Should never happen */
1101 return sprintf(buf, "%d\n", map);
1103 static ssize_t set_pwm_temp_map(struct device *dev,
1104 struct device_attribute *attr, const char *buf, size_t count)
1106 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1107 int nr = sensor_attr->index;
1109 struct it87_data *data = dev_get_drvdata(dev);
1114 * This check can go away if we ever support automatic fan speed
1115 * control on newer chips.
1117 if (!has_old_autopwm(data)) {
1118 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1122 if (kstrtol(buf, 10, &val) < 0)
1139 mutex_lock(&data->update_lock);
1140 data->pwm_temp_map[nr] = reg;
1142 * If we are in automatic mode, write the temp mapping immediately;
1143 * otherwise, just store it for later use.
1145 if (data->pwm_ctrl[nr] & 0x80) {
1146 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1147 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1149 mutex_unlock(&data->update_lock);
1153 static ssize_t show_auto_pwm(struct device *dev,
1154 struct device_attribute *attr, char *buf)
1156 struct it87_data *data = it87_update_device(dev);
1157 struct sensor_device_attribute_2 *sensor_attr =
1158 to_sensor_dev_attr_2(attr);
1159 int nr = sensor_attr->nr;
1160 int point = sensor_attr->index;
1162 return sprintf(buf, "%d\n",
1163 pwm_from_reg(data, data->auto_pwm[nr][point]));
1166 static ssize_t set_auto_pwm(struct device *dev,
1167 struct device_attribute *attr, const char *buf, size_t count)
1169 struct it87_data *data = dev_get_drvdata(dev);
1170 struct sensor_device_attribute_2 *sensor_attr =
1171 to_sensor_dev_attr_2(attr);
1172 int nr = sensor_attr->nr;
1173 int point = sensor_attr->index;
1176 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1179 mutex_lock(&data->update_lock);
1180 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1181 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1182 data->auto_pwm[nr][point]);
1183 mutex_unlock(&data->update_lock);
1187 static ssize_t show_auto_temp(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1190 struct it87_data *data = it87_update_device(dev);
1191 struct sensor_device_attribute_2 *sensor_attr =
1192 to_sensor_dev_attr_2(attr);
1193 int nr = sensor_attr->nr;
1194 int point = sensor_attr->index;
1196 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1199 static ssize_t set_auto_temp(struct device *dev,
1200 struct device_attribute *attr, const char *buf, size_t count)
1202 struct it87_data *data = dev_get_drvdata(dev);
1203 struct sensor_device_attribute_2 *sensor_attr =
1204 to_sensor_dev_attr_2(attr);
1205 int nr = sensor_attr->nr;
1206 int point = sensor_attr->index;
1209 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1212 mutex_lock(&data->update_lock);
1213 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1214 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1215 data->auto_temp[nr][point]);
1216 mutex_unlock(&data->update_lock);
1220 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1221 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1223 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1226 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1227 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1229 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1232 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1233 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1235 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1238 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1239 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1242 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1243 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1246 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1247 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1250 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1251 show_pwm_enable, set_pwm_enable, 0);
1252 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1253 static DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq, set_pwm_freq);
1254 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1255 show_pwm_temp_map, set_pwm_temp_map, 0);
1256 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1257 show_auto_pwm, set_auto_pwm, 0, 0);
1258 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1259 show_auto_pwm, set_auto_pwm, 0, 1);
1260 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1261 show_auto_pwm, set_auto_pwm, 0, 2);
1262 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1263 show_auto_pwm, NULL, 0, 3);
1264 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1265 show_auto_temp, set_auto_temp, 0, 1);
1266 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1267 show_auto_temp, set_auto_temp, 0, 0);
1268 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1269 show_auto_temp, set_auto_temp, 0, 2);
1270 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1271 show_auto_temp, set_auto_temp, 0, 3);
1272 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1273 show_auto_temp, set_auto_temp, 0, 4);
1275 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1276 show_pwm_enable, set_pwm_enable, 1);
1277 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1278 static DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, NULL);
1279 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1280 show_pwm_temp_map, set_pwm_temp_map, 1);
1281 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1282 show_auto_pwm, set_auto_pwm, 1, 0);
1283 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1284 show_auto_pwm, set_auto_pwm, 1, 1);
1285 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1286 show_auto_pwm, set_auto_pwm, 1, 2);
1287 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1288 show_auto_pwm, NULL, 1, 3);
1289 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1290 show_auto_temp, set_auto_temp, 1, 1);
1291 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1292 show_auto_temp, set_auto_temp, 1, 0);
1293 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1294 show_auto_temp, set_auto_temp, 1, 2);
1295 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1296 show_auto_temp, set_auto_temp, 1, 3);
1297 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1298 show_auto_temp, set_auto_temp, 1, 4);
1300 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1301 show_pwm_enable, set_pwm_enable, 2);
1302 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1303 static DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL);
1304 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1305 show_pwm_temp_map, set_pwm_temp_map, 2);
1306 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1307 show_auto_pwm, set_auto_pwm, 2, 0);
1308 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1309 show_auto_pwm, set_auto_pwm, 2, 1);
1310 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1311 show_auto_pwm, set_auto_pwm, 2, 2);
1312 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1313 show_auto_pwm, NULL, 2, 3);
1314 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1315 show_auto_temp, set_auto_temp, 2, 1);
1316 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1317 show_auto_temp, set_auto_temp, 2, 0);
1318 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1319 show_auto_temp, set_auto_temp, 2, 2);
1320 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1321 show_auto_temp, set_auto_temp, 2, 3);
1322 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1323 show_auto_temp, set_auto_temp, 2, 4);
1326 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1329 struct it87_data *data = it87_update_device(dev);
1330 return sprintf(buf, "%u\n", data->alarms);
1332 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1334 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1337 int bitnr = to_sensor_dev_attr(attr)->index;
1338 struct it87_data *data = it87_update_device(dev);
1339 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1342 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1343 *attr, const char *buf, size_t count)
1345 struct it87_data *data = dev_get_drvdata(dev);
1349 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1352 mutex_lock(&data->update_lock);
1353 config = it87_read_value(data, IT87_REG_CONFIG);
1358 it87_write_value(data, IT87_REG_CONFIG, config);
1359 /* Invalidate cache to force re-read */
1362 mutex_unlock(&data->update_lock);
1367 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1368 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1369 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1370 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1371 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1372 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1373 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1374 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1375 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1376 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1377 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1378 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1379 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1380 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1381 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1382 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1383 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1384 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1385 show_alarm, clear_intrusion, 4);
1387 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1390 int bitnr = to_sensor_dev_attr(attr)->index;
1391 struct it87_data *data = it87_update_device(dev);
1392 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1394 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1395 const char *buf, size_t count)
1397 int bitnr = to_sensor_dev_attr(attr)->index;
1398 struct it87_data *data = dev_get_drvdata(dev);
1401 if (kstrtol(buf, 10, &val) < 0
1402 || (val != 0 && val != 1))
1405 mutex_lock(&data->update_lock);
1406 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1408 data->beeps |= (1 << bitnr);
1410 data->beeps &= ~(1 << bitnr);
1411 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1412 mutex_unlock(&data->update_lock);
1416 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1417 show_beep, set_beep, 1);
1418 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1419 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1420 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1421 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1422 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1423 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1424 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1425 /* fanX_beep writability is set later */
1426 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1427 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1428 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1429 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1430 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1431 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1432 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1433 show_beep, set_beep, 2);
1434 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1435 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1437 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1440 struct it87_data *data = dev_get_drvdata(dev);
1441 return sprintf(buf, "%u\n", data->vrm);
1443 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1444 const char *buf, size_t count)
1446 struct it87_data *data = dev_get_drvdata(dev);
1449 if (kstrtoul(buf, 10, &val) < 0)
1456 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1458 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1461 struct it87_data *data = it87_update_device(dev);
1462 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1464 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1466 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1469 static const char * const labels[] = {
1474 static const char * const labels_it8721[] = {
1479 struct it87_data *data = dev_get_drvdata(dev);
1480 int nr = to_sensor_dev_attr(attr)->index;
1482 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1485 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1486 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1487 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1488 /* special AVCC3 IT8603E in9 */
1489 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1491 static ssize_t show_name(struct device *dev, struct device_attribute
1492 *devattr, char *buf)
1494 struct it87_data *data = dev_get_drvdata(dev);
1495 return sprintf(buf, "%s\n", data->name);
1497 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1499 static struct attribute *it87_attributes_in[10][5] = {
1501 &sensor_dev_attr_in0_input.dev_attr.attr,
1502 &sensor_dev_attr_in0_min.dev_attr.attr,
1503 &sensor_dev_attr_in0_max.dev_attr.attr,
1504 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1507 &sensor_dev_attr_in1_input.dev_attr.attr,
1508 &sensor_dev_attr_in1_min.dev_attr.attr,
1509 &sensor_dev_attr_in1_max.dev_attr.attr,
1510 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1513 &sensor_dev_attr_in2_input.dev_attr.attr,
1514 &sensor_dev_attr_in2_min.dev_attr.attr,
1515 &sensor_dev_attr_in2_max.dev_attr.attr,
1516 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1519 &sensor_dev_attr_in3_input.dev_attr.attr,
1520 &sensor_dev_attr_in3_min.dev_attr.attr,
1521 &sensor_dev_attr_in3_max.dev_attr.attr,
1522 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1525 &sensor_dev_attr_in4_input.dev_attr.attr,
1526 &sensor_dev_attr_in4_min.dev_attr.attr,
1527 &sensor_dev_attr_in4_max.dev_attr.attr,
1528 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1531 &sensor_dev_attr_in5_input.dev_attr.attr,
1532 &sensor_dev_attr_in5_min.dev_attr.attr,
1533 &sensor_dev_attr_in5_max.dev_attr.attr,
1534 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1537 &sensor_dev_attr_in6_input.dev_attr.attr,
1538 &sensor_dev_attr_in6_min.dev_attr.attr,
1539 &sensor_dev_attr_in6_max.dev_attr.attr,
1540 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1543 &sensor_dev_attr_in7_input.dev_attr.attr,
1544 &sensor_dev_attr_in7_min.dev_attr.attr,
1545 &sensor_dev_attr_in7_max.dev_attr.attr,
1546 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1549 &sensor_dev_attr_in8_input.dev_attr.attr,
1552 &sensor_dev_attr_in9_input.dev_attr.attr,
1556 static const struct attribute_group it87_group_in[10] = {
1557 { .attrs = it87_attributes_in[0] },
1558 { .attrs = it87_attributes_in[1] },
1559 { .attrs = it87_attributes_in[2] },
1560 { .attrs = it87_attributes_in[3] },
1561 { .attrs = it87_attributes_in[4] },
1562 { .attrs = it87_attributes_in[5] },
1563 { .attrs = it87_attributes_in[6] },
1564 { .attrs = it87_attributes_in[7] },
1565 { .attrs = it87_attributes_in[8] },
1566 { .attrs = it87_attributes_in[9] },
1569 static struct attribute *it87_attributes_temp[3][6] = {
1571 &sensor_dev_attr_temp1_input.dev_attr.attr,
1572 &sensor_dev_attr_temp1_max.dev_attr.attr,
1573 &sensor_dev_attr_temp1_min.dev_attr.attr,
1574 &sensor_dev_attr_temp1_type.dev_attr.attr,
1575 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1578 &sensor_dev_attr_temp2_input.dev_attr.attr,
1579 &sensor_dev_attr_temp2_max.dev_attr.attr,
1580 &sensor_dev_attr_temp2_min.dev_attr.attr,
1581 &sensor_dev_attr_temp2_type.dev_attr.attr,
1582 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1585 &sensor_dev_attr_temp3_input.dev_attr.attr,
1586 &sensor_dev_attr_temp3_max.dev_attr.attr,
1587 &sensor_dev_attr_temp3_min.dev_attr.attr,
1588 &sensor_dev_attr_temp3_type.dev_attr.attr,
1589 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1593 static const struct attribute_group it87_group_temp[3] = {
1594 { .attrs = it87_attributes_temp[0] },
1595 { .attrs = it87_attributes_temp[1] },
1596 { .attrs = it87_attributes_temp[2] },
1599 static struct attribute *it87_attributes_temp_offset[] = {
1600 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1601 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1602 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1605 static struct attribute *it87_attributes[] = {
1606 &dev_attr_alarms.attr,
1607 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1608 &dev_attr_name.attr,
1612 static const struct attribute_group it87_group = {
1613 .attrs = it87_attributes,
1616 static struct attribute *it87_attributes_in_beep[] = {
1617 &sensor_dev_attr_in0_beep.dev_attr.attr,
1618 &sensor_dev_attr_in1_beep.dev_attr.attr,
1619 &sensor_dev_attr_in2_beep.dev_attr.attr,
1620 &sensor_dev_attr_in3_beep.dev_attr.attr,
1621 &sensor_dev_attr_in4_beep.dev_attr.attr,
1622 &sensor_dev_attr_in5_beep.dev_attr.attr,
1623 &sensor_dev_attr_in6_beep.dev_attr.attr,
1624 &sensor_dev_attr_in7_beep.dev_attr.attr,
1629 static struct attribute *it87_attributes_temp_beep[] = {
1630 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1631 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1632 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1635 static struct attribute *it87_attributes_fan[6][3+1] = { {
1636 &sensor_dev_attr_fan1_input.dev_attr.attr,
1637 &sensor_dev_attr_fan1_min.dev_attr.attr,
1638 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1641 &sensor_dev_attr_fan2_input.dev_attr.attr,
1642 &sensor_dev_attr_fan2_min.dev_attr.attr,
1643 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1646 &sensor_dev_attr_fan3_input.dev_attr.attr,
1647 &sensor_dev_attr_fan3_min.dev_attr.attr,
1648 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1651 &sensor_dev_attr_fan4_input.dev_attr.attr,
1652 &sensor_dev_attr_fan4_min.dev_attr.attr,
1653 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1656 &sensor_dev_attr_fan5_input.dev_attr.attr,
1657 &sensor_dev_attr_fan5_min.dev_attr.attr,
1658 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1661 &sensor_dev_attr_fan6_input.dev_attr.attr,
1662 &sensor_dev_attr_fan6_min.dev_attr.attr,
1663 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1667 static const struct attribute_group it87_group_fan[6] = {
1668 { .attrs = it87_attributes_fan[0] },
1669 { .attrs = it87_attributes_fan[1] },
1670 { .attrs = it87_attributes_fan[2] },
1671 { .attrs = it87_attributes_fan[3] },
1672 { .attrs = it87_attributes_fan[4] },
1673 { .attrs = it87_attributes_fan[5] },
1676 static const struct attribute *it87_attributes_fan_div[] = {
1677 &sensor_dev_attr_fan1_div.dev_attr.attr,
1678 &sensor_dev_attr_fan2_div.dev_attr.attr,
1679 &sensor_dev_attr_fan3_div.dev_attr.attr,
1682 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1683 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1684 &sensor_dev_attr_pwm1.dev_attr.attr,
1685 &dev_attr_pwm1_freq.attr,
1686 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1689 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1690 &sensor_dev_attr_pwm2.dev_attr.attr,
1691 &dev_attr_pwm2_freq.attr,
1692 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1695 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1696 &sensor_dev_attr_pwm3.dev_attr.attr,
1697 &dev_attr_pwm3_freq.attr,
1698 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1702 static const struct attribute_group it87_group_pwm[3] = {
1703 { .attrs = it87_attributes_pwm[0] },
1704 { .attrs = it87_attributes_pwm[1] },
1705 { .attrs = it87_attributes_pwm[2] },
1708 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1709 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1710 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1711 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1712 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1713 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1714 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1715 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1716 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1717 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1720 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1721 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1722 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1723 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1724 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1725 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1726 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1727 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1728 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1731 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1732 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1733 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1734 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1735 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1736 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1737 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1738 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1739 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1743 static const struct attribute_group it87_group_autopwm[3] = {
1744 { .attrs = it87_attributes_autopwm[0] },
1745 { .attrs = it87_attributes_autopwm[1] },
1746 { .attrs = it87_attributes_autopwm[2] },
1749 static struct attribute *it87_attributes_fan_beep[] = {
1750 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1751 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1752 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1753 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1754 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1755 &sensor_dev_attr_fan6_beep.dev_attr.attr,
1758 static struct attribute *it87_attributes_vid[] = {
1760 &dev_attr_cpu0_vid.attr,
1764 static const struct attribute_group it87_group_vid = {
1765 .attrs = it87_attributes_vid,
1768 static struct attribute *it87_attributes_label[] = {
1769 &sensor_dev_attr_in3_label.dev_attr.attr,
1770 &sensor_dev_attr_in7_label.dev_attr.attr,
1771 &sensor_dev_attr_in8_label.dev_attr.attr,
1772 &sensor_dev_attr_in9_label.dev_attr.attr,
1776 static const struct attribute_group it87_group_label = {
1777 .attrs = it87_attributes_label,
1780 /* SuperIO detection - will change isa_address if a chip is found */
1781 static int __init it87_find(unsigned short *address,
1782 struct it87_sio_data *sio_data)
1786 const char *board_vendor, *board_name;
1788 err = superio_enter();
1793 chip_type = force_id ? force_id : superio_inw(DEVID);
1795 switch (chip_type) {
1797 sio_data->type = it87;
1800 sio_data->type = it8712;
1804 sio_data->type = it8716;
1807 sio_data->type = it8718;
1810 sio_data->type = it8720;
1813 sio_data->type = it8721;
1816 sio_data->type = it8728;
1819 sio_data->type = it8771;
1822 sio_data->type = it8772;
1825 sio_data->type = it8781;
1828 sio_data->type = it8782;
1831 sio_data->type = it8783;
1834 sio_data->type = it8786;
1838 sio_data->type = it8603;
1841 sio_data->type = it8620;
1844 sio_data->type = it8790;
1846 case 0xffff: /* No device at all */
1849 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1853 superio_select(PME);
1854 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1855 pr_info("Device not activated, skipping\n");
1859 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1860 if (*address == 0) {
1861 pr_info("Base address not set, skipping\n");
1866 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1867 pr_info("Found IT%04x%c chip at 0x%x, revision %d\n", chip_type,
1868 chip_type == 0x8771 || chip_type == 0x8772 ||
1869 chip_type == 0x8786 || chip_type == 0x8603 ||
1870 chip_type == 0x8620 || chip_type == 0x8790 ? 'E' : 'F',
1871 *address, sio_data->revision);
1873 /* in8 (Vbat) is always internal */
1874 sio_data->internal = (1 << 2);
1875 /* Only the IT8603E has in9 */
1876 if (sio_data->type != it8603)
1877 sio_data->skip_in |= (1 << 9);
1879 if (!(it87_devices[sio_data->type].features & FEAT_VID))
1880 sio_data->skip_vid = 1;
1882 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1883 if (sio_data->type == it87) {
1884 /* The IT8705F has a different LD number for GPIO */
1886 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1887 } else if (sio_data->type == it8783) {
1888 int reg25, reg27, reg2a, reg2c, regef;
1890 superio_select(GPIO);
1892 reg25 = superio_inb(IT87_SIO_GPIO1_REG);
1893 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1894 reg2a = superio_inb(IT87_SIO_PINX1_REG);
1895 reg2c = superio_inb(IT87_SIO_PINX2_REG);
1896 regef = superio_inb(IT87_SIO_SPI_REG);
1898 /* Check if fan3 is there or not */
1899 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
1900 sio_data->skip_fan |= (1 << 2);
1901 if ((reg25 & (1 << 4))
1902 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
1903 sio_data->skip_pwm |= (1 << 2);
1905 /* Check if fan2 is there or not */
1906 if (reg27 & (1 << 7))
1907 sio_data->skip_fan |= (1 << 1);
1908 if (reg27 & (1 << 3))
1909 sio_data->skip_pwm |= (1 << 1);
1912 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
1913 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1916 if (reg27 & (1 << 1))
1917 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1921 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
1923 if (reg27 & (1 << 2)) {
1925 * The data sheet is a bit unclear regarding the
1926 * internal voltage divider for VCCH5V. It says
1927 * "This bit enables and switches VIN7 (pin 91) to the
1928 * internal voltage divider for VCCH5V".
1929 * This is different to other chips, where the internal
1930 * voltage divider would connect VIN7 to an internal
1931 * voltage source. Maybe that is the case here as well.
1933 * Since we don't know for sure, re-route it if that is
1934 * not the case, and ask the user to report if the
1935 * resulting voltage is sane.
1937 if (!(reg2c & (1 << 1))) {
1939 superio_outb(IT87_SIO_PINX2_REG, reg2c);
1940 pr_notice("Routing internal VCCH5V to in7.\n");
1942 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
1943 pr_notice("Please report if it displays a reasonable voltage.\n");
1946 if (reg2c & (1 << 0))
1947 sio_data->internal |= (1 << 0);
1948 if (reg2c & (1 << 1))
1949 sio_data->internal |= (1 << 1);
1951 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1952 } else if (sio_data->type == it8603) {
1955 superio_select(GPIO);
1957 reg27 = superio_inb(IT87_SIO_GPIO3_REG);
1959 /* Check if fan3 is there or not */
1960 if (reg27 & (1 << 6))
1961 sio_data->skip_pwm |= (1 << 2);
1962 if (reg27 & (1 << 7))
1963 sio_data->skip_fan |= (1 << 2);
1965 /* Check if fan2 is there or not */
1966 reg29 = superio_inb(IT87_SIO_GPIO5_REG);
1967 if (reg29 & (1 << 1))
1968 sio_data->skip_pwm |= (1 << 1);
1969 if (reg29 & (1 << 2))
1970 sio_data->skip_fan |= (1 << 1);
1972 sio_data->skip_in |= (1 << 5); /* No VIN5 */
1973 sio_data->skip_in |= (1 << 6); /* No VIN6 */
1975 sio_data->internal |= (1 << 1); /* in7 is VSB */
1976 sio_data->internal |= (1 << 3); /* in9 is AVCC */
1978 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1983 superio_select(GPIO);
1985 reg = superio_inb(IT87_SIO_GPIO3_REG);
1986 if (!sio_data->skip_vid) {
1987 /* We need at least 4 VID pins */
1989 pr_info("VID is disabled (pins used for GPIO)\n");
1990 sio_data->skip_vid = 1;
1994 /* Check if fan3 is there or not */
1996 sio_data->skip_pwm |= (1 << 2);
1998 sio_data->skip_fan |= (1 << 2);
2000 /* Check if fan2 is there or not */
2001 reg = superio_inb(IT87_SIO_GPIO5_REG);
2003 sio_data->skip_pwm |= (1 << 1);
2005 sio_data->skip_fan |= (1 << 1);
2007 if ((sio_data->type == it8718 || sio_data->type == it8720)
2008 && !(sio_data->skip_vid))
2009 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
2011 reg = superio_inb(IT87_SIO_PINX2_REG);
2013 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2016 * The IT8720F has no VIN7 pin, so VCCH should always be
2017 * routed internally to VIN7 with an internal divider.
2018 * Curiously, there still is a configuration bit to control
2019 * this, which means it can be set incorrectly. And even
2020 * more curiously, many boards out there are improperly
2021 * configured, even though the IT8720F datasheet claims
2022 * that the internal routing of VCCH to VIN7 is the default
2023 * setting. So we force the internal routing in this case.
2025 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2026 * If UART6 is enabled, re-route VIN7 to the internal divider
2027 * if that is not already the case.
2029 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
2031 superio_outb(IT87_SIO_PINX2_REG, reg);
2032 pr_notice("Routing internal VCCH to in7\n");
2035 sio_data->internal |= (1 << 0);
2036 if ((reg & (1 << 1)) ||
2037 sio_data->type == it8620 || sio_data->type == it8721 ||
2038 sio_data->type == it8728 || sio_data->type == it8771 ||
2039 sio_data->type == it8772 || sio_data->type == it8786)
2040 sio_data->internal |= (1 << 1);
2043 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2044 * While VIN7 can be routed to the internal voltage divider,
2045 * VIN5 and VIN6 are not available if UART6 is enabled.
2047 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2048 * is the temperature source. Since we can not read the
2049 * temperature source here, skip_temp is preliminary.
2052 sio_data->skip_in |= (1 << 5) | (1 << 6);
2053 sio_data->skip_temp |= (1 << 2);
2056 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
2058 if (sio_data->beep_pin)
2059 pr_info("Beeping is supported\n");
2061 /* Disable specific features based on DMI strings */
2062 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2063 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2064 if (board_vendor && board_name) {
2065 if (strcmp(board_vendor, "nVIDIA") == 0
2066 && strcmp(board_name, "FN68PT") == 0) {
2068 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2069 * connected to a fan, but to something else. One user
2070 * has reported instant system power-off when changing
2071 * the PWM2 duty cycle, so we disable it.
2072 * I use the board name string as the trigger in case
2073 * the same board is ever used in other systems.
2075 pr_info("Disabling pwm2 due to hardware constraints\n");
2076 sio_data->skip_pwm = (1 << 1);
2085 static void it87_remove_files(struct device *dev)
2087 struct it87_data *data = platform_get_drvdata(pdev);
2088 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2091 sysfs_remove_group(&dev->kobj, &it87_group);
2092 for (i = 0; i < 10; i++) {
2093 if (sio_data->skip_in & (1 << i))
2095 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2096 if (it87_attributes_in_beep[i])
2097 sysfs_remove_file(&dev->kobj,
2098 it87_attributes_in_beep[i]);
2100 for (i = 0; i < 3; i++) {
2101 if (!(data->has_temp & (1 << i)))
2103 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
2104 if (has_temp_offset(data))
2105 sysfs_remove_file(&dev->kobj,
2106 it87_attributes_temp_offset[i]);
2107 if (sio_data->beep_pin)
2108 sysfs_remove_file(&dev->kobj,
2109 it87_attributes_temp_beep[i]);
2111 for (i = 0; i < 6; i++) {
2112 if (!(data->has_fan & (1 << i)))
2114 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
2115 if (sio_data->beep_pin)
2116 sysfs_remove_file(&dev->kobj,
2117 it87_attributes_fan_beep[i]);
2118 if (i < 3 && !has_16bit_fans(data))
2119 sysfs_remove_file(&dev->kobj,
2120 it87_attributes_fan_div[i]);
2122 for (i = 0; i < 3; i++) {
2123 if (sio_data->skip_pwm & (1 << 0))
2125 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
2126 if (has_old_autopwm(data))
2127 sysfs_remove_group(&dev->kobj,
2128 &it87_group_autopwm[i]);
2130 if (!sio_data->skip_vid)
2131 sysfs_remove_group(&dev->kobj, &it87_group_vid);
2132 sysfs_remove_group(&dev->kobj, &it87_group_label);
2135 static int it87_probe(struct platform_device *pdev)
2137 struct it87_data *data;
2138 struct resource *res;
2139 struct device *dev = &pdev->dev;
2140 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2142 int enable_pwm_interface;
2143 int fan_beep_need_rw;
2145 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2146 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2148 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2149 (unsigned long)res->start,
2150 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2154 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2158 data->addr = res->start;
2159 data->type = sio_data->type;
2160 data->features = it87_devices[sio_data->type].features;
2161 data->peci_mask = it87_devices[sio_data->type].peci_mask;
2162 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
2163 data->name = it87_devices[sio_data->type].name;
2165 * IT8705F Datasheet 0.4.1, 3h == Version G.
2166 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2167 * These are the first revisions with 16-bit tachometer support.
2169 switch (data->type) {
2171 if (sio_data->revision >= 0x03) {
2172 data->features &= ~FEAT_OLD_AUTOPWM;
2173 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
2177 if (sio_data->revision >= 0x08) {
2178 data->features &= ~FEAT_OLD_AUTOPWM;
2179 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2187 /* Now, we do the remaining detection. */
2188 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2189 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2192 platform_set_drvdata(pdev, data);
2194 mutex_init(&data->update_lock);
2196 /* Check PWM configuration */
2197 enable_pwm_interface = it87_check_pwm(dev);
2199 /* Starting with IT8721F, we handle scaling of internal voltages */
2200 if (has_12mv_adc(data)) {
2201 if (sio_data->internal & (1 << 0))
2202 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2203 if (sio_data->internal & (1 << 1))
2204 data->in_scaled |= (1 << 7); /* in7 is VSB */
2205 if (sio_data->internal & (1 << 2))
2206 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2207 if (sio_data->internal & (1 << 3))
2208 data->in_scaled |= (1 << 9); /* in9 is AVCC */
2209 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2210 sio_data->type == it8783) {
2211 if (sio_data->internal & (1 << 0))
2212 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2213 if (sio_data->internal & (1 << 1))
2214 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
2217 data->has_temp = 0x07;
2218 if (sio_data->skip_temp & (1 << 2)) {
2219 if (sio_data->type == it8782
2220 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2221 data->has_temp &= ~(1 << 2);
2224 /* Initialize the IT87 chip */
2225 it87_init_device(pdev);
2227 /* Register sysfs hooks */
2228 err = sysfs_create_group(&dev->kobj, &it87_group);
2232 for (i = 0; i < 10; i++) {
2233 if (sio_data->skip_in & (1 << i))
2235 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2238 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2239 err = sysfs_create_file(&dev->kobj,
2240 it87_attributes_in_beep[i]);
2246 for (i = 0; i < 3; i++) {
2247 if (!(data->has_temp & (1 << i)))
2249 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2252 if (has_temp_offset(data)) {
2253 err = sysfs_create_file(&dev->kobj,
2254 it87_attributes_temp_offset[i]);
2258 if (sio_data->beep_pin) {
2259 err = sysfs_create_file(&dev->kobj,
2260 it87_attributes_temp_beep[i]);
2266 /* Do not create fan files for disabled fans */
2267 fan_beep_need_rw = 1;
2268 for (i = 0; i < 6; i++) {
2269 if (!(data->has_fan & (1 << i)))
2271 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2275 if (i < 3 && !has_16bit_fans(data)) {
2276 err = sysfs_create_file(&dev->kobj,
2277 it87_attributes_fan_div[i]);
2282 if (sio_data->beep_pin) {
2283 err = sysfs_create_file(&dev->kobj,
2284 it87_attributes_fan_beep[i]);
2287 if (!fan_beep_need_rw)
2291 * As we have a single beep enable bit for all fans,
2292 * only the first enabled fan has a writable attribute
2295 if (sysfs_chmod_file(&dev->kobj,
2296 it87_attributes_fan_beep[i],
2298 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2300 fan_beep_need_rw = 0;
2304 if (enable_pwm_interface) {
2305 for (i = 0; i < 3; i++) {
2306 if (sio_data->skip_pwm & (1 << i))
2308 err = sysfs_create_group(&dev->kobj,
2309 &it87_group_pwm[i]);
2313 if (!has_old_autopwm(data))
2315 err = sysfs_create_group(&dev->kobj,
2316 &it87_group_autopwm[i]);
2322 if (!sio_data->skip_vid) {
2323 data->vrm = vid_which_vrm();
2324 /* VID reading from Super-I/O config space if available */
2325 data->vid = sio_data->vid_value;
2326 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2331 /* Export labels for internal sensors */
2332 for (i = 0; i < 4; i++) {
2333 if (!(sio_data->internal & (1 << i)))
2335 err = sysfs_create_file(&dev->kobj,
2336 it87_attributes_label[i]);
2341 data->hwmon_dev = hwmon_device_register(dev);
2342 if (IS_ERR(data->hwmon_dev)) {
2343 err = PTR_ERR(data->hwmon_dev);
2350 it87_remove_files(dev);
2354 static int it87_remove(struct platform_device *pdev)
2356 struct it87_data *data = platform_get_drvdata(pdev);
2358 hwmon_device_unregister(data->hwmon_dev);
2359 it87_remove_files(&pdev->dev);
2365 * Must be called with data->update_lock held, except during initialization.
2366 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2367 * would slow down the IT87 access and should not be necessary.
2369 static int it87_read_value(struct it87_data *data, u8 reg)
2371 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2372 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2376 * Must be called with data->update_lock held, except during initialization.
2377 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2378 * would slow down the IT87 access and should not be necessary.
2380 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2382 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2383 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2386 /* Return 1 if and only if the PWM interface is safe to use */
2387 static int it87_check_pwm(struct device *dev)
2389 struct it87_data *data = dev_get_drvdata(dev);
2391 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2392 * and polarity set to active low is sign that this is the case so we
2393 * disable pwm control to protect the user.
2395 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2396 if ((tmp & 0x87) == 0) {
2397 if (fix_pwm_polarity) {
2399 * The user asks us to attempt a chip reconfiguration.
2400 * This means switching to active high polarity and
2401 * inverting all fan speed values.
2406 for (i = 0; i < 3; i++)
2407 pwm[i] = it87_read_value(data,
2411 * If any fan is in automatic pwm mode, the polarity
2412 * might be correct, as suspicious as it seems, so we
2413 * better don't change anything (but still disable the
2416 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2418 "Reconfiguring PWM to active high polarity\n");
2419 it87_write_value(data, IT87_REG_FAN_CTL,
2421 for (i = 0; i < 3; i++)
2422 it87_write_value(data,
2429 "PWM configuration is too broken to be fixed\n");
2433 "Detected broken BIOS defaults, disabling PWM interface\n");
2435 } else if (fix_pwm_polarity) {
2437 "PWM configuration looks sane, won't touch\n");
2443 /* Called when we have found a new IT87. */
2444 static void it87_init_device(struct platform_device *pdev)
2446 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2447 struct it87_data *data = platform_get_drvdata(pdev);
2452 * For each PWM channel:
2453 * - If it is in automatic mode, setting to manual mode should set
2454 * the fan to full speed by default.
2455 * - If it is in manual mode, we need a mapping to temperature
2456 * channels to use when later setting to automatic mode later.
2457 * Use a 1:1 mapping by default (we are clueless.)
2458 * In both cases, the value can (and should) be changed by the user
2459 * prior to switching to a different mode.
2460 * Note that this is no longer needed for the IT8721F and later, as
2461 * these have separate registers for the temperature mapping and the
2462 * manual duty cycle.
2464 for (i = 0; i < 3; i++) {
2465 data->pwm_temp_map[i] = i;
2466 data->pwm_duty[i] = 0x7f; /* Full speed */
2467 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2471 * Some chips seem to have default value 0xff for all limit
2472 * registers. For low voltage limits it makes no sense and triggers
2473 * alarms, so change to 0 instead. For high temperature limits, it
2474 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2475 * but is still confusing, so change to 127 degrees C.
2477 for (i = 0; i < 8; i++) {
2478 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2480 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2482 for (i = 0; i < 3; i++) {
2483 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2485 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2489 * Temperature channels are not forcibly enabled, as they can be
2490 * set to two different sensor types and we can't guess which one
2491 * is correct for a given system. These channels can be enabled at
2492 * run-time through the temp{1-3}_type sysfs accessors if needed.
2495 /* Check if voltage monitors are reset manually or by some reason */
2496 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2497 if ((tmp & 0xff) == 0) {
2498 /* Enable all voltage monitors */
2499 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2502 /* Check if tachometers are reset manually or by some reason */
2503 mask = 0x70 & ~(sio_data->skip_fan << 4);
2504 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2505 if ((data->fan_main_ctrl & mask) == 0) {
2506 /* Enable all fan tachometers */
2507 data->fan_main_ctrl |= mask;
2508 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2509 data->fan_main_ctrl);
2511 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2513 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2515 /* Set tachometers to 16-bit mode if needed */
2516 if (has_fan16_config(data)) {
2517 if (~tmp & 0x07 & data->has_fan) {
2519 "Setting fan1-3 to 16-bit mode\n");
2520 it87_write_value(data, IT87_REG_FAN_16BIT,
2525 /* Check for additional fans */
2526 if (has_five_fans(data)) {
2528 data->has_fan |= (1 << 3); /* fan4 enabled */
2530 data->has_fan |= (1 << 4); /* fan5 enabled */
2533 if (has_six_fans(data)) {
2535 data->has_fan |= (1 << 5); /* fan6 enabled */
2538 /* Fan input pins may be used for alternative functions */
2539 data->has_fan &= ~sio_data->skip_fan;
2541 /* Start monitoring */
2542 it87_write_value(data, IT87_REG_CONFIG,
2543 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2544 | (update_vbat ? 0x41 : 0x01));
2547 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2549 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2550 if (has_newer_autopwm(data)) {
2551 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2552 data->pwm_duty[nr] = it87_read_value(data,
2553 IT87_REG_PWM_DUTY(nr));
2555 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2556 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2557 else /* Manual mode */
2558 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2561 if (has_old_autopwm(data)) {
2564 for (i = 0; i < 5 ; i++)
2565 data->auto_temp[nr][i] = it87_read_value(data,
2566 IT87_REG_AUTO_TEMP(nr, i));
2567 for (i = 0; i < 3 ; i++)
2568 data->auto_pwm[nr][i] = it87_read_value(data,
2569 IT87_REG_AUTO_PWM(nr, i));
2573 static struct it87_data *it87_update_device(struct device *dev)
2575 struct it87_data *data = dev_get_drvdata(dev);
2578 mutex_lock(&data->update_lock);
2580 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2584 * Cleared after each update, so reenable. Value
2585 * returned by this read will be previous value
2587 it87_write_value(data, IT87_REG_CONFIG,
2588 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2590 for (i = 0; i <= 7; i++) {
2592 it87_read_value(data, IT87_REG_VIN(i));
2594 it87_read_value(data, IT87_REG_VIN_MIN(i));
2596 it87_read_value(data, IT87_REG_VIN_MAX(i));
2598 /* in8 (battery) has no limit registers */
2599 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2600 if (data->type == it8603)
2601 data->in[9][0] = it87_read_value(data, 0x2f);
2603 for (i = 0; i < 6; i++) {
2604 /* Skip disabled fans */
2605 if (!(data->has_fan & (1 << i)))
2609 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2610 data->fan[i][0] = it87_read_value(data,
2612 /* Add high byte if in 16-bit mode */
2613 if (has_16bit_fans(data)) {
2614 data->fan[i][0] |= it87_read_value(data,
2615 IT87_REG_FANX[i]) << 8;
2616 data->fan[i][1] |= it87_read_value(data,
2617 IT87_REG_FANX_MIN[i]) << 8;
2620 for (i = 0; i < 3; i++) {
2621 if (!(data->has_temp & (1 << i)))
2624 it87_read_value(data, IT87_REG_TEMP(i));
2626 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2628 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2629 if (has_temp_offset(data))
2631 it87_read_value(data,
2632 IT87_REG_TEMP_OFFSET[i]);
2635 /* Newer chips don't have clock dividers */
2636 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2637 i = it87_read_value(data, IT87_REG_FAN_DIV);
2638 data->fan_div[0] = i & 0x07;
2639 data->fan_div[1] = (i >> 3) & 0x07;
2640 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2644 it87_read_value(data, IT87_REG_ALARM1) |
2645 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2646 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2647 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2649 data->fan_main_ctrl = it87_read_value(data,
2650 IT87_REG_FAN_MAIN_CTRL);
2651 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2652 for (i = 0; i < 3; i++)
2653 it87_update_pwm_ctrl(data, i);
2655 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2656 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
2658 * The IT8705F does not have VID capability.
2659 * The IT8718F and later don't use IT87_REG_VID for the
2662 if (data->type == it8712 || data->type == it8716) {
2663 data->vid = it87_read_value(data, IT87_REG_VID);
2665 * The older IT8712F revisions had only 5 VID pins,
2666 * but we assume it is always safe to read 6 bits.
2670 data->last_updated = jiffies;
2674 mutex_unlock(&data->update_lock);
2679 static int __init it87_device_add(unsigned short address,
2680 const struct it87_sio_data *sio_data)
2682 struct resource res = {
2683 .start = address + IT87_EC_OFFSET,
2684 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2686 .flags = IORESOURCE_IO,
2690 err = acpi_check_resource_conflict(&res);
2694 pdev = platform_device_alloc(DRVNAME, address);
2697 pr_err("Device allocation failed\n");
2701 err = platform_device_add_resources(pdev, &res, 1);
2703 pr_err("Device resource addition failed (%d)\n", err);
2704 goto exit_device_put;
2707 err = platform_device_add_data(pdev, sio_data,
2708 sizeof(struct it87_sio_data));
2710 pr_err("Platform data allocation failed\n");
2711 goto exit_device_put;
2714 err = platform_device_add(pdev);
2716 pr_err("Device addition failed (%d)\n", err);
2717 goto exit_device_put;
2723 platform_device_put(pdev);
2728 static int __init sm_it87_init(void)
2731 unsigned short isa_address = 0;
2732 struct it87_sio_data sio_data;
2734 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2735 err = it87_find(&isa_address, &sio_data);
2738 err = platform_driver_register(&it87_driver);
2742 err = it87_device_add(isa_address, &sio_data);
2744 platform_driver_unregister(&it87_driver);
2751 static void __exit sm_it87_exit(void)
2753 platform_device_unregister(pdev);
2754 platform_driver_unregister(&it87_driver);
2758 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
2759 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2760 module_param(update_vbat, bool, 0);
2761 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2762 module_param(fix_pwm_polarity, bool, 0);
2763 MODULE_PARM_DESC(fix_pwm_polarity,
2764 "Force PWM polarity to active high (DANGEROUS)");
2765 MODULE_LICENSE("GPL");
2767 module_init(sm_it87_init);
2768 module_exit(sm_it87_exit);