]> git.samba.org - sfrench/cifs-2.6.git/blob - drivers/hwmon/f71882fg.c
fix short copy handling in copy_mc_pipe_to_iter()
[sfrench/cifs-2.6.git] / drivers / hwmon / f71882fg.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3  *   Copyright (C) 2006 by Hans Edgington <hans@edgington.nl>              *
4  *   Copyright (C) 2007-2011 Hans de Goede <hdegoede@redhat.com>           *
5  *                                                                         *
6  ***************************************************************************/
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/jiffies.h>
14 #include <linux/platform_device.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/io.h>
20 #include <linux/acpi.h>
21
22 #define DRVNAME "f71882fg"
23
24 #define SIO_F71858FG_LD_HWM     0x02    /* Hardware monitor logical device */
25 #define SIO_F71882FG_LD_HWM     0x04    /* Hardware monitor logical device */
26 #define SIO_UNLOCK_KEY          0x87    /* Key to enable Super-I/O */
27 #define SIO_LOCK_KEY            0xAA    /* Key to disable Super-I/O */
28
29 #define SIO_REG_LDSEL           0x07    /* Logical device select */
30 #define SIO_REG_DEVID           0x20    /* Device ID (2 bytes) */
31 #define SIO_REG_DEVREV          0x22    /* Device revision */
32 #define SIO_REG_MANID           0x23    /* Fintek ID (2 bytes) */
33 #define SIO_REG_ENABLE          0x30    /* Logical device enable */
34 #define SIO_REG_ADDR            0x60    /* Logical device address (2 bytes) */
35
36 #define SIO_FINTEK_ID           0x1934  /* Manufacturers ID */
37 #define SIO_F71808E_ID          0x0901  /* Chipset ID */
38 #define SIO_F71808A_ID          0x1001  /* Chipset ID */
39 #define SIO_F71858_ID           0x0507  /* Chipset ID */
40 #define SIO_F71862_ID           0x0601  /* Chipset ID */
41 #define SIO_F71868_ID           0x1106  /* Chipset ID */
42 #define SIO_F71869_ID           0x0814  /* Chipset ID */
43 #define SIO_F71869A_ID          0x1007  /* Chipset ID */
44 #define SIO_F71882_ID           0x0541  /* Chipset ID */
45 #define SIO_F71889_ID           0x0723  /* Chipset ID */
46 #define SIO_F71889E_ID          0x0909  /* Chipset ID */
47 #define SIO_F71889A_ID          0x1005  /* Chipset ID */
48 #define SIO_F8000_ID            0x0581  /* Chipset ID */
49 #define SIO_F81768D_ID          0x1210  /* Chipset ID */
50 #define SIO_F81865_ID           0x0704  /* Chipset ID */
51 #define SIO_F81866_ID           0x1010  /* Chipset ID */
52 #define SIO_F81966_ID           0x1502  /* Chipset ID */
53
54 #define REGION_LENGTH           8
55 #define ADDR_REG_OFFSET         5
56 #define DATA_REG_OFFSET         6
57
58 #define F71882FG_REG_IN_STATUS          0x12 /* f7188x only */
59 #define F71882FG_REG_IN_BEEP            0x13 /* f7188x only */
60 #define F71882FG_REG_IN(nr)             (0x20  + (nr))
61 #define F71882FG_REG_IN1_HIGH           0x32 /* f7188x only */
62
63 #define F81866_REG_IN_STATUS            0x16 /* F81866 only */
64 #define F81866_REG_IN_BEEP                      0x17 /* F81866 only */
65 #define F81866_REG_IN1_HIGH             0x3a /* F81866 only */
66
67 #define F71882FG_REG_FAN(nr)            (0xA0 + (16 * (nr)))
68 #define F71882FG_REG_FAN_TARGET(nr)     (0xA2 + (16 * (nr)))
69 #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
70 #define F71882FG_REG_FAN_STATUS         0x92
71 #define F71882FG_REG_FAN_BEEP           0x93
72
73 #define F71882FG_REG_TEMP(nr)           (0x70 + 2 * (nr))
74 #define F71882FG_REG_TEMP_OVT(nr)       (0x80 + 2 * (nr))
75 #define F71882FG_REG_TEMP_HIGH(nr)      (0x81 + 2 * (nr))
76 #define F71882FG_REG_TEMP_STATUS        0x62
77 #define F71882FG_REG_TEMP_BEEP          0x63
78 #define F71882FG_REG_TEMP_CONFIG        0x69
79 #define F71882FG_REG_TEMP_HYST(nr)      (0x6C + (nr))
80 #define F71882FG_REG_TEMP_TYPE          0x6B
81 #define F71882FG_REG_TEMP_DIODE_OPEN    0x6F
82
83 #define F71882FG_REG_PWM(nr)            (0xA3 + (16 * (nr)))
84 #define F71882FG_REG_PWM_TYPE           0x94
85 #define F71882FG_REG_PWM_ENABLE         0x96
86
87 #define F71882FG_REG_FAN_HYST(nr)       (0x98 + (nr))
88
89 #define F71882FG_REG_FAN_FAULT_T        0x9F
90 #define F71882FG_FAN_NEG_TEMP_EN        0x20
91 #define F71882FG_FAN_PROG_SEL           0x80
92
93 #define F71882FG_REG_POINT_PWM(pwm, point)      (0xAA + (point) + (16 * (pwm)))
94 #define F71882FG_REG_POINT_TEMP(pwm, point)     (0xA6 + (point) + (16 * (pwm)))
95 #define F71882FG_REG_POINT_MAPPING(nr)          (0xAF + 16 * (nr))
96
97 #define F71882FG_REG_START              0x01
98
99 #define F71882FG_MAX_INS                11
100
101 #define FAN_MIN_DETECT                  366 /* Lowest detectable fanspeed */
102
103 static unsigned short force_id;
104 module_param(force_id, ushort, 0);
105 MODULE_PARM_DESC(force_id, "Override the detected device ID");
106
107 enum chips { f71808e, f71808a, f71858fg, f71862fg, f71868a, f71869, f71869a,
108         f71882fg, f71889fg, f71889ed, f71889a, f8000, f81768d, f81865f,
109         f81866a};
110
111 static const char *const f71882fg_names[] = {
112         "f71808e",
113         "f71808a",
114         "f71858fg",
115         "f71862fg",
116         "f71868a",
117         "f71869", /* Both f71869f and f71869e, reg. compatible and same id */
118         "f71869a",
119         "f71882fg",
120         "f71889fg", /* f81801u too, same id */
121         "f71889ed",
122         "f71889a",
123         "f8000",
124         "f81768d",
125         "f81865f",
126         "f81866a",
127 };
128
129 static const char f71882fg_has_in[][F71882FG_MAX_INS] = {
130         [f71808e]       = { 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0 },
131         [f71808a]       = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0 },
132         [f71858fg]      = { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
133         [f71862fg]      = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
134         [f71868a]       = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
135         [f71869]        = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
136         [f71869a]       = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
137         [f71882fg]      = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
138         [f71889fg]      = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
139         [f71889ed]      = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
140         [f71889a]       = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
141         [f8000]         = { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
142         [f81768d]       = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
143         [f81865f]       = { 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
144         [f81866a]       = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 },
145 };
146
147 static const char f71882fg_has_in1_alarm[] = {
148         [f71808e]       = 0,
149         [f71808a]       = 0,
150         [f71858fg]      = 0,
151         [f71862fg]      = 0,
152         [f71868a]       = 0,
153         [f71869]        = 0,
154         [f71869a]       = 0,
155         [f71882fg]      = 1,
156         [f71889fg]      = 1,
157         [f71889ed]      = 1,
158         [f71889a]       = 1,
159         [f8000]         = 0,
160         [f81768d]       = 1,
161         [f81865f]       = 1,
162         [f81866a]       = 1,
163 };
164
165 static const char f71882fg_fan_has_beep[] = {
166         [f71808e]       = 0,
167         [f71808a]       = 0,
168         [f71858fg]      = 0,
169         [f71862fg]      = 1,
170         [f71868a]       = 1,
171         [f71869]        = 1,
172         [f71869a]       = 1,
173         [f71882fg]      = 1,
174         [f71889fg]      = 1,
175         [f71889ed]      = 1,
176         [f71889a]       = 1,
177         [f8000]         = 0,
178         [f81768d]       = 1,
179         [f81865f]       = 1,
180         [f81866a]       = 1,
181 };
182
183 static const char f71882fg_nr_fans[] = {
184         [f71808e]       = 3,
185         [f71808a]       = 2, /* +1 fan which is monitor + simple pwm only */
186         [f71858fg]      = 3,
187         [f71862fg]      = 3,
188         [f71868a]       = 3,
189         [f71869]        = 3,
190         [f71869a]       = 3,
191         [f71882fg]      = 4,
192         [f71889fg]      = 3,
193         [f71889ed]      = 3,
194         [f71889a]       = 3,
195         [f8000]         = 3, /* +1 fan which is monitor only */
196         [f81768d]       = 3,
197         [f81865f]       = 2,
198         [f81866a]       = 3,
199 };
200
201 static const char f71882fg_temp_has_beep[] = {
202         [f71808e]       = 0,
203         [f71808a]       = 1,
204         [f71858fg]      = 0,
205         [f71862fg]      = 1,
206         [f71868a]       = 1,
207         [f71869]        = 1,
208         [f71869a]       = 1,
209         [f71882fg]      = 1,
210         [f71889fg]      = 1,
211         [f71889ed]      = 1,
212         [f71889a]       = 1,
213         [f8000]         = 0,
214         [f81768d]       = 1,
215         [f81865f]       = 1,
216         [f81866a]       = 1,
217 };
218
219 static const char f71882fg_nr_temps[] = {
220         [f71808e]       = 2,
221         [f71808a]       = 2,
222         [f71858fg]      = 3,
223         [f71862fg]      = 3,
224         [f71868a]       = 3,
225         [f71869]        = 3,
226         [f71869a]       = 3,
227         [f71882fg]      = 3,
228         [f71889fg]      = 3,
229         [f71889ed]      = 3,
230         [f71889a]       = 3,
231         [f8000]         = 3,
232         [f81768d]       = 3,
233         [f81865f]       = 2,
234         [f81866a]       = 3,
235 };
236
237 static struct platform_device *f71882fg_pdev;
238
239 /* Super-I/O Function prototypes */
240 static inline int superio_inb(int base, int reg);
241 static inline int superio_inw(int base, int reg);
242 static inline int superio_enter(int base);
243 static inline void superio_select(int base, int ld);
244 static inline void superio_exit(int base);
245
246 struct f71882fg_sio_data {
247         enum chips type;
248 };
249
250 struct f71882fg_data {
251         unsigned short addr;
252         enum chips type;
253         struct device *hwmon_dev;
254
255         struct mutex update_lock;
256         int temp_start;                 /* temp numbering start (0 or 1) */
257         bool valid;                     /* true if following fields are valid */
258         char auto_point_temp_signed;
259         unsigned long last_updated;     /* In jiffies */
260         unsigned long last_limits;      /* In jiffies */
261
262         /* Register Values */
263         u8      in[F71882FG_MAX_INS];
264         u8      in1_max;
265         u8      in_status;
266         u8      in_beep;
267         u16     fan[4];
268         u16     fan_target[4];
269         u16     fan_full_speed[4];
270         u8      fan_status;
271         u8      fan_beep;
272         /*
273          * Note: all models have max 3 temperature channels, but on some
274          * they are addressed as 0-2 and on others as 1-3, so for coding
275          * convenience we reserve space for 4 channels
276          */
277         u16     temp[4];
278         u8      temp_ovt[4];
279         u8      temp_high[4];
280         u8      temp_hyst[2]; /* 2 hysts stored per reg */
281         u8      temp_type[4];
282         u8      temp_status;
283         u8      temp_beep;
284         u8      temp_diode_open;
285         u8      temp_config;
286         u8      pwm[4];
287         u8      pwm_enable;
288         u8      pwm_auto_point_hyst[2];
289         u8      pwm_auto_point_mapping[4];
290         u8      pwm_auto_point_pwm[4][5];
291         s8      pwm_auto_point_temp[4][4];
292 };
293
294 /* Sysfs in */
295 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
296         char *buf);
297 static ssize_t show_in_max(struct device *dev, struct device_attribute
298         *devattr, char *buf);
299 static ssize_t store_in_max(struct device *dev, struct device_attribute
300         *devattr, const char *buf, size_t count);
301 static ssize_t show_in_beep(struct device *dev, struct device_attribute
302         *devattr, char *buf);
303 static ssize_t store_in_beep(struct device *dev, struct device_attribute
304         *devattr, const char *buf, size_t count);
305 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
306         *devattr, char *buf);
307 /* Sysfs Fan */
308 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
309         char *buf);
310 static ssize_t show_fan_full_speed(struct device *dev,
311         struct device_attribute *devattr, char *buf);
312 static ssize_t store_fan_full_speed(struct device *dev,
313         struct device_attribute *devattr, const char *buf, size_t count);
314 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
315         *devattr, char *buf);
316 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
317         *devattr, const char *buf, size_t count);
318 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
319         *devattr, char *buf);
320 /* Sysfs Temp */
321 static ssize_t show_temp(struct device *dev, struct device_attribute
322         *devattr, char *buf);
323 static ssize_t show_temp_max(struct device *dev, struct device_attribute
324         *devattr, char *buf);
325 static ssize_t store_temp_max(struct device *dev, struct device_attribute
326         *devattr, const char *buf, size_t count);
327 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
328         *devattr, char *buf);
329 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
330         *devattr, const char *buf, size_t count);
331 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
332         *devattr, char *buf);
333 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
334         *devattr, const char *buf, size_t count);
335 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
336         *devattr, char *buf);
337 static ssize_t show_temp_type(struct device *dev, struct device_attribute
338         *devattr, char *buf);
339 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
340         *devattr, char *buf);
341 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
342         *devattr, const char *buf, size_t count);
343 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
344         *devattr, char *buf);
345 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
346         *devattr, char *buf);
347 /* PWM and Auto point control */
348 static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
349         char *buf);
350 static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
351         const char *buf, size_t count);
352 static ssize_t show_simple_pwm(struct device *dev,
353         struct device_attribute *devattr, char *buf);
354 static ssize_t store_simple_pwm(struct device *dev,
355         struct device_attribute *devattr, const char *buf, size_t count);
356 static ssize_t show_pwm_enable(struct device *dev,
357         struct device_attribute *devattr, char *buf);
358 static ssize_t store_pwm_enable(struct device *dev,
359         struct device_attribute *devattr, const char *buf, size_t count);
360 static ssize_t show_pwm_interpolate(struct device *dev,
361         struct device_attribute *devattr, char *buf);
362 static ssize_t store_pwm_interpolate(struct device *dev,
363         struct device_attribute *devattr, const char *buf, size_t count);
364 static ssize_t show_pwm_auto_point_channel(struct device *dev,
365         struct device_attribute *devattr, char *buf);
366 static ssize_t store_pwm_auto_point_channel(struct device *dev,
367         struct device_attribute *devattr, const char *buf, size_t count);
368 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
369         struct device_attribute *devattr, char *buf);
370 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
371         struct device_attribute *devattr, const char *buf, size_t count);
372 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
373         struct device_attribute *devattr, char *buf);
374 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
375         struct device_attribute *devattr, const char *buf, size_t count);
376 static ssize_t show_pwm_auto_point_temp(struct device *dev,
377         struct device_attribute *devattr, char *buf);
378 static ssize_t store_pwm_auto_point_temp(struct device *dev,
379         struct device_attribute *devattr, const char *buf, size_t count);
380 /* Sysfs misc */
381 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
382         char *buf);
383
384 static int f71882fg_probe(struct platform_device *pdev);
385 static int f71882fg_remove(struct platform_device *pdev);
386
387 static struct platform_driver f71882fg_driver = {
388         .driver = {
389                 .name   = DRVNAME,
390         },
391         .probe          = f71882fg_probe,
392         .remove         = f71882fg_remove,
393 };
394
395 static DEVICE_ATTR_RO(name);
396
397 /*
398  * Temp attr for the f71858fg, the f71858fg is special as it has its
399  * temperature indexes start at 0 (the others start at 1)
400  */
401 static struct sensor_device_attribute_2 f71858fg_temp_attr[] = {
402         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
403         SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
404                 store_temp_max, 0, 0),
405         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
406                 store_temp_max_hyst, 0, 0),
407         SENSOR_ATTR_2(temp1_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 0),
408         SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
409                 store_temp_crit, 0, 0),
410         SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
411                 0, 0),
412         SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
413         SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
414         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
415         SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
416                 store_temp_max, 0, 1),
417         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
418                 store_temp_max_hyst, 0, 1),
419         SENSOR_ATTR_2(temp2_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
420         SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
421                 store_temp_crit, 0, 1),
422         SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
423                 0, 1),
424         SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
425         SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
426         SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
427         SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
428                 store_temp_max, 0, 2),
429         SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
430                 store_temp_max_hyst, 0, 2),
431         SENSOR_ATTR_2(temp3_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
432         SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
433                 store_temp_crit, 0, 2),
434         SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
435                 0, 2),
436         SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
437         SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
438 };
439
440 /* Temp attr for the standard models */
441 static struct sensor_device_attribute_2 fxxxx_temp_attr[3][9] = { {
442         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
443         SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
444                 store_temp_max, 0, 1),
445         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
446                 store_temp_max_hyst, 0, 1),
447         /*
448          * Should really be temp1_max_alarm, but older versions did not handle
449          * the max and crit alarms separately and lm_sensors v2 depends on the
450          * presence of temp#_alarm files. The same goes for temp2/3 _alarm.
451          */
452         SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
453         SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
454                 store_temp_crit, 0, 1),
455         SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
456                 0, 1),
457         SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
458         SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
459         SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
460 }, {
461         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
462         SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
463                 store_temp_max, 0, 2),
464         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
465                 store_temp_max_hyst, 0, 2),
466         /* Should be temp2_max_alarm, see temp1_alarm note */
467         SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
468         SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
469                 store_temp_crit, 0, 2),
470         SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
471                 0, 2),
472         SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
473         SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
474         SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
475 }, {
476         SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
477         SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
478                 store_temp_max, 0, 3),
479         SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
480                 store_temp_max_hyst, 0, 3),
481         /* Should be temp3_max_alarm, see temp1_alarm note */
482         SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
483         SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
484                 store_temp_crit, 0, 3),
485         SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
486                 0, 3),
487         SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 7),
488         SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
489         SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
490 } };
491
492 /* Temp attr for models which can beep on temp alarm */
493 static struct sensor_device_attribute_2 fxxxx_temp_beep_attr[3][2] = { {
494         SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
495                 store_temp_beep, 0, 1),
496         SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
497                 store_temp_beep, 0, 5),
498 }, {
499         SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
500                 store_temp_beep, 0, 2),
501         SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
502                 store_temp_beep, 0, 6),
503 }, {
504         SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
505                 store_temp_beep, 0, 3),
506         SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
507                 store_temp_beep, 0, 7),
508 } };
509
510 static struct sensor_device_attribute_2 f81866_temp_beep_attr[3][2] = { {
511         SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
512                 store_temp_beep, 0, 0),
513         SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
514                 store_temp_beep, 0, 4),
515 }, {
516         SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
517                 store_temp_beep, 0, 1),
518         SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
519                 store_temp_beep, 0, 5),
520 }, {
521         SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
522                 store_temp_beep, 0, 2),
523         SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
524                 store_temp_beep, 0, 6),
525 } };
526
527 /*
528  * Temp attr for the f8000
529  * Note on the f8000 temp_ovt (crit) is used as max, and temp_high (max)
530  * is used as hysteresis value to clear alarms
531  * Also like the f71858fg its temperature indexes start at 0
532  */
533 static struct sensor_device_attribute_2 f8000_temp_attr[] = {
534         SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
535         SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_crit,
536                 store_temp_crit, 0, 0),
537         SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
538                 store_temp_max, 0, 0),
539         SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
540         SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
541         SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
542         SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_crit,
543                 store_temp_crit, 0, 1),
544         SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
545                 store_temp_max, 0, 1),
546         SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
547         SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
548         SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
549         SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_crit,
550                 store_temp_crit, 0, 2),
551         SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
552                 store_temp_max, 0, 2),
553         SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
554         SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
555 };
556
557 /* in attr for all models */
558 static struct sensor_device_attribute_2 fxxxx_in_attr[] = {
559         SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
560         SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
561         SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
562         SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
563         SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
564         SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
565         SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
566         SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
567         SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
568         SENSOR_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 0, 9),
569         SENSOR_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 0, 10),
570 };
571
572 /* For models with in1 alarm capability */
573 static struct sensor_device_attribute_2 fxxxx_in1_alarm_attr[] = {
574         SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
575                 0, 1),
576         SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
577                 0, 1),
578         SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
579 };
580
581 /* Fan / PWM attr common to all models */
582 static struct sensor_device_attribute_2 fxxxx_fan_attr[4][6] = { {
583         SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
584         SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
585                       show_fan_full_speed,
586                       store_fan_full_speed, 0, 0),
587         SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
588         SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
589         SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
590                       store_pwm_enable, 0, 0),
591         SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
592                       show_pwm_interpolate, store_pwm_interpolate, 0, 0),
593 }, {
594         SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
595         SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
596                       show_fan_full_speed,
597                       store_fan_full_speed, 0, 1),
598         SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
599         SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
600         SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
601                       store_pwm_enable, 0, 1),
602         SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
603                       show_pwm_interpolate, store_pwm_interpolate, 0, 1),
604 }, {
605         SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
606         SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
607                       show_fan_full_speed,
608                       store_fan_full_speed, 0, 2),
609         SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
610         SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
611         SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
612                       store_pwm_enable, 0, 2),
613         SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
614                       show_pwm_interpolate, store_pwm_interpolate, 0, 2),
615 }, {
616         SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
617         SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
618                       show_fan_full_speed,
619                       store_fan_full_speed, 0, 3),
620         SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
621         SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
622         SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
623                       store_pwm_enable, 0, 3),
624         SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
625                       show_pwm_interpolate, store_pwm_interpolate, 0, 3),
626 } };
627
628 /* Attr for the third fan of the f71808a, which only has manual pwm */
629 static struct sensor_device_attribute_2 f71808a_fan3_attr[] = {
630         SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
631         SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
632         SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR,
633                       show_simple_pwm, store_simple_pwm, 0, 2),
634 };
635
636 /* Attr for models which can beep on Fan alarm */
637 static struct sensor_device_attribute_2 fxxxx_fan_beep_attr[] = {
638         SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
639                 store_fan_beep, 0, 0),
640         SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
641                 store_fan_beep, 0, 1),
642         SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
643                 store_fan_beep, 0, 2),
644         SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
645                 store_fan_beep, 0, 3),
646 };
647
648 /*
649  * PWM attr for the f71862fg, fewer pwms and fewer zones per pwm than the
650  * standard models
651  */
652 static struct sensor_device_attribute_2 f71862fg_auto_pwm_attr[3][7] = { {
653         SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
654                       show_pwm_auto_point_channel,
655                       store_pwm_auto_point_channel, 0, 0),
656         SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
657                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
658                       1, 0),
659         SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
660                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
661                       4, 0),
662         SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
663                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
664                       0, 0),
665         SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
666                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
667                       3, 0),
668         SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
669                       show_pwm_auto_point_temp_hyst,
670                       store_pwm_auto_point_temp_hyst,
671                       0, 0),
672         SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
673                       show_pwm_auto_point_temp_hyst, NULL, 3, 0),
674 }, {
675         SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
676                       show_pwm_auto_point_channel,
677                       store_pwm_auto_point_channel, 0, 1),
678         SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
679                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
680                       1, 1),
681         SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
682                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
683                       4, 1),
684         SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
685                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
686                       0, 1),
687         SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
688                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
689                       3, 1),
690         SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
691                       show_pwm_auto_point_temp_hyst,
692                       store_pwm_auto_point_temp_hyst,
693                       0, 1),
694         SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
695                       show_pwm_auto_point_temp_hyst, NULL, 3, 1),
696 }, {
697         SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
698                       show_pwm_auto_point_channel,
699                       store_pwm_auto_point_channel, 0, 2),
700         SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
701                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
702                       1, 2),
703         SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
704                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
705                       4, 2),
706         SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
707                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
708                       0, 2),
709         SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
710                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
711                       3, 2),
712         SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
713                       show_pwm_auto_point_temp_hyst,
714                       store_pwm_auto_point_temp_hyst,
715                       0, 2),
716         SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
717                       show_pwm_auto_point_temp_hyst, NULL, 3, 2),
718 } };
719
720 /*
721  * PWM attr for the f71808e/f71869, almost identical to the f71862fg, but the
722  * pwm setting when the temperature is above the pwmX_auto_point1_temp can be
723  * programmed instead of being hardcoded to 0xff
724  */
725 static struct sensor_device_attribute_2 f71869_auto_pwm_attr[3][8] = { {
726         SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
727                       show_pwm_auto_point_channel,
728                       store_pwm_auto_point_channel, 0, 0),
729         SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
730                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
731                       0, 0),
732         SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
733                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
734                       1, 0),
735         SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
736                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
737                       4, 0),
738         SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
739                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
740                       0, 0),
741         SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
742                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
743                       3, 0),
744         SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
745                       show_pwm_auto_point_temp_hyst,
746                       store_pwm_auto_point_temp_hyst,
747                       0, 0),
748         SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
749                       show_pwm_auto_point_temp_hyst, NULL, 3, 0),
750 }, {
751         SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
752                       show_pwm_auto_point_channel,
753                       store_pwm_auto_point_channel, 0, 1),
754         SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
755                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
756                       0, 1),
757         SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
758                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
759                       1, 1),
760         SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
761                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
762                       4, 1),
763         SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
764                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
765                       0, 1),
766         SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
767                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
768                       3, 1),
769         SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
770                       show_pwm_auto_point_temp_hyst,
771                       store_pwm_auto_point_temp_hyst,
772                       0, 1),
773         SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
774                       show_pwm_auto_point_temp_hyst, NULL, 3, 1),
775 }, {
776         SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
777                       show_pwm_auto_point_channel,
778                       store_pwm_auto_point_channel, 0, 2),
779         SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
780                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
781                       0, 2),
782         SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
783                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
784                       1, 2),
785         SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
786                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
787                       4, 2),
788         SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
789                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
790                       0, 2),
791         SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
792                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
793                       3, 2),
794         SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
795                       show_pwm_auto_point_temp_hyst,
796                       store_pwm_auto_point_temp_hyst,
797                       0, 2),
798         SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
799                       show_pwm_auto_point_temp_hyst, NULL, 3, 2),
800 } };
801
802 /* PWM attr for the standard models */
803 static struct sensor_device_attribute_2 fxxxx_auto_pwm_attr[4][14] = { {
804         SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
805                       show_pwm_auto_point_channel,
806                       store_pwm_auto_point_channel, 0, 0),
807         SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
808                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
809                       0, 0),
810         SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
811                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
812                       1, 0),
813         SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
814                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
815                       2, 0),
816         SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
817                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
818                       3, 0),
819         SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
820                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
821                       4, 0),
822         SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
823                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
824                       0, 0),
825         SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
826                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
827                       1, 0),
828         SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
829                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
830                       2, 0),
831         SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
832                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
833                       3, 0),
834         SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
835                       show_pwm_auto_point_temp_hyst,
836                       store_pwm_auto_point_temp_hyst,
837                       0, 0),
838         SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
839                       show_pwm_auto_point_temp_hyst, NULL, 1, 0),
840         SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
841                       show_pwm_auto_point_temp_hyst, NULL, 2, 0),
842         SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
843                       show_pwm_auto_point_temp_hyst, NULL, 3, 0),
844 }, {
845         SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
846                       show_pwm_auto_point_channel,
847                       store_pwm_auto_point_channel, 0, 1),
848         SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
849                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
850                       0, 1),
851         SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
852                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
853                       1, 1),
854         SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
855                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
856                       2, 1),
857         SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
858                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
859                       3, 1),
860         SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
861                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
862                       4, 1),
863         SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
864                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
865                       0, 1),
866         SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
867                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
868                       1, 1),
869         SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
870                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
871                       2, 1),
872         SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
873                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
874                       3, 1),
875         SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
876                       show_pwm_auto_point_temp_hyst,
877                       store_pwm_auto_point_temp_hyst,
878                       0, 1),
879         SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
880                       show_pwm_auto_point_temp_hyst, NULL, 1, 1),
881         SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
882                       show_pwm_auto_point_temp_hyst, NULL, 2, 1),
883         SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
884                       show_pwm_auto_point_temp_hyst, NULL, 3, 1),
885 }, {
886         SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
887                       show_pwm_auto_point_channel,
888                       store_pwm_auto_point_channel, 0, 2),
889         SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
890                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
891                       0, 2),
892         SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
893                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
894                       1, 2),
895         SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
896                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
897                       2, 2),
898         SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
899                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
900                       3, 2),
901         SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
902                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
903                       4, 2),
904         SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
905                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
906                       0, 2),
907         SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
908                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
909                       1, 2),
910         SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
911                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
912                       2, 2),
913         SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
914                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
915                       3, 2),
916         SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
917                       show_pwm_auto_point_temp_hyst,
918                       store_pwm_auto_point_temp_hyst,
919                       0, 2),
920         SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
921                       show_pwm_auto_point_temp_hyst, NULL, 1, 2),
922         SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
923                       show_pwm_auto_point_temp_hyst, NULL, 2, 2),
924         SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
925                       show_pwm_auto_point_temp_hyst, NULL, 3, 2),
926 }, {
927         SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
928                       show_pwm_auto_point_channel,
929                       store_pwm_auto_point_channel, 0, 3),
930         SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
931                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
932                       0, 3),
933         SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
934                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
935                       1, 3),
936         SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
937                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
938                       2, 3),
939         SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
940                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
941                       3, 3),
942         SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
943                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
944                       4, 3),
945         SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
946                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
947                       0, 3),
948         SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
949                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
950                       1, 3),
951         SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
952                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
953                       2, 3),
954         SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
955                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
956                       3, 3),
957         SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
958                       show_pwm_auto_point_temp_hyst,
959                       store_pwm_auto_point_temp_hyst,
960                       0, 3),
961         SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
962                       show_pwm_auto_point_temp_hyst, NULL, 1, 3),
963         SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
964                       show_pwm_auto_point_temp_hyst, NULL, 2, 3),
965         SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
966                       show_pwm_auto_point_temp_hyst, NULL, 3, 3),
967 } };
968
969 /* Fan attr specific to the f8000 (4th fan input can only measure speed) */
970 static struct sensor_device_attribute_2 f8000_fan_attr[] = {
971         SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
972 };
973
974 /*
975  * PWM attr for the f8000, zones mapped to temp instead of to pwm!
976  * Also the register block at offset A0 maps to TEMP1 (so our temp2, as the
977  * F8000 starts counting temps at 0), B0 maps the TEMP2 and C0 maps to TEMP0
978  */
979 static struct sensor_device_attribute_2 f8000_auto_pwm_attr[3][14] = { {
980         SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
981                       show_pwm_auto_point_channel,
982                       store_pwm_auto_point_channel, 0, 0),
983         SENSOR_ATTR_2(temp1_auto_point1_pwm, S_IRUGO|S_IWUSR,
984                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
985                       0, 2),
986         SENSOR_ATTR_2(temp1_auto_point2_pwm, S_IRUGO|S_IWUSR,
987                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
988                       1, 2),
989         SENSOR_ATTR_2(temp1_auto_point3_pwm, S_IRUGO|S_IWUSR,
990                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
991                       2, 2),
992         SENSOR_ATTR_2(temp1_auto_point4_pwm, S_IRUGO|S_IWUSR,
993                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
994                       3, 2),
995         SENSOR_ATTR_2(temp1_auto_point5_pwm, S_IRUGO|S_IWUSR,
996                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
997                       4, 2),
998         SENSOR_ATTR_2(temp1_auto_point1_temp, S_IRUGO|S_IWUSR,
999                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1000                       0, 2),
1001         SENSOR_ATTR_2(temp1_auto_point2_temp, S_IRUGO|S_IWUSR,
1002                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1003                       1, 2),
1004         SENSOR_ATTR_2(temp1_auto_point3_temp, S_IRUGO|S_IWUSR,
1005                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1006                       2, 2),
1007         SENSOR_ATTR_2(temp1_auto_point4_temp, S_IRUGO|S_IWUSR,
1008                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1009                       3, 2),
1010         SENSOR_ATTR_2(temp1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
1011                       show_pwm_auto_point_temp_hyst,
1012                       store_pwm_auto_point_temp_hyst,
1013                       0, 2),
1014         SENSOR_ATTR_2(temp1_auto_point2_temp_hyst, S_IRUGO,
1015                       show_pwm_auto_point_temp_hyst, NULL, 1, 2),
1016         SENSOR_ATTR_2(temp1_auto_point3_temp_hyst, S_IRUGO,
1017                       show_pwm_auto_point_temp_hyst, NULL, 2, 2),
1018         SENSOR_ATTR_2(temp1_auto_point4_temp_hyst, S_IRUGO,
1019                       show_pwm_auto_point_temp_hyst, NULL, 3, 2),
1020 }, {
1021         SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
1022                       show_pwm_auto_point_channel,
1023                       store_pwm_auto_point_channel, 0, 1),
1024         SENSOR_ATTR_2(temp2_auto_point1_pwm, S_IRUGO|S_IWUSR,
1025                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1026                       0, 0),
1027         SENSOR_ATTR_2(temp2_auto_point2_pwm, S_IRUGO|S_IWUSR,
1028                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1029                       1, 0),
1030         SENSOR_ATTR_2(temp2_auto_point3_pwm, S_IRUGO|S_IWUSR,
1031                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1032                       2, 0),
1033         SENSOR_ATTR_2(temp2_auto_point4_pwm, S_IRUGO|S_IWUSR,
1034                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1035                       3, 0),
1036         SENSOR_ATTR_2(temp2_auto_point5_pwm, S_IRUGO|S_IWUSR,
1037                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1038                       4, 0),
1039         SENSOR_ATTR_2(temp2_auto_point1_temp, S_IRUGO|S_IWUSR,
1040                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1041                       0, 0),
1042         SENSOR_ATTR_2(temp2_auto_point2_temp, S_IRUGO|S_IWUSR,
1043                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1044                       1, 0),
1045         SENSOR_ATTR_2(temp2_auto_point3_temp, S_IRUGO|S_IWUSR,
1046                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1047                       2, 0),
1048         SENSOR_ATTR_2(temp2_auto_point4_temp, S_IRUGO|S_IWUSR,
1049                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1050                       3, 0),
1051         SENSOR_ATTR_2(temp2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
1052                       show_pwm_auto_point_temp_hyst,
1053                       store_pwm_auto_point_temp_hyst,
1054                       0, 0),
1055         SENSOR_ATTR_2(temp2_auto_point2_temp_hyst, S_IRUGO,
1056                       show_pwm_auto_point_temp_hyst, NULL, 1, 0),
1057         SENSOR_ATTR_2(temp2_auto_point3_temp_hyst, S_IRUGO,
1058                       show_pwm_auto_point_temp_hyst, NULL, 2, 0),
1059         SENSOR_ATTR_2(temp2_auto_point4_temp_hyst, S_IRUGO,
1060                       show_pwm_auto_point_temp_hyst, NULL, 3, 0),
1061 }, {
1062         SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
1063                       show_pwm_auto_point_channel,
1064                       store_pwm_auto_point_channel, 0, 2),
1065         SENSOR_ATTR_2(temp3_auto_point1_pwm, S_IRUGO|S_IWUSR,
1066                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1067                       0, 1),
1068         SENSOR_ATTR_2(temp3_auto_point2_pwm, S_IRUGO|S_IWUSR,
1069                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1070                       1, 1),
1071         SENSOR_ATTR_2(temp3_auto_point3_pwm, S_IRUGO|S_IWUSR,
1072                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1073                       2, 1),
1074         SENSOR_ATTR_2(temp3_auto_point4_pwm, S_IRUGO|S_IWUSR,
1075                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1076                       3, 1),
1077         SENSOR_ATTR_2(temp3_auto_point5_pwm, S_IRUGO|S_IWUSR,
1078                       show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
1079                       4, 1),
1080         SENSOR_ATTR_2(temp3_auto_point1_temp, S_IRUGO|S_IWUSR,
1081                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1082                       0, 1),
1083         SENSOR_ATTR_2(temp3_auto_point2_temp, S_IRUGO|S_IWUSR,
1084                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1085                       1, 1),
1086         SENSOR_ATTR_2(temp3_auto_point3_temp, S_IRUGO|S_IWUSR,
1087                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1088                       2, 1),
1089         SENSOR_ATTR_2(temp3_auto_point4_temp, S_IRUGO|S_IWUSR,
1090                       show_pwm_auto_point_temp, store_pwm_auto_point_temp,
1091                       3, 1),
1092         SENSOR_ATTR_2(temp3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
1093                       show_pwm_auto_point_temp_hyst,
1094                       store_pwm_auto_point_temp_hyst,
1095                       0, 1),
1096         SENSOR_ATTR_2(temp3_auto_point2_temp_hyst, S_IRUGO,
1097                       show_pwm_auto_point_temp_hyst, NULL, 1, 1),
1098         SENSOR_ATTR_2(temp3_auto_point3_temp_hyst, S_IRUGO,
1099                       show_pwm_auto_point_temp_hyst, NULL, 2, 1),
1100         SENSOR_ATTR_2(temp3_auto_point4_temp_hyst, S_IRUGO,
1101                       show_pwm_auto_point_temp_hyst, NULL, 3, 1),
1102 } };
1103
1104 /* Super I/O functions */
1105 static inline int superio_inb(int base, int reg)
1106 {
1107         outb(reg, base);
1108         return inb(base + 1);
1109 }
1110
1111 static int superio_inw(int base, int reg)
1112 {
1113         int val;
1114         val  = superio_inb(base, reg) << 8;
1115         val |= superio_inb(base, reg + 1);
1116         return val;
1117 }
1118
1119 static inline int superio_enter(int base)
1120 {
1121         /* Don't step on other drivers' I/O space by accident */
1122         if (!request_muxed_region(base, 2, DRVNAME)) {
1123                 pr_err("I/O address 0x%04x already in use\n", base);
1124                 return -EBUSY;
1125         }
1126
1127         /* according to the datasheet the key must be send twice! */
1128         outb(SIO_UNLOCK_KEY, base);
1129         outb(SIO_UNLOCK_KEY, base);
1130
1131         return 0;
1132 }
1133
1134 static inline void superio_select(int base, int ld)
1135 {
1136         outb(SIO_REG_LDSEL, base);
1137         outb(ld, base + 1);
1138 }
1139
1140 static inline void superio_exit(int base)
1141 {
1142         outb(SIO_LOCK_KEY, base);
1143         release_region(base, 2);
1144 }
1145
1146 static inline int fan_from_reg(u16 reg)
1147 {
1148         return reg ? (1500000 / reg) : 0;
1149 }
1150
1151 static inline u16 fan_to_reg(int fan)
1152 {
1153         return fan ? (1500000 / fan) : 0;
1154 }
1155
1156 static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
1157 {
1158         u8 val;
1159
1160         outb(reg, data->addr + ADDR_REG_OFFSET);
1161         val = inb(data->addr + DATA_REG_OFFSET);
1162
1163         return val;
1164 }
1165
1166 static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
1167 {
1168         u16 val;
1169
1170         val  = f71882fg_read8(data, reg) << 8;
1171         val |= f71882fg_read8(data, reg + 1);
1172
1173         return val;
1174 }
1175
1176 static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
1177 {
1178         outb(reg, data->addr + ADDR_REG_OFFSET);
1179         outb(val, data->addr + DATA_REG_OFFSET);
1180 }
1181
1182 static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
1183 {
1184         f71882fg_write8(data, reg,     val >> 8);
1185         f71882fg_write8(data, reg + 1, val & 0xff);
1186 }
1187
1188 static u16 f71882fg_read_temp(struct f71882fg_data *data, int nr)
1189 {
1190         if (data->type == f71858fg)
1191                 return f71882fg_read16(data, F71882FG_REG_TEMP(nr));
1192         else
1193                 return f71882fg_read8(data, F71882FG_REG_TEMP(nr));
1194 }
1195
1196 static struct f71882fg_data *f71882fg_update_device(struct device *dev)
1197 {
1198         struct f71882fg_data *data = dev_get_drvdata(dev);
1199         int nr_fans = f71882fg_nr_fans[data->type];
1200         int nr_temps = f71882fg_nr_temps[data->type];
1201         int nr, reg, point;
1202
1203         mutex_lock(&data->update_lock);
1204
1205         /* Update once every 60 seconds */
1206         if (time_after(jiffies, data->last_limits + 60 * HZ) ||
1207                         !data->valid) {
1208                 if (f71882fg_has_in1_alarm[data->type]) {
1209                         if (data->type == f81866a) {
1210                                 data->in1_max =
1211                                         f71882fg_read8(data,
1212                                                        F81866_REG_IN1_HIGH);
1213                                 data->in_beep =
1214                                         f71882fg_read8(data,
1215                                                        F81866_REG_IN_BEEP);
1216                         } else {
1217                                 data->in1_max =
1218                                         f71882fg_read8(data,
1219                                                        F71882FG_REG_IN1_HIGH);
1220                                 data->in_beep =
1221                                         f71882fg_read8(data,
1222                                                        F71882FG_REG_IN_BEEP);
1223                         }
1224                 }
1225
1226                 /* Get High & boundary temps*/
1227                 for (nr = data->temp_start; nr < nr_temps + data->temp_start;
1228                                                                         nr++) {
1229                         data->temp_ovt[nr] = f71882fg_read8(data,
1230                                                 F71882FG_REG_TEMP_OVT(nr));
1231                         data->temp_high[nr] = f71882fg_read8(data,
1232                                                 F71882FG_REG_TEMP_HIGH(nr));
1233                 }
1234
1235                 if (data->type != f8000) {
1236                         data->temp_hyst[0] = f71882fg_read8(data,
1237                                                 F71882FG_REG_TEMP_HYST(0));
1238                         data->temp_hyst[1] = f71882fg_read8(data,
1239                                                 F71882FG_REG_TEMP_HYST(1));
1240                 }
1241                 /* All but the f71858fg / f8000 have this register */
1242                 if ((data->type != f71858fg) && (data->type != f8000)) {
1243                         reg  = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
1244                         data->temp_type[1] = (reg & 0x02) ? 2 : 4;
1245                         data->temp_type[2] = (reg & 0x04) ? 2 : 4;
1246                         data->temp_type[3] = (reg & 0x08) ? 2 : 4;
1247                 }
1248
1249                 if (f71882fg_fan_has_beep[data->type])
1250                         data->fan_beep = f71882fg_read8(data,
1251                                                 F71882FG_REG_FAN_BEEP);
1252
1253                 if (f71882fg_temp_has_beep[data->type])
1254                         data->temp_beep = f71882fg_read8(data,
1255                                                 F71882FG_REG_TEMP_BEEP);
1256
1257                 data->pwm_enable = f71882fg_read8(data,
1258                                                   F71882FG_REG_PWM_ENABLE);
1259                 data->pwm_auto_point_hyst[0] =
1260                         f71882fg_read8(data, F71882FG_REG_FAN_HYST(0));
1261                 data->pwm_auto_point_hyst[1] =
1262                         f71882fg_read8(data, F71882FG_REG_FAN_HYST(1));
1263
1264                 for (nr = 0; nr < nr_fans; nr++) {
1265                         data->pwm_auto_point_mapping[nr] =
1266                             f71882fg_read8(data,
1267                                            F71882FG_REG_POINT_MAPPING(nr));
1268
1269                         switch (data->type) {
1270                         default:
1271                                 for (point = 0; point < 5; point++) {
1272                                         data->pwm_auto_point_pwm[nr][point] =
1273                                                 f71882fg_read8(data,
1274                                                         F71882FG_REG_POINT_PWM
1275                                                         (nr, point));
1276                                 }
1277                                 for (point = 0; point < 4; point++) {
1278                                         data->pwm_auto_point_temp[nr][point] =
1279                                                 f71882fg_read8(data,
1280                                                         F71882FG_REG_POINT_TEMP
1281                                                         (nr, point));
1282                                 }
1283                                 break;
1284                         case f71808e:
1285                         case f71869:
1286                                 data->pwm_auto_point_pwm[nr][0] =
1287                                         f71882fg_read8(data,
1288                                                 F71882FG_REG_POINT_PWM(nr, 0));
1289                                 fallthrough;
1290                         case f71862fg:
1291                                 data->pwm_auto_point_pwm[nr][1] =
1292                                         f71882fg_read8(data,
1293                                                 F71882FG_REG_POINT_PWM
1294                                                 (nr, 1));
1295                                 data->pwm_auto_point_pwm[nr][4] =
1296                                         f71882fg_read8(data,
1297                                                 F71882FG_REG_POINT_PWM
1298                                                 (nr, 4));
1299                                 data->pwm_auto_point_temp[nr][0] =
1300                                         f71882fg_read8(data,
1301                                                 F71882FG_REG_POINT_TEMP
1302                                                 (nr, 0));
1303                                 data->pwm_auto_point_temp[nr][3] =
1304                                         f71882fg_read8(data,
1305                                                 F71882FG_REG_POINT_TEMP
1306                                                 (nr, 3));
1307                                 break;
1308                         }
1309                 }
1310                 data->last_limits = jiffies;
1311         }
1312
1313         /* Update every second */
1314         if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
1315                 data->temp_status = f71882fg_read8(data,
1316                                                 F71882FG_REG_TEMP_STATUS);
1317                 data->temp_diode_open = f71882fg_read8(data,
1318                                                 F71882FG_REG_TEMP_DIODE_OPEN);
1319                 for (nr = data->temp_start; nr < nr_temps + data->temp_start;
1320                                                                         nr++)
1321                         data->temp[nr] = f71882fg_read_temp(data, nr);
1322
1323                 data->fan_status = f71882fg_read8(data,
1324                                                 F71882FG_REG_FAN_STATUS);
1325                 for (nr = 0; nr < nr_fans; nr++) {
1326                         data->fan[nr] = f71882fg_read16(data,
1327                                                 F71882FG_REG_FAN(nr));
1328                         data->fan_target[nr] =
1329                             f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
1330                         data->fan_full_speed[nr] =
1331                             f71882fg_read16(data,
1332                                             F71882FG_REG_FAN_FULL_SPEED(nr));
1333                         data->pwm[nr] =
1334                             f71882fg_read8(data, F71882FG_REG_PWM(nr));
1335                 }
1336                 /* Some models have 1 more fan with limited capabilities */
1337                 if (data->type == f71808a) {
1338                         data->fan[2] = f71882fg_read16(data,
1339                                                 F71882FG_REG_FAN(2));
1340                         data->pwm[2] = f71882fg_read8(data,
1341                                                         F71882FG_REG_PWM(2));
1342                 }
1343                 if (data->type == f8000)
1344                         data->fan[3] = f71882fg_read16(data,
1345                                                 F71882FG_REG_FAN(3));
1346
1347                 if (f71882fg_has_in1_alarm[data->type]) {
1348                         if (data->type == f81866a)
1349                                 data->in_status = f71882fg_read8(data,
1350                                                 F81866_REG_IN_STATUS);
1351
1352                         else
1353                                 data->in_status = f71882fg_read8(data,
1354                                                 F71882FG_REG_IN_STATUS);
1355                 }
1356
1357                 for (nr = 0; nr < F71882FG_MAX_INS; nr++)
1358                         if (f71882fg_has_in[data->type][nr])
1359                                 data->in[nr] = f71882fg_read8(data,
1360                                                         F71882FG_REG_IN(nr));
1361
1362                 data->last_updated = jiffies;
1363                 data->valid = true;
1364         }
1365
1366         mutex_unlock(&data->update_lock);
1367
1368         return data;
1369 }
1370
1371 /* Sysfs Interface */
1372 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
1373         char *buf)
1374 {
1375         struct f71882fg_data *data = f71882fg_update_device(dev);
1376         int nr = to_sensor_dev_attr_2(devattr)->index;
1377         int speed = fan_from_reg(data->fan[nr]);
1378
1379         if (speed == FAN_MIN_DETECT)
1380                 speed = 0;
1381
1382         return sprintf(buf, "%d\n", speed);
1383 }
1384
1385 static ssize_t show_fan_full_speed(struct device *dev,
1386                                    struct device_attribute *devattr, char *buf)
1387 {
1388         struct f71882fg_data *data = f71882fg_update_device(dev);
1389         int nr = to_sensor_dev_attr_2(devattr)->index;
1390         int speed = fan_from_reg(data->fan_full_speed[nr]);
1391         return sprintf(buf, "%d\n", speed);
1392 }
1393
1394 static ssize_t store_fan_full_speed(struct device *dev,
1395                                     struct device_attribute *devattr,
1396                                     const char *buf, size_t count)
1397 {
1398         struct f71882fg_data *data = dev_get_drvdata(dev);
1399         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1400         long val;
1401
1402         err = kstrtol(buf, 10, &val);
1403         if (err)
1404                 return err;
1405
1406         val = clamp_val(val, 23, 1500000);
1407         val = fan_to_reg(val);
1408
1409         mutex_lock(&data->update_lock);
1410         f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
1411         data->fan_full_speed[nr] = val;
1412         mutex_unlock(&data->update_lock);
1413
1414         return count;
1415 }
1416
1417 static ssize_t show_fan_beep(struct device *dev, struct device_attribute
1418         *devattr, char *buf)
1419 {
1420         struct f71882fg_data *data = f71882fg_update_device(dev);
1421         int nr = to_sensor_dev_attr_2(devattr)->index;
1422
1423         if (data->fan_beep & (1 << nr))
1424                 return sprintf(buf, "1\n");
1425         else
1426                 return sprintf(buf, "0\n");
1427 }
1428
1429 static ssize_t store_fan_beep(struct device *dev, struct device_attribute
1430         *devattr, const char *buf, size_t count)
1431 {
1432         struct f71882fg_data *data = dev_get_drvdata(dev);
1433         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1434         unsigned long val;
1435
1436         err = kstrtoul(buf, 10, &val);
1437         if (err)
1438                 return err;
1439
1440         mutex_lock(&data->update_lock);
1441         data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
1442         if (val)
1443                 data->fan_beep |= 1 << nr;
1444         else
1445                 data->fan_beep &= ~(1 << nr);
1446
1447         f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
1448         mutex_unlock(&data->update_lock);
1449
1450         return count;
1451 }
1452
1453 static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
1454         *devattr, char *buf)
1455 {
1456         struct f71882fg_data *data = f71882fg_update_device(dev);
1457         int nr = to_sensor_dev_attr_2(devattr)->index;
1458
1459         if (data->fan_status & (1 << nr))
1460                 return sprintf(buf, "1\n");
1461         else
1462                 return sprintf(buf, "0\n");
1463 }
1464
1465 static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
1466         char *buf)
1467 {
1468         struct f71882fg_data *data = f71882fg_update_device(dev);
1469         int nr = to_sensor_dev_attr_2(devattr)->index;
1470
1471         return sprintf(buf, "%d\n", data->in[nr] * 8);
1472 }
1473
1474 static ssize_t show_in_max(struct device *dev, struct device_attribute
1475         *devattr, char *buf)
1476 {
1477         struct f71882fg_data *data = f71882fg_update_device(dev);
1478
1479         return sprintf(buf, "%d\n", data->in1_max * 8);
1480 }
1481
1482 static ssize_t store_in_max(struct device *dev, struct device_attribute
1483         *devattr, const char *buf, size_t count)
1484 {
1485         struct f71882fg_data *data = dev_get_drvdata(dev);
1486         int err;
1487         long val;
1488
1489         err = kstrtol(buf, 10, &val);
1490         if (err)
1491                 return err;
1492
1493         val /= 8;
1494         val = clamp_val(val, 0, 255);
1495
1496         mutex_lock(&data->update_lock);
1497         if (data->type == f81866a)
1498                 f71882fg_write8(data, F81866_REG_IN1_HIGH, val);
1499         else
1500                 f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
1501         data->in1_max = val;
1502         mutex_unlock(&data->update_lock);
1503
1504         return count;
1505 }
1506
1507 static ssize_t show_in_beep(struct device *dev, struct device_attribute
1508         *devattr, char *buf)
1509 {
1510         struct f71882fg_data *data = f71882fg_update_device(dev);
1511         int nr = to_sensor_dev_attr_2(devattr)->index;
1512
1513         if (data->in_beep & (1 << nr))
1514                 return sprintf(buf, "1\n");
1515         else
1516                 return sprintf(buf, "0\n");
1517 }
1518
1519 static ssize_t store_in_beep(struct device *dev, struct device_attribute
1520         *devattr, const char *buf, size_t count)
1521 {
1522         struct f71882fg_data *data = dev_get_drvdata(dev);
1523         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1524         unsigned long val;
1525
1526         err = kstrtoul(buf, 10, &val);
1527         if (err)
1528                 return err;
1529
1530         mutex_lock(&data->update_lock);
1531         if (data->type == f81866a)
1532                 data->in_beep = f71882fg_read8(data, F81866_REG_IN_BEEP);
1533         else
1534                 data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
1535
1536         if (val)
1537                 data->in_beep |= 1 << nr;
1538         else
1539                 data->in_beep &= ~(1 << nr);
1540
1541         if (data->type == f81866a)
1542                 f71882fg_write8(data, F81866_REG_IN_BEEP, data->in_beep);
1543         else
1544                 f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
1545         mutex_unlock(&data->update_lock);
1546
1547         return count;
1548 }
1549
1550 static ssize_t show_in_alarm(struct device *dev, struct device_attribute
1551         *devattr, char *buf)
1552 {
1553         struct f71882fg_data *data = f71882fg_update_device(dev);
1554         int nr = to_sensor_dev_attr_2(devattr)->index;
1555
1556         if (data->in_status & (1 << nr))
1557                 return sprintf(buf, "1\n");
1558         else
1559                 return sprintf(buf, "0\n");
1560 }
1561
1562 static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
1563         char *buf)
1564 {
1565         struct f71882fg_data *data = f71882fg_update_device(dev);
1566         int nr = to_sensor_dev_attr_2(devattr)->index;
1567         int sign, temp;
1568
1569         if (data->type == f71858fg) {
1570                 /* TEMP_TABLE_SEL 1 or 3 ? */
1571                 if (data->temp_config & 1) {
1572                         sign = data->temp[nr] & 0x0001;
1573                         temp = (data->temp[nr] >> 5) & 0x7ff;
1574                 } else {
1575                         sign = data->temp[nr] & 0x8000;
1576                         temp = (data->temp[nr] >> 5) & 0x3ff;
1577                 }
1578                 temp *= 125;
1579                 if (sign)
1580                         temp -= 128000;
1581         } else {
1582                 temp = ((s8)data->temp[nr]) * 1000;
1583         }
1584
1585         return sprintf(buf, "%d\n", temp);
1586 }
1587
1588 static ssize_t show_temp_max(struct device *dev, struct device_attribute
1589         *devattr, char *buf)
1590 {
1591         struct f71882fg_data *data = f71882fg_update_device(dev);
1592         int nr = to_sensor_dev_attr_2(devattr)->index;
1593
1594         return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
1595 }
1596
1597 static ssize_t store_temp_max(struct device *dev, struct device_attribute
1598         *devattr, const char *buf, size_t count)
1599 {
1600         struct f71882fg_data *data = dev_get_drvdata(dev);
1601         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1602         long val;
1603
1604         err = kstrtol(buf, 10, &val);
1605         if (err)
1606                 return err;
1607
1608         val /= 1000;
1609         val = clamp_val(val, 0, 255);
1610
1611         mutex_lock(&data->update_lock);
1612         f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
1613         data->temp_high[nr] = val;
1614         mutex_unlock(&data->update_lock);
1615
1616         return count;
1617 }
1618
1619 static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
1620         *devattr, char *buf)
1621 {
1622         struct f71882fg_data *data = f71882fg_update_device(dev);
1623         int nr = to_sensor_dev_attr_2(devattr)->index;
1624         int temp_max_hyst;
1625
1626         mutex_lock(&data->update_lock);
1627         if (nr & 1)
1628                 temp_max_hyst = data->temp_hyst[nr / 2] >> 4;
1629         else
1630                 temp_max_hyst = data->temp_hyst[nr / 2] & 0x0f;
1631         temp_max_hyst = (data->temp_high[nr] - temp_max_hyst) * 1000;
1632         mutex_unlock(&data->update_lock);
1633
1634         return sprintf(buf, "%d\n", temp_max_hyst);
1635 }
1636
1637 static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
1638         *devattr, const char *buf, size_t count)
1639 {
1640         struct f71882fg_data *data = dev_get_drvdata(dev);
1641         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1642         ssize_t ret = count;
1643         u8 reg;
1644         long val;
1645
1646         err = kstrtol(buf, 10, &val);
1647         if (err)
1648                 return err;
1649
1650         val /= 1000;
1651
1652         mutex_lock(&data->update_lock);
1653
1654         /* convert abs to relative and check */
1655         data->temp_high[nr] = f71882fg_read8(data, F71882FG_REG_TEMP_HIGH(nr));
1656         val = clamp_val(val, data->temp_high[nr] - 15, data->temp_high[nr]);
1657         val = data->temp_high[nr] - val;
1658
1659         /* convert value to register contents */
1660         reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST(nr / 2));
1661         if (nr & 1)
1662                 reg = (reg & 0x0f) | (val << 4);
1663         else
1664                 reg = (reg & 0xf0) | val;
1665         f71882fg_write8(data, F71882FG_REG_TEMP_HYST(nr / 2), reg);
1666         data->temp_hyst[nr / 2] = reg;
1667
1668         mutex_unlock(&data->update_lock);
1669         return ret;
1670 }
1671
1672 static ssize_t show_temp_crit(struct device *dev, struct device_attribute
1673         *devattr, char *buf)
1674 {
1675         struct f71882fg_data *data = f71882fg_update_device(dev);
1676         int nr = to_sensor_dev_attr_2(devattr)->index;
1677
1678         return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
1679 }
1680
1681 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
1682         *devattr, const char *buf, size_t count)
1683 {
1684         struct f71882fg_data *data = dev_get_drvdata(dev);
1685         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1686         long val;
1687
1688         err = kstrtol(buf, 10, &val);
1689         if (err)
1690                 return err;
1691
1692         val /= 1000;
1693         val = clamp_val(val, 0, 255);
1694
1695         mutex_lock(&data->update_lock);
1696         f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
1697         data->temp_ovt[nr] = val;
1698         mutex_unlock(&data->update_lock);
1699
1700         return count;
1701 }
1702
1703 static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
1704         *devattr, char *buf)
1705 {
1706         struct f71882fg_data *data = f71882fg_update_device(dev);
1707         int nr = to_sensor_dev_attr_2(devattr)->index;
1708         int temp_crit_hyst;
1709
1710         mutex_lock(&data->update_lock);
1711         if (nr & 1)
1712                 temp_crit_hyst = data->temp_hyst[nr / 2] >> 4;
1713         else
1714                 temp_crit_hyst = data->temp_hyst[nr / 2] & 0x0f;
1715         temp_crit_hyst = (data->temp_ovt[nr] - temp_crit_hyst) * 1000;
1716         mutex_unlock(&data->update_lock);
1717
1718         return sprintf(buf, "%d\n", temp_crit_hyst);
1719 }
1720
1721 static ssize_t show_temp_type(struct device *dev, struct device_attribute
1722         *devattr, char *buf)
1723 {
1724         struct f71882fg_data *data = f71882fg_update_device(dev);
1725         int nr = to_sensor_dev_attr_2(devattr)->index;
1726
1727         return sprintf(buf, "%d\n", data->temp_type[nr]);
1728 }
1729
1730 static ssize_t show_temp_beep(struct device *dev, struct device_attribute
1731         *devattr, char *buf)
1732 {
1733         struct f71882fg_data *data = f71882fg_update_device(dev);
1734         int nr = to_sensor_dev_attr_2(devattr)->index;
1735
1736         if (data->temp_beep & (1 << nr))
1737                 return sprintf(buf, "1\n");
1738         else
1739                 return sprintf(buf, "0\n");
1740 }
1741
1742 static ssize_t store_temp_beep(struct device *dev, struct device_attribute
1743         *devattr, const char *buf, size_t count)
1744 {
1745         struct f71882fg_data *data = dev_get_drvdata(dev);
1746         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1747         unsigned long val;
1748
1749         err = kstrtoul(buf, 10, &val);
1750         if (err)
1751                 return err;
1752
1753         mutex_lock(&data->update_lock);
1754         data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
1755         if (val)
1756                 data->temp_beep |= 1 << nr;
1757         else
1758                 data->temp_beep &= ~(1 << nr);
1759
1760         f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
1761         mutex_unlock(&data->update_lock);
1762
1763         return count;
1764 }
1765
1766 static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
1767         *devattr, char *buf)
1768 {
1769         struct f71882fg_data *data = f71882fg_update_device(dev);
1770         int nr = to_sensor_dev_attr_2(devattr)->index;
1771
1772         if (data->temp_status & (1 << nr))
1773                 return sprintf(buf, "1\n");
1774         else
1775                 return sprintf(buf, "0\n");
1776 }
1777
1778 static ssize_t show_temp_fault(struct device *dev, struct device_attribute
1779         *devattr, char *buf)
1780 {
1781         struct f71882fg_data *data = f71882fg_update_device(dev);
1782         int nr = to_sensor_dev_attr_2(devattr)->index;
1783
1784         if (data->temp_diode_open & (1 << nr))
1785                 return sprintf(buf, "1\n");
1786         else
1787                 return sprintf(buf, "0\n");
1788 }
1789
1790 static ssize_t show_pwm(struct device *dev,
1791                         struct device_attribute *devattr, char *buf)
1792 {
1793         struct f71882fg_data *data = f71882fg_update_device(dev);
1794         int val, nr = to_sensor_dev_attr_2(devattr)->index;
1795         mutex_lock(&data->update_lock);
1796         if (data->pwm_enable & (1 << (2 * nr)))
1797                 /* PWM mode */
1798                 val = data->pwm[nr];
1799         else {
1800                 /* RPM mode */
1801                 val = 255 * fan_from_reg(data->fan_target[nr])
1802                         / fan_from_reg(data->fan_full_speed[nr]);
1803         }
1804         mutex_unlock(&data->update_lock);
1805         return sprintf(buf, "%d\n", val);
1806 }
1807
1808 static ssize_t store_pwm(struct device *dev,
1809                          struct device_attribute *devattr, const char *buf,
1810                          size_t count)
1811 {
1812         struct f71882fg_data *data = dev_get_drvdata(dev);
1813         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1814         long val;
1815
1816         err = kstrtol(buf, 10, &val);
1817         if (err)
1818                 return err;
1819
1820         val = clamp_val(val, 0, 255);
1821
1822         mutex_lock(&data->update_lock);
1823         data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1824         if ((data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 3) != 2) ||
1825             (data->type != f8000 && !((data->pwm_enable >> 2 * nr) & 2))) {
1826                 count = -EROFS;
1827                 goto leave;
1828         }
1829         if (data->pwm_enable & (1 << (2 * nr))) {
1830                 /* PWM mode */
1831                 f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1832                 data->pwm[nr] = val;
1833         } else {
1834                 /* RPM mode */
1835                 int target, full_speed;
1836                 full_speed = f71882fg_read16(data,
1837                                              F71882FG_REG_FAN_FULL_SPEED(nr));
1838                 target = fan_to_reg(val * fan_from_reg(full_speed) / 255);
1839                 f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr), target);
1840                 data->fan_target[nr] = target;
1841                 data->fan_full_speed[nr] = full_speed;
1842         }
1843 leave:
1844         mutex_unlock(&data->update_lock);
1845
1846         return count;
1847 }
1848
1849 static ssize_t show_simple_pwm(struct device *dev,
1850                                struct device_attribute *devattr, char *buf)
1851 {
1852         struct f71882fg_data *data = f71882fg_update_device(dev);
1853         int val, nr = to_sensor_dev_attr_2(devattr)->index;
1854
1855         val = data->pwm[nr];
1856         return sprintf(buf, "%d\n", val);
1857 }
1858
1859 static ssize_t store_simple_pwm(struct device *dev,
1860                                 struct device_attribute *devattr,
1861                                 const char *buf, size_t count)
1862 {
1863         struct f71882fg_data *data = dev_get_drvdata(dev);
1864         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1865         long val;
1866
1867         err = kstrtol(buf, 10, &val);
1868         if (err)
1869                 return err;
1870
1871         val = clamp_val(val, 0, 255);
1872
1873         mutex_lock(&data->update_lock);
1874         f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
1875         data->pwm[nr] = val;
1876         mutex_unlock(&data->update_lock);
1877
1878         return count;
1879 }
1880
1881 static ssize_t show_pwm_enable(struct device *dev,
1882                                struct device_attribute *devattr, char *buf)
1883 {
1884         int result = 0;
1885         struct f71882fg_data *data = f71882fg_update_device(dev);
1886         int nr = to_sensor_dev_attr_2(devattr)->index;
1887
1888         switch ((data->pwm_enable >> 2 * nr) & 3) {
1889         case 0:
1890         case 1:
1891                 result = 2; /* Normal auto mode */
1892                 break;
1893         case 2:
1894                 result = 1; /* Manual mode */
1895                 break;
1896         case 3:
1897                 if (data->type == f8000)
1898                         result = 3; /* Thermostat mode */
1899                 else
1900                         result = 1; /* Manual mode */
1901                 break;
1902         }
1903
1904         return sprintf(buf, "%d\n", result);
1905 }
1906
1907 static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
1908                                 *devattr, const char *buf, size_t count)
1909 {
1910         struct f71882fg_data *data = dev_get_drvdata(dev);
1911         int err, nr = to_sensor_dev_attr_2(devattr)->index;
1912         long val;
1913
1914         err = kstrtol(buf, 10, &val);
1915         if (err)
1916                 return err;
1917
1918         /* Special case for F8000 pwm channel 3 which only does auto mode */
1919         if (data->type == f8000 && nr == 2 && val != 2)
1920                 return -EINVAL;
1921
1922         mutex_lock(&data->update_lock);
1923         data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
1924         /* Special case for F8000 auto PWM mode / Thermostat mode */
1925         if (data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 1)) {
1926                 switch (val) {
1927                 case 2:
1928                         data->pwm_enable &= ~(2 << (2 * nr));
1929                         break;          /* Normal auto mode */
1930                 case 3:
1931                         data->pwm_enable |= 2 << (2 * nr);
1932                         break;          /* Thermostat mode */
1933                 default:
1934                         count = -EINVAL;
1935                         goto leave;
1936                 }
1937         } else {
1938                 switch (val) {
1939                 case 1:
1940                         /* The f71858fg does not support manual RPM mode */
1941                         if (data->type == f71858fg &&
1942                             ((data->pwm_enable >> (2 * nr)) & 1)) {
1943                                 count = -EINVAL;
1944                                 goto leave;
1945                         }
1946                         data->pwm_enable |= 2 << (2 * nr);
1947                         break;          /* Manual */
1948                 case 2:
1949                         data->pwm_enable &= ~(2 << (2 * nr));
1950                         break;          /* Normal auto mode */
1951                 default:
1952                         count = -EINVAL;
1953                         goto leave;
1954                 }
1955         }
1956         f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
1957 leave:
1958         mutex_unlock(&data->update_lock);
1959
1960         return count;
1961 }
1962
1963 static ssize_t show_pwm_auto_point_pwm(struct device *dev,
1964                                        struct device_attribute *devattr,
1965                                        char *buf)
1966 {
1967         int result;
1968         struct f71882fg_data *data = f71882fg_update_device(dev);
1969         int pwm = to_sensor_dev_attr_2(devattr)->index;
1970         int point = to_sensor_dev_attr_2(devattr)->nr;
1971
1972         mutex_lock(&data->update_lock);
1973         if (data->pwm_enable & (1 << (2 * pwm))) {
1974                 /* PWM mode */
1975                 result = data->pwm_auto_point_pwm[pwm][point];
1976         } else {
1977                 /* RPM mode */
1978                 result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
1979         }
1980         mutex_unlock(&data->update_lock);
1981
1982         return sprintf(buf, "%d\n", result);
1983 }
1984
1985 static ssize_t store_pwm_auto_point_pwm(struct device *dev,
1986                                         struct device_attribute *devattr,
1987                                         const char *buf, size_t count)
1988 {
1989         struct f71882fg_data *data = dev_get_drvdata(dev);
1990         int err, pwm = to_sensor_dev_attr_2(devattr)->index;
1991         int point = to_sensor_dev_attr_2(devattr)->nr;
1992         long val;
1993
1994         err = kstrtol(buf, 10, &val);
1995         if (err)
1996                 return err;
1997
1998         val = clamp_val(val, 0, 255);
1999
2000         mutex_lock(&data->update_lock);
2001         data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
2002         if (data->pwm_enable & (1 << (2 * pwm))) {
2003                 /* PWM mode */
2004         } else {
2005                 /* RPM mode */
2006                 if (val < 29)   /* Prevent negative numbers */
2007                         val = 255;
2008                 else
2009                         val = (255 - val) * 32 / val;
2010         }
2011         f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
2012         data->pwm_auto_point_pwm[pwm][point] = val;
2013         mutex_unlock(&data->update_lock);
2014
2015         return count;
2016 }
2017
2018 static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
2019                                              struct device_attribute *devattr,
2020                                              char *buf)
2021 {
2022         int result = 0;
2023         struct f71882fg_data *data = f71882fg_update_device(dev);
2024         int nr = to_sensor_dev_attr_2(devattr)->index;
2025         int point = to_sensor_dev_attr_2(devattr)->nr;
2026
2027         mutex_lock(&data->update_lock);
2028         if (nr & 1)
2029                 result = data->pwm_auto_point_hyst[nr / 2] >> 4;
2030         else
2031                 result = data->pwm_auto_point_hyst[nr / 2] & 0x0f;
2032         result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
2033         mutex_unlock(&data->update_lock);
2034
2035         return sprintf(buf, "%d\n", result);
2036 }
2037
2038 static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
2039                                               struct device_attribute *devattr,
2040                                               const char *buf, size_t count)
2041 {
2042         struct f71882fg_data *data = dev_get_drvdata(dev);
2043         int err, nr = to_sensor_dev_attr_2(devattr)->index;
2044         int point = to_sensor_dev_attr_2(devattr)->nr;
2045         u8 reg;
2046         long val;
2047
2048         err = kstrtol(buf, 10, &val);
2049         if (err)
2050                 return err;
2051
2052         val /= 1000;
2053
2054         mutex_lock(&data->update_lock);
2055         data->pwm_auto_point_temp[nr][point] =
2056                 f71882fg_read8(data, F71882FG_REG_POINT_TEMP(nr, point));
2057         val = clamp_val(val, data->pwm_auto_point_temp[nr][point] - 15,
2058                         data->pwm_auto_point_temp[nr][point]);
2059         val = data->pwm_auto_point_temp[nr][point] - val;
2060
2061         reg = f71882fg_read8(data, F71882FG_REG_FAN_HYST(nr / 2));
2062         if (nr & 1)
2063                 reg = (reg & 0x0f) | (val << 4);
2064         else
2065                 reg = (reg & 0xf0) | val;
2066
2067         f71882fg_write8(data, F71882FG_REG_FAN_HYST(nr / 2), reg);
2068         data->pwm_auto_point_hyst[nr / 2] = reg;
2069         mutex_unlock(&data->update_lock);
2070
2071         return count;
2072 }
2073
2074 static ssize_t show_pwm_interpolate(struct device *dev,
2075                                     struct device_attribute *devattr, char *buf)
2076 {
2077         int result;
2078         struct f71882fg_data *data = f71882fg_update_device(dev);
2079         int nr = to_sensor_dev_attr_2(devattr)->index;
2080
2081         result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
2082
2083         return sprintf(buf, "%d\n", result);
2084 }
2085
2086 static ssize_t store_pwm_interpolate(struct device *dev,
2087                                      struct device_attribute *devattr,
2088                                      const char *buf, size_t count)
2089 {
2090         struct f71882fg_data *data = dev_get_drvdata(dev);
2091         int err, nr = to_sensor_dev_attr_2(devattr)->index;
2092         unsigned long val;
2093
2094         err = kstrtoul(buf, 10, &val);
2095         if (err)
2096                 return err;
2097
2098         mutex_lock(&data->update_lock);
2099         data->pwm_auto_point_mapping[nr] =
2100                 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
2101         if (val)
2102                 val = data->pwm_auto_point_mapping[nr] | (1 << 4);
2103         else
2104                 val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
2105         f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
2106         data->pwm_auto_point_mapping[nr] = val;
2107         mutex_unlock(&data->update_lock);
2108
2109         return count;
2110 }
2111
2112 static ssize_t show_pwm_auto_point_channel(struct device *dev,
2113                                            struct device_attribute *devattr,
2114                                            char *buf)
2115 {
2116         int result;
2117         struct f71882fg_data *data = f71882fg_update_device(dev);
2118         int nr = to_sensor_dev_attr_2(devattr)->index;
2119
2120         result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) -
2121                        data->temp_start);
2122
2123         return sprintf(buf, "%d\n", result);
2124 }
2125
2126 static ssize_t store_pwm_auto_point_channel(struct device *dev,
2127                                             struct device_attribute *devattr,
2128                                             const char *buf, size_t count)
2129 {
2130         struct f71882fg_data *data = dev_get_drvdata(dev);
2131         int err, nr = to_sensor_dev_attr_2(devattr)->index;
2132         long val;
2133
2134         err = kstrtol(buf, 10, &val);
2135         if (err)
2136                 return err;
2137
2138         switch (val) {
2139         case 1:
2140                 val = 0;
2141                 break;
2142         case 2:
2143                 val = 1;
2144                 break;
2145         case 4:
2146                 val = 2;
2147                 break;
2148         default:
2149                 return -EINVAL;
2150         }
2151         val += data->temp_start;
2152         mutex_lock(&data->update_lock);
2153         data->pwm_auto_point_mapping[nr] =
2154                 f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
2155         val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
2156         f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
2157         data->pwm_auto_point_mapping[nr] = val;
2158         mutex_unlock(&data->update_lock);
2159
2160         return count;
2161 }
2162
2163 static ssize_t show_pwm_auto_point_temp(struct device *dev,
2164                                         struct device_attribute *devattr,
2165                                         char *buf)
2166 {
2167         int result;
2168         struct f71882fg_data *data = f71882fg_update_device(dev);
2169         int pwm = to_sensor_dev_attr_2(devattr)->index;
2170         int point = to_sensor_dev_attr_2(devattr)->nr;
2171
2172         result = data->pwm_auto_point_temp[pwm][point];
2173         return sprintf(buf, "%d\n", 1000 * result);
2174 }
2175
2176 static ssize_t store_pwm_auto_point_temp(struct device *dev,
2177                                          struct device_attribute *devattr,
2178                                          const char *buf, size_t count)
2179 {
2180         struct f71882fg_data *data = dev_get_drvdata(dev);
2181         int err, pwm = to_sensor_dev_attr_2(devattr)->index;
2182         int point = to_sensor_dev_attr_2(devattr)->nr;
2183         long val;
2184
2185         err = kstrtol(buf, 10, &val);
2186         if (err)
2187                 return err;
2188
2189         val /= 1000;
2190
2191         if (data->auto_point_temp_signed)
2192                 val = clamp_val(val, -128, 127);
2193         else
2194                 val = clamp_val(val, 0, 127);
2195
2196         mutex_lock(&data->update_lock);
2197         f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
2198         data->pwm_auto_point_temp[pwm][point] = val;
2199         mutex_unlock(&data->update_lock);
2200
2201         return count;
2202 }
2203
2204 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
2205         char *buf)
2206 {
2207         struct f71882fg_data *data = dev_get_drvdata(dev);
2208         return sprintf(buf, "%s\n", f71882fg_names[data->type]);
2209 }
2210
2211 static int f71882fg_create_sysfs_files(struct platform_device *pdev,
2212         struct sensor_device_attribute_2 *attr, int count)
2213 {
2214         int err, i;
2215
2216         for (i = 0; i < count; i++) {
2217                 err = device_create_file(&pdev->dev, &attr[i].dev_attr);
2218                 if (err)
2219                         return err;
2220         }
2221         return 0;
2222 }
2223
2224 static void f71882fg_remove_sysfs_files(struct platform_device *pdev,
2225         struct sensor_device_attribute_2 *attr, int count)
2226 {
2227         int i;
2228
2229         for (i = 0; i < count; i++)
2230                 device_remove_file(&pdev->dev, &attr[i].dev_attr);
2231 }
2232
2233 static int f71882fg_create_fan_sysfs_files(
2234         struct platform_device *pdev, int idx)
2235 {
2236         struct f71882fg_data *data = platform_get_drvdata(pdev);
2237         int err;
2238
2239         /* Sanity check the pwm setting */
2240         err = 0;
2241         switch (data->type) {
2242         case f71858fg:
2243                 if (((data->pwm_enable >> (idx * 2)) & 3) == 3)
2244                         err = 1;
2245                 break;
2246         case f71862fg:
2247                 if (((data->pwm_enable >> (idx * 2)) & 1) != 1)
2248                         err = 1;
2249                 break;
2250         case f8000:
2251                 if (idx == 2)
2252                         err = data->pwm_enable & 0x20;
2253                 break;
2254         default:
2255                 break;
2256         }
2257         if (err) {
2258                 dev_err(&pdev->dev,
2259                         "Invalid (reserved) pwm settings: 0x%02x, "
2260                         "skipping fan %d\n",
2261                         (data->pwm_enable >> (idx * 2)) & 3, idx + 1);
2262                 return 0; /* This is a non fatal condition */
2263         }
2264
2265         err = f71882fg_create_sysfs_files(pdev, &fxxxx_fan_attr[idx][0],
2266                                           ARRAY_SIZE(fxxxx_fan_attr[0]));
2267         if (err)
2268                 return err;
2269
2270         if (f71882fg_fan_has_beep[data->type]) {
2271                 err = f71882fg_create_sysfs_files(pdev,
2272                                                   &fxxxx_fan_beep_attr[idx],
2273                                                   1);
2274                 if (err)
2275                         return err;
2276         }
2277
2278         dev_info(&pdev->dev, "Fan: %d is in %s mode\n", idx + 1,
2279                  (data->pwm_enable & (1 << (2 * idx))) ? "duty-cycle" : "RPM");
2280
2281         /* Check for unsupported auto pwm settings */
2282         switch (data->type) {
2283         case f71808e:
2284         case f71808a:
2285         case f71869:
2286         case f71869a:
2287         case f71889fg:
2288         case f71889ed:
2289         case f71889a:
2290                 data->pwm_auto_point_mapping[idx] =
2291                         f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(idx));
2292                 if ((data->pwm_auto_point_mapping[idx] & 0x80) ||
2293                     (data->pwm_auto_point_mapping[idx] & 3) == 0) {
2294                         dev_warn(&pdev->dev,
2295                                  "Auto pwm controlled by raw digital "
2296                                  "data, disabling pwm auto_point "
2297                                  "sysfs attributes for fan %d\n", idx + 1);
2298                         return 0; /* This is a non fatal condition */
2299                 }
2300                 break;
2301         default:
2302                 break;
2303         }
2304
2305         switch (data->type) {
2306         case f71862fg:
2307                 err = f71882fg_create_sysfs_files(pdev,
2308                                         &f71862fg_auto_pwm_attr[idx][0],
2309                                         ARRAY_SIZE(f71862fg_auto_pwm_attr[0]));
2310                 break;
2311         case f71808e:
2312         case f71869:
2313                 err = f71882fg_create_sysfs_files(pdev,
2314                                         &f71869_auto_pwm_attr[idx][0],
2315                                         ARRAY_SIZE(f71869_auto_pwm_attr[0]));
2316                 break;
2317         case f8000:
2318                 err = f71882fg_create_sysfs_files(pdev,
2319                                         &f8000_auto_pwm_attr[idx][0],
2320                                         ARRAY_SIZE(f8000_auto_pwm_attr[0]));
2321                 break;
2322         default:
2323                 err = f71882fg_create_sysfs_files(pdev,
2324                                         &fxxxx_auto_pwm_attr[idx][0],
2325                                         ARRAY_SIZE(fxxxx_auto_pwm_attr[0]));
2326         }
2327
2328         return err;
2329 }
2330
2331 static int f71882fg_probe(struct platform_device *pdev)
2332 {
2333         struct f71882fg_data *data;
2334         struct f71882fg_sio_data *sio_data = dev_get_platdata(&pdev->dev);
2335         int nr_fans = f71882fg_nr_fans[sio_data->type];
2336         int nr_temps = f71882fg_nr_temps[sio_data->type];
2337         int err, i;
2338         int size;
2339         u8 start_reg, reg;
2340
2341         data = devm_kzalloc(&pdev->dev, sizeof(struct f71882fg_data),
2342                             GFP_KERNEL);
2343         if (!data)
2344                 return -ENOMEM;
2345
2346         data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
2347         data->type = sio_data->type;
2348         data->temp_start =
2349             (data->type == f71858fg || data->type == f8000 ||
2350                 data->type == f81866a) ? 0 : 1;
2351         mutex_init(&data->update_lock);
2352         platform_set_drvdata(pdev, data);
2353
2354         start_reg = f71882fg_read8(data, F71882FG_REG_START);
2355         if (start_reg & 0x04) {
2356                 dev_warn(&pdev->dev, "Hardware monitor is powered down\n");
2357                 return -ENODEV;
2358         }
2359         if (!(start_reg & 0x03)) {
2360                 dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
2361                 return -ENODEV;
2362         }
2363
2364         /* Register sysfs interface files */
2365         err = device_create_file(&pdev->dev, &dev_attr_name);
2366         if (err)
2367                 goto exit_unregister_sysfs;
2368
2369         if (start_reg & 0x01) {
2370                 switch (data->type) {
2371                 case f71858fg:
2372                         data->temp_config =
2373                                 f71882fg_read8(data, F71882FG_REG_TEMP_CONFIG);
2374                         if (data->temp_config & 0x10)
2375                                 /*
2376                                  * The f71858fg temperature alarms behave as
2377                                  * the f8000 alarms in this mode
2378                                  */
2379                                 err = f71882fg_create_sysfs_files(pdev,
2380                                         f8000_temp_attr,
2381                                         ARRAY_SIZE(f8000_temp_attr));
2382                         else
2383                                 err = f71882fg_create_sysfs_files(pdev,
2384                                         f71858fg_temp_attr,
2385                                         ARRAY_SIZE(f71858fg_temp_attr));
2386                         break;
2387                 case f8000:
2388                         err = f71882fg_create_sysfs_files(pdev,
2389                                         f8000_temp_attr,
2390                                         ARRAY_SIZE(f8000_temp_attr));
2391                         break;
2392                 case f81866a:
2393                         err = f71882fg_create_sysfs_files(pdev,
2394                                         f71858fg_temp_attr,
2395                                         ARRAY_SIZE(f71858fg_temp_attr));
2396                         break;
2397                 default:
2398                         err = f71882fg_create_sysfs_files(pdev,
2399                                 &fxxxx_temp_attr[0][0],
2400                                 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
2401                 }
2402                 if (err)
2403                         goto exit_unregister_sysfs;
2404
2405                 if (f71882fg_temp_has_beep[data->type]) {
2406                         if (data->type == f81866a) {
2407                                 size = ARRAY_SIZE(f81866_temp_beep_attr[0]);
2408                                 err = f71882fg_create_sysfs_files(pdev,
2409                                                 &f81866_temp_beep_attr[0][0],
2410                                                 size * nr_temps);
2411
2412                         } else {
2413                                 size = ARRAY_SIZE(fxxxx_temp_beep_attr[0]);
2414                                 err = f71882fg_create_sysfs_files(pdev,
2415                                                 &fxxxx_temp_beep_attr[0][0],
2416                                                 size * nr_temps);
2417                         }
2418                         if (err)
2419                                 goto exit_unregister_sysfs;
2420                 }
2421
2422                 for (i = 0; i < F71882FG_MAX_INS; i++) {
2423                         if (f71882fg_has_in[data->type][i]) {
2424                                 err = device_create_file(&pdev->dev,
2425                                                 &fxxxx_in_attr[i].dev_attr);
2426                                 if (err)
2427                                         goto exit_unregister_sysfs;
2428                         }
2429                 }
2430                 if (f71882fg_has_in1_alarm[data->type]) {
2431                         err = f71882fg_create_sysfs_files(pdev,
2432                                         fxxxx_in1_alarm_attr,
2433                                         ARRAY_SIZE(fxxxx_in1_alarm_attr));
2434                         if (err)
2435                                 goto exit_unregister_sysfs;
2436                 }
2437         }
2438
2439         if (start_reg & 0x02) {
2440                 switch (data->type) {
2441                 case f71808e:
2442                 case f71808a:
2443                 case f71869:
2444                 case f71869a:
2445                         /* These always have signed auto point temps */
2446                         data->auto_point_temp_signed = 1;
2447                         fallthrough;    /* to select correct fan/pwm reg bank! */
2448                 case f71889fg:
2449                 case f71889ed:
2450                 case f71889a:
2451                         reg = f71882fg_read8(data, F71882FG_REG_FAN_FAULT_T);
2452                         if (reg & F71882FG_FAN_NEG_TEMP_EN)
2453                                 data->auto_point_temp_signed = 1;
2454                         /* Ensure banked pwm registers point to right bank */
2455                         reg &= ~F71882FG_FAN_PROG_SEL;
2456                         f71882fg_write8(data, F71882FG_REG_FAN_FAULT_T, reg);
2457                         break;
2458                 default:
2459                         break;
2460                 }
2461
2462                 data->pwm_enable =
2463                         f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
2464
2465                 for (i = 0; i < nr_fans; i++) {
2466                         err = f71882fg_create_fan_sysfs_files(pdev, i);
2467                         if (err)
2468                                 goto exit_unregister_sysfs;
2469                 }
2470
2471                 /* Some types have 1 extra fan with limited functionality */
2472                 switch (data->type) {
2473                 case f71808a:
2474                         err = f71882fg_create_sysfs_files(pdev,
2475                                         f71808a_fan3_attr,
2476                                         ARRAY_SIZE(f71808a_fan3_attr));
2477                         break;
2478                 case f8000:
2479                         err = f71882fg_create_sysfs_files(pdev,
2480                                         f8000_fan_attr,
2481                                         ARRAY_SIZE(f8000_fan_attr));
2482                         break;
2483                 default:
2484                         break;
2485                 }
2486                 if (err)
2487                         goto exit_unregister_sysfs;
2488         }
2489
2490         data->hwmon_dev = hwmon_device_register(&pdev->dev);
2491         if (IS_ERR(data->hwmon_dev)) {
2492                 err = PTR_ERR(data->hwmon_dev);
2493                 data->hwmon_dev = NULL;
2494                 goto exit_unregister_sysfs;
2495         }
2496
2497         return 0;
2498
2499 exit_unregister_sysfs:
2500         f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
2501         return err; /* f71882fg_remove() also frees our data */
2502 }
2503
2504 static int f71882fg_remove(struct platform_device *pdev)
2505 {
2506         struct f71882fg_data *data = platform_get_drvdata(pdev);
2507         int nr_fans = f71882fg_nr_fans[data->type];
2508         int nr_temps = f71882fg_nr_temps[data->type];
2509         int i;
2510         u8 start_reg = f71882fg_read8(data, F71882FG_REG_START);
2511
2512         if (data->hwmon_dev)
2513                 hwmon_device_unregister(data->hwmon_dev);
2514
2515         device_remove_file(&pdev->dev, &dev_attr_name);
2516
2517         if (start_reg & 0x01) {
2518                 switch (data->type) {
2519                 case f71858fg:
2520                         if (data->temp_config & 0x10)
2521                                 f71882fg_remove_sysfs_files(pdev,
2522                                         f8000_temp_attr,
2523                                         ARRAY_SIZE(f8000_temp_attr));
2524                         else
2525                                 f71882fg_remove_sysfs_files(pdev,
2526                                         f71858fg_temp_attr,
2527                                         ARRAY_SIZE(f71858fg_temp_attr));
2528                         break;
2529                 case f8000:
2530                         f71882fg_remove_sysfs_files(pdev,
2531                                         f8000_temp_attr,
2532                                         ARRAY_SIZE(f8000_temp_attr));
2533                         break;
2534                 case f81866a:
2535                         f71882fg_remove_sysfs_files(pdev,
2536                                         f71858fg_temp_attr,
2537                                         ARRAY_SIZE(f71858fg_temp_attr));
2538                         break;
2539                 default:
2540                         f71882fg_remove_sysfs_files(pdev,
2541                                 &fxxxx_temp_attr[0][0],
2542                                 ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
2543                 }
2544                 if (f71882fg_temp_has_beep[data->type]) {
2545                         if (data->type == f81866a)
2546                                 f71882fg_remove_sysfs_files(pdev,
2547                                         &f81866_temp_beep_attr[0][0],
2548                                         ARRAY_SIZE(f81866_temp_beep_attr[0])
2549                                                 * nr_temps);
2550                         else
2551                                 f71882fg_remove_sysfs_files(pdev,
2552                                         &fxxxx_temp_beep_attr[0][0],
2553                                         ARRAY_SIZE(fxxxx_temp_beep_attr[0])
2554                                                 * nr_temps);
2555                 }
2556
2557                 for (i = 0; i < F71882FG_MAX_INS; i++) {
2558                         if (f71882fg_has_in[data->type][i]) {
2559                                 device_remove_file(&pdev->dev,
2560                                                 &fxxxx_in_attr[i].dev_attr);
2561                         }
2562                 }
2563                 if (f71882fg_has_in1_alarm[data->type]) {
2564                         f71882fg_remove_sysfs_files(pdev,
2565                                         fxxxx_in1_alarm_attr,
2566                                         ARRAY_SIZE(fxxxx_in1_alarm_attr));
2567                 }
2568         }
2569
2570         if (start_reg & 0x02) {
2571                 f71882fg_remove_sysfs_files(pdev, &fxxxx_fan_attr[0][0],
2572                                 ARRAY_SIZE(fxxxx_fan_attr[0]) * nr_fans);
2573
2574                 if (f71882fg_fan_has_beep[data->type]) {
2575                         f71882fg_remove_sysfs_files(pdev,
2576                                         fxxxx_fan_beep_attr, nr_fans);
2577                 }
2578
2579                 switch (data->type) {
2580                 case f71808a:
2581                         f71882fg_remove_sysfs_files(pdev,
2582                                 &fxxxx_auto_pwm_attr[0][0],
2583                                 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
2584                         f71882fg_remove_sysfs_files(pdev,
2585                                         f71808a_fan3_attr,
2586                                         ARRAY_SIZE(f71808a_fan3_attr));
2587                         break;
2588                 case f71862fg:
2589                         f71882fg_remove_sysfs_files(pdev,
2590                                 &f71862fg_auto_pwm_attr[0][0],
2591                                 ARRAY_SIZE(f71862fg_auto_pwm_attr[0]) *
2592                                         nr_fans);
2593                         break;
2594                 case f71808e:
2595                 case f71869:
2596                         f71882fg_remove_sysfs_files(pdev,
2597                                 &f71869_auto_pwm_attr[0][0],
2598                                 ARRAY_SIZE(f71869_auto_pwm_attr[0]) * nr_fans);
2599                         break;
2600                 case f8000:
2601                         f71882fg_remove_sysfs_files(pdev,
2602                                         f8000_fan_attr,
2603                                         ARRAY_SIZE(f8000_fan_attr));
2604                         f71882fg_remove_sysfs_files(pdev,
2605                                 &f8000_auto_pwm_attr[0][0],
2606                                 ARRAY_SIZE(f8000_auto_pwm_attr[0]) * nr_fans);
2607                         break;
2608                 default:
2609                         f71882fg_remove_sysfs_files(pdev,
2610                                 &fxxxx_auto_pwm_attr[0][0],
2611                                 ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
2612                 }
2613         }
2614         return 0;
2615 }
2616
2617 static int __init f71882fg_find(int sioaddr, struct f71882fg_sio_data *sio_data)
2618 {
2619         u16 devid;
2620         unsigned short address;
2621         int err = superio_enter(sioaddr);
2622         if (err)
2623                 return err;
2624
2625         devid = superio_inw(sioaddr, SIO_REG_MANID);
2626         if (devid != SIO_FINTEK_ID) {
2627                 pr_debug("Not a Fintek device\n");
2628                 err = -ENODEV;
2629                 goto exit;
2630         }
2631
2632         devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
2633         switch (devid) {
2634         case SIO_F71808E_ID:
2635                 sio_data->type = f71808e;
2636                 break;
2637         case SIO_F71808A_ID:
2638                 sio_data->type = f71808a;
2639                 break;
2640         case SIO_F71858_ID:
2641                 sio_data->type = f71858fg;
2642                 break;
2643         case SIO_F71862_ID:
2644                 sio_data->type = f71862fg;
2645                 break;
2646         case SIO_F71868_ID:
2647                 sio_data->type = f71868a;
2648                 break;
2649         case SIO_F71869_ID:
2650                 sio_data->type = f71869;
2651                 break;
2652         case SIO_F71869A_ID:
2653                 sio_data->type = f71869a;
2654                 break;
2655         case SIO_F71882_ID:
2656                 sio_data->type = f71882fg;
2657                 break;
2658         case SIO_F71889_ID:
2659                 sio_data->type = f71889fg;
2660                 break;
2661         case SIO_F71889E_ID:
2662                 sio_data->type = f71889ed;
2663                 break;
2664         case SIO_F71889A_ID:
2665                 sio_data->type = f71889a;
2666                 break;
2667         case SIO_F8000_ID:
2668                 sio_data->type = f8000;
2669                 break;
2670         case SIO_F81768D_ID:
2671                 sio_data->type = f81768d;
2672                 break;
2673         case SIO_F81865_ID:
2674                 sio_data->type = f81865f;
2675                 break;
2676         case SIO_F81866_ID:
2677         case SIO_F81966_ID:
2678                 sio_data->type = f81866a;
2679                 break;
2680         default:
2681                 pr_info("Unsupported Fintek device: %04x\n",
2682                         (unsigned int)devid);
2683                 err = -ENODEV;
2684                 goto exit;
2685         }
2686
2687         if (sio_data->type == f71858fg)
2688                 superio_select(sioaddr, SIO_F71858FG_LD_HWM);
2689         else
2690                 superio_select(sioaddr, SIO_F71882FG_LD_HWM);
2691
2692         if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
2693                 pr_warn("Device not activated\n");
2694                 err = -ENODEV;
2695                 goto exit;
2696         }
2697
2698         address = superio_inw(sioaddr, SIO_REG_ADDR);
2699         if (address == 0) {
2700                 pr_warn("Base address not set\n");
2701                 err = -ENODEV;
2702                 goto exit;
2703         }
2704         address &= ~(REGION_LENGTH - 1);        /* Ignore 3 LSB */
2705
2706         err = address;
2707         pr_info("Found %s chip at %#x, revision %d\n",
2708                 f71882fg_names[sio_data->type], (unsigned int)address,
2709                 (int)superio_inb(sioaddr, SIO_REG_DEVREV));
2710 exit:
2711         superio_exit(sioaddr);
2712         return err;
2713 }
2714
2715 static int __init f71882fg_device_add(int address,
2716                                       const struct f71882fg_sio_data *sio_data)
2717 {
2718         struct resource res = {
2719                 .start  = address,
2720                 .end    = address + REGION_LENGTH - 1,
2721                 .flags  = IORESOURCE_IO,
2722         };
2723         int err;
2724
2725         f71882fg_pdev = platform_device_alloc(DRVNAME, address);
2726         if (!f71882fg_pdev)
2727                 return -ENOMEM;
2728
2729         res.name = f71882fg_pdev->name;
2730         err = acpi_check_resource_conflict(&res);
2731         if (err)
2732                 goto exit_device_put;
2733
2734         err = platform_device_add_resources(f71882fg_pdev, &res, 1);
2735         if (err) {
2736                 pr_err("Device resource addition failed\n");
2737                 goto exit_device_put;
2738         }
2739
2740         err = platform_device_add_data(f71882fg_pdev, sio_data,
2741                                        sizeof(struct f71882fg_sio_data));
2742         if (err) {
2743                 pr_err("Platform data allocation failed\n");
2744                 goto exit_device_put;
2745         }
2746
2747         err = platform_device_add(f71882fg_pdev);
2748         if (err) {
2749                 pr_err("Device addition failed\n");
2750                 goto exit_device_put;
2751         }
2752
2753         return 0;
2754
2755 exit_device_put:
2756         platform_device_put(f71882fg_pdev);
2757
2758         return err;
2759 }
2760
2761 static int __init f71882fg_init(void)
2762 {
2763         int err;
2764         int address;
2765         struct f71882fg_sio_data sio_data;
2766
2767         memset(&sio_data, 0, sizeof(sio_data));
2768
2769         address = f71882fg_find(0x2e, &sio_data);
2770         if (address < 0)
2771                 address = f71882fg_find(0x4e, &sio_data);
2772         if (address < 0)
2773                 return address;
2774
2775         err = platform_driver_register(&f71882fg_driver);
2776         if (err)
2777                 return err;
2778
2779         err = f71882fg_device_add(address, &sio_data);
2780         if (err)
2781                 goto exit_driver;
2782
2783         return 0;
2784
2785 exit_driver:
2786         platform_driver_unregister(&f71882fg_driver);
2787         return err;
2788 }
2789
2790 static void __exit f71882fg_exit(void)
2791 {
2792         platform_device_unregister(f71882fg_pdev);
2793         platform_driver_unregister(&f71882fg_driver);
2794 }
2795
2796 MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
2797 MODULE_AUTHOR("Hans Edgington, Hans de Goede <hdegoede@redhat.com>");
2798 MODULE_LICENSE("GPL");
2799
2800 module_init(f71882fg_init);
2801 module_exit(f71882fg_exit);