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 IT8726F Super I/O chip w/LPC interface
18 Sis950 A clone of the IT8705F
20 Copyright (C) 2001 Chris Gauthron
21 Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org>
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2 of the License, or
26 (at your option) any later version.
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/jiffies.h>
42 #include <linux/platform_device.h>
43 #include <linux/hwmon.h>
44 #include <linux/hwmon-sysfs.h>
45 #include <linux/hwmon-vid.h>
46 #include <linux/err.h>
47 #include <linux/mutex.h>
48 #include <linux/sysfs.h>
51 #define DRVNAME "it87"
53 enum chips { it87, it8712, it8716, it8718 };
55 static unsigned short force_id;
56 module_param(force_id, ushort, 0);
57 MODULE_PARM_DESC(force_id, "Override the detected device ID");
59 static struct platform_device *pdev;
61 #define REG 0x2e /* The register to read/write */
62 #define DEV 0x07 /* Register: Logical device select */
63 #define VAL 0x2f /* The value to read/write */
64 #define PME 0x04 /* The device with the fan registers in it */
65 #define GPIO 0x07 /* The device with the IT8718F VID value in it */
66 #define DEVID 0x20 /* Register: Device ID */
67 #define DEVREV 0x22 /* Register: Device Revision */
76 static int superio_inw(int reg)
87 superio_select(int ldn)
109 /* Logical device 4 registers */
110 #define IT8712F_DEVID 0x8712
111 #define IT8705F_DEVID 0x8705
112 #define IT8716F_DEVID 0x8716
113 #define IT8718F_DEVID 0x8718
114 #define IT8726F_DEVID 0x8726
115 #define IT87_ACT_REG 0x30
116 #define IT87_BASE_REG 0x60
118 /* Logical device 7 registers (IT8712F and later) */
119 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
120 #define IT87_SIO_VID_REG 0xfc /* VID value */
122 /* Update battery voltage after every reading if true */
123 static int update_vbat;
125 /* Not all BIOSes properly configure the PWM registers */
126 static int fix_pwm_polarity;
128 /* Many IT87 constants specified below */
130 /* Length of ISA address segment */
131 #define IT87_EXTENT 8
133 /* Length of ISA address segment for Environmental Controller */
134 #define IT87_EC_EXTENT 2
136 /* Offset of EC registers from ISA base address */
137 #define IT87_EC_OFFSET 5
139 /* Where are the ISA address/data registers relative to the EC base address */
140 #define IT87_ADDR_REG_OFFSET 0
141 #define IT87_DATA_REG_OFFSET 1
143 /*----- The IT87 registers -----*/
145 #define IT87_REG_CONFIG 0x00
147 #define IT87_REG_ALARM1 0x01
148 #define IT87_REG_ALARM2 0x02
149 #define IT87_REG_ALARM3 0x03
151 /* The IT8718F has the VID value in a different register, in Super-I/O
152 configuration space. */
153 #define IT87_REG_VID 0x0a
154 /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
155 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
157 #define IT87_REG_FAN_DIV 0x0b
158 #define IT87_REG_FAN_16BIT 0x0c
160 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
162 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
163 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
164 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
165 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
166 #define IT87_REG_FAN_MAIN_CTRL 0x13
167 #define IT87_REG_FAN_CTL 0x14
168 #define IT87_REG_PWM(nr) (0x15 + (nr))
170 #define IT87_REG_VIN(nr) (0x20 + (nr))
171 #define IT87_REG_TEMP(nr) (0x29 + (nr))
173 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
174 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
175 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
176 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
178 #define IT87_REG_VIN_ENABLE 0x50
179 #define IT87_REG_TEMP_ENABLE 0x51
181 #define IT87_REG_CHIPID 0x58
183 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
184 #define IN_FROM_REG(val) ((val) * 16)
186 static inline u8 FAN_TO_REG(long rpm, int div)
190 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
191 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
195 static inline u16 FAN16_TO_REG(long rpm)
199 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
202 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
203 /* The divider is fixed to 2 in 16-bit mode */
204 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
206 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
207 ((val)+500)/1000),-128,127))
208 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
210 #define PWM_TO_REG(val) ((val) >> 1)
211 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
213 static int DIV_TO_REG(int val)
216 while (answer < 7 && (val >>= 1))
220 #define DIV_FROM_REG(val) (1 << (val))
222 static const unsigned int pwm_freq[8] = {
234 struct it87_sio_data {
236 /* Values read from Super-I/O config space */
241 /* For each registered chip, we need to keep some data in memory.
242 The structure is dynamically allocated. */
244 struct device *hwmon_dev;
250 struct mutex update_lock;
251 char valid; /* !=0 if following fields are valid */
252 unsigned long last_updated; /* In jiffies */
254 u8 in[9]; /* Register value */
255 u8 in_max[8]; /* Register value */
256 u8 in_min[8]; /* Register value */
257 u8 has_fan; /* Bitfield, fans enabled */
258 u16 fan[5]; /* Register values, possibly combined */
259 u16 fan_min[5]; /* Register values, possibly combined */
260 u8 temp[3]; /* Register value */
261 u8 temp_high[3]; /* Register value */
262 u8 temp_low[3]; /* Register value */
263 u8 sensor; /* Register value */
264 u8 fan_div[3]; /* Register encoding, shifted right */
265 u8 vid; /* Register encoding, combined */
267 u32 alarms; /* Register encoding, combined */
268 u8 fan_main_ctrl; /* Register value */
269 u8 fan_ctl; /* Register value */
270 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
273 static inline int has_16bit_fans(const struct it87_data *data)
275 /* IT8705F Datasheet 0.4.1, 3h == Version G.
276 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 7h == Version I.
277 These are the first revisions with 16bit tachometer support. */
278 return (data->type == it87 && data->revision >= 0x03)
279 || (data->type == it8712 && data->revision >= 0x07)
280 || data->type == it8716
281 || data->type == it8718;
284 static int it87_probe(struct platform_device *pdev);
285 static int __devexit it87_remove(struct platform_device *pdev);
287 static int it87_read_value(struct it87_data *data, u8 reg);
288 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
289 static struct it87_data *it87_update_device(struct device *dev);
290 static int it87_check_pwm(struct device *dev);
291 static void it87_init_device(struct platform_device *pdev);
294 static struct platform_driver it87_driver = {
296 .owner = THIS_MODULE,
300 .remove = __devexit_p(it87_remove),
303 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
306 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
307 int nr = sensor_attr->index;
309 struct it87_data *data = it87_update_device(dev);
310 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
313 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
316 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
317 int nr = sensor_attr->index;
319 struct it87_data *data = it87_update_device(dev);
320 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
323 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
326 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
327 int nr = sensor_attr->index;
329 struct it87_data *data = it87_update_device(dev);
330 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
333 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
334 const char *buf, size_t count)
336 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
337 int nr = sensor_attr->index;
339 struct it87_data *data = dev_get_drvdata(dev);
340 unsigned long val = simple_strtoul(buf, NULL, 10);
342 mutex_lock(&data->update_lock);
343 data->in_min[nr] = IN_TO_REG(val);
344 it87_write_value(data, IT87_REG_VIN_MIN(nr),
346 mutex_unlock(&data->update_lock);
349 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
350 const char *buf, size_t count)
352 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
353 int nr = sensor_attr->index;
355 struct it87_data *data = dev_get_drvdata(dev);
356 unsigned long val = simple_strtoul(buf, NULL, 10);
358 mutex_lock(&data->update_lock);
359 data->in_max[nr] = IN_TO_REG(val);
360 it87_write_value(data, IT87_REG_VIN_MAX(nr),
362 mutex_unlock(&data->update_lock);
366 #define show_in_offset(offset) \
367 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
368 show_in, NULL, offset);
370 #define limit_in_offset(offset) \
371 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
372 show_in_min, set_in_min, offset); \
373 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
374 show_in_max, set_in_max, offset);
395 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
398 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
399 int nr = sensor_attr->index;
401 struct it87_data *data = it87_update_device(dev);
402 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
404 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
407 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
408 int nr = sensor_attr->index;
410 struct it87_data *data = it87_update_device(dev);
411 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
413 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
416 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
417 int nr = sensor_attr->index;
419 struct it87_data *data = it87_update_device(dev);
420 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
422 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
425 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
426 int nr = sensor_attr->index;
428 struct it87_data *data = dev_get_drvdata(dev);
429 int val = simple_strtol(buf, NULL, 10);
431 mutex_lock(&data->update_lock);
432 data->temp_high[nr] = TEMP_TO_REG(val);
433 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
434 mutex_unlock(&data->update_lock);
437 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
440 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
441 int nr = sensor_attr->index;
443 struct it87_data *data = dev_get_drvdata(dev);
444 int val = simple_strtol(buf, NULL, 10);
446 mutex_lock(&data->update_lock);
447 data->temp_low[nr] = TEMP_TO_REG(val);
448 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
449 mutex_unlock(&data->update_lock);
452 #define show_temp_offset(offset) \
453 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
454 show_temp, NULL, offset - 1); \
455 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
456 show_temp_max, set_temp_max, offset - 1); \
457 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
458 show_temp_min, set_temp_min, offset - 1);
464 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
467 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
468 int nr = sensor_attr->index;
470 struct it87_data *data = it87_update_device(dev);
471 u8 reg = data->sensor; /* In case the value is updated while we use it */
474 return sprintf(buf, "3\n"); /* thermal diode */
476 return sprintf(buf, "2\n"); /* thermistor */
477 return sprintf(buf, "0\n"); /* disabled */
479 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
480 const char *buf, size_t count)
482 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
483 int nr = sensor_attr->index;
485 struct it87_data *data = dev_get_drvdata(dev);
486 int val = simple_strtol(buf, NULL, 10);
488 mutex_lock(&data->update_lock);
490 data->sensor &= ~(1 << nr);
491 data->sensor &= ~(8 << nr);
492 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
494 data->sensor |= 1 << nr;
496 data->sensor |= 8 << nr;
498 mutex_unlock(&data->update_lock);
501 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
502 mutex_unlock(&data->update_lock);
505 #define show_sensor_offset(offset) \
506 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
507 show_sensor, set_sensor, offset - 1);
509 show_sensor_offset(1);
510 show_sensor_offset(2);
511 show_sensor_offset(3);
514 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
517 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
518 int nr = sensor_attr->index;
520 struct it87_data *data = it87_update_device(dev);
521 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
522 DIV_FROM_REG(data->fan_div[nr])));
524 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
527 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
528 int nr = sensor_attr->index;
530 struct it87_data *data = it87_update_device(dev);
531 return sprintf(buf,"%d\n",
532 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
534 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
537 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
538 int nr = sensor_attr->index;
540 struct it87_data *data = it87_update_device(dev);
541 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
543 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
546 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
547 int nr = sensor_attr->index;
549 struct it87_data *data = it87_update_device(dev);
550 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
552 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
555 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
556 int nr = sensor_attr->index;
558 struct it87_data *data = it87_update_device(dev);
559 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
561 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
564 struct it87_data *data = it87_update_device(dev);
565 int index = (data->fan_ctl >> 4) & 0x07;
567 return sprintf(buf, "%u\n", pwm_freq[index]);
569 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
570 const char *buf, size_t count)
572 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
573 int nr = sensor_attr->index;
575 struct it87_data *data = dev_get_drvdata(dev);
576 int val = simple_strtol(buf, NULL, 10);
579 mutex_lock(&data->update_lock);
580 reg = it87_read_value(data, IT87_REG_FAN_DIV);
582 case 0: data->fan_div[nr] = reg & 0x07; break;
583 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
584 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
587 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
588 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
589 mutex_unlock(&data->update_lock);
592 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
593 const char *buf, size_t count)
595 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
596 int nr = sensor_attr->index;
598 struct it87_data *data = dev_get_drvdata(dev);
599 unsigned long val = simple_strtoul(buf, NULL, 10);
603 mutex_lock(&data->update_lock);
604 old = it87_read_value(data, IT87_REG_FAN_DIV);
606 /* Save fan min limit */
607 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
612 data->fan_div[nr] = DIV_TO_REG(val);
616 data->fan_div[nr] = 1;
618 data->fan_div[nr] = 3;
621 val |= (data->fan_div[0] & 0x07);
622 val |= (data->fan_div[1] & 0x07) << 3;
623 if (data->fan_div[2] == 3)
625 it87_write_value(data, IT87_REG_FAN_DIV, val);
627 /* Restore fan min limit */
628 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
629 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
631 mutex_unlock(&data->update_lock);
634 static ssize_t set_pwm_enable(struct device *dev,
635 struct device_attribute *attr, const char *buf, size_t count)
637 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
638 int nr = sensor_attr->index;
640 struct it87_data *data = dev_get_drvdata(dev);
641 int val = simple_strtol(buf, NULL, 10);
643 mutex_lock(&data->update_lock);
647 /* make sure the fan is on when in on/off mode */
648 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
649 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
650 /* set on/off mode */
651 data->fan_main_ctrl &= ~(1 << nr);
652 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
653 } else if (val == 1) {
654 /* set SmartGuardian mode */
655 data->fan_main_ctrl |= (1 << nr);
656 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
657 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
658 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
660 mutex_unlock(&data->update_lock);
664 mutex_unlock(&data->update_lock);
667 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
668 const char *buf, size_t count)
670 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
671 int nr = sensor_attr->index;
673 struct it87_data *data = dev_get_drvdata(dev);
674 int val = simple_strtol(buf, NULL, 10);
676 if (val < 0 || val > 255)
679 mutex_lock(&data->update_lock);
680 data->manual_pwm_ctl[nr] = val;
681 if (data->fan_main_ctrl & (1 << nr))
682 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
683 mutex_unlock(&data->update_lock);
686 static ssize_t set_pwm_freq(struct device *dev,
687 struct device_attribute *attr, const char *buf, size_t count)
689 struct it87_data *data = dev_get_drvdata(dev);
690 unsigned long val = simple_strtoul(buf, NULL, 10);
693 /* Search for the nearest available frequency */
694 for (i = 0; i < 7; i++) {
695 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
699 mutex_lock(&data->update_lock);
700 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
701 data->fan_ctl |= i << 4;
702 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
703 mutex_unlock(&data->update_lock);
708 #define show_fan_offset(offset) \
709 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
710 show_fan, NULL, offset - 1); \
711 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
712 show_fan_min, set_fan_min, offset - 1); \
713 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
714 show_fan_div, set_fan_div, offset - 1);
720 #define show_pwm_offset(offset) \
721 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
722 show_pwm_enable, set_pwm_enable, offset - 1); \
723 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
724 show_pwm, set_pwm, offset - 1); \
725 static DEVICE_ATTR(pwm##offset##_freq, \
726 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
727 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
733 /* A different set of callbacks for 16-bit fans */
734 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
737 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
738 int nr = sensor_attr->index;
739 struct it87_data *data = it87_update_device(dev);
740 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
743 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
746 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
747 int nr = sensor_attr->index;
748 struct it87_data *data = it87_update_device(dev);
749 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
752 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
753 const char *buf, size_t count)
755 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
756 int nr = sensor_attr->index;
757 struct it87_data *data = dev_get_drvdata(dev);
758 int val = simple_strtol(buf, NULL, 10);
760 mutex_lock(&data->update_lock);
761 data->fan_min[nr] = FAN16_TO_REG(val);
762 it87_write_value(data, IT87_REG_FAN_MIN[nr],
763 data->fan_min[nr] & 0xff);
764 it87_write_value(data, IT87_REG_FANX_MIN[nr],
765 data->fan_min[nr] >> 8);
766 mutex_unlock(&data->update_lock);
770 /* We want to use the same sysfs file names as 8-bit fans, but we need
771 different variable names, so we have to use SENSOR_ATTR instead of
772 SENSOR_DEVICE_ATTR. */
773 #define show_fan16_offset(offset) \
774 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
775 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
776 show_fan16, NULL, offset - 1); \
777 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
778 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
779 show_fan16_min, set_fan16_min, offset - 1)
781 show_fan16_offset(1);
782 show_fan16_offset(2);
783 show_fan16_offset(3);
784 show_fan16_offset(4);
785 show_fan16_offset(5);
788 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
790 struct it87_data *data = it87_update_device(dev);
791 return sprintf(buf, "%u\n", data->alarms);
793 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
795 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
798 int bitnr = to_sensor_dev_attr(attr)->index;
799 struct it87_data *data = it87_update_device(dev);
800 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
802 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
803 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
804 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
805 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
806 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
807 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
808 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
809 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
810 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
811 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
812 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
813 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
814 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
815 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
816 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
817 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
820 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
822 struct it87_data *data = dev_get_drvdata(dev);
823 return sprintf(buf, "%u\n", data->vrm);
826 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
828 struct it87_data *data = dev_get_drvdata(dev);
831 val = simple_strtoul(buf, NULL, 10);
836 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
839 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
841 struct it87_data *data = it87_update_device(dev);
842 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
844 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
846 static ssize_t show_name(struct device *dev, struct device_attribute
849 struct it87_data *data = dev_get_drvdata(dev);
850 return sprintf(buf, "%s\n", data->name);
852 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
854 static struct attribute *it87_attributes[] = {
855 &sensor_dev_attr_in0_input.dev_attr.attr,
856 &sensor_dev_attr_in1_input.dev_attr.attr,
857 &sensor_dev_attr_in2_input.dev_attr.attr,
858 &sensor_dev_attr_in3_input.dev_attr.attr,
859 &sensor_dev_attr_in4_input.dev_attr.attr,
860 &sensor_dev_attr_in5_input.dev_attr.attr,
861 &sensor_dev_attr_in6_input.dev_attr.attr,
862 &sensor_dev_attr_in7_input.dev_attr.attr,
863 &sensor_dev_attr_in8_input.dev_attr.attr,
864 &sensor_dev_attr_in0_min.dev_attr.attr,
865 &sensor_dev_attr_in1_min.dev_attr.attr,
866 &sensor_dev_attr_in2_min.dev_attr.attr,
867 &sensor_dev_attr_in3_min.dev_attr.attr,
868 &sensor_dev_attr_in4_min.dev_attr.attr,
869 &sensor_dev_attr_in5_min.dev_attr.attr,
870 &sensor_dev_attr_in6_min.dev_attr.attr,
871 &sensor_dev_attr_in7_min.dev_attr.attr,
872 &sensor_dev_attr_in0_max.dev_attr.attr,
873 &sensor_dev_attr_in1_max.dev_attr.attr,
874 &sensor_dev_attr_in2_max.dev_attr.attr,
875 &sensor_dev_attr_in3_max.dev_attr.attr,
876 &sensor_dev_attr_in4_max.dev_attr.attr,
877 &sensor_dev_attr_in5_max.dev_attr.attr,
878 &sensor_dev_attr_in6_max.dev_attr.attr,
879 &sensor_dev_attr_in7_max.dev_attr.attr,
880 &sensor_dev_attr_in0_alarm.dev_attr.attr,
881 &sensor_dev_attr_in1_alarm.dev_attr.attr,
882 &sensor_dev_attr_in2_alarm.dev_attr.attr,
883 &sensor_dev_attr_in3_alarm.dev_attr.attr,
884 &sensor_dev_attr_in4_alarm.dev_attr.attr,
885 &sensor_dev_attr_in5_alarm.dev_attr.attr,
886 &sensor_dev_attr_in6_alarm.dev_attr.attr,
887 &sensor_dev_attr_in7_alarm.dev_attr.attr,
889 &sensor_dev_attr_temp1_input.dev_attr.attr,
890 &sensor_dev_attr_temp2_input.dev_attr.attr,
891 &sensor_dev_attr_temp3_input.dev_attr.attr,
892 &sensor_dev_attr_temp1_max.dev_attr.attr,
893 &sensor_dev_attr_temp2_max.dev_attr.attr,
894 &sensor_dev_attr_temp3_max.dev_attr.attr,
895 &sensor_dev_attr_temp1_min.dev_attr.attr,
896 &sensor_dev_attr_temp2_min.dev_attr.attr,
897 &sensor_dev_attr_temp3_min.dev_attr.attr,
898 &sensor_dev_attr_temp1_type.dev_attr.attr,
899 &sensor_dev_attr_temp2_type.dev_attr.attr,
900 &sensor_dev_attr_temp3_type.dev_attr.attr,
901 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
902 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
903 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
905 &dev_attr_alarms.attr,
910 static const struct attribute_group it87_group = {
911 .attrs = it87_attributes,
914 static struct attribute *it87_attributes_opt[] = {
915 &sensor_dev_attr_fan1_input16.dev_attr.attr,
916 &sensor_dev_attr_fan1_min16.dev_attr.attr,
917 &sensor_dev_attr_fan2_input16.dev_attr.attr,
918 &sensor_dev_attr_fan2_min16.dev_attr.attr,
919 &sensor_dev_attr_fan3_input16.dev_attr.attr,
920 &sensor_dev_attr_fan3_min16.dev_attr.attr,
921 &sensor_dev_attr_fan4_input16.dev_attr.attr,
922 &sensor_dev_attr_fan4_min16.dev_attr.attr,
923 &sensor_dev_attr_fan5_input16.dev_attr.attr,
924 &sensor_dev_attr_fan5_min16.dev_attr.attr,
926 &sensor_dev_attr_fan1_input.dev_attr.attr,
927 &sensor_dev_attr_fan1_min.dev_attr.attr,
928 &sensor_dev_attr_fan1_div.dev_attr.attr,
929 &sensor_dev_attr_fan2_input.dev_attr.attr,
930 &sensor_dev_attr_fan2_min.dev_attr.attr,
931 &sensor_dev_attr_fan2_div.dev_attr.attr,
932 &sensor_dev_attr_fan3_input.dev_attr.attr,
933 &sensor_dev_attr_fan3_min.dev_attr.attr,
934 &sensor_dev_attr_fan3_div.dev_attr.attr,
936 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
937 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
938 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
939 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
940 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
942 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
943 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
944 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
945 &sensor_dev_attr_pwm1.dev_attr.attr,
946 &sensor_dev_attr_pwm2.dev_attr.attr,
947 &sensor_dev_attr_pwm3.dev_attr.attr,
948 &dev_attr_pwm1_freq.attr,
949 &dev_attr_pwm2_freq.attr,
950 &dev_attr_pwm3_freq.attr,
953 &dev_attr_cpu0_vid.attr,
957 static const struct attribute_group it87_group_opt = {
958 .attrs = it87_attributes_opt,
961 /* SuperIO detection - will change isa_address if a chip is found */
962 static int __init it87_find(unsigned short *address,
963 struct it87_sio_data *sio_data)
969 chip_type = force_id ? force_id : superio_inw(DEVID);
973 sio_data->type = it87;
976 sio_data->type = it8712;
980 sio_data->type = it8716;
983 sio_data->type = it8718;
985 case 0xffff: /* No device at all */
988 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
994 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
995 pr_info("it87: Device not activated, skipping\n");
999 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1000 if (*address == 0) {
1001 pr_info("it87: Base address not set, skipping\n");
1006 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1007 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
1008 chip_type, *address, sio_data->revision);
1010 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1011 if (chip_type != IT8705F_DEVID) {
1014 superio_select(GPIO);
1015 if (chip_type == it8718)
1016 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1018 reg = superio_inb(IT87_SIO_PINX2_REG);
1020 pr_info("it87: in3 is VCC (+5V)\n");
1022 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1030 static int __devinit it87_probe(struct platform_device *pdev)
1032 struct it87_data *data;
1033 struct resource *res;
1034 struct device *dev = &pdev->dev;
1035 struct it87_sio_data *sio_data = dev->platform_data;
1037 int enable_pwm_interface;
1038 static const char *names[] = {
1045 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1046 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1047 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1048 (unsigned long)res->start,
1049 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1054 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
1059 data->addr = res->start;
1060 data->type = sio_data->type;
1061 data->revision = sio_data->revision;
1062 data->name = names[sio_data->type];
1064 /* Now, we do the remaining detection. */
1065 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1066 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1071 platform_set_drvdata(pdev, data);
1073 mutex_init(&data->update_lock);
1075 /* Check PWM configuration */
1076 enable_pwm_interface = it87_check_pwm(dev);
1078 /* Initialize the IT87 chip */
1079 it87_init_device(pdev);
1081 /* Register sysfs hooks */
1082 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1085 /* Do not create fan files for disabled fans */
1086 if (has_16bit_fans(data)) {
1087 /* 16-bit tachometers */
1088 if (data->has_fan & (1 << 0)) {
1089 if ((err = device_create_file(dev,
1090 &sensor_dev_attr_fan1_input16.dev_attr))
1091 || (err = device_create_file(dev,
1092 &sensor_dev_attr_fan1_min16.dev_attr))
1093 || (err = device_create_file(dev,
1094 &sensor_dev_attr_fan1_alarm.dev_attr)))
1097 if (data->has_fan & (1 << 1)) {
1098 if ((err = device_create_file(dev,
1099 &sensor_dev_attr_fan2_input16.dev_attr))
1100 || (err = device_create_file(dev,
1101 &sensor_dev_attr_fan2_min16.dev_attr))
1102 || (err = device_create_file(dev,
1103 &sensor_dev_attr_fan2_alarm.dev_attr)))
1106 if (data->has_fan & (1 << 2)) {
1107 if ((err = device_create_file(dev,
1108 &sensor_dev_attr_fan3_input16.dev_attr))
1109 || (err = device_create_file(dev,
1110 &sensor_dev_attr_fan3_min16.dev_attr))
1111 || (err = device_create_file(dev,
1112 &sensor_dev_attr_fan3_alarm.dev_attr)))
1115 if (data->has_fan & (1 << 3)) {
1116 if ((err = device_create_file(dev,
1117 &sensor_dev_attr_fan4_input16.dev_attr))
1118 || (err = device_create_file(dev,
1119 &sensor_dev_attr_fan4_min16.dev_attr))
1120 || (err = device_create_file(dev,
1121 &sensor_dev_attr_fan4_alarm.dev_attr)))
1124 if (data->has_fan & (1 << 4)) {
1125 if ((err = device_create_file(dev,
1126 &sensor_dev_attr_fan5_input16.dev_attr))
1127 || (err = device_create_file(dev,
1128 &sensor_dev_attr_fan5_min16.dev_attr))
1129 || (err = device_create_file(dev,
1130 &sensor_dev_attr_fan5_alarm.dev_attr)))
1134 /* 8-bit tachometers with clock divider */
1135 if (data->has_fan & (1 << 0)) {
1136 if ((err = device_create_file(dev,
1137 &sensor_dev_attr_fan1_input.dev_attr))
1138 || (err = device_create_file(dev,
1139 &sensor_dev_attr_fan1_min.dev_attr))
1140 || (err = device_create_file(dev,
1141 &sensor_dev_attr_fan1_div.dev_attr))
1142 || (err = device_create_file(dev,
1143 &sensor_dev_attr_fan1_alarm.dev_attr)))
1146 if (data->has_fan & (1 << 1)) {
1147 if ((err = device_create_file(dev,
1148 &sensor_dev_attr_fan2_input.dev_attr))
1149 || (err = device_create_file(dev,
1150 &sensor_dev_attr_fan2_min.dev_attr))
1151 || (err = device_create_file(dev,
1152 &sensor_dev_attr_fan2_div.dev_attr))
1153 || (err = device_create_file(dev,
1154 &sensor_dev_attr_fan2_alarm.dev_attr)))
1157 if (data->has_fan & (1 << 2)) {
1158 if ((err = device_create_file(dev,
1159 &sensor_dev_attr_fan3_input.dev_attr))
1160 || (err = device_create_file(dev,
1161 &sensor_dev_attr_fan3_min.dev_attr))
1162 || (err = device_create_file(dev,
1163 &sensor_dev_attr_fan3_div.dev_attr))
1164 || (err = device_create_file(dev,
1165 &sensor_dev_attr_fan3_alarm.dev_attr)))
1170 if (enable_pwm_interface) {
1171 if ((err = device_create_file(dev,
1172 &sensor_dev_attr_pwm1_enable.dev_attr))
1173 || (err = device_create_file(dev,
1174 &sensor_dev_attr_pwm2_enable.dev_attr))
1175 || (err = device_create_file(dev,
1176 &sensor_dev_attr_pwm3_enable.dev_attr))
1177 || (err = device_create_file(dev,
1178 &sensor_dev_attr_pwm1.dev_attr))
1179 || (err = device_create_file(dev,
1180 &sensor_dev_attr_pwm2.dev_attr))
1181 || (err = device_create_file(dev,
1182 &sensor_dev_attr_pwm3.dev_attr))
1183 || (err = device_create_file(dev,
1184 &dev_attr_pwm1_freq))
1185 || (err = device_create_file(dev,
1186 &dev_attr_pwm2_freq))
1187 || (err = device_create_file(dev,
1188 &dev_attr_pwm3_freq)))
1192 if (data->type == it8712 || data->type == it8716
1193 || data->type == it8718) {
1194 data->vrm = vid_which_vrm();
1195 /* VID reading from Super-I/O config space if available */
1196 data->vid = sio_data->vid_value;
1197 if ((err = device_create_file(dev,
1199 || (err = device_create_file(dev,
1200 &dev_attr_cpu0_vid)))
1204 data->hwmon_dev = hwmon_device_register(dev);
1205 if (IS_ERR(data->hwmon_dev)) {
1206 err = PTR_ERR(data->hwmon_dev);
1213 sysfs_remove_group(&dev->kobj, &it87_group);
1214 sysfs_remove_group(&dev->kobj, &it87_group_opt);
1216 platform_set_drvdata(pdev, NULL);
1219 release_region(res->start, IT87_EC_EXTENT);
1224 static int __devexit it87_remove(struct platform_device *pdev)
1226 struct it87_data *data = platform_get_drvdata(pdev);
1228 hwmon_device_unregister(data->hwmon_dev);
1229 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1230 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
1232 release_region(data->addr, IT87_EC_EXTENT);
1233 platform_set_drvdata(pdev, NULL);
1239 /* Must be called with data->update_lock held, except during initialization.
1240 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1241 would slow down the IT87 access and should not be necessary. */
1242 static int it87_read_value(struct it87_data *data, u8 reg)
1244 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1245 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
1248 /* Must be called with data->update_lock held, except during initialization.
1249 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1250 would slow down the IT87 access and should not be necessary. */
1251 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
1253 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1254 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
1257 /* Return 1 if and only if the PWM interface is safe to use */
1258 static int __devinit it87_check_pwm(struct device *dev)
1260 struct it87_data *data = dev_get_drvdata(dev);
1261 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1262 * and polarity set to active low is sign that this is the case so we
1263 * disable pwm control to protect the user. */
1264 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1265 if ((tmp & 0x87) == 0) {
1266 if (fix_pwm_polarity) {
1267 /* The user asks us to attempt a chip reconfiguration.
1268 * This means switching to active high polarity and
1269 * inverting all fan speed values. */
1273 for (i = 0; i < 3; i++)
1274 pwm[i] = it87_read_value(data,
1277 /* If any fan is in automatic pwm mode, the polarity
1278 * might be correct, as suspicious as it seems, so we
1279 * better don't change anything (but still disable the
1280 * PWM interface). */
1281 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1282 dev_info(dev, "Reconfiguring PWM to "
1283 "active high polarity\n");
1284 it87_write_value(data, IT87_REG_FAN_CTL,
1286 for (i = 0; i < 3; i++)
1287 it87_write_value(data,
1293 dev_info(dev, "PWM configuration is "
1294 "too broken to be fixed\n");
1297 dev_info(dev, "Detected broken BIOS "
1298 "defaults, disabling PWM interface\n");
1300 } else if (fix_pwm_polarity) {
1301 dev_info(dev, "PWM configuration looks "
1302 "sane, won't touch\n");
1308 /* Called when we have found a new IT87. */
1309 static void __devinit it87_init_device(struct platform_device *pdev)
1311 struct it87_data *data = platform_get_drvdata(pdev);
1314 /* initialize to sane defaults:
1315 * - if the chip is in manual pwm mode, this will be overwritten with
1316 * the actual settings on the chip (so in this case, initialization
1318 * - if in automatic or on/off mode, we could switch to manual mode,
1319 * read the registers and set manual_pwm_ctl accordingly, but currently
1320 * this is not implemented, so we initialize to something sane */
1321 for (i = 0; i < 3; i++) {
1322 data->manual_pwm_ctl[i] = 0xff;
1325 /* Some chips seem to have default value 0xff for all limit
1326 * registers. For low voltage limits it makes no sense and triggers
1327 * alarms, so change to 0 instead. For high temperature limits, it
1328 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1329 * but is still confusing, so change to 127 degrees C. */
1330 for (i = 0; i < 8; i++) {
1331 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
1333 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
1335 for (i = 0; i < 3; i++) {
1336 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1338 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
1341 /* Check if temperature channnels are reset manually or by some reason */
1342 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1343 if ((tmp & 0x3f) == 0) {
1344 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1345 tmp = (tmp & 0xc0) | 0x2a;
1346 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
1350 /* Check if voltage monitors are reset manually or by some reason */
1351 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
1352 if ((tmp & 0xff) == 0) {
1353 /* Enable all voltage monitors */
1354 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
1357 /* Check if tachometers are reset manually or by some reason */
1358 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
1359 if ((data->fan_main_ctrl & 0x70) == 0) {
1360 /* Enable all fan tachometers */
1361 data->fan_main_ctrl |= 0x70;
1362 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1364 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1366 /* Set tachometers to 16-bit mode if needed */
1367 if (has_16bit_fans(data)) {
1368 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
1369 if (~tmp & 0x07 & data->has_fan) {
1371 "Setting fan1-3 to 16-bit mode\n");
1372 it87_write_value(data, IT87_REG_FAN_16BIT,
1375 /* IT8705F only supports three fans. */
1376 if (data->type != it87) {
1378 data->has_fan |= (1 << 3); /* fan4 enabled */
1380 data->has_fan |= (1 << 4); /* fan5 enabled */
1384 /* Set current fan mode registers and the default settings for the
1385 * other mode registers */
1386 for (i = 0; i < 3; i++) {
1387 if (data->fan_main_ctrl & (1 << i)) {
1389 tmp = it87_read_value(data, IT87_REG_PWM(i));
1391 /* automatic pwm - not yet implemented, but
1392 * leave the settings made by the BIOS alone
1393 * until a change is requested via the sysfs
1397 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1402 /* Start monitoring */
1403 it87_write_value(data, IT87_REG_CONFIG,
1404 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1405 | (update_vbat ? 0x41 : 0x01));
1408 static struct it87_data *it87_update_device(struct device *dev)
1410 struct it87_data *data = dev_get_drvdata(dev);
1413 mutex_lock(&data->update_lock);
1415 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1419 /* Cleared after each update, so reenable. Value
1420 returned by this read will be previous value */
1421 it87_write_value(data, IT87_REG_CONFIG,
1422 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1424 for (i = 0; i <= 7; i++) {
1426 it87_read_value(data, IT87_REG_VIN(i));
1428 it87_read_value(data, IT87_REG_VIN_MIN(i));
1430 it87_read_value(data, IT87_REG_VIN_MAX(i));
1432 /* in8 (battery) has no limit registers */
1434 it87_read_value(data, IT87_REG_VIN(8));
1436 for (i = 0; i < 5; i++) {
1437 /* Skip disabled fans */
1438 if (!(data->has_fan & (1 << i)))
1442 it87_read_value(data, IT87_REG_FAN_MIN[i]);
1443 data->fan[i] = it87_read_value(data,
1445 /* Add high byte if in 16-bit mode */
1446 if (has_16bit_fans(data)) {
1447 data->fan[i] |= it87_read_value(data,
1448 IT87_REG_FANX[i]) << 8;
1449 data->fan_min[i] |= it87_read_value(data,
1450 IT87_REG_FANX_MIN[i]) << 8;
1453 for (i = 0; i < 3; i++) {
1455 it87_read_value(data, IT87_REG_TEMP(i));
1456 data->temp_high[i] =
1457 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1459 it87_read_value(data, IT87_REG_TEMP_LOW(i));
1462 /* Newer chips don't have clock dividers */
1463 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1464 i = it87_read_value(data, IT87_REG_FAN_DIV);
1465 data->fan_div[0] = i & 0x07;
1466 data->fan_div[1] = (i >> 3) & 0x07;
1467 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1471 it87_read_value(data, IT87_REG_ALARM1) |
1472 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1473 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1474 data->fan_main_ctrl = it87_read_value(data,
1475 IT87_REG_FAN_MAIN_CTRL);
1476 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
1478 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1479 /* The 8705 does not have VID capability.
1480 The 8718 does not use IT87_REG_VID for the same purpose. */
1481 if (data->type == it8712 || data->type == it8716) {
1482 data->vid = it87_read_value(data, IT87_REG_VID);
1483 /* The older IT8712F revisions had only 5 VID pins,
1484 but we assume it is always safe to read 6 bits. */
1487 data->last_updated = jiffies;
1491 mutex_unlock(&data->update_lock);
1496 static int __init it87_device_add(unsigned short address,
1497 const struct it87_sio_data *sio_data)
1499 struct resource res = {
1500 .start = address + IT87_EC_OFFSET,
1501 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
1503 .flags = IORESOURCE_IO,
1507 pdev = platform_device_alloc(DRVNAME, address);
1510 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1514 err = platform_device_add_resources(pdev, &res, 1);
1516 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1518 goto exit_device_put;
1521 err = platform_device_add_data(pdev, sio_data,
1522 sizeof(struct it87_sio_data));
1524 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1525 goto exit_device_put;
1528 err = platform_device_add(pdev);
1530 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1532 goto exit_device_put;
1538 platform_device_put(pdev);
1543 static int __init sm_it87_init(void)
1546 unsigned short isa_address=0;
1547 struct it87_sio_data sio_data;
1549 err = it87_find(&isa_address, &sio_data);
1552 err = platform_driver_register(&it87_driver);
1556 err = it87_device_add(isa_address, &sio_data);
1558 platform_driver_unregister(&it87_driver);
1565 static void __exit sm_it87_exit(void)
1567 platform_device_unregister(pdev);
1568 platform_driver_unregister(&it87_driver);
1572 MODULE_AUTHOR("Chris Gauthron, "
1573 "Jean Delvare <khali@linux-fr.org>");
1574 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver");
1575 module_param(update_vbat, bool, 0);
1576 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1577 module_param(fix_pwm_polarity, bool, 0);
1578 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1579 MODULE_LICENSE("GPL");
1581 module_init(sm_it87_init);
1582 module_exit(sm_it87_exit);