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>
43 /* Addresses to scan */
44 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
45 static unsigned short normal_i2c_range[] = { 0x20, 0x2f, I2C_CLIENT_END };
46 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
47 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
49 /* Insmod parameters */
50 SENSORS_INSMOD_1(it87);
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 */
59 superio_outb(int reg, int val)
95 /* just IT8712F for now - this should be extended to support the other
97 #define IT8712F_DEVID 0x8712
98 #define IT87_ACT_REG 0x30
99 #define IT87_BASE_REG 0x60
101 /* Update battery voltage after every reading if true */
102 static int update_vbat;
104 /* Reset the registers on init if true */
107 /* Many IT87 constants specified below */
109 /* Length of ISA address segment */
110 #define IT87_EXTENT 8
112 /* Where are the ISA address/data registers relative to the base address */
113 #define IT87_ADDR_REG_OFFSET 5
114 #define IT87_DATA_REG_OFFSET 6
116 /*----- The IT87 registers -----*/
118 #define IT87_REG_CONFIG 0x00
120 #define IT87_REG_ALARM1 0x01
121 #define IT87_REG_ALARM2 0x02
122 #define IT87_REG_ALARM3 0x03
124 #define IT87_REG_VID 0x0a
125 #define IT87_REG_FAN_DIV 0x0b
127 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
129 #define IT87_REG_FAN(nr) (0x0d + (nr))
130 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
131 #define IT87_REG_FAN_CTRL 0x13
133 #define IT87_REG_VIN(nr) (0x20 + (nr))
134 #define IT87_REG_TEMP(nr) (0x29 + (nr))
136 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
137 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
138 #define IT87_REG_TEMP_HIGH(nr) (0x40 + ((nr) * 2))
139 #define IT87_REG_TEMP_LOW(nr) (0x41 + ((nr) * 2))
141 #define IT87_REG_I2C_ADDR 0x48
143 #define IT87_REG_VIN_ENABLE 0x50
144 #define IT87_REG_TEMP_ENABLE 0x51
146 #define IT87_REG_CHIPID 0x58
148 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
149 #define IN_FROM_REG(val) (((val) * 16) / 10)
151 static inline u8 FAN_TO_REG(long rpm, int div)
155 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
156 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
160 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
162 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-5)/10):\
163 ((val)+5)/10),0,255))
164 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*10)
166 #define VID_FROM_REG(val) ((val)==0x1f?0:(val)>=0x10?510-(val)*10:\
168 #define ALARMS_FROM_REG(val) (val)
170 static int DIV_TO_REG(int val)
177 #define DIV_FROM_REG(val) (1 << (val))
180 /* For each registered IT87, we need to keep some data in memory. That
181 data is pointed to by it87_list[NR]->data. The structure itself is
182 dynamically allocated, at the same time when a new it87 client is
185 struct i2c_client client;
186 struct semaphore lock;
189 struct semaphore update_lock;
190 char valid; /* !=0 if following fields are valid */
191 unsigned long last_updated; /* In jiffies */
193 u8 in[9]; /* Register value */
194 u8 in_max[9]; /* Register value */
195 u8 in_min[9]; /* Register value */
196 u8 fan[3]; /* Register value */
197 u8 fan_min[3]; /* Register value */
198 u8 temp[3]; /* Register value */
199 u8 temp_high[3]; /* Register value */
200 u8 temp_low[3]; /* Register value */
201 u8 sensor; /* Register value */
202 u8 fan_div[3]; /* Register encoding, shifted right */
203 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])*10 );
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])*10 );
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])*10 );
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)/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)/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])*100 );
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])*100);
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])*100);
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)/100;
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)/100;
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);
546 /* This function is called when:
547 * it87_driver is inserted (when this module is loaded), for each
549 * when a new adapter is inserted (and it87_driver is still present) */
550 static int it87_attach_adapter(struct i2c_adapter *adapter)
552 if (!(adapter->class & I2C_CLASS_HWMON))
554 return i2c_detect(adapter, &addr_data, it87_detect);
557 /* SuperIO detection - will change normal_isa[0] if a chip is found */
558 static int it87_find(int *address)
563 val = (superio_inb(DEVID) << 8) |
564 superio_inb(DEVID + 1);
565 if (val != IT8712F_DEVID) {
571 val = (superio_inb(IT87_BASE_REG) << 8) |
572 superio_inb(IT87_BASE_REG + 1);
574 *address = val & ~(IT87_EXTENT - 1);
581 /* This function is called by i2c_detect */
582 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
585 struct i2c_client *new_client;
586 struct it87_data *data;
588 const char *name = "";
589 int is_isa = i2c_is_isa_adapter(adapter);
592 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
595 /* Reserve the ISA region */
597 if (!request_region(address, IT87_EXTENT, name))
600 /* Probe whether there is anything available on this address. Already
601 done for SMBus clients */
605 #define REALLY_SLOW_IO
606 /* We need the timeouts for at least some IT87-like chips. But only
607 if we read 'undefined' registers. */
608 i = inb_p(address + 1);
609 if (inb_p(address + 2) != i
610 || inb_p(address + 3) != i
611 || inb_p(address + 7) != i) {
615 #undef REALLY_SLOW_IO
617 /* Let's just hope nothing breaks here */
618 i = inb_p(address + 5) & 0x7f;
619 outb_p(~i & 0x7f, address + 5);
620 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
621 outb_p(i, address + 5);
628 /* OK. For now, we presume we have a valid client. We now create the
629 client structure, even though we cannot fill it completely yet.
630 But it allows us to access it87_{read,write}_value. */
632 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
636 memset(data, 0, sizeof(struct it87_data));
638 new_client = &data->client;
640 init_MUTEX(&data->lock);
641 i2c_set_clientdata(new_client, data);
642 new_client->addr = address;
643 new_client->adapter = adapter;
644 new_client->driver = &it87_driver;
645 new_client->flags = 0;
647 /* Now, we do the remaining detection. */
650 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
652 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
658 /* Determine the chip type. */
660 i = it87_read_value(new_client, IT87_REG_CHIPID);
666 dev_info(&adapter->dev,
667 "Ignoring 'force' parameter for unknown chip at "
668 "adapter %d, address 0x%02x\n",
669 i2c_adapter_id(adapter), address);
679 /* Fill in the remaining client fields and put it into the global list */
680 strlcpy(new_client->name, name, I2C_NAME_SIZE);
684 new_client->id = it87_id++;
686 init_MUTEX(&data->update_lock);
688 /* Tell the I2C layer a new client has arrived */
689 if ((err = i2c_attach_client(new_client)))
692 /* Initialize the IT87 chip */
693 it87_init_client(new_client, data);
695 /* Register sysfs hooks */
696 device_create_file(&new_client->dev, &dev_attr_in0_input);
697 device_create_file(&new_client->dev, &dev_attr_in1_input);
698 device_create_file(&new_client->dev, &dev_attr_in2_input);
699 device_create_file(&new_client->dev, &dev_attr_in3_input);
700 device_create_file(&new_client->dev, &dev_attr_in4_input);
701 device_create_file(&new_client->dev, &dev_attr_in5_input);
702 device_create_file(&new_client->dev, &dev_attr_in6_input);
703 device_create_file(&new_client->dev, &dev_attr_in7_input);
704 device_create_file(&new_client->dev, &dev_attr_in8_input);
705 device_create_file(&new_client->dev, &dev_attr_in0_min);
706 device_create_file(&new_client->dev, &dev_attr_in1_min);
707 device_create_file(&new_client->dev, &dev_attr_in2_min);
708 device_create_file(&new_client->dev, &dev_attr_in3_min);
709 device_create_file(&new_client->dev, &dev_attr_in4_min);
710 device_create_file(&new_client->dev, &dev_attr_in5_min);
711 device_create_file(&new_client->dev, &dev_attr_in6_min);
712 device_create_file(&new_client->dev, &dev_attr_in7_min);
713 device_create_file(&new_client->dev, &dev_attr_in0_max);
714 device_create_file(&new_client->dev, &dev_attr_in1_max);
715 device_create_file(&new_client->dev, &dev_attr_in2_max);
716 device_create_file(&new_client->dev, &dev_attr_in3_max);
717 device_create_file(&new_client->dev, &dev_attr_in4_max);
718 device_create_file(&new_client->dev, &dev_attr_in5_max);
719 device_create_file(&new_client->dev, &dev_attr_in6_max);
720 device_create_file(&new_client->dev, &dev_attr_in7_max);
721 device_create_file(&new_client->dev, &dev_attr_temp1_input);
722 device_create_file(&new_client->dev, &dev_attr_temp2_input);
723 device_create_file(&new_client->dev, &dev_attr_temp3_input);
724 device_create_file(&new_client->dev, &dev_attr_temp1_max);
725 device_create_file(&new_client->dev, &dev_attr_temp2_max);
726 device_create_file(&new_client->dev, &dev_attr_temp3_max);
727 device_create_file(&new_client->dev, &dev_attr_temp1_min);
728 device_create_file(&new_client->dev, &dev_attr_temp2_min);
729 device_create_file(&new_client->dev, &dev_attr_temp3_min);
730 device_create_file(&new_client->dev, &dev_attr_temp1_type);
731 device_create_file(&new_client->dev, &dev_attr_temp2_type);
732 device_create_file(&new_client->dev, &dev_attr_temp3_type);
733 device_create_file(&new_client->dev, &dev_attr_fan1_input);
734 device_create_file(&new_client->dev, &dev_attr_fan2_input);
735 device_create_file(&new_client->dev, &dev_attr_fan3_input);
736 device_create_file(&new_client->dev, &dev_attr_fan1_min);
737 device_create_file(&new_client->dev, &dev_attr_fan2_min);
738 device_create_file(&new_client->dev, &dev_attr_fan3_min);
739 device_create_file(&new_client->dev, &dev_attr_fan1_div);
740 device_create_file(&new_client->dev, &dev_attr_fan2_div);
741 device_create_file(&new_client->dev, &dev_attr_fan3_div);
742 device_create_file(&new_client->dev, &dev_attr_alarms);
750 release_region(address, IT87_EXTENT);
755 static int it87_detach_client(struct i2c_client *client)
759 if ((err = i2c_detach_client(client))) {
760 dev_err(&client->dev,
761 "Client deregistration failed, client not detached.\n");
765 if(i2c_is_isa_client(client))
766 release_region(client->addr, IT87_EXTENT);
767 kfree(i2c_get_clientdata(client));
772 /* The SMBus locks itself, but ISA access must be locked explicitely!
773 We don't want to lock the whole ISA bus, so we lock each client
775 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
776 would slow down the IT87 access and should not be necessary.
777 There are some ugly typecasts here, but the good new is - they should
778 nowhere else be necessary! */
779 static int it87_read_value(struct i2c_client *client, u8 reg)
781 struct it87_data *data = i2c_get_clientdata(client);
784 if (i2c_is_isa_client(client)) {
786 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
787 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
791 return i2c_smbus_read_byte_data(client, reg);
794 /* The SMBus locks itself, but ISA access muse be locked explicitely!
795 We don't want to lock the whole ISA bus, so we lock each client
797 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
798 would slow down the IT87 access and should not be necessary.
799 There are some ugly typecasts here, but the good new is - they should
800 nowhere else be necessary! */
801 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
803 struct it87_data *data = i2c_get_clientdata(client);
805 if (i2c_is_isa_client(client)) {
807 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
808 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
812 return i2c_smbus_write_byte_data(client, reg, value);
815 /* Called when we have found a new IT87. */
816 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
821 /* Reset all except Watchdog values and last conversion values
822 This sets fan-divs to 2, among others */
823 it87_write_value(client, IT87_REG_CONFIG, 0x80);
826 /* Check if temperature channnels are reset manually or by some reason */
827 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
828 if ((tmp & 0x3f) == 0) {
829 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
830 tmp = (tmp & 0xc0) | 0x2a;
831 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
835 /* Check if voltage monitors are reset manually or by some reason */
836 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
837 if ((tmp & 0xff) == 0) {
838 /* Enable all voltage monitors */
839 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
842 /* Check if tachometers are reset manually or by some reason */
843 tmp = it87_read_value(client, IT87_REG_FAN_CTRL);
844 if ((tmp & 0x70) == 0) {
845 /* Enable all fan tachometers */
846 tmp = (tmp & 0x8f) | 0x70;
847 it87_write_value(client, IT87_REG_FAN_CTRL, tmp);
850 /* Start monitoring */
851 it87_write_value(client, IT87_REG_CONFIG,
852 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
853 | (update_vbat ? 0x41 : 0x01));
856 static struct it87_data *it87_update_device(struct device *dev)
858 struct i2c_client *client = to_i2c_client(dev);
859 struct it87_data *data = i2c_get_clientdata(client);
862 down(&data->update_lock);
864 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
865 (jiffies < data->last_updated) || !data->valid) {
868 /* Cleared after each update, so reenable. Value
869 returned by this read will be previous value */
870 it87_write_value(client, IT87_REG_CONFIG,
871 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
873 for (i = 0; i <= 7; i++) {
875 it87_read_value(client, IT87_REG_VIN(i));
877 it87_read_value(client, IT87_REG_VIN_MIN(i));
879 it87_read_value(client, IT87_REG_VIN_MAX(i));
882 it87_read_value(client, IT87_REG_VIN(8));
883 /* Temperature sensor doesn't have limit registers, set
884 to min and max value */
886 data->in_max[8] = 255;
888 for (i = 0; i < 3; i++) {
890 it87_read_value(client, IT87_REG_FAN(i));
892 it87_read_value(client, IT87_REG_FAN_MIN(i));
894 for (i = 0; i < 3; i++) {
896 it87_read_value(client, IT87_REG_TEMP(i));
898 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
900 it87_read_value(client, IT87_REG_TEMP_LOW(i));
903 /* The 8705 does not have VID capability */
906 i = it87_read_value(client, IT87_REG_FAN_DIV);
907 data->fan_div[0] = i & 0x07;
908 data->fan_div[1] = (i >> 3) & 0x07;
909 data->fan_div[2] = (i & 0x40) ? 3 : 1;
912 it87_read_value(client, IT87_REG_ALARM1) |
913 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
914 (it87_read_value(client, IT87_REG_ALARM3) << 16);
916 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
918 data->last_updated = jiffies;
922 up(&data->update_lock);
927 static int __init sm_it87_init(void)
931 if (!it87_find(&addr)) {
932 normal_isa[0] = addr;
934 return i2c_add_driver(&it87_driver);
937 static void __exit sm_it87_exit(void)
939 i2c_del_driver(&it87_driver);
943 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
944 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
945 MODULE_PARM(update_vbat, "i");
946 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
947 MODULE_PARM(reset, "i");
948 MODULE_PARM_DESC(reset, "Reset the chip's registers, default no");
949 MODULE_LICENSE("GPL");
951 module_init(sm_it87_init);
952 module_exit(sm_it87_exit);