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