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