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