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