USB: use function attribute __maybe_unused
[linux-2.6/mini2440.git] / drivers / hwmon / it87.c
blob62afc63708a5c85dec58aae278b2f2047c476e3e
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
6 IT8712F Super I/O chip w/LPC interface
7 IT8716F Super I/O chip w/LPC interface
8 IT8718F Super I/O chip w/LPC interface
9 Sis950 A clone of the IT8705F
11 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
12 Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org>
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/jiffies.h>
33 #include <linux/i2c.h>
34 #include <linux/i2c-isa.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
37 #include <linux/hwmon-vid.h>
38 #include <linux/err.h>
39 #include <linux/mutex.h>
40 #include <linux/sysfs.h>
41 #include <asm/io.h>
44 static unsigned short isa_address;
45 enum chips { it87, it8712, it8716, it8718 };
47 #define REG 0x2e /* The register to read/write */
48 #define DEV 0x07 /* Register: Logical device select */
49 #define VAL 0x2f /* The value to read/write */
50 #define PME 0x04 /* The device with the fan registers in it */
51 #define GPIO 0x07 /* The device with the IT8718F VID value in it */
52 #define DEVID 0x20 /* Register: Device ID */
53 #define DEVREV 0x22 /* Register: Device Revision */
55 static inline int
56 superio_inb(int reg)
58 outb(reg, REG);
59 return inb(VAL);
62 static int superio_inw(int reg)
64 int val;
65 outb(reg++, REG);
66 val = inb(VAL) << 8;
67 outb(reg, REG);
68 val |= inb(VAL);
69 return val;
72 static inline void
73 superio_select(int ldn)
75 outb(DEV, REG);
76 outb(ldn, VAL);
79 static inline void
80 superio_enter(void)
82 outb(0x87, REG);
83 outb(0x01, REG);
84 outb(0x55, REG);
85 outb(0x55, REG);
88 static inline void
89 superio_exit(void)
91 outb(0x02, REG);
92 outb(0x02, VAL);
95 /* Logical device 4 registers */
96 #define IT8712F_DEVID 0x8712
97 #define IT8705F_DEVID 0x8705
98 #define IT8716F_DEVID 0x8716
99 #define IT8718F_DEVID 0x8718
100 #define IT87_ACT_REG 0x30
101 #define IT87_BASE_REG 0x60
103 /* Logical device 7 registers (IT8712F and later) */
104 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
105 #define IT87_SIO_VID_REG 0xfc /* VID value */
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 /* Values read from Super-I/O config space */
114 static u16 chip_type;
115 static u8 vid_value;
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 /* The IT8718F has the VID value in a different register, in Super-I/O
135 configuration space. */
136 #define IT87_REG_VID 0x0a
137 /* Warning: register 0x0b is used for something completely different in
138 new chips/revisions. I suspect only 16-bit tachometer mode will work
139 for these. */
140 #define IT87_REG_FAN_DIV 0x0b
141 #define IT87_REG_FAN_16BIT 0x0c
143 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
145 #define IT87_REG_FAN(nr) (0x0d + (nr))
146 #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
147 #define IT87_REG_FANX(nr) (0x18 + (nr))
148 #define IT87_REG_FANX_MIN(nr) (0x1b + (nr))
149 #define IT87_REG_FAN_MAIN_CTRL 0x13
150 #define IT87_REG_FAN_CTL 0x14
151 #define IT87_REG_PWM(nr) (0x15 + (nr))
153 #define IT87_REG_VIN(nr) (0x20 + (nr))
154 #define IT87_REG_TEMP(nr) (0x29 + (nr))
156 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
157 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
158 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
159 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
161 #define IT87_REG_VIN_ENABLE 0x50
162 #define IT87_REG_TEMP_ENABLE 0x51
164 #define IT87_REG_CHIPID 0x58
166 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
167 #define IN_FROM_REG(val) ((val) * 16)
169 static inline u8 FAN_TO_REG(long rpm, int div)
171 if (rpm == 0)
172 return 255;
173 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
174 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
175 254);
178 static inline u16 FAN16_TO_REG(long rpm)
180 if (rpm == 0)
181 return 0xffff;
182 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
185 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
186 /* The divider is fixed to 2 in 16-bit mode */
187 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
189 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
190 ((val)+500)/1000),-128,127))
191 #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
193 #define PWM_TO_REG(val) ((val) >> 1)
194 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
196 static int DIV_TO_REG(int val)
198 int answer = 0;
199 while (answer < 7 && (val >>= 1))
200 answer++;
201 return answer;
203 #define DIV_FROM_REG(val) (1 << (val))
205 static const unsigned int pwm_freq[8] = {
206 48000000 / 128,
207 24000000 / 128,
208 12000000 / 128,
209 8000000 / 128,
210 6000000 / 128,
211 3000000 / 128,
212 1500000 / 128,
213 750000 / 128,
217 /* For each registered chip, we need to keep some data in memory.
218 The structure is dynamically allocated. */
219 struct it87_data {
220 struct i2c_client client;
221 struct class_device *class_dev;
222 enum chips type;
224 struct mutex update_lock;
225 char valid; /* !=0 if following fields are valid */
226 unsigned long last_updated; /* In jiffies */
228 u8 in[9]; /* Register value */
229 u8 in_max[8]; /* Register value */
230 u8 in_min[8]; /* Register value */
231 u8 has_fan; /* Bitfield, fans enabled */
232 u16 fan[3]; /* Register values, possibly combined */
233 u16 fan_min[3]; /* Register values, possibly combined */
234 u8 temp[3]; /* Register value */
235 u8 temp_high[3]; /* Register value */
236 u8 temp_low[3]; /* Register value */
237 u8 sensor; /* Register value */
238 u8 fan_div[3]; /* Register encoding, shifted right */
239 u8 vid; /* Register encoding, combined */
240 u8 vrm;
241 u32 alarms; /* Register encoding, combined */
242 u8 fan_main_ctrl; /* Register value */
243 u8 fan_ctl; /* Register value */
244 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
248 static int it87_detect(struct i2c_adapter *adapter);
249 static int it87_detach_client(struct i2c_client *client);
251 static int it87_read_value(struct i2c_client *client, u8 reg);
252 static void it87_write_value(struct i2c_client *client, u8 reg, u8 value);
253 static struct it87_data *it87_update_device(struct device *dev);
254 static int it87_check_pwm(struct i2c_client *client);
255 static void it87_init_client(struct i2c_client *client, struct it87_data *data);
258 static struct i2c_driver it87_isa_driver = {
259 .driver = {
260 .owner = THIS_MODULE,
261 .name = "it87-isa",
263 .attach_adapter = it87_detect,
264 .detach_client = it87_detach_client,
268 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
269 char *buf)
271 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
272 int nr = sensor_attr->index;
274 struct it87_data *data = it87_update_device(dev);
275 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
278 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
279 char *buf)
281 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
282 int nr = sensor_attr->index;
284 struct it87_data *data = it87_update_device(dev);
285 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
288 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
289 char *buf)
291 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
292 int nr = sensor_attr->index;
294 struct it87_data *data = it87_update_device(dev);
295 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
298 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t count)
301 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
302 int nr = sensor_attr->index;
304 struct i2c_client *client = to_i2c_client(dev);
305 struct it87_data *data = i2c_get_clientdata(client);
306 unsigned long val = simple_strtoul(buf, NULL, 10);
308 mutex_lock(&data->update_lock);
309 data->in_min[nr] = IN_TO_REG(val);
310 it87_write_value(client, IT87_REG_VIN_MIN(nr),
311 data->in_min[nr]);
312 mutex_unlock(&data->update_lock);
313 return count;
315 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
316 const char *buf, size_t count)
318 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
319 int nr = sensor_attr->index;
321 struct i2c_client *client = to_i2c_client(dev);
322 struct it87_data *data = i2c_get_clientdata(client);
323 unsigned long val = simple_strtoul(buf, NULL, 10);
325 mutex_lock(&data->update_lock);
326 data->in_max[nr] = IN_TO_REG(val);
327 it87_write_value(client, IT87_REG_VIN_MAX(nr),
328 data->in_max[nr]);
329 mutex_unlock(&data->update_lock);
330 return count;
333 #define show_in_offset(offset) \
334 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
335 show_in, NULL, offset);
337 #define limit_in_offset(offset) \
338 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
339 show_in_min, set_in_min, offset); \
340 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
341 show_in_max, set_in_max, offset);
343 show_in_offset(0);
344 limit_in_offset(0);
345 show_in_offset(1);
346 limit_in_offset(1);
347 show_in_offset(2);
348 limit_in_offset(2);
349 show_in_offset(3);
350 limit_in_offset(3);
351 show_in_offset(4);
352 limit_in_offset(4);
353 show_in_offset(5);
354 limit_in_offset(5);
355 show_in_offset(6);
356 limit_in_offset(6);
357 show_in_offset(7);
358 limit_in_offset(7);
359 show_in_offset(8);
361 /* 3 temperatures */
362 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
363 char *buf)
365 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
366 int nr = sensor_attr->index;
368 struct it87_data *data = it87_update_device(dev);
369 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
371 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
372 char *buf)
374 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
375 int nr = sensor_attr->index;
377 struct it87_data *data = it87_update_device(dev);
378 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
380 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
381 char *buf)
383 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
384 int nr = sensor_attr->index;
386 struct it87_data *data = it87_update_device(dev);
387 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
389 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
392 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
393 int nr = sensor_attr->index;
395 struct i2c_client *client = to_i2c_client(dev);
396 struct it87_data *data = i2c_get_clientdata(client);
397 int val = simple_strtol(buf, NULL, 10);
399 mutex_lock(&data->update_lock);
400 data->temp_high[nr] = TEMP_TO_REG(val);
401 it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
402 mutex_unlock(&data->update_lock);
403 return count;
405 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
406 const char *buf, size_t count)
408 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
409 int nr = sensor_attr->index;
411 struct i2c_client *client = to_i2c_client(dev);
412 struct it87_data *data = i2c_get_clientdata(client);
413 int val = simple_strtol(buf, NULL, 10);
415 mutex_lock(&data->update_lock);
416 data->temp_low[nr] = TEMP_TO_REG(val);
417 it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
418 mutex_unlock(&data->update_lock);
419 return count;
421 #define show_temp_offset(offset) \
422 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
423 show_temp, NULL, offset - 1); \
424 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
425 show_temp_max, set_temp_max, offset - 1); \
426 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
427 show_temp_min, set_temp_min, offset - 1);
429 show_temp_offset(1);
430 show_temp_offset(2);
431 show_temp_offset(3);
433 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
434 char *buf)
436 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
437 int nr = sensor_attr->index;
439 struct it87_data *data = it87_update_device(dev);
440 u8 reg = data->sensor; /* In case the value is updated while we use it */
442 if (reg & (1 << nr))
443 return sprintf(buf, "3\n"); /* thermal diode */
444 if (reg & (8 << nr))
445 return sprintf(buf, "2\n"); /* thermistor */
446 return sprintf(buf, "0\n"); /* disabled */
448 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
449 const char *buf, size_t count)
451 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
452 int nr = sensor_attr->index;
454 struct i2c_client *client = to_i2c_client(dev);
455 struct it87_data *data = i2c_get_clientdata(client);
456 int val = simple_strtol(buf, NULL, 10);
458 mutex_lock(&data->update_lock);
460 data->sensor &= ~(1 << nr);
461 data->sensor &= ~(8 << nr);
462 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
463 if (val == 3)
464 data->sensor |= 1 << nr;
465 else if (val == 2)
466 data->sensor |= 8 << nr;
467 else if (val != 0) {
468 mutex_unlock(&data->update_lock);
469 return -EINVAL;
471 it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
472 mutex_unlock(&data->update_lock);
473 return count;
475 #define show_sensor_offset(offset) \
476 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
477 show_sensor, set_sensor, offset - 1);
479 show_sensor_offset(1);
480 show_sensor_offset(2);
481 show_sensor_offset(3);
483 /* 3 Fans */
484 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
485 char *buf)
487 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
488 int nr = sensor_attr->index;
490 struct it87_data *data = it87_update_device(dev);
491 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
492 DIV_FROM_REG(data->fan_div[nr])));
494 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
495 char *buf)
497 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
498 int nr = sensor_attr->index;
500 struct it87_data *data = it87_update_device(dev);
501 return sprintf(buf,"%d\n",
502 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
504 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
505 char *buf)
507 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
508 int nr = sensor_attr->index;
510 struct it87_data *data = it87_update_device(dev);
511 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
513 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
514 char *buf)
516 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
517 int nr = sensor_attr->index;
519 struct it87_data *data = it87_update_device(dev);
520 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
522 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
523 char *buf)
525 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
526 int nr = sensor_attr->index;
528 struct it87_data *data = it87_update_device(dev);
529 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
531 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
532 char *buf)
534 struct it87_data *data = it87_update_device(dev);
535 int index = (data->fan_ctl >> 4) & 0x07;
537 return sprintf(buf, "%u\n", pwm_freq[index]);
539 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
540 const char *buf, size_t count)
542 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
543 int nr = sensor_attr->index;
545 struct i2c_client *client = to_i2c_client(dev);
546 struct it87_data *data = i2c_get_clientdata(client);
547 int val = simple_strtol(buf, NULL, 10);
548 u8 reg;
550 mutex_lock(&data->update_lock);
551 reg = it87_read_value(client, IT87_REG_FAN_DIV);
552 switch (nr) {
553 case 0: data->fan_div[nr] = reg & 0x07; break;
554 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
555 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
558 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
559 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
560 mutex_unlock(&data->update_lock);
561 return count;
563 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
566 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
567 int nr = sensor_attr->index;
569 struct i2c_client *client = to_i2c_client(dev);
570 struct it87_data *data = i2c_get_clientdata(client);
571 unsigned long val = simple_strtoul(buf, NULL, 10);
572 int min;
573 u8 old;
575 mutex_lock(&data->update_lock);
576 old = it87_read_value(client, IT87_REG_FAN_DIV);
578 /* Save fan min limit */
579 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
581 switch (nr) {
582 case 0:
583 case 1:
584 data->fan_div[nr] = DIV_TO_REG(val);
585 break;
586 case 2:
587 if (val < 8)
588 data->fan_div[nr] = 1;
589 else
590 data->fan_div[nr] = 3;
592 val = old & 0x80;
593 val |= (data->fan_div[0] & 0x07);
594 val |= (data->fan_div[1] & 0x07) << 3;
595 if (data->fan_div[2] == 3)
596 val |= 0x1 << 6;
597 it87_write_value(client, IT87_REG_FAN_DIV, val);
599 /* Restore fan min limit */
600 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
601 it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
603 mutex_unlock(&data->update_lock);
604 return count;
606 static ssize_t set_pwm_enable(struct device *dev,
607 struct device_attribute *attr, const char *buf, size_t count)
609 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
610 int nr = sensor_attr->index;
612 struct i2c_client *client = to_i2c_client(dev);
613 struct it87_data *data = i2c_get_clientdata(client);
614 int val = simple_strtol(buf, NULL, 10);
616 mutex_lock(&data->update_lock);
618 if (val == 0) {
619 int tmp;
620 /* make sure the fan is on when in on/off mode */
621 tmp = it87_read_value(client, IT87_REG_FAN_CTL);
622 it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
623 /* set on/off mode */
624 data->fan_main_ctrl &= ~(1 << nr);
625 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
626 } else if (val == 1) {
627 /* set SmartGuardian mode */
628 data->fan_main_ctrl |= (1 << nr);
629 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
630 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
631 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
632 } else {
633 mutex_unlock(&data->update_lock);
634 return -EINVAL;
637 mutex_unlock(&data->update_lock);
638 return count;
640 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
641 const char *buf, size_t count)
643 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
644 int nr = sensor_attr->index;
646 struct i2c_client *client = to_i2c_client(dev);
647 struct it87_data *data = i2c_get_clientdata(client);
648 int val = simple_strtol(buf, NULL, 10);
650 if (val < 0 || val > 255)
651 return -EINVAL;
653 mutex_lock(&data->update_lock);
654 data->manual_pwm_ctl[nr] = val;
655 if (data->fan_main_ctrl & (1 << nr))
656 it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
657 mutex_unlock(&data->update_lock);
658 return count;
660 static ssize_t set_pwm_freq(struct device *dev,
661 struct device_attribute *attr, const char *buf, size_t count)
663 struct i2c_client *client = to_i2c_client(dev);
664 struct it87_data *data = i2c_get_clientdata(client);
665 unsigned long val = simple_strtoul(buf, NULL, 10);
666 int i;
668 /* Search for the nearest available frequency */
669 for (i = 0; i < 7; i++) {
670 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
671 break;
674 mutex_lock(&data->update_lock);
675 data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL) & 0x8f;
676 data->fan_ctl |= i << 4;
677 it87_write_value(client, IT87_REG_FAN_CTL, data->fan_ctl);
678 mutex_unlock(&data->update_lock);
680 return count;
683 #define show_fan_offset(offset) \
684 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
685 show_fan, NULL, offset - 1); \
686 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
687 show_fan_min, set_fan_min, offset - 1); \
688 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
689 show_fan_div, set_fan_div, offset - 1);
691 show_fan_offset(1);
692 show_fan_offset(2);
693 show_fan_offset(3);
695 #define show_pwm_offset(offset) \
696 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
697 show_pwm_enable, set_pwm_enable, offset - 1); \
698 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
699 show_pwm, set_pwm, offset - 1); \
700 static DEVICE_ATTR(pwm##offset##_freq, \
701 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
702 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
704 show_pwm_offset(1);
705 show_pwm_offset(2);
706 show_pwm_offset(3);
708 /* A different set of callbacks for 16-bit fans */
709 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
710 char *buf)
712 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
713 int nr = sensor_attr->index;
714 struct it87_data *data = it87_update_device(dev);
715 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
718 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
719 char *buf)
721 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
722 int nr = sensor_attr->index;
723 struct it87_data *data = it87_update_device(dev);
724 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
727 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
728 const char *buf, size_t count)
730 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
731 int nr = sensor_attr->index;
732 struct i2c_client *client = to_i2c_client(dev);
733 struct it87_data *data = i2c_get_clientdata(client);
734 int val = simple_strtol(buf, NULL, 10);
736 mutex_lock(&data->update_lock);
737 data->fan_min[nr] = FAN16_TO_REG(val);
738 it87_write_value(client, IT87_REG_FAN_MIN(nr),
739 data->fan_min[nr] & 0xff);
740 it87_write_value(client, IT87_REG_FANX_MIN(nr),
741 data->fan_min[nr] >> 8);
742 mutex_unlock(&data->update_lock);
743 return count;
746 /* We want to use the same sysfs file names as 8-bit fans, but we need
747 different variable names, so we have to use SENSOR_ATTR instead of
748 SENSOR_DEVICE_ATTR. */
749 #define show_fan16_offset(offset) \
750 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
751 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
752 show_fan16, NULL, offset - 1); \
753 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
754 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
755 show_fan16_min, set_fan16_min, offset - 1)
757 show_fan16_offset(1);
758 show_fan16_offset(2);
759 show_fan16_offset(3);
761 /* Alarms */
762 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
764 struct it87_data *data = it87_update_device(dev);
765 return sprintf(buf, "%u\n", data->alarms);
767 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
769 static ssize_t
770 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
772 struct it87_data *data = it87_update_device(dev);
773 return sprintf(buf, "%u\n", data->vrm);
775 static ssize_t
776 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
778 struct i2c_client *client = to_i2c_client(dev);
779 struct it87_data *data = i2c_get_clientdata(client);
780 u32 val;
782 val = simple_strtoul(buf, NULL, 10);
783 data->vrm = val;
785 return count;
787 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
789 static ssize_t
790 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
792 struct it87_data *data = it87_update_device(dev);
793 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
795 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
797 static struct attribute *it87_attributes[] = {
798 &sensor_dev_attr_in0_input.dev_attr.attr,
799 &sensor_dev_attr_in1_input.dev_attr.attr,
800 &sensor_dev_attr_in2_input.dev_attr.attr,
801 &sensor_dev_attr_in3_input.dev_attr.attr,
802 &sensor_dev_attr_in4_input.dev_attr.attr,
803 &sensor_dev_attr_in5_input.dev_attr.attr,
804 &sensor_dev_attr_in6_input.dev_attr.attr,
805 &sensor_dev_attr_in7_input.dev_attr.attr,
806 &sensor_dev_attr_in8_input.dev_attr.attr,
807 &sensor_dev_attr_in0_min.dev_attr.attr,
808 &sensor_dev_attr_in1_min.dev_attr.attr,
809 &sensor_dev_attr_in2_min.dev_attr.attr,
810 &sensor_dev_attr_in3_min.dev_attr.attr,
811 &sensor_dev_attr_in4_min.dev_attr.attr,
812 &sensor_dev_attr_in5_min.dev_attr.attr,
813 &sensor_dev_attr_in6_min.dev_attr.attr,
814 &sensor_dev_attr_in7_min.dev_attr.attr,
815 &sensor_dev_attr_in0_max.dev_attr.attr,
816 &sensor_dev_attr_in1_max.dev_attr.attr,
817 &sensor_dev_attr_in2_max.dev_attr.attr,
818 &sensor_dev_attr_in3_max.dev_attr.attr,
819 &sensor_dev_attr_in4_max.dev_attr.attr,
820 &sensor_dev_attr_in5_max.dev_attr.attr,
821 &sensor_dev_attr_in6_max.dev_attr.attr,
822 &sensor_dev_attr_in7_max.dev_attr.attr,
824 &sensor_dev_attr_temp1_input.dev_attr.attr,
825 &sensor_dev_attr_temp2_input.dev_attr.attr,
826 &sensor_dev_attr_temp3_input.dev_attr.attr,
827 &sensor_dev_attr_temp1_max.dev_attr.attr,
828 &sensor_dev_attr_temp2_max.dev_attr.attr,
829 &sensor_dev_attr_temp3_max.dev_attr.attr,
830 &sensor_dev_attr_temp1_min.dev_attr.attr,
831 &sensor_dev_attr_temp2_min.dev_attr.attr,
832 &sensor_dev_attr_temp3_min.dev_attr.attr,
833 &sensor_dev_attr_temp1_type.dev_attr.attr,
834 &sensor_dev_attr_temp2_type.dev_attr.attr,
835 &sensor_dev_attr_temp3_type.dev_attr.attr,
837 &dev_attr_alarms.attr,
838 NULL
841 static const struct attribute_group it87_group = {
842 .attrs = it87_attributes,
845 static struct attribute *it87_attributes_opt[] = {
846 &sensor_dev_attr_fan1_input16.dev_attr.attr,
847 &sensor_dev_attr_fan1_min16.dev_attr.attr,
848 &sensor_dev_attr_fan2_input16.dev_attr.attr,
849 &sensor_dev_attr_fan2_min16.dev_attr.attr,
850 &sensor_dev_attr_fan3_input16.dev_attr.attr,
851 &sensor_dev_attr_fan3_min16.dev_attr.attr,
853 &sensor_dev_attr_fan1_input.dev_attr.attr,
854 &sensor_dev_attr_fan1_min.dev_attr.attr,
855 &sensor_dev_attr_fan1_div.dev_attr.attr,
856 &sensor_dev_attr_fan2_input.dev_attr.attr,
857 &sensor_dev_attr_fan2_min.dev_attr.attr,
858 &sensor_dev_attr_fan2_div.dev_attr.attr,
859 &sensor_dev_attr_fan3_input.dev_attr.attr,
860 &sensor_dev_attr_fan3_min.dev_attr.attr,
861 &sensor_dev_attr_fan3_div.dev_attr.attr,
863 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
864 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
865 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
866 &sensor_dev_attr_pwm1.dev_attr.attr,
867 &sensor_dev_attr_pwm2.dev_attr.attr,
868 &sensor_dev_attr_pwm3.dev_attr.attr,
870 &dev_attr_vrm.attr,
871 &dev_attr_cpu0_vid.attr,
872 NULL
875 static const struct attribute_group it87_group_opt = {
876 .attrs = it87_attributes_opt,
879 /* SuperIO detection - will change isa_address if a chip is found */
880 static int __init it87_find(unsigned short *address)
882 int err = -ENODEV;
884 superio_enter();
885 chip_type = superio_inw(DEVID);
886 if (chip_type != IT8712F_DEVID
887 && chip_type != IT8716F_DEVID
888 && chip_type != IT8718F_DEVID
889 && chip_type != IT8705F_DEVID)
890 goto exit;
892 superio_select(PME);
893 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
894 pr_info("it87: Device not activated, skipping\n");
895 goto exit;
898 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
899 if (*address == 0) {
900 pr_info("it87: Base address not set, skipping\n");
901 goto exit;
904 err = 0;
905 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
906 chip_type, *address, superio_inb(DEVREV) & 0x0f);
908 /* Read GPIO config and VID value from LDN 7 (GPIO) */
909 if (chip_type != IT8705F_DEVID) {
910 int reg;
912 superio_select(GPIO);
913 if (chip_type == it8718)
914 vid_value = superio_inb(IT87_SIO_VID_REG);
916 reg = superio_inb(IT87_SIO_PINX2_REG);
917 if (reg & (1 << 0))
918 pr_info("it87: in3 is VCC (+5V)\n");
919 if (reg & (1 << 1))
920 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
923 exit:
924 superio_exit();
925 return err;
928 /* This function is called by i2c_probe */
929 static int it87_detect(struct i2c_adapter *adapter)
931 struct i2c_client *new_client;
932 struct it87_data *data;
933 int err = 0;
934 const char *name;
935 int enable_pwm_interface;
937 /* Reserve the ISA region */
938 if (!request_region(isa_address, IT87_EXTENT,
939 it87_isa_driver.driver.name)){
940 err = -EBUSY;
941 goto ERROR0;
944 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
945 err = -ENOMEM;
946 goto ERROR1;
949 new_client = &data->client;
950 i2c_set_clientdata(new_client, data);
951 new_client->addr = isa_address;
952 new_client->adapter = adapter;
953 new_client->driver = &it87_isa_driver;
955 /* Now, we do the remaining detection. */
956 if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
957 || it87_read_value(new_client, IT87_REG_CHIPID) != 0x90) {
958 err = -ENODEV;
959 goto ERROR2;
962 /* Determine the chip type. */
963 switch (chip_type) {
964 case IT8712F_DEVID:
965 data->type = it8712;
966 name = "it8712";
967 break;
968 case IT8716F_DEVID:
969 data->type = it8716;
970 name = "it8716";
971 break;
972 case IT8718F_DEVID:
973 data->type = it8718;
974 name = "it8718";
975 break;
976 default:
977 data->type = it87;
978 name = "it87";
981 /* Fill in the remaining client fields and put it into the global list */
982 strlcpy(new_client->name, name, I2C_NAME_SIZE);
983 mutex_init(&data->update_lock);
985 /* Tell the I2C layer a new client has arrived */
986 if ((err = i2c_attach_client(new_client)))
987 goto ERROR2;
989 /* Check PWM configuration */
990 enable_pwm_interface = it87_check_pwm(new_client);
992 /* Initialize the IT87 chip */
993 it87_init_client(new_client, data);
995 /* Register sysfs hooks */
996 if ((err = sysfs_create_group(&new_client->dev.kobj, &it87_group)))
997 goto ERROR3;
999 /* Do not create fan files for disabled fans */
1000 if (data->type == it8716 || data->type == it8718) {
1001 /* 16-bit tachometers */
1002 if (data->has_fan & (1 << 0)) {
1003 if ((err = device_create_file(&new_client->dev,
1004 &sensor_dev_attr_fan1_input16.dev_attr))
1005 || (err = device_create_file(&new_client->dev,
1006 &sensor_dev_attr_fan1_min16.dev_attr)))
1007 goto ERROR4;
1009 if (data->has_fan & (1 << 1)) {
1010 if ((err = device_create_file(&new_client->dev,
1011 &sensor_dev_attr_fan2_input16.dev_attr))
1012 || (err = device_create_file(&new_client->dev,
1013 &sensor_dev_attr_fan2_min16.dev_attr)))
1014 goto ERROR4;
1016 if (data->has_fan & (1 << 2)) {
1017 if ((err = device_create_file(&new_client->dev,
1018 &sensor_dev_attr_fan3_input16.dev_attr))
1019 || (err = device_create_file(&new_client->dev,
1020 &sensor_dev_attr_fan3_min16.dev_attr)))
1021 goto ERROR4;
1023 } else {
1024 /* 8-bit tachometers with clock divider */
1025 if (data->has_fan & (1 << 0)) {
1026 if ((err = device_create_file(&new_client->dev,
1027 &sensor_dev_attr_fan1_input.dev_attr))
1028 || (err = device_create_file(&new_client->dev,
1029 &sensor_dev_attr_fan1_min.dev_attr))
1030 || (err = device_create_file(&new_client->dev,
1031 &sensor_dev_attr_fan1_div.dev_attr)))
1032 goto ERROR4;
1034 if (data->has_fan & (1 << 1)) {
1035 if ((err = device_create_file(&new_client->dev,
1036 &sensor_dev_attr_fan2_input.dev_attr))
1037 || (err = device_create_file(&new_client->dev,
1038 &sensor_dev_attr_fan2_min.dev_attr))
1039 || (err = device_create_file(&new_client->dev,
1040 &sensor_dev_attr_fan2_div.dev_attr)))
1041 goto ERROR4;
1043 if (data->has_fan & (1 << 2)) {
1044 if ((err = device_create_file(&new_client->dev,
1045 &sensor_dev_attr_fan3_input.dev_attr))
1046 || (err = device_create_file(&new_client->dev,
1047 &sensor_dev_attr_fan3_min.dev_attr))
1048 || (err = device_create_file(&new_client->dev,
1049 &sensor_dev_attr_fan3_div.dev_attr)))
1050 goto ERROR4;
1054 if (enable_pwm_interface) {
1055 if ((err = device_create_file(&new_client->dev,
1056 &sensor_dev_attr_pwm1_enable.dev_attr))
1057 || (err = device_create_file(&new_client->dev,
1058 &sensor_dev_attr_pwm2_enable.dev_attr))
1059 || (err = device_create_file(&new_client->dev,
1060 &sensor_dev_attr_pwm3_enable.dev_attr))
1061 || (err = device_create_file(&new_client->dev,
1062 &sensor_dev_attr_pwm1.dev_attr))
1063 || (err = device_create_file(&new_client->dev,
1064 &sensor_dev_attr_pwm2.dev_attr))
1065 || (err = device_create_file(&new_client->dev,
1066 &sensor_dev_attr_pwm3.dev_attr))
1067 || (err = device_create_file(&new_client->dev,
1068 &dev_attr_pwm1_freq))
1069 || (err = device_create_file(&new_client->dev,
1070 &dev_attr_pwm2_freq))
1071 || (err = device_create_file(&new_client->dev,
1072 &dev_attr_pwm3_freq)))
1073 goto ERROR4;
1076 if (data->type == it8712 || data->type == it8716
1077 || data->type == it8718) {
1078 data->vrm = vid_which_vrm();
1079 /* VID reading from Super-I/O config space if available */
1080 data->vid = vid_value;
1081 if ((err = device_create_file(&new_client->dev,
1082 &dev_attr_vrm))
1083 || (err = device_create_file(&new_client->dev,
1084 &dev_attr_cpu0_vid)))
1085 goto ERROR4;
1088 data->class_dev = hwmon_device_register(&new_client->dev);
1089 if (IS_ERR(data->class_dev)) {
1090 err = PTR_ERR(data->class_dev);
1091 goto ERROR4;
1094 return 0;
1096 ERROR4:
1097 sysfs_remove_group(&new_client->dev.kobj, &it87_group);
1098 sysfs_remove_group(&new_client->dev.kobj, &it87_group_opt);
1099 ERROR3:
1100 i2c_detach_client(new_client);
1101 ERROR2:
1102 kfree(data);
1103 ERROR1:
1104 release_region(isa_address, IT87_EXTENT);
1105 ERROR0:
1106 return err;
1109 static int it87_detach_client(struct i2c_client *client)
1111 struct it87_data *data = i2c_get_clientdata(client);
1112 int err;
1114 hwmon_device_unregister(data->class_dev);
1115 sysfs_remove_group(&client->dev.kobj, &it87_group);
1116 sysfs_remove_group(&client->dev.kobj, &it87_group_opt);
1118 if ((err = i2c_detach_client(client)))
1119 return err;
1121 release_region(client->addr, IT87_EXTENT);
1122 kfree(data);
1124 return 0;
1127 /* Must be called with data->update_lock held, except during initialization.
1128 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1129 would slow down the IT87 access and should not be necessary. */
1130 static int it87_read_value(struct i2c_client *client, u8 reg)
1132 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1133 return inb_p(client->addr + IT87_DATA_REG_OFFSET);
1136 /* Must be called with data->update_lock held, except during initialization.
1137 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1138 would slow down the IT87 access and should not be necessary. */
1139 static void it87_write_value(struct i2c_client *client, u8 reg, u8 value)
1141 outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
1142 outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
1145 /* Return 1 if and only if the PWM interface is safe to use */
1146 static int it87_check_pwm(struct i2c_client *client)
1148 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1149 * and polarity set to active low is sign that this is the case so we
1150 * disable pwm control to protect the user. */
1151 int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
1152 if ((tmp & 0x87) == 0) {
1153 if (fix_pwm_polarity) {
1154 /* The user asks us to attempt a chip reconfiguration.
1155 * This means switching to active high polarity and
1156 * inverting all fan speed values. */
1157 int i;
1158 u8 pwm[3];
1160 for (i = 0; i < 3; i++)
1161 pwm[i] = it87_read_value(client,
1162 IT87_REG_PWM(i));
1164 /* If any fan is in automatic pwm mode, the polarity
1165 * might be correct, as suspicious as it seems, so we
1166 * better don't change anything (but still disable the
1167 * PWM interface). */
1168 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1169 dev_info(&client->dev, "Reconfiguring PWM to "
1170 "active high polarity\n");
1171 it87_write_value(client, IT87_REG_FAN_CTL,
1172 tmp | 0x87);
1173 for (i = 0; i < 3; i++)
1174 it87_write_value(client,
1175 IT87_REG_PWM(i),
1176 0x7f & ~pwm[i]);
1177 return 1;
1180 dev_info(&client->dev, "PWM configuration is "
1181 "too broken to be fixed\n");
1184 dev_info(&client->dev, "Detected broken BIOS "
1185 "defaults, disabling PWM interface\n");
1186 return 0;
1187 } else if (fix_pwm_polarity) {
1188 dev_info(&client->dev, "PWM configuration looks "
1189 "sane, won't touch\n");
1192 return 1;
1195 /* Called when we have found a new IT87. */
1196 static void it87_init_client(struct i2c_client *client, struct it87_data *data)
1198 int tmp, i;
1200 /* initialize to sane defaults:
1201 * - if the chip is in manual pwm mode, this will be overwritten with
1202 * the actual settings on the chip (so in this case, initialization
1203 * is not needed)
1204 * - if in automatic or on/off mode, we could switch to manual mode,
1205 * read the registers and set manual_pwm_ctl accordingly, but currently
1206 * this is not implemented, so we initialize to something sane */
1207 for (i = 0; i < 3; i++) {
1208 data->manual_pwm_ctl[i] = 0xff;
1211 /* Some chips seem to have default value 0xff for all limit
1212 * registers. For low voltage limits it makes no sense and triggers
1213 * alarms, so change to 0 instead. For high temperature limits, it
1214 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1215 * but is still confusing, so change to 127 degrees C. */
1216 for (i = 0; i < 8; i++) {
1217 tmp = it87_read_value(client, IT87_REG_VIN_MIN(i));
1218 if (tmp == 0xff)
1219 it87_write_value(client, IT87_REG_VIN_MIN(i), 0);
1221 for (i = 0; i < 3; i++) {
1222 tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1223 if (tmp == 0xff)
1224 it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127);
1227 /* Check if temperature channnels are reset manually or by some reason */
1228 tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1229 if ((tmp & 0x3f) == 0) {
1230 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1231 tmp = (tmp & 0xc0) | 0x2a;
1232 it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
1234 data->sensor = tmp;
1236 /* Check if voltage monitors are reset manually or by some reason */
1237 tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
1238 if ((tmp & 0xff) == 0) {
1239 /* Enable all voltage monitors */
1240 it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
1243 /* Check if tachometers are reset manually or by some reason */
1244 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1245 if ((data->fan_main_ctrl & 0x70) == 0) {
1246 /* Enable all fan tachometers */
1247 data->fan_main_ctrl |= 0x70;
1248 it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1250 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1252 /* Set tachometers to 16-bit mode if needed */
1253 if (data->type == it8716 || data->type == it8718) {
1254 tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
1255 if (~tmp & 0x07 & data->has_fan) {
1256 dev_dbg(&client->dev,
1257 "Setting fan1-3 to 16-bit mode\n");
1258 it87_write_value(client, IT87_REG_FAN_16BIT,
1259 tmp | 0x07);
1263 /* Set current fan mode registers and the default settings for the
1264 * other mode registers */
1265 for (i = 0; i < 3; i++) {
1266 if (data->fan_main_ctrl & (1 << i)) {
1267 /* pwm mode */
1268 tmp = it87_read_value(client, IT87_REG_PWM(i));
1269 if (tmp & 0x80) {
1270 /* automatic pwm - not yet implemented, but
1271 * leave the settings made by the BIOS alone
1272 * until a change is requested via the sysfs
1273 * interface */
1274 } else {
1275 /* manual pwm */
1276 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1281 /* Start monitoring */
1282 it87_write_value(client, IT87_REG_CONFIG,
1283 (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
1284 | (update_vbat ? 0x41 : 0x01));
1287 static struct it87_data *it87_update_device(struct device *dev)
1289 struct i2c_client *client = to_i2c_client(dev);
1290 struct it87_data *data = i2c_get_clientdata(client);
1291 int i;
1293 mutex_lock(&data->update_lock);
1295 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1296 || !data->valid) {
1298 if (update_vbat) {
1299 /* Cleared after each update, so reenable. Value
1300 returned by this read will be previous value */
1301 it87_write_value(client, IT87_REG_CONFIG,
1302 it87_read_value(client, IT87_REG_CONFIG) | 0x40);
1304 for (i = 0; i <= 7; i++) {
1305 data->in[i] =
1306 it87_read_value(client, IT87_REG_VIN(i));
1307 data->in_min[i] =
1308 it87_read_value(client, IT87_REG_VIN_MIN(i));
1309 data->in_max[i] =
1310 it87_read_value(client, IT87_REG_VIN_MAX(i));
1312 /* in8 (battery) has no limit registers */
1313 data->in[8] =
1314 it87_read_value(client, IT87_REG_VIN(8));
1316 for (i = 0; i < 3; i++) {
1317 /* Skip disabled fans */
1318 if (!(data->has_fan & (1 << i)))
1319 continue;
1321 data->fan_min[i] =
1322 it87_read_value(client, IT87_REG_FAN_MIN(i));
1323 data->fan[i] = it87_read_value(client,
1324 IT87_REG_FAN(i));
1325 /* Add high byte if in 16-bit mode */
1326 if (data->type == it8716 || data->type == it8718) {
1327 data->fan[i] |= it87_read_value(client,
1328 IT87_REG_FANX(i)) << 8;
1329 data->fan_min[i] |= it87_read_value(client,
1330 IT87_REG_FANX_MIN(i)) << 8;
1333 for (i = 0; i < 3; i++) {
1334 data->temp[i] =
1335 it87_read_value(client, IT87_REG_TEMP(i));
1336 data->temp_high[i] =
1337 it87_read_value(client, IT87_REG_TEMP_HIGH(i));
1338 data->temp_low[i] =
1339 it87_read_value(client, IT87_REG_TEMP_LOW(i));
1342 /* Newer chips don't have clock dividers */
1343 if ((data->has_fan & 0x07) && data->type != it8716
1344 && data->type != it8718) {
1345 i = it87_read_value(client, IT87_REG_FAN_DIV);
1346 data->fan_div[0] = i & 0x07;
1347 data->fan_div[1] = (i >> 3) & 0x07;
1348 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1351 data->alarms =
1352 it87_read_value(client, IT87_REG_ALARM1) |
1353 (it87_read_value(client, IT87_REG_ALARM2) << 8) |
1354 (it87_read_value(client, IT87_REG_ALARM3) << 16);
1355 data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
1356 data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL);
1358 data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
1359 /* The 8705 does not have VID capability */
1360 if (data->type == it8712 || data->type == it8716) {
1361 data->vid = it87_read_value(client, IT87_REG_VID);
1362 /* The older IT8712F revisions had only 5 VID pins,
1363 but we assume it is always safe to read 6 bits. */
1364 data->vid &= 0x3f;
1366 data->last_updated = jiffies;
1367 data->valid = 1;
1370 mutex_unlock(&data->update_lock);
1372 return data;
1375 static int __init sm_it87_init(void)
1377 int res;
1379 if ((res = it87_find(&isa_address)))
1380 return res;
1381 return i2c_isa_add_driver(&it87_isa_driver);
1384 static void __exit sm_it87_exit(void)
1386 i2c_isa_del_driver(&it87_isa_driver);
1390 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, "
1391 "Jean Delvare <khali@linux-fr.org>");
1392 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver");
1393 module_param(update_vbat, bool, 0);
1394 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1395 module_param(fix_pwm_polarity, bool, 0);
1396 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1397 MODULE_LICENSE("GPL");
1399 module_init(sm_it87_init);
1400 module_exit(sm_it87_exit);