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>
44 /* Addresses to scan */
45 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
46 0x2e, 0x2f, I2C_CLIENT_END };
47 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
49 /* Insmod parameters */
50 SENSORS_INSMOD_2(it87, it8712);
52 #define REG 0x2e /* The register to read/write */
53 #define DEV 0x07 /* Register: Logical device select */
54 #define VAL 0x2f /* The value to read/write */
55 #define PME 0x04 /* The device with the fan registers in it */
56 #define DEVID 0x20 /* Register: Device ID */
57 #define DEVREV 0x22 /* Register: Device Revision */
66 static int superio_inw(int reg)
99 #define IT8712F_DEVID 0x8712
100 #define IT8705F_DEVID 0x8705
101 #define IT87_ACT_REG 0x30
102 #define IT87_BASE_REG 0x60
104 /* Update battery voltage after every reading if true */
105 static int update_vbat;
107 /* Not all BIOSes properly configure the PWM registers */
108 static int fix_pwm_polarity;
112 static u16 chip_type;
114 /* Many IT87 constants specified below */
116 /* Length of ISA address segment */
117 #define IT87_EXTENT 8
119 /* Where are the ISA address/data registers relative to the base address */
120 #define IT87_ADDR_REG_OFFSET 5
121 #define IT87_DATA_REG_OFFSET 6
123 /*----- The IT87 registers -----*/
125 #define IT87_REG_CONFIG 0x00
127 #define IT87_REG_ALARM1 0x01
128 #define IT87_REG_ALARM2 0x02
129 #define IT87_REG_ALARM3 0x03
131 #define IT87_REG_VID 0x0a
132 #define IT87_REG_FAN_DIV 0x0b
134 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
136 #define IT87_REG_FAN(nr) (0x0d + (nr))
137 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
138 #define IT87_REG_FAN_MAIN_CTRL 0x13
139 #define IT87_REG_FAN_CTL 0x14
140 #define IT87_REG_PWM(nr) (0x15 + (nr))
142 #define IT87_REG_VIN(nr) (0x20 + (nr))
143 #define IT87_REG_TEMP(nr) (0x29 + (nr))
145 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
146 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
147 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
148 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
150 #define IT87_REG_I2C_ADDR 0x48
152 #define IT87_REG_VIN_ENABLE 0x50
153 #define IT87_REG_TEMP_ENABLE 0x51
155 #define IT87_REG_CHIPID 0x58
157 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
158 #define IN_FROM_REG(val) ((val) * 16)
160 static inline u8 FAN_TO_REG(long rpm, int div)
164 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
165 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
169 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
171 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
172 ((val)+500)/1000),-128,127))
173 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
175 #define ALARMS_FROM_REG(val) (val)
177 #define PWM_TO_REG(val) ((val) >> 1)
178 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
180 static int DIV_TO_REG(int val)
183 while ((val >>= 1) != 0)
187 #define DIV_FROM_REG(val) (1 << (val))
190 /* For each registered IT87, we need to keep some data in memory. That
191 data is pointed to by it87_list[NR]->data. The structure itself is
192 dynamically allocated, at the same time when a new it87 client is
195 struct i2c_client client;
196 struct semaphore lock;
199 struct semaphore update_lock;
200 char valid; /* !=0 if following fields are valid */
201 unsigned long last_updated; /* In jiffies */
203 u8 in[9]; /* Register value */
204 u8 in_max[9]; /* Register value */
205 u8 in_min[9]; /* Register value */
206 u8 fan[3]; /* Register value */
207 u8 fan_min[3]; /* Register value */
208 u8 temp[3]; /* Register value */
209 u8 temp_high[3]; /* Register value */
210 u8 temp_low[3]; /* Register value */
211 u8 sensor; /* Register value */
212 u8 fan_div[3]; /* Register encoding, shifted right */
213 u8 vid; /* Register encoding, combined */
215 u32 alarms; /* Register encoding, combined */
216 u8 fan_main_ctrl; /* Register value */
217 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
221 static int it87_attach_adapter(struct i2c_adapter *adapter);
222 static int it87_find(int *address);
223 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
224 static int it87_detach_client(struct i2c_client *client);
226 static int it87_read_value(struct i2c_client *client, u8 register);
227 static int it87_write_value(struct i2c_client *client, u8 register,
229 static struct it87_data *it87_update_device(struct device *dev);
230 static int it87_check_pwm(struct i2c_client *client);
231 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
234 static struct i2c_driver it87_driver = {
235 .owner = THIS_MODULE,
237 .id = I2C_DRIVERID_IT87,
238 .flags = I2C_DF_NOTIFY,
239 .attach_adapter = it87_attach_adapter,
240 .detach_client = it87_detach_client,
243 static ssize_t show_in(struct device *dev, char *buf, int nr)
245 struct it87_data *data = it87_update_device(dev);
246 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
249 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
251 struct it87_data *data = it87_update_device(dev);
252 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
255 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
257 struct it87_data *data = it87_update_device(dev);
258 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
261 static ssize_t set_in_min(struct device *dev, const char *buf,
262 size_t count, int nr)
264 struct i2c_client *client = to_i2c_client(dev);
265 struct it87_data *data = i2c_get_clientdata(client);
266 unsigned long val = simple_strtoul(buf, NULL, 10);
268 down(&data->update_lock);
269 data->in_min[nr] = IN_TO_REG(val);
270 it87_write_value(client, IT87_REG_VIN_MIN(nr),
272 up(&data->update_lock);
275 static ssize_t set_in_max(struct device *dev, const char *buf,
276 size_t count, int nr)
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_max[nr] = IN_TO_REG(val);
284 it87_write_value(client, IT87_REG_VIN_MAX(nr),
286 up(&data->update_lock);
290 #define show_in_offset(offset) \
292 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
294 return show_in(dev, buf, offset); \
296 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
298 #define limit_in_offset(offset) \
300 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
302 return show_in_min(dev, buf, offset); \
305 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
307 return show_in_max(dev, buf, offset); \
309 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
310 const char *buf, size_t count) \
312 return set_in_min(dev, buf, count, offset); \
314 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
315 const char *buf, size_t count) \
317 return set_in_max(dev, buf, count, offset); \
319 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
320 show_in##offset##_min, set_in##offset##_min); \
321 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
322 show_in##offset##_max, set_in##offset##_max);
343 static ssize_t show_temp(struct device *dev, char *buf, int nr)
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, char *buf, int nr)
350 struct it87_data *data = it87_update_device(dev);
351 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
353 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
355 struct it87_data *data = it87_update_device(dev);
356 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
358 static ssize_t set_temp_max(struct device *dev, const char *buf,
359 size_t count, int nr)
361 struct i2c_client *client = to_i2c_client(dev);
362 struct it87_data *data = i2c_get_clientdata(client);
363 int val = simple_strtol(buf, NULL, 10);
365 down(&data->update_lock);
366 data->temp_high[nr] = TEMP_TO_REG(val);
367 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
368 up(&data->update_lock);
371 static ssize_t set_temp_min(struct device *dev, const char *buf,
372 size_t count, int nr)
374 struct i2c_client *client = to_i2c_client(dev);
375 struct it87_data *data = i2c_get_clientdata(client);
376 int val = simple_strtol(buf, NULL, 10);
378 down(&data->update_lock);
379 data->temp_low[nr] = TEMP_TO_REG(val);
380 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
381 up(&data->update_lock);
384 #define show_temp_offset(offset) \
385 static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
387 return show_temp(dev, buf, offset - 1); \
390 show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
392 return show_temp_max(dev, buf, offset - 1); \
395 show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
397 return show_temp_min(dev, buf, offset - 1); \
399 static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
400 const char *buf, size_t count) \
402 return set_temp_max(dev, buf, count, offset - 1); \
404 static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
405 const char *buf, size_t count) \
407 return set_temp_min(dev, buf, count, offset - 1); \
409 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
410 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
411 show_temp_##offset##_max, set_temp_##offset##_max); \
412 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
413 show_temp_##offset##_min, set_temp_##offset##_min);
419 static ssize_t show_sensor(struct device *dev, char *buf, int nr)
421 struct it87_data *data = it87_update_device(dev);
422 u8 reg = data->sensor; /* In case the value is updated while we use it */
425 return sprintf(buf, "3\n"); /* thermal diode */
427 return sprintf(buf, "2\n"); /* thermistor */
428 return sprintf(buf, "0\n"); /* disabled */
430 static ssize_t set_sensor(struct device *dev, const char *buf,
431 size_t count, int nr)
433 struct i2c_client *client = to_i2c_client(dev);
434 struct it87_data *data = i2c_get_clientdata(client);
435 int val = simple_strtol(buf, NULL, 10);
437 down(&data->update_lock);
439 data->sensor &= ~(1 << nr);
440 data->sensor &= ~(8 << nr);
441 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
443 data->sensor |= 1 << nr;
445 data->sensor |= 8 << nr;
447 up(&data->update_lock);
450 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
451 up(&data->update_lock);
454 #define show_sensor_offset(offset) \
455 static ssize_t show_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
457 return show_sensor(dev, buf, offset - 1); \
459 static ssize_t set_sensor_##offset (struct device *dev, struct device_attribute *attr, \
460 const char *buf, size_t count) \
462 return set_sensor(dev, buf, count, offset - 1); \
464 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
465 show_sensor_##offset, set_sensor_##offset);
467 show_sensor_offset(1);
468 show_sensor_offset(2);
469 show_sensor_offset(3);
472 static ssize_t show_fan(struct device *dev, char *buf, int nr)
474 struct it87_data *data = it87_update_device(dev);
475 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
476 DIV_FROM_REG(data->fan_div[nr])));
478 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
480 struct it87_data *data = it87_update_device(dev);
481 return sprintf(buf,"%d\n",
482 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
484 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
486 struct it87_data *data = it87_update_device(dev);
487 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
489 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
491 struct it87_data *data = it87_update_device(dev);
492 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
494 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
496 struct it87_data *data = it87_update_device(dev);
497 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
499 static ssize_t set_fan_min(struct device *dev, const char *buf,
500 size_t count, int nr)
502 struct i2c_client *client = to_i2c_client(dev);
503 struct it87_data *data = i2c_get_clientdata(client);
504 int val = simple_strtol(buf, NULL, 10);
506 down(&data->update_lock);
507 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
508 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
509 up(&data->update_lock);
512 static ssize_t set_fan_div(struct device *dev, const char *buf,
513 size_t count, int nr)
515 struct i2c_client *client = to_i2c_client(dev);
516 struct it87_data *data = i2c_get_clientdata(client);
517 int val = simple_strtol(buf, NULL, 10);
521 down(&data->update_lock);
522 old = it87_read_value(client, IT87_REG_FAN_DIV);
524 for (i = 0; i < 3; i++)
525 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
530 data->fan_div[nr] = DIV_TO_REG(val);
534 data->fan_div[nr] = 1;
536 data->fan_div[nr] = 3;
539 val |= (data->fan_div[0] & 0x07);
540 val |= (data->fan_div[1] & 0x07) << 3;
541 if (data->fan_div[2] == 3)
543 it87_write_value(client, IT87_REG_FAN_DIV, val);
545 for (i = 0; i < 3; i++) {
546 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
547 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
549 up(&data->update_lock);
552 static ssize_t set_pwm_enable(struct device *dev, const char *buf,
553 size_t count, int nr)
555 struct i2c_client *client = to_i2c_client(dev);
556 struct it87_data *data = i2c_get_clientdata(client);
557 int val = simple_strtol(buf, NULL, 10);
559 down(&data->update_lock);
563 /* make sure the fan is on when in on/off mode */
564 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
565 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
566 /* set on/off mode */
567 data->fan_main_ctrl &= ~(1 << nr);
568 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
569 } else if (val == 1) {
570 /* set SmartGuardian mode */
571 data->fan_main_ctrl |= (1 << nr);
572 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
573 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
574 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
576 up(&data->update_lock);
580 up(&data->update_lock);
583 static ssize_t set_pwm(struct device *dev, const char *buf,
584 size_t count, int nr)
586 struct i2c_client *client = to_i2c_client(dev);
587 struct it87_data *data = i2c_get_clientdata(client);
588 int val = simple_strtol(buf, NULL, 10);
590 if (val < 0 || val > 255)
593 down(&data->update_lock);
594 data->manual_pwm_ctl[nr] = val;
595 if (data->fan_main_ctrl & (1 << nr))
596 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
597 up(&data->update_lock);
601 #define show_fan_offset(offset) \
602 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
604 return show_fan(dev, buf, offset - 1); \
606 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
608 return show_fan_min(dev, buf, offset - 1); \
610 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
612 return show_fan_div(dev, buf, offset - 1); \
614 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
615 const char *buf, size_t count) \
617 return set_fan_min(dev, buf, count, offset - 1); \
619 static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
620 const char *buf, size_t count) \
622 return set_fan_div(dev, buf, count, offset - 1); \
624 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
625 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
626 show_fan_##offset##_min, set_fan_##offset##_min); \
627 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
628 show_fan_##offset##_div, set_fan_##offset##_div);
634 #define show_pwm_offset(offset) \
635 static ssize_t show_pwm##offset##_enable (struct device *dev, struct device_attribute *attr, \
638 return show_pwm_enable(dev, buf, offset - 1); \
640 static ssize_t show_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \
642 return show_pwm(dev, buf, offset - 1); \
644 static ssize_t set_pwm##offset##_enable (struct device *dev, struct device_attribute *attr, \
645 const char *buf, size_t count) \
647 return set_pwm_enable(dev, buf, count, offset - 1); \
649 static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \
650 const char *buf, size_t count) \
652 return set_pwm(dev, buf, count, offset - 1); \
654 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
655 show_pwm##offset##_enable, \
656 set_pwm##offset##_enable); \
657 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
658 show_pwm##offset , set_pwm##offset );
665 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
667 struct it87_data *data = it87_update_device(dev);
668 return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
670 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
673 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
675 struct it87_data *data = it87_update_device(dev);
676 return sprintf(buf, "%ld\n", (long) data->vrm);
679 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
681 struct i2c_client *client = to_i2c_client(dev);
682 struct it87_data *data = i2c_get_clientdata(client);
685 val = simple_strtoul(buf, NULL, 10);
690 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
691 #define device_create_file_vrm(client) \
692 device_create_file(&client->dev, &dev_attr_vrm)
695 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
697 struct it87_data *data = it87_update_device(dev);
698 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
700 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
701 #define device_create_file_vid(client) \
702 device_create_file(&client->dev, &dev_attr_cpu0_vid)
704 /* This function is called when:
705 * it87_driver is inserted (when this module is loaded), for each
707 * when a new adapter is inserted (and it87_driver is still present) */
708 static int it87_attach_adapter(struct i2c_adapter *adapter)
710 if (!(adapter->class & I2C_CLASS_HWMON))
712 return i2c_detect(adapter, &addr_data, it87_detect);
715 /* SuperIO detection - will change normal_isa[0] if a chip is found */
716 static int it87_find(int *address)
721 chip_type = superio_inw(DEVID);
722 if (chip_type != IT8712F_DEVID
723 && chip_type != IT8705F_DEVID)
727 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
728 pr_info("it87: Device not activated, skipping\n");
732 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
734 pr_info("it87: Base address not set, skipping\n");
739 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
740 chip_type, *address, superio_inb(DEVREV) & 0x0f);
747 /* This function is called by i2c_detect */
748 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
751 struct i2c_client *new_client;
752 struct it87_data *data;
754 const char *name = "";
755 int is_isa = i2c_is_isa_adapter(adapter);
756 int enable_pwm_interface;
759 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
762 /* Reserve the ISA region */
764 if (!request_region(address, IT87_EXTENT, it87_driver.name))
767 /* Probe whether there is anything available on this address. Already
768 done for SMBus and Super-I/O clients */
770 if (is_isa && !chip_type) {
771 #define REALLY_SLOW_IO
772 /* We need the timeouts for at least some IT87-like chips. But only
773 if we read 'undefined' registers. */
774 i = inb_p(address + 1);
775 if (inb_p(address + 2) != i
776 || inb_p(address + 3) != i
777 || inb_p(address + 7) != i) {
781 #undef REALLY_SLOW_IO
783 /* Let's just hope nothing breaks here */
784 i = inb_p(address + 5) & 0x7f;
785 outb_p(~i & 0x7f, address + 5);
786 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
787 outb_p(i, address + 5);
794 /* OK. For now, we presume we have a valid client. We now create the
795 client structure, even though we cannot fill it completely yet.
796 But it allows us to access it87_{read,write}_value. */
798 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
802 memset(data, 0, sizeof(struct it87_data));
804 new_client = &data->client;
806 init_MUTEX(&data->lock);
807 i2c_set_clientdata(new_client, data);
808 new_client->addr = address;
809 new_client->adapter = adapter;
810 new_client->driver = &it87_driver;
811 new_client->flags = 0;
813 /* Now, we do the remaining detection. */
816 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
818 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
824 /* Determine the chip type. */
826 i = it87_read_value(new_client, IT87_REG_CHIPID);
829 if ((is_isa) && (chip_type == IT8712F_DEVID))
834 dev_info(&adapter->dev,
835 "Ignoring 'force' parameter for unknown chip at "
836 "adapter %d, address 0x%02x\n",
837 i2c_adapter_id(adapter), address);
845 } else if (kind == it8712) {
849 /* Fill in the remaining client fields and put it into the global list */
850 strlcpy(new_client->name, name, I2C_NAME_SIZE);
853 init_MUTEX(&data->update_lock);
855 /* Tell the I2C layer a new client has arrived */
856 if ((err = i2c_attach_client(new_client)))
859 /* Check PWM configuration */
860 enable_pwm_interface = it87_check_pwm(new_client);
862 /* Initialize the IT87 chip */
863 it87_init_client(new_client, data);
865 /* Register sysfs hooks */
866 device_create_file(&new_client->dev, &dev_attr_in0_input);
867 device_create_file(&new_client->dev, &dev_attr_in1_input);
868 device_create_file(&new_client->dev, &dev_attr_in2_input);
869 device_create_file(&new_client->dev, &dev_attr_in3_input);
870 device_create_file(&new_client->dev, &dev_attr_in4_input);
871 device_create_file(&new_client->dev, &dev_attr_in5_input);
872 device_create_file(&new_client->dev, &dev_attr_in6_input);
873 device_create_file(&new_client->dev, &dev_attr_in7_input);
874 device_create_file(&new_client->dev, &dev_attr_in8_input);
875 device_create_file(&new_client->dev, &dev_attr_in0_min);
876 device_create_file(&new_client->dev, &dev_attr_in1_min);
877 device_create_file(&new_client->dev, &dev_attr_in2_min);
878 device_create_file(&new_client->dev, &dev_attr_in3_min);
879 device_create_file(&new_client->dev, &dev_attr_in4_min);
880 device_create_file(&new_client->dev, &dev_attr_in5_min);
881 device_create_file(&new_client->dev, &dev_attr_in6_min);
882 device_create_file(&new_client->dev, &dev_attr_in7_min);
883 device_create_file(&new_client->dev, &dev_attr_in0_max);
884 device_create_file(&new_client->dev, &dev_attr_in1_max);
885 device_create_file(&new_client->dev, &dev_attr_in2_max);
886 device_create_file(&new_client->dev, &dev_attr_in3_max);
887 device_create_file(&new_client->dev, &dev_attr_in4_max);
888 device_create_file(&new_client->dev, &dev_attr_in5_max);
889 device_create_file(&new_client->dev, &dev_attr_in6_max);
890 device_create_file(&new_client->dev, &dev_attr_in7_max);
891 device_create_file(&new_client->dev, &dev_attr_temp1_input);
892 device_create_file(&new_client->dev, &dev_attr_temp2_input);
893 device_create_file(&new_client->dev, &dev_attr_temp3_input);
894 device_create_file(&new_client->dev, &dev_attr_temp1_max);
895 device_create_file(&new_client->dev, &dev_attr_temp2_max);
896 device_create_file(&new_client->dev, &dev_attr_temp3_max);
897 device_create_file(&new_client->dev, &dev_attr_temp1_min);
898 device_create_file(&new_client->dev, &dev_attr_temp2_min);
899 device_create_file(&new_client->dev, &dev_attr_temp3_min);
900 device_create_file(&new_client->dev, &dev_attr_temp1_type);
901 device_create_file(&new_client->dev, &dev_attr_temp2_type);
902 device_create_file(&new_client->dev, &dev_attr_temp3_type);
903 device_create_file(&new_client->dev, &dev_attr_fan1_input);
904 device_create_file(&new_client->dev, &dev_attr_fan2_input);
905 device_create_file(&new_client->dev, &dev_attr_fan3_input);
906 device_create_file(&new_client->dev, &dev_attr_fan1_min);
907 device_create_file(&new_client->dev, &dev_attr_fan2_min);
908 device_create_file(&new_client->dev, &dev_attr_fan3_min);
909 device_create_file(&new_client->dev, &dev_attr_fan1_div);
910 device_create_file(&new_client->dev, &dev_attr_fan2_div);
911 device_create_file(&new_client->dev, &dev_attr_fan3_div);
912 device_create_file(&new_client->dev, &dev_attr_alarms);
913 if (enable_pwm_interface) {
914 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
915 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
916 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
917 device_create_file(&new_client->dev, &dev_attr_pwm1);
918 device_create_file(&new_client->dev, &dev_attr_pwm2);
919 device_create_file(&new_client->dev, &dev_attr_pwm3);
922 if (data->type == it8712) {
923 data->vrm = i2c_which_vrm();
924 device_create_file_vrm(new_client);
925 device_create_file_vid(new_client);
934 release_region(address, IT87_EXTENT);
939 static int it87_detach_client(struct i2c_client *client)
943 if ((err = i2c_detach_client(client))) {
944 dev_err(&client->dev,
945 "Client deregistration failed, client not detached.\n");
949 if(i2c_is_isa_client(client))
950 release_region(client->addr, IT87_EXTENT);
951 kfree(i2c_get_clientdata(client));
956 /* The SMBus locks itself, but ISA access must be locked explicitely!
957 We don't want to lock the whole ISA bus, so we lock each client
959 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
960 would slow down the IT87 access and should not be necessary. */
961 static int it87_read_value(struct i2c_client *client, u8 reg)
963 struct it87_data *data = i2c_get_clientdata(client);
966 if (i2c_is_isa_client(client)) {
968 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
969 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
973 return i2c_smbus_read_byte_data(client, reg);
976 /* The SMBus locks itself, but ISA access muse be locked explicitely!
977 We don't want to lock the whole ISA bus, so we lock each client
979 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
980 would slow down the IT87 access and should not be necessary. */
981 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
983 struct it87_data *data = i2c_get_clientdata(client);
985 if (i2c_is_isa_client(client)) {
987 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
988 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
992 return i2c_smbus_write_byte_data(client, reg, value);
995 /* Return 1 if and only if the PWM interface is safe to use */
996 static int it87_check_pwm(struct i2c_client *client)
998 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
999 * and polarity set to active low is sign that this is the case so we
1000 * disable pwm control to protect the user. */
1001 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1002 if ((tmp & 0x87) == 0) {
1003 if (fix_pwm_polarity) {
1004 /* The user asks us to attempt a chip reconfiguration.
1005 * This means switching to active high polarity and
1006 * inverting all fan speed values. */
1010 for (i = 0; i < 3; i++)
1011 pwm[i] = it87_read_value(client,
1014 /* If any fan is in automatic pwm mode, the polarity
1015 * might be correct, as suspicious as it seems, so we
1016 * better don't change anything (but still disable the
1017 * PWM interface). */
1018 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1019 dev_info(&client->dev, "Reconfiguring PWM to "
1020 "active high polarity\n");
1021 it87_write_value(client, IT87_REG_FAN_CTL,
1023 for (i = 0; i < 3; i++)
1024 it87_write_value(client,
1030 dev_info(&client->dev, "PWM configuration is "
1031 "too broken to be fixed\n");
1034 dev_info(&client->dev, "Detected broken BIOS "
1035 "defaults, disabling PWM interface\n");
1037 } else if (fix_pwm_polarity) {
1038 dev_info(&client->dev, "PWM configuration looks "
1039 "sane, won't touch\n");
1045 /* Called when we have found a new IT87. */
1046 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1050 /* initialize to sane defaults:
1051 * - if the chip is in manual pwm mode, this will be overwritten with
1052 * the actual settings on the chip (so in this case, initialization
1054 * - if in automatic or on/off mode, we could switch to manual mode,
1055 * read the registers and set manual_pwm_ctl accordingly, but currently
1056 * this is not implemented, so we initialize to something sane */
1057 for (i = 0; i < 3; i++) {
1058 data->manual_pwm_ctl[i] = 0xff;
1061 /* Check if temperature channnels are reset manually or by some reason */
1062 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1063 if ((tmp & 0x3f) == 0) {
1064 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1065 tmp = (tmp & 0xc0) | 0x2a;
1066 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1070 /* Check if voltage monitors are reset manually or by some reason */
1071 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1072 if ((tmp & 0xff) == 0) {
1073 /* Enable all voltage monitors */
1074 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1077 /* Check if tachometers are reset manually or by some reason */
1078 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1079 if ((data->fan_main_ctrl & 0x70) == 0) {
1080 /* Enable all fan tachometers */
1081 data->fan_main_ctrl |= 0x70;
1082 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1085 /* Set current fan mode registers and the default settings for the
1086 * other mode registers */
1087 for (i = 0; i < 3; i++) {
1088 if (data->fan_main_ctrl & (1 << i)) {
1090 tmp = it87_read_value(client, IT87_REG_PWM(i));
1092 /* automatic pwm - not yet implemented, but
1093 * leave the settings made by the BIOS alone
1094 * until a change is requested via the sysfs
1098 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1103 /* Start monitoring */
1104 it87_write_value(client, IT87_REG_CONFIG,
1105 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1106 | (update_vbat ? 0x41 : 0x01));
1109 static struct it87_data *it87_update_device(struct device *dev)
1111 struct i2c_client *client = to_i2c_client(dev);
1112 struct it87_data *data = i2c_get_clientdata(client);
1115 down(&data->update_lock);
1117 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1121 /* Cleared after each update, so reenable. Value
1122 returned by this read will be previous value */
1123 it87_write_value(client, IT87_REG_CONFIG,
1124 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1126 for (i = 0; i <= 7; i++) {
1128 it87_read_value(client, IT87_REG_VIN(i));
1130 it87_read_value(client, IT87_REG_VIN_MIN(i));
1132 it87_read_value(client, IT87_REG_VIN_MAX(i));
1135 it87_read_value(client, IT87_REG_VIN(8));
1136 /* Temperature sensor doesn't have limit registers, set
1137 to min and max value */
1138 data->in_min[8] = 0;
1139 data->in_max[8] = 255;
1141 for (i = 0; i < 3; i++) {
1143 it87_read_value(client, IT87_REG_FAN(i));
1145 it87_read_value(client, IT87_REG_FAN_MIN(i));
1147 for (i = 0; i < 3; i++) {
1149 it87_read_value(client, IT87_REG_TEMP(i));
1150 data->temp_high[i] =
1151 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1153 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1156 i = it87_read_value(client, IT87_REG_FAN_DIV);
1157 data->fan_div[0] = i & 0x07;
1158 data->fan_div[1] = (i >> 3) & 0x07;
1159 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1162 it87_read_value(client, IT87_REG_ALARM1) |
1163 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1164 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1165 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1167 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1168 /* The 8705 does not have VID capability */
1169 if (data->type == it8712) {
1170 data->vid = it87_read_value(client, IT87_REG_VID);
1173 data->last_updated = jiffies;
1177 up(&data->update_lock);
1182 static int __init sm_it87_init(void)
1186 if (!it87_find(&addr)) {
1187 normal_isa[0] = addr;
1189 return i2c_add_driver(&it87_driver);
1192 static void __exit sm_it87_exit(void)
1194 i2c_del_driver(&it87_driver);
1198 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1199 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1200 module_param(update_vbat, bool, 0);
1201 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1202 module_param(fix_pwm_polarity, bool, 0);
1203 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1204 MODULE_LICENSE("GPL");
1206 module_init(sm_it87_init);
1207 module_exit(sm_it87_exit);