[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / i2c / chips / it87.c
blobcf7e6898754f6dffe9a0bad6b77230f2922a37c9
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/config.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/jiffies.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-sensor.h>
41 #include <linux/i2c-vid.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 ALARMS_FROM_REG(val) (val)
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 semaphore lock;
198 enum chips type;
200 struct semaphore update_lock;
201 char valid; /* !=0 if following fields are valid */
202 unsigned long last_updated; /* In jiffies */
204 u8 in[9]; /* Register value */
205 u8 in_max[9]; /* Register value */
206 u8 in_min[9]; /* Register value */
207 u8 fan[3]; /* Register value */
208 u8 fan_min[3]; /* Register value */
209 u8 temp[3]; /* Register value */
210 u8 temp_high[3]; /* Register value */
211 u8 temp_low[3]; /* Register value */
212 u8 sensor; /* Register value */
213 u8 fan_div[3]; /* Register encoding, shifted right */
214 u8 vid; /* Register encoding, combined */
215 int vrm;
216 u32 alarms; /* Register encoding, combined */
217 u8 fan_main_ctrl; /* Register value */
218 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
222 static int it87_attach_adapter(struct i2c_adapter *adapter);
223 static int it87_find(int *address);
224 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
225 static int it87_detach_client(struct i2c_client *client);
227 static int it87_read_value(struct i2c_client *client, u8 register);
228 static int it87_write_value(struct i2c_client *client, u8 register,
229 u8 value);
230 static struct it87_data *it87_update_device(struct device *dev);
231 static int it87_check_pwm(struct i2c_client *client);
232 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
235 static struct i2c_driver it87_driver = {
236 .owner = THIS_MODULE,
237 .name = "it87",
238 .id = I2C_DRIVERID_IT87,
239 .flags = I2C_DF_NOTIFY,
240 .attach_adapter = it87_attach_adapter,
241 .detach_client = it87_detach_client,
244 static ssize_t show_in(struct device *dev, char *buf, int nr)
246 struct it87_data *data = it87_update_device(dev);
247 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
250 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
252 struct it87_data *data = it87_update_device(dev);
253 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
256 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
258 struct it87_data *data = it87_update_device(dev);
259 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
262 static ssize_t set_in_min(struct device *dev, const char *buf,
263 size_t count, int nr)
265 struct i2c_client *client = to_i2c_client(dev);
266 struct it87_data *data = i2c_get_clientdata(client);
267 unsigned long val = simple_strtoul(buf, NULL, 10);
269 down(&data->update_lock);
270 data->in_min[nr] = IN_TO_REG(val);
271 it87_write_value(client, IT87_REG_VIN_MIN(nr),
272 data->in_min[nr]);
273 up(&data->update_lock);
274 return count;
276 static ssize_t set_in_max(struct device *dev, const char *buf,
277 size_t count, int nr)
279 struct i2c_client *client = to_i2c_client(dev);
280 struct it87_data *data = i2c_get_clientdata(client);
281 unsigned long val = simple_strtoul(buf, NULL, 10);
283 down(&data->update_lock);
284 data->in_max[nr] = IN_TO_REG(val);
285 it87_write_value(client, IT87_REG_VIN_MAX(nr),
286 data->in_max[nr]);
287 up(&data->update_lock);
288 return count;
291 #define show_in_offset(offset) \
292 static ssize_t \
293 show_in##offset (struct device *dev, char *buf) \
295 return show_in(dev, buf, offset); \
297 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
299 #define limit_in_offset(offset) \
300 static ssize_t \
301 show_in##offset##_min (struct device *dev, char *buf) \
303 return show_in_min(dev, buf, offset); \
305 static ssize_t \
306 show_in##offset##_max (struct device *dev, char *buf) \
308 return show_in_max(dev, buf, offset); \
310 static ssize_t set_in##offset##_min (struct device *dev, \
311 const char *buf, size_t count) \
313 return set_in_min(dev, buf, count, offset); \
315 static ssize_t set_in##offset##_max (struct device *dev, \
316 const char *buf, size_t count) \
318 return set_in_max(dev, buf, count, offset); \
320 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
321 show_in##offset##_min, set_in##offset##_min); \
322 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
323 show_in##offset##_max, set_in##offset##_max);
325 show_in_offset(0);
326 limit_in_offset(0);
327 show_in_offset(1);
328 limit_in_offset(1);
329 show_in_offset(2);
330 limit_in_offset(2);
331 show_in_offset(3);
332 limit_in_offset(3);
333 show_in_offset(4);
334 limit_in_offset(4);
335 show_in_offset(5);
336 limit_in_offset(5);
337 show_in_offset(6);
338 limit_in_offset(6);
339 show_in_offset(7);
340 limit_in_offset(7);
341 show_in_offset(8);
343 /* 3 temperatures */
344 static ssize_t show_temp(struct device *dev, char *buf, int nr)
346 struct it87_data *data = it87_update_device(dev);
347 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
349 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
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, char *buf, int nr)
356 struct it87_data *data = it87_update_device(dev);
357 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
359 static ssize_t set_temp_max(struct device *dev, const char *buf,
360 size_t count, int nr)
362 struct i2c_client *client = to_i2c_client(dev);
363 struct it87_data *data = i2c_get_clientdata(client);
364 int val = simple_strtol(buf, NULL, 10);
366 down(&data->update_lock);
367 data->temp_high[nr] = TEMP_TO_REG(val);
368 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
369 up(&data->update_lock);
370 return count;
372 static ssize_t set_temp_min(struct device *dev, const char *buf,
373 size_t count, int nr)
375 struct i2c_client *client = to_i2c_client(dev);
376 struct it87_data *data = i2c_get_clientdata(client);
377 int val = simple_strtol(buf, NULL, 10);
379 down(&data->update_lock);
380 data->temp_low[nr] = TEMP_TO_REG(val);
381 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
382 up(&data->update_lock);
383 return count;
385 #define show_temp_offset(offset) \
386 static ssize_t show_temp_##offset (struct device *dev, char *buf) \
388 return show_temp(dev, buf, offset - 1); \
390 static ssize_t \
391 show_temp_##offset##_max (struct device *dev, char *buf) \
393 return show_temp_max(dev, buf, offset - 1); \
395 static ssize_t \
396 show_temp_##offset##_min (struct device *dev, char *buf) \
398 return show_temp_min(dev, buf, offset - 1); \
400 static ssize_t set_temp_##offset##_max (struct device *dev, \
401 const char *buf, size_t count) \
403 return set_temp_max(dev, buf, count, offset - 1); \
405 static ssize_t set_temp_##offset##_min (struct device *dev, \
406 const char *buf, size_t count) \
408 return set_temp_min(dev, buf, count, offset - 1); \
410 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
411 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
412 show_temp_##offset##_max, set_temp_##offset##_max); \
413 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
414 show_temp_##offset##_min, set_temp_##offset##_min);
416 show_temp_offset(1);
417 show_temp_offset(2);
418 show_temp_offset(3);
420 static ssize_t show_sensor(struct device *dev, char *buf, int nr)
422 struct it87_data *data = it87_update_device(dev);
423 u8 reg = data->sensor; /* In case the value is updated while we use it */
425 if (reg & (1 << nr))
426 return sprintf(buf, "3\n"); /* thermal diode */
427 if (reg & (8 << nr))
428 return sprintf(buf, "2\n"); /* thermistor */
429 return sprintf(buf, "0\n"); /* disabled */
431 static ssize_t set_sensor(struct device *dev, const char *buf,
432 size_t count, int nr)
434 struct i2c_client *client = to_i2c_client(dev);
435 struct it87_data *data = i2c_get_clientdata(client);
436 int val = simple_strtol(buf, NULL, 10);
438 down(&data->update_lock);
440 data->sensor &= ~(1 << nr);
441 data->sensor &= ~(8 << nr);
442 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
443 if (val == 3)
444 data->sensor |= 1 << nr;
445 else if (val == 2)
446 data->sensor |= 8 << nr;
447 else if (val != 0) {
448 up(&data->update_lock);
449 return -EINVAL;
451 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
452 up(&data->update_lock);
453 return count;
455 #define show_sensor_offset(offset) \
456 static ssize_t show_sensor_##offset (struct device *dev, char *buf) \
458 return show_sensor(dev, buf, offset - 1); \
460 static ssize_t set_sensor_##offset (struct device *dev, \
461 const char *buf, size_t count) \
463 return set_sensor(dev, buf, count, offset - 1); \
465 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
466 show_sensor_##offset, set_sensor_##offset);
468 show_sensor_offset(1);
469 show_sensor_offset(2);
470 show_sensor_offset(3);
472 /* 3 Fans */
473 static ssize_t show_fan(struct device *dev, char *buf, int nr)
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, char *buf, int nr)
481 struct it87_data *data = it87_update_device(dev);
482 return sprintf(buf,"%d\n",
483 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
485 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
487 struct it87_data *data = it87_update_device(dev);
488 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
490 static ssize_t show_pwm_enable(struct device *dev, char *buf, int nr)
492 struct it87_data *data = it87_update_device(dev);
493 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
495 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
497 struct it87_data *data = it87_update_device(dev);
498 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
500 static ssize_t set_fan_min(struct device *dev, const char *buf,
501 size_t count, int nr)
503 struct i2c_client *client = to_i2c_client(dev);
504 struct it87_data *data = i2c_get_clientdata(client);
505 int val = simple_strtol(buf, NULL, 10);
507 down(&data->update_lock);
508 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
509 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
510 up(&data->update_lock);
511 return count;
513 static ssize_t set_fan_div(struct device *dev, const char *buf,
514 size_t count, int nr)
516 struct i2c_client *client = to_i2c_client(dev);
517 struct it87_data *data = i2c_get_clientdata(client);
518 int val = simple_strtol(buf, NULL, 10);
519 int i, min[3];
520 u8 old;
522 down(&data->update_lock);
523 old = it87_read_value(client, IT87_REG_FAN_DIV);
525 for (i = 0; i < 3; i++)
526 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
528 switch (nr) {
529 case 0:
530 case 1:
531 data->fan_div[nr] = DIV_TO_REG(val);
532 break;
533 case 2:
534 if (val < 8)
535 data->fan_div[nr] = 1;
536 else
537 data->fan_div[nr] = 3;
539 val = old & 0x80;
540 val |= (data->fan_div[0] & 0x07);
541 val |= (data->fan_div[1] & 0x07) << 3;
542 if (data->fan_div[2] == 3)
543 val |= 0x1 << 6;
544 it87_write_value(client, IT87_REG_FAN_DIV, val);
546 for (i = 0; i < 3; i++) {
547 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
548 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
550 up(&data->update_lock);
551 return count;
553 static ssize_t set_pwm_enable(struct device *dev, const char *buf,
554 size_t count, int nr)
556 struct i2c_client *client = to_i2c_client(dev);
557 struct it87_data *data = i2c_get_clientdata(client);
558 int val = simple_strtol(buf, NULL, 10);
560 down(&data->update_lock);
562 if (val == 0) {
563 int tmp;
564 /* make sure the fan is on when in on/off mode */
565 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
566 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
567 /* set on/off mode */
568 data->fan_main_ctrl &= ~(1 << nr);
569 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
570 } else if (val == 1) {
571 /* set SmartGuardian mode */
572 data->fan_main_ctrl |= (1 << nr);
573 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
574 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
575 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
576 } else {
577 up(&data->update_lock);
578 return -EINVAL;
581 up(&data->update_lock);
582 return count;
584 static ssize_t set_pwm(struct device *dev, const char *buf,
585 size_t count, int nr)
587 struct i2c_client *client = to_i2c_client(dev);
588 struct it87_data *data = i2c_get_clientdata(client);
589 int val = simple_strtol(buf, NULL, 10);
591 if (val < 0 || val > 255)
592 return -EINVAL;
594 down(&data->update_lock);
595 data->manual_pwm_ctl[nr] = val;
596 if (data->fan_main_ctrl & (1 << nr))
597 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
598 up(&data->update_lock);
599 return count;
602 #define show_fan_offset(offset) \
603 static ssize_t show_fan_##offset (struct device *dev, char *buf) \
605 return show_fan(dev, buf, offset - 1); \
607 static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
609 return show_fan_min(dev, buf, offset - 1); \
611 static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
613 return show_fan_div(dev, buf, offset - 1); \
615 static ssize_t set_fan_##offset##_min (struct device *dev, \
616 const char *buf, size_t count) \
618 return set_fan_min(dev, buf, count, offset - 1); \
620 static ssize_t set_fan_##offset##_div (struct device *dev, \
621 const char *buf, size_t count) \
623 return set_fan_div(dev, buf, count, offset - 1); \
625 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
626 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
627 show_fan_##offset##_min, set_fan_##offset##_min); \
628 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
629 show_fan_##offset##_div, set_fan_##offset##_div);
631 show_fan_offset(1);
632 show_fan_offset(2);
633 show_fan_offset(3);
635 #define show_pwm_offset(offset) \
636 static ssize_t show_pwm##offset##_enable (struct device *dev, \
637 char *buf) \
639 return show_pwm_enable(dev, buf, offset - 1); \
641 static ssize_t show_pwm##offset (struct device *dev, char *buf) \
643 return show_pwm(dev, buf, offset - 1); \
645 static ssize_t set_pwm##offset##_enable (struct device *dev, \
646 const char *buf, size_t count) \
648 return set_pwm_enable(dev, buf, count, offset - 1); \
650 static ssize_t set_pwm##offset (struct device *dev, \
651 const char *buf, size_t count) \
653 return set_pwm(dev, buf, count, offset - 1); \
655 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
656 show_pwm##offset##_enable, \
657 set_pwm##offset##_enable); \
658 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
659 show_pwm##offset , set_pwm##offset );
661 show_pwm_offset(1);
662 show_pwm_offset(2);
663 show_pwm_offset(3);
665 /* Alarms */
666 static ssize_t show_alarms(struct device *dev, char *buf)
668 struct it87_data *data = it87_update_device(dev);
669 return sprintf(buf,"%d\n", ALARMS_FROM_REG(data->alarms));
671 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
673 static ssize_t
674 show_vrm_reg(struct device *dev, char *buf)
676 struct it87_data *data = it87_update_device(dev);
677 return sprintf(buf, "%ld\n", (long) data->vrm);
679 static ssize_t
680 store_vrm_reg(struct device *dev, const char *buf, size_t count)
682 struct i2c_client *client = to_i2c_client(dev);
683 struct it87_data *data = i2c_get_clientdata(client);
684 u32 val;
686 val = simple_strtoul(buf, NULL, 10);
687 data->vrm = val;
689 return count;
691 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
692 #define device_create_file_vrm(client) \
693 device_create_file(&client->dev, &dev_attr_vrm)
695 static ssize_t
696 show_vid_reg(struct device *dev, char *buf)
698 struct it87_data *data = it87_update_device(dev);
699 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
701 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
702 #define device_create_file_vid(client) \
703 device_create_file(&client->dev, &dev_attr_cpu0_vid)
705 /* This function is called when:
706 * it87_driver is inserted (when this module is loaded), for each
707 available adapter
708 * when a new adapter is inserted (and it87_driver is still present) */
709 static int it87_attach_adapter(struct i2c_adapter *adapter)
711 if (!(adapter->class & I2C_CLASS_HWMON))
712 return 0;
713 return i2c_detect(adapter, &addr_data, it87_detect);
716 /* SuperIO detection - will change normal_isa[0] if a chip is found */
717 static int it87_find(int *address)
719 int err = -ENODEV;
721 superio_enter();
722 chip_type = superio_inw(DEVID);
723 if (chip_type != IT8712F_DEVID
724 && chip_type != IT8705F_DEVID)
725 goto exit;
727 superio_select();
728 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
729 pr_info("it87: Device not activated, skipping\n");
730 goto exit;
733 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
734 if (*address == 0) {
735 pr_info("it87: Base address not set, skipping\n");
736 goto exit;
739 err = 0;
740 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
741 chip_type, *address, superio_inb(DEVREV) & 0x0f);
743 exit:
744 superio_exit();
745 return err;
748 /* This function is called by i2c_detect */
749 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
751 int i;
752 struct i2c_client *new_client;
753 struct it87_data *data;
754 int err = 0;
755 const char *name = "";
756 int is_isa = i2c_is_isa_adapter(adapter);
757 int enable_pwm_interface;
759 if (!is_isa &&
760 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
761 goto ERROR0;
763 /* Reserve the ISA region */
764 if (is_isa)
765 if (!request_region(address, IT87_EXTENT, it87_driver.name))
766 goto ERROR0;
768 /* Probe whether there is anything available on this address. Already
769 done for SMBus and Super-I/O clients */
770 if (kind < 0) {
771 if (is_isa && !chip_type) {
772 #define REALLY_SLOW_IO
773 /* We need the timeouts for at least some IT87-like chips. But only
774 if we read 'undefined' registers. */
775 i = inb_p(address + 1);
776 if (inb_p(address + 2) != i
777 || inb_p(address + 3) != i
778 || inb_p(address + 7) != i) {
779 err = -ENODEV;
780 goto ERROR1;
782 #undef REALLY_SLOW_IO
784 /* Let's just hope nothing breaks here */
785 i = inb_p(address + 5) & 0x7f;
786 outb_p(~i & 0x7f, address + 5);
787 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
788 outb_p(i, address + 5);
789 err = -ENODEV;
790 goto ERROR1;
795 /* OK. For now, we presume we have a valid client. We now create the
796 client structure, even though we cannot fill it completely yet.
797 But it allows us to access it87_{read,write}_value. */
799 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
800 err = -ENOMEM;
801 goto ERROR1;
803 memset(data, 0, sizeof(struct it87_data));
805 new_client = &data->client;
806 if (is_isa)
807 init_MUTEX(&data->lock);
808 i2c_set_clientdata(new_client, data);
809 new_client->addr = address;
810 new_client->adapter = adapter;
811 new_client->driver = &it87_driver;
812 new_client->flags = 0;
814 /* Now, we do the remaining detection. */
816 if (kind < 0) {
817 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
818 || (!is_isa
819 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
820 err = -ENODEV;
821 goto ERROR2;
825 /* Determine the chip type. */
826 if (kind <= 0) {
827 i = it87_read_value(new_client, IT87_REG_CHIPID);
828 if (i == 0x90) {
829 kind = it87;
830 if ((is_isa) && (chip_type == IT8712F_DEVID))
831 kind = it8712;
833 else {
834 if (kind == 0)
835 dev_info(&adapter->dev,
836 "Ignoring 'force' parameter for unknown chip at "
837 "adapter %d, address 0x%02x\n",
838 i2c_adapter_id(adapter), address);
839 err = -ENODEV;
840 goto ERROR2;
844 if (kind == it87) {
845 name = "it87";
846 } else if (kind == it8712) {
847 name = "it8712";
850 /* Fill in the remaining client fields and put it into the global list */
851 strlcpy(new_client->name, name, I2C_NAME_SIZE);
852 data->type = kind;
853 data->valid = 0;
854 init_MUTEX(&data->update_lock);
856 /* Tell the I2C layer a new client has arrived */
857 if ((err = i2c_attach_client(new_client)))
858 goto ERROR2;
860 /* Check PWM configuration */
861 enable_pwm_interface = it87_check_pwm(new_client);
863 /* Initialize the IT87 chip */
864 it87_init_client(new_client, data);
866 /* Register sysfs hooks */
867 device_create_file(&new_client->dev, &dev_attr_in0_input);
868 device_create_file(&new_client->dev, &dev_attr_in1_input);
869 device_create_file(&new_client->dev, &dev_attr_in2_input);
870 device_create_file(&new_client->dev, &dev_attr_in3_input);
871 device_create_file(&new_client->dev, &dev_attr_in4_input);
872 device_create_file(&new_client->dev, &dev_attr_in5_input);
873 device_create_file(&new_client->dev, &dev_attr_in6_input);
874 device_create_file(&new_client->dev, &dev_attr_in7_input);
875 device_create_file(&new_client->dev, &dev_attr_in8_input);
876 device_create_file(&new_client->dev, &dev_attr_in0_min);
877 device_create_file(&new_client->dev, &dev_attr_in1_min);
878 device_create_file(&new_client->dev, &dev_attr_in2_min);
879 device_create_file(&new_client->dev, &dev_attr_in3_min);
880 device_create_file(&new_client->dev, &dev_attr_in4_min);
881 device_create_file(&new_client->dev, &dev_attr_in5_min);
882 device_create_file(&new_client->dev, &dev_attr_in6_min);
883 device_create_file(&new_client->dev, &dev_attr_in7_min);
884 device_create_file(&new_client->dev, &dev_attr_in0_max);
885 device_create_file(&new_client->dev, &dev_attr_in1_max);
886 device_create_file(&new_client->dev, &dev_attr_in2_max);
887 device_create_file(&new_client->dev, &dev_attr_in3_max);
888 device_create_file(&new_client->dev, &dev_attr_in4_max);
889 device_create_file(&new_client->dev, &dev_attr_in5_max);
890 device_create_file(&new_client->dev, &dev_attr_in6_max);
891 device_create_file(&new_client->dev, &dev_attr_in7_max);
892 device_create_file(&new_client->dev, &dev_attr_temp1_input);
893 device_create_file(&new_client->dev, &dev_attr_temp2_input);
894 device_create_file(&new_client->dev, &dev_attr_temp3_input);
895 device_create_file(&new_client->dev, &dev_attr_temp1_max);
896 device_create_file(&new_client->dev, &dev_attr_temp2_max);
897 device_create_file(&new_client->dev, &dev_attr_temp3_max);
898 device_create_file(&new_client->dev, &dev_attr_temp1_min);
899 device_create_file(&new_client->dev, &dev_attr_temp2_min);
900 device_create_file(&new_client->dev, &dev_attr_temp3_min);
901 device_create_file(&new_client->dev, &dev_attr_temp1_type);
902 device_create_file(&new_client->dev, &dev_attr_temp2_type);
903 device_create_file(&new_client->dev, &dev_attr_temp3_type);
904 device_create_file(&new_client->dev, &dev_attr_fan1_input);
905 device_create_file(&new_client->dev, &dev_attr_fan2_input);
906 device_create_file(&new_client->dev, &dev_attr_fan3_input);
907 device_create_file(&new_client->dev, &dev_attr_fan1_min);
908 device_create_file(&new_client->dev, &dev_attr_fan2_min);
909 device_create_file(&new_client->dev, &dev_attr_fan3_min);
910 device_create_file(&new_client->dev, &dev_attr_fan1_div);
911 device_create_file(&new_client->dev, &dev_attr_fan2_div);
912 device_create_file(&new_client->dev, &dev_attr_fan3_div);
913 device_create_file(&new_client->dev, &dev_attr_alarms);
914 if (enable_pwm_interface) {
915 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
916 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
917 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
918 device_create_file(&new_client->dev, &dev_attr_pwm1);
919 device_create_file(&new_client->dev, &dev_attr_pwm2);
920 device_create_file(&new_client->dev, &dev_attr_pwm3);
923 if (data->type == it8712) {
924 data->vrm = i2c_which_vrm();
925 device_create_file_vrm(new_client);
926 device_create_file_vid(new_client);
929 return 0;
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 int err;
944 if ((err = i2c_detach_client(client))) {
945 dev_err(&client->dev,
946 "Client deregistration failed, client not detached.\n");
947 return err;
950 if(i2c_is_isa_client(client))
951 release_region(client->addr, IT87_EXTENT);
952 kfree(i2c_get_clientdata(client));
954 return 0;
957 /* The SMBus locks itself, but ISA access must be locked explicitely!
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 explicitely!
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;
1187 if (!it87_find(&addr)) {
1188 normal_isa[0] = addr;
1190 return i2c_add_driver(&it87_driver);
1193 static void __exit sm_it87_exit(void)
1195 i2c_del_driver(&it87_driver);
1199 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1200 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1201 module_param(update_vbat, bool, 0);
1202 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1203 module_param(fix_pwm_polarity, bool, 0);
1204 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1205 MODULE_LICENSE("GPL");
1207 module_init(sm_it87_init);
1208 module_exit(sm_it87_exit);