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-sensor.h>
40 #include <linux/i2c-vid.h>
41 #include <linux/hwmon-sysfs.h>
42 #include <linux/hwmon.h>
43 #include <linux/err.h>
47 /* Addresses to scan */
48 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
49 0x2e, 0x2f, I2C_CLIENT_END };
50 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
52 /* Insmod parameters */
53 SENSORS_INSMOD_2(it87, it8712);
55 #define REG 0x2e /* The register to read/write */
56 #define DEV 0x07 /* Register: Logical device select */
57 #define VAL 0x2f /* The value to read/write */
58 #define PME 0x04 /* The device with the fan registers in it */
59 #define DEVID 0x20 /* Register: Device ID */
60 #define DEVREV 0x22 /* Register: Device Revision */
69 static int superio_inw(int reg)
102 #define IT8712F_DEVID 0x8712
103 #define IT8705F_DEVID 0x8705
104 #define IT87_ACT_REG 0x30
105 #define IT87_BASE_REG 0x60
107 /* Update battery voltage after every reading if true */
108 static int update_vbat;
110 /* Not all BIOSes properly configure the PWM registers */
111 static int fix_pwm_polarity;
115 static u16 chip_type;
117 /* Many IT87 constants specified below */
119 /* Length of ISA address segment */
120 #define IT87_EXTENT 8
122 /* Where are the ISA address/data registers relative to the base address */
123 #define IT87_ADDR_REG_OFFSET 5
124 #define IT87_DATA_REG_OFFSET 6
126 /*----- The IT87 registers -----*/
128 #define IT87_REG_CONFIG 0x00
130 #define IT87_REG_ALARM1 0x01
131 #define IT87_REG_ALARM2 0x02
132 #define IT87_REG_ALARM3 0x03
134 #define IT87_REG_VID 0x0a
135 #define IT87_REG_FAN_DIV 0x0b
137 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
139 #define IT87_REG_FAN(nr) (0x0d + (nr))
140 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
141 #define IT87_REG_FAN_MAIN_CTRL 0x13
142 #define IT87_REG_FAN_CTL 0x14
143 #define IT87_REG_PWM(nr) (0x15 + (nr))
145 #define IT87_REG_VIN(nr) (0x20 + (nr))
146 #define IT87_REG_TEMP(nr) (0x29 + (nr))
148 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
149 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
150 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
151 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
153 #define IT87_REG_I2C_ADDR 0x48
155 #define IT87_REG_VIN_ENABLE 0x50
156 #define IT87_REG_TEMP_ENABLE 0x51
158 #define IT87_REG_CHIPID 0x58
160 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
161 #define IN_FROM_REG(val) ((val) * 16)
163 static inline u8 FAN_TO_REG(long rpm, int div)
167 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
168 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
172 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
174 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
175 ((val)+500)/1000),-128,127))
176 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
178 #define PWM_TO_REG(val) ((val) >> 1)
179 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
181 static int DIV_TO_REG(int val)
184 while ((val >>= 1) != 0)
188 #define DIV_FROM_REG(val) (1 << (val))
191 /* For each registered IT87, we need to keep some data in memory. That
192 data is pointed to by it87_list[NR]->data. The structure itself is
193 dynamically allocated, at the same time when a new it87 client is
196 struct i2c_client client;
197 struct class_device *class_dev;
198 struct semaphore lock;
201 struct semaphore update_lock;
202 char valid; /* !=0 if following fields are valid */
203 unsigned long last_updated; /* In jiffies */
205 u8 in[9]; /* Register value */
206 u8 in_max[9]; /* Register value */
207 u8 in_min[9]; /* Register value */
208 u8 fan[3]; /* Register value */
209 u8 fan_min[3]; /* Register value */
210 u8 temp[3]; /* Register value */
211 u8 temp_high[3]; /* Register value */
212 u8 temp_low[3]; /* Register value */
213 u8 sensor; /* Register value */
214 u8 fan_div[3]; /* Register encoding, shifted right */
215 u8 vid; /* Register encoding, combined */
217 u32 alarms; /* Register encoding, combined */
218 u8 fan_main_ctrl; /* Register value */
219 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
223 static int it87_attach_adapter(struct i2c_adapter *adapter);
224 static int it87_find(int *address);
225 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
226 static int it87_detach_client(struct i2c_client *client);
228 static int it87_read_value(struct i2c_client *client, u8 register);
229 static int it87_write_value(struct i2c_client *client, u8 register,
231 static struct it87_data *it87_update_device(struct device *dev);
232 static int it87_check_pwm(struct i2c_client *client);
233 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
236 static struct i2c_driver it87_driver = {
237 .owner = THIS_MODULE,
239 .id = I2C_DRIVERID_IT87,
240 .flags = I2C_DF_NOTIFY,
241 .attach_adapter = it87_attach_adapter,
242 .detach_client = it87_detach_client,
245 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
248 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
249 int nr = sensor_attr->index;
251 struct it87_data *data = it87_update_device(dev);
252 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
255 static ssize_t show_in_min(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_min[nr]));
265 static ssize_t show_in_max(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_max[nr]));
275 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
276 const char *buf, size_t count)
278 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
279 int nr = sensor_attr->index;
281 struct i2c_client *client = to_i2c_client(dev);
282 struct it87_data *data = i2c_get_clientdata(client);
283 unsigned long val = simple_strtoul(buf, NULL, 10);
285 down(&data->update_lock);
286 data->in_min[nr] = IN_TO_REG(val);
287 it87_write_value(client, IT87_REG_VIN_MIN(nr),
289 up(&data->update_lock);
292 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
295 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
296 int nr = sensor_attr->index;
298 struct i2c_client *client = to_i2c_client(dev);
299 struct it87_data *data = i2c_get_clientdata(client);
300 unsigned long val = simple_strtoul(buf, NULL, 10);
302 down(&data->update_lock);
303 data->in_max[nr] = IN_TO_REG(val);
304 it87_write_value(client, IT87_REG_VIN_MAX(nr),
306 up(&data->update_lock);
310 #define show_in_offset(offset) \
311 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
312 show_in, NULL, offset);
314 #define limit_in_offset(offset) \
315 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
316 show_in_min, set_in_min, offset); \
317 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
318 show_in_max, set_in_max, offset);
339 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
342 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
343 int nr = sensor_attr->index;
345 struct it87_data *data = it87_update_device(dev);
346 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
348 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
351 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
352 int nr = sensor_attr->index;
354 struct it87_data *data = it87_update_device(dev);
355 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
357 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
360 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
361 int nr = sensor_attr->index;
363 struct it87_data *data = it87_update_device(dev);
364 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
366 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
367 const char *buf, size_t count)
369 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
370 int nr = sensor_attr->index;
372 struct i2c_client *client = to_i2c_client(dev);
373 struct it87_data *data = i2c_get_clientdata(client);
374 int val = simple_strtol(buf, NULL, 10);
376 down(&data->update_lock);
377 data->temp_high[nr] = TEMP_TO_REG(val);
378 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
379 up(&data->update_lock);
382 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
383 const char *buf, size_t count)
385 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
386 int nr = sensor_attr->index;
388 struct i2c_client *client = to_i2c_client(dev);
389 struct it87_data *data = i2c_get_clientdata(client);
390 int val = simple_strtol(buf, NULL, 10);
392 down(&data->update_lock);
393 data->temp_low[nr] = TEMP_TO_REG(val);
394 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
395 up(&data->update_lock);
398 #define show_temp_offset(offset) \
399 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
400 show_temp, NULL, offset - 1); \
401 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
402 show_temp_max, set_temp_max, offset - 1); \
403 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
404 show_temp_min, set_temp_min, offset - 1);
410 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
413 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
414 int nr = sensor_attr->index;
416 struct it87_data *data = it87_update_device(dev);
417 u8 reg = data->sensor; /* In case the value is updated while we use it */
420 return sprintf(buf, "3\n"); /* thermal diode */
422 return sprintf(buf, "2\n"); /* thermistor */
423 return sprintf(buf, "0\n"); /* disabled */
425 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
426 const char *buf, size_t count)
428 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
429 int nr = sensor_attr->index;
431 struct i2c_client *client = to_i2c_client(dev);
432 struct it87_data *data = i2c_get_clientdata(client);
433 int val = simple_strtol(buf, NULL, 10);
435 down(&data->update_lock);
437 data->sensor &= ~(1 << nr);
438 data->sensor &= ~(8 << nr);
439 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
441 data->sensor |= 1 << nr;
443 data->sensor |= 8 << nr;
445 up(&data->update_lock);
448 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
449 up(&data->update_lock);
452 #define show_sensor_offset(offset) \
453 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
454 show_sensor, set_sensor, offset - 1);
456 show_sensor_offset(1);
457 show_sensor_offset(2);
458 show_sensor_offset(3);
461 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
464 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
465 int nr = sensor_attr->index;
467 struct it87_data *data = it87_update_device(dev);
468 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
469 DIV_FROM_REG(data->fan_div[nr])));
471 static ssize_t show_fan_min(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",
479 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
481 static ssize_t show_fan_div(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", DIV_FROM_REG(data->fan_div[nr]));
490 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
493 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
494 int nr = sensor_attr->index;
496 struct it87_data *data = it87_update_device(dev);
497 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
499 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
502 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
503 int nr = sensor_attr->index;
505 struct it87_data *data = it87_update_device(dev);
506 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
508 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
511 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
512 int nr = sensor_attr->index;
514 struct i2c_client *client = to_i2c_client(dev);
515 struct it87_data *data = i2c_get_clientdata(client);
516 int val = simple_strtol(buf, NULL, 10);
518 down(&data->update_lock);
519 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
520 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
521 up(&data->update_lock);
524 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
525 const char *buf, size_t count)
527 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
528 int nr = sensor_attr->index;
530 struct i2c_client *client = to_i2c_client(dev);
531 struct it87_data *data = i2c_get_clientdata(client);
532 int val = simple_strtol(buf, NULL, 10);
536 down(&data->update_lock);
537 old = it87_read_value(client, IT87_REG_FAN_DIV);
539 for (i = 0; i < 3; i++)
540 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
545 data->fan_div[nr] = DIV_TO_REG(val);
549 data->fan_div[nr] = 1;
551 data->fan_div[nr] = 3;
554 val |= (data->fan_div[0] & 0x07);
555 val |= (data->fan_div[1] & 0x07) << 3;
556 if (data->fan_div[2] == 3)
558 it87_write_value(client, IT87_REG_FAN_DIV, val);
560 for (i = 0; i < 3; i++) {
561 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
562 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
564 up(&data->update_lock);
567 static ssize_t set_pwm_enable(struct device *dev,
568 struct device_attribute *attr, const char *buf, size_t count)
570 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
571 int nr = sensor_attr->index;
573 struct i2c_client *client = to_i2c_client(dev);
574 struct it87_data *data = i2c_get_clientdata(client);
575 int val = simple_strtol(buf, NULL, 10);
577 down(&data->update_lock);
581 /* make sure the fan is on when in on/off mode */
582 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
583 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
584 /* set on/off mode */
585 data->fan_main_ctrl &= ~(1 << nr);
586 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
587 } else if (val == 1) {
588 /* set SmartGuardian mode */
589 data->fan_main_ctrl |= (1 << nr);
590 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
591 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
592 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
594 up(&data->update_lock);
598 up(&data->update_lock);
601 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
602 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 if (val < 0 || val > 255)
614 down(&data->update_lock);
615 data->manual_pwm_ctl[nr] = val;
616 if (data->fan_main_ctrl & (1 << nr))
617 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
618 up(&data->update_lock);
622 #define show_fan_offset(offset) \
623 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
624 show_fan, NULL, offset - 1); \
625 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
626 show_fan_min, set_fan_min, offset - 1); \
627 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
628 show_fan_div, set_fan_div, offset - 1);
634 #define show_pwm_offset(offset) \
635 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
636 show_pwm_enable, set_pwm_enable, offset - 1); \
637 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
638 show_pwm, set_pwm, offset - 1);
645 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
647 struct it87_data *data = it87_update_device(dev);
648 return sprintf(buf, "%u\n", data->alarms);
650 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
653 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
655 struct it87_data *data = it87_update_device(dev);
656 return sprintf(buf, "%ld\n", (long) data->vrm);
659 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
661 struct i2c_client *client = to_i2c_client(dev);
662 struct it87_data *data = i2c_get_clientdata(client);
665 val = simple_strtoul(buf, NULL, 10);
670 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
671 #define device_create_file_vrm(client) \
672 device_create_file(&client->dev, &dev_attr_vrm)
675 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
677 struct it87_data *data = it87_update_device(dev);
678 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
680 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
681 #define device_create_file_vid(client) \
682 device_create_file(&client->dev, &dev_attr_cpu0_vid)
684 /* This function is called when:
685 * it87_driver is inserted (when this module is loaded), for each
687 * when a new adapter is inserted (and it87_driver is still present) */
688 static int it87_attach_adapter(struct i2c_adapter *adapter)
690 if (!(adapter->class & I2C_CLASS_HWMON))
692 return i2c_detect(adapter, &addr_data, it87_detect);
695 /* SuperIO detection - will change normal_isa[0] if a chip is found */
696 static int it87_find(int *address)
701 chip_type = superio_inw(DEVID);
702 if (chip_type != IT8712F_DEVID
703 && chip_type != IT8705F_DEVID)
707 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
708 pr_info("it87: Device not activated, skipping\n");
712 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
714 pr_info("it87: Base address not set, skipping\n");
719 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
720 chip_type, *address, superio_inb(DEVREV) & 0x0f);
727 /* This function is called by i2c_detect */
728 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
731 struct i2c_client *new_client;
732 struct it87_data *data;
734 const char *name = "";
735 int is_isa = i2c_is_isa_adapter(adapter);
736 int enable_pwm_interface;
739 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
742 /* Reserve the ISA region */
744 if (!request_region(address, IT87_EXTENT, it87_driver.name))
747 /* Probe whether there is anything available on this address. Already
748 done for SMBus and Super-I/O clients */
750 if (is_isa && !chip_type) {
751 #define REALLY_SLOW_IO
752 /* We need the timeouts for at least some IT87-like chips. But only
753 if we read 'undefined' registers. */
754 i = inb_p(address + 1);
755 if (inb_p(address + 2) != i
756 || inb_p(address + 3) != i
757 || inb_p(address + 7) != i) {
761 #undef REALLY_SLOW_IO
763 /* Let's just hope nothing breaks here */
764 i = inb_p(address + 5) & 0x7f;
765 outb_p(~i & 0x7f, address + 5);
766 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
767 outb_p(i, address + 5);
774 /* OK. For now, we presume we have a valid client. We now create the
775 client structure, even though we cannot fill it completely yet.
776 But it allows us to access it87_{read,write}_value. */
778 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
782 memset(data, 0, sizeof(struct it87_data));
784 new_client = &data->client;
786 init_MUTEX(&data->lock);
787 i2c_set_clientdata(new_client, data);
788 new_client->addr = address;
789 new_client->adapter = adapter;
790 new_client->driver = &it87_driver;
791 new_client->flags = 0;
793 /* Now, we do the remaining detection. */
796 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
798 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
804 /* Determine the chip type. */
806 i = it87_read_value(new_client, IT87_REG_CHIPID);
809 if ((is_isa) && (chip_type == IT8712F_DEVID))
814 dev_info(&adapter->dev,
815 "Ignoring 'force' parameter for unknown chip at "
816 "adapter %d, address 0x%02x\n",
817 i2c_adapter_id(adapter), address);
825 } else if (kind == it8712) {
829 /* Fill in the remaining client fields and put it into the global list */
830 strlcpy(new_client->name, name, I2C_NAME_SIZE);
833 init_MUTEX(&data->update_lock);
835 /* Tell the I2C layer a new client has arrived */
836 if ((err = i2c_attach_client(new_client)))
839 /* Check PWM configuration */
840 enable_pwm_interface = it87_check_pwm(new_client);
842 /* Initialize the IT87 chip */
843 it87_init_client(new_client, data);
845 /* Register sysfs hooks */
846 data->class_dev = hwmon_device_register(&new_client->dev);
847 if (IS_ERR(data->class_dev)) {
848 err = PTR_ERR(data->class_dev);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
898 device_create_file(&new_client->dev, &dev_attr_alarms);
899 if (enable_pwm_interface) {
900 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
905 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
908 if (data->type == it8712) {
909 data->vrm = i2c_which_vrm();
910 device_create_file_vrm(new_client);
911 device_create_file_vid(new_client);
917 i2c_detach_client(new_client);
922 release_region(address, IT87_EXTENT);
927 static int it87_detach_client(struct i2c_client *client)
929 struct it87_data *data = i2c_get_clientdata(client);
932 hwmon_device_unregister(data->class_dev);
934 if ((err = i2c_detach_client(client))) {
935 dev_err(&client->dev,
936 "Client deregistration failed, client not detached.\n");
940 if(i2c_is_isa_client(client))
941 release_region(client->addr, IT87_EXTENT);
947 /* The SMBus locks itself, but ISA access must be locked explicitly!
948 We don't want to lock the whole ISA bus, so we lock each client
950 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
951 would slow down the IT87 access and should not be necessary. */
952 static int it87_read_value(struct i2c_client *client, u8 reg)
954 struct it87_data *data = i2c_get_clientdata(client);
957 if (i2c_is_isa_client(client)) {
959 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
960 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
964 return i2c_smbus_read_byte_data(client, reg);
967 /* The SMBus locks itself, but ISA access muse be locked explicitly!
968 We don't want to lock the whole ISA bus, so we lock each client
970 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
971 would slow down the IT87 access and should not be necessary. */
972 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
974 struct it87_data *data = i2c_get_clientdata(client);
976 if (i2c_is_isa_client(client)) {
978 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
979 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
983 return i2c_smbus_write_byte_data(client, reg, value);
986 /* Return 1 if and only if the PWM interface is safe to use */
987 static int it87_check_pwm(struct i2c_client *client)
989 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
990 * and polarity set to active low is sign that this is the case so we
991 * disable pwm control to protect the user. */
992 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
993 if ((tmp & 0x87) == 0) {
994 if (fix_pwm_polarity) {
995 /* The user asks us to attempt a chip reconfiguration.
996 * This means switching to active high polarity and
997 * inverting all fan speed values. */
1001 for (i = 0; i < 3; i++)
1002 pwm[i] = it87_read_value(client,
1005 /* If any fan is in automatic pwm mode, the polarity
1006 * might be correct, as suspicious as it seems, so we
1007 * better don't change anything (but still disable the
1008 * PWM interface). */
1009 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1010 dev_info(&client->dev, "Reconfiguring PWM to "
1011 "active high polarity\n");
1012 it87_write_value(client, IT87_REG_FAN_CTL,
1014 for (i = 0; i < 3; i++)
1015 it87_write_value(client,
1021 dev_info(&client->dev, "PWM configuration is "
1022 "too broken to be fixed\n");
1025 dev_info(&client->dev, "Detected broken BIOS "
1026 "defaults, disabling PWM interface\n");
1028 } else if (fix_pwm_polarity) {
1029 dev_info(&client->dev, "PWM configuration looks "
1030 "sane, won't touch\n");
1036 /* Called when we have found a new IT87. */
1037 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1041 /* initialize to sane defaults:
1042 * - if the chip is in manual pwm mode, this will be overwritten with
1043 * the actual settings on the chip (so in this case, initialization
1045 * - if in automatic or on/off mode, we could switch to manual mode,
1046 * read the registers and set manual_pwm_ctl accordingly, but currently
1047 * this is not implemented, so we initialize to something sane */
1048 for (i = 0; i < 3; i++) {
1049 data->manual_pwm_ctl[i] = 0xff;
1052 /* Check if temperature channnels are reset manually or by some reason */
1053 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1054 if ((tmp & 0x3f) == 0) {
1055 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1056 tmp = (tmp & 0xc0) | 0x2a;
1057 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1061 /* Check if voltage monitors are reset manually or by some reason */
1062 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1063 if ((tmp & 0xff) == 0) {
1064 /* Enable all voltage monitors */
1065 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1068 /* Check if tachometers are reset manually or by some reason */
1069 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1070 if ((data->fan_main_ctrl & 0x70) == 0) {
1071 /* Enable all fan tachometers */
1072 data->fan_main_ctrl |= 0x70;
1073 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1076 /* Set current fan mode registers and the default settings for the
1077 * other mode registers */
1078 for (i = 0; i < 3; i++) {
1079 if (data->fan_main_ctrl & (1 << i)) {
1081 tmp = it87_read_value(client, IT87_REG_PWM(i));
1083 /* automatic pwm - not yet implemented, but
1084 * leave the settings made by the BIOS alone
1085 * until a change is requested via the sysfs
1089 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1094 /* Start monitoring */
1095 it87_write_value(client, IT87_REG_CONFIG,
1096 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1097 | (update_vbat ? 0x41 : 0x01));
1100 static struct it87_data *it87_update_device(struct device *dev)
1102 struct i2c_client *client = to_i2c_client(dev);
1103 struct it87_data *data = i2c_get_clientdata(client);
1106 down(&data->update_lock);
1108 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1112 /* Cleared after each update, so reenable. Value
1113 returned by this read will be previous value */
1114 it87_write_value(client, IT87_REG_CONFIG,
1115 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1117 for (i = 0; i <= 7; i++) {
1119 it87_read_value(client, IT87_REG_VIN(i));
1121 it87_read_value(client, IT87_REG_VIN_MIN(i));
1123 it87_read_value(client, IT87_REG_VIN_MAX(i));
1126 it87_read_value(client, IT87_REG_VIN(8));
1127 /* Temperature sensor doesn't have limit registers, set
1128 to min and max value */
1129 data->in_min[8] = 0;
1130 data->in_max[8] = 255;
1132 for (i = 0; i < 3; i++) {
1134 it87_read_value(client, IT87_REG_FAN(i));
1136 it87_read_value(client, IT87_REG_FAN_MIN(i));
1138 for (i = 0; i < 3; i++) {
1140 it87_read_value(client, IT87_REG_TEMP(i));
1141 data->temp_high[i] =
1142 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1144 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1147 i = it87_read_value(client, IT87_REG_FAN_DIV);
1148 data->fan_div[0] = i & 0x07;
1149 data->fan_div[1] = (i >> 3) & 0x07;
1150 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1153 it87_read_value(client, IT87_REG_ALARM1) |
1154 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1155 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1156 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1158 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1159 /* The 8705 does not have VID capability */
1160 if (data->type == it8712) {
1161 data->vid = it87_read_value(client, IT87_REG_VID);
1164 data->last_updated = jiffies;
1168 up(&data->update_lock);
1173 static int __init sm_it87_init(void)
1177 if (!it87_find(&addr)) {
1178 normal_isa[0] = addr;
1180 return i2c_add_driver(&it87_driver);
1183 static void __exit sm_it87_exit(void)
1185 i2c_del_driver(&it87_driver);
1189 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1190 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1191 module_param(update_vbat, bool, 0);
1192 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1193 module_param(fix_pwm_polarity, bool, 0);
1194 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1195 MODULE_LICENSE("GPL");
1197 module_init(sm_it87_init);
1198 module_exit(sm_it87_exit);