[PATCH] I2C: Move hwmon drivers (2/3)
[linux-2.6.22.y-op.git] / drivers / hwmon / it87.c
blobdb20c9e47393a36bbd04b580ce92560ff6dcc331
1 /*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
5 Supports: IT8705F Super I/O chip w/LPC interface & SMBus
6 IT8712F Super I/O chip w/LPC interface & SMBus
7 Sis950 A clone of the IT8705F
9 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
10 Largely inspired by lm78.c of the same package
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 djg@pdp8.net David Gesswein 7/18/01
29 Modified to fix bug with not all alarms enabled.
30 Added ability to read battery voltage and select temperature sensor
31 type at module load time.
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/jiffies.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-sensor.h>
40 #include <linux/i2c-vid.h>
41 #include <linux/hwmon-sysfs.h>
42 #include <asm/io.h>
45 /* Addresses to scan */
46 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
47 0x2e, 0x2f, I2C_CLIENT_END };
48 static unsigned int normal_isa[] = { 0x0290, 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 */
58 #define DEVREV 0x22 /* Register: Device Revision */
60 static inline int
61 superio_inb(int reg)
63 outb(reg, REG);
64 return inb(VAL);
67 static int superio_inw(int reg)
69 int val;
70 outb(reg++, REG);
71 val = inb(VAL) << 8;
72 outb(reg, REG);
73 val |= inb(VAL);
74 return val;
77 static inline void
78 superio_select(void)
80 outb(DEV, REG);
81 outb(PME, VAL);
84 static inline void
85 superio_enter(void)
87 outb(0x87, REG);
88 outb(0x01, REG);
89 outb(0x55, REG);
90 outb(0x55, REG);
93 static inline void
94 superio_exit(void)
96 outb(0x02, REG);
97 outb(0x02, VAL);
100 #define IT8712F_DEVID 0x8712
101 #define IT8705F_DEVID 0x8705
102 #define IT87_ACT_REG 0x30
103 #define IT87_BASE_REG 0x60
105 /* Update battery voltage after every reading if true */
106 static int update_vbat;
108 /* Not all BIOSes properly configure the PWM registers */
109 static int fix_pwm_polarity;
111 /* Chip Type */
113 static u16 chip_type;
115 /* Many IT87 constants specified below */
117 /* Length of ISA address segment */
118 #define IT87_EXTENT 8
120 /* Where are the ISA address/data registers relative to the base address */
121 #define IT87_ADDR_REG_OFFSET 5
122 #define IT87_DATA_REG_OFFSET 6
124 /*----- The IT87 registers -----*/
126 #define IT87_REG_CONFIG 0x00
128 #define IT87_REG_ALARM1 0x01
129 #define IT87_REG_ALARM2 0x02
130 #define IT87_REG_ALARM3 0x03
132 #define IT87_REG_VID 0x0a
133 #define IT87_REG_FAN_DIV 0x0b
135 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
137 #define IT87_REG_FAN(nr) (0x0d + (nr))
138 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
139 #define IT87_REG_FAN_MAIN_CTRL 0x13
140 #define IT87_REG_FAN_CTL 0x14
141 #define IT87_REG_PWM(nr) (0x15 + (nr))
143 #define IT87_REG_VIN(nr) (0x20 + (nr))
144 #define IT87_REG_TEMP(nr) (0x29 + (nr))
146 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
147 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
148 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
149 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
151 #define IT87_REG_I2C_ADDR 0x48
153 #define IT87_REG_VIN_ENABLE 0x50
154 #define IT87_REG_TEMP_ENABLE 0x51
156 #define IT87_REG_CHIPID 0x58
158 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
159 #define IN_FROM_REG(val) ((val) * 16)
161 static inline u8 FAN_TO_REG(long rpm, int div)
163 if (rpm == 0)
164 return 255;
165 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
166 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
167 254);
170 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
172 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
173 ((val)+500)/1000),-128,127))
174 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
176 #define PWM_TO_REG(val) ((val) >> 1)
177 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
179 static int DIV_TO_REG(int val)
181 int answer = 0;
182 while ((val >>= 1) != 0)
183 answer++;
184 return answer;
186 #define DIV_FROM_REG(val) (1 << (val))
189 /* For each registered IT87, we need to keep some data in memory. That
190 data is pointed to by it87_list[NR]->data. The structure itself is
191 dynamically allocated, at the same time when a new it87 client is
192 allocated. */
193 struct it87_data {
194 struct i2c_client client;
195 struct semaphore lock;
196 enum chips type;
198 struct semaphore update_lock;
199 char valid; /* !=0 if following fields are valid */
200 unsigned long last_updated; /* In jiffies */
202 u8 in[9]; /* Register value */
203 u8 in_max[9]; /* Register value */
204 u8 in_min[9]; /* Register value */
205 u8 fan[3]; /* Register value */
206 u8 fan_min[3]; /* Register value */
207 u8 temp[3]; /* Register value */
208 u8 temp_high[3]; /* Register value */
209 u8 temp_low[3]; /* Register value */
210 u8 sensor; /* Register value */
211 u8 fan_div[3]; /* Register encoding, shifted right */
212 u8 vid; /* Register encoding, combined */
213 int vrm;
214 u32 alarms; /* Register encoding, combined */
215 u8 fan_main_ctrl; /* Register value */
216 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
220 static int it87_attach_adapter(struct i2c_adapter *adapter);
221 static int it87_find(int *address);
222 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
223 static int it87_detach_client(struct i2c_client *client);
225 static int it87_read_value(struct i2c_client *client, u8 register);
226 static int it87_write_value(struct i2c_client *client, u8 register,
227 u8 value);
228 static struct it87_data *it87_update_device(struct device *dev);
229 static int it87_check_pwm(struct i2c_client *client);
230 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
233 static struct i2c_driver it87_driver = {
234 .owner = THIS_MODULE,
235 .name = "it87",
236 .id = I2C_DRIVERID_IT87,
237 .flags = I2C_DF_NOTIFY,
238 .attach_adapter = it87_attach_adapter,
239 .detach_client = it87_detach_client,
242 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
243 char *buf)
245 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
246 int nr = sensor_attr->index;
248 struct it87_data *data = it87_update_device(dev);
249 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
252 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
253 char *buf)
255 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
256 int nr = sensor_attr->index;
258 struct it87_data *data = it87_update_device(dev);
259 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
262 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
263 char *buf)
265 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
266 int nr = sensor_attr->index;
268 struct it87_data *data = it87_update_device(dev);
269 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
272 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
275 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
276 int nr = sensor_attr->index;
278 struct i2c_client *client = to_i2c_client(dev);
279 struct it87_data *data = i2c_get_clientdata(client);
280 unsigned long val = simple_strtoul(buf, NULL, 10);
282 down(&data->update_lock);
283 data->in_min[nr] = IN_TO_REG(val);
284 it87_write_value(client, IT87_REG_VIN_MIN(nr),
285 data->in_min[nr]);
286 up(&data->update_lock);
287 return count;
289 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
292 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
293 int nr = sensor_attr->index;
295 struct i2c_client *client = to_i2c_client(dev);
296 struct it87_data *data = i2c_get_clientdata(client);
297 unsigned long val = simple_strtoul(buf, NULL, 10);
299 down(&data->update_lock);
300 data->in_max[nr] = IN_TO_REG(val);
301 it87_write_value(client, IT87_REG_VIN_MAX(nr),
302 data->in_max[nr]);
303 up(&data->update_lock);
304 return count;
307 #define show_in_offset(offset) \
308 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
309 show_in, NULL, offset);
311 #define limit_in_offset(offset) \
312 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
313 show_in_min, set_in_min, offset); \
314 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
315 show_in_max, set_in_max, offset);
317 show_in_offset(0);
318 limit_in_offset(0);
319 show_in_offset(1);
320 limit_in_offset(1);
321 show_in_offset(2);
322 limit_in_offset(2);
323 show_in_offset(3);
324 limit_in_offset(3);
325 show_in_offset(4);
326 limit_in_offset(4);
327 show_in_offset(5);
328 limit_in_offset(5);
329 show_in_offset(6);
330 limit_in_offset(6);
331 show_in_offset(7);
332 limit_in_offset(7);
333 show_in_offset(8);
335 /* 3 temperatures */
336 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
337 char *buf)
339 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
340 int nr = sensor_attr->index;
342 struct it87_data *data = it87_update_device(dev);
343 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
345 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
346 char *buf)
348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
349 int nr = sensor_attr->index;
351 struct it87_data *data = it87_update_device(dev);
352 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
354 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
355 char *buf)
357 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
358 int nr = sensor_attr->index;
360 struct it87_data *data = it87_update_device(dev);
361 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
363 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
364 const char *buf, size_t count)
366 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
367 int nr = sensor_attr->index;
369 struct i2c_client *client = to_i2c_client(dev);
370 struct it87_data *data = i2c_get_clientdata(client);
371 int val = simple_strtol(buf, NULL, 10);
373 down(&data->update_lock);
374 data->temp_high[nr] = TEMP_TO_REG(val);
375 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
376 up(&data->update_lock);
377 return count;
379 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
380 const char *buf, size_t count)
382 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
383 int nr = sensor_attr->index;
385 struct i2c_client *client = to_i2c_client(dev);
386 struct it87_data *data = i2c_get_clientdata(client);
387 int val = simple_strtol(buf, NULL, 10);
389 down(&data->update_lock);
390 data->temp_low[nr] = TEMP_TO_REG(val);
391 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
392 up(&data->update_lock);
393 return count;
395 #define show_temp_offset(offset) \
396 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
397 show_temp, NULL, offset - 1); \
398 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
399 show_temp_max, set_temp_max, offset - 1); \
400 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
401 show_temp_min, set_temp_min, offset - 1);
403 show_temp_offset(1);
404 show_temp_offset(2);
405 show_temp_offset(3);
407 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
408 char *buf)
410 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
411 int nr = sensor_attr->index;
413 struct it87_data *data = it87_update_device(dev);
414 u8 reg = data->sensor; /* In case the value is updated while we use it */
416 if (reg & (1 << nr))
417 return sprintf(buf, "3\n"); /* thermal diode */
418 if (reg & (8 << nr))
419 return sprintf(buf, "2\n"); /* thermistor */
420 return sprintf(buf, "0\n"); /* disabled */
422 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
423 const char *buf, size_t count)
425 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
426 int nr = sensor_attr->index;
428 struct i2c_client *client = to_i2c_client(dev);
429 struct it87_data *data = i2c_get_clientdata(client);
430 int val = simple_strtol(buf, NULL, 10);
432 down(&data->update_lock);
434 data->sensor &= ~(1 << nr);
435 data->sensor &= ~(8 << nr);
436 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
437 if (val == 3)
438 data->sensor |= 1 << nr;
439 else if (val == 2)
440 data->sensor |= 8 << nr;
441 else if (val != 0) {
442 up(&data->update_lock);
443 return -EINVAL;
445 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
446 up(&data->update_lock);
447 return count;
449 #define show_sensor_offset(offset) \
450 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
451 show_sensor, set_sensor, offset - 1);
453 show_sensor_offset(1);
454 show_sensor_offset(2);
455 show_sensor_offset(3);
457 /* 3 Fans */
458 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
459 char *buf)
461 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
462 int nr = sensor_attr->index;
464 struct it87_data *data = it87_update_device(dev);
465 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
466 DIV_FROM_REG(data->fan_div[nr])));
468 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
469 char *buf)
471 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
472 int nr = sensor_attr->index;
474 struct it87_data *data = it87_update_device(dev);
475 return sprintf(buf,"%d\n",
476 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
478 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
479 char *buf)
481 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
482 int nr = sensor_attr->index;
484 struct it87_data *data = it87_update_device(dev);
485 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
487 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
488 char *buf)
490 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
491 int nr = sensor_attr->index;
493 struct it87_data *data = it87_update_device(dev);
494 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
496 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
497 char *buf)
499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
502 struct it87_data *data = it87_update_device(dev);
503 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
505 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
509 int nr = sensor_attr->index;
511 struct i2c_client *client = to_i2c_client(dev);
512 struct it87_data *data = i2c_get_clientdata(client);
513 int val = simple_strtol(buf, NULL, 10);
515 down(&data->update_lock);
516 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
517 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
518 up(&data->update_lock);
519 return count;
521 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
522 const char *buf, size_t count)
524 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
525 int nr = sensor_attr->index;
527 struct i2c_client *client = to_i2c_client(dev);
528 struct it87_data *data = i2c_get_clientdata(client);
529 int val = simple_strtol(buf, NULL, 10);
530 int i, min[3];
531 u8 old;
533 down(&data->update_lock);
534 old = it87_read_value(client, IT87_REG_FAN_DIV);
536 for (i = 0; i < 3; i++)
537 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
539 switch (nr) {
540 case 0:
541 case 1:
542 data->fan_div[nr] = DIV_TO_REG(val);
543 break;
544 case 2:
545 if (val < 8)
546 data->fan_div[nr] = 1;
547 else
548 data->fan_div[nr] = 3;
550 val = old & 0x80;
551 val |= (data->fan_div[0] & 0x07);
552 val |= (data->fan_div[1] & 0x07) << 3;
553 if (data->fan_div[2] == 3)
554 val |= 0x1 << 6;
555 it87_write_value(client, IT87_REG_FAN_DIV, val);
557 for (i = 0; i < 3; i++) {
558 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
559 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
561 up(&data->update_lock);
562 return count;
564 static ssize_t set_pwm_enable(struct device *dev,
565 struct device_attribute *attr, const char *buf, size_t count)
567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
570 struct i2c_client *client = to_i2c_client(dev);
571 struct it87_data *data = i2c_get_clientdata(client);
572 int val = simple_strtol(buf, NULL, 10);
574 down(&data->update_lock);
576 if (val == 0) {
577 int tmp;
578 /* make sure the fan is on when in on/off mode */
579 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
580 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
581 /* set on/off mode */
582 data->fan_main_ctrl &= ~(1 << nr);
583 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
584 } else if (val == 1) {
585 /* set SmartGuardian mode */
586 data->fan_main_ctrl |= (1 << nr);
587 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
588 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
589 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
590 } else {
591 up(&data->update_lock);
592 return -EINVAL;
595 up(&data->update_lock);
596 return count;
598 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
599 const char *buf, size_t count)
601 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
602 int nr = sensor_attr->index;
604 struct i2c_client *client = to_i2c_client(dev);
605 struct it87_data *data = i2c_get_clientdata(client);
606 int val = simple_strtol(buf, NULL, 10);
608 if (val < 0 || val > 255)
609 return -EINVAL;
611 down(&data->update_lock);
612 data->manual_pwm_ctl[nr] = val;
613 if (data->fan_main_ctrl & (1 << nr))
614 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
615 up(&data->update_lock);
616 return count;
619 #define show_fan_offset(offset) \
620 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
621 show_fan, NULL, offset - 1); \
622 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
623 show_fan_min, set_fan_min, offset - 1); \
624 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
625 show_fan_div, set_fan_div, offset - 1);
627 show_fan_offset(1);
628 show_fan_offset(2);
629 show_fan_offset(3);
631 #define show_pwm_offset(offset) \
632 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
633 show_pwm_enable, set_pwm_enable, offset - 1); \
634 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
635 show_pwm, set_pwm, offset - 1);
637 show_pwm_offset(1);
638 show_pwm_offset(2);
639 show_pwm_offset(3);
641 /* Alarms */
642 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
644 struct it87_data *data = it87_update_device(dev);
645 return sprintf(buf, "%u\n", data->alarms);
647 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
649 static ssize_t
650 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
652 struct it87_data *data = it87_update_device(dev);
653 return sprintf(buf, "%ld\n", (long) data->vrm);
655 static ssize_t
656 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
658 struct i2c_client *client = to_i2c_client(dev);
659 struct it87_data *data = i2c_get_clientdata(client);
660 u32 val;
662 val = simple_strtoul(buf, NULL, 10);
663 data->vrm = val;
665 return count;
667 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
668 #define device_create_file_vrm(client) \
669 device_create_file(&client->dev, &dev_attr_vrm)
671 static ssize_t
672 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
674 struct it87_data *data = it87_update_device(dev);
675 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
677 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
678 #define device_create_file_vid(client) \
679 device_create_file(&client->dev, &dev_attr_cpu0_vid)
681 /* This function is called when:
682 * it87_driver is inserted (when this module is loaded), for each
683 available adapter
684 * when a new adapter is inserted (and it87_driver is still present) */
685 static int it87_attach_adapter(struct i2c_adapter *adapter)
687 if (!(adapter->class & I2C_CLASS_HWMON))
688 return 0;
689 return i2c_detect(adapter, &addr_data, it87_detect);
692 /* SuperIO detection - will change normal_isa[0] if a chip is found */
693 static int it87_find(int *address)
695 int err = -ENODEV;
697 superio_enter();
698 chip_type = superio_inw(DEVID);
699 if (chip_type != IT8712F_DEVID
700 && chip_type != IT8705F_DEVID)
701 goto exit;
703 superio_select();
704 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
705 pr_info("it87: Device not activated, skipping\n");
706 goto exit;
709 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
710 if (*address == 0) {
711 pr_info("it87: Base address not set, skipping\n");
712 goto exit;
715 err = 0;
716 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
717 chip_type, *address, superio_inb(DEVREV) & 0x0f);
719 exit:
720 superio_exit();
721 return err;
724 /* This function is called by i2c_detect */
725 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
727 int i;
728 struct i2c_client *new_client;
729 struct it87_data *data;
730 int err = 0;
731 const char *name = "";
732 int is_isa = i2c_is_isa_adapter(adapter);
733 int enable_pwm_interface;
735 if (!is_isa &&
736 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
737 goto ERROR0;
739 /* Reserve the ISA region */
740 if (is_isa)
741 if (!request_region(address, IT87_EXTENT, it87_driver.name))
742 goto ERROR0;
744 /* Probe whether there is anything available on this address. Already
745 done for SMBus and Super-I/O clients */
746 if (kind < 0) {
747 if (is_isa && !chip_type) {
748 #define REALLY_SLOW_IO
749 /* We need the timeouts for at least some IT87-like chips. But only
750 if we read 'undefined' registers. */
751 i = inb_p(address + 1);
752 if (inb_p(address + 2) != i
753 || inb_p(address + 3) != i
754 || inb_p(address + 7) != i) {
755 err = -ENODEV;
756 goto ERROR1;
758 #undef REALLY_SLOW_IO
760 /* Let's just hope nothing breaks here */
761 i = inb_p(address + 5) & 0x7f;
762 outb_p(~i & 0x7f, address + 5);
763 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
764 outb_p(i, address + 5);
765 err = -ENODEV;
766 goto ERROR1;
771 /* OK. For now, we presume we have a valid client. We now create the
772 client structure, even though we cannot fill it completely yet.
773 But it allows us to access it87_{read,write}_value. */
775 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
776 err = -ENOMEM;
777 goto ERROR1;
779 memset(data, 0, sizeof(struct it87_data));
781 new_client = &data->client;
782 if (is_isa)
783 init_MUTEX(&data->lock);
784 i2c_set_clientdata(new_client, data);
785 new_client->addr = address;
786 new_client->adapter = adapter;
787 new_client->driver = &it87_driver;
788 new_client->flags = 0;
790 /* Now, we do the remaining detection. */
792 if (kind < 0) {
793 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
794 || (!is_isa
795 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
796 err = -ENODEV;
797 goto ERROR2;
801 /* Determine the chip type. */
802 if (kind <= 0) {
803 i = it87_read_value(new_client, IT87_REG_CHIPID);
804 if (i == 0x90) {
805 kind = it87;
806 if ((is_isa) && (chip_type == IT8712F_DEVID))
807 kind = it8712;
809 else {
810 if (kind == 0)
811 dev_info(&adapter->dev,
812 "Ignoring 'force' parameter for unknown chip at "
813 "adapter %d, address 0x%02x\n",
814 i2c_adapter_id(adapter), address);
815 err = -ENODEV;
816 goto ERROR2;
820 if (kind == it87) {
821 name = "it87";
822 } else if (kind == it8712) {
823 name = "it8712";
826 /* Fill in the remaining client fields and put it into the global list */
827 strlcpy(new_client->name, name, I2C_NAME_SIZE);
828 data->type = kind;
829 data->valid = 0;
830 init_MUTEX(&data->update_lock);
832 /* Tell the I2C layer a new client has arrived */
833 if ((err = i2c_attach_client(new_client)))
834 goto ERROR2;
836 /* Check PWM configuration */
837 enable_pwm_interface = it87_check_pwm(new_client);
839 /* Initialize the IT87 chip */
840 it87_init_client(new_client, data);
842 /* Register sysfs hooks */
843 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
844 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
845 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
846 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
847 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
848 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
849 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
850 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
851 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
852 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
853 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
854 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
855 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
856 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
857 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
858 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
859 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
860 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
861 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
862 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
863 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
864 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
865 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
889 device_create_file(&new_client->dev, &dev_attr_alarms);
890 if (enable_pwm_interface) {
891 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
899 if (data->type == it8712) {
900 data->vrm = i2c_which_vrm();
901 device_create_file_vrm(new_client);
902 device_create_file_vid(new_client);
905 return 0;
907 ERROR2:
908 kfree(data);
909 ERROR1:
910 if (is_isa)
911 release_region(address, IT87_EXTENT);
912 ERROR0:
913 return err;
916 static int it87_detach_client(struct i2c_client *client)
918 int err;
920 if ((err = i2c_detach_client(client))) {
921 dev_err(&client->dev,
922 "Client deregistration failed, client not detached.\n");
923 return err;
926 if(i2c_is_isa_client(client))
927 release_region(client->addr, IT87_EXTENT);
928 kfree(i2c_get_clientdata(client));
930 return 0;
933 /* The SMBus locks itself, but ISA access must be locked explicitly!
934 We don't want to lock the whole ISA bus, so we lock each client
935 separately.
936 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
937 would slow down the IT87 access and should not be necessary. */
938 static int it87_read_value(struct i2c_client *client, u8 reg)
940 struct it87_data *data = i2c_get_clientdata(client);
942 int res;
943 if (i2c_is_isa_client(client)) {
944 down(&data->lock);
945 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
946 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
947 up(&data->lock);
948 return res;
949 } else
950 return i2c_smbus_read_byte_data(client, reg);
953 /* The SMBus locks itself, but ISA access muse be locked explicitly!
954 We don't want to lock the whole ISA bus, so we lock each client
955 separately.
956 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
957 would slow down the IT87 access and should not be necessary. */
958 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
960 struct it87_data *data = i2c_get_clientdata(client);
962 if (i2c_is_isa_client(client)) {
963 down(&data->lock);
964 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
965 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
966 up(&data->lock);
967 return 0;
968 } else
969 return i2c_smbus_write_byte_data(client, reg, value);
972 /* Return 1 if and only if the PWM interface is safe to use */
973 static int it87_check_pwm(struct i2c_client *client)
975 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
976 * and polarity set to active low is sign that this is the case so we
977 * disable pwm control to protect the user. */
978 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
979 if ((tmp & 0x87) == 0) {
980 if (fix_pwm_polarity) {
981 /* The user asks us to attempt a chip reconfiguration.
982 * This means switching to active high polarity and
983 * inverting all fan speed values. */
984 int i;
985 u8 pwm[3];
987 for (i = 0; i < 3; i++)
988 pwm[i] = it87_read_value(client,
989 IT87_REG_PWM(i));
991 /* If any fan is in automatic pwm mode, the polarity
992 * might be correct, as suspicious as it seems, so we
993 * better don't change anything (but still disable the
994 * PWM interface). */
995 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
996 dev_info(&client->dev, "Reconfiguring PWM to "
997 "active high polarity\n");
998 it87_write_value(client, IT87_REG_FAN_CTL,
999 tmp | 0x87);
1000 for (i = 0; i < 3; i++)
1001 it87_write_value(client,
1002 IT87_REG_PWM(i),
1003 0x7f & ~pwm[i]);
1004 return 1;
1007 dev_info(&client->dev, "PWM configuration is "
1008 "too broken to be fixed\n");
1011 dev_info(&client->dev, "Detected broken BIOS "
1012 "defaults, disabling PWM interface\n");
1013 return 0;
1014 } else if (fix_pwm_polarity) {
1015 dev_info(&client->dev, "PWM configuration looks "
1016 "sane, won't touch\n");
1019 return 1;
1022 /* Called when we have found a new IT87. */
1023 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1025 int tmp, i;
1027 /* initialize to sane defaults:
1028 * - if the chip is in manual pwm mode, this will be overwritten with
1029 * the actual settings on the chip (so in this case, initialization
1030 * is not needed)
1031 * - if in automatic or on/off mode, we could switch to manual mode,
1032 * read the registers and set manual_pwm_ctl accordingly, but currently
1033 * this is not implemented, so we initialize to something sane */
1034 for (i = 0; i < 3; i++) {
1035 data->manual_pwm_ctl[i] = 0xff;
1038 /* Check if temperature channnels are reset manually or by some reason */
1039 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1040 if ((tmp & 0x3f) == 0) {
1041 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1042 tmp = (tmp & 0xc0) | 0x2a;
1043 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1045 data->sensor = tmp;
1047 /* Check if voltage monitors are reset manually or by some reason */
1048 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1049 if ((tmp & 0xff) == 0) {
1050 /* Enable all voltage monitors */
1051 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1054 /* Check if tachometers are reset manually or by some reason */
1055 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1056 if ((data->fan_main_ctrl & 0x70) == 0) {
1057 /* Enable all fan tachometers */
1058 data->fan_main_ctrl |= 0x70;
1059 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1062 /* Set current fan mode registers and the default settings for the
1063 * other mode registers */
1064 for (i = 0; i < 3; i++) {
1065 if (data->fan_main_ctrl & (1 << i)) {
1066 /* pwm mode */
1067 tmp = it87_read_value(client, IT87_REG_PWM(i));
1068 if (tmp & 0x80) {
1069 /* automatic pwm - not yet implemented, but
1070 * leave the settings made by the BIOS alone
1071 * until a change is requested via the sysfs
1072 * interface */
1073 } else {
1074 /* manual pwm */
1075 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1080 /* Start monitoring */
1081 it87_write_value(client, IT87_REG_CONFIG,
1082 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1083 | (update_vbat ? 0x41 : 0x01));
1086 static struct it87_data *it87_update_device(struct device *dev)
1088 struct i2c_client *client = to_i2c_client(dev);
1089 struct it87_data *data = i2c_get_clientdata(client);
1090 int i;
1092 down(&data->update_lock);
1094 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1095 || !data->valid) {
1097 if (update_vbat) {
1098 /* Cleared after each update, so reenable. Value
1099 returned by this read will be previous value */
1100 it87_write_value(client, IT87_REG_CONFIG,
1101 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1103 for (i = 0; i <= 7; i++) {
1104 data->in[i] =
1105 it87_read_value(client, IT87_REG_VIN(i));
1106 data->in_min[i] =
1107 it87_read_value(client, IT87_REG_VIN_MIN(i));
1108 data->in_max[i] =
1109 it87_read_value(client, IT87_REG_VIN_MAX(i));
1111 data->in[8] =
1112 it87_read_value(client, IT87_REG_VIN(8));
1113 /* Temperature sensor doesn't have limit registers, set
1114 to min and max value */
1115 data->in_min[8] = 0;
1116 data->in_max[8] = 255;
1118 for (i = 0; i < 3; i++) {
1119 data->fan[i] =
1120 it87_read_value(client, IT87_REG_FAN(i));
1121 data->fan_min[i] =
1122 it87_read_value(client, IT87_REG_FAN_MIN(i));
1124 for (i = 0; i < 3; i++) {
1125 data->temp[i] =
1126 it87_read_value(client, IT87_REG_TEMP(i));
1127 data->temp_high[i] =
1128 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1129 data->temp_low[i] =
1130 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1133 i = it87_read_value(client, IT87_REG_FAN_DIV);
1134 data->fan_div[0] = i & 0x07;
1135 data->fan_div[1] = (i >> 3) & 0x07;
1136 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1138 data->alarms =
1139 it87_read_value(client, IT87_REG_ALARM1) |
1140 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1141 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1142 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1144 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1145 /* The 8705 does not have VID capability */
1146 if (data->type == it8712) {
1147 data->vid = it87_read_value(client, IT87_REG_VID);
1148 data->vid &= 0x1f;
1150 data->last_updated = jiffies;
1151 data->valid = 1;
1154 up(&data->update_lock);
1156 return data;
1159 static int __init sm_it87_init(void)
1161 int addr;
1163 if (!it87_find(&addr)) {
1164 normal_isa[0] = addr;
1166 return i2c_add_driver(&it87_driver);
1169 static void __exit sm_it87_exit(void)
1171 i2c_del_driver(&it87_driver);
1175 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1176 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1177 module_param(update_vbat, bool, 0);
1178 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1179 module_param(fix_pwm_polarity, bool, 0);
1180 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1181 MODULE_LICENSE("GPL");
1183 module_init(sm_it87_init);
1184 module_exit(sm_it87_exit);