2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 * addition to an Environment Controller (Enhanced Hardware Monitor and
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
13 * Supports: IT8705F Super I/O chip w/LPC interface
14 * IT8712F Super I/O chip w/LPC interface
15 * IT8716F Super I/O chip w/LPC interface
16 * IT8718F Super I/O chip w/LPC interface
17 * IT8720F Super I/O chip w/LPC interface
18 * IT8721F Super I/O chip w/LPC interface
19 * IT8726F Super I/O chip w/LPC interface
20 * IT8728F Super I/O chip w/LPC interface
21 * IT8758E Super I/O chip w/LPC interface
22 * Sis950 A clone of the IT8705F
24 * Copyright (C) 2001 Chris Gauthron
25 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 #include <linux/module.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <linux/jiffies.h>
48 #include <linux/platform_device.h>
49 #include <linux/hwmon.h>
50 #include <linux/hwmon-sysfs.h>
51 #include <linux/hwmon-vid.h>
52 #include <linux/err.h>
53 #include <linux/mutex.h>
54 #include <linux/sysfs.h>
55 #include <linux/string.h>
56 #include <linux/dmi.h>
57 #include <linux/acpi.h>
60 #define DRVNAME "it87"
62 enum chips
{ it87
, it8712
, it8716
, it8718
, it8720
, it8721
, it8728
};
64 static unsigned short force_id
;
65 module_param(force_id
, ushort
, 0);
66 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
68 static struct platform_device
*pdev
;
70 #define REG 0x2e /* The register to read/write */
71 #define DEV 0x07 /* Register: Logical device select */
72 #define VAL 0x2f /* The value to read/write */
73 #define PME 0x04 /* The device with the fan registers in it */
75 /* The device with the IT8718F/IT8720F VID value in it */
78 #define DEVID 0x20 /* Register: Device ID */
79 #define DEVREV 0x22 /* Register: Device Revision */
81 static inline int superio_inb(int reg
)
87 static inline void superio_outb(int reg
, int val
)
93 static int superio_inw(int reg
)
103 static inline void superio_select(int ldn
)
109 static inline int superio_enter(void)
112 * Try to reserve REG and REG + 1 for exclusive access.
114 if (!request_muxed_region(REG
, 2, DRVNAME
))
124 static inline void superio_exit(void)
128 release_region(REG
, 2);
131 /* Logical device 4 registers */
132 #define IT8712F_DEVID 0x8712
133 #define IT8705F_DEVID 0x8705
134 #define IT8716F_DEVID 0x8716
135 #define IT8718F_DEVID 0x8718
136 #define IT8720F_DEVID 0x8720
137 #define IT8721F_DEVID 0x8721
138 #define IT8726F_DEVID 0x8726
139 #define IT8728F_DEVID 0x8728
140 #define IT87_ACT_REG 0x30
141 #define IT87_BASE_REG 0x60
143 /* Logical device 7 registers (IT8712F and later) */
144 #define IT87_SIO_GPIO3_REG 0x27
145 #define IT87_SIO_GPIO5_REG 0x29
146 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
147 #define IT87_SIO_VID_REG 0xfc /* VID value */
148 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
150 /* Update battery voltage after every reading if true */
151 static bool update_vbat
;
153 /* Not all BIOSes properly configure the PWM registers */
154 static bool fix_pwm_polarity
;
156 /* Many IT87 constants specified below */
158 /* Length of ISA address segment */
159 #define IT87_EXTENT 8
161 /* Length of ISA address segment for Environmental Controller */
162 #define IT87_EC_EXTENT 2
164 /* Offset of EC registers from ISA base address */
165 #define IT87_EC_OFFSET 5
167 /* Where are the ISA address/data registers relative to the EC base address */
168 #define IT87_ADDR_REG_OFFSET 0
169 #define IT87_DATA_REG_OFFSET 1
171 /*----- The IT87 registers -----*/
173 #define IT87_REG_CONFIG 0x00
175 #define IT87_REG_ALARM1 0x01
176 #define IT87_REG_ALARM2 0x02
177 #define IT87_REG_ALARM3 0x03
179 /* The IT8718F and IT8720F have the VID value in a different register, in
180 Super-I/O configuration space. */
181 #define IT87_REG_VID 0x0a
182 /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
183 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
185 #define IT87_REG_FAN_DIV 0x0b
186 #define IT87_REG_FAN_16BIT 0x0c
188 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
190 static const u8 IT87_REG_FAN
[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
191 static const u8 IT87_REG_FAN_MIN
[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
192 static const u8 IT87_REG_FANX
[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
193 static const u8 IT87_REG_FANX_MIN
[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
194 #define IT87_REG_FAN_MAIN_CTRL 0x13
195 #define IT87_REG_FAN_CTL 0x14
196 #define IT87_REG_PWM(nr) (0x15 + (nr))
197 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
199 #define IT87_REG_VIN(nr) (0x20 + (nr))
200 #define IT87_REG_TEMP(nr) (0x29 + (nr))
202 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
203 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
204 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
205 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
207 #define IT87_REG_VIN_ENABLE 0x50
208 #define IT87_REG_TEMP_ENABLE 0x51
209 #define IT87_REG_BEEP_ENABLE 0x5c
211 #define IT87_REG_CHIPID 0x58
213 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
214 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
217 struct it87_sio_data
{
219 /* Values read from Super-I/O config space */
223 u8 internal
; /* Internal sensors can be labeled */
224 /* Features skipped based on config or DMI */
230 /* For each registered chip, we need to keep some data in memory.
231 The structure is dynamically allocated. */
233 struct device
*hwmon_dev
;
239 struct mutex update_lock
;
240 char valid
; /* !=0 if following fields are valid */
241 unsigned long last_updated
; /* In jiffies */
243 u16 in_scaled
; /* Internal voltage sensors are scaled */
244 u8 in
[9]; /* Register value */
245 u8 in_max
[8]; /* Register value */
246 u8 in_min
[8]; /* Register value */
247 u8 has_fan
; /* Bitfield, fans enabled */
248 u16 fan
[5]; /* Register values, possibly combined */
249 u16 fan_min
[5]; /* Register values, possibly combined */
250 s8 temp
[3]; /* Register value */
251 s8 temp_high
[3]; /* Register value */
252 s8 temp_low
[3]; /* Register value */
253 u8 sensor
; /* Register value */
254 u8 fan_div
[3]; /* Register encoding, shifted right */
255 u8 vid
; /* Register encoding, combined */
257 u32 alarms
; /* Register encoding, combined */
258 u8 beeps
; /* Register encoding */
259 u8 fan_main_ctrl
; /* Register value */
260 u8 fan_ctl
; /* Register value */
262 /* The following 3 arrays correspond to the same registers up to
263 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
264 * 7, and we want to preserve settings on mode changes, so we have
265 * to track all values separately.
266 * Starting with the IT8721F, the manual PWM duty cycles are stored
267 * in separate registers (8-bit values), so the separate tracking
268 * is no longer needed, but it is still done to keep the driver
270 u8 pwm_ctrl
[3]; /* Register value */
271 u8 pwm_duty
[3]; /* Manual PWM value set by user */
272 u8 pwm_temp_map
[3]; /* PWM to temp. chan. mapping (bits 1-0) */
274 /* Automatic fan speed control registers */
275 u8 auto_pwm
[3][4]; /* [nr][3] is hard-coded */
276 s8 auto_temp
[3][5]; /* [nr][0] is point1_temp_hyst */
279 static inline int has_12mv_adc(const struct it87_data
*data
)
282 * IT8721F and later have a 12 mV ADC, also with internal scaling
283 * on selected inputs.
285 return data
->type
== it8721
286 || data
->type
== it8728
;
289 static inline int has_newer_autopwm(const struct it87_data
*data
)
292 * IT8721F and later have separate registers for the temperature
293 * mapping and the manual duty cycle.
295 return data
->type
== it8721
296 || data
->type
== it8728
;
299 static u8
in_to_reg(const struct it87_data
*data
, int nr
, long val
)
303 if (has_12mv_adc(data
)) {
304 if (data
->in_scaled
& (1 << nr
))
311 val
= DIV_ROUND_CLOSEST(val
, lsb
);
312 return SENSORS_LIMIT(val
, 0, 255);
315 static int in_from_reg(const struct it87_data
*data
, int nr
, int val
)
317 if (has_12mv_adc(data
)) {
318 if (data
->in_scaled
& (1 << nr
))
326 static inline u8
FAN_TO_REG(long rpm
, int div
)
330 rpm
= SENSORS_LIMIT(rpm
, 1, 1000000);
331 return SENSORS_LIMIT((1350000 + rpm
* div
/ 2) / (rpm
* div
), 1,
335 static inline u16
FAN16_TO_REG(long rpm
)
339 return SENSORS_LIMIT((1350000 + rpm
) / (rpm
* 2), 1, 0xfffe);
342 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
343 1350000 / ((val) * (div)))
344 /* The divider is fixed to 2 in 16-bit mode */
345 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
346 1350000 / ((val) * 2))
348 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
349 ((val) + 500) / 1000), -128, 127))
350 #define TEMP_FROM_REG(val) ((val) * 1000)
352 static u8
pwm_to_reg(const struct it87_data
*data
, long val
)
354 if (has_newer_autopwm(data
))
360 static int pwm_from_reg(const struct it87_data
*data
, u8 reg
)
362 if (has_newer_autopwm(data
))
365 return (reg
& 0x7f) << 1;
369 static int DIV_TO_REG(int val
)
372 while (answer
< 7 && (val
>>= 1))
376 #define DIV_FROM_REG(val) (1 << (val))
378 static const unsigned int pwm_freq
[8] = {
389 static inline int has_16bit_fans(const struct it87_data
*data
)
391 /* IT8705F Datasheet 0.4.1, 3h == Version G.
392 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
393 These are the first revisions with 16bit tachometer support. */
394 return (data
->type
== it87
&& data
->revision
>= 0x03)
395 || (data
->type
== it8712
&& data
->revision
>= 0x08)
396 || data
->type
== it8716
397 || data
->type
== it8718
398 || data
->type
== it8720
399 || data
->type
== it8721
400 || data
->type
== it8728
;
403 static inline int has_old_autopwm(const struct it87_data
*data
)
405 /* The old automatic fan speed control interface is implemented
406 by IT8705F chips up to revision F and IT8712F chips up to
408 return (data
->type
== it87
&& data
->revision
< 0x03)
409 || (data
->type
== it8712
&& data
->revision
< 0x08);
412 static int it87_probe(struct platform_device
*pdev
);
413 static int __devexit
it87_remove(struct platform_device
*pdev
);
415 static int it87_read_value(struct it87_data
*data
, u8 reg
);
416 static void it87_write_value(struct it87_data
*data
, u8 reg
, u8 value
);
417 static struct it87_data
*it87_update_device(struct device
*dev
);
418 static int it87_check_pwm(struct device
*dev
);
419 static void it87_init_device(struct platform_device
*pdev
);
422 static struct platform_driver it87_driver
= {
424 .owner
= THIS_MODULE
,
428 .remove
= __devexit_p(it87_remove
),
431 static ssize_t
show_in(struct device
*dev
, struct device_attribute
*attr
,
434 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
435 int nr
= sensor_attr
->index
;
437 struct it87_data
*data
= it87_update_device(dev
);
438 return sprintf(buf
, "%d\n", in_from_reg(data
, nr
, data
->in
[nr
]));
441 static ssize_t
show_in_min(struct device
*dev
, struct device_attribute
*attr
,
444 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
445 int nr
= sensor_attr
->index
;
447 struct it87_data
*data
= it87_update_device(dev
);
448 return sprintf(buf
, "%d\n", in_from_reg(data
, nr
, data
->in_min
[nr
]));
451 static ssize_t
show_in_max(struct device
*dev
, struct device_attribute
*attr
,
454 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
455 int nr
= sensor_attr
->index
;
457 struct it87_data
*data
= it87_update_device(dev
);
458 return sprintf(buf
, "%d\n", in_from_reg(data
, nr
, data
->in_max
[nr
]));
461 static ssize_t
set_in_min(struct device
*dev
, struct device_attribute
*attr
,
462 const char *buf
, size_t count
)
464 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
465 int nr
= sensor_attr
->index
;
467 struct it87_data
*data
= dev_get_drvdata(dev
);
470 if (kstrtoul(buf
, 10, &val
) < 0)
473 mutex_lock(&data
->update_lock
);
474 data
->in_min
[nr
] = in_to_reg(data
, nr
, val
);
475 it87_write_value(data
, IT87_REG_VIN_MIN(nr
),
477 mutex_unlock(&data
->update_lock
);
480 static ssize_t
set_in_max(struct device
*dev
, struct device_attribute
*attr
,
481 const char *buf
, size_t count
)
483 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
484 int nr
= sensor_attr
->index
;
486 struct it87_data
*data
= dev_get_drvdata(dev
);
489 if (kstrtoul(buf
, 10, &val
) < 0)
492 mutex_lock(&data
->update_lock
);
493 data
->in_max
[nr
] = in_to_reg(data
, nr
, val
);
494 it87_write_value(data
, IT87_REG_VIN_MAX(nr
),
496 mutex_unlock(&data
->update_lock
);
500 #define show_in_offset(offset) \
501 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
502 show_in, NULL, offset);
504 #define limit_in_offset(offset) \
505 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
506 show_in_min, set_in_min, offset); \
507 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
508 show_in_max, set_in_max, offset);
529 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
532 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
533 int nr
= sensor_attr
->index
;
535 struct it87_data
*data
= it87_update_device(dev
);
536 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp
[nr
]));
538 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
541 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
542 int nr
= sensor_attr
->index
;
544 struct it87_data
*data
= it87_update_device(dev
);
545 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_high
[nr
]));
547 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*attr
,
550 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
551 int nr
= sensor_attr
->index
;
553 struct it87_data
*data
= it87_update_device(dev
);
554 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->temp_low
[nr
]));
556 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
557 const char *buf
, size_t count
)
559 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
560 int nr
= sensor_attr
->index
;
562 struct it87_data
*data
= dev_get_drvdata(dev
);
565 if (kstrtol(buf
, 10, &val
) < 0)
568 mutex_lock(&data
->update_lock
);
569 data
->temp_high
[nr
] = TEMP_TO_REG(val
);
570 it87_write_value(data
, IT87_REG_TEMP_HIGH(nr
), data
->temp_high
[nr
]);
571 mutex_unlock(&data
->update_lock
);
574 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
575 const char *buf
, size_t count
)
577 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
578 int nr
= sensor_attr
->index
;
580 struct it87_data
*data
= dev_get_drvdata(dev
);
583 if (kstrtol(buf
, 10, &val
) < 0)
586 mutex_lock(&data
->update_lock
);
587 data
->temp_low
[nr
] = TEMP_TO_REG(val
);
588 it87_write_value(data
, IT87_REG_TEMP_LOW(nr
), data
->temp_low
[nr
]);
589 mutex_unlock(&data
->update_lock
);
592 #define show_temp_offset(offset) \
593 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
594 show_temp, NULL, offset - 1); \
595 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
596 show_temp_max, set_temp_max, offset - 1); \
597 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
598 show_temp_min, set_temp_min, offset - 1);
604 static ssize_t
show_sensor(struct device
*dev
, struct device_attribute
*attr
,
607 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
608 int nr
= sensor_attr
->index
;
610 struct it87_data
*data
= it87_update_device(dev
);
611 u8 reg
= data
->sensor
; /* In case the value is updated while
615 return sprintf(buf
, "3\n"); /* thermal diode */
617 return sprintf(buf
, "4\n"); /* thermistor */
618 return sprintf(buf
, "0\n"); /* disabled */
620 static ssize_t
set_sensor(struct device
*dev
, struct device_attribute
*attr
,
621 const char *buf
, size_t count
)
623 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
624 int nr
= sensor_attr
->index
;
626 struct it87_data
*data
= dev_get_drvdata(dev
);
630 if (kstrtol(buf
, 10, &val
) < 0)
633 reg
= it87_read_value(data
, IT87_REG_TEMP_ENABLE
);
636 if (val
== 2) { /* backwards compatibility */
637 dev_warn(dev
, "Sensor type 2 is deprecated, please use 4 "
641 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
649 mutex_lock(&data
->update_lock
);
651 it87_write_value(data
, IT87_REG_TEMP_ENABLE
, data
->sensor
);
652 data
->valid
= 0; /* Force cache refresh */
653 mutex_unlock(&data
->update_lock
);
656 #define show_sensor_offset(offset) \
657 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
658 show_sensor, set_sensor, offset - 1);
660 show_sensor_offset(1);
661 show_sensor_offset(2);
662 show_sensor_offset(3);
666 static int pwm_mode(const struct it87_data
*data
, int nr
)
668 int ctrl
= data
->fan_main_ctrl
& (1 << nr
);
670 if (ctrl
== 0) /* Full speed */
672 if (data
->pwm_ctrl
[nr
] & 0x80) /* Automatic mode */
674 else /* Manual mode */
678 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
681 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
682 int nr
= sensor_attr
->index
;
684 struct it87_data
*data
= it87_update_device(dev
);
685 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan
[nr
],
686 DIV_FROM_REG(data
->fan_div
[nr
])));
688 static ssize_t
show_fan_min(struct device
*dev
, struct device_attribute
*attr
,
691 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
692 int nr
= sensor_attr
->index
;
694 struct it87_data
*data
= it87_update_device(dev
);
695 return sprintf(buf
, "%d\n", FAN_FROM_REG(data
->fan_min
[nr
],
696 DIV_FROM_REG(data
->fan_div
[nr
])));
698 static ssize_t
show_fan_div(struct device
*dev
, struct device_attribute
*attr
,
701 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
702 int nr
= sensor_attr
->index
;
704 struct it87_data
*data
= it87_update_device(dev
);
705 return sprintf(buf
, "%d\n", DIV_FROM_REG(data
->fan_div
[nr
]));
707 static ssize_t
show_pwm_enable(struct device
*dev
,
708 struct device_attribute
*attr
, char *buf
)
710 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
711 int nr
= sensor_attr
->index
;
713 struct it87_data
*data
= it87_update_device(dev
);
714 return sprintf(buf
, "%d\n", pwm_mode(data
, nr
));
716 static ssize_t
show_pwm(struct device
*dev
, struct device_attribute
*attr
,
719 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
720 int nr
= sensor_attr
->index
;
722 struct it87_data
*data
= it87_update_device(dev
);
723 return sprintf(buf
, "%d\n",
724 pwm_from_reg(data
, data
->pwm_duty
[nr
]));
726 static ssize_t
show_pwm_freq(struct device
*dev
, struct device_attribute
*attr
,
729 struct it87_data
*data
= it87_update_device(dev
);
730 int index
= (data
->fan_ctl
>> 4) & 0x07;
732 return sprintf(buf
, "%u\n", pwm_freq
[index
]);
734 static ssize_t
set_fan_min(struct device
*dev
, struct device_attribute
*attr
,
735 const char *buf
, size_t count
)
737 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
738 int nr
= sensor_attr
->index
;
740 struct it87_data
*data
= dev_get_drvdata(dev
);
744 if (kstrtol(buf
, 10, &val
) < 0)
747 mutex_lock(&data
->update_lock
);
748 reg
= it87_read_value(data
, IT87_REG_FAN_DIV
);
751 data
->fan_div
[nr
] = reg
& 0x07;
754 data
->fan_div
[nr
] = (reg
>> 3) & 0x07;
757 data
->fan_div
[nr
] = (reg
& 0x40) ? 3 : 1;
761 data
->fan_min
[nr
] = FAN_TO_REG(val
, DIV_FROM_REG(data
->fan_div
[nr
]));
762 it87_write_value(data
, IT87_REG_FAN_MIN
[nr
], data
->fan_min
[nr
]);
763 mutex_unlock(&data
->update_lock
);
766 static ssize_t
set_fan_div(struct device
*dev
, struct device_attribute
*attr
,
767 const char *buf
, size_t count
)
769 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
770 int nr
= sensor_attr
->index
;
772 struct it87_data
*data
= dev_get_drvdata(dev
);
777 if (kstrtoul(buf
, 10, &val
) < 0)
780 mutex_lock(&data
->update_lock
);
781 old
= it87_read_value(data
, IT87_REG_FAN_DIV
);
783 /* Save fan min limit */
784 min
= FAN_FROM_REG(data
->fan_min
[nr
], DIV_FROM_REG(data
->fan_div
[nr
]));
789 data
->fan_div
[nr
] = DIV_TO_REG(val
);
793 data
->fan_div
[nr
] = 1;
795 data
->fan_div
[nr
] = 3;
798 val
|= (data
->fan_div
[0] & 0x07);
799 val
|= (data
->fan_div
[1] & 0x07) << 3;
800 if (data
->fan_div
[2] == 3)
802 it87_write_value(data
, IT87_REG_FAN_DIV
, val
);
804 /* Restore fan min limit */
805 data
->fan_min
[nr
] = FAN_TO_REG(min
, DIV_FROM_REG(data
->fan_div
[nr
]));
806 it87_write_value(data
, IT87_REG_FAN_MIN
[nr
], data
->fan_min
[nr
]);
808 mutex_unlock(&data
->update_lock
);
812 /* Returns 0 if OK, -EINVAL otherwise */
813 static int check_trip_points(struct device
*dev
, int nr
)
815 const struct it87_data
*data
= dev_get_drvdata(dev
);
818 if (has_old_autopwm(data
)) {
819 for (i
= 0; i
< 3; i
++) {
820 if (data
->auto_temp
[nr
][i
] > data
->auto_temp
[nr
][i
+ 1])
823 for (i
= 0; i
< 2; i
++) {
824 if (data
->auto_pwm
[nr
][i
] > data
->auto_pwm
[nr
][i
+ 1])
830 dev_err(dev
, "Inconsistent trip points, not switching to "
832 dev_err(dev
, "Adjust the trip points and try again\n");
837 static ssize_t
set_pwm_enable(struct device
*dev
,
838 struct device_attribute
*attr
, const char *buf
, size_t count
)
840 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
841 int nr
= sensor_attr
->index
;
843 struct it87_data
*data
= dev_get_drvdata(dev
);
846 if (kstrtol(buf
, 10, &val
) < 0 || val
< 0 || val
> 2)
849 /* Check trip points before switching to automatic mode */
851 if (check_trip_points(dev
, nr
) < 0)
855 mutex_lock(&data
->update_lock
);
859 /* make sure the fan is on when in on/off mode */
860 tmp
= it87_read_value(data
, IT87_REG_FAN_CTL
);
861 it87_write_value(data
, IT87_REG_FAN_CTL
, tmp
| (1 << nr
));
862 /* set on/off mode */
863 data
->fan_main_ctrl
&= ~(1 << nr
);
864 it87_write_value(data
, IT87_REG_FAN_MAIN_CTRL
,
865 data
->fan_main_ctrl
);
867 if (val
== 1) /* Manual mode */
868 data
->pwm_ctrl
[nr
] = has_newer_autopwm(data
) ?
869 data
->pwm_temp_map
[nr
] :
871 else /* Automatic mode */
872 data
->pwm_ctrl
[nr
] = 0x80 | data
->pwm_temp_map
[nr
];
873 it87_write_value(data
, IT87_REG_PWM(nr
), data
->pwm_ctrl
[nr
]);
874 /* set SmartGuardian mode */
875 data
->fan_main_ctrl
|= (1 << nr
);
876 it87_write_value(data
, IT87_REG_FAN_MAIN_CTRL
,
877 data
->fan_main_ctrl
);
880 mutex_unlock(&data
->update_lock
);
883 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
884 const char *buf
, size_t count
)
886 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
887 int nr
= sensor_attr
->index
;
889 struct it87_data
*data
= dev_get_drvdata(dev
);
892 if (kstrtol(buf
, 10, &val
) < 0 || val
< 0 || val
> 255)
895 mutex_lock(&data
->update_lock
);
896 if (has_newer_autopwm(data
)) {
897 /* If we are in automatic mode, the PWM duty cycle register
898 * is read-only so we can't write the value */
899 if (data
->pwm_ctrl
[nr
] & 0x80) {
900 mutex_unlock(&data
->update_lock
);
903 data
->pwm_duty
[nr
] = pwm_to_reg(data
, val
);
904 it87_write_value(data
, IT87_REG_PWM_DUTY(nr
),
907 data
->pwm_duty
[nr
] = pwm_to_reg(data
, val
);
908 /* If we are in manual mode, write the duty cycle immediately;
909 * otherwise, just store it for later use. */
910 if (!(data
->pwm_ctrl
[nr
] & 0x80)) {
911 data
->pwm_ctrl
[nr
] = data
->pwm_duty
[nr
];
912 it87_write_value(data
, IT87_REG_PWM(nr
),
916 mutex_unlock(&data
->update_lock
);
919 static ssize_t
set_pwm_freq(struct device
*dev
,
920 struct device_attribute
*attr
, const char *buf
, size_t count
)
922 struct it87_data
*data
= dev_get_drvdata(dev
);
926 if (kstrtoul(buf
, 10, &val
) < 0)
929 /* Search for the nearest available frequency */
930 for (i
= 0; i
< 7; i
++) {
931 if (val
> (pwm_freq
[i
] + pwm_freq
[i
+1]) / 2)
935 mutex_lock(&data
->update_lock
);
936 data
->fan_ctl
= it87_read_value(data
, IT87_REG_FAN_CTL
) & 0x8f;
937 data
->fan_ctl
|= i
<< 4;
938 it87_write_value(data
, IT87_REG_FAN_CTL
, data
->fan_ctl
);
939 mutex_unlock(&data
->update_lock
);
943 static ssize_t
show_pwm_temp_map(struct device
*dev
,
944 struct device_attribute
*attr
, char *buf
)
946 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
947 int nr
= sensor_attr
->index
;
949 struct it87_data
*data
= it87_update_device(dev
);
952 if (data
->pwm_temp_map
[nr
] < 3)
953 map
= 1 << data
->pwm_temp_map
[nr
];
955 map
= 0; /* Should never happen */
956 return sprintf(buf
, "%d\n", map
);
958 static ssize_t
set_pwm_temp_map(struct device
*dev
,
959 struct device_attribute
*attr
, const char *buf
, size_t count
)
961 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
962 int nr
= sensor_attr
->index
;
964 struct it87_data
*data
= dev_get_drvdata(dev
);
968 /* This check can go away if we ever support automatic fan speed
969 control on newer chips. */
970 if (!has_old_autopwm(data
)) {
971 dev_notice(dev
, "Mapping change disabled for safety reasons\n");
975 if (kstrtol(buf
, 10, &val
) < 0)
992 mutex_lock(&data
->update_lock
);
993 data
->pwm_temp_map
[nr
] = reg
;
994 /* If we are in automatic mode, write the temp mapping immediately;
995 * otherwise, just store it for later use. */
996 if (data
->pwm_ctrl
[nr
] & 0x80) {
997 data
->pwm_ctrl
[nr
] = 0x80 | data
->pwm_temp_map
[nr
];
998 it87_write_value(data
, IT87_REG_PWM(nr
), data
->pwm_ctrl
[nr
]);
1000 mutex_unlock(&data
->update_lock
);
1004 static ssize_t
show_auto_pwm(struct device
*dev
,
1005 struct device_attribute
*attr
, char *buf
)
1007 struct it87_data
*data
= it87_update_device(dev
);
1008 struct sensor_device_attribute_2
*sensor_attr
=
1009 to_sensor_dev_attr_2(attr
);
1010 int nr
= sensor_attr
->nr
;
1011 int point
= sensor_attr
->index
;
1013 return sprintf(buf
, "%d\n",
1014 pwm_from_reg(data
, data
->auto_pwm
[nr
][point
]));
1017 static ssize_t
set_auto_pwm(struct device
*dev
,
1018 struct device_attribute
*attr
, const char *buf
, size_t count
)
1020 struct it87_data
*data
= dev_get_drvdata(dev
);
1021 struct sensor_device_attribute_2
*sensor_attr
=
1022 to_sensor_dev_attr_2(attr
);
1023 int nr
= sensor_attr
->nr
;
1024 int point
= sensor_attr
->index
;
1027 if (kstrtol(buf
, 10, &val
) < 0 || val
< 0 || val
> 255)
1030 mutex_lock(&data
->update_lock
);
1031 data
->auto_pwm
[nr
][point
] = pwm_to_reg(data
, val
);
1032 it87_write_value(data
, IT87_REG_AUTO_PWM(nr
, point
),
1033 data
->auto_pwm
[nr
][point
]);
1034 mutex_unlock(&data
->update_lock
);
1038 static ssize_t
show_auto_temp(struct device
*dev
,
1039 struct device_attribute
*attr
, char *buf
)
1041 struct it87_data
*data
= it87_update_device(dev
);
1042 struct sensor_device_attribute_2
*sensor_attr
=
1043 to_sensor_dev_attr_2(attr
);
1044 int nr
= sensor_attr
->nr
;
1045 int point
= sensor_attr
->index
;
1047 return sprintf(buf
, "%d\n", TEMP_FROM_REG(data
->auto_temp
[nr
][point
]));
1050 static ssize_t
set_auto_temp(struct device
*dev
,
1051 struct device_attribute
*attr
, const char *buf
, size_t count
)
1053 struct it87_data
*data
= dev_get_drvdata(dev
);
1054 struct sensor_device_attribute_2
*sensor_attr
=
1055 to_sensor_dev_attr_2(attr
);
1056 int nr
= sensor_attr
->nr
;
1057 int point
= sensor_attr
->index
;
1060 if (kstrtol(buf
, 10, &val
) < 0 || val
< -128000 || val
> 127000)
1063 mutex_lock(&data
->update_lock
);
1064 data
->auto_temp
[nr
][point
] = TEMP_TO_REG(val
);
1065 it87_write_value(data
, IT87_REG_AUTO_TEMP(nr
, point
),
1066 data
->auto_temp
[nr
][point
]);
1067 mutex_unlock(&data
->update_lock
);
1071 #define show_fan_offset(offset) \
1072 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1073 show_fan, NULL, offset - 1); \
1074 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1075 show_fan_min, set_fan_min, offset - 1); \
1076 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1077 show_fan_div, set_fan_div, offset - 1);
1083 #define show_pwm_offset(offset) \
1084 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1085 show_pwm_enable, set_pwm_enable, offset - 1); \
1086 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1087 show_pwm, set_pwm, offset - 1); \
1088 static DEVICE_ATTR(pwm##offset##_freq, \
1089 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1090 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1091 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1092 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1094 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1095 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1097 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1098 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1100 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1101 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1103 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1104 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1105 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1106 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1108 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1109 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1111 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1112 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1114 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1115 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1117 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1118 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1125 /* A different set of callbacks for 16-bit fans */
1126 static ssize_t
show_fan16(struct device
*dev
, struct device_attribute
*attr
,
1129 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
1130 int nr
= sensor_attr
->index
;
1131 struct it87_data
*data
= it87_update_device(dev
);
1132 return sprintf(buf
, "%d\n", FAN16_FROM_REG(data
->fan
[nr
]));
1135 static ssize_t
show_fan16_min(struct device
*dev
, struct device_attribute
*attr
,
1138 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
1139 int nr
= sensor_attr
->index
;
1140 struct it87_data
*data
= it87_update_device(dev
);
1141 return sprintf(buf
, "%d\n", FAN16_FROM_REG(data
->fan_min
[nr
]));
1144 static ssize_t
set_fan16_min(struct device
*dev
, struct device_attribute
*attr
,
1145 const char *buf
, size_t count
)
1147 struct sensor_device_attribute
*sensor_attr
= to_sensor_dev_attr(attr
);
1148 int nr
= sensor_attr
->index
;
1149 struct it87_data
*data
= dev_get_drvdata(dev
);
1152 if (kstrtol(buf
, 10, &val
) < 0)
1155 mutex_lock(&data
->update_lock
);
1156 data
->fan_min
[nr
] = FAN16_TO_REG(val
);
1157 it87_write_value(data
, IT87_REG_FAN_MIN
[nr
],
1158 data
->fan_min
[nr
] & 0xff);
1159 it87_write_value(data
, IT87_REG_FANX_MIN
[nr
],
1160 data
->fan_min
[nr
] >> 8);
1161 mutex_unlock(&data
->update_lock
);
1165 /* We want to use the same sysfs file names as 8-bit fans, but we need
1166 different variable names, so we have to use SENSOR_ATTR instead of
1167 SENSOR_DEVICE_ATTR. */
1168 #define show_fan16_offset(offset) \
1169 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1170 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1171 show_fan16, NULL, offset - 1); \
1172 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1173 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1174 show_fan16_min, set_fan16_min, offset - 1)
1176 show_fan16_offset(1);
1177 show_fan16_offset(2);
1178 show_fan16_offset(3);
1179 show_fan16_offset(4);
1180 show_fan16_offset(5);
1183 static ssize_t
show_alarms(struct device
*dev
, struct device_attribute
*attr
,
1186 struct it87_data
*data
= it87_update_device(dev
);
1187 return sprintf(buf
, "%u\n", data
->alarms
);
1189 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
1191 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
1194 int bitnr
= to_sensor_dev_attr(attr
)->index
;
1195 struct it87_data
*data
= it87_update_device(dev
);
1196 return sprintf(buf
, "%u\n", (data
->alarms
>> bitnr
) & 1);
1199 static ssize_t
clear_intrusion(struct device
*dev
, struct device_attribute
1200 *attr
, const char *buf
, size_t count
)
1202 struct it87_data
*data
= dev_get_drvdata(dev
);
1206 if (kstrtol(buf
, 10, &val
) < 0 || val
!= 0)
1209 mutex_lock(&data
->update_lock
);
1210 config
= it87_read_value(data
, IT87_REG_CONFIG
);
1215 it87_write_value(data
, IT87_REG_CONFIG
, config
);
1216 /* Invalidate cache to force re-read */
1219 mutex_unlock(&data
->update_lock
);
1224 static SENSOR_DEVICE_ATTR(in0_alarm
, S_IRUGO
, show_alarm
, NULL
, 8);
1225 static SENSOR_DEVICE_ATTR(in1_alarm
, S_IRUGO
, show_alarm
, NULL
, 9);
1226 static SENSOR_DEVICE_ATTR(in2_alarm
, S_IRUGO
, show_alarm
, NULL
, 10);
1227 static SENSOR_DEVICE_ATTR(in3_alarm
, S_IRUGO
, show_alarm
, NULL
, 11);
1228 static SENSOR_DEVICE_ATTR(in4_alarm
, S_IRUGO
, show_alarm
, NULL
, 12);
1229 static SENSOR_DEVICE_ATTR(in5_alarm
, S_IRUGO
, show_alarm
, NULL
, 13);
1230 static SENSOR_DEVICE_ATTR(in6_alarm
, S_IRUGO
, show_alarm
, NULL
, 14);
1231 static SENSOR_DEVICE_ATTR(in7_alarm
, S_IRUGO
, show_alarm
, NULL
, 15);
1232 static SENSOR_DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
1233 static SENSOR_DEVICE_ATTR(fan2_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
1234 static SENSOR_DEVICE_ATTR(fan3_alarm
, S_IRUGO
, show_alarm
, NULL
, 2);
1235 static SENSOR_DEVICE_ATTR(fan4_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
1236 static SENSOR_DEVICE_ATTR(fan5_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
1237 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 16);
1238 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 17);
1239 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 18);
1240 static SENSOR_DEVICE_ATTR(intrusion0_alarm
, S_IRUGO
| S_IWUSR
,
1241 show_alarm
, clear_intrusion
, 4);
1243 static ssize_t
show_beep(struct device
*dev
, struct device_attribute
*attr
,
1246 int bitnr
= to_sensor_dev_attr(attr
)->index
;
1247 struct it87_data
*data
= it87_update_device(dev
);
1248 return sprintf(buf
, "%u\n", (data
->beeps
>> bitnr
) & 1);
1250 static ssize_t
set_beep(struct device
*dev
, struct device_attribute
*attr
,
1251 const char *buf
, size_t count
)
1253 int bitnr
= to_sensor_dev_attr(attr
)->index
;
1254 struct it87_data
*data
= dev_get_drvdata(dev
);
1257 if (kstrtol(buf
, 10, &val
) < 0
1258 || (val
!= 0 && val
!= 1))
1261 mutex_lock(&data
->update_lock
);
1262 data
->beeps
= it87_read_value(data
, IT87_REG_BEEP_ENABLE
);
1264 data
->beeps
|= (1 << bitnr
);
1266 data
->beeps
&= ~(1 << bitnr
);
1267 it87_write_value(data
, IT87_REG_BEEP_ENABLE
, data
->beeps
);
1268 mutex_unlock(&data
->update_lock
);
1272 static SENSOR_DEVICE_ATTR(in0_beep
, S_IRUGO
| S_IWUSR
,
1273 show_beep
, set_beep
, 1);
1274 static SENSOR_DEVICE_ATTR(in1_beep
, S_IRUGO
, show_beep
, NULL
, 1);
1275 static SENSOR_DEVICE_ATTR(in2_beep
, S_IRUGO
, show_beep
, NULL
, 1);
1276 static SENSOR_DEVICE_ATTR(in3_beep
, S_IRUGO
, show_beep
, NULL
, 1);
1277 static SENSOR_DEVICE_ATTR(in4_beep
, S_IRUGO
, show_beep
, NULL
, 1);
1278 static SENSOR_DEVICE_ATTR(in5_beep
, S_IRUGO
, show_beep
, NULL
, 1);
1279 static SENSOR_DEVICE_ATTR(in6_beep
, S_IRUGO
, show_beep
, NULL
, 1);
1280 static SENSOR_DEVICE_ATTR(in7_beep
, S_IRUGO
, show_beep
, NULL
, 1);
1281 /* fanX_beep writability is set later */
1282 static SENSOR_DEVICE_ATTR(fan1_beep
, S_IRUGO
, show_beep
, set_beep
, 0);
1283 static SENSOR_DEVICE_ATTR(fan2_beep
, S_IRUGO
, show_beep
, set_beep
, 0);
1284 static SENSOR_DEVICE_ATTR(fan3_beep
, S_IRUGO
, show_beep
, set_beep
, 0);
1285 static SENSOR_DEVICE_ATTR(fan4_beep
, S_IRUGO
, show_beep
, set_beep
, 0);
1286 static SENSOR_DEVICE_ATTR(fan5_beep
, S_IRUGO
, show_beep
, set_beep
, 0);
1287 static SENSOR_DEVICE_ATTR(temp1_beep
, S_IRUGO
| S_IWUSR
,
1288 show_beep
, set_beep
, 2);
1289 static SENSOR_DEVICE_ATTR(temp2_beep
, S_IRUGO
, show_beep
, NULL
, 2);
1290 static SENSOR_DEVICE_ATTR(temp3_beep
, S_IRUGO
, show_beep
, NULL
, 2);
1292 static ssize_t
show_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
1295 struct it87_data
*data
= dev_get_drvdata(dev
);
1296 return sprintf(buf
, "%u\n", data
->vrm
);
1298 static ssize_t
store_vrm_reg(struct device
*dev
, struct device_attribute
*attr
,
1299 const char *buf
, size_t count
)
1301 struct it87_data
*data
= dev_get_drvdata(dev
);
1304 if (kstrtoul(buf
, 10, &val
) < 0)
1311 static DEVICE_ATTR(vrm
, S_IRUGO
| S_IWUSR
, show_vrm_reg
, store_vrm_reg
);
1313 static ssize_t
show_vid_reg(struct device
*dev
, struct device_attribute
*attr
,
1316 struct it87_data
*data
= it87_update_device(dev
);
1317 return sprintf(buf
, "%ld\n", (long) vid_from_reg(data
->vid
, data
->vrm
));
1319 static DEVICE_ATTR(cpu0_vid
, S_IRUGO
, show_vid_reg
, NULL
);
1321 static ssize_t
show_label(struct device
*dev
, struct device_attribute
*attr
,
1324 static const char *labels
[] = {
1329 static const char *labels_it8721
[] = {
1334 struct it87_data
*data
= dev_get_drvdata(dev
);
1335 int nr
= to_sensor_dev_attr(attr
)->index
;
1337 return sprintf(buf
, "%s\n", has_12mv_adc(data
) ? labels_it8721
[nr
]
1340 static SENSOR_DEVICE_ATTR(in3_label
, S_IRUGO
, show_label
, NULL
, 0);
1341 static SENSOR_DEVICE_ATTR(in7_label
, S_IRUGO
, show_label
, NULL
, 1);
1342 static SENSOR_DEVICE_ATTR(in8_label
, S_IRUGO
, show_label
, NULL
, 2);
1344 static ssize_t
show_name(struct device
*dev
, struct device_attribute
1345 *devattr
, char *buf
)
1347 struct it87_data
*data
= dev_get_drvdata(dev
);
1348 return sprintf(buf
, "%s\n", data
->name
);
1350 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
1352 static struct attribute
*it87_attributes
[] = {
1353 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
1354 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
1355 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
1356 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
1357 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
1358 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
1359 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
1360 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
1361 &sensor_dev_attr_in8_input
.dev_attr
.attr
,
1362 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
1363 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
1364 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
1365 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
1366 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
1367 &sensor_dev_attr_in5_min
.dev_attr
.attr
,
1368 &sensor_dev_attr_in6_min
.dev_attr
.attr
,
1369 &sensor_dev_attr_in7_min
.dev_attr
.attr
,
1370 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
1371 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
1372 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
1373 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
1374 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
1375 &sensor_dev_attr_in5_max
.dev_attr
.attr
,
1376 &sensor_dev_attr_in6_max
.dev_attr
.attr
,
1377 &sensor_dev_attr_in7_max
.dev_attr
.attr
,
1378 &sensor_dev_attr_in0_alarm
.dev_attr
.attr
,
1379 &sensor_dev_attr_in1_alarm
.dev_attr
.attr
,
1380 &sensor_dev_attr_in2_alarm
.dev_attr
.attr
,
1381 &sensor_dev_attr_in3_alarm
.dev_attr
.attr
,
1382 &sensor_dev_attr_in4_alarm
.dev_attr
.attr
,
1383 &sensor_dev_attr_in5_alarm
.dev_attr
.attr
,
1384 &sensor_dev_attr_in6_alarm
.dev_attr
.attr
,
1385 &sensor_dev_attr_in7_alarm
.dev_attr
.attr
,
1387 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1388 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1389 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1390 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
1391 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
1392 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
1393 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
1394 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
1395 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
1396 &sensor_dev_attr_temp1_type
.dev_attr
.attr
,
1397 &sensor_dev_attr_temp2_type
.dev_attr
.attr
,
1398 &sensor_dev_attr_temp3_type
.dev_attr
.attr
,
1399 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
1400 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
1401 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
1403 &dev_attr_alarms
.attr
,
1404 &sensor_dev_attr_intrusion0_alarm
.dev_attr
.attr
,
1405 &dev_attr_name
.attr
,
1409 static const struct attribute_group it87_group
= {
1410 .attrs
= it87_attributes
,
1413 static struct attribute
*it87_attributes_beep
[] = {
1414 &sensor_dev_attr_in0_beep
.dev_attr
.attr
,
1415 &sensor_dev_attr_in1_beep
.dev_attr
.attr
,
1416 &sensor_dev_attr_in2_beep
.dev_attr
.attr
,
1417 &sensor_dev_attr_in3_beep
.dev_attr
.attr
,
1418 &sensor_dev_attr_in4_beep
.dev_attr
.attr
,
1419 &sensor_dev_attr_in5_beep
.dev_attr
.attr
,
1420 &sensor_dev_attr_in6_beep
.dev_attr
.attr
,
1421 &sensor_dev_attr_in7_beep
.dev_attr
.attr
,
1423 &sensor_dev_attr_temp1_beep
.dev_attr
.attr
,
1424 &sensor_dev_attr_temp2_beep
.dev_attr
.attr
,
1425 &sensor_dev_attr_temp3_beep
.dev_attr
.attr
,
1429 static const struct attribute_group it87_group_beep
= {
1430 .attrs
= it87_attributes_beep
,
1433 static struct attribute
*it87_attributes_fan16
[5][3+1] = { {
1434 &sensor_dev_attr_fan1_input16
.dev_attr
.attr
,
1435 &sensor_dev_attr_fan1_min16
.dev_attr
.attr
,
1436 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1439 &sensor_dev_attr_fan2_input16
.dev_attr
.attr
,
1440 &sensor_dev_attr_fan2_min16
.dev_attr
.attr
,
1441 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1444 &sensor_dev_attr_fan3_input16
.dev_attr
.attr
,
1445 &sensor_dev_attr_fan3_min16
.dev_attr
.attr
,
1446 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1449 &sensor_dev_attr_fan4_input16
.dev_attr
.attr
,
1450 &sensor_dev_attr_fan4_min16
.dev_attr
.attr
,
1451 &sensor_dev_attr_fan4_alarm
.dev_attr
.attr
,
1454 &sensor_dev_attr_fan5_input16
.dev_attr
.attr
,
1455 &sensor_dev_attr_fan5_min16
.dev_attr
.attr
,
1456 &sensor_dev_attr_fan5_alarm
.dev_attr
.attr
,
1460 static const struct attribute_group it87_group_fan16
[5] = {
1461 { .attrs
= it87_attributes_fan16
[0] },
1462 { .attrs
= it87_attributes_fan16
[1] },
1463 { .attrs
= it87_attributes_fan16
[2] },
1464 { .attrs
= it87_attributes_fan16
[3] },
1465 { .attrs
= it87_attributes_fan16
[4] },
1468 static struct attribute
*it87_attributes_fan
[3][4+1] = { {
1469 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
1470 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
1471 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
1472 &sensor_dev_attr_fan1_alarm
.dev_attr
.attr
,
1475 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
1476 &sensor_dev_attr_fan2_min
.dev_attr
.attr
,
1477 &sensor_dev_attr_fan2_div
.dev_attr
.attr
,
1478 &sensor_dev_attr_fan2_alarm
.dev_attr
.attr
,
1481 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
1482 &sensor_dev_attr_fan3_min
.dev_attr
.attr
,
1483 &sensor_dev_attr_fan3_div
.dev_attr
.attr
,
1484 &sensor_dev_attr_fan3_alarm
.dev_attr
.attr
,
1488 static const struct attribute_group it87_group_fan
[3] = {
1489 { .attrs
= it87_attributes_fan
[0] },
1490 { .attrs
= it87_attributes_fan
[1] },
1491 { .attrs
= it87_attributes_fan
[2] },
1494 static const struct attribute_group
*
1495 it87_get_fan_group(const struct it87_data
*data
)
1497 return has_16bit_fans(data
) ? it87_group_fan16
: it87_group_fan
;
1500 static struct attribute
*it87_attributes_pwm
[3][4+1] = { {
1501 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
1502 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
1503 &dev_attr_pwm1_freq
.attr
,
1504 &sensor_dev_attr_pwm1_auto_channels_temp
.dev_attr
.attr
,
1507 &sensor_dev_attr_pwm2_enable
.dev_attr
.attr
,
1508 &sensor_dev_attr_pwm2
.dev_attr
.attr
,
1509 &dev_attr_pwm2_freq
.attr
,
1510 &sensor_dev_attr_pwm2_auto_channels_temp
.dev_attr
.attr
,
1513 &sensor_dev_attr_pwm3_enable
.dev_attr
.attr
,
1514 &sensor_dev_attr_pwm3
.dev_attr
.attr
,
1515 &dev_attr_pwm3_freq
.attr
,
1516 &sensor_dev_attr_pwm3_auto_channels_temp
.dev_attr
.attr
,
1520 static const struct attribute_group it87_group_pwm
[3] = {
1521 { .attrs
= it87_attributes_pwm
[0] },
1522 { .attrs
= it87_attributes_pwm
[1] },
1523 { .attrs
= it87_attributes_pwm
[2] },
1526 static struct attribute
*it87_attributes_autopwm
[3][9+1] = { {
1527 &sensor_dev_attr_pwm1_auto_point1_pwm
.dev_attr
.attr
,
1528 &sensor_dev_attr_pwm1_auto_point2_pwm
.dev_attr
.attr
,
1529 &sensor_dev_attr_pwm1_auto_point3_pwm
.dev_attr
.attr
,
1530 &sensor_dev_attr_pwm1_auto_point4_pwm
.dev_attr
.attr
,
1531 &sensor_dev_attr_pwm1_auto_point1_temp
.dev_attr
.attr
,
1532 &sensor_dev_attr_pwm1_auto_point1_temp_hyst
.dev_attr
.attr
,
1533 &sensor_dev_attr_pwm1_auto_point2_temp
.dev_attr
.attr
,
1534 &sensor_dev_attr_pwm1_auto_point3_temp
.dev_attr
.attr
,
1535 &sensor_dev_attr_pwm1_auto_point4_temp
.dev_attr
.attr
,
1538 &sensor_dev_attr_pwm2_auto_point1_pwm
.dev_attr
.attr
,
1539 &sensor_dev_attr_pwm2_auto_point2_pwm
.dev_attr
.attr
,
1540 &sensor_dev_attr_pwm2_auto_point3_pwm
.dev_attr
.attr
,
1541 &sensor_dev_attr_pwm2_auto_point4_pwm
.dev_attr
.attr
,
1542 &sensor_dev_attr_pwm2_auto_point1_temp
.dev_attr
.attr
,
1543 &sensor_dev_attr_pwm2_auto_point1_temp_hyst
.dev_attr
.attr
,
1544 &sensor_dev_attr_pwm2_auto_point2_temp
.dev_attr
.attr
,
1545 &sensor_dev_attr_pwm2_auto_point3_temp
.dev_attr
.attr
,
1546 &sensor_dev_attr_pwm2_auto_point4_temp
.dev_attr
.attr
,
1549 &sensor_dev_attr_pwm3_auto_point1_pwm
.dev_attr
.attr
,
1550 &sensor_dev_attr_pwm3_auto_point2_pwm
.dev_attr
.attr
,
1551 &sensor_dev_attr_pwm3_auto_point3_pwm
.dev_attr
.attr
,
1552 &sensor_dev_attr_pwm3_auto_point4_pwm
.dev_attr
.attr
,
1553 &sensor_dev_attr_pwm3_auto_point1_temp
.dev_attr
.attr
,
1554 &sensor_dev_attr_pwm3_auto_point1_temp_hyst
.dev_attr
.attr
,
1555 &sensor_dev_attr_pwm3_auto_point2_temp
.dev_attr
.attr
,
1556 &sensor_dev_attr_pwm3_auto_point3_temp
.dev_attr
.attr
,
1557 &sensor_dev_attr_pwm3_auto_point4_temp
.dev_attr
.attr
,
1561 static const struct attribute_group it87_group_autopwm
[3] = {
1562 { .attrs
= it87_attributes_autopwm
[0] },
1563 { .attrs
= it87_attributes_autopwm
[1] },
1564 { .attrs
= it87_attributes_autopwm
[2] },
1567 static struct attribute
*it87_attributes_fan_beep
[] = {
1568 &sensor_dev_attr_fan1_beep
.dev_attr
.attr
,
1569 &sensor_dev_attr_fan2_beep
.dev_attr
.attr
,
1570 &sensor_dev_attr_fan3_beep
.dev_attr
.attr
,
1571 &sensor_dev_attr_fan4_beep
.dev_attr
.attr
,
1572 &sensor_dev_attr_fan5_beep
.dev_attr
.attr
,
1575 static struct attribute
*it87_attributes_vid
[] = {
1577 &dev_attr_cpu0_vid
.attr
,
1581 static const struct attribute_group it87_group_vid
= {
1582 .attrs
= it87_attributes_vid
,
1585 static struct attribute
*it87_attributes_label
[] = {
1586 &sensor_dev_attr_in3_label
.dev_attr
.attr
,
1587 &sensor_dev_attr_in7_label
.dev_attr
.attr
,
1588 &sensor_dev_attr_in8_label
.dev_attr
.attr
,
1592 static const struct attribute_group it87_group_label
= {
1593 .attrs
= it87_attributes_label
,
1596 /* SuperIO detection - will change isa_address if a chip is found */
1597 static int __init
it87_find(unsigned short *address
,
1598 struct it87_sio_data
*sio_data
)
1602 const char *board_vendor
, *board_name
;
1604 err
= superio_enter();
1609 chip_type
= force_id
? force_id
: superio_inw(DEVID
);
1611 switch (chip_type
) {
1613 sio_data
->type
= it87
;
1616 sio_data
->type
= it8712
;
1620 sio_data
->type
= it8716
;
1623 sio_data
->type
= it8718
;
1626 sio_data
->type
= it8720
;
1629 sio_data
->type
= it8721
;
1632 sio_data
->type
= it8728
;
1634 case 0xffff: /* No device at all */
1637 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type
);
1641 superio_select(PME
);
1642 if (!(superio_inb(IT87_ACT_REG
) & 0x01)) {
1643 pr_info("Device not activated, skipping\n");
1647 *address
= superio_inw(IT87_BASE_REG
) & ~(IT87_EXTENT
- 1);
1648 if (*address
== 0) {
1649 pr_info("Base address not set, skipping\n");
1654 sio_data
->revision
= superio_inb(DEVREV
) & 0x0f;
1655 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1656 chip_type
, *address
, sio_data
->revision
);
1658 /* in8 (Vbat) is always internal */
1659 sio_data
->internal
= (1 << 2);
1661 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1662 if (sio_data
->type
== it87
) {
1663 /* The IT8705F doesn't have VID pins at all */
1664 sio_data
->skip_vid
= 1;
1666 /* The IT8705F has a different LD number for GPIO */
1668 sio_data
->beep_pin
= superio_inb(IT87_SIO_BEEP_PIN_REG
) & 0x3f;
1672 superio_select(GPIO
);
1674 reg
= superio_inb(IT87_SIO_GPIO3_REG
);
1675 if (sio_data
->type
== it8721
|| sio_data
->type
== it8728
) {
1677 * The IT8721F/IT8758E doesn't have VID pins at all,
1678 * not sure about the IT8728F.
1680 sio_data
->skip_vid
= 1;
1682 /* We need at least 4 VID pins */
1684 pr_info("VID is disabled (pins used for GPIO)\n");
1685 sio_data
->skip_vid
= 1;
1689 /* Check if fan3 is there or not */
1691 sio_data
->skip_pwm
|= (1 << 2);
1693 sio_data
->skip_fan
|= (1 << 2);
1695 /* Check if fan2 is there or not */
1696 reg
= superio_inb(IT87_SIO_GPIO5_REG
);
1698 sio_data
->skip_pwm
|= (1 << 1);
1700 sio_data
->skip_fan
|= (1 << 1);
1702 if ((sio_data
->type
== it8718
|| sio_data
->type
== it8720
)
1703 && !(sio_data
->skip_vid
))
1704 sio_data
->vid_value
= superio_inb(IT87_SIO_VID_REG
);
1706 reg
= superio_inb(IT87_SIO_PINX2_REG
);
1708 * The IT8720F has no VIN7 pin, so VCCH should always be
1709 * routed internally to VIN7 with an internal divider.
1710 * Curiously, there still is a configuration bit to control
1711 * this, which means it can be set incorrectly. And even
1712 * more curiously, many boards out there are improperly
1713 * configured, even though the IT8720F datasheet claims
1714 * that the internal routing of VCCH to VIN7 is the default
1715 * setting. So we force the internal routing in this case.
1717 if (sio_data
->type
== it8720
&& !(reg
& (1 << 1))) {
1719 superio_outb(IT87_SIO_PINX2_REG
, reg
);
1720 pr_notice("Routing internal VCCH to in7\n");
1723 sio_data
->internal
|= (1 << 0);
1724 if ((reg
& (1 << 1)) || sio_data
->type
== it8721
||
1725 sio_data
->type
== it8728
)
1726 sio_data
->internal
|= (1 << 1);
1728 sio_data
->beep_pin
= superio_inb(IT87_SIO_BEEP_PIN_REG
) & 0x3f;
1730 if (sio_data
->beep_pin
)
1731 pr_info("Beeping is supported\n");
1733 /* Disable specific features based on DMI strings */
1734 board_vendor
= dmi_get_system_info(DMI_BOARD_VENDOR
);
1735 board_name
= dmi_get_system_info(DMI_BOARD_NAME
);
1736 if (board_vendor
&& board_name
) {
1737 if (strcmp(board_vendor
, "nVIDIA") == 0
1738 && strcmp(board_name
, "FN68PT") == 0) {
1739 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1740 connected to a fan, but to something else. One user
1741 has reported instant system power-off when changing
1742 the PWM2 duty cycle, so we disable it.
1743 I use the board name string as the trigger in case
1744 the same board is ever used in other systems. */
1745 pr_info("Disabling pwm2 due to hardware constraints\n");
1746 sio_data
->skip_pwm
= (1 << 1);
1755 static void it87_remove_files(struct device
*dev
)
1757 struct it87_data
*data
= platform_get_drvdata(pdev
);
1758 struct it87_sio_data
*sio_data
= dev
->platform_data
;
1759 const struct attribute_group
*fan_group
= it87_get_fan_group(data
);
1762 sysfs_remove_group(&dev
->kobj
, &it87_group
);
1763 if (sio_data
->beep_pin
)
1764 sysfs_remove_group(&dev
->kobj
, &it87_group_beep
);
1765 for (i
= 0; i
< 5; i
++) {
1766 if (!(data
->has_fan
& (1 << i
)))
1768 sysfs_remove_group(&dev
->kobj
, &fan_group
[i
]);
1769 if (sio_data
->beep_pin
)
1770 sysfs_remove_file(&dev
->kobj
,
1771 it87_attributes_fan_beep
[i
]);
1773 for (i
= 0; i
< 3; i
++) {
1774 if (sio_data
->skip_pwm
& (1 << 0))
1776 sysfs_remove_group(&dev
->kobj
, &it87_group_pwm
[i
]);
1777 if (has_old_autopwm(data
))
1778 sysfs_remove_group(&dev
->kobj
,
1779 &it87_group_autopwm
[i
]);
1781 if (!sio_data
->skip_vid
)
1782 sysfs_remove_group(&dev
->kobj
, &it87_group_vid
);
1783 sysfs_remove_group(&dev
->kobj
, &it87_group_label
);
1786 static int __devinit
it87_probe(struct platform_device
*pdev
)
1788 struct it87_data
*data
;
1789 struct resource
*res
;
1790 struct device
*dev
= &pdev
->dev
;
1791 struct it87_sio_data
*sio_data
= dev
->platform_data
;
1792 const struct attribute_group
*fan_group
;
1794 int enable_pwm_interface
;
1795 int fan_beep_need_rw
;
1796 static const char *names
[] = {
1806 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
1807 if (!request_region(res
->start
, IT87_EC_EXTENT
, DRVNAME
)) {
1808 dev_err(dev
, "Failed to request region 0x%lx-0x%lx\n",
1809 (unsigned long)res
->start
,
1810 (unsigned long)(res
->start
+ IT87_EC_EXTENT
- 1));
1815 data
= kzalloc(sizeof(struct it87_data
), GFP_KERNEL
);
1821 data
->addr
= res
->start
;
1822 data
->type
= sio_data
->type
;
1823 data
->revision
= sio_data
->revision
;
1824 data
->name
= names
[sio_data
->type
];
1826 /* Now, we do the remaining detection. */
1827 if ((it87_read_value(data
, IT87_REG_CONFIG
) & 0x80)
1828 || it87_read_value(data
, IT87_REG_CHIPID
) != 0x90) {
1833 platform_set_drvdata(pdev
, data
);
1835 mutex_init(&data
->update_lock
);
1837 /* Check PWM configuration */
1838 enable_pwm_interface
= it87_check_pwm(dev
);
1840 /* Starting with IT8721F, we handle scaling of internal voltages */
1841 if (has_12mv_adc(data
)) {
1842 if (sio_data
->internal
& (1 << 0))
1843 data
->in_scaled
|= (1 << 3); /* in3 is AVCC */
1844 if (sio_data
->internal
& (1 << 1))
1845 data
->in_scaled
|= (1 << 7); /* in7 is VSB */
1846 if (sio_data
->internal
& (1 << 2))
1847 data
->in_scaled
|= (1 << 8); /* in8 is Vbat */
1850 /* Initialize the IT87 chip */
1851 it87_init_device(pdev
);
1853 /* Register sysfs hooks */
1854 err
= sysfs_create_group(&dev
->kobj
, &it87_group
);
1858 if (sio_data
->beep_pin
) {
1859 err
= sysfs_create_group(&dev
->kobj
, &it87_group_beep
);
1864 /* Do not create fan files for disabled fans */
1865 fan_group
= it87_get_fan_group(data
);
1866 fan_beep_need_rw
= 1;
1867 for (i
= 0; i
< 5; i
++) {
1868 if (!(data
->has_fan
& (1 << i
)))
1870 err
= sysfs_create_group(&dev
->kobj
, &fan_group
[i
]);
1874 if (sio_data
->beep_pin
) {
1875 err
= sysfs_create_file(&dev
->kobj
,
1876 it87_attributes_fan_beep
[i
]);
1879 if (!fan_beep_need_rw
)
1882 /* As we have a single beep enable bit for all fans,
1883 * only the first enabled fan has a writable attribute
1885 if (sysfs_chmod_file(&dev
->kobj
,
1886 it87_attributes_fan_beep
[i
],
1888 dev_dbg(dev
, "chmod +w fan%d_beep failed\n",
1890 fan_beep_need_rw
= 0;
1894 if (enable_pwm_interface
) {
1895 for (i
= 0; i
< 3; i
++) {
1896 if (sio_data
->skip_pwm
& (1 << i
))
1898 err
= sysfs_create_group(&dev
->kobj
,
1899 &it87_group_pwm
[i
]);
1903 if (!has_old_autopwm(data
))
1905 err
= sysfs_create_group(&dev
->kobj
,
1906 &it87_group_autopwm
[i
]);
1912 if (!sio_data
->skip_vid
) {
1913 data
->vrm
= vid_which_vrm();
1914 /* VID reading from Super-I/O config space if available */
1915 data
->vid
= sio_data
->vid_value
;
1916 err
= sysfs_create_group(&dev
->kobj
, &it87_group_vid
);
1921 /* Export labels for internal sensors */
1922 for (i
= 0; i
< 3; i
++) {
1923 if (!(sio_data
->internal
& (1 << i
)))
1925 err
= sysfs_create_file(&dev
->kobj
,
1926 it87_attributes_label
[i
]);
1931 data
->hwmon_dev
= hwmon_device_register(dev
);
1932 if (IS_ERR(data
->hwmon_dev
)) {
1933 err
= PTR_ERR(data
->hwmon_dev
);
1940 it87_remove_files(dev
);
1942 platform_set_drvdata(pdev
, NULL
);
1945 release_region(res
->start
, IT87_EC_EXTENT
);
1950 static int __devexit
it87_remove(struct platform_device
*pdev
)
1952 struct it87_data
*data
= platform_get_drvdata(pdev
);
1954 hwmon_device_unregister(data
->hwmon_dev
);
1955 it87_remove_files(&pdev
->dev
);
1957 release_region(data
->addr
, IT87_EC_EXTENT
);
1958 platform_set_drvdata(pdev
, NULL
);
1964 /* Must be called with data->update_lock held, except during initialization.
1965 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1966 would slow down the IT87 access and should not be necessary. */
1967 static int it87_read_value(struct it87_data
*data
, u8 reg
)
1969 outb_p(reg
, data
->addr
+ IT87_ADDR_REG_OFFSET
);
1970 return inb_p(data
->addr
+ IT87_DATA_REG_OFFSET
);
1973 /* Must be called with data->update_lock held, except during initialization.
1974 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1975 would slow down the IT87 access and should not be necessary. */
1976 static void it87_write_value(struct it87_data
*data
, u8 reg
, u8 value
)
1978 outb_p(reg
, data
->addr
+ IT87_ADDR_REG_OFFSET
);
1979 outb_p(value
, data
->addr
+ IT87_DATA_REG_OFFSET
);
1982 /* Return 1 if and only if the PWM interface is safe to use */
1983 static int __devinit
it87_check_pwm(struct device
*dev
)
1985 struct it87_data
*data
= dev_get_drvdata(dev
);
1986 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1987 * and polarity set to active low is sign that this is the case so we
1988 * disable pwm control to protect the user. */
1989 int tmp
= it87_read_value(data
, IT87_REG_FAN_CTL
);
1990 if ((tmp
& 0x87) == 0) {
1991 if (fix_pwm_polarity
) {
1992 /* The user asks us to attempt a chip reconfiguration.
1993 * This means switching to active high polarity and
1994 * inverting all fan speed values. */
1998 for (i
= 0; i
< 3; i
++)
1999 pwm
[i
] = it87_read_value(data
,
2002 /* If any fan is in automatic pwm mode, the polarity
2003 * might be correct, as suspicious as it seems, so we
2004 * better don't change anything (but still disable the
2005 * PWM interface). */
2006 if (!((pwm
[0] | pwm
[1] | pwm
[2]) & 0x80)) {
2007 dev_info(dev
, "Reconfiguring PWM to "
2008 "active high polarity\n");
2009 it87_write_value(data
, IT87_REG_FAN_CTL
,
2011 for (i
= 0; i
< 3; i
++)
2012 it87_write_value(data
,
2018 dev_info(dev
, "PWM configuration is "
2019 "too broken to be fixed\n");
2022 dev_info(dev
, "Detected broken BIOS "
2023 "defaults, disabling PWM interface\n");
2025 } else if (fix_pwm_polarity
) {
2026 dev_info(dev
, "PWM configuration looks "
2027 "sane, won't touch\n");
2033 /* Called when we have found a new IT87. */
2034 static void __devinit
it87_init_device(struct platform_device
*pdev
)
2036 struct it87_sio_data
*sio_data
= pdev
->dev
.platform_data
;
2037 struct it87_data
*data
= platform_get_drvdata(pdev
);
2041 /* For each PWM channel:
2042 * - If it is in automatic mode, setting to manual mode should set
2043 * the fan to full speed by default.
2044 * - If it is in manual mode, we need a mapping to temperature
2045 * channels to use when later setting to automatic mode later.
2046 * Use a 1:1 mapping by default (we are clueless.)
2047 * In both cases, the value can (and should) be changed by the user
2048 * prior to switching to a different mode.
2049 * Note that this is no longer needed for the IT8721F and later, as
2050 * these have separate registers for the temperature mapping and the
2051 * manual duty cycle. */
2052 for (i
= 0; i
< 3; i
++) {
2053 data
->pwm_temp_map
[i
] = i
;
2054 data
->pwm_duty
[i
] = 0x7f; /* Full speed */
2055 data
->auto_pwm
[i
][3] = 0x7f; /* Full speed, hard-coded */
2058 /* Some chips seem to have default value 0xff for all limit
2059 * registers. For low voltage limits it makes no sense and triggers
2060 * alarms, so change to 0 instead. For high temperature limits, it
2061 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2062 * but is still confusing, so change to 127 degrees C. */
2063 for (i
= 0; i
< 8; i
++) {
2064 tmp
= it87_read_value(data
, IT87_REG_VIN_MIN(i
));
2066 it87_write_value(data
, IT87_REG_VIN_MIN(i
), 0);
2068 for (i
= 0; i
< 3; i
++) {
2069 tmp
= it87_read_value(data
, IT87_REG_TEMP_HIGH(i
));
2071 it87_write_value(data
, IT87_REG_TEMP_HIGH(i
), 127);
2074 /* Temperature channels are not forcibly enabled, as they can be
2075 * set to two different sensor types and we can't guess which one
2076 * is correct for a given system. These channels can be enabled at
2077 * run-time through the temp{1-3}_type sysfs accessors if needed. */
2079 /* Check if voltage monitors are reset manually or by some reason */
2080 tmp
= it87_read_value(data
, IT87_REG_VIN_ENABLE
);
2081 if ((tmp
& 0xff) == 0) {
2082 /* Enable all voltage monitors */
2083 it87_write_value(data
, IT87_REG_VIN_ENABLE
, 0xff);
2086 /* Check if tachometers are reset manually or by some reason */
2087 mask
= 0x70 & ~(sio_data
->skip_fan
<< 4);
2088 data
->fan_main_ctrl
= it87_read_value(data
, IT87_REG_FAN_MAIN_CTRL
);
2089 if ((data
->fan_main_ctrl
& mask
) == 0) {
2090 /* Enable all fan tachometers */
2091 data
->fan_main_ctrl
|= mask
;
2092 it87_write_value(data
, IT87_REG_FAN_MAIN_CTRL
,
2093 data
->fan_main_ctrl
);
2095 data
->has_fan
= (data
->fan_main_ctrl
>> 4) & 0x07;
2097 /* Set tachometers to 16-bit mode if needed */
2098 if (has_16bit_fans(data
)) {
2099 tmp
= it87_read_value(data
, IT87_REG_FAN_16BIT
);
2100 if (~tmp
& 0x07 & data
->has_fan
) {
2102 "Setting fan1-3 to 16-bit mode\n");
2103 it87_write_value(data
, IT87_REG_FAN_16BIT
,
2106 /* IT8705F only supports three fans. */
2107 if (data
->type
!= it87
) {
2109 data
->has_fan
|= (1 << 3); /* fan4 enabled */
2111 data
->has_fan
|= (1 << 4); /* fan5 enabled */
2115 /* Fan input pins may be used for alternative functions */
2116 data
->has_fan
&= ~sio_data
->skip_fan
;
2118 /* Start monitoring */
2119 it87_write_value(data
, IT87_REG_CONFIG
,
2120 (it87_read_value(data
, IT87_REG_CONFIG
) & 0x36)
2121 | (update_vbat
? 0x41 : 0x01));
2124 static void it87_update_pwm_ctrl(struct it87_data
*data
, int nr
)
2126 data
->pwm_ctrl
[nr
] = it87_read_value(data
, IT87_REG_PWM(nr
));
2127 if (has_newer_autopwm(data
)) {
2128 data
->pwm_temp_map
[nr
] = data
->pwm_ctrl
[nr
] & 0x03;
2129 data
->pwm_duty
[nr
] = it87_read_value(data
,
2130 IT87_REG_PWM_DUTY(nr
));
2132 if (data
->pwm_ctrl
[nr
] & 0x80) /* Automatic mode */
2133 data
->pwm_temp_map
[nr
] = data
->pwm_ctrl
[nr
] & 0x03;
2134 else /* Manual mode */
2135 data
->pwm_duty
[nr
] = data
->pwm_ctrl
[nr
] & 0x7f;
2138 if (has_old_autopwm(data
)) {
2141 for (i
= 0; i
< 5 ; i
++)
2142 data
->auto_temp
[nr
][i
] = it87_read_value(data
,
2143 IT87_REG_AUTO_TEMP(nr
, i
));
2144 for (i
= 0; i
< 3 ; i
++)
2145 data
->auto_pwm
[nr
][i
] = it87_read_value(data
,
2146 IT87_REG_AUTO_PWM(nr
, i
));
2150 static struct it87_data
*it87_update_device(struct device
*dev
)
2152 struct it87_data
*data
= dev_get_drvdata(dev
);
2155 mutex_lock(&data
->update_lock
);
2157 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
2160 /* Cleared after each update, so reenable. Value
2161 returned by this read will be previous value */
2162 it87_write_value(data
, IT87_REG_CONFIG
,
2163 it87_read_value(data
, IT87_REG_CONFIG
) | 0x40);
2165 for (i
= 0; i
<= 7; i
++) {
2167 it87_read_value(data
, IT87_REG_VIN(i
));
2169 it87_read_value(data
, IT87_REG_VIN_MIN(i
));
2171 it87_read_value(data
, IT87_REG_VIN_MAX(i
));
2173 /* in8 (battery) has no limit registers */
2174 data
->in
[8] = it87_read_value(data
, IT87_REG_VIN(8));
2176 for (i
= 0; i
< 5; i
++) {
2177 /* Skip disabled fans */
2178 if (!(data
->has_fan
& (1 << i
)))
2182 it87_read_value(data
, IT87_REG_FAN_MIN
[i
]);
2183 data
->fan
[i
] = it87_read_value(data
,
2185 /* Add high byte if in 16-bit mode */
2186 if (has_16bit_fans(data
)) {
2187 data
->fan
[i
] |= it87_read_value(data
,
2188 IT87_REG_FANX
[i
]) << 8;
2189 data
->fan_min
[i
] |= it87_read_value(data
,
2190 IT87_REG_FANX_MIN
[i
]) << 8;
2193 for (i
= 0; i
< 3; i
++) {
2195 it87_read_value(data
, IT87_REG_TEMP(i
));
2196 data
->temp_high
[i
] =
2197 it87_read_value(data
, IT87_REG_TEMP_HIGH(i
));
2199 it87_read_value(data
, IT87_REG_TEMP_LOW(i
));
2202 /* Newer chips don't have clock dividers */
2203 if ((data
->has_fan
& 0x07) && !has_16bit_fans(data
)) {
2204 i
= it87_read_value(data
, IT87_REG_FAN_DIV
);
2205 data
->fan_div
[0] = i
& 0x07;
2206 data
->fan_div
[1] = (i
>> 3) & 0x07;
2207 data
->fan_div
[2] = (i
& 0x40) ? 3 : 1;
2211 it87_read_value(data
, IT87_REG_ALARM1
) |
2212 (it87_read_value(data
, IT87_REG_ALARM2
) << 8) |
2213 (it87_read_value(data
, IT87_REG_ALARM3
) << 16);
2214 data
->beeps
= it87_read_value(data
, IT87_REG_BEEP_ENABLE
);
2216 data
->fan_main_ctrl
= it87_read_value(data
,
2217 IT87_REG_FAN_MAIN_CTRL
);
2218 data
->fan_ctl
= it87_read_value(data
, IT87_REG_FAN_CTL
);
2219 for (i
= 0; i
< 3; i
++)
2220 it87_update_pwm_ctrl(data
, i
);
2222 data
->sensor
= it87_read_value(data
, IT87_REG_TEMP_ENABLE
);
2223 /* The 8705 does not have VID capability.
2224 The 8718 and later don't use IT87_REG_VID for the
2226 if (data
->type
== it8712
|| data
->type
== it8716
) {
2227 data
->vid
= it87_read_value(data
, IT87_REG_VID
);
2228 /* The older IT8712F revisions had only 5 VID pins,
2229 but we assume it is always safe to read 6 bits. */
2232 data
->last_updated
= jiffies
;
2236 mutex_unlock(&data
->update_lock
);
2241 static int __init
it87_device_add(unsigned short address
,
2242 const struct it87_sio_data
*sio_data
)
2244 struct resource res
= {
2245 .start
= address
+ IT87_EC_OFFSET
,
2246 .end
= address
+ IT87_EC_OFFSET
+ IT87_EC_EXTENT
- 1,
2248 .flags
= IORESOURCE_IO
,
2252 err
= acpi_check_resource_conflict(&res
);
2256 pdev
= platform_device_alloc(DRVNAME
, address
);
2259 pr_err("Device allocation failed\n");
2263 err
= platform_device_add_resources(pdev
, &res
, 1);
2265 pr_err("Device resource addition failed (%d)\n", err
);
2266 goto exit_device_put
;
2269 err
= platform_device_add_data(pdev
, sio_data
,
2270 sizeof(struct it87_sio_data
));
2272 pr_err("Platform data allocation failed\n");
2273 goto exit_device_put
;
2276 err
= platform_device_add(pdev
);
2278 pr_err("Device addition failed (%d)\n", err
);
2279 goto exit_device_put
;
2285 platform_device_put(pdev
);
2290 static int __init
sm_it87_init(void)
2293 unsigned short isa_address
= 0;
2294 struct it87_sio_data sio_data
;
2296 memset(&sio_data
, 0, sizeof(struct it87_sio_data
));
2297 err
= it87_find(&isa_address
, &sio_data
);
2300 err
= platform_driver_register(&it87_driver
);
2304 err
= it87_device_add(isa_address
, &sio_data
);
2306 platform_driver_unregister(&it87_driver
);
2313 static void __exit
sm_it87_exit(void)
2315 platform_device_unregister(pdev
);
2316 platform_driver_unregister(&it87_driver
);
2320 MODULE_AUTHOR("Chris Gauthron, "
2321 "Jean Delvare <khali@linux-fr.org>");
2322 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2323 module_param(update_vbat
, bool, 0);
2324 MODULE_PARM_DESC(update_vbat
, "Update vbat if set else return powerup value");
2325 module_param(fix_pwm_polarity
, bool, 0);
2326 MODULE_PARM_DESC(fix_pwm_polarity
,
2327 "Force PWM polarity to active high (DANGEROUS)");
2328 MODULE_LICENSE("GPL");
2330 module_init(sm_it87_init
);
2331 module_exit(sm_it87_exit
);