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