hwmon: (f71805f) List the F71806F/FG as supported
[safe/jmp/linux-2.6] / drivers / hwmon / f71805f.c
1 /*
2  * f71805f.c - driver for the Fintek F71805F/FG and F71872F/FG Super-I/O
3  *             chips integrated hardware monitoring features
4  * Copyright (C) 2005-2006  Jean Delvare <khali@linux-fr.org>
5  *
6  * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates
7  * complete hardware monitoring features: voltage, fan and temperature
8  * sensors, and manual and automatic fan speed control.
9  *
10  * The F71872F/FG is almost the same, with two more voltages monitored,
11  * and 6 VID inputs.
12  *
13  * The F71806F/FG is essentially the same as the F71872F/FG. It even has
14  * the same chip ID, so the driver can't differentiate between.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/jiffies.h>
35 #include <linux/platform_device.h>
36 #include <linux/hwmon.h>
37 #include <linux/hwmon-sysfs.h>
38 #include <linux/err.h>
39 #include <linux/mutex.h>
40 #include <linux/sysfs.h>
41 #include <linux/ioport.h>
42 #include <asm/io.h>
43
44 static struct platform_device *pdev;
45
46 #define DRVNAME "f71805f"
47 enum kinds { f71805f, f71872f };
48
49 /*
50  * Super-I/O constants and functions
51  */
52
53 #define F71805F_LD_HWM          0x04
54
55 #define SIO_REG_LDSEL           0x07    /* Logical device select */
56 #define SIO_REG_DEVID           0x20    /* Device ID (2 bytes) */
57 #define SIO_REG_DEVREV          0x22    /* Device revision */
58 #define SIO_REG_MANID           0x23    /* Fintek ID (2 bytes) */
59 #define SIO_REG_FNSEL1          0x29    /* Multi Function Select 1 (F71872F) */
60 #define SIO_REG_ENABLE          0x30    /* Logical device enable */
61 #define SIO_REG_ADDR            0x60    /* Logical device address (2 bytes) */
62
63 #define SIO_FINTEK_ID           0x1934
64 #define SIO_F71805F_ID          0x0406
65 #define SIO_F71872F_ID          0x0341
66
67 static inline int
68 superio_inb(int base, int reg)
69 {
70         outb(reg, base);
71         return inb(base + 1);
72 }
73
74 static int
75 superio_inw(int base, int reg)
76 {
77         int val;
78         outb(reg++, base);
79         val = inb(base + 1) << 8;
80         outb(reg, base);
81         val |= inb(base + 1);
82         return val;
83 }
84
85 static inline void
86 superio_select(int base, int ld)
87 {
88         outb(SIO_REG_LDSEL, base);
89         outb(ld, base + 1);
90 }
91
92 static inline void
93 superio_enter(int base)
94 {
95         outb(0x87, base);
96         outb(0x87, base);
97 }
98
99 static inline void
100 superio_exit(int base)
101 {
102         outb(0xaa, base);
103 }
104
105 /*
106  * ISA constants
107  */
108
109 #define REGION_LENGTH           8
110 #define ADDR_REG_OFFSET         5
111 #define DATA_REG_OFFSET         6
112
113 /*
114  * Registers
115  */
116
117 /* in nr from 0 to 10 (8-bit values) */
118 #define F71805F_REG_IN(nr)              (0x10 + (nr))
119 #define F71805F_REG_IN_HIGH(nr)         ((nr) < 10 ? 0x40 + 2 * (nr) : 0x2E)
120 #define F71805F_REG_IN_LOW(nr)          ((nr) < 10 ? 0x41 + 2 * (nr) : 0x2F)
121 /* fan nr from 0 to 2 (12-bit values, two registers) */
122 #define F71805F_REG_FAN(nr)             (0x20 + 2 * (nr))
123 #define F71805F_REG_FAN_LOW(nr)         (0x28 + 2 * (nr))
124 #define F71805F_REG_FAN_TARGET(nr)      (0x69 + 16 * (nr))
125 #define F71805F_REG_FAN_CTRL(nr)        (0x60 + 16 * (nr))
126 #define F71805F_REG_PWM_FREQ(nr)        (0x63 + 16 * (nr))
127 #define F71805F_REG_PWM_DUTY(nr)        (0x6B + 16 * (nr))
128 /* temp nr from 0 to 2 (8-bit values) */
129 #define F71805F_REG_TEMP(nr)            (0x1B + (nr))
130 #define F71805F_REG_TEMP_HIGH(nr)       (0x54 + 2 * (nr))
131 #define F71805F_REG_TEMP_HYST(nr)       (0x55 + 2 * (nr))
132 #define F71805F_REG_TEMP_MODE           0x01
133 /* pwm/fan pwmnr from 0 to 2, auto point apnr from 0 to 2 */
134 /* map Fintek numbers to our numbers as follows: 9->0, 5->1, 1->2 */
135 #define F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr) \
136                                         (0xA0 + 0x10 * (pwmnr) + (2 - (apnr)))
137 #define F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr) \
138                                         (0xA4 + 0x10 * (pwmnr) + \
139                                                 2 * (2 - (apnr)))
140
141 #define F71805F_REG_START               0x00
142 /* status nr from 0 to 2 */
143 #define F71805F_REG_STATUS(nr)          (0x36 + (nr))
144
145 /* individual register bits */
146 #define FAN_CTRL_DC_MODE                0x10
147 #define FAN_CTRL_LATCH_FULL             0x08
148 #define FAN_CTRL_MODE_MASK              0x03
149 #define FAN_CTRL_MODE_SPEED             0x00
150 #define FAN_CTRL_MODE_TEMPERATURE       0x01
151 #define FAN_CTRL_MODE_MANUAL            0x02
152
153 /*
154  * Data structures and manipulation thereof
155  */
156
157 struct f71805f_auto_point {
158         u8 temp[3];
159         u16 fan[3];
160 };
161
162 struct f71805f_data {
163         unsigned short addr;
164         const char *name;
165         struct class_device *class_dev;
166
167         struct mutex update_lock;
168         char valid;             /* !=0 if following fields are valid */
169         unsigned long last_updated;     /* In jiffies */
170         unsigned long last_limits;      /* In jiffies */
171
172         /* Register values */
173         u8 in[11];
174         u8 in_high[11];
175         u8 in_low[11];
176         u16 has_in;
177         u16 fan[3];
178         u16 fan_low[3];
179         u16 fan_target[3];
180         u8 fan_ctrl[3];
181         u8 pwm[3];
182         u8 pwm_freq[3];
183         u8 temp[3];
184         u8 temp_high[3];
185         u8 temp_hyst[3];
186         u8 temp_mode;
187         unsigned long alarms;
188         struct f71805f_auto_point auto_points[3];
189 };
190
191 struct f71805f_sio_data {
192         enum kinds kind;
193         u8 fnsel1;
194 };
195
196 static inline long in_from_reg(u8 reg)
197 {
198         return (reg * 8);
199 }
200
201 /* The 2 least significant bits are not used */
202 static inline u8 in_to_reg(long val)
203 {
204         if (val <= 0)
205                 return 0;
206         if (val >= 2016)
207                 return 0xfc;
208         return (((val + 16) / 32) << 2);
209 }
210
211 /* in0 is downscaled by a factor 2 internally */
212 static inline long in0_from_reg(u8 reg)
213 {
214         return (reg * 16);
215 }
216
217 static inline u8 in0_to_reg(long val)
218 {
219         if (val <= 0)
220                 return 0;
221         if (val >= 4032)
222                 return 0xfc;
223         return (((val + 32) / 64) << 2);
224 }
225
226 /* The 4 most significant bits are not used */
227 static inline long fan_from_reg(u16 reg)
228 {
229         reg &= 0xfff;
230         if (!reg || reg == 0xfff)
231                 return 0;
232         return (1500000 / reg);
233 }
234
235 static inline u16 fan_to_reg(long rpm)
236 {
237         /* If the low limit is set below what the chip can measure,
238            store the largest possible 12-bit value in the registers,
239            so that no alarm will ever trigger. */
240         if (rpm < 367)
241                 return 0xfff;
242         return (1500000 / rpm);
243 }
244
245 static inline unsigned long pwm_freq_from_reg(u8 reg)
246 {
247         unsigned long clock = (reg & 0x80) ? 48000000UL : 1000000UL;
248
249         reg &= 0x7f;
250         if (reg == 0)
251                 reg++;
252         return clock / (reg << 8);
253 }
254
255 static inline u8 pwm_freq_to_reg(unsigned long val)
256 {
257         if (val >= 187500)      /* The highest we can do */
258                 return 0x80;
259         if (val >= 1475)        /* Use 48 MHz clock */
260                 return 0x80 | (48000000UL / (val << 8));
261         if (val < 31)           /* The lowest we can do */
262                 return 0x7f;
263         else                    /* Use 1 MHz clock */
264                 return 1000000UL / (val << 8);
265 }
266
267 static inline int pwm_mode_from_reg(u8 reg)
268 {
269         return !(reg & FAN_CTRL_DC_MODE);
270 }
271
272 static inline long temp_from_reg(u8 reg)
273 {
274         return (reg * 1000);
275 }
276
277 static inline u8 temp_to_reg(long val)
278 {
279         if (val < 0)
280                 val = 0;
281         else if (val > 1000 * 0xff)
282                 val = 0xff;
283         return ((val + 500) / 1000);
284 }
285
286 /*
287  * Device I/O access
288  */
289
290 /* Must be called with data->update_lock held, except during initialization */
291 static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
292 {
293         outb(reg, data->addr + ADDR_REG_OFFSET);
294         return inb(data->addr + DATA_REG_OFFSET);
295 }
296
297 /* Must be called with data->update_lock held, except during initialization */
298 static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
299 {
300         outb(reg, data->addr + ADDR_REG_OFFSET);
301         outb(val, data->addr + DATA_REG_OFFSET);
302 }
303
304 /* It is important to read the MSB first, because doing so latches the
305    value of the LSB, so we are sure both bytes belong to the same value.
306    Must be called with data->update_lock held, except during initialization */
307 static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
308 {
309         u16 val;
310
311         outb(reg, data->addr + ADDR_REG_OFFSET);
312         val = inb(data->addr + DATA_REG_OFFSET) << 8;
313         outb(++reg, data->addr + ADDR_REG_OFFSET);
314         val |= inb(data->addr + DATA_REG_OFFSET);
315
316         return val;
317 }
318
319 /* Must be called with data->update_lock held, except during initialization */
320 static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
321 {
322         outb(reg, data->addr + ADDR_REG_OFFSET);
323         outb(val >> 8, data->addr + DATA_REG_OFFSET);
324         outb(++reg, data->addr + ADDR_REG_OFFSET);
325         outb(val & 0xff, data->addr + DATA_REG_OFFSET);
326 }
327
328 static struct f71805f_data *f71805f_update_device(struct device *dev)
329 {
330         struct f71805f_data *data = dev_get_drvdata(dev);
331         int nr, apnr;
332
333         mutex_lock(&data->update_lock);
334
335         /* Limit registers cache is refreshed after 60 seconds */
336         if (time_after(jiffies, data->last_updated + 60 * HZ)
337          || !data->valid) {
338                 for (nr = 0; nr < 11; nr++) {
339                         if (!(data->has_in & (1 << nr)))
340                                 continue;
341                         data->in_high[nr] = f71805f_read8(data,
342                                             F71805F_REG_IN_HIGH(nr));
343                         data->in_low[nr] = f71805f_read8(data,
344                                            F71805F_REG_IN_LOW(nr));
345                 }
346                 for (nr = 0; nr < 3; nr++) {
347                         data->fan_low[nr] = f71805f_read16(data,
348                                             F71805F_REG_FAN_LOW(nr));
349                         data->fan_target[nr] = f71805f_read16(data,
350                                                F71805F_REG_FAN_TARGET(nr));
351                         data->pwm_freq[nr] = f71805f_read8(data,
352                                              F71805F_REG_PWM_FREQ(nr));
353                 }
354                 for (nr = 0; nr < 3; nr++) {
355                         data->temp_high[nr] = f71805f_read8(data,
356                                               F71805F_REG_TEMP_HIGH(nr));
357                         data->temp_hyst[nr] = f71805f_read8(data,
358                                               F71805F_REG_TEMP_HYST(nr));
359                 }
360                 data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
361                 for (nr = 0; nr < 3; nr++) {
362                         for (apnr = 0; apnr < 3; apnr++) {
363                                 data->auto_points[nr].temp[apnr] =
364                                         f71805f_read8(data,
365                                         F71805F_REG_PWM_AUTO_POINT_TEMP(nr,
366                                                                         apnr));
367                                 data->auto_points[nr].fan[apnr] =
368                                         f71805f_read16(data,
369                                         F71805F_REG_PWM_AUTO_POINT_FAN(nr,
370                                                                        apnr));
371                         }
372                 }
373
374                 data->last_limits = jiffies;
375         }
376
377         /* Measurement registers cache is refreshed after 1 second */
378         if (time_after(jiffies, data->last_updated + HZ)
379          || !data->valid) {
380                 for (nr = 0; nr < 11; nr++) {
381                         if (!(data->has_in & (1 << nr)))
382                                 continue;
383                         data->in[nr] = f71805f_read8(data,
384                                        F71805F_REG_IN(nr));
385                 }
386                 for (nr = 0; nr < 3; nr++) {
387                         data->fan[nr] = f71805f_read16(data,
388                                         F71805F_REG_FAN(nr));
389                         data->fan_ctrl[nr] = f71805f_read8(data,
390                                              F71805F_REG_FAN_CTRL(nr));
391                         data->pwm[nr] = f71805f_read8(data,
392                                         F71805F_REG_PWM_DUTY(nr));
393                 }
394                 for (nr = 0; nr < 3; nr++) {
395                         data->temp[nr] = f71805f_read8(data,
396                                          F71805F_REG_TEMP(nr));
397                 }
398                 data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
399                         + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
400                         + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
401
402                 data->last_updated = jiffies;
403                 data->valid = 1;
404         }
405
406         mutex_unlock(&data->update_lock);
407
408         return data;
409 }
410
411 /*
412  * Sysfs interface
413  */
414
415 static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
416                         char *buf)
417 {
418         struct f71805f_data *data = f71805f_update_device(dev);
419         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
420         int nr = attr->index;
421
422         return sprintf(buf, "%ld\n", in0_from_reg(data->in[nr]));
423 }
424
425 static ssize_t show_in0_max(struct device *dev, struct device_attribute
426                             *devattr, char *buf)
427 {
428         struct f71805f_data *data = f71805f_update_device(dev);
429         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
430         int nr = attr->index;
431
432         return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[nr]));
433 }
434
435 static ssize_t show_in0_min(struct device *dev, struct device_attribute
436                             *devattr, char *buf)
437 {
438         struct f71805f_data *data = f71805f_update_device(dev);
439         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
440         int nr = attr->index;
441
442         return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[nr]));
443 }
444
445 static ssize_t set_in0_max(struct device *dev, struct device_attribute
446                            *devattr, const char *buf, size_t count)
447 {
448         struct f71805f_data *data = dev_get_drvdata(dev);
449         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
450         int nr = attr->index;
451         long val = simple_strtol(buf, NULL, 10);
452
453         mutex_lock(&data->update_lock);
454         data->in_high[nr] = in0_to_reg(val);
455         f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
456         mutex_unlock(&data->update_lock);
457
458         return count;
459 }
460
461 static ssize_t set_in0_min(struct device *dev, struct device_attribute
462                            *devattr, const char *buf, size_t count)
463 {
464         struct f71805f_data *data = dev_get_drvdata(dev);
465         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
466         int nr = attr->index;
467         long val = simple_strtol(buf, NULL, 10);
468
469         mutex_lock(&data->update_lock);
470         data->in_low[nr] = in0_to_reg(val);
471         f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
472         mutex_unlock(&data->update_lock);
473
474         return count;
475 }
476
477 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
478                        char *buf)
479 {
480         struct f71805f_data *data = f71805f_update_device(dev);
481         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
482         int nr = attr->index;
483
484         return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
485 }
486
487 static ssize_t show_in_max(struct device *dev, struct device_attribute
488                            *devattr, char *buf)
489 {
490         struct f71805f_data *data = f71805f_update_device(dev);
491         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
492         int nr = attr->index;
493
494         return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
495 }
496
497 static ssize_t show_in_min(struct device *dev, struct device_attribute
498                            *devattr, char *buf)
499 {
500         struct f71805f_data *data = f71805f_update_device(dev);
501         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
502         int nr = attr->index;
503
504         return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
505 }
506
507 static ssize_t set_in_max(struct device *dev, struct device_attribute
508                           *devattr, const char *buf, size_t count)
509 {
510         struct f71805f_data *data = dev_get_drvdata(dev);
511         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
512         int nr = attr->index;
513         long val = simple_strtol(buf, NULL, 10);
514
515         mutex_lock(&data->update_lock);
516         data->in_high[nr] = in_to_reg(val);
517         f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
518         mutex_unlock(&data->update_lock);
519
520         return count;
521 }
522
523 static ssize_t set_in_min(struct device *dev, struct device_attribute
524                           *devattr, const char *buf, size_t count)
525 {
526         struct f71805f_data *data = dev_get_drvdata(dev);
527         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
528         int nr = attr->index;
529         long val = simple_strtol(buf, NULL, 10);
530
531         mutex_lock(&data->update_lock);
532         data->in_low[nr] = in_to_reg(val);
533         f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
534         mutex_unlock(&data->update_lock);
535
536         return count;
537 }
538
539 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
540                         char *buf)
541 {
542         struct f71805f_data *data = f71805f_update_device(dev);
543         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
544         int nr = attr->index;
545
546         return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
547 }
548
549 static ssize_t show_fan_min(struct device *dev, struct device_attribute
550                             *devattr, char *buf)
551 {
552         struct f71805f_data *data = f71805f_update_device(dev);
553         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
554         int nr = attr->index;
555
556         return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
557 }
558
559 static ssize_t show_fan_target(struct device *dev, struct device_attribute
560                                *devattr, char *buf)
561 {
562         struct f71805f_data *data = f71805f_update_device(dev);
563         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
564         int nr = attr->index;
565
566         return sprintf(buf, "%ld\n", fan_from_reg(data->fan_target[nr]));
567 }
568
569 static ssize_t set_fan_min(struct device *dev, struct device_attribute
570                            *devattr, const char *buf, size_t count)
571 {
572         struct f71805f_data *data = dev_get_drvdata(dev);
573         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
574         int nr = attr->index;
575         long val = simple_strtol(buf, NULL, 10);
576
577         mutex_lock(&data->update_lock);
578         data->fan_low[nr] = fan_to_reg(val);
579         f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
580         mutex_unlock(&data->update_lock);
581
582         return count;
583 }
584
585 static ssize_t set_fan_target(struct device *dev, struct device_attribute
586                               *devattr, const char *buf, size_t count)
587 {
588         struct f71805f_data *data = dev_get_drvdata(dev);
589         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
590         int nr = attr->index;
591         long val = simple_strtol(buf, NULL, 10);
592
593         mutex_lock(&data->update_lock);
594         data->fan_target[nr] = fan_to_reg(val);
595         f71805f_write16(data, F71805F_REG_FAN_TARGET(nr),
596                         data->fan_target[nr]);
597         mutex_unlock(&data->update_lock);
598
599         return count;
600 }
601
602 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
603                         char *buf)
604 {
605         struct f71805f_data *data = f71805f_update_device(dev);
606         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
607         int nr = attr->index;
608
609         return sprintf(buf, "%d\n", (int)data->pwm[nr]);
610 }
611
612 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
613                                *devattr, char *buf)
614 {
615         struct f71805f_data *data = f71805f_update_device(dev);
616         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
617         int nr = attr->index;
618         int mode;
619
620         switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
621         case FAN_CTRL_MODE_SPEED:
622                 mode = 3;
623                 break;
624         case FAN_CTRL_MODE_TEMPERATURE:
625                 mode = 2;
626                 break;
627         default: /* MANUAL */
628                 mode = 1;
629         }
630
631         return sprintf(buf, "%d\n", mode);
632 }
633
634 static ssize_t show_pwm_freq(struct device *dev, struct device_attribute
635                              *devattr, char *buf)
636 {
637         struct f71805f_data *data = f71805f_update_device(dev);
638         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
639         int nr = attr->index;
640
641         return sprintf(buf, "%lu\n", pwm_freq_from_reg(data->pwm_freq[nr]));
642 }
643
644 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
645                              *devattr, char *buf)
646 {
647         struct f71805f_data *data = f71805f_update_device(dev);
648         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
649         int nr = attr->index;
650
651         return sprintf(buf, "%d\n", pwm_mode_from_reg(data->fan_ctrl[nr]));
652 }
653
654 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
655                        const char *buf, size_t count)
656 {
657         struct f71805f_data *data = dev_get_drvdata(dev);
658         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
659         int nr = attr->index;
660         unsigned long val = simple_strtoul(buf, NULL, 10);
661
662         if (val > 255)
663                 return -EINVAL;
664
665         mutex_lock(&data->update_lock);
666         data->pwm[nr] = val;
667         f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
668         mutex_unlock(&data->update_lock);
669
670         return count;
671 }
672
673 static struct attribute *f71805f_attr_pwm[];
674
675 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
676                               *devattr, const char *buf, size_t count)
677 {
678         struct f71805f_data *data = dev_get_drvdata(dev);
679         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
680         int nr = attr->index;
681         unsigned long val = simple_strtoul(buf, NULL, 10);
682         u8 reg;
683
684         if (val < 1 || val > 3)
685                 return -EINVAL;
686
687         if (val > 1) { /* Automatic mode, user can't set PWM value */
688                 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
689                                      S_IRUGO))
690                         dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
691         }
692
693         mutex_lock(&data->update_lock);
694         reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
695             & ~FAN_CTRL_MODE_MASK;
696         switch (val) {
697         case 1:
698                 reg |= FAN_CTRL_MODE_MANUAL;
699                 break;
700         case 2:
701                 reg |= FAN_CTRL_MODE_TEMPERATURE;
702                 break;
703         case 3:
704                 reg |= FAN_CTRL_MODE_SPEED;
705                 break;
706         }
707         data->fan_ctrl[nr] = reg;
708         f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
709         mutex_unlock(&data->update_lock);
710
711         if (val == 1) { /* Manual mode, user can set PWM value */
712                 if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
713                                      S_IRUGO | S_IWUSR))
714                         dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
715         }
716
717         return count;
718 }
719
720 static ssize_t set_pwm_freq(struct device *dev, struct device_attribute
721                             *devattr, const char *buf, size_t count)
722 {
723         struct f71805f_data *data = dev_get_drvdata(dev);
724         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
725         int nr = attr->index;
726         unsigned long val = simple_strtoul(buf, NULL, 10);
727
728         mutex_lock(&data->update_lock);
729         data->pwm_freq[nr] = pwm_freq_to_reg(val);
730         f71805f_write8(data, F71805F_REG_PWM_FREQ(nr), data->pwm_freq[nr]);
731         mutex_unlock(&data->update_lock);
732
733         return count;
734 }
735
736 static ssize_t show_pwm_auto_point_temp(struct device *dev,
737                                         struct device_attribute *devattr,
738                                         char* buf)
739 {
740         struct f71805f_data *data = dev_get_drvdata(dev);
741         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
742         int pwmnr = attr->nr;
743         int apnr = attr->index;
744
745         return sprintf(buf, "%ld\n",
746                        temp_from_reg(data->auto_points[pwmnr].temp[apnr]));
747 }
748
749 static ssize_t set_pwm_auto_point_temp(struct device *dev,
750                                        struct device_attribute *devattr,
751                                        const char* buf, size_t count)
752 {
753         struct f71805f_data *data = dev_get_drvdata(dev);
754         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
755         int pwmnr = attr->nr;
756         int apnr = attr->index;
757         unsigned long val = simple_strtol(buf, NULL, 10);
758
759         mutex_lock(&data->update_lock);
760         data->auto_points[pwmnr].temp[apnr] = temp_to_reg(val);
761         f71805f_write8(data, F71805F_REG_PWM_AUTO_POINT_TEMP(pwmnr, apnr),
762                        data->auto_points[pwmnr].temp[apnr]);
763         mutex_unlock(&data->update_lock);
764
765         return count;
766 }
767
768 static ssize_t show_pwm_auto_point_fan(struct device *dev,
769                                        struct device_attribute *devattr,
770                                        char* buf)
771 {
772         struct f71805f_data *data = dev_get_drvdata(dev);
773         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
774         int pwmnr = attr->nr;
775         int apnr = attr->index;
776
777         return sprintf(buf, "%ld\n",
778                        fan_from_reg(data->auto_points[pwmnr].fan[apnr]));
779 }
780
781 static ssize_t set_pwm_auto_point_fan(struct device *dev,
782                                       struct device_attribute *devattr,
783                                       const char* buf, size_t count)
784 {
785         struct f71805f_data *data = dev_get_drvdata(dev);
786         struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
787         int pwmnr = attr->nr;
788         int apnr = attr->index;
789         unsigned long val = simple_strtoul(buf, NULL, 10);
790
791         mutex_lock(&data->update_lock);
792         data->auto_points[pwmnr].fan[apnr] = fan_to_reg(val);
793         f71805f_write16(data, F71805F_REG_PWM_AUTO_POINT_FAN(pwmnr, apnr),
794                         data->auto_points[pwmnr].fan[apnr]);
795         mutex_unlock(&data->update_lock);
796
797         return count;
798 }
799
800 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
801                          char *buf)
802 {
803         struct f71805f_data *data = f71805f_update_device(dev);
804         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
805         int nr = attr->index;
806
807         return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
808 }
809
810 static ssize_t show_temp_max(struct device *dev, struct device_attribute
811                              *devattr, char *buf)
812 {
813         struct f71805f_data *data = f71805f_update_device(dev);
814         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
815         int nr = attr->index;
816
817         return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
818 }
819
820 static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
821                               *devattr, char *buf)
822 {
823         struct f71805f_data *data = f71805f_update_device(dev);
824         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
825         int nr = attr->index;
826
827         return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
828 }
829
830 static ssize_t show_temp_type(struct device *dev, struct device_attribute
831                               *devattr, char *buf)
832 {
833         struct f71805f_data *data = f71805f_update_device(dev);
834         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
835         int nr = attr->index;
836
837         /* 3 is diode, 4 is thermistor */
838         return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
839 }
840
841 static ssize_t set_temp_max(struct device *dev, struct device_attribute
842                             *devattr, const char *buf, size_t count)
843 {
844         struct f71805f_data *data = dev_get_drvdata(dev);
845         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
846         int nr = attr->index;
847         long val = simple_strtol(buf, NULL, 10);
848
849         mutex_lock(&data->update_lock);
850         data->temp_high[nr] = temp_to_reg(val);
851         f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
852         mutex_unlock(&data->update_lock);
853
854         return count;
855 }
856
857 static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
858                              *devattr, const char *buf, size_t count)
859 {
860         struct f71805f_data *data = dev_get_drvdata(dev);
861         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
862         int nr = attr->index;
863         long val = simple_strtol(buf, NULL, 10);
864
865         mutex_lock(&data->update_lock);
866         data->temp_hyst[nr] = temp_to_reg(val);
867         f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
868         mutex_unlock(&data->update_lock);
869
870         return count;
871 }
872
873 static ssize_t show_alarms_in(struct device *dev, struct device_attribute
874                               *devattr, char *buf)
875 {
876         struct f71805f_data *data = f71805f_update_device(dev);
877
878         return sprintf(buf, "%lu\n", data->alarms & 0x7ff);
879 }
880
881 static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
882                                *devattr, char *buf)
883 {
884         struct f71805f_data *data = f71805f_update_device(dev);
885
886         return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
887 }
888
889 static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
890                                 *devattr, char *buf)
891 {
892         struct f71805f_data *data = f71805f_update_device(dev);
893
894         return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
895 }
896
897 static ssize_t show_alarm(struct device *dev, struct device_attribute
898                           *devattr, char *buf)
899 {
900         struct f71805f_data *data = f71805f_update_device(dev);
901         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
902         int bitnr = attr->index;
903
904         return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
905 }
906
907 static ssize_t show_name(struct device *dev, struct device_attribute
908                          *devattr, char *buf)
909 {
910         struct f71805f_data *data = dev_get_drvdata(dev);
911
912         return sprintf(buf, "%s\n", data->name);
913 }
914
915 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL, 0);
916 static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR,
917                           show_in0_max, set_in0_max, 0);
918 static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR,
919                           show_in0_min, set_in0_min, 0);
920 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
921 static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
922                           show_in_max, set_in_max, 1);
923 static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
924                           show_in_min, set_in_min, 1);
925 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
926 static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
927                           show_in_max, set_in_max, 2);
928 static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
929                           show_in_min, set_in_min, 2);
930 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
931 static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
932                           show_in_max, set_in_max, 3);
933 static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
934                           show_in_min, set_in_min, 3);
935 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
936 static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
937                           show_in_max, set_in_max, 4);
938 static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
939                           show_in_min, set_in_min, 4);
940 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
941 static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
942                           show_in_max, set_in_max, 5);
943 static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
944                           show_in_min, set_in_min, 5);
945 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
946 static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
947                           show_in_max, set_in_max, 6);
948 static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
949                           show_in_min, set_in_min, 6);
950 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
951 static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
952                           show_in_max, set_in_max, 7);
953 static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
954                           show_in_min, set_in_min, 7);
955 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
956 static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
957                           show_in_max, set_in_max, 8);
958 static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
959                           show_in_min, set_in_min, 8);
960 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in0, NULL, 9);
961 static SENSOR_DEVICE_ATTR(in9_max, S_IRUGO | S_IWUSR,
962                           show_in0_max, set_in0_max, 9);
963 static SENSOR_DEVICE_ATTR(in9_min, S_IRUGO | S_IWUSR,
964                           show_in0_min, set_in0_min, 9);
965 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in0, NULL, 10);
966 static SENSOR_DEVICE_ATTR(in10_max, S_IRUGO | S_IWUSR,
967                           show_in0_max, set_in0_max, 10);
968 static SENSOR_DEVICE_ATTR(in10_min, S_IRUGO | S_IWUSR,
969                           show_in0_min, set_in0_min, 10);
970
971 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
972 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
973                           show_fan_min, set_fan_min, 0);
974 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR,
975                           show_fan_target, set_fan_target, 0);
976 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
977 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
978                           show_fan_min, set_fan_min, 1);
979 static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO | S_IWUSR,
980                           show_fan_target, set_fan_target, 1);
981 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
982 static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
983                           show_fan_min, set_fan_min, 2);
984 static SENSOR_DEVICE_ATTR(fan3_target, S_IRUGO | S_IWUSR,
985                           show_fan_target, set_fan_target, 2);
986
987 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
988 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
989                     show_temp_max, set_temp_max, 0);
990 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
991                     show_temp_hyst, set_temp_hyst, 0);
992 static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
993 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
994 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
995                     show_temp_max, set_temp_max, 1);
996 static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
997                     show_temp_hyst, set_temp_hyst, 1);
998 static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
999 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1000 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
1001                     show_temp_max, set_temp_max, 2);
1002 static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
1003                     show_temp_hyst, set_temp_hyst, 2);
1004 static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
1005
1006 /* pwm (value) files are created read-only, write permission is
1007    then added or removed dynamically as needed */
1008 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
1009 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
1010                           show_pwm_enable, set_pwm_enable, 0);
1011 static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR,
1012                           show_pwm_freq, set_pwm_freq, 0);
1013 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
1014 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
1015 static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
1016                           show_pwm_enable, set_pwm_enable, 1);
1017 static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO | S_IWUSR,
1018                           show_pwm_freq, set_pwm_freq, 1);
1019 static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
1020 static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
1021 static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
1022                           show_pwm_enable, set_pwm_enable, 2);
1023 static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO | S_IWUSR,
1024                           show_pwm_freq, set_pwm_freq, 2);
1025 static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
1026
1027 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
1028                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1029                             0, 0);
1030 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_fan, S_IRUGO | S_IWUSR,
1031                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1032                             0, 0);
1033 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
1034                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1035                             0, 1);
1036 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_fan, S_IRUGO | S_IWUSR,
1037                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1038                             0, 1);
1039 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
1040                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1041                             0, 2);
1042 static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_fan, S_IRUGO | S_IWUSR,
1043                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1044                             0, 2);
1045
1046 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
1047                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1048                             1, 0);
1049 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_fan, S_IRUGO | S_IWUSR,
1050                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1051                             1, 0);
1052 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
1053                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1054                             1, 1);
1055 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_fan, S_IRUGO | S_IWUSR,
1056                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1057                             1, 1);
1058 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
1059                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1060                             1, 2);
1061 static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_fan, S_IRUGO | S_IWUSR,
1062                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1063                             1, 2);
1064
1065 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
1066                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1067                             2, 0);
1068 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_fan, S_IRUGO | S_IWUSR,
1069                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1070                             2, 0);
1071 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
1072                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1073                             2, 1);
1074 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_fan, S_IRUGO | S_IWUSR,
1075                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1076                             2, 1);
1077 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
1078                             show_pwm_auto_point_temp, set_pwm_auto_point_temp,
1079                             2, 2);
1080 static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_fan, S_IRUGO | S_IWUSR,
1081                             show_pwm_auto_point_fan, set_pwm_auto_point_fan,
1082                             2, 2);
1083
1084 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
1085 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
1086 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
1087 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
1088 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
1089 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
1090 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
1091 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
1092 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
1093 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
1094 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
1095 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
1096 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
1097 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
1098 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1099 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1100 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1101 static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
1102 static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
1103 static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
1104
1105 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
1106
1107 static struct attribute *f71805f_attributes[] = {
1108         &sensor_dev_attr_in0_input.dev_attr.attr,
1109         &sensor_dev_attr_in0_max.dev_attr.attr,
1110         &sensor_dev_attr_in0_min.dev_attr.attr,
1111         &sensor_dev_attr_in1_input.dev_attr.attr,
1112         &sensor_dev_attr_in1_max.dev_attr.attr,
1113         &sensor_dev_attr_in1_min.dev_attr.attr,
1114         &sensor_dev_attr_in2_input.dev_attr.attr,
1115         &sensor_dev_attr_in2_max.dev_attr.attr,
1116         &sensor_dev_attr_in2_min.dev_attr.attr,
1117         &sensor_dev_attr_in3_input.dev_attr.attr,
1118         &sensor_dev_attr_in3_max.dev_attr.attr,
1119         &sensor_dev_attr_in3_min.dev_attr.attr,
1120         &sensor_dev_attr_in5_input.dev_attr.attr,
1121         &sensor_dev_attr_in5_max.dev_attr.attr,
1122         &sensor_dev_attr_in5_min.dev_attr.attr,
1123         &sensor_dev_attr_in6_input.dev_attr.attr,
1124         &sensor_dev_attr_in6_max.dev_attr.attr,
1125         &sensor_dev_attr_in6_min.dev_attr.attr,
1126         &sensor_dev_attr_in7_input.dev_attr.attr,
1127         &sensor_dev_attr_in7_max.dev_attr.attr,
1128         &sensor_dev_attr_in7_min.dev_attr.attr,
1129
1130         &sensor_dev_attr_fan1_input.dev_attr.attr,
1131         &sensor_dev_attr_fan1_min.dev_attr.attr,
1132         &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1133         &sensor_dev_attr_fan1_target.dev_attr.attr,
1134         &sensor_dev_attr_fan2_input.dev_attr.attr,
1135         &sensor_dev_attr_fan2_min.dev_attr.attr,
1136         &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1137         &sensor_dev_attr_fan2_target.dev_attr.attr,
1138         &sensor_dev_attr_fan3_input.dev_attr.attr,
1139         &sensor_dev_attr_fan3_min.dev_attr.attr,
1140         &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1141         &sensor_dev_attr_fan3_target.dev_attr.attr,
1142
1143         &sensor_dev_attr_pwm1.dev_attr.attr,
1144         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1145         &sensor_dev_attr_pwm1_mode.dev_attr.attr,
1146         &sensor_dev_attr_pwm2.dev_attr.attr,
1147         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1148         &sensor_dev_attr_pwm2_mode.dev_attr.attr,
1149         &sensor_dev_attr_pwm3.dev_attr.attr,
1150         &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1151         &sensor_dev_attr_pwm3_mode.dev_attr.attr,
1152
1153         &sensor_dev_attr_temp1_input.dev_attr.attr,
1154         &sensor_dev_attr_temp1_max.dev_attr.attr,
1155         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
1156         &sensor_dev_attr_temp1_type.dev_attr.attr,
1157         &sensor_dev_attr_temp2_input.dev_attr.attr,
1158         &sensor_dev_attr_temp2_max.dev_attr.attr,
1159         &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
1160         &sensor_dev_attr_temp2_type.dev_attr.attr,
1161         &sensor_dev_attr_temp3_input.dev_attr.attr,
1162         &sensor_dev_attr_temp3_max.dev_attr.attr,
1163         &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
1164         &sensor_dev_attr_temp3_type.dev_attr.attr,
1165
1166         &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
1167         &sensor_dev_attr_pwm1_auto_point1_fan.dev_attr.attr,
1168         &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
1169         &sensor_dev_attr_pwm1_auto_point2_fan.dev_attr.attr,
1170         &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
1171         &sensor_dev_attr_pwm1_auto_point3_fan.dev_attr.attr,
1172         &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
1173         &sensor_dev_attr_pwm2_auto_point1_fan.dev_attr.attr,
1174         &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
1175         &sensor_dev_attr_pwm2_auto_point2_fan.dev_attr.attr,
1176         &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
1177         &sensor_dev_attr_pwm2_auto_point3_fan.dev_attr.attr,
1178         &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
1179         &sensor_dev_attr_pwm3_auto_point1_fan.dev_attr.attr,
1180         &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
1181         &sensor_dev_attr_pwm3_auto_point2_fan.dev_attr.attr,
1182         &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
1183         &sensor_dev_attr_pwm3_auto_point3_fan.dev_attr.attr,
1184
1185         &sensor_dev_attr_in0_alarm.dev_attr.attr,
1186         &sensor_dev_attr_in1_alarm.dev_attr.attr,
1187         &sensor_dev_attr_in2_alarm.dev_attr.attr,
1188         &sensor_dev_attr_in3_alarm.dev_attr.attr,
1189         &sensor_dev_attr_in5_alarm.dev_attr.attr,
1190         &sensor_dev_attr_in6_alarm.dev_attr.attr,
1191         &sensor_dev_attr_in7_alarm.dev_attr.attr,
1192         &dev_attr_alarms_in.attr,
1193         &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1194         &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1195         &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1196         &dev_attr_alarms_temp.attr,
1197         &dev_attr_alarms_fan.attr,
1198
1199         &dev_attr_name.attr,
1200         NULL
1201 };
1202
1203 static const struct attribute_group f71805f_group = {
1204         .attrs = f71805f_attributes,
1205 };
1206
1207 static struct attribute *f71805f_attributes_optin[4][5] = {
1208         {
1209                 &sensor_dev_attr_in4_input.dev_attr.attr,
1210                 &sensor_dev_attr_in4_max.dev_attr.attr,
1211                 &sensor_dev_attr_in4_min.dev_attr.attr,
1212                 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1213                 NULL
1214         }, {
1215                 &sensor_dev_attr_in8_input.dev_attr.attr,
1216                 &sensor_dev_attr_in8_max.dev_attr.attr,
1217                 &sensor_dev_attr_in8_min.dev_attr.attr,
1218                 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1219                 NULL
1220         }, {
1221                 &sensor_dev_attr_in9_input.dev_attr.attr,
1222                 &sensor_dev_attr_in9_max.dev_attr.attr,
1223                 &sensor_dev_attr_in9_min.dev_attr.attr,
1224                 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1225                 NULL
1226         }, {
1227                 &sensor_dev_attr_in10_input.dev_attr.attr,
1228                 &sensor_dev_attr_in10_max.dev_attr.attr,
1229                 &sensor_dev_attr_in10_min.dev_attr.attr,
1230                 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1231                 NULL
1232         }
1233 };
1234
1235 static const struct attribute_group f71805f_group_optin[4] = {
1236         { .attrs = f71805f_attributes_optin[0] },
1237         { .attrs = f71805f_attributes_optin[1] },
1238         { .attrs = f71805f_attributes_optin[2] },
1239         { .attrs = f71805f_attributes_optin[3] },
1240 };
1241
1242 /* We don't include pwm_freq files in the arrays above, because they must be
1243    created conditionally (only if pwm_mode is 1 == PWM) */
1244 static struct attribute *f71805f_attributes_pwm_freq[] = {
1245         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
1246         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
1247         &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1248         NULL
1249 };
1250
1251 static const struct attribute_group f71805f_group_pwm_freq = {
1252         .attrs = f71805f_attributes_pwm_freq,
1253 };
1254
1255 /* We also need an indexed access to pwmN files to toggle writability */
1256 static struct attribute *f71805f_attr_pwm[] = {
1257         &sensor_dev_attr_pwm1.dev_attr.attr,
1258         &sensor_dev_attr_pwm2.dev_attr.attr,
1259         &sensor_dev_attr_pwm3.dev_attr.attr,
1260 };
1261
1262 /*
1263  * Device registration and initialization
1264  */
1265
1266 static void __devinit f71805f_init_device(struct f71805f_data *data)
1267 {
1268         u8 reg;
1269         int i;
1270
1271         reg = f71805f_read8(data, F71805F_REG_START);
1272         if ((reg & 0x41) != 0x01) {
1273                 printk(KERN_DEBUG DRVNAME ": Starting monitoring "
1274                        "operations\n");
1275                 f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
1276         }
1277
1278         /* Fan monitoring can be disabled. If it is, we won't be polling
1279            the register values, and won't create the related sysfs files. */
1280         for (i = 0; i < 3; i++) {
1281                 data->fan_ctrl[i] = f71805f_read8(data,
1282                                                   F71805F_REG_FAN_CTRL(i));
1283                 /* Clear latch full bit, else "speed mode" fan speed control
1284                    doesn't work */
1285                 if (data->fan_ctrl[i] & FAN_CTRL_LATCH_FULL) {
1286                         data->fan_ctrl[i] &= ~FAN_CTRL_LATCH_FULL;
1287                         f71805f_write8(data, F71805F_REG_FAN_CTRL(i),
1288                                        data->fan_ctrl[i]);
1289                 }
1290         }
1291 }
1292
1293 static int __devinit f71805f_probe(struct platform_device *pdev)
1294 {
1295         struct f71805f_sio_data *sio_data = pdev->dev.platform_data;
1296         struct f71805f_data *data;
1297         struct resource *res;
1298         int i, err;
1299
1300         static const char *names[] = {
1301                 "f71805f",
1302                 "f71872f",
1303         };
1304
1305         if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
1306                 err = -ENOMEM;
1307                 printk(KERN_ERR DRVNAME ": Out of memory\n");
1308                 goto exit;
1309         }
1310
1311         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1312         if (!request_region(res->start + ADDR_REG_OFFSET, 2, DRVNAME)) {
1313                 err = -EBUSY;
1314                 dev_err(&pdev->dev, "Failed to request region 0x%lx-0x%lx\n",
1315                         (unsigned long)(res->start + ADDR_REG_OFFSET),
1316                         (unsigned long)(res->start + ADDR_REG_OFFSET + 1));
1317                 goto exit_free;
1318         }
1319         data->addr = res->start;
1320         data->name = names[sio_data->kind];
1321         mutex_init(&data->update_lock);
1322
1323         platform_set_drvdata(pdev, data);
1324
1325         /* Some voltage inputs depend on chip model and configuration */
1326         switch (sio_data->kind) {
1327         case f71805f:
1328                 data->has_in = 0x1ff;
1329                 break;
1330         case f71872f:
1331                 data->has_in = 0x6ef;
1332                 if (sio_data->fnsel1 & 0x01)
1333                         data->has_in |= (1 << 4); /* in4 */
1334                 if (sio_data->fnsel1 & 0x02)
1335                         data->has_in |= (1 << 8); /* in8 */
1336                 break;
1337         }
1338
1339         /* Initialize the F71805F chip */
1340         f71805f_init_device(data);
1341
1342         /* Register sysfs interface files */
1343         if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
1344                 goto exit_release_region;
1345         if (data->has_in & (1 << 4)) { /* in4 */
1346                 if ((err = sysfs_create_group(&pdev->dev.kobj,
1347                                               &f71805f_group_optin[0])))
1348                         goto exit_remove_files;
1349         }
1350         if (data->has_in & (1 << 8)) { /* in8 */
1351                 if ((err = sysfs_create_group(&pdev->dev.kobj,
1352                                               &f71805f_group_optin[1])))
1353                         goto exit_remove_files;
1354         }
1355         if (data->has_in & (1 << 9)) { /* in9 (F71872F/FG only) */
1356                 if ((err = sysfs_create_group(&pdev->dev.kobj,
1357                                               &f71805f_group_optin[2])))
1358                         goto exit_remove_files;
1359         }
1360         if (data->has_in & (1 << 10)) { /* in9 (F71872F/FG only) */
1361                 if ((err = sysfs_create_group(&pdev->dev.kobj,
1362                                               &f71805f_group_optin[3])))
1363                         goto exit_remove_files;
1364         }
1365         for (i = 0; i < 3; i++) {
1366                 /* If control mode is PWM, create pwm_freq file */
1367                 if (!(data->fan_ctrl[i] & FAN_CTRL_DC_MODE)) {
1368                         if ((err = sysfs_create_file(&pdev->dev.kobj,
1369                                         f71805f_attributes_pwm_freq[i])))
1370                                 goto exit_remove_files;
1371                 }
1372                 /* If PWM is in manual mode, add write permission */
1373                 if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
1374                         if ((err = sysfs_chmod_file(&pdev->dev.kobj,
1375                                                     f71805f_attr_pwm[i],
1376                                                     S_IRUGO | S_IWUSR))) {
1377                                 dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
1378                                         i + 1);
1379                                 goto exit_remove_files;
1380                         }
1381                 }
1382         }
1383
1384         data->class_dev = hwmon_device_register(&pdev->dev);
1385         if (IS_ERR(data->class_dev)) {
1386                 err = PTR_ERR(data->class_dev);
1387                 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
1388                 goto exit_remove_files;
1389         }
1390
1391         return 0;
1392
1393 exit_remove_files:
1394         sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
1395         for (i = 0; i < 4; i++)
1396                 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
1397         sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
1398 exit_release_region:
1399         release_region(res->start + ADDR_REG_OFFSET, 2);
1400 exit_free:
1401         platform_set_drvdata(pdev, NULL);
1402         kfree(data);
1403 exit:
1404         return err;
1405 }
1406
1407 static int __devexit f71805f_remove(struct platform_device *pdev)
1408 {
1409         struct f71805f_data *data = platform_get_drvdata(pdev);
1410         struct resource *res;
1411         int i;
1412
1413         hwmon_device_unregister(data->class_dev);
1414         sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
1415         for (i = 0; i < 4; i++)
1416                 sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
1417         sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
1418         platform_set_drvdata(pdev, NULL);
1419         kfree(data);
1420
1421         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1422         release_region(res->start + ADDR_REG_OFFSET, 2);
1423
1424         return 0;
1425 }
1426
1427 static struct platform_driver f71805f_driver = {
1428         .driver = {
1429                 .owner  = THIS_MODULE,
1430                 .name   = DRVNAME,
1431         },
1432         .probe          = f71805f_probe,
1433         .remove         = __devexit_p(f71805f_remove),
1434 };
1435
1436 static int __init f71805f_device_add(unsigned short address,
1437                                      const struct f71805f_sio_data *sio_data)
1438 {
1439         struct resource res = {
1440                 .start  = address,
1441                 .end    = address + REGION_LENGTH - 1,
1442                 .flags  = IORESOURCE_IO,
1443         };
1444         int err;
1445
1446         pdev = platform_device_alloc(DRVNAME, address);
1447         if (!pdev) {
1448                 err = -ENOMEM;
1449                 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
1450                 goto exit;
1451         }
1452
1453         res.name = pdev->name;
1454         err = platform_device_add_resources(pdev, &res, 1);
1455         if (err) {
1456                 printk(KERN_ERR DRVNAME ": Device resource addition failed "
1457                        "(%d)\n", err);
1458                 goto exit_device_put;
1459         }
1460
1461         err = platform_device_add_data(pdev, sio_data,
1462                                        sizeof(struct f71805f_sio_data));
1463         if (err) {
1464                 printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
1465                 goto exit_device_put;
1466         }
1467
1468         err = platform_device_add(pdev);
1469         if (err) {
1470                 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
1471                        err);
1472                 goto exit_device_put;
1473         }
1474
1475         return 0;
1476
1477 exit_device_put:
1478         platform_device_put(pdev);
1479 exit:
1480         return err;
1481 }
1482
1483 static int __init f71805f_find(int sioaddr, unsigned short *address,
1484                                struct f71805f_sio_data *sio_data)
1485 {
1486         int err = -ENODEV;
1487         u16 devid;
1488
1489         static const char *names[] = {
1490                 "F71805F/FG",
1491                 "F71872F/FG or F71806F/FG",
1492         };
1493
1494         superio_enter(sioaddr);
1495
1496         devid = superio_inw(sioaddr, SIO_REG_MANID);
1497         if (devid != SIO_FINTEK_ID)
1498                 goto exit;
1499
1500         devid = superio_inw(sioaddr, SIO_REG_DEVID);
1501         switch (devid) {
1502         case SIO_F71805F_ID:
1503                 sio_data->kind = f71805f;
1504                 break;
1505         case SIO_F71872F_ID:
1506                 sio_data->kind = f71872f;
1507                 sio_data->fnsel1 = superio_inb(sioaddr, SIO_REG_FNSEL1);
1508                 break;
1509         default:
1510                 printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
1511                        "skipping\n");
1512                 goto exit;
1513         }
1514
1515         superio_select(sioaddr, F71805F_LD_HWM);
1516         if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1517                 printk(KERN_WARNING DRVNAME ": Device not activated, "
1518                        "skipping\n");
1519                 goto exit;
1520         }
1521
1522         *address = superio_inw(sioaddr, SIO_REG_ADDR);
1523         if (*address == 0) {
1524                 printk(KERN_WARNING DRVNAME ": Base address not set, "
1525                        "skipping\n");
1526                 goto exit;
1527         }
1528         *address &= ~(REGION_LENGTH - 1);       /* Ignore 3 LSB */
1529
1530         err = 0;
1531         printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %u\n",
1532                names[sio_data->kind], *address,
1533                superio_inb(sioaddr, SIO_REG_DEVREV));
1534
1535 exit:
1536         superio_exit(sioaddr);
1537         return err;
1538 }
1539
1540 static int __init f71805f_init(void)
1541 {
1542         int err;
1543         unsigned short address;
1544         struct f71805f_sio_data sio_data;
1545
1546         if (f71805f_find(0x2e, &address, &sio_data)
1547          && f71805f_find(0x4e, &address, &sio_data))
1548                 return -ENODEV;
1549
1550         err = platform_driver_register(&f71805f_driver);
1551         if (err)
1552                 goto exit;
1553
1554         /* Sets global pdev as a side effect */
1555         err = f71805f_device_add(address, &sio_data);
1556         if (err)
1557                 goto exit_driver;
1558
1559         return 0;
1560
1561 exit_driver:
1562         platform_driver_unregister(&f71805f_driver);
1563 exit:
1564         return err;
1565 }
1566
1567 static void __exit f71805f_exit(void)
1568 {
1569         platform_device_unregister(pdev);
1570         platform_driver_unregister(&f71805f_driver);
1571 }
1572
1573 MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
1574 MODULE_LICENSE("GPL");
1575 MODULE_DESCRIPTION("F71805F/F71872F hardware monitoring driver");
1576
1577 module_init(f71805f_init);
1578 module_exit(f71805f_exit);