2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 Supports: IT8705F Super I/O chip w/LPC interface & SMBus
6 IT8712F Super I/O chip w/LPC interface & SMBus
7 Sis950 A clone of the IT8705F
9 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
10 Largely inspired by lm78.c of the same package
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 djg@pdp8.net David Gesswein 7/18/01
29 Modified to fix bug with not all alarms enabled.
30 Added ability to read battery voltage and select temperature sensor
31 type at module load time.
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/jiffies.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-isa.h>
40 #include <linux/i2c-sensor.h>
41 #include <linux/i2c-vid.h>
42 #include <linux/hwmon-sysfs.h>
43 #include <linux/hwmon.h>
44 #include <linux/err.h>
48 /* Addresses to scan */
49 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
50 0x2e, 0x2f, I2C_CLIENT_END };
51 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
52 static unsigned short isa_address = 0x290;
54 /* Insmod parameters */
55 SENSORS_INSMOD_2(it87, it8712);
57 #define REG 0x2e /* The register to read/write */
58 #define DEV 0x07 /* Register: Logical device select */
59 #define VAL 0x2f /* The value to read/write */
60 #define PME 0x04 /* The device with the fan registers in it */
61 #define DEVID 0x20 /* Register: Device ID */
62 #define DEVREV 0x22 /* Register: Device Revision */
71 static int superio_inw(int reg)
104 #define IT8712F_DEVID 0x8712
105 #define IT8705F_DEVID 0x8705
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 #define IT87_REG_FAN_DIV 0x0b
139 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
141 #define IT87_REG_FAN(nr) (0x0d + (nr))
142 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
143 #define IT87_REG_FAN_MAIN_CTRL 0x13
144 #define IT87_REG_FAN_CTL 0x14
145 #define IT87_REG_PWM(nr) (0x15 + (nr))
147 #define IT87_REG_VIN(nr) (0x20 + (nr))
148 #define IT87_REG_TEMP(nr) (0x29 + (nr))
150 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
151 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
152 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
153 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
155 #define IT87_REG_I2C_ADDR 0x48
157 #define IT87_REG_VIN_ENABLE 0x50
158 #define IT87_REG_TEMP_ENABLE 0x51
160 #define IT87_REG_CHIPID 0x58
162 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
163 #define IN_FROM_REG(val) ((val) * 16)
165 static inline u8 FAN_TO_REG(long rpm, int div)
169 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
170 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
174 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
176 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
177 ((val)+500)/1000),-128,127))
178 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
180 #define PWM_TO_REG(val) ((val) >> 1)
181 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
183 static int DIV_TO_REG(int val)
186 while ((val >>= 1) != 0)
190 #define DIV_FROM_REG(val) (1 << (val))
193 /* For each registered IT87, we need to keep some data in memory. That
194 data is pointed to by it87_list[NR]->data. The structure itself is
195 dynamically allocated, at the same time when a new it87 client is
198 struct i2c_client client;
199 struct class_device *class_dev;
200 struct semaphore lock;
203 struct semaphore update_lock;
204 char valid; /* !=0 if following fields are valid */
205 unsigned long last_updated; /* In jiffies */
207 u8 in[9]; /* Register value */
208 u8 in_max[9]; /* Register value */
209 u8 in_min[9]; /* Register value */
210 u8 fan[3]; /* Register value */
211 u8 fan_min[3]; /* Register value */
212 u8 temp[3]; /* Register value */
213 u8 temp_high[3]; /* Register value */
214 u8 temp_low[3]; /* Register value */
215 u8 sensor; /* Register value */
216 u8 fan_div[3]; /* Register encoding, shifted right */
217 u8 vid; /* Register encoding, combined */
219 u32 alarms; /* Register encoding, combined */
220 u8 fan_main_ctrl; /* Register value */
221 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
225 static int it87_attach_adapter(struct i2c_adapter *adapter);
226 static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
227 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
228 static int it87_detach_client(struct i2c_client *client);
230 static int it87_read_value(struct i2c_client *client, u8 register);
231 static int it87_write_value(struct i2c_client *client, u8 register,
233 static struct it87_data *it87_update_device(struct device *dev);
234 static int it87_check_pwm(struct i2c_client *client);
235 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
238 static struct i2c_driver it87_driver = {
239 .owner = THIS_MODULE,
241 .id = I2C_DRIVERID_IT87,
242 .flags = I2C_DF_NOTIFY,
243 .attach_adapter = it87_attach_adapter,
244 .detach_client = it87_detach_client,
247 static struct i2c_driver it87_isa_driver = {
248 .owner = THIS_MODULE,
250 .attach_adapter = it87_isa_attach_adapter,
251 .detach_client = it87_detach_client,
255 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
258 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
259 int nr = sensor_attr->index;
261 struct it87_data *data = it87_update_device(dev);
262 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
265 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
268 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
269 int nr = sensor_attr->index;
271 struct it87_data *data = it87_update_device(dev);
272 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
275 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
278 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
279 int nr = sensor_attr->index;
281 struct it87_data *data = it87_update_device(dev);
282 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
285 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
286 const char *buf, size_t count)
288 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
289 int nr = sensor_attr->index;
291 struct i2c_client *client = to_i2c_client(dev);
292 struct it87_data *data = i2c_get_clientdata(client);
293 unsigned long val = simple_strtoul(buf, NULL, 10);
295 down(&data->update_lock);
296 data->in_min[nr] = IN_TO_REG(val);
297 it87_write_value(client, IT87_REG_VIN_MIN(nr),
299 up(&data->update_lock);
302 static ssize_t set_in_max(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 down(&data->update_lock);
313 data->in_max[nr] = IN_TO_REG(val);
314 it87_write_value(client, IT87_REG_VIN_MAX(nr),
316 up(&data->update_lock);
320 #define show_in_offset(offset) \
321 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
322 show_in, NULL, offset);
324 #define limit_in_offset(offset) \
325 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
326 show_in_min, set_in_min, offset); \
327 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
328 show_in_max, set_in_max, offset);
349 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
352 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
353 int nr = sensor_attr->index;
355 struct it87_data *data = it87_update_device(dev);
356 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
358 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
361 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
362 int nr = sensor_attr->index;
364 struct it87_data *data = it87_update_device(dev);
365 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
367 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
370 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
371 int nr = sensor_attr->index;
373 struct it87_data *data = it87_update_device(dev);
374 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
376 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
377 const char *buf, size_t count)
379 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
380 int nr = sensor_attr->index;
382 struct i2c_client *client = to_i2c_client(dev);
383 struct it87_data *data = i2c_get_clientdata(client);
384 int val = simple_strtol(buf, NULL, 10);
386 down(&data->update_lock);
387 data->temp_high[nr] = TEMP_TO_REG(val);
388 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
389 up(&data->update_lock);
392 static ssize_t set_temp_min(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 down(&data->update_lock);
403 data->temp_low[nr] = TEMP_TO_REG(val);
404 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
405 up(&data->update_lock);
408 #define show_temp_offset(offset) \
409 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
410 show_temp, NULL, offset - 1); \
411 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
412 show_temp_max, set_temp_max, offset - 1); \
413 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
414 show_temp_min, set_temp_min, offset - 1);
420 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
423 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
424 int nr = sensor_attr->index;
426 struct it87_data *data = it87_update_device(dev);
427 u8 reg = data->sensor; /* In case the value is updated while we use it */
430 return sprintf(buf, "3\n"); /* thermal diode */
432 return sprintf(buf, "2\n"); /* thermistor */
433 return sprintf(buf, "0\n"); /* disabled */
435 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
436 const char *buf, size_t count)
438 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
439 int nr = sensor_attr->index;
441 struct i2c_client *client = to_i2c_client(dev);
442 struct it87_data *data = i2c_get_clientdata(client);
443 int val = simple_strtol(buf, NULL, 10);
445 down(&data->update_lock);
447 data->sensor &= ~(1 << nr);
448 data->sensor &= ~(8 << nr);
449 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
451 data->sensor |= 1 << nr;
453 data->sensor |= 8 << nr;
455 up(&data->update_lock);
458 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
459 up(&data->update_lock);
462 #define show_sensor_offset(offset) \
463 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
464 show_sensor, set_sensor, offset - 1);
466 show_sensor_offset(1);
467 show_sensor_offset(2);
468 show_sensor_offset(3);
471 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
474 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
475 int nr = sensor_attr->index;
477 struct it87_data *data = it87_update_device(dev);
478 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
479 DIV_FROM_REG(data->fan_div[nr])));
481 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
484 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
485 int nr = sensor_attr->index;
487 struct it87_data *data = it87_update_device(dev);
488 return sprintf(buf,"%d\n",
489 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
491 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
494 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
495 int nr = sensor_attr->index;
497 struct it87_data *data = it87_update_device(dev);
498 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
500 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
503 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
504 int nr = sensor_attr->index;
506 struct it87_data *data = it87_update_device(dev);
507 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
509 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
512 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
513 int nr = sensor_attr->index;
515 struct it87_data *data = it87_update_device(dev);
516 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
518 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
519 const char *buf, size_t count)
521 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
522 int nr = sensor_attr->index;
524 struct i2c_client *client = to_i2c_client(dev);
525 struct it87_data *data = i2c_get_clientdata(client);
526 int val = simple_strtol(buf, NULL, 10);
528 down(&data->update_lock);
529 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
530 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
531 up(&data->update_lock);
534 static ssize_t set_fan_div(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);
546 down(&data->update_lock);
547 old = it87_read_value(client, IT87_REG_FAN_DIV);
549 for (i = 0; i < 3; i++)
550 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
555 data->fan_div[nr] = DIV_TO_REG(val);
559 data->fan_div[nr] = 1;
561 data->fan_div[nr] = 3;
564 val |= (data->fan_div[0] & 0x07);
565 val |= (data->fan_div[1] & 0x07) << 3;
566 if (data->fan_div[2] == 3)
568 it87_write_value(client, IT87_REG_FAN_DIV, val);
570 for (i = 0; i < 3; i++) {
571 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
572 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
574 up(&data->update_lock);
577 static ssize_t set_pwm_enable(struct device *dev,
578 struct device_attribute *attr, const char *buf, size_t count)
580 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
581 int nr = sensor_attr->index;
583 struct i2c_client *client = to_i2c_client(dev);
584 struct it87_data *data = i2c_get_clientdata(client);
585 int val = simple_strtol(buf, NULL, 10);
587 down(&data->update_lock);
591 /* make sure the fan is on when in on/off mode */
592 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
593 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
594 /* set on/off mode */
595 data->fan_main_ctrl &= ~(1 << nr);
596 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
597 } else if (val == 1) {
598 /* set SmartGuardian mode */
599 data->fan_main_ctrl |= (1 << nr);
600 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
601 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
602 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
604 up(&data->update_lock);
608 up(&data->update_lock);
611 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
612 const char *buf, size_t count)
614 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
615 int nr = sensor_attr->index;
617 struct i2c_client *client = to_i2c_client(dev);
618 struct it87_data *data = i2c_get_clientdata(client);
619 int val = simple_strtol(buf, NULL, 10);
621 if (val < 0 || val > 255)
624 down(&data->update_lock);
625 data->manual_pwm_ctl[nr] = val;
626 if (data->fan_main_ctrl & (1 << nr))
627 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
628 up(&data->update_lock);
632 #define show_fan_offset(offset) \
633 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
634 show_fan, NULL, offset - 1); \
635 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
636 show_fan_min, set_fan_min, offset - 1); \
637 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
638 show_fan_div, set_fan_div, offset - 1);
644 #define show_pwm_offset(offset) \
645 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
646 show_pwm_enable, set_pwm_enable, offset - 1); \
647 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
648 show_pwm, set_pwm, offset - 1);
655 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
657 struct it87_data *data = it87_update_device(dev);
658 return sprintf(buf, "%u\n", data->alarms);
660 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
663 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
665 struct it87_data *data = it87_update_device(dev);
666 return sprintf(buf, "%ld\n", (long) data->vrm);
669 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
671 struct i2c_client *client = to_i2c_client(dev);
672 struct it87_data *data = i2c_get_clientdata(client);
675 val = simple_strtoul(buf, NULL, 10);
680 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
681 #define device_create_file_vrm(client) \
682 device_create_file(&client->dev, &dev_attr_vrm)
685 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
687 struct it87_data *data = it87_update_device(dev);
688 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
690 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
691 #define device_create_file_vid(client) \
692 device_create_file(&client->dev, &dev_attr_cpu0_vid)
694 /* This function is called when:
695 * it87_driver is inserted (when this module is loaded), for each
697 * when a new adapter is inserted (and it87_driver is still present) */
698 static int it87_attach_adapter(struct i2c_adapter *adapter)
700 if (!(adapter->class & I2C_CLASS_HWMON))
702 return i2c_detect(adapter, &addr_data, it87_detect);
705 static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
707 return it87_detect(adapter, isa_address, -1);
710 /* SuperIO detection - will change isa_address if a chip is found */
711 static int it87_find(int *address)
716 chip_type = superio_inw(DEVID);
717 if (chip_type != IT8712F_DEVID
718 && chip_type != IT8705F_DEVID)
722 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
723 pr_info("it87: Device not activated, skipping\n");
727 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
729 pr_info("it87: Base address not set, skipping\n");
734 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
735 chip_type, *address, superio_inb(DEVREV) & 0x0f);
742 /* This function is called by i2c_detect */
743 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
746 struct i2c_client *new_client;
747 struct it87_data *data;
749 const char *name = "";
750 int is_isa = i2c_is_isa_adapter(adapter);
751 int enable_pwm_interface;
754 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
757 /* Reserve the ISA region */
759 if (!request_region(address, IT87_EXTENT, it87_isa_driver.name))
762 /* Probe whether there is anything available on this address. Already
763 done for SMBus and Super-I/O clients */
765 if (is_isa && !chip_type) {
766 #define REALLY_SLOW_IO
767 /* We need the timeouts for at least some IT87-like chips. But only
768 if we read 'undefined' registers. */
769 i = inb_p(address + 1);
770 if (inb_p(address + 2) != i
771 || inb_p(address + 3) != i
772 || inb_p(address + 7) != i) {
776 #undef REALLY_SLOW_IO
778 /* Let's just hope nothing breaks here */
779 i = inb_p(address + 5) & 0x7f;
780 outb_p(~i & 0x7f, address + 5);
781 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
782 outb_p(i, address + 5);
789 /* OK. For now, we presume we have a valid client. We now create the
790 client structure, even though we cannot fill it completely yet.
791 But it allows us to access it87_{read,write}_value. */
793 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
797 memset(data, 0, sizeof(struct it87_data));
799 new_client = &data->client;
801 init_MUTEX(&data->lock);
802 i2c_set_clientdata(new_client, data);
803 new_client->addr = address;
804 new_client->adapter = adapter;
805 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
806 new_client->flags = 0;
808 /* Now, we do the remaining detection. */
811 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
813 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
819 /* Determine the chip type. */
821 i = it87_read_value(new_client, IT87_REG_CHIPID);
824 if ((is_isa) && (chip_type == IT8712F_DEVID))
829 dev_info(&adapter->dev,
830 "Ignoring 'force' parameter for unknown chip at "
831 "adapter %d, address 0x%02x\n",
832 i2c_adapter_id(adapter), address);
840 } else if (kind == it8712) {
844 /* Fill in the remaining client fields and put it into the global list */
845 strlcpy(new_client->name, name, I2C_NAME_SIZE);
848 init_MUTEX(&data->update_lock);
850 /* Tell the I2C layer a new client has arrived */
851 if ((err = i2c_attach_client(new_client)))
854 /* Check PWM configuration */
855 enable_pwm_interface = it87_check_pwm(new_client);
857 /* Initialize the IT87 chip */
858 it87_init_client(new_client, data);
860 /* Register sysfs hooks */
861 data->class_dev = hwmon_device_register(&new_client->dev);
862 if (IS_ERR(data->class_dev)) {
863 err = PTR_ERR(data->class_dev);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
898 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
899 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
905 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
906 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
907 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
908 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
909 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
910 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
911 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
912 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
913 device_create_file(&new_client->dev, &dev_attr_alarms);
914 if (enable_pwm_interface) {
915 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
916 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
917 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
918 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
919 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
920 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
923 if (data->type == it8712) {
924 data->vrm = i2c_which_vrm();
925 device_create_file_vrm(new_client);
926 device_create_file_vid(new_client);
932 i2c_detach_client(new_client);
937 release_region(address, IT87_EXTENT);
942 static int it87_detach_client(struct i2c_client *client)
944 struct it87_data *data = i2c_get_clientdata(client);
947 hwmon_device_unregister(data->class_dev);
949 if ((err = i2c_detach_client(client))) {
950 dev_err(&client->dev,
951 "Client deregistration failed, client not detached.\n");
955 if(i2c_is_isa_client(client))
956 release_region(client->addr, IT87_EXTENT);
962 /* The SMBus locks itself, but ISA access must be locked explicitly!
963 We don't want to lock the whole ISA bus, so we lock each client
965 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
966 would slow down the IT87 access and should not be necessary. */
967 static int it87_read_value(struct i2c_client *client, u8 reg)
969 struct it87_data *data = i2c_get_clientdata(client);
972 if (i2c_is_isa_client(client)) {
974 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
975 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
979 return i2c_smbus_read_byte_data(client, reg);
982 /* The SMBus locks itself, but ISA access muse be locked explicitly!
983 We don't want to lock the whole ISA bus, so we lock each client
985 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
986 would slow down the IT87 access and should not be necessary. */
987 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
989 struct it87_data *data = i2c_get_clientdata(client);
991 if (i2c_is_isa_client(client)) {
993 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
994 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
998 return i2c_smbus_write_byte_data(client, reg, value);
1001 /* Return 1 if and only if the PWM interface is safe to use */
1002 static int it87_check_pwm(struct i2c_client *client)
1004 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1005 * and polarity set to active low is sign that this is the case so we
1006 * disable pwm control to protect the user. */
1007 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1008 if ((tmp & 0x87) == 0) {
1009 if (fix_pwm_polarity) {
1010 /* The user asks us to attempt a chip reconfiguration.
1011 * This means switching to active high polarity and
1012 * inverting all fan speed values. */
1016 for (i = 0; i < 3; i++)
1017 pwm[i] = it87_read_value(client,
1020 /* If any fan is in automatic pwm mode, the polarity
1021 * might be correct, as suspicious as it seems, so we
1022 * better don't change anything (but still disable the
1023 * PWM interface). */
1024 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1025 dev_info(&client->dev, "Reconfiguring PWM to "
1026 "active high polarity\n");
1027 it87_write_value(client, IT87_REG_FAN_CTL,
1029 for (i = 0; i < 3; i++)
1030 it87_write_value(client,
1036 dev_info(&client->dev, "PWM configuration is "
1037 "too broken to be fixed\n");
1040 dev_info(&client->dev, "Detected broken BIOS "
1041 "defaults, disabling PWM interface\n");
1043 } else if (fix_pwm_polarity) {
1044 dev_info(&client->dev, "PWM configuration looks "
1045 "sane, won't touch\n");
1051 /* Called when we have found a new IT87. */
1052 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1056 /* initialize to sane defaults:
1057 * - if the chip is in manual pwm mode, this will be overwritten with
1058 * the actual settings on the chip (so in this case, initialization
1060 * - if in automatic or on/off mode, we could switch to manual mode,
1061 * read the registers and set manual_pwm_ctl accordingly, but currently
1062 * this is not implemented, so we initialize to something sane */
1063 for (i = 0; i < 3; i++) {
1064 data->manual_pwm_ctl[i] = 0xff;
1067 /* Check if temperature channnels are reset manually or by some reason */
1068 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1069 if ((tmp & 0x3f) == 0) {
1070 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1071 tmp = (tmp & 0xc0) | 0x2a;
1072 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1076 /* Check if voltage monitors are reset manually or by some reason */
1077 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1078 if ((tmp & 0xff) == 0) {
1079 /* Enable all voltage monitors */
1080 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1083 /* Check if tachometers are reset manually or by some reason */
1084 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1085 if ((data->fan_main_ctrl & 0x70) == 0) {
1086 /* Enable all fan tachometers */
1087 data->fan_main_ctrl |= 0x70;
1088 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1091 /* Set current fan mode registers and the default settings for the
1092 * other mode registers */
1093 for (i = 0; i < 3; i++) {
1094 if (data->fan_main_ctrl & (1 << i)) {
1096 tmp = it87_read_value(client, IT87_REG_PWM(i));
1098 /* automatic pwm - not yet implemented, but
1099 * leave the settings made by the BIOS alone
1100 * until a change is requested via the sysfs
1104 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1109 /* Start monitoring */
1110 it87_write_value(client, IT87_REG_CONFIG,
1111 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1112 | (update_vbat ? 0x41 : 0x01));
1115 static struct it87_data *it87_update_device(struct device *dev)
1117 struct i2c_client *client = to_i2c_client(dev);
1118 struct it87_data *data = i2c_get_clientdata(client);
1121 down(&data->update_lock);
1123 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1127 /* Cleared after each update, so reenable. Value
1128 returned by this read will be previous value */
1129 it87_write_value(client, IT87_REG_CONFIG,
1130 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1132 for (i = 0; i <= 7; i++) {
1134 it87_read_value(client, IT87_REG_VIN(i));
1136 it87_read_value(client, IT87_REG_VIN_MIN(i));
1138 it87_read_value(client, IT87_REG_VIN_MAX(i));
1141 it87_read_value(client, IT87_REG_VIN(8));
1142 /* Temperature sensor doesn't have limit registers, set
1143 to min and max value */
1144 data->in_min[8] = 0;
1145 data->in_max[8] = 255;
1147 for (i = 0; i < 3; i++) {
1149 it87_read_value(client, IT87_REG_FAN(i));
1151 it87_read_value(client, IT87_REG_FAN_MIN(i));
1153 for (i = 0; i < 3; i++) {
1155 it87_read_value(client, IT87_REG_TEMP(i));
1156 data->temp_high[i] =
1157 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1159 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1162 i = it87_read_value(client, IT87_REG_FAN_DIV);
1163 data->fan_div[0] = i & 0x07;
1164 data->fan_div[1] = (i >> 3) & 0x07;
1165 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1168 it87_read_value(client, IT87_REG_ALARM1) |
1169 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1170 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1171 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1173 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1174 /* The 8705 does not have VID capability */
1175 if (data->type == it8712) {
1176 data->vid = it87_read_value(client, IT87_REG_VID);
1179 data->last_updated = jiffies;
1183 up(&data->update_lock);
1188 static int __init sm_it87_init(void)
1192 if (!it87_find(&addr)) {
1196 res = i2c_add_driver(&it87_driver);
1200 res = i2c_isa_add_driver(&it87_isa_driver);
1202 i2c_del_driver(&it87_driver);
1209 static void __exit sm_it87_exit(void)
1211 i2c_isa_del_driver(&it87_isa_driver);
1212 i2c_del_driver(&it87_driver);
1216 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1217 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1218 module_param(update_vbat, bool, 0);
1219 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1220 module_param(fix_pwm_polarity, bool, 0);
1221 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1222 MODULE_LICENSE("GPL");
1224 module_init(sm_it87_init);
1225 module_exit(sm_it87_exit);