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, it8790, it8603, it8620 };
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 *it87_pdev[2];
82 #define REG_2E 0x2e /* The register to read/write */
83 #define REG_4E 0x4e /* Secondary register to read/write */
85 #define DEV 0x07 /* Register: Logical device select */
86 #define PME 0x04 /* The device with the fan registers in it */
88 /* The device with the IT8718F/IT8720F VID value in it */
91 #define DEVID 0x20 /* Register: Device ID */
92 #define DEVREV 0x22 /* Register: Device Revision */
94 static inline int superio_inb(int ioreg, int reg)
97 return inb(ioreg + 1);
100 static inline void superio_outb(int ioreg, int reg, int val)
103 outb(val, ioreg + 1);
106 static int superio_inw(int ioreg, int reg)
110 val = inb(ioreg + 1) << 8;
112 val |= inb(ioreg + 1);
116 static inline void superio_select(int ioreg, int ldn)
119 outb(ldn, ioreg + 1);
122 static inline int superio_enter(int ioreg)
125 * Try to reserve ioreg and ioreg + 1 for exclusive access.
127 if (!request_muxed_region(ioreg, 2, DRVNAME))
137 static inline void superio_exit(int ioreg)
140 outb(0x02, ioreg + 1);
141 release_region(ioreg, 2);
144 /* Logical device 4 registers */
145 #define IT8712F_DEVID 0x8712
146 #define IT8705F_DEVID 0x8705
147 #define IT8716F_DEVID 0x8716
148 #define IT8718F_DEVID 0x8718
149 #define IT8720F_DEVID 0x8720
150 #define IT8721F_DEVID 0x8721
151 #define IT8726F_DEVID 0x8726
152 #define IT8728F_DEVID 0x8728
153 #define IT8771E_DEVID 0x8771
154 #define IT8772E_DEVID 0x8772
155 #define IT8781F_DEVID 0x8781
156 #define IT8782F_DEVID 0x8782
157 #define IT8783E_DEVID 0x8783
158 #define IT8786E_DEVID 0x8786
159 #define IT8603E_DEVID 0x8603
160 #define IT8620E_DEVID 0x8620
161 #define IT8623E_DEVID 0x8623
162 #define IT8790E_DEVID 0x8790
163 #define IT87_ACT_REG 0x30
164 #define IT87_BASE_REG 0x60
166 /* Logical device 7 registers (IT8712F and later) */
167 #define IT87_SIO_GPIO1_REG 0x25
168 #define IT87_SIO_GPIO2_REG 0x26
169 #define IT87_SIO_GPIO3_REG 0x27
170 #define IT87_SIO_GPIO4_REG 0x28
171 #define IT87_SIO_GPIO5_REG 0x29
172 #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
173 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
174 #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
175 #define IT87_SIO_VID_REG 0xfc /* VID value */
176 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
178 /* Update battery voltage after every reading if true */
179 static bool update_vbat;
181 /* Not all BIOSes properly configure the PWM registers */
182 static bool fix_pwm_polarity;
184 /* Many IT87 constants specified below */
186 /* Length of ISA address segment */
187 #define IT87_EXTENT 8
189 /* Length of ISA address segment for Environmental Controller */
190 #define IT87_EC_EXTENT 2
192 /* Offset of EC registers from ISA base address */
193 #define IT87_EC_OFFSET 5
195 /* Where are the ISA address/data registers relative to the EC base address */
196 #define IT87_ADDR_REG_OFFSET 0
197 #define IT87_DATA_REG_OFFSET 1
199 /*----- The IT87 registers -----*/
201 #define IT87_REG_CONFIG 0x00
203 #define IT87_REG_ALARM1 0x01
204 #define IT87_REG_ALARM2 0x02
205 #define IT87_REG_ALARM3 0x03
208 * The IT8718F and IT8720F have the VID value in a different register, in
209 * Super-I/O configuration space.
211 #define IT87_REG_VID 0x0a
213 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
214 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
217 #define IT87_REG_FAN_DIV 0x0b
218 #define IT87_REG_FAN_16BIT 0x0c
220 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
222 static const u8 IT87_REG_FAN[] = {0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c};
223 static const u8 IT87_REG_FAN_MIN[] = {0x10, 0x11, 0x12, 0x84, 0x86, 0x4e};
224 static const u8 IT87_REG_FANX[] = {0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d};
225 static const u8 IT87_REG_FANX_MIN[] = {0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f};
226 static const u8 IT87_REG_TEMP_OFFSET[] = {0x56, 0x57, 0x59};
228 #define IT87_REG_FAN_MAIN_CTRL 0x13
229 #define IT87_REG_FAN_CTL 0x14
230 static const u8 IT87_REG_PWM[] = {0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf};
231 static const u8 IT87_REG_PWM_DUTY[] = {0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab};
233 #define IT87_REG_VIN(nr) (0x20 + (nr))
234 #define IT87_REG_TEMP(nr) (0x29 + (nr))
236 #define IT87_REG_AVCC3 0x2f
238 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
239 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
240 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
241 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
243 #define IT87_REG_VIN_ENABLE 0x50
244 #define IT87_REG_TEMP_ENABLE 0x51
245 #define IT87_REG_TEMP_EXTRA 0x55
246 #define IT87_REG_BEEP_ENABLE 0x5c
248 #define IT87_REG_CHIPID 0x58
250 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
251 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
253 struct it87_devices {
258 const char * const suffix;
261 #define FEAT_12MV_ADC (1 << 0)
262 #define FEAT_NEWER_AUTOPWM (1 << 1)
263 #define FEAT_OLD_AUTOPWM (1 << 2)
264 #define FEAT_16BIT_FANS (1 << 3)
265 #define FEAT_TEMP_OFFSET (1 << 4)
266 #define FEAT_TEMP_PECI (1 << 5)
267 #define FEAT_TEMP_OLD_PECI (1 << 6)
268 #define FEAT_FAN16_CONFIG (1 << 7) /* Need to enable 16-bit fans */
269 #define FEAT_FIVE_FANS (1 << 8) /* Supports five fans */
270 #define FEAT_VID (1 << 9) /* Set if chip supports VID */
271 #define FEAT_IN7_INTERNAL (1 << 10) /* Set if in7 is internal */
272 #define FEAT_SIX_FANS (1 << 11) /* Supports six fans */
273 #define FEAT_AVCC3 (1 << 12) /* Chip supports in9/AVCC3 */
274 #define FEAT_SIX_PWM (1 << 13) /* Chip supports 6 pwm chn */
275 #define FEAT_PWM_FREQ2 (1 << 14) /* Separate pwm freq 2 */
277 static const struct it87_devices it87_devices[] = {
280 .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
285 .features = FEAT_OLD_AUTOPWM | FEAT_VID,
286 /* may need to overwrite */
291 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
292 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
297 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
298 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
300 .old_peci_mask = 0x4,
305 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
306 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
308 .old_peci_mask = 0x4,
313 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
314 | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
315 | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
318 .old_peci_mask = 0x02, /* Actually reports PCH */
323 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
324 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
325 | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
331 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
332 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
334 /* PECI: guesswork */
336 /* 16 bit fans (OHM) */
337 /* three fans, always 16 bit (guesswork) */
343 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
344 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
346 /* PECI (coreboot) */
347 /* 12mV ADC (HWSensors4, OHM) */
348 /* 16 bit fans (HWSensors4, OHM) */
349 /* three fans, always 16 bit (datasheet) */
355 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
356 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
357 .old_peci_mask = 0x4,
362 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
363 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
364 .old_peci_mask = 0x4,
369 .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
370 | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
371 .old_peci_mask = 0x4,
376 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
377 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
384 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
385 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
392 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
393 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
394 | FEAT_AVCC3 | FEAT_PWM_FREQ2,
400 .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
401 | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
402 | FEAT_IN7_INTERNAL | FEAT_AVCC3 | FEAT_SIX_PWM
409 #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
410 #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
411 #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
412 #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
413 #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
414 #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
415 ((data)->peci_mask & (1 << nr)))
416 #define has_temp_old_peci(data, nr) \
417 (((data)->features & FEAT_TEMP_OLD_PECI) && \
418 ((data)->old_peci_mask & (1 << nr)))
419 #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
420 #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
422 #define has_vid(data) ((data)->features & FEAT_VID)
423 #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
424 #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
425 #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
426 #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
427 #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
429 struct it87_sio_data {
431 /* Values read from Super-I/O config space */
435 u8 internal; /* Internal sensors can be labeled */
436 /* Features skipped based on config or DMI */
445 * For each registered chip, we need to keep some data in memory.
446 * The structure is dynamically allocated.
449 struct device *hwmon_dev;
457 struct mutex update_lock;
458 char valid; /* !=0 if following fields are valid */
459 unsigned long last_updated; /* In jiffies */
461 u16 in_scaled; /* Internal voltage sensors are scaled */
462 u8 in[10][3]; /* [nr][0]=in, [1]=min, [2]=max */
463 u8 has_fan; /* Bitfield, fans enabled */
464 u16 fan[6][2]; /* Register values, [nr][0]=fan, [1]=min */
465 u8 has_temp; /* Bitfield, temp sensors enabled */
466 s8 temp[3][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
467 u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
468 u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
469 u8 fan_div[3]; /* Register encoding, shifted right */
470 u8 vid; /* Register encoding, combined */
472 u32 alarms; /* Register encoding, combined */
473 u8 beeps; /* Register encoding */
474 u8 fan_main_ctrl; /* Register value */
475 u8 fan_ctl; /* Register value */
478 * The following 3 arrays correspond to the same registers up to
479 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
480 * 7, and we want to preserve settings on mode changes, so we have
481 * to track all values separately.
482 * Starting with the IT8721F, the manual PWM duty cycles are stored
483 * in separate registers (8-bit values), so the separate tracking
484 * is no longer needed, but it is still done to keep the driver
487 u8 pwm_ctrl[6]; /* Register value */
488 u8 pwm_duty[6]; /* Manual PWM value set by user */
489 u8 pwm_temp_map[6]; /* PWM to temp. chan. mapping (bits 1-0) */
491 /* Automatic fan speed control registers */
492 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
493 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
496 static int adc_lsb(const struct it87_data *data, int nr)
498 int lsb = has_12mv_adc(data) ? 12 : 16;
499 if (data->in_scaled & (1 << nr))
504 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
506 val = DIV_ROUND_CLOSEST(val, adc_lsb(data, nr));
507 return clamp_val(val, 0, 255);
510 static int in_from_reg(const struct it87_data *data, int nr, int val)
512 return val * adc_lsb(data, nr);
515 static inline u8 FAN_TO_REG(long rpm, int div)
519 rpm = clamp_val(rpm, 1, 1000000);
520 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
523 static inline u16 FAN16_TO_REG(long rpm)
527 return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
530 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
531 1350000 / ((val) * (div)))
532 /* The divider is fixed to 2 in 16-bit mode */
533 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
534 1350000 / ((val) * 2))
536 #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
537 ((val) + 500) / 1000), -128, 127))
538 #define TEMP_FROM_REG(val) ((val) * 1000)
540 static u8 pwm_to_reg(const struct it87_data *data, long val)
542 if (has_newer_autopwm(data))
548 static int pwm_from_reg(const struct it87_data *data, u8 reg)
550 if (has_newer_autopwm(data))
553 return (reg & 0x7f) << 1;
557 static int DIV_TO_REG(int val)
560 while (answer < 7 && (val >>= 1))
564 #define DIV_FROM_REG(val) (1 << (val))
567 * pwm base frequencies. The frequency has to be divided by either 128 or 256,
568 * depending on the chip type, to calculate the actual pwm frequency.
570 * Some of the chip datasheets suggest a base frequency of 51kHz instead
571 * of 750kHz for the slowest base frequency, resulting in a pwm frequency
572 * of 200Hz. Sometimes both pwm frequency select registers are affected,
573 * sometimes just one. It is unknown if this is a datasheet error or real,
574 * so this is ignored for now.
576 static const unsigned int pwm_freq[8] = {
587 static int it87_probe(struct platform_device *pdev);
588 static int it87_remove(struct platform_device *pdev);
590 static int it87_read_value(struct it87_data *data, u8 reg);
591 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
592 static struct it87_data *it87_update_device(struct device *dev);
593 static int it87_check_pwm(struct device *dev);
594 static void it87_init_device(struct platform_device *pdev);
597 static struct platform_driver it87_driver = {
602 .remove = it87_remove,
605 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
608 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
610 int index = sattr->index;
612 struct it87_data *data = it87_update_device(dev);
613 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
616 static ssize_t set_in(struct device *dev, struct device_attribute *attr,
617 const char *buf, size_t count)
619 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
621 int index = sattr->index;
623 struct it87_data *data = dev_get_drvdata(dev);
626 if (kstrtoul(buf, 10, &val) < 0)
629 mutex_lock(&data->update_lock);
630 data->in[nr][index] = in_to_reg(data, nr, val);
631 it87_write_value(data,
632 index == 1 ? IT87_REG_VIN_MIN(nr)
633 : IT87_REG_VIN_MAX(nr),
634 data->in[nr][index]);
635 mutex_unlock(&data->update_lock);
639 static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
640 static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
642 static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
645 static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
646 static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
648 static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
651 static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
652 static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
654 static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
657 static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
658 static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
660 static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
663 static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
664 static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
666 static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
669 static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
670 static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
672 static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
675 static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
676 static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
678 static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
681 static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
682 static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
684 static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
687 static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
688 static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
691 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
694 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
696 int index = sattr->index;
697 struct it87_data *data = it87_update_device(dev);
699 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
702 static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
703 const char *buf, size_t count)
705 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
707 int index = sattr->index;
708 struct it87_data *data = dev_get_drvdata(dev);
712 if (kstrtol(buf, 10, &val) < 0)
715 mutex_lock(&data->update_lock);
720 reg = IT87_REG_TEMP_LOW(nr);
723 reg = IT87_REG_TEMP_HIGH(nr);
726 regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
727 if (!(regval & 0x80)) {
729 it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
732 reg = IT87_REG_TEMP_OFFSET[nr];
736 data->temp[nr][index] = TEMP_TO_REG(val);
737 it87_write_value(data, reg, data->temp[nr][index]);
738 mutex_unlock(&data->update_lock);
742 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
743 static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
745 static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
747 static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
749 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
750 static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
752 static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
754 static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
756 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
757 static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
759 static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
761 static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
764 static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
767 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
768 int nr = sensor_attr->index;
769 struct it87_data *data = it87_update_device(dev);
770 u8 reg = data->sensor; /* In case value is updated while used */
771 u8 extra = data->extra;
773 if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1))
774 || (has_temp_old_peci(data, nr) && (extra & 0x80)))
775 return sprintf(buf, "6\n"); /* Intel PECI */
777 return sprintf(buf, "3\n"); /* thermal diode */
779 return sprintf(buf, "4\n"); /* thermistor */
780 return sprintf(buf, "0\n"); /* disabled */
783 static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
784 const char *buf, size_t count)
786 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
787 int nr = sensor_attr->index;
789 struct it87_data *data = dev_get_drvdata(dev);
793 if (kstrtol(buf, 10, &val) < 0)
796 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
799 if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
801 extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
802 if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
804 if (val == 2) { /* backwards compatibility */
806 "Sensor type 2 is deprecated, please use 4 instead\n");
809 /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
814 else if (has_temp_peci(data, nr) && val == 6)
815 reg |= (nr + 1) << 6;
816 else if (has_temp_old_peci(data, nr) && val == 6)
821 mutex_lock(&data->update_lock);
824 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
825 if (has_temp_old_peci(data, nr))
826 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
827 data->valid = 0; /* Force cache refresh */
828 mutex_unlock(&data->update_lock);
832 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
834 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
836 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
841 static int pwm_mode(const struct it87_data *data, int nr)
843 int ctrl = data->fan_main_ctrl & (1 << nr);
845 if (ctrl == 0 && data->type != it8603) /* Full speed */
847 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
849 else /* Manual mode */
853 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
856 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
858 int index = sattr->index;
860 struct it87_data *data = it87_update_device(dev);
862 speed = has_16bit_fans(data) ?
863 FAN16_FROM_REG(data->fan[nr][index]) :
864 FAN_FROM_REG(data->fan[nr][index],
865 DIV_FROM_REG(data->fan_div[nr]));
866 return sprintf(buf, "%d\n", speed);
869 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
872 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
873 int nr = sensor_attr->index;
875 struct it87_data *data = it87_update_device(dev);
876 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
878 static ssize_t show_pwm_enable(struct device *dev,
879 struct device_attribute *attr, char *buf)
881 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
882 int nr = sensor_attr->index;
884 struct it87_data *data = it87_update_device(dev);
885 return sprintf(buf, "%d\n", pwm_mode(data, nr));
887 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
890 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
891 int nr = sensor_attr->index;
893 struct it87_data *data = it87_update_device(dev);
894 return sprintf(buf, "%d\n",
895 pwm_from_reg(data, data->pwm_duty[nr]));
897 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
900 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
901 struct it87_data *data = it87_update_device(dev);
902 int nr = sensor_attr->index;
906 if (has_pwm_freq2(data) && nr == 1)
907 index = (data->extra >> 4) & 0x07;
909 index = (data->fan_ctl >> 4) & 0x07;
911 freq = pwm_freq[index] / has_newer_autopwm(data) ? 256 : 128;
913 return sprintf(buf, "%u\n", freq);
916 static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
917 const char *buf, size_t count)
919 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
921 int index = sattr->index;
923 struct it87_data *data = dev_get_drvdata(dev);
927 if (kstrtol(buf, 10, &val) < 0)
930 mutex_lock(&data->update_lock);
932 if (has_16bit_fans(data)) {
933 data->fan[nr][index] = FAN16_TO_REG(val);
934 it87_write_value(data, IT87_REG_FAN_MIN[nr],
935 data->fan[nr][index] & 0xff);
936 it87_write_value(data, IT87_REG_FANX_MIN[nr],
937 data->fan[nr][index] >> 8);
939 reg = it87_read_value(data, IT87_REG_FAN_DIV);
942 data->fan_div[nr] = reg & 0x07;
945 data->fan_div[nr] = (reg >> 3) & 0x07;
948 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
951 data->fan[nr][index] =
952 FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
953 it87_write_value(data, IT87_REG_FAN_MIN[nr],
954 data->fan[nr][index]);
957 mutex_unlock(&data->update_lock);
961 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
962 const char *buf, size_t count)
964 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
965 int nr = sensor_attr->index;
967 struct it87_data *data = dev_get_drvdata(dev);
972 if (kstrtoul(buf, 10, &val) < 0)
975 mutex_lock(&data->update_lock);
976 old = it87_read_value(data, IT87_REG_FAN_DIV);
978 /* Save fan min limit */
979 min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
984 data->fan_div[nr] = DIV_TO_REG(val);
988 data->fan_div[nr] = 1;
990 data->fan_div[nr] = 3;
993 val |= (data->fan_div[0] & 0x07);
994 val |= (data->fan_div[1] & 0x07) << 3;
995 if (data->fan_div[2] == 3)
997 it87_write_value(data, IT87_REG_FAN_DIV, val);
999 /* Restore fan min limit */
1000 data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
1001 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
1003 mutex_unlock(&data->update_lock);
1007 /* Returns 0 if OK, -EINVAL otherwise */
1008 static int check_trip_points(struct device *dev, int nr)
1010 const struct it87_data *data = dev_get_drvdata(dev);
1013 if (has_old_autopwm(data)) {
1014 for (i = 0; i < 3; i++) {
1015 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
1018 for (i = 0; i < 2; i++) {
1019 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
1026 "Inconsistent trip points, not switching to automatic mode\n");
1027 dev_err(dev, "Adjust the trip points and try again\n");
1032 static ssize_t set_pwm_enable(struct device *dev,
1033 struct device_attribute *attr, const char *buf, size_t count)
1035 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1036 int nr = sensor_attr->index;
1038 struct it87_data *data = dev_get_drvdata(dev);
1041 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
1044 /* Check trip points before switching to automatic mode */
1046 if (check_trip_points(dev, nr) < 0)
1050 /* IT8603E does not have on/off mode */
1051 if (val == 0 && data->type == it8603)
1054 mutex_lock(&data->update_lock);
1058 /* make sure the fan is on when in on/off mode */
1059 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1060 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
1061 /* set on/off mode */
1062 data->fan_main_ctrl &= ~(1 << nr);
1063 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1064 data->fan_main_ctrl);
1066 if (val == 1) /* Manual mode */
1067 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
1068 data->pwm_temp_map[nr] :
1070 else /* Automatic mode */
1071 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1072 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1074 if (data->type != it8603) {
1075 /* set SmartGuardian mode */
1076 data->fan_main_ctrl |= (1 << nr);
1077 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1078 data->fan_main_ctrl);
1082 mutex_unlock(&data->update_lock);
1085 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
1086 const char *buf, size_t count)
1088 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1089 int nr = sensor_attr->index;
1091 struct it87_data *data = dev_get_drvdata(dev);
1094 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1097 mutex_lock(&data->update_lock);
1098 if (has_newer_autopwm(data)) {
1100 * If we are in automatic mode, the PWM duty cycle register
1101 * is read-only so we can't write the value.
1103 if (data->pwm_ctrl[nr] & 0x80) {
1104 mutex_unlock(&data->update_lock);
1107 data->pwm_duty[nr] = pwm_to_reg(data, val);
1108 it87_write_value(data, IT87_REG_PWM_DUTY[nr],
1109 data->pwm_duty[nr]);
1111 data->pwm_duty[nr] = pwm_to_reg(data, val);
1113 * If we are in manual mode, write the duty cycle immediately;
1114 * otherwise, just store it for later use.
1116 if (!(data->pwm_ctrl[nr] & 0x80)) {
1117 data->pwm_ctrl[nr] = data->pwm_duty[nr];
1118 it87_write_value(data, IT87_REG_PWM[nr],
1119 data->pwm_ctrl[nr]);
1122 mutex_unlock(&data->update_lock);
1125 static ssize_t set_pwm_freq(struct device *dev,
1126 struct device_attribute *attr, const char *buf, size_t count)
1128 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1129 struct it87_data *data = dev_get_drvdata(dev);
1130 int nr = sensor_attr->index;
1134 if (kstrtoul(buf, 10, &val) < 0)
1137 val = clamp_val(val, 0, 1000000);
1138 val *= has_newer_autopwm(data) ? 256 : 128;
1140 /* Search for the nearest available frequency */
1141 for (i = 0; i < 7; i++) {
1142 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
1146 mutex_lock(&data->update_lock);
1148 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
1149 data->fan_ctl |= i << 4;
1150 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
1152 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
1153 data->extra |= i << 4;
1154 it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
1156 mutex_unlock(&data->update_lock);
1160 static ssize_t show_pwm_temp_map(struct device *dev,
1161 struct device_attribute *attr, char *buf)
1163 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1164 int nr = sensor_attr->index;
1166 struct it87_data *data = it87_update_device(dev);
1169 if (data->pwm_temp_map[nr] < 3)
1170 map = 1 << data->pwm_temp_map[nr];
1172 map = 0; /* Should never happen */
1173 return sprintf(buf, "%d\n", map);
1175 static ssize_t set_pwm_temp_map(struct device *dev,
1176 struct device_attribute *attr, const char *buf, size_t count)
1178 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1179 int nr = sensor_attr->index;
1181 struct it87_data *data = dev_get_drvdata(dev);
1186 * This check can go away if we ever support automatic fan speed
1187 * control on newer chips.
1189 if (!has_old_autopwm(data)) {
1190 dev_notice(dev, "Mapping change disabled for safety reasons\n");
1194 if (kstrtol(buf, 10, &val) < 0)
1211 mutex_lock(&data->update_lock);
1212 data->pwm_temp_map[nr] = reg;
1214 * If we are in automatic mode, write the temp mapping immediately;
1215 * otherwise, just store it for later use.
1217 if (data->pwm_ctrl[nr] & 0x80) {
1218 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1219 it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
1221 mutex_unlock(&data->update_lock);
1225 static ssize_t show_auto_pwm(struct device *dev,
1226 struct device_attribute *attr, char *buf)
1228 struct it87_data *data = it87_update_device(dev);
1229 struct sensor_device_attribute_2 *sensor_attr =
1230 to_sensor_dev_attr_2(attr);
1231 int nr = sensor_attr->nr;
1232 int point = sensor_attr->index;
1234 return sprintf(buf, "%d\n",
1235 pwm_from_reg(data, data->auto_pwm[nr][point]));
1238 static ssize_t set_auto_pwm(struct device *dev,
1239 struct device_attribute *attr, const char *buf, size_t count)
1241 struct it87_data *data = dev_get_drvdata(dev);
1242 struct sensor_device_attribute_2 *sensor_attr =
1243 to_sensor_dev_attr_2(attr);
1244 int nr = sensor_attr->nr;
1245 int point = sensor_attr->index;
1248 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1251 mutex_lock(&data->update_lock);
1252 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1253 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1254 data->auto_pwm[nr][point]);
1255 mutex_unlock(&data->update_lock);
1259 static ssize_t show_auto_temp(struct device *dev,
1260 struct device_attribute *attr, char *buf)
1262 struct it87_data *data = it87_update_device(dev);
1263 struct sensor_device_attribute_2 *sensor_attr =
1264 to_sensor_dev_attr_2(attr);
1265 int nr = sensor_attr->nr;
1266 int point = sensor_attr->index;
1268 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1271 static ssize_t set_auto_temp(struct device *dev,
1272 struct device_attribute *attr, const char *buf, size_t count)
1274 struct it87_data *data = dev_get_drvdata(dev);
1275 struct sensor_device_attribute_2 *sensor_attr =
1276 to_sensor_dev_attr_2(attr);
1277 int nr = sensor_attr->nr;
1278 int point = sensor_attr->index;
1281 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1284 mutex_lock(&data->update_lock);
1285 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1286 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1287 data->auto_temp[nr][point]);
1288 mutex_unlock(&data->update_lock);
1292 static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
1293 static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1295 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
1298 static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
1299 static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1301 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
1304 static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
1305 static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1307 static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
1310 static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
1311 static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1314 static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
1315 static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1318 static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
1319 static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
1322 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1323 show_pwm_enable, set_pwm_enable, 0);
1324 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
1325 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
1327 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
1328 show_pwm_temp_map, set_pwm_temp_map, 0);
1329 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1330 show_auto_pwm, set_auto_pwm, 0, 0);
1331 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
1332 show_auto_pwm, set_auto_pwm, 0, 1);
1333 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
1334 show_auto_pwm, set_auto_pwm, 0, 2);
1335 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
1336 show_auto_pwm, NULL, 0, 3);
1337 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1338 show_auto_temp, set_auto_temp, 0, 1);
1339 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1340 show_auto_temp, set_auto_temp, 0, 0);
1341 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1342 show_auto_temp, set_auto_temp, 0, 2);
1343 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1344 show_auto_temp, set_auto_temp, 0, 3);
1345 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
1346 show_auto_temp, set_auto_temp, 0, 4);
1348 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1349 show_pwm_enable, set_pwm_enable, 1);
1350 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
1351 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
1352 static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
1353 show_pwm_temp_map, set_pwm_temp_map, 1);
1354 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1355 show_auto_pwm, set_auto_pwm, 1, 0);
1356 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
1357 show_auto_pwm, set_auto_pwm, 1, 1);
1358 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
1359 show_auto_pwm, set_auto_pwm, 1, 2);
1360 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
1361 show_auto_pwm, NULL, 1, 3);
1362 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1363 show_auto_temp, set_auto_temp, 1, 1);
1364 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1365 show_auto_temp, set_auto_temp, 1, 0);
1366 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1367 show_auto_temp, set_auto_temp, 1, 2);
1368 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1369 show_auto_temp, set_auto_temp, 1, 3);
1370 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
1371 show_auto_temp, set_auto_temp, 1, 4);
1373 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1374 show_pwm_enable, set_pwm_enable, 2);
1375 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
1376 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
1377 static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
1378 show_pwm_temp_map, set_pwm_temp_map, 2);
1379 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1380 show_auto_pwm, set_auto_pwm, 2, 0);
1381 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
1382 show_auto_pwm, set_auto_pwm, 2, 1);
1383 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
1384 show_auto_pwm, set_auto_pwm, 2, 2);
1385 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
1386 show_auto_pwm, NULL, 2, 3);
1387 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1388 show_auto_temp, set_auto_temp, 2, 1);
1389 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
1390 show_auto_temp, set_auto_temp, 2, 0);
1391 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1392 show_auto_temp, set_auto_temp, 2, 2);
1393 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1394 show_auto_temp, set_auto_temp, 2, 3);
1395 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
1396 show_auto_temp, set_auto_temp, 2, 4);
1398 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
1399 show_pwm_enable, set_pwm_enable, 3);
1400 static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
1401 static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
1402 static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO | S_IWUSR,
1403 show_pwm_temp_map, set_pwm_temp_map, 3);
1405 static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
1406 show_pwm_enable, set_pwm_enable, 4);
1407 static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
1408 static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
1409 static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO | S_IWUSR,
1410 show_pwm_temp_map, set_pwm_temp_map, 4);
1412 static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
1413 show_pwm_enable, set_pwm_enable, 5);
1414 static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
1415 static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
1416 static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO | S_IWUSR,
1417 show_pwm_temp_map, set_pwm_temp_map, 5);
1420 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1423 struct it87_data *data = it87_update_device(dev);
1424 return sprintf(buf, "%u\n", data->alarms);
1426 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1428 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1431 int bitnr = to_sensor_dev_attr(attr)->index;
1432 struct it87_data *data = it87_update_device(dev);
1433 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1436 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1437 *attr, const char *buf, size_t count)
1439 struct it87_data *data = dev_get_drvdata(dev);
1443 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1446 mutex_lock(&data->update_lock);
1447 config = it87_read_value(data, IT87_REG_CONFIG);
1452 it87_write_value(data, IT87_REG_CONFIG, config);
1453 /* Invalidate cache to force re-read */
1456 mutex_unlock(&data->update_lock);
1461 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1462 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1463 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1464 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1465 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1466 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1467 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1468 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1469 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1470 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1471 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1472 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1473 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1474 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
1475 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1476 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1477 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1478 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1479 show_alarm, clear_intrusion, 4);
1481 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1484 int bitnr = to_sensor_dev_attr(attr)->index;
1485 struct it87_data *data = it87_update_device(dev);
1486 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1488 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1489 const char *buf, size_t count)
1491 int bitnr = to_sensor_dev_attr(attr)->index;
1492 struct it87_data *data = dev_get_drvdata(dev);
1495 if (kstrtol(buf, 10, &val) < 0
1496 || (val != 0 && val != 1))
1499 mutex_lock(&data->update_lock);
1500 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1502 data->beeps |= (1 << bitnr);
1504 data->beeps &= ~(1 << bitnr);
1505 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1506 mutex_unlock(&data->update_lock);
1510 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1511 show_beep, set_beep, 1);
1512 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1513 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1514 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1515 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1516 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1517 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1518 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1519 /* fanX_beep writability is set later */
1520 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1521 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1522 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1523 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1524 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1525 static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
1526 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1527 show_beep, set_beep, 2);
1528 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1529 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1531 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1534 struct it87_data *data = dev_get_drvdata(dev);
1535 return sprintf(buf, "%u\n", data->vrm);
1537 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1538 const char *buf, size_t count)
1540 struct it87_data *data = dev_get_drvdata(dev);
1543 if (kstrtoul(buf, 10, &val) < 0)
1550 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1552 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1555 struct it87_data *data = it87_update_device(dev);
1556 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1558 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1560 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1563 static const char * const labels[] = {
1568 static const char * const labels_it8721[] = {
1573 struct it87_data *data = dev_get_drvdata(dev);
1574 int nr = to_sensor_dev_attr(attr)->index;
1576 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1579 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1580 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1581 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1583 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
1585 static ssize_t show_name(struct device *dev, struct device_attribute
1586 *devattr, char *buf)
1588 struct it87_data *data = dev_get_drvdata(dev);
1589 return sprintf(buf, "%s\n", data->name);
1591 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1593 static struct attribute *it87_attributes_in[10][5] = {
1595 &sensor_dev_attr_in0_input.dev_attr.attr,
1596 &sensor_dev_attr_in0_min.dev_attr.attr,
1597 &sensor_dev_attr_in0_max.dev_attr.attr,
1598 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1601 &sensor_dev_attr_in1_input.dev_attr.attr,
1602 &sensor_dev_attr_in1_min.dev_attr.attr,
1603 &sensor_dev_attr_in1_max.dev_attr.attr,
1604 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1607 &sensor_dev_attr_in2_input.dev_attr.attr,
1608 &sensor_dev_attr_in2_min.dev_attr.attr,
1609 &sensor_dev_attr_in2_max.dev_attr.attr,
1610 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1613 &sensor_dev_attr_in3_input.dev_attr.attr,
1614 &sensor_dev_attr_in3_min.dev_attr.attr,
1615 &sensor_dev_attr_in3_max.dev_attr.attr,
1616 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1619 &sensor_dev_attr_in4_input.dev_attr.attr,
1620 &sensor_dev_attr_in4_min.dev_attr.attr,
1621 &sensor_dev_attr_in4_max.dev_attr.attr,
1622 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1625 &sensor_dev_attr_in5_input.dev_attr.attr,
1626 &sensor_dev_attr_in5_min.dev_attr.attr,
1627 &sensor_dev_attr_in5_max.dev_attr.attr,
1628 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1631 &sensor_dev_attr_in6_input.dev_attr.attr,
1632 &sensor_dev_attr_in6_min.dev_attr.attr,
1633 &sensor_dev_attr_in6_max.dev_attr.attr,
1634 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1637 &sensor_dev_attr_in7_input.dev_attr.attr,
1638 &sensor_dev_attr_in7_min.dev_attr.attr,
1639 &sensor_dev_attr_in7_max.dev_attr.attr,
1640 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1643 &sensor_dev_attr_in8_input.dev_attr.attr,
1646 &sensor_dev_attr_in9_input.dev_attr.attr,
1650 static const struct attribute_group it87_group_in[10] = {
1651 { .attrs = it87_attributes_in[0] },
1652 { .attrs = it87_attributes_in[1] },
1653 { .attrs = it87_attributes_in[2] },
1654 { .attrs = it87_attributes_in[3] },
1655 { .attrs = it87_attributes_in[4] },
1656 { .attrs = it87_attributes_in[5] },
1657 { .attrs = it87_attributes_in[6] },
1658 { .attrs = it87_attributes_in[7] },
1659 { .attrs = it87_attributes_in[8] },
1660 { .attrs = it87_attributes_in[9] },
1663 static struct attribute *it87_attributes_temp[3][6] = {
1665 &sensor_dev_attr_temp1_input.dev_attr.attr,
1666 &sensor_dev_attr_temp1_max.dev_attr.attr,
1667 &sensor_dev_attr_temp1_min.dev_attr.attr,
1668 &sensor_dev_attr_temp1_type.dev_attr.attr,
1669 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1672 &sensor_dev_attr_temp2_input.dev_attr.attr,
1673 &sensor_dev_attr_temp2_max.dev_attr.attr,
1674 &sensor_dev_attr_temp2_min.dev_attr.attr,
1675 &sensor_dev_attr_temp2_type.dev_attr.attr,
1676 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1679 &sensor_dev_attr_temp3_input.dev_attr.attr,
1680 &sensor_dev_attr_temp3_max.dev_attr.attr,
1681 &sensor_dev_attr_temp3_min.dev_attr.attr,
1682 &sensor_dev_attr_temp3_type.dev_attr.attr,
1683 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1687 static const struct attribute_group it87_group_temp[3] = {
1688 { .attrs = it87_attributes_temp[0] },
1689 { .attrs = it87_attributes_temp[1] },
1690 { .attrs = it87_attributes_temp[2] },
1693 static struct attribute *it87_attributes_temp_offset[] = {
1694 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1695 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1696 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1699 static struct attribute *it87_attributes[] = {
1700 &dev_attr_alarms.attr,
1701 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1702 &dev_attr_name.attr,
1706 static const struct attribute_group it87_group = {
1707 .attrs = it87_attributes,
1710 static struct attribute *it87_attributes_in_beep[] = {
1711 &sensor_dev_attr_in0_beep.dev_attr.attr,
1712 &sensor_dev_attr_in1_beep.dev_attr.attr,
1713 &sensor_dev_attr_in2_beep.dev_attr.attr,
1714 &sensor_dev_attr_in3_beep.dev_attr.attr,
1715 &sensor_dev_attr_in4_beep.dev_attr.attr,
1716 &sensor_dev_attr_in5_beep.dev_attr.attr,
1717 &sensor_dev_attr_in6_beep.dev_attr.attr,
1718 &sensor_dev_attr_in7_beep.dev_attr.attr,
1723 static struct attribute *it87_attributes_temp_beep[] = {
1724 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1725 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1726 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1729 static struct attribute *it87_attributes_fan[6][3+1] = { {
1730 &sensor_dev_attr_fan1_input.dev_attr.attr,
1731 &sensor_dev_attr_fan1_min.dev_attr.attr,
1732 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1735 &sensor_dev_attr_fan2_input.dev_attr.attr,
1736 &sensor_dev_attr_fan2_min.dev_attr.attr,
1737 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1740 &sensor_dev_attr_fan3_input.dev_attr.attr,
1741 &sensor_dev_attr_fan3_min.dev_attr.attr,
1742 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1745 &sensor_dev_attr_fan4_input.dev_attr.attr,
1746 &sensor_dev_attr_fan4_min.dev_attr.attr,
1747 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1750 &sensor_dev_attr_fan5_input.dev_attr.attr,
1751 &sensor_dev_attr_fan5_min.dev_attr.attr,
1752 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1755 &sensor_dev_attr_fan6_input.dev_attr.attr,
1756 &sensor_dev_attr_fan6_min.dev_attr.attr,
1757 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1761 static const struct attribute_group it87_group_fan[6] = {
1762 { .attrs = it87_attributes_fan[0] },
1763 { .attrs = it87_attributes_fan[1] },
1764 { .attrs = it87_attributes_fan[2] },
1765 { .attrs = it87_attributes_fan[3] },
1766 { .attrs = it87_attributes_fan[4] },
1767 { .attrs = it87_attributes_fan[5] },
1770 static const struct attribute *it87_attributes_fan_div[] = {
1771 &sensor_dev_attr_fan1_div.dev_attr.attr,
1772 &sensor_dev_attr_fan2_div.dev_attr.attr,
1773 &sensor_dev_attr_fan3_div.dev_attr.attr,
1776 static struct attribute *it87_attributes_pwm[6][4+1] = { {
1777 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1778 &sensor_dev_attr_pwm1.dev_attr.attr,
1779 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1780 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1783 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1784 &sensor_dev_attr_pwm2.dev_attr.attr,
1785 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1786 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1789 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1790 &sensor_dev_attr_pwm3.dev_attr.attr,
1791 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1792 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1795 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1796 &sensor_dev_attr_pwm4.dev_attr.attr,
1797 &sensor_dev_attr_pwm4_freq.dev_attr.attr,
1798 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1801 &sensor_dev_attr_pwm5_enable.dev_attr.attr,
1802 &sensor_dev_attr_pwm5.dev_attr.attr,
1803 &sensor_dev_attr_pwm5_freq.dev_attr.attr,
1804 &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
1807 &sensor_dev_attr_pwm6_enable.dev_attr.attr,
1808 &sensor_dev_attr_pwm6.dev_attr.attr,
1809 &sensor_dev_attr_pwm6_freq.dev_attr.attr,
1810 &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
1814 static umode_t pwm_attribute_mode(struct kobject *kobj, struct attribute *attr,
1817 struct device *dev = container_of(kobj, struct device, kobj);
1818 struct it87_data *data = dev_get_drvdata(dev);
1820 if (has_pwm_freq2(data) && index == 2)
1821 return attr->mode | S_IWUSR;
1826 static const struct attribute_group it87_group_pwm[6] = {
1827 { .attrs = it87_attributes_pwm[0] },
1828 { .attrs = it87_attributes_pwm[1],
1829 .is_visible = pwm_attribute_mode, },
1830 { .attrs = it87_attributes_pwm[2] },
1831 { .attrs = it87_attributes_pwm[3] },
1832 { .attrs = it87_attributes_pwm[4] },
1833 { .attrs = it87_attributes_pwm[5] },
1836 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1837 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1838 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1839 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1840 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1841 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1842 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1843 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1844 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1845 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1848 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1849 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1850 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1851 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1852 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1853 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1854 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1855 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1856 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1859 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1860 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1861 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1862 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1863 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1864 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1865 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1866 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1867 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1871 static const struct attribute_group it87_group_autopwm[3] = {
1872 { .attrs = it87_attributes_autopwm[0] },
1873 { .attrs = it87_attributes_autopwm[1] },
1874 { .attrs = it87_attributes_autopwm[2] },
1877 static struct attribute *it87_attributes_fan_beep[] = {
1878 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1879 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1880 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1881 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1882 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1883 &sensor_dev_attr_fan6_beep.dev_attr.attr,
1886 static struct attribute *it87_attributes_vid[] = {
1888 &dev_attr_cpu0_vid.attr,
1892 static const struct attribute_group it87_group_vid = {
1893 .attrs = it87_attributes_vid,
1896 static struct attribute *it87_attributes_label[] = {
1897 &sensor_dev_attr_in3_label.dev_attr.attr,
1898 &sensor_dev_attr_in7_label.dev_attr.attr,
1899 &sensor_dev_attr_in8_label.dev_attr.attr,
1900 &sensor_dev_attr_in9_label.dev_attr.attr,
1904 static const struct attribute_group it87_group_label = {
1905 .attrs = it87_attributes_label,
1908 /* SuperIO detection - will change isa_address if a chip is found */
1909 static int __init it87_find(int sioaddr, unsigned short *address,
1910 struct it87_sio_data *sio_data)
1914 const char *board_vendor, *board_name;
1916 err = superio_enter(sioaddr);
1921 chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
1923 switch (chip_type) {
1925 sio_data->type = it87;
1928 sio_data->type = it8712;
1932 sio_data->type = it8716;
1935 sio_data->type = it8718;
1938 sio_data->type = it8720;
1941 sio_data->type = it8721;
1944 sio_data->type = it8728;
1947 sio_data->type = it8771;
1950 sio_data->type = it8772;
1953 sio_data->type = it8781;
1956 sio_data->type = it8782;
1959 sio_data->type = it8783;
1962 sio_data->type = it8786;
1965 sio_data->type = it8790;
1969 sio_data->type = it8603;
1972 sio_data->type = it8620;
1974 case 0xffff: /* No device at all */
1977 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1981 superio_select(sioaddr, PME);
1982 if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
1983 pr_info("Device not activated, skipping\n");
1987 *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1988 if (*address == 0) {
1989 pr_info("Base address not set, skipping\n");
1994 sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
1995 pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
1996 it87_devices[sio_data->type].suffix,
1997 *address, sio_data->revision);
1999 /* in8 (Vbat) is always internal */
2000 sio_data->internal = (1 << 2);
2002 /* in7 (VSB or VCCH5V) is always internal on some chips */
2003 if (it87_devices[sio_data->type].features & FEAT_IN7_INTERNAL)
2004 sio_data->internal |= (1 << 1);
2006 /* in9 (AVCC3), always internal if supported */
2007 if (it87_devices[sio_data->type].features & FEAT_AVCC3)
2008 sio_data->internal |= (1 << 3); /* in9 is AVCC */
2010 sio_data->skip_in |= (1 << 9);
2012 if (!(it87_devices[sio_data->type].features & FEAT_SIX_PWM))
2013 sio_data->skip_pwm |= (1 << 3) | (1 << 4) | (1 << 5);
2015 if (!(it87_devices[sio_data->type].features & FEAT_VID))
2016 sio_data->skip_vid = 1;
2018 /* Read GPIO config and VID value from LDN 7 (GPIO) */
2019 if (sio_data->type == it87) {
2020 /* The IT8705F has a different LD number for GPIO */
2021 superio_select(sioaddr, 5);
2022 sio_data->beep_pin = superio_inb(sioaddr,
2023 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2024 } else if (sio_data->type == it8783) {
2025 int reg25, reg27, reg2a, reg2c, regef;
2027 superio_select(sioaddr, GPIO);
2029 reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2030 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2031 reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
2032 reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2033 regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
2035 /* Check if fan3 is there or not */
2036 if ((reg27 & (1 << 0)) || !(reg2c & (1 << 2)))
2037 sio_data->skip_fan |= (1 << 2);
2038 if ((reg25 & (1 << 4))
2039 || (!(reg2a & (1 << 1)) && (regef & (1 << 0))))
2040 sio_data->skip_pwm |= (1 << 2);
2042 /* Check if fan2 is there or not */
2043 if (reg27 & (1 << 7))
2044 sio_data->skip_fan |= (1 << 1);
2045 if (reg27 & (1 << 3))
2046 sio_data->skip_pwm |= (1 << 1);
2049 if ((reg27 & (1 << 0)) || (reg2c & (1 << 2)))
2050 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2053 if (reg27 & (1 << 1))
2054 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2058 * Does not depend on bit 2 of Reg2C, contrary to datasheet.
2060 if (reg27 & (1 << 2)) {
2062 * The data sheet is a bit unclear regarding the
2063 * internal voltage divider for VCCH5V. It says
2064 * "This bit enables and switches VIN7 (pin 91) to the
2065 * internal voltage divider for VCCH5V".
2066 * This is different to other chips, where the internal
2067 * voltage divider would connect VIN7 to an internal
2068 * voltage source. Maybe that is the case here as well.
2070 * Since we don't know for sure, re-route it if that is
2071 * not the case, and ask the user to report if the
2072 * resulting voltage is sane.
2074 if (!(reg2c & (1 << 1))) {
2076 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg2c);
2077 pr_notice("Routing internal VCCH5V to in7.\n");
2079 pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
2080 pr_notice("Please report if it displays a reasonable voltage.\n");
2083 if (reg2c & (1 << 0))
2084 sio_data->internal |= (1 << 0);
2085 if (reg2c & (1 << 1))
2086 sio_data->internal |= (1 << 1);
2088 sio_data->beep_pin = superio_inb(sioaddr,
2089 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2090 } else if (sio_data->type == it8603) {
2093 superio_select(sioaddr, GPIO);
2095 reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2097 /* Check if fan3 is there or not */
2098 if (reg27 & (1 << 6))
2099 sio_data->skip_pwm |= (1 << 2);
2100 if (reg27 & (1 << 7))
2101 sio_data->skip_fan |= (1 << 2);
2103 /* Check if fan2 is there or not */
2104 reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2105 if (reg29 & (1 << 1))
2106 sio_data->skip_pwm |= (1 << 1);
2107 if (reg29 & (1 << 2))
2108 sio_data->skip_fan |= (1 << 1);
2110 sio_data->skip_in |= (1 << 5); /* No VIN5 */
2111 sio_data->skip_in |= (1 << 6); /* No VIN6 */
2113 sio_data->beep_pin = superio_inb(sioaddr,
2114 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2115 } else if (sio_data->type == it8620) {
2118 superio_select(sioaddr, GPIO);
2120 /* Check for pwm5 */
2121 reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
2123 sio_data->skip_pwm |= (1 << 4);
2125 /* Check for fan4, fan5 */
2126 reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
2127 if (!(reg & (1 << 5)))
2128 sio_data->skip_fan |= (1 << 3);
2129 if (!(reg & (1 << 4)))
2130 sio_data->skip_fan |= (1 << 4);
2132 /* Check for pwm3, fan3 */
2133 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2135 sio_data->skip_pwm |= (1 << 2);
2137 sio_data->skip_fan |= (1 << 2);
2139 /* Check for pwm4 */
2140 reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
2141 if (!(reg & (1 << 4)))
2142 sio_data->skip_pwm |= (1 << 3);
2144 /* Check for pwm2, fan2 */
2145 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2147 sio_data->skip_pwm |= (1 << 1);
2149 sio_data->skip_fan |= (1 << 1);
2150 /* Check for pwm6, fan6 */
2151 if (!(reg & (1 << 7))) {
2152 sio_data->skip_pwm |= (1 << 5);
2153 sio_data->skip_fan |= (1 << 5);
2156 sio_data->beep_pin = superio_inb(sioaddr,
2157 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2162 superio_select(sioaddr, GPIO);
2164 reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
2165 if (!sio_data->skip_vid) {
2166 /* We need at least 4 VID pins */
2168 pr_info("VID is disabled (pins used for GPIO)\n");
2169 sio_data->skip_vid = 1;
2173 /* Check if fan3 is there or not */
2175 sio_data->skip_pwm |= (1 << 2);
2177 sio_data->skip_fan |= (1 << 2);
2179 /* Check if fan2 is there or not */
2180 reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
2182 sio_data->skip_pwm |= (1 << 1);
2184 sio_data->skip_fan |= (1 << 1);
2186 if ((sio_data->type == it8718 || sio_data->type == it8720)
2187 && !(sio_data->skip_vid))
2188 sio_data->vid_value = superio_inb(sioaddr,
2191 reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
2193 uart6 = sio_data->type == it8782 && (reg & (1 << 2));
2196 * The IT8720F has no VIN7 pin, so VCCH should always be
2197 * routed internally to VIN7 with an internal divider.
2198 * Curiously, there still is a configuration bit to control
2199 * this, which means it can be set incorrectly. And even
2200 * more curiously, many boards out there are improperly
2201 * configured, even though the IT8720F datasheet claims
2202 * that the internal routing of VCCH to VIN7 is the default
2203 * setting. So we force the internal routing in this case.
2205 * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
2206 * If UART6 is enabled, re-route VIN7 to the internal divider
2207 * if that is not already the case.
2209 if ((sio_data->type == it8720 || uart6) && !(reg & (1 << 1))) {
2211 superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
2212 pr_notice("Routing internal VCCH to in7\n");
2215 sio_data->internal |= (1 << 0);
2217 sio_data->internal |= (1 << 1);
2220 * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
2221 * While VIN7 can be routed to the internal voltage divider,
2222 * VIN5 and VIN6 are not available if UART6 is enabled.
2224 * Also, temp3 is not available if UART6 is enabled and TEMPIN3
2225 * is the temperature source. Since we can not read the
2226 * temperature source here, skip_temp is preliminary.
2229 sio_data->skip_in |= (1 << 5) | (1 << 6);
2230 sio_data->skip_temp |= (1 << 2);
2233 sio_data->beep_pin = superio_inb(sioaddr,
2234 IT87_SIO_BEEP_PIN_REG) & 0x3f;
2236 if (sio_data->beep_pin)
2237 pr_info("Beeping is supported\n");
2239 /* Disable specific features based on DMI strings */
2240 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
2241 board_name = dmi_get_system_info(DMI_BOARD_NAME);
2242 if (board_vendor && board_name) {
2243 if (strcmp(board_vendor, "nVIDIA") == 0
2244 && strcmp(board_name, "FN68PT") == 0) {
2246 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
2247 * connected to a fan, but to something else. One user
2248 * has reported instant system power-off when changing
2249 * the PWM2 duty cycle, so we disable it.
2250 * I use the board name string as the trigger in case
2251 * the same board is ever used in other systems.
2253 pr_info("Disabling pwm2 due to hardware constraints\n");
2254 sio_data->skip_pwm = (1 << 1);
2259 superio_exit(sioaddr);
2263 static void it87_remove_files(struct device *dev)
2265 struct it87_data *data = dev_get_drvdata(dev);
2266 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2269 sysfs_remove_group(&dev->kobj, &it87_group);
2270 for (i = 0; i < 10; i++) {
2271 if (sio_data->skip_in & (1 << i))
2273 sysfs_remove_group(&dev->kobj, &it87_group_in[i]);
2274 if (it87_attributes_in_beep[i])
2275 sysfs_remove_file(&dev->kobj,
2276 it87_attributes_in_beep[i]);
2278 for (i = 0; i < 3; i++) {
2279 if (!(data->has_temp & (1 << i)))
2281 sysfs_remove_group(&dev->kobj, &it87_group_temp[i]);
2282 if (has_temp_offset(data))
2283 sysfs_remove_file(&dev->kobj,
2284 it87_attributes_temp_offset[i]);
2285 if (sio_data->beep_pin)
2286 sysfs_remove_file(&dev->kobj,
2287 it87_attributes_temp_beep[i]);
2289 for (i = 0; i < 6; i++) {
2290 if (!(data->has_fan & (1 << i)))
2292 sysfs_remove_group(&dev->kobj, &it87_group_fan[i]);
2293 if (sio_data->beep_pin)
2294 sysfs_remove_file(&dev->kobj,
2295 it87_attributes_fan_beep[i]);
2296 if (i < 3 && !has_16bit_fans(data))
2297 sysfs_remove_file(&dev->kobj,
2298 it87_attributes_fan_div[i]);
2300 for (i = 0; i < 6; i++) {
2301 if (sio_data->skip_pwm & (1 << i))
2303 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
2304 if (has_old_autopwm(data))
2305 sysfs_remove_group(&dev->kobj,
2306 &it87_group_autopwm[i]);
2308 if (!sio_data->skip_vid)
2309 sysfs_remove_group(&dev->kobj, &it87_group_vid);
2310 sysfs_remove_group(&dev->kobj, &it87_group_label);
2313 static int it87_probe(struct platform_device *pdev)
2315 struct it87_data *data;
2316 struct resource *res;
2317 struct device *dev = &pdev->dev;
2318 struct it87_sio_data *sio_data = dev_get_platdata(dev);
2320 int enable_pwm_interface;
2321 int fan_beep_need_rw;
2323 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
2324 if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
2326 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
2327 (unsigned long)res->start,
2328 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
2332 data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
2336 data->addr = res->start;
2337 data->type = sio_data->type;
2338 data->features = it87_devices[sio_data->type].features;
2339 data->peci_mask = it87_devices[sio_data->type].peci_mask;
2340 data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
2341 data->name = it87_devices[sio_data->type].name;
2343 * IT8705F Datasheet 0.4.1, 3h == Version G.
2344 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
2345 * These are the first revisions with 16-bit tachometer support.
2347 switch (data->type) {
2349 if (sio_data->revision >= 0x03) {
2350 data->features &= ~FEAT_OLD_AUTOPWM;
2351 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
2355 if (sio_data->revision >= 0x08) {
2356 data->features &= ~FEAT_OLD_AUTOPWM;
2357 data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
2365 /* Now, we do the remaining detection. */
2366 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
2367 || it87_read_value(data, IT87_REG_CHIPID) != 0x90)
2370 platform_set_drvdata(pdev, data);
2372 mutex_init(&data->update_lock);
2374 /* Check PWM configuration */
2375 enable_pwm_interface = it87_check_pwm(dev);
2377 /* Starting with IT8721F, we handle scaling of internal voltages */
2378 if (has_12mv_adc(data)) {
2379 if (sio_data->internal & (1 << 0))
2380 data->in_scaled |= (1 << 3); /* in3 is AVCC */
2381 if (sio_data->internal & (1 << 1))
2382 data->in_scaled |= (1 << 7); /* in7 is VSB */
2383 if (sio_data->internal & (1 << 2))
2384 data->in_scaled |= (1 << 8); /* in8 is Vbat */
2385 if (sio_data->internal & (1 << 3))
2386 data->in_scaled |= (1 << 9); /* in9 is AVCC */
2387 } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
2388 sio_data->type == it8783) {
2389 if (sio_data->internal & (1 << 0))
2390 data->in_scaled |= (1 << 3); /* in3 is VCC5V */
2391 if (sio_data->internal & (1 << 1))
2392 data->in_scaled |= (1 << 7); /* in7 is VCCH5V */
2395 data->has_temp = 0x07;
2396 if (sio_data->skip_temp & (1 << 2)) {
2397 if (sio_data->type == it8782
2398 && !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
2399 data->has_temp &= ~(1 << 2);
2402 /* Initialize the IT87 chip */
2403 it87_init_device(pdev);
2405 /* Register sysfs hooks */
2406 err = sysfs_create_group(&dev->kobj, &it87_group);
2410 for (i = 0; i < 10; i++) {
2411 if (sio_data->skip_in & (1 << i))
2413 err = sysfs_create_group(&dev->kobj, &it87_group_in[i]);
2416 if (sio_data->beep_pin && it87_attributes_in_beep[i]) {
2417 err = sysfs_create_file(&dev->kobj,
2418 it87_attributes_in_beep[i]);
2424 for (i = 0; i < 3; i++) {
2425 if (!(data->has_temp & (1 << i)))
2427 err = sysfs_create_group(&dev->kobj, &it87_group_temp[i]);
2430 if (has_temp_offset(data)) {
2431 err = sysfs_create_file(&dev->kobj,
2432 it87_attributes_temp_offset[i]);
2436 if (sio_data->beep_pin) {
2437 err = sysfs_create_file(&dev->kobj,
2438 it87_attributes_temp_beep[i]);
2444 /* Do not create fan files for disabled fans */
2445 fan_beep_need_rw = 1;
2446 for (i = 0; i < 6; i++) {
2447 if (!(data->has_fan & (1 << i)))
2449 err = sysfs_create_group(&dev->kobj, &it87_group_fan[i]);
2453 if (i < 3 && !has_16bit_fans(data)) {
2454 err = sysfs_create_file(&dev->kobj,
2455 it87_attributes_fan_div[i]);
2460 if (sio_data->beep_pin) {
2461 err = sysfs_create_file(&dev->kobj,
2462 it87_attributes_fan_beep[i]);
2465 if (!fan_beep_need_rw)
2469 * As we have a single beep enable bit for all fans,
2470 * only the first enabled fan has a writable attribute
2473 if (sysfs_chmod_file(&dev->kobj,
2474 it87_attributes_fan_beep[i],
2476 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
2478 fan_beep_need_rw = 0;
2482 if (enable_pwm_interface) {
2483 for (i = 0; i < 6; i++) {
2484 if (sio_data->skip_pwm & (1 << i))
2486 err = sysfs_create_group(&dev->kobj,
2487 &it87_group_pwm[i]);
2491 if (!has_old_autopwm(data))
2493 err = sysfs_create_group(&dev->kobj,
2494 &it87_group_autopwm[i]);
2500 if (!sio_data->skip_vid) {
2501 data->vrm = vid_which_vrm();
2502 /* VID reading from Super-I/O config space if available */
2503 data->vid = sio_data->vid_value;
2504 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
2509 /* Export labels for internal sensors */
2510 for (i = 0; i < 4; i++) {
2511 if (!(sio_data->internal & (1 << i)))
2513 err = sysfs_create_file(&dev->kobj,
2514 it87_attributes_label[i]);
2519 data->hwmon_dev = hwmon_device_register(dev);
2520 if (IS_ERR(data->hwmon_dev)) {
2521 err = PTR_ERR(data->hwmon_dev);
2528 it87_remove_files(dev);
2532 static int it87_remove(struct platform_device *pdev)
2534 struct it87_data *data = platform_get_drvdata(pdev);
2536 hwmon_device_unregister(data->hwmon_dev);
2537 it87_remove_files(&pdev->dev);
2543 * Must be called with data->update_lock held, except during initialization.
2544 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2545 * would slow down the IT87 access and should not be necessary.
2547 static int it87_read_value(struct it87_data *data, u8 reg)
2549 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2550 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2554 * Must be called with data->update_lock held, except during initialization.
2555 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2556 * would slow down the IT87 access and should not be necessary.
2558 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2560 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2561 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2564 /* Return 1 if and only if the PWM interface is safe to use */
2565 static int it87_check_pwm(struct device *dev)
2567 struct it87_data *data = dev_get_drvdata(dev);
2569 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2570 * and polarity set to active low is sign that this is the case so we
2571 * disable pwm control to protect the user.
2573 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2574 if ((tmp & 0x87) == 0) {
2575 if (fix_pwm_polarity) {
2577 * The user asks us to attempt a chip reconfiguration.
2578 * This means switching to active high polarity and
2579 * inverting all fan speed values.
2584 for (i = 0; i < 3; i++)
2585 pwm[i] = it87_read_value(data,
2589 * If any fan is in automatic pwm mode, the polarity
2590 * might be correct, as suspicious as it seems, so we
2591 * better don't change anything (but still disable the
2594 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2596 "Reconfiguring PWM to active high polarity\n");
2597 it87_write_value(data, IT87_REG_FAN_CTL,
2599 for (i = 0; i < 3; i++)
2600 it87_write_value(data,
2607 "PWM configuration is too broken to be fixed\n");
2611 "Detected broken BIOS defaults, disabling PWM interface\n");
2613 } else if (fix_pwm_polarity) {
2615 "PWM configuration looks sane, won't touch\n");
2621 /* Called when we have found a new IT87. */
2622 static void it87_init_device(struct platform_device *pdev)
2624 struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2625 struct it87_data *data = platform_get_drvdata(pdev);
2630 * For each PWM channel:
2631 * - If it is in automatic mode, setting to manual mode should set
2632 * the fan to full speed by default.
2633 * - If it is in manual mode, we need a mapping to temperature
2634 * channels to use when later setting to automatic mode later.
2635 * Use a 1:1 mapping by default (we are clueless.)
2636 * In both cases, the value can (and should) be changed by the user
2637 * prior to switching to a different mode.
2638 * Note that this is no longer needed for the IT8721F and later, as
2639 * these have separate registers for the temperature mapping and the
2640 * manual duty cycle.
2642 for (i = 0; i < 3; i++) {
2643 data->pwm_temp_map[i] = i;
2644 data->pwm_duty[i] = 0x7f; /* Full speed */
2645 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2649 * Some chips seem to have default value 0xff for all limit
2650 * registers. For low voltage limits it makes no sense and triggers
2651 * alarms, so change to 0 instead. For high temperature limits, it
2652 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2653 * but is still confusing, so change to 127 degrees C.
2655 for (i = 0; i < 8; i++) {
2656 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2658 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2660 for (i = 0; i < 3; i++) {
2661 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2663 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2667 * Temperature channels are not forcibly enabled, as they can be
2668 * set to two different sensor types and we can't guess which one
2669 * is correct for a given system. These channels can be enabled at
2670 * run-time through the temp{1-3}_type sysfs accessors if needed.
2673 /* Check if voltage monitors are reset manually or by some reason */
2674 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2675 if ((tmp & 0xff) == 0) {
2676 /* Enable all voltage monitors */
2677 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2680 /* Check if tachometers are reset manually or by some reason */
2681 mask = 0x70 & ~(sio_data->skip_fan << 4);
2682 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2683 if ((data->fan_main_ctrl & mask) == 0) {
2684 /* Enable all fan tachometers */
2685 data->fan_main_ctrl |= mask;
2686 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2687 data->fan_main_ctrl);
2689 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2691 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2693 /* Set tachometers to 16-bit mode if needed */
2694 if (has_fan16_config(data)) {
2695 if (~tmp & 0x07 & data->has_fan) {
2697 "Setting fan1-3 to 16-bit mode\n");
2698 it87_write_value(data, IT87_REG_FAN_16BIT,
2703 /* Check for additional fans */
2704 if (has_five_fans(data)) {
2706 data->has_fan |= (1 << 3); /* fan4 enabled */
2708 data->has_fan |= (1 << 4); /* fan5 enabled */
2711 if (has_six_fans(data)) {
2713 data->has_fan |= (1 << 5); /* fan6 enabled */
2716 /* Fan input pins may be used for alternative functions */
2717 data->has_fan &= ~sio_data->skip_fan;
2719 /* Check if pwm5, pwm6 are enabled */
2720 if (has_six_pwm(data)) {
2721 /* The following code may be IT8620E specific */
2722 tmp = it87_read_value(data, IT87_REG_FAN_DIV);
2723 if ((tmp & 0xc0) == 0xc0)
2724 sio_data->skip_pwm |= (1 << 4);
2725 if (!(tmp & (1 << 3)))
2726 sio_data->skip_pwm |= (1 << 5);
2729 /* Start monitoring */
2730 it87_write_value(data, IT87_REG_CONFIG,
2731 (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
2732 | (update_vbat ? 0x41 : 0x01));
2735 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2737 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
2738 if (has_newer_autopwm(data)) {
2739 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2740 data->pwm_duty[nr] = it87_read_value(data,
2741 IT87_REG_PWM_DUTY[nr]);
2743 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2744 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2745 else /* Manual mode */
2746 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2749 if (has_old_autopwm(data)) {
2752 for (i = 0; i < 5 ; i++)
2753 data->auto_temp[nr][i] = it87_read_value(data,
2754 IT87_REG_AUTO_TEMP(nr, i));
2755 for (i = 0; i < 3 ; i++)
2756 data->auto_pwm[nr][i] = it87_read_value(data,
2757 IT87_REG_AUTO_PWM(nr, i));
2761 static struct it87_data *it87_update_device(struct device *dev)
2763 struct it87_data *data = dev_get_drvdata(dev);
2766 mutex_lock(&data->update_lock);
2768 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2772 * Cleared after each update, so reenable. Value
2773 * returned by this read will be previous value
2775 it87_write_value(data, IT87_REG_CONFIG,
2776 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2778 for (i = 0; i <= 7; i++) {
2780 it87_read_value(data, IT87_REG_VIN(i));
2782 it87_read_value(data, IT87_REG_VIN_MIN(i));
2784 it87_read_value(data, IT87_REG_VIN_MAX(i));
2786 /* in8 (battery) has no limit registers */
2787 data->in[8][0] = it87_read_value(data, IT87_REG_VIN(8));
2788 if (has_avcc3(data))
2789 data->in[9][0] = it87_read_value(data, IT87_REG_AVCC3);
2791 for (i = 0; i < 6; i++) {
2792 /* Skip disabled fans */
2793 if (!(data->has_fan & (1 << i)))
2797 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2798 data->fan[i][0] = it87_read_value(data,
2800 /* Add high byte if in 16-bit mode */
2801 if (has_16bit_fans(data)) {
2802 data->fan[i][0] |= it87_read_value(data,
2803 IT87_REG_FANX[i]) << 8;
2804 data->fan[i][1] |= it87_read_value(data,
2805 IT87_REG_FANX_MIN[i]) << 8;
2808 for (i = 0; i < 3; i++) {
2809 if (!(data->has_temp & (1 << i)))
2812 it87_read_value(data, IT87_REG_TEMP(i));
2814 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2816 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2817 if (has_temp_offset(data))
2819 it87_read_value(data,
2820 IT87_REG_TEMP_OFFSET[i]);
2823 /* Newer chips don't have clock dividers */
2824 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2825 i = it87_read_value(data, IT87_REG_FAN_DIV);
2826 data->fan_div[0] = i & 0x07;
2827 data->fan_div[1] = (i >> 3) & 0x07;
2828 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2832 it87_read_value(data, IT87_REG_ALARM1) |
2833 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2834 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2835 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2837 data->fan_main_ctrl = it87_read_value(data,
2838 IT87_REG_FAN_MAIN_CTRL);
2839 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2840 for (i = 0; i < 3; i++)
2841 it87_update_pwm_ctrl(data, i);
2843 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2844 data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
2846 * The IT8705F does not have VID capability.
2847 * The IT8718F and later don't use IT87_REG_VID for the
2850 if (data->type == it8712 || data->type == it8716) {
2851 data->vid = it87_read_value(data, IT87_REG_VID);
2853 * The older IT8712F revisions had only 5 VID pins,
2854 * but we assume it is always safe to read 6 bits.
2858 data->last_updated = jiffies;
2862 mutex_unlock(&data->update_lock);
2867 static int __init it87_device_add(int index, unsigned short address,
2868 const struct it87_sio_data *sio_data)
2870 struct platform_device *pdev;
2871 struct resource res = {
2872 .start = address + IT87_EC_OFFSET,
2873 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2875 .flags = IORESOURCE_IO,
2879 err = acpi_check_resource_conflict(&res);
2883 pdev = platform_device_alloc(DRVNAME, address);
2887 err = platform_device_add_resources(pdev, &res, 1);
2889 pr_err("Device resource addition failed (%d)\n", err);
2890 goto exit_device_put;
2893 err = platform_device_add_data(pdev, sio_data,
2894 sizeof(struct it87_sio_data));
2896 pr_err("Platform data allocation failed\n");
2897 goto exit_device_put;
2900 err = platform_device_add(pdev);
2902 pr_err("Device addition failed (%d)\n", err);
2903 goto exit_device_put;
2906 it87_pdev[index] = pdev;
2910 platform_device_put(pdev);
2914 static int __init sm_it87_init(void)
2916 int sioaddr[2] = { REG_2E, REG_4E };
2917 struct it87_sio_data sio_data;
2918 unsigned short isa_address;
2922 err = platform_driver_register(&it87_driver);
2926 for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
2927 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2929 err = it87_find(sioaddr[i], &isa_address, &sio_data);
2930 if (err || isa_address == 0)
2933 err = it87_device_add(i, isa_address, &sio_data);
2935 goto exit_dev_unregister;
2941 goto exit_unregister;
2945 exit_dev_unregister:
2946 /* NULL check handled by platform_device_unregister */
2947 platform_device_unregister(it87_pdev[0]);
2949 platform_driver_unregister(&it87_driver);
2953 static void __exit sm_it87_exit(void)
2955 /* NULL check handled by platform_device_unregister */
2956 platform_device_unregister(it87_pdev[1]);
2957 platform_device_unregister(it87_pdev[0]);
2958 platform_driver_unregister(&it87_driver);
2962 MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
2963 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2964 module_param(update_vbat, bool, 0);
2965 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2966 module_param(fix_pwm_polarity, bool, 0);
2967 MODULE_PARM_DESC(fix_pwm_polarity,
2968 "Force PWM polarity to active high (DANGEROUS)");
2969 MODULE_LICENSE("GPL");
2971 module_init(sm_it87_init);
2972 module_exit(sm_it87_exit);