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