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 ((val >>= 1) != 0)
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 u16 fan[3]; /* Register values, possibly combined */
226 u16 fan_min[3]; /* Register values, possibly combined */
227 u8 temp[3]; /* Register value */
228 u8 temp_high[3]; /* Register value */
229 u8 temp_low[3]; /* Register value */
230 u8 sensor; /* Register value */
231 u8 fan_div[3]; /* Register encoding, shifted right */
232 u8 vid; /* Register encoding, combined */
234 u32 alarms; /* Register encoding, combined */
235 u8 fan_main_ctrl; /* Register value */
236 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
240 static int it87_attach_adapter(struct i2c_adapter *adapter);
241 static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
242 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
243 static int it87_detach_client(struct i2c_client *client);
245 static int it87_read_value(struct i2c_client *client, u8 reg);
246 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value);
247 static struct it87_data *it87_update_device(struct device *dev);
248 static int it87_check_pwm(struct i2c_client *client);
249 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
252 static struct i2c_driver it87_driver = {
256 .id = I2C_DRIVERID_IT87,
257 .attach_adapter = it87_attach_adapter,
258 .detach_client = it87_detach_client,
261 static struct i2c_driver it87_isa_driver = {
263 .owner = THIS_MODULE,
266 .attach_adapter = it87_isa_attach_adapter,
267 .detach_client = it87_detach_client,
271 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
274 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
275 int nr = sensor_attr->index;
277 struct it87_data *data = it87_update_device(dev);
278 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
281 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
284 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
285 int nr = sensor_attr->index;
287 struct it87_data *data = it87_update_device(dev);
288 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
291 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
294 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
295 int nr = sensor_attr->index;
297 struct it87_data *data = it87_update_device(dev);
298 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
301 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
302 const char *buf, size_t count)
304 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
305 int nr = sensor_attr->index;
307 struct i2c_client *client = to_i2c_client(dev);
308 struct it87_data *data = i2c_get_clientdata(client);
309 unsigned long val = simple_strtoul(buf, NULL, 10);
311 mutex_lock(&data->update_lock);
312 data->in_min[nr] = IN_TO_REG(val);
313 it87_write_value(client, IT87_REG_VIN_MIN(nr),
315 mutex_unlock(&data->update_lock);
318 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
319 const char *buf, size_t count)
321 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
322 int nr = sensor_attr->index;
324 struct i2c_client *client = to_i2c_client(dev);
325 struct it87_data *data = i2c_get_clientdata(client);
326 unsigned long val = simple_strtoul(buf, NULL, 10);
328 mutex_lock(&data->update_lock);
329 data->in_max[nr] = IN_TO_REG(val);
330 it87_write_value(client, IT87_REG_VIN_MAX(nr),
332 mutex_unlock(&data->update_lock);
336 #define show_in_offset(offset) \
337 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
338 show_in, NULL, offset);
340 #define limit_in_offset(offset) \
341 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
342 show_in_min, set_in_min, offset); \
343 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
344 show_in_max, set_in_max, offset);
365 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369 int nr = sensor_attr->index;
371 struct it87_data *data = it87_update_device(dev);
372 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
374 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
377 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
378 int nr = sensor_attr->index;
380 struct it87_data *data = it87_update_device(dev);
381 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
383 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
386 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
387 int nr = sensor_attr->index;
389 struct it87_data *data = it87_update_device(dev);
390 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
392 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
393 const char *buf, size_t count)
395 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
396 int nr = sensor_attr->index;
398 struct i2c_client *client = to_i2c_client(dev);
399 struct it87_data *data = i2c_get_clientdata(client);
400 int val = simple_strtol(buf, NULL, 10);
402 mutex_lock(&data->update_lock);
403 data->temp_high[nr] = TEMP_TO_REG(val);
404 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
405 mutex_unlock(&data->update_lock);
408 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
409 const char *buf, size_t count)
411 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412 int nr = sensor_attr->index;
414 struct i2c_client *client = to_i2c_client(dev);
415 struct it87_data *data = i2c_get_clientdata(client);
416 int val = simple_strtol(buf, NULL, 10);
418 mutex_lock(&data->update_lock);
419 data->temp_low[nr] = TEMP_TO_REG(val);
420 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
421 mutex_unlock(&data->update_lock);
424 #define show_temp_offset(offset) \
425 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
426 show_temp, NULL, offset - 1); \
427 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
428 show_temp_max, set_temp_max, offset - 1); \
429 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
430 show_temp_min, set_temp_min, offset - 1);
436 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
439 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
440 int nr = sensor_attr->index;
442 struct it87_data *data = it87_update_device(dev);
443 u8 reg = data->sensor; /* In case the value is updated while we use it */
446 return sprintf(buf, "3\n"); /* thermal diode */
448 return sprintf(buf, "2\n"); /* thermistor */
449 return sprintf(buf, "0\n"); /* disabled */
451 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
452 const char *buf, size_t count)
454 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
455 int nr = sensor_attr->index;
457 struct i2c_client *client = to_i2c_client(dev);
458 struct it87_data *data = i2c_get_clientdata(client);
459 int val = simple_strtol(buf, NULL, 10);
461 mutex_lock(&data->update_lock);
463 data->sensor &= ~(1 << nr);
464 data->sensor &= ~(8 << nr);
465 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
467 data->sensor |= 1 << nr;
469 data->sensor |= 8 << nr;
471 mutex_unlock(&data->update_lock);
474 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
475 mutex_unlock(&data->update_lock);
478 #define show_sensor_offset(offset) \
479 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
480 show_sensor, set_sensor, offset - 1);
482 show_sensor_offset(1);
483 show_sensor_offset(2);
484 show_sensor_offset(3);
487 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
490 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
491 int nr = sensor_attr->index;
493 struct it87_data *data = it87_update_device(dev);
494 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
495 DIV_FROM_REG(data->fan_div[nr])));
497 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
500 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
501 int nr = sensor_attr->index;
503 struct it87_data *data = it87_update_device(dev);
504 return sprintf(buf,"%d\n",
505 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
507 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
510 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
511 int nr = sensor_attr->index;
513 struct it87_data *data = it87_update_device(dev);
514 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
516 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
519 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
520 int nr = sensor_attr->index;
522 struct it87_data *data = it87_update_device(dev);
523 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
525 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
528 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
529 int nr = sensor_attr->index;
531 struct it87_data *data = it87_update_device(dev);
532 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
534 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
535 const char *buf, size_t count)
537 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
538 int nr = sensor_attr->index;
540 struct i2c_client *client = to_i2c_client(dev);
541 struct it87_data *data = i2c_get_clientdata(client);
542 int val = simple_strtol(buf, NULL, 10);
543 u8 reg = it87_read_value(client, IT87_REG_FAN_DIV);
545 mutex_lock(&data->update_lock);
547 case 0: data->fan_div[nr] = reg & 0x07; break;
548 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
549 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
552 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
553 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
554 mutex_unlock(&data->update_lock);
557 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
558 const char *buf, size_t count)
560 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
561 int nr = sensor_attr->index;
563 struct i2c_client *client = to_i2c_client(dev);
564 struct it87_data *data = i2c_get_clientdata(client);
565 int val = simple_strtol(buf, NULL, 10);
569 mutex_lock(&data->update_lock);
570 old = it87_read_value(client, IT87_REG_FAN_DIV);
572 for (i = 0; i < 3; i++)
573 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
578 data->fan_div[nr] = DIV_TO_REG(val);
582 data->fan_div[nr] = 1;
584 data->fan_div[nr] = 3;
587 val |= (data->fan_div[0] & 0x07);
588 val |= (data->fan_div[1] & 0x07) << 3;
589 if (data->fan_div[2] == 3)
591 it87_write_value(client, IT87_REG_FAN_DIV, val);
593 for (i = 0; i < 3; i++) {
594 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
595 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
597 mutex_unlock(&data->update_lock);
600 static ssize_t set_pwm_enable(struct device *dev,
601 struct device_attribute *attr, const char *buf, size_t count)
603 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
604 int nr = sensor_attr->index;
606 struct i2c_client *client = to_i2c_client(dev);
607 struct it87_data *data = i2c_get_clientdata(client);
608 int val = simple_strtol(buf, NULL, 10);
610 mutex_lock(&data->update_lock);
614 /* make sure the fan is on when in on/off mode */
615 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
616 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
617 /* set on/off mode */
618 data->fan_main_ctrl &= ~(1 << nr);
619 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
620 } else if (val == 1) {
621 /* set SmartGuardian mode */
622 data->fan_main_ctrl |= (1 << nr);
623 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
624 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
625 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
627 mutex_unlock(&data->update_lock);
631 mutex_unlock(&data->update_lock);
634 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
635 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 i2c_client *client = to_i2c_client(dev);
641 struct it87_data *data = i2c_get_clientdata(client);
642 int val = simple_strtol(buf, NULL, 10);
644 if (val < 0 || val > 255)
647 mutex_lock(&data->update_lock);
648 data->manual_pwm_ctl[nr] = val;
649 if (data->fan_main_ctrl & (1 << nr))
650 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
651 mutex_unlock(&data->update_lock);
655 #define show_fan_offset(offset) \
656 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
657 show_fan, NULL, offset - 1); \
658 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
659 show_fan_min, set_fan_min, offset - 1); \
660 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
661 show_fan_div, set_fan_div, offset - 1);
667 #define show_pwm_offset(offset) \
668 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
669 show_pwm_enable, set_pwm_enable, offset - 1); \
670 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
671 show_pwm, set_pwm, offset - 1);
677 /* A different set of callbacks for 16-bit fans */
678 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
681 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
682 int nr = sensor_attr->index;
683 struct it87_data *data = it87_update_device(dev);
684 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
687 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
690 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
691 int nr = sensor_attr->index;
692 struct it87_data *data = it87_update_device(dev);
693 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
696 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
697 const char *buf, size_t count)
699 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
700 int nr = sensor_attr->index;
701 struct i2c_client *client = to_i2c_client(dev);
702 struct it87_data *data = i2c_get_clientdata(client);
703 int val = simple_strtol(buf, NULL, 10);
705 mutex_lock(&data->update_lock);
706 data->fan_min[nr] = FAN16_TO_REG(val);
707 it87_write_value(client, IT87_REG_FAN_MIN(nr),
708 data->fan_min[nr] & 0xff);
709 it87_write_value(client, IT87_REG_FANX_MIN(nr),
710 data->fan_min[nr] >> 8);
711 mutex_unlock(&data->update_lock);
715 /* We want to use the same sysfs file names as 8-bit fans, but we need
716 different variable names, so we have to use SENSOR_ATTR instead of
717 SENSOR_DEVICE_ATTR. */
718 #define show_fan16_offset(offset) \
719 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
720 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
721 show_fan16, NULL, offset - 1); \
722 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
723 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
724 show_fan16_min, set_fan16_min, offset - 1)
726 show_fan16_offset(1);
727 show_fan16_offset(2);
728 show_fan16_offset(3);
731 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
733 struct it87_data *data = it87_update_device(dev);
734 return sprintf(buf, "%u\n", data->alarms);
736 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
739 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
741 struct it87_data *data = it87_update_device(dev);
742 return sprintf(buf, "%u\n", data->vrm);
745 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
747 struct i2c_client *client = to_i2c_client(dev);
748 struct it87_data *data = i2c_get_clientdata(client);
751 val = simple_strtoul(buf, NULL, 10);
756 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
757 #define device_create_file_vrm(client) \
758 device_create_file(&client->dev, &dev_attr_vrm)
761 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
763 struct it87_data *data = it87_update_device(dev);
764 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
766 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
767 #define device_create_file_vid(client) \
768 device_create_file(&client->dev, &dev_attr_cpu0_vid)
770 /* This function is called when:
771 * it87_driver is inserted (when this module is loaded), for each
773 * when a new adapter is inserted (and it87_driver is still present) */
774 static int it87_attach_adapter(struct i2c_adapter *adapter)
776 if (!(adapter->class & I2C_CLASS_HWMON))
778 return i2c_probe(adapter, &addr_data, it87_detect);
781 static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
783 return it87_detect(adapter, isa_address, -1);
786 /* SuperIO detection - will change isa_address if a chip is found */
787 static int __init it87_find(unsigned short *address)
792 chip_type = superio_inw(DEVID);
793 if (chip_type != IT8712F_DEVID
794 && chip_type != IT8716F_DEVID
795 && chip_type != IT8705F_DEVID)
799 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
800 pr_info("it87: Device not activated, skipping\n");
804 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
806 pr_info("it87: Base address not set, skipping\n");
811 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
812 chip_type, *address, superio_inb(DEVREV) & 0x0f);
819 /* This function is called by i2c_probe */
820 static int it87_detect(struct i2c_adapter *adapter, int address, int kind)
823 struct i2c_client *new_client;
824 struct it87_data *data;
826 const char *name = "";
827 int is_isa = i2c_is_isa_adapter(adapter);
828 int enable_pwm_interface;
831 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
834 /* Reserve the ISA region */
836 if (!request_region(address, IT87_EXTENT,
837 it87_isa_driver.driver.name))
840 /* For now, we presume we have a valid client. We create the
841 client structure, even though we cannot fill it completely yet.
842 But it allows us to access it87_{read,write}_value. */
844 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
849 new_client = &data->client;
851 mutex_init(&data->lock);
852 i2c_set_clientdata(new_client, data);
853 new_client->addr = address;
854 new_client->adapter = adapter;
855 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
856 new_client->flags = 0;
858 /* Now, we do the remaining detection. */
861 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
863 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
869 /* Determine the chip type. */
871 i = it87_read_value(new_client, IT87_REG_CHIPID);
887 dev_info(&adapter->dev,
888 "Ignoring 'force' parameter for unknown chip at "
889 "adapter %d, address 0x%02x\n",
890 i2c_adapter_id(adapter), address);
898 } else if (kind == it8712) {
900 } else if (kind == it8716) {
904 /* Fill in the remaining client fields and put it into the global list */
905 strlcpy(new_client->name, name, I2C_NAME_SIZE);
908 mutex_init(&data->update_lock);
910 /* Tell the I2C layer a new client has arrived */
911 if ((err = i2c_attach_client(new_client)))
915 dev_info(&new_client->dev, "The I2C interface to IT87xxF "
916 "hardware monitoring chips is deprecated. Please "
917 "report if you still rely on it.\n");
919 /* Check PWM configuration */
920 enable_pwm_interface = it87_check_pwm(new_client);
922 /* Initialize the IT87 chip */
923 it87_init_client(new_client, data);
925 /* Register sysfs hooks */
926 data->class_dev = hwmon_device_register(&new_client->dev);
927 if (IS_ERR(data->class_dev)) {
928 err = PTR_ERR(data->class_dev);
932 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
933 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
934 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
935 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
936 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
937 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
938 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
939 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
940 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
941 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
942 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
943 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
944 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
945 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
946 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
947 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
948 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
949 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
950 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
951 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
952 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
953 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
954 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
955 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
956 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
957 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
958 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
959 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
960 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
961 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
962 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
963 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
964 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
965 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
966 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
967 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
968 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
970 if (data->type == it8716) { /* 16-bit tachometers */
971 device_create_file(&new_client->dev,
972 &sensor_dev_attr_fan1_input16.dev_attr);
973 device_create_file(&new_client->dev,
974 &sensor_dev_attr_fan2_input16.dev_attr);
975 device_create_file(&new_client->dev,
976 &sensor_dev_attr_fan3_input16.dev_attr);
977 device_create_file(&new_client->dev,
978 &sensor_dev_attr_fan1_min16.dev_attr);
979 device_create_file(&new_client->dev,
980 &sensor_dev_attr_fan2_min16.dev_attr);
981 device_create_file(&new_client->dev,
982 &sensor_dev_attr_fan3_min16.dev_attr);
984 device_create_file(&new_client->dev,
985 &sensor_dev_attr_fan1_input.dev_attr);
986 device_create_file(&new_client->dev,
987 &sensor_dev_attr_fan2_input.dev_attr);
988 device_create_file(&new_client->dev,
989 &sensor_dev_attr_fan3_input.dev_attr);
990 device_create_file(&new_client->dev,
991 &sensor_dev_attr_fan1_min.dev_attr);
992 device_create_file(&new_client->dev,
993 &sensor_dev_attr_fan2_min.dev_attr);
994 device_create_file(&new_client->dev,
995 &sensor_dev_attr_fan3_min.dev_attr);
996 device_create_file(&new_client->dev,
997 &sensor_dev_attr_fan1_div.dev_attr);
998 device_create_file(&new_client->dev,
999 &sensor_dev_attr_fan2_div.dev_attr);
1000 device_create_file(&new_client->dev,
1001 &sensor_dev_attr_fan3_div.dev_attr);
1004 device_create_file(&new_client->dev, &dev_attr_alarms);
1005 if (enable_pwm_interface) {
1006 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
1007 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
1008 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
1009 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
1010 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
1011 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
1014 if (data->type == it8712 || data->type == it8716) {
1015 data->vrm = vid_which_vrm();
1016 device_create_file_vrm(new_client);
1017 device_create_file_vid(new_client);
1023 i2c_detach_client(new_client);
1028 release_region(address, IT87_EXTENT);
1033 static int it87_detach_client(struct i2c_client *client)
1035 struct it87_data *data = i2c_get_clientdata(client);
1038 hwmon_device_unregister(data->class_dev);
1040 if ((err = i2c_detach_client(client)))
1043 if(i2c_is_isa_client(client))
1044 release_region(client->addr, IT87_EXTENT);
1050 /* The SMBus locks itself, but ISA access must be locked explicitly!
1051 We don't want to lock the whole ISA bus, so we lock each client
1053 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1054 would slow down the IT87 access and should not be necessary. */
1055 static int it87_read_value(struct i2c_client *client, u8 reg)
1057 struct it87_data *data = i2c_get_clientdata(client);
1060 if (i2c_is_isa_client(client)) {
1061 mutex_lock(&data->lock);
1062 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1063 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
1064 mutex_unlock(&data->lock);
1067 return i2c_smbus_read_byte_data(client, reg);
1070 /* The SMBus locks itself, but ISA access muse be locked explicitly!
1071 We don't want to lock the whole ISA bus, so we lock each client
1073 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1074 would slow down the IT87 access and should not be necessary. */
1075 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
1077 struct it87_data *data = i2c_get_clientdata(client);
1079 if (i2c_is_isa_client(client)) {
1080 mutex_lock(&data->lock);
1081 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1082 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
1083 mutex_unlock(&data->lock);
1086 return i2c_smbus_write_byte_data(client, reg, value);
1089 /* Return 1 if and only if the PWM interface is safe to use */
1090 static int it87_check_pwm(struct i2c_client *client)
1092 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1093 * and polarity set to active low is sign that this is the case so we
1094 * disable pwm control to protect the user. */
1095 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1096 if ((tmp & 0x87) == 0) {
1097 if (fix_pwm_polarity) {
1098 /* The user asks us to attempt a chip reconfiguration.
1099 * This means switching to active high polarity and
1100 * inverting all fan speed values. */
1104 for (i = 0; i < 3; i++)
1105 pwm[i] = it87_read_value(client,
1108 /* If any fan is in automatic pwm mode, the polarity
1109 * might be correct, as suspicious as it seems, so we
1110 * better don't change anything (but still disable the
1111 * PWM interface). */
1112 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1113 dev_info(&client->dev, "Reconfiguring PWM to "
1114 "active high polarity\n");
1115 it87_write_value(client, IT87_REG_FAN_CTL,
1117 for (i = 0; i < 3; i++)
1118 it87_write_value(client,
1124 dev_info(&client->dev, "PWM configuration is "
1125 "too broken to be fixed\n");
1128 dev_info(&client->dev, "Detected broken BIOS "
1129 "defaults, disabling PWM interface\n");
1131 } else if (fix_pwm_polarity) {
1132 dev_info(&client->dev, "PWM configuration looks "
1133 "sane, won't touch\n");
1139 /* Called when we have found a new IT87. */
1140 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1144 /* initialize to sane defaults:
1145 * - if the chip is in manual pwm mode, this will be overwritten with
1146 * the actual settings on the chip (so in this case, initialization
1148 * - if in automatic or on/off mode, we could switch to manual mode,
1149 * read the registers and set manual_pwm_ctl accordingly, but currently
1150 * this is not implemented, so we initialize to something sane */
1151 for (i = 0; i < 3; i++) {
1152 data->manual_pwm_ctl[i] = 0xff;
1155 /* Check if temperature channnels are reset manually or by some reason */
1156 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1157 if ((tmp & 0x3f) == 0) {
1158 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1159 tmp = (tmp & 0xc0) | 0x2a;
1160 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1164 /* Check if voltage monitors are reset manually or by some reason */
1165 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1166 if ((tmp & 0xff) == 0) {
1167 /* Enable all voltage monitors */
1168 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1171 /* Check if tachometers are reset manually or by some reason */
1172 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1173 if ((data->fan_main_ctrl & 0x70) == 0) {
1174 /* Enable all fan tachometers */
1175 data->fan_main_ctrl |= 0x70;
1176 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1179 /* Set tachometers to 16-bit mode if needed */
1180 if (data->type == it8716) {
1181 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
1182 if ((tmp & 0x07) != 0x07) {
1183 dev_dbg(&client->dev,
1184 "Setting fan1-3 to 16-bit mode\n");
1185 it87_write_value(client, IT87_REG_FAN_16BIT,
1190 /* Set current fan mode registers and the default settings for the
1191 * other mode registers */
1192 for (i = 0; i < 3; i++) {
1193 if (data->fan_main_ctrl & (1 << i)) {
1195 tmp = it87_read_value(client, IT87_REG_PWM(i));
1197 /* automatic pwm - not yet implemented, but
1198 * leave the settings made by the BIOS alone
1199 * until a change is requested via the sysfs
1203 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1208 /* Start monitoring */
1209 it87_write_value(client, IT87_REG_CONFIG,
1210 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1211 | (update_vbat ? 0x41 : 0x01));
1214 static struct it87_data *it87_update_device(struct device *dev)
1216 struct i2c_client *client = to_i2c_client(dev);
1217 struct it87_data *data = i2c_get_clientdata(client);
1220 mutex_lock(&data->update_lock);
1222 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1226 /* Cleared after each update, so reenable. Value
1227 returned by this read will be previous value */
1228 it87_write_value(client, IT87_REG_CONFIG,
1229 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1231 for (i = 0; i <= 7; i++) {
1233 it87_read_value(client, IT87_REG_VIN(i));
1235 it87_read_value(client, IT87_REG_VIN_MIN(i));
1237 it87_read_value(client, IT87_REG_VIN_MAX(i));
1240 it87_read_value(client, IT87_REG_VIN(8));
1241 /* Temperature sensor doesn't have limit registers, set
1242 to min and max value */
1243 data->in_min[8] = 0;
1244 data->in_max[8] = 255;
1246 for (i = 0; i < 3; i++) {
1248 it87_read_value(client, IT87_REG_FAN_MIN(i));
1249 data->fan[i] = it87_read_value(client,
1251 /* Add high byte if in 16-bit mode */
1252 if (data->type == it8716) {
1253 data->fan[i] |= it87_read_value(client,
1254 IT87_REG_FANX(i)) << 8;
1255 data->fan_min[i] |= it87_read_value(client,
1256 IT87_REG_FANX_MIN(i)) << 8;
1259 for (i = 0; i < 3; i++) {
1261 it87_read_value(client, IT87_REG_TEMP(i));
1262 data->temp_high[i] =
1263 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1265 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1268 /* Newer chips don't have clock dividers */
1269 if (data->type != it8716) {
1270 i = it87_read_value(client, IT87_REG_FAN_DIV);
1271 data->fan_div[0] = i & 0x07;
1272 data->fan_div[1] = (i >> 3) & 0x07;
1273 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1277 it87_read_value(client, IT87_REG_ALARM1) |
1278 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1279 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1280 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1282 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1283 /* The 8705 does not have VID capability */
1284 if (data->type == it8712 || data->type == it8716) {
1285 data->vid = it87_read_value(client, IT87_REG_VID);
1286 /* The older IT8712F revisions had only 5 VID pins,
1287 but we assume it is always safe to read 6 bits. */
1290 data->last_updated = jiffies;
1294 mutex_unlock(&data->update_lock);
1299 static int __init sm_it87_init(void)
1303 res = i2c_add_driver(&it87_driver);
1307 if (!it87_find(&isa_address)) {
1308 res = i2c_isa_add_driver(&it87_isa_driver);
1310 i2c_del_driver(&it87_driver);
1318 static void __exit sm_it87_exit(void)
1321 i2c_isa_del_driver(&it87_isa_driver);
1322 i2c_del_driver(&it87_driver);
1326 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1327 MODULE_DESCRIPTION("IT8705F/8712F/8716F, SiS950 driver");
1328 module_param(update_vbat, bool, 0);
1329 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1330 module_param(fix_pwm_polarity, bool, 0);
1331 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1332 MODULE_LICENSE("GPL");
1334 module_init(sm_it87_init);
1335 module_exit(sm_it87_exit);