revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / drivers / hwmon / it87.c
blobd69ef377c4f2d43317249b5a4fcec6502b93e365
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 IT8726F Super I/O chip w/LPC interface
10 Sis950 A clone of the IT8705F
12 Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
13 Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org>
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/jiffies.h>
34 #include <linux/platform_device.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 <linux/acpi.h>
42 #include <asm/io.h>
44 #define DRVNAME "it87"
46 enum chips { it87, it8712, it8716, it8718 };
48 static struct platform_device *pdev;
50 #define REG 0x2e /* The register to read/write */
51 #define DEV 0x07 /* Register: Logical device select */
52 #define VAL 0x2f /* The value to read/write */
53 #define PME 0x04 /* The device with the fan registers in it */
54 #define GPIO 0x07 /* The device with the IT8718F VID value in it */
55 #define DEVID 0x20 /* Register: Device ID */
56 #define DEVREV 0x22 /* Register: Device Revision */
58 static inline int
59 superio_inb(int reg)
61 outb(reg, REG);
62 return inb(VAL);
65 static int superio_inw(int reg)
67 int val;
68 outb(reg++, REG);
69 val = inb(VAL) << 8;
70 outb(reg, REG);
71 val |= inb(VAL);
72 return val;
75 static inline void
76 superio_select(int ldn)
78 outb(DEV, REG);
79 outb(ldn, VAL);
82 static inline void
83 superio_enter(void)
85 outb(0x87, REG);
86 outb(0x01, REG);
87 outb(0x55, REG);
88 outb(0x55, REG);
91 static inline void
92 superio_exit(void)
94 outb(0x02, REG);
95 outb(0x02, VAL);
98 /* Logical device 4 registers */
99 #define IT8712F_DEVID 0x8712
100 #define IT8705F_DEVID 0x8705
101 #define IT8716F_DEVID 0x8716
102 #define IT8718F_DEVID 0x8718
103 #define IT8726F_DEVID 0x8726
104 #define IT87_ACT_REG 0x30
105 #define IT87_BASE_REG 0x60
107 /* Logical device 7 registers (IT8712F and later) */
108 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
109 #define IT87_SIO_VID_REG 0xfc /* VID value */
111 /* Update battery voltage after every reading if true */
112 static int update_vbat;
114 /* Not all BIOSes properly configure the PWM registers */
115 static int fix_pwm_polarity;
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 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
146 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
147 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
148 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
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 struct it87_sio_data {
218 enum chips type;
219 /* Values read from Super-I/O config space */
220 u8 vid_value;
223 /* For each registered chip, we need to keep some data in memory.
224 The structure is dynamically allocated. */
225 struct it87_data {
226 struct device *hwmon_dev;
227 enum chips type;
229 unsigned short addr;
230 const char *name;
231 struct mutex update_lock;
232 char valid; /* !=0 if following fields are valid */
233 unsigned long last_updated; /* In jiffies */
235 u8 in[9]; /* Register value */
236 u8 in_max[8]; /* Register value */
237 u8 in_min[8]; /* Register value */
238 u8 has_fan; /* Bitfield, fans enabled */
239 u16 fan[5]; /* Register values, possibly combined */
240 u16 fan_min[5]; /* Register values, possibly combined */
241 u8 temp[3]; /* Register value */
242 u8 temp_high[3]; /* Register value */
243 u8 temp_low[3]; /* Register value */
244 u8 sensor; /* Register value */
245 u8 fan_div[3]; /* Register encoding, shifted right */
246 u8 vid; /* Register encoding, combined */
247 u8 vrm;
248 u32 alarms; /* Register encoding, combined */
249 u8 fan_main_ctrl; /* Register value */
250 u8 fan_ctl; /* Register value */
251 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
255 static int it87_probe(struct platform_device *pdev);
256 static int __devexit it87_remove(struct platform_device *pdev);
258 static int it87_read_value(struct it87_data *data, u8 reg);
259 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
260 static struct it87_data *it87_update_device(struct device *dev);
261 static int it87_check_pwm(struct device *dev);
262 static void it87_init_device(struct platform_device *pdev);
265 static struct platform_driver it87_driver = {
266 .driver = {
267 .owner = THIS_MODULE,
268 .name = DRVNAME,
270 .probe = it87_probe,
271 .remove = __devexit_p(it87_remove),
274 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
275 char *buf)
277 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
278 int nr = sensor_attr->index;
280 struct it87_data *data = it87_update_device(dev);
281 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
284 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
285 char *buf)
287 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
288 int nr = sensor_attr->index;
290 struct it87_data *data = it87_update_device(dev);
291 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
294 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
295 char *buf)
297 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
298 int nr = sensor_attr->index;
300 struct it87_data *data = it87_update_device(dev);
301 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
304 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
307 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
308 int nr = sensor_attr->index;
310 struct it87_data *data = dev_get_drvdata(dev);
311 unsigned long val = simple_strtoul(buf, NULL, 10);
313 mutex_lock(&data->update_lock);
314 data->in_min[nr] = IN_TO_REG(val);
315 it87_write_value(data, IT87_REG_VIN_MIN(nr),
316 data->in_min[nr]);
317 mutex_unlock(&data->update_lock);
318 return count;
320 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
321 const char *buf, size_t count)
323 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
324 int nr = sensor_attr->index;
326 struct it87_data *data = dev_get_drvdata(dev);
327 unsigned long val = simple_strtoul(buf, NULL, 10);
329 mutex_lock(&data->update_lock);
330 data->in_max[nr] = IN_TO_REG(val);
331 it87_write_value(data, IT87_REG_VIN_MAX(nr),
332 data->in_max[nr]);
333 mutex_unlock(&data->update_lock);
334 return count;
337 #define show_in_offset(offset) \
338 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
339 show_in, NULL, offset);
341 #define limit_in_offset(offset) \
342 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
343 show_in_min, set_in_min, offset); \
344 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
345 show_in_max, set_in_max, offset);
347 show_in_offset(0);
348 limit_in_offset(0);
349 show_in_offset(1);
350 limit_in_offset(1);
351 show_in_offset(2);
352 limit_in_offset(2);
353 show_in_offset(3);
354 limit_in_offset(3);
355 show_in_offset(4);
356 limit_in_offset(4);
357 show_in_offset(5);
358 limit_in_offset(5);
359 show_in_offset(6);
360 limit_in_offset(6);
361 show_in_offset(7);
362 limit_in_offset(7);
363 show_in_offset(8);
365 /* 3 temperatures */
366 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
367 char *buf)
369 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
370 int nr = sensor_attr->index;
372 struct it87_data *data = it87_update_device(dev);
373 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
375 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
376 char *buf)
378 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
379 int nr = sensor_attr->index;
381 struct it87_data *data = it87_update_device(dev);
382 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
384 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
385 char *buf)
387 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
388 int nr = sensor_attr->index;
390 struct it87_data *data = it87_update_device(dev);
391 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
393 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
396 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
397 int nr = sensor_attr->index;
399 struct it87_data *data = dev_get_drvdata(dev);
400 int val = simple_strtol(buf, NULL, 10);
402 mutex_lock(&data->update_lock);
403 data->temp_high[nr] = TEMP_TO_REG(val);
404 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
405 mutex_unlock(&data->update_lock);
406 return count;
408 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
409 const char *buf, size_t count)
411 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
412 int nr = sensor_attr->index;
414 struct it87_data *data = dev_get_drvdata(dev);
415 int val = simple_strtol(buf, NULL, 10);
417 mutex_lock(&data->update_lock);
418 data->temp_low[nr] = TEMP_TO_REG(val);
419 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
420 mutex_unlock(&data->update_lock);
421 return count;
423 #define show_temp_offset(offset) \
424 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
425 show_temp, NULL, offset - 1); \
426 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
427 show_temp_max, set_temp_max, offset - 1); \
428 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
429 show_temp_min, set_temp_min, offset - 1);
431 show_temp_offset(1);
432 show_temp_offset(2);
433 show_temp_offset(3);
435 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
436 char *buf)
438 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
439 int nr = sensor_attr->index;
441 struct it87_data *data = it87_update_device(dev);
442 u8 reg = data->sensor; /* In case the value is updated while we use it */
444 if (reg & (1 << nr))
445 return sprintf(buf, "3\n"); /* thermal diode */
446 if (reg & (8 << nr))
447 return sprintf(buf, "2\n"); /* thermistor */
448 return sprintf(buf, "0\n"); /* disabled */
450 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
451 const char *buf, size_t count)
453 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
454 int nr = sensor_attr->index;
456 struct it87_data *data = dev_get_drvdata(dev);
457 int val = simple_strtol(buf, NULL, 10);
459 mutex_lock(&data->update_lock);
461 data->sensor &= ~(1 << nr);
462 data->sensor &= ~(8 << nr);
463 /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
464 if (val == 3)
465 data->sensor |= 1 << nr;
466 else if (val == 2)
467 data->sensor |= 8 << nr;
468 else if (val != 0) {
469 mutex_unlock(&data->update_lock);
470 return -EINVAL;
472 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
473 mutex_unlock(&data->update_lock);
474 return count;
476 #define show_sensor_offset(offset) \
477 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
478 show_sensor, set_sensor, offset - 1);
480 show_sensor_offset(1);
481 show_sensor_offset(2);
482 show_sensor_offset(3);
484 /* 3 Fans */
485 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
486 char *buf)
488 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
489 int nr = sensor_attr->index;
491 struct it87_data *data = it87_update_device(dev);
492 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
493 DIV_FROM_REG(data->fan_div[nr])));
495 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
496 char *buf)
498 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
499 int nr = sensor_attr->index;
501 struct it87_data *data = it87_update_device(dev);
502 return sprintf(buf,"%d\n",
503 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
505 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
506 char *buf)
508 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
509 int nr = sensor_attr->index;
511 struct it87_data *data = it87_update_device(dev);
512 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
514 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
515 char *buf)
517 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
518 int nr = sensor_attr->index;
520 struct it87_data *data = it87_update_device(dev);
521 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
523 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
524 char *buf)
526 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
527 int nr = sensor_attr->index;
529 struct it87_data *data = it87_update_device(dev);
530 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
532 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
533 char *buf)
535 struct it87_data *data = it87_update_device(dev);
536 int index = (data->fan_ctl >> 4) & 0x07;
538 return sprintf(buf, "%u\n", pwm_freq[index]);
540 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
541 const char *buf, size_t count)
543 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
544 int nr = sensor_attr->index;
546 struct it87_data *data = dev_get_drvdata(dev);
547 int val = simple_strtol(buf, NULL, 10);
548 u8 reg;
550 mutex_lock(&data->update_lock);
551 reg = it87_read_value(data, 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(data, 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 it87_data *data = dev_get_drvdata(dev);
570 unsigned long val = simple_strtoul(buf, NULL, 10);
571 int min;
572 u8 old;
574 mutex_lock(&data->update_lock);
575 old = it87_read_value(data, IT87_REG_FAN_DIV);
577 /* Save fan min limit */
578 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
580 switch (nr) {
581 case 0:
582 case 1:
583 data->fan_div[nr] = DIV_TO_REG(val);
584 break;
585 case 2:
586 if (val < 8)
587 data->fan_div[nr] = 1;
588 else
589 data->fan_div[nr] = 3;
591 val = old & 0x80;
592 val |= (data->fan_div[0] & 0x07);
593 val |= (data->fan_div[1] & 0x07) << 3;
594 if (data->fan_div[2] == 3)
595 val |= 0x1 << 6;
596 it87_write_value(data, IT87_REG_FAN_DIV, val);
598 /* Restore fan min limit */
599 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
600 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
602 mutex_unlock(&data->update_lock);
603 return count;
605 static ssize_t set_pwm_enable(struct device *dev,
606 struct device_attribute *attr, const char *buf, size_t count)
608 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
609 int nr = sensor_attr->index;
611 struct it87_data *data = dev_get_drvdata(dev);
612 int val = simple_strtol(buf, NULL, 10);
614 mutex_lock(&data->update_lock);
616 if (val == 0) {
617 int tmp;
618 /* make sure the fan is on when in on/off mode */
619 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
620 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
621 /* set on/off mode */
622 data->fan_main_ctrl &= ~(1 << nr);
623 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
624 } else if (val == 1) {
625 /* set SmartGuardian mode */
626 data->fan_main_ctrl |= (1 << nr);
627 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
628 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
629 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
630 } else {
631 mutex_unlock(&data->update_lock);
632 return -EINVAL;
635 mutex_unlock(&data->update_lock);
636 return count;
638 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
639 const char *buf, size_t count)
641 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
642 int nr = sensor_attr->index;
644 struct it87_data *data = dev_get_drvdata(dev);
645 int val = simple_strtol(buf, NULL, 10);
647 if (val < 0 || val > 255)
648 return -EINVAL;
650 mutex_lock(&data->update_lock);
651 data->manual_pwm_ctl[nr] = val;
652 if (data->fan_main_ctrl & (1 << nr))
653 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
654 mutex_unlock(&data->update_lock);
655 return count;
657 static ssize_t set_pwm_freq(struct device *dev,
658 struct device_attribute *attr, const char *buf, size_t count)
660 struct it87_data *data = dev_get_drvdata(dev);
661 unsigned long val = simple_strtoul(buf, NULL, 10);
662 int i;
664 /* Search for the nearest available frequency */
665 for (i = 0; i < 7; i++) {
666 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
667 break;
670 mutex_lock(&data->update_lock);
671 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
672 data->fan_ctl |= i << 4;
673 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
674 mutex_unlock(&data->update_lock);
676 return count;
679 #define show_fan_offset(offset) \
680 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
681 show_fan, NULL, offset - 1); \
682 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
683 show_fan_min, set_fan_min, offset - 1); \
684 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
685 show_fan_div, set_fan_div, offset - 1);
687 show_fan_offset(1);
688 show_fan_offset(2);
689 show_fan_offset(3);
691 #define show_pwm_offset(offset) \
692 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
693 show_pwm_enable, set_pwm_enable, offset - 1); \
694 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
695 show_pwm, set_pwm, offset - 1); \
696 static DEVICE_ATTR(pwm##offset##_freq, \
697 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
698 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
700 show_pwm_offset(1);
701 show_pwm_offset(2);
702 show_pwm_offset(3);
704 /* A different set of callbacks for 16-bit fans */
705 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
706 char *buf)
708 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
709 int nr = sensor_attr->index;
710 struct it87_data *data = it87_update_device(dev);
711 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
714 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
715 char *buf)
717 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
718 int nr = sensor_attr->index;
719 struct it87_data *data = it87_update_device(dev);
720 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
723 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
724 const char *buf, size_t count)
726 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
727 int nr = sensor_attr->index;
728 struct it87_data *data = dev_get_drvdata(dev);
729 int val = simple_strtol(buf, NULL, 10);
731 mutex_lock(&data->update_lock);
732 data->fan_min[nr] = FAN16_TO_REG(val);
733 it87_write_value(data, IT87_REG_FAN_MIN[nr],
734 data->fan_min[nr] & 0xff);
735 it87_write_value(data, IT87_REG_FANX_MIN[nr],
736 data->fan_min[nr] >> 8);
737 mutex_unlock(&data->update_lock);
738 return count;
741 /* We want to use the same sysfs file names as 8-bit fans, but we need
742 different variable names, so we have to use SENSOR_ATTR instead of
743 SENSOR_DEVICE_ATTR. */
744 #define show_fan16_offset(offset) \
745 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
746 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
747 show_fan16, NULL, offset - 1); \
748 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
749 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
750 show_fan16_min, set_fan16_min, offset - 1)
752 show_fan16_offset(1);
753 show_fan16_offset(2);
754 show_fan16_offset(3);
755 show_fan16_offset(4);
756 show_fan16_offset(5);
758 /* Alarms */
759 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
761 struct it87_data *data = it87_update_device(dev);
762 return sprintf(buf, "%u\n", data->alarms);
764 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
766 static ssize_t
767 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
769 struct it87_data *data = dev_get_drvdata(dev);
770 return sprintf(buf, "%u\n", data->vrm);
772 static ssize_t
773 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
775 struct it87_data *data = dev_get_drvdata(dev);
776 u32 val;
778 val = simple_strtoul(buf, NULL, 10);
779 data->vrm = val;
781 return count;
783 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
785 static ssize_t
786 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
788 struct it87_data *data = it87_update_device(dev);
789 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
791 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
793 static ssize_t show_name(struct device *dev, struct device_attribute
794 *devattr, char *buf)
796 struct it87_data *data = dev_get_drvdata(dev);
797 return sprintf(buf, "%s\n", data->name);
799 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
801 static struct attribute *it87_attributes[] = {
802 &sensor_dev_attr_in0_input.dev_attr.attr,
803 &sensor_dev_attr_in1_input.dev_attr.attr,
804 &sensor_dev_attr_in2_input.dev_attr.attr,
805 &sensor_dev_attr_in3_input.dev_attr.attr,
806 &sensor_dev_attr_in4_input.dev_attr.attr,
807 &sensor_dev_attr_in5_input.dev_attr.attr,
808 &sensor_dev_attr_in6_input.dev_attr.attr,
809 &sensor_dev_attr_in7_input.dev_attr.attr,
810 &sensor_dev_attr_in8_input.dev_attr.attr,
811 &sensor_dev_attr_in0_min.dev_attr.attr,
812 &sensor_dev_attr_in1_min.dev_attr.attr,
813 &sensor_dev_attr_in2_min.dev_attr.attr,
814 &sensor_dev_attr_in3_min.dev_attr.attr,
815 &sensor_dev_attr_in4_min.dev_attr.attr,
816 &sensor_dev_attr_in5_min.dev_attr.attr,
817 &sensor_dev_attr_in6_min.dev_attr.attr,
818 &sensor_dev_attr_in7_min.dev_attr.attr,
819 &sensor_dev_attr_in0_max.dev_attr.attr,
820 &sensor_dev_attr_in1_max.dev_attr.attr,
821 &sensor_dev_attr_in2_max.dev_attr.attr,
822 &sensor_dev_attr_in3_max.dev_attr.attr,
823 &sensor_dev_attr_in4_max.dev_attr.attr,
824 &sensor_dev_attr_in5_max.dev_attr.attr,
825 &sensor_dev_attr_in6_max.dev_attr.attr,
826 &sensor_dev_attr_in7_max.dev_attr.attr,
828 &sensor_dev_attr_temp1_input.dev_attr.attr,
829 &sensor_dev_attr_temp2_input.dev_attr.attr,
830 &sensor_dev_attr_temp3_input.dev_attr.attr,
831 &sensor_dev_attr_temp1_max.dev_attr.attr,
832 &sensor_dev_attr_temp2_max.dev_attr.attr,
833 &sensor_dev_attr_temp3_max.dev_attr.attr,
834 &sensor_dev_attr_temp1_min.dev_attr.attr,
835 &sensor_dev_attr_temp2_min.dev_attr.attr,
836 &sensor_dev_attr_temp3_min.dev_attr.attr,
837 &sensor_dev_attr_temp1_type.dev_attr.attr,
838 &sensor_dev_attr_temp2_type.dev_attr.attr,
839 &sensor_dev_attr_temp3_type.dev_attr.attr,
841 &dev_attr_alarms.attr,
842 &dev_attr_name.attr,
843 NULL
846 static const struct attribute_group it87_group = {
847 .attrs = it87_attributes,
850 static struct attribute *it87_attributes_opt[] = {
851 &sensor_dev_attr_fan1_input16.dev_attr.attr,
852 &sensor_dev_attr_fan1_min16.dev_attr.attr,
853 &sensor_dev_attr_fan2_input16.dev_attr.attr,
854 &sensor_dev_attr_fan2_min16.dev_attr.attr,
855 &sensor_dev_attr_fan3_input16.dev_attr.attr,
856 &sensor_dev_attr_fan3_min16.dev_attr.attr,
857 &sensor_dev_attr_fan4_input16.dev_attr.attr,
858 &sensor_dev_attr_fan4_min16.dev_attr.attr,
859 &sensor_dev_attr_fan5_input16.dev_attr.attr,
860 &sensor_dev_attr_fan5_min16.dev_attr.attr,
862 &sensor_dev_attr_fan1_input.dev_attr.attr,
863 &sensor_dev_attr_fan1_min.dev_attr.attr,
864 &sensor_dev_attr_fan1_div.dev_attr.attr,
865 &sensor_dev_attr_fan2_input.dev_attr.attr,
866 &sensor_dev_attr_fan2_min.dev_attr.attr,
867 &sensor_dev_attr_fan2_div.dev_attr.attr,
868 &sensor_dev_attr_fan3_input.dev_attr.attr,
869 &sensor_dev_attr_fan3_min.dev_attr.attr,
870 &sensor_dev_attr_fan3_div.dev_attr.attr,
872 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
873 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
874 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
875 &sensor_dev_attr_pwm1.dev_attr.attr,
876 &sensor_dev_attr_pwm2.dev_attr.attr,
877 &sensor_dev_attr_pwm3.dev_attr.attr,
879 &dev_attr_vrm.attr,
880 &dev_attr_cpu0_vid.attr,
881 NULL
884 static const struct attribute_group it87_group_opt = {
885 .attrs = it87_attributes_opt,
888 /* SuperIO detection - will change isa_address if a chip is found */
889 static int __init it87_find(unsigned short *address,
890 struct it87_sio_data *sio_data)
892 int err = -ENODEV;
893 u16 chip_type;
895 superio_enter();
896 chip_type = superio_inw(DEVID);
898 switch (chip_type) {
899 case IT8705F_DEVID:
900 sio_data->type = it87;
901 break;
902 case IT8712F_DEVID:
903 sio_data->type = it8712;
904 break;
905 case IT8716F_DEVID:
906 case IT8726F_DEVID:
907 sio_data->type = it8716;
908 break;
909 case IT8718F_DEVID:
910 sio_data->type = it8718;
911 break;
912 case 0xffff: /* No device at all */
913 goto exit;
914 default:
915 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
916 chip_type);
917 goto exit;
920 superio_select(PME);
921 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
922 pr_info("it87: Device not activated, skipping\n");
923 goto exit;
926 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
927 if (*address == 0) {
928 pr_info("it87: Base address not set, skipping\n");
929 goto exit;
932 err = 0;
933 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
934 chip_type, *address, superio_inb(DEVREV) & 0x0f);
936 /* Read GPIO config and VID value from LDN 7 (GPIO) */
937 if (chip_type != IT8705F_DEVID) {
938 int reg;
940 superio_select(GPIO);
941 if (chip_type == it8718)
942 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
944 reg = superio_inb(IT87_SIO_PINX2_REG);
945 if (reg & (1 << 0))
946 pr_info("it87: in3 is VCC (+5V)\n");
947 if (reg & (1 << 1))
948 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
951 exit:
952 superio_exit();
953 return err;
956 static int __devinit it87_probe(struct platform_device *pdev)
958 struct it87_data *data;
959 struct resource *res;
960 struct device *dev = &pdev->dev;
961 struct it87_sio_data *sio_data = dev->platform_data;
962 int err = 0;
963 int enable_pwm_interface;
964 static const char *names[] = {
965 "it87",
966 "it8712",
967 "it8716",
968 "it8718",
971 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
972 if (!request_region(res->start, IT87_EXTENT, DRVNAME)) {
973 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
974 (unsigned long)res->start,
975 (unsigned long)(res->start + IT87_EXTENT - 1));
976 err = -EBUSY;
977 goto ERROR0;
980 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
981 err = -ENOMEM;
982 goto ERROR1;
985 data->addr = res->start;
986 data->type = sio_data->type;
987 data->name = names[sio_data->type];
989 /* Now, we do the remaining detection. */
990 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
991 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
992 err = -ENODEV;
993 goto ERROR2;
996 platform_set_drvdata(pdev, data);
998 mutex_init(&data->update_lock);
1000 /* Check PWM configuration */
1001 enable_pwm_interface = it87_check_pwm(dev);
1003 /* Initialize the IT87 chip */
1004 it87_init_device(pdev);
1006 /* Register sysfs hooks */
1007 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1008 goto ERROR2;
1010 /* Do not create fan files for disabled fans */
1011 if (data->type == it8716 || data->type == it8718) {
1012 /* 16-bit tachometers */
1013 if (data->has_fan & (1 << 0)) {
1014 if ((err = device_create_file(dev,
1015 &sensor_dev_attr_fan1_input16.dev_attr))
1016 || (err = device_create_file(dev,
1017 &sensor_dev_attr_fan1_min16.dev_attr)))
1018 goto ERROR4;
1020 if (data->has_fan & (1 << 1)) {
1021 if ((err = device_create_file(dev,
1022 &sensor_dev_attr_fan2_input16.dev_attr))
1023 || (err = device_create_file(dev,
1024 &sensor_dev_attr_fan2_min16.dev_attr)))
1025 goto ERROR4;
1027 if (data->has_fan & (1 << 2)) {
1028 if ((err = device_create_file(dev,
1029 &sensor_dev_attr_fan3_input16.dev_attr))
1030 || (err = device_create_file(dev,
1031 &sensor_dev_attr_fan3_min16.dev_attr)))
1032 goto ERROR4;
1034 if (data->has_fan & (1 << 3)) {
1035 if ((err = device_create_file(dev,
1036 &sensor_dev_attr_fan4_input16.dev_attr))
1037 || (err = device_create_file(dev,
1038 &sensor_dev_attr_fan4_min16.dev_attr)))
1039 goto ERROR4;
1041 if (data->has_fan & (1 << 4)) {
1042 if ((err = device_create_file(dev,
1043 &sensor_dev_attr_fan5_input16.dev_attr))
1044 || (err = device_create_file(dev,
1045 &sensor_dev_attr_fan5_min16.dev_attr)))
1046 goto ERROR4;
1048 } else {
1049 /* 8-bit tachometers with clock divider */
1050 if (data->has_fan & (1 << 0)) {
1051 if ((err = device_create_file(dev,
1052 &sensor_dev_attr_fan1_input.dev_attr))
1053 || (err = device_create_file(dev,
1054 &sensor_dev_attr_fan1_min.dev_attr))
1055 || (err = device_create_file(dev,
1056 &sensor_dev_attr_fan1_div.dev_attr)))
1057 goto ERROR4;
1059 if (data->has_fan & (1 << 1)) {
1060 if ((err = device_create_file(dev,
1061 &sensor_dev_attr_fan2_input.dev_attr))
1062 || (err = device_create_file(dev,
1063 &sensor_dev_attr_fan2_min.dev_attr))
1064 || (err = device_create_file(dev,
1065 &sensor_dev_attr_fan2_div.dev_attr)))
1066 goto ERROR4;
1068 if (data->has_fan & (1 << 2)) {
1069 if ((err = device_create_file(dev,
1070 &sensor_dev_attr_fan3_input.dev_attr))
1071 || (err = device_create_file(dev,
1072 &sensor_dev_attr_fan3_min.dev_attr))
1073 || (err = device_create_file(dev,
1074 &sensor_dev_attr_fan3_div.dev_attr)))
1075 goto ERROR4;
1079 if (enable_pwm_interface) {
1080 if ((err = device_create_file(dev,
1081 &sensor_dev_attr_pwm1_enable.dev_attr))
1082 || (err = device_create_file(dev,
1083 &sensor_dev_attr_pwm2_enable.dev_attr))
1084 || (err = device_create_file(dev,
1085 &sensor_dev_attr_pwm3_enable.dev_attr))
1086 || (err = device_create_file(dev,
1087 &sensor_dev_attr_pwm1.dev_attr))
1088 || (err = device_create_file(dev,
1089 &sensor_dev_attr_pwm2.dev_attr))
1090 || (err = device_create_file(dev,
1091 &sensor_dev_attr_pwm3.dev_attr))
1092 || (err = device_create_file(dev,
1093 &dev_attr_pwm1_freq))
1094 || (err = device_create_file(dev,
1095 &dev_attr_pwm2_freq))
1096 || (err = device_create_file(dev,
1097 &dev_attr_pwm3_freq)))
1098 goto ERROR4;
1101 if (data->type == it8712 || data->type == it8716
1102 || data->type == it8718) {
1103 data->vrm = vid_which_vrm();
1104 /* VID reading from Super-I/O config space if available */
1105 data->vid = sio_data->vid_value;
1106 if ((err = device_create_file(dev,
1107 &dev_attr_vrm))
1108 || (err = device_create_file(dev,
1109 &dev_attr_cpu0_vid)))
1110 goto ERROR4;
1113 data->hwmon_dev = hwmon_device_register(dev);
1114 if (IS_ERR(data->hwmon_dev)) {
1115 err = PTR_ERR(data->hwmon_dev);
1116 goto ERROR4;
1119 return 0;
1121 ERROR4:
1122 sysfs_remove_group(&dev->kobj, &it87_group);
1123 sysfs_remove_group(&dev->kobj, &it87_group_opt);
1124 ERROR2:
1125 platform_set_drvdata(pdev, NULL);
1126 kfree(data);
1127 ERROR1:
1128 release_region(res->start, IT87_EXTENT);
1129 ERROR0:
1130 return err;
1133 static int __devexit it87_remove(struct platform_device *pdev)
1135 struct it87_data *data = platform_get_drvdata(pdev);
1137 hwmon_device_unregister(data->hwmon_dev);
1138 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1139 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
1141 release_region(data->addr, IT87_EXTENT);
1142 platform_set_drvdata(pdev, NULL);
1143 kfree(data);
1145 return 0;
1148 /* Must be called with data->update_lock held, except during initialization.
1149 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1150 would slow down the IT87 access and should not be necessary. */
1151 static int it87_read_value(struct it87_data *data, u8 reg)
1153 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1154 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
1157 /* Must be called with data->update_lock held, except during initialization.
1158 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1159 would slow down the IT87 access and should not be necessary. */
1160 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
1162 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1163 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
1166 /* Return 1 if and only if the PWM interface is safe to use */
1167 static int __devinit it87_check_pwm(struct device *dev)
1169 struct it87_data *data = dev_get_drvdata(dev);
1170 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1171 * and polarity set to active low is sign that this is the case so we
1172 * disable pwm control to protect the user. */
1173 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1174 if ((tmp & 0x87) == 0) {
1175 if (fix_pwm_polarity) {
1176 /* The user asks us to attempt a chip reconfiguration.
1177 * This means switching to active high polarity and
1178 * inverting all fan speed values. */
1179 int i;
1180 u8 pwm[3];
1182 for (i = 0; i < 3; i++)
1183 pwm[i] = it87_read_value(data,
1184 IT87_REG_PWM(i));
1186 /* If any fan is in automatic pwm mode, the polarity
1187 * might be correct, as suspicious as it seems, so we
1188 * better don't change anything (but still disable the
1189 * PWM interface). */
1190 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1191 dev_info(dev, "Reconfiguring PWM to "
1192 "active high polarity\n");
1193 it87_write_value(data, IT87_REG_FAN_CTL,
1194 tmp | 0x87);
1195 for (i = 0; i < 3; i++)
1196 it87_write_value(data,
1197 IT87_REG_PWM(i),
1198 0x7f & ~pwm[i]);
1199 return 1;
1202 dev_info(dev, "PWM configuration is "
1203 "too broken to be fixed\n");
1206 dev_info(dev, "Detected broken BIOS "
1207 "defaults, disabling PWM interface\n");
1208 return 0;
1209 } else if (fix_pwm_polarity) {
1210 dev_info(dev, "PWM configuration looks "
1211 "sane, won't touch\n");
1214 return 1;
1217 /* Called when we have found a new IT87. */
1218 static void __devinit it87_init_device(struct platform_device *pdev)
1220 struct it87_data *data = platform_get_drvdata(pdev);
1221 int tmp, i;
1223 /* initialize to sane defaults:
1224 * - if the chip is in manual pwm mode, this will be overwritten with
1225 * the actual settings on the chip (so in this case, initialization
1226 * is not needed)
1227 * - if in automatic or on/off mode, we could switch to manual mode,
1228 * read the registers and set manual_pwm_ctl accordingly, but currently
1229 * this is not implemented, so we initialize to something sane */
1230 for (i = 0; i < 3; i++) {
1231 data->manual_pwm_ctl[i] = 0xff;
1234 /* Some chips seem to have default value 0xff for all limit
1235 * registers. For low voltage limits it makes no sense and triggers
1236 * alarms, so change to 0 instead. For high temperature limits, it
1237 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1238 * but is still confusing, so change to 127 degrees C. */
1239 for (i = 0; i < 8; i++) {
1240 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
1241 if (tmp == 0xff)
1242 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
1244 for (i = 0; i < 3; i++) {
1245 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1246 if (tmp == 0xff)
1247 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
1250 /* Check if temperature channnels are reset manually or by some reason */
1251 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1252 if ((tmp & 0x3f) == 0) {
1253 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1254 tmp = (tmp & 0xc0) | 0x2a;
1255 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
1257 data->sensor = tmp;
1259 /* Check if voltage monitors are reset manually or by some reason */
1260 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
1261 if ((tmp & 0xff) == 0) {
1262 /* Enable all voltage monitors */
1263 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
1266 /* Check if tachometers are reset manually or by some reason */
1267 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
1268 if ((data->fan_main_ctrl & 0x70) == 0) {
1269 /* Enable all fan tachometers */
1270 data->fan_main_ctrl |= 0x70;
1271 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1273 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1275 /* Set tachometers to 16-bit mode if needed */
1276 if (data->type == it8716 || data->type == it8718) {
1277 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
1278 if (~tmp & 0x07 & data->has_fan) {
1279 dev_dbg(&pdev->dev,
1280 "Setting fan1-3 to 16-bit mode\n");
1281 it87_write_value(data, IT87_REG_FAN_16BIT,
1282 tmp | 0x07);
1284 if (tmp & (1 << 4))
1285 data->has_fan |= (1 << 3); /* fan4 enabled */
1286 if (tmp & (1 << 5))
1287 data->has_fan |= (1 << 4); /* fan5 enabled */
1290 /* Set current fan mode registers and the default settings for the
1291 * other mode registers */
1292 for (i = 0; i < 3; i++) {
1293 if (data->fan_main_ctrl & (1 << i)) {
1294 /* pwm mode */
1295 tmp = it87_read_value(data, IT87_REG_PWM(i));
1296 if (tmp & 0x80) {
1297 /* automatic pwm - not yet implemented, but
1298 * leave the settings made by the BIOS alone
1299 * until a change is requested via the sysfs
1300 * interface */
1301 } else {
1302 /* manual pwm */
1303 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1308 /* Start monitoring */
1309 it87_write_value(data, IT87_REG_CONFIG,
1310 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1311 | (update_vbat ? 0x41 : 0x01));
1314 static struct it87_data *it87_update_device(struct device *dev)
1316 struct it87_data *data = dev_get_drvdata(dev);
1317 int i;
1319 mutex_lock(&data->update_lock);
1321 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1322 || !data->valid) {
1324 if (update_vbat) {
1325 /* Cleared after each update, so reenable. Value
1326 returned by this read will be previous value */
1327 it87_write_value(data, IT87_REG_CONFIG,
1328 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1330 for (i = 0; i <= 7; i++) {
1331 data->in[i] =
1332 it87_read_value(data, IT87_REG_VIN(i));
1333 data->in_min[i] =
1334 it87_read_value(data, IT87_REG_VIN_MIN(i));
1335 data->in_max[i] =
1336 it87_read_value(data, IT87_REG_VIN_MAX(i));
1338 /* in8 (battery) has no limit registers */
1339 data->in[8] =
1340 it87_read_value(data, IT87_REG_VIN(8));
1342 for (i = 0; i < 5; i++) {
1343 /* Skip disabled fans */
1344 if (!(data->has_fan & (1 << i)))
1345 continue;
1347 data->fan_min[i] =
1348 it87_read_value(data, IT87_REG_FAN_MIN[i]);
1349 data->fan[i] = it87_read_value(data,
1350 IT87_REG_FAN[i]);
1351 /* Add high byte if in 16-bit mode */
1352 if (data->type == it8716 || data->type == it8718) {
1353 data->fan[i] |= it87_read_value(data,
1354 IT87_REG_FANX[i]) << 8;
1355 data->fan_min[i] |= it87_read_value(data,
1356 IT87_REG_FANX_MIN[i]) << 8;
1359 for (i = 0; i < 3; i++) {
1360 data->temp[i] =
1361 it87_read_value(data, IT87_REG_TEMP(i));
1362 data->temp_high[i] =
1363 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1364 data->temp_low[i] =
1365 it87_read_value(data, IT87_REG_TEMP_LOW(i));
1368 /* Newer chips don't have clock dividers */
1369 if ((data->has_fan & 0x07) && data->type != it8716
1370 && data->type != it8718) {
1371 i = it87_read_value(data, IT87_REG_FAN_DIV);
1372 data->fan_div[0] = i & 0x07;
1373 data->fan_div[1] = (i >> 3) & 0x07;
1374 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1377 data->alarms =
1378 it87_read_value(data, IT87_REG_ALARM1) |
1379 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1380 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1381 data->fan_main_ctrl = it87_read_value(data,
1382 IT87_REG_FAN_MAIN_CTRL);
1383 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
1385 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1386 /* The 8705 does not have VID capability */
1387 if (data->type == it8712 || data->type == it8716) {
1388 data->vid = it87_read_value(data, IT87_REG_VID);
1389 /* The older IT8712F revisions had only 5 VID pins,
1390 but we assume it is always safe to read 6 bits. */
1391 data->vid &= 0x3f;
1393 data->last_updated = jiffies;
1394 data->valid = 1;
1397 mutex_unlock(&data->update_lock);
1399 return data;
1402 static int __init it87_device_add(unsigned short address,
1403 const struct it87_sio_data *sio_data)
1405 struct resource res = {
1406 .start = address ,
1407 .end = address + IT87_EXTENT - 1,
1408 .name = DRVNAME,
1409 .flags = IORESOURCE_IO,
1411 int err;
1413 err = acpi_check_resource_conflict(&res);
1414 if (err)
1415 goto exit;
1417 pdev = platform_device_alloc(DRVNAME, address);
1418 if (!pdev) {
1419 err = -ENOMEM;
1420 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1421 goto exit;
1424 err = platform_device_add_resources(pdev, &res, 1);
1425 if (err) {
1426 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1427 "(%d)\n", err);
1428 goto exit_device_put;
1431 err = platform_device_add_data(pdev, sio_data,
1432 sizeof(struct it87_sio_data));
1433 if (err) {
1434 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1435 goto exit_device_put;
1438 err = platform_device_add(pdev);
1439 if (err) {
1440 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1441 err);
1442 goto exit_device_put;
1445 return 0;
1447 exit_device_put:
1448 platform_device_put(pdev);
1449 exit:
1450 return err;
1453 static int __init sm_it87_init(void)
1455 int err;
1456 unsigned short isa_address=0;
1457 struct it87_sio_data sio_data;
1459 err = it87_find(&isa_address, &sio_data);
1460 if (err)
1461 return err;
1462 err = platform_driver_register(&it87_driver);
1463 if (err)
1464 return err;
1466 err = it87_device_add(isa_address, &sio_data);
1467 if (err){
1468 platform_driver_unregister(&it87_driver);
1469 return err;
1472 return 0;
1475 static void __exit sm_it87_exit(void)
1477 platform_device_unregister(pdev);
1478 platform_driver_unregister(&it87_driver);
1482 MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, "
1483 "Jean Delvare <khali@linux-fr.org>");
1484 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver");
1485 module_param(update_vbat, bool, 0);
1486 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1487 module_param(fix_pwm_polarity, bool, 0);
1488 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1489 MODULE_LICENSE("GPL");
1491 module_init(sm_it87_init);
1492 module_exit(sm_it87_exit);