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