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