2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
5 Supports: IT8705F Super I/O chip w/LPC interface
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[] = { I2C_CLIENT_END };
46 static unsigned short normal_i2c_range[] = { 0x20, 0x2f, I2C_CLIENT_END };
47 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
48 static unsigned int normal_isa_range[] = { 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 */
60 superio_outb(int reg, int val)
96 /* just IT8712F for now - this should be extended to support the other
98 #define IT8712F_DEVID 0x8712
99 #define IT87_ACT_REG 0x30
100 #define IT87_BASE_REG 0x60
102 /* Update battery voltage after every reading if true */
103 static int update_vbat;
105 /* Reset the registers on init if true */
108 /* Many IT87 constants specified below */
110 /* Length of ISA address segment */
111 #define IT87_EXTENT 8
113 /* Where are the ISA address/data registers relative to the base address */
114 #define IT87_ADDR_REG_OFFSET 5
115 #define IT87_DATA_REG_OFFSET 6
117 /*----- The IT87 registers -----*/
119 #define IT87_REG_CONFIG 0x00
121 #define IT87_REG_ALARM1 0x01
122 #define IT87_REG_ALARM2 0x02
123 #define IT87_REG_ALARM3 0x03
125 #define IT87_REG_VID 0x0a
126 #define IT87_REG_FAN_DIV 0x0b
128 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
130 #define IT87_REG_FAN(nr) (0x0d + (nr))
131 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
132 #define IT87_REG_FAN_MAIN_CTRL 0x13
134 #define IT87_REG_VIN(nr) (0x20 + (nr))
135 #define IT87_REG_TEMP(nr) (0x29 + (nr))
137 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
138 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
139 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
140 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
142 #define IT87_REG_I2C_ADDR 0x48
144 #define IT87_REG_VIN_ENABLE 0x50
145 #define IT87_REG_TEMP_ENABLE 0x51
147 #define IT87_REG_CHIPID 0x58
149 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
150 #define IN_FROM_REG(val) ((val) * 16)
152 static inline u8 FAN_TO_REG(long rpm, int div)
156 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
157 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
161 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
163 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
164 ((val)+500)/1000),-128,127))
165 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
167 #define ALARMS_FROM_REG(val) (val)
169 static int DIV_TO_REG(int val)
172 while ((val >>= 1) != 0)
176 #define DIV_FROM_REG(val) (1 << (val))
179 /* For each registered IT87, we need to keep some data in memory. That
180 data is pointed to by it87_list[NR]->data. The structure itself is
181 dynamically allocated, at the same time when a new it87 client is
184 struct i2c_client client;
185 struct semaphore lock;
188 struct semaphore update_lock;
189 char valid; /* !=0 if following fields are valid */
190 unsigned long last_updated; /* In jiffies */
192 u8 in[9]; /* Register value */
193 u8 in_max[9]; /* Register value */
194 u8 in_min[9]; /* Register value */
195 u8 fan[3]; /* Register value */
196 u8 fan_min[3]; /* Register value */
197 u8 temp[3]; /* Register value */
198 u8 temp_high[3]; /* Register value */
199 u8 temp_low[3]; /* Register value */
200 u8 sensor; /* Register value */
201 u8 fan_div[3]; /* Register encoding, shifted right */
202 u8 vid; /* Register encoding, combined */
204 u32 alarms; /* Register encoding, combined */
208 static int it87_attach_adapter(struct i2c_adapter *adapter);
209 static int it87_find(int *address);
210 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
211 static int it87_detach_client(struct i2c_client *client);
213 static int it87_read_value(struct i2c_client *client, u8 register);
214 static int it87_write_value(struct i2c_client *client, u8 register,
216 static struct it87_data *it87_update_device(struct device *dev);
217 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
220 static struct i2c_driver it87_driver = {
221 .owner = THIS_MODULE,
223 .id = I2C_DRIVERID_IT87,
224 .flags = I2C_DF_NOTIFY,
225 .attach_adapter = it87_attach_adapter,
226 .detach_client = it87_detach_client,
229 static int it87_id = 0;
231 static ssize_t show_in(struct device *dev, char *buf, int nr)
233 struct it87_data *data = it87_update_device(dev);
234 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
237 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
239 struct it87_data *data = it87_update_device(dev);
240 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
243 static ssize_t show_in_max(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_max[nr]));
249 static ssize_t set_in_min(struct device *dev, const char *buf,
250 size_t count, int nr)
252 struct i2c_client *client = to_i2c_client(dev);
253 struct it87_data *data = i2c_get_clientdata(client);
254 unsigned long val = simple_strtoul(buf, NULL, 10);
255 data->in_min[nr] = IN_TO_REG(val);
256 it87_write_value(client, IT87_REG_VIN_MIN(nr),
260 static ssize_t set_in_max(struct device *dev, const char *buf,
261 size_t count, int nr)
263 struct i2c_client *client = to_i2c_client(dev);
264 struct it87_data *data = i2c_get_clientdata(client);
265 unsigned long val = simple_strtoul(buf, NULL, 10);
266 data->in_max[nr] = IN_TO_REG(val);
267 it87_write_value(client, IT87_REG_VIN_MAX(nr),
272 #define show_in_offset(offset) \
274 show_in##offset (struct device *dev, char *buf) \
276 return show_in(dev, buf, 0x##offset); \
278 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
280 #define limit_in_offset(offset) \
282 show_in##offset##_min (struct device *dev, char *buf) \
284 return show_in_min(dev, buf, 0x##offset); \
287 show_in##offset##_max (struct device *dev, char *buf) \
289 return show_in_max(dev, buf, 0x##offset); \
291 static ssize_t set_in##offset##_min (struct device *dev, \
292 const char *buf, size_t count) \
294 return set_in_min(dev, buf, count, 0x##offset); \
296 static ssize_t set_in##offset##_max (struct device *dev, \
297 const char *buf, size_t count) \
299 return set_in_max(dev, buf, count, 0x##offset); \
301 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
302 show_in##offset##_min, set_in##offset##_min); \
303 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
304 show_in##offset##_max, set_in##offset##_max);
325 static ssize_t show_temp(struct device *dev, char *buf, int nr)
327 struct it87_data *data = it87_update_device(dev);
328 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
330 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
332 struct it87_data *data = it87_update_device(dev);
333 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
335 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
337 struct it87_data *data = it87_update_device(dev);
338 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
340 static ssize_t set_temp_max(struct device *dev, const char *buf,
341 size_t count, int nr)
343 struct i2c_client *client = to_i2c_client(dev);
344 struct it87_data *data = i2c_get_clientdata(client);
345 int val = simple_strtol(buf, NULL, 10);
346 data->temp_high[nr] = TEMP_TO_REG(val);
347 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
350 static ssize_t set_temp_min(struct device *dev, const char *buf,
351 size_t count, int nr)
353 struct i2c_client *client = to_i2c_client(dev);
354 struct it87_data *data = i2c_get_clientdata(client);
355 int val = simple_strtol(buf, NULL, 10);
356 data->temp_low[nr] = TEMP_TO_REG(val);
357 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
360 #define show_temp_offset(offset) \
361 static ssize_t show_temp_##offset (struct device *dev, char *buf) \
363 return show_temp(dev, buf, 0x##offset - 1); \
366 show_temp_##offset##_max (struct device *dev, char *buf) \
368 return show_temp_max(dev, buf, 0x##offset - 1); \
371 show_temp_##offset##_min (struct device *dev, char *buf) \
373 return show_temp_min(dev, buf, 0x##offset - 1); \
375 static ssize_t set_temp_##offset##_max (struct device *dev, \
376 const char *buf, size_t count) \
378 return set_temp_max(dev, buf, count, 0x##offset - 1); \
380 static ssize_t set_temp_##offset##_min (struct device *dev, \
381 const char *buf, size_t count) \
383 return set_temp_min(dev, buf, count, 0x##offset - 1); \
385 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
386 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
387 show_temp_##offset##_max, set_temp_##offset##_max); \
388 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
389 show_temp_##offset##_min, set_temp_##offset##_min);
395 static ssize_t show_sensor(struct device *dev, char *buf, int nr)
397 struct it87_data *data = it87_update_device(dev);
398 if (data->sensor & (1 << nr))
399 return sprintf(buf, "3\n"); /* thermal diode */
400 if (data->sensor & (8 << nr))
401 return sprintf(buf, "2\n"); /* thermistor */
402 return sprintf(buf, "0\n"); /* disabled */
404 static ssize_t set_sensor(struct device *dev, const char *buf,
405 size_t count, int nr)
407 struct i2c_client *client = to_i2c_client(dev);
408 struct it87_data *data = i2c_get_clientdata(client);
409 int val = simple_strtol(buf, NULL, 10);
411 data->sensor &= ~(1 << nr);
412 data->sensor &= ~(8 << nr);
413 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
415 data->sensor |= 1 << nr;
417 data->sensor |= 8 << nr;
420 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
423 #define show_sensor_offset(offset) \
424 static ssize_t show_sensor_##offset (struct device *dev, char *buf) \
426 return show_sensor(dev, buf, 0x##offset - 1); \
428 static ssize_t set_sensor_##offset (struct device *dev, \
429 const char *buf, size_t count) \
431 return set_sensor(dev, buf, count, 0x##offset - 1); \
433 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
434 show_sensor_##offset, set_sensor_##offset);
436 show_sensor_offset(1);
437 show_sensor_offset(2);
438 show_sensor_offset(3);
441 static ssize_t show_fan(struct device *dev, char *buf, int nr)
443 struct it87_data *data = it87_update_device(dev);
444 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
445 DIV_FROM_REG(data->fan_div[nr])) );
447 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
449 struct it87_data *data = it87_update_device(dev);
450 return sprintf(buf,"%d\n",
451 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])) );
453 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
455 struct it87_data *data = it87_update_device(dev);
456 return sprintf(buf,"%d\n", DIV_FROM_REG(data->fan_div[nr]) );
458 static ssize_t set_fan_min(struct device *dev, const char *buf,
459 size_t count, int nr)
461 struct i2c_client *client = to_i2c_client(dev);
462 struct it87_data *data = i2c_get_clientdata(client);
463 int val = simple_strtol(buf, NULL, 10);
464 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
465 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
468 static ssize_t set_fan_div(struct device *dev, const char *buf,
469 size_t count, int nr)
471 struct i2c_client *client = to_i2c_client(dev);
472 struct it87_data *data = i2c_get_clientdata(client);
473 int val = simple_strtol(buf, NULL, 10);
475 u8 old = it87_read_value(client, IT87_REG_FAN_DIV);
477 for (i = 0; i < 3; i++)
478 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
483 data->fan_div[nr] = DIV_TO_REG(val);
487 data->fan_div[nr] = 1;
489 data->fan_div[nr] = 3;
492 val |= (data->fan_div[0] & 0x07);
493 val |= (data->fan_div[1] & 0x07) << 3;
494 if (data->fan_div[2] == 3)
496 it87_write_value(client, IT87_REG_FAN_DIV, val);
498 for (i = 0; i < 3; i++) {
499 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
500 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
505 #define show_fan_offset(offset) \
506 static ssize_t show_fan_##offset (struct device *dev, char *buf) \
508 return show_fan(dev, buf, 0x##offset - 1); \
510 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
512 return show_fan_min(dev, buf, 0x##offset - 1); \
514 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
516 return show_fan_div(dev, buf, 0x##offset - 1); \
518 static ssize_t set_fan_##offset##_min (struct device *dev, \
519 const char *buf, size_t count) \
521 return set_fan_min(dev, buf, count, 0x##offset - 1); \
523 static ssize_t set_fan_##offset##_div (struct device *dev, \
524 const char *buf, size_t count) \
526 return set_fan_div(dev, buf, count, 0x##offset - 1); \
528 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
529 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
530 show_fan_##offset##_min, set_fan_##offset##_min); \
531 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
532 show_fan_##offset##_div, set_fan_##offset##_div);
539 static ssize_t show_alarms(struct device *dev, char *buf)
541 struct it87_data *data = it87_update_device(dev);
542 return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
544 static DEVICE_ATTR(alarms, S_IRUGO | S_IWUSR, show_alarms, NULL);
547 show_vrm_reg(struct device *dev, char *buf)
549 struct it87_data *data = it87_update_device(dev);
550 return sprintf(buf, "%ld\n", (long) data->vrm);
553 store_vrm_reg(struct device *dev, const char *buf, size_t count)
555 struct i2c_client *client = to_i2c_client(dev);
556 struct it87_data *data = i2c_get_clientdata(client);
559 val = simple_strtoul(buf, NULL, 10);
564 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
565 #define device_create_file_vrm(client) \
566 device_create_file(&client->dev, &dev_attr_vrm)
569 show_vid_reg(struct device *dev, char *buf)
571 struct it87_data *data = it87_update_device(dev);
572 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
574 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
575 #define device_create_file_vid(client) \
576 device_create_file(&client->dev, &dev_attr_cpu0_vid)
578 /* This function is called when:
579 * it87_driver is inserted (when this module is loaded), for each
581 * when a new adapter is inserted (and it87_driver is still present) */
582 static int it87_attach_adapter(struct i2c_adapter *adapter)
584 if (!(adapter->class & I2C_CLASS_HWMON))
586 return i2c_detect(adapter, &addr_data, it87_detect);
589 /* SuperIO detection - will change normal_isa[0] if a chip is found */
590 static int it87_find(int *address)
595 val = (superio_inb(DEVID) << 8) |
596 superio_inb(DEVID + 1);
597 if (val != IT8712F_DEVID) {
603 val = (superio_inb(IT87_BASE_REG) << 8) |
604 superio_inb(IT87_BASE_REG + 1);
606 *address = val & ~(IT87_EXTENT - 1);
613 /* This function is called by i2c_detect */
614 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
617 struct i2c_client *new_client;
618 struct it87_data *data;
620 const char *name = "";
621 int is_isa = i2c_is_isa_adapter(adapter);
624 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
627 /* Reserve the ISA region */
629 if (!request_region(address, IT87_EXTENT, name))
632 /* Probe whether there is anything available on this address. Already
633 done for SMBus clients */
637 #define REALLY_SLOW_IO
638 /* We need the timeouts for at least some IT87-like chips. But only
639 if we read 'undefined' registers. */
640 i = inb_p(address + 1);
641 if (inb_p(address + 2) != i
642 || inb_p(address + 3) != i
643 || inb_p(address + 7) != i) {
647 #undef REALLY_SLOW_IO
649 /* Let's just hope nothing breaks here */
650 i = inb_p(address + 5) & 0x7f;
651 outb_p(~i & 0x7f, address + 5);
652 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
653 outb_p(i, address + 5);
660 /* OK. For now, we presume we have a valid client. We now create the
661 client structure, even though we cannot fill it completely yet.
662 But it allows us to access it87_{read,write}_value. */
664 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
668 memset(data, 0, sizeof(struct it87_data));
670 new_client = &data->client;
672 init_MUTEX(&data->lock);
673 i2c_set_clientdata(new_client, data);
674 new_client->addr = address;
675 new_client->adapter = adapter;
676 new_client->driver = &it87_driver;
677 new_client->flags = 0;
679 /* Now, we do the remaining detection. */
682 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
684 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
690 /* Determine the chip type. */
692 i = it87_read_value(new_client, IT87_REG_CHIPID);
696 val = (superio_inb(DEVID) << 8) |
697 superio_inb(DEVID + 1);
698 if (val == IT8712F_DEVID) kind = it8712;
702 dev_info(&adapter->dev,
703 "Ignoring 'force' parameter for unknown chip at "
704 "adapter %d, address 0x%02x\n",
705 i2c_adapter_id(adapter), address);
713 } else if (kind == it8712) {
717 /* Fill in the remaining client fields and put it into the global list */
718 strlcpy(new_client->name, name, I2C_NAME_SIZE);
722 new_client->id = it87_id++;
724 init_MUTEX(&data->update_lock);
726 /* Tell the I2C layer a new client has arrived */
727 if ((err = i2c_attach_client(new_client)))
730 /* Initialize the IT87 chip */
731 it87_init_client(new_client, data);
733 /* Register sysfs hooks */
734 device_create_file(&new_client->dev, &dev_attr_in0_input);
735 device_create_file(&new_client->dev, &dev_attr_in1_input);
736 device_create_file(&new_client->dev, &dev_attr_in2_input);
737 device_create_file(&new_client->dev, &dev_attr_in3_input);
738 device_create_file(&new_client->dev, &dev_attr_in4_input);
739 device_create_file(&new_client->dev, &dev_attr_in5_input);
740 device_create_file(&new_client->dev, &dev_attr_in6_input);
741 device_create_file(&new_client->dev, &dev_attr_in7_input);
742 device_create_file(&new_client->dev, &dev_attr_in8_input);
743 device_create_file(&new_client->dev, &dev_attr_in0_min);
744 device_create_file(&new_client->dev, &dev_attr_in1_min);
745 device_create_file(&new_client->dev, &dev_attr_in2_min);
746 device_create_file(&new_client->dev, &dev_attr_in3_min);
747 device_create_file(&new_client->dev, &dev_attr_in4_min);
748 device_create_file(&new_client->dev, &dev_attr_in5_min);
749 device_create_file(&new_client->dev, &dev_attr_in6_min);
750 device_create_file(&new_client->dev, &dev_attr_in7_min);
751 device_create_file(&new_client->dev, &dev_attr_in0_max);
752 device_create_file(&new_client->dev, &dev_attr_in1_max);
753 device_create_file(&new_client->dev, &dev_attr_in2_max);
754 device_create_file(&new_client->dev, &dev_attr_in3_max);
755 device_create_file(&new_client->dev, &dev_attr_in4_max);
756 device_create_file(&new_client->dev, &dev_attr_in5_max);
757 device_create_file(&new_client->dev, &dev_attr_in6_max);
758 device_create_file(&new_client->dev, &dev_attr_in7_max);
759 device_create_file(&new_client->dev, &dev_attr_temp1_input);
760 device_create_file(&new_client->dev, &dev_attr_temp2_input);
761 device_create_file(&new_client->dev, &dev_attr_temp3_input);
762 device_create_file(&new_client->dev, &dev_attr_temp1_max);
763 device_create_file(&new_client->dev, &dev_attr_temp2_max);
764 device_create_file(&new_client->dev, &dev_attr_temp3_max);
765 device_create_file(&new_client->dev, &dev_attr_temp1_min);
766 device_create_file(&new_client->dev, &dev_attr_temp2_min);
767 device_create_file(&new_client->dev, &dev_attr_temp3_min);
768 device_create_file(&new_client->dev, &dev_attr_temp1_type);
769 device_create_file(&new_client->dev, &dev_attr_temp2_type);
770 device_create_file(&new_client->dev, &dev_attr_temp3_type);
771 device_create_file(&new_client->dev, &dev_attr_fan1_input);
772 device_create_file(&new_client->dev, &dev_attr_fan2_input);
773 device_create_file(&new_client->dev, &dev_attr_fan3_input);
774 device_create_file(&new_client->dev, &dev_attr_fan1_min);
775 device_create_file(&new_client->dev, &dev_attr_fan2_min);
776 device_create_file(&new_client->dev, &dev_attr_fan3_min);
777 device_create_file(&new_client->dev, &dev_attr_fan1_div);
778 device_create_file(&new_client->dev, &dev_attr_fan2_div);
779 device_create_file(&new_client->dev, &dev_attr_fan3_div);
780 device_create_file(&new_client->dev, &dev_attr_alarms);
782 if (data->type == it8712) {
783 device_create_file_vrm(new_client);
784 device_create_file_vid(new_client);
785 data->vrm = i2c_which_vrm();
794 release_region(address, IT87_EXTENT);
799 static int it87_detach_client(struct i2c_client *client)
803 if ((err = i2c_detach_client(client))) {
804 dev_err(&client->dev,
805 "Client deregistration failed, client not detached.\n");
809 if(i2c_is_isa_client(client))
810 release_region(client->addr, IT87_EXTENT);
811 kfree(i2c_get_clientdata(client));
816 /* The SMBus locks itself, but ISA access must be locked explicitely!
817 We don't want to lock the whole ISA bus, so we lock each client
819 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
820 would slow down the IT87 access and should not be necessary. */
821 static int it87_read_value(struct i2c_client *client, u8 reg)
823 struct it87_data *data = i2c_get_clientdata(client);
826 if (i2c_is_isa_client(client)) {
828 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
829 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
833 return i2c_smbus_read_byte_data(client, reg);
836 /* The SMBus locks itself, but ISA access muse be locked explicitely!
837 We don't want to lock the whole ISA bus, so we lock each client
839 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
840 would slow down the IT87 access and should not be necessary. */
841 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
843 struct it87_data *data = i2c_get_clientdata(client);
845 if (i2c_is_isa_client(client)) {
847 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
848 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
852 return i2c_smbus_write_byte_data(client, reg, value);
855 /* Called when we have found a new IT87. */
856 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
861 /* Reset all except Watchdog values and last conversion values
862 This sets fan-divs to 2, among others */
863 it87_write_value(client, IT87_REG_CONFIG, 0x80);
866 /* Check if temperature channnels are reset manually or by some reason */
867 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
868 if ((tmp & 0x3f) == 0) {
869 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
870 tmp = (tmp & 0xc0) | 0x2a;
871 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
875 /* Check if voltage monitors are reset manually or by some reason */
876 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
877 if ((tmp & 0xff) == 0) {
878 /* Enable all voltage monitors */
879 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
882 /* Check if tachometers are reset manually or by some reason */
883 tmp = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
884 if ((tmp & 0x70) == 0) {
885 /* Enable all fan tachometers */
886 tmp = (tmp & 0x8f) | 0x70;
887 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, tmp);
890 /* Start monitoring */
891 it87_write_value(client, IT87_REG_CONFIG,
892 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
893 | (update_vbat ? 0x41 : 0x01));
896 static struct it87_data *it87_update_device(struct device *dev)
898 struct i2c_client *client = to_i2c_client(dev);
899 struct it87_data *data = i2c_get_clientdata(client);
902 down(&data->update_lock);
904 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
905 (jiffies < data->last_updated) || !data->valid) {
908 /* Cleared after each update, so reenable. Value
909 returned by this read will be previous value */
910 it87_write_value(client, IT87_REG_CONFIG,
911 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
913 for (i = 0; i <= 7; i++) {
915 it87_read_value(client, IT87_REG_VIN(i));
917 it87_read_value(client, IT87_REG_VIN_MIN(i));
919 it87_read_value(client, IT87_REG_VIN_MAX(i));
922 it87_read_value(client, IT87_REG_VIN(8));
923 /* Temperature sensor doesn't have limit registers, set
924 to min and max value */
926 data->in_max[8] = 255;
928 for (i = 0; i < 3; i++) {
930 it87_read_value(client, IT87_REG_FAN(i));
932 it87_read_value(client, IT87_REG_FAN_MIN(i));
934 for (i = 0; i < 3; i++) {
936 it87_read_value(client, IT87_REG_TEMP(i));
938 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
940 it87_read_value(client, IT87_REG_TEMP_LOW(i));
943 /* The 8705 does not have VID capability */
946 i = it87_read_value(client, IT87_REG_FAN_DIV);
947 data->fan_div[0] = i & 0x07;
948 data->fan_div[1] = (i >> 3) & 0x07;
949 data->fan_div[2] = (i & 0x40) ? 3 : 1;
952 it87_read_value(client, IT87_REG_ALARM1) |
953 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
954 (it87_read_value(client, IT87_REG_ALARM3) << 16);
956 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
957 /* The 8705 does not have VID capability */
958 if (data->type == it8712) {
959 data->vid = it87_read_value(client, IT87_REG_VID);
962 data->last_updated = jiffies;
966 up(&data->update_lock);
971 static int __init sm_it87_init(void)
975 if (!it87_find(&addr)) {
976 normal_isa[0] = addr;
978 return i2c_add_driver(&it87_driver);
981 static void __exit sm_it87_exit(void)
983 i2c_del_driver(&it87_driver);
987 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
988 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
989 module_param(update_vbat, bool, 0);
990 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
991 module_param(reset, bool, 0);
992 MODULE_PARM_DESC(reset, "Reset the chip's registers, default no");
993 MODULE_LICENSE("GPL");
995 module_init(sm_it87_init);
996 module_exit(sm_it87_exit);