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>
45 /* Addresses to scan */
46 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
47 0x2e, 0x2f, I2C_CLIENT_END };
48 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
50 /* Insmod parameters */
51 SENSORS_INSMOD_2(it87, it8712);
53 #define REG 0x2e /* The register to read/write */
54 #define DEV 0x07 /* Register: Logical device select */
55 #define VAL 0x2f /* The value to read/write */
56 #define PME 0x04 /* The device with the fan registers in it */
57 #define DEVID 0x20 /* Register: Device ID */
58 #define DEVREV 0x22 /* Register: Device Revision */
67 static int superio_inw(int reg)
100 #define IT8712F_DEVID 0x8712
101 #define IT8705F_DEVID 0x8705
102 #define IT87_ACT_REG 0x30
103 #define IT87_BASE_REG 0x60
105 /* Update battery voltage after every reading if true */
106 static int update_vbat;
108 /* Not all BIOSes properly configure the PWM registers */
109 static int fix_pwm_polarity;
113 static u16 chip_type;
115 /* Many IT87 constants specified below */
117 /* Length of ISA address segment */
118 #define IT87_EXTENT 8
120 /* Where are the ISA address/data registers relative to the base address */
121 #define IT87_ADDR_REG_OFFSET 5
122 #define IT87_DATA_REG_OFFSET 6
124 /*----- The IT87 registers -----*/
126 #define IT87_REG_CONFIG 0x00
128 #define IT87_REG_ALARM1 0x01
129 #define IT87_REG_ALARM2 0x02
130 #define IT87_REG_ALARM3 0x03
132 #define IT87_REG_VID 0x0a
133 #define IT87_REG_FAN_DIV 0x0b
135 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
137 #define IT87_REG_FAN(nr) (0x0d + (nr))
138 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
139 #define IT87_REG_FAN_MAIN_CTRL 0x13
140 #define IT87_REG_FAN_CTL 0x14
141 #define IT87_REG_PWM(nr) (0x15 + (nr))
143 #define IT87_REG_VIN(nr) (0x20 + (nr))
144 #define IT87_REG_TEMP(nr) (0x29 + (nr))
146 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
147 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
148 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
149 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
151 #define IT87_REG_I2C_ADDR 0x48
153 #define IT87_REG_VIN_ENABLE 0x50
154 #define IT87_REG_TEMP_ENABLE 0x51
156 #define IT87_REG_CHIPID 0x58
158 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
159 #define IN_FROM_REG(val) ((val) * 16)
161 static inline u8 FAN_TO_REG(long rpm, int div)
165 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
166 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
170 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
172 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
173 ((val)+500)/1000),-128,127))
174 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
176 #define PWM_TO_REG(val) ((val) >> 1)
177 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
179 static int DIV_TO_REG(int val)
182 while ((val >>= 1) != 0)
186 #define DIV_FROM_REG(val) (1 << (val))
189 /* For each registered IT87, we need to keep some data in memory. That
190 data is pointed to by it87_list[NR]->data. The structure itself is
191 dynamically allocated, at the same time when a new it87 client is
194 struct i2c_client client;
195 struct semaphore lock;
198 struct semaphore update_lock;
199 char valid; /* !=0 if following fields are valid */
200 unsigned long last_updated; /* In jiffies */
202 u8 in[9]; /* Register value */
203 u8 in_max[9]; /* Register value */
204 u8 in_min[9]; /* Register value */
205 u8 fan[3]; /* Register value */
206 u8 fan_min[3]; /* Register value */
207 u8 temp[3]; /* Register value */
208 u8 temp_high[3]; /* Register value */
209 u8 temp_low[3]; /* Register value */
210 u8 sensor; /* Register value */
211 u8 fan_div[3]; /* Register encoding, shifted right */
212 u8 vid; /* Register encoding, combined */
214 u32 alarms; /* Register encoding, combined */
215 u8 fan_main_ctrl; /* Register value */
216 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
220 static int it87_attach_adapter(struct i2c_adapter *adapter);
221 static int it87_find(int *address);
222 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
223 static int it87_detach_client(struct i2c_client *client);
225 static int it87_read_value(struct i2c_client *client, u8 register);
226 static int it87_write_value(struct i2c_client *client, u8 register,
228 static struct it87_data *it87_update_device(struct device *dev);
229 static int it87_check_pwm(struct i2c_client *client);
230 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
233 static struct i2c_driver it87_driver = {
234 .owner = THIS_MODULE,
236 .id = I2C_DRIVERID_IT87,
237 .flags = I2C_DF_NOTIFY,
238 .attach_adapter = it87_attach_adapter,
239 .detach_client = it87_detach_client,
242 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
245 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
246 int nr = sensor_attr->index;
248 struct it87_data *data = it87_update_device(dev);
249 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
252 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
255 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
256 int nr = sensor_attr->index;
258 struct it87_data *data = it87_update_device(dev);
259 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
262 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
265 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
266 int nr = sensor_attr->index;
268 struct it87_data *data = it87_update_device(dev);
269 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
272 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
275 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
276 int nr = sensor_attr->index;
278 struct i2c_client *client = to_i2c_client(dev);
279 struct it87_data *data = i2c_get_clientdata(client);
280 unsigned long val = simple_strtoul(buf, NULL, 10);
282 down(&data->update_lock);
283 data->in_min[nr] = IN_TO_REG(val);
284 it87_write_value(client, IT87_REG_VIN_MIN(nr),
286 up(&data->update_lock);
289 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
292 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
293 int nr = sensor_attr->index;
295 struct i2c_client *client = to_i2c_client(dev);
296 struct it87_data *data = i2c_get_clientdata(client);
297 unsigned long val = simple_strtoul(buf, NULL, 10);
299 down(&data->update_lock);
300 data->in_max[nr] = IN_TO_REG(val);
301 it87_write_value(client, IT87_REG_VIN_MAX(nr),
303 up(&data->update_lock);
307 #define show_in_offset(offset) \
308 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
309 show_in, NULL, offset);
311 #define limit_in_offset(offset) \
312 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
313 show_in_min, set_in_min, offset); \
314 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
315 show_in_max, set_in_max, offset);
336 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
339 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
340 int nr = sensor_attr->index;
342 struct it87_data *data = it87_update_device(dev);
343 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
345 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
349 int nr = sensor_attr->index;
351 struct it87_data *data = it87_update_device(dev);
352 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
354 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
357 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
358 int nr = sensor_attr->index;
360 struct it87_data *data = it87_update_device(dev);
361 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
363 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
364 const char *buf, size_t count)
366 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
367 int nr = sensor_attr->index;
369 struct i2c_client *client = to_i2c_client(dev);
370 struct it87_data *data = i2c_get_clientdata(client);
371 int val = simple_strtol(buf, NULL, 10);
373 down(&data->update_lock);
374 data->temp_high[nr] = TEMP_TO_REG(val);
375 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
376 up(&data->update_lock);
379 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t count)
382 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
383 int nr = sensor_attr->index;
385 struct i2c_client *client = to_i2c_client(dev);
386 struct it87_data *data = i2c_get_clientdata(client);
387 int val = simple_strtol(buf, NULL, 10);
389 down(&data->update_lock);
390 data->temp_low[nr] = TEMP_TO_REG(val);
391 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
392 up(&data->update_lock);
395 #define show_temp_offset(offset) \
396 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
397 show_temp, NULL, offset - 1); \
398 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
399 show_temp_max, set_temp_max, offset - 1); \
400 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
401 show_temp_min, set_temp_min, offset - 1);
407 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
410 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
411 int nr = sensor_attr->index;
413 struct it87_data *data = it87_update_device(dev);
414 u8 reg = data->sensor; /* In case the value is updated while we use it */
417 return sprintf(buf, "3\n"); /* thermal diode */
419 return sprintf(buf, "2\n"); /* thermistor */
420 return sprintf(buf, "0\n"); /* disabled */
422 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
425 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
426 int nr = sensor_attr->index;
428 struct i2c_client *client = to_i2c_client(dev);
429 struct it87_data *data = i2c_get_clientdata(client);
430 int val = simple_strtol(buf, NULL, 10);
432 down(&data->update_lock);
434 data->sensor &= ~(1 << nr);
435 data->sensor &= ~(8 << nr);
436 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
438 data->sensor |= 1 << nr;
440 data->sensor |= 8 << nr;
442 up(&data->update_lock);
445 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
446 up(&data->update_lock);
449 #define show_sensor_offset(offset) \
450 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
451 show_sensor, set_sensor, offset - 1);
453 show_sensor_offset(1);
454 show_sensor_offset(2);
455 show_sensor_offset(3);
458 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
461 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
462 int nr = sensor_attr->index;
464 struct it87_data *data = it87_update_device(dev);
465 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
466 DIV_FROM_REG(data->fan_div[nr])));
468 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
471 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
472 int nr = sensor_attr->index;
474 struct it87_data *data = it87_update_device(dev);
475 return sprintf(buf,"%d\n",
476 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
478 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
481 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
482 int nr = sensor_attr->index;
484 struct it87_data *data = it87_update_device(dev);
485 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
487 static ssize_t show_pwm_enable(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", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
496 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
502 struct it87_data *data = it87_update_device(dev);
503 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
505 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
509 int nr = sensor_attr->index;
511 struct i2c_client *client = to_i2c_client(dev);
512 struct it87_data *data = i2c_get_clientdata(client);
513 int val = simple_strtol(buf, NULL, 10);
515 down(&data->update_lock);
516 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
517 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
518 up(&data->update_lock);
521 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
522 const char *buf, size_t count)
524 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
525 int nr = sensor_attr->index;
527 struct i2c_client *client = to_i2c_client(dev);
528 struct it87_data *data = i2c_get_clientdata(client);
529 int val = simple_strtol(buf, NULL, 10);
533 down(&data->update_lock);
534 old = it87_read_value(client, IT87_REG_FAN_DIV);
536 for (i = 0; i < 3; i++)
537 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
542 data->fan_div[nr] = DIV_TO_REG(val);
546 data->fan_div[nr] = 1;
548 data->fan_div[nr] = 3;
551 val |= (data->fan_div[0] & 0x07);
552 val |= (data->fan_div[1] & 0x07) << 3;
553 if (data->fan_div[2] == 3)
555 it87_write_value(client, IT87_REG_FAN_DIV, val);
557 for (i = 0; i < 3; i++) {
558 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
559 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
561 up(&data->update_lock);
564 static ssize_t set_pwm_enable(struct device *dev,
565 struct device_attribute *attr, const char *buf, size_t count)
567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
570 struct i2c_client *client = to_i2c_client(dev);
571 struct it87_data *data = i2c_get_clientdata(client);
572 int val = simple_strtol(buf, NULL, 10);
574 down(&data->update_lock);
578 /* make sure the fan is on when in on/off mode */
579 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
580 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
581 /* set on/off mode */
582 data->fan_main_ctrl &= ~(1 << nr);
583 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
584 } else if (val == 1) {
585 /* set SmartGuardian mode */
586 data->fan_main_ctrl |= (1 << nr);
587 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
588 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
589 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
591 up(&data->update_lock);
595 up(&data->update_lock);
598 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
599 const char *buf, size_t count)
601 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
602 int nr = sensor_attr->index;
604 struct i2c_client *client = to_i2c_client(dev);
605 struct it87_data *data = i2c_get_clientdata(client);
606 int val = simple_strtol(buf, NULL, 10);
608 if (val < 0 || val > 255)
611 down(&data->update_lock);
612 data->manual_pwm_ctl[nr] = val;
613 if (data->fan_main_ctrl & (1 << nr))
614 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
615 up(&data->update_lock);
619 #define show_fan_offset(offset) \
620 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
621 show_fan, NULL, offset - 1); \
622 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
623 show_fan_min, set_fan_min, offset - 1); \
624 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
625 show_fan_div, set_fan_div, offset - 1);
631 #define show_pwm_offset(offset) \
632 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
633 show_pwm_enable, set_pwm_enable, offset - 1); \
634 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
635 show_pwm, set_pwm, offset - 1);
642 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
644 struct it87_data *data = it87_update_device(dev);
645 return sprintf(buf, "%u\n", data->alarms);
647 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
650 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
652 struct it87_data *data = it87_update_device(dev);
653 return sprintf(buf, "%ld\n", (long) data->vrm);
656 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
658 struct i2c_client *client = to_i2c_client(dev);
659 struct it87_data *data = i2c_get_clientdata(client);
662 val = simple_strtoul(buf, NULL, 10);
667 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
668 #define device_create_file_vrm(client) \
669 device_create_file(&client->dev, &dev_attr_vrm)
672 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
674 struct it87_data *data = it87_update_device(dev);
675 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
677 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
678 #define device_create_file_vid(client) \
679 device_create_file(&client->dev, &dev_attr_cpu0_vid)
681 /* This function is called when:
682 * it87_driver is inserted (when this module is loaded), for each
684 * when a new adapter is inserted (and it87_driver is still present) */
685 static int it87_attach_adapter(struct i2c_adapter *adapter)
687 if (!(adapter->class & I2C_CLASS_HWMON))
689 return i2c_detect(adapter, &addr_data, it87_detect);
692 /* SuperIO detection - will change normal_isa[0] if a chip is found */
693 static int it87_find(int *address)
698 chip_type = superio_inw(DEVID);
699 if (chip_type != IT8712F_DEVID
700 && chip_type != IT8705F_DEVID)
704 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
705 pr_info("it87: Device not activated, skipping\n");
709 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
711 pr_info("it87: Base address not set, skipping\n");
716 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
717 chip_type, *address, superio_inb(DEVREV) & 0x0f);
724 /* This function is called by i2c_detect */
725 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
728 struct i2c_client *new_client;
729 struct it87_data *data;
731 const char *name = "";
732 int is_isa = i2c_is_isa_adapter(adapter);
733 int enable_pwm_interface;
736 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
739 /* Reserve the ISA region */
741 if (!request_region(address, IT87_EXTENT, it87_driver.name))
744 /* Probe whether there is anything available on this address. Already
745 done for SMBus and Super-I/O clients */
747 if (is_isa && !chip_type) {
748 #define REALLY_SLOW_IO
749 /* We need the timeouts for at least some IT87-like chips. But only
750 if we read 'undefined' registers. */
751 i = inb_p(address + 1);
752 if (inb_p(address + 2) != i
753 || inb_p(address + 3) != i
754 || inb_p(address + 7) != i) {
758 #undef REALLY_SLOW_IO
760 /* Let's just hope nothing breaks here */
761 i = inb_p(address + 5) & 0x7f;
762 outb_p(~i & 0x7f, address + 5);
763 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
764 outb_p(i, address + 5);
771 /* OK. For now, we presume we have a valid client. We now create the
772 client structure, even though we cannot fill it completely yet.
773 But it allows us to access it87_{read,write}_value. */
775 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
779 memset(data, 0, sizeof(struct it87_data));
781 new_client = &data->client;
783 init_MUTEX(&data->lock);
784 i2c_set_clientdata(new_client, data);
785 new_client->addr = address;
786 new_client->adapter = adapter;
787 new_client->driver = &it87_driver;
788 new_client->flags = 0;
790 /* Now, we do the remaining detection. */
793 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
795 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
801 /* Determine the chip type. */
803 i = it87_read_value(new_client, IT87_REG_CHIPID);
806 if ((is_isa) && (chip_type == IT8712F_DEVID))
811 dev_info(&adapter->dev,
812 "Ignoring 'force' parameter for unknown chip at "
813 "adapter %d, address 0x%02x\n",
814 i2c_adapter_id(adapter), address);
822 } else if (kind == it8712) {
826 /* Fill in the remaining client fields and put it into the global list */
827 strlcpy(new_client->name, name, I2C_NAME_SIZE);
830 init_MUTEX(&data->update_lock);
832 /* Tell the I2C layer a new client has arrived */
833 if ((err = i2c_attach_client(new_client)))
836 /* Check PWM configuration */
837 enable_pwm_interface = it87_check_pwm(new_client);
839 /* Initialize the IT87 chip */
840 it87_init_client(new_client, data);
842 /* Register sysfs hooks */
843 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
844 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
845 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
846 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
847 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
848 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
849 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
850 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
851 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
889 device_create_file(&new_client->dev, &dev_attr_alarms);
890 if (enable_pwm_interface) {
891 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
899 if (data->type == it8712) {
900 data->vrm = i2c_which_vrm();
901 device_create_file_vrm(new_client);
902 device_create_file_vid(new_client);
911 release_region(address, IT87_EXTENT);
916 static int it87_detach_client(struct i2c_client *client)
920 if ((err = i2c_detach_client(client))) {
921 dev_err(&client->dev,
922 "Client deregistration failed, client not detached.\n");
926 if(i2c_is_isa_client(client))
927 release_region(client->addr, IT87_EXTENT);
928 kfree(i2c_get_clientdata(client));
933 /* The SMBus locks itself, but ISA access must be locked explicitly!
934 We don't want to lock the whole ISA bus, so we lock each client
936 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
937 would slow down the IT87 access and should not be necessary. */
938 static int it87_read_value(struct i2c_client *client, u8 reg)
940 struct it87_data *data = i2c_get_clientdata(client);
943 if (i2c_is_isa_client(client)) {
945 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
946 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
950 return i2c_smbus_read_byte_data(client, reg);
953 /* The SMBus locks itself, but ISA access muse be locked explicitly!
954 We don't want to lock the whole ISA bus, so we lock each client
956 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
957 would slow down the IT87 access and should not be necessary. */
958 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
960 struct it87_data *data = i2c_get_clientdata(client);
962 if (i2c_is_isa_client(client)) {
964 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
965 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
969 return i2c_smbus_write_byte_data(client, reg, value);
972 /* Return 1 if and only if the PWM interface is safe to use */
973 static int it87_check_pwm(struct i2c_client *client)
975 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
976 * and polarity set to active low is sign that this is the case so we
977 * disable pwm control to protect the user. */
978 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
979 if ((tmp & 0x87) == 0) {
980 if (fix_pwm_polarity) {
981 /* The user asks us to attempt a chip reconfiguration.
982 * This means switching to active high polarity and
983 * inverting all fan speed values. */
987 for (i = 0; i < 3; i++)
988 pwm[i] = it87_read_value(client,
991 /* If any fan is in automatic pwm mode, the polarity
992 * might be correct, as suspicious as it seems, so we
993 * better don't change anything (but still disable the
995 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
996 dev_info(&client->dev, "Reconfiguring PWM to "
997 "active high polarity\n");
998 it87_write_value(client, IT87_REG_FAN_CTL,
1000 for (i = 0; i < 3; i++)
1001 it87_write_value(client,
1007 dev_info(&client->dev, "PWM configuration is "
1008 "too broken to be fixed\n");
1011 dev_info(&client->dev, "Detected broken BIOS "
1012 "defaults, disabling PWM interface\n");
1014 } else if (fix_pwm_polarity) {
1015 dev_info(&client->dev, "PWM configuration looks "
1016 "sane, won't touch\n");
1022 /* Called when we have found a new IT87. */
1023 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1027 /* initialize to sane defaults:
1028 * - if the chip is in manual pwm mode, this will be overwritten with
1029 * the actual settings on the chip (so in this case, initialization
1031 * - if in automatic or on/off mode, we could switch to manual mode,
1032 * read the registers and set manual_pwm_ctl accordingly, but currently
1033 * this is not implemented, so we initialize to something sane */
1034 for (i = 0; i < 3; i++) {
1035 data->manual_pwm_ctl[i] = 0xff;
1038 /* Check if temperature channnels are reset manually or by some reason */
1039 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1040 if ((tmp & 0x3f) == 0) {
1041 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1042 tmp = (tmp & 0xc0) | 0x2a;
1043 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1047 /* Check if voltage monitors are reset manually or by some reason */
1048 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1049 if ((tmp & 0xff) == 0) {
1050 /* Enable all voltage monitors */
1051 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1054 /* Check if tachometers are reset manually or by some reason */
1055 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1056 if ((data->fan_main_ctrl & 0x70) == 0) {
1057 /* Enable all fan tachometers */
1058 data->fan_main_ctrl |= 0x70;
1059 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1062 /* Set current fan mode registers and the default settings for the
1063 * other mode registers */
1064 for (i = 0; i < 3; i++) {
1065 if (data->fan_main_ctrl & (1 << i)) {
1067 tmp = it87_read_value(client, IT87_REG_PWM(i));
1069 /* automatic pwm - not yet implemented, but
1070 * leave the settings made by the BIOS alone
1071 * until a change is requested via the sysfs
1075 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1080 /* Start monitoring */
1081 it87_write_value(client, IT87_REG_CONFIG,
1082 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1083 | (update_vbat ? 0x41 : 0x01));
1086 static struct it87_data *it87_update_device(struct device *dev)
1088 struct i2c_client *client = to_i2c_client(dev);
1089 struct it87_data *data = i2c_get_clientdata(client);
1092 down(&data->update_lock);
1094 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1098 /* Cleared after each update, so reenable. Value
1099 returned by this read will be previous value */
1100 it87_write_value(client, IT87_REG_CONFIG,
1101 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1103 for (i = 0; i <= 7; i++) {
1105 it87_read_value(client, IT87_REG_VIN(i));
1107 it87_read_value(client, IT87_REG_VIN_MIN(i));
1109 it87_read_value(client, IT87_REG_VIN_MAX(i));
1112 it87_read_value(client, IT87_REG_VIN(8));
1113 /* Temperature sensor doesn't have limit registers, set
1114 to min and max value */
1115 data->in_min[8] = 0;
1116 data->in_max[8] = 255;
1118 for (i = 0; i < 3; i++) {
1120 it87_read_value(client, IT87_REG_FAN(i));
1122 it87_read_value(client, IT87_REG_FAN_MIN(i));
1124 for (i = 0; i < 3; i++) {
1126 it87_read_value(client, IT87_REG_TEMP(i));
1127 data->temp_high[i] =
1128 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1130 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1133 i = it87_read_value(client, IT87_REG_FAN_DIV);
1134 data->fan_div[0] = i & 0x07;
1135 data->fan_div[1] = (i >> 3) & 0x07;
1136 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1139 it87_read_value(client, IT87_REG_ALARM1) |
1140 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1141 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1142 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1144 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1145 /* The 8705 does not have VID capability */
1146 if (data->type == it8712) {
1147 data->vid = it87_read_value(client, IT87_REG_VID);
1150 data->last_updated = jiffies;
1154 up(&data->update_lock);
1159 static int __init sm_it87_init(void)
1163 if (!it87_find(&addr)) {
1164 normal_isa[0] = addr;
1166 return i2c_add_driver(&it87_driver);
1169 static void __exit sm_it87_exit(void)
1171 i2c_del_driver(&it87_driver);
1175 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1176 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1177 module_param(update_vbat, bool, 0);
1178 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1179 module_param(fix_pwm_polarity, bool, 0);
1180 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1181 MODULE_LICENSE("GPL");
1183 module_init(sm_it87_init);
1184 module_exit(sm_it87_exit);