[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / hwmon / it87.c
blob53cc2b6d63854a835e661a0b2921640b6e36e915
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-isa.h>
40 #include <linux/hwmon.h>
41 #include <linux/hwmon-sysfs.h>
42 #include <linux/hwmon-vid.h>
43 #include <linux/err.h>
44 #include <asm/io.h>
47 /* Addresses to scan */
48 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
49 0x2e, 0x2f, I2C_CLIENT_END };
50 static unsigned short isa_address = 0x290;
52 /* Insmod parameters */
53 I2C_CLIENT_INSMOD_2(it87, it8712);
55 #define REG 0x2e /* The register to read/write */
56 #define DEV 0x07 /* Register: Logical device select */
57 #define VAL 0x2f /* The value to read/write */
58 #define PME 0x04 /* The device with the fan registers in it */
59 #define DEVID 0x20 /* Register: Device ID */
60 #define DEVREV 0x22 /* Register: Device Revision */
62 static inline int
63 superio_inb(int reg)
65 outb(reg, REG);
66 return inb(VAL);
69 static int superio_inw(int reg)
71 int val;
72 outb(reg++, REG);
73 val = inb(VAL) << 8;
74 outb(reg, REG);
75 val |= inb(VAL);
76 return val;
79 static inline void
80 superio_select(void)
82 outb(DEV, REG);
83 outb(PME, VAL);
86 static inline void
87 superio_enter(void)
89 outb(0x87, REG);
90 outb(0x01, REG);
91 outb(0x55, REG);
92 outb(0x55, REG);
95 static inline void
96 superio_exit(void)
98 outb(0x02, REG);
99 outb(0x02, VAL);
102 #define IT8712F_DEVID 0x8712
103 #define IT8705F_DEVID 0x8705
104 #define IT87_ACT_REG 0x30
105 #define IT87_BASE_REG 0x60
107 /* Update battery voltage after every reading if true */
108 static int update_vbat;
110 /* Not all BIOSes properly configure the PWM registers */
111 static int fix_pwm_polarity;
113 /* Chip Type */
115 static u16 chip_type;
117 /* Many IT87 constants specified below */
119 /* Length of ISA address segment */
120 #define IT87_EXTENT 8
122 /* Where are the ISA address/data registers relative to the base address */
123 #define IT87_ADDR_REG_OFFSET 5
124 #define IT87_DATA_REG_OFFSET 6
126 /*----- The IT87 registers -----*/
128 #define IT87_REG_CONFIG 0x00
130 #define IT87_REG_ALARM1 0x01
131 #define IT87_REG_ALARM2 0x02
132 #define IT87_REG_ALARM3 0x03
134 #define IT87_REG_VID 0x0a
135 #define IT87_REG_FAN_DIV 0x0b
137 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
139 #define IT87_REG_FAN(nr) (0x0d + (nr))
140 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
141 #define IT87_REG_FAN_MAIN_CTRL 0x13
142 #define IT87_REG_FAN_CTL 0x14
143 #define IT87_REG_PWM(nr) (0x15 + (nr))
145 #define IT87_REG_VIN(nr) (0x20 + (nr))
146 #define IT87_REG_TEMP(nr) (0x29 + (nr))
148 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
149 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
150 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
151 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
153 #define IT87_REG_I2C_ADDR 0x48
155 #define IT87_REG_VIN_ENABLE 0x50
156 #define IT87_REG_TEMP_ENABLE 0x51
158 #define IT87_REG_CHIPID 0x58
160 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
161 #define IN_FROM_REG(val) ((val) * 16)
163 static inline u8 FAN_TO_REG(long rpm, int div)
165 if (rpm == 0)
166 return 255;
167 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
168 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
169 254);
172 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
174 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
175 ((val)+500)/1000),-128,127))
176 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
178 #define PWM_TO_REG(val) ((val) >> 1)
179 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
181 static int DIV_TO_REG(int val)
183 int answer = 0;
184 while ((val >>= 1) != 0)
185 answer++;
186 return answer;
188 #define DIV_FROM_REG(val) (1 << (val))
191 /* For each registered IT87, we need to keep some data in memory. That
192 data is pointed to by it87_list[NR]->data. The structure itself is
193 dynamically allocated, at the same time when a new it87 client is
194 allocated. */
195 struct it87_data {
196 struct i2c_client client;
197 struct class_device *class_dev;
198 struct semaphore lock;
199 enum chips type;
201 struct semaphore update_lock;
202 char valid; /* !=0 if following fields are valid */
203 unsigned long last_updated; /* In jiffies */
205 u8 in[9]; /* Register value */
206 u8 in_max[9]; /* Register value */
207 u8 in_min[9]; /* Register value */
208 u8 fan[3]; /* Register value */
209 u8 fan_min[3]; /* Register value */
210 u8 temp[3]; /* Register value */
211 u8 temp_high[3]; /* Register value */
212 u8 temp_low[3]; /* Register value */
213 u8 sensor; /* Register value */
214 u8 fan_div[3]; /* Register encoding, shifted right */
215 u8 vid; /* Register encoding, combined */
216 int vrm;
217 u32 alarms; /* Register encoding, combined */
218 u8 fan_main_ctrl; /* Register value */
219 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
223 static int it87_attach_adapter(struct i2c_adapter *adapter);
224 static int it87_isa_attach_adapter(struct i2c_adapter *adapter);
225 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
226 static int it87_detach_client(struct i2c_client *client);
228 static int it87_read_value(struct i2c_client *client, u8 register);
229 static int it87_write_value(struct i2c_client *client, u8 register,
230 u8 value);
231 static struct it87_data *it87_update_device(struct device *dev);
232 static int it87_check_pwm(struct i2c_client *client);
233 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
236 static struct i2c_driver it87_driver = {
237 .owner = THIS_MODULE,
238 .name = "it87",
239 .id = I2C_DRIVERID_IT87,
240 .flags = I2C_DF_NOTIFY,
241 .attach_adapter = it87_attach_adapter,
242 .detach_client = it87_detach_client,
245 static struct i2c_driver it87_isa_driver = {
246 .owner = THIS_MODULE,
247 .name = "it87-isa",
248 .attach_adapter = it87_isa_attach_adapter,
249 .detach_client = it87_detach_client,
253 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
254 char *buf)
256 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
257 int nr = sensor_attr->index;
259 struct it87_data *data = it87_update_device(dev);
260 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
263 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
264 char *buf)
266 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
267 int nr = sensor_attr->index;
269 struct it87_data *data = it87_update_device(dev);
270 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
273 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
274 char *buf)
276 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
277 int nr = sensor_attr->index;
279 struct it87_data *data = it87_update_device(dev);
280 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
283 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
284 const char *buf, size_t count)
286 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
287 int nr = sensor_attr->index;
289 struct i2c_client *client = to_i2c_client(dev);
290 struct it87_data *data = i2c_get_clientdata(client);
291 unsigned long val = simple_strtoul(buf, NULL, 10);
293 down(&data->update_lock);
294 data->in_min[nr] = IN_TO_REG(val);
295 it87_write_value(client, IT87_REG_VIN_MIN(nr),
296 data->in_min[nr]);
297 up(&data->update_lock);
298 return count;
300 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
301 const char *buf, size_t count)
303 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
304 int nr = sensor_attr->index;
306 struct i2c_client *client = to_i2c_client(dev);
307 struct it87_data *data = i2c_get_clientdata(client);
308 unsigned long val = simple_strtoul(buf, NULL, 10);
310 down(&data->update_lock);
311 data->in_max[nr] = IN_TO_REG(val);
312 it87_write_value(client, IT87_REG_VIN_MAX(nr),
313 data->in_max[nr]);
314 up(&data->update_lock);
315 return count;
318 #define show_in_offset(offset) \
319 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
320 show_in, NULL, offset);
322 #define limit_in_offset(offset) \
323 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
324 show_in_min, set_in_min, offset); \
325 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
326 show_in_max, set_in_max, offset);
328 show_in_offset(0);
329 limit_in_offset(0);
330 show_in_offset(1);
331 limit_in_offset(1);
332 show_in_offset(2);
333 limit_in_offset(2);
334 show_in_offset(3);
335 limit_in_offset(3);
336 show_in_offset(4);
337 limit_in_offset(4);
338 show_in_offset(5);
339 limit_in_offset(5);
340 show_in_offset(6);
341 limit_in_offset(6);
342 show_in_offset(7);
343 limit_in_offset(7);
344 show_in_offset(8);
346 /* 3 temperatures */
347 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
348 char *buf)
350 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
351 int nr = sensor_attr->index;
353 struct it87_data *data = it87_update_device(dev);
354 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
356 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
357 char *buf)
359 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
360 int nr = sensor_attr->index;
362 struct it87_data *data = it87_update_device(dev);
363 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
365 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
366 char *buf)
368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369 int nr = sensor_attr->index;
371 struct it87_data *data = it87_update_device(dev);
372 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
374 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
375 const char *buf, size_t count)
377 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
378 int nr = sensor_attr->index;
380 struct i2c_client *client = to_i2c_client(dev);
381 struct it87_data *data = i2c_get_clientdata(client);
382 int val = simple_strtol(buf, NULL, 10);
384 down(&data->update_lock);
385 data->temp_high[nr] = TEMP_TO_REG(val);
386 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
387 up(&data->update_lock);
388 return count;
390 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
391 const char *buf, size_t count)
393 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
394 int nr = sensor_attr->index;
396 struct i2c_client *client = to_i2c_client(dev);
397 struct it87_data *data = i2c_get_clientdata(client);
398 int val = simple_strtol(buf, NULL, 10);
400 down(&data->update_lock);
401 data->temp_low[nr] = TEMP_TO_REG(val);
402 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
403 up(&data->update_lock);
404 return count;
406 #define show_temp_offset(offset) \
407 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
408 show_temp, NULL, offset - 1); \
409 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
410 show_temp_max, set_temp_max, offset - 1); \
411 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
412 show_temp_min, set_temp_min, offset - 1);
414 show_temp_offset(1);
415 show_temp_offset(2);
416 show_temp_offset(3);
418 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
419 char *buf)
421 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
422 int nr = sensor_attr->index;
424 struct it87_data *data = it87_update_device(dev);
425 u8 reg = data->sensor; /* In case the value is updated while we use it */
427 if (reg & (1 << nr))
428 return sprintf(buf, "3\n"); /* thermal diode */
429 if (reg & (8 << nr))
430 return sprintf(buf, "2\n"); /* thermistor */
431 return sprintf(buf, "0\n"); /* disabled */
433 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
436 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
437 int nr = sensor_attr->index;
439 struct i2c_client *client = to_i2c_client(dev);
440 struct it87_data *data = i2c_get_clientdata(client);
441 int val = simple_strtol(buf, NULL, 10);
443 down(&data->update_lock);
445 data->sensor &= ~(1 << nr);
446 data->sensor &= ~(8 << nr);
447 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
448 if (val == 3)
449 data->sensor |= 1 << nr;
450 else if (val == 2)
451 data->sensor |= 8 << nr;
452 else if (val != 0) {
453 up(&data->update_lock);
454 return -EINVAL;
456 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
457 up(&data->update_lock);
458 return count;
460 #define show_sensor_offset(offset) \
461 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
462 show_sensor, set_sensor, offset - 1);
464 show_sensor_offset(1);
465 show_sensor_offset(2);
466 show_sensor_offset(3);
468 /* 3 Fans */
469 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
470 char *buf)
472 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
473 int nr = sensor_attr->index;
475 struct it87_data *data = it87_update_device(dev);
476 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
477 DIV_FROM_REG(data->fan_div[nr])));
479 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
480 char *buf)
482 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
483 int nr = sensor_attr->index;
485 struct it87_data *data = it87_update_device(dev);
486 return sprintf(buf,"%d\n",
487 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
489 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
490 char *buf)
492 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
493 int nr = sensor_attr->index;
495 struct it87_data *data = it87_update_device(dev);
496 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
498 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
499 char *buf)
501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
504 struct it87_data *data = it87_update_device(dev);
505 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
507 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
508 char *buf)
510 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
511 int nr = sensor_attr->index;
513 struct it87_data *data = it87_update_device(dev);
514 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
516 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
517 const char *buf, size_t count)
519 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
520 int nr = sensor_attr->index;
522 struct i2c_client *client = to_i2c_client(dev);
523 struct it87_data *data = i2c_get_clientdata(client);
524 int val = simple_strtol(buf, NULL, 10);
526 down(&data->update_lock);
527 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
528 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
529 up(&data->update_lock);
530 return count;
532 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
533 const char *buf, size_t count)
535 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
536 int nr = sensor_attr->index;
538 struct i2c_client *client = to_i2c_client(dev);
539 struct it87_data *data = i2c_get_clientdata(client);
540 int val = simple_strtol(buf, NULL, 10);
541 int i, min[3];
542 u8 old;
544 down(&data->update_lock);
545 old = it87_read_value(client, IT87_REG_FAN_DIV);
547 for (i = 0; i < 3; i++)
548 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
550 switch (nr) {
551 case 0:
552 case 1:
553 data->fan_div[nr] = DIV_TO_REG(val);
554 break;
555 case 2:
556 if (val < 8)
557 data->fan_div[nr] = 1;
558 else
559 data->fan_div[nr] = 3;
561 val = old & 0x80;
562 val |= (data->fan_div[0] & 0x07);
563 val |= (data->fan_div[1] & 0x07) << 3;
564 if (data->fan_div[2] == 3)
565 val |= 0x1 << 6;
566 it87_write_value(client, IT87_REG_FAN_DIV, val);
568 for (i = 0; i < 3; i++) {
569 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
570 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
572 up(&data->update_lock);
573 return count;
575 static ssize_t set_pwm_enable(struct device *dev,
576 struct device_attribute *attr, const char *buf, size_t count)
578 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
579 int nr = sensor_attr->index;
581 struct i2c_client *client = to_i2c_client(dev);
582 struct it87_data *data = i2c_get_clientdata(client);
583 int val = simple_strtol(buf, NULL, 10);
585 down(&data->update_lock);
587 if (val == 0) {
588 int tmp;
589 /* make sure the fan is on when in on/off mode */
590 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
591 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
592 /* set on/off mode */
593 data->fan_main_ctrl &= ~(1 << nr);
594 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
595 } else if (val == 1) {
596 /* set SmartGuardian mode */
597 data->fan_main_ctrl |= (1 << nr);
598 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
599 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
600 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
601 } else {
602 up(&data->update_lock);
603 return -EINVAL;
606 up(&data->update_lock);
607 return count;
609 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
610 const char *buf, size_t count)
612 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
613 int nr = sensor_attr->index;
615 struct i2c_client *client = to_i2c_client(dev);
616 struct it87_data *data = i2c_get_clientdata(client);
617 int val = simple_strtol(buf, NULL, 10);
619 if (val < 0 || val > 255)
620 return -EINVAL;
622 down(&data->update_lock);
623 data->manual_pwm_ctl[nr] = val;
624 if (data->fan_main_ctrl & (1 << nr))
625 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
626 up(&data->update_lock);
627 return count;
630 #define show_fan_offset(offset) \
631 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
632 show_fan, NULL, offset - 1); \
633 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
634 show_fan_min, set_fan_min, offset - 1); \
635 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
636 show_fan_div, set_fan_div, offset - 1);
638 show_fan_offset(1);
639 show_fan_offset(2);
640 show_fan_offset(3);
642 #define show_pwm_offset(offset) \
643 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
644 show_pwm_enable, set_pwm_enable, offset - 1); \
645 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
646 show_pwm, set_pwm, offset - 1);
648 show_pwm_offset(1);
649 show_pwm_offset(2);
650 show_pwm_offset(3);
652 /* Alarms */
653 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
655 struct it87_data *data = it87_update_device(dev);
656 return sprintf(buf, "%u\n", data->alarms);
658 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
660 static ssize_t
661 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
663 struct it87_data *data = it87_update_device(dev);
664 return sprintf(buf, "%ld\n", (long) data->vrm);
666 static ssize_t
667 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
669 struct i2c_client *client = to_i2c_client(dev);
670 struct it87_data *data = i2c_get_clientdata(client);
671 u32 val;
673 val = simple_strtoul(buf, NULL, 10);
674 data->vrm = val;
676 return count;
678 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
679 #define device_create_file_vrm(client) \
680 device_create_file(&client->dev, &dev_attr_vrm)
682 static ssize_t
683 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
685 struct it87_data *data = it87_update_device(dev);
686 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
688 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
689 #define device_create_file_vid(client) \
690 device_create_file(&client->dev, &dev_attr_cpu0_vid)
692 /* This function is called when:
693 * it87_driver is inserted (when this module is loaded), for each
694 available adapter
695 * when a new adapter is inserted (and it87_driver is still present) */
696 static int it87_attach_adapter(struct i2c_adapter *adapter)
698 if (!(adapter->class & I2C_CLASS_HWMON))
699 return 0;
700 return i2c_probe(adapter, &addr_data, it87_detect);
703 static int it87_isa_attach_adapter(struct i2c_adapter *adapter)
705 return it87_detect(adapter, isa_address, -1);
708 /* SuperIO detection - will change isa_address if a chip is found */
709 static int __init it87_find(int *address)
711 int err = -ENODEV;
713 superio_enter();
714 chip_type = superio_inw(DEVID);
715 if (chip_type != IT8712F_DEVID
716 && chip_type != IT8705F_DEVID)
717 goto exit;
719 superio_select();
720 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
721 pr_info("it87: Device not activated, skipping\n");
722 goto exit;
725 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
726 if (*address == 0) {
727 pr_info("it87: Base address not set, skipping\n");
728 goto exit;
731 err = 0;
732 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
733 chip_type, *address, superio_inb(DEVREV) & 0x0f);
735 exit:
736 superio_exit();
737 return err;
740 /* This function is called by i2c_probe */
741 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
743 int i;
744 struct i2c_client *new_client;
745 struct it87_data *data;
746 int err = 0;
747 const char *name = "";
748 int is_isa = i2c_is_isa_adapter(adapter);
749 int enable_pwm_interface;
751 if (!is_isa &&
752 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
753 goto ERROR0;
755 /* Reserve the ISA region */
756 if (is_isa)
757 if (!request_region(address, IT87_EXTENT, it87_isa_driver.name))
758 goto ERROR0;
760 /* Probe whether there is anything available on this address. Already
761 done for SMBus and Super-I/O clients */
762 if (kind < 0) {
763 if (is_isa && !chip_type) {
764 #define REALLY_SLOW_IO
765 /* We need the timeouts for at least some IT87-like chips. But only
766 if we read 'undefined' registers. */
767 i = inb_p(address + 1);
768 if (inb_p(address + 2) != i
769 || inb_p(address + 3) != i
770 || inb_p(address + 7) != i) {
771 err = -ENODEV;
772 goto ERROR1;
774 #undef REALLY_SLOW_IO
776 /* Let's just hope nothing breaks here */
777 i = inb_p(address + 5) & 0x7f;
778 outb_p(~i & 0x7f, address + 5);
779 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
780 outb_p(i, address + 5);
781 err = -ENODEV;
782 goto ERROR1;
787 /* OK. For now, we presume we have a valid client. We now create the
788 client structure, even though we cannot fill it completely yet.
789 But it allows us to access it87_{read,write}_value. */
791 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
792 err = -ENOMEM;
793 goto ERROR1;
795 memset(data, 0, sizeof(struct it87_data));
797 new_client = &data->client;
798 if (is_isa)
799 init_MUTEX(&data->lock);
800 i2c_set_clientdata(new_client, data);
801 new_client->addr = address;
802 new_client->adapter = adapter;
803 new_client->driver = is_isa ? &it87_isa_driver : &it87_driver;
804 new_client->flags = 0;
806 /* Now, we do the remaining detection. */
808 if (kind < 0) {
809 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
810 || (!is_isa
811 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
812 err = -ENODEV;
813 goto ERROR2;
817 /* Determine the chip type. */
818 if (kind <= 0) {
819 i = it87_read_value(new_client, IT87_REG_CHIPID);
820 if (i == 0x90) {
821 kind = it87;
822 if ((is_isa) && (chip_type == IT8712F_DEVID))
823 kind = it8712;
825 else {
826 if (kind == 0)
827 dev_info(&adapter->dev,
828 "Ignoring 'force' parameter for unknown chip at "
829 "adapter %d, address 0x%02x\n",
830 i2c_adapter_id(adapter), address);
831 err = -ENODEV;
832 goto ERROR2;
836 if (kind == it87) {
837 name = "it87";
838 } else if (kind == it8712) {
839 name = "it8712";
842 /* Fill in the remaining client fields and put it into the global list */
843 strlcpy(new_client->name, name, I2C_NAME_SIZE);
844 data->type = kind;
845 data->valid = 0;
846 init_MUTEX(&data->update_lock);
848 /* Tell the I2C layer a new client has arrived */
849 if ((err = i2c_attach_client(new_client)))
850 goto ERROR2;
852 /* Check PWM configuration */
853 enable_pwm_interface = it87_check_pwm(new_client);
855 /* Initialize the IT87 chip */
856 it87_init_client(new_client, data);
858 /* Register sysfs hooks */
859 data->class_dev = hwmon_device_register(&new_client->dev);
860 if (IS_ERR(data->class_dev)) {
861 err = PTR_ERR(data->class_dev);
862 goto ERROR3;
865 device_create_file(&new_client->dev, &sensor_dev_attr_in0_input.dev_attr);
866 device_create_file(&new_client->dev, &sensor_dev_attr_in1_input.dev_attr);
867 device_create_file(&new_client->dev, &sensor_dev_attr_in2_input.dev_attr);
868 device_create_file(&new_client->dev, &sensor_dev_attr_in3_input.dev_attr);
869 device_create_file(&new_client->dev, &sensor_dev_attr_in4_input.dev_attr);
870 device_create_file(&new_client->dev, &sensor_dev_attr_in5_input.dev_attr);
871 device_create_file(&new_client->dev, &sensor_dev_attr_in6_input.dev_attr);
872 device_create_file(&new_client->dev, &sensor_dev_attr_in7_input.dev_attr);
873 device_create_file(&new_client->dev, &sensor_dev_attr_in8_input.dev_attr);
874 device_create_file(&new_client->dev, &sensor_dev_attr_in0_min.dev_attr);
875 device_create_file(&new_client->dev, &sensor_dev_attr_in1_min.dev_attr);
876 device_create_file(&new_client->dev, &sensor_dev_attr_in2_min.dev_attr);
877 device_create_file(&new_client->dev, &sensor_dev_attr_in3_min.dev_attr);
878 device_create_file(&new_client->dev, &sensor_dev_attr_in4_min.dev_attr);
879 device_create_file(&new_client->dev, &sensor_dev_attr_in5_min.dev_attr);
880 device_create_file(&new_client->dev, &sensor_dev_attr_in6_min.dev_attr);
881 device_create_file(&new_client->dev, &sensor_dev_attr_in7_min.dev_attr);
882 device_create_file(&new_client->dev, &sensor_dev_attr_in0_max.dev_attr);
883 device_create_file(&new_client->dev, &sensor_dev_attr_in1_max.dev_attr);
884 device_create_file(&new_client->dev, &sensor_dev_attr_in2_max.dev_attr);
885 device_create_file(&new_client->dev, &sensor_dev_attr_in3_max.dev_attr);
886 device_create_file(&new_client->dev, &sensor_dev_attr_in4_max.dev_attr);
887 device_create_file(&new_client->dev, &sensor_dev_attr_in5_max.dev_attr);
888 device_create_file(&new_client->dev, &sensor_dev_attr_in6_max.dev_attr);
889 device_create_file(&new_client->dev, &sensor_dev_attr_in7_max.dev_attr);
890 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_input.dev_attr);
891 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_input.dev_attr);
892 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_input.dev_attr);
893 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_max.dev_attr);
894 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_max.dev_attr);
895 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_max.dev_attr);
896 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_min.dev_attr);
897 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_min.dev_attr);
898 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_min.dev_attr);
899 device_create_file(&new_client->dev, &sensor_dev_attr_temp1_type.dev_attr);
900 device_create_file(&new_client->dev, &sensor_dev_attr_temp2_type.dev_attr);
901 device_create_file(&new_client->dev, &sensor_dev_attr_temp3_type.dev_attr);
902 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_input.dev_attr);
903 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_input.dev_attr);
904 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_input.dev_attr);
905 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_min.dev_attr);
906 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_min.dev_attr);
907 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_min.dev_attr);
908 device_create_file(&new_client->dev, &sensor_dev_attr_fan1_div.dev_attr);
909 device_create_file(&new_client->dev, &sensor_dev_attr_fan2_div.dev_attr);
910 device_create_file(&new_client->dev, &sensor_dev_attr_fan3_div.dev_attr);
911 device_create_file(&new_client->dev, &dev_attr_alarms);
912 if (enable_pwm_interface) {
913 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1_enable.dev_attr);
914 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2_enable.dev_attr);
915 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3_enable.dev_attr);
916 device_create_file(&new_client->dev, &sensor_dev_attr_pwm1.dev_attr);
917 device_create_file(&new_client->dev, &sensor_dev_attr_pwm2.dev_attr);
918 device_create_file(&new_client->dev, &sensor_dev_attr_pwm3.dev_attr);
921 if (data->type == it8712) {
922 data->vrm = vid_which_vrm();
923 device_create_file_vrm(new_client);
924 device_create_file_vid(new_client);
927 return 0;
929 ERROR3:
930 i2c_detach_client(new_client);
931 ERROR2:
932 kfree(data);
933 ERROR1:
934 if (is_isa)
935 release_region(address, IT87_EXTENT);
936 ERROR0:
937 return err;
940 static int it87_detach_client(struct i2c_client *client)
942 struct it87_data *data = i2c_get_clientdata(client);
943 int err;
945 hwmon_device_unregister(data->class_dev);
947 if ((err = i2c_detach_client(client)))
948 return err;
950 if(i2c_is_isa_client(client))
951 release_region(client->addr, IT87_EXTENT);
952 kfree(data);
954 return 0;
957 /* The SMBus locks itself, but ISA access must be locked explicitly!
958 We don't want to lock the whole ISA bus, so we lock each client
959 separately.
960 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
961 would slow down the IT87 access and should not be necessary. */
962 static int it87_read_value(struct i2c_client *client, u8 reg)
964 struct it87_data *data = i2c_get_clientdata(client);
966 int res;
967 if (i2c_is_isa_client(client)) {
968 down(&data->lock);
969 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
970 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
971 up(&data->lock);
972 return res;
973 } else
974 return i2c_smbus_read_byte_data(client, reg);
977 /* The SMBus locks itself, but ISA access muse be locked explicitly!
978 We don't want to lock the whole ISA bus, so we lock each client
979 separately.
980 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
981 would slow down the IT87 access and should not be necessary. */
982 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
984 struct it87_data *data = i2c_get_clientdata(client);
986 if (i2c_is_isa_client(client)) {
987 down(&data->lock);
988 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
989 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
990 up(&data->lock);
991 return 0;
992 } else
993 return i2c_smbus_write_byte_data(client, reg, value);
996 /* Return 1 if and only if the PWM interface is safe to use */
997 static int it87_check_pwm(struct i2c_client *client)
999 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1000 * and polarity set to active low is sign that this is the case so we
1001 * disable pwm control to protect the user. */
1002 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1003 if ((tmp & 0x87) == 0) {
1004 if (fix_pwm_polarity) {
1005 /* The user asks us to attempt a chip reconfiguration.
1006 * This means switching to active high polarity and
1007 * inverting all fan speed values. */
1008 int i;
1009 u8 pwm[3];
1011 for (i = 0; i < 3; i++)
1012 pwm[i] = it87_read_value(client,
1013 IT87_REG_PWM(i));
1015 /* If any fan is in automatic pwm mode, the polarity
1016 * might be correct, as suspicious as it seems, so we
1017 * better don't change anything (but still disable the
1018 * PWM interface). */
1019 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1020 dev_info(&client->dev, "Reconfiguring PWM to "
1021 "active high polarity\n");
1022 it87_write_value(client, IT87_REG_FAN_CTL,
1023 tmp | 0x87);
1024 for (i = 0; i < 3; i++)
1025 it87_write_value(client,
1026 IT87_REG_PWM(i),
1027 0x7f & ~pwm[i]);
1028 return 1;
1031 dev_info(&client->dev, "PWM configuration is "
1032 "too broken to be fixed\n");
1035 dev_info(&client->dev, "Detected broken BIOS "
1036 "defaults, disabling PWM interface\n");
1037 return 0;
1038 } else if (fix_pwm_polarity) {
1039 dev_info(&client->dev, "PWM configuration looks "
1040 "sane, won't touch\n");
1043 return 1;
1046 /* Called when we have found a new IT87. */
1047 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1049 int tmp, i;
1051 /* initialize to sane defaults:
1052 * - if the chip is in manual pwm mode, this will be overwritten with
1053 * the actual settings on the chip (so in this case, initialization
1054 * is not needed)
1055 * - if in automatic or on/off mode, we could switch to manual mode,
1056 * read the registers and set manual_pwm_ctl accordingly, but currently
1057 * this is not implemented, so we initialize to something sane */
1058 for (i = 0; i < 3; i++) {
1059 data->manual_pwm_ctl[i] = 0xff;
1062 /* Check if temperature channnels are reset manually or by some reason */
1063 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1064 if ((tmp & 0x3f) == 0) {
1065 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1066 tmp = (tmp & 0xc0) | 0x2a;
1067 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1069 data->sensor = tmp;
1071 /* Check if voltage monitors are reset manually or by some reason */
1072 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1073 if ((tmp & 0xff) == 0) {
1074 /* Enable all voltage monitors */
1075 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1078 /* Check if tachometers are reset manually or by some reason */
1079 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1080 if ((data->fan_main_ctrl & 0x70) == 0) {
1081 /* Enable all fan tachometers */
1082 data->fan_main_ctrl |= 0x70;
1083 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1086 /* Set current fan mode registers and the default settings for the
1087 * other mode registers */
1088 for (i = 0; i < 3; i++) {
1089 if (data->fan_main_ctrl & (1 << i)) {
1090 /* pwm mode */
1091 tmp = it87_read_value(client, IT87_REG_PWM(i));
1092 if (tmp & 0x80) {
1093 /* automatic pwm - not yet implemented, but
1094 * leave the settings made by the BIOS alone
1095 * until a change is requested via the sysfs
1096 * interface */
1097 } else {
1098 /* manual pwm */
1099 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1104 /* Start monitoring */
1105 it87_write_value(client, IT87_REG_CONFIG,
1106 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1107 | (update_vbat ? 0x41 : 0x01));
1110 static struct it87_data *it87_update_device(struct device *dev)
1112 struct i2c_client *client = to_i2c_client(dev);
1113 struct it87_data *data = i2c_get_clientdata(client);
1114 int i;
1116 down(&data->update_lock);
1118 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1119 || !data->valid) {
1121 if (update_vbat) {
1122 /* Cleared after each update, so reenable. Value
1123 returned by this read will be previous value */
1124 it87_write_value(client, IT87_REG_CONFIG,
1125 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1127 for (i = 0; i <= 7; i++) {
1128 data->in[i] =
1129 it87_read_value(client, IT87_REG_VIN(i));
1130 data->in_min[i] =
1131 it87_read_value(client, IT87_REG_VIN_MIN(i));
1132 data->in_max[i] =
1133 it87_read_value(client, IT87_REG_VIN_MAX(i));
1135 data->in[8] =
1136 it87_read_value(client, IT87_REG_VIN(8));
1137 /* Temperature sensor doesn't have limit registers, set
1138 to min and max value */
1139 data->in_min[8] = 0;
1140 data->in_max[8] = 255;
1142 for (i = 0; i < 3; i++) {
1143 data->fan[i] =
1144 it87_read_value(client, IT87_REG_FAN(i));
1145 data->fan_min[i] =
1146 it87_read_value(client, IT87_REG_FAN_MIN(i));
1148 for (i = 0; i < 3; i++) {
1149 data->temp[i] =
1150 it87_read_value(client, IT87_REG_TEMP(i));
1151 data->temp_high[i] =
1152 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1153 data->temp_low[i] =
1154 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1157 i = it87_read_value(client, IT87_REG_FAN_DIV);
1158 data->fan_div[0] = i & 0x07;
1159 data->fan_div[1] = (i >> 3) & 0x07;
1160 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1162 data->alarms =
1163 it87_read_value(client, IT87_REG_ALARM1) |
1164 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1165 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1166 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1168 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1169 /* The 8705 does not have VID capability */
1170 if (data->type == it8712) {
1171 data->vid = it87_read_value(client, IT87_REG_VID);
1172 data->vid &= 0x1f;
1174 data->last_updated = jiffies;
1175 data->valid = 1;
1178 up(&data->update_lock);
1180 return data;
1183 static int __init sm_it87_init(void)
1185 int addr, res;
1187 if (!it87_find(&addr)) {
1188 isa_address = addr;
1191 res = i2c_add_driver(&it87_driver);
1192 if (res)
1193 return res;
1195 res = i2c_isa_add_driver(&it87_isa_driver);
1196 if (res) {
1197 i2c_del_driver(&it87_driver);
1198 return res;
1201 return 0;
1204 static void __exit sm_it87_exit(void)
1206 i2c_isa_del_driver(&it87_isa_driver);
1207 i2c_del_driver(&it87_driver);
1211 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1212 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1213 module_param(update_vbat, bool, 0);
1214 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1215 module_param(fix_pwm_polarity, bool, 0);
1216 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1217 MODULE_LICENSE("GPL");
1219 module_init(sm_it87_init);
1220 module_exit(sm_it87_exit);