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