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