hwmon: (it87) Create vid attributes by group
[linux/fpc-iii.git] / drivers / hwmon / it87.c
blob7a349e066fdc6b020c25c327df10cab9a0959e92
1 /*
2 it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring.
5 The IT8705F is an LPC-based Super I/O part that contains UARTs, a
6 parallel port, an IR port, a MIDI port, a floppy controller, etc., in
7 addition to an Environment Controller (Enhanced Hardware Monitor and
8 Fan Controller)
10 This driver supports only the Environment Controller in the IT8705F and
11 similar parts. The other devices are supported by different drivers.
13 Supports: IT8705F Super I/O chip w/LPC interface
14 IT8712F Super I/O chip w/LPC interface
15 IT8716F Super I/O chip w/LPC interface
16 IT8718F Super I/O chip w/LPC interface
17 IT8720F Super I/O chip w/LPC interface
18 IT8726F Super I/O chip w/LPC interface
19 Sis950 A clone of the IT8705F
21 Copyright (C) 2001 Chris Gauthron
22 Copyright (C) 2005-2007 Jean Delvare <khali@linux-fr.org>
24 This program is free software; you can redistribute it and/or modify
25 it under the terms of the GNU General Public License as published by
26 the Free Software Foundation; either version 2 of the License, or
27 (at your option) any later version.
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details.
34 You should have received a copy of the GNU General Public License
35 along with this program; if not, write to the Free Software
36 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/jiffies.h>
43 #include <linux/platform_device.h>
44 #include <linux/hwmon.h>
45 #include <linux/hwmon-sysfs.h>
46 #include <linux/hwmon-vid.h>
47 #include <linux/err.h>
48 #include <linux/mutex.h>
49 #include <linux/sysfs.h>
50 #include <linux/string.h>
51 #include <linux/dmi.h>
52 #include <linux/acpi.h>
53 #include <linux/io.h>
55 #define DRVNAME "it87"
57 enum chips { it87, it8712, it8716, it8718, it8720 };
59 static unsigned short force_id;
60 module_param(force_id, ushort, 0);
61 MODULE_PARM_DESC(force_id, "Override the detected device ID");
63 static struct platform_device *pdev;
65 #define REG 0x2e /* The register to read/write */
66 #define DEV 0x07 /* Register: Logical device select */
67 #define VAL 0x2f /* The value to read/write */
68 #define PME 0x04 /* The device with the fan registers in it */
70 /* The device with the IT8718F/IT8720F VID value in it */
71 #define GPIO 0x07
73 #define DEVID 0x20 /* Register: Device ID */
74 #define DEVREV 0x22 /* Register: Device Revision */
76 static inline int
77 superio_inb(int reg)
79 outb(reg, REG);
80 return inb(VAL);
83 static int superio_inw(int reg)
85 int val;
86 outb(reg++, REG);
87 val = inb(VAL) << 8;
88 outb(reg, REG);
89 val |= inb(VAL);
90 return val;
93 static inline void
94 superio_select(int ldn)
96 outb(DEV, REG);
97 outb(ldn, VAL);
100 static inline void
101 superio_enter(void)
103 outb(0x87, REG);
104 outb(0x01, REG);
105 outb(0x55, REG);
106 outb(0x55, REG);
109 static inline void
110 superio_exit(void)
112 outb(0x02, REG);
113 outb(0x02, VAL);
116 /* Logical device 4 registers */
117 #define IT8712F_DEVID 0x8712
118 #define IT8705F_DEVID 0x8705
119 #define IT8716F_DEVID 0x8716
120 #define IT8718F_DEVID 0x8718
121 #define IT8720F_DEVID 0x8720
122 #define IT8726F_DEVID 0x8726
123 #define IT87_ACT_REG 0x30
124 #define IT87_BASE_REG 0x60
126 /* Logical device 7 registers (IT8712F and later) */
127 #define IT87_SIO_GPIO3_REG 0x27
128 #define IT87_SIO_GPIO5_REG 0x29
129 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
130 #define IT87_SIO_VID_REG 0xfc /* VID value */
132 /* Update battery voltage after every reading if true */
133 static int update_vbat;
135 /* Not all BIOSes properly configure the PWM registers */
136 static int fix_pwm_polarity;
138 /* Many IT87 constants specified below */
140 /* Length of ISA address segment */
141 #define IT87_EXTENT 8
143 /* Length of ISA address segment for Environmental Controller */
144 #define IT87_EC_EXTENT 2
146 /* Offset of EC registers from ISA base address */
147 #define IT87_EC_OFFSET 5
149 /* Where are the ISA address/data registers relative to the EC base address */
150 #define IT87_ADDR_REG_OFFSET 0
151 #define IT87_DATA_REG_OFFSET 1
153 /*----- The IT87 registers -----*/
155 #define IT87_REG_CONFIG 0x00
157 #define IT87_REG_ALARM1 0x01
158 #define IT87_REG_ALARM2 0x02
159 #define IT87_REG_ALARM3 0x03
161 /* The IT8718F and IT8720F have the VID value in a different register, in
162 Super-I/O configuration space. */
163 #define IT87_REG_VID 0x0a
164 /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
165 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
166 mode. */
167 #define IT87_REG_FAN_DIV 0x0b
168 #define IT87_REG_FAN_16BIT 0x0c
170 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
172 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
173 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
174 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
175 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
176 #define IT87_REG_FAN_MAIN_CTRL 0x13
177 #define IT87_REG_FAN_CTL 0x14
178 #define IT87_REG_PWM(nr) (0x15 + (nr))
180 #define IT87_REG_VIN(nr) (0x20 + (nr))
181 #define IT87_REG_TEMP(nr) (0x29 + (nr))
183 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
184 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
185 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
186 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
188 #define IT87_REG_VIN_ENABLE 0x50
189 #define IT87_REG_TEMP_ENABLE 0x51
191 #define IT87_REG_CHIPID 0x58
193 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
194 #define IN_FROM_REG(val) ((val) * 16)
196 static inline u8 FAN_TO_REG(long rpm, int div)
198 if (rpm == 0)
199 return 255;
200 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
201 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
202 254);
205 static inline u16 FAN16_TO_REG(long rpm)
207 if (rpm == 0)
208 return 0xffff;
209 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
212 #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
213 /* The divider is fixed to 2 in 16-bit mode */
214 #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
216 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
217 ((val)+500)/1000),-128,127))
218 #define TEMP_FROM_REG(val) ((val) * 1000)
220 #define PWM_TO_REG(val) ((val) >> 1)
221 #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
223 static int DIV_TO_REG(int val)
225 int answer = 0;
226 while (answer < 7 && (val >>= 1))
227 answer++;
228 return answer;
230 #define DIV_FROM_REG(val) (1 << (val))
232 static const unsigned int pwm_freq[8] = {
233 48000000 / 128,
234 24000000 / 128,
235 12000000 / 128,
236 8000000 / 128,
237 6000000 / 128,
238 3000000 / 128,
239 1500000 / 128,
240 750000 / 128,
244 struct it87_sio_data {
245 enum chips type;
246 /* Values read from Super-I/O config space */
247 u8 revision;
248 u8 vid_value;
249 /* Features skipped based on config or DMI */
250 u8 skip_vid;
251 u8 skip_fan;
252 u8 skip_pwm;
255 /* For each registered chip, we need to keep some data in memory.
256 The structure is dynamically allocated. */
257 struct it87_data {
258 struct device *hwmon_dev;
259 enum chips type;
260 u8 revision;
262 unsigned short addr;
263 const char *name;
264 struct mutex update_lock;
265 char valid; /* !=0 if following fields are valid */
266 unsigned long last_updated; /* In jiffies */
268 u8 in[9]; /* Register value */
269 u8 in_max[8]; /* Register value */
270 u8 in_min[8]; /* Register value */
271 u8 has_fan; /* Bitfield, fans enabled */
272 u16 fan[5]; /* Register values, possibly combined */
273 u16 fan_min[5]; /* Register values, possibly combined */
274 s8 temp[3]; /* Register value */
275 s8 temp_high[3]; /* Register value */
276 s8 temp_low[3]; /* Register value */
277 u8 sensor; /* Register value */
278 u8 fan_div[3]; /* Register encoding, shifted right */
279 u8 vid; /* Register encoding, combined */
280 u8 vrm;
281 u32 alarms; /* Register encoding, combined */
282 u8 fan_main_ctrl; /* Register value */
283 u8 fan_ctl; /* Register value */
285 /* The following 3 arrays correspond to the same registers. The
286 * meaning of bits 6-0 depends on the value of bit 7, and we want
287 * to preserve settings on mode changes, so we have to track all
288 * values separately. */
289 u8 pwm_ctrl[3]; /* Register value */
290 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
291 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
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 */
542 static int pwm_mode(const struct it87_data *data, int nr)
544 int ctrl = data->fan_main_ctrl & (1 << nr);
546 if (ctrl == 0) /* Full speed */
547 return 0;
548 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
549 return 2;
550 else /* Manual mode */
551 return 1;
554 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
555 char *buf)
557 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
558 int nr = sensor_attr->index;
560 struct it87_data *data = it87_update_device(dev);
561 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
562 DIV_FROM_REG(data->fan_div[nr])));
564 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
565 char *buf)
567 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
568 int nr = sensor_attr->index;
570 struct it87_data *data = it87_update_device(dev);
571 return sprintf(buf,"%d\n",
572 FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
574 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
575 char *buf)
577 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
578 int nr = sensor_attr->index;
580 struct it87_data *data = it87_update_device(dev);
581 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
583 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
584 char *buf)
586 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
587 int nr = sensor_attr->index;
589 struct it87_data *data = it87_update_device(dev);
590 return sprintf(buf, "%d\n", pwm_mode(data, nr));
592 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
593 char *buf)
595 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
596 int nr = sensor_attr->index;
598 struct it87_data *data = it87_update_device(dev);
599 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr]));
601 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
602 char *buf)
604 struct it87_data *data = it87_update_device(dev);
605 int index = (data->fan_ctl >> 4) & 0x07;
607 return sprintf(buf, "%u\n", pwm_freq[index]);
609 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
610 const char *buf, size_t count)
612 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
613 int nr = sensor_attr->index;
615 struct it87_data *data = dev_get_drvdata(dev);
616 int val = simple_strtol(buf, NULL, 10);
617 u8 reg;
619 mutex_lock(&data->update_lock);
620 reg = it87_read_value(data, IT87_REG_FAN_DIV);
621 switch (nr) {
622 case 0: data->fan_div[nr] = reg & 0x07; break;
623 case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
624 case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
627 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
628 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
629 mutex_unlock(&data->update_lock);
630 return count;
632 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
633 const char *buf, size_t count)
635 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
636 int nr = sensor_attr->index;
638 struct it87_data *data = dev_get_drvdata(dev);
639 unsigned long val = simple_strtoul(buf, NULL, 10);
640 int min;
641 u8 old;
643 mutex_lock(&data->update_lock);
644 old = it87_read_value(data, IT87_REG_FAN_DIV);
646 /* Save fan min limit */
647 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
649 switch (nr) {
650 case 0:
651 case 1:
652 data->fan_div[nr] = DIV_TO_REG(val);
653 break;
654 case 2:
655 if (val < 8)
656 data->fan_div[nr] = 1;
657 else
658 data->fan_div[nr] = 3;
660 val = old & 0x80;
661 val |= (data->fan_div[0] & 0x07);
662 val |= (data->fan_div[1] & 0x07) << 3;
663 if (data->fan_div[2] == 3)
664 val |= 0x1 << 6;
665 it87_write_value(data, IT87_REG_FAN_DIV, val);
667 /* Restore fan min limit */
668 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
669 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
671 mutex_unlock(&data->update_lock);
672 return count;
674 static ssize_t set_pwm_enable(struct device *dev,
675 struct device_attribute *attr, const char *buf, size_t count)
677 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
678 int nr = sensor_attr->index;
680 struct it87_data *data = dev_get_drvdata(dev);
681 int val = simple_strtol(buf, NULL, 10);
683 if (val < 0 || val > 2)
684 return -EINVAL;
686 mutex_lock(&data->update_lock);
688 if (val == 0) {
689 int tmp;
690 /* make sure the fan is on when in on/off mode */
691 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
692 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
693 /* set on/off mode */
694 data->fan_main_ctrl &= ~(1 << nr);
695 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
696 } else {
697 if (val == 1) /* Manual mode */
698 data->pwm_ctrl[nr] = data->pwm_duty[nr];
699 else /* Automatic mode */
700 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
701 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
702 /* set SmartGuardian mode */
703 data->fan_main_ctrl |= (1 << nr);
704 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
707 mutex_unlock(&data->update_lock);
708 return count;
710 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
713 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
714 int nr = sensor_attr->index;
716 struct it87_data *data = dev_get_drvdata(dev);
717 int val = simple_strtol(buf, NULL, 10);
719 if (val < 0 || val > 255)
720 return -EINVAL;
722 mutex_lock(&data->update_lock);
723 data->pwm_duty[nr] = PWM_TO_REG(val);
724 /* If we are in manual mode, write the duty cycle immediately;
725 * otherwise, just store it for later use. */
726 if (!(data->pwm_ctrl[nr] & 0x80)) {
727 data->pwm_ctrl[nr] = data->pwm_duty[nr];
728 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
730 mutex_unlock(&data->update_lock);
731 return count;
733 static ssize_t set_pwm_freq(struct device *dev,
734 struct device_attribute *attr, const char *buf, size_t count)
736 struct it87_data *data = dev_get_drvdata(dev);
737 unsigned long val = simple_strtoul(buf, NULL, 10);
738 int i;
740 /* Search for the nearest available frequency */
741 for (i = 0; i < 7; i++) {
742 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
743 break;
746 mutex_lock(&data->update_lock);
747 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
748 data->fan_ctl |= i << 4;
749 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
750 mutex_unlock(&data->update_lock);
752 return count;
754 static ssize_t show_pwm_temp_map(struct device *dev,
755 struct device_attribute *attr, char *buf)
757 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
758 int nr = sensor_attr->index;
760 struct it87_data *data = it87_update_device(dev);
761 int map;
763 if (data->pwm_temp_map[nr] < 3)
764 map = 1 << data->pwm_temp_map[nr];
765 else
766 map = 0; /* Should never happen */
767 return sprintf(buf, "%d\n", map);
769 static ssize_t set_pwm_temp_map(struct device *dev,
770 struct device_attribute *attr, const char *buf, size_t count)
772 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
773 int nr = sensor_attr->index;
775 struct it87_data *data = dev_get_drvdata(dev);
776 long val;
777 u8 reg;
779 if (strict_strtol(buf, 10, &val) < 0)
780 return -EINVAL;
782 switch (val) {
783 case (1 << 0):
784 reg = 0x00;
785 break;
786 case (1 << 1):
787 reg = 0x01;
788 break;
789 case (1 << 2):
790 reg = 0x02;
791 break;
792 default:
793 return -EINVAL;
796 mutex_lock(&data->update_lock);
797 data->pwm_temp_map[nr] = reg;
798 /* If we are in automatic mode, write the temp mapping immediately;
799 * otherwise, just store it for later use. */
800 if (data->pwm_ctrl[nr] & 0x80) {
801 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
802 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
804 mutex_unlock(&data->update_lock);
805 return count;
808 #define show_fan_offset(offset) \
809 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
810 show_fan, NULL, offset - 1); \
811 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
812 show_fan_min, set_fan_min, offset - 1); \
813 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
814 show_fan_div, set_fan_div, offset - 1);
816 show_fan_offset(1);
817 show_fan_offset(2);
818 show_fan_offset(3);
820 #define show_pwm_offset(offset) \
821 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
822 show_pwm_enable, set_pwm_enable, offset - 1); \
823 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
824 show_pwm, set_pwm, offset - 1); \
825 static DEVICE_ATTR(pwm##offset##_freq, \
826 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
827 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
828 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
829 S_IRUGO, show_pwm_temp_map, set_pwm_temp_map, \
830 offset - 1);
832 show_pwm_offset(1);
833 show_pwm_offset(2);
834 show_pwm_offset(3);
836 /* A different set of callbacks for 16-bit fans */
837 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
838 char *buf)
840 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
841 int nr = sensor_attr->index;
842 struct it87_data *data = it87_update_device(dev);
843 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
846 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
847 char *buf)
849 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
850 int nr = sensor_attr->index;
851 struct it87_data *data = it87_update_device(dev);
852 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
855 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
856 const char *buf, size_t count)
858 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
859 int nr = sensor_attr->index;
860 struct it87_data *data = dev_get_drvdata(dev);
861 int val = simple_strtol(buf, NULL, 10);
863 mutex_lock(&data->update_lock);
864 data->fan_min[nr] = FAN16_TO_REG(val);
865 it87_write_value(data, IT87_REG_FAN_MIN[nr],
866 data->fan_min[nr] & 0xff);
867 it87_write_value(data, IT87_REG_FANX_MIN[nr],
868 data->fan_min[nr] >> 8);
869 mutex_unlock(&data->update_lock);
870 return count;
873 /* We want to use the same sysfs file names as 8-bit fans, but we need
874 different variable names, so we have to use SENSOR_ATTR instead of
875 SENSOR_DEVICE_ATTR. */
876 #define show_fan16_offset(offset) \
877 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
878 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
879 show_fan16, NULL, offset - 1); \
880 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
881 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
882 show_fan16_min, set_fan16_min, offset - 1)
884 show_fan16_offset(1);
885 show_fan16_offset(2);
886 show_fan16_offset(3);
887 show_fan16_offset(4);
888 show_fan16_offset(5);
890 /* Alarms */
891 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
893 struct it87_data *data = it87_update_device(dev);
894 return sprintf(buf, "%u\n", data->alarms);
896 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
898 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
899 char *buf)
901 int bitnr = to_sensor_dev_attr(attr)->index;
902 struct it87_data *data = it87_update_device(dev);
903 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
905 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
906 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
907 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
908 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
909 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
910 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
911 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
912 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
913 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
914 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
915 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
916 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
917 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
918 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
919 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
920 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
922 static ssize_t
923 show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
925 struct it87_data *data = dev_get_drvdata(dev);
926 return sprintf(buf, "%u\n", data->vrm);
928 static ssize_t
929 store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
931 struct it87_data *data = dev_get_drvdata(dev);
932 u32 val;
934 val = simple_strtoul(buf, NULL, 10);
935 data->vrm = val;
937 return count;
939 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
941 static ssize_t
942 show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
944 struct it87_data *data = it87_update_device(dev);
945 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
947 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
949 static ssize_t show_name(struct device *dev, struct device_attribute
950 *devattr, char *buf)
952 struct it87_data *data = dev_get_drvdata(dev);
953 return sprintf(buf, "%s\n", data->name);
955 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
957 static struct attribute *it87_attributes[] = {
958 &sensor_dev_attr_in0_input.dev_attr.attr,
959 &sensor_dev_attr_in1_input.dev_attr.attr,
960 &sensor_dev_attr_in2_input.dev_attr.attr,
961 &sensor_dev_attr_in3_input.dev_attr.attr,
962 &sensor_dev_attr_in4_input.dev_attr.attr,
963 &sensor_dev_attr_in5_input.dev_attr.attr,
964 &sensor_dev_attr_in6_input.dev_attr.attr,
965 &sensor_dev_attr_in7_input.dev_attr.attr,
966 &sensor_dev_attr_in8_input.dev_attr.attr,
967 &sensor_dev_attr_in0_min.dev_attr.attr,
968 &sensor_dev_attr_in1_min.dev_attr.attr,
969 &sensor_dev_attr_in2_min.dev_attr.attr,
970 &sensor_dev_attr_in3_min.dev_attr.attr,
971 &sensor_dev_attr_in4_min.dev_attr.attr,
972 &sensor_dev_attr_in5_min.dev_attr.attr,
973 &sensor_dev_attr_in6_min.dev_attr.attr,
974 &sensor_dev_attr_in7_min.dev_attr.attr,
975 &sensor_dev_attr_in0_max.dev_attr.attr,
976 &sensor_dev_attr_in1_max.dev_attr.attr,
977 &sensor_dev_attr_in2_max.dev_attr.attr,
978 &sensor_dev_attr_in3_max.dev_attr.attr,
979 &sensor_dev_attr_in4_max.dev_attr.attr,
980 &sensor_dev_attr_in5_max.dev_attr.attr,
981 &sensor_dev_attr_in6_max.dev_attr.attr,
982 &sensor_dev_attr_in7_max.dev_attr.attr,
983 &sensor_dev_attr_in0_alarm.dev_attr.attr,
984 &sensor_dev_attr_in1_alarm.dev_attr.attr,
985 &sensor_dev_attr_in2_alarm.dev_attr.attr,
986 &sensor_dev_attr_in3_alarm.dev_attr.attr,
987 &sensor_dev_attr_in4_alarm.dev_attr.attr,
988 &sensor_dev_attr_in5_alarm.dev_attr.attr,
989 &sensor_dev_attr_in6_alarm.dev_attr.attr,
990 &sensor_dev_attr_in7_alarm.dev_attr.attr,
992 &sensor_dev_attr_temp1_input.dev_attr.attr,
993 &sensor_dev_attr_temp2_input.dev_attr.attr,
994 &sensor_dev_attr_temp3_input.dev_attr.attr,
995 &sensor_dev_attr_temp1_max.dev_attr.attr,
996 &sensor_dev_attr_temp2_max.dev_attr.attr,
997 &sensor_dev_attr_temp3_max.dev_attr.attr,
998 &sensor_dev_attr_temp1_min.dev_attr.attr,
999 &sensor_dev_attr_temp2_min.dev_attr.attr,
1000 &sensor_dev_attr_temp3_min.dev_attr.attr,
1001 &sensor_dev_attr_temp1_type.dev_attr.attr,
1002 &sensor_dev_attr_temp2_type.dev_attr.attr,
1003 &sensor_dev_attr_temp3_type.dev_attr.attr,
1004 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1005 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1006 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1008 &dev_attr_alarms.attr,
1009 &dev_attr_name.attr,
1010 NULL
1013 static const struct attribute_group it87_group = {
1014 .attrs = it87_attributes,
1017 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1018 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1019 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1020 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1021 NULL
1022 }, {
1023 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1024 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1025 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1026 NULL
1027 }, {
1028 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1029 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1030 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1031 NULL
1032 }, {
1033 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1034 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1035 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1036 NULL
1037 }, {
1038 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1039 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1040 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1041 NULL
1042 } };
1044 static const struct attribute_group it87_group_fan16[5] = {
1045 { .attrs = it87_attributes_fan16[0] },
1046 { .attrs = it87_attributes_fan16[1] },
1047 { .attrs = it87_attributes_fan16[2] },
1048 { .attrs = it87_attributes_fan16[3] },
1049 { .attrs = it87_attributes_fan16[4] },
1052 static struct attribute *it87_attributes_fan[3][4+1] = { {
1053 &sensor_dev_attr_fan1_input.dev_attr.attr,
1054 &sensor_dev_attr_fan1_min.dev_attr.attr,
1055 &sensor_dev_attr_fan1_div.dev_attr.attr,
1056 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1057 NULL
1058 }, {
1059 &sensor_dev_attr_fan2_input.dev_attr.attr,
1060 &sensor_dev_attr_fan2_min.dev_attr.attr,
1061 &sensor_dev_attr_fan2_div.dev_attr.attr,
1062 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1063 NULL
1064 }, {
1065 &sensor_dev_attr_fan3_input.dev_attr.attr,
1066 &sensor_dev_attr_fan3_min.dev_attr.attr,
1067 &sensor_dev_attr_fan3_div.dev_attr.attr,
1068 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1069 NULL
1070 } };
1072 static const struct attribute_group it87_group_fan[3] = {
1073 { .attrs = it87_attributes_fan[0] },
1074 { .attrs = it87_attributes_fan[1] },
1075 { .attrs = it87_attributes_fan[2] },
1078 static const struct attribute_group *
1079 it87_get_fan_group(const struct it87_data *data)
1081 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1084 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1085 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1086 &sensor_dev_attr_pwm1.dev_attr.attr,
1087 &dev_attr_pwm1_freq.attr,
1088 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1089 NULL
1090 }, {
1091 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1092 &sensor_dev_attr_pwm2.dev_attr.attr,
1093 &dev_attr_pwm2_freq.attr,
1094 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1095 NULL
1096 }, {
1097 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1098 &sensor_dev_attr_pwm3.dev_attr.attr,
1099 &dev_attr_pwm3_freq.attr,
1100 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1101 NULL
1102 } };
1104 static const struct attribute_group it87_group_pwm[3] = {
1105 { .attrs = it87_attributes_pwm[0] },
1106 { .attrs = it87_attributes_pwm[1] },
1107 { .attrs = it87_attributes_pwm[2] },
1110 static struct attribute *it87_attributes_vid[] = {
1111 &dev_attr_vrm.attr,
1112 &dev_attr_cpu0_vid.attr,
1113 NULL
1116 static const struct attribute_group it87_group_vid = {
1117 .attrs = it87_attributes_vid,
1120 /* SuperIO detection - will change isa_address if a chip is found */
1121 static int __init it87_find(unsigned short *address,
1122 struct it87_sio_data *sio_data)
1124 int err = -ENODEV;
1125 u16 chip_type;
1126 const char *board_vendor, *board_name;
1128 superio_enter();
1129 chip_type = force_id ? force_id : superio_inw(DEVID);
1131 switch (chip_type) {
1132 case IT8705F_DEVID:
1133 sio_data->type = it87;
1134 break;
1135 case IT8712F_DEVID:
1136 sio_data->type = it8712;
1137 break;
1138 case IT8716F_DEVID:
1139 case IT8726F_DEVID:
1140 sio_data->type = it8716;
1141 break;
1142 case IT8718F_DEVID:
1143 sio_data->type = it8718;
1144 break;
1145 case IT8720F_DEVID:
1146 sio_data->type = it8720;
1147 break;
1148 case 0xffff: /* No device at all */
1149 goto exit;
1150 default:
1151 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1152 chip_type);
1153 goto exit;
1156 superio_select(PME);
1157 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1158 pr_info("it87: Device not activated, skipping\n");
1159 goto exit;
1162 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1163 if (*address == 0) {
1164 pr_info("it87: Base address not set, skipping\n");
1165 goto exit;
1168 err = 0;
1169 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1170 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
1171 chip_type, *address, sio_data->revision);
1173 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1174 if (sio_data->type == it87) {
1175 /* The IT8705F doesn't have VID pins at all */
1176 sio_data->skip_vid = 1;
1177 } else {
1178 int reg;
1180 superio_select(GPIO);
1181 /* We need at least 4 VID pins */
1182 reg = superio_inb(IT87_SIO_GPIO3_REG);
1183 if (reg & 0x0f) {
1184 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1185 sio_data->skip_vid = 1;
1188 /* Check if fan3 is there or not */
1189 if (reg & (1 << 6))
1190 sio_data->skip_pwm |= (1 << 2);
1191 if (reg & (1 << 7))
1192 sio_data->skip_fan |= (1 << 2);
1194 /* Check if fan2 is there or not */
1195 reg = superio_inb(IT87_SIO_GPIO5_REG);
1196 if (reg & (1 << 1))
1197 sio_data->skip_pwm |= (1 << 1);
1198 if (reg & (1 << 2))
1199 sio_data->skip_fan |= (1 << 1);
1201 if ((sio_data->type == it8718 || sio_data->type == it8720)
1202 && !(sio_data->skip_vid))
1203 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1205 reg = superio_inb(IT87_SIO_PINX2_REG);
1206 if (reg & (1 << 0))
1207 pr_info("it87: in3 is VCC (+5V)\n");
1208 if (reg & (1 << 1))
1209 pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
1212 /* Disable specific features based on DMI strings */
1213 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1214 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1215 if (board_vendor && board_name) {
1216 if (strcmp(board_vendor, "nVIDIA") == 0
1217 && strcmp(board_name, "FN68PT") == 0) {
1218 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1219 connected to a fan, but to something else. One user
1220 has reported instant system power-off when changing
1221 the PWM2 duty cycle, so we disable it.
1222 I use the board name string as the trigger in case
1223 the same board is ever used in other systems. */
1224 pr_info("it87: Disabling pwm2 due to "
1225 "hardware constraints\n");
1226 sio_data->skip_pwm = (1 << 1);
1230 exit:
1231 superio_exit();
1232 return err;
1235 static void it87_remove_files(struct device *dev)
1237 struct it87_data *data = platform_get_drvdata(pdev);
1238 struct it87_sio_data *sio_data = dev->platform_data;
1239 const struct attribute_group *fan_group = it87_get_fan_group(data);
1240 int i;
1242 sysfs_remove_group(&dev->kobj, &it87_group);
1243 for (i = 0; i < 5; i++) {
1244 if (!(data->has_fan & (1 << i)))
1245 continue;
1246 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1248 for (i = 0; i < 3; i++) {
1249 if (sio_data->skip_pwm & (1 << 0))
1250 continue;
1251 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1253 if (!sio_data->skip_vid)
1254 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1257 static int __devinit it87_probe(struct platform_device *pdev)
1259 struct it87_data *data;
1260 struct resource *res;
1261 struct device *dev = &pdev->dev;
1262 struct it87_sio_data *sio_data = dev->platform_data;
1263 const struct attribute_group *fan_group;
1264 int err = 0, i;
1265 int enable_pwm_interface;
1266 static const char *names[] = {
1267 "it87",
1268 "it8712",
1269 "it8716",
1270 "it8718",
1271 "it8720",
1274 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1275 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1276 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1277 (unsigned long)res->start,
1278 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1279 err = -EBUSY;
1280 goto ERROR0;
1283 if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
1284 err = -ENOMEM;
1285 goto ERROR1;
1288 data->addr = res->start;
1289 data->type = sio_data->type;
1290 data->revision = sio_data->revision;
1291 data->name = names[sio_data->type];
1293 /* Now, we do the remaining detection. */
1294 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1295 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1296 err = -ENODEV;
1297 goto ERROR2;
1300 platform_set_drvdata(pdev, data);
1302 mutex_init(&data->update_lock);
1304 /* Check PWM configuration */
1305 enable_pwm_interface = it87_check_pwm(dev);
1307 /* Initialize the IT87 chip */
1308 it87_init_device(pdev);
1310 /* Register sysfs hooks */
1311 if ((err = sysfs_create_group(&dev->kobj, &it87_group)))
1312 goto ERROR2;
1314 /* Do not create fan files for disabled fans */
1315 fan_group = it87_get_fan_group(data);
1316 for (i = 0; i < 5; i++) {
1317 if (!(data->has_fan & (1 << i)))
1318 continue;
1319 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1320 if (err)
1321 goto ERROR4;
1324 if (enable_pwm_interface) {
1325 for (i = 0; i < 3; i++) {
1326 if (sio_data->skip_pwm & (1 << i))
1327 continue;
1328 err = sysfs_create_group(&dev->kobj,
1329 &it87_group_pwm[i]);
1330 if (err)
1331 goto ERROR4;
1335 if (!sio_data->skip_vid) {
1336 data->vrm = vid_which_vrm();
1337 /* VID reading from Super-I/O config space if available */
1338 data->vid = sio_data->vid_value;
1339 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1340 if (err)
1341 goto ERROR4;
1344 data->hwmon_dev = hwmon_device_register(dev);
1345 if (IS_ERR(data->hwmon_dev)) {
1346 err = PTR_ERR(data->hwmon_dev);
1347 goto ERROR4;
1350 return 0;
1352 ERROR4:
1353 it87_remove_files(dev);
1354 ERROR2:
1355 platform_set_drvdata(pdev, NULL);
1356 kfree(data);
1357 ERROR1:
1358 release_region(res->start, IT87_EC_EXTENT);
1359 ERROR0:
1360 return err;
1363 static int __devexit it87_remove(struct platform_device *pdev)
1365 struct it87_data *data = platform_get_drvdata(pdev);
1367 hwmon_device_unregister(data->hwmon_dev);
1368 it87_remove_files(&pdev->dev);
1370 release_region(data->addr, IT87_EC_EXTENT);
1371 platform_set_drvdata(pdev, NULL);
1372 kfree(data);
1374 return 0;
1377 /* Must be called with data->update_lock held, except during initialization.
1378 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1379 would slow down the IT87 access and should not be necessary. */
1380 static int it87_read_value(struct it87_data *data, u8 reg)
1382 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1383 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
1386 /* Must be called with data->update_lock held, except during initialization.
1387 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1388 would slow down the IT87 access and should not be necessary. */
1389 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
1391 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1392 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
1395 /* Return 1 if and only if the PWM interface is safe to use */
1396 static int __devinit it87_check_pwm(struct device *dev)
1398 struct it87_data *data = dev_get_drvdata(dev);
1399 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1400 * and polarity set to active low is sign that this is the case so we
1401 * disable pwm control to protect the user. */
1402 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1403 if ((tmp & 0x87) == 0) {
1404 if (fix_pwm_polarity) {
1405 /* The user asks us to attempt a chip reconfiguration.
1406 * This means switching to active high polarity and
1407 * inverting all fan speed values. */
1408 int i;
1409 u8 pwm[3];
1411 for (i = 0; i < 3; i++)
1412 pwm[i] = it87_read_value(data,
1413 IT87_REG_PWM(i));
1415 /* If any fan is in automatic pwm mode, the polarity
1416 * might be correct, as suspicious as it seems, so we
1417 * better don't change anything (but still disable the
1418 * PWM interface). */
1419 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1420 dev_info(dev, "Reconfiguring PWM to "
1421 "active high polarity\n");
1422 it87_write_value(data, IT87_REG_FAN_CTL,
1423 tmp | 0x87);
1424 for (i = 0; i < 3; i++)
1425 it87_write_value(data,
1426 IT87_REG_PWM(i),
1427 0x7f & ~pwm[i]);
1428 return 1;
1431 dev_info(dev, "PWM configuration is "
1432 "too broken to be fixed\n");
1435 dev_info(dev, "Detected broken BIOS "
1436 "defaults, disabling PWM interface\n");
1437 return 0;
1438 } else if (fix_pwm_polarity) {
1439 dev_info(dev, "PWM configuration looks "
1440 "sane, won't touch\n");
1443 return 1;
1446 /* Called when we have found a new IT87. */
1447 static void __devinit it87_init_device(struct platform_device *pdev)
1449 struct it87_sio_data *sio_data = pdev->dev.platform_data;
1450 struct it87_data *data = platform_get_drvdata(pdev);
1451 int tmp, i;
1452 u8 mask;
1454 /* For each PWM channel:
1455 * - If it is in automatic mode, setting to manual mode should set
1456 * the fan to full speed by default.
1457 * - If it is in manual mode, we need a mapping to temperature
1458 * channels to use when later setting to automatic mode later.
1459 * Use a 1:1 mapping by default (we are clueless.)
1460 * In both cases, the value can (and should) be changed by the user
1461 * prior to switching to a different mode. */
1462 for (i = 0; i < 3; i++) {
1463 data->pwm_temp_map[i] = i;
1464 data->pwm_duty[i] = 0x7f; /* Full speed */
1467 /* Some chips seem to have default value 0xff for all limit
1468 * registers. For low voltage limits it makes no sense and triggers
1469 * alarms, so change to 0 instead. For high temperature limits, it
1470 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1471 * but is still confusing, so change to 127 degrees C. */
1472 for (i = 0; i < 8; i++) {
1473 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
1474 if (tmp == 0xff)
1475 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
1477 for (i = 0; i < 3; i++) {
1478 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1479 if (tmp == 0xff)
1480 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
1483 /* Check if temperature channels are reset manually or by some reason */
1484 tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1485 if ((tmp & 0x3f) == 0) {
1486 /* Temp1,Temp3=thermistor; Temp2=thermal diode */
1487 tmp = (tmp & 0xc0) | 0x2a;
1488 it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
1490 data->sensor = tmp;
1492 /* Check if voltage monitors are reset manually or by some reason */
1493 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
1494 if ((tmp & 0xff) == 0) {
1495 /* Enable all voltage monitors */
1496 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
1499 /* Check if tachometers are reset manually or by some reason */
1500 mask = 0x70 & ~(sio_data->skip_fan << 4);
1501 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
1502 if ((data->fan_main_ctrl & mask) == 0) {
1503 /* Enable all fan tachometers */
1504 data->fan_main_ctrl |= mask;
1505 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
1507 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1509 /* Set tachometers to 16-bit mode if needed */
1510 if (has_16bit_fans(data)) {
1511 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
1512 if (~tmp & 0x07 & data->has_fan) {
1513 dev_dbg(&pdev->dev,
1514 "Setting fan1-3 to 16-bit mode\n");
1515 it87_write_value(data, IT87_REG_FAN_16BIT,
1516 tmp | 0x07);
1518 /* IT8705F only supports three fans. */
1519 if (data->type != it87) {
1520 if (tmp & (1 << 4))
1521 data->has_fan |= (1 << 3); /* fan4 enabled */
1522 if (tmp & (1 << 5))
1523 data->has_fan |= (1 << 4); /* fan5 enabled */
1527 /* Fan input pins may be used for alternative functions */
1528 data->has_fan &= ~sio_data->skip_fan;
1530 /* Start monitoring */
1531 it87_write_value(data, IT87_REG_CONFIG,
1532 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1533 | (update_vbat ? 0x41 : 0x01));
1536 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1538 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
1539 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1540 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
1541 else /* Manual mode */
1542 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
1545 static struct it87_data *it87_update_device(struct device *dev)
1547 struct it87_data *data = dev_get_drvdata(dev);
1548 int i;
1550 mutex_lock(&data->update_lock);
1552 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1553 || !data->valid) {
1555 if (update_vbat) {
1556 /* Cleared after each update, so reenable. Value
1557 returned by this read will be previous value */
1558 it87_write_value(data, IT87_REG_CONFIG,
1559 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1561 for (i = 0; i <= 7; i++) {
1562 data->in[i] =
1563 it87_read_value(data, IT87_REG_VIN(i));
1564 data->in_min[i] =
1565 it87_read_value(data, IT87_REG_VIN_MIN(i));
1566 data->in_max[i] =
1567 it87_read_value(data, IT87_REG_VIN_MAX(i));
1569 /* in8 (battery) has no limit registers */
1570 data->in[8] =
1571 it87_read_value(data, IT87_REG_VIN(8));
1573 for (i = 0; i < 5; i++) {
1574 /* Skip disabled fans */
1575 if (!(data->has_fan & (1 << i)))
1576 continue;
1578 data->fan_min[i] =
1579 it87_read_value(data, IT87_REG_FAN_MIN[i]);
1580 data->fan[i] = it87_read_value(data,
1581 IT87_REG_FAN[i]);
1582 /* Add high byte if in 16-bit mode */
1583 if (has_16bit_fans(data)) {
1584 data->fan[i] |= it87_read_value(data,
1585 IT87_REG_FANX[i]) << 8;
1586 data->fan_min[i] |= it87_read_value(data,
1587 IT87_REG_FANX_MIN[i]) << 8;
1590 for (i = 0; i < 3; i++) {
1591 data->temp[i] =
1592 it87_read_value(data, IT87_REG_TEMP(i));
1593 data->temp_high[i] =
1594 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1595 data->temp_low[i] =
1596 it87_read_value(data, IT87_REG_TEMP_LOW(i));
1599 /* Newer chips don't have clock dividers */
1600 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
1601 i = it87_read_value(data, IT87_REG_FAN_DIV);
1602 data->fan_div[0] = i & 0x07;
1603 data->fan_div[1] = (i >> 3) & 0x07;
1604 data->fan_div[2] = (i & 0x40) ? 3 : 1;
1607 data->alarms =
1608 it87_read_value(data, IT87_REG_ALARM1) |
1609 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
1610 (it87_read_value(data, IT87_REG_ALARM3) << 16);
1612 data->fan_main_ctrl = it87_read_value(data,
1613 IT87_REG_FAN_MAIN_CTRL);
1614 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
1615 for (i = 0; i < 3; i++)
1616 it87_update_pwm_ctrl(data, i);
1618 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
1619 /* The 8705 does not have VID capability.
1620 The 8718 and the 8720 don't use IT87_REG_VID for the
1621 same purpose. */
1622 if (data->type == it8712 || data->type == it8716) {
1623 data->vid = it87_read_value(data, IT87_REG_VID);
1624 /* The older IT8712F revisions had only 5 VID pins,
1625 but we assume it is always safe to read 6 bits. */
1626 data->vid &= 0x3f;
1628 data->last_updated = jiffies;
1629 data->valid = 1;
1632 mutex_unlock(&data->update_lock);
1634 return data;
1637 static int __init it87_device_add(unsigned short address,
1638 const struct it87_sio_data *sio_data)
1640 struct resource res = {
1641 .start = address + IT87_EC_OFFSET,
1642 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
1643 .name = DRVNAME,
1644 .flags = IORESOURCE_IO,
1646 int err;
1648 err = acpi_check_resource_conflict(&res);
1649 if (err)
1650 goto exit;
1652 pdev = platform_device_alloc(DRVNAME, address);
1653 if (!pdev) {
1654 err = -ENOMEM;
1655 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1656 goto exit;
1659 err = platform_device_add_resources(pdev, &res, 1);
1660 if (err) {
1661 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1662 "(%d)\n", err);
1663 goto exit_device_put;
1666 err = platform_device_add_data(pdev, sio_data,
1667 sizeof(struct it87_sio_data));
1668 if (err) {
1669 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1670 goto exit_device_put;
1673 err = platform_device_add(pdev);
1674 if (err) {
1675 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1676 err);
1677 goto exit_device_put;
1680 return 0;
1682 exit_device_put:
1683 platform_device_put(pdev);
1684 exit:
1685 return err;
1688 static int __init sm_it87_init(void)
1690 int err;
1691 unsigned short isa_address=0;
1692 struct it87_sio_data sio_data;
1694 memset(&sio_data, 0, sizeof(struct it87_sio_data));
1695 err = it87_find(&isa_address, &sio_data);
1696 if (err)
1697 return err;
1698 err = platform_driver_register(&it87_driver);
1699 if (err)
1700 return err;
1702 err = it87_device_add(isa_address, &sio_data);
1703 if (err){
1704 platform_driver_unregister(&it87_driver);
1705 return err;
1708 return 0;
1711 static void __exit sm_it87_exit(void)
1713 platform_device_unregister(pdev);
1714 platform_driver_unregister(&it87_driver);
1718 MODULE_AUTHOR("Chris Gauthron, "
1719 "Jean Delvare <khali@linux-fr.org>");
1720 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
1721 module_param(update_vbat, bool, 0);
1722 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
1723 module_param(fix_pwm_polarity, bool, 0);
1724 MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
1725 MODULE_LICENSE("GPL");
1727 module_init(sm_it87_init);
1728 module_exit(sm_it87_exit);