]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/hwmon/pc87427.c
hwmon: (pc87427) Add temperature monitoring support
[net-next-2.6.git] / drivers / hwmon / pc87427.c
1 /*
2  *  pc87427.c - hardware monitoring driver for the
3  *              National Semiconductor PC87427 Super-I/O chip
4  *  Copyright (C) 2006, 2008, 2010  Jean Delvare <khali@linux-fr.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  Supports the following chips:
16  *
17  *  Chip        #vin    #fan    #pwm    #temp   devid
18  *  PC87427     -       8       4       6       0xF2
19  *
20  *  This driver assumes that no more than one chip is present.
21  *  Only fans are fully supported so far. Temperatures are in read-only
22  *  mode, and voltages aren't supported at all.
23  */
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/platform_device.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <linux/sysfs.h>
35 #include <linux/ioport.h>
36 #include <linux/acpi.h>
37 #include <linux/io.h>
38
39 static unsigned short force_id;
40 module_param(force_id, ushort, 0);
41 MODULE_PARM_DESC(force_id, "Override the detected device ID");
42
43 static struct platform_device *pdev;
44
45 #define DRVNAME "pc87427"
46
47 /* The lock mutex protects both the I/O accesses (needed because the
48    device is using banked registers) and the register cache (needed to keep
49    the data in the registers and the cache in sync at any time). */
50 struct pc87427_data {
51         struct device *hwmon_dev;
52         struct mutex lock;
53         int address[2];
54         const char *name;
55
56         unsigned long last_updated;     /* in jiffies */
57         u8 fan_enabled;                 /* bit vector */
58         u16 fan[8];                     /* register values */
59         u16 fan_min[8];                 /* register values */
60         u8 fan_status[8];               /* register values */
61
62         u8 pwm_enabled;                 /* bit vector */
63         u8 pwm_auto_ok;                 /* bit vector */
64         u8 pwm_enable[4];               /* register values */
65         u8 pwm[4];                      /* register values */
66
67         u8 temp_enabled;                /* bit vector */
68         s16 temp[6];                    /* register values */
69         s8 temp_min[6];                 /* register values */
70         s8 temp_max[6];                 /* register values */
71         s8 temp_crit[6];                /* register values */
72         u8 temp_status[6];              /* register values */
73         u8 temp_type[6];                /* register values */
74 };
75
76 struct pc87427_sio_data {
77         unsigned short address[2];
78         u8 has_fanin;
79         u8 has_fanout;
80 };
81
82 /*
83  * Super-I/O registers and operations
84  */
85
86 #define SIOREG_LDSEL    0x07    /* Logical device select */
87 #define SIOREG_DEVID    0x20    /* Device ID */
88 #define SIOREG_CF2      0x22    /* Configuration 2 */
89 #define SIOREG_CF3      0x23    /* Configuration 3 */
90 #define SIOREG_CF4      0x24    /* Configuration 4 */
91 #define SIOREG_CF5      0x25    /* Configuration 5 */
92 #define SIOREG_CFB      0x2B    /* Configuration B */
93 #define SIOREG_CFC      0x2C    /* Configuration C */
94 #define SIOREG_CFD      0x2D    /* Configuration D */
95 #define SIOREG_ACT      0x30    /* Device activation */
96 #define SIOREG_MAP      0x50    /* I/O or memory mapping */
97 #define SIOREG_IOBASE   0x60    /* I/O base address */
98
99 static const u8 logdev[2] = { 0x09, 0x14 };
100 static const char *logdev_str[2] = { DRVNAME " FMC", DRVNAME " HMC" };
101 #define LD_FAN          0
102 #define LD_IN           1
103 #define LD_TEMP         1
104
105 static inline void superio_outb(int sioaddr, int reg, int val)
106 {
107         outb(reg, sioaddr);
108         outb(val, sioaddr + 1);
109 }
110
111 static inline int superio_inb(int sioaddr, int reg)
112 {
113         outb(reg, sioaddr);
114         return inb(sioaddr + 1);
115 }
116
117 static inline void superio_exit(int sioaddr)
118 {
119         outb(0x02, sioaddr);
120         outb(0x02, sioaddr + 1);
121 }
122
123 /*
124  * Logical devices
125  */
126
127 #define REGION_LENGTH           32
128 #define PC87427_REG_BANK        0x0f
129 #define BANK_FM(nr)             (nr)
130 #define BANK_FT(nr)             (0x08 + (nr))
131 #define BANK_FC(nr)             (0x10 + (nr) * 2)
132 #define BANK_TM(nr)             (nr)
133 #define BANK_VM(nr)             (0x08 + (nr))
134
135 /*
136  * I/O access functions
137  */
138
139 /* ldi is the logical device index */
140 static inline int pc87427_read8(struct pc87427_data *data, u8 ldi, u8 reg)
141 {
142         return inb(data->address[ldi] + reg);
143 }
144
145 /* Must be called with data->lock held, except during init */
146 static inline int pc87427_read8_bank(struct pc87427_data *data, u8 ldi,
147                                      u8 bank, u8 reg)
148 {
149         outb(bank, data->address[ldi] + PC87427_REG_BANK);
150         return inb(data->address[ldi] + reg);
151 }
152
153 /* Must be called with data->lock held, except during init */
154 static inline void pc87427_write8_bank(struct pc87427_data *data, u8 ldi,
155                                        u8 bank, u8 reg, u8 value)
156 {
157         outb(bank, data->address[ldi] + PC87427_REG_BANK);
158         outb(value, data->address[ldi] + reg);
159 }
160
161 /*
162  * Fan registers and conversions
163  */
164
165 /* fan data registers are 16-bit wide */
166 #define PC87427_REG_FAN                 0x12
167 #define PC87427_REG_FAN_MIN             0x14
168 #define PC87427_REG_FAN_STATUS          0x10
169
170 #define FAN_STATUS_STALL                (1 << 3)
171 #define FAN_STATUS_LOSPD                (1 << 1)
172 #define FAN_STATUS_MONEN                (1 << 0)
173
174 /* Dedicated function to read all registers related to a given fan input.
175    This saves us quite a few locks and bank selections.
176    Must be called with data->lock held.
177    nr is from 0 to 7 */
178 static void pc87427_readall_fan(struct pc87427_data *data, u8 nr)
179 {
180         int iobase = data->address[LD_FAN];
181
182         outb(BANK_FM(nr), iobase + PC87427_REG_BANK);
183         data->fan[nr] = inw(iobase + PC87427_REG_FAN);
184         data->fan_min[nr] = inw(iobase + PC87427_REG_FAN_MIN);
185         data->fan_status[nr] = inb(iobase + PC87427_REG_FAN_STATUS);
186         /* Clear fan alarm bits */
187         outb(data->fan_status[nr], iobase + PC87427_REG_FAN_STATUS);
188 }
189
190 /* The 2 LSB of fan speed registers are used for something different.
191    The actual 2 LSB of the measurements are not available. */
192 static inline unsigned long fan_from_reg(u16 reg)
193 {
194         reg &= 0xfffc;
195         if (reg == 0x0000 || reg == 0xfffc)
196                 return 0;
197         return 5400000UL / reg;
198 }
199
200 /* The 2 LSB of the fan speed limit registers are not significant. */
201 static inline u16 fan_to_reg(unsigned long val)
202 {
203         if (val < 83UL)
204                 return 0xffff;
205         if (val >= 1350000UL)
206                 return 0x0004;
207         return ((1350000UL + val / 2) / val) << 2;
208 }
209
210 /*
211  * PWM registers and conversions
212  */
213
214 #define PC87427_REG_PWM_ENABLE          0x10
215 #define PC87427_REG_PWM_DUTY            0x12
216
217 #define PWM_ENABLE_MODE_MASK            (7 << 4)
218 #define PWM_ENABLE_CTLEN                (1 << 0)
219
220 #define PWM_MODE_MANUAL                 (0 << 4)
221 #define PWM_MODE_AUTO                   (1 << 4)
222 #define PWM_MODE_OFF                    (2 << 4)
223 #define PWM_MODE_ON                     (7 << 4)
224
225 /* Dedicated function to read all registers related to a given PWM output.
226    This saves us quite a few locks and bank selections.
227    Must be called with data->lock held.
228    nr is from 0 to 3 */
229 static void pc87427_readall_pwm(struct pc87427_data *data, u8 nr)
230 {
231         int iobase = data->address[LD_FAN];
232
233         outb(BANK_FC(nr), iobase + PC87427_REG_BANK);
234         data->pwm_enable[nr] = inb(iobase + PC87427_REG_PWM_ENABLE);
235         data->pwm[nr] = inb(iobase + PC87427_REG_PWM_DUTY);
236 }
237
238 static inline int pwm_enable_from_reg(u8 reg)
239 {
240         switch (reg & PWM_ENABLE_MODE_MASK) {
241         case PWM_MODE_ON:
242                 return 0;
243         case PWM_MODE_MANUAL:
244         case PWM_MODE_OFF:
245                 return 1;
246         case PWM_MODE_AUTO:
247                 return 2;
248         default:
249                 return -EPROTO;
250         }
251 }
252
253 static inline u8 pwm_enable_to_reg(unsigned long val, u8 pwmval)
254 {
255         switch (val) {
256         default:
257                 return PWM_MODE_ON;
258         case 1:
259                 return pwmval ? PWM_MODE_MANUAL : PWM_MODE_OFF;
260         case 2:
261                 return PWM_MODE_AUTO;
262         }
263 }
264
265 /*
266  * Temperature registers and conversions
267  */
268
269 #define PC87427_REG_TEMP_STATUS         0x10
270 #define PC87427_REG_TEMP                0x14
271 #define PC87427_REG_TEMP_MAX            0x18
272 #define PC87427_REG_TEMP_MIN            0x19
273 #define PC87427_REG_TEMP_CRIT           0x1a
274 #define PC87427_REG_TEMP_TYPE           0x1d
275
276 #define TEMP_STATUS_CHANEN              (1 << 0)
277 #define TEMP_STATUS_LOWFLG              (1 << 1)
278 #define TEMP_STATUS_HIGHFLG             (1 << 2)
279 #define TEMP_STATUS_CRITFLG             (1 << 3)
280 #define TEMP_STATUS_SENSERR             (1 << 5)
281 #define TEMP_TYPE_MASK                  (3 << 5)
282
283 #define TEMP_TYPE_THERMISTOR            (1 << 5)
284 #define TEMP_TYPE_REMOTE_DIODE          (2 << 5)
285 #define TEMP_TYPE_LOCAL_DIODE           (3 << 5)
286
287 /* Dedicated function to read all registers related to a given temperature
288    input. This saves us quite a few locks and bank selections.
289    Must be called with data->lock held.
290    nr is from 0 to 5 */
291 static void pc87427_readall_temp(struct pc87427_data *data, u8 nr)
292 {
293         int iobase = data->address[LD_TEMP];
294
295         outb(BANK_TM(nr), iobase + PC87427_REG_BANK);
296         data->temp[nr] = le16_to_cpu(inw(iobase + PC87427_REG_TEMP));
297         data->temp_max[nr] = inb(iobase + PC87427_REG_TEMP_MAX);
298         data->temp_min[nr] = inb(iobase + PC87427_REG_TEMP_MIN);
299         data->temp_crit[nr] = inb(iobase + PC87427_REG_TEMP_CRIT);
300         data->temp_type[nr] = inb(iobase + PC87427_REG_TEMP_TYPE);
301         data->temp_status[nr] = inb(iobase + PC87427_REG_TEMP_STATUS);
302         /* Clear fan alarm bits */
303         outb(data->temp_status[nr], iobase + PC87427_REG_TEMP_STATUS);
304 }
305
306 static inline unsigned int temp_type_from_reg(u8 reg)
307 {
308         switch (reg & TEMP_TYPE_MASK) {
309         case TEMP_TYPE_THERMISTOR:
310                 return 4;
311         case TEMP_TYPE_REMOTE_DIODE:
312         case TEMP_TYPE_LOCAL_DIODE:
313                 return 3;
314         default:
315                 return 0;
316         }
317 }
318
319 /* We assume 8-bit thermal sensors; 9-bit thermal sensors are possible
320    too, but I have no idea how to figure out when they are used. */
321 static inline long temp_from_reg(s16 reg)
322 {
323         return reg * 1000 / 256;
324 }
325
326 static inline long temp_from_reg8(s8 reg)
327 {
328         return reg * 1000;
329 }
330
331 /*
332  * Data interface
333  */
334
335 static struct pc87427_data *pc87427_update_device(struct device *dev)
336 {
337         struct pc87427_data *data = dev_get_drvdata(dev);
338         int i;
339
340         mutex_lock(&data->lock);
341         if (!time_after(jiffies, data->last_updated + HZ)
342          && data->last_updated)
343                 goto done;
344
345         /* Fans */
346         for (i = 0; i < 8; i++) {
347                 if (!(data->fan_enabled & (1 << i)))
348                         continue;
349                 pc87427_readall_fan(data, i);
350         }
351
352         /* PWM outputs */
353         for (i = 0; i < 4; i++) {
354                 if (!(data->pwm_enabled & (1 << i)))
355                         continue;
356                 pc87427_readall_pwm(data, i);
357         }
358
359         /* Temperature channels */
360         for (i = 0; i < 6; i++) {
361                 if (!(data->temp_enabled & (1 << i)))
362                         continue;
363                 pc87427_readall_temp(data, i);
364         }
365
366         data->last_updated = jiffies;
367
368 done:
369         mutex_unlock(&data->lock);
370         return data;
371 }
372
373 static ssize_t show_fan_input(struct device *dev, struct device_attribute
374                               *devattr, char *buf)
375 {
376         struct pc87427_data *data = pc87427_update_device(dev);
377         int nr = to_sensor_dev_attr(devattr)->index;
378
379         return sprintf(buf, "%lu\n", fan_from_reg(data->fan[nr]));
380 }
381
382 static ssize_t show_fan_min(struct device *dev, struct device_attribute
383                             *devattr, char *buf)
384 {
385         struct pc87427_data *data = pc87427_update_device(dev);
386         int nr = to_sensor_dev_attr(devattr)->index;
387
388         return sprintf(buf, "%lu\n", fan_from_reg(data->fan_min[nr]));
389 }
390
391 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
392                               *devattr, char *buf)
393 {
394         struct pc87427_data *data = pc87427_update_device(dev);
395         int nr = to_sensor_dev_attr(devattr)->index;
396
397         return sprintf(buf, "%d\n", !!(data->fan_status[nr]
398                                        & FAN_STATUS_LOSPD));
399 }
400
401 static ssize_t show_fan_fault(struct device *dev, struct device_attribute
402                               *devattr, char *buf)
403 {
404         struct pc87427_data *data = pc87427_update_device(dev);
405         int nr = to_sensor_dev_attr(devattr)->index;
406
407         return sprintf(buf, "%d\n", !!(data->fan_status[nr]
408                                        & FAN_STATUS_STALL));
409 }
410
411 static ssize_t set_fan_min(struct device *dev, struct device_attribute
412                            *devattr, const char *buf, size_t count)
413 {
414         struct pc87427_data *data = dev_get_drvdata(dev);
415         int nr = to_sensor_dev_attr(devattr)->index;
416         unsigned long val;
417         int iobase = data->address[LD_FAN];
418
419         if (strict_strtoul(buf, 10, &val) < 0)
420                 return -EINVAL;
421
422         mutex_lock(&data->lock);
423         outb(BANK_FM(nr), iobase + PC87427_REG_BANK);
424         /* The low speed limit registers are read-only while monitoring
425            is enabled, so we have to disable monitoring, then change the
426            limit, and finally enable monitoring again. */
427         outb(0, iobase + PC87427_REG_FAN_STATUS);
428         data->fan_min[nr] = fan_to_reg(val);
429         outw(data->fan_min[nr], iobase + PC87427_REG_FAN_MIN);
430         outb(FAN_STATUS_MONEN, iobase + PC87427_REG_FAN_STATUS);
431         mutex_unlock(&data->lock);
432
433         return count;
434 }
435
436 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
437 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
438 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2);
439 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_input, NULL, 3);
440 static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan_input, NULL, 4);
441 static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan_input, NULL, 5);
442 static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan_input, NULL, 6);
443 static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan_input, NULL, 7);
444
445 static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
446                           show_fan_min, set_fan_min, 0);
447 static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
448                           show_fan_min, set_fan_min, 1);
449 static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO,
450                           show_fan_min, set_fan_min, 2);
451 static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO,
452                           show_fan_min, set_fan_min, 3);
453 static SENSOR_DEVICE_ATTR(fan5_min, S_IWUSR | S_IRUGO,
454                           show_fan_min, set_fan_min, 4);
455 static SENSOR_DEVICE_ATTR(fan6_min, S_IWUSR | S_IRUGO,
456                           show_fan_min, set_fan_min, 5);
457 static SENSOR_DEVICE_ATTR(fan7_min, S_IWUSR | S_IRUGO,
458                           show_fan_min, set_fan_min, 6);
459 static SENSOR_DEVICE_ATTR(fan8_min, S_IWUSR | S_IRUGO,
460                           show_fan_min, set_fan_min, 7);
461
462 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0);
463 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 1);
464 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 2);
465 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 3);
466 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_fan_alarm, NULL, 4);
467 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_fan_alarm, NULL, 5);
468 static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_fan_alarm, NULL, 6);
469 static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_fan_alarm, NULL, 7);
470
471 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL, 0);
472 static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_fan_fault, NULL, 1);
473 static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, show_fan_fault, NULL, 2);
474 static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, show_fan_fault, NULL, 3);
475 static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, show_fan_fault, NULL, 4);
476 static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, show_fan_fault, NULL, 5);
477 static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, show_fan_fault, NULL, 6);
478 static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, show_fan_fault, NULL, 7);
479
480 static struct attribute *pc87427_attributes_fan[8][5] = {
481         {
482                 &sensor_dev_attr_fan1_input.dev_attr.attr,
483                 &sensor_dev_attr_fan1_min.dev_attr.attr,
484                 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
485                 &sensor_dev_attr_fan1_fault.dev_attr.attr,
486                 NULL
487         }, {
488                 &sensor_dev_attr_fan2_input.dev_attr.attr,
489                 &sensor_dev_attr_fan2_min.dev_attr.attr,
490                 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
491                 &sensor_dev_attr_fan2_fault.dev_attr.attr,
492                 NULL
493         }, {
494                 &sensor_dev_attr_fan3_input.dev_attr.attr,
495                 &sensor_dev_attr_fan3_min.dev_attr.attr,
496                 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
497                 &sensor_dev_attr_fan3_fault.dev_attr.attr,
498                 NULL
499         }, {
500                 &sensor_dev_attr_fan4_input.dev_attr.attr,
501                 &sensor_dev_attr_fan4_min.dev_attr.attr,
502                 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
503                 &sensor_dev_attr_fan4_fault.dev_attr.attr,
504                 NULL
505         }, {
506                 &sensor_dev_attr_fan5_input.dev_attr.attr,
507                 &sensor_dev_attr_fan5_min.dev_attr.attr,
508                 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
509                 &sensor_dev_attr_fan5_fault.dev_attr.attr,
510                 NULL
511         }, {
512                 &sensor_dev_attr_fan6_input.dev_attr.attr,
513                 &sensor_dev_attr_fan6_min.dev_attr.attr,
514                 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
515                 &sensor_dev_attr_fan6_fault.dev_attr.attr,
516                 NULL
517         }, {
518                 &sensor_dev_attr_fan7_input.dev_attr.attr,
519                 &sensor_dev_attr_fan7_min.dev_attr.attr,
520                 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
521                 &sensor_dev_attr_fan7_fault.dev_attr.attr,
522                 NULL
523         }, {
524                 &sensor_dev_attr_fan8_input.dev_attr.attr,
525                 &sensor_dev_attr_fan8_min.dev_attr.attr,
526                 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
527                 &sensor_dev_attr_fan8_fault.dev_attr.attr,
528                 NULL
529         }
530 };
531
532 static const struct attribute_group pc87427_group_fan[8] = {
533         { .attrs = pc87427_attributes_fan[0] },
534         { .attrs = pc87427_attributes_fan[1] },
535         { .attrs = pc87427_attributes_fan[2] },
536         { .attrs = pc87427_attributes_fan[3] },
537         { .attrs = pc87427_attributes_fan[4] },
538         { .attrs = pc87427_attributes_fan[5] },
539         { .attrs = pc87427_attributes_fan[6] },
540         { .attrs = pc87427_attributes_fan[7] },
541 };
542
543 /* Must be called with data->lock held and pc87427_readall_pwm() freshly
544    called */
545 static void update_pwm_enable(struct pc87427_data *data, int nr, u8 mode)
546 {
547         int iobase = data->address[LD_FAN];
548         data->pwm_enable[nr] &= ~PWM_ENABLE_MODE_MASK;
549         data->pwm_enable[nr] |= mode;
550         outb(data->pwm_enable[nr], iobase + PC87427_REG_PWM_ENABLE);
551 }
552
553 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
554                                *devattr, char *buf)
555 {
556         struct pc87427_data *data = pc87427_update_device(dev);
557         int nr = to_sensor_dev_attr(devattr)->index;
558         int pwm_enable;
559
560         pwm_enable = pwm_enable_from_reg(data->pwm_enable[nr]);
561         if (pwm_enable < 0)
562                 return pwm_enable;
563         return sprintf(buf, "%d\n", pwm_enable);
564 }
565
566 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
567                               *devattr, const char *buf, size_t count)
568 {
569         struct pc87427_data *data = dev_get_drvdata(dev);
570         int nr = to_sensor_dev_attr(devattr)->index;
571         unsigned long val;
572
573         if (strict_strtoul(buf, 10, &val) < 0 || val > 2)
574                 return -EINVAL;
575         /* Can't go to automatic mode if it isn't configured */
576         if (val == 2 && !(data->pwm_auto_ok & (1 << nr)))
577                 return -EINVAL;
578
579         mutex_lock(&data->lock);
580         pc87427_readall_pwm(data, nr);
581         update_pwm_enable(data, nr, pwm_enable_to_reg(val, data->pwm[nr]));
582         mutex_unlock(&data->lock);
583
584         return count;
585 }
586
587 static ssize_t show_pwm(struct device *dev, struct device_attribute
588                         *devattr, char *buf)
589 {
590         struct pc87427_data *data = pc87427_update_device(dev);
591         int nr = to_sensor_dev_attr(devattr)->index;
592
593         return sprintf(buf, "%d\n", (int)data->pwm[nr]);
594 }
595
596 static ssize_t set_pwm(struct device *dev, struct device_attribute
597                        *devattr, const char *buf, size_t count)
598 {
599         struct pc87427_data *data = dev_get_drvdata(dev);
600         int nr = to_sensor_dev_attr(devattr)->index;
601         unsigned long val;
602         int iobase = data->address[LD_FAN];
603         u8 mode;
604
605         if (strict_strtoul(buf, 10, &val) < 0 || val > 0xff)
606                 return -EINVAL;
607
608         mutex_lock(&data->lock);
609         pc87427_readall_pwm(data, nr);
610         mode = data->pwm_enable[nr] & PWM_ENABLE_MODE_MASK;
611         if (mode != PWM_MODE_MANUAL && mode != PWM_MODE_OFF) {
612                 dev_notice(dev, "Can't set PWM%d duty cycle while not in "
613                            "manual mode\n", nr + 1);
614                 mutex_unlock(&data->lock);
615                 return -EPERM;
616         }
617
618         /* We may have to change the mode */
619         if (mode == PWM_MODE_MANUAL && val == 0) {
620                 /* Transition from Manual to Off */
621                 update_pwm_enable(data, nr, PWM_MODE_OFF);
622                 mode = PWM_MODE_OFF;
623                 dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1,
624                         "manual", "off");
625         } else if (mode == PWM_MODE_OFF && val != 0) {
626                 /* Transition from Off to Manual */
627                 update_pwm_enable(data, nr, PWM_MODE_MANUAL);
628                 mode = PWM_MODE_MANUAL;
629                 dev_dbg(dev, "Switching PWM%d from %s to %s\n", nr + 1,
630                         "off", "manual");
631         }
632
633         data->pwm[nr] = val;
634         if (mode == PWM_MODE_MANUAL)
635                 outb(val, iobase + PC87427_REG_PWM_DUTY);
636         mutex_unlock(&data->lock);
637
638         return count;
639 }
640
641 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
642                           show_pwm_enable, set_pwm_enable, 0);
643 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
644                           show_pwm_enable, set_pwm_enable, 1);
645 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
646                           show_pwm_enable, set_pwm_enable, 2);
647 static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
648                           show_pwm_enable, set_pwm_enable, 3);
649
650 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
651 static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
652 static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
653 static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
654
655 static struct attribute *pc87427_attributes_pwm[4][3] = {
656         {
657                 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
658                 &sensor_dev_attr_pwm1.dev_attr.attr,
659                 NULL
660         }, {
661                 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
662                 &sensor_dev_attr_pwm2.dev_attr.attr,
663                 NULL
664         }, {
665                 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
666                 &sensor_dev_attr_pwm3.dev_attr.attr,
667                 NULL
668         }, {
669                 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
670                 &sensor_dev_attr_pwm4.dev_attr.attr,
671                 NULL
672         }
673 };
674
675 static const struct attribute_group pc87427_group_pwm[4] = {
676         { .attrs = pc87427_attributes_pwm[0] },
677         { .attrs = pc87427_attributes_pwm[1] },
678         { .attrs = pc87427_attributes_pwm[2] },
679         { .attrs = pc87427_attributes_pwm[3] },
680 };
681
682 static ssize_t show_temp_input(struct device *dev, struct device_attribute
683                                *devattr, char *buf)
684 {
685         struct pc87427_data *data = pc87427_update_device(dev);
686         int nr = to_sensor_dev_attr(devattr)->index;
687
688         return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
689 }
690
691 static ssize_t show_temp_min(struct device *dev, struct device_attribute
692                              *devattr, char *buf)
693 {
694         struct pc87427_data *data = pc87427_update_device(dev);
695         int nr = to_sensor_dev_attr(devattr)->index;
696
697         return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_min[nr]));
698 }
699
700 static ssize_t show_temp_max(struct device *dev, struct device_attribute
701                              *devattr, char *buf)
702 {
703         struct pc87427_data *data = pc87427_update_device(dev);
704         int nr = to_sensor_dev_attr(devattr)->index;
705
706         return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_max[nr]));
707 }
708
709 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
710                               *devattr, char *buf)
711 {
712         struct pc87427_data *data = pc87427_update_device(dev);
713         int nr = to_sensor_dev_attr(devattr)->index;
714
715         return sprintf(buf, "%ld\n", temp_from_reg8(data->temp_crit[nr]));
716 }
717
718 static ssize_t show_temp_type(struct device *dev, struct device_attribute
719                               *devattr, char *buf)
720 {
721         struct pc87427_data *data = pc87427_update_device(dev);
722         int nr = to_sensor_dev_attr(devattr)->index;
723
724         return sprintf(buf, "%u\n", temp_type_from_reg(data->temp_type[nr]));
725 }
726
727 static ssize_t show_temp_min_alarm(struct device *dev, struct device_attribute
728                                    *devattr, char *buf)
729 {
730         struct pc87427_data *data = pc87427_update_device(dev);
731         int nr = to_sensor_dev_attr(devattr)->index;
732
733         return sprintf(buf, "%d\n", !!(data->temp_status[nr]
734                                        & TEMP_STATUS_LOWFLG));
735 }
736
737 static ssize_t show_temp_max_alarm(struct device *dev, struct device_attribute
738                                    *devattr, char *buf)
739 {
740         struct pc87427_data *data = pc87427_update_device(dev);
741         int nr = to_sensor_dev_attr(devattr)->index;
742
743         return sprintf(buf, "%d\n", !!(data->temp_status[nr]
744                                        & TEMP_STATUS_HIGHFLG));
745 }
746
747 static ssize_t show_temp_crit_alarm(struct device *dev, struct device_attribute
748                                    *devattr, char *buf)
749 {
750         struct pc87427_data *data = pc87427_update_device(dev);
751         int nr = to_sensor_dev_attr(devattr)->index;
752
753         return sprintf(buf, "%d\n", !!(data->temp_status[nr]
754                                        & TEMP_STATUS_CRITFLG));
755 }
756
757 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
758                                *devattr, char *buf)
759 {
760         struct pc87427_data *data = pc87427_update_device(dev);
761         int nr = to_sensor_dev_attr(devattr)->index;
762
763         return sprintf(buf, "%d\n", !!(data->temp_status[nr]
764                                        & TEMP_STATUS_SENSERR));
765 }
766
767 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
768 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
769 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input, NULL, 2);
770 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input, NULL, 3);
771 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp_input, NULL, 4);
772 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp_input, NULL, 5);
773
774 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, NULL, 0);
775 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, NULL, 1);
776 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, NULL, 2);
777 static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min, NULL, 3);
778 static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min, NULL, 4);
779 static SENSOR_DEVICE_ATTR(temp6_min, S_IRUGO, show_temp_min, NULL, 5);
780
781 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, NULL, 0);
782 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, NULL, 1);
783 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, NULL, 2);
784 static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, NULL, 3);
785 static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, NULL, 4);
786 static SENSOR_DEVICE_ATTR(temp6_max, S_IRUGO, show_temp_max, NULL, 5);
787
788 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit, NULL, 0);
789 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit, NULL, 1);
790 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit, NULL, 2);
791 static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp_crit, NULL, 3);
792 static SENSOR_DEVICE_ATTR(temp5_crit, S_IRUGO, show_temp_crit, NULL, 4);
793 static SENSOR_DEVICE_ATTR(temp6_crit, S_IRUGO, show_temp_crit, NULL, 5);
794
795 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
796 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
797 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
798 static SENSOR_DEVICE_ATTR(temp4_type, S_IRUGO, show_temp_type, NULL, 3);
799 static SENSOR_DEVICE_ATTR(temp5_type, S_IRUGO, show_temp_type, NULL, 4);
800 static SENSOR_DEVICE_ATTR(temp6_type, S_IRUGO, show_temp_type, NULL, 5);
801
802 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
803                           show_temp_min_alarm, NULL, 0);
804 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
805                           show_temp_min_alarm, NULL, 1);
806 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO,
807                           show_temp_min_alarm, NULL, 2);
808 static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO,
809                           show_temp_min_alarm, NULL, 3);
810 static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO,
811                           show_temp_min_alarm, NULL, 4);
812 static SENSOR_DEVICE_ATTR(temp6_min_alarm, S_IRUGO,
813                           show_temp_min_alarm, NULL, 5);
814
815 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
816                           show_temp_max_alarm, NULL, 0);
817 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
818                           show_temp_max_alarm, NULL, 1);
819 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO,
820                           show_temp_max_alarm, NULL, 2);
821 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO,
822                           show_temp_max_alarm, NULL, 3);
823 static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO,
824                           show_temp_max_alarm, NULL, 4);
825 static SENSOR_DEVICE_ATTR(temp6_max_alarm, S_IRUGO,
826                           show_temp_max_alarm, NULL, 5);
827
828 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
829                           show_temp_crit_alarm, NULL, 0);
830 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
831                           show_temp_crit_alarm, NULL, 1);
832 static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO,
833                           show_temp_crit_alarm, NULL, 2);
834 static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO,
835                           show_temp_crit_alarm, NULL, 3);
836 static SENSOR_DEVICE_ATTR(temp5_crit_alarm, S_IRUGO,
837                           show_temp_crit_alarm, NULL, 4);
838 static SENSOR_DEVICE_ATTR(temp6_crit_alarm, S_IRUGO,
839                           show_temp_crit_alarm, NULL, 5);
840
841 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
842 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
843 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
844 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
845 static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_temp_fault, NULL, 4);
846 static SENSOR_DEVICE_ATTR(temp6_fault, S_IRUGO, show_temp_fault, NULL, 5);
847
848 static struct attribute *pc87427_attributes_temp[6][10] = {
849         {
850                 &sensor_dev_attr_temp1_input.dev_attr.attr,
851                 &sensor_dev_attr_temp1_min.dev_attr.attr,
852                 &sensor_dev_attr_temp1_max.dev_attr.attr,
853                 &sensor_dev_attr_temp1_crit.dev_attr.attr,
854                 &sensor_dev_attr_temp1_type.dev_attr.attr,
855                 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
856                 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
857                 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
858                 &sensor_dev_attr_temp1_fault.dev_attr.attr,
859                 NULL
860         }, {
861                 &sensor_dev_attr_temp2_input.dev_attr.attr,
862                 &sensor_dev_attr_temp2_min.dev_attr.attr,
863                 &sensor_dev_attr_temp2_max.dev_attr.attr,
864                 &sensor_dev_attr_temp2_crit.dev_attr.attr,
865                 &sensor_dev_attr_temp2_type.dev_attr.attr,
866                 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
867                 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
868                 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
869                 &sensor_dev_attr_temp2_fault.dev_attr.attr,
870                 NULL
871         }, {
872                 &sensor_dev_attr_temp3_input.dev_attr.attr,
873                 &sensor_dev_attr_temp3_min.dev_attr.attr,
874                 &sensor_dev_attr_temp3_max.dev_attr.attr,
875                 &sensor_dev_attr_temp3_crit.dev_attr.attr,
876                 &sensor_dev_attr_temp3_type.dev_attr.attr,
877                 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
878                 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
879                 &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
880                 &sensor_dev_attr_temp3_fault.dev_attr.attr,
881                 NULL
882         }, {
883                 &sensor_dev_attr_temp4_input.dev_attr.attr,
884                 &sensor_dev_attr_temp4_min.dev_attr.attr,
885                 &sensor_dev_attr_temp4_max.dev_attr.attr,
886                 &sensor_dev_attr_temp4_crit.dev_attr.attr,
887                 &sensor_dev_attr_temp4_type.dev_attr.attr,
888                 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
889                 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
890                 &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
891                 &sensor_dev_attr_temp4_fault.dev_attr.attr,
892                 NULL
893         }, {
894                 &sensor_dev_attr_temp5_input.dev_attr.attr,
895                 &sensor_dev_attr_temp5_min.dev_attr.attr,
896                 &sensor_dev_attr_temp5_max.dev_attr.attr,
897                 &sensor_dev_attr_temp5_crit.dev_attr.attr,
898                 &sensor_dev_attr_temp5_type.dev_attr.attr,
899                 &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
900                 &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
901                 &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
902                 &sensor_dev_attr_temp5_fault.dev_attr.attr,
903                 NULL
904         }, {
905                 &sensor_dev_attr_temp6_input.dev_attr.attr,
906                 &sensor_dev_attr_temp6_min.dev_attr.attr,
907                 &sensor_dev_attr_temp6_max.dev_attr.attr,
908                 &sensor_dev_attr_temp6_crit.dev_attr.attr,
909                 &sensor_dev_attr_temp6_type.dev_attr.attr,
910                 &sensor_dev_attr_temp6_min_alarm.dev_attr.attr,
911                 &sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
912                 &sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
913                 &sensor_dev_attr_temp6_fault.dev_attr.attr,
914                 NULL
915         }
916 };
917
918 static const struct attribute_group pc87427_group_temp[6] = {
919         { .attrs = pc87427_attributes_temp[0] },
920         { .attrs = pc87427_attributes_temp[1] },
921         { .attrs = pc87427_attributes_temp[2] },
922         { .attrs = pc87427_attributes_temp[3] },
923         { .attrs = pc87427_attributes_temp[4] },
924         { .attrs = pc87427_attributes_temp[5] },
925 };
926
927 static ssize_t show_name(struct device *dev, struct device_attribute
928                          *devattr, char *buf)
929 {
930         struct pc87427_data *data = dev_get_drvdata(dev);
931
932         return sprintf(buf, "%s\n", data->name);
933 }
934 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
935
936
937 /*
938  * Device detection, attach and detach
939  */
940
941 static void pc87427_release_regions(struct platform_device *pdev, int count)
942 {
943         struct resource *res;
944         int i;
945
946         for (i = 0; i < count; i++) {
947                 res = platform_get_resource(pdev, IORESOURCE_IO, i);
948                 release_region(res->start, resource_size(res));
949         }
950 }
951
952 static int __devinit pc87427_request_regions(struct platform_device *pdev,
953                                              int count)
954 {
955         struct resource *res;
956         int i, err = 0;
957
958         for (i = 0; i < count; i++) {
959                 res = platform_get_resource(pdev, IORESOURCE_IO, i);
960                 if (!res) {
961                         err = -ENOENT;
962                         dev_err(&pdev->dev, "Missing resource #%d\n", i);
963                         break;
964                 }
965                 if (!request_region(res->start, resource_size(res), DRVNAME)) {
966                         err = -EBUSY;
967                         dev_err(&pdev->dev,
968                                 "Failed to request region 0x%lx-0x%lx\n",
969                                 (unsigned long)res->start,
970                                 (unsigned long)res->end);
971                         break;
972                 }
973         }
974
975         if (err && i)
976                 pc87427_release_regions(pdev, i);
977
978         return err;
979 }
980
981 static void __devinit pc87427_init_device(struct device *dev)
982 {
983         struct pc87427_sio_data *sio_data = dev->platform_data;
984         struct pc87427_data *data = dev_get_drvdata(dev);
985         int i;
986         u8 reg;
987
988         /* The FMC module should be ready */
989         reg = pc87427_read8(data, LD_FAN, PC87427_REG_BANK);
990         if (!(reg & 0x80))
991                 dev_warn(dev, "%s module not ready!\n", "FMC");
992
993         /* Check which fans are enabled */
994         for (i = 0; i < 8; i++) {
995                 if (!(sio_data->has_fanin & (1 << i)))  /* Not wired */
996                         continue;
997                 reg = pc87427_read8_bank(data, LD_FAN, BANK_FM(i),
998                                          PC87427_REG_FAN_STATUS);
999                 if (reg & FAN_STATUS_MONEN)
1000                         data->fan_enabled |= (1 << i);
1001         }
1002
1003         if (!data->fan_enabled) {
1004                 dev_dbg(dev, "Enabling monitoring of all fans\n");
1005                 for (i = 0; i < 8; i++) {
1006                         if (!(sio_data->has_fanin & (1 << i)))  /* Not wired */
1007                                 continue;
1008                         pc87427_write8_bank(data, LD_FAN, BANK_FM(i),
1009                                             PC87427_REG_FAN_STATUS,
1010                                             FAN_STATUS_MONEN);
1011                 }
1012                 data->fan_enabled = sio_data->has_fanin;
1013         }
1014
1015         /* Check which PWM outputs are enabled */
1016         for (i = 0; i < 4; i++) {
1017                 if (!(sio_data->has_fanout & (1 << i))) /* Not wired */
1018                         continue;
1019                 reg = pc87427_read8_bank(data, LD_FAN, BANK_FC(i),
1020                                          PC87427_REG_PWM_ENABLE);
1021                 if (reg & PWM_ENABLE_CTLEN)
1022                         data->pwm_enabled |= (1 << i);
1023
1024                 /* We don't expose an interface to reconfigure the automatic
1025                    fan control mode, so only allow to return to this mode if
1026                    it was originally set. */
1027                 if ((reg & PWM_ENABLE_MODE_MASK) == PWM_MODE_AUTO) {
1028                         dev_dbg(dev, "PWM%d is in automatic control mode\n",
1029                                 i + 1);
1030                         data->pwm_auto_ok |= (1 << i);
1031                 }
1032         }
1033
1034         /* The HMC module should be ready */
1035         reg = pc87427_read8(data, LD_TEMP, PC87427_REG_BANK);
1036         if (!(reg & 0x80))
1037                 dev_warn(dev, "%s module not ready!\n", "HMC");
1038
1039         /* Check which temperature channels are enabled */
1040         for (i = 0; i < 6; i++) {
1041                 reg = pc87427_read8_bank(data, LD_TEMP, BANK_TM(i),
1042                                          PC87427_REG_TEMP_STATUS);
1043                 if (reg & TEMP_STATUS_CHANEN)
1044                         data->temp_enabled |= (1 << i);
1045         }
1046 }
1047
1048 static int __devinit pc87427_probe(struct platform_device *pdev)
1049 {
1050         struct pc87427_sio_data *sio_data = pdev->dev.platform_data;
1051         struct pc87427_data *data;
1052         int i, err, res_count;
1053
1054         data = kzalloc(sizeof(struct pc87427_data), GFP_KERNEL);
1055         if (!data) {
1056                 err = -ENOMEM;
1057                 printk(KERN_ERR DRVNAME ": Out of memory\n");
1058                 goto exit;
1059         }
1060
1061         data->address[0] = sio_data->address[0];
1062         data->address[1] = sio_data->address[1];
1063         res_count = (data->address[0] != 0) + (data->address[1] != 0);
1064
1065         err = pc87427_request_regions(pdev, res_count);
1066         if (err)
1067                 goto exit_kfree;
1068
1069         mutex_init(&data->lock);
1070         data->name = "pc87427";
1071         platform_set_drvdata(pdev, data);
1072         pc87427_init_device(&pdev->dev);
1073
1074         /* Register sysfs hooks */
1075         err = device_create_file(&pdev->dev, &dev_attr_name);
1076         if (err)
1077                 goto exit_release_region;
1078         for (i = 0; i < 8; i++) {
1079                 if (!(data->fan_enabled & (1 << i)))
1080                         continue;
1081                 err = sysfs_create_group(&pdev->dev.kobj,
1082                                          &pc87427_group_fan[i]);
1083                 if (err)
1084                         goto exit_remove_files;
1085         }
1086         for (i = 0; i < 4; i++) {
1087                 if (!(data->pwm_enabled & (1 << i)))
1088                         continue;
1089                 err = sysfs_create_group(&pdev->dev.kobj,
1090                                          &pc87427_group_pwm[i]);
1091                 if (err)
1092                         goto exit_remove_files;
1093         }
1094         for (i = 0; i < 6; i++) {
1095                 if (!(data->temp_enabled & (1 << i)))
1096                         continue;
1097                 err = sysfs_create_group(&pdev->dev.kobj,
1098                                          &pc87427_group_temp[i]);
1099                 if (err)
1100                         goto exit_remove_files;
1101         }
1102
1103         data->hwmon_dev = hwmon_device_register(&pdev->dev);
1104         if (IS_ERR(data->hwmon_dev)) {
1105                 err = PTR_ERR(data->hwmon_dev);
1106                 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1107                 goto exit_remove_files;
1108         }
1109
1110         return 0;
1111
1112 exit_remove_files:
1113         for (i = 0; i < 8; i++) {
1114                 if (!(data->fan_enabled & (1 << i)))
1115                         continue;
1116                 sysfs_remove_group(&pdev->dev.kobj, &pc87427_group_fan[i]);
1117         }
1118         for (i = 0; i < 4; i++) {
1119                 if (!(data->pwm_enabled & (1 << i)))
1120                         continue;
1121                 sysfs_remove_group(&pdev->dev.kobj, &pc87427_group_pwm[i]);
1122         }
1123         for (i = 0; i < 6; i++) {
1124                 if (!(data->temp_enabled & (1 << i)))
1125                         continue;
1126                 sysfs_remove_group(&pdev->dev.kobj, &pc87427_group_temp[i]);
1127         }
1128 exit_release_region:
1129         pc87427_release_regions(pdev, res_count);
1130 exit_kfree:
1131         platform_set_drvdata(pdev, NULL);
1132         kfree(data);
1133 exit:
1134         return err;
1135 }
1136
1137 static int __devexit pc87427_remove(struct platform_device *pdev)
1138 {
1139         struct pc87427_data *data = platform_get_drvdata(pdev);
1140         int i, res_count;
1141
1142         res_count = (data->address[0] != 0) + (data->address[1] != 0);
1143
1144         hwmon_device_unregister(data->hwmon_dev);
1145         device_remove_file(&pdev->dev, &dev_attr_name);
1146         for (i = 0; i < 8; i++) {
1147                 if (!(data->fan_enabled & (1 << i)))
1148                         continue;
1149                 sysfs_remove_group(&pdev->dev.kobj, &pc87427_group_fan[i]);
1150         }
1151         for (i = 0; i < 4; i++) {
1152                 if (!(data->pwm_enabled & (1 << i)))
1153                         continue;
1154                 sysfs_remove_group(&pdev->dev.kobj, &pc87427_group_pwm[i]);
1155         }
1156         for (i = 0; i < 6; i++) {
1157                 if (!(data->temp_enabled & (1 << i)))
1158                         continue;
1159                 sysfs_remove_group(&pdev->dev.kobj, &pc87427_group_temp[i]);
1160         }
1161         platform_set_drvdata(pdev, NULL);
1162         kfree(data);
1163
1164         pc87427_release_regions(pdev, res_count);
1165
1166         return 0;
1167 }
1168
1169
1170 static struct platform_driver pc87427_driver = {
1171         .driver = {
1172                 .owner  = THIS_MODULE,
1173                 .name   = DRVNAME,
1174         },
1175         .probe          = pc87427_probe,
1176         .remove         = __devexit_p(pc87427_remove),
1177 };
1178
1179 static int __init pc87427_device_add(const struct pc87427_sio_data *sio_data)
1180 {
1181         struct resource res[2] = {
1182                 { .flags        = IORESOURCE_IO },
1183                 { .flags        = IORESOURCE_IO },
1184         };
1185         int err, i, res_count;
1186
1187         res_count = 0;
1188         for (i = 0; i < 2; i++) {
1189                 if (!sio_data->address[i])
1190                         continue;
1191                 res[res_count].start = sio_data->address[i];
1192                 res[res_count].end = sio_data->address[i] + REGION_LENGTH - 1;
1193                 res[res_count].name = logdev_str[i];
1194
1195                 err = acpi_check_resource_conflict(&res[res_count]);
1196                 if (err)
1197                         goto exit;
1198
1199                 res_count++;
1200         }
1201
1202         pdev = platform_device_alloc(DRVNAME, res[0].start);
1203         if (!pdev) {
1204                 err = -ENOMEM;
1205                 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1206                 goto exit;
1207         }
1208
1209         err = platform_device_add_resources(pdev, res, res_count);
1210         if (err) {
1211                 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1212                        "(%d)\n", err);
1213                 goto exit_device_put;
1214         }
1215
1216         err = platform_device_add_data(pdev, sio_data,
1217                                        sizeof(struct pc87427_sio_data));
1218         if (err) {
1219                 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1220                 goto exit_device_put;
1221         }
1222
1223         err = platform_device_add(pdev);
1224         if (err) {
1225                 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1226                        err);
1227                 goto exit_device_put;
1228         }
1229
1230         return 0;
1231
1232 exit_device_put:
1233         platform_device_put(pdev);
1234 exit:
1235         return err;
1236 }
1237
1238 static int __init pc87427_find(int sioaddr, struct pc87427_sio_data *sio_data)
1239 {
1240         u16 val;
1241         u8 cfg, cfg_b;
1242         int i, err = 0;
1243
1244         /* Identify device */
1245         val = force_id ? force_id : superio_inb(sioaddr, SIOREG_DEVID);
1246         if (val != 0xf2) {      /* PC87427 */
1247                 err = -ENODEV;
1248                 goto exit;
1249         }
1250
1251         for (i = 0; i < 2; i++) {
1252                 sio_data->address[i] = 0;
1253                 /* Select logical device */
1254                 superio_outb(sioaddr, SIOREG_LDSEL, logdev[i]);
1255
1256                 val = superio_inb(sioaddr, SIOREG_ACT);
1257                 if (!(val & 0x01)) {
1258                         printk(KERN_INFO DRVNAME ": Logical device 0x%02x "
1259                                "not activated\n", logdev[i]);
1260                         continue;
1261                 }
1262
1263                 val = superio_inb(sioaddr, SIOREG_MAP);
1264                 if (val & 0x01) {
1265                         printk(KERN_WARNING DRVNAME ": Logical device 0x%02x "
1266                                "is memory-mapped, can't use\n", logdev[i]);
1267                         continue;
1268                 }
1269
1270                 val = (superio_inb(sioaddr, SIOREG_IOBASE) << 8)
1271                     | superio_inb(sioaddr, SIOREG_IOBASE + 1);
1272                 if (!val) {
1273                         printk(KERN_INFO DRVNAME ": I/O base address not set "
1274                                "for logical device 0x%02x\n", logdev[i]);
1275                         continue;
1276                 }
1277                 sio_data->address[i] = val;
1278         }
1279
1280         /* No point in loading the driver if everything is disabled */
1281         if (!sio_data->address[0] && !sio_data->address[1]) {
1282                 err = -ENODEV;
1283                 goto exit;
1284         }
1285
1286         /* Check which fan inputs are wired */
1287         sio_data->has_fanin = (1 << 2) | (1 << 3);      /* FANIN2, FANIN3 */
1288
1289         cfg = superio_inb(sioaddr, SIOREG_CF2);
1290         if (!(cfg & (1 << 3)))
1291                 sio_data->has_fanin |= (1 << 0);        /* FANIN0 */
1292         if (!(cfg & (1 << 2)))
1293                 sio_data->has_fanin |= (1 << 4);        /* FANIN4 */
1294
1295         cfg = superio_inb(sioaddr, SIOREG_CFD);
1296         if (!(cfg & (1 << 0)))
1297                 sio_data->has_fanin |= (1 << 1);        /* FANIN1 */
1298
1299         cfg = superio_inb(sioaddr, SIOREG_CF4);
1300         if (!(cfg & (1 << 0)))
1301                 sio_data->has_fanin |= (1 << 7);        /* FANIN7 */
1302         cfg_b = superio_inb(sioaddr, SIOREG_CFB);
1303         if (!(cfg & (1 << 1)) && (cfg_b & (1 << 3)))
1304                 sio_data->has_fanin |= (1 << 5);        /* FANIN5 */
1305         cfg = superio_inb(sioaddr, SIOREG_CF3);
1306         if ((cfg & (1 << 3)) && !(cfg_b & (1 << 5)))
1307                 sio_data->has_fanin |= (1 << 6);        /* FANIN6 */
1308
1309         /* Check which fan outputs are wired */
1310         sio_data->has_fanout = (1 << 0);                /* FANOUT0 */
1311         if (cfg_b & (1 << 0))
1312                 sio_data->has_fanout |= (1 << 3);       /* FANOUT3 */
1313
1314         cfg = superio_inb(sioaddr, SIOREG_CFC);
1315         if (!(cfg & (1 << 4))) {
1316                 if (cfg_b & (1 << 1))
1317                         sio_data->has_fanout |= (1 << 1); /* FANOUT1 */
1318                 if (cfg_b & (1 << 2))
1319                         sio_data->has_fanout |= (1 << 2); /* FANOUT2 */
1320         }
1321
1322         /* FANOUT1 and FANOUT2 can each be routed to 2 different pins */
1323         cfg = superio_inb(sioaddr, SIOREG_CF5);
1324         if (cfg & (1 << 6))
1325                 sio_data->has_fanout |= (1 << 1);       /* FANOUT1 */
1326         if (cfg & (1 << 5))
1327                 sio_data->has_fanout |= (1 << 2);       /* FANOUT2 */
1328
1329 exit:
1330         superio_exit(sioaddr);
1331         return err;
1332 }
1333
1334 static int __init pc87427_init(void)
1335 {
1336         int err;
1337         struct pc87427_sio_data sio_data;
1338
1339         if (pc87427_find(0x2e, &sio_data)
1340          && pc87427_find(0x4e, &sio_data))
1341                 return -ENODEV;
1342
1343         err = platform_driver_register(&pc87427_driver);
1344         if (err)
1345                 goto exit;
1346
1347         /* Sets global pdev as a side effect */
1348         err = pc87427_device_add(&sio_data);
1349         if (err)
1350                 goto exit_driver;
1351
1352         return 0;
1353
1354 exit_driver:
1355         platform_driver_unregister(&pc87427_driver);
1356 exit:
1357         return err;
1358 }
1359
1360 static void __exit pc87427_exit(void)
1361 {
1362         platform_device_unregister(pdev);
1363         platform_driver_unregister(&pc87427_driver);
1364 }
1365
1366 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
1367 MODULE_DESCRIPTION("PC87427 hardware monitoring driver");
1368 MODULE_LICENSE("GPL");
1369
1370 module_init(pc87427_init);
1371 module_exit(pc87427_exit);