[PATCH] I2C: Kill common macro abuse in chip drivers
[linux-2.6/verdex.git] / drivers / i2c / chips / it87.c
blob7984bf920d3138b34a563971290a44dfcc1aa3b8
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 <asm/io.h>
44 /* Addresses to scan */
45 static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
46 0x2e, 0x2f, I2C_CLIENT_END };
47 static unsigned int normal_isa[] = { 0x0290, I2C_CLIENT_ISA_END };
49 /* Insmod parameters */
50 SENSORS_INSMOD_2(it87, it8712);
52 #define REG 0x2e /* The register to read/write */
53 #define DEV 0x07 /* Register: Logical device select */
54 #define VAL 0x2f /* The value to read/write */
55 #define PME 0x04 /* The device with the fan registers in it */
56 #define DEVID 0x20 /* Register: Device ID */
57 #define DEVREV 0x22 /* Register: Device Revision */
59 static inline int
60 superio_inb(int reg)
62 outb(reg, REG);
63 return inb(VAL);
66 static int superio_inw(int reg)
68 int val;
69 outb(reg++, REG);
70 val = inb(VAL) << 8;
71 outb(reg, REG);
72 val |= inb(VAL);
73 return val;
76 static inline void
77 superio_select(void)
79 outb(DEV, REG);
80 outb(PME, VAL);
83 static inline void
84 superio_enter(void)
86 outb(0x87, REG);
87 outb(0x01, REG);
88 outb(0x55, REG);
89 outb(0x55, REG);
92 static inline void
93 superio_exit(void)
95 outb(0x02, REG);
96 outb(0x02, VAL);
99 #define IT8712F_DEVID 0x8712
100 #define IT8705F_DEVID 0x8705
101 #define IT87_ACT_REG 0x30
102 #define IT87_BASE_REG 0x60
104 /* Update battery voltage after every reading if true */
105 static int update_vbat;
107 /* Not all BIOSes properly configure the PWM registers */
108 static int fix_pwm_polarity;
110 /* Chip Type */
112 static u16 chip_type;
114 /* Many IT87 constants specified below */
116 /* Length of ISA address segment */
117 #define IT87_EXTENT 8
119 /* Where are the ISA address/data registers relative to the base address */
120 #define IT87_ADDR_REG_OFFSET 5
121 #define IT87_DATA_REG_OFFSET 6
123 /*----- The IT87 registers -----*/
125 #define IT87_REG_CONFIG 0x00
127 #define IT87_REG_ALARM1 0x01
128 #define IT87_REG_ALARM2 0x02
129 #define IT87_REG_ALARM3 0x03
131 #define IT87_REG_VID 0x0a
132 #define IT87_REG_FAN_DIV 0x0b
134 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
136 #define IT87_REG_FAN(nr) (0x0d + (nr))
137 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
138 #define IT87_REG_FAN_MAIN_CTRL 0x13
139 #define IT87_REG_FAN_CTL 0x14
140 #define IT87_REG_PWM(nr) (0x15 + (nr))
142 #define IT87_REG_VIN(nr) (0x20 + (nr))
143 #define IT87_REG_TEMP(nr) (0x29 + (nr))
145 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
146 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
147 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
148 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
150 #define IT87_REG_I2C_ADDR 0x48
152 #define IT87_REG_VIN_ENABLE 0x50
153 #define IT87_REG_TEMP_ENABLE 0x51
155 #define IT87_REG_CHIPID 0x58
157 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
158 #define IN_FROM_REG(val) ((val) * 16)
160 static inline u8 FAN_TO_REG(long rpm, int div)
162 if (rpm == 0)
163 return 255;
164 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
165 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
166 254);
169 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
171 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
172 ((val)+500)/1000),-128,127))
173 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
175 #define PWM_TO_REG(val) ((val) >> 1)
176 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
178 static int DIV_TO_REG(int val)
180 int answer = 0;
181 while ((val >>= 1) != 0)
182 answer++;
183 return answer;
185 #define DIV_FROM_REG(val) (1 << (val))
188 /* For each registered IT87, we need to keep some data in memory. That
189 data is pointed to by it87_list[NR]->data. The structure itself is
190 dynamically allocated, at the same time when a new it87 client is
191 allocated. */
192 struct it87_data {
193 struct i2c_client client;
194 struct semaphore lock;
195 enum chips type;
197 struct semaphore update_lock;
198 char valid; /* !=0 if following fields are valid */
199 unsigned long last_updated; /* In jiffies */
201 u8 in[9]; /* Register value */
202 u8 in_max[9]; /* Register value */
203 u8 in_min[9]; /* Register value */
204 u8 fan[3]; /* Register value */
205 u8 fan_min[3]; /* Register value */
206 u8 temp[3]; /* Register value */
207 u8 temp_high[3]; /* Register value */
208 u8 temp_low[3]; /* Register value */
209 u8 sensor; /* Register value */
210 u8 fan_div[3]; /* Register encoding, shifted right */
211 u8 vid; /* Register encoding, combined */
212 int vrm;
213 u32 alarms; /* Register encoding, combined */
214 u8 fan_main_ctrl; /* Register value */
215 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
219 static int it87_attach_adapter(struct i2c_adapter *adapter);
220 static int it87_find(int *address);
221 static int it87_detect(struct i2c_adapter *adapter, int address, int kind);
222 static int it87_detach_client(struct i2c_client *client);
224 static int it87_read_value(struct i2c_client *client, u8 register);
225 static int it87_write_value(struct i2c_client *client, u8 register,
226 u8 value);
227 static struct it87_data *it87_update_device(struct device *dev);
228 static int it87_check_pwm(struct i2c_client *client);
229 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
232 static struct i2c_driver it87_driver = {
233 .owner = THIS_MODULE,
234 .name = "it87",
235 .id = I2C_DRIVERID_IT87,
236 .flags = I2C_DF_NOTIFY,
237 .attach_adapter = it87_attach_adapter,
238 .detach_client = it87_detach_client,
241 static ssize_t show_in(struct device *dev, char *buf, int nr)
243 struct it87_data *data = it87_update_device(dev);
244 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
247 static ssize_t show_in_min(struct device *dev, char *buf, int nr)
249 struct it87_data *data = it87_update_device(dev);
250 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
253 static ssize_t show_in_max(struct device *dev, char *buf, int nr)
255 struct it87_data *data = it87_update_device(dev);
256 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
259 static ssize_t set_in_min(struct device *dev, const char *buf,
260 size_t count, int nr)
262 struct i2c_client *client = to_i2c_client(dev);
263 struct it87_data *data = i2c_get_clientdata(client);
264 unsigned long val = simple_strtoul(buf, NULL, 10);
266 down(&data->update_lock);
267 data->in_min[nr] = IN_TO_REG(val);
268 it87_write_value(client, IT87_REG_VIN_MIN(nr),
269 data->in_min[nr]);
270 up(&data->update_lock);
271 return count;
273 static ssize_t set_in_max(struct device *dev, const char *buf,
274 size_t count, int nr)
276 struct i2c_client *client = to_i2c_client(dev);
277 struct it87_data *data = i2c_get_clientdata(client);
278 unsigned long val = simple_strtoul(buf, NULL, 10);
280 down(&data->update_lock);
281 data->in_max[nr] = IN_TO_REG(val);
282 it87_write_value(client, IT87_REG_VIN_MAX(nr),
283 data->in_max[nr]);
284 up(&data->update_lock);
285 return count;
288 #define show_in_offset(offset) \
289 static ssize_t \
290 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
292 return show_in(dev, buf, offset); \
294 static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
296 #define limit_in_offset(offset) \
297 static ssize_t \
298 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
300 return show_in_min(dev, buf, offset); \
302 static ssize_t \
303 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
305 return show_in_max(dev, buf, offset); \
307 static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
308 const char *buf, size_t count) \
310 return set_in_min(dev, buf, count, offset); \
312 static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
313 const char *buf, size_t count) \
315 return set_in_max(dev, buf, count, offset); \
317 static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
318 show_in##offset##_min, set_in##offset##_min); \
319 static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
320 show_in##offset##_max, set_in##offset##_max);
322 show_in_offset(0);
323 limit_in_offset(0);
324 show_in_offset(1);
325 limit_in_offset(1);
326 show_in_offset(2);
327 limit_in_offset(2);
328 show_in_offset(3);
329 limit_in_offset(3);
330 show_in_offset(4);
331 limit_in_offset(4);
332 show_in_offset(5);
333 limit_in_offset(5);
334 show_in_offset(6);
335 limit_in_offset(6);
336 show_in_offset(7);
337 limit_in_offset(7);
338 show_in_offset(8);
340 /* 3 temperatures */
341 static ssize_t show_temp(struct device *dev, char *buf, int nr)
343 struct it87_data *data = it87_update_device(dev);
344 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
346 static ssize_t show_temp_max(struct device *dev, char *buf, int nr)
348 struct it87_data *data = it87_update_device(dev);
349 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
351 static ssize_t show_temp_min(struct device *dev, char *buf, int nr)
353 struct it87_data *data = it87_update_device(dev);
354 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
356 static ssize_t set_temp_max(struct device *dev, const char *buf,
357 size_t count, int nr)
359 struct i2c_client *client = to_i2c_client(dev);
360 struct it87_data *data = i2c_get_clientdata(client);
361 int val = simple_strtol(buf, NULL, 10);
363 down(&data->update_lock);
364 data->temp_high[nr] = TEMP_TO_REG(val);
365 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
366 up(&data->update_lock);
367 return count;
369 static ssize_t set_temp_min(struct device *dev, const char *buf,
370 size_t count, int nr)
372 struct i2c_client *client = to_i2c_client(dev);
373 struct it87_data *data = i2c_get_clientdata(client);
374 int val = simple_strtol(buf, NULL, 10);
376 down(&data->update_lock);
377 data->temp_low[nr] = TEMP_TO_REG(val);
378 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
379 up(&data->update_lock);
380 return count;
382 #define show_temp_offset(offset) \
383 static ssize_t show_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
385 return show_temp(dev, buf, offset - 1); \
387 static ssize_t \
388 show_temp_##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
390 return show_temp_max(dev, buf, offset - 1); \
392 static ssize_t \
393 show_temp_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
395 return show_temp_min(dev, buf, offset - 1); \
397 static ssize_t set_temp_##offset##_max (struct device *dev, struct device_attribute *attr, \
398 const char *buf, size_t count) \
400 return set_temp_max(dev, buf, count, offset - 1); \
402 static ssize_t set_temp_##offset##_min (struct device *dev, struct device_attribute *attr, \
403 const char *buf, size_t count) \
405 return set_temp_min(dev, buf, count, offset - 1); \
407 static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp_##offset, NULL); \
408 static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
409 show_temp_##offset##_max, set_temp_##offset##_max); \
410 static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
411 show_temp_##offset##_min, set_temp_##offset##_min);
413 show_temp_offset(1);
414 show_temp_offset(2);
415 show_temp_offset(3);
417 static ssize_t show_sensor(struct device *dev, char *buf, int nr)
419 struct it87_data *data = it87_update_device(dev);
420 u8 reg = data->sensor; /* In case the value is updated while we use it */
422 if (reg & (1 << nr))
423 return sprintf(buf, "3\n"); /* thermal diode */
424 if (reg & (8 << nr))
425 return sprintf(buf, "2\n"); /* thermistor */
426 return sprintf(buf, "0\n"); /* disabled */
428 static ssize_t set_sensor(struct device *dev, const char *buf,
429 size_t count, int nr)
431 struct i2c_client *client = to_i2c_client(dev);
432 struct it87_data *data = i2c_get_clientdata(client);
433 int val = simple_strtol(buf, NULL, 10);
435 down(&data->update_lock);
437 data->sensor &= ~(1 << nr);
438 data->sensor &= ~(8 << nr);
439 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
440 if (val == 3)
441 data->sensor |= 1 << nr;
442 else if (val == 2)
443 data->sensor |= 8 << nr;
444 else if (val != 0) {
445 up(&data->update_lock);
446 return -EINVAL;
448 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
449 up(&data->update_lock);
450 return count;
452 #define show_sensor_offset(offset) \
453 static ssize_t show_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
455 return show_sensor(dev, buf, offset - 1); \
457 static ssize_t set_sensor_##offset (struct device *dev, struct device_attribute *attr, \
458 const char *buf, size_t count) \
460 return set_sensor(dev, buf, count, offset - 1); \
462 static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
463 show_sensor_##offset, set_sensor_##offset);
465 show_sensor_offset(1);
466 show_sensor_offset(2);
467 show_sensor_offset(3);
469 /* 3 Fans */
470 static ssize_t show_fan(struct device *dev, char *buf, int nr)
472 struct it87_data *data = it87_update_device(dev);
473 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
474 DIV_FROM_REG(data->fan_div[nr])));
476 static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
478 struct it87_data *data = it87_update_device(dev);
479 return sprintf(buf,"%d\n",
480 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
482 static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
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, char *buf, int nr)
489 struct it87_data *data = it87_update_device(dev);
490 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
492 static ssize_t show_pwm(struct device *dev, char *buf, int nr)
494 struct it87_data *data = it87_update_device(dev);
495 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
497 static ssize_t set_fan_min(struct device *dev, const char *buf,
498 size_t count, int nr)
500 struct i2c_client *client = to_i2c_client(dev);
501 struct it87_data *data = i2c_get_clientdata(client);
502 int val = simple_strtol(buf, NULL, 10);
504 down(&data->update_lock);
505 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
506 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
507 up(&data->update_lock);
508 return count;
510 static ssize_t set_fan_div(struct device *dev, const char *buf,
511 size_t count, int nr)
513 struct i2c_client *client = to_i2c_client(dev);
514 struct it87_data *data = i2c_get_clientdata(client);
515 int val = simple_strtol(buf, NULL, 10);
516 int i, min[3];
517 u8 old;
519 down(&data->update_lock);
520 old = it87_read_value(client, IT87_REG_FAN_DIV);
522 for (i = 0; i < 3; i++)
523 min[i] = FAN_FROM_REG(data->fan_min[i], DIV_FROM_REG(data->fan_div[i]));
525 switch (nr) {
526 case 0:
527 case 1:
528 data->fan_div[nr] = DIV_TO_REG(val);
529 break;
530 case 2:
531 if (val < 8)
532 data->fan_div[nr] = 1;
533 else
534 data->fan_div[nr] = 3;
536 val = old & 0x80;
537 val |= (data->fan_div[0] & 0x07);
538 val |= (data->fan_div[1] & 0x07) << 3;
539 if (data->fan_div[2] == 3)
540 val |= 0x1 << 6;
541 it87_write_value(client, IT87_REG_FAN_DIV, val);
543 for (i = 0; i < 3; i++) {
544 data->fan_min[i]=FAN_TO_REG(min[i], DIV_FROM_REG(data->fan_div[i]));
545 it87_write_value(client, IT87_REG_FAN_MIN(i), data->fan_min[i]);
547 up(&data->update_lock);
548 return count;
550 static ssize_t set_pwm_enable(struct device *dev, const char *buf,
551 size_t count, int nr)
553 struct i2c_client *client = to_i2c_client(dev);
554 struct it87_data *data = i2c_get_clientdata(client);
555 int val = simple_strtol(buf, NULL, 10);
557 down(&data->update_lock);
559 if (val == 0) {
560 int tmp;
561 /* make sure the fan is on when in on/off mode */
562 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
563 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
564 /* set on/off mode */
565 data->fan_main_ctrl &= ~(1 << nr);
566 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
567 } else if (val == 1) {
568 /* set SmartGuardian mode */
569 data->fan_main_ctrl |= (1 << nr);
570 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
571 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
572 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
573 } else {
574 up(&data->update_lock);
575 return -EINVAL;
578 up(&data->update_lock);
579 return count;
581 static ssize_t set_pwm(struct device *dev, const char *buf,
582 size_t count, int nr)
584 struct i2c_client *client = to_i2c_client(dev);
585 struct it87_data *data = i2c_get_clientdata(client);
586 int val = simple_strtol(buf, NULL, 10);
588 if (val < 0 || val > 255)
589 return -EINVAL;
591 down(&data->update_lock);
592 data->manual_pwm_ctl[nr] = val;
593 if (data->fan_main_ctrl & (1 << nr))
594 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
595 up(&data->update_lock);
596 return count;
599 #define show_fan_offset(offset) \
600 static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
602 return show_fan(dev, buf, offset - 1); \
604 static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
606 return show_fan_min(dev, buf, offset - 1); \
608 static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
610 return show_fan_div(dev, buf, offset - 1); \
612 static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
613 const char *buf, size_t count) \
615 return set_fan_min(dev, buf, count, offset - 1); \
617 static ssize_t set_fan_##offset##_div (struct device *dev, struct device_attribute *attr, \
618 const char *buf, size_t count) \
620 return set_fan_div(dev, buf, count, offset - 1); \
622 static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL); \
623 static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
624 show_fan_##offset##_min, set_fan_##offset##_min); \
625 static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
626 show_fan_##offset##_div, set_fan_##offset##_div);
628 show_fan_offset(1);
629 show_fan_offset(2);
630 show_fan_offset(3);
632 #define show_pwm_offset(offset) \
633 static ssize_t show_pwm##offset##_enable (struct device *dev, struct device_attribute *attr, \
634 char *buf) \
636 return show_pwm_enable(dev, buf, offset - 1); \
638 static ssize_t show_pwm##offset (struct device *dev, struct device_attribute *attr, char *buf) \
640 return show_pwm(dev, buf, offset - 1); \
642 static ssize_t set_pwm##offset##_enable (struct device *dev, struct device_attribute *attr, \
643 const char *buf, size_t count) \
645 return set_pwm_enable(dev, buf, count, offset - 1); \
647 static ssize_t set_pwm##offset (struct device *dev, struct device_attribute *attr, \
648 const char *buf, size_t count) \
650 return set_pwm(dev, buf, count, offset - 1); \
652 static DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
653 show_pwm##offset##_enable, \
654 set_pwm##offset##_enable); \
655 static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
656 show_pwm##offset , set_pwm##offset );
658 show_pwm_offset(1);
659 show_pwm_offset(2);
660 show_pwm_offset(3);
662 /* Alarms */
663 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
665 struct it87_data *data = it87_update_device(dev);
666 return sprintf(buf, "%u\n", data->alarms);
668 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
670 static ssize_t
671 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
673 struct it87_data *data = it87_update_device(dev);
674 return sprintf(buf, "%ld\n", (long) data->vrm);
676 static ssize_t
677 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
679 struct i2c_client *client = to_i2c_client(dev);
680 struct it87_data *data = i2c_get_clientdata(client);
681 u32 val;
683 val = simple_strtoul(buf, NULL, 10);
684 data->vrm = val;
686 return count;
688 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
689 #define device_create_file_vrm(client) \
690 device_create_file(&client->dev, &dev_attr_vrm)
692 static ssize_t
693 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
695 struct it87_data *data = it87_update_device(dev);
696 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
698 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
699 #define device_create_file_vid(client) \
700 device_create_file(&client->dev, &dev_attr_cpu0_vid)
702 /* This function is called when:
703 * it87_driver is inserted (when this module is loaded), for each
704 available adapter
705 * when a new adapter is inserted (and it87_driver is still present) */
706 static int it87_attach_adapter(struct i2c_adapter *adapter)
708 if (!(adapter->class & I2C_CLASS_HWMON))
709 return 0;
710 return i2c_detect(adapter, &addr_data, it87_detect);
713 /* SuperIO detection - will change normal_isa[0] if a chip is found */
714 static int it87_find(int *address)
716 int err = -ENODEV;
718 superio_enter();
719 chip_type = superio_inw(DEVID);
720 if (chip_type != IT8712F_DEVID
721 && chip_type != IT8705F_DEVID)
722 goto exit;
724 superio_select();
725 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
726 pr_info("it87: Device not activated, skipping\n");
727 goto exit;
730 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
731 if (*address == 0) {
732 pr_info("it87: Base address not set, skipping\n");
733 goto exit;
736 err = 0;
737 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
738 chip_type, *address, superio_inb(DEVREV) & 0x0f);
740 exit:
741 superio_exit();
742 return err;
745 /* This function is called by i2c_detect */
746 int it87_detect(struct i2c_adapter *adapter, int address, int kind)
748 int i;
749 struct i2c_client *new_client;
750 struct it87_data *data;
751 int err = 0;
752 const char *name = "";
753 int is_isa = i2c_is_isa_adapter(adapter);
754 int enable_pwm_interface;
756 if (!is_isa &&
757 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
758 goto ERROR0;
760 /* Reserve the ISA region */
761 if (is_isa)
762 if (!request_region(address, IT87_EXTENT, it87_driver.name))
763 goto ERROR0;
765 /* Probe whether there is anything available on this address. Already
766 done for SMBus and Super-I/O clients */
767 if (kind < 0) {
768 if (is_isa && !chip_type) {
769 #define REALLY_SLOW_IO
770 /* We need the timeouts for at least some IT87-like chips. But only
771 if we read 'undefined' registers. */
772 i = inb_p(address + 1);
773 if (inb_p(address + 2) != i
774 || inb_p(address + 3) != i
775 || inb_p(address + 7) != i) {
776 err = -ENODEV;
777 goto ERROR1;
779 #undef REALLY_SLOW_IO
781 /* Let's just hope nothing breaks here */
782 i = inb_p(address + 5) & 0x7f;
783 outb_p(~i & 0x7f, address + 5);
784 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
785 outb_p(i, address + 5);
786 err = -ENODEV;
787 goto ERROR1;
792 /* OK. For now, we presume we have a valid client. We now create the
793 client structure, even though we cannot fill it completely yet.
794 But it allows us to access it87_{read,write}_value. */
796 if (!(data = kmalloc(sizeof(struct it87_data), GFP_KERNEL))) {
797 err = -ENOMEM;
798 goto ERROR1;
800 memset(data, 0, sizeof(struct it87_data));
802 new_client = &data->client;
803 if (is_isa)
804 init_MUTEX(&data->lock);
805 i2c_set_clientdata(new_client, data);
806 new_client->addr = address;
807 new_client->adapter = adapter;
808 new_client->driver = &it87_driver;
809 new_client->flags = 0;
811 /* Now, we do the remaining detection. */
813 if (kind < 0) {
814 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
815 || (!is_isa
816 && it87_read_value(new_client, IT87_REG_I2C_ADDR) != address)) {
817 err = -ENODEV;
818 goto ERROR2;
822 /* Determine the chip type. */
823 if (kind <= 0) {
824 i = it87_read_value(new_client, IT87_REG_CHIPID);
825 if (i == 0x90) {
826 kind = it87;
827 if ((is_isa) && (chip_type == IT8712F_DEVID))
828 kind = it8712;
830 else {
831 if (kind == 0)
832 dev_info(&adapter->dev,
833 "Ignoring 'force' parameter for unknown chip at "
834 "adapter %d, address 0x%02x\n",
835 i2c_adapter_id(adapter), address);
836 err = -ENODEV;
837 goto ERROR2;
841 if (kind == it87) {
842 name = "it87";
843 } else if (kind == it8712) {
844 name = "it8712";
847 /* Fill in the remaining client fields and put it into the global list */
848 strlcpy(new_client->name, name, I2C_NAME_SIZE);
849 data->type = kind;
850 data->valid = 0;
851 init_MUTEX(&data->update_lock);
853 /* Tell the I2C layer a new client has arrived */
854 if ((err = i2c_attach_client(new_client)))
855 goto ERROR2;
857 /* Check PWM configuration */
858 enable_pwm_interface = it87_check_pwm(new_client);
860 /* Initialize the IT87 chip */
861 it87_init_client(new_client, data);
863 /* Register sysfs hooks */
864 device_create_file(&new_client->dev, &dev_attr_in0_input);
865 device_create_file(&new_client->dev, &dev_attr_in1_input);
866 device_create_file(&new_client->dev, &dev_attr_in2_input);
867 device_create_file(&new_client->dev, &dev_attr_in3_input);
868 device_create_file(&new_client->dev, &dev_attr_in4_input);
869 device_create_file(&new_client->dev, &dev_attr_in5_input);
870 device_create_file(&new_client->dev, &dev_attr_in6_input);
871 device_create_file(&new_client->dev, &dev_attr_in7_input);
872 device_create_file(&new_client->dev, &dev_attr_in8_input);
873 device_create_file(&new_client->dev, &dev_attr_in0_min);
874 device_create_file(&new_client->dev, &dev_attr_in1_min);
875 device_create_file(&new_client->dev, &dev_attr_in2_min);
876 device_create_file(&new_client->dev, &dev_attr_in3_min);
877 device_create_file(&new_client->dev, &dev_attr_in4_min);
878 device_create_file(&new_client->dev, &dev_attr_in5_min);
879 device_create_file(&new_client->dev, &dev_attr_in6_min);
880 device_create_file(&new_client->dev, &dev_attr_in7_min);
881 device_create_file(&new_client->dev, &dev_attr_in0_max);
882 device_create_file(&new_client->dev, &dev_attr_in1_max);
883 device_create_file(&new_client->dev, &dev_attr_in2_max);
884 device_create_file(&new_client->dev, &dev_attr_in3_max);
885 device_create_file(&new_client->dev, &dev_attr_in4_max);
886 device_create_file(&new_client->dev, &dev_attr_in5_max);
887 device_create_file(&new_client->dev, &dev_attr_in6_max);
888 device_create_file(&new_client->dev, &dev_attr_in7_max);
889 device_create_file(&new_client->dev, &dev_attr_temp1_input);
890 device_create_file(&new_client->dev, &dev_attr_temp2_input);
891 device_create_file(&new_client->dev, &dev_attr_temp3_input);
892 device_create_file(&new_client->dev, &dev_attr_temp1_max);
893 device_create_file(&new_client->dev, &dev_attr_temp2_max);
894 device_create_file(&new_client->dev, &dev_attr_temp3_max);
895 device_create_file(&new_client->dev, &dev_attr_temp1_min);
896 device_create_file(&new_client->dev, &dev_attr_temp2_min);
897 device_create_file(&new_client->dev, &dev_attr_temp3_min);
898 device_create_file(&new_client->dev, &dev_attr_temp1_type);
899 device_create_file(&new_client->dev, &dev_attr_temp2_type);
900 device_create_file(&new_client->dev, &dev_attr_temp3_type);
901 device_create_file(&new_client->dev, &dev_attr_fan1_input);
902 device_create_file(&new_client->dev, &dev_attr_fan2_input);
903 device_create_file(&new_client->dev, &dev_attr_fan3_input);
904 device_create_file(&new_client->dev, &dev_attr_fan1_min);
905 device_create_file(&new_client->dev, &dev_attr_fan2_min);
906 device_create_file(&new_client->dev, &dev_attr_fan3_min);
907 device_create_file(&new_client->dev, &dev_attr_fan1_div);
908 device_create_file(&new_client->dev, &dev_attr_fan2_div);
909 device_create_file(&new_client->dev, &dev_attr_fan3_div);
910 device_create_file(&new_client->dev, &dev_attr_alarms);
911 if (enable_pwm_interface) {
912 device_create_file(&new_client->dev, &dev_attr_pwm1_enable);
913 device_create_file(&new_client->dev, &dev_attr_pwm2_enable);
914 device_create_file(&new_client->dev, &dev_attr_pwm3_enable);
915 device_create_file(&new_client->dev, &dev_attr_pwm1);
916 device_create_file(&new_client->dev, &dev_attr_pwm2);
917 device_create_file(&new_client->dev, &dev_attr_pwm3);
920 if (data->type == it8712) {
921 data->vrm = i2c_which_vrm();
922 device_create_file_vrm(new_client);
923 device_create_file_vid(new_client);
926 return 0;
928 ERROR2:
929 kfree(data);
930 ERROR1:
931 if (is_isa)
932 release_region(address, IT87_EXTENT);
933 ERROR0:
934 return err;
937 static int it87_detach_client(struct i2c_client *client)
939 int err;
941 if ((err = i2c_detach_client(client))) {
942 dev_err(&client->dev,
943 "Client deregistration failed, client not detached.\n");
944 return err;
947 if(i2c_is_isa_client(client))
948 release_region(client->addr, IT87_EXTENT);
949 kfree(i2c_get_clientdata(client));
951 return 0;
954 /* The SMBus locks itself, but ISA access must be locked explicitly!
955 We don't want to lock the whole ISA bus, so we lock each client
956 separately.
957 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
958 would slow down the IT87 access and should not be necessary. */
959 static int it87_read_value(struct i2c_client *client, u8 reg)
961 struct it87_data *data = i2c_get_clientdata(client);
963 int res;
964 if (i2c_is_isa_client(client)) {
965 down(&data->lock);
966 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
967 res = inb_p(client->addr + IT87_DATA_REG_OFFSET);
968 up(&data->lock);
969 return res;
970 } else
971 return i2c_smbus_read_byte_data(client, reg);
974 /* The SMBus locks itself, but ISA access muse be locked explicitly!
975 We don't want to lock the whole ISA bus, so we lock each client
976 separately.
977 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
978 would slow down the IT87 access and should not be necessary. */
979 static int it87_write_value(struct i2c_client *client, u8 reg, u8 value)
981 struct it87_data *data = i2c_get_clientdata(client);
983 if (i2c_is_isa_client(client)) {
984 down(&data->lock);
985 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
986 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
987 up(&data->lock);
988 return 0;
989 } else
990 return i2c_smbus_write_byte_data(client, reg, value);
993 /* Return 1 if and only if the PWM interface is safe to use */
994 static int it87_check_pwm(struct i2c_client *client)
996 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
997 * and polarity set to active low is sign that this is the case so we
998 * disable pwm control to protect the user. */
999 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1000 if ((tmp & 0x87) == 0) {
1001 if (fix_pwm_polarity) {
1002 /* The user asks us to attempt a chip reconfiguration.
1003 * This means switching to active high polarity and
1004 * inverting all fan speed values. */
1005 int i;
1006 u8 pwm[3];
1008 for (i = 0; i < 3; i++)
1009 pwm[i] = it87_read_value(client,
1010 IT87_REG_PWM(i));
1012 /* If any fan is in automatic pwm mode, the polarity
1013 * might be correct, as suspicious as it seems, so we
1014 * better don't change anything (but still disable the
1015 * PWM interface). */
1016 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1017 dev_info(&client->dev, "Reconfiguring PWM to "
1018 "active high polarity\n");
1019 it87_write_value(client, IT87_REG_FAN_CTL,
1020 tmp | 0x87);
1021 for (i = 0; i < 3; i++)
1022 it87_write_value(client,
1023 IT87_REG_PWM(i),
1024 0x7f & ~pwm[i]);
1025 return 1;
1028 dev_info(&client->dev, "PWM configuration is "
1029 "too broken to be fixed\n");
1032 dev_info(&client->dev, "Detected broken BIOS "
1033 "defaults, disabling PWM interface\n");
1034 return 0;
1035 } else if (fix_pwm_polarity) {
1036 dev_info(&client->dev, "PWM configuration looks "
1037 "sane, won't touch\n");
1040 return 1;
1043 /* Called when we have found a new IT87. */
1044 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1046 int tmp, i;
1048 /* initialize to sane defaults:
1049 * - if the chip is in manual pwm mode, this will be overwritten with
1050 * the actual settings on the chip (so in this case, initialization
1051 * is not needed)
1052 * - if in automatic or on/off mode, we could switch to manual mode,
1053 * read the registers and set manual_pwm_ctl accordingly, but currently
1054 * this is not implemented, so we initialize to something sane */
1055 for (i = 0; i < 3; i++) {
1056 data->manual_pwm_ctl[i] = 0xff;
1059 /* Check if temperature channnels are reset manually or by some reason */
1060 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1061 if ((tmp & 0x3f) == 0) {
1062 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1063 tmp = (tmp & 0xc0) | 0x2a;
1064 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1066 data->sensor = tmp;
1068 /* Check if voltage monitors are reset manually or by some reason */
1069 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1070 if ((tmp & 0xff) == 0) {
1071 /* Enable all voltage monitors */
1072 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1075 /* Check if tachometers are reset manually or by some reason */
1076 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1077 if ((data->fan_main_ctrl & 0x70) == 0) {
1078 /* Enable all fan tachometers */
1079 data->fan_main_ctrl |= 0x70;
1080 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1083 /* Set current fan mode registers and the default settings for the
1084 * other mode registers */
1085 for (i = 0; i < 3; i++) {
1086 if (data->fan_main_ctrl & (1 << i)) {
1087 /* pwm mode */
1088 tmp = it87_read_value(client, IT87_REG_PWM(i));
1089 if (tmp & 0x80) {
1090 /* automatic pwm - not yet implemented, but
1091 * leave the settings made by the BIOS alone
1092 * until a change is requested via the sysfs
1093 * interface */
1094 } else {
1095 /* manual pwm */
1096 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1101 /* Start monitoring */
1102 it87_write_value(client, IT87_REG_CONFIG,
1103 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1104 | (update_vbat ? 0x41 : 0x01));
1107 static struct it87_data *it87_update_device(struct device *dev)
1109 struct i2c_client *client = to_i2c_client(dev);
1110 struct it87_data *data = i2c_get_clientdata(client);
1111 int i;
1113 down(&data->update_lock);
1115 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1116 || !data->valid) {
1118 if (update_vbat) {
1119 /* Cleared after each update, so reenable. Value
1120 returned by this read will be previous value */
1121 it87_write_value(client, IT87_REG_CONFIG,
1122 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1124 for (i = 0; i <= 7; i++) {
1125 data->in[i] =
1126 it87_read_value(client, IT87_REG_VIN(i));
1127 data->in_min[i] =
1128 it87_read_value(client, IT87_REG_VIN_MIN(i));
1129 data->in_max[i] =
1130 it87_read_value(client, IT87_REG_VIN_MAX(i));
1132 data->in[8] =
1133 it87_read_value(client, IT87_REG_VIN(8));
1134 /* Temperature sensor doesn't have limit registers, set
1135 to min and max value */
1136 data->in_min[8] = 0;
1137 data->in_max[8] = 255;
1139 for (i = 0; i < 3; i++) {
1140 data->fan[i] =
1141 it87_read_value(client, IT87_REG_FAN(i));
1142 data->fan_min[i] =
1143 it87_read_value(client, IT87_REG_FAN_MIN(i));
1145 for (i = 0; i < 3; i++) {
1146 data->temp[i] =
1147 it87_read_value(client, IT87_REG_TEMP(i));
1148 data->temp_high[i] =
1149 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1150 data->temp_low[i] =
1151 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1154 i = it87_read_value(client, IT87_REG_FAN_DIV);
1155 data->fan_div[0] = i & 0x07;
1156 data->fan_div[1] = (i >> 3) & 0x07;
1157 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1159 data->alarms =
1160 it87_read_value(client, IT87_REG_ALARM1) |
1161 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1162 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1163 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1165 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1166 /* The 8705 does not have VID capability */
1167 if (data->type == it8712) {
1168 data->vid = it87_read_value(client, IT87_REG_VID);
1169 data->vid &= 0x1f;
1171 data->last_updated = jiffies;
1172 data->valid = 1;
1175 up(&data->update_lock);
1177 return data;
1180 static int __init sm_it87_init(void)
1182 int addr;
1184 if (!it87_find(&addr)) {
1185 normal_isa[0] = addr;
1187 return i2c_add_driver(&it87_driver);
1190 static void __exit sm_it87_exit(void)
1192 i2c_del_driver(&it87_driver);
1196 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>");
1197 MODULE_DESCRIPTION("IT8705F, IT8712F, Sis950 driver");
1198 module_param(update_vbat, bool, 0);
1199 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1200 module_param(fix_pwm_polarity, bool, 0);
1201 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1202 MODULE_LICENSE("GPL");
1204 module_init(sm_it87_init);
1205 module_exit(sm_it87_exit);