Linux 2.6.32.7
[linux/fpc-iii.git] / drivers / hwmon / it87.c
bloba3749cb0f181ec623a4d628ea615c66a154a6c5e
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_PINX2_REG 0x2c /* Pin selection */
128 #define IT87_SIO_VID_REG 0xfc /* VID value */
130 /* Update battery voltage after every reading if true */
131 static int update_vbat;
133 /* Not all BIOSes properly configure the PWM registers */
134 static int fix_pwm_polarity;
136 /* Many IT87 constants specified below */
138 /* Length of ISA address segment */
139 #define IT87_EXTENT 8
141 /* Length of ISA address segment for Environmental Controller */
142 #define IT87_EC_EXTENT 2
144 /* Offset of EC registers from ISA base address */
145 #define IT87_EC_OFFSET 5
147 /* Where are the ISA address/data registers relative to the EC base address */
148 #define IT87_ADDR_REG_OFFSET 0
149 #define IT87_DATA_REG_OFFSET 1
151 /*----- The IT87 registers -----*/
153 #define IT87_REG_CONFIG 0x00
155 #define IT87_REG_ALARM1 0x01
156 #define IT87_REG_ALARM2 0x02
157 #define IT87_REG_ALARM3 0x03
159 /* The IT8718F and IT8720F have the VID value in a different register, in
160 Super-I/O configuration space. */
161 #define IT87_REG_VID 0x0a
162 /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
163 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
164 mode. */
165 #define IT87_REG_FAN_DIV 0x0b
166 #define IT87_REG_FAN_16BIT 0x0c
168 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
170 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
171 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
172 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
173 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
174 #define IT87_REG_FAN_MAIN_CTRL 0x13
175 #define IT87_REG_FAN_CTL 0x14
176 #define IT87_REG_PWM(nr) (0x15 + (nr))
178 #define IT87_REG_VIN(nr) (0x20 + (nr))
179 #define IT87_REG_TEMP(nr) (0x29 + (nr))
181 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
182 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
183 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
184 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
186 #define IT87_REG_VIN_ENABLE 0x50
187 #define IT87_REG_TEMP_ENABLE 0x51
189 #define IT87_REG_CHIPID 0x58
191 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
192 #define IN_FROM_REG(val) ((val) * 16)
194 static inline u8 FAN_TO_REG(long rpm, int div)
196 if (rpm == 0)
197 return 255;
198 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
199 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
200 254);
203 static inline u16 FAN16_TO_REG(long rpm)
205 if (rpm == 0)
206 return 0xffff;
207 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
210 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
211 /* The divider is fixed to 2 in 16-bit mode */
212 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
214 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
215 ((val)+500)/1000),-128,127))
216 #define TEMP_FROM_REG(val) ((val) * 1000)
218 #define PWM_TO_REG(val) ((val) >> 1)
219 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
221 static int DIV_TO_REG(int val)
223 int answer = 0;
224 while (answer < 7 && (val >>= 1))
225 answer++;
226 return answer;
228 #define DIV_FROM_REG(val) (1 << (val))
230 static const unsigned int pwm_freq[8] = {
231 48000000 / 128,
232 24000000 / 128,
233 12000000 / 128,
234 8000000 / 128,
235 6000000 / 128,
236 3000000 / 128,
237 1500000 / 128,
238 750000 / 128,
242 struct it87_sio_data {
243 enum chips type;
244 /* Values read from Super-I/O config space */
245 u8 revision;
246 u8 vid_value;
247 /* Values set based on DMI strings */
248 u8 skip_pwm;
251 /* For each registered chip, we need to keep some data in memory.
252 The structure is dynamically allocated. */
253 struct it87_data {
254 struct device *hwmon_dev;
255 enum chips type;
256 u8 revision;
258 unsigned short addr;
259 const char *name;
260 struct mutex update_lock;
261 char valid; /* !=0 if following fields are valid */
262 unsigned long last_updated; /* In jiffies */
264 u8 in[9]; /* Register value */
265 u8 in_max[8]; /* Register value */
266 u8 in_min[8]; /* Register value */
267 u8 has_fan; /* Bitfield, fans enabled */
268 u16 fan[5]; /* Register values, possibly combined */
269 u16 fan_min[5]; /* Register values, possibly combined */
270 s8 temp[3]; /* Register value */
271 s8 temp_high[3]; /* Register value */
272 s8 temp_low[3]; /* Register value */
273 u8 sensor; /* Register value */
274 u8 fan_div[3]; /* Register encoding, shifted right */
275 u8 vid; /* Register encoding, combined */
276 u8 vrm;
277 u32 alarms; /* Register encoding, combined */
278 u8 fan_main_ctrl; /* Register value */
279 u8 fan_ctl; /* Register value */
280 u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
283 static inline int has_16bit_fans(const struct it87_data *data)
285 /* IT8705F Datasheet 0.4.1, 3h == Version G.
286 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
287 These are the first revisions with 16bit tachometer support. */
288 return (data->type == it87 && data->revision >= 0x03)
289 || (data->type == it8712 && data->revision >= 0x08)
290 || data->type == it8716
291 || data->type == it8718
292 || data->type == it8720;
295 static int it87_probe(struct platform_device *pdev);
296 static int __devexit it87_remove(struct platform_device *pdev);
298 static int it87_read_value(struct it87_data *data, u8 reg);
299 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
300 static struct it87_data *it87_update_device(struct device *dev);
301 static int it87_check_pwm(struct device *dev);
302 static void it87_init_device(struct platform_device *pdev);
305 static struct platform_driver it87_driver = {
306 .driver = {
307 .owner = THIS_MODULE,
308 .name = DRVNAME,
310 .probe = it87_probe,
311 .remove = __devexit_p(it87_remove),
314 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
315 char *buf)
317 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
318 int nr = sensor_attr->index;
320 struct it87_data *data = it87_update_device(dev);
321 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
324 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
325 char *buf)
327 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
328 int nr = sensor_attr->index;
330 struct it87_data *data = it87_update_device(dev);
331 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
334 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
335 char *buf)
337 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
338 int nr = sensor_attr->index;
340 struct it87_data *data = it87_update_device(dev);
341 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
344 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
345 const char *buf, size_t count)
347 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
348 int nr = sensor_attr->index;
350 struct it87_data *data = dev_get_drvdata(dev);
351 unsigned long val = simple_strtoul(buf, NULL, 10);
353 mutex_lock(&data->update_lock);
354 data->in_min[nr] = IN_TO_REG(val);
355 it87_write_value(data, IT87_REG_VIN_MIN(nr),
356 data->in_min[nr]);
357 mutex_unlock(&data->update_lock);
358 return count;
360 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t count)
363 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
364 int nr = sensor_attr->index;
366 struct it87_data *data = dev_get_drvdata(dev);
367 unsigned long val = simple_strtoul(buf, NULL, 10);
369 mutex_lock(&data->update_lock);
370 data->in_max[nr] = IN_TO_REG(val);
371 it87_write_value(data, IT87_REG_VIN_MAX(nr),
372 data->in_max[nr]);
373 mutex_unlock(&data->update_lock);
374 return count;
377 #define show_in_offset(offset) \
378 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
379 show_in, NULL, offset);
381 #define limit_in_offset(offset) \
382 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
383 show_in_min, set_in_min, offset); \
384 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
385 show_in_max, set_in_max, offset);
387 show_in_offset(0);
388 limit_in_offset(0);
389 show_in_offset(1);
390 limit_in_offset(1);
391 show_in_offset(2);
392 limit_in_offset(2);
393 show_in_offset(3);
394 limit_in_offset(3);
395 show_in_offset(4);
396 limit_in_offset(4);
397 show_in_offset(5);
398 limit_in_offset(5);
399 show_in_offset(6);
400 limit_in_offset(6);
401 show_in_offset(7);
402 limit_in_offset(7);
403 show_in_offset(8);
405 /* 3 temperatures */
406 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
407 char *buf)
409 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
410 int nr = sensor_attr->index;
412 struct it87_data *data = it87_update_device(dev);
413 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
415 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
416 char *buf)
418 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
419 int nr = sensor_attr->index;
421 struct it87_data *data = it87_update_device(dev);
422 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
424 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
425 char *buf)
427 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
428 int nr = sensor_attr->index;
430 struct it87_data *data = it87_update_device(dev);
431 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
433 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
436 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
437 int nr = sensor_attr->index;
439 struct it87_data *data = dev_get_drvdata(dev);
440 int val = simple_strtol(buf, NULL, 10);
442 mutex_lock(&data->update_lock);
443 data->temp_high[nr] = TEMP_TO_REG(val);
444 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
445 mutex_unlock(&data->update_lock);
446 return count;
448 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
449 const char *buf, size_t count)
451 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
452 int nr = sensor_attr->index;
454 struct it87_data *data = dev_get_drvdata(dev);
455 int val = simple_strtol(buf, NULL, 10);
457 mutex_lock(&data->update_lock);
458 data->temp_low[nr] = TEMP_TO_REG(val);
459 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
460 mutex_unlock(&data->update_lock);
461 return count;
463 #define show_temp_offset(offset) \
464 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
465 show_temp, NULL, offset - 1); \
466 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
467 show_temp_max, set_temp_max, offset - 1); \
468 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
469 show_temp_min, set_temp_min, offset - 1);
471 show_temp_offset(1);
472 show_temp_offset(2);
473 show_temp_offset(3);
475 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
476 char *buf)
478 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
479 int nr = sensor_attr->index;
481 struct it87_data *data = it87_update_device(dev);
482 u8 reg = data->sensor; /* In case the value is updated while we use it */
484 if (reg & (1 << nr))
485 return sprintf(buf, "3\n"); /* thermal diode */
486 if (reg & (8 << nr))
487 return sprintf(buf, "4\n"); /* thermistor */
488 return sprintf(buf, "0\n"); /* disabled */
490 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
493 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
494 int nr = sensor_attr->index;
496 struct it87_data *data = dev_get_drvdata(dev);
497 int val = simple_strtol(buf, NULL, 10);
499 mutex_lock(&data->update_lock);
501 data->sensor &= ~(1 << nr);
502 data->sensor &= ~(8 << nr);
503 if (val == 2) { /* backwards compatibility */
504 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
505 "instead\n");
506 val = 4;
508 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
509 if (val == 3)
510 data->sensor |= 1 << nr;
511 else if (val == 4)
512 data->sensor |= 8 << nr;
513 else if (val != 0) {
514 mutex_unlock(&data->update_lock);
515 return -EINVAL;
517 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
518 mutex_unlock(&data->update_lock);
519 return count;
521 #define show_sensor_offset(offset) \
522 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
523 show_sensor, set_sensor, offset - 1);
525 show_sensor_offset(1);
526 show_sensor_offset(2);
527 show_sensor_offset(3);
529 /* 3 Fans */
530 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
531 char *buf)
533 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
534 int nr = sensor_attr->index;
536 struct it87_data *data = it87_update_device(dev);
537 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
538 DIV_FROM_REG(data->fan_div[nr])));
540 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
541 char *buf)
543 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
544 int nr = sensor_attr->index;
546 struct it87_data *data = it87_update_device(dev);
547 return sprintf(buf,"%d\n",
548 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
550 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
551 char *buf)
553 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
554 int nr = sensor_attr->index;
556 struct it87_data *data = it87_update_device(dev);
557 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
559 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
560 char *buf)
562 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
563 int nr = sensor_attr->index;
565 struct it87_data *data = it87_update_device(dev);
566 return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
568 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
569 char *buf)
571 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
572 int nr = sensor_attr->index;
574 struct it87_data *data = it87_update_device(dev);
575 return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
577 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
578 char *buf)
580 struct it87_data *data = it87_update_device(dev);
581 int index = (data->fan_ctl >> 4) & 0x07;
583 return sprintf(buf, "%u\n", pwm_freq[index]);
585 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
586 const char *buf, size_t count)
588 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
589 int nr = sensor_attr->index;
591 struct it87_data *data = dev_get_drvdata(dev);
592 int val = simple_strtol(buf, NULL, 10);
593 u8 reg;
595 mutex_lock(&data->update_lock);
596 reg = it87_read_value(data, IT87_REG_FAN_DIV);
597 switch (nr) {
598 case 0: data->fan_div[nr] = reg & 0x07; break;
599 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
600 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
603 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
604 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
605 mutex_unlock(&data->update_lock);
606 return count;
608 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
611 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
612 int nr = sensor_attr->index;
614 struct it87_data *data = dev_get_drvdata(dev);
615 unsigned long val = simple_strtoul(buf, NULL, 10);
616 int min;
617 u8 old;
619 mutex_lock(&data->update_lock);
620 old = it87_read_value(data, IT87_REG_FAN_DIV);
622 /* Save fan min limit */
623 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
625 switch (nr) {
626 case 0:
627 case 1:
628 data->fan_div[nr] = DIV_TO_REG(val);
629 break;
630 case 2:
631 if (val < 8)
632 data->fan_div[nr] = 1;
633 else
634 data->fan_div[nr] = 3;
636 val = old & 0x80;
637 val |= (data->fan_div[0] & 0x07);
638 val |= (data->fan_div[1] & 0x07) << 3;
639 if (data->fan_div[2] == 3)
640 val |= 0x1 << 6;
641 it87_write_value(data, IT87_REG_FAN_DIV, val);
643 /* Restore fan min limit */
644 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
645 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
647 mutex_unlock(&data->update_lock);
648 return count;
650 static ssize_t set_pwm_enable(struct device *dev,
651 struct device_attribute *attr, const char *buf, size_t count)
653 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
654 int nr = sensor_attr->index;
656 struct it87_data *data = dev_get_drvdata(dev);
657 int val = simple_strtol(buf, NULL, 10);
659 mutex_lock(&data->update_lock);
661 if (val == 0) {
662 int tmp;
663 /* make sure the fan is on when in on/off mode */
664 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
665 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
666 /* set on/off mode */
667 data->fan_main_ctrl &= ~(1 << nr);
668 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
669 } else if (val == 1) {
670 /* set SmartGuardian mode */
671 data->fan_main_ctrl |= (1 << nr);
672 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
673 /* set saved pwm value, clear FAN_CTLX PWM mode bit */
674 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
675 } else {
676 mutex_unlock(&data->update_lock);
677 return -EINVAL;
680 mutex_unlock(&data->update_lock);
681 return count;
683 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
684 const char *buf, size_t count)
686 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
687 int nr = sensor_attr->index;
689 struct it87_data *data = dev_get_drvdata(dev);
690 int val = simple_strtol(buf, NULL, 10);
692 if (val < 0 || val > 255)
693 return -EINVAL;
695 mutex_lock(&data->update_lock);
696 data->manual_pwm_ctl[nr] = val;
697 if (data->fan_main_ctrl & (1 << nr))
698 it87_write_value(data, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
699 mutex_unlock(&data->update_lock);
700 return count;
702 static ssize_t set_pwm_freq(struct device *dev,
703 struct device_attribute *attr, const char *buf, size_t count)
705 struct it87_data *data = dev_get_drvdata(dev);
706 unsigned long val = simple_strtoul(buf, NULL, 10);
707 int i;
709 /* Search for the nearest available frequency */
710 for (i = 0; i < 7; i++) {
711 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
712 break;
715 mutex_lock(&data->update_lock);
716 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
717 data->fan_ctl |= i << 4;
718 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
719 mutex_unlock(&data->update_lock);
721 return count;
724 #define show_fan_offset(offset) \
725 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
726 show_fan, NULL, offset - 1); \
727 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
728 show_fan_min, set_fan_min, offset - 1); \
729 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
730 show_fan_div, set_fan_div, offset - 1);
732 show_fan_offset(1);
733 show_fan_offset(2);
734 show_fan_offset(3);
736 #define show_pwm_offset(offset) \
737 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
738 show_pwm_enable, set_pwm_enable, offset - 1); \
739 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
740 show_pwm, set_pwm, offset - 1); \
741 static DEVICE_ATTR(pwm##offset##_freq, \
742 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
743 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
745 show_pwm_offset(1);
746 show_pwm_offset(2);
747 show_pwm_offset(3);
749 /* A different set of callbacks for 16-bit fans */
750 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
751 char *buf)
753 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
754 int nr = sensor_attr->index;
755 struct it87_data *data = it87_update_device(dev);
756 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
759 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
760 char *buf)
762 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
763 int nr = sensor_attr->index;
764 struct it87_data *data = it87_update_device(dev);
765 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
768 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
769 const char *buf, size_t count)
771 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
772 int nr = sensor_attr->index;
773 struct it87_data *data = dev_get_drvdata(dev);
774 int val = simple_strtol(buf, NULL, 10);
776 mutex_lock(&data->update_lock);
777 data->fan_min[nr] = FAN16_TO_REG(val);
778 it87_write_value(data, IT87_REG_FAN_MIN[nr],
779 data->fan_min[nr] & 0xff);
780 it87_write_value(data, IT87_REG_FANX_MIN[nr],
781 data->fan_min[nr] >> 8);
782 mutex_unlock(&data->update_lock);
783 return count;
786 /* We want to use the same sysfs file names as 8-bit fans, but we need
787 different variable names, so we have to use SENSOR_ATTR instead of
788 SENSOR_DEVICE_ATTR. */
789 #define show_fan16_offset(offset) \
790 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
791 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
792 show_fan16, NULL, offset - 1); \
793 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
794 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
795 show_fan16_min, set_fan16_min, offset - 1)
797 show_fan16_offset(1);
798 show_fan16_offset(2);
799 show_fan16_offset(3);
800 show_fan16_offset(4);
801 show_fan16_offset(5);
803 /* Alarms */
804 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
806 struct it87_data *data = it87_update_device(dev);
807 return sprintf(buf, "%u\n", data->alarms);
809 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
811 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
812 char *buf)
814 int bitnr = to_sensor_dev_attr(attr)->index;
815 struct it87_data *data = it87_update_device(dev);
816 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
818 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
819 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
820 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
821 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
822 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
823 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
824 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
825 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
826 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
827 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
828 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
829 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
830 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
831 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
832 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
833 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
835 static ssize_t
836 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
838 struct it87_data *data = dev_get_drvdata(dev);
839 return sprintf(buf, "%u\n", data->vrm);
841 static ssize_t
842 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
844 struct it87_data *data = dev_get_drvdata(dev);
845 u32 val;
847 val = simple_strtoul(buf, NULL, 10);
848 data->vrm = val;
850 return count;
852 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
854 static ssize_t
855 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
857 struct it87_data *data = it87_update_device(dev);
858 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
860 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
862 static ssize_t show_name(struct device *dev, struct device_attribute
863 *devattr, char *buf)
865 struct it87_data *data = dev_get_drvdata(dev);
866 return sprintf(buf, "%s\n", data->name);
868 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
870 static struct attribute *it87_attributes[] = {
871 &sensor_dev_attr_in0_input.dev_attr.attr,
872 &sensor_dev_attr_in1_input.dev_attr.attr,
873 &sensor_dev_attr_in2_input.dev_attr.attr,
874 &sensor_dev_attr_in3_input.dev_attr.attr,
875 &sensor_dev_attr_in4_input.dev_attr.attr,
876 &sensor_dev_attr_in5_input.dev_attr.attr,
877 &sensor_dev_attr_in6_input.dev_attr.attr,
878 &sensor_dev_attr_in7_input.dev_attr.attr,
879 &sensor_dev_attr_in8_input.dev_attr.attr,
880 &sensor_dev_attr_in0_min.dev_attr.attr,
881 &sensor_dev_attr_in1_min.dev_attr.attr,
882 &sensor_dev_attr_in2_min.dev_attr.attr,
883 &sensor_dev_attr_in3_min.dev_attr.attr,
884 &sensor_dev_attr_in4_min.dev_attr.attr,
885 &sensor_dev_attr_in5_min.dev_attr.attr,
886 &sensor_dev_attr_in6_min.dev_attr.attr,
887 &sensor_dev_attr_in7_min.dev_attr.attr,
888 &sensor_dev_attr_in0_max.dev_attr.attr,
889 &sensor_dev_attr_in1_max.dev_attr.attr,
890 &sensor_dev_attr_in2_max.dev_attr.attr,
891 &sensor_dev_attr_in3_max.dev_attr.attr,
892 &sensor_dev_attr_in4_max.dev_attr.attr,
893 &sensor_dev_attr_in5_max.dev_attr.attr,
894 &sensor_dev_attr_in6_max.dev_attr.attr,
895 &sensor_dev_attr_in7_max.dev_attr.attr,
896 &sensor_dev_attr_in0_alarm.dev_attr.attr,
897 &sensor_dev_attr_in1_alarm.dev_attr.attr,
898 &sensor_dev_attr_in2_alarm.dev_attr.attr,
899 &sensor_dev_attr_in3_alarm.dev_attr.attr,
900 &sensor_dev_attr_in4_alarm.dev_attr.attr,
901 &sensor_dev_attr_in5_alarm.dev_attr.attr,
902 &sensor_dev_attr_in6_alarm.dev_attr.attr,
903 &sensor_dev_attr_in7_alarm.dev_attr.attr,
905 &sensor_dev_attr_temp1_input.dev_attr.attr,
906 &sensor_dev_attr_temp2_input.dev_attr.attr,
907 &sensor_dev_attr_temp3_input.dev_attr.attr,
908 &sensor_dev_attr_temp1_max.dev_attr.attr,
909 &sensor_dev_attr_temp2_max.dev_attr.attr,
910 &sensor_dev_attr_temp3_max.dev_attr.attr,
911 &sensor_dev_attr_temp1_min.dev_attr.attr,
912 &sensor_dev_attr_temp2_min.dev_attr.attr,
913 &sensor_dev_attr_temp3_min.dev_attr.attr,
914 &sensor_dev_attr_temp1_type.dev_attr.attr,
915 &sensor_dev_attr_temp2_type.dev_attr.attr,
916 &sensor_dev_attr_temp3_type.dev_attr.attr,
917 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
918 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
919 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
921 &dev_attr_alarms.attr,
922 &dev_attr_name.attr,
923 NULL
926 static const struct attribute_group it87_group = {
927 .attrs = it87_attributes,
930 static struct attribute *it87_attributes_opt[] = {
931 &sensor_dev_attr_fan1_input16.dev_attr.attr,
932 &sensor_dev_attr_fan1_min16.dev_attr.attr,
933 &sensor_dev_attr_fan2_input16.dev_attr.attr,
934 &sensor_dev_attr_fan2_min16.dev_attr.attr,
935 &sensor_dev_attr_fan3_input16.dev_attr.attr,
936 &sensor_dev_attr_fan3_min16.dev_attr.attr,
937 &sensor_dev_attr_fan4_input16.dev_attr.attr,
938 &sensor_dev_attr_fan4_min16.dev_attr.attr,
939 &sensor_dev_attr_fan5_input16.dev_attr.attr,
940 &sensor_dev_attr_fan5_min16.dev_attr.attr,
942 &sensor_dev_attr_fan1_input.dev_attr.attr,
943 &sensor_dev_attr_fan1_min.dev_attr.attr,
944 &sensor_dev_attr_fan1_div.dev_attr.attr,
945 &sensor_dev_attr_fan2_input.dev_attr.attr,
946 &sensor_dev_attr_fan2_min.dev_attr.attr,
947 &sensor_dev_attr_fan2_div.dev_attr.attr,
948 &sensor_dev_attr_fan3_input.dev_attr.attr,
949 &sensor_dev_attr_fan3_min.dev_attr.attr,
950 &sensor_dev_attr_fan3_div.dev_attr.attr,
952 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
953 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
954 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
955 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
956 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
958 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
959 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
960 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
961 &sensor_dev_attr_pwm1.dev_attr.attr,
962 &sensor_dev_attr_pwm2.dev_attr.attr,
963 &sensor_dev_attr_pwm3.dev_attr.attr,
964 &dev_attr_pwm1_freq.attr,
965 &dev_attr_pwm2_freq.attr,
966 &dev_attr_pwm3_freq.attr,
968 &dev_attr_vrm.attr,
969 &dev_attr_cpu0_vid.attr,
970 NULL
973 static const struct attribute_group it87_group_opt = {
974 .attrs = it87_attributes_opt,
977 /* SuperIO detection - will change isa_address if a chip is found */
978 static int __init it87_find(unsigned short *address,
979 struct it87_sio_data *sio_data)
981 int err = -ENODEV;
982 u16 chip_type;
983 const char *board_vendor, *board_name;
985 superio_enter();
986 chip_type = force_id ? force_id : superio_inw(DEVID);
988 switch (chip_type) {
989 case IT8705F_DEVID:
990 sio_data->type = it87;
991 break;
992 case IT8712F_DEVID:
993 sio_data->type = it8712;
994 break;
995 case IT8716F_DEVID:
996 case IT8726F_DEVID:
997 sio_data->type = it8716;
998 break;
999 case IT8718F_DEVID:
1000 sio_data->type = it8718;
1001 break;
1002 case IT8720F_DEVID:
1003 sio_data->type = it8720;
1004 break;
1005 case 0xffff: /* No device at all */
1006 goto exit;
1007 default:
1008 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1009 chip_type);
1010 goto exit;
1013 superio_select(PME);
1014 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1015 pr_info("it87: Device not activated, skipping\n");
1016 goto exit;
1019 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1020 if (*address == 0) {
1021 pr_info("it87: Base address not set, skipping\n");
1022 goto exit;
1025 err = 0;
1026 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1027 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
1028 chip_type, *address, sio_data->revision);
1030 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1031 if (sio_data->type != it87) {
1032 int reg;
1034 superio_select(GPIO);
1035 if (sio_data->type == it8718 || sio_data->type == it8720)
1036 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1038 reg = superio_inb(IT87_SIO_PINX2_REG);
1039 if (reg & (1 << 0))
1040 pr_info("it87: in3 is VCC (+5V)\n");
1041 if (reg & (1 << 1))
1042 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1045 /* Disable specific features based on DMI strings */
1046 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1047 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1048 if (board_vendor && board_name) {
1049 if (strcmp(board_vendor, "nVIDIA") == 0
1050 && strcmp(board_name, "FN68PT") == 0) {
1051 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1052 connected to a fan, but to something else. One user
1053 has reported instant system power-off when changing
1054 the PWM2 duty cycle, so we disable it.
1055 I use the board name string as the trigger in case
1056 the same board is ever used in other systems. */
1057 pr_info("it87: Disabling pwm2 due to "
1058 "hardware constraints\n");
1059 sio_data->skip_pwm = (1 << 1);
1063 exit:
1064 superio_exit();
1065 return err;
1068 static int __devinit it87_probe(struct platform_device *pdev)
1070 struct it87_data *data;
1071 struct resource *res;
1072 struct device *dev = &pdev->dev;
1073 struct it87_sio_data *sio_data = dev->platform_data;
1074 int err = 0;
1075 int enable_pwm_interface;
1076 static const char *names[] = {
1077 "it87",
1078 "it8712",
1079 "it8716",
1080 "it8718",
1081 "it8720",
1084 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1085 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1086 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1087 (unsigned long)res->start,
1088 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1089 err = -EBUSY;
1090 goto ERROR0;
1093 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
1094 err = -ENOMEM;
1095 goto ERROR1;
1098 data->addr = res->start;
1099 data->type = sio_data->type;
1100 data->revision = sio_data->revision;
1101 data->name = names[sio_data->type];
1103 /* Now, we do the remaining detection. */
1104 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1105 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1106 err = -ENODEV;
1107 goto ERROR2;
1110 platform_set_drvdata(pdev, data);
1112 mutex_init(&data->update_lock);
1114 /* Check PWM configuration */
1115 enable_pwm_interface = it87_check_pwm(dev);
1117 /* Initialize the IT87 chip */
1118 it87_init_device(pdev);
1120 /* Register sysfs hooks */
1121 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1122 goto ERROR2;
1124 /* Do not create fan files for disabled fans */
1125 if (has_16bit_fans(data)) {
1126 /* 16-bit tachometers */
1127 if (data->has_fan & (1 << 0)) {
1128 if ((err = device_create_file(dev,
1129 &sensor_dev_attr_fan1_input16.dev_attr))
1130 || (err = device_create_file(dev,
1131 &sensor_dev_attr_fan1_min16.dev_attr))
1132 || (err = device_create_file(dev,
1133 &sensor_dev_attr_fan1_alarm.dev_attr)))
1134 goto ERROR4;
1136 if (data->has_fan & (1 << 1)) {
1137 if ((err = device_create_file(dev,
1138 &sensor_dev_attr_fan2_input16.dev_attr))
1139 || (err = device_create_file(dev,
1140 &sensor_dev_attr_fan2_min16.dev_attr))
1141 || (err = device_create_file(dev,
1142 &sensor_dev_attr_fan2_alarm.dev_attr)))
1143 goto ERROR4;
1145 if (data->has_fan & (1 << 2)) {
1146 if ((err = device_create_file(dev,
1147 &sensor_dev_attr_fan3_input16.dev_attr))
1148 || (err = device_create_file(dev,
1149 &sensor_dev_attr_fan3_min16.dev_attr))
1150 || (err = device_create_file(dev,
1151 &sensor_dev_attr_fan3_alarm.dev_attr)))
1152 goto ERROR4;
1154 if (data->has_fan & (1 << 3)) {
1155 if ((err = device_create_file(dev,
1156 &sensor_dev_attr_fan4_input16.dev_attr))
1157 || (err = device_create_file(dev,
1158 &sensor_dev_attr_fan4_min16.dev_attr))
1159 || (err = device_create_file(dev,
1160 &sensor_dev_attr_fan4_alarm.dev_attr)))
1161 goto ERROR4;
1163 if (data->has_fan & (1 << 4)) {
1164 if ((err = device_create_file(dev,
1165 &sensor_dev_attr_fan5_input16.dev_attr))
1166 || (err = device_create_file(dev,
1167 &sensor_dev_attr_fan5_min16.dev_attr))
1168 || (err = device_create_file(dev,
1169 &sensor_dev_attr_fan5_alarm.dev_attr)))
1170 goto ERROR4;
1172 } else {
1173 /* 8-bit tachometers with clock divider */
1174 if (data->has_fan & (1 << 0)) {
1175 if ((err = device_create_file(dev,
1176 &sensor_dev_attr_fan1_input.dev_attr))
1177 || (err = device_create_file(dev,
1178 &sensor_dev_attr_fan1_min.dev_attr))
1179 || (err = device_create_file(dev,
1180 &sensor_dev_attr_fan1_div.dev_attr))
1181 || (err = device_create_file(dev,
1182 &sensor_dev_attr_fan1_alarm.dev_attr)))
1183 goto ERROR4;
1185 if (data->has_fan & (1 << 1)) {
1186 if ((err = device_create_file(dev,
1187 &sensor_dev_attr_fan2_input.dev_attr))
1188 || (err = device_create_file(dev,
1189 &sensor_dev_attr_fan2_min.dev_attr))
1190 || (err = device_create_file(dev,
1191 &sensor_dev_attr_fan2_div.dev_attr))
1192 || (err = device_create_file(dev,
1193 &sensor_dev_attr_fan2_alarm.dev_attr)))
1194 goto ERROR4;
1196 if (data->has_fan & (1 << 2)) {
1197 if ((err = device_create_file(dev,
1198 &sensor_dev_attr_fan3_input.dev_attr))
1199 || (err = device_create_file(dev,
1200 &sensor_dev_attr_fan3_min.dev_attr))
1201 || (err = device_create_file(dev,
1202 &sensor_dev_attr_fan3_div.dev_attr))
1203 || (err = device_create_file(dev,
1204 &sensor_dev_attr_fan3_alarm.dev_attr)))
1205 goto ERROR4;
1209 if (enable_pwm_interface) {
1210 if (!(sio_data->skip_pwm & (1 << 0))) {
1211 if ((err = device_create_file(dev,
1212 &sensor_dev_attr_pwm1_enable.dev_attr))
1213 || (err = device_create_file(dev,
1214 &sensor_dev_attr_pwm1.dev_attr))
1215 || (err = device_create_file(dev,
1216 &dev_attr_pwm1_freq)))
1217 goto ERROR4;
1219 if (!(sio_data->skip_pwm & (1 << 1))) {
1220 if ((err = device_create_file(dev,
1221 &sensor_dev_attr_pwm2_enable.dev_attr))
1222 || (err = device_create_file(dev,
1223 &sensor_dev_attr_pwm2.dev_attr))
1224 || (err = device_create_file(dev,
1225 &dev_attr_pwm2_freq)))
1226 goto ERROR4;
1228 if (!(sio_data->skip_pwm & (1 << 2))) {
1229 if ((err = device_create_file(dev,
1230 &sensor_dev_attr_pwm3_enable.dev_attr))
1231 || (err = device_create_file(dev,
1232 &sensor_dev_attr_pwm3.dev_attr))
1233 || (err = device_create_file(dev,
1234 &dev_attr_pwm3_freq)))
1235 goto ERROR4;
1239 if (data->type == it8712 || data->type == it8716
1240 || data->type == it8718 || data->type == it8720) {
1241 data->vrm = vid_which_vrm();
1242 /* VID reading from Super-I/O config space if available */
1243 data->vid = sio_data->vid_value;
1244 if ((err = device_create_file(dev,
1245 &dev_attr_vrm))
1246 || (err = device_create_file(dev,
1247 &dev_attr_cpu0_vid)))
1248 goto ERROR4;
1251 data->hwmon_dev = hwmon_device_register(dev);
1252 if (IS_ERR(data->hwmon_dev)) {
1253 err = PTR_ERR(data->hwmon_dev);
1254 goto ERROR4;
1257 return 0;
1259 ERROR4:
1260 sysfs_remove_group(&dev->kobj, &it87_group);
1261 sysfs_remove_group(&dev->kobj, &it87_group_opt);
1262 ERROR2:
1263 platform_set_drvdata(pdev, NULL);
1264 kfree(data);
1265 ERROR1:
1266 release_region(res->start, IT87_EC_EXTENT);
1267 ERROR0:
1268 return err;
1271 static int __devexit it87_remove(struct platform_device *pdev)
1273 struct it87_data *data = platform_get_drvdata(pdev);
1275 hwmon_device_unregister(data->hwmon_dev);
1276 sysfs_remove_group(&pdev->dev.kobj, &it87_group);
1277 sysfs_remove_group(&pdev->dev.kobj, &it87_group_opt);
1279 release_region(data->addr, IT87_EC_EXTENT);
1280 platform_set_drvdata(pdev, NULL);
1281 kfree(data);
1283 return 0;
1286 /* Must be called with data->update_lock held, except during initialization.
1287 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1288 would slow down the IT87 access and should not be necessary. */
1289 static int it87_read_value(struct it87_data *data, u8 reg)
1291 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1292 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
1295 /* Must be called with data->update_lock held, except during initialization.
1296 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1297 would slow down the IT87 access and should not be necessary. */
1298 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
1300 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1301 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
1304 /* Return 1 if and only if the PWM interface is safe to use */
1305 static int __devinit it87_check_pwm(struct device *dev)
1307 struct it87_data *data = dev_get_drvdata(dev);
1308 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1309 * and polarity set to active low is sign that this is the case so we
1310 * disable pwm control to protect the user. */
1311 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1312 if ((tmp & 0x87) == 0) {
1313 if (fix_pwm_polarity) {
1314 /* The user asks us to attempt a chip reconfiguration.
1315 * This means switching to active high polarity and
1316 * inverting all fan speed values. */
1317 int i;
1318 u8 pwm[3];
1320 for (i = 0; i < 3; i++)
1321 pwm[i] = it87_read_value(data,
1322 IT87_REG_PWM(i));
1324 /* If any fan is in automatic pwm mode, the polarity
1325 * might be correct, as suspicious as it seems, so we
1326 * better don't change anything (but still disable the
1327 * PWM interface). */
1328 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1329 dev_info(dev, "Reconfiguring PWM to "
1330 "active high polarity\n");
1331 it87_write_value(data, IT87_REG_FAN_CTL,
1332 tmp | 0x87);
1333 for (i = 0; i < 3; i++)
1334 it87_write_value(data,
1335 IT87_REG_PWM(i),
1336 0x7f & ~pwm[i]);
1337 return 1;
1340 dev_info(dev, "PWM configuration is "
1341 "too broken to be fixed\n");
1344 dev_info(dev, "Detected broken BIOS "
1345 "defaults, disabling PWM interface\n");
1346 return 0;
1347 } else if (fix_pwm_polarity) {
1348 dev_info(dev, "PWM configuration looks "
1349 "sane, won't touch\n");
1352 return 1;
1355 /* Called when we have found a new IT87. */
1356 static void __devinit it87_init_device(struct platform_device *pdev)
1358 struct it87_data *data = platform_get_drvdata(pdev);
1359 int tmp, i;
1361 /* initialize to sane defaults:
1362 * - if the chip is in manual pwm mode, this will be overwritten with
1363 * the actual settings on the chip (so in this case, initialization
1364 * is not needed)
1365 * - if in automatic or on/off mode, we could switch to manual mode,
1366 * read the registers and set manual_pwm_ctl accordingly, but currently
1367 * this is not implemented, so we initialize to something sane */
1368 for (i = 0; i < 3; i++) {
1369 data->manual_pwm_ctl[i] = 0xff;
1372 /* Some chips seem to have default value 0xff for all limit
1373 * registers. For low voltage limits it makes no sense and triggers
1374 * alarms, so change to 0 instead. For high temperature limits, it
1375 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1376 * but is still confusing, so change to 127 degrees C. */
1377 for (i = 0; i < 8; i++) {
1378 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
1379 if (tmp == 0xff)
1380 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
1382 for (i = 0; i < 3; i++) {
1383 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1384 if (tmp == 0xff)
1385 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
1388 /* Check if temperature channels are reset manually or by some reason */
1389 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1390 if ((tmp & 0x3f) == 0) {
1391 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1392 tmp = (tmp & 0xc0) | 0x2a;
1393 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
1395 data->sensor = tmp;
1397 /* Check if voltage monitors are reset manually or by some reason */
1398 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
1399 if ((tmp & 0xff) == 0) {
1400 /* Enable all voltage monitors */
1401 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
1404 /* Check if tachometers are reset manually or by some reason */
1405 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
1406 if ((data->fan_main_ctrl & 0x70) == 0) {
1407 /* Enable all fan tachometers */
1408 data->fan_main_ctrl |= 0x70;
1409 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1411 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1413 /* Set tachometers to 16-bit mode if needed */
1414 if (has_16bit_fans(data)) {
1415 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
1416 if (~tmp & 0x07 & data->has_fan) {
1417 dev_dbg(&pdev->dev,
1418 "Setting fan1-3 to 16-bit mode\n");
1419 it87_write_value(data, IT87_REG_FAN_16BIT,
1420 tmp | 0x07);
1422 /* IT8705F only supports three fans. */
1423 if (data->type != it87) {
1424 if (tmp & (1 << 4))
1425 data->has_fan |= (1 << 3); /* fan4 enabled */
1426 if (tmp & (1 << 5))
1427 data->has_fan |= (1 << 4); /* fan5 enabled */
1431 /* Set current fan mode registers and the default settings for the
1432 * other mode registers */
1433 for (i = 0; i < 3; i++) {
1434 if (data->fan_main_ctrl & (1 << i)) {
1435 /* pwm mode */
1436 tmp = it87_read_value(data, IT87_REG_PWM(i));
1437 if (tmp & 0x80) {
1438 /* automatic pwm - not yet implemented, but
1439 * leave the settings made by the BIOS alone
1440 * until a change is requested via the sysfs
1441 * interface */
1442 } else {
1443 /* manual pwm */
1444 data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
1449 /* Start monitoring */
1450 it87_write_value(data, IT87_REG_CONFIG,
1451 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1452 | (update_vbat ? 0x41 : 0x01));
1455 static struct it87_data *it87_update_device(struct device *dev)
1457 struct it87_data *data = dev_get_drvdata(dev);
1458 int i;
1460 mutex_lock(&data->update_lock);
1462 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1463 || !data->valid) {
1465 if (update_vbat) {
1466 /* Cleared after each update, so reenable. Value
1467 returned by this read will be previous value */
1468 it87_write_value(data, IT87_REG_CONFIG,
1469 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1471 for (i = 0; i <= 7; i++) {
1472 data->in[i] =
1473 it87_read_value(data, IT87_REG_VIN(i));
1474 data->in_min[i] =
1475 it87_read_value(data, IT87_REG_VIN_MIN(i));
1476 data->in_max[i] =
1477 it87_read_value(data, IT87_REG_VIN_MAX(i));
1479 /* in8 (battery) has no limit registers */
1480 data->in[8] =
1481 it87_read_value(data, IT87_REG_VIN(8));
1483 for (i = 0; i < 5; i++) {
1484 /* Skip disabled fans */
1485 if (!(data->has_fan & (1 << i)))
1486 continue;
1488 data->fan_min[i] =
1489 it87_read_value(data, IT87_REG_FAN_MIN[i]);
1490 data->fan[i] = it87_read_value(data,
1491 IT87_REG_FAN[i]);
1492 /* Add high byte if in 16-bit mode */
1493 if (has_16bit_fans(data)) {
1494 data->fan[i] |= it87_read_value(data,
1495 IT87_REG_FANX[i]) << 8;
1496 data->fan_min[i] |= it87_read_value(data,
1497 IT87_REG_FANX_MIN[i]) << 8;
1500 for (i = 0; i < 3; i++) {
1501 data->temp[i] =
1502 it87_read_value(data, IT87_REG_TEMP(i));
1503 data->temp_high[i] =
1504 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1505 data->temp_low[i] =
1506 it87_read_value(data, IT87_REG_TEMP_LOW(i));
1509 /* Newer chips don't have clock dividers */
1510 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1511 i = it87_read_value(data, IT87_REG_FAN_DIV);
1512 data->fan_div[0] = i & 0x07;
1513 data->fan_div[1] = (i >> 3) & 0x07;
1514 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1517 data->alarms =
1518 it87_read_value(data, IT87_REG_ALARM1) |
1519 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1520 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1521 data->fan_main_ctrl = it87_read_value(data,
1522 IT87_REG_FAN_MAIN_CTRL);
1523 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
1525 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1526 /* The 8705 does not have VID capability.
1527 The 8718 and the 8720 don't use IT87_REG_VID for the
1528 same purpose. */
1529 if (data->type == it8712 || data->type == it8716) {
1530 data->vid = it87_read_value(data, IT87_REG_VID);
1531 /* The older IT8712F revisions had only 5 VID pins,
1532 but we assume it is always safe to read 6 bits. */
1533 data->vid &= 0x3f;
1535 data->last_updated = jiffies;
1536 data->valid = 1;
1539 mutex_unlock(&data->update_lock);
1541 return data;
1544 static int __init it87_device_add(unsigned short address,
1545 const struct it87_sio_data *sio_data)
1547 struct resource res = {
1548 .start = address + IT87_EC_OFFSET,
1549 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
1550 .name = DRVNAME,
1551 .flags = IORESOURCE_IO,
1553 int err;
1555 err = acpi_check_resource_conflict(&res);
1556 if (err)
1557 goto exit;
1559 pdev = platform_device_alloc(DRVNAME, address);
1560 if (!pdev) {
1561 err = -ENOMEM;
1562 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1563 goto exit;
1566 err = platform_device_add_resources(pdev, &res, 1);
1567 if (err) {
1568 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1569 "(%d)\n", err);
1570 goto exit_device_put;
1573 err = platform_device_add_data(pdev, sio_data,
1574 sizeof(struct it87_sio_data));
1575 if (err) {
1576 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1577 goto exit_device_put;
1580 err = platform_device_add(pdev);
1581 if (err) {
1582 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1583 err);
1584 goto exit_device_put;
1587 return 0;
1589 exit_device_put:
1590 platform_device_put(pdev);
1591 exit:
1592 return err;
1595 static int __init sm_it87_init(void)
1597 int err;
1598 unsigned short isa_address=0;
1599 struct it87_sio_data sio_data;
1601 memset(&sio_data, 0, sizeof(struct it87_sio_data));
1602 err = it87_find(&isa_address, &sio_data);
1603 if (err)
1604 return err;
1605 err = platform_driver_register(&it87_driver);
1606 if (err)
1607 return err;
1609 err = it87_device_add(isa_address, &sio_data);
1610 if (err){
1611 platform_driver_unregister(&it87_driver);
1612 return err;
1615 return 0;
1618 static void __exit sm_it87_exit(void)
1620 platform_device_unregister(pdev);
1621 platform_driver_unregister(&it87_driver);
1625 MODULE_AUTHOR("Chris Gauthron, "
1626 "Jean Delvare <khali@linux-fr.org>");
1627 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
1628 module_param(update_vbat, bool, 0);
1629 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1630 module_param(fix_pwm_polarity, bool, 0);
1631 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1632 MODULE_LICENSE("GPL");
1634 module_init(sm_it87_init);
1635 module_exit(sm_it87_exit);