KVM: SVM: Fix memory leaks that happen when svm_create_vcpu() fails
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / it87.c
blob0ffe84d190bbfabae0b7343ba6f3d39c078eabee
1 /*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
5 The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 addition to an Environment Controller (Enhanced Hardware Monitor and
8 Fan Controller)
10 This driver supports only the Environment Controller in the IT8705F and
11 similar parts. The other devices are supported by different drivers.
13 Supports: IT8705F Super I/O chip w/LPC interface
14 IT8712F Super I/O chip w/LPC interface
15 IT8716F Super I/O chip w/LPC interface
16 IT8718F Super I/O chip w/LPC interface
17 IT8720F Super I/O chip w/LPC interface
18 IT8726F Super I/O chip w/LPC interface
19 Sis950 A clone of the IT8705F
21 Copyright (C) 2001 Chris Gauthron
22 Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org>
24 This program is free software; you can redistribute it and/or modify
25 it under the terms of the GNU General Public License as published by
26 the Free Software Foundation; either version 2 of the License, or
27 (at your option) any later version.
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details.
34 You should have received a copy of the GNU General Public License
35 along with this program; if not, write to the Free Software
36 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/jiffies.h>
43 #include <linux/platform_device.h>
44 #include <linux/hwmon.h>
45 #include <linux/hwmon-sysfs.h>
46 #include <linux/hwmon-vid.h>
47 #include <linux/err.h>
48 #include <linux/mutex.h>
49 #include <linux/sysfs.h>
50 #include <linux/string.h>
51 #include <linux/dmi.h>
52 #include <linux/acpi.h>
53 #include <linux/io.h>
55 #define DRVNAME "it87"
57 enum chips { it87, it8712, it8716, it8718, it8720 };
59 static unsigned short force_id;
60 module_param(force_id, ushort, 0);
61 MODULE_PARM_DESC(force_id, "Override the detected device ID");
63 static struct platform_device *pdev;
65 #define REG 0x2e /* The register to read/write */
66 #define DEV 0x07 /* Register: Logical device select */
67 #define VAL 0x2f /* The value to read/write */
68 #define PME 0x04 /* The device with the fan registers in it */
70 /* The device with the IT8718F/IT8720F VID value in it */
71 #define GPIO 0x07
73 #define DEVID 0x20 /* Register: Device ID */
74 #define DEVREV 0x22 /* Register: Device Revision */
76 static inline int
77 superio_inb(int reg)
79 outb(reg, REG);
80 return inb(VAL);
83 static int superio_inw(int reg)
85 int val;
86 outb(reg++, REG);
87 val = inb(VAL) << 8;
88 outb(reg, REG);
89 val |= inb(VAL);
90 return val;
93 static inline void
94 superio_select(int ldn)
96 outb(DEV, REG);
97 outb(ldn, VAL);
100 static inline void
101 superio_enter(void)
103 outb(0x87, REG);
104 outb(0x01, REG);
105 outb(0x55, REG);
106 outb(0x55, REG);
109 static inline void
110 superio_exit(void)
112 outb(0x02, REG);
113 outb(0x02, VAL);
116 /* Logical device 4 registers */
117 #define IT8712F_DEVID 0x8712
118 #define IT8705F_DEVID 0x8705
119 #define IT8716F_DEVID 0x8716
120 #define IT8718F_DEVID 0x8718
121 #define IT8720F_DEVID 0x8720
122 #define IT8726F_DEVID 0x8726
123 #define IT87_ACT_REG 0x30
124 #define IT87_BASE_REG 0x60
126 /* Logical device 7 registers (IT8712F and later) */
127 #define IT87_SIO_GPIO3_REG 0x27
128 #define IT87_SIO_GPIO5_REG 0x29
129 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
130 #define IT87_SIO_VID_REG 0xfc /* VID value */
132 /* Update battery voltage after every reading if true */
133 static int update_vbat;
135 /* Not all BIOSes properly configure the PWM registers */
136 static int fix_pwm_polarity;
138 /* Many IT87 constants specified below */
140 /* Length of ISA address segment */
141 #define IT87_EXTENT 8
143 /* Length of ISA address segment for Environmental Controller */
144 #define IT87_EC_EXTENT 2
146 /* Offset of EC registers from ISA base address */
147 #define IT87_EC_OFFSET 5
149 /* Where are the ISA address/data registers relative to the EC base address */
150 #define IT87_ADDR_REG_OFFSET 0
151 #define IT87_DATA_REG_OFFSET 1
153 /*----- The IT87 registers -----*/
155 #define IT87_REG_CONFIG 0x00
157 #define IT87_REG_ALARM1 0x01
158 #define IT87_REG_ALARM2 0x02
159 #define IT87_REG_ALARM3 0x03
161 /* The IT8718F and IT8720F have the VID value in a different register, in
162 Super-I/O configuration space. */
163 #define IT87_REG_VID 0x0a
164 /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
165 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
166 mode. */
167 #define IT87_REG_FAN_DIV 0x0b
168 #define IT87_REG_FAN_16BIT 0x0c
170 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
172 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
173 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
174 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
175 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
176 #define IT87_REG_FAN_MAIN_CTRL 0x13
177 #define IT87_REG_FAN_CTL 0x14
178 #define IT87_REG_PWM(nr) (0x15 + (nr))
180 #define IT87_REG_VIN(nr) (0x20 + (nr))
181 #define IT87_REG_TEMP(nr) (0x29 + (nr))
183 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
184 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
185 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
186 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
188 #define IT87_REG_VIN_ENABLE 0x50
189 #define IT87_REG_TEMP_ENABLE 0x51
191 #define IT87_REG_CHIPID 0x58
193 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
194 #define IN_FROM_REG(val) ((val) * 16)
196 static inline u8 FAN_TO_REG(long rpm, int div)
198 if (rpm == 0)
199 return 255;
200 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
201 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
202 254);
205 static inline u16 FAN16_TO_REG(long rpm)
207 if (rpm == 0)
208 return 0xffff;
209 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
212 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
213 /* The divider is fixed to 2 in 16-bit mode */
214 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
216 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
217 ((val)+500)/1000),-128,127))
218 #define TEMP_FROM_REG(val) ((val) * 1000)
220 #define PWM_TO_REG(val) ((val) >> 1)
221 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
223 static int DIV_TO_REG(int val)
225 int answer = 0;
226 while (answer < 7 && (val >>= 1))
227 answer++;
228 return answer;
230 #define DIV_FROM_REG(val) (1 << (val))
232 static const unsigned int pwm_freq[8] = {
233 48000000 / 128,
234 24000000 / 128,
235 12000000 / 128,
236 8000000 / 128,
237 6000000 / 128,
238 3000000 / 128,
239 1500000 / 128,
240 750000 / 128,
244 struct it87_sio_data {
245 enum chips type;
246 /* Values read from Super-I/O config space */
247 u8 revision;
248 u8 vid_value;
249 /* Features skipped based on config or DMI */
250 u8 skip_vid;
251 u8 skip_fan;
252 u8 skip_pwm;
255 /* For each registered chip, we need to keep some data in memory.
256 The structure is dynamically allocated. */
257 struct it87_data {
258 struct device *hwmon_dev;
259 enum chips type;
260 u8 revision;
262 unsigned short addr;
263 const char *name;
264 struct mutex update_lock;
265 char valid; /* !=0 if following fields are valid */
266 unsigned long last_updated; /* In jiffies */
268 u8 in[9]; /* Register value */
269 u8 in_max[8]; /* Register value */
270 u8 in_min[8]; /* Register value */
271 u8 has_fan; /* Bitfield, fans enabled */
272 u16 fan[5]; /* Register values, possibly combined */
273 u16 fan_min[5]; /* Register values, possibly combined */
274 s8 temp[3]; /* Register value */
275 s8 temp_high[3]; /* Register value */
276 s8 temp_low[3]; /* Register value */
277 u8 sensor; /* Register value */
278 u8 fan_div[3]; /* Register encoding, shifted right */
279 u8 vid; /* Register encoding, combined */
280 u8 vrm;
281 u32 alarms; /* Register encoding, combined */
282 u8 fan_main_ctrl; /* Register value */
283 u8 fan_ctl; /* Register value */
284 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
287 static inline int has_16bit_fans(const struct it87_data *data)
289 /* IT8705F Datasheet 0.4.1, 3h == Version G.
290 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
291 These are the first revisions with 16bit tachometer support. */
292 return (data->type == it87 && data->revision >= 0x03)
293 || (data->type == it8712 && data->revision >= 0x08)
294 || data->type == it8716
295 || data->type == it8718
296 || data->type == it8720;
299 static int it87_probe(struct platform_device *pdev);
300 static int __devexit it87_remove(struct platform_device *pdev);
302 static int it87_read_value(struct it87_data *data, u8 reg);
303 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
304 static struct it87_data *it87_update_device(struct device *dev);
305 static int it87_check_pwm(struct device *dev);
306 static void it87_init_device(struct platform_device *pdev);
309 static struct platform_driver it87_driver = {
310 .driver = {
311 .owner = THIS_MODULE,
312 .name = DRVNAME,
314 .probe = it87_probe,
315 .remove = __devexit_p(it87_remove),
318 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
319 char *buf)
321 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
322 int nr = sensor_attr->index;
324 struct it87_data *data = it87_update_device(dev);
325 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
328 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
329 char *buf)
331 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
332 int nr = sensor_attr->index;
334 struct it87_data *data = it87_update_device(dev);
335 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
338 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
339 char *buf)
341 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
342 int nr = sensor_attr->index;
344 struct it87_data *data = it87_update_device(dev);
345 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
348 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t count)
351 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
352 int nr = sensor_attr->index;
354 struct it87_data *data = dev_get_drvdata(dev);
355 unsigned long val = simple_strtoul(buf, NULL, 10);
357 mutex_lock(&data->update_lock);
358 data->in_min[nr] = IN_TO_REG(val);
359 it87_write_value(data, IT87_REG_VIN_MIN(nr),
360 data->in_min[nr]);
361 mutex_unlock(&data->update_lock);
362 return count;
364 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
365 const char *buf, size_t count)
367 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
368 int nr = sensor_attr->index;
370 struct it87_data *data = dev_get_drvdata(dev);
371 unsigned long val = simple_strtoul(buf, NULL, 10);
373 mutex_lock(&data->update_lock);
374 data->in_max[nr] = IN_TO_REG(val);
375 it87_write_value(data, IT87_REG_VIN_MAX(nr),
376 data->in_max[nr]);
377 mutex_unlock(&data->update_lock);
378 return count;
381 #define show_in_offset(offset) \
382 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
383 show_in, NULL, offset);
385 #define limit_in_offset(offset) \
386 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
387 show_in_min, set_in_min, offset); \
388 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
389 show_in_max, set_in_max, offset);
391 show_in_offset(0);
392 limit_in_offset(0);
393 show_in_offset(1);
394 limit_in_offset(1);
395 show_in_offset(2);
396 limit_in_offset(2);
397 show_in_offset(3);
398 limit_in_offset(3);
399 show_in_offset(4);
400 limit_in_offset(4);
401 show_in_offset(5);
402 limit_in_offset(5);
403 show_in_offset(6);
404 limit_in_offset(6);
405 show_in_offset(7);
406 limit_in_offset(7);
407 show_in_offset(8);
409 /* 3 temperatures */
410 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
411 char *buf)
413 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
414 int nr = sensor_attr->index;
416 struct it87_data *data = it87_update_device(dev);
417 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
419 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
420 char *buf)
422 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
423 int nr = sensor_attr->index;
425 struct it87_data *data = it87_update_device(dev);
426 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
428 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
429 char *buf)
431 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
432 int nr = sensor_attr->index;
434 struct it87_data *data = it87_update_device(dev);
435 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
437 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
440 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
441 int nr = sensor_attr->index;
443 struct it87_data *data = dev_get_drvdata(dev);
444 int val = simple_strtol(buf, NULL, 10);
446 mutex_lock(&data->update_lock);
447 data->temp_high[nr] = TEMP_TO_REG(val);
448 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
449 mutex_unlock(&data->update_lock);
450 return count;
452 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
453 const char *buf, size_t count)
455 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
456 int nr = sensor_attr->index;
458 struct it87_data *data = dev_get_drvdata(dev);
459 int val = simple_strtol(buf, NULL, 10);
461 mutex_lock(&data->update_lock);
462 data->temp_low[nr] = TEMP_TO_REG(val);
463 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
464 mutex_unlock(&data->update_lock);
465 return count;
467 #define show_temp_offset(offset) \
468 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
469 show_temp, NULL, offset - 1); \
470 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
471 show_temp_max, set_temp_max, offset - 1); \
472 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
473 show_temp_min, set_temp_min, offset - 1);
475 show_temp_offset(1);
476 show_temp_offset(2);
477 show_temp_offset(3);
479 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
480 char *buf)
482 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
483 int nr = sensor_attr->index;
485 struct it87_data *data = it87_update_device(dev);
486 u8 reg = data->sensor; /* In case the value is updated while we use it */
488 if (reg & (1 << nr))
489 return sprintf(buf, "3\n"); /* thermal diode */
490 if (reg & (8 << nr))
491 return sprintf(buf, "4\n"); /* thermistor */
492 return sprintf(buf, "0\n"); /* disabled */
494 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
497 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
498 int nr = sensor_attr->index;
500 struct it87_data *data = dev_get_drvdata(dev);
501 int val = simple_strtol(buf, NULL, 10);
503 mutex_lock(&data->update_lock);
505 data->sensor &= ~(1 << nr);
506 data->sensor &= ~(8 << nr);
507 if (val == 2) { /* backwards compatibility */
508 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
509 "instead\n");
510 val = 4;
512 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
513 if (val == 3)
514 data->sensor |= 1 << nr;
515 else if (val == 4)
516 data->sensor |= 8 << nr;
517 else if (val != 0) {
518 mutex_unlock(&data->update_lock);
519 return -EINVAL;
521 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
522 mutex_unlock(&data->update_lock);
523 return count;
525 #define show_sensor_offset(offset) \
526 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
527 show_sensor, set_sensor, offset - 1);
529 show_sensor_offset(1);
530 show_sensor_offset(2);
531 show_sensor_offset(3);
533 /* 3 Fans */
534 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
535 char *buf)
537 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
538 int nr = sensor_attr->index;
540 struct it87_data *data = it87_update_device(dev);
541 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
542 DIV_FROM_REG(data->fan_div[nr])));
544 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
545 char *buf)
547 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
548 int nr = sensor_attr->index;
550 struct it87_data *data = it87_update_device(dev);
551 return sprintf(buf,"%d\n",
552 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
554 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
555 char *buf)
557 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
558 int nr = sensor_attr->index;
560 struct it87_data *data = it87_update_device(dev);
561 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
563 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
564 char *buf)
566 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
567 int nr = sensor_attr->index;
569 struct it87_data *data = it87_update_device(dev);
570 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
572 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
573 char *buf)
575 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
576 int nr = sensor_attr->index;
578 struct it87_data *data = it87_update_device(dev);
579 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
581 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
582 char *buf)
584 struct it87_data *data = it87_update_device(dev);
585 int index = (data->fan_ctl >> 4) & 0x07;
587 return sprintf(buf, "%u\n", pwm_freq[index]);
589 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
590 const char *buf, size_t count)
592 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
593 int nr = sensor_attr->index;
595 struct it87_data *data = dev_get_drvdata(dev);
596 int val = simple_strtol(buf, NULL, 10);
597 u8 reg;
599 mutex_lock(&data->update_lock);
600 reg = it87_read_value(data, IT87_REG_FAN_DIV);
601 switch (nr) {
602 case 0: data->fan_div[nr] = reg & 0x07; break;
603 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
604 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
607 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
608 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
609 mutex_unlock(&data->update_lock);
610 return count;
612 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
613 const char *buf, size_t count)
615 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
616 int nr = sensor_attr->index;
618 struct it87_data *data = dev_get_drvdata(dev);
619 unsigned long val = simple_strtoul(buf, NULL, 10);
620 int min;
621 u8 old;
623 mutex_lock(&data->update_lock);
624 old = it87_read_value(data, IT87_REG_FAN_DIV);
626 /* Save fan min limit */
627 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
629 switch (nr) {
630 case 0:
631 case 1:
632 data->fan_div[nr] = DIV_TO_REG(val);
633 break;
634 case 2:
635 if (val < 8)
636 data->fan_div[nr] = 1;
637 else
638 data->fan_div[nr] = 3;
640 val = old & 0x80;
641 val |= (data->fan_div[0] & 0x07);
642 val |= (data->fan_div[1] & 0x07) << 3;
643 if (data->fan_div[2] == 3)
644 val |= 0x1 << 6;
645 it87_write_value(data, IT87_REG_FAN_DIV, val);
647 /* Restore fan min limit */
648 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
649 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
651 mutex_unlock(&data->update_lock);
652 return count;
654 static ssize_t set_pwm_enable(struct device *dev,
655 struct device_attribute *attr, const char *buf, size_t count)
657 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
658 int nr = sensor_attr->index;
660 struct it87_data *data = dev_get_drvdata(dev);
661 int val = simple_strtol(buf, NULL, 10);
663 mutex_lock(&data->update_lock);
665 if (val == 0) {
666 int tmp;
667 /* make sure the fan is on when in on/off mode */
668 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
669 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
670 /* set on/off mode */
671 data->fan_main_ctrl &= ~(1 << nr);
672 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
673 } else if (val == 1) {
674 /* set SmartGuardian mode */
675 data->fan_main_ctrl |= (1 << nr);
676 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
677 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
678 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
679 } else {
680 mutex_unlock(&data->update_lock);
681 return -EINVAL;
684 mutex_unlock(&data->update_lock);
685 return count;
687 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
688 const char *buf, size_t count)
690 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
691 int nr = sensor_attr->index;
693 struct it87_data *data = dev_get_drvdata(dev);
694 int val = simple_strtol(buf, NULL, 10);
696 if (val < 0 || val > 255)
697 return -EINVAL;
699 mutex_lock(&data->update_lock);
700 data->manual_pwm_ctl[nr] = val;
701 if (data->fan_main_ctrl & (1 << nr))
702 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
703 mutex_unlock(&data->update_lock);
704 return count;
706 static ssize_t set_pwm_freq(struct device *dev,
707 struct device_attribute *attr, const char *buf, size_t count)
709 struct it87_data *data = dev_get_drvdata(dev);
710 unsigned long val = simple_strtoul(buf, NULL, 10);
711 int i;
713 /* Search for the nearest available frequency */
714 for (i = 0; i < 7; i++) {
715 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
716 break;
719 mutex_lock(&data->update_lock);
720 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
721 data->fan_ctl |= i << 4;
722 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
723 mutex_unlock(&data->update_lock);
725 return count;
728 #define show_fan_offset(offset) \
729 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
730 show_fan, NULL, offset - 1); \
731 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
732 show_fan_min, set_fan_min, offset - 1); \
733 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
734 show_fan_div, set_fan_div, offset - 1);
736 show_fan_offset(1);
737 show_fan_offset(2);
738 show_fan_offset(3);
740 #define show_pwm_offset(offset) \
741 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
742 show_pwm_enable, set_pwm_enable, offset - 1); \
743 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
744 show_pwm, set_pwm, offset - 1); \
745 static DEVICE_ATTR(pwm##offset##_freq, \
746 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
747 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
749 show_pwm_offset(1);
750 show_pwm_offset(2);
751 show_pwm_offset(3);
753 /* A different set of callbacks for 16-bit fans */
754 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
755 char *buf)
757 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
758 int nr = sensor_attr->index;
759 struct it87_data *data = it87_update_device(dev);
760 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
763 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
764 char *buf)
766 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
767 int nr = sensor_attr->index;
768 struct it87_data *data = it87_update_device(dev);
769 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
772 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
773 const char *buf, size_t count)
775 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
776 int nr = sensor_attr->index;
777 struct it87_data *data = dev_get_drvdata(dev);
778 int val = simple_strtol(buf, NULL, 10);
780 mutex_lock(&data->update_lock);
781 data->fan_min[nr] = FAN16_TO_REG(val);
782 it87_write_value(data, IT87_REG_FAN_MIN[nr],
783 data->fan_min[nr] & 0xff);
784 it87_write_value(data, IT87_REG_FANX_MIN[nr],
785 data->fan_min[nr] >> 8);
786 mutex_unlock(&data->update_lock);
787 return count;
790 /* We want to use the same sysfs file names as 8-bit fans, but we need
791 different variable names, so we have to use SENSOR_ATTR instead of
792 SENSOR_DEVICE_ATTR. */
793 #define show_fan16_offset(offset) \
794 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
795 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
796 show_fan16, NULL, offset - 1); \
797 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
798 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
799 show_fan16_min, set_fan16_min, offset - 1)
801 show_fan16_offset(1);
802 show_fan16_offset(2);
803 show_fan16_offset(3);
804 show_fan16_offset(4);
805 show_fan16_offset(5);
807 /* Alarms */
808 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
810 struct it87_data *data = it87_update_device(dev);
811 return sprintf(buf, "%u\n", data->alarms);
813 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
815 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
816 char *buf)
818 int bitnr = to_sensor_dev_attr(attr)->index;
819 struct it87_data *data = it87_update_device(dev);
820 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
822 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
823 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
824 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
825 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
826 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
827 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
828 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
829 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
830 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
831 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
832 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
833 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
834 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
835 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
836 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
837 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
839 static ssize_t
840 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
842 struct it87_data *data = dev_get_drvdata(dev);
843 return sprintf(buf, "%u\n", data->vrm);
845 static ssize_t
846 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
848 struct it87_data *data = dev_get_drvdata(dev);
849 u32 val;
851 val = simple_strtoul(buf, NULL, 10);
852 data->vrm = val;
854 return count;
856 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
858 static ssize_t
859 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
861 struct it87_data *data = it87_update_device(dev);
862 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
864 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
866 static ssize_t show_name(struct device *dev, struct device_attribute
867 *devattr, char *buf)
869 struct it87_data *data = dev_get_drvdata(dev);
870 return sprintf(buf, "%s\n", data->name);
872 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
874 static struct attribute *it87_attributes[] = {
875 &sensor_dev_attr_in0_input.dev_attr.attr,
876 &sensor_dev_attr_in1_input.dev_attr.attr,
877 &sensor_dev_attr_in2_input.dev_attr.attr,
878 &sensor_dev_attr_in3_input.dev_attr.attr,
879 &sensor_dev_attr_in4_input.dev_attr.attr,
880 &sensor_dev_attr_in5_input.dev_attr.attr,
881 &sensor_dev_attr_in6_input.dev_attr.attr,
882 &sensor_dev_attr_in7_input.dev_attr.attr,
883 &sensor_dev_attr_in8_input.dev_attr.attr,
884 &sensor_dev_attr_in0_min.dev_attr.attr,
885 &sensor_dev_attr_in1_min.dev_attr.attr,
886 &sensor_dev_attr_in2_min.dev_attr.attr,
887 &sensor_dev_attr_in3_min.dev_attr.attr,
888 &sensor_dev_attr_in4_min.dev_attr.attr,
889 &sensor_dev_attr_in5_min.dev_attr.attr,
890 &sensor_dev_attr_in6_min.dev_attr.attr,
891 &sensor_dev_attr_in7_min.dev_attr.attr,
892 &sensor_dev_attr_in0_max.dev_attr.attr,
893 &sensor_dev_attr_in1_max.dev_attr.attr,
894 &sensor_dev_attr_in2_max.dev_attr.attr,
895 &sensor_dev_attr_in3_max.dev_attr.attr,
896 &sensor_dev_attr_in4_max.dev_attr.attr,
897 &sensor_dev_attr_in5_max.dev_attr.attr,
898 &sensor_dev_attr_in6_max.dev_attr.attr,
899 &sensor_dev_attr_in7_max.dev_attr.attr,
900 &sensor_dev_attr_in0_alarm.dev_attr.attr,
901 &sensor_dev_attr_in1_alarm.dev_attr.attr,
902 &sensor_dev_attr_in2_alarm.dev_attr.attr,
903 &sensor_dev_attr_in3_alarm.dev_attr.attr,
904 &sensor_dev_attr_in4_alarm.dev_attr.attr,
905 &sensor_dev_attr_in5_alarm.dev_attr.attr,
906 &sensor_dev_attr_in6_alarm.dev_attr.attr,
907 &sensor_dev_attr_in7_alarm.dev_attr.attr,
909 &sensor_dev_attr_temp1_input.dev_attr.attr,
910 &sensor_dev_attr_temp2_input.dev_attr.attr,
911 &sensor_dev_attr_temp3_input.dev_attr.attr,
912 &sensor_dev_attr_temp1_max.dev_attr.attr,
913 &sensor_dev_attr_temp2_max.dev_attr.attr,
914 &sensor_dev_attr_temp3_max.dev_attr.attr,
915 &sensor_dev_attr_temp1_min.dev_attr.attr,
916 &sensor_dev_attr_temp2_min.dev_attr.attr,
917 &sensor_dev_attr_temp3_min.dev_attr.attr,
918 &sensor_dev_attr_temp1_type.dev_attr.attr,
919 &sensor_dev_attr_temp2_type.dev_attr.attr,
920 &sensor_dev_attr_temp3_type.dev_attr.attr,
921 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
922 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
923 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
925 &dev_attr_alarms.attr,
926 &dev_attr_name.attr,
927 NULL
930 static const struct attribute_group it87_group = {
931 .attrs = it87_attributes,
934 static struct attribute *it87_attributes_opt[] = {
935 &sensor_dev_attr_fan1_input16.dev_attr.attr,
936 &sensor_dev_attr_fan1_min16.dev_attr.attr,
937 &sensor_dev_attr_fan2_input16.dev_attr.attr,
938 &sensor_dev_attr_fan2_min16.dev_attr.attr,
939 &sensor_dev_attr_fan3_input16.dev_attr.attr,
940 &sensor_dev_attr_fan3_min16.dev_attr.attr,
941 &sensor_dev_attr_fan4_input16.dev_attr.attr,
942 &sensor_dev_attr_fan4_min16.dev_attr.attr,
943 &sensor_dev_attr_fan5_input16.dev_attr.attr,
944 &sensor_dev_attr_fan5_min16.dev_attr.attr,
946 &sensor_dev_attr_fan1_input.dev_attr.attr,
947 &sensor_dev_attr_fan1_min.dev_attr.attr,
948 &sensor_dev_attr_fan1_div.dev_attr.attr,
949 &sensor_dev_attr_fan2_input.dev_attr.attr,
950 &sensor_dev_attr_fan2_min.dev_attr.attr,
951 &sensor_dev_attr_fan2_div.dev_attr.attr,
952 &sensor_dev_attr_fan3_input.dev_attr.attr,
953 &sensor_dev_attr_fan3_min.dev_attr.attr,
954 &sensor_dev_attr_fan3_div.dev_attr.attr,
956 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
957 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
958 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
959 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
960 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
962 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
963 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
964 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
965 &sensor_dev_attr_pwm1.dev_attr.attr,
966 &sensor_dev_attr_pwm2.dev_attr.attr,
967 &sensor_dev_attr_pwm3.dev_attr.attr,
968 &dev_attr_pwm1_freq.attr,
969 &dev_attr_pwm2_freq.attr,
970 &dev_attr_pwm3_freq.attr,
972 &dev_attr_vrm.attr,
973 &dev_attr_cpu0_vid.attr,
974 NULL
977 static const struct attribute_group it87_group_opt = {
978 .attrs = it87_attributes_opt,
981 /* SuperIO detection - will change isa_address if a chip is found */
982 static int __init it87_find(unsigned short *address,
983 struct it87_sio_data *sio_data)
985 int err = -ENODEV;
986 u16 chip_type;
987 const char *board_vendor, *board_name;
989 superio_enter();
990 chip_type = force_id ? force_id : superio_inw(DEVID);
992 switch (chip_type) {
993 case IT8705F_DEVID:
994 sio_data->type = it87;
995 break;
996 case IT8712F_DEVID:
997 sio_data->type = it8712;
998 break;
999 case IT8716F_DEVID:
1000 case IT8726F_DEVID:
1001 sio_data->type = it8716;
1002 break;
1003 case IT8718F_DEVID:
1004 sio_data->type = it8718;
1005 break;
1006 case IT8720F_DEVID:
1007 sio_data->type = it8720;
1008 break;
1009 case 0xffff: /* No device at all */
1010 goto exit;
1011 default:
1012 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1013 chip_type);
1014 goto exit;
1017 superio_select(PME);
1018 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1019 pr_info("it87: Device not activated, skipping\n");
1020 goto exit;
1023 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1024 if (*address == 0) {
1025 pr_info("it87: Base address not set, skipping\n");
1026 goto exit;
1029 err = 0;
1030 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1031 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
1032 chip_type, *address, sio_data->revision);
1034 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1035 if (sio_data->type == it87) {
1036 /* The IT8705F doesn't have VID pins at all */
1037 sio_data->skip_vid = 1;
1038 } else {
1039 int reg;
1041 superio_select(GPIO);
1042 /* We need at least 4 VID pins */
1043 reg = superio_inb(IT87_SIO_GPIO3_REG);
1044 if (reg & 0x0f) {
1045 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1046 sio_data->skip_vid = 1;
1049 /* Check if fan3 is there or not */
1050 if (reg & (1 << 6))
1051 sio_data->skip_pwm |= (1 << 2);
1052 if (reg & (1 << 7))
1053 sio_data->skip_fan |= (1 << 2);
1055 /* Check if fan2 is there or not */
1056 reg = superio_inb(IT87_SIO_GPIO5_REG);
1057 if (reg & (1 << 1))
1058 sio_data->skip_pwm |= (1 << 1);
1059 if (reg & (1 << 2))
1060 sio_data->skip_fan |= (1 << 1);
1062 if ((sio_data->type == it8718 || sio_data->type == it8720)
1063 && !(sio_data->skip_vid))
1064 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1066 reg = superio_inb(IT87_SIO_PINX2_REG);
1067 if (reg & (1 << 0))
1068 pr_info("it87: in3 is VCC (+5V)\n");
1069 if (reg & (1 << 1))
1070 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1073 /* Disable specific features based on DMI strings */
1074 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1075 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1076 if (board_vendor && board_name) {
1077 if (strcmp(board_vendor, "nVIDIA") == 0
1078 && strcmp(board_name, "FN68PT") == 0) {
1079 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1080 connected to a fan, but to something else. One user
1081 has reported instant system power-off when changing
1082 the PWM2 duty cycle, so we disable it.
1083 I use the board name string as the trigger in case
1084 the same board is ever used in other systems. */
1085 pr_info("it87: Disabling pwm2 due to "
1086 "hardware constraints\n");
1087 sio_data->skip_pwm = (1 << 1);
1091 exit:
1092 superio_exit();
1093 return err;
1096 static int __devinit it87_probe(struct platform_device *pdev)
1098 struct it87_data *data;
1099 struct resource *res;
1100 struct device *dev = &pdev->dev;
1101 struct it87_sio_data *sio_data = dev->platform_data;
1102 int err = 0;
1103 int enable_pwm_interface;
1104 static const char *names[] = {
1105 "it87",
1106 "it8712",
1107 "it8716",
1108 "it8718",
1109 "it8720",
1112 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1113 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1114 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1115 (unsigned long)res->start,
1116 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1117 err = -EBUSY;
1118 goto ERROR0;
1121 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
1122 err = -ENOMEM;
1123 goto ERROR1;
1126 data->addr = res->start;
1127 data->type = sio_data->type;
1128 data->revision = sio_data->revision;
1129 data->name = names[sio_data->type];
1131 /* Now, we do the remaining detection. */
1132 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1133 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1134 err = -ENODEV;
1135 goto ERROR2;
1138 platform_set_drvdata(pdev, data);
1140 mutex_init(&data->update_lock);
1142 /* Check PWM configuration */
1143 enable_pwm_interface = it87_check_pwm(dev);
1145 /* Initialize the IT87 chip */
1146 it87_init_device(pdev);
1148 /* Register sysfs hooks */
1149 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1150 goto ERROR2;
1152 /* Do not create fan files for disabled fans */
1153 if (has_16bit_fans(data)) {
1154 /* 16-bit tachometers */
1155 if (data->has_fan & (1 << 0)) {
1156 if ((err = device_create_file(dev,
1157 &sensor_dev_attr_fan1_input16.dev_attr))
1158 || (err = device_create_file(dev,
1159 &sensor_dev_attr_fan1_min16.dev_attr))
1160 || (err = device_create_file(dev,
1161 &sensor_dev_attr_fan1_alarm.dev_attr)))
1162 goto ERROR4;
1164 if (data->has_fan & (1 << 1)) {
1165 if ((err = device_create_file(dev,
1166 &sensor_dev_attr_fan2_input16.dev_attr))
1167 || (err = device_create_file(dev,
1168 &sensor_dev_attr_fan2_min16.dev_attr))
1169 || (err = device_create_file(dev,
1170 &sensor_dev_attr_fan2_alarm.dev_attr)))
1171 goto ERROR4;
1173 if (data->has_fan & (1 << 2)) {
1174 if ((err = device_create_file(dev,
1175 &sensor_dev_attr_fan3_input16.dev_attr))
1176 || (err = device_create_file(dev,
1177 &sensor_dev_attr_fan3_min16.dev_attr))
1178 || (err = device_create_file(dev,
1179 &sensor_dev_attr_fan3_alarm.dev_attr)))
1180 goto ERROR4;
1182 if (data->has_fan & (1 << 3)) {
1183 if ((err = device_create_file(dev,
1184 &sensor_dev_attr_fan4_input16.dev_attr))
1185 || (err = device_create_file(dev,
1186 &sensor_dev_attr_fan4_min16.dev_attr))
1187 || (err = device_create_file(dev,
1188 &sensor_dev_attr_fan4_alarm.dev_attr)))
1189 goto ERROR4;
1191 if (data->has_fan & (1 << 4)) {
1192 if ((err = device_create_file(dev,
1193 &sensor_dev_attr_fan5_input16.dev_attr))
1194 || (err = device_create_file(dev,
1195 &sensor_dev_attr_fan5_min16.dev_attr))
1196 || (err = device_create_file(dev,
1197 &sensor_dev_attr_fan5_alarm.dev_attr)))
1198 goto ERROR4;
1200 } else {
1201 /* 8-bit tachometers with clock divider */
1202 if (data->has_fan & (1 << 0)) {
1203 if ((err = device_create_file(dev,
1204 &sensor_dev_attr_fan1_input.dev_attr))
1205 || (err = device_create_file(dev,
1206 &sensor_dev_attr_fan1_min.dev_attr))
1207 || (err = device_create_file(dev,
1208 &sensor_dev_attr_fan1_div.dev_attr))
1209 || (err = device_create_file(dev,
1210 &sensor_dev_attr_fan1_alarm.dev_attr)))
1211 goto ERROR4;
1213 if (data->has_fan & (1 << 1)) {
1214 if ((err = device_create_file(dev,
1215 &sensor_dev_attr_fan2_input.dev_attr))
1216 || (err = device_create_file(dev,
1217 &sensor_dev_attr_fan2_min.dev_attr))
1218 || (err = device_create_file(dev,
1219 &sensor_dev_attr_fan2_div.dev_attr))
1220 || (err = device_create_file(dev,
1221 &sensor_dev_attr_fan2_alarm.dev_attr)))
1222 goto ERROR4;
1224 if (data->has_fan & (1 << 2)) {
1225 if ((err = device_create_file(dev,
1226 &sensor_dev_attr_fan3_input.dev_attr))
1227 || (err = device_create_file(dev,
1228 &sensor_dev_attr_fan3_min.dev_attr))
1229 || (err = device_create_file(dev,
1230 &sensor_dev_attr_fan3_div.dev_attr))
1231 || (err = device_create_file(dev,
1232 &sensor_dev_attr_fan3_alarm.dev_attr)))
1233 goto ERROR4;
1237 if (enable_pwm_interface) {
1238 if (!(sio_data->skip_pwm & (1 << 0))) {
1239 if ((err = device_create_file(dev,
1240 &sensor_dev_attr_pwm1_enable.dev_attr))
1241 || (err = device_create_file(dev,
1242 &sensor_dev_attr_pwm1.dev_attr))
1243 || (err = device_create_file(dev,
1244 &dev_attr_pwm1_freq)))
1245 goto ERROR4;
1247 if (!(sio_data->skip_pwm & (1 << 1))) {
1248 if ((err = device_create_file(dev,
1249 &sensor_dev_attr_pwm2_enable.dev_attr))
1250 || (err = device_create_file(dev,
1251 &sensor_dev_attr_pwm2.dev_attr))
1252 || (err = device_create_file(dev,
1253 &dev_attr_pwm2_freq)))
1254 goto ERROR4;
1256 if (!(sio_data->skip_pwm & (1 << 2))) {
1257 if ((err = device_create_file(dev,
1258 &sensor_dev_attr_pwm3_enable.dev_attr))
1259 || (err = device_create_file(dev,
1260 &sensor_dev_attr_pwm3.dev_attr))
1261 || (err = device_create_file(dev,
1262 &dev_attr_pwm3_freq)))
1263 goto ERROR4;
1267 if (!sio_data->skip_vid) {
1268 data->vrm = vid_which_vrm();
1269 /* VID reading from Super-I/O config space if available */
1270 data->vid = sio_data->vid_value;
1271 if ((err = device_create_file(dev,
1272 &dev_attr_vrm))
1273 || (err = device_create_file(dev,
1274 &dev_attr_cpu0_vid)))
1275 goto ERROR4;
1278 data->hwmon_dev = hwmon_device_register(dev);
1279 if (IS_ERR(data->hwmon_dev)) {
1280 err = PTR_ERR(data->hwmon_dev);
1281 goto ERROR4;
1284 return 0;
1286 ERROR4:
1287 sysfs_remove_group(&dev->kobj, &it87_group);
1288 sysfs_remove_group(&dev->kobj, &it87_group_opt);
1289 ERROR2:
1290 platform_set_drvdata(pdev, NULL);
1291 kfree(data);
1292 ERROR1:
1293 release_region(res->start, IT87_EC_EXTENT);
1294 ERROR0:
1295 return err;
1298 static int __devexit it87_remove(struct platform_device *pdev)
1300 struct it87_data *data = platform_get_drvdata(pdev);
1302 hwmon_device_unregister(data->hwmon_dev);
1303 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1304 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
1306 release_region(data->addr, IT87_EC_EXTENT);
1307 platform_set_drvdata(pdev, NULL);
1308 kfree(data);
1310 return 0;
1313 /* Must be called with data->update_lock held, except during initialization.
1314 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1315 would slow down the IT87 access and should not be necessary. */
1316 static int it87_read_value(struct it87_data *data, u8 reg)
1318 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1319 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
1322 /* Must be called with data->update_lock held, except during initialization.
1323 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1324 would slow down the IT87 access and should not be necessary. */
1325 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
1327 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1328 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
1331 /* Return 1 if and only if the PWM interface is safe to use */
1332 static int __devinit it87_check_pwm(struct device *dev)
1334 struct it87_data *data = dev_get_drvdata(dev);
1335 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1336 * and polarity set to active low is sign that this is the case so we
1337 * disable pwm control to protect the user. */
1338 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1339 if ((tmp & 0x87) == 0) {
1340 if (fix_pwm_polarity) {
1341 /* The user asks us to attempt a chip reconfiguration.
1342 * This means switching to active high polarity and
1343 * inverting all fan speed values. */
1344 int i;
1345 u8 pwm[3];
1347 for (i = 0; i < 3; i++)
1348 pwm[i] = it87_read_value(data,
1349 IT87_REG_PWM(i));
1351 /* If any fan is in automatic pwm mode, the polarity
1352 * might be correct, as suspicious as it seems, so we
1353 * better don't change anything (but still disable the
1354 * PWM interface). */
1355 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1356 dev_info(dev, "Reconfiguring PWM to "
1357 "active high polarity\n");
1358 it87_write_value(data, IT87_REG_FAN_CTL,
1359 tmp | 0x87);
1360 for (i = 0; i < 3; i++)
1361 it87_write_value(data,
1362 IT87_REG_PWM(i),
1363 0x7f & ~pwm[i]);
1364 return 1;
1367 dev_info(dev, "PWM configuration is "
1368 "too broken to be fixed\n");
1371 dev_info(dev, "Detected broken BIOS "
1372 "defaults, disabling PWM interface\n");
1373 return 0;
1374 } else if (fix_pwm_polarity) {
1375 dev_info(dev, "PWM configuration looks "
1376 "sane, won't touch\n");
1379 return 1;
1382 /* Called when we have found a new IT87. */
1383 static void __devinit it87_init_device(struct platform_device *pdev)
1385 struct it87_sio_data *sio_data = pdev->dev.platform_data;
1386 struct it87_data *data = platform_get_drvdata(pdev);
1387 int tmp, i;
1388 u8 mask;
1390 /* initialize to sane defaults:
1391 * - if the chip is in manual pwm mode, this will be overwritten with
1392 * the actual settings on the chip (so in this case, initialization
1393 * is not needed)
1394 * - if in automatic or on/off mode, we could switch to manual mode,
1395 * read the registers and set manual_pwm_ctl accordingly, but currently
1396 * this is not implemented, so we initialize to something sane */
1397 for (i = 0; i < 3; i++) {
1398 data->manual_pwm_ctl[i] = 0xff;
1401 /* Some chips seem to have default value 0xff for all limit
1402 * registers. For low voltage limits it makes no sense and triggers
1403 * alarms, so change to 0 instead. For high temperature limits, it
1404 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1405 * but is still confusing, so change to 127 degrees C. */
1406 for (i = 0; i < 8; i++) {
1407 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
1408 if (tmp == 0xff)
1409 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
1411 for (i = 0; i < 3; i++) {
1412 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1413 if (tmp == 0xff)
1414 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
1417 /* Check if temperature channels are reset manually or by some reason */
1418 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1419 if ((tmp & 0x3f) == 0) {
1420 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1421 tmp = (tmp & 0xc0) | 0x2a;
1422 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
1424 data->sensor = tmp;
1426 /* Check if voltage monitors are reset manually or by some reason */
1427 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
1428 if ((tmp & 0xff) == 0) {
1429 /* Enable all voltage monitors */
1430 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
1433 /* Check if tachometers are reset manually or by some reason */
1434 mask = 0x70 & ~(sio_data->skip_fan << 4);
1435 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
1436 if ((data->fan_main_ctrl & mask) == 0) {
1437 /* Enable all fan tachometers */
1438 data->fan_main_ctrl |= mask;
1439 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1441 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1443 /* Set tachometers to 16-bit mode if needed */
1444 if (has_16bit_fans(data)) {
1445 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
1446 if (~tmp & 0x07 & data->has_fan) {
1447 dev_dbg(&pdev->dev,
1448 "Setting fan1-3 to 16-bit mode\n");
1449 it87_write_value(data, IT87_REG_FAN_16BIT,
1450 tmp | 0x07);
1452 /* IT8705F only supports three fans. */
1453 if (data->type != it87) {
1454 if (tmp & (1 << 4))
1455 data->has_fan |= (1 << 3); /* fan4 enabled */
1456 if (tmp & (1 << 5))
1457 data->has_fan |= (1 << 4); /* fan5 enabled */
1461 /* Fan input pins may be used for alternative functions */
1462 data->has_fan &= ~sio_data->skip_fan;
1464 /* Set current fan mode registers and the default settings for the
1465 * other mode registers */
1466 for (i = 0; i < 3; i++) {
1467 if (data->fan_main_ctrl & (1 << i)) {
1468 /* pwm mode */
1469 tmp = it87_read_value(data, IT87_REG_PWM(i));
1470 if (tmp & 0x80) {
1471 /* automatic pwm - not yet implemented, but
1472 * leave the settings made by the BIOS alone
1473 * until a change is requested via the sysfs
1474 * interface */
1475 } else {
1476 /* manual pwm */
1477 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1482 /* Start monitoring */
1483 it87_write_value(data, IT87_REG_CONFIG,
1484 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1485 | (update_vbat ? 0x41 : 0x01));
1488 static struct it87_data *it87_update_device(struct device *dev)
1490 struct it87_data *data = dev_get_drvdata(dev);
1491 int i;
1493 mutex_lock(&data->update_lock);
1495 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1496 || !data->valid) {
1498 if (update_vbat) {
1499 /* Cleared after each update, so reenable. Value
1500 returned by this read will be previous value */
1501 it87_write_value(data, IT87_REG_CONFIG,
1502 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1504 for (i = 0; i <= 7; i++) {
1505 data->in[i] =
1506 it87_read_value(data, IT87_REG_VIN(i));
1507 data->in_min[i] =
1508 it87_read_value(data, IT87_REG_VIN_MIN(i));
1509 data->in_max[i] =
1510 it87_read_value(data, IT87_REG_VIN_MAX(i));
1512 /* in8 (battery) has no limit registers */
1513 data->in[8] =
1514 it87_read_value(data, IT87_REG_VIN(8));
1516 for (i = 0; i < 5; i++) {
1517 /* Skip disabled fans */
1518 if (!(data->has_fan & (1 << i)))
1519 continue;
1521 data->fan_min[i] =
1522 it87_read_value(data, IT87_REG_FAN_MIN[i]);
1523 data->fan[i] = it87_read_value(data,
1524 IT87_REG_FAN[i]);
1525 /* Add high byte if in 16-bit mode */
1526 if (has_16bit_fans(data)) {
1527 data->fan[i] |= it87_read_value(data,
1528 IT87_REG_FANX[i]) << 8;
1529 data->fan_min[i] |= it87_read_value(data,
1530 IT87_REG_FANX_MIN[i]) << 8;
1533 for (i = 0; i < 3; i++) {
1534 data->temp[i] =
1535 it87_read_value(data, IT87_REG_TEMP(i));
1536 data->temp_high[i] =
1537 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1538 data->temp_low[i] =
1539 it87_read_value(data, IT87_REG_TEMP_LOW(i));
1542 /* Newer chips don't have clock dividers */
1543 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1544 i = it87_read_value(data, IT87_REG_FAN_DIV);
1545 data->fan_div[0] = i & 0x07;
1546 data->fan_div[1] = (i >> 3) & 0x07;
1547 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1550 data->alarms =
1551 it87_read_value(data, IT87_REG_ALARM1) |
1552 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1553 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1554 data->fan_main_ctrl = it87_read_value(data,
1555 IT87_REG_FAN_MAIN_CTRL);
1556 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
1558 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1559 /* The 8705 does not have VID capability.
1560 The 8718 and the 8720 don't use IT87_REG_VID for the
1561 same purpose. */
1562 if (data->type == it8712 || data->type == it8716) {
1563 data->vid = it87_read_value(data, IT87_REG_VID);
1564 /* The older IT8712F revisions had only 5 VID pins,
1565 but we assume it is always safe to read 6 bits. */
1566 data->vid &= 0x3f;
1568 data->last_updated = jiffies;
1569 data->valid = 1;
1572 mutex_unlock(&data->update_lock);
1574 return data;
1577 static int __init it87_device_add(unsigned short address,
1578 const struct it87_sio_data *sio_data)
1580 struct resource res = {
1581 .start = address + IT87_EC_OFFSET,
1582 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
1583 .name = DRVNAME,
1584 .flags = IORESOURCE_IO,
1586 int err;
1588 err = acpi_check_resource_conflict(&res);
1589 if (err)
1590 goto exit;
1592 pdev = platform_device_alloc(DRVNAME, address);
1593 if (!pdev) {
1594 err = -ENOMEM;
1595 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1596 goto exit;
1599 err = platform_device_add_resources(pdev, &res, 1);
1600 if (err) {
1601 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1602 "(%d)\n", err);
1603 goto exit_device_put;
1606 err = platform_device_add_data(pdev, sio_data,
1607 sizeof(struct it87_sio_data));
1608 if (err) {
1609 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1610 goto exit_device_put;
1613 err = platform_device_add(pdev);
1614 if (err) {
1615 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1616 err);
1617 goto exit_device_put;
1620 return 0;
1622 exit_device_put:
1623 platform_device_put(pdev);
1624 exit:
1625 return err;
1628 static int __init sm_it87_init(void)
1630 int err;
1631 unsigned short isa_address=0;
1632 struct it87_sio_data sio_data;
1634 memset(&sio_data, 0, sizeof(struct it87_sio_data));
1635 err = it87_find(&isa_address, &sio_data);
1636 if (err)
1637 return err;
1638 err = platform_driver_register(&it87_driver);
1639 if (err)
1640 return err;
1642 err = it87_device_add(isa_address, &sio_data);
1643 if (err){
1644 platform_driver_unregister(&it87_driver);
1645 return err;
1648 return 0;
1651 static void __exit sm_it87_exit(void)
1653 platform_device_unregister(pdev);
1654 platform_driver_unregister(&it87_driver);
1658 MODULE_AUTHOR("Chris Gauthron, "
1659 "Jean Delvare <khali@linux-fr.org>");
1660 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
1661 module_param(update_vbat, bool, 0);
1662 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1663 module_param(fix_pwm_polarity, bool, 0);
1664 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1665 MODULE_LICENSE("GPL");
1667 module_init(sm_it87_init);
1668 module_exit(sm_it87_exit);