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