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