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/config.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.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;
109 static u16 chip_type;
111 /* Many IT87 constants specified below */
113 /* Length of ISA address segment */
114 #define IT87_EXTENT 8
116 /* Where are the ISA address/data registers relative to the base address */
117 #define IT87_ADDR_REG_OFFSET 5
118 #define IT87_DATA_REG_OFFSET 6
120 /*----- The IT87 registers -----*/
122 #define IT87_REG_CONFIG 0x00
124 #define IT87_REG_ALARM1 0x01
125 #define IT87_REG_ALARM2 0x02
126 #define IT87_REG_ALARM3 0x03
128 #define IT87_REG_VID 0x0a
129 #define IT87_REG_FAN_DIV 0x0b
131 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
133 #define IT87_REG_FAN(nr) (0x0d + (nr))
134 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
135 #define IT87_REG_FAN_MAIN_CTRL 0x13
136 #define IT87_REG_FAN_CTL 0x14
137 #define IT87_REG_PWM(nr) (0x15 + (nr))
139 #define IT87_REG_VIN(nr) (0x20 + (nr))
140 #define IT87_REG_TEMP(nr) (0x29 + (nr))
142 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
143 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
144 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
145 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
147 #define IT87_REG_I2C_ADDR 0x48
149 #define IT87_REG_VIN_ENABLE 0x50
150 #define IT87_REG_TEMP_ENABLE 0x51
152 #define IT87_REG_CHIPID 0x58
154 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
155 #define IN_FROM_REG(val) ((val) * 16)
157 static inline u8 FAN_TO_REG(long rpm, int div)
161 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
162 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
166 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
168 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
169 ((val)+500)/1000),-128,127))
170 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
172 #define ALARMS_FROM_REG(val) (val)
174 #define PWM_TO_REG(val) ((val) >> 1)
175 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
177 static int DIV_TO_REG(int val)
180 while ((val >>= 1) != 0)
184 #define DIV_FROM_REG(val) (1 << (val))
187 /* For each registered IT87, we need to keep some data in memory. That
188 data is pointed to by it87_list[NR]->data. The structure itself is
189 dynamically allocated, at the same time when a new it87 client is
192 struct i2c_client client;
193 struct semaphore lock;
196 struct semaphore update_lock;
197 char valid; /* !=0 if following fields are valid */
198 unsigned long last_updated; /* In jiffies */
200 u8 in[9]; /* Register value */
201 u8 in_max[9]; /* Register value */
202 u8 in_min[9]; /* Register value */
203 u8 fan[3]; /* Register value */
204 u8 fan_min[3]; /* Register value */
205 u8 temp[3]; /* Register value */
206 u8 temp_high[3]; /* Register value */
207 u8 temp_low[3]; /* Register value */
208 u8 sensor; /* Register value */
209 u8 fan_div[3]; /* Register encoding, shifted right */
210 u8 vid; /* Register encoding, combined */
212 u32 alarms; /* Register encoding, combined */
213 u8 fan_main_ctrl; /* Register value */
214 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
218 static int it87_attach_adapter(struct i2c_adapter *adapter);
219 static int it87_find(int *address);
220 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
221 static int it87_detach_client(struct i2c_client *client);
223 static int it87_read_value(struct i2c_client *client, u8 register);
224 static int it87_write_value(struct i2c_client *client, u8 register,
226 static struct it87_data *it87_update_device(struct device *dev);
227 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
230 static struct i2c_driver it87_driver = {
231 .owner = THIS_MODULE,
233 .id = I2C_DRIVERID_IT87,
234 .flags = I2C_DF_NOTIFY,
235 .attach_adapter = it87_attach_adapter,
236 .detach_client = it87_detach_client,
239 static ssize_t show_in(struct device *dev, char *buf, int nr)
241 struct it87_data *data = it87_update_device(dev);
242 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
245 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
247 struct it87_data *data = it87_update_device(dev);
248 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
251 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
253 struct it87_data *data = it87_update_device(dev);
254 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
257 static ssize_t set_in_min(struct device *dev, const char *buf,
258 size_t count, int nr)
260 struct i2c_client *client = to_i2c_client(dev);
261 struct it87_data *data = i2c_get_clientdata(client);
262 unsigned long val = simple_strtoul(buf, NULL, 10);
263 data->in_min[nr] = IN_TO_REG(val);
264 it87_write_value(client, IT87_REG_VIN_MIN(nr),
268 static ssize_t set_in_max(struct device *dev, const char *buf,
269 size_t count, int nr)
271 struct i2c_client *client = to_i2c_client(dev);
272 struct it87_data *data = i2c_get_clientdata(client);
273 unsigned long val = simple_strtoul(buf, NULL, 10);
274 data->in_max[nr] = IN_TO_REG(val);
275 it87_write_value(client, IT87_REG_VIN_MAX(nr),
280 #define show_in_offset(offset) \
282 show_in##offset (struct device *dev, char *buf) \
284 return show_in(dev, buf, offset); \
286 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
288 #define limit_in_offset(offset) \
290 show_in##offset##_min (struct device *dev, char *buf) \
292 return show_in_min(dev, buf, offset); \
295 show_in##offset##_max (struct device *dev, char *buf) \
297 return show_in_max(dev, buf, offset); \
299 static ssize_t set_in##offset##_min (struct device *dev, \
300 const char *buf, size_t count) \
302 return set_in_min(dev, buf, count, offset); \
304 static ssize_t set_in##offset##_max (struct device *dev, \
305 const char *buf, size_t count) \
307 return set_in_max(dev, buf, count, offset); \
309 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
310 show_in##offset##_min, set_in##offset##_min); \
311 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
312 show_in##offset##_max, set_in##offset##_max);
333 static ssize_t show_temp(struct device *dev, char *buf, int nr)
335 struct it87_data *data = it87_update_device(dev);
336 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
338 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
340 struct it87_data *data = it87_update_device(dev);
341 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
343 static ssize_t show_temp_min(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_low[nr]));
348 static ssize_t set_temp_max(struct device *dev, const char *buf,
349 size_t count, int nr)
351 struct i2c_client *client = to_i2c_client(dev);
352 struct it87_data *data = i2c_get_clientdata(client);
353 int val = simple_strtol(buf, NULL, 10);
354 data->temp_high[nr] = TEMP_TO_REG(val);
355 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
358 static ssize_t set_temp_min(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);
364 data->temp_low[nr] = TEMP_TO_REG(val);
365 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
368 #define show_temp_offset(offset) \
369 static ssize_t show_temp_##offset (struct device *dev, char *buf) \
371 return show_temp(dev, buf, offset - 1); \
374 show_temp_##offset##_max (struct device *dev, char *buf) \
376 return show_temp_max(dev, buf, offset - 1); \
379 show_temp_##offset##_min (struct device *dev, char *buf) \
381 return show_temp_min(dev, buf, offset - 1); \
383 static ssize_t set_temp_##offset##_max (struct device *dev, \
384 const char *buf, size_t count) \
386 return set_temp_max(dev, buf, count, offset - 1); \
388 static ssize_t set_temp_##offset##_min (struct device *dev, \
389 const char *buf, size_t count) \
391 return set_temp_min(dev, buf, count, offset - 1); \
393 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
394 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
395 show_temp_##offset##_max, set_temp_##offset##_max); \
396 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
397 show_temp_##offset##_min, set_temp_##offset##_min);
403 static ssize_t show_sensor(struct device *dev, char *buf, int nr)
405 struct it87_data *data = it87_update_device(dev);
406 if (data->sensor & (1 << nr))
407 return sprintf(buf, "3\n"); /* thermal diode */
408 if (data->sensor & (8 << nr))
409 return sprintf(buf, "2\n"); /* thermistor */
410 return sprintf(buf, "0\n"); /* disabled */
412 static ssize_t set_sensor(struct device *dev, const char *buf,
413 size_t count, int nr)
415 struct i2c_client *client = to_i2c_client(dev);
416 struct it87_data *data = i2c_get_clientdata(client);
417 int val = simple_strtol(buf, NULL, 10);
419 data->sensor &= ~(1 << nr);
420 data->sensor &= ~(8 << nr);
421 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
423 data->sensor |= 1 << nr;
425 data->sensor |= 8 << nr;
428 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
431 #define show_sensor_offset(offset) \
432 static ssize_t show_sensor_##offset (struct device *dev, char *buf) \
434 return show_sensor(dev, buf, offset - 1); \
436 static ssize_t set_sensor_##offset (struct device *dev, \
437 const char *buf, size_t count) \
439 return set_sensor(dev, buf, count, offset - 1); \
441 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
442 show_sensor_##offset, set_sensor_##offset);
444 show_sensor_offset(1);
445 show_sensor_offset(2);
446 show_sensor_offset(3);
449 static ssize_t show_fan(struct device *dev, char *buf, int nr)
451 struct it87_data *data = it87_update_device(dev);
452 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
453 DIV_FROM_REG(data->fan_div[nr])));
455 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
457 struct it87_data *data = it87_update_device(dev);
458 return sprintf(buf,"%d\n",
459 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
461 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
463 struct it87_data *data = it87_update_device(dev);
464 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
466 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
468 struct it87_data *data = it87_update_device(dev);
469 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
471 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
473 struct it87_data *data = it87_update_device(dev);
474 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
476 static ssize_t set_fan_min(struct device *dev, const char *buf,
477 size_t count, int nr)
479 struct i2c_client *client = to_i2c_client(dev);
480 struct it87_data *data = i2c_get_clientdata(client);
481 int val = simple_strtol(buf, NULL, 10);
482 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
483 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
486 static ssize_t set_fan_div(struct device *dev, const char *buf,
487 size_t count, int nr)
489 struct i2c_client *client = to_i2c_client(dev);
490 struct it87_data *data = i2c_get_clientdata(client);
491 int val = simple_strtol(buf, NULL, 10);
493 u8 old = it87_read_value(client, IT87_REG_FAN_DIV);
495 for (i = 0; i < 3; i++)
496 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
501 data->fan_div[nr] = DIV_TO_REG(val);
505 data->fan_div[nr] = 1;
507 data->fan_div[nr] = 3;
510 val |= (data->fan_div[0] & 0x07);
511 val |= (data->fan_div[1] & 0x07) << 3;
512 if (data->fan_div[2] == 3)
514 it87_write_value(client, IT87_REG_FAN_DIV, val);
516 for (i = 0; i < 3; i++) {
517 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
518 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
522 static ssize_t set_pwm_enable(struct device *dev, const char *buf,
523 size_t count, int nr)
525 struct i2c_client *client = to_i2c_client(dev);
526 struct it87_data *data = i2c_get_clientdata(client);
527 int val = simple_strtol(buf, NULL, 10);
531 /* make sure the fan is on when in on/off mode */
532 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
533 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
534 /* set on/off mode */
535 data->fan_main_ctrl &= ~(1 << nr);
536 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
537 } else if (val == 1) {
538 /* set SmartGuardian mode */
539 data->fan_main_ctrl |= (1 << nr);
540 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
541 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
542 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
548 static ssize_t set_pwm(struct device *dev, const char *buf,
549 size_t count, int nr)
551 struct i2c_client *client = to_i2c_client(dev);
552 struct it87_data *data = i2c_get_clientdata(client);
553 int val = simple_strtol(buf, NULL, 10);
555 if (val < 0 || val > 255)
558 data->manual_pwm_ctl[nr] = val;
559 if (data->fan_main_ctrl & (1 << nr))
560 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
565 #define show_fan_offset(offset) \
566 static ssize_t show_fan_##offset (struct device *dev, char *buf) \
568 return show_fan(dev, buf, offset - 1); \
570 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
572 return show_fan_min(dev, buf, offset - 1); \
574 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
576 return show_fan_div(dev, buf, offset - 1); \
578 static ssize_t set_fan_##offset##_min (struct device *dev, \
579 const char *buf, size_t count) \
581 return set_fan_min(dev, buf, count, offset - 1); \
583 static ssize_t set_fan_##offset##_div (struct device *dev, \
584 const char *buf, size_t count) \
586 return set_fan_div(dev, buf, count, offset - 1); \
588 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
589 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
590 show_fan_##offset##_min, set_fan_##offset##_min); \
591 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
592 show_fan_##offset##_div, set_fan_##offset##_div);
598 #define show_pwm_offset(offset) \
599 static ssize_t show_pwm##offset##_enable (struct device *dev, \
602 return show_pwm_enable(dev, buf, offset - 1); \
604 static ssize_t show_pwm##offset (struct device *dev, char *buf) \
606 return show_pwm(dev, buf, offset - 1); \
608 static ssize_t set_pwm##offset##_enable (struct device *dev, \
609 const char *buf, size_t count) \
611 return set_pwm_enable(dev, buf, count, offset - 1); \
613 static ssize_t set_pwm##offset (struct device *dev, \
614 const char *buf, size_t count) \
616 return set_pwm(dev, buf, count, offset - 1); \
618 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
619 show_pwm##offset##_enable, \
620 set_pwm##offset##_enable); \
621 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
622 show_pwm##offset , set_pwm##offset );
629 static ssize_t show_alarms(struct device *dev, char *buf)
631 struct it87_data *data = it87_update_device(dev);
632 return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
634 static DEVICE_ATTR(alarms, S_IRUGO | S_IWUSR, show_alarms, NULL);
637 show_vrm_reg(struct device *dev, char *buf)
639 struct it87_data *data = it87_update_device(dev);
640 return sprintf(buf, "%ld\n", (long) data->vrm);
643 store_vrm_reg(struct device *dev, const char *buf, size_t count)
645 struct i2c_client *client = to_i2c_client(dev);
646 struct it87_data *data = i2c_get_clientdata(client);
649 val = simple_strtoul(buf, NULL, 10);
654 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
655 #define device_create_file_vrm(client) \
656 device_create_file(&client->dev, &dev_attr_vrm)
659 show_vid_reg(struct device *dev, char *buf)
661 struct it87_data *data = it87_update_device(dev);
662 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
664 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
665 #define device_create_file_vid(client) \
666 device_create_file(&client->dev, &dev_attr_cpu0_vid)
668 /* This function is called when:
669 * it87_driver is inserted (when this module is loaded), for each
671 * when a new adapter is inserted (and it87_driver is still present) */
672 static int it87_attach_adapter(struct i2c_adapter *adapter)
674 if (!(adapter->class & I2C_CLASS_HWMON))
676 return i2c_detect(adapter, &addr_data, it87_detect);
679 /* SuperIO detection - will change normal_isa[0] if a chip is found */
680 static int it87_find(int *address)
685 chip_type = superio_inw(DEVID);
686 if (chip_type != IT8712F_DEVID
687 && chip_type != IT8705F_DEVID)
691 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
692 pr_info("it87: Device not activated, skipping\n");
696 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
698 pr_info("it87: Base address not set, skipping\n");
703 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
704 chip_type, *address, superio_inb(DEVREV) & 0x0f);
711 /* This function is called by i2c_detect */
712 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
715 struct i2c_client *new_client;
716 struct it87_data *data;
718 const char *name = "";
719 int is_isa = i2c_is_isa_adapter(adapter);
720 int enable_pwm_interface;
724 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
727 /* Reserve the ISA region */
729 if (!request_region(address, IT87_EXTENT, it87_driver.name))
732 /* Probe whether there is anything available on this address. Already
733 done for SMBus clients */
737 #define REALLY_SLOW_IO
738 /* We need the timeouts for at least some IT87-like chips. But only
739 if we read 'undefined' registers. */
740 i = inb_p(address + 1);
741 if (inb_p(address + 2) != i
742 || inb_p(address + 3) != i
743 || inb_p(address + 7) != i) {
747 #undef REALLY_SLOW_IO
749 /* Let's just hope nothing breaks here */
750 i = inb_p(address + 5) & 0x7f;
751 outb_p(~i & 0x7f, address + 5);
752 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
753 outb_p(i, address + 5);
760 /* OK. For now, we presume we have a valid client. We now create the
761 client structure, even though we cannot fill it completely yet.
762 But it allows us to access it87_{read,write}_value. */
764 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
768 memset(data, 0, sizeof(struct it87_data));
770 new_client = &data->client;
772 init_MUTEX(&data->lock);
773 i2c_set_clientdata(new_client, data);
774 new_client->addr = address;
775 new_client->adapter = adapter;
776 new_client->driver = &it87_driver;
777 new_client->flags = 0;
779 /* Now, we do the remaining detection. */
782 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
784 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
790 /* Determine the chip type. */
792 i = it87_read_value(new_client, IT87_REG_CHIPID);
795 if ((is_isa) && (chip_type == IT8712F_DEVID))
800 dev_info(&adapter->dev,
801 "Ignoring 'force' parameter for unknown chip at "
802 "adapter %d, address 0x%02x\n",
803 i2c_adapter_id(adapter), address);
811 } else if (kind == it8712) {
815 /* Fill in the remaining client fields and put it into the global list */
816 strlcpy(new_client->name, name, I2C_NAME_SIZE);
819 init_MUTEX(&data->update_lock);
821 /* Tell the I2C layer a new client has arrived */
822 if ((err = i2c_attach_client(new_client)))
825 /* Initialize the IT87 chip */
826 it87_init_client(new_client, data);
828 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
829 * and polarity set to active low is sign that this is the case so we
830 * disable pwm control to protect the user. */
831 enable_pwm_interface = 1;
832 tmp = it87_read_value(new_client, IT87_REG_FAN_CTL);
833 if ((tmp & 0x87) == 0) {
834 enable_pwm_interface = 0;
835 dev_info(&new_client->dev,
836 "detected broken BIOS defaults, disabling pwm interface");
839 /* Register sysfs hooks */
840 device_create_file(&new_client->dev, &dev_attr_in0_input);
841 device_create_file(&new_client->dev, &dev_attr_in1_input);
842 device_create_file(&new_client->dev, &dev_attr_in2_input);
843 device_create_file(&new_client->dev, &dev_attr_in3_input);
844 device_create_file(&new_client->dev, &dev_attr_in4_input);
845 device_create_file(&new_client->dev, &dev_attr_in5_input);
846 device_create_file(&new_client->dev, &dev_attr_in6_input);
847 device_create_file(&new_client->dev, &dev_attr_in7_input);
848 device_create_file(&new_client->dev, &dev_attr_in8_input);
849 device_create_file(&new_client->dev, &dev_attr_in0_min);
850 device_create_file(&new_client->dev, &dev_attr_in1_min);
851 device_create_file(&new_client->dev, &dev_attr_in2_min);
852 device_create_file(&new_client->dev, &dev_attr_in3_min);
853 device_create_file(&new_client->dev, &dev_attr_in4_min);
854 device_create_file(&new_client->dev, &dev_attr_in5_min);
855 device_create_file(&new_client->dev, &dev_attr_in6_min);
856 device_create_file(&new_client->dev, &dev_attr_in7_min);
857 device_create_file(&new_client->dev, &dev_attr_in0_max);
858 device_create_file(&new_client->dev, &dev_attr_in1_max);
859 device_create_file(&new_client->dev, &dev_attr_in2_max);
860 device_create_file(&new_client->dev, &dev_attr_in3_max);
861 device_create_file(&new_client->dev, &dev_attr_in4_max);
862 device_create_file(&new_client->dev, &dev_attr_in5_max);
863 device_create_file(&new_client->dev, &dev_attr_in6_max);
864 device_create_file(&new_client->dev, &dev_attr_in7_max);
865 device_create_file(&new_client->dev, &dev_attr_temp1_input);
866 device_create_file(&new_client->dev, &dev_attr_temp2_input);
867 device_create_file(&new_client->dev, &dev_attr_temp3_input);
868 device_create_file(&new_client->dev, &dev_attr_temp1_max);
869 device_create_file(&new_client->dev, &dev_attr_temp2_max);
870 device_create_file(&new_client->dev, &dev_attr_temp3_max);
871 device_create_file(&new_client->dev, &dev_attr_temp1_min);
872 device_create_file(&new_client->dev, &dev_attr_temp2_min);
873 device_create_file(&new_client->dev, &dev_attr_temp3_min);
874 device_create_file(&new_client->dev, &dev_attr_temp1_type);
875 device_create_file(&new_client->dev, &dev_attr_temp2_type);
876 device_create_file(&new_client->dev, &dev_attr_temp3_type);
877 device_create_file(&new_client->dev, &dev_attr_fan1_input);
878 device_create_file(&new_client->dev, &dev_attr_fan2_input);
879 device_create_file(&new_client->dev, &dev_attr_fan3_input);
880 device_create_file(&new_client->dev, &dev_attr_fan1_min);
881 device_create_file(&new_client->dev, &dev_attr_fan2_min);
882 device_create_file(&new_client->dev, &dev_attr_fan3_min);
883 device_create_file(&new_client->dev, &dev_attr_fan1_div);
884 device_create_file(&new_client->dev, &dev_attr_fan2_div);
885 device_create_file(&new_client->dev, &dev_attr_fan3_div);
886 device_create_file(&new_client->dev, &dev_attr_alarms);
887 if (enable_pwm_interface) {
888 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
889 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
890 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
891 device_create_file(&new_client->dev, &dev_attr_pwm1);
892 device_create_file(&new_client->dev, &dev_attr_pwm2);
893 device_create_file(&new_client->dev, &dev_attr_pwm3);
896 if (data->type == it8712) {
897 device_create_file_vrm(new_client);
898 device_create_file_vid(new_client);
899 data->vrm = i2c_which_vrm();
908 release_region(address, IT87_EXTENT);
913 static int it87_detach_client(struct i2c_client *client)
917 if ((err = i2c_detach_client(client))) {
918 dev_err(&client->dev,
919 "Client deregistration failed, client not detached.\n");
923 if(i2c_is_isa_client(client))
924 release_region(client->addr, IT87_EXTENT);
925 kfree(i2c_get_clientdata(client));
930 /* The SMBus locks itself, but ISA access must be locked explicitely!
931 We don't want to lock the whole ISA bus, so we lock each client
933 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
934 would slow down the IT87 access and should not be necessary. */
935 static int it87_read_value(struct i2c_client *client, u8 reg)
937 struct it87_data *data = i2c_get_clientdata(client);
940 if (i2c_is_isa_client(client)) {
942 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
943 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
947 return i2c_smbus_read_byte_data(client, reg);
950 /* The SMBus locks itself, but ISA access muse be locked explicitely!
951 We don't want to lock the whole ISA bus, so we lock each client
953 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
954 would slow down the IT87 access and should not be necessary. */
955 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
957 struct it87_data *data = i2c_get_clientdata(client);
959 if (i2c_is_isa_client(client)) {
961 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
962 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
966 return i2c_smbus_write_byte_data(client, reg, value);
969 /* Called when we have found a new IT87. */
970 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
974 /* initialize to sane defaults:
975 * - if the chip is in manual pwm mode, this will be overwritten with
976 * the actual settings on the chip (so in this case, initialization
978 * - if in automatic or on/off mode, we could switch to manual mode,
979 * read the registers and set manual_pwm_ctl accordingly, but currently
980 * this is not implemented, so we initialize to something sane */
981 for (i = 0; i < 3; i++) {
982 data->manual_pwm_ctl[i] = 0xff;
985 /* Check if temperature channnels are reset manually or by some reason */
986 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
987 if ((tmp & 0x3f) == 0) {
988 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
989 tmp = (tmp & 0xc0) | 0x2a;
990 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
994 /* Check if voltage monitors are reset manually or by some reason */
995 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
996 if ((tmp & 0xff) == 0) {
997 /* Enable all voltage monitors */
998 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1001 /* Check if tachometers are reset manually or by some reason */
1002 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1003 if ((data->fan_main_ctrl & 0x70) == 0) {
1004 /* Enable all fan tachometers */
1005 data->fan_main_ctrl |= 0x70;
1006 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1009 /* Set current fan mode registers and the default settings for the
1010 * other mode registers */
1011 for (i = 0; i < 3; i++) {
1012 if (data->fan_main_ctrl & (1 << i)) {
1014 tmp = it87_read_value(client, IT87_REG_PWM(i));
1016 /* automatic pwm - not yet implemented, but
1017 * leave the settings made by the BIOS alone
1018 * until a change is requested via the sysfs
1022 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1027 /* Start monitoring */
1028 it87_write_value(client, IT87_REG_CONFIG,
1029 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1030 | (update_vbat ? 0x41 : 0x01));
1033 static struct it87_data *it87_update_device(struct device *dev)
1035 struct i2c_client *client = to_i2c_client(dev);
1036 struct it87_data *data = i2c_get_clientdata(client);
1039 down(&data->update_lock);
1041 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
1042 (jiffies < data->last_updated) || !data->valid) {
1045 /* Cleared after each update, so reenable. Value
1046 returned by this read will be previous value */
1047 it87_write_value(client, IT87_REG_CONFIG,
1048 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1050 for (i = 0; i <= 7; i++) {
1052 it87_read_value(client, IT87_REG_VIN(i));
1054 it87_read_value(client, IT87_REG_VIN_MIN(i));
1056 it87_read_value(client, IT87_REG_VIN_MAX(i));
1059 it87_read_value(client, IT87_REG_VIN(8));
1060 /* Temperature sensor doesn't have limit registers, set
1061 to min and max value */
1062 data->in_min[8] = 0;
1063 data->in_max[8] = 255;
1065 for (i = 0; i < 3; i++) {
1067 it87_read_value(client, IT87_REG_FAN(i));
1069 it87_read_value(client, IT87_REG_FAN_MIN(i));
1071 for (i = 0; i < 3; i++) {
1073 it87_read_value(client, IT87_REG_TEMP(i));
1074 data->temp_high[i] =
1075 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1077 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1080 /* The 8705 does not have VID capability */
1083 i = it87_read_value(client, IT87_REG_FAN_DIV);
1084 data->fan_div[0] = i & 0x07;
1085 data->fan_div[1] = (i >> 3) & 0x07;
1086 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1089 it87_read_value(client, IT87_REG_ALARM1) |
1090 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1091 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1092 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1094 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1095 /* The 8705 does not have VID capability */
1096 if (data->type == it8712) {
1097 data->vid = it87_read_value(client, IT87_REG_VID);
1100 data->last_updated = jiffies;
1104 up(&data->update_lock);
1109 static int __init sm_it87_init(void)
1113 if (!it87_find(&addr)) {
1114 normal_isa[0] = addr;
1116 return i2c_add_driver(&it87_driver);
1119 static void __exit sm_it87_exit(void)
1121 i2c_del_driver(&it87_driver);
1125 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1126 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1127 module_param(update_vbat, bool, 0);
1128 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1129 MODULE_LICENSE("GPL");
1131 module_init(sm_it87_init);
1132 module_exit(sm_it87_exit);