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);
53 /* Update battery voltage after every reading if true */
54 static int update_vbat;
56 /* Reset the registers on init if true */
59 /* Many IT87 constants specified below */
61 /* Length of ISA address segment */
64 /* Where are the ISA address/data registers relative to the base address */
65 #define IT87_ADDR_REG_OFFSET 5
66 #define IT87_DATA_REG_OFFSET 6
68 /*----- The IT87 registers -----*/
70 #define IT87_REG_CONFIG 0x00
72 #define IT87_REG_ALARM1 0x01
73 #define IT87_REG_ALARM2 0x02
74 #define IT87_REG_ALARM3 0x03
76 #define IT87_REG_VID 0x0a
77 #define IT87_REG_FAN_DIV 0x0b
79 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
81 #define IT87_REG_FAN(nr) (0x0d + (nr))
82 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
83 #define IT87_REG_FAN_CTRL 0x13
85 #define IT87_REG_VIN(nr) (0x20 + (nr))
86 #define IT87_REG_TEMP(nr) (0x29 + (nr))
88 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
89 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
90 #define IT87_REG_TEMP_HIGH(nr) (0x40 + ((nr) * 2))
91 #define IT87_REG_TEMP_LOW(nr) (0x41 + ((nr) * 2))
93 #define IT87_REG_I2C_ADDR 0x48
95 #define IT87_REG_VIN_ENABLE 0x50
96 #define IT87_REG_TEMP_ENABLE 0x51
98 #define IT87_REG_CHIPID 0x58
100 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) * 10 + 8)/16),0,255))
101 #define IN_FROM_REG(val) (((val) * 16) / 10)
103 static inline u8 FAN_TO_REG(long rpm, int div)
107 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
108 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
112 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
114 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-5)/10):\
115 ((val)+5)/10),0,255))
116 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*10)
118 #define VID_FROM_REG(val) ((val)==0x1f?0:(val)>=0x10?510-(val)*10:\
120 #define ALARMS_FROM_REG(val) (val)
122 static int DIV_TO_REG(int val)
129 #define DIV_FROM_REG(val) (1 << (val))
132 /* For each registered IT87, we need to keep some data in memory. That
133 data is pointed to by it87_list[NR]->data. The structure itself is
134 dynamically allocated, at the same time when a new it87 client is
137 struct i2c_client client;
138 struct semaphore lock;
141 struct semaphore update_lock;
142 char valid; /* !=0 if following fields are valid */
143 unsigned long last_updated; /* In jiffies */
145 u8 in[9]; /* Register value */
146 u8 in_max[9]; /* Register value */
147 u8 in_min[9]; /* Register value */
148 u8 fan[3]; /* Register value */
149 u8 fan_min[3]; /* Register value */
150 u8 temp[3]; /* Register value */
151 u8 temp_high[3]; /* Register value */
152 u8 temp_low[3]; /* Register value */
153 u8 sensor; /* Register value */
154 u8 fan_div[3]; /* Register encoding, shifted right */
155 u8 vid; /* Register encoding, combined */
156 u32 alarms; /* Register encoding, combined */
160 static int it87_attach_adapter(struct i2c_adapter *adapter);
161 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
162 static int it87_detach_client(struct i2c_client *client);
164 static int it87_read_value(struct i2c_client *client, u8 register);
165 static int it87_write_value(struct i2c_client *client, u8 register,
167 static struct it87_data *it87_update_device(struct device *dev);
168 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
171 static struct i2c_driver it87_driver = {
172 .owner = THIS_MODULE,
174 .id = I2C_DRIVERID_IT87,
175 .flags = I2C_DF_NOTIFY,
176 .attach_adapter = it87_attach_adapter,
177 .detach_client = it87_detach_client,
180 static int it87_id = 0;
182 static ssize_t show_in(struct device *dev, char *buf, int nr)
184 struct it87_data *data = it87_update_device(dev);
185 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr])*10 );
188 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
190 struct it87_data *data = it87_update_device(dev);
191 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr])*10 );
194 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
196 struct it87_data *data = it87_update_device(dev);
197 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])*10 );
200 static ssize_t set_in_min(struct device *dev, const char *buf,
201 size_t count, int nr)
203 struct i2c_client *client = to_i2c_client(dev);
204 struct it87_data *data = i2c_get_clientdata(client);
205 unsigned long val = simple_strtoul(buf, NULL, 10)/10;
206 data->in_min[nr] = IN_TO_REG(val);
207 it87_write_value(client, IT87_REG_VIN_MIN(nr),
211 static ssize_t set_in_max(struct device *dev, const char *buf,
212 size_t count, int nr)
214 struct i2c_client *client = to_i2c_client(dev);
215 struct it87_data *data = i2c_get_clientdata(client);
216 unsigned long val = simple_strtoul(buf, NULL, 10)/10;
217 data->in_max[nr] = IN_TO_REG(val);
218 it87_write_value(client, IT87_REG_VIN_MAX(nr),
223 #define show_in_offset(offset) \
225 show_in##offset (struct device *dev, char *buf) \
227 return show_in(dev, buf, 0x##offset); \
229 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL)
231 #define limit_in_offset(offset) \
233 show_in##offset##_min (struct device *dev, char *buf) \
235 return show_in_min(dev, buf, 0x##offset); \
238 show_in##offset##_max (struct device *dev, char *buf) \
240 return show_in_max(dev, buf, 0x##offset); \
242 static ssize_t set_in##offset##_min (struct device *dev, \
243 const char *buf, size_t count) \
245 return set_in_min(dev, buf, count, 0x##offset); \
247 static ssize_t set_in##offset##_max (struct device *dev, \
248 const char *buf, size_t count) \
250 return set_in_max(dev, buf, count, 0x##offset); \
252 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
253 show_in##offset##_min, set_in##offset##_min) \
254 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
255 show_in##offset##_max, set_in##offset##_max)
276 static ssize_t show_temp(struct device *dev, char *buf, int nr)
278 struct it87_data *data = it87_update_device(dev);
279 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])*100 );
281 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
283 struct it87_data *data = it87_update_device(dev);
284 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])*100);
286 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
288 struct it87_data *data = it87_update_device(dev);
289 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr])*100);
291 static ssize_t set_temp_max(struct device *dev, const char *buf,
292 size_t count, int nr)
294 struct i2c_client *client = to_i2c_client(dev);
295 struct it87_data *data = i2c_get_clientdata(client);
296 int val = simple_strtol(buf, NULL, 10)/100;
297 data->temp_high[nr] = TEMP_TO_REG(val);
298 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
301 static ssize_t set_temp_min(struct device *dev, const char *buf,
302 size_t count, int nr)
304 struct i2c_client *client = to_i2c_client(dev);
305 struct it87_data *data = i2c_get_clientdata(client);
306 int val = simple_strtol(buf, NULL, 10)/100;
307 data->temp_low[nr] = TEMP_TO_REG(val);
308 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
311 #define show_temp_offset(offset) \
312 static ssize_t show_temp_##offset (struct device *dev, char *buf) \
314 return show_temp(dev, buf, 0x##offset - 1); \
317 show_temp_##offset##_max (struct device *dev, char *buf) \
319 return show_temp_max(dev, buf, 0x##offset - 1); \
322 show_temp_##offset##_min (struct device *dev, char *buf) \
324 return show_temp_min(dev, buf, 0x##offset - 1); \
326 static ssize_t set_temp_##offset##_max (struct device *dev, \
327 const char *buf, size_t count) \
329 return set_temp_max(dev, buf, count, 0x##offset - 1); \
331 static ssize_t set_temp_##offset##_min (struct device *dev, \
332 const char *buf, size_t count) \
334 return set_temp_min(dev, buf, count, 0x##offset - 1); \
336 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL) \
337 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
338 show_temp_##offset##_max, set_temp_##offset##_max) \
339 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
340 show_temp_##offset##_min, set_temp_##offset##_min)
346 static ssize_t show_sensor(struct device *dev, char *buf, int nr)
348 struct it87_data *data = it87_update_device(dev);
349 if (data->sensor & (1 << nr))
350 return sprintf(buf, "3\n"); /* thermal diode */
351 if (data->sensor & (8 << nr))
352 return sprintf(buf, "2\n"); /* thermistor */
353 return sprintf(buf, "0\n"); /* disabled */
355 static ssize_t set_sensor(struct device *dev, const char *buf,
356 size_t count, int nr)
358 struct i2c_client *client = to_i2c_client(dev);
359 struct it87_data *data = i2c_get_clientdata(client);
360 int val = simple_strtol(buf, NULL, 10);
362 data->sensor &= ~(1 << nr);
363 data->sensor &= ~(8 << nr);
364 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
366 data->sensor |= 1 << nr;
368 data->sensor |= 8 << nr;
371 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
374 #define show_sensor_offset(offset) \
375 static ssize_t show_sensor_##offset (struct device *dev, char *buf) \
377 return show_sensor(dev, buf, 0x##offset - 1); \
379 static ssize_t set_sensor_##offset (struct device *dev, \
380 const char *buf, size_t count) \
382 return set_sensor(dev, buf, count, 0x##offset - 1); \
384 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
385 show_sensor_##offset, set_sensor_##offset)
387 show_sensor_offset(1);
388 show_sensor_offset(2);
389 show_sensor_offset(3);
392 static ssize_t show_fan(struct device *dev, char *buf, int nr)
394 struct it87_data *data = it87_update_device(dev);
395 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
396 DIV_FROM_REG(data->fan_div[nr])) );
398 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
400 struct it87_data *data = it87_update_device(dev);
401 return sprintf(buf,"%d\n",
402 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])) );
404 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
406 struct it87_data *data = it87_update_device(dev);
407 return sprintf(buf,"%d\n", DIV_FROM_REG(data->fan_div[nr]) );
409 static ssize_t set_fan_min(struct device *dev, const char *buf,
410 size_t count, int nr)
412 struct i2c_client *client = to_i2c_client(dev);
413 struct it87_data *data = i2c_get_clientdata(client);
414 int val = simple_strtol(buf, NULL, 10);
415 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
416 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
419 static ssize_t set_fan_div(struct device *dev, const char *buf,
420 size_t count, int nr)
422 struct i2c_client *client = to_i2c_client(dev);
423 struct it87_data *data = i2c_get_clientdata(client);
424 int val = simple_strtol(buf, NULL, 10);
426 u8 old = it87_read_value(client, IT87_REG_FAN_DIV);
428 for (i = 0; i < 3; i++)
429 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
434 data->fan_div[nr] = DIV_TO_REG(val);
438 data->fan_div[nr] = 1;
440 data->fan_div[nr] = 3;
443 val |= (data->fan_div[0] & 0x07);
444 val |= (data->fan_div[1] & 0x07) << 3;
445 if (data->fan_div[2] == 3)
447 it87_write_value(client, IT87_REG_FAN_DIV, val);
449 for (i = 0; i < 3; i++) {
450 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
451 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
456 #define show_fan_offset(offset) \
457 static ssize_t show_fan_##offset (struct device *dev, char *buf) \
459 return show_fan(dev, buf, 0x##offset - 1); \
461 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
463 return show_fan_min(dev, buf, 0x##offset - 1); \
465 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
467 return show_fan_div(dev, buf, 0x##offset - 1); \
469 static ssize_t set_fan_##offset##_min (struct device *dev, \
470 const char *buf, size_t count) \
472 return set_fan_min(dev, buf, count, 0x##offset - 1); \
474 static ssize_t set_fan_##offset##_div (struct device *dev, \
475 const char *buf, size_t count) \
477 return set_fan_div(dev, buf, count, 0x##offset - 1); \
479 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL) \
480 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
481 show_fan_##offset##_min, set_fan_##offset##_min) \
482 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
483 show_fan_##offset##_div, set_fan_##offset##_div)
490 static ssize_t show_alarms(struct device *dev, char *buf)
492 struct it87_data *data = it87_update_device(dev);
493 return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
495 static DEVICE_ATTR(alarms, S_IRUGO | S_IWUSR, show_alarms, NULL);
497 /* This function is called when:
498 * it87_driver is inserted (when this module is loaded), for each
500 * when a new adapter is inserted (and it87_driver is still present) */
501 static int it87_attach_adapter(struct i2c_adapter *adapter)
503 if (!(adapter->class & I2C_ADAP_CLASS_SMBUS))
505 return i2c_detect(adapter, &addr_data, it87_detect);
508 /* This function is called by i2c_detect */
509 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
512 struct i2c_client *new_client;
513 struct it87_data *data;
515 const char *name = "";
516 int is_isa = i2c_is_isa_adapter(adapter);
519 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
522 /* Reserve the ISA region */
524 if (!request_region(address, IT87_EXTENT, name))
527 /* Probe whether there is anything available on this address. Already
528 done for SMBus clients */
532 #define REALLY_SLOW_IO
533 /* We need the timeouts for at least some IT87-like chips. But only
534 if we read 'undefined' registers. */
535 i = inb_p(address + 1);
536 if (inb_p(address + 2) != i
537 || inb_p(address + 3) != i
538 || inb_p(address + 7) != i) {
542 #undef REALLY_SLOW_IO
544 /* Let's just hope nothing breaks here */
545 i = inb_p(address + 5) & 0x7f;
546 outb_p(~i & 0x7f, address + 5);
547 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
548 outb_p(i, address + 5);
555 /* OK. For now, we presume we have a valid client. We now create the
556 client structure, even though we cannot fill it completely yet.
557 But it allows us to access it87_{read,write}_value. */
559 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
563 memset(data, 0, sizeof(struct it87_data));
565 new_client = &data->client;
567 init_MUTEX(&data->lock);
568 i2c_set_clientdata(new_client, data);
569 new_client->addr = address;
570 new_client->adapter = adapter;
571 new_client->driver = &it87_driver;
572 new_client->flags = 0;
574 /* Now, we do the remaining detection. */
577 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
579 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
585 /* Determine the chip type. */
587 i = it87_read_value(new_client, IT87_REG_CHIPID);
593 dev_info(&adapter->dev,
594 "Ignoring 'force' parameter for unknown chip at "
595 "adapter %d, address 0x%02x\n",
596 i2c_adapter_id(adapter), address);
606 /* Fill in the remaining client fields and put it into the global list */
607 strlcpy(new_client->name, name, I2C_NAME_SIZE);
611 new_client->id = it87_id++;
613 init_MUTEX(&data->update_lock);
615 /* Tell the I2C layer a new client has arrived */
616 if ((err = i2c_attach_client(new_client)))
619 /* Initialize the IT87 chip */
620 it87_init_client(new_client, data);
622 /* Register sysfs hooks */
623 device_create_file(&new_client->dev, &dev_attr_in0_input);
624 device_create_file(&new_client->dev, &dev_attr_in1_input);
625 device_create_file(&new_client->dev, &dev_attr_in2_input);
626 device_create_file(&new_client->dev, &dev_attr_in3_input);
627 device_create_file(&new_client->dev, &dev_attr_in4_input);
628 device_create_file(&new_client->dev, &dev_attr_in5_input);
629 device_create_file(&new_client->dev, &dev_attr_in6_input);
630 device_create_file(&new_client->dev, &dev_attr_in7_input);
631 device_create_file(&new_client->dev, &dev_attr_in8_input);
632 device_create_file(&new_client->dev, &dev_attr_in0_min);
633 device_create_file(&new_client->dev, &dev_attr_in1_min);
634 device_create_file(&new_client->dev, &dev_attr_in2_min);
635 device_create_file(&new_client->dev, &dev_attr_in3_min);
636 device_create_file(&new_client->dev, &dev_attr_in4_min);
637 device_create_file(&new_client->dev, &dev_attr_in5_min);
638 device_create_file(&new_client->dev, &dev_attr_in6_min);
639 device_create_file(&new_client->dev, &dev_attr_in7_min);
640 device_create_file(&new_client->dev, &dev_attr_in0_max);
641 device_create_file(&new_client->dev, &dev_attr_in1_max);
642 device_create_file(&new_client->dev, &dev_attr_in2_max);
643 device_create_file(&new_client->dev, &dev_attr_in3_max);
644 device_create_file(&new_client->dev, &dev_attr_in4_max);
645 device_create_file(&new_client->dev, &dev_attr_in5_max);
646 device_create_file(&new_client->dev, &dev_attr_in6_max);
647 device_create_file(&new_client->dev, &dev_attr_in7_max);
648 device_create_file(&new_client->dev, &dev_attr_temp1_input);
649 device_create_file(&new_client->dev, &dev_attr_temp2_input);
650 device_create_file(&new_client->dev, &dev_attr_temp3_input);
651 device_create_file(&new_client->dev, &dev_attr_temp1_max);
652 device_create_file(&new_client->dev, &dev_attr_temp2_max);
653 device_create_file(&new_client->dev, &dev_attr_temp3_max);
654 device_create_file(&new_client->dev, &dev_attr_temp1_min);
655 device_create_file(&new_client->dev, &dev_attr_temp2_min);
656 device_create_file(&new_client->dev, &dev_attr_temp3_min);
657 device_create_file(&new_client->dev, &dev_attr_temp1_type);
658 device_create_file(&new_client->dev, &dev_attr_temp2_type);
659 device_create_file(&new_client->dev, &dev_attr_temp3_type);
660 device_create_file(&new_client->dev, &dev_attr_fan1_input);
661 device_create_file(&new_client->dev, &dev_attr_fan2_input);
662 device_create_file(&new_client->dev, &dev_attr_fan3_input);
663 device_create_file(&new_client->dev, &dev_attr_fan1_min);
664 device_create_file(&new_client->dev, &dev_attr_fan2_min);
665 device_create_file(&new_client->dev, &dev_attr_fan3_min);
666 device_create_file(&new_client->dev, &dev_attr_fan1_div);
667 device_create_file(&new_client->dev, &dev_attr_fan2_div);
668 device_create_file(&new_client->dev, &dev_attr_fan3_div);
669 device_create_file(&new_client->dev, &dev_attr_alarms);
677 release_region(address, IT87_EXTENT);
682 static int it87_detach_client(struct i2c_client *client)
686 if ((err = i2c_detach_client(client))) {
687 dev_err(&client->dev,
688 "Client deregistration failed, client not detached.\n");
692 if(i2c_is_isa_client(client))
693 release_region(client->addr, IT87_EXTENT);
694 kfree(i2c_get_clientdata(client));
699 /* The SMBus locks itself, but ISA access must be locked explicitely!
700 We don't want to lock the whole ISA bus, so we lock each client
702 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
703 would slow down the IT87 access and should not be necessary.
704 There are some ugly typecasts here, but the good new is - they should
705 nowhere else be necessary! */
706 static int it87_read_value(struct i2c_client *client, u8 reg)
708 struct it87_data *data = i2c_get_clientdata(client);
711 if (i2c_is_isa_client(client)) {
713 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
714 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
718 return i2c_smbus_read_byte_data(client, reg);
721 /* The SMBus locks itself, but ISA access muse be locked explicitely!
722 We don't want to lock the whole ISA bus, so we lock each client
724 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
725 would slow down the IT87 access and should not be necessary.
726 There are some ugly typecasts here, but the good new is - they should
727 nowhere else be necessary! */
728 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
730 struct it87_data *data = i2c_get_clientdata(client);
732 if (i2c_is_isa_client(client)) {
734 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
735 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
739 return i2c_smbus_write_byte_data(client, reg, value);
742 /* Called when we have found a new IT87. */
743 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
748 /* Reset all except Watchdog values and last conversion values
749 This sets fan-divs to 2, among others */
750 it87_write_value(client, IT87_REG_CONFIG, 0x80);
753 /* Check if temperature channnels are reset manually or by some reason */
754 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
755 if ((tmp & 0x3f) == 0) {
756 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
757 tmp = (tmp & 0xc0) | 0x2a;
758 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
762 /* Check if voltage monitors are reset manually or by some reason */
763 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
764 if ((tmp & 0xff) == 0) {
765 /* Enable all voltage monitors */
766 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
769 /* Check if tachometers are reset manually or by some reason */
770 tmp = it87_read_value(client, IT87_REG_FAN_CTRL);
771 if ((tmp & 0x70) == 0) {
772 /* Enable all fan tachometers */
773 tmp = (tmp & 0x8f) | 0x70;
774 it87_write_value(client, IT87_REG_FAN_CTRL, tmp);
777 /* Start monitoring */
778 it87_write_value(client, IT87_REG_CONFIG,
779 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
780 | (update_vbat ? 0x41 : 0x01));
783 static struct it87_data *it87_update_device(struct device *dev)
785 struct i2c_client *client = to_i2c_client(dev);
786 struct it87_data *data = i2c_get_clientdata(client);
789 down(&data->update_lock);
791 if ((jiffies - data->last_updated > HZ + HZ / 2) ||
792 (jiffies < data->last_updated) || !data->valid) {
795 /* Cleared after each update, so reenable. Value
796 returned by this read will be previous value */
797 it87_write_value(client, IT87_REG_CONFIG,
798 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
800 for (i = 0; i <= 7; i++) {
802 it87_read_value(client, IT87_REG_VIN(i));
804 it87_read_value(client, IT87_REG_VIN_MIN(i));
806 it87_read_value(client, IT87_REG_VIN_MAX(i));
809 it87_read_value(client, IT87_REG_VIN(8));
810 /* Temperature sensor doesn't have limit registers, set
811 to min and max value */
813 data->in_max[8] = 255;
815 for (i = 0; i < 3; i++) {
817 it87_read_value(client, IT87_REG_FAN(i));
819 it87_read_value(client, IT87_REG_FAN_MIN(i));
821 for (i = 0; i < 3; i++) {
823 it87_read_value(client, IT87_REG_TEMP(i));
825 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
827 it87_read_value(client, IT87_REG_TEMP_LOW(i));
830 /* The 8705 does not have VID capability */
833 i = it87_read_value(client, IT87_REG_FAN_DIV);
834 data->fan_div[0] = i & 0x07;
835 data->fan_div[1] = (i >> 3) & 0x07;
836 data->fan_div[2] = (i & 0x40) ? 3 : 1;
839 it87_read_value(client, IT87_REG_ALARM1) |
840 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
841 (it87_read_value(client, IT87_REG_ALARM3) << 16);
843 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
845 data->last_updated = jiffies;
849 up(&data->update_lock);
854 static int __init sm_it87_init(void)
856 return i2c_add_driver(&it87_driver);
859 static void __exit sm_it87_exit(void)
861 i2c_del_driver(&it87_driver);
865 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
866 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
867 MODULE_PARM(update_vbat, "i");
868 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
869 MODULE_PARM(reset, "i");
870 MODULE_PARM_DESC(reset, "Reset the chip's registers, default no");
871 MODULE_LICENSE("GPL");
873 module_init(sm_it87_init);
874 module_exit(sm_it87_exit);