iwlwifi: fix skb usage after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / it87.c
blobec7e8a0b06485488d000a5ebe2c7051219ce6676
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 inline void
84 superio_outb(int reg, int val)
86 outb(reg, REG);
87 outb(val, VAL);
90 static int superio_inw(int reg)
92 int val;
93 outb(reg++, REG);
94 val = inb(VAL) << 8;
95 outb(reg, REG);
96 val |= inb(VAL);
97 return val;
100 static inline void
101 superio_select(int ldn)
103 outb(DEV, REG);
104 outb(ldn, VAL);
107 static inline void
108 superio_enter(void)
110 outb(0x87, REG);
111 outb(0x01, REG);
112 outb(0x55, REG);
113 outb(0x55, REG);
116 static inline void
117 superio_exit(void)
119 outb(0x02, REG);
120 outb(0x02, VAL);
123 /* Logical device 4 registers */
124 #define IT8712F_DEVID 0x8712
125 #define IT8705F_DEVID 0x8705
126 #define IT8716F_DEVID 0x8716
127 #define IT8718F_DEVID 0x8718
128 #define IT8720F_DEVID 0x8720
129 #define IT8726F_DEVID 0x8726
130 #define IT87_ACT_REG 0x30
131 #define IT87_BASE_REG 0x60
133 /* Logical device 7 registers (IT8712F and later) */
134 #define IT87_SIO_GPIO3_REG 0x27
135 #define IT87_SIO_GPIO5_REG 0x29
136 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
137 #define IT87_SIO_VID_REG 0xfc /* VID value */
139 /* Update battery voltage after every reading if true */
140 static int update_vbat;
142 /* Not all BIOSes properly configure the PWM registers */
143 static int fix_pwm_polarity;
145 /* Many IT87 constants specified below */
147 /* Length of ISA address segment */
148 #define IT87_EXTENT 8
150 /* Length of ISA address segment for Environmental Controller */
151 #define IT87_EC_EXTENT 2
153 /* Offset of EC registers from ISA base address */
154 #define IT87_EC_OFFSET 5
156 /* Where are the ISA address/data registers relative to the EC base address */
157 #define IT87_ADDR_REG_OFFSET 0
158 #define IT87_DATA_REG_OFFSET 1
160 /*----- The IT87 registers -----*/
162 #define IT87_REG_CONFIG 0x00
164 #define IT87_REG_ALARM1 0x01
165 #define IT87_REG_ALARM2 0x02
166 #define IT87_REG_ALARM3 0x03
168 /* The IT8718F and IT8720F have the VID value in a different register, in
169 Super-I/O configuration space. */
170 #define IT87_REG_VID 0x0a
171 /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
172 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
173 mode. */
174 #define IT87_REG_FAN_DIV 0x0b
175 #define IT87_REG_FAN_16BIT 0x0c
177 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
179 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
180 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
181 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
182 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
183 #define IT87_REG_FAN_MAIN_CTRL 0x13
184 #define IT87_REG_FAN_CTL 0x14
185 #define IT87_REG_PWM(nr) (0x15 + (nr))
187 #define IT87_REG_VIN(nr) (0x20 + (nr))
188 #define IT87_REG_TEMP(nr) (0x29 + (nr))
190 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
191 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
192 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
193 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
195 #define IT87_REG_VIN_ENABLE 0x50
196 #define IT87_REG_TEMP_ENABLE 0x51
198 #define IT87_REG_CHIPID 0x58
200 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
201 #define IN_FROM_REG(val) ((val) * 16)
203 static inline u8 FAN_TO_REG(long rpm, int div)
205 if (rpm == 0)
206 return 255;
207 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
208 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
209 254);
212 static inline u16 FAN16_TO_REG(long rpm)
214 if (rpm == 0)
215 return 0xffff;
216 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
219 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
220 /* The divider is fixed to 2 in 16-bit mode */
221 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
223 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
224 ((val)+500)/1000),-128,127))
225 #define TEMP_FROM_REG(val) ((val) * 1000)
227 #define PWM_TO_REG(val) ((val) >> 1)
228 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
230 static int DIV_TO_REG(int val)
232 int answer = 0;
233 while (answer < 7 && (val >>= 1))
234 answer++;
235 return answer;
237 #define DIV_FROM_REG(val) (1 << (val))
239 static const unsigned int pwm_freq[8] = {
240 48000000 / 128,
241 24000000 / 128,
242 12000000 / 128,
243 8000000 / 128,
244 6000000 / 128,
245 3000000 / 128,
246 1500000 / 128,
247 750000 / 128,
251 struct it87_sio_data {
252 enum chips type;
253 /* Values read from Super-I/O config space */
254 u8 revision;
255 u8 vid_value;
256 /* Features skipped based on config or DMI */
257 u8 skip_vid;
258 u8 skip_fan;
259 u8 skip_pwm;
262 /* For each registered chip, we need to keep some data in memory.
263 The structure is dynamically allocated. */
264 struct it87_data {
265 struct device *hwmon_dev;
266 enum chips type;
267 u8 revision;
269 unsigned short addr;
270 const char *name;
271 struct mutex update_lock;
272 char valid; /* !=0 if following fields are valid */
273 unsigned long last_updated; /* In jiffies */
275 u8 in[9]; /* Register value */
276 u8 in_max[8]; /* Register value */
277 u8 in_min[8]; /* Register value */
278 u8 has_fan; /* Bitfield, fans enabled */
279 u16 fan[5]; /* Register values, possibly combined */
280 u16 fan_min[5]; /* Register values, possibly combined */
281 s8 temp[3]; /* Register value */
282 s8 temp_high[3]; /* Register value */
283 s8 temp_low[3]; /* Register value */
284 u8 sensor; /* Register value */
285 u8 fan_div[3]; /* Register encoding, shifted right */
286 u8 vid; /* Register encoding, combined */
287 u8 vrm;
288 u32 alarms; /* Register encoding, combined */
289 u8 fan_main_ctrl; /* Register value */
290 u8 fan_ctl; /* Register value */
291 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
294 static inline int has_16bit_fans(const struct it87_data *data)
296 /* IT8705F Datasheet 0.4.1, 3h == Version G.
297 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
298 These are the first revisions with 16bit tachometer support. */
299 return (data->type == it87 && data->revision >= 0x03)
300 || (data->type == it8712 && data->revision >= 0x08)
301 || data->type == it8716
302 || data->type == it8718
303 || data->type == it8720;
306 static int it87_probe(struct platform_device *pdev);
307 static int __devexit it87_remove(struct platform_device *pdev);
309 static int it87_read_value(struct it87_data *data, u8 reg);
310 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
311 static struct it87_data *it87_update_device(struct device *dev);
312 static int it87_check_pwm(struct device *dev);
313 static void it87_init_device(struct platform_device *pdev);
316 static struct platform_driver it87_driver = {
317 .driver = {
318 .owner = THIS_MODULE,
319 .name = DRVNAME,
321 .probe = it87_probe,
322 .remove = __devexit_p(it87_remove),
325 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
326 char *buf)
328 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
329 int nr = sensor_attr->index;
331 struct it87_data *data = it87_update_device(dev);
332 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
335 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
336 char *buf)
338 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
339 int nr = sensor_attr->index;
341 struct it87_data *data = it87_update_device(dev);
342 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
345 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
346 char *buf)
348 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
349 int nr = sensor_attr->index;
351 struct it87_data *data = it87_update_device(dev);
352 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
355 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
356 const char *buf, size_t count)
358 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
359 int nr = sensor_attr->index;
361 struct it87_data *data = dev_get_drvdata(dev);
362 unsigned long val = simple_strtoul(buf, NULL, 10);
364 mutex_lock(&data->update_lock);
365 data->in_min[nr] = IN_TO_REG(val);
366 it87_write_value(data, IT87_REG_VIN_MIN(nr),
367 data->in_min[nr]);
368 mutex_unlock(&data->update_lock);
369 return count;
371 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
372 const char *buf, size_t count)
374 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
375 int nr = sensor_attr->index;
377 struct it87_data *data = dev_get_drvdata(dev);
378 unsigned long val = simple_strtoul(buf, NULL, 10);
380 mutex_lock(&data->update_lock);
381 data->in_max[nr] = IN_TO_REG(val);
382 it87_write_value(data, IT87_REG_VIN_MAX(nr),
383 data->in_max[nr]);
384 mutex_unlock(&data->update_lock);
385 return count;
388 #define show_in_offset(offset) \
389 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
390 show_in, NULL, offset);
392 #define limit_in_offset(offset) \
393 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
394 show_in_min, set_in_min, offset); \
395 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
396 show_in_max, set_in_max, offset);
398 show_in_offset(0);
399 limit_in_offset(0);
400 show_in_offset(1);
401 limit_in_offset(1);
402 show_in_offset(2);
403 limit_in_offset(2);
404 show_in_offset(3);
405 limit_in_offset(3);
406 show_in_offset(4);
407 limit_in_offset(4);
408 show_in_offset(5);
409 limit_in_offset(5);
410 show_in_offset(6);
411 limit_in_offset(6);
412 show_in_offset(7);
413 limit_in_offset(7);
414 show_in_offset(8);
416 /* 3 temperatures */
417 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
418 char *buf)
420 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
421 int nr = sensor_attr->index;
423 struct it87_data *data = it87_update_device(dev);
424 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
426 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
427 char *buf)
429 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
430 int nr = sensor_attr->index;
432 struct it87_data *data = it87_update_device(dev);
433 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
435 static ssize_t show_temp_min(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 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
444 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
445 const char *buf, size_t count)
447 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
448 int nr = sensor_attr->index;
450 struct it87_data *data = dev_get_drvdata(dev);
451 int val = simple_strtol(buf, NULL, 10);
453 mutex_lock(&data->update_lock);
454 data->temp_high[nr] = TEMP_TO_REG(val);
455 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
456 mutex_unlock(&data->update_lock);
457 return count;
459 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
460 const char *buf, size_t count)
462 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
463 int nr = sensor_attr->index;
465 struct it87_data *data = dev_get_drvdata(dev);
466 int val = simple_strtol(buf, NULL, 10);
468 mutex_lock(&data->update_lock);
469 data->temp_low[nr] = TEMP_TO_REG(val);
470 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
471 mutex_unlock(&data->update_lock);
472 return count;
474 #define show_temp_offset(offset) \
475 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
476 show_temp, NULL, offset - 1); \
477 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
478 show_temp_max, set_temp_max, offset - 1); \
479 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
480 show_temp_min, set_temp_min, offset - 1);
482 show_temp_offset(1);
483 show_temp_offset(2);
484 show_temp_offset(3);
486 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
487 char *buf)
489 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
490 int nr = sensor_attr->index;
492 struct it87_data *data = it87_update_device(dev);
493 u8 reg = data->sensor; /* In case the value is updated while we use it */
495 if (reg & (1 << nr))
496 return sprintf(buf, "3\n"); /* thermal diode */
497 if (reg & (8 << nr))
498 return sprintf(buf, "4\n"); /* thermistor */
499 return sprintf(buf, "0\n"); /* disabled */
501 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
502 const char *buf, size_t count)
504 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
505 int nr = sensor_attr->index;
507 struct it87_data *data = dev_get_drvdata(dev);
508 int val = simple_strtol(buf, NULL, 10);
510 mutex_lock(&data->update_lock);
512 data->sensor &= ~(1 << nr);
513 data->sensor &= ~(8 << nr);
514 if (val == 2) { /* backwards compatibility */
515 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
516 "instead\n");
517 val = 4;
519 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
520 if (val == 3)
521 data->sensor |= 1 << nr;
522 else if (val == 4)
523 data->sensor |= 8 << nr;
524 else if (val != 0) {
525 mutex_unlock(&data->update_lock);
526 return -EINVAL;
528 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
529 mutex_unlock(&data->update_lock);
530 return count;
532 #define show_sensor_offset(offset) \
533 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
534 show_sensor, set_sensor, offset - 1);
536 show_sensor_offset(1);
537 show_sensor_offset(2);
538 show_sensor_offset(3);
540 /* 3 Fans */
541 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
542 char *buf)
544 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
545 int nr = sensor_attr->index;
547 struct it87_data *data = it87_update_device(dev);
548 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
549 DIV_FROM_REG(data->fan_div[nr])));
551 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
552 char *buf)
554 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
555 int nr = sensor_attr->index;
557 struct it87_data *data = it87_update_device(dev);
558 return sprintf(buf,"%d\n",
559 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
561 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
562 char *buf)
564 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
565 int nr = sensor_attr->index;
567 struct it87_data *data = it87_update_device(dev);
568 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
570 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
571 char *buf)
573 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
574 int nr = sensor_attr->index;
576 struct it87_data *data = it87_update_device(dev);
577 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
579 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
580 char *buf)
582 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
583 int nr = sensor_attr->index;
585 struct it87_data *data = it87_update_device(dev);
586 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
588 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
589 char *buf)
591 struct it87_data *data = it87_update_device(dev);
592 int index = (data->fan_ctl >> 4) & 0x07;
594 return sprintf(buf, "%u\n", pwm_freq[index]);
596 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
599 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
600 int nr = sensor_attr->index;
602 struct it87_data *data = dev_get_drvdata(dev);
603 int val = simple_strtol(buf, NULL, 10);
604 u8 reg;
606 mutex_lock(&data->update_lock);
607 reg = it87_read_value(data, IT87_REG_FAN_DIV);
608 switch (nr) {
609 case 0: data->fan_div[nr] = reg & 0x07; break;
610 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
611 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
614 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
615 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
616 mutex_unlock(&data->update_lock);
617 return count;
619 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
620 const char *buf, size_t count)
622 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
623 int nr = sensor_attr->index;
625 struct it87_data *data = dev_get_drvdata(dev);
626 unsigned long val = simple_strtoul(buf, NULL, 10);
627 int min;
628 u8 old;
630 mutex_lock(&data->update_lock);
631 old = it87_read_value(data, IT87_REG_FAN_DIV);
633 /* Save fan min limit */
634 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
636 switch (nr) {
637 case 0:
638 case 1:
639 data->fan_div[nr] = DIV_TO_REG(val);
640 break;
641 case 2:
642 if (val < 8)
643 data->fan_div[nr] = 1;
644 else
645 data->fan_div[nr] = 3;
647 val = old & 0x80;
648 val |= (data->fan_div[0] & 0x07);
649 val |= (data->fan_div[1] & 0x07) << 3;
650 if (data->fan_div[2] == 3)
651 val |= 0x1 << 6;
652 it87_write_value(data, IT87_REG_FAN_DIV, val);
654 /* Restore fan min limit */
655 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
656 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
658 mutex_unlock(&data->update_lock);
659 return count;
661 static ssize_t set_pwm_enable(struct device *dev,
662 struct device_attribute *attr, const char *buf, size_t count)
664 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
665 int nr = sensor_attr->index;
667 struct it87_data *data = dev_get_drvdata(dev);
668 int val = simple_strtol(buf, NULL, 10);
670 mutex_lock(&data->update_lock);
672 if (val == 0) {
673 int tmp;
674 /* make sure the fan is on when in on/off mode */
675 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
676 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
677 /* set on/off mode */
678 data->fan_main_ctrl &= ~(1 << nr);
679 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
680 } else if (val == 1) {
681 /* set SmartGuardian mode */
682 data->fan_main_ctrl |= (1 << nr);
683 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
684 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
685 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
686 } else {
687 mutex_unlock(&data->update_lock);
688 return -EINVAL;
691 mutex_unlock(&data->update_lock);
692 return count;
694 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
695 const char *buf, size_t count)
697 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
698 int nr = sensor_attr->index;
700 struct it87_data *data = dev_get_drvdata(dev);
701 int val = simple_strtol(buf, NULL, 10);
703 if (val < 0 || val > 255)
704 return -EINVAL;
706 mutex_lock(&data->update_lock);
707 data->manual_pwm_ctl[nr] = val;
708 if (data->fan_main_ctrl & (1 << nr))
709 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
710 mutex_unlock(&data->update_lock);
711 return count;
713 static ssize_t set_pwm_freq(struct device *dev,
714 struct device_attribute *attr, const char *buf, size_t count)
716 struct it87_data *data = dev_get_drvdata(dev);
717 unsigned long val = simple_strtoul(buf, NULL, 10);
718 int i;
720 /* Search for the nearest available frequency */
721 for (i = 0; i < 7; i++) {
722 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
723 break;
726 mutex_lock(&data->update_lock);
727 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
728 data->fan_ctl |= i << 4;
729 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
730 mutex_unlock(&data->update_lock);
732 return count;
735 #define show_fan_offset(offset) \
736 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
737 show_fan, NULL, offset - 1); \
738 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
739 show_fan_min, set_fan_min, offset - 1); \
740 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
741 show_fan_div, set_fan_div, offset - 1);
743 show_fan_offset(1);
744 show_fan_offset(2);
745 show_fan_offset(3);
747 #define show_pwm_offset(offset) \
748 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
749 show_pwm_enable, set_pwm_enable, offset - 1); \
750 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
751 show_pwm, set_pwm, offset - 1); \
752 static DEVICE_ATTR(pwm##offset##_freq, \
753 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
754 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
756 show_pwm_offset(1);
757 show_pwm_offset(2);
758 show_pwm_offset(3);
760 /* A different set of callbacks for 16-bit fans */
761 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
762 char *buf)
764 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
765 int nr = sensor_attr->index;
766 struct it87_data *data = it87_update_device(dev);
767 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
770 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
771 char *buf)
773 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
774 int nr = sensor_attr->index;
775 struct it87_data *data = it87_update_device(dev);
776 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
779 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
780 const char *buf, size_t count)
782 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
783 int nr = sensor_attr->index;
784 struct it87_data *data = dev_get_drvdata(dev);
785 int val = simple_strtol(buf, NULL, 10);
787 mutex_lock(&data->update_lock);
788 data->fan_min[nr] = FAN16_TO_REG(val);
789 it87_write_value(data, IT87_REG_FAN_MIN[nr],
790 data->fan_min[nr] & 0xff);
791 it87_write_value(data, IT87_REG_FANX_MIN[nr],
792 data->fan_min[nr] >> 8);
793 mutex_unlock(&data->update_lock);
794 return count;
797 /* We want to use the same sysfs file names as 8-bit fans, but we need
798 different variable names, so we have to use SENSOR_ATTR instead of
799 SENSOR_DEVICE_ATTR. */
800 #define show_fan16_offset(offset) \
801 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
802 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
803 show_fan16, NULL, offset - 1); \
804 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
805 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
806 show_fan16_min, set_fan16_min, offset - 1)
808 show_fan16_offset(1);
809 show_fan16_offset(2);
810 show_fan16_offset(3);
811 show_fan16_offset(4);
812 show_fan16_offset(5);
814 /* Alarms */
815 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
817 struct it87_data *data = it87_update_device(dev);
818 return sprintf(buf, "%u\n", data->alarms);
820 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
822 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
823 char *buf)
825 int bitnr = to_sensor_dev_attr(attr)->index;
826 struct it87_data *data = it87_update_device(dev);
827 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
829 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
830 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
831 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
832 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
833 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
834 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
835 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
836 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
837 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
838 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
839 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
840 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
841 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
842 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
843 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
844 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
846 static ssize_t
847 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
849 struct it87_data *data = dev_get_drvdata(dev);
850 return sprintf(buf, "%u\n", data->vrm);
852 static ssize_t
853 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
855 struct it87_data *data = dev_get_drvdata(dev);
856 u32 val;
858 val = simple_strtoul(buf, NULL, 10);
859 data->vrm = val;
861 return count;
863 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
865 static ssize_t
866 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
868 struct it87_data *data = it87_update_device(dev);
869 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
871 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
873 static ssize_t show_name(struct device *dev, struct device_attribute
874 *devattr, char *buf)
876 struct it87_data *data = dev_get_drvdata(dev);
877 return sprintf(buf, "%s\n", data->name);
879 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
881 static struct attribute *it87_attributes[] = {
882 &sensor_dev_attr_in0_input.dev_attr.attr,
883 &sensor_dev_attr_in1_input.dev_attr.attr,
884 &sensor_dev_attr_in2_input.dev_attr.attr,
885 &sensor_dev_attr_in3_input.dev_attr.attr,
886 &sensor_dev_attr_in4_input.dev_attr.attr,
887 &sensor_dev_attr_in5_input.dev_attr.attr,
888 &sensor_dev_attr_in6_input.dev_attr.attr,
889 &sensor_dev_attr_in7_input.dev_attr.attr,
890 &sensor_dev_attr_in8_input.dev_attr.attr,
891 &sensor_dev_attr_in0_min.dev_attr.attr,
892 &sensor_dev_attr_in1_min.dev_attr.attr,
893 &sensor_dev_attr_in2_min.dev_attr.attr,
894 &sensor_dev_attr_in3_min.dev_attr.attr,
895 &sensor_dev_attr_in4_min.dev_attr.attr,
896 &sensor_dev_attr_in5_min.dev_attr.attr,
897 &sensor_dev_attr_in6_min.dev_attr.attr,
898 &sensor_dev_attr_in7_min.dev_attr.attr,
899 &sensor_dev_attr_in0_max.dev_attr.attr,
900 &sensor_dev_attr_in1_max.dev_attr.attr,
901 &sensor_dev_attr_in2_max.dev_attr.attr,
902 &sensor_dev_attr_in3_max.dev_attr.attr,
903 &sensor_dev_attr_in4_max.dev_attr.attr,
904 &sensor_dev_attr_in5_max.dev_attr.attr,
905 &sensor_dev_attr_in6_max.dev_attr.attr,
906 &sensor_dev_attr_in7_max.dev_attr.attr,
907 &sensor_dev_attr_in0_alarm.dev_attr.attr,
908 &sensor_dev_attr_in1_alarm.dev_attr.attr,
909 &sensor_dev_attr_in2_alarm.dev_attr.attr,
910 &sensor_dev_attr_in3_alarm.dev_attr.attr,
911 &sensor_dev_attr_in4_alarm.dev_attr.attr,
912 &sensor_dev_attr_in5_alarm.dev_attr.attr,
913 &sensor_dev_attr_in6_alarm.dev_attr.attr,
914 &sensor_dev_attr_in7_alarm.dev_attr.attr,
916 &sensor_dev_attr_temp1_input.dev_attr.attr,
917 &sensor_dev_attr_temp2_input.dev_attr.attr,
918 &sensor_dev_attr_temp3_input.dev_attr.attr,
919 &sensor_dev_attr_temp1_max.dev_attr.attr,
920 &sensor_dev_attr_temp2_max.dev_attr.attr,
921 &sensor_dev_attr_temp3_max.dev_attr.attr,
922 &sensor_dev_attr_temp1_min.dev_attr.attr,
923 &sensor_dev_attr_temp2_min.dev_attr.attr,
924 &sensor_dev_attr_temp3_min.dev_attr.attr,
925 &sensor_dev_attr_temp1_type.dev_attr.attr,
926 &sensor_dev_attr_temp2_type.dev_attr.attr,
927 &sensor_dev_attr_temp3_type.dev_attr.attr,
928 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
929 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
930 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
932 &dev_attr_alarms.attr,
933 &dev_attr_name.attr,
934 NULL
937 static const struct attribute_group it87_group = {
938 .attrs = it87_attributes,
941 static struct attribute *it87_attributes_opt[] = {
942 &sensor_dev_attr_fan1_input16.dev_attr.attr,
943 &sensor_dev_attr_fan1_min16.dev_attr.attr,
944 &sensor_dev_attr_fan2_input16.dev_attr.attr,
945 &sensor_dev_attr_fan2_min16.dev_attr.attr,
946 &sensor_dev_attr_fan3_input16.dev_attr.attr,
947 &sensor_dev_attr_fan3_min16.dev_attr.attr,
948 &sensor_dev_attr_fan4_input16.dev_attr.attr,
949 &sensor_dev_attr_fan4_min16.dev_attr.attr,
950 &sensor_dev_attr_fan5_input16.dev_attr.attr,
951 &sensor_dev_attr_fan5_min16.dev_attr.attr,
953 &sensor_dev_attr_fan1_input.dev_attr.attr,
954 &sensor_dev_attr_fan1_min.dev_attr.attr,
955 &sensor_dev_attr_fan1_div.dev_attr.attr,
956 &sensor_dev_attr_fan2_input.dev_attr.attr,
957 &sensor_dev_attr_fan2_min.dev_attr.attr,
958 &sensor_dev_attr_fan2_div.dev_attr.attr,
959 &sensor_dev_attr_fan3_input.dev_attr.attr,
960 &sensor_dev_attr_fan3_min.dev_attr.attr,
961 &sensor_dev_attr_fan3_div.dev_attr.attr,
963 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
964 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
965 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
966 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
967 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
969 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
970 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
971 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
972 &sensor_dev_attr_pwm1.dev_attr.attr,
973 &sensor_dev_attr_pwm2.dev_attr.attr,
974 &sensor_dev_attr_pwm3.dev_attr.attr,
975 &dev_attr_pwm1_freq.attr,
976 &dev_attr_pwm2_freq.attr,
977 &dev_attr_pwm3_freq.attr,
979 &dev_attr_vrm.attr,
980 &dev_attr_cpu0_vid.attr,
981 NULL
984 static const struct attribute_group it87_group_opt = {
985 .attrs = it87_attributes_opt,
988 /* SuperIO detection - will change isa_address if a chip is found */
989 static int __init it87_find(unsigned short *address,
990 struct it87_sio_data *sio_data)
992 int err = -ENODEV;
993 u16 chip_type;
994 const char *board_vendor, *board_name;
996 superio_enter();
997 chip_type = force_id ? force_id : superio_inw(DEVID);
999 switch (chip_type) {
1000 case IT8705F_DEVID:
1001 sio_data->type = it87;
1002 break;
1003 case IT8712F_DEVID:
1004 sio_data->type = it8712;
1005 break;
1006 case IT8716F_DEVID:
1007 case IT8726F_DEVID:
1008 sio_data->type = it8716;
1009 break;
1010 case IT8718F_DEVID:
1011 sio_data->type = it8718;
1012 break;
1013 case IT8720F_DEVID:
1014 sio_data->type = it8720;
1015 break;
1016 case 0xffff: /* No device at all */
1017 goto exit;
1018 default:
1019 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1020 chip_type);
1021 goto exit;
1024 superio_select(PME);
1025 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1026 pr_info("it87: Device not activated, skipping\n");
1027 goto exit;
1030 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1031 if (*address == 0) {
1032 pr_info("it87: Base address not set, skipping\n");
1033 goto exit;
1036 err = 0;
1037 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1038 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
1039 chip_type, *address, sio_data->revision);
1041 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1042 if (sio_data->type == it87) {
1043 /* The IT8705F doesn't have VID pins at all */
1044 sio_data->skip_vid = 1;
1045 } else {
1046 int reg;
1048 superio_select(GPIO);
1049 /* We need at least 4 VID pins */
1050 reg = superio_inb(IT87_SIO_GPIO3_REG);
1051 if (reg & 0x0f) {
1052 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1053 sio_data->skip_vid = 1;
1056 /* Check if fan3 is there or not */
1057 if (reg & (1 << 6))
1058 sio_data->skip_pwm |= (1 << 2);
1059 if (reg & (1 << 7))
1060 sio_data->skip_fan |= (1 << 2);
1062 /* Check if fan2 is there or not */
1063 reg = superio_inb(IT87_SIO_GPIO5_REG);
1064 if (reg & (1 << 1))
1065 sio_data->skip_pwm |= (1 << 1);
1066 if (reg & (1 << 2))
1067 sio_data->skip_fan |= (1 << 1);
1069 if ((sio_data->type == it8718 || sio_data->type == it8720)
1070 && !(sio_data->skip_vid))
1071 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1073 reg = superio_inb(IT87_SIO_PINX2_REG);
1075 * The IT8720F has no VIN7 pin, so VCCH should always be
1076 * routed internally to VIN7 with an internal divider.
1077 * Curiously, there still is a configuration bit to control
1078 * this, which means it can be set incorrectly. And even
1079 * more curiously, many boards out there are improperly
1080 * configured, even though the IT8720F datasheet claims
1081 * that the internal routing of VCCH to VIN7 is the default
1082 * setting. So we force the internal routing in this case.
1084 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1085 reg |= (1 << 1);
1086 superio_outb(IT87_SIO_PINX2_REG, reg);
1087 pr_notice("it87: Routing internal VCCH to in7\n");
1089 if (reg & (1 << 0))
1090 pr_info("it87: in3 is VCC (+5V)\n");
1091 if (reg & (1 << 1))
1092 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1095 /* Disable specific features based on DMI strings */
1096 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1097 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1098 if (board_vendor && board_name) {
1099 if (strcmp(board_vendor, "nVIDIA") == 0
1100 && strcmp(board_name, "FN68PT") == 0) {
1101 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1102 connected to a fan, but to something else. One user
1103 has reported instant system power-off when changing
1104 the PWM2 duty cycle, so we disable it.
1105 I use the board name string as the trigger in case
1106 the same board is ever used in other systems. */
1107 pr_info("it87: Disabling pwm2 due to "
1108 "hardware constraints\n");
1109 sio_data->skip_pwm = (1 << 1);
1113 exit:
1114 superio_exit();
1115 return err;
1118 static int __devinit it87_probe(struct platform_device *pdev)
1120 struct it87_data *data;
1121 struct resource *res;
1122 struct device *dev = &pdev->dev;
1123 struct it87_sio_data *sio_data = dev->platform_data;
1124 int err = 0;
1125 int enable_pwm_interface;
1126 static const char *names[] = {
1127 "it87",
1128 "it8712",
1129 "it8716",
1130 "it8718",
1131 "it8720",
1134 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1135 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1136 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1137 (unsigned long)res->start,
1138 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1139 err = -EBUSY;
1140 goto ERROR0;
1143 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
1144 err = -ENOMEM;
1145 goto ERROR1;
1148 data->addr = res->start;
1149 data->type = sio_data->type;
1150 data->revision = sio_data->revision;
1151 data->name = names[sio_data->type];
1153 /* Now, we do the remaining detection. */
1154 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1155 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1156 err = -ENODEV;
1157 goto ERROR2;
1160 platform_set_drvdata(pdev, data);
1162 mutex_init(&data->update_lock);
1164 /* Check PWM configuration */
1165 enable_pwm_interface = it87_check_pwm(dev);
1167 /* Initialize the IT87 chip */
1168 it87_init_device(pdev);
1170 /* Register sysfs hooks */
1171 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1172 goto ERROR2;
1174 /* Do not create fan files for disabled fans */
1175 if (has_16bit_fans(data)) {
1176 /* 16-bit tachometers */
1177 if (data->has_fan & (1 << 0)) {
1178 if ((err = device_create_file(dev,
1179 &sensor_dev_attr_fan1_input16.dev_attr))
1180 || (err = device_create_file(dev,
1181 &sensor_dev_attr_fan1_min16.dev_attr))
1182 || (err = device_create_file(dev,
1183 &sensor_dev_attr_fan1_alarm.dev_attr)))
1184 goto ERROR4;
1186 if (data->has_fan & (1 << 1)) {
1187 if ((err = device_create_file(dev,
1188 &sensor_dev_attr_fan2_input16.dev_attr))
1189 || (err = device_create_file(dev,
1190 &sensor_dev_attr_fan2_min16.dev_attr))
1191 || (err = device_create_file(dev,
1192 &sensor_dev_attr_fan2_alarm.dev_attr)))
1193 goto ERROR4;
1195 if (data->has_fan & (1 << 2)) {
1196 if ((err = device_create_file(dev,
1197 &sensor_dev_attr_fan3_input16.dev_attr))
1198 || (err = device_create_file(dev,
1199 &sensor_dev_attr_fan3_min16.dev_attr))
1200 || (err = device_create_file(dev,
1201 &sensor_dev_attr_fan3_alarm.dev_attr)))
1202 goto ERROR4;
1204 if (data->has_fan & (1 << 3)) {
1205 if ((err = device_create_file(dev,
1206 &sensor_dev_attr_fan4_input16.dev_attr))
1207 || (err = device_create_file(dev,
1208 &sensor_dev_attr_fan4_min16.dev_attr))
1209 || (err = device_create_file(dev,
1210 &sensor_dev_attr_fan4_alarm.dev_attr)))
1211 goto ERROR4;
1213 if (data->has_fan & (1 << 4)) {
1214 if ((err = device_create_file(dev,
1215 &sensor_dev_attr_fan5_input16.dev_attr))
1216 || (err = device_create_file(dev,
1217 &sensor_dev_attr_fan5_min16.dev_attr))
1218 || (err = device_create_file(dev,
1219 &sensor_dev_attr_fan5_alarm.dev_attr)))
1220 goto ERROR4;
1222 } else {
1223 /* 8-bit tachometers with clock divider */
1224 if (data->has_fan & (1 << 0)) {
1225 if ((err = device_create_file(dev,
1226 &sensor_dev_attr_fan1_input.dev_attr))
1227 || (err = device_create_file(dev,
1228 &sensor_dev_attr_fan1_min.dev_attr))
1229 || (err = device_create_file(dev,
1230 &sensor_dev_attr_fan1_div.dev_attr))
1231 || (err = device_create_file(dev,
1232 &sensor_dev_attr_fan1_alarm.dev_attr)))
1233 goto ERROR4;
1235 if (data->has_fan & (1 << 1)) {
1236 if ((err = device_create_file(dev,
1237 &sensor_dev_attr_fan2_input.dev_attr))
1238 || (err = device_create_file(dev,
1239 &sensor_dev_attr_fan2_min.dev_attr))
1240 || (err = device_create_file(dev,
1241 &sensor_dev_attr_fan2_div.dev_attr))
1242 || (err = device_create_file(dev,
1243 &sensor_dev_attr_fan2_alarm.dev_attr)))
1244 goto ERROR4;
1246 if (data->has_fan & (1 << 2)) {
1247 if ((err = device_create_file(dev,
1248 &sensor_dev_attr_fan3_input.dev_attr))
1249 || (err = device_create_file(dev,
1250 &sensor_dev_attr_fan3_min.dev_attr))
1251 || (err = device_create_file(dev,
1252 &sensor_dev_attr_fan3_div.dev_attr))
1253 || (err = device_create_file(dev,
1254 &sensor_dev_attr_fan3_alarm.dev_attr)))
1255 goto ERROR4;
1259 if (enable_pwm_interface) {
1260 if (!(sio_data->skip_pwm & (1 << 0))) {
1261 if ((err = device_create_file(dev,
1262 &sensor_dev_attr_pwm1_enable.dev_attr))
1263 || (err = device_create_file(dev,
1264 &sensor_dev_attr_pwm1.dev_attr))
1265 || (err = device_create_file(dev,
1266 &dev_attr_pwm1_freq)))
1267 goto ERROR4;
1269 if (!(sio_data->skip_pwm & (1 << 1))) {
1270 if ((err = device_create_file(dev,
1271 &sensor_dev_attr_pwm2_enable.dev_attr))
1272 || (err = device_create_file(dev,
1273 &sensor_dev_attr_pwm2.dev_attr))
1274 || (err = device_create_file(dev,
1275 &dev_attr_pwm2_freq)))
1276 goto ERROR4;
1278 if (!(sio_data->skip_pwm & (1 << 2))) {
1279 if ((err = device_create_file(dev,
1280 &sensor_dev_attr_pwm3_enable.dev_attr))
1281 || (err = device_create_file(dev,
1282 &sensor_dev_attr_pwm3.dev_attr))
1283 || (err = device_create_file(dev,
1284 &dev_attr_pwm3_freq)))
1285 goto ERROR4;
1289 if (!sio_data->skip_vid) {
1290 data->vrm = vid_which_vrm();
1291 /* VID reading from Super-I/O config space if available */
1292 data->vid = sio_data->vid_value;
1293 if ((err = device_create_file(dev,
1294 &dev_attr_vrm))
1295 || (err = device_create_file(dev,
1296 &dev_attr_cpu0_vid)))
1297 goto ERROR4;
1300 data->hwmon_dev = hwmon_device_register(dev);
1301 if (IS_ERR(data->hwmon_dev)) {
1302 err = PTR_ERR(data->hwmon_dev);
1303 goto ERROR4;
1306 return 0;
1308 ERROR4:
1309 sysfs_remove_group(&dev->kobj, &it87_group);
1310 sysfs_remove_group(&dev->kobj, &it87_group_opt);
1311 ERROR2:
1312 platform_set_drvdata(pdev, NULL);
1313 kfree(data);
1314 ERROR1:
1315 release_region(res->start, IT87_EC_EXTENT);
1316 ERROR0:
1317 return err;
1320 static int __devexit it87_remove(struct platform_device *pdev)
1322 struct it87_data *data = platform_get_drvdata(pdev);
1324 hwmon_device_unregister(data->hwmon_dev);
1325 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1326 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
1328 release_region(data->addr, IT87_EC_EXTENT);
1329 platform_set_drvdata(pdev, NULL);
1330 kfree(data);
1332 return 0;
1335 /* Must be called with data->update_lock held, except during initialization.
1336 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1337 would slow down the IT87 access and should not be necessary. */
1338 static int it87_read_value(struct it87_data *data, u8 reg)
1340 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1341 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
1344 /* Must be called with data->update_lock held, except during initialization.
1345 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1346 would slow down the IT87 access and should not be necessary. */
1347 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
1349 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1350 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
1353 /* Return 1 if and only if the PWM interface is safe to use */
1354 static int __devinit it87_check_pwm(struct device *dev)
1356 struct it87_data *data = dev_get_drvdata(dev);
1357 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1358 * and polarity set to active low is sign that this is the case so we
1359 * disable pwm control to protect the user. */
1360 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1361 if ((tmp & 0x87) == 0) {
1362 if (fix_pwm_polarity) {
1363 /* The user asks us to attempt a chip reconfiguration.
1364 * This means switching to active high polarity and
1365 * inverting all fan speed values. */
1366 int i;
1367 u8 pwm[3];
1369 for (i = 0; i < 3; i++)
1370 pwm[i] = it87_read_value(data,
1371 IT87_REG_PWM(i));
1373 /* If any fan is in automatic pwm mode, the polarity
1374 * might be correct, as suspicious as it seems, so we
1375 * better don't change anything (but still disable the
1376 * PWM interface). */
1377 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1378 dev_info(dev, "Reconfiguring PWM to "
1379 "active high polarity\n");
1380 it87_write_value(data, IT87_REG_FAN_CTL,
1381 tmp | 0x87);
1382 for (i = 0; i < 3; i++)
1383 it87_write_value(data,
1384 IT87_REG_PWM(i),
1385 0x7f & ~pwm[i]);
1386 return 1;
1389 dev_info(dev, "PWM configuration is "
1390 "too broken to be fixed\n");
1393 dev_info(dev, "Detected broken BIOS "
1394 "defaults, disabling PWM interface\n");
1395 return 0;
1396 } else if (fix_pwm_polarity) {
1397 dev_info(dev, "PWM configuration looks "
1398 "sane, won't touch\n");
1401 return 1;
1404 /* Called when we have found a new IT87. */
1405 static void __devinit it87_init_device(struct platform_device *pdev)
1407 struct it87_sio_data *sio_data = pdev->dev.platform_data;
1408 struct it87_data *data = platform_get_drvdata(pdev);
1409 int tmp, i;
1410 u8 mask;
1412 /* initialize to sane defaults:
1413 * - if the chip is in manual pwm mode, this will be overwritten with
1414 * the actual settings on the chip (so in this case, initialization
1415 * is not needed)
1416 * - if in automatic or on/off mode, we could switch to manual mode,
1417 * read the registers and set manual_pwm_ctl accordingly, but currently
1418 * this is not implemented, so we initialize to something sane */
1419 for (i = 0; i < 3; i++) {
1420 data->manual_pwm_ctl[i] = 0xff;
1423 /* Some chips seem to have default value 0xff for all limit
1424 * registers. For low voltage limits it makes no sense and triggers
1425 * alarms, so change to 0 instead. For high temperature limits, it
1426 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1427 * but is still confusing, so change to 127 degrees C. */
1428 for (i = 0; i < 8; i++) {
1429 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
1430 if (tmp == 0xff)
1431 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
1433 for (i = 0; i < 3; i++) {
1434 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1435 if (tmp == 0xff)
1436 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
1439 /* Check if temperature channels are reset manually or by some reason */
1440 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1441 if ((tmp & 0x3f) == 0) {
1442 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1443 tmp = (tmp & 0xc0) | 0x2a;
1444 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
1446 data->sensor = tmp;
1448 /* Check if voltage monitors are reset manually or by some reason */
1449 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
1450 if ((tmp & 0xff) == 0) {
1451 /* Enable all voltage monitors */
1452 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
1455 /* Check if tachometers are reset manually or by some reason */
1456 mask = 0x70 & ~(sio_data->skip_fan << 4);
1457 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
1458 if ((data->fan_main_ctrl & mask) == 0) {
1459 /* Enable all fan tachometers */
1460 data->fan_main_ctrl |= mask;
1461 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1463 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1465 /* Set tachometers to 16-bit mode if needed */
1466 if (has_16bit_fans(data)) {
1467 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
1468 if (~tmp & 0x07 & data->has_fan) {
1469 dev_dbg(&pdev->dev,
1470 "Setting fan1-3 to 16-bit mode\n");
1471 it87_write_value(data, IT87_REG_FAN_16BIT,
1472 tmp | 0x07);
1474 /* IT8705F only supports three fans. */
1475 if (data->type != it87) {
1476 if (tmp & (1 << 4))
1477 data->has_fan |= (1 << 3); /* fan4 enabled */
1478 if (tmp & (1 << 5))
1479 data->has_fan |= (1 << 4); /* fan5 enabled */
1483 /* Fan input pins may be used for alternative functions */
1484 data->has_fan &= ~sio_data->skip_fan;
1486 /* Set current fan mode registers and the default settings for the
1487 * other mode registers */
1488 for (i = 0; i < 3; i++) {
1489 if (data->fan_main_ctrl & (1 << i)) {
1490 /* pwm mode */
1491 tmp = it87_read_value(data, IT87_REG_PWM(i));
1492 if (tmp & 0x80) {
1493 /* automatic pwm - not yet implemented, but
1494 * leave the settings made by the BIOS alone
1495 * until a change is requested via the sysfs
1496 * interface */
1497 } else {
1498 /* manual pwm */
1499 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1504 /* Start monitoring */
1505 it87_write_value(data, IT87_REG_CONFIG,
1506 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1507 | (update_vbat ? 0x41 : 0x01));
1510 static struct it87_data *it87_update_device(struct device *dev)
1512 struct it87_data *data = dev_get_drvdata(dev);
1513 int i;
1515 mutex_lock(&data->update_lock);
1517 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1518 || !data->valid) {
1520 if (update_vbat) {
1521 /* Cleared after each update, so reenable. Value
1522 returned by this read will be previous value */
1523 it87_write_value(data, IT87_REG_CONFIG,
1524 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1526 for (i = 0; i <= 7; i++) {
1527 data->in[i] =
1528 it87_read_value(data, IT87_REG_VIN(i));
1529 data->in_min[i] =
1530 it87_read_value(data, IT87_REG_VIN_MIN(i));
1531 data->in_max[i] =
1532 it87_read_value(data, IT87_REG_VIN_MAX(i));
1534 /* in8 (battery) has no limit registers */
1535 data->in[8] =
1536 it87_read_value(data, IT87_REG_VIN(8));
1538 for (i = 0; i < 5; i++) {
1539 /* Skip disabled fans */
1540 if (!(data->has_fan & (1 << i)))
1541 continue;
1543 data->fan_min[i] =
1544 it87_read_value(data, IT87_REG_FAN_MIN[i]);
1545 data->fan[i] = it87_read_value(data,
1546 IT87_REG_FAN[i]);
1547 /* Add high byte if in 16-bit mode */
1548 if (has_16bit_fans(data)) {
1549 data->fan[i] |= it87_read_value(data,
1550 IT87_REG_FANX[i]) << 8;
1551 data->fan_min[i] |= it87_read_value(data,
1552 IT87_REG_FANX_MIN[i]) << 8;
1555 for (i = 0; i < 3; i++) {
1556 data->temp[i] =
1557 it87_read_value(data, IT87_REG_TEMP(i));
1558 data->temp_high[i] =
1559 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1560 data->temp_low[i] =
1561 it87_read_value(data, IT87_REG_TEMP_LOW(i));
1564 /* Newer chips don't have clock dividers */
1565 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1566 i = it87_read_value(data, IT87_REG_FAN_DIV);
1567 data->fan_div[0] = i & 0x07;
1568 data->fan_div[1] = (i >> 3) & 0x07;
1569 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1572 data->alarms =
1573 it87_read_value(data, IT87_REG_ALARM1) |
1574 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1575 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1576 data->fan_main_ctrl = it87_read_value(data,
1577 IT87_REG_FAN_MAIN_CTRL);
1578 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
1580 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1581 /* The 8705 does not have VID capability.
1582 The 8718 and the 8720 don't use IT87_REG_VID for the
1583 same purpose. */
1584 if (data->type == it8712 || data->type == it8716) {
1585 data->vid = it87_read_value(data, IT87_REG_VID);
1586 /* The older IT8712F revisions had only 5 VID pins,
1587 but we assume it is always safe to read 6 bits. */
1588 data->vid &= 0x3f;
1590 data->last_updated = jiffies;
1591 data->valid = 1;
1594 mutex_unlock(&data->update_lock);
1596 return data;
1599 static int __init it87_device_add(unsigned short address,
1600 const struct it87_sio_data *sio_data)
1602 struct resource res = {
1603 .start = address + IT87_EC_OFFSET,
1604 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
1605 .name = DRVNAME,
1606 .flags = IORESOURCE_IO,
1608 int err;
1610 err = acpi_check_resource_conflict(&res);
1611 if (err)
1612 goto exit;
1614 pdev = platform_device_alloc(DRVNAME, address);
1615 if (!pdev) {
1616 err = -ENOMEM;
1617 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1618 goto exit;
1621 err = platform_device_add_resources(pdev, &res, 1);
1622 if (err) {
1623 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1624 "(%d)\n", err);
1625 goto exit_device_put;
1628 err = platform_device_add_data(pdev, sio_data,
1629 sizeof(struct it87_sio_data));
1630 if (err) {
1631 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1632 goto exit_device_put;
1635 err = platform_device_add(pdev);
1636 if (err) {
1637 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1638 err);
1639 goto exit_device_put;
1642 return 0;
1644 exit_device_put:
1645 platform_device_put(pdev);
1646 exit:
1647 return err;
1650 static int __init sm_it87_init(void)
1652 int err;
1653 unsigned short isa_address=0;
1654 struct it87_sio_data sio_data;
1656 memset(&sio_data, 0, sizeof(struct it87_sio_data));
1657 err = it87_find(&isa_address, &sio_data);
1658 if (err)
1659 return err;
1660 err = platform_driver_register(&it87_driver);
1661 if (err)
1662 return err;
1664 err = it87_device_add(isa_address, &sio_data);
1665 if (err){
1666 platform_driver_unregister(&it87_driver);
1667 return err;
1670 return 0;
1673 static void __exit sm_it87_exit(void)
1675 platform_device_unregister(pdev);
1676 platform_driver_unregister(&it87_driver);
1680 MODULE_AUTHOR("Chris Gauthron, "
1681 "Jean Delvare <khali@linux-fr.org>");
1682 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
1683 module_param(update_vbat, bool, 0);
1684 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1685 module_param(fix_pwm_polarity, bool, 0);
1686 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1687 MODULE_LICENSE("GPL");
1689 module_init(sm_it87_init);
1690 module_exit(sm_it87_exit);