memcg: always create memsw files if CONFIG_CGROUP_MEM_RES_CTLR_SWAP
[linux-2.6.git] / drivers / hwmon / it87.c
blob0b204e4cf51c5303836c9254d64f808fd8364115
1 /*
2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
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
8 * Fan Controller)
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>
58 #include <linux/io.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 */
76 #define GPIO 0x07
78 #define DEVID 0x20 /* Register: Device ID */
79 #define DEVREV 0x22 /* Register: Device Revision */
81 static inline int superio_inb(int reg)
83 outb(reg, REG);
84 return inb(VAL);
87 static inline void superio_outb(int reg, int val)
89 outb(reg, REG);
90 outb(val, VAL);
93 static int superio_inw(int reg)
95 int val;
96 outb(reg++, REG);
97 val = inb(VAL) << 8;
98 outb(reg, REG);
99 val |= inb(VAL);
100 return val;
103 static inline void superio_select(int ldn)
105 outb(DEV, REG);
106 outb(ldn, VAL);
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))
115 return -EBUSY;
117 outb(0x87, REG);
118 outb(0x01, REG);
119 outb(0x55, REG);
120 outb(0x55, REG);
121 return 0;
124 static inline void superio_exit(void)
126 outb(0x02, REG);
127 outb(0x02, VAL);
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
180 * The IT8718F and IT8720F have the VID value in a different register, in
181 * Super-I/O configuration space.
183 #define IT87_REG_VID 0x0a
185 * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
186 * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
187 * mode.
189 #define IT87_REG_FAN_DIV 0x0b
190 #define IT87_REG_FAN_16BIT 0x0c
192 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
194 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
195 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
196 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
197 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
198 #define IT87_REG_FAN_MAIN_CTRL 0x13
199 #define IT87_REG_FAN_CTL 0x14
200 #define IT87_REG_PWM(nr) (0x15 + (nr))
201 #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
203 #define IT87_REG_VIN(nr) (0x20 + (nr))
204 #define IT87_REG_TEMP(nr) (0x29 + (nr))
206 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
207 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
208 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
209 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
211 #define IT87_REG_VIN_ENABLE 0x50
212 #define IT87_REG_TEMP_ENABLE 0x51
213 #define IT87_REG_BEEP_ENABLE 0x5c
215 #define IT87_REG_CHIPID 0x58
217 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
218 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
221 struct it87_sio_data {
222 enum chips type;
223 /* Values read from Super-I/O config space */
224 u8 revision;
225 u8 vid_value;
226 u8 beep_pin;
227 u8 internal; /* Internal sensors can be labeled */
228 /* Features skipped based on config or DMI */
229 u8 skip_vid;
230 u8 skip_fan;
231 u8 skip_pwm;
235 * For each registered chip, we need to keep some data in memory.
236 * The structure is dynamically allocated.
238 struct it87_data {
239 struct device *hwmon_dev;
240 enum chips type;
241 u8 revision;
243 unsigned short addr;
244 const char *name;
245 struct mutex update_lock;
246 char valid; /* !=0 if following fields are valid */
247 unsigned long last_updated; /* In jiffies */
249 u16 in_scaled; /* Internal voltage sensors are scaled */
250 u8 in[9]; /* Register value */
251 u8 in_max[8]; /* Register value */
252 u8 in_min[8]; /* Register value */
253 u8 has_fan; /* Bitfield, fans enabled */
254 u16 fan[5]; /* Register values, possibly combined */
255 u16 fan_min[5]; /* Register values, possibly combined */
256 s8 temp[3]; /* Register value */
257 s8 temp_high[3]; /* Register value */
258 s8 temp_low[3]; /* Register value */
259 u8 sensor; /* Register value */
260 u8 fan_div[3]; /* Register encoding, shifted right */
261 u8 vid; /* Register encoding, combined */
262 u8 vrm;
263 u32 alarms; /* Register encoding, combined */
264 u8 beeps; /* Register encoding */
265 u8 fan_main_ctrl; /* Register value */
266 u8 fan_ctl; /* Register value */
269 * The following 3 arrays correspond to the same registers up to
270 * the IT8720F. The meaning of bits 6-0 depends on the value of bit
271 * 7, and we want to preserve settings on mode changes, so we have
272 * to track all values separately.
273 * Starting with the IT8721F, the manual PWM duty cycles are stored
274 * in separate registers (8-bit values), so the separate tracking
275 * is no longer needed, but it is still done to keep the driver
276 * simple.
278 u8 pwm_ctrl[3]; /* Register value */
279 u8 pwm_duty[3]; /* Manual PWM value set by user */
280 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
282 /* Automatic fan speed control registers */
283 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
284 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
287 static inline int has_12mv_adc(const struct it87_data *data)
290 * IT8721F and later have a 12 mV ADC, also with internal scaling
291 * on selected inputs.
293 return data->type == it8721
294 || data->type == it8728;
297 static inline int has_newer_autopwm(const struct it87_data *data)
300 * IT8721F and later have separate registers for the temperature
301 * mapping and the manual duty cycle.
303 return data->type == it8721
304 || data->type == it8728;
307 static u8 in_to_reg(const struct it87_data *data, int nr, long val)
309 long lsb;
311 if (has_12mv_adc(data)) {
312 if (data->in_scaled & (1 << nr))
313 lsb = 24;
314 else
315 lsb = 12;
316 } else
317 lsb = 16;
319 val = DIV_ROUND_CLOSEST(val, lsb);
320 return SENSORS_LIMIT(val, 0, 255);
323 static int in_from_reg(const struct it87_data *data, int nr, int val)
325 if (has_12mv_adc(data)) {
326 if (data->in_scaled & (1 << nr))
327 return val * 24;
328 else
329 return val * 12;
330 } else
331 return val * 16;
334 static inline u8 FAN_TO_REG(long rpm, int div)
336 if (rpm == 0)
337 return 255;
338 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
339 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
340 254);
343 static inline u16 FAN16_TO_REG(long rpm)
345 if (rpm == 0)
346 return 0xffff;
347 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
350 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
351 1350000 / ((val) * (div)))
352 /* The divider is fixed to 2 in 16-bit mode */
353 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
354 1350000 / ((val) * 2))
356 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
357 ((val) + 500) / 1000), -128, 127))
358 #define TEMP_FROM_REG(val) ((val) * 1000)
360 static u8 pwm_to_reg(const struct it87_data *data, long val)
362 if (has_newer_autopwm(data))
363 return val;
364 else
365 return val >> 1;
368 static int pwm_from_reg(const struct it87_data *data, u8 reg)
370 if (has_newer_autopwm(data))
371 return reg;
372 else
373 return (reg & 0x7f) << 1;
377 static int DIV_TO_REG(int val)
379 int answer = 0;
380 while (answer < 7 && (val >>= 1))
381 answer++;
382 return answer;
384 #define DIV_FROM_REG(val) (1 << (val))
386 static const unsigned int pwm_freq[8] = {
387 48000000 / 128,
388 24000000 / 128,
389 12000000 / 128,
390 8000000 / 128,
391 6000000 / 128,
392 3000000 / 128,
393 1500000 / 128,
394 750000 / 128,
397 static inline int has_16bit_fans(const struct it87_data *data)
400 * IT8705F Datasheet 0.4.1, 3h == Version G.
401 * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
402 * These are the first revisions with 16-bit tachometer support.
404 return (data->type == it87 && data->revision >= 0x03)
405 || (data->type == it8712 && data->revision >= 0x08)
406 || data->type == it8716
407 || data->type == it8718
408 || data->type == it8720
409 || data->type == it8721
410 || data->type == it8728;
413 static inline int has_old_autopwm(const struct it87_data *data)
416 * The old automatic fan speed control interface is implemented
417 * by IT8705F chips up to revision F and IT8712F chips up to
418 * revision G.
420 return (data->type == it87 && data->revision < 0x03)
421 || (data->type == it8712 && data->revision < 0x08);
424 static int it87_probe(struct platform_device *pdev);
425 static int __devexit it87_remove(struct platform_device *pdev);
427 static int it87_read_value(struct it87_data *data, u8 reg);
428 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
429 static struct it87_data *it87_update_device(struct device *dev);
430 static int it87_check_pwm(struct device *dev);
431 static void it87_init_device(struct platform_device *pdev);
434 static struct platform_driver it87_driver = {
435 .driver = {
436 .owner = THIS_MODULE,
437 .name = DRVNAME,
439 .probe = it87_probe,
440 .remove = __devexit_p(it87_remove),
443 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
444 char *buf)
446 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
447 int nr = sensor_attr->index;
449 struct it87_data *data = it87_update_device(dev);
450 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
453 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
454 char *buf)
456 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
457 int nr = sensor_attr->index;
459 struct it87_data *data = it87_update_device(dev);
460 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
463 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
464 char *buf)
466 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
467 int nr = sensor_attr->index;
469 struct it87_data *data = it87_update_device(dev);
470 return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
473 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
474 const char *buf, size_t count)
476 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
477 int nr = sensor_attr->index;
479 struct it87_data *data = dev_get_drvdata(dev);
480 unsigned long val;
482 if (kstrtoul(buf, 10, &val) < 0)
483 return -EINVAL;
485 mutex_lock(&data->update_lock);
486 data->in_min[nr] = in_to_reg(data, nr, val);
487 it87_write_value(data, IT87_REG_VIN_MIN(nr),
488 data->in_min[nr]);
489 mutex_unlock(&data->update_lock);
490 return count;
492 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
493 const char *buf, size_t count)
495 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
496 int nr = sensor_attr->index;
498 struct it87_data *data = dev_get_drvdata(dev);
499 unsigned long val;
501 if (kstrtoul(buf, 10, &val) < 0)
502 return -EINVAL;
504 mutex_lock(&data->update_lock);
505 data->in_max[nr] = in_to_reg(data, nr, val);
506 it87_write_value(data, IT87_REG_VIN_MAX(nr),
507 data->in_max[nr]);
508 mutex_unlock(&data->update_lock);
509 return count;
512 #define show_in_offset(offset) \
513 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
514 show_in, NULL, offset);
516 #define limit_in_offset(offset) \
517 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
518 show_in_min, set_in_min, offset); \
519 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
520 show_in_max, set_in_max, offset);
522 show_in_offset(0);
523 limit_in_offset(0);
524 show_in_offset(1);
525 limit_in_offset(1);
526 show_in_offset(2);
527 limit_in_offset(2);
528 show_in_offset(3);
529 limit_in_offset(3);
530 show_in_offset(4);
531 limit_in_offset(4);
532 show_in_offset(5);
533 limit_in_offset(5);
534 show_in_offset(6);
535 limit_in_offset(6);
536 show_in_offset(7);
537 limit_in_offset(7);
538 show_in_offset(8);
540 /* 3 temperatures */
541 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
542 char *buf)
544 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
545 int nr = sensor_attr->index;
547 struct it87_data *data = it87_update_device(dev);
548 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
550 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
551 char *buf)
553 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
554 int nr = sensor_attr->index;
556 struct it87_data *data = it87_update_device(dev);
557 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
559 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
560 char *buf)
562 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
563 int nr = sensor_attr->index;
565 struct it87_data *data = it87_update_device(dev);
566 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
568 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
569 const char *buf, size_t count)
571 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572 int nr = sensor_attr->index;
574 struct it87_data *data = dev_get_drvdata(dev);
575 long val;
577 if (kstrtol(buf, 10, &val) < 0)
578 return -EINVAL;
580 mutex_lock(&data->update_lock);
581 data->temp_high[nr] = TEMP_TO_REG(val);
582 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
583 mutex_unlock(&data->update_lock);
584 return count;
586 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
587 const char *buf, size_t count)
589 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
590 int nr = sensor_attr->index;
592 struct it87_data *data = dev_get_drvdata(dev);
593 long val;
595 if (kstrtol(buf, 10, &val) < 0)
596 return -EINVAL;
598 mutex_lock(&data->update_lock);
599 data->temp_low[nr] = TEMP_TO_REG(val);
600 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
601 mutex_unlock(&data->update_lock);
602 return count;
604 #define show_temp_offset(offset) \
605 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
606 show_temp, NULL, offset - 1); \
607 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
608 show_temp_max, set_temp_max, offset - 1); \
609 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
610 show_temp_min, set_temp_min, offset - 1);
612 show_temp_offset(1);
613 show_temp_offset(2);
614 show_temp_offset(3);
616 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
617 char *buf)
619 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
620 int nr = sensor_attr->index;
621 struct it87_data *data = it87_update_device(dev);
622 u8 reg = data->sensor; /* In case value is updated while used */
624 if (reg & (1 << nr))
625 return sprintf(buf, "3\n"); /* thermal diode */
626 if (reg & (8 << nr))
627 return sprintf(buf, "4\n"); /* thermistor */
628 return sprintf(buf, "0\n"); /* disabled */
630 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
631 const char *buf, size_t count)
633 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
634 int nr = sensor_attr->index;
636 struct it87_data *data = dev_get_drvdata(dev);
637 long val;
638 u8 reg;
640 if (kstrtol(buf, 10, &val) < 0)
641 return -EINVAL;
643 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
644 reg &= ~(1 << nr);
645 reg &= ~(8 << nr);
646 if (val == 2) { /* backwards compatibility */
647 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
648 "instead\n");
649 val = 4;
651 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
652 if (val == 3)
653 reg |= 1 << nr;
654 else if (val == 4)
655 reg |= 8 << nr;
656 else if (val != 0)
657 return -EINVAL;
659 mutex_lock(&data->update_lock);
660 data->sensor = reg;
661 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
662 data->valid = 0; /* Force cache refresh */
663 mutex_unlock(&data->update_lock);
664 return count;
666 #define show_sensor_offset(offset) \
667 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
668 show_sensor, set_sensor, offset - 1);
670 show_sensor_offset(1);
671 show_sensor_offset(2);
672 show_sensor_offset(3);
674 /* 3 Fans */
676 static int pwm_mode(const struct it87_data *data, int nr)
678 int ctrl = data->fan_main_ctrl & (1 << nr);
680 if (ctrl == 0) /* Full speed */
681 return 0;
682 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
683 return 2;
684 else /* Manual mode */
685 return 1;
688 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
689 char *buf)
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[nr],
696 DIV_FROM_REG(data->fan_div[nr])));
698 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
699 char *buf)
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", FAN_FROM_REG(data->fan_min[nr],
706 DIV_FROM_REG(data->fan_div[nr])));
708 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
709 char *buf)
711 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
712 int nr = sensor_attr->index;
714 struct it87_data *data = it87_update_device(dev);
715 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
717 static ssize_t show_pwm_enable(struct device *dev,
718 struct device_attribute *attr, char *buf)
720 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
721 int nr = sensor_attr->index;
723 struct it87_data *data = it87_update_device(dev);
724 return sprintf(buf, "%d\n", pwm_mode(data, nr));
726 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
727 char *buf)
729 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
730 int nr = sensor_attr->index;
732 struct it87_data *data = it87_update_device(dev);
733 return sprintf(buf, "%d\n",
734 pwm_from_reg(data, data->pwm_duty[nr]));
736 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
737 char *buf)
739 struct it87_data *data = it87_update_device(dev);
740 int index = (data->fan_ctl >> 4) & 0x07;
742 return sprintf(buf, "%u\n", pwm_freq[index]);
744 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
747 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
748 int nr = sensor_attr->index;
750 struct it87_data *data = dev_get_drvdata(dev);
751 long val;
752 u8 reg;
754 if (kstrtol(buf, 10, &val) < 0)
755 return -EINVAL;
757 mutex_lock(&data->update_lock);
758 reg = it87_read_value(data, IT87_REG_FAN_DIV);
759 switch (nr) {
760 case 0:
761 data->fan_div[nr] = reg & 0x07;
762 break;
763 case 1:
764 data->fan_div[nr] = (reg >> 3) & 0x07;
765 break;
766 case 2:
767 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
768 break;
771 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
772 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
773 mutex_unlock(&data->update_lock);
774 return count;
776 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
777 const char *buf, size_t count)
779 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
780 int nr = sensor_attr->index;
782 struct it87_data *data = dev_get_drvdata(dev);
783 unsigned long val;
784 int min;
785 u8 old;
787 if (kstrtoul(buf, 10, &val) < 0)
788 return -EINVAL;
790 mutex_lock(&data->update_lock);
791 old = it87_read_value(data, IT87_REG_FAN_DIV);
793 /* Save fan min limit */
794 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
796 switch (nr) {
797 case 0:
798 case 1:
799 data->fan_div[nr] = DIV_TO_REG(val);
800 break;
801 case 2:
802 if (val < 8)
803 data->fan_div[nr] = 1;
804 else
805 data->fan_div[nr] = 3;
807 val = old & 0x80;
808 val |= (data->fan_div[0] & 0x07);
809 val |= (data->fan_div[1] & 0x07) << 3;
810 if (data->fan_div[2] == 3)
811 val |= 0x1 << 6;
812 it87_write_value(data, IT87_REG_FAN_DIV, val);
814 /* Restore fan min limit */
815 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
816 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
818 mutex_unlock(&data->update_lock);
819 return count;
822 /* Returns 0 if OK, -EINVAL otherwise */
823 static int check_trip_points(struct device *dev, int nr)
825 const struct it87_data *data = dev_get_drvdata(dev);
826 int i, err = 0;
828 if (has_old_autopwm(data)) {
829 for (i = 0; i < 3; i++) {
830 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
831 err = -EINVAL;
833 for (i = 0; i < 2; i++) {
834 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
835 err = -EINVAL;
839 if (err) {
840 dev_err(dev, "Inconsistent trip points, not switching to "
841 "automatic mode\n");
842 dev_err(dev, "Adjust the trip points and try again\n");
844 return err;
847 static ssize_t set_pwm_enable(struct device *dev,
848 struct device_attribute *attr, const char *buf, size_t count)
850 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
851 int nr = sensor_attr->index;
853 struct it87_data *data = dev_get_drvdata(dev);
854 long val;
856 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
857 return -EINVAL;
859 /* Check trip points before switching to automatic mode */
860 if (val == 2) {
861 if (check_trip_points(dev, nr) < 0)
862 return -EINVAL;
865 mutex_lock(&data->update_lock);
867 if (val == 0) {
868 int tmp;
869 /* make sure the fan is on when in on/off mode */
870 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
871 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
872 /* set on/off mode */
873 data->fan_main_ctrl &= ~(1 << nr);
874 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
875 data->fan_main_ctrl);
876 } else {
877 if (val == 1) /* Manual mode */
878 data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
879 data->pwm_temp_map[nr] :
880 data->pwm_duty[nr];
881 else /* Automatic mode */
882 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
883 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
884 /* set SmartGuardian mode */
885 data->fan_main_ctrl |= (1 << nr);
886 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
887 data->fan_main_ctrl);
890 mutex_unlock(&data->update_lock);
891 return count;
893 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
894 const char *buf, size_t count)
896 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
897 int nr = sensor_attr->index;
899 struct it87_data *data = dev_get_drvdata(dev);
900 long val;
902 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
903 return -EINVAL;
905 mutex_lock(&data->update_lock);
906 if (has_newer_autopwm(data)) {
908 * If we are in automatic mode, the PWM duty cycle register
909 * is read-only so we can't write the value.
911 if (data->pwm_ctrl[nr] & 0x80) {
912 mutex_unlock(&data->update_lock);
913 return -EBUSY;
915 data->pwm_duty[nr] = pwm_to_reg(data, val);
916 it87_write_value(data, IT87_REG_PWM_DUTY(nr),
917 data->pwm_duty[nr]);
918 } else {
919 data->pwm_duty[nr] = pwm_to_reg(data, val);
921 * If we are in manual mode, write the duty cycle immediately;
922 * otherwise, just store it for later use.
924 if (!(data->pwm_ctrl[nr] & 0x80)) {
925 data->pwm_ctrl[nr] = data->pwm_duty[nr];
926 it87_write_value(data, IT87_REG_PWM(nr),
927 data->pwm_ctrl[nr]);
930 mutex_unlock(&data->update_lock);
931 return count;
933 static ssize_t set_pwm_freq(struct device *dev,
934 struct device_attribute *attr, const char *buf, size_t count)
936 struct it87_data *data = dev_get_drvdata(dev);
937 unsigned long val;
938 int i;
940 if (kstrtoul(buf, 10, &val) < 0)
941 return -EINVAL;
943 /* Search for the nearest available frequency */
944 for (i = 0; i < 7; i++) {
945 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
946 break;
949 mutex_lock(&data->update_lock);
950 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
951 data->fan_ctl |= i << 4;
952 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
953 mutex_unlock(&data->update_lock);
955 return count;
957 static ssize_t show_pwm_temp_map(struct device *dev,
958 struct device_attribute *attr, char *buf)
960 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
961 int nr = sensor_attr->index;
963 struct it87_data *data = it87_update_device(dev);
964 int map;
966 if (data->pwm_temp_map[nr] < 3)
967 map = 1 << data->pwm_temp_map[nr];
968 else
969 map = 0; /* Should never happen */
970 return sprintf(buf, "%d\n", map);
972 static ssize_t set_pwm_temp_map(struct device *dev,
973 struct device_attribute *attr, const char *buf, size_t count)
975 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
976 int nr = sensor_attr->index;
978 struct it87_data *data = dev_get_drvdata(dev);
979 long val;
980 u8 reg;
983 * This check can go away if we ever support automatic fan speed
984 * control on newer chips.
986 if (!has_old_autopwm(data)) {
987 dev_notice(dev, "Mapping change disabled for safety reasons\n");
988 return -EINVAL;
991 if (kstrtol(buf, 10, &val) < 0)
992 return -EINVAL;
994 switch (val) {
995 case (1 << 0):
996 reg = 0x00;
997 break;
998 case (1 << 1):
999 reg = 0x01;
1000 break;
1001 case (1 << 2):
1002 reg = 0x02;
1003 break;
1004 default:
1005 return -EINVAL;
1008 mutex_lock(&data->update_lock);
1009 data->pwm_temp_map[nr] = reg;
1011 * If we are in automatic mode, write the temp mapping immediately;
1012 * otherwise, just store it for later use.
1014 if (data->pwm_ctrl[nr] & 0x80) {
1015 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
1016 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
1018 mutex_unlock(&data->update_lock);
1019 return count;
1022 static ssize_t show_auto_pwm(struct device *dev,
1023 struct device_attribute *attr, char *buf)
1025 struct it87_data *data = it87_update_device(dev);
1026 struct sensor_device_attribute_2 *sensor_attr =
1027 to_sensor_dev_attr_2(attr);
1028 int nr = sensor_attr->nr;
1029 int point = sensor_attr->index;
1031 return sprintf(buf, "%d\n",
1032 pwm_from_reg(data, data->auto_pwm[nr][point]));
1035 static ssize_t set_auto_pwm(struct device *dev,
1036 struct device_attribute *attr, const char *buf, size_t count)
1038 struct it87_data *data = dev_get_drvdata(dev);
1039 struct sensor_device_attribute_2 *sensor_attr =
1040 to_sensor_dev_attr_2(attr);
1041 int nr = sensor_attr->nr;
1042 int point = sensor_attr->index;
1043 long val;
1045 if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
1046 return -EINVAL;
1048 mutex_lock(&data->update_lock);
1049 data->auto_pwm[nr][point] = pwm_to_reg(data, val);
1050 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
1051 data->auto_pwm[nr][point]);
1052 mutex_unlock(&data->update_lock);
1053 return count;
1056 static ssize_t show_auto_temp(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1059 struct it87_data *data = it87_update_device(dev);
1060 struct sensor_device_attribute_2 *sensor_attr =
1061 to_sensor_dev_attr_2(attr);
1062 int nr = sensor_attr->nr;
1063 int point = sensor_attr->index;
1065 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
1068 static ssize_t set_auto_temp(struct device *dev,
1069 struct device_attribute *attr, const char *buf, size_t count)
1071 struct it87_data *data = dev_get_drvdata(dev);
1072 struct sensor_device_attribute_2 *sensor_attr =
1073 to_sensor_dev_attr_2(attr);
1074 int nr = sensor_attr->nr;
1075 int point = sensor_attr->index;
1076 long val;
1078 if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
1079 return -EINVAL;
1081 mutex_lock(&data->update_lock);
1082 data->auto_temp[nr][point] = TEMP_TO_REG(val);
1083 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
1084 data->auto_temp[nr][point]);
1085 mutex_unlock(&data->update_lock);
1086 return count;
1089 #define show_fan_offset(offset) \
1090 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
1091 show_fan, NULL, offset - 1); \
1092 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1093 show_fan_min, set_fan_min, offset - 1); \
1094 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
1095 show_fan_div, set_fan_div, offset - 1);
1097 show_fan_offset(1);
1098 show_fan_offset(2);
1099 show_fan_offset(3);
1101 #define show_pwm_offset(offset) \
1102 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
1103 show_pwm_enable, set_pwm_enable, offset - 1); \
1104 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
1105 show_pwm, set_pwm, offset - 1); \
1106 static DEVICE_ATTR(pwm##offset##_freq, \
1107 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
1108 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
1109 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
1110 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1111 offset - 1); \
1112 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1113 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1114 offset - 1, 0); \
1115 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1116 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1117 offset - 1, 1); \
1118 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1119 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1120 offset - 1, 2); \
1121 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1122 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1123 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1124 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1125 offset - 1, 1); \
1126 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1127 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1128 offset - 1, 0); \
1129 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1130 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1131 offset - 1, 2); \
1132 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1133 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1134 offset - 1, 3); \
1135 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1136 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1137 offset - 1, 4);
1139 show_pwm_offset(1);
1140 show_pwm_offset(2);
1141 show_pwm_offset(3);
1143 /* A different set of callbacks for 16-bit fans */
1144 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1145 char *buf)
1147 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1148 int nr = sensor_attr->index;
1149 struct it87_data *data = it87_update_device(dev);
1150 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1153 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1154 char *buf)
1156 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1157 int nr = sensor_attr->index;
1158 struct it87_data *data = it87_update_device(dev);
1159 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1162 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1163 const char *buf, size_t count)
1165 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1166 int nr = sensor_attr->index;
1167 struct it87_data *data = dev_get_drvdata(dev);
1168 long val;
1170 if (kstrtol(buf, 10, &val) < 0)
1171 return -EINVAL;
1173 mutex_lock(&data->update_lock);
1174 data->fan_min[nr] = FAN16_TO_REG(val);
1175 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1176 data->fan_min[nr] & 0xff);
1177 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1178 data->fan_min[nr] >> 8);
1179 mutex_unlock(&data->update_lock);
1180 return count;
1184 * We want to use the same sysfs file names as 8-bit fans, but we need
1185 * different variable names, so we have to use SENSOR_ATTR instead of
1186 * SENSOR_DEVICE_ATTR.
1188 #define show_fan16_offset(offset) \
1189 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1190 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1191 show_fan16, NULL, offset - 1); \
1192 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1193 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1194 show_fan16_min, set_fan16_min, offset - 1)
1196 show_fan16_offset(1);
1197 show_fan16_offset(2);
1198 show_fan16_offset(3);
1199 show_fan16_offset(4);
1200 show_fan16_offset(5);
1202 /* Alarms */
1203 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1204 char *buf)
1206 struct it87_data *data = it87_update_device(dev);
1207 return sprintf(buf, "%u\n", data->alarms);
1209 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1211 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1212 char *buf)
1214 int bitnr = to_sensor_dev_attr(attr)->index;
1215 struct it87_data *data = it87_update_device(dev);
1216 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1219 static ssize_t clear_intrusion(struct device *dev, struct device_attribute
1220 *attr, const char *buf, size_t count)
1222 struct it87_data *data = dev_get_drvdata(dev);
1223 long val;
1224 int config;
1226 if (kstrtol(buf, 10, &val) < 0 || val != 0)
1227 return -EINVAL;
1229 mutex_lock(&data->update_lock);
1230 config = it87_read_value(data, IT87_REG_CONFIG);
1231 if (config < 0) {
1232 count = config;
1233 } else {
1234 config |= 1 << 5;
1235 it87_write_value(data, IT87_REG_CONFIG, config);
1236 /* Invalidate cache to force re-read */
1237 data->valid = 0;
1239 mutex_unlock(&data->update_lock);
1241 return count;
1244 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1245 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1246 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1247 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1248 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1249 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1250 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1251 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1252 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1253 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1254 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1255 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1256 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1257 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1258 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1259 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1260 static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
1261 show_alarm, clear_intrusion, 4);
1263 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1264 char *buf)
1266 int bitnr = to_sensor_dev_attr(attr)->index;
1267 struct it87_data *data = it87_update_device(dev);
1268 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1270 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1271 const char *buf, size_t count)
1273 int bitnr = to_sensor_dev_attr(attr)->index;
1274 struct it87_data *data = dev_get_drvdata(dev);
1275 long val;
1277 if (kstrtol(buf, 10, &val) < 0
1278 || (val != 0 && val != 1))
1279 return -EINVAL;
1281 mutex_lock(&data->update_lock);
1282 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1283 if (val)
1284 data->beeps |= (1 << bitnr);
1285 else
1286 data->beeps &= ~(1 << bitnr);
1287 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1288 mutex_unlock(&data->update_lock);
1289 return count;
1292 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1293 show_beep, set_beep, 1);
1294 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1295 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1296 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1297 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1298 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1299 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1300 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1301 /* fanX_beep writability is set later */
1302 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1303 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1304 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1305 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1306 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1307 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1308 show_beep, set_beep, 2);
1309 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1310 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1312 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1313 char *buf)
1315 struct it87_data *data = dev_get_drvdata(dev);
1316 return sprintf(buf, "%u\n", data->vrm);
1318 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1319 const char *buf, size_t count)
1321 struct it87_data *data = dev_get_drvdata(dev);
1322 unsigned long val;
1324 if (kstrtoul(buf, 10, &val) < 0)
1325 return -EINVAL;
1327 data->vrm = val;
1329 return count;
1331 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1333 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1334 char *buf)
1336 struct it87_data *data = it87_update_device(dev);
1337 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1339 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1341 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1342 char *buf)
1344 static const char * const labels[] = {
1345 "+5V",
1346 "5VSB",
1347 "Vbat",
1349 static const char * const labels_it8721[] = {
1350 "+3.3V",
1351 "3VSB",
1352 "Vbat",
1354 struct it87_data *data = dev_get_drvdata(dev);
1355 int nr = to_sensor_dev_attr(attr)->index;
1357 return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
1358 : labels[nr]);
1360 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1361 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1362 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1364 static ssize_t show_name(struct device *dev, struct device_attribute
1365 *devattr, char *buf)
1367 struct it87_data *data = dev_get_drvdata(dev);
1368 return sprintf(buf, "%s\n", data->name);
1370 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1372 static struct attribute *it87_attributes[] = {
1373 &sensor_dev_attr_in0_input.dev_attr.attr,
1374 &sensor_dev_attr_in1_input.dev_attr.attr,
1375 &sensor_dev_attr_in2_input.dev_attr.attr,
1376 &sensor_dev_attr_in3_input.dev_attr.attr,
1377 &sensor_dev_attr_in4_input.dev_attr.attr,
1378 &sensor_dev_attr_in5_input.dev_attr.attr,
1379 &sensor_dev_attr_in6_input.dev_attr.attr,
1380 &sensor_dev_attr_in7_input.dev_attr.attr,
1381 &sensor_dev_attr_in8_input.dev_attr.attr,
1382 &sensor_dev_attr_in0_min.dev_attr.attr,
1383 &sensor_dev_attr_in1_min.dev_attr.attr,
1384 &sensor_dev_attr_in2_min.dev_attr.attr,
1385 &sensor_dev_attr_in3_min.dev_attr.attr,
1386 &sensor_dev_attr_in4_min.dev_attr.attr,
1387 &sensor_dev_attr_in5_min.dev_attr.attr,
1388 &sensor_dev_attr_in6_min.dev_attr.attr,
1389 &sensor_dev_attr_in7_min.dev_attr.attr,
1390 &sensor_dev_attr_in0_max.dev_attr.attr,
1391 &sensor_dev_attr_in1_max.dev_attr.attr,
1392 &sensor_dev_attr_in2_max.dev_attr.attr,
1393 &sensor_dev_attr_in3_max.dev_attr.attr,
1394 &sensor_dev_attr_in4_max.dev_attr.attr,
1395 &sensor_dev_attr_in5_max.dev_attr.attr,
1396 &sensor_dev_attr_in6_max.dev_attr.attr,
1397 &sensor_dev_attr_in7_max.dev_attr.attr,
1398 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1399 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1400 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1401 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1402 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1403 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1404 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1405 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1407 &sensor_dev_attr_temp1_input.dev_attr.attr,
1408 &sensor_dev_attr_temp2_input.dev_attr.attr,
1409 &sensor_dev_attr_temp3_input.dev_attr.attr,
1410 &sensor_dev_attr_temp1_max.dev_attr.attr,
1411 &sensor_dev_attr_temp2_max.dev_attr.attr,
1412 &sensor_dev_attr_temp3_max.dev_attr.attr,
1413 &sensor_dev_attr_temp1_min.dev_attr.attr,
1414 &sensor_dev_attr_temp2_min.dev_attr.attr,
1415 &sensor_dev_attr_temp3_min.dev_attr.attr,
1416 &sensor_dev_attr_temp1_type.dev_attr.attr,
1417 &sensor_dev_attr_temp2_type.dev_attr.attr,
1418 &sensor_dev_attr_temp3_type.dev_attr.attr,
1419 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1420 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1421 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1423 &dev_attr_alarms.attr,
1424 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
1425 &dev_attr_name.attr,
1426 NULL
1429 static const struct attribute_group it87_group = {
1430 .attrs = it87_attributes,
1433 static struct attribute *it87_attributes_beep[] = {
1434 &sensor_dev_attr_in0_beep.dev_attr.attr,
1435 &sensor_dev_attr_in1_beep.dev_attr.attr,
1436 &sensor_dev_attr_in2_beep.dev_attr.attr,
1437 &sensor_dev_attr_in3_beep.dev_attr.attr,
1438 &sensor_dev_attr_in4_beep.dev_attr.attr,
1439 &sensor_dev_attr_in5_beep.dev_attr.attr,
1440 &sensor_dev_attr_in6_beep.dev_attr.attr,
1441 &sensor_dev_attr_in7_beep.dev_attr.attr,
1443 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1444 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1445 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1446 NULL
1449 static const struct attribute_group it87_group_beep = {
1450 .attrs = it87_attributes_beep,
1453 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1454 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1455 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1456 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1457 NULL
1458 }, {
1459 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1460 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1461 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1462 NULL
1463 }, {
1464 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1465 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1466 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1467 NULL
1468 }, {
1469 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1470 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1471 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1472 NULL
1473 }, {
1474 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1475 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1476 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1477 NULL
1478 } };
1480 static const struct attribute_group it87_group_fan16[5] = {
1481 { .attrs = it87_attributes_fan16[0] },
1482 { .attrs = it87_attributes_fan16[1] },
1483 { .attrs = it87_attributes_fan16[2] },
1484 { .attrs = it87_attributes_fan16[3] },
1485 { .attrs = it87_attributes_fan16[4] },
1488 static struct attribute *it87_attributes_fan[3][4+1] = { {
1489 &sensor_dev_attr_fan1_input.dev_attr.attr,
1490 &sensor_dev_attr_fan1_min.dev_attr.attr,
1491 &sensor_dev_attr_fan1_div.dev_attr.attr,
1492 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1493 NULL
1494 }, {
1495 &sensor_dev_attr_fan2_input.dev_attr.attr,
1496 &sensor_dev_attr_fan2_min.dev_attr.attr,
1497 &sensor_dev_attr_fan2_div.dev_attr.attr,
1498 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1499 NULL
1500 }, {
1501 &sensor_dev_attr_fan3_input.dev_attr.attr,
1502 &sensor_dev_attr_fan3_min.dev_attr.attr,
1503 &sensor_dev_attr_fan3_div.dev_attr.attr,
1504 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1505 NULL
1506 } };
1508 static const struct attribute_group it87_group_fan[3] = {
1509 { .attrs = it87_attributes_fan[0] },
1510 { .attrs = it87_attributes_fan[1] },
1511 { .attrs = it87_attributes_fan[2] },
1514 static const struct attribute_group *
1515 it87_get_fan_group(const struct it87_data *data)
1517 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1520 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1521 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1522 &sensor_dev_attr_pwm1.dev_attr.attr,
1523 &dev_attr_pwm1_freq.attr,
1524 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1525 NULL
1526 }, {
1527 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1528 &sensor_dev_attr_pwm2.dev_attr.attr,
1529 &dev_attr_pwm2_freq.attr,
1530 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1531 NULL
1532 }, {
1533 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1534 &sensor_dev_attr_pwm3.dev_attr.attr,
1535 &dev_attr_pwm3_freq.attr,
1536 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1537 NULL
1538 } };
1540 static const struct attribute_group it87_group_pwm[3] = {
1541 { .attrs = it87_attributes_pwm[0] },
1542 { .attrs = it87_attributes_pwm[1] },
1543 { .attrs = it87_attributes_pwm[2] },
1546 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1547 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1548 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1549 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1550 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1551 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1552 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1553 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1554 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1555 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1556 NULL
1557 }, {
1558 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1559 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1560 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1561 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1562 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1563 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1564 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1565 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1566 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1567 NULL
1568 }, {
1569 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1570 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1571 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1572 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1573 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1574 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1575 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1576 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1577 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1578 NULL
1579 } };
1581 static const struct attribute_group it87_group_autopwm[3] = {
1582 { .attrs = it87_attributes_autopwm[0] },
1583 { .attrs = it87_attributes_autopwm[1] },
1584 { .attrs = it87_attributes_autopwm[2] },
1587 static struct attribute *it87_attributes_fan_beep[] = {
1588 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1589 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1590 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1591 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1592 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1595 static struct attribute *it87_attributes_vid[] = {
1596 &dev_attr_vrm.attr,
1597 &dev_attr_cpu0_vid.attr,
1598 NULL
1601 static const struct attribute_group it87_group_vid = {
1602 .attrs = it87_attributes_vid,
1605 static struct attribute *it87_attributes_label[] = {
1606 &sensor_dev_attr_in3_label.dev_attr.attr,
1607 &sensor_dev_attr_in7_label.dev_attr.attr,
1608 &sensor_dev_attr_in8_label.dev_attr.attr,
1609 NULL
1612 static const struct attribute_group it87_group_label = {
1613 .attrs = it87_attributes_label,
1616 /* SuperIO detection - will change isa_address if a chip is found */
1617 static int __init it87_find(unsigned short *address,
1618 struct it87_sio_data *sio_data)
1620 int err;
1621 u16 chip_type;
1622 const char *board_vendor, *board_name;
1624 err = superio_enter();
1625 if (err)
1626 return err;
1628 err = -ENODEV;
1629 chip_type = force_id ? force_id : superio_inw(DEVID);
1631 switch (chip_type) {
1632 case IT8705F_DEVID:
1633 sio_data->type = it87;
1634 break;
1635 case IT8712F_DEVID:
1636 sio_data->type = it8712;
1637 break;
1638 case IT8716F_DEVID:
1639 case IT8726F_DEVID:
1640 sio_data->type = it8716;
1641 break;
1642 case IT8718F_DEVID:
1643 sio_data->type = it8718;
1644 break;
1645 case IT8720F_DEVID:
1646 sio_data->type = it8720;
1647 break;
1648 case IT8721F_DEVID:
1649 sio_data->type = it8721;
1650 break;
1651 case IT8728F_DEVID:
1652 sio_data->type = it8728;
1653 break;
1654 case 0xffff: /* No device at all */
1655 goto exit;
1656 default:
1657 pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
1658 goto exit;
1661 superio_select(PME);
1662 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1663 pr_info("Device not activated, skipping\n");
1664 goto exit;
1667 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1668 if (*address == 0) {
1669 pr_info("Base address not set, skipping\n");
1670 goto exit;
1673 err = 0;
1674 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1675 pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
1676 chip_type, *address, sio_data->revision);
1678 /* in8 (Vbat) is always internal */
1679 sio_data->internal = (1 << 2);
1681 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1682 if (sio_data->type == it87) {
1683 /* The IT8705F doesn't have VID pins at all */
1684 sio_data->skip_vid = 1;
1686 /* The IT8705F has a different LD number for GPIO */
1687 superio_select(5);
1688 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1689 } else {
1690 int reg;
1692 superio_select(GPIO);
1694 reg = superio_inb(IT87_SIO_GPIO3_REG);
1695 if (sio_data->type == it8721 || sio_data->type == it8728) {
1697 * The IT8721F/IT8758E doesn't have VID pins at all,
1698 * not sure about the IT8728F.
1700 sio_data->skip_vid = 1;
1701 } else {
1702 /* We need at least 4 VID pins */
1703 if (reg & 0x0f) {
1704 pr_info("VID is disabled (pins used for GPIO)\n");
1705 sio_data->skip_vid = 1;
1709 /* Check if fan3 is there or not */
1710 if (reg & (1 << 6))
1711 sio_data->skip_pwm |= (1 << 2);
1712 if (reg & (1 << 7))
1713 sio_data->skip_fan |= (1 << 2);
1715 /* Check if fan2 is there or not */
1716 reg = superio_inb(IT87_SIO_GPIO5_REG);
1717 if (reg & (1 << 1))
1718 sio_data->skip_pwm |= (1 << 1);
1719 if (reg & (1 << 2))
1720 sio_data->skip_fan |= (1 << 1);
1722 if ((sio_data->type == it8718 || sio_data->type == it8720)
1723 && !(sio_data->skip_vid))
1724 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1726 reg = superio_inb(IT87_SIO_PINX2_REG);
1728 * The IT8720F has no VIN7 pin, so VCCH should always be
1729 * routed internally to VIN7 with an internal divider.
1730 * Curiously, there still is a configuration bit to control
1731 * this, which means it can be set incorrectly. And even
1732 * more curiously, many boards out there are improperly
1733 * configured, even though the IT8720F datasheet claims
1734 * that the internal routing of VCCH to VIN7 is the default
1735 * setting. So we force the internal routing in this case.
1737 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1738 reg |= (1 << 1);
1739 superio_outb(IT87_SIO_PINX2_REG, reg);
1740 pr_notice("Routing internal VCCH to in7\n");
1742 if (reg & (1 << 0))
1743 sio_data->internal |= (1 << 0);
1744 if ((reg & (1 << 1)) || sio_data->type == it8721 ||
1745 sio_data->type == it8728)
1746 sio_data->internal |= (1 << 1);
1748 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1750 if (sio_data->beep_pin)
1751 pr_info("Beeping is supported\n");
1753 /* Disable specific features based on DMI strings */
1754 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1755 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1756 if (board_vendor && board_name) {
1757 if (strcmp(board_vendor, "nVIDIA") == 0
1758 && strcmp(board_name, "FN68PT") == 0) {
1760 * On the Shuttle SN68PT, FAN_CTL2 is apparently not
1761 * connected to a fan, but to something else. One user
1762 * has reported instant system power-off when changing
1763 * the PWM2 duty cycle, so we disable it.
1764 * I use the board name string as the trigger in case
1765 * the same board is ever used in other systems.
1767 pr_info("Disabling pwm2 due to hardware constraints\n");
1768 sio_data->skip_pwm = (1 << 1);
1772 exit:
1773 superio_exit();
1774 return err;
1777 static void it87_remove_files(struct device *dev)
1779 struct it87_data *data = platform_get_drvdata(pdev);
1780 struct it87_sio_data *sio_data = dev->platform_data;
1781 const struct attribute_group *fan_group = it87_get_fan_group(data);
1782 int i;
1784 sysfs_remove_group(&dev->kobj, &it87_group);
1785 if (sio_data->beep_pin)
1786 sysfs_remove_group(&dev->kobj, &it87_group_beep);
1787 for (i = 0; i < 5; i++) {
1788 if (!(data->has_fan & (1 << i)))
1789 continue;
1790 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1791 if (sio_data->beep_pin)
1792 sysfs_remove_file(&dev->kobj,
1793 it87_attributes_fan_beep[i]);
1795 for (i = 0; i < 3; i++) {
1796 if (sio_data->skip_pwm & (1 << 0))
1797 continue;
1798 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1799 if (has_old_autopwm(data))
1800 sysfs_remove_group(&dev->kobj,
1801 &it87_group_autopwm[i]);
1803 if (!sio_data->skip_vid)
1804 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1805 sysfs_remove_group(&dev->kobj, &it87_group_label);
1808 static int __devinit it87_probe(struct platform_device *pdev)
1810 struct it87_data *data;
1811 struct resource *res;
1812 struct device *dev = &pdev->dev;
1813 struct it87_sio_data *sio_data = dev->platform_data;
1814 const struct attribute_group *fan_group;
1815 int err = 0, i;
1816 int enable_pwm_interface;
1817 int fan_beep_need_rw;
1818 static const char * const names[] = {
1819 "it87",
1820 "it8712",
1821 "it8716",
1822 "it8718",
1823 "it8720",
1824 "it8721",
1825 "it8728",
1828 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1829 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1830 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1831 (unsigned long)res->start,
1832 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1833 err = -EBUSY;
1834 goto ERROR0;
1837 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1838 if (!data) {
1839 err = -ENOMEM;
1840 goto ERROR1;
1843 data->addr = res->start;
1844 data->type = sio_data->type;
1845 data->revision = sio_data->revision;
1846 data->name = names[sio_data->type];
1848 /* Now, we do the remaining detection. */
1849 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1850 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1851 err = -ENODEV;
1852 goto ERROR2;
1855 platform_set_drvdata(pdev, data);
1857 mutex_init(&data->update_lock);
1859 /* Check PWM configuration */
1860 enable_pwm_interface = it87_check_pwm(dev);
1862 /* Starting with IT8721F, we handle scaling of internal voltages */
1863 if (has_12mv_adc(data)) {
1864 if (sio_data->internal & (1 << 0))
1865 data->in_scaled |= (1 << 3); /* in3 is AVCC */
1866 if (sio_data->internal & (1 << 1))
1867 data->in_scaled |= (1 << 7); /* in7 is VSB */
1868 if (sio_data->internal & (1 << 2))
1869 data->in_scaled |= (1 << 8); /* in8 is Vbat */
1872 /* Initialize the IT87 chip */
1873 it87_init_device(pdev);
1875 /* Register sysfs hooks */
1876 err = sysfs_create_group(&dev->kobj, &it87_group);
1877 if (err)
1878 goto ERROR2;
1880 if (sio_data->beep_pin) {
1881 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1882 if (err)
1883 goto ERROR4;
1886 /* Do not create fan files for disabled fans */
1887 fan_group = it87_get_fan_group(data);
1888 fan_beep_need_rw = 1;
1889 for (i = 0; i < 5; i++) {
1890 if (!(data->has_fan & (1 << i)))
1891 continue;
1892 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1893 if (err)
1894 goto ERROR4;
1896 if (sio_data->beep_pin) {
1897 err = sysfs_create_file(&dev->kobj,
1898 it87_attributes_fan_beep[i]);
1899 if (err)
1900 goto ERROR4;
1901 if (!fan_beep_need_rw)
1902 continue;
1905 * As we have a single beep enable bit for all fans,
1906 * only the first enabled fan has a writable attribute
1907 * for it.
1909 if (sysfs_chmod_file(&dev->kobj,
1910 it87_attributes_fan_beep[i],
1911 S_IRUGO | S_IWUSR))
1912 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1913 i + 1);
1914 fan_beep_need_rw = 0;
1918 if (enable_pwm_interface) {
1919 for (i = 0; i < 3; i++) {
1920 if (sio_data->skip_pwm & (1 << i))
1921 continue;
1922 err = sysfs_create_group(&dev->kobj,
1923 &it87_group_pwm[i]);
1924 if (err)
1925 goto ERROR4;
1927 if (!has_old_autopwm(data))
1928 continue;
1929 err = sysfs_create_group(&dev->kobj,
1930 &it87_group_autopwm[i]);
1931 if (err)
1932 goto ERROR4;
1936 if (!sio_data->skip_vid) {
1937 data->vrm = vid_which_vrm();
1938 /* VID reading from Super-I/O config space if available */
1939 data->vid = sio_data->vid_value;
1940 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1941 if (err)
1942 goto ERROR4;
1945 /* Export labels for internal sensors */
1946 for (i = 0; i < 3; i++) {
1947 if (!(sio_data->internal & (1 << i)))
1948 continue;
1949 err = sysfs_create_file(&dev->kobj,
1950 it87_attributes_label[i]);
1951 if (err)
1952 goto ERROR4;
1955 data->hwmon_dev = hwmon_device_register(dev);
1956 if (IS_ERR(data->hwmon_dev)) {
1957 err = PTR_ERR(data->hwmon_dev);
1958 goto ERROR4;
1961 return 0;
1963 ERROR4:
1964 it87_remove_files(dev);
1965 ERROR2:
1966 platform_set_drvdata(pdev, NULL);
1967 kfree(data);
1968 ERROR1:
1969 release_region(res->start, IT87_EC_EXTENT);
1970 ERROR0:
1971 return err;
1974 static int __devexit it87_remove(struct platform_device *pdev)
1976 struct it87_data *data = platform_get_drvdata(pdev);
1978 hwmon_device_unregister(data->hwmon_dev);
1979 it87_remove_files(&pdev->dev);
1981 release_region(data->addr, IT87_EC_EXTENT);
1982 platform_set_drvdata(pdev, NULL);
1983 kfree(data);
1985 return 0;
1989 * Must be called with data->update_lock held, except during initialization.
1990 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1991 * would slow down the IT87 access and should not be necessary.
1993 static int it87_read_value(struct it87_data *data, u8 reg)
1995 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1996 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
2000 * Must be called with data->update_lock held, except during initialization.
2001 * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
2002 * would slow down the IT87 access and should not be necessary.
2004 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
2006 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
2007 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
2010 /* Return 1 if and only if the PWM interface is safe to use */
2011 static int __devinit it87_check_pwm(struct device *dev)
2013 struct it87_data *data = dev_get_drvdata(dev);
2015 * Some BIOSes fail to correctly configure the IT87 fans. All fans off
2016 * and polarity set to active low is sign that this is the case so we
2017 * disable pwm control to protect the user.
2019 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
2020 if ((tmp & 0x87) == 0) {
2021 if (fix_pwm_polarity) {
2023 * The user asks us to attempt a chip reconfiguration.
2024 * This means switching to active high polarity and
2025 * inverting all fan speed values.
2027 int i;
2028 u8 pwm[3];
2030 for (i = 0; i < 3; i++)
2031 pwm[i] = it87_read_value(data,
2032 IT87_REG_PWM(i));
2035 * If any fan is in automatic pwm mode, the polarity
2036 * might be correct, as suspicious as it seems, so we
2037 * better don't change anything (but still disable the
2038 * PWM interface).
2040 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
2041 dev_info(dev, "Reconfiguring PWM to "
2042 "active high polarity\n");
2043 it87_write_value(data, IT87_REG_FAN_CTL,
2044 tmp | 0x87);
2045 for (i = 0; i < 3; i++)
2046 it87_write_value(data,
2047 IT87_REG_PWM(i),
2048 0x7f & ~pwm[i]);
2049 return 1;
2052 dev_info(dev, "PWM configuration is "
2053 "too broken to be fixed\n");
2056 dev_info(dev, "Detected broken BIOS "
2057 "defaults, disabling PWM interface\n");
2058 return 0;
2059 } else if (fix_pwm_polarity) {
2060 dev_info(dev, "PWM configuration looks "
2061 "sane, won't touch\n");
2064 return 1;
2067 /* Called when we have found a new IT87. */
2068 static void __devinit it87_init_device(struct platform_device *pdev)
2070 struct it87_sio_data *sio_data = pdev->dev.platform_data;
2071 struct it87_data *data = platform_get_drvdata(pdev);
2072 int tmp, i;
2073 u8 mask;
2076 * For each PWM channel:
2077 * - If it is in automatic mode, setting to manual mode should set
2078 * the fan to full speed by default.
2079 * - If it is in manual mode, we need a mapping to temperature
2080 * channels to use when later setting to automatic mode later.
2081 * Use a 1:1 mapping by default (we are clueless.)
2082 * In both cases, the value can (and should) be changed by the user
2083 * prior to switching to a different mode.
2084 * Note that this is no longer needed for the IT8721F and later, as
2085 * these have separate registers for the temperature mapping and the
2086 * manual duty cycle.
2088 for (i = 0; i < 3; i++) {
2089 data->pwm_temp_map[i] = i;
2090 data->pwm_duty[i] = 0x7f; /* Full speed */
2091 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
2095 * Some chips seem to have default value 0xff for all limit
2096 * registers. For low voltage limits it makes no sense and triggers
2097 * alarms, so change to 0 instead. For high temperature limits, it
2098 * means -1 degree C, which surprisingly doesn't trigger an alarm,
2099 * but is still confusing, so change to 127 degrees C.
2101 for (i = 0; i < 8; i++) {
2102 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
2103 if (tmp == 0xff)
2104 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
2106 for (i = 0; i < 3; i++) {
2107 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2108 if (tmp == 0xff)
2109 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
2113 * Temperature channels are not forcibly enabled, as they can be
2114 * set to two different sensor types and we can't guess which one
2115 * is correct for a given system. These channels can be enabled at
2116 * run-time through the temp{1-3}_type sysfs accessors if needed.
2119 /* Check if voltage monitors are reset manually or by some reason */
2120 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
2121 if ((tmp & 0xff) == 0) {
2122 /* Enable all voltage monitors */
2123 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
2126 /* Check if tachometers are reset manually or by some reason */
2127 mask = 0x70 & ~(sio_data->skip_fan << 4);
2128 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
2129 if ((data->fan_main_ctrl & mask) == 0) {
2130 /* Enable all fan tachometers */
2131 data->fan_main_ctrl |= mask;
2132 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
2133 data->fan_main_ctrl);
2135 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
2137 /* Set tachometers to 16-bit mode if needed */
2138 if (has_16bit_fans(data)) {
2139 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
2140 if (~tmp & 0x07 & data->has_fan) {
2141 dev_dbg(&pdev->dev,
2142 "Setting fan1-3 to 16-bit mode\n");
2143 it87_write_value(data, IT87_REG_FAN_16BIT,
2144 tmp | 0x07);
2146 /* IT8705F only supports three fans. */
2147 if (data->type != it87) {
2148 if (tmp & (1 << 4))
2149 data->has_fan |= (1 << 3); /* fan4 enabled */
2150 if (tmp & (1 << 5))
2151 data->has_fan |= (1 << 4); /* fan5 enabled */
2155 /* Fan input pins may be used for alternative functions */
2156 data->has_fan &= ~sio_data->skip_fan;
2158 /* Start monitoring */
2159 it87_write_value(data, IT87_REG_CONFIG,
2160 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
2161 | (update_vbat ? 0x41 : 0x01));
2164 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
2166 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
2167 if (has_newer_autopwm(data)) {
2168 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2169 data->pwm_duty[nr] = it87_read_value(data,
2170 IT87_REG_PWM_DUTY(nr));
2171 } else {
2172 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
2173 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
2174 else /* Manual mode */
2175 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
2178 if (has_old_autopwm(data)) {
2179 int i;
2181 for (i = 0; i < 5 ; i++)
2182 data->auto_temp[nr][i] = it87_read_value(data,
2183 IT87_REG_AUTO_TEMP(nr, i));
2184 for (i = 0; i < 3 ; i++)
2185 data->auto_pwm[nr][i] = it87_read_value(data,
2186 IT87_REG_AUTO_PWM(nr, i));
2190 static struct it87_data *it87_update_device(struct device *dev)
2192 struct it87_data *data = dev_get_drvdata(dev);
2193 int i;
2195 mutex_lock(&data->update_lock);
2197 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
2198 || !data->valid) {
2199 if (update_vbat) {
2201 * Cleared after each update, so reenable. Value
2202 * returned by this read will be previous value
2204 it87_write_value(data, IT87_REG_CONFIG,
2205 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
2207 for (i = 0; i <= 7; i++) {
2208 data->in[i] =
2209 it87_read_value(data, IT87_REG_VIN(i));
2210 data->in_min[i] =
2211 it87_read_value(data, IT87_REG_VIN_MIN(i));
2212 data->in_max[i] =
2213 it87_read_value(data, IT87_REG_VIN_MAX(i));
2215 /* in8 (battery) has no limit registers */
2216 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
2218 for (i = 0; i < 5; i++) {
2219 /* Skip disabled fans */
2220 if (!(data->has_fan & (1 << i)))
2221 continue;
2223 data->fan_min[i] =
2224 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2225 data->fan[i] = it87_read_value(data,
2226 IT87_REG_FAN[i]);
2227 /* Add high byte if in 16-bit mode */
2228 if (has_16bit_fans(data)) {
2229 data->fan[i] |= it87_read_value(data,
2230 IT87_REG_FANX[i]) << 8;
2231 data->fan_min[i] |= it87_read_value(data,
2232 IT87_REG_FANX_MIN[i]) << 8;
2235 for (i = 0; i < 3; i++) {
2236 data->temp[i] =
2237 it87_read_value(data, IT87_REG_TEMP(i));
2238 data->temp_high[i] =
2239 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2240 data->temp_low[i] =
2241 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2244 /* Newer chips don't have clock dividers */
2245 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2246 i = it87_read_value(data, IT87_REG_FAN_DIV);
2247 data->fan_div[0] = i & 0x07;
2248 data->fan_div[1] = (i >> 3) & 0x07;
2249 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2252 data->alarms =
2253 it87_read_value(data, IT87_REG_ALARM1) |
2254 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2255 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2256 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2258 data->fan_main_ctrl = it87_read_value(data,
2259 IT87_REG_FAN_MAIN_CTRL);
2260 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2261 for (i = 0; i < 3; i++)
2262 it87_update_pwm_ctrl(data, i);
2264 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2266 * The IT8705F does not have VID capability.
2267 * The IT8718F and later don't use IT87_REG_VID for the
2268 * same purpose.
2270 if (data->type == it8712 || data->type == it8716) {
2271 data->vid = it87_read_value(data, IT87_REG_VID);
2273 * The older IT8712F revisions had only 5 VID pins,
2274 * but we assume it is always safe to read 6 bits.
2276 data->vid &= 0x3f;
2278 data->last_updated = jiffies;
2279 data->valid = 1;
2282 mutex_unlock(&data->update_lock);
2284 return data;
2287 static int __init it87_device_add(unsigned short address,
2288 const struct it87_sio_data *sio_data)
2290 struct resource res = {
2291 .start = address + IT87_EC_OFFSET,
2292 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2293 .name = DRVNAME,
2294 .flags = IORESOURCE_IO,
2296 int err;
2298 err = acpi_check_resource_conflict(&res);
2299 if (err)
2300 goto exit;
2302 pdev = platform_device_alloc(DRVNAME, address);
2303 if (!pdev) {
2304 err = -ENOMEM;
2305 pr_err("Device allocation failed\n");
2306 goto exit;
2309 err = platform_device_add_resources(pdev, &res, 1);
2310 if (err) {
2311 pr_err("Device resource addition failed (%d)\n", err);
2312 goto exit_device_put;
2315 err = platform_device_add_data(pdev, sio_data,
2316 sizeof(struct it87_sio_data));
2317 if (err) {
2318 pr_err("Platform data allocation failed\n");
2319 goto exit_device_put;
2322 err = platform_device_add(pdev);
2323 if (err) {
2324 pr_err("Device addition failed (%d)\n", err);
2325 goto exit_device_put;
2328 return 0;
2330 exit_device_put:
2331 platform_device_put(pdev);
2332 exit:
2333 return err;
2336 static int __init sm_it87_init(void)
2338 int err;
2339 unsigned short isa_address = 0;
2340 struct it87_sio_data sio_data;
2342 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2343 err = it87_find(&isa_address, &sio_data);
2344 if (err)
2345 return err;
2346 err = platform_driver_register(&it87_driver);
2347 if (err)
2348 return err;
2350 err = it87_device_add(isa_address, &sio_data);
2351 if (err) {
2352 platform_driver_unregister(&it87_driver);
2353 return err;
2356 return 0;
2359 static void __exit sm_it87_exit(void)
2361 platform_device_unregister(pdev);
2362 platform_driver_unregister(&it87_driver);
2366 MODULE_AUTHOR("Chris Gauthron, "
2367 "Jean Delvare <khali@linux-fr.org>");
2368 MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
2369 module_param(update_vbat, bool, 0);
2370 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2371 module_param(fix_pwm_polarity, bool, 0);
2372 MODULE_PARM_DESC(fix_pwm_polarity,
2373 "Force PWM polarity to active high (DANGEROUS)");
2374 MODULE_LICENSE("GPL");
2376 module_init(sm_it87_init);
2377 module_exit(sm_it87_exit);