]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/hwmon/it87.c
7a3616ccbf05798637991c25bf326f9e1e9899e7
[mirror_ubuntu-zesty-kernel.git] / drivers / hwmon / it87.c
1 /*
2 * it87.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
4 *
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)
9 *
10 * This driver supports only the Environment Controller in the IT8705F and
11 * similar parts. The other devices are supported by different drivers.
12 *
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
20 *
21 * Copyright (C) 2001 Chris Gauthron
22 * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
23 *
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.
28 *
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.
33 *
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.
37 */
38
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>
54
55 #define DRVNAME "it87"
56
57 enum chips { it87, it8712, it8716, it8718, it8720 };
58
59 static unsigned short force_id;
60 module_param(force_id, ushort, 0);
61 MODULE_PARM_DESC(force_id, "Override the detected device ID");
62
63 static struct platform_device *pdev;
64
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 */
69
70 /* The device with the IT8718F/IT8720F VID value in it */
71 #define GPIO 0x07
72
73 #define DEVID 0x20 /* Register: Device ID */
74 #define DEVREV 0x22 /* Register: Device Revision */
75
76 static inline int
77 superio_inb(int reg)
78 {
79 outb(reg, REG);
80 return inb(VAL);
81 }
82
83 static inline void
84 superio_outb(int reg, int val)
85 {
86 outb(reg, REG);
87 outb(val, VAL);
88 }
89
90 static int superio_inw(int reg)
91 {
92 int val;
93 outb(reg++, REG);
94 val = inb(VAL) << 8;
95 outb(reg, REG);
96 val |= inb(VAL);
97 return val;
98 }
99
100 static inline void
101 superio_select(int ldn)
102 {
103 outb(DEV, REG);
104 outb(ldn, VAL);
105 }
106
107 static inline void
108 superio_enter(void)
109 {
110 outb(0x87, REG);
111 outb(0x01, REG);
112 outb(0x55, REG);
113 outb(0x55, REG);
114 }
115
116 static inline void
117 superio_exit(void)
118 {
119 outb(0x02, REG);
120 outb(0x02, VAL);
121 }
122
123 /* Logical device 4 registers */
124 #define IT8712F_DEVID 0x8712
125 #define IT8705F_DEVID 0x8705
126 #define IT8716F_DEVID 0x8716
127 #define IT8718F_DEVID 0x8718
128 #define IT8720F_DEVID 0x8720
129 #define IT8726F_DEVID 0x8726
130 #define IT87_ACT_REG 0x30
131 #define IT87_BASE_REG 0x60
132
133 /* Logical device 7 registers (IT8712F and later) */
134 #define IT87_SIO_GPIO3_REG 0x27
135 #define IT87_SIO_GPIO5_REG 0x29
136 #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
137 #define IT87_SIO_VID_REG 0xfc /* VID value */
138 #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
139
140 /* Update battery voltage after every reading if true */
141 static int update_vbat;
142
143 /* Not all BIOSes properly configure the PWM registers */
144 static int fix_pwm_polarity;
145
146 /* Many IT87 constants specified below */
147
148 /* Length of ISA address segment */
149 #define IT87_EXTENT 8
150
151 /* Length of ISA address segment for Environmental Controller */
152 #define IT87_EC_EXTENT 2
153
154 /* Offset of EC registers from ISA base address */
155 #define IT87_EC_OFFSET 5
156
157 /* Where are the ISA address/data registers relative to the EC base address */
158 #define IT87_ADDR_REG_OFFSET 0
159 #define IT87_DATA_REG_OFFSET 1
160
161 /*----- The IT87 registers -----*/
162
163 #define IT87_REG_CONFIG 0x00
164
165 #define IT87_REG_ALARM1 0x01
166 #define IT87_REG_ALARM2 0x02
167 #define IT87_REG_ALARM3 0x03
168
169 /* The IT8718F and IT8720F have the VID value in a different register, in
170 Super-I/O configuration space. */
171 #define IT87_REG_VID 0x0a
172 /* The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
173 for fan divisors. Later IT8712F revisions must use 16-bit tachometer
174 mode. */
175 #define IT87_REG_FAN_DIV 0x0b
176 #define IT87_REG_FAN_16BIT 0x0c
177
178 /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
179
180 static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
181 static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
182 static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
183 static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
184 #define IT87_REG_FAN_MAIN_CTRL 0x13
185 #define IT87_REG_FAN_CTL 0x14
186 #define IT87_REG_PWM(nr) (0x15 + (nr))
187
188 #define IT87_REG_VIN(nr) (0x20 + (nr))
189 #define IT87_REG_TEMP(nr) (0x29 + (nr))
190
191 #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
192 #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
193 #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
194 #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
195
196 #define IT87_REG_VIN_ENABLE 0x50
197 #define IT87_REG_TEMP_ENABLE 0x51
198 #define IT87_REG_BEEP_ENABLE 0x5c
199
200 #define IT87_REG_CHIPID 0x58
201
202 #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
203 #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
204
205
206 struct it87_sio_data {
207 enum chips type;
208 /* Values read from Super-I/O config space */
209 u8 revision;
210 u8 vid_value;
211 u8 beep_pin;
212 u8 internal; /* Internal sensors can be labeled */
213 /* Features skipped based on config or DMI */
214 u8 skip_vid;
215 u8 skip_fan;
216 u8 skip_pwm;
217 };
218
219 /* For each registered chip, we need to keep some data in memory.
220 The structure is dynamically allocated. */
221 struct it87_data {
222 struct device *hwmon_dev;
223 enum chips type;
224 u8 revision;
225
226 unsigned short addr;
227 const char *name;
228 struct mutex update_lock;
229 char valid; /* !=0 if following fields are valid */
230 unsigned long last_updated; /* In jiffies */
231
232 u8 in[9]; /* Register value */
233 u8 in_max[8]; /* Register value */
234 u8 in_min[8]; /* Register value */
235 u8 has_fan; /* Bitfield, fans enabled */
236 u16 fan[5]; /* Register values, possibly combined */
237 u16 fan_min[5]; /* Register values, possibly combined */
238 s8 temp[3]; /* Register value */
239 s8 temp_high[3]; /* Register value */
240 s8 temp_low[3]; /* Register value */
241 u8 sensor; /* Register value */
242 u8 fan_div[3]; /* Register encoding, shifted right */
243 u8 vid; /* Register encoding, combined */
244 u8 vrm;
245 u32 alarms; /* Register encoding, combined */
246 u8 beeps; /* Register encoding */
247 u8 fan_main_ctrl; /* Register value */
248 u8 fan_ctl; /* Register value */
249
250 /* The following 3 arrays correspond to the same registers. The
251 * meaning of bits 6-0 depends on the value of bit 7, and we want
252 * to preserve settings on mode changes, so we have to track all
253 * values separately. */
254 u8 pwm_ctrl[3]; /* Register value */
255 u8 pwm_duty[3]; /* Manual PWM value set by user (bit 6-0) */
256 u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
257
258 /* Automatic fan speed control registers */
259 u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
260 s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
261 };
262
263 #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8) / 16), 0, 255))
264 #define IN_FROM_REG(val) ((val) * 16)
265
266 static inline u8 FAN_TO_REG(long rpm, int div)
267 {
268 if (rpm == 0)
269 return 255;
270 rpm = SENSORS_LIMIT(rpm, 1, 1000000);
271 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
272 254);
273 }
274
275 static inline u16 FAN16_TO_REG(long rpm)
276 {
277 if (rpm == 0)
278 return 0xffff;
279 return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
280 }
281
282 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
283 1350000 / ((val) * (div)))
284 /* The divider is fixed to 2 in 16-bit mode */
285 #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
286 1350000 / ((val) * 2))
287
288 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
289 ((val) + 500) / 1000), -128, 127))
290 #define TEMP_FROM_REG(val) ((val) * 1000)
291
292 #define PWM_TO_REG(val) ((val) >> 1)
293 #define PWM_FROM_REG(val) (((val) & 0x7f) << 1)
294
295 static int DIV_TO_REG(int val)
296 {
297 int answer = 0;
298 while (answer < 7 && (val >>= 1))
299 answer++;
300 return answer;
301 }
302 #define DIV_FROM_REG(val) (1 << (val))
303
304 static const unsigned int pwm_freq[8] = {
305 48000000 / 128,
306 24000000 / 128,
307 12000000 / 128,
308 8000000 / 128,
309 6000000 / 128,
310 3000000 / 128,
311 1500000 / 128,
312 750000 / 128,
313 };
314
315 static inline int has_16bit_fans(const struct it87_data *data)
316 {
317 /* IT8705F Datasheet 0.4.1, 3h == Version G.
318 IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
319 These are the first revisions with 16bit tachometer support. */
320 return (data->type == it87 && data->revision >= 0x03)
321 || (data->type == it8712 && data->revision >= 0x08)
322 || data->type == it8716
323 || data->type == it8718
324 || data->type == it8720;
325 }
326
327 static inline int has_old_autopwm(const struct it87_data *data)
328 {
329 /* The old automatic fan speed control interface is implemented
330 by IT8705F chips up to revision F and IT8712F chips up to
331 revision G. */
332 return (data->type == it87 && data->revision < 0x03)
333 || (data->type == it8712 && data->revision < 0x08);
334 }
335
336 static int it87_probe(struct platform_device *pdev);
337 static int __devexit it87_remove(struct platform_device *pdev);
338
339 static int it87_read_value(struct it87_data *data, u8 reg);
340 static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
341 static struct it87_data *it87_update_device(struct device *dev);
342 static int it87_check_pwm(struct device *dev);
343 static void it87_init_device(struct platform_device *pdev);
344
345
346 static struct platform_driver it87_driver = {
347 .driver = {
348 .owner = THIS_MODULE,
349 .name = DRVNAME,
350 },
351 .probe = it87_probe,
352 .remove = __devexit_p(it87_remove),
353 };
354
355 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
356 char *buf)
357 {
358 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
359 int nr = sensor_attr->index;
360
361 struct it87_data *data = it87_update_device(dev);
362 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
363 }
364
365 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
366 char *buf)
367 {
368 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
369 int nr = sensor_attr->index;
370
371 struct it87_data *data = it87_update_device(dev);
372 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
373 }
374
375 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
376 char *buf)
377 {
378 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
379 int nr = sensor_attr->index;
380
381 struct it87_data *data = it87_update_device(dev);
382 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
383 }
384
385 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
387 {
388 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
389 int nr = sensor_attr->index;
390
391 struct it87_data *data = dev_get_drvdata(dev);
392 unsigned long val;
393
394 if (strict_strtoul(buf, 10, &val) < 0)
395 return -EINVAL;
396
397 mutex_lock(&data->update_lock);
398 data->in_min[nr] = IN_TO_REG(val);
399 it87_write_value(data, IT87_REG_VIN_MIN(nr),
400 data->in_min[nr]);
401 mutex_unlock(&data->update_lock);
402 return count;
403 }
404 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
405 const char *buf, size_t count)
406 {
407 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
408 int nr = sensor_attr->index;
409
410 struct it87_data *data = dev_get_drvdata(dev);
411 unsigned long val;
412
413 if (strict_strtoul(buf, 10, &val) < 0)
414 return -EINVAL;
415
416 mutex_lock(&data->update_lock);
417 data->in_max[nr] = IN_TO_REG(val);
418 it87_write_value(data, IT87_REG_VIN_MAX(nr),
419 data->in_max[nr]);
420 mutex_unlock(&data->update_lock);
421 return count;
422 }
423
424 #define show_in_offset(offset) \
425 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
426 show_in, NULL, offset);
427
428 #define limit_in_offset(offset) \
429 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
430 show_in_min, set_in_min, offset); \
431 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
432 show_in_max, set_in_max, offset);
433
434 show_in_offset(0);
435 limit_in_offset(0);
436 show_in_offset(1);
437 limit_in_offset(1);
438 show_in_offset(2);
439 limit_in_offset(2);
440 show_in_offset(3);
441 limit_in_offset(3);
442 show_in_offset(4);
443 limit_in_offset(4);
444 show_in_offset(5);
445 limit_in_offset(5);
446 show_in_offset(6);
447 limit_in_offset(6);
448 show_in_offset(7);
449 limit_in_offset(7);
450 show_in_offset(8);
451
452 /* 3 temperatures */
453 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
454 char *buf)
455 {
456 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
457 int nr = sensor_attr->index;
458
459 struct it87_data *data = it87_update_device(dev);
460 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
461 }
462 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
463 char *buf)
464 {
465 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
466 int nr = sensor_attr->index;
467
468 struct it87_data *data = it87_update_device(dev);
469 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
470 }
471 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
472 char *buf)
473 {
474 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
475 int nr = sensor_attr->index;
476
477 struct it87_data *data = it87_update_device(dev);
478 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
479 }
480 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
481 const char *buf, size_t count)
482 {
483 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
484 int nr = sensor_attr->index;
485
486 struct it87_data *data = dev_get_drvdata(dev);
487 long val;
488
489 if (strict_strtol(buf, 10, &val) < 0)
490 return -EINVAL;
491
492 mutex_lock(&data->update_lock);
493 data->temp_high[nr] = TEMP_TO_REG(val);
494 it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
495 mutex_unlock(&data->update_lock);
496 return count;
497 }
498 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
499 const char *buf, size_t count)
500 {
501 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
502 int nr = sensor_attr->index;
503
504 struct it87_data *data = dev_get_drvdata(dev);
505 long val;
506
507 if (strict_strtol(buf, 10, &val) < 0)
508 return -EINVAL;
509
510 mutex_lock(&data->update_lock);
511 data->temp_low[nr] = TEMP_TO_REG(val);
512 it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
513 mutex_unlock(&data->update_lock);
514 return count;
515 }
516 #define show_temp_offset(offset) \
517 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
518 show_temp, NULL, offset - 1); \
519 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
520 show_temp_max, set_temp_max, offset - 1); \
521 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
522 show_temp_min, set_temp_min, offset - 1);
523
524 show_temp_offset(1);
525 show_temp_offset(2);
526 show_temp_offset(3);
527
528 static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
529 char *buf)
530 {
531 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
532 int nr = sensor_attr->index;
533
534 struct it87_data *data = it87_update_device(dev);
535 u8 reg = data->sensor; /* In case the value is updated while
536 we use it */
537
538 if (reg & (1 << nr))
539 return sprintf(buf, "3\n"); /* thermal diode */
540 if (reg & (8 << nr))
541 return sprintf(buf, "4\n"); /* thermistor */
542 return sprintf(buf, "0\n"); /* disabled */
543 }
544 static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
545 const char *buf, size_t count)
546 {
547 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
548 int nr = sensor_attr->index;
549
550 struct it87_data *data = dev_get_drvdata(dev);
551 long val;
552 u8 reg;
553
554 if (strict_strtol(buf, 10, &val) < 0)
555 return -EINVAL;
556
557 reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
558 reg &= ~(1 << nr);
559 reg &= ~(8 << nr);
560 if (val == 2) { /* backwards compatibility */
561 dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
562 "instead\n");
563 val = 4;
564 }
565 /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
566 if (val == 3)
567 reg |= 1 << nr;
568 else if (val == 4)
569 reg |= 8 << nr;
570 else if (val != 0)
571 return -EINVAL;
572
573 mutex_lock(&data->update_lock);
574 data->sensor = reg;
575 it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
576 data->valid = 0; /* Force cache refresh */
577 mutex_unlock(&data->update_lock);
578 return count;
579 }
580 #define show_sensor_offset(offset) \
581 static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
582 show_sensor, set_sensor, offset - 1);
583
584 show_sensor_offset(1);
585 show_sensor_offset(2);
586 show_sensor_offset(3);
587
588 /* 3 Fans */
589
590 static int pwm_mode(const struct it87_data *data, int nr)
591 {
592 int ctrl = data->fan_main_ctrl & (1 << nr);
593
594 if (ctrl == 0) /* Full speed */
595 return 0;
596 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
597 return 2;
598 else /* Manual mode */
599 return 1;
600 }
601
602 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
603 char *buf)
604 {
605 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
606 int nr = sensor_attr->index;
607
608 struct it87_data *data = it87_update_device(dev);
609 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
610 DIV_FROM_REG(data->fan_div[nr])));
611 }
612 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
613 char *buf)
614 {
615 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
616 int nr = sensor_attr->index;
617
618 struct it87_data *data = it87_update_device(dev);
619 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
620 DIV_FROM_REG(data->fan_div[nr])));
621 }
622 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
623 char *buf)
624 {
625 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
626 int nr = sensor_attr->index;
627
628 struct it87_data *data = it87_update_device(dev);
629 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
630 }
631 static ssize_t show_pwm_enable(struct device *dev,
632 struct device_attribute *attr, char *buf)
633 {
634 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
635 int nr = sensor_attr->index;
636
637 struct it87_data *data = it87_update_device(dev);
638 return sprintf(buf, "%d\n", pwm_mode(data, nr));
639 }
640 static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
641 char *buf)
642 {
643 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
644 int nr = sensor_attr->index;
645
646 struct it87_data *data = it87_update_device(dev);
647 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm_duty[nr]));
648 }
649 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
650 char *buf)
651 {
652 struct it87_data *data = it87_update_device(dev);
653 int index = (data->fan_ctl >> 4) & 0x07;
654
655 return sprintf(buf, "%u\n", pwm_freq[index]);
656 }
657 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
658 const char *buf, size_t count)
659 {
660 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
661 int nr = sensor_attr->index;
662
663 struct it87_data *data = dev_get_drvdata(dev);
664 long val;
665 u8 reg;
666
667 if (strict_strtol(buf, 10, &val) < 0)
668 return -EINVAL;
669
670 mutex_lock(&data->update_lock);
671 reg = it87_read_value(data, IT87_REG_FAN_DIV);
672 switch (nr) {
673 case 0:
674 data->fan_div[nr] = reg & 0x07;
675 break;
676 case 1:
677 data->fan_div[nr] = (reg >> 3) & 0x07;
678 break;
679 case 2:
680 data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
681 break;
682 }
683
684 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
685 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
686 mutex_unlock(&data->update_lock);
687 return count;
688 }
689 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
690 const char *buf, size_t count)
691 {
692 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
693 int nr = sensor_attr->index;
694
695 struct it87_data *data = dev_get_drvdata(dev);
696 unsigned long val;
697 int min;
698 u8 old;
699
700 if (strict_strtoul(buf, 10, &val) < 0)
701 return -EINVAL;
702
703 mutex_lock(&data->update_lock);
704 old = it87_read_value(data, IT87_REG_FAN_DIV);
705
706 /* Save fan min limit */
707 min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
708
709 switch (nr) {
710 case 0:
711 case 1:
712 data->fan_div[nr] = DIV_TO_REG(val);
713 break;
714 case 2:
715 if (val < 8)
716 data->fan_div[nr] = 1;
717 else
718 data->fan_div[nr] = 3;
719 }
720 val = old & 0x80;
721 val |= (data->fan_div[0] & 0x07);
722 val |= (data->fan_div[1] & 0x07) << 3;
723 if (data->fan_div[2] == 3)
724 val |= 0x1 << 6;
725 it87_write_value(data, IT87_REG_FAN_DIV, val);
726
727 /* Restore fan min limit */
728 data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
729 it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
730
731 mutex_unlock(&data->update_lock);
732 return count;
733 }
734
735 /* Returns 0 if OK, -EINVAL otherwise */
736 static int check_trip_points(struct device *dev, int nr)
737 {
738 const struct it87_data *data = dev_get_drvdata(dev);
739 int i, err = 0;
740
741 if (has_old_autopwm(data)) {
742 for (i = 0; i < 3; i++) {
743 if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
744 err = -EINVAL;
745 }
746 for (i = 0; i < 2; i++) {
747 if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
748 err = -EINVAL;
749 }
750 }
751
752 if (err) {
753 dev_err(dev, "Inconsistent trip points, not switching to "
754 "automatic mode\n");
755 dev_err(dev, "Adjust the trip points and try again\n");
756 }
757 return err;
758 }
759
760 static ssize_t set_pwm_enable(struct device *dev,
761 struct device_attribute *attr, const char *buf, size_t count)
762 {
763 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
764 int nr = sensor_attr->index;
765
766 struct it87_data *data = dev_get_drvdata(dev);
767 long val;
768
769 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2)
770 return -EINVAL;
771
772 /* Check trip points before switching to automatic mode */
773 if (val == 2) {
774 if (check_trip_points(dev, nr) < 0)
775 return -EINVAL;
776 }
777
778 mutex_lock(&data->update_lock);
779
780 if (val == 0) {
781 int tmp;
782 /* make sure the fan is on when in on/off mode */
783 tmp = it87_read_value(data, IT87_REG_FAN_CTL);
784 it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
785 /* set on/off mode */
786 data->fan_main_ctrl &= ~(1 << nr);
787 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
788 data->fan_main_ctrl);
789 } else {
790 if (val == 1) /* Manual mode */
791 data->pwm_ctrl[nr] = data->pwm_duty[nr];
792 else /* Automatic mode */
793 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
794 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
795 /* set SmartGuardian mode */
796 data->fan_main_ctrl |= (1 << nr);
797 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
798 data->fan_main_ctrl);
799 }
800
801 mutex_unlock(&data->update_lock);
802 return count;
803 }
804 static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
805 const char *buf, size_t count)
806 {
807 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
808 int nr = sensor_attr->index;
809
810 struct it87_data *data = dev_get_drvdata(dev);
811 long val;
812
813 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
814 return -EINVAL;
815
816 mutex_lock(&data->update_lock);
817 data->pwm_duty[nr] = PWM_TO_REG(val);
818 /* If we are in manual mode, write the duty cycle immediately;
819 * otherwise, just store it for later use. */
820 if (!(data->pwm_ctrl[nr] & 0x80)) {
821 data->pwm_ctrl[nr] = data->pwm_duty[nr];
822 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
823 }
824 mutex_unlock(&data->update_lock);
825 return count;
826 }
827 static ssize_t set_pwm_freq(struct device *dev,
828 struct device_attribute *attr, const char *buf, size_t count)
829 {
830 struct it87_data *data = dev_get_drvdata(dev);
831 unsigned long val;
832 int i;
833
834 if (strict_strtoul(buf, 10, &val) < 0)
835 return -EINVAL;
836
837 /* Search for the nearest available frequency */
838 for (i = 0; i < 7; i++) {
839 if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
840 break;
841 }
842
843 mutex_lock(&data->update_lock);
844 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
845 data->fan_ctl |= i << 4;
846 it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
847 mutex_unlock(&data->update_lock);
848
849 return count;
850 }
851 static ssize_t show_pwm_temp_map(struct device *dev,
852 struct device_attribute *attr, char *buf)
853 {
854 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
855 int nr = sensor_attr->index;
856
857 struct it87_data *data = it87_update_device(dev);
858 int map;
859
860 if (data->pwm_temp_map[nr] < 3)
861 map = 1 << data->pwm_temp_map[nr];
862 else
863 map = 0; /* Should never happen */
864 return sprintf(buf, "%d\n", map);
865 }
866 static ssize_t set_pwm_temp_map(struct device *dev,
867 struct device_attribute *attr, const char *buf, size_t count)
868 {
869 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
870 int nr = sensor_attr->index;
871
872 struct it87_data *data = dev_get_drvdata(dev);
873 long val;
874 u8 reg;
875
876 /* This check can go away if we ever support automatic fan speed
877 control on newer chips. */
878 if (!has_old_autopwm(data)) {
879 dev_notice(dev, "Mapping change disabled for safety reasons\n");
880 return -EINVAL;
881 }
882
883 if (strict_strtol(buf, 10, &val) < 0)
884 return -EINVAL;
885
886 switch (val) {
887 case (1 << 0):
888 reg = 0x00;
889 break;
890 case (1 << 1):
891 reg = 0x01;
892 break;
893 case (1 << 2):
894 reg = 0x02;
895 break;
896 default:
897 return -EINVAL;
898 }
899
900 mutex_lock(&data->update_lock);
901 data->pwm_temp_map[nr] = reg;
902 /* If we are in automatic mode, write the temp mapping immediately;
903 * otherwise, just store it for later use. */
904 if (data->pwm_ctrl[nr] & 0x80) {
905 data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
906 it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
907 }
908 mutex_unlock(&data->update_lock);
909 return count;
910 }
911
912 static ssize_t show_auto_pwm(struct device *dev,
913 struct device_attribute *attr, char *buf)
914 {
915 struct it87_data *data = it87_update_device(dev);
916 struct sensor_device_attribute_2 *sensor_attr =
917 to_sensor_dev_attr_2(attr);
918 int nr = sensor_attr->nr;
919 int point = sensor_attr->index;
920
921 return sprintf(buf, "%d\n", PWM_FROM_REG(data->auto_pwm[nr][point]));
922 }
923
924 static ssize_t set_auto_pwm(struct device *dev,
925 struct device_attribute *attr, const char *buf, size_t count)
926 {
927 struct it87_data *data = dev_get_drvdata(dev);
928 struct sensor_device_attribute_2 *sensor_attr =
929 to_sensor_dev_attr_2(attr);
930 int nr = sensor_attr->nr;
931 int point = sensor_attr->index;
932 long val;
933
934 if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
935 return -EINVAL;
936
937 mutex_lock(&data->update_lock);
938 data->auto_pwm[nr][point] = PWM_TO_REG(val);
939 it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
940 data->auto_pwm[nr][point]);
941 mutex_unlock(&data->update_lock);
942 return count;
943 }
944
945 static ssize_t show_auto_temp(struct device *dev,
946 struct device_attribute *attr, char *buf)
947 {
948 struct it87_data *data = it87_update_device(dev);
949 struct sensor_device_attribute_2 *sensor_attr =
950 to_sensor_dev_attr_2(attr);
951 int nr = sensor_attr->nr;
952 int point = sensor_attr->index;
953
954 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
955 }
956
957 static ssize_t set_auto_temp(struct device *dev,
958 struct device_attribute *attr, const char *buf, size_t count)
959 {
960 struct it87_data *data = dev_get_drvdata(dev);
961 struct sensor_device_attribute_2 *sensor_attr =
962 to_sensor_dev_attr_2(attr);
963 int nr = sensor_attr->nr;
964 int point = sensor_attr->index;
965 long val;
966
967 if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
968 return -EINVAL;
969
970 mutex_lock(&data->update_lock);
971 data->auto_temp[nr][point] = TEMP_TO_REG(val);
972 it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
973 data->auto_temp[nr][point]);
974 mutex_unlock(&data->update_lock);
975 return count;
976 }
977
978 #define show_fan_offset(offset) \
979 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
980 show_fan, NULL, offset - 1); \
981 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
982 show_fan_min, set_fan_min, offset - 1); \
983 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
984 show_fan_div, set_fan_div, offset - 1);
985
986 show_fan_offset(1);
987 show_fan_offset(2);
988 show_fan_offset(3);
989
990 #define show_pwm_offset(offset) \
991 static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
992 show_pwm_enable, set_pwm_enable, offset - 1); \
993 static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
994 show_pwm, set_pwm, offset - 1); \
995 static DEVICE_ATTR(pwm##offset##_freq, \
996 (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
997 show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
998 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
999 S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
1000 offset - 1); \
1001 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
1002 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1003 offset - 1, 0); \
1004 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
1005 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1006 offset - 1, 1); \
1007 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
1008 S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
1009 offset - 1, 2); \
1010 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
1011 S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
1012 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
1013 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1014 offset - 1, 1); \
1015 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
1016 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1017 offset - 1, 0); \
1018 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
1019 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1020 offset - 1, 2); \
1021 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
1022 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1023 offset - 1, 3); \
1024 static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
1025 S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
1026 offset - 1, 4);
1027
1028 show_pwm_offset(1);
1029 show_pwm_offset(2);
1030 show_pwm_offset(3);
1031
1032 /* A different set of callbacks for 16-bit fans */
1033 static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
1034 char *buf)
1035 {
1036 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1037 int nr = sensor_attr->index;
1038 struct it87_data *data = it87_update_device(dev);
1039 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
1040 }
1041
1042 static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
1043 char *buf)
1044 {
1045 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1046 int nr = sensor_attr->index;
1047 struct it87_data *data = it87_update_device(dev);
1048 return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
1049 }
1050
1051 static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
1052 const char *buf, size_t count)
1053 {
1054 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1055 int nr = sensor_attr->index;
1056 struct it87_data *data = dev_get_drvdata(dev);
1057 long val;
1058
1059 if (strict_strtol(buf, 10, &val) < 0)
1060 return -EINVAL;
1061
1062 mutex_lock(&data->update_lock);
1063 data->fan_min[nr] = FAN16_TO_REG(val);
1064 it87_write_value(data, IT87_REG_FAN_MIN[nr],
1065 data->fan_min[nr] & 0xff);
1066 it87_write_value(data, IT87_REG_FANX_MIN[nr],
1067 data->fan_min[nr] >> 8);
1068 mutex_unlock(&data->update_lock);
1069 return count;
1070 }
1071
1072 /* We want to use the same sysfs file names as 8-bit fans, but we need
1073 different variable names, so we have to use SENSOR_ATTR instead of
1074 SENSOR_DEVICE_ATTR. */
1075 #define show_fan16_offset(offset) \
1076 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
1077 = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
1078 show_fan16, NULL, offset - 1); \
1079 static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
1080 = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
1081 show_fan16_min, set_fan16_min, offset - 1)
1082
1083 show_fan16_offset(1);
1084 show_fan16_offset(2);
1085 show_fan16_offset(3);
1086 show_fan16_offset(4);
1087 show_fan16_offset(5);
1088
1089 /* Alarms */
1090 static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
1091 char *buf)
1092 {
1093 struct it87_data *data = it87_update_device(dev);
1094 return sprintf(buf, "%u\n", data->alarms);
1095 }
1096 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
1097
1098 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1099 char *buf)
1100 {
1101 int bitnr = to_sensor_dev_attr(attr)->index;
1102 struct it87_data *data = it87_update_device(dev);
1103 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
1104 }
1105 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1106 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1107 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1108 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1109 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1110 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1111 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1112 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1113 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
1114 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
1115 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
1116 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
1117 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
1118 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
1119 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
1120 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
1121
1122 static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
1123 char *buf)
1124 {
1125 int bitnr = to_sensor_dev_attr(attr)->index;
1126 struct it87_data *data = it87_update_device(dev);
1127 return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
1128 }
1129 static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
1130 const char *buf, size_t count)
1131 {
1132 int bitnr = to_sensor_dev_attr(attr)->index;
1133 struct it87_data *data = dev_get_drvdata(dev);
1134 long val;
1135
1136 if (strict_strtol(buf, 10, &val) < 0
1137 || (val != 0 && val != 1))
1138 return -EINVAL;
1139
1140 mutex_lock(&data->update_lock);
1141 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
1142 if (val)
1143 data->beeps |= (1 << bitnr);
1144 else
1145 data->beeps &= ~(1 << bitnr);
1146 it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
1147 mutex_unlock(&data->update_lock);
1148 return count;
1149 }
1150
1151 static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
1152 show_beep, set_beep, 1);
1153 static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
1154 static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
1155 static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
1156 static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
1157 static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
1158 static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
1159 static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
1160 /* fanX_beep writability is set later */
1161 static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
1162 static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
1163 static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
1164 static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
1165 static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
1166 static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
1167 show_beep, set_beep, 2);
1168 static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
1169 static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
1170
1171 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1172 char *buf)
1173 {
1174 struct it87_data *data = dev_get_drvdata(dev);
1175 return sprintf(buf, "%u\n", data->vrm);
1176 }
1177 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1178 const char *buf, size_t count)
1179 {
1180 struct it87_data *data = dev_get_drvdata(dev);
1181 unsigned long val;
1182
1183 if (strict_strtoul(buf, 10, &val) < 0)
1184 return -EINVAL;
1185
1186 data->vrm = val;
1187
1188 return count;
1189 }
1190 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1191
1192 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1193 char *buf)
1194 {
1195 struct it87_data *data = it87_update_device(dev);
1196 return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
1197 }
1198 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1199
1200 static ssize_t show_label(struct device *dev, struct device_attribute *attr,
1201 char *buf)
1202 {
1203 static const char *labels[] = {
1204 "+5V",
1205 "5VSB",
1206 "Vbat",
1207 };
1208 int nr = to_sensor_dev_attr(attr)->index;
1209
1210 return sprintf(buf, "%s\n", labels[nr]);
1211 }
1212 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
1213 static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
1214 static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
1215
1216 static ssize_t show_name(struct device *dev, struct device_attribute
1217 *devattr, char *buf)
1218 {
1219 struct it87_data *data = dev_get_drvdata(dev);
1220 return sprintf(buf, "%s\n", data->name);
1221 }
1222 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1223
1224 static struct attribute *it87_attributes[] = {
1225 &sensor_dev_attr_in0_input.dev_attr.attr,
1226 &sensor_dev_attr_in1_input.dev_attr.attr,
1227 &sensor_dev_attr_in2_input.dev_attr.attr,
1228 &sensor_dev_attr_in3_input.dev_attr.attr,
1229 &sensor_dev_attr_in4_input.dev_attr.attr,
1230 &sensor_dev_attr_in5_input.dev_attr.attr,
1231 &sensor_dev_attr_in6_input.dev_attr.attr,
1232 &sensor_dev_attr_in7_input.dev_attr.attr,
1233 &sensor_dev_attr_in8_input.dev_attr.attr,
1234 &sensor_dev_attr_in0_min.dev_attr.attr,
1235 &sensor_dev_attr_in1_min.dev_attr.attr,
1236 &sensor_dev_attr_in2_min.dev_attr.attr,
1237 &sensor_dev_attr_in3_min.dev_attr.attr,
1238 &sensor_dev_attr_in4_min.dev_attr.attr,
1239 &sensor_dev_attr_in5_min.dev_attr.attr,
1240 &sensor_dev_attr_in6_min.dev_attr.attr,
1241 &sensor_dev_attr_in7_min.dev_attr.attr,
1242 &sensor_dev_attr_in0_max.dev_attr.attr,
1243 &sensor_dev_attr_in1_max.dev_attr.attr,
1244 &sensor_dev_attr_in2_max.dev_attr.attr,
1245 &sensor_dev_attr_in3_max.dev_attr.attr,
1246 &sensor_dev_attr_in4_max.dev_attr.attr,
1247 &sensor_dev_attr_in5_max.dev_attr.attr,
1248 &sensor_dev_attr_in6_max.dev_attr.attr,
1249 &sensor_dev_attr_in7_max.dev_attr.attr,
1250 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1251 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1252 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1253 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1254 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1255 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1256 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1257 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1258
1259 &sensor_dev_attr_temp1_input.dev_attr.attr,
1260 &sensor_dev_attr_temp2_input.dev_attr.attr,
1261 &sensor_dev_attr_temp3_input.dev_attr.attr,
1262 &sensor_dev_attr_temp1_max.dev_attr.attr,
1263 &sensor_dev_attr_temp2_max.dev_attr.attr,
1264 &sensor_dev_attr_temp3_max.dev_attr.attr,
1265 &sensor_dev_attr_temp1_min.dev_attr.attr,
1266 &sensor_dev_attr_temp2_min.dev_attr.attr,
1267 &sensor_dev_attr_temp3_min.dev_attr.attr,
1268 &sensor_dev_attr_temp1_type.dev_attr.attr,
1269 &sensor_dev_attr_temp2_type.dev_attr.attr,
1270 &sensor_dev_attr_temp3_type.dev_attr.attr,
1271 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1272 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1273 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1274
1275 &dev_attr_alarms.attr,
1276 &dev_attr_name.attr,
1277 NULL
1278 };
1279
1280 static const struct attribute_group it87_group = {
1281 .attrs = it87_attributes,
1282 };
1283
1284 static struct attribute *it87_attributes_beep[] = {
1285 &sensor_dev_attr_in0_beep.dev_attr.attr,
1286 &sensor_dev_attr_in1_beep.dev_attr.attr,
1287 &sensor_dev_attr_in2_beep.dev_attr.attr,
1288 &sensor_dev_attr_in3_beep.dev_attr.attr,
1289 &sensor_dev_attr_in4_beep.dev_attr.attr,
1290 &sensor_dev_attr_in5_beep.dev_attr.attr,
1291 &sensor_dev_attr_in6_beep.dev_attr.attr,
1292 &sensor_dev_attr_in7_beep.dev_attr.attr,
1293
1294 &sensor_dev_attr_temp1_beep.dev_attr.attr,
1295 &sensor_dev_attr_temp2_beep.dev_attr.attr,
1296 &sensor_dev_attr_temp3_beep.dev_attr.attr,
1297 NULL
1298 };
1299
1300 static const struct attribute_group it87_group_beep = {
1301 .attrs = it87_attributes_beep,
1302 };
1303
1304 static struct attribute *it87_attributes_fan16[5][3+1] = { {
1305 &sensor_dev_attr_fan1_input16.dev_attr.attr,
1306 &sensor_dev_attr_fan1_min16.dev_attr.attr,
1307 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1308 NULL
1309 }, {
1310 &sensor_dev_attr_fan2_input16.dev_attr.attr,
1311 &sensor_dev_attr_fan2_min16.dev_attr.attr,
1312 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1313 NULL
1314 }, {
1315 &sensor_dev_attr_fan3_input16.dev_attr.attr,
1316 &sensor_dev_attr_fan3_min16.dev_attr.attr,
1317 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1318 NULL
1319 }, {
1320 &sensor_dev_attr_fan4_input16.dev_attr.attr,
1321 &sensor_dev_attr_fan4_min16.dev_attr.attr,
1322 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1323 NULL
1324 }, {
1325 &sensor_dev_attr_fan5_input16.dev_attr.attr,
1326 &sensor_dev_attr_fan5_min16.dev_attr.attr,
1327 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1328 NULL
1329 } };
1330
1331 static const struct attribute_group it87_group_fan16[5] = {
1332 { .attrs = it87_attributes_fan16[0] },
1333 { .attrs = it87_attributes_fan16[1] },
1334 { .attrs = it87_attributes_fan16[2] },
1335 { .attrs = it87_attributes_fan16[3] },
1336 { .attrs = it87_attributes_fan16[4] },
1337 };
1338
1339 static struct attribute *it87_attributes_fan[3][4+1] = { {
1340 &sensor_dev_attr_fan1_input.dev_attr.attr,
1341 &sensor_dev_attr_fan1_min.dev_attr.attr,
1342 &sensor_dev_attr_fan1_div.dev_attr.attr,
1343 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1344 NULL
1345 }, {
1346 &sensor_dev_attr_fan2_input.dev_attr.attr,
1347 &sensor_dev_attr_fan2_min.dev_attr.attr,
1348 &sensor_dev_attr_fan2_div.dev_attr.attr,
1349 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1350 NULL
1351 }, {
1352 &sensor_dev_attr_fan3_input.dev_attr.attr,
1353 &sensor_dev_attr_fan3_min.dev_attr.attr,
1354 &sensor_dev_attr_fan3_div.dev_attr.attr,
1355 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1356 NULL
1357 } };
1358
1359 static const struct attribute_group it87_group_fan[3] = {
1360 { .attrs = it87_attributes_fan[0] },
1361 { .attrs = it87_attributes_fan[1] },
1362 { .attrs = it87_attributes_fan[2] },
1363 };
1364
1365 static const struct attribute_group *
1366 it87_get_fan_group(const struct it87_data *data)
1367 {
1368 return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
1369 }
1370
1371 static struct attribute *it87_attributes_pwm[3][4+1] = { {
1372 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1373 &sensor_dev_attr_pwm1.dev_attr.attr,
1374 &dev_attr_pwm1_freq.attr,
1375 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1376 NULL
1377 }, {
1378 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1379 &sensor_dev_attr_pwm2.dev_attr.attr,
1380 &dev_attr_pwm2_freq.attr,
1381 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1382 NULL
1383 }, {
1384 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1385 &sensor_dev_attr_pwm3.dev_attr.attr,
1386 &dev_attr_pwm3_freq.attr,
1387 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1388 NULL
1389 } };
1390
1391 static const struct attribute_group it87_group_pwm[3] = {
1392 { .attrs = it87_attributes_pwm[0] },
1393 { .attrs = it87_attributes_pwm[1] },
1394 { .attrs = it87_attributes_pwm[2] },
1395 };
1396
1397 static struct attribute *it87_attributes_autopwm[3][9+1] = { {
1398 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1399 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1400 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
1401 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
1402 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1403 &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
1404 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1405 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1406 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
1407 NULL
1408 }, {
1409 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1410 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1411 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
1412 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
1413 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1414 &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
1415 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1416 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1417 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
1418 NULL
1419 }, {
1420 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1421 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1422 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
1423 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
1424 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1425 &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
1426 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1427 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1428 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
1429 NULL
1430 } };
1431
1432 static const struct attribute_group it87_group_autopwm[3] = {
1433 { .attrs = it87_attributes_autopwm[0] },
1434 { .attrs = it87_attributes_autopwm[1] },
1435 { .attrs = it87_attributes_autopwm[2] },
1436 };
1437
1438 static struct attribute *it87_attributes_fan_beep[] = {
1439 &sensor_dev_attr_fan1_beep.dev_attr.attr,
1440 &sensor_dev_attr_fan2_beep.dev_attr.attr,
1441 &sensor_dev_attr_fan3_beep.dev_attr.attr,
1442 &sensor_dev_attr_fan4_beep.dev_attr.attr,
1443 &sensor_dev_attr_fan5_beep.dev_attr.attr,
1444 };
1445
1446 static struct attribute *it87_attributes_vid[] = {
1447 &dev_attr_vrm.attr,
1448 &dev_attr_cpu0_vid.attr,
1449 NULL
1450 };
1451
1452 static const struct attribute_group it87_group_vid = {
1453 .attrs = it87_attributes_vid,
1454 };
1455
1456 static struct attribute *it87_attributes_label[] = {
1457 &sensor_dev_attr_in3_label.dev_attr.attr,
1458 &sensor_dev_attr_in7_label.dev_attr.attr,
1459 &sensor_dev_attr_in8_label.dev_attr.attr,
1460 NULL
1461 };
1462
1463 static const struct attribute_group it87_group_label = {
1464 .attrs = it87_attributes_vid,
1465 };
1466
1467 /* SuperIO detection - will change isa_address if a chip is found */
1468 static int __init it87_find(unsigned short *address,
1469 struct it87_sio_data *sio_data)
1470 {
1471 int err = -ENODEV;
1472 u16 chip_type;
1473 const char *board_vendor, *board_name;
1474
1475 superio_enter();
1476 chip_type = force_id ? force_id : superio_inw(DEVID);
1477
1478 switch (chip_type) {
1479 case IT8705F_DEVID:
1480 sio_data->type = it87;
1481 break;
1482 case IT8712F_DEVID:
1483 sio_data->type = it8712;
1484 break;
1485 case IT8716F_DEVID:
1486 case IT8726F_DEVID:
1487 sio_data->type = it8716;
1488 break;
1489 case IT8718F_DEVID:
1490 sio_data->type = it8718;
1491 break;
1492 case IT8720F_DEVID:
1493 sio_data->type = it8720;
1494 break;
1495 case 0xffff: /* No device at all */
1496 goto exit;
1497 default:
1498 pr_debug(DRVNAME ": Unsupported chip (DEVID=0x%x)\n",
1499 chip_type);
1500 goto exit;
1501 }
1502
1503 superio_select(PME);
1504 if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
1505 pr_info("it87: Device not activated, skipping\n");
1506 goto exit;
1507 }
1508
1509 *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
1510 if (*address == 0) {
1511 pr_info("it87: Base address not set, skipping\n");
1512 goto exit;
1513 }
1514
1515 err = 0;
1516 sio_data->revision = superio_inb(DEVREV) & 0x0f;
1517 pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
1518 chip_type, *address, sio_data->revision);
1519
1520 /* in8 (Vbat) is always internal */
1521 sio_data->internal = (1 << 2);
1522
1523 /* Read GPIO config and VID value from LDN 7 (GPIO) */
1524 if (sio_data->type == it87) {
1525 /* The IT8705F doesn't have VID pins at all */
1526 sio_data->skip_vid = 1;
1527
1528 /* The IT8705F has a different LD number for GPIO */
1529 superio_select(5);
1530 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1531 } else {
1532 int reg;
1533
1534 superio_select(GPIO);
1535 /* We need at least 4 VID pins */
1536 reg = superio_inb(IT87_SIO_GPIO3_REG);
1537 if (reg & 0x0f) {
1538 pr_info("it87: VID is disabled (pins used for GPIO)\n");
1539 sio_data->skip_vid = 1;
1540 }
1541
1542 /* Check if fan3 is there or not */
1543 if (reg & (1 << 6))
1544 sio_data->skip_pwm |= (1 << 2);
1545 if (reg & (1 << 7))
1546 sio_data->skip_fan |= (1 << 2);
1547
1548 /* Check if fan2 is there or not */
1549 reg = superio_inb(IT87_SIO_GPIO5_REG);
1550 if (reg & (1 << 1))
1551 sio_data->skip_pwm |= (1 << 1);
1552 if (reg & (1 << 2))
1553 sio_data->skip_fan |= (1 << 1);
1554
1555 if ((sio_data->type == it8718 || sio_data->type == it8720)
1556 && !(sio_data->skip_vid))
1557 sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
1558
1559 reg = superio_inb(IT87_SIO_PINX2_REG);
1560 /*
1561 * The IT8720F has no VIN7 pin, so VCCH should always be
1562 * routed internally to VIN7 with an internal divider.
1563 * Curiously, there still is a configuration bit to control
1564 * this, which means it can be set incorrectly. And even
1565 * more curiously, many boards out there are improperly
1566 * configured, even though the IT8720F datasheet claims
1567 * that the internal routing of VCCH to VIN7 is the default
1568 * setting. So we force the internal routing in this case.
1569 */
1570 if (sio_data->type == it8720 && !(reg & (1 << 1))) {
1571 reg |= (1 << 1);
1572 superio_outb(IT87_SIO_PINX2_REG, reg);
1573 pr_notice("it87: Routing internal VCCH to in7\n");
1574 }
1575 if (reg & (1 << 0))
1576 sio_data->internal |= (1 << 0);
1577 if (reg & (1 << 1))
1578 sio_data->internal |= (1 << 1);
1579
1580 sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
1581 }
1582 if (sio_data->beep_pin)
1583 pr_info("it87: Beeping is supported\n");
1584
1585 /* Disable specific features based on DMI strings */
1586 board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1587 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1588 if (board_vendor && board_name) {
1589 if (strcmp(board_vendor, "nVIDIA") == 0
1590 && strcmp(board_name, "FN68PT") == 0) {
1591 /* On the Shuttle SN68PT, FAN_CTL2 is apparently not
1592 connected to a fan, but to something else. One user
1593 has reported instant system power-off when changing
1594 the PWM2 duty cycle, so we disable it.
1595 I use the board name string as the trigger in case
1596 the same board is ever used in other systems. */
1597 pr_info("it87: Disabling pwm2 due to "
1598 "hardware constraints\n");
1599 sio_data->skip_pwm = (1 << 1);
1600 }
1601 }
1602
1603 exit:
1604 superio_exit();
1605 return err;
1606 }
1607
1608 static void it87_remove_files(struct device *dev)
1609 {
1610 struct it87_data *data = platform_get_drvdata(pdev);
1611 struct it87_sio_data *sio_data = dev->platform_data;
1612 const struct attribute_group *fan_group = it87_get_fan_group(data);
1613 int i;
1614
1615 sysfs_remove_group(&dev->kobj, &it87_group);
1616 if (sio_data->beep_pin)
1617 sysfs_remove_group(&dev->kobj, &it87_group_beep);
1618 for (i = 0; i < 5; i++) {
1619 if (!(data->has_fan & (1 << i)))
1620 continue;
1621 sysfs_remove_group(&dev->kobj, &fan_group[i]);
1622 if (sio_data->beep_pin)
1623 sysfs_remove_file(&dev->kobj,
1624 it87_attributes_fan_beep[i]);
1625 }
1626 for (i = 0; i < 3; i++) {
1627 if (sio_data->skip_pwm & (1 << 0))
1628 continue;
1629 sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
1630 if (has_old_autopwm(data))
1631 sysfs_remove_group(&dev->kobj,
1632 &it87_group_autopwm[i]);
1633 }
1634 if (!sio_data->skip_vid)
1635 sysfs_remove_group(&dev->kobj, &it87_group_vid);
1636 sysfs_remove_group(&dev->kobj, &it87_group_label);
1637 }
1638
1639 static int __devinit it87_probe(struct platform_device *pdev)
1640 {
1641 struct it87_data *data;
1642 struct resource *res;
1643 struct device *dev = &pdev->dev;
1644 struct it87_sio_data *sio_data = dev->platform_data;
1645 const struct attribute_group *fan_group;
1646 int err = 0, i;
1647 int enable_pwm_interface;
1648 int fan_beep_need_rw;
1649 static const char *names[] = {
1650 "it87",
1651 "it8712",
1652 "it8716",
1653 "it8718",
1654 "it8720",
1655 };
1656
1657 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1658 if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
1659 dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
1660 (unsigned long)res->start,
1661 (unsigned long)(res->start + IT87_EC_EXTENT - 1));
1662 err = -EBUSY;
1663 goto ERROR0;
1664 }
1665
1666 data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
1667 if (!data) {
1668 err = -ENOMEM;
1669 goto ERROR1;
1670 }
1671
1672 data->addr = res->start;
1673 data->type = sio_data->type;
1674 data->revision = sio_data->revision;
1675 data->name = names[sio_data->type];
1676
1677 /* Now, we do the remaining detection. */
1678 if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
1679 || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
1680 err = -ENODEV;
1681 goto ERROR2;
1682 }
1683
1684 platform_set_drvdata(pdev, data);
1685
1686 mutex_init(&data->update_lock);
1687
1688 /* Check PWM configuration */
1689 enable_pwm_interface = it87_check_pwm(dev);
1690
1691 /* Initialize the IT87 chip */
1692 it87_init_device(pdev);
1693
1694 /* Register sysfs hooks */
1695 err = sysfs_create_group(&dev->kobj, &it87_group);
1696 if (err)
1697 goto ERROR2;
1698
1699 if (sio_data->beep_pin) {
1700 err = sysfs_create_group(&dev->kobj, &it87_group_beep);
1701 if (err)
1702 goto ERROR4;
1703 }
1704
1705 /* Do not create fan files for disabled fans */
1706 fan_group = it87_get_fan_group(data);
1707 fan_beep_need_rw = 1;
1708 for (i = 0; i < 5; i++) {
1709 if (!(data->has_fan & (1 << i)))
1710 continue;
1711 err = sysfs_create_group(&dev->kobj, &fan_group[i]);
1712 if (err)
1713 goto ERROR4;
1714
1715 if (sio_data->beep_pin) {
1716 err = sysfs_create_file(&dev->kobj,
1717 it87_attributes_fan_beep[i]);
1718 if (err)
1719 goto ERROR4;
1720 if (!fan_beep_need_rw)
1721 continue;
1722
1723 /* As we have a single beep enable bit for all fans,
1724 * only the first enabled fan has a writable attribute
1725 * for it. */
1726 if (sysfs_chmod_file(&dev->kobj,
1727 it87_attributes_fan_beep[i],
1728 S_IRUGO | S_IWUSR))
1729 dev_dbg(dev, "chmod +w fan%d_beep failed\n",
1730 i + 1);
1731 fan_beep_need_rw = 0;
1732 }
1733 }
1734
1735 if (enable_pwm_interface) {
1736 for (i = 0; i < 3; i++) {
1737 if (sio_data->skip_pwm & (1 << i))
1738 continue;
1739 err = sysfs_create_group(&dev->kobj,
1740 &it87_group_pwm[i]);
1741 if (err)
1742 goto ERROR4;
1743
1744 if (!has_old_autopwm(data))
1745 continue;
1746 err = sysfs_create_group(&dev->kobj,
1747 &it87_group_autopwm[i]);
1748 if (err)
1749 goto ERROR4;
1750 }
1751 }
1752
1753 if (!sio_data->skip_vid) {
1754 data->vrm = vid_which_vrm();
1755 /* VID reading from Super-I/O config space if available */
1756 data->vid = sio_data->vid_value;
1757 err = sysfs_create_group(&dev->kobj, &it87_group_vid);
1758 if (err)
1759 goto ERROR4;
1760 }
1761
1762 /* Export labels for internal sensors */
1763 for (i = 0; i < 3; i++) {
1764 if (!(sio_data->internal & (1 << i)))
1765 continue;
1766 err = sysfs_create_file(&dev->kobj,
1767 it87_attributes_label[i]);
1768 if (err)
1769 goto ERROR4;
1770 }
1771
1772 data->hwmon_dev = hwmon_device_register(dev);
1773 if (IS_ERR(data->hwmon_dev)) {
1774 err = PTR_ERR(data->hwmon_dev);
1775 goto ERROR4;
1776 }
1777
1778 return 0;
1779
1780 ERROR4:
1781 it87_remove_files(dev);
1782 ERROR2:
1783 platform_set_drvdata(pdev, NULL);
1784 kfree(data);
1785 ERROR1:
1786 release_region(res->start, IT87_EC_EXTENT);
1787 ERROR0:
1788 return err;
1789 }
1790
1791 static int __devexit it87_remove(struct platform_device *pdev)
1792 {
1793 struct it87_data *data = platform_get_drvdata(pdev);
1794
1795 hwmon_device_unregister(data->hwmon_dev);
1796 it87_remove_files(&pdev->dev);
1797
1798 release_region(data->addr, IT87_EC_EXTENT);
1799 platform_set_drvdata(pdev, NULL);
1800 kfree(data);
1801
1802 return 0;
1803 }
1804
1805 /* Must be called with data->update_lock held, except during initialization.
1806 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1807 would slow down the IT87 access and should not be necessary. */
1808 static int it87_read_value(struct it87_data *data, u8 reg)
1809 {
1810 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1811 return inb_p(data->addr + IT87_DATA_REG_OFFSET);
1812 }
1813
1814 /* Must be called with data->update_lock held, except during initialization.
1815 We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
1816 would slow down the IT87 access and should not be necessary. */
1817 static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
1818 {
1819 outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
1820 outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
1821 }
1822
1823 /* Return 1 if and only if the PWM interface is safe to use */
1824 static int __devinit it87_check_pwm(struct device *dev)
1825 {
1826 struct it87_data *data = dev_get_drvdata(dev);
1827 /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
1828 * and polarity set to active low is sign that this is the case so we
1829 * disable pwm control to protect the user. */
1830 int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
1831 if ((tmp & 0x87) == 0) {
1832 if (fix_pwm_polarity) {
1833 /* The user asks us to attempt a chip reconfiguration.
1834 * This means switching to active high polarity and
1835 * inverting all fan speed values. */
1836 int i;
1837 u8 pwm[3];
1838
1839 for (i = 0; i < 3; i++)
1840 pwm[i] = it87_read_value(data,
1841 IT87_REG_PWM(i));
1842
1843 /* If any fan is in automatic pwm mode, the polarity
1844 * might be correct, as suspicious as it seems, so we
1845 * better don't change anything (but still disable the
1846 * PWM interface). */
1847 if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
1848 dev_info(dev, "Reconfiguring PWM to "
1849 "active high polarity\n");
1850 it87_write_value(data, IT87_REG_FAN_CTL,
1851 tmp | 0x87);
1852 for (i = 0; i < 3; i++)
1853 it87_write_value(data,
1854 IT87_REG_PWM(i),
1855 0x7f & ~pwm[i]);
1856 return 1;
1857 }
1858
1859 dev_info(dev, "PWM configuration is "
1860 "too broken to be fixed\n");
1861 }
1862
1863 dev_info(dev, "Detected broken BIOS "
1864 "defaults, disabling PWM interface\n");
1865 return 0;
1866 } else if (fix_pwm_polarity) {
1867 dev_info(dev, "PWM configuration looks "
1868 "sane, won't touch\n");
1869 }
1870
1871 return 1;
1872 }
1873
1874 /* Called when we have found a new IT87. */
1875 static void __devinit it87_init_device(struct platform_device *pdev)
1876 {
1877 struct it87_sio_data *sio_data = pdev->dev.platform_data;
1878 struct it87_data *data = platform_get_drvdata(pdev);
1879 int tmp, i;
1880 u8 mask;
1881
1882 /* For each PWM channel:
1883 * - If it is in automatic mode, setting to manual mode should set
1884 * the fan to full speed by default.
1885 * - If it is in manual mode, we need a mapping to temperature
1886 * channels to use when later setting to automatic mode later.
1887 * Use a 1:1 mapping by default (we are clueless.)
1888 * In both cases, the value can (and should) be changed by the user
1889 * prior to switching to a different mode. */
1890 for (i = 0; i < 3; i++) {
1891 data->pwm_temp_map[i] = i;
1892 data->pwm_duty[i] = 0x7f; /* Full speed */
1893 data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
1894 }
1895
1896 /* Some chips seem to have default value 0xff for all limit
1897 * registers. For low voltage limits it makes no sense and triggers
1898 * alarms, so change to 0 instead. For high temperature limits, it
1899 * means -1 degree C, which surprisingly doesn't trigger an alarm,
1900 * but is still confusing, so change to 127 degrees C. */
1901 for (i = 0; i < 8; i++) {
1902 tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
1903 if (tmp == 0xff)
1904 it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
1905 }
1906 for (i = 0; i < 3; i++) {
1907 tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
1908 if (tmp == 0xff)
1909 it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
1910 }
1911
1912 /* Temperature channels are not forcibly enabled, as they can be
1913 * set to two different sensor types and we can't guess which one
1914 * is correct for a given system. These channels can be enabled at
1915 * run-time through the temp{1-3}_type sysfs accessors if needed. */
1916
1917 /* Check if voltage monitors are reset manually or by some reason */
1918 tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
1919 if ((tmp & 0xff) == 0) {
1920 /* Enable all voltage monitors */
1921 it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
1922 }
1923
1924 /* Check if tachometers are reset manually or by some reason */
1925 mask = 0x70 & ~(sio_data->skip_fan << 4);
1926 data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
1927 if ((data->fan_main_ctrl & mask) == 0) {
1928 /* Enable all fan tachometers */
1929 data->fan_main_ctrl |= mask;
1930 it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
1931 data->fan_main_ctrl);
1932 }
1933 data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
1934
1935 /* Set tachometers to 16-bit mode if needed */
1936 if (has_16bit_fans(data)) {
1937 tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
1938 if (~tmp & 0x07 & data->has_fan) {
1939 dev_dbg(&pdev->dev,
1940 "Setting fan1-3 to 16-bit mode\n");
1941 it87_write_value(data, IT87_REG_FAN_16BIT,
1942 tmp | 0x07);
1943 }
1944 /* IT8705F only supports three fans. */
1945 if (data->type != it87) {
1946 if (tmp & (1 << 4))
1947 data->has_fan |= (1 << 3); /* fan4 enabled */
1948 if (tmp & (1 << 5))
1949 data->has_fan |= (1 << 4); /* fan5 enabled */
1950 }
1951 }
1952
1953 /* Fan input pins may be used for alternative functions */
1954 data->has_fan &= ~sio_data->skip_fan;
1955
1956 /* Start monitoring */
1957 it87_write_value(data, IT87_REG_CONFIG,
1958 (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
1959 | (update_vbat ? 0x41 : 0x01));
1960 }
1961
1962 static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
1963 {
1964 data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
1965 if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
1966 data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
1967 else /* Manual mode */
1968 data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
1969
1970 if (has_old_autopwm(data)) {
1971 int i;
1972
1973 for (i = 0; i < 5 ; i++)
1974 data->auto_temp[nr][i] = it87_read_value(data,
1975 IT87_REG_AUTO_TEMP(nr, i));
1976 for (i = 0; i < 3 ; i++)
1977 data->auto_pwm[nr][i] = it87_read_value(data,
1978 IT87_REG_AUTO_PWM(nr, i));
1979 }
1980 }
1981
1982 static struct it87_data *it87_update_device(struct device *dev)
1983 {
1984 struct it87_data *data = dev_get_drvdata(dev);
1985 int i;
1986
1987 mutex_lock(&data->update_lock);
1988
1989 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
1990 || !data->valid) {
1991 if (update_vbat) {
1992 /* Cleared after each update, so reenable. Value
1993 returned by this read will be previous value */
1994 it87_write_value(data, IT87_REG_CONFIG,
1995 it87_read_value(data, IT87_REG_CONFIG) | 0x40);
1996 }
1997 for (i = 0; i <= 7; i++) {
1998 data->in[i] =
1999 it87_read_value(data, IT87_REG_VIN(i));
2000 data->in_min[i] =
2001 it87_read_value(data, IT87_REG_VIN_MIN(i));
2002 data->in_max[i] =
2003 it87_read_value(data, IT87_REG_VIN_MAX(i));
2004 }
2005 /* in8 (battery) has no limit registers */
2006 data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
2007
2008 for (i = 0; i < 5; i++) {
2009 /* Skip disabled fans */
2010 if (!(data->has_fan & (1 << i)))
2011 continue;
2012
2013 data->fan_min[i] =
2014 it87_read_value(data, IT87_REG_FAN_MIN[i]);
2015 data->fan[i] = it87_read_value(data,
2016 IT87_REG_FAN[i]);
2017 /* Add high byte if in 16-bit mode */
2018 if (has_16bit_fans(data)) {
2019 data->fan[i] |= it87_read_value(data,
2020 IT87_REG_FANX[i]) << 8;
2021 data->fan_min[i] |= it87_read_value(data,
2022 IT87_REG_FANX_MIN[i]) << 8;
2023 }
2024 }
2025 for (i = 0; i < 3; i++) {
2026 data->temp[i] =
2027 it87_read_value(data, IT87_REG_TEMP(i));
2028 data->temp_high[i] =
2029 it87_read_value(data, IT87_REG_TEMP_HIGH(i));
2030 data->temp_low[i] =
2031 it87_read_value(data, IT87_REG_TEMP_LOW(i));
2032 }
2033
2034 /* Newer chips don't have clock dividers */
2035 if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
2036 i = it87_read_value(data, IT87_REG_FAN_DIV);
2037 data->fan_div[0] = i & 0x07;
2038 data->fan_div[1] = (i >> 3) & 0x07;
2039 data->fan_div[2] = (i & 0x40) ? 3 : 1;
2040 }
2041
2042 data->alarms =
2043 it87_read_value(data, IT87_REG_ALARM1) |
2044 (it87_read_value(data, IT87_REG_ALARM2) << 8) |
2045 (it87_read_value(data, IT87_REG_ALARM3) << 16);
2046 data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
2047
2048 data->fan_main_ctrl = it87_read_value(data,
2049 IT87_REG_FAN_MAIN_CTRL);
2050 data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
2051 for (i = 0; i < 3; i++)
2052 it87_update_pwm_ctrl(data, i);
2053
2054 data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
2055 /* The 8705 does not have VID capability.
2056 The 8718 and the 8720 don't use IT87_REG_VID for the
2057 same purpose. */
2058 if (data->type == it8712 || data->type == it8716) {
2059 data->vid = it87_read_value(data, IT87_REG_VID);
2060 /* The older IT8712F revisions had only 5 VID pins,
2061 but we assume it is always safe to read 6 bits. */
2062 data->vid &= 0x3f;
2063 }
2064 data->last_updated = jiffies;
2065 data->valid = 1;
2066 }
2067
2068 mutex_unlock(&data->update_lock);
2069
2070 return data;
2071 }
2072
2073 static int __init it87_device_add(unsigned short address,
2074 const struct it87_sio_data *sio_data)
2075 {
2076 struct resource res = {
2077 .start = address + IT87_EC_OFFSET,
2078 .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
2079 .name = DRVNAME,
2080 .flags = IORESOURCE_IO,
2081 };
2082 int err;
2083
2084 err = acpi_check_resource_conflict(&res);
2085 if (err)
2086 goto exit;
2087
2088 pdev = platform_device_alloc(DRVNAME, address);
2089 if (!pdev) {
2090 err = -ENOMEM;
2091 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
2092 goto exit;
2093 }
2094
2095 err = platform_device_add_resources(pdev, &res, 1);
2096 if (err) {
2097 printk(KERN_ERR DRVNAME ": Device resource addition failed "
2098 "(%d)\n", err);
2099 goto exit_device_put;
2100 }
2101
2102 err = platform_device_add_data(pdev, sio_data,
2103 sizeof(struct it87_sio_data));
2104 if (err) {
2105 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
2106 goto exit_device_put;
2107 }
2108
2109 err = platform_device_add(pdev);
2110 if (err) {
2111 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
2112 err);
2113 goto exit_device_put;
2114 }
2115
2116 return 0;
2117
2118 exit_device_put:
2119 platform_device_put(pdev);
2120 exit:
2121 return err;
2122 }
2123
2124 static int __init sm_it87_init(void)
2125 {
2126 int err;
2127 unsigned short isa_address = 0;
2128 struct it87_sio_data sio_data;
2129
2130 memset(&sio_data, 0, sizeof(struct it87_sio_data));
2131 err = it87_find(&isa_address, &sio_data);
2132 if (err)
2133 return err;
2134 err = platform_driver_register(&it87_driver);
2135 if (err)
2136 return err;
2137
2138 err = it87_device_add(isa_address, &sio_data);
2139 if (err) {
2140 platform_driver_unregister(&it87_driver);
2141 return err;
2142 }
2143
2144 return 0;
2145 }
2146
2147 static void __exit sm_it87_exit(void)
2148 {
2149 platform_device_unregister(pdev);
2150 platform_driver_unregister(&it87_driver);
2151 }
2152
2153
2154 MODULE_AUTHOR("Chris Gauthron, "
2155 "Jean Delvare <khali@linux-fr.org>");
2156 MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8720F/8726F, SiS950 driver");
2157 module_param(update_vbat, bool, 0);
2158 MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
2159 module_param(fix_pwm_polarity, bool, 0);
2160 MODULE_PARM_DESC(fix_pwm_polarity,
2161 "Force PWM polarity to active high (DANGEROUS)");
2162 MODULE_LICENSE("GPL");
2163
2164 module_init(sm_it87_init);
2165 module_exit(sm_it87_exit);