hwmon: (f71882fg) Add PWM support
[safe/jmp/linux-2.6] / drivers / hwmon / f71882fg.c
1 /***************************************************************************
2  *   Copyright (C) 2006 by Hans Edgington <hans@edgington.nl>              *
3  *   Copyright (C) 2007 by Hans de Goede  <j.w.r.degoede@hhs.nl>           *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
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  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/platform_device.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/io.h>
31
32 #define DRVNAME "f71882fg"
33
34 #define SIO_F71882FG_LD_HWM     0x04    /* Hardware monitor logical device */
35 #define SIO_UNLOCK_KEY          0x87    /* Key to enable Super-I/O */
36 #define SIO_LOCK_KEY            0xAA    /* Key to diasble Super-I/O */
37
38 #define SIO_REG_LDSEL           0x07    /* Logical device select */
39 #define SIO_REG_DEVID           0x20    /* Device ID (2 bytes) */
40 #define SIO_REG_DEVREV          0x22    /* Device revision */
41 #define SIO_REG_MANID           0x23    /* Fintek ID (2 bytes) */
42 #define SIO_REG_ENABLE          0x30    /* Logical device enable */
43 #define SIO_REG_ADDR            0x60    /* Logical device address (2 bytes) */
44
45 #define SIO_FINTEK_ID           0x1934  /* Manufacturers ID */
46 #define SIO_F71882_ID           0x0541  /* Chipset ID */
47
48 #define REGION_LENGTH           8
49 #define ADDR_REG_OFFSET         5
50 #define DATA_REG_OFFSET         6
51
52 #define F71882FG_REG_PECI               0x0A
53
54 #define F71882FG_REG_IN_STATUS          0x12
55 #define F71882FG_REG_IN_BEEP            0x13
56 #define F71882FG_REG_IN(nr)             (0x20  + (nr))
57 #define F71882FG_REG_IN1_HIGH           0x32
58
59 #define F71882FG_REG_FAN(nr)            (0xA0 + (16 * (nr)))
60 #define F71882FG_REG_FAN_TARGET(nr)     (0xA2 + (16 * (nr)))
61 #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
62 #define F71882FG_REG_FAN_STATUS         0x92
63 #define F71882FG_REG_FAN_BEEP           0x93
64
65 #define F71882FG_REG_TEMP(nr)           (0x72 + 2 * (nr))
66 #define F71882FG_REG_TEMP_OVT(nr)       (0x82 + 2 * (nr))
67 #define F71882FG_REG_TEMP_HIGH(nr)      (0x83 + 2 * (nr))
68 #define F71882FG_REG_TEMP_STATUS        0x62
69 #define F71882FG_REG_TEMP_BEEP          0x63
70 #define F71882FG_REG_TEMP_HYST1         0x6C
71 #define F71882FG_REG_TEMP_HYST23        0x6D
72 #define F71882FG_REG_TEMP_TYPE          0x6B
73 #define F71882FG_REG_TEMP_DIODE_OPEN    0x6F
74
75 #define F71882FG_REG_PWM(nr)            (0xA3 + (16 * (nr)))
76 #define F71882FG_REG_PWM_TYPE           0x94
77 #define F71882FG_REG_PWM_ENABLE         0x96
78
79 #define F71882FG_REG_FAN_HYST0          0x98
80 #define F71882FG_REG_FAN_HYST1          0x99
81
82 #define F71882FG_REG_POINT_PWM(pwm, point)      (0xAA + (point) + (16 * (pwm)))
83 #define F71882FG_REG_POINT_TEMP(pwm, point)     (0xA6 + (point) + (16 * (pwm)))
84 #define F71882FG_REG_POINT_MAPPING(nr)          (0xAF + 16 * (nr))
85
86 #define F71882FG_REG_START              0x01
87
88 #define FAN_MIN_DETECT                  366 /* Lowest detectable fanspeed */
89
90 static unsigned short force_id;
91 module_param(force_id, ushort, 0);
92 MODULE_PARM_DESC(force_id, "Override the detected device ID");
93
94 static int fan_mode[4] = { 0, 0, 0, 0 };
95 module_param_array(fan_mode, int, NULL, 0644);
96 MODULE_PARM_DESC(fan_mode, "List of fan control modes (f71882fg only) "
97                  "(0=don't change, 1=pwm, 2=rpm)\n"
98                  "Note: this needs a write to pwm#_enable to take effect");
99
100 static struct platform_device *f71882fg_pdev;
101
102 /* Super-I/O Function prototypes */
103 static inline int superio_inb(int base, int reg);
104 static inline int superio_inw(int base, int reg);
105 static inline void superio_enter(int base);
106 static inline void superio_select(int base, int ld);
107 static inline void superio_exit(int base);
108
109 struct f71882fg_data {
110         unsigned short addr;
111         struct device *hwmon_dev;
112
113         struct mutex update_lock;
114         char valid;                     /* !=0 if following fields are valid */
115         unsigned long last_updated;     /* In jiffies */
116         unsigned long last_limits;      /* In jiffies */
117
118         /* Register Values */
119         u8      in[9];
120         u8      in1_max;
121         u8      in_status;
122         u8      in_beep;
123         u16     fan[4];
124         u16     fan_target[4];
125         u16     fan_full_speed[4];
126         u8      fan_status;
127         u8      fan_beep;
128         u8      temp[3];
129         u8      temp_ovt[3];
130         u8      temp_high[3];
131         u8      temp_hyst[3];
132         u8      temp_type[3];
133         u8      temp_status;
134         u8      temp_beep;
135         u8      temp_diode_open;
136         u8      pwm[4];
137         u8      pwm_enable;
138         u8      pwm_auto_point_hyst[2];
139         u8      pwm_auto_point_mapping[4];
140         u8      pwm_auto_point_pwm[4][5];
141         u8      pwm_auto_point_temp[4][4];
142 };
143
144 /* Sysfs in */
145 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
146         char *buf);
147 static ssize_t show_in_max(struct device *dev, struct device_attribute
148         *devattr, char *buf);
149 static ssize_t store_in_max(struct device *dev, struct device_attribute
150         *devattr, const char *buf, size_t count);
151 static ssize_t show_in_beep(struct device *dev, struct device_attribute
152         *devattr, char *buf);
153 static ssize_t store_in_beep(struct device *dev, struct device_attribute
154         *devattr, const char *buf, size_t count);
155 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
156         *devattr, char *buf);
157 /* Sysfs Fan */
158 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
159         char *buf);
160 static ssize_t show_fan_full_speed(struct device *dev,
161         struct device_attribute *devattr, char *buf);
162 static ssize_t store_fan_full_speed(struct device *dev,
163         struct device_attribute *devattr, const char *buf, size_t count);
164 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
165         *devattr, char *buf);
166 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
167         *devattr, const char *buf, size_t count);
168 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
169         *devattr, char *buf);
170 /* Sysfs Temp */
171 static ssize_t show_temp(struct device *dev, struct device_attribute
172         *devattr, char *buf);
173 static ssize_t show_temp_max(struct device *dev, struct device_attribute
174         *devattr, char *buf);
175 static ssize_t store_temp_max(struct device *dev, struct device_attribute
176         *devattr, const char *buf, size_t count);
177 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
178         *devattr, char *buf);
179 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
180         *devattr, const char *buf, size_t count);
181 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
182         *devattr, char *buf);
183 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
184         *devattr, const char *buf, size_t count);
185 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
186         *devattr, char *buf);
187 static ssize_t show_temp_type(struct device *dev, struct device_attribute
188         *devattr, char *buf);
189 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
190         *devattr, char *buf);
191 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
192         *devattr, const char *buf, size_t count);
193 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
194         *devattr, char *buf);
195 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
196         *devattr, char *buf);
197 /* PWM and Auto point control */
198 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
199         char *buf);
200 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
201         const char *buf, size_t count);
202 static ssize_t show_pwm_enable(struct device *dev,
203         struct device_attribute *devattr, char *buf);
204 static ssize_t store_pwm_enable(struct device *dev,
205         struct device_attribute *devattr, const char *buf, size_t count);
206 static ssize_t show_pwm_interpolate(struct device *dev,
207         struct device_attribute *devattr, char *buf);
208 static ssize_t store_pwm_interpolate(struct device *dev,
209         struct device_attribute *devattr, const char *buf, size_t count);
210 static ssize_t show_pwm_auto_point_channel(struct device *dev,
211         struct device_attribute *devattr, char *buf);
212 static ssize_t store_pwm_auto_point_channel(struct device *dev,
213         struct device_attribute *devattr, const char *buf, size_t count);
214 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
215         struct device_attribute *devattr, char *buf);
216 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
217         struct device_attribute *devattr, const char *buf, size_t count);
218 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
219         struct device_attribute *devattr, char *buf);
220 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
221         struct device_attribute *devattr, const char *buf, size_t count);
222 static ssize_t show_pwm_auto_point_temp(struct device *dev,
223         struct device_attribute *devattr, char *buf);
224 static ssize_t store_pwm_auto_point_temp(struct device *dev,
225         struct device_attribute *devattr, const char *buf, size_t count);
226 /* Sysfs misc */
227 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
228         char *buf);
229
230 static int __devinit f71882fg_probe(struct platform_device * pdev);
231 static int __devexit f71882fg_remove(struct platform_device *pdev);
232 static int __init f71882fg_init(void);
233 static int __init f71882fg_find(int sioaddr, unsigned short *address);
234 static int __init f71882fg_device_add(unsigned short address);
235 static void __exit f71882fg_exit(void);
236
237 static struct platform_driver f71882fg_driver = {
238         .driver = {
239                 .owner  = THIS_MODULE,
240                 .name   = DRVNAME,
241         },
242         .probe          = f71882fg_probe,
243         .remove         = __devexit_p(f71882fg_remove),
244 };
245
246 static struct device_attribute f71882fg_dev_attr[] =
247 {
248         __ATTR( name, S_IRUGO, show_name, NULL ),
249 };
250
251 static struct sensor_device_attribute_2 f71882fg_in_temp_attr[] = {
252         SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
253         SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
254         SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
255                 0, 1),
256         SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
257                 0, 1),
258         SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
259         SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
260         SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
261         SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
262         SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
263         SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
264         SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
265         SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
266         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
267         SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
268                 store_temp_max, 0, 0),
269         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
270                 store_temp_max_hyst, 0, 0),
271         SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
272                 store_temp_crit, 0, 0),
273         SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
274                 0, 0),
275         SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 0),
276         SENSOR_ATTR_2(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
277                 store_temp_beep, 0, 0),
278         SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 0),
279         SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
280         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
281         SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
282                 store_temp_max, 0, 1),
283         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
284                 store_temp_max_hyst, 0, 1),
285         SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
286                 store_temp_crit, 0, 1),
287         SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
288                 0, 1),
289         SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 1),
290         SENSOR_ATTR_2(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
291                 store_temp_beep, 0, 1),
292         SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
293         SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
294         SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
295         SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
296                 store_temp_max, 0, 2),
297         SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
298                 store_temp_max_hyst, 0, 2),
299         SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
300                 store_temp_crit, 0, 2),
301         SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
302                 0, 2),
303         SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 2),
304         SENSOR_ATTR_2(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
305                 store_temp_beep, 0, 2),
306         SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
307         SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
308 };
309
310 static struct sensor_device_attribute_2 f71882fg_fan_attr[] = {
311         SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
312         SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
313                       show_fan_full_speed,
314                       store_fan_full_speed, 0, 0),
315         SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
316                 store_fan_beep, 0, 0),
317         SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
318         SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
319         SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
320                       show_fan_full_speed,
321                       store_fan_full_speed, 0, 1),
322         SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
323                 store_fan_beep, 0, 1),
324         SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
325         SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
326         SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
327                       show_fan_full_speed,
328                       store_fan_full_speed, 0, 2),
329         SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
330                 store_fan_beep, 0, 2),
331         SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
332         SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
333         SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
334                       show_fan_full_speed,
335                       store_fan_full_speed, 0, 3),
336         SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
337                 store_fan_beep, 0, 3),
338         SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
339
340         SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
341         SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
342                       store_pwm_enable, 0, 0),
343         SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
344                       show_pwm_interpolate, store_pwm_interpolate, 0, 0),
345         SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
346                       show_pwm_auto_point_channel,
347                       store_pwm_auto_point_channel, 0, 0),
348         SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
349                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
350                       0, 0),
351         SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
352                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
353                       1, 0),
354         SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
355                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
356                       2, 0),
357         SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
358                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
359                       3, 0),
360         SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
361                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
362                       4, 0),
363         SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
364                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
365                       0, 0),
366         SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
367                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
368                       1, 0),
369         SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
370                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
371                       2, 0),
372         SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
373                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
374                       3, 0),
375         SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
376                       show_pwm_auto_point_temp_hyst,
377                       store_pwm_auto_point_temp_hyst,
378                       0, 0),
379         SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
380                       show_pwm_auto_point_temp_hyst, NULL, 1, 0),
381         SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
382                       show_pwm_auto_point_temp_hyst, NULL, 2, 0),
383         SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
384                       show_pwm_auto_point_temp_hyst, NULL, 3, 0),
385
386         SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
387         SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
388                       store_pwm_enable, 0, 1),
389         SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
390                       show_pwm_interpolate, store_pwm_interpolate, 0, 1),
391         SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
392                       show_pwm_auto_point_channel,
393                       store_pwm_auto_point_channel, 0, 1),
394         SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
395                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
396                       0, 1),
397         SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
398                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
399                       1, 1),
400         SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
401                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
402                       2, 1),
403         SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
404                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
405                       3, 1),
406         SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
407                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
408                       4, 1),
409         SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
410                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
411                       0, 1),
412         SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
413                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
414                       1, 1),
415         SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
416                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
417                       2, 1),
418         SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
419                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
420                       3, 1),
421         SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
422                       show_pwm_auto_point_temp_hyst,
423                       store_pwm_auto_point_temp_hyst,
424                       0, 1),
425         SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
426                       show_pwm_auto_point_temp_hyst, NULL, 1, 1),
427         SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
428                       show_pwm_auto_point_temp_hyst, NULL, 2, 1),
429         SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
430                       show_pwm_auto_point_temp_hyst, NULL, 3, 1),
431
432         SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
433         SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
434                       store_pwm_enable, 0, 2),
435         SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
436                       show_pwm_interpolate, store_pwm_interpolate, 0, 2),
437         SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
438                       show_pwm_auto_point_channel,
439                       store_pwm_auto_point_channel, 0, 2),
440         SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
441                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
442                       0, 2),
443         SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
444                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
445                       1, 2),
446         SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
447                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
448                       2, 2),
449         SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
450                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
451                       3, 2),
452         SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
453                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
454                       4, 2),
455         SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
456                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
457                       0, 2),
458         SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
459                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
460                       1, 2),
461         SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
462                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
463                       2, 2),
464         SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
465                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
466                       3, 2),
467         SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
468                       show_pwm_auto_point_temp_hyst,
469                       store_pwm_auto_point_temp_hyst,
470                       0, 2),
471         SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
472                       show_pwm_auto_point_temp_hyst, NULL, 1, 2),
473         SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
474                       show_pwm_auto_point_temp_hyst, NULL, 2, 2),
475         SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
476                       show_pwm_auto_point_temp_hyst, NULL, 3, 2),
477
478         SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
479         SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
480                       store_pwm_enable, 0, 3),
481         SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
482                       show_pwm_interpolate, store_pwm_interpolate, 0, 3),
483         SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
484                       show_pwm_auto_point_channel,
485                       store_pwm_auto_point_channel, 0, 3),
486         SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
487                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
488                       0, 3),
489         SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
490                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
491                       1, 3),
492         SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
493                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
494                       2, 3),
495         SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
496                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
497                       3, 3),
498         SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
499                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
500                       4, 3),
501         SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
502                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
503                       0, 3),
504         SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
505                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
506                       1, 3),
507         SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
508                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
509                       2, 3),
510         SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
511                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
512                       3, 3),
513         SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
514                       show_pwm_auto_point_temp_hyst,
515                       store_pwm_auto_point_temp_hyst,
516                       0, 3),
517         SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
518                       show_pwm_auto_point_temp_hyst, NULL, 1, 3),
519         SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
520                       show_pwm_auto_point_temp_hyst, NULL, 2, 3),
521         SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
522                       show_pwm_auto_point_temp_hyst, NULL, 3, 3),
523 };
524
525
526 /* Super I/O functions */
527 static inline int superio_inb(int base, int reg)
528 {
529         outb(reg, base);
530         return inb(base + 1);
531 }
532
533 static int superio_inw(int base, int reg)
534 {
535         int val;
536         outb(reg++, base);
537         val = inb(base + 1) << 8;
538         outb(reg, base);
539         val |= inb(base + 1);
540         return val;
541 }
542
543 static inline void superio_enter(int base)
544 {
545         /* according to the datasheet the key must be send twice! */
546         outb( SIO_UNLOCK_KEY, base);
547         outb( SIO_UNLOCK_KEY, base);
548 }
549
550 static inline void superio_select( int base, int ld)
551 {
552         outb(SIO_REG_LDSEL, base);
553         outb(ld, base + 1);
554 }
555
556 static inline void superio_exit(int base)
557 {
558         outb(SIO_LOCK_KEY, base);
559 }
560
561 static inline u16 fan_from_reg(u16 reg)
562 {
563         return reg ? (1500000 / reg) : 0;
564 }
565
566 static inline u16 fan_to_reg(u16 fan)
567 {
568         return fan ? (1500000 / fan) : 0;
569 }
570
571 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
572 {
573         u8 val;
574
575         outb(reg, data->addr + ADDR_REG_OFFSET);
576         val = inb(data->addr + DATA_REG_OFFSET);
577
578         return val;
579 }
580
581 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
582 {
583         u16 val;
584
585         outb(reg++, data->addr + ADDR_REG_OFFSET);
586         val = inb(data->addr + DATA_REG_OFFSET) << 8;
587         outb(reg, data->addr + ADDR_REG_OFFSET);
588         val |= inb(data->addr + DATA_REG_OFFSET);
589
590         return val;
591 }
592
593 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
594 {
595         outb(reg, data->addr + ADDR_REG_OFFSET);
596         outb(val, data->addr + DATA_REG_OFFSET);
597 }
598
599 static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
600 {
601         outb(reg++, data->addr + ADDR_REG_OFFSET);
602         outb(val >> 8, data->addr + DATA_REG_OFFSET);
603         outb(reg, data->addr + ADDR_REG_OFFSET);
604         outb(val & 255, data->addr + DATA_REG_OFFSET);
605 }
606
607 static struct f71882fg_data *f71882fg_update_device(struct device *dev)
608 {
609         struct f71882fg_data *data = dev_get_drvdata(dev);
610         int nr, reg, reg2;
611
612         mutex_lock(&data->update_lock);
613
614         /* Update once every 60 seconds */
615         if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
616                         !data->valid) {
617                 data->in1_max = f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
618                 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
619
620                 /* Get High & boundary temps*/
621                 for (nr = 0; nr < 3; nr++) {
622                         data->temp_ovt[nr] = f71882fg_read8(data,
623                                                 F71882FG_REG_TEMP_OVT(nr));
624                         data->temp_high[nr] = f71882fg_read8(data,
625                                                 F71882FG_REG_TEMP_HIGH(nr));
626                 }
627
628                 /* Have to hardcode hyst*/
629                 data->temp_hyst[0] = f71882fg_read8(data,
630                                                 F71882FG_REG_TEMP_HYST1) >> 4;
631                 /* Hyst temps 2 & 3 stored in same register */
632                 reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
633                 data->temp_hyst[1] = reg & 0x0F;
634                 data->temp_hyst[2] = reg >> 4;
635
636                 /* Have to hardcode type, because temp1 is special */
637                 reg  = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
638                 reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
639                 if ((reg2 & 0x03) == 0x01)
640                         data->temp_type[0] = 6 /* PECI */;
641                 else if ((reg2 & 0x03) == 0x02)
642                         data->temp_type[0] = 5 /* AMDSI */;
643                 else
644                         data->temp_type[0] = (reg & 0x02) ? 2 : 4;
645
646                 data->temp_type[1] = (reg & 0x04) ? 2 : 4;
647                 data->temp_type[2] = (reg & 0x08) ? 2 : 4;
648
649                 data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
650
651                 data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
652
653                 data->pwm_enable = f71882fg_read8(data,
654                                                   F71882FG_REG_PWM_ENABLE);
655                 data->pwm_auto_point_hyst[0] = f71882fg_read8(data,
656                                                       F71882FG_REG_FAN_HYST0);
657                 data->pwm_auto_point_hyst[1] = f71882fg_read8(data,
658                                                       F71882FG_REG_FAN_HYST1);
659                 for (nr = 0; nr < 4; nr++) {
660                         int point;
661
662                         data->pwm_auto_point_mapping[nr] =
663                             f71882fg_read8(data,
664                                            F71882FG_REG_POINT_MAPPING(nr));
665
666                         for (point = 0; point < 5; point++) {
667                                 data->pwm_auto_point_pwm[nr][point] =
668                                     f71882fg_read8(data,
669                                                    F71882FG_REG_POINT_PWM
670                                                    (nr, point));
671                         }
672                         for (point = 0; point < 4; point++) {
673                                 data->pwm_auto_point_temp[nr][point] =
674                                     f71882fg_read8(data,
675                                                    F71882FG_REG_POINT_TEMP
676                                                    (nr, point));
677                         }
678                 }
679                 data->last_limits = jiffies;
680         }
681
682         /* Update every second */
683         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
684                 data->temp_status = f71882fg_read8(data,
685                                                 F71882FG_REG_TEMP_STATUS);
686                 data->temp_diode_open = f71882fg_read8(data,
687                                                 F71882FG_REG_TEMP_DIODE_OPEN);
688                 for (nr = 0; nr < 3; nr++)
689                         data->temp[nr] = f71882fg_read8(data,
690                                                 F71882FG_REG_TEMP(nr));
691
692                 data->fan_status = f71882fg_read8(data,
693                                                 F71882FG_REG_FAN_STATUS);
694                 for (nr = 0; nr < 4; nr++) {
695                         data->fan[nr] = f71882fg_read16(data,
696                                                 F71882FG_REG_FAN(nr));
697                         data->fan_target[nr] =
698                             f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
699                         data->fan_full_speed[nr] =
700                             f71882fg_read16(data,
701                                             F71882FG_REG_FAN_FULL_SPEED(nr));
702                         data->pwm[nr] =
703                             f71882fg_read8(data, F71882FG_REG_PWM(nr));
704                 }
705
706                 data->in_status = f71882fg_read8(data,
707                                                 F71882FG_REG_IN_STATUS);
708                 for (nr = 0; nr < 9; nr++)
709                         data->in[nr] = f71882fg_read8(data,
710                                                 F71882FG_REG_IN(nr));
711
712                 data->last_updated = jiffies;
713                 data->valid = 1;
714         }
715
716         mutex_unlock(&data->update_lock);
717
718         return data;
719 }
720
721 /* Sysfs Interface */
722 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
723         char *buf)
724 {
725         struct f71882fg_data *data = f71882fg_update_device(dev);
726         int nr = to_sensor_dev_attr_2(devattr)->index;
727         int speed = fan_from_reg(data->fan[nr]);
728
729         if (speed == FAN_MIN_DETECT)
730                 speed = 0;
731
732         return sprintf(buf, "%d\n", speed);
733 }
734
735 static ssize_t show_fan_full_speed(struct device *dev,
736                                    struct device_attribute *devattr, char *buf)
737 {
738         struct f71882fg_data *data = f71882fg_update_device(dev);
739         int nr = to_sensor_dev_attr_2(devattr)->index;
740         int speed = fan_from_reg(data->fan_full_speed[nr]);
741         return sprintf(buf, "%d\n", speed);
742 }
743
744 static ssize_t store_fan_full_speed(struct device *dev,
745                                     struct device_attribute *devattr,
746                                     const char *buf, size_t count)
747 {
748         struct f71882fg_data *data = dev_get_drvdata(dev);
749         int nr = to_sensor_dev_attr_2(devattr)->index;
750         long val = simple_strtol(buf, NULL, 10);
751
752         val = SENSORS_LIMIT(val, 23, 1500000);
753         val = fan_to_reg(val);
754
755         mutex_lock(&data->update_lock);
756         if (data->pwm_enable & (1 << (2 * nr)))
757                 /* PWM mode */
758                 count = -EINVAL;
759         else {
760                 /* RPM mode */
761                 f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
762                 data->fan_full_speed[nr] = val;
763         }
764         mutex_unlock(&data->update_lock);
765
766         return count;
767 }
768
769 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
770         *devattr, char *buf)
771 {
772         struct f71882fg_data *data = f71882fg_update_device(dev);
773         int nr = to_sensor_dev_attr_2(devattr)->index;
774
775         if (data->fan_beep & (1 << nr))
776                 return sprintf(buf, "1\n");
777         else
778                 return sprintf(buf, "0\n");
779 }
780
781 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
782         *devattr, const char *buf, size_t count)
783 {
784         struct f71882fg_data *data = dev_get_drvdata(dev);
785         int nr = to_sensor_dev_attr_2(devattr)->index;
786         int val = simple_strtoul(buf, NULL, 10);
787
788         mutex_lock(&data->update_lock);
789         if (val)
790                 data->fan_beep |= 1 << nr;
791         else
792                 data->fan_beep &= ~(1 << nr);
793
794         f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
795         mutex_unlock(&data->update_lock);
796
797         return count;
798 }
799
800 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
801         *devattr, char *buf)
802 {
803         struct f71882fg_data *data = f71882fg_update_device(dev);
804         int nr = to_sensor_dev_attr_2(devattr)->index;
805
806         if (data->fan_status & (1 << nr))
807                 return sprintf(buf, "1\n");
808         else
809                 return sprintf(buf, "0\n");
810 }
811
812 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
813         char *buf)
814 {
815         struct f71882fg_data *data = f71882fg_update_device(dev);
816         int nr = to_sensor_dev_attr_2(devattr)->index;
817
818         return sprintf(buf, "%d\n", data->in[nr] * 8);
819 }
820
821 static ssize_t show_in_max(struct device *dev, struct device_attribute
822         *devattr, char *buf)
823 {
824         struct f71882fg_data *data = f71882fg_update_device(dev);
825
826         return sprintf(buf, "%d\n", data->in1_max * 8);
827 }
828
829 static ssize_t store_in_max(struct device *dev, struct device_attribute
830         *devattr, const char *buf, size_t count)
831 {
832         struct f71882fg_data *data = dev_get_drvdata(dev);
833         int val = simple_strtoul(buf, NULL, 10) / 8;
834
835         if (val > 255)
836                 val = 255;
837
838         mutex_lock(&data->update_lock);
839         f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
840         data->in1_max = val;
841         mutex_unlock(&data->update_lock);
842
843         return count;
844 }
845
846 static ssize_t show_in_beep(struct device *dev, struct device_attribute
847         *devattr, char *buf)
848 {
849         struct f71882fg_data *data = f71882fg_update_device(dev);
850         int nr = to_sensor_dev_attr_2(devattr)->index;
851
852         if (data->in_beep & (1 << nr))
853                 return sprintf(buf, "1\n");
854         else
855                 return sprintf(buf, "0\n");
856 }
857
858 static ssize_t store_in_beep(struct device *dev, struct device_attribute
859         *devattr, const char *buf, size_t count)
860 {
861         struct f71882fg_data *data = dev_get_drvdata(dev);
862         int nr = to_sensor_dev_attr_2(devattr)->index;
863         int val = simple_strtoul(buf, NULL, 10);
864
865         mutex_lock(&data->update_lock);
866         if (val)
867                 data->in_beep |= 1 << nr;
868         else
869                 data->in_beep &= ~(1 << nr);
870
871         f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
872         mutex_unlock(&data->update_lock);
873
874         return count;
875 }
876
877 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
878         *devattr, char *buf)
879 {
880         struct f71882fg_data *data = f71882fg_update_device(dev);
881         int nr = to_sensor_dev_attr_2(devattr)->index;
882
883         if (data->in_status & (1 << nr))
884                 return sprintf(buf, "1\n");
885         else
886                 return sprintf(buf, "0\n");
887 }
888
889 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
890         char *buf)
891 {
892         struct f71882fg_data *data = f71882fg_update_device(dev);
893         int nr = to_sensor_dev_attr_2(devattr)->index;
894
895         return sprintf(buf, "%d\n", data->temp[nr] * 1000);
896 }
897
898 static ssize_t show_temp_max(struct device *dev, struct device_attribute
899         *devattr, char *buf)
900 {
901         struct f71882fg_data *data = f71882fg_update_device(dev);
902         int nr = to_sensor_dev_attr_2(devattr)->index;
903
904         return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
905 }
906
907 static ssize_t store_temp_max(struct device *dev, struct device_attribute
908         *devattr, const char *buf, size_t count)
909 {
910         struct f71882fg_data *data = dev_get_drvdata(dev);
911         int nr = to_sensor_dev_attr_2(devattr)->index;
912         int val = simple_strtoul(buf, NULL, 10) / 1000;
913
914         if (val > 255)
915                 val = 255;
916
917         mutex_lock(&data->update_lock);
918         f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
919         data->temp_high[nr] = val;
920         mutex_unlock(&data->update_lock);
921
922         return count;
923 }
924
925 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
926         *devattr, char *buf)
927 {
928         struct f71882fg_data *data = f71882fg_update_device(dev);
929         int nr = to_sensor_dev_attr_2(devattr)->index;
930
931         return sprintf(buf, "%d\n",
932                 (data->temp_high[nr] - data->temp_hyst[nr]) * 1000);
933 }
934
935 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
936         *devattr, const char *buf, size_t count)
937 {
938         struct f71882fg_data *data = dev_get_drvdata(dev);
939         int nr = to_sensor_dev_attr_2(devattr)->index;
940         int val = simple_strtoul(buf, NULL, 10) / 1000;
941         ssize_t ret = count;
942
943         mutex_lock(&data->update_lock);
944
945         /* convert abs to relative and check */
946         val = data->temp_high[nr] - val;
947         if (val < 0 || val > 15) {
948                 ret = -EINVAL;
949                 goto store_temp_max_hyst_exit;
950         }
951
952         data->temp_hyst[nr] = val;
953
954         /* convert value to register contents */
955         switch (nr) {
956                 case 0:
957                         val = val << 4;
958                         break;
959                 case 1:
960                         val = val | (data->temp_hyst[2] << 4);
961                         break;
962                 case 2:
963                         val = data->temp_hyst[1] | (val << 4);
964                         break;
965         }
966
967         f71882fg_write8(data, nr ? F71882FG_REG_TEMP_HYST23 :
968                 F71882FG_REG_TEMP_HYST1, val);
969
970 store_temp_max_hyst_exit:
971         mutex_unlock(&data->update_lock);
972         return ret;
973 }
974
975 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
976         *devattr, char *buf)
977 {
978         struct f71882fg_data *data = f71882fg_update_device(dev);
979         int nr = to_sensor_dev_attr_2(devattr)->index;
980
981         return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
982 }
983
984 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
985         *devattr, const char *buf, size_t count)
986 {
987         struct f71882fg_data *data = dev_get_drvdata(dev);
988         int nr = to_sensor_dev_attr_2(devattr)->index;
989         int val = simple_strtoul(buf, NULL, 10) / 1000;
990
991         if (val > 255)
992                 val = 255;
993
994         mutex_lock(&data->update_lock);
995         f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
996         data->temp_ovt[nr] = val;
997         mutex_unlock(&data->update_lock);
998
999         return count;
1000 }
1001
1002 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1003         *devattr, char *buf)
1004 {
1005         struct f71882fg_data *data = f71882fg_update_device(dev);
1006         int nr = to_sensor_dev_attr_2(devattr)->index;
1007
1008         return sprintf(buf, "%d\n",
1009                 (data->temp_ovt[nr] - data->temp_hyst[nr]) * 1000);
1010 }
1011
1012 static ssize_t show_temp_type(struct device *dev, struct device_attribute
1013         *devattr, char *buf)
1014 {
1015         struct f71882fg_data *data = f71882fg_update_device(dev);
1016         int nr = to_sensor_dev_attr_2(devattr)->index;
1017
1018         return sprintf(buf, "%d\n", data->temp_type[nr]);
1019 }
1020
1021 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1022         *devattr, char *buf)
1023 {
1024         struct f71882fg_data *data = f71882fg_update_device(dev);
1025         int nr = to_sensor_dev_attr_2(devattr)->index;
1026
1027         if (data->temp_beep & (1 << (nr + 1)))
1028                 return sprintf(buf, "1\n");
1029         else
1030                 return sprintf(buf, "0\n");
1031 }
1032
1033 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1034         *devattr, const char *buf, size_t count)
1035 {
1036         struct f71882fg_data *data = dev_get_drvdata(dev);
1037         int nr = to_sensor_dev_attr_2(devattr)->index;
1038         int val = simple_strtoul(buf, NULL, 10);
1039
1040         mutex_lock(&data->update_lock);
1041         if (val)
1042                 data->temp_beep |= 1 << (nr + 1);
1043         else
1044                 data->temp_beep &= ~(1 << (nr + 1));
1045
1046         f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1047         mutex_unlock(&data->update_lock);
1048
1049         return count;
1050 }
1051
1052 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1053         *devattr, char *buf)
1054 {
1055         struct f71882fg_data *data = f71882fg_update_device(dev);
1056         int nr = to_sensor_dev_attr_2(devattr)->index;
1057
1058         if (data->temp_status & (1 << (nr + 1)))
1059                 return sprintf(buf, "1\n");
1060         else
1061                 return sprintf(buf, "0\n");
1062 }
1063
1064 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1065         *devattr, char *buf)
1066 {
1067         struct f71882fg_data *data = f71882fg_update_device(dev);
1068         int nr = to_sensor_dev_attr_2(devattr)->index;
1069
1070         if (data->temp_diode_open & (1 << (nr + 1)))
1071                 return sprintf(buf, "1\n");
1072         else
1073                 return sprintf(buf, "0\n");
1074 }
1075
1076 static ssize_t show_pwm(struct device *dev,
1077                         struct device_attribute *devattr, char *buf)
1078 {
1079         struct f71882fg_data *data = f71882fg_update_device(dev);
1080         int val, nr = to_sensor_dev_attr_2(devattr)->index;
1081         if (data->pwm_enable & (1 << (2 * nr)))
1082                 /* PWM mode */
1083                 val = data->pwm[nr];
1084         else {
1085                 /* RPM mode */
1086                 mutex_lock(&data->update_lock);
1087                 val = 255 * fan_from_reg(data->fan_target[nr])
1088                         / fan_from_reg(data->fan_full_speed[nr]);
1089                 mutex_unlock(&data->update_lock);
1090         }
1091         return sprintf(buf, "%d\n", val);
1092 }
1093
1094 static ssize_t store_pwm(struct device *dev,
1095                          struct device_attribute *devattr, const char *buf,
1096                          size_t count)
1097 {
1098         /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1099         struct f71882fg_data *data = f71882fg_update_device(dev);
1100         int nr = to_sensor_dev_attr_2(devattr)->index;
1101         long val = simple_strtol(buf, NULL, 10);
1102         val = SENSORS_LIMIT(val, 0, 255);
1103
1104         mutex_lock(&data->update_lock);
1105         if (data->pwm_enable & (1 << (2 * nr))) {
1106                 /* PWM mode */
1107                 f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1108                 data->pwm[nr] = val;
1109         } else {
1110                 /* RPM mode */
1111                 int target = val * fan_from_reg(data->fan_full_speed[nr]) / 255;
1112                 f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr),
1113                                  fan_to_reg(target));
1114                 data->fan_target[nr] = fan_to_reg(target);
1115         }
1116         mutex_unlock(&data->update_lock);
1117
1118         return count;
1119 }
1120
1121 static ssize_t show_pwm_enable(struct device *dev,
1122                                struct device_attribute *devattr, char *buf)
1123 {
1124         int result;
1125         struct f71882fg_data *data = f71882fg_update_device(dev);
1126         int nr = to_sensor_dev_attr_2(devattr)->index;
1127
1128         if (data->pwm_enable & (2 << (2 * nr)))
1129                 result = 1;
1130         else
1131                 result = 2;
1132
1133         return sprintf(buf, "%d\n", result);
1134 }
1135
1136 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1137                                 *devattr, const char *buf, size_t count)
1138 {
1139         struct f71882fg_data *data = dev_get_drvdata(dev);
1140         int nr = to_sensor_dev_attr_2(devattr)->index;
1141         long val = simple_strtol(buf, NULL, 10);
1142         if (val < 1 || val > 2)
1143                 return -EINVAL;
1144
1145         mutex_lock(&data->update_lock);
1146         switch (val) {
1147         case 1:
1148                 data->pwm_enable |= 2 << (2 * nr);
1149                 break;          /* Manual */
1150         case 2:
1151                 data->pwm_enable &= ~(2 << (2 * nr));
1152                 break;          /* Temperature ctrl */
1153         }
1154         switch (fan_mode[nr]) {
1155         case 1:
1156                 data->pwm_enable |= 1 << (2 * nr);
1157                 break;          /* Duty cycle mode */
1158         case 2:
1159                 data->pwm_enable &= ~(1 << (2 * nr));
1160                 break;          /* RPM mode */
1161         }
1162         f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1163         mutex_unlock(&data->update_lock);
1164
1165         return count;
1166 }
1167
1168 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1169                                        struct device_attribute *devattr,
1170                                        char *buf)
1171 {
1172         int result;
1173         struct f71882fg_data *data = f71882fg_update_device(dev);
1174         int pwm = to_sensor_dev_attr_2(devattr)->index;
1175         int point = to_sensor_dev_attr_2(devattr)->nr;
1176
1177         if (data->pwm_enable & (1 << (2 * pwm))) {
1178                 /* PWM mode */
1179                 result = data->pwm_auto_point_pwm[pwm][point];
1180         } else {
1181                 /* RPM mode */
1182                 result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1183         }
1184
1185         return sprintf(buf, "%d\n", result);
1186 }
1187
1188 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1189                                         struct device_attribute *devattr,
1190                                         const char *buf, size_t count)
1191 {
1192         /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1193         struct f71882fg_data *data = f71882fg_update_device(dev);
1194         int pwm = to_sensor_dev_attr_2(devattr)->index;
1195         int point = to_sensor_dev_attr_2(devattr)->nr;
1196         int val = simple_strtoul(buf, NULL, 10);
1197         val = SENSORS_LIMIT(val, 0, 255);
1198
1199         mutex_lock(&data->update_lock);
1200         if (data->pwm_enable & (1 << (2 * pwm))) {
1201                 /* PWM mode */
1202         } else {
1203                 /* RPM mode */
1204                 if (val < 29)   /* Prevent negative numbers */
1205                         val = 255;
1206                 else
1207                         val = (255 - val) * 32 / val;
1208         }
1209         f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
1210         data->pwm_auto_point_pwm[pwm][point] = val;
1211         mutex_unlock(&data->update_lock);
1212
1213         return count;
1214 }
1215
1216 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
1217                                              struct device_attribute *devattr,
1218                                              char *buf)
1219 {
1220         int result = 0;
1221         struct f71882fg_data *data = f71882fg_update_device(dev);
1222         int nr = to_sensor_dev_attr_2(devattr)->index;
1223         int point = to_sensor_dev_attr_2(devattr)->nr;
1224
1225         mutex_lock(&data->update_lock);
1226         switch (nr) {
1227         case 0:
1228                 result = data->pwm_auto_point_hyst[0] & 0x0f;
1229                 break;
1230         case 1:
1231                 result = data->pwm_auto_point_hyst[0] >> 4;
1232                 break;
1233         case 2:
1234                 result = data->pwm_auto_point_hyst[1] & 0x0f;
1235                 break;
1236         case 3:
1237                 result = data->pwm_auto_point_hyst[1] >> 4;
1238                 break;
1239         }
1240         result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
1241         mutex_unlock(&data->update_lock);
1242
1243         return sprintf(buf, "%d\n", result);
1244 }
1245
1246 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
1247                                               struct device_attribute *devattr,
1248                                               const char *buf, size_t count)
1249 {
1250         struct f71882fg_data *data = f71882fg_update_device(dev);
1251         int nr = to_sensor_dev_attr_2(devattr)->index;
1252         int point = to_sensor_dev_attr_2(devattr)->nr;
1253         long val = simple_strtol(buf, NULL, 10) / 1000;
1254
1255         mutex_lock(&data->update_lock);
1256         val = SENSORS_LIMIT(val, data->pwm_auto_point_temp[nr][point] - 15,
1257                                 data->pwm_auto_point_temp[nr][point]);
1258         val = data->pwm_auto_point_temp[nr][point] - val;
1259
1260         switch (nr) {
1261         case 0:
1262                 val = (data->pwm_auto_point_hyst[0] & 0xf0) | val;
1263                 break;
1264         case 1:
1265                 val = (data->pwm_auto_point_hyst[0] & 0x0f) | (val << 4);
1266                 break;
1267         case 2:
1268                 val = (data->pwm_auto_point_hyst[1] & 0xf0) | val;
1269                 break;
1270         case 3:
1271                 val = (data->pwm_auto_point_hyst[1] & 0x0f) | (val << 4);
1272                 break;
1273         }
1274         if (nr == 0 || nr == 1) {
1275                 f71882fg_write8(data, F71882FG_REG_FAN_HYST0, val);
1276                 data->pwm_auto_point_hyst[0] = val;
1277         } else {
1278                 f71882fg_write8(data, F71882FG_REG_FAN_HYST1, val);
1279                 data->pwm_auto_point_hyst[1] = val;
1280         }
1281         mutex_unlock(&data->update_lock);
1282
1283         return count;
1284 }
1285
1286 static ssize_t show_pwm_interpolate(struct device *dev,
1287                                     struct device_attribute *devattr, char *buf)
1288 {
1289         int result;
1290         struct f71882fg_data *data = f71882fg_update_device(dev);
1291         int nr = to_sensor_dev_attr_2(devattr)->index;
1292
1293         result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
1294
1295         return sprintf(buf, "%d\n", result);
1296 }
1297
1298 static ssize_t store_pwm_interpolate(struct device *dev,
1299                                      struct device_attribute *devattr,
1300                                      const char *buf, size_t count)
1301 {
1302         /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1303         struct f71882fg_data *data = f71882fg_update_device(dev);
1304         int nr = to_sensor_dev_attr_2(devattr)->index;
1305         int val = simple_strtoul(buf, NULL, 10);
1306         mutex_lock(&data->update_lock);
1307         if (val)
1308                 val = data->pwm_auto_point_mapping[nr] | (1 << 4);
1309         else
1310                 val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
1311         f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1312         data->pwm_auto_point_mapping[nr] = val;
1313         mutex_unlock(&data->update_lock);
1314
1315         return count;
1316 }
1317
1318 static ssize_t show_pwm_auto_point_channel(struct device *dev,
1319                                            struct device_attribute *devattr,
1320                                            char *buf)
1321 {
1322         int result;
1323         struct f71882fg_data *data = f71882fg_update_device(dev);
1324         int nr = to_sensor_dev_attr_2(devattr)->index;
1325
1326         result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) - 1);
1327
1328         return sprintf(buf, "%d\n", result);
1329 }
1330
1331 static ssize_t store_pwm_auto_point_channel(struct device *dev,
1332                                             struct device_attribute *devattr,
1333                                             const char *buf, size_t count)
1334 {
1335         /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1336         struct f71882fg_data *data = f71882fg_update_device(dev);
1337         int nr = to_sensor_dev_attr_2(devattr)->index;
1338         long val = simple_strtol(buf, NULL, 10);
1339         switch (val) {
1340         case 1:
1341                 val = 1;
1342                 break;
1343         case 2:
1344                 val = 2;
1345                 break;
1346         case 4:
1347                 val = 3;
1348                 break;
1349         default:
1350                 return -EINVAL;
1351         }
1352         mutex_lock(&data->update_lock);
1353         val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
1354         f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
1355         data->pwm_auto_point_mapping[nr] = val;
1356         mutex_unlock(&data->update_lock);
1357
1358         return count;
1359 }
1360
1361 static ssize_t show_pwm_auto_point_temp(struct device *dev,
1362                                         struct device_attribute *devattr,
1363                                         char *buf)
1364 {
1365         int result;
1366         struct f71882fg_data *data = f71882fg_update_device(dev);
1367         int pwm = to_sensor_dev_attr_2(devattr)->index;
1368         int point = to_sensor_dev_attr_2(devattr)->nr;
1369
1370         result = data->pwm_auto_point_temp[pwm][point];
1371         return sprintf(buf, "%d\n", 1000 * result);
1372 }
1373
1374 static ssize_t store_pwm_auto_point_temp(struct device *dev,
1375                                          struct device_attribute *devattr,
1376                                          const char *buf, size_t count)
1377 {
1378         /* struct f71882fg_data *data = dev_get_drvdata(dev); */
1379         struct f71882fg_data *data = f71882fg_update_device(dev);
1380         int pwm = to_sensor_dev_attr_2(devattr)->index;
1381         int point = to_sensor_dev_attr_2(devattr)->nr;
1382         long val = simple_strtol(buf, NULL, 10) / 1000;
1383         val = SENSORS_LIMIT(val, 0, 255);
1384
1385         mutex_lock(&data->update_lock);
1386         f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
1387         data->pwm_auto_point_temp[pwm][point] = val;
1388         mutex_unlock(&data->update_lock);
1389
1390         return count;
1391 }
1392
1393 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
1394         char *buf)
1395 {
1396         return sprintf(buf, DRVNAME "\n");
1397 }
1398
1399
1400 static int __devinit f71882fg_probe(struct platform_device * pdev)
1401 {
1402         struct f71882fg_data *data;
1403         int err, i;
1404         u8 start_reg;
1405
1406         if (!(data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL)))
1407                 return -ENOMEM;
1408
1409         data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
1410         mutex_init(&data->update_lock);
1411         platform_set_drvdata(pdev, data);
1412
1413         /* Register sysfs interface files */
1414         for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++) {
1415                 err = device_create_file(&pdev->dev, &f71882fg_dev_attr[i]);
1416                 if (err)
1417                         goto exit_unregister_sysfs;
1418         }
1419
1420         start_reg = f71882fg_read8(data, F71882FG_REG_START);
1421         if (start_reg & 0x01) {
1422                 for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++) {
1423                         err = device_create_file(&pdev->dev,
1424                                         &f71882fg_in_temp_attr[i].dev_attr);
1425                         if (err)
1426                                 goto exit_unregister_sysfs;
1427                 }
1428         }
1429
1430         if (start_reg & 0x02) {
1431                 for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++) {
1432                         err = device_create_file(&pdev->dev,
1433                                         &f71882fg_fan_attr[i].dev_attr);
1434                         if (err)
1435                                 goto exit_unregister_sysfs;
1436                 }
1437         }
1438
1439         data->hwmon_dev = hwmon_device_register(&pdev->dev);
1440         if (IS_ERR(data->hwmon_dev)) {
1441                 err = PTR_ERR(data->hwmon_dev);
1442                 goto exit_unregister_sysfs;
1443         }
1444
1445         return 0;
1446
1447 exit_unregister_sysfs:
1448         for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
1449                 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
1450
1451         for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
1452                 device_remove_file(&pdev->dev,
1453                                         &f71882fg_in_temp_attr[i].dev_attr);
1454
1455         for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
1456                 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
1457
1458         kfree(data);
1459
1460         return err;
1461 }
1462
1463 static int __devexit f71882fg_remove(struct platform_device *pdev)
1464 {
1465         int i;
1466         struct f71882fg_data *data = platform_get_drvdata(pdev);
1467
1468         platform_set_drvdata(pdev, NULL);
1469         hwmon_device_unregister(data->hwmon_dev);
1470
1471         for (i = 0; i < ARRAY_SIZE(f71882fg_dev_attr); i++)
1472                 device_remove_file(&pdev->dev, &f71882fg_dev_attr[i]);
1473
1474         for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
1475                 device_remove_file(&pdev->dev,
1476                                         &f71882fg_in_temp_attr[i].dev_attr);
1477
1478         for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
1479                 device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
1480
1481         kfree(data);
1482
1483         return 0;
1484 }
1485
1486 static int __init f71882fg_find(int sioaddr, unsigned short *address)
1487 {
1488         int err = -ENODEV;
1489         u16 devid;
1490         u8 start_reg;
1491         struct f71882fg_data data;
1492
1493         superio_enter(sioaddr);
1494
1495         devid = superio_inw(sioaddr, SIO_REG_MANID);
1496         if (devid != SIO_FINTEK_ID) {
1497                 printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
1498                 goto exit;
1499         }
1500
1501         devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
1502         if (devid != SIO_F71882_ID) {
1503                 printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
1504                 goto exit;
1505         }
1506
1507         superio_select(sioaddr, SIO_F71882FG_LD_HWM);
1508         if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
1509                 printk(KERN_WARNING DRVNAME ": Device not activated\n");
1510                 goto exit;
1511         }
1512
1513         *address = superio_inw(sioaddr, SIO_REG_ADDR);
1514         if (*address == 0)
1515         {
1516                 printk(KERN_WARNING DRVNAME ": Base address not set\n");
1517                 goto exit;
1518         }
1519         *address &= ~(REGION_LENGTH - 1);       /* Ignore 3 LSB */
1520
1521         data.addr = *address;
1522         start_reg = f71882fg_read8(&data, F71882FG_REG_START);
1523         if (!(start_reg & 0x03)) {
1524                 printk(KERN_WARNING DRVNAME
1525                         ": Hardware monitoring not activated\n");
1526                 goto exit;
1527         }
1528
1529         err = 0;
1530         printk(KERN_INFO DRVNAME ": Found F71882FG chip at %#x, revision %d\n",
1531                 (unsigned int)*address,
1532                 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
1533 exit:
1534         superio_exit(sioaddr);
1535         return err;
1536 }
1537
1538 static int __init f71882fg_device_add(unsigned short address)
1539 {
1540         struct resource res = {
1541                 .start  = address,
1542                 .end    = address + REGION_LENGTH - 1,
1543                 .flags  = IORESOURCE_IO,
1544         };
1545         int err;
1546
1547         f71882fg_pdev = platform_device_alloc(DRVNAME, address);
1548         if (!f71882fg_pdev)
1549                 return -ENOMEM;
1550
1551         res.name = f71882fg_pdev->name;
1552         err = platform_device_add_resources(f71882fg_pdev, &res, 1);
1553         if (err) {
1554                 printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
1555                 goto exit_device_put;
1556         }
1557
1558         err = platform_device_add(f71882fg_pdev);
1559         if (err) {
1560                 printk(KERN_ERR DRVNAME ": Device addition failed\n");
1561                 goto exit_device_put;
1562         }
1563
1564         return 0;
1565
1566 exit_device_put:
1567         platform_device_put(f71882fg_pdev);
1568
1569         return err;
1570 }
1571
1572 static int __init f71882fg_init(void)
1573 {
1574         int err = -ENODEV;
1575         unsigned short address;
1576
1577         if (f71882fg_find(0x2e, &address) && f71882fg_find(0x4e, &address))
1578                 goto exit;
1579
1580         if ((err = platform_driver_register(&f71882fg_driver)))
1581                 goto exit;
1582
1583         if ((err = f71882fg_device_add(address)))
1584                 goto exit_driver;
1585
1586         return 0;
1587
1588 exit_driver:
1589         platform_driver_unregister(&f71882fg_driver);
1590 exit:
1591         return err;
1592 }
1593
1594 static void __exit f71882fg_exit(void)
1595 {
1596         platform_device_unregister(f71882fg_pdev);
1597         platform_driver_unregister(&f71882fg_driver);
1598 }
1599
1600 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
1601 MODULE_AUTHOR("Hans Edgington (hans@edgington.nl)");
1602 MODULE_LICENSE("GPL");
1603
1604 module_init(f71882fg_init);
1605 module_exit(f71882fg_exit);