2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 Supports: IT8705F Super I/O chip w/LPC interface
6 IT8712F Super I/O chip w/LPC interface & SMBus
7 IT8716F Super I/O chip w/LPC interface
8 Sis950 A clone of the IT8705F
10 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
11 Largely inspired by lm78.c of the same package
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 djg@pdp8.net David Gesswein 7/18/01
30 Modified to fix bug with not all alarms enabled.
31 Added ability to read battery voltage and select temperature sensor
32 type at module load time.
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/jiffies.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-isa.h>
41 #include <linux/hwmon.h>
42 #include <linux/hwmon-sysfs.h>
43 #include <linux/hwmon-vid.h>
44 #include <linux/err.h>
45 #include <linux/mutex.h>
49 /* Addresses to scan */
50 static unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
51 static unsigned short isa_address;
53 /* Insmod parameters */
54 I2C_CLIENT_INSMOD_3(it87, it8712, it8716);
56 #define REG 0x2e /* The register to read/write */
57 #define DEV 0x07 /* Register: Logical device select */
58 #define VAL 0x2f /* The value to read/write */
59 #define PME 0x04 /* The device with the fan registers in it */
60 #define DEVID 0x20 /* Register: Device ID */
61 #define DEVREV 0x22 /* Register: Device Revision */
70 static int superio_inw(int reg)
103 #define IT8712F_DEVID 0x8712
104 #define IT8705F_DEVID 0x8705
105 #define IT8716F_DEVID 0x8716
106 #define IT87_ACT_REG 0x30
107 #define IT87_BASE_REG 0x60
109 /* Update battery voltage after every reading if true */
110 static int update_vbat;
112 /* Not all BIOSes properly configure the PWM registers */
113 static int fix_pwm_polarity;
117 static u16 chip_type;
119 /* Many IT87 constants specified below */
121 /* Length of ISA address segment */
122 #define IT87_EXTENT 8
124 /* Where are the ISA address/data registers relative to the base address */
125 #define IT87_ADDR_REG_OFFSET 5
126 #define IT87_DATA_REG_OFFSET 6
128 /*----- The IT87 registers -----*/
130 #define IT87_REG_CONFIG 0x00
132 #define IT87_REG_ALARM1 0x01
133 #define IT87_REG_ALARM2 0x02
134 #define IT87_REG_ALARM3 0x03
136 #define IT87_REG_VID 0x0a
137 /* Warning: register 0x0b is used for something completely different in
138 new chips/revisions. I suspect only 16-bit tachometer mode will work
140 #define IT87_REG_FAN_DIV 0x0b
141 #define IT87_REG_FAN_16BIT 0x0c
143 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
145 #define IT87_REG_FAN(nr) (0x0d + (nr))
146 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
147 #define IT87_REG_FANX(nr) (0x18 + (nr))
148 #define IT87_REG_FANX_MIN(nr) (0x1b + (nr))
149 #define IT87_REG_FAN_MAIN_CTRL 0x13
150 #define IT87_REG_FAN_CTL 0x14
151 #define IT87_REG_PWM(nr) (0x15 + (nr))
153 #define IT87_REG_VIN(nr) (0x20 + (nr))
154 #define IT87_REG_TEMP(nr) (0x29 + (nr))
156 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
157 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
158 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
159 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
161 #define IT87_REG_I2C_ADDR 0x48
163 #define IT87_REG_VIN_ENABLE 0x50
164 #define IT87_REG_TEMP_ENABLE 0x51
166 #define IT87_REG_CHIPID 0x58
168 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
169 #define IN_FROM_REG(val) ((val) * 16)
171 static inline u8 FAN_TO_REG(long rpm, int div)
175 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
176 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
180 static inline u16 FAN16_TO_REG(long rpm)
184 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
187 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
188 /* The divider is fixed to 2 in 16-bit mode */
189 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
191 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
192 ((val)+500)/1000),-128,127))
193 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
195 #define PWM_TO_REG(val) ((val) >> 1)
196 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
198 static int DIV_TO_REG(int val)
201 while (answer < 7 && (val >>= 1))
205 #define DIV_FROM_REG(val) (1 << (val))
208 /* For each registered IT87, we need to keep some data in memory. That
209 data is pointed to by it87_list[NR]->data. The structure itself is
210 dynamically allocated, at the same time when a new it87 client is
213 struct i2c_client client;
214 struct class_device *class_dev;
218 struct mutex update_lock;
219 char valid; /* !=0 if following fields are valid */
220 unsigned long last_updated; /* In jiffies */
222 u8 in[9]; /* Register value */
223 u8 in_max[9]; /* Register value */
224 u8 in_min[9]; /* Register value */
225 u8 has_fan; /* Bitfield, fans enabled */
226 u16 fan[3]; /* Register values, possibly combined */
227 u16 fan_min[3]; /* Register values, possibly combined */
228 u8 temp[3]; /* Register value */
229 u8 temp_high[3]; /* Register value */
230 u8 temp_low[3]; /* Register value */
231 u8 sensor; /* Register value */
232 u8 fan_div[3]; /* Register encoding, shifted right */
233 u8 vid; /* Register encoding, combined */
235 u32 alarms; /* Register encoding, combined */
236 u8 fan_main_ctrl; /* Register value */
237 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
241 static int it87_attach_adapter(struct i2c_adapter *adapter);
242 static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
243 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
244 static int it87_detach_client(struct i2c_client *client);
246 static int it87_read_value(struct i2c_client *client, u8 reg);
247 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
248 static struct it87_data *it87_update_device(struct device *dev);
249 static int it87_check_pwm(struct i2c_client *client);
250 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
253 static struct i2c_driver it87_driver = {
257 .id = I2C_DRIVERID_IT87,
258 .attach_adapter = it87_attach_adapter,
259 .detach_client = it87_detach_client,
262 static struct i2c_driver it87_isa_driver = {
264 .owner = THIS_MODULE,
267 .attach_adapter = it87_isa_attach_adapter,
268 .detach_client = it87_detach_client,
272 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
275 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
276 int nr = sensor_attr->index;
278 struct it87_data *data = it87_update_device(dev);
279 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
282 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
285 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
286 int nr = sensor_attr->index;
288 struct it87_data *data = it87_update_device(dev);
289 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
292 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
295 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
296 int nr = sensor_attr->index;
298 struct it87_data *data = it87_update_device(dev);
299 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
302 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
303 const char *buf, size_t count)
305 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
306 int nr = sensor_attr->index;
308 struct i2c_client *client = to_i2c_client(dev);
309 struct it87_data *data = i2c_get_clientdata(client);
310 unsigned long val = simple_strtoul(buf, NULL, 10);
312 mutex_lock(&data->update_lock);
313 data->in_min[nr] = IN_TO_REG(val);
314 it87_write_value(client, IT87_REG_VIN_MIN(nr),
316 mutex_unlock(&data->update_lock);
319 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
320 const char *buf, size_t count)
322 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
323 int nr = sensor_attr->index;
325 struct i2c_client *client = to_i2c_client(dev);
326 struct it87_data *data = i2c_get_clientdata(client);
327 unsigned long val = simple_strtoul(buf, NULL, 10);
329 mutex_lock(&data->update_lock);
330 data->in_max[nr] = IN_TO_REG(val);
331 it87_write_value(client, IT87_REG_VIN_MAX(nr),
333 mutex_unlock(&data->update_lock);
337 #define show_in_offset(offset) \
338 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
339 show_in, NULL, offset);
341 #define limit_in_offset(offset) \
342 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
343 show_in_min, set_in_min, offset); \
344 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
345 show_in_max, set_in_max, offset);
366 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
369 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
370 int nr = sensor_attr->index;
372 struct it87_data *data = it87_update_device(dev);
373 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
375 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
378 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
379 int nr = sensor_attr->index;
381 struct it87_data *data = it87_update_device(dev);
382 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
384 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
387 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
388 int nr = sensor_attr->index;
390 struct it87_data *data = it87_update_device(dev);
391 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
393 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
396 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
397 int nr = sensor_attr->index;
399 struct i2c_client *client = to_i2c_client(dev);
400 struct it87_data *data = i2c_get_clientdata(client);
401 int val = simple_strtol(buf, NULL, 10);
403 mutex_lock(&data->update_lock);
404 data->temp_high[nr] = TEMP_TO_REG(val);
405 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
406 mutex_unlock(&data->update_lock);
409 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
410 const char *buf, size_t count)
412 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
413 int nr = sensor_attr->index;
415 struct i2c_client *client = to_i2c_client(dev);
416 struct it87_data *data = i2c_get_clientdata(client);
417 int val = simple_strtol(buf, NULL, 10);
419 mutex_lock(&data->update_lock);
420 data->temp_low[nr] = TEMP_TO_REG(val);
421 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
422 mutex_unlock(&data->update_lock);
425 #define show_temp_offset(offset) \
426 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
427 show_temp, NULL, offset - 1); \
428 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
429 show_temp_max, set_temp_max, offset - 1); \
430 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
431 show_temp_min, set_temp_min, offset - 1);
437 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
440 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
441 int nr = sensor_attr->index;
443 struct it87_data *data = it87_update_device(dev);
444 u8 reg = data->sensor; /* In case the value is updated while we use it */
447 return sprintf(buf, "3\n"); /* thermal diode */
449 return sprintf(buf, "2\n"); /* thermistor */
450 return sprintf(buf, "0\n"); /* disabled */
452 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
453 const char *buf, size_t count)
455 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
456 int nr = sensor_attr->index;
458 struct i2c_client *client = to_i2c_client(dev);
459 struct it87_data *data = i2c_get_clientdata(client);
460 int val = simple_strtol(buf, NULL, 10);
462 mutex_lock(&data->update_lock);
464 data->sensor &= ~(1 << nr);
465 data->sensor &= ~(8 << nr);
466 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
468 data->sensor |= 1 << nr;
470 data->sensor |= 8 << nr;
472 mutex_unlock(&data->update_lock);
475 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
476 mutex_unlock(&data->update_lock);
479 #define show_sensor_offset(offset) \
480 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
481 show_sensor, set_sensor, offset - 1);
483 show_sensor_offset(1);
484 show_sensor_offset(2);
485 show_sensor_offset(3);
488 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
491 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
492 int nr = sensor_attr->index;
494 struct it87_data *data = it87_update_device(dev);
495 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
496 DIV_FROM_REG(data->fan_div[nr])));
498 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
504 struct it87_data *data = it87_update_device(dev);
505 return sprintf(buf,"%d\n",
506 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
508 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
511 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
512 int nr = sensor_attr->index;
514 struct it87_data *data = it87_update_device(dev);
515 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
517 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
520 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
521 int nr = sensor_attr->index;
523 struct it87_data *data = it87_update_device(dev);
524 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
526 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
529 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
530 int nr = sensor_attr->index;
532 struct it87_data *data = it87_update_device(dev);
533 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
535 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
536 const char *buf, size_t count)
538 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
539 int nr = sensor_attr->index;
541 struct i2c_client *client = to_i2c_client(dev);
542 struct it87_data *data = i2c_get_clientdata(client);
543 int val = simple_strtol(buf, NULL, 10);
544 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
546 mutex_lock(&data->update_lock);
548 case 0: data->fan_div[nr] = reg & 0x07; break;
549 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
550 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
553 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
554 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
555 mutex_unlock(&data->update_lock);
558 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
559 const char *buf, size_t count)
561 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
562 int nr = sensor_attr->index;
564 struct i2c_client *client = to_i2c_client(dev);
565 struct it87_data *data = i2c_get_clientdata(client);
566 unsigned long val = simple_strtoul(buf, NULL, 10);
570 mutex_lock(&data->update_lock);
571 old = it87_read_value(client, IT87_REG_FAN_DIV);
573 for (i = 0; i < 3; i++)
574 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
579 data->fan_div[nr] = DIV_TO_REG(val);
583 data->fan_div[nr] = 1;
585 data->fan_div[nr] = 3;
588 val |= (data->fan_div[0] & 0x07);
589 val |= (data->fan_div[1] & 0x07) << 3;
590 if (data->fan_div[2] == 3)
592 it87_write_value(client, IT87_REG_FAN_DIV, val);
594 for (i = 0; i < 3; i++) {
595 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
596 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
598 mutex_unlock(&data->update_lock);
601 static ssize_t set_pwm_enable(struct device *dev,
602 struct device_attribute *attr, const char *buf, size_t count)
604 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
605 int nr = sensor_attr->index;
607 struct i2c_client *client = to_i2c_client(dev);
608 struct it87_data *data = i2c_get_clientdata(client);
609 int val = simple_strtol(buf, NULL, 10);
611 mutex_lock(&data->update_lock);
615 /* make sure the fan is on when in on/off mode */
616 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
617 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
618 /* set on/off mode */
619 data->fan_main_ctrl &= ~(1 << nr);
620 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
621 } else if (val == 1) {
622 /* set SmartGuardian mode */
623 data->fan_main_ctrl |= (1 << nr);
624 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
625 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
626 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
628 mutex_unlock(&data->update_lock);
632 mutex_unlock(&data->update_lock);
635 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
636 const char *buf, size_t count)
638 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
639 int nr = sensor_attr->index;
641 struct i2c_client *client = to_i2c_client(dev);
642 struct it87_data *data = i2c_get_clientdata(client);
643 int val = simple_strtol(buf, NULL, 10);
645 if (val < 0 || val > 255)
648 mutex_lock(&data->update_lock);
649 data->manual_pwm_ctl[nr] = val;
650 if (data->fan_main_ctrl & (1 << nr))
651 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
652 mutex_unlock(&data->update_lock);
656 #define show_fan_offset(offset) \
657 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
658 show_fan, NULL, offset - 1); \
659 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
660 show_fan_min, set_fan_min, offset - 1); \
661 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
662 show_fan_div, set_fan_div, offset - 1);
668 #define show_pwm_offset(offset) \
669 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
670 show_pwm_enable, set_pwm_enable, offset - 1); \
671 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
672 show_pwm, set_pwm, offset - 1);
678 /* A different set of callbacks for 16-bit fans */
679 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
682 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
683 int nr = sensor_attr->index;
684 struct it87_data *data = it87_update_device(dev);
685 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
688 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
691 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
692 int nr = sensor_attr->index;
693 struct it87_data *data = it87_update_device(dev);
694 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
697 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
698 const char *buf, size_t count)
700 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
701 int nr = sensor_attr->index;
702 struct i2c_client *client = to_i2c_client(dev);
703 struct it87_data *data = i2c_get_clientdata(client);
704 int val = simple_strtol(buf, NULL, 10);
706 mutex_lock(&data->update_lock);
707 data->fan_min[nr] = FAN16_TO_REG(val);
708 it87_write_value(client, IT87_REG_FAN_MIN(nr),
709 data->fan_min[nr] & 0xff);
710 it87_write_value(client, IT87_REG_FANX_MIN(nr),
711 data->fan_min[nr] >> 8);
712 mutex_unlock(&data->update_lock);
716 /* We want to use the same sysfs file names as 8-bit fans, but we need
717 different variable names, so we have to use SENSOR_ATTR instead of
718 SENSOR_DEVICE_ATTR. */
719 #define show_fan16_offset(offset) \
720 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
721 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
722 show_fan16, NULL, offset - 1); \
723 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
724 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
725 show_fan16_min, set_fan16_min, offset - 1)
727 show_fan16_offset(1);
728 show_fan16_offset(2);
729 show_fan16_offset(3);
732 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
734 struct it87_data *data = it87_update_device(dev);
735 return sprintf(buf, "%u\n", data->alarms);
737 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
740 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
742 struct it87_data *data = it87_update_device(dev);
743 return sprintf(buf, "%u\n", data->vrm);
746 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
748 struct i2c_client *client = to_i2c_client(dev);
749 struct it87_data *data = i2c_get_clientdata(client);
752 val = simple_strtoul(buf, NULL, 10);
757 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
758 #define device_create_file_vrm(client) \
759 device_create_file(&client->dev, &dev_attr_vrm)
762 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
764 struct it87_data *data = it87_update_device(dev);
765 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
767 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
768 #define device_create_file_vid(client) \
769 device_create_file(&client->dev, &dev_attr_cpu0_vid)
771 /* This function is called when:
772 * it87_driver is inserted (when this module is loaded), for each
774 * when a new adapter is inserted (and it87_driver is still present) */
775 static int it87_attach_adapter(struct i2c_adapter *adapter)
777 if (!(adapter->class & I2C_CLASS_HWMON))
779 return i2c_probe(adapter, &addr_data, it87_detect);
782 static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
784 return it87_detect(adapter, isa_address, -1);
787 /* SuperIO detection - will change isa_address if a chip is found */
788 static int __init it87_find(unsigned short *address)
793 chip_type = superio_inw(DEVID);
794 if (chip_type != IT8712F_DEVID
795 && chip_type != IT8716F_DEVID
796 && chip_type != IT8705F_DEVID)
800 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
801 pr_info("it87: Device not activated, skipping\n");
805 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
807 pr_info("it87: Base address not set, skipping\n");
812 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
813 chip_type, *address, superio_inb(DEVREV) & 0x0f);
820 /* This function is called by i2c_probe */
821 static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
824 struct i2c_client *new_client;
825 struct it87_data *data;
827 const char *name = "";
828 int is_isa = i2c_is_isa_adapter(adapter);
829 int enable_pwm_interface;
832 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
835 /* Reserve the ISA region */
837 if (!request_region(address, IT87_EXTENT,
838 it87_isa_driver.driver.name))
841 /* For now, we presume we have a valid client. We create the
842 client structure, even though we cannot fill it completely yet.
843 But it allows us to access it87_{read,write}_value. */
845 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
850 new_client = &data->client;
852 mutex_init(&data->lock);
853 i2c_set_clientdata(new_client, data);
854 new_client->addr = address;
855 new_client->adapter = adapter;
856 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
857 new_client->flags = 0;
859 /* Now, we do the remaining detection. */
862 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
864 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
870 /* Determine the chip type. */
872 i = it87_read_value(new_client, IT87_REG_CHIPID);
888 dev_info(&adapter->dev,
889 "Ignoring 'force' parameter for unknown chip at "
890 "adapter %d, address 0x%02x\n",
891 i2c_adapter_id(adapter), address);
899 } else if (kind == it8712) {
901 } else if (kind == it8716) {
905 /* Fill in the remaining client fields and put it into the global list */
906 strlcpy(new_client->name, name, I2C_NAME_SIZE);
909 mutex_init(&data->update_lock);
911 /* Tell the I2C layer a new client has arrived */
912 if ((err = i2c_attach_client(new_client)))
916 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
917 "hardware monitoring chips is deprecated. Please "
918 "report if you still rely on it.\n");
920 /* Check PWM configuration */
921 enable_pwm_interface = it87_check_pwm(new_client);
923 /* Initialize the IT87 chip */
924 it87_init_client(new_client, data);
926 /* Register sysfs hooks */
927 data->class_dev = hwmon_device_register(&new_client->dev);
928 if (IS_ERR(data->class_dev)) {
929 err = PTR_ERR(data->class_dev);
933 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
934 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
935 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
936 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
937 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
938 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
939 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
940 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
941 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
942 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
943 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
944 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
945 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
946 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
947 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
948 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
949 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
950 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
951 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
952 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
953 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
954 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
955 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
956 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
957 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
958 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
959 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
960 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
961 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
962 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
963 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
964 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
965 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
966 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
967 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
968 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
969 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
971 /* Do not create fan files for disabled fans */
972 if (data->type == it8716) { /* 16-bit tachometers */
973 if (data->has_fan & (1 << 0)) {
974 device_create_file(&new_client->dev,
975 &sensor_dev_attr_fan1_input16.dev_attr);
976 device_create_file(&new_client->dev,
977 &sensor_dev_attr_fan1_min16.dev_attr);
979 if (data->has_fan & (1 << 1)) {
980 device_create_file(&new_client->dev,
981 &sensor_dev_attr_fan2_input16.dev_attr);
982 device_create_file(&new_client->dev,
983 &sensor_dev_attr_fan2_min16.dev_attr);
985 if (data->has_fan & (1 << 2)) {
986 device_create_file(&new_client->dev,
987 &sensor_dev_attr_fan3_input16.dev_attr);
988 device_create_file(&new_client->dev,
989 &sensor_dev_attr_fan3_min16.dev_attr);
992 if (data->has_fan & (1 << 0)) {
993 device_create_file(&new_client->dev,
994 &sensor_dev_attr_fan1_input.dev_attr);
995 device_create_file(&new_client->dev,
996 &sensor_dev_attr_fan1_min.dev_attr);
997 device_create_file(&new_client->dev,
998 &sensor_dev_attr_fan1_div.dev_attr);
1000 if (data->has_fan & (1 << 1)) {
1001 device_create_file(&new_client->dev,
1002 &sensor_dev_attr_fan2_input.dev_attr);
1003 device_create_file(&new_client->dev,
1004 &sensor_dev_attr_fan2_min.dev_attr);
1005 device_create_file(&new_client->dev,
1006 &sensor_dev_attr_fan2_div.dev_attr);
1008 if (data->has_fan & (1 << 2)) {
1009 device_create_file(&new_client->dev,
1010 &sensor_dev_attr_fan3_input.dev_attr);
1011 device_create_file(&new_client->dev,
1012 &sensor_dev_attr_fan3_min.dev_attr);
1013 device_create_file(&new_client->dev,
1014 &sensor_dev_attr_fan3_div.dev_attr);
1018 device_create_file(&new_client->dev, &dev_attr_alarms);
1019 if (enable_pwm_interface) {
1020 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
1021 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
1022 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
1023 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
1024 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
1025 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
1028 if (data->type == it8712 || data->type == it8716) {
1029 data->vrm = vid_which_vrm();
1030 device_create_file_vrm(new_client);
1031 device_create_file_vid(new_client);
1037 i2c_detach_client(new_client);
1042 release_region(address, IT87_EXTENT);
1047 static int it87_detach_client(struct i2c_client *client)
1049 struct it87_data *data = i2c_get_clientdata(client);
1052 hwmon_device_unregister(data->class_dev);
1054 if ((err = i2c_detach_client(client)))
1057 if(i2c_is_isa_client(client))
1058 release_region(client->addr, IT87_EXTENT);
1064 /* The SMBus locks itself, but ISA access must be locked explicitly!
1065 We don't want to lock the whole ISA bus, so we lock each client
1067 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1068 would slow down the IT87 access and should not be necessary. */
1069 static int it87_read_value(struct i2c_client *client, u8 reg)
1071 struct it87_data *data = i2c_get_clientdata(client);
1074 if (i2c_is_isa_client(client)) {
1075 mutex_lock(&data->lock);
1076 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1077 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
1078 mutex_unlock(&data->lock);
1081 return i2c_smbus_read_byte_data(client, reg);
1084 /* The SMBus locks itself, but ISA access muse be locked explicitly!
1085 We don't want to lock the whole ISA bus, so we lock each client
1087 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1088 would slow down the IT87 access and should not be necessary. */
1089 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
1091 struct it87_data *data = i2c_get_clientdata(client);
1093 if (i2c_is_isa_client(client)) {
1094 mutex_lock(&data->lock);
1095 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1096 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
1097 mutex_unlock(&data->lock);
1100 return i2c_smbus_write_byte_data(client, reg, value);
1103 /* Return 1 if and only if the PWM interface is safe to use */
1104 static int it87_check_pwm(struct i2c_client *client)
1106 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1107 * and polarity set to active low is sign that this is the case so we
1108 * disable pwm control to protect the user. */
1109 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1110 if ((tmp & 0x87) == 0) {
1111 if (fix_pwm_polarity) {
1112 /* The user asks us to attempt a chip reconfiguration.
1113 * This means switching to active high polarity and
1114 * inverting all fan speed values. */
1118 for (i = 0; i < 3; i++)
1119 pwm[i] = it87_read_value(client,
1122 /* If any fan is in automatic pwm mode, the polarity
1123 * might be correct, as suspicious as it seems, so we
1124 * better don't change anything (but still disable the
1125 * PWM interface). */
1126 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1127 dev_info(&client->dev, "Reconfiguring PWM to "
1128 "active high polarity\n");
1129 it87_write_value(client, IT87_REG_FAN_CTL,
1131 for (i = 0; i < 3; i++)
1132 it87_write_value(client,
1138 dev_info(&client->dev, "PWM configuration is "
1139 "too broken to be fixed\n");
1142 dev_info(&client->dev, "Detected broken BIOS "
1143 "defaults, disabling PWM interface\n");
1145 } else if (fix_pwm_polarity) {
1146 dev_info(&client->dev, "PWM configuration looks "
1147 "sane, won't touch\n");
1153 /* Called when we have found a new IT87. */
1154 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1158 /* initialize to sane defaults:
1159 * - if the chip is in manual pwm mode, this will be overwritten with
1160 * the actual settings on the chip (so in this case, initialization
1162 * - if in automatic or on/off mode, we could switch to manual mode,
1163 * read the registers and set manual_pwm_ctl accordingly, but currently
1164 * this is not implemented, so we initialize to something sane */
1165 for (i = 0; i < 3; i++) {
1166 data->manual_pwm_ctl[i] = 0xff;
1169 /* Check if temperature channnels are reset manually or by some reason */
1170 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1171 if ((tmp & 0x3f) == 0) {
1172 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1173 tmp = (tmp & 0xc0) | 0x2a;
1174 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1178 /* Check if voltage monitors are reset manually or by some reason */
1179 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1180 if ((tmp & 0xff) == 0) {
1181 /* Enable all voltage monitors */
1182 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1185 /* Check if tachometers are reset manually or by some reason */
1186 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1187 if ((data->fan_main_ctrl & 0x70) == 0) {
1188 /* Enable all fan tachometers */
1189 data->fan_main_ctrl |= 0x70;
1190 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1192 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1194 /* Set tachometers to 16-bit mode if needed */
1195 if (data->type == it8716) {
1196 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
1197 if (~tmp & 0x07 & data->has_fan) {
1198 dev_dbg(&client->dev,
1199 "Setting fan1-3 to 16-bit mode\n");
1200 it87_write_value(client, IT87_REG_FAN_16BIT,
1205 /* Set current fan mode registers and the default settings for the
1206 * other mode registers */
1207 for (i = 0; i < 3; i++) {
1208 if (data->fan_main_ctrl & (1 << i)) {
1210 tmp = it87_read_value(client, IT87_REG_PWM(i));
1212 /* automatic pwm - not yet implemented, but
1213 * leave the settings made by the BIOS alone
1214 * until a change is requested via the sysfs
1218 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1223 /* Start monitoring */
1224 it87_write_value(client, IT87_REG_CONFIG,
1225 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1226 | (update_vbat ? 0x41 : 0x01));
1229 static struct it87_data *it87_update_device(struct device *dev)
1231 struct i2c_client *client = to_i2c_client(dev);
1232 struct it87_data *data = i2c_get_clientdata(client);
1235 mutex_lock(&data->update_lock);
1237 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1241 /* Cleared after each update, so reenable. Value
1242 returned by this read will be previous value */
1243 it87_write_value(client, IT87_REG_CONFIG,
1244 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1246 for (i = 0; i <= 7; i++) {
1248 it87_read_value(client, IT87_REG_VIN(i));
1250 it87_read_value(client, IT87_REG_VIN_MIN(i));
1252 it87_read_value(client, IT87_REG_VIN_MAX(i));
1255 it87_read_value(client, IT87_REG_VIN(8));
1256 /* Temperature sensor doesn't have limit registers, set
1257 to min and max value */
1258 data->in_min[8] = 0;
1259 data->in_max[8] = 255;
1261 for (i = 0; i < 3; i++) {
1262 /* Skip disabled fans */
1263 if (!(data->has_fan & (1 << i)))
1267 it87_read_value(client, IT87_REG_FAN_MIN(i));
1268 data->fan[i] = it87_read_value(client,
1270 /* Add high byte if in 16-bit mode */
1271 if (data->type == it8716) {
1272 data->fan[i] |= it87_read_value(client,
1273 IT87_REG_FANX(i)) << 8;
1274 data->fan_min[i] |= it87_read_value(client,
1275 IT87_REG_FANX_MIN(i)) << 8;
1278 for (i = 0; i < 3; i++) {
1280 it87_read_value(client, IT87_REG_TEMP(i));
1281 data->temp_high[i] =
1282 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1284 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1287 /* Newer chips don't have clock dividers */
1288 if ((data->has_fan & 0x07) && data->type != it8716) {
1289 i = it87_read_value(client, IT87_REG_FAN_DIV);
1290 data->fan_div[0] = i & 0x07;
1291 data->fan_div[1] = (i >> 3) & 0x07;
1292 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1296 it87_read_value(client, IT87_REG_ALARM1) |
1297 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1298 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1299 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1301 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1302 /* The 8705 does not have VID capability */
1303 if (data->type == it8712 || data->type == it8716) {
1304 data->vid = it87_read_value(client, IT87_REG_VID);
1305 /* The older IT8712F revisions had only 5 VID pins,
1306 but we assume it is always safe to read 6 bits. */
1309 data->last_updated = jiffies;
1313 mutex_unlock(&data->update_lock);
1318 static int __init sm_it87_init(void)
1322 res = i2c_add_driver(&it87_driver);
1326 if (!it87_find(&isa_address)) {
1327 res = i2c_isa_add_driver(&it87_isa_driver);
1329 i2c_del_driver(&it87_driver);
1337 static void __exit sm_it87_exit(void)
1340 i2c_isa_del_driver(&it87_isa_driver);
1341 i2c_del_driver(&it87_driver);
1345 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1346 MODULE_DESCRIPTION("IT8705F/8712F/8716F, SiS950 driver");
1347 module_param(update_vbat, bool, 0);
1348 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1349 module_param(fix_pwm_polarity, bool, 0);
1350 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1351 MODULE_LICENSE("GPL");
1353 module_init(sm_it87_init);
1354 module_exit(sm_it87_exit);