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