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