xref: /linux/drivers/hwmon/adt7462.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
1 /*
2  * A hwmon driver for the Analog Devices ADT7462
3  * Copyright (C) 2008 IBM
4  *
5  * Author: Darrick J. Wong <darrick.wong@oracle.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 #include <linux/module.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/log2.h>
30 #include <linux/slab.h>
31 
32 /* Addresses to scan */
33 static const unsigned short normal_i2c[] = { 0x58, 0x5C, I2C_CLIENT_END };
34 
35 /* ADT7462 registers */
36 #define ADT7462_REG_DEVICE			0x3D
37 #define ADT7462_REG_VENDOR			0x3E
38 #define ADT7462_REG_REVISION			0x3F
39 
40 #define ADT7462_REG_MIN_TEMP_BASE_ADDR		0x44
41 #define ADT7462_REG_MIN_TEMP_MAX_ADDR		0x47
42 #define ADT7462_REG_MAX_TEMP_BASE_ADDR		0x48
43 #define ADT7462_REG_MAX_TEMP_MAX_ADDR		0x4B
44 #define ADT7462_REG_TEMP_BASE_ADDR		0x88
45 #define ADT7462_REG_TEMP_MAX_ADDR		0x8F
46 
47 #define ADT7462_REG_FAN_BASE_ADDR		0x98
48 #define ADT7462_REG_FAN_MAX_ADDR		0x9F
49 #define ADT7462_REG_FAN2_BASE_ADDR		0xA2
50 #define ADT7462_REG_FAN2_MAX_ADDR		0xA9
51 #define ADT7462_REG_FAN_ENABLE			0x07
52 #define ADT7462_REG_FAN_MIN_BASE_ADDR		0x78
53 #define ADT7462_REG_FAN_MIN_MAX_ADDR		0x7F
54 
55 #define ADT7462_REG_CFG2			0x02
56 #define		ADT7462_FSPD_MASK		0x20
57 
58 #define ADT7462_REG_PWM_BASE_ADDR		0xAA
59 #define ADT7462_REG_PWM_MAX_ADDR		0xAD
60 #define	ADT7462_REG_PWM_MIN_BASE_ADDR		0x28
61 #define ADT7462_REG_PWM_MIN_MAX_ADDR		0x2B
62 #define ADT7462_REG_PWM_MAX			0x2C
63 #define ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR	0x5C
64 #define ADT7462_REG_PWM_TEMP_MIN_MAX_ADDR	0x5F
65 #define ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR	0x60
66 #define ADT7462_REG_PWM_TEMP_RANGE_MAX_ADDR	0x63
67 #define	ADT7462_PWM_HYST_MASK			0x0F
68 #define	ADT7462_PWM_RANGE_MASK			0xF0
69 #define		ADT7462_PWM_RANGE_SHIFT		4
70 #define ADT7462_REG_PWM_CFG_BASE_ADDR		0x21
71 #define ADT7462_REG_PWM_CFG_MAX_ADDR		0x24
72 #define		ADT7462_PWM_CHANNEL_MASK	0xE0
73 #define		ADT7462_PWM_CHANNEL_SHIFT	5
74 
75 #define ADT7462_REG_PIN_CFG_BASE_ADDR		0x10
76 #define ADT7462_REG_PIN_CFG_MAX_ADDR		0x13
77 #define		ADT7462_PIN7_INPUT		0x01	/* cfg0 */
78 #define		ADT7462_DIODE3_INPUT		0x20
79 #define		ADT7462_DIODE1_INPUT		0x40
80 #define		ADT7462_VID_INPUT		0x80
81 #define		ADT7462_PIN22_INPUT		0x04	/* cfg1 */
82 #define		ADT7462_PIN21_INPUT		0x08
83 #define		ADT7462_PIN19_INPUT		0x10
84 #define		ADT7462_PIN15_INPUT		0x20
85 #define		ADT7462_PIN13_INPUT		0x40
86 #define		ADT7462_PIN8_INPUT		0x80
87 #define		ADT7462_PIN23_MASK		0x03
88 #define		ADT7462_PIN23_SHIFT		0
89 #define		ADT7462_PIN26_MASK		0x0C	/* cfg2 */
90 #define		ADT7462_PIN26_SHIFT		2
91 #define		ADT7462_PIN25_MASK		0x30
92 #define		ADT7462_PIN25_SHIFT		4
93 #define		ADT7462_PIN24_MASK		0xC0
94 #define		ADT7462_PIN24_SHIFT		6
95 #define		ADT7462_PIN26_VOLT_INPUT	0x08
96 #define		ADT7462_PIN25_VOLT_INPUT	0x20
97 #define		ADT7462_PIN28_SHIFT		4	/* cfg3 */
98 #define		ADT7462_PIN28_VOLT		0x5
99 
100 #define ADT7462_REG_ALARM1			0xB8
101 #define	ADT7462_LT_ALARM			0x02
102 #define		ADT7462_R1T_ALARM		0x04
103 #define		ADT7462_R2T_ALARM		0x08
104 #define		ADT7462_R3T_ALARM		0x10
105 #define ADT7462_REG_ALARM2			0xBB
106 #define		ADT7462_V0_ALARM		0x01
107 #define		ADT7462_V1_ALARM		0x02
108 #define		ADT7462_V2_ALARM		0x04
109 #define		ADT7462_V3_ALARM		0x08
110 #define		ADT7462_V4_ALARM		0x10
111 #define		ADT7462_V5_ALARM		0x20
112 #define		ADT7462_V6_ALARM		0x40
113 #define		ADT7462_V7_ALARM		0x80
114 #define ADT7462_REG_ALARM3			0xBC
115 #define		ADT7462_V8_ALARM		0x08
116 #define		ADT7462_V9_ALARM		0x10
117 #define		ADT7462_V10_ALARM		0x20
118 #define		ADT7462_V11_ALARM		0x40
119 #define		ADT7462_V12_ALARM		0x80
120 #define ADT7462_REG_ALARM4			0xBD
121 #define		ADT7462_F0_ALARM		0x01
122 #define		ADT7462_F1_ALARM		0x02
123 #define		ADT7462_F2_ALARM		0x04
124 #define		ADT7462_F3_ALARM		0x08
125 #define		ADT7462_F4_ALARM		0x10
126 #define		ADT7462_F5_ALARM		0x20
127 #define		ADT7462_F6_ALARM		0x40
128 #define		ADT7462_F7_ALARM		0x80
129 #define ADT7462_ALARM1				0x0000
130 #define ADT7462_ALARM2				0x0100
131 #define ADT7462_ALARM3				0x0200
132 #define ADT7462_ALARM4				0x0300
133 #define ADT7462_ALARM_REG_SHIFT			8
134 #define ADT7462_ALARM_FLAG_MASK			0x0F
135 
136 #define ADT7462_TEMP_COUNT		4
137 #define ADT7462_TEMP_REG(x)		(ADT7462_REG_TEMP_BASE_ADDR + ((x) * 2))
138 #define ADT7462_TEMP_MIN_REG(x)		(ADT7462_REG_MIN_TEMP_BASE_ADDR + (x))
139 #define ADT7462_TEMP_MAX_REG(x)		(ADT7462_REG_MAX_TEMP_BASE_ADDR + (x))
140 #define TEMP_FRAC_OFFSET		6
141 
142 #define ADT7462_FAN_COUNT		8
143 #define ADT7462_REG_FAN_MIN(x)		(ADT7462_REG_FAN_MIN_BASE_ADDR + (x))
144 
145 #define ADT7462_PWM_COUNT		4
146 #define ADT7462_REG_PWM(x)		(ADT7462_REG_PWM_BASE_ADDR + (x))
147 #define ADT7462_REG_PWM_MIN(x)		(ADT7462_REG_PWM_MIN_BASE_ADDR + (x))
148 #define ADT7462_REG_PWM_TMIN(x)		\
149 	(ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
150 #define ADT7462_REG_PWM_TRANGE(x)	\
151 	(ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR + (x))
152 
153 #define ADT7462_PIN_CFG_REG_COUNT	4
154 #define ADT7462_REG_PIN_CFG(x)		(ADT7462_REG_PIN_CFG_BASE_ADDR + (x))
155 #define ADT7462_REG_PWM_CFG(x)		(ADT7462_REG_PWM_CFG_BASE_ADDR + (x))
156 
157 #define ADT7462_ALARM_REG_COUNT		4
158 
159 /*
160  * The chip can measure 13 different voltage sources:
161  *
162  * 1. +12V1 (pin 7)
163  * 2. Vccp1/+2.5V/+1.8V/+1.5V (pin 23)
164  * 3. +12V3 (pin 22)
165  * 4. +5V (pin 21)
166  * 5. +1.25V/+0.9V (pin 19)
167  * 6. +2.5V/+1.8V (pin 15)
168  * 7. +3.3v (pin 13)
169  * 8. +12V2 (pin 8)
170  * 9. Vbatt/FSB_Vtt (pin 26)
171  * A. +3.3V/+1.2V1 (pin 25)
172  * B. Vccp2/+2.5V/+1.8V/+1.5V (pin 24)
173  * C. +1.5V ICH (only if BOTH pin 28/29 are set to +1.5V)
174  * D. +1.5V 3GPIO (only if BOTH pin 28/29 are set to +1.5V)
175  *
176  * Each of these 13 has a factor to convert raw to voltage.  Even better,
177  * the pins can be connected to other sensors (tach/gpio/hot/etc), which
178  * makes the bookkeeping tricky.
179  *
180  * Some, but not all, of these voltages have low/high limits.
181  */
182 #define ADT7462_VOLT_COUNT	13
183 
184 #define ADT7462_VENDOR		0x41
185 #define ADT7462_DEVICE		0x62
186 /* datasheet only mentions a revision 4 */
187 #define ADT7462_REVISION	0x04
188 
189 /* How often do we reread sensors values? (In jiffies) */
190 #define SENSOR_REFRESH_INTERVAL	(2 * HZ)
191 
192 /* How often do we reread sensor limit values? (In jiffies) */
193 #define LIMIT_REFRESH_INTERVAL	(60 * HZ)
194 
195 /* datasheet says to divide this number by the fan reading to get fan rpm */
196 #define FAN_PERIOD_TO_RPM(x)	((90000 * 60) / (x))
197 #define FAN_RPM_TO_PERIOD	FAN_PERIOD_TO_RPM
198 #define FAN_PERIOD_INVALID	65535
199 #define FAN_DATA_VALID(x)	((x) && (x) != FAN_PERIOD_INVALID)
200 
201 #define MASK_AND_SHIFT(value, prefix)	\
202 	(((value) & prefix##_MASK) >> prefix##_SHIFT)
203 
204 struct adt7462_data {
205 	struct i2c_client	*client;
206 	struct mutex		lock;
207 	char			sensors_valid;
208 	char			limits_valid;
209 	unsigned long		sensors_last_updated;	/* In jiffies */
210 	unsigned long		limits_last_updated;	/* In jiffies */
211 
212 	u8			temp[ADT7462_TEMP_COUNT];
213 				/* bits 6-7 are quarter pieces of temp */
214 	u8			temp_frac[ADT7462_TEMP_COUNT];
215 	u8			temp_min[ADT7462_TEMP_COUNT];
216 	u8			temp_max[ADT7462_TEMP_COUNT];
217 	u16			fan[ADT7462_FAN_COUNT];
218 	u8			fan_enabled;
219 	u8			fan_min[ADT7462_FAN_COUNT];
220 	u8			cfg2;
221 	u8			pwm[ADT7462_PWM_COUNT];
222 	u8			pin_cfg[ADT7462_PIN_CFG_REG_COUNT];
223 	u8			voltages[ADT7462_VOLT_COUNT];
224 	u8			volt_max[ADT7462_VOLT_COUNT];
225 	u8			volt_min[ADT7462_VOLT_COUNT];
226 	u8			pwm_min[ADT7462_PWM_COUNT];
227 	u8			pwm_tmin[ADT7462_PWM_COUNT];
228 	u8			pwm_trange[ADT7462_PWM_COUNT];
229 	u8			pwm_max;	/* only one per chip */
230 	u8			pwm_cfg[ADT7462_PWM_COUNT];
231 	u8			alarms[ADT7462_ALARM_REG_COUNT];
232 };
233 
234 /*
235  * 16-bit registers on the ADT7462 are low-byte first.  The data sheet says
236  * that the low byte must be read before the high byte.
237  */
238 static inline int adt7462_read_word_data(struct i2c_client *client, u8 reg)
239 {
240 	u16 foo;
241 	foo = i2c_smbus_read_byte_data(client, reg);
242 	foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
243 	return foo;
244 }
245 
246 /* For some reason these registers are not contiguous. */
247 static int ADT7462_REG_FAN(int fan)
248 {
249 	if (fan < 4)
250 		return ADT7462_REG_FAN_BASE_ADDR + (2 * fan);
251 	return ADT7462_REG_FAN2_BASE_ADDR + (2 * (fan - 4));
252 }
253 
254 /* Voltage registers are scattered everywhere */
255 static int ADT7462_REG_VOLT_MAX(struct adt7462_data *data, int which)
256 {
257 	switch (which) {
258 	case 0:
259 		if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
260 			return 0x7C;
261 		break;
262 	case 1:
263 		return 0x69;
264 	case 2:
265 		if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
266 			return 0x7F;
267 		break;
268 	case 3:
269 		if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
270 			return 0x7E;
271 		break;
272 	case 4:
273 		if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
274 			return 0x4B;
275 		break;
276 	case 5:
277 		if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
278 			return 0x49;
279 		break;
280 	case 6:
281 		if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
282 			return 0x68;
283 		break;
284 	case 7:
285 		if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
286 			return 0x7D;
287 		break;
288 	case 8:
289 		if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
290 			return 0x6C;
291 		break;
292 	case 9:
293 		if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
294 			return 0x6B;
295 		break;
296 	case 10:
297 		return 0x6A;
298 	case 11:
299 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
300 					ADT7462_PIN28_VOLT &&
301 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
302 			return 0x50;
303 		break;
304 	case 12:
305 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
306 					ADT7462_PIN28_VOLT &&
307 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
308 			return 0x4C;
309 		break;
310 	}
311 	return 0;
312 }
313 
314 static int ADT7462_REG_VOLT_MIN(struct adt7462_data *data, int which)
315 {
316 	switch (which) {
317 	case 0:
318 		if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
319 			return 0x6D;
320 		break;
321 	case 1:
322 		return 0x72;
323 	case 2:
324 		if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
325 			return 0x6F;
326 		break;
327 	case 3:
328 		if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
329 			return 0x71;
330 		break;
331 	case 4:
332 		if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
333 			return 0x47;
334 		break;
335 	case 5:
336 		if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
337 			return 0x45;
338 		break;
339 	case 6:
340 		if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
341 			return 0x70;
342 		break;
343 	case 7:
344 		if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
345 			return 0x6E;
346 		break;
347 	case 8:
348 		if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
349 			return 0x75;
350 		break;
351 	case 9:
352 		if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
353 			return 0x74;
354 		break;
355 	case 10:
356 		return 0x73;
357 	case 11:
358 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
359 					ADT7462_PIN28_VOLT &&
360 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
361 			return 0x76;
362 		break;
363 	case 12:
364 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
365 					ADT7462_PIN28_VOLT &&
366 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
367 			return 0x77;
368 		break;
369 	}
370 	return 0;
371 }
372 
373 static int ADT7462_REG_VOLT(struct adt7462_data *data, int which)
374 {
375 	switch (which) {
376 	case 0:
377 		if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
378 			return 0xA3;
379 		break;
380 	case 1:
381 		return 0x90;
382 	case 2:
383 		if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
384 			return 0xA9;
385 		break;
386 	case 3:
387 		if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
388 			return 0xA7;
389 		break;
390 	case 4:
391 		if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
392 			return 0x8F;
393 		break;
394 	case 5:
395 		if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
396 			return 0x8B;
397 		break;
398 	case 6:
399 		if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
400 			return 0x96;
401 		break;
402 	case 7:
403 		if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
404 			return 0xA5;
405 		break;
406 	case 8:
407 		if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
408 			return 0x93;
409 		break;
410 	case 9:
411 		if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
412 			return 0x92;
413 		break;
414 	case 10:
415 		return 0x91;
416 	case 11:
417 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
418 					ADT7462_PIN28_VOLT &&
419 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
420 			return 0x94;
421 		break;
422 	case 12:
423 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
424 					ADT7462_PIN28_VOLT &&
425 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
426 			return 0x95;
427 		break;
428 	}
429 	return -ENODEV;
430 }
431 
432 /* Provide labels for sysfs */
433 static const char *voltage_label(struct adt7462_data *data, int which)
434 {
435 	switch (which) {
436 	case 0:
437 		if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
438 			return "+12V1";
439 		break;
440 	case 1:
441 		switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
442 		case 0:
443 			return "Vccp1";
444 		case 1:
445 			return "+2.5V";
446 		case 2:
447 			return "+1.8V";
448 		case 3:
449 			return "+1.5V";
450 		}
451 		/* fall through */
452 	case 2:
453 		if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
454 			return "+12V3";
455 		break;
456 	case 3:
457 		if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
458 			return "+5V";
459 		break;
460 	case 4:
461 		if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
462 			if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
463 				return "+0.9V";
464 			return "+1.25V";
465 		}
466 		break;
467 	case 5:
468 		if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
469 			if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
470 				return "+1.8V";
471 			return "+2.5V";
472 		}
473 		break;
474 	case 6:
475 		if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
476 			return "+3.3V";
477 		break;
478 	case 7:
479 		if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
480 			return "+12V2";
481 		break;
482 	case 8:
483 		switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
484 		case 0:
485 			return "Vbatt";
486 		case 1:
487 			return "FSB_Vtt";
488 		}
489 		break;
490 	case 9:
491 		switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
492 		case 0:
493 			return "+3.3V";
494 		case 1:
495 			return "+1.2V1";
496 		}
497 		break;
498 	case 10:
499 		switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
500 		case 0:
501 			return "Vccp2";
502 		case 1:
503 			return "+2.5V";
504 		case 2:
505 			return "+1.8V";
506 		case 3:
507 			return "+1.5";
508 		}
509 		/* fall through */
510 	case 11:
511 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
512 					ADT7462_PIN28_VOLT &&
513 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
514 			return "+1.5V ICH";
515 		break;
516 	case 12:
517 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
518 					ADT7462_PIN28_VOLT &&
519 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
520 			return "+1.5V 3GPIO";
521 		break;
522 	}
523 	return "N/A";
524 }
525 
526 /* Multipliers are actually in uV, not mV. */
527 static int voltage_multiplier(struct adt7462_data *data, int which)
528 {
529 	switch (which) {
530 	case 0:
531 		if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
532 			return 62500;
533 		break;
534 	case 1:
535 		switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
536 		case 0:
537 			if (data->pin_cfg[0] & ADT7462_VID_INPUT)
538 				return 12500;
539 			return 6250;
540 		case 1:
541 			return 13000;
542 		case 2:
543 			return 9400;
544 		case 3:
545 			return 7800;
546 		}
547 		/* fall through */
548 	case 2:
549 		if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
550 			return 62500;
551 		break;
552 	case 3:
553 		if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
554 			return 26000;
555 		break;
556 	case 4:
557 		if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
558 			if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
559 				return 4690;
560 			return 6500;
561 		}
562 		break;
563 	case 5:
564 		if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
565 			if (data->pin_cfg[1] & ADT7462_PIN15_INPUT)
566 				return 9400;
567 			return 13000;
568 		}
569 		break;
570 	case 6:
571 		if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
572 			return 17200;
573 		break;
574 	case 7:
575 		if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
576 			return 62500;
577 		break;
578 	case 8:
579 		switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
580 		case 0:
581 			return 15600;
582 		case 1:
583 			return 6250;
584 		}
585 		break;
586 	case 9:
587 		switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
588 		case 0:
589 			return 17200;
590 		case 1:
591 			return 6250;
592 		}
593 		break;
594 	case 10:
595 		switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
596 		case 0:
597 			return 6250;
598 		case 1:
599 			return 13000;
600 		case 2:
601 			return 9400;
602 		case 3:
603 			return 7800;
604 		}
605 		/* fall through */
606 	case 11:
607 	case 12:
608 		if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
609 					ADT7462_PIN28_VOLT &&
610 		    !(data->pin_cfg[0] & ADT7462_VID_INPUT))
611 			return 7800;
612 	}
613 	return 0;
614 }
615 
616 static int temp_enabled(struct adt7462_data *data, int which)
617 {
618 	switch (which) {
619 	case 0:
620 	case 2:
621 		return 1;
622 	case 1:
623 		if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
624 			return 1;
625 		break;
626 	case 3:
627 		if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
628 			return 1;
629 		break;
630 	}
631 	return 0;
632 }
633 
634 static const char *temp_label(struct adt7462_data *data, int which)
635 {
636 	switch (which) {
637 	case 0:
638 		return "local";
639 	case 1:
640 		if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
641 			return "remote1";
642 		break;
643 	case 2:
644 		return "remote2";
645 	case 3:
646 		if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
647 			return "remote3";
648 		break;
649 	}
650 	return "N/A";
651 }
652 
653 /* Map Trange register values to mC */
654 #define NUM_TRANGE_VALUES	16
655 static const int trange_values[NUM_TRANGE_VALUES] = {
656 	2000,
657 	2500,
658 	3300,
659 	4000,
660 	5000,
661 	6700,
662 	8000,
663 	10000,
664 	13300,
665 	16000,
666 	20000,
667 	26700,
668 	32000,
669 	40000,
670 	53300,
671 	80000
672 };
673 
674 static int find_trange_value(int trange)
675 {
676 	int i;
677 
678 	for (i = 0; i < NUM_TRANGE_VALUES; i++)
679 		if (trange_values[i] == trange)
680 			return i;
681 
682 	return -EINVAL;
683 }
684 
685 static struct adt7462_data *adt7462_update_device(struct device *dev)
686 {
687 	struct adt7462_data *data = dev_get_drvdata(dev);
688 	struct i2c_client *client = data->client;
689 	unsigned long local_jiffies = jiffies;
690 	int i;
691 
692 	mutex_lock(&data->lock);
693 	if (time_before(local_jiffies, data->sensors_last_updated +
694 		SENSOR_REFRESH_INTERVAL)
695 		&& data->sensors_valid)
696 		goto no_sensor_update;
697 
698 	for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
699 		/*
700 		 * Reading the fractional register locks the integral
701 		 * register until both have been read.
702 		 */
703 		data->temp_frac[i] = i2c_smbus_read_byte_data(client,
704 						ADT7462_TEMP_REG(i));
705 		data->temp[i] = i2c_smbus_read_byte_data(client,
706 						ADT7462_TEMP_REG(i) + 1);
707 	}
708 
709 	for (i = 0; i < ADT7462_FAN_COUNT; i++)
710 		data->fan[i] = adt7462_read_word_data(client,
711 						ADT7462_REG_FAN(i));
712 
713 	data->fan_enabled = i2c_smbus_read_byte_data(client,
714 					ADT7462_REG_FAN_ENABLE);
715 
716 	for (i = 0; i < ADT7462_PWM_COUNT; i++)
717 		data->pwm[i] = i2c_smbus_read_byte_data(client,
718 						ADT7462_REG_PWM(i));
719 
720 	for (i = 0; i < ADT7462_PIN_CFG_REG_COUNT; i++)
721 		data->pin_cfg[i] = i2c_smbus_read_byte_data(client,
722 				ADT7462_REG_PIN_CFG(i));
723 
724 	for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
725 		int reg = ADT7462_REG_VOLT(data, i);
726 		if (!reg)
727 			data->voltages[i] = 0;
728 		else
729 			data->voltages[i] = i2c_smbus_read_byte_data(client,
730 								     reg);
731 	}
732 
733 	data->alarms[0] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM1);
734 	data->alarms[1] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM2);
735 	data->alarms[2] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM3);
736 	data->alarms[3] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM4);
737 
738 	data->sensors_last_updated = local_jiffies;
739 	data->sensors_valid = 1;
740 
741 no_sensor_update:
742 	if (time_before(local_jiffies, data->limits_last_updated +
743 		LIMIT_REFRESH_INTERVAL)
744 		&& data->limits_valid)
745 		goto out;
746 
747 	for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
748 		data->temp_min[i] = i2c_smbus_read_byte_data(client,
749 						ADT7462_TEMP_MIN_REG(i));
750 		data->temp_max[i] = i2c_smbus_read_byte_data(client,
751 						ADT7462_TEMP_MAX_REG(i));
752 	}
753 
754 	for (i = 0; i < ADT7462_FAN_COUNT; i++)
755 		data->fan_min[i] = i2c_smbus_read_byte_data(client,
756 						ADT7462_REG_FAN_MIN(i));
757 
758 	for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
759 		int reg = ADT7462_REG_VOLT_MAX(data, i);
760 		data->volt_max[i] =
761 			(reg ? i2c_smbus_read_byte_data(client, reg) : 0);
762 
763 		reg = ADT7462_REG_VOLT_MIN(data, i);
764 		data->volt_min[i] =
765 			(reg ? i2c_smbus_read_byte_data(client, reg) : 0);
766 	}
767 
768 	for (i = 0; i < ADT7462_PWM_COUNT; i++) {
769 		data->pwm_min[i] = i2c_smbus_read_byte_data(client,
770 						ADT7462_REG_PWM_MIN(i));
771 		data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
772 						ADT7462_REG_PWM_TMIN(i));
773 		data->pwm_trange[i] = i2c_smbus_read_byte_data(client,
774 						ADT7462_REG_PWM_TRANGE(i));
775 		data->pwm_cfg[i] = i2c_smbus_read_byte_data(client,
776 						ADT7462_REG_PWM_CFG(i));
777 	}
778 
779 	data->pwm_max = i2c_smbus_read_byte_data(client, ADT7462_REG_PWM_MAX);
780 
781 	data->cfg2 = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
782 
783 	data->limits_last_updated = local_jiffies;
784 	data->limits_valid = 1;
785 
786 out:
787 	mutex_unlock(&data->lock);
788 	return data;
789 }
790 
791 static ssize_t temp_min_show(struct device *dev,
792 			     struct device_attribute *devattr, char *buf)
793 {
794 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
795 	struct adt7462_data *data = adt7462_update_device(dev);
796 
797 	if (!temp_enabled(data, attr->index))
798 		return sprintf(buf, "0\n");
799 
800 	return sprintf(buf, "%d\n", 1000 * (data->temp_min[attr->index] - 64));
801 }
802 
803 static ssize_t temp_min_store(struct device *dev,
804 			      struct device_attribute *devattr,
805 			      const char *buf, size_t count)
806 {
807 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
808 	struct adt7462_data *data = dev_get_drvdata(dev);
809 	struct i2c_client *client = data->client;
810 	long temp;
811 
812 	if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
813 		return -EINVAL;
814 
815 	temp = clamp_val(temp, -64000, 191000);
816 	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
817 
818 	mutex_lock(&data->lock);
819 	data->temp_min[attr->index] = temp;
820 	i2c_smbus_write_byte_data(client, ADT7462_TEMP_MIN_REG(attr->index),
821 				  temp);
822 	mutex_unlock(&data->lock);
823 
824 	return count;
825 }
826 
827 static ssize_t temp_max_show(struct device *dev,
828 			     struct device_attribute *devattr, char *buf)
829 {
830 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
831 	struct adt7462_data *data = adt7462_update_device(dev);
832 
833 	if (!temp_enabled(data, attr->index))
834 		return sprintf(buf, "0\n");
835 
836 	return sprintf(buf, "%d\n", 1000 * (data->temp_max[attr->index] - 64));
837 }
838 
839 static ssize_t temp_max_store(struct device *dev,
840 			      struct device_attribute *devattr,
841 			      const char *buf, size_t count)
842 {
843 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
844 	struct adt7462_data *data = dev_get_drvdata(dev);
845 	struct i2c_client *client = data->client;
846 	long temp;
847 
848 	if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
849 		return -EINVAL;
850 
851 	temp = clamp_val(temp, -64000, 191000);
852 	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
853 
854 	mutex_lock(&data->lock);
855 	data->temp_max[attr->index] = temp;
856 	i2c_smbus_write_byte_data(client, ADT7462_TEMP_MAX_REG(attr->index),
857 				  temp);
858 	mutex_unlock(&data->lock);
859 
860 	return count;
861 }
862 
863 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
864 			 char *buf)
865 {
866 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
867 	struct adt7462_data *data = adt7462_update_device(dev);
868 	u8 frac = data->temp_frac[attr->index] >> TEMP_FRAC_OFFSET;
869 
870 	if (!temp_enabled(data, attr->index))
871 		return sprintf(buf, "0\n");
872 
873 	return sprintf(buf, "%d\n", 1000 * (data->temp[attr->index] - 64) +
874 				     250 * frac);
875 }
876 
877 static ssize_t temp_label_show(struct device *dev,
878 			       struct device_attribute *devattr, char *buf)
879 {
880 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
881 	struct adt7462_data *data = adt7462_update_device(dev);
882 
883 	return sprintf(buf, "%s\n", temp_label(data, attr->index));
884 }
885 
886 static ssize_t volt_max_show(struct device *dev,
887 			     struct device_attribute *devattr, char *buf)
888 {
889 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
890 	struct adt7462_data *data = adt7462_update_device(dev);
891 	int x = voltage_multiplier(data, attr->index);
892 
893 	x *= data->volt_max[attr->index];
894 	x /= 1000; /* convert from uV to mV */
895 
896 	return sprintf(buf, "%d\n", x);
897 }
898 
899 static ssize_t volt_max_store(struct device *dev,
900 			      struct device_attribute *devattr,
901 			      const char *buf, size_t count)
902 {
903 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
904 	struct adt7462_data *data = dev_get_drvdata(dev);
905 	struct i2c_client *client = data->client;
906 	int x = voltage_multiplier(data, attr->index);
907 	long temp;
908 
909 	if (kstrtol(buf, 10, &temp) || !x)
910 		return -EINVAL;
911 
912 	temp = clamp_val(temp, 0, 255 * x / 1000);
913 	temp *= 1000; /* convert mV to uV */
914 	temp = DIV_ROUND_CLOSEST(temp, x);
915 
916 	mutex_lock(&data->lock);
917 	data->volt_max[attr->index] = temp;
918 	i2c_smbus_write_byte_data(client,
919 				  ADT7462_REG_VOLT_MAX(data, attr->index),
920 				  temp);
921 	mutex_unlock(&data->lock);
922 
923 	return count;
924 }
925 
926 static ssize_t volt_min_show(struct device *dev,
927 			     struct device_attribute *devattr, char *buf)
928 {
929 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
930 	struct adt7462_data *data = adt7462_update_device(dev);
931 	int x = voltage_multiplier(data, attr->index);
932 
933 	x *= data->volt_min[attr->index];
934 	x /= 1000; /* convert from uV to mV */
935 
936 	return sprintf(buf, "%d\n", x);
937 }
938 
939 static ssize_t volt_min_store(struct device *dev,
940 			      struct device_attribute *devattr,
941 			      const char *buf, size_t count)
942 {
943 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
944 	struct adt7462_data *data = dev_get_drvdata(dev);
945 	struct i2c_client *client = data->client;
946 	int x = voltage_multiplier(data, attr->index);
947 	long temp;
948 
949 	if (kstrtol(buf, 10, &temp) || !x)
950 		return -EINVAL;
951 
952 	temp = clamp_val(temp, 0, 255 * x / 1000);
953 	temp *= 1000; /* convert mV to uV */
954 	temp = DIV_ROUND_CLOSEST(temp, x);
955 
956 	mutex_lock(&data->lock);
957 	data->volt_min[attr->index] = temp;
958 	i2c_smbus_write_byte_data(client,
959 				  ADT7462_REG_VOLT_MIN(data, attr->index),
960 				  temp);
961 	mutex_unlock(&data->lock);
962 
963 	return count;
964 }
965 
966 static ssize_t voltage_show(struct device *dev,
967 			    struct device_attribute *devattr, char *buf)
968 {
969 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
970 	struct adt7462_data *data = adt7462_update_device(dev);
971 	int x = voltage_multiplier(data, attr->index);
972 
973 	x *= data->voltages[attr->index];
974 	x /= 1000; /* convert from uV to mV */
975 
976 	return sprintf(buf, "%d\n", x);
977 }
978 
979 static ssize_t voltage_label_show(struct device *dev,
980 				  struct device_attribute *devattr, char *buf)
981 {
982 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
983 	struct adt7462_data *data = adt7462_update_device(dev);
984 
985 	return sprintf(buf, "%s\n", voltage_label(data, attr->index));
986 }
987 
988 static ssize_t alarm_show(struct device *dev,
989 			  struct device_attribute *devattr, char *buf)
990 {
991 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
992 	struct adt7462_data *data = adt7462_update_device(dev);
993 	int reg = attr->index >> ADT7462_ALARM_REG_SHIFT;
994 	int mask = attr->index & ADT7462_ALARM_FLAG_MASK;
995 
996 	if (data->alarms[reg] & mask)
997 		return sprintf(buf, "1\n");
998 	else
999 		return sprintf(buf, "0\n");
1000 }
1001 
1002 static int fan_enabled(struct adt7462_data *data, int fan)
1003 {
1004 	return data->fan_enabled & (1 << fan);
1005 }
1006 
1007 static ssize_t fan_min_show(struct device *dev,
1008 			    struct device_attribute *devattr, char *buf)
1009 {
1010 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1011 	struct adt7462_data *data = adt7462_update_device(dev);
1012 	u16 temp;
1013 
1014 	/* Only the MSB of the min fan period is stored... */
1015 	temp = data->fan_min[attr->index];
1016 	temp <<= 8;
1017 
1018 	if (!fan_enabled(data, attr->index) ||
1019 	    !FAN_DATA_VALID(temp))
1020 		return sprintf(buf, "0\n");
1021 
1022 	return sprintf(buf, "%d\n", FAN_PERIOD_TO_RPM(temp));
1023 }
1024 
1025 static ssize_t fan_min_store(struct device *dev,
1026 			     struct device_attribute *devattr,
1027 			     const char *buf, size_t count)
1028 {
1029 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1030 	struct adt7462_data *data = dev_get_drvdata(dev);
1031 	struct i2c_client *client = data->client;
1032 	long temp;
1033 
1034 	if (kstrtol(buf, 10, &temp) || !temp ||
1035 	    !fan_enabled(data, attr->index))
1036 		return -EINVAL;
1037 
1038 	temp = FAN_RPM_TO_PERIOD(temp);
1039 	temp >>= 8;
1040 	temp = clamp_val(temp, 1, 255);
1041 
1042 	mutex_lock(&data->lock);
1043 	data->fan_min[attr->index] = temp;
1044 	i2c_smbus_write_byte_data(client, ADT7462_REG_FAN_MIN(attr->index),
1045 				  temp);
1046 	mutex_unlock(&data->lock);
1047 
1048 	return count;
1049 }
1050 
1051 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
1052 			char *buf)
1053 {
1054 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1055 	struct adt7462_data *data = adt7462_update_device(dev);
1056 
1057 	if (!fan_enabled(data, attr->index) ||
1058 	    !FAN_DATA_VALID(data->fan[attr->index]))
1059 		return sprintf(buf, "0\n");
1060 
1061 	return sprintf(buf, "%d\n",
1062 		       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
1063 }
1064 
1065 static ssize_t force_pwm_max_show(struct device *dev,
1066 				  struct device_attribute *devattr, char *buf)
1067 {
1068 	struct adt7462_data *data = adt7462_update_device(dev);
1069 	return sprintf(buf, "%d\n", (data->cfg2 & ADT7462_FSPD_MASK ? 1 : 0));
1070 }
1071 
1072 static ssize_t force_pwm_max_store(struct device *dev,
1073 				   struct device_attribute *devattr,
1074 				   const char *buf, size_t count)
1075 {
1076 	struct adt7462_data *data = dev_get_drvdata(dev);
1077 	struct i2c_client *client = data->client;
1078 	long temp;
1079 	u8 reg;
1080 
1081 	if (kstrtol(buf, 10, &temp))
1082 		return -EINVAL;
1083 
1084 	mutex_lock(&data->lock);
1085 	reg = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
1086 	if (temp)
1087 		reg |= ADT7462_FSPD_MASK;
1088 	else
1089 		reg &= ~ADT7462_FSPD_MASK;
1090 	data->cfg2 = reg;
1091 	i2c_smbus_write_byte_data(client, ADT7462_REG_CFG2, reg);
1092 	mutex_unlock(&data->lock);
1093 
1094 	return count;
1095 }
1096 
1097 static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
1098 			char *buf)
1099 {
1100 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1101 	struct adt7462_data *data = adt7462_update_device(dev);
1102 	return sprintf(buf, "%d\n", data->pwm[attr->index]);
1103 }
1104 
1105 static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
1106 			 const char *buf, size_t count)
1107 {
1108 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1109 	struct adt7462_data *data = dev_get_drvdata(dev);
1110 	struct i2c_client *client = data->client;
1111 	long temp;
1112 
1113 	if (kstrtol(buf, 10, &temp))
1114 		return -EINVAL;
1115 
1116 	temp = clamp_val(temp, 0, 255);
1117 
1118 	mutex_lock(&data->lock);
1119 	data->pwm[attr->index] = temp;
1120 	i2c_smbus_write_byte_data(client, ADT7462_REG_PWM(attr->index), temp);
1121 	mutex_unlock(&data->lock);
1122 
1123 	return count;
1124 }
1125 
1126 static ssize_t pwm_max_show(struct device *dev,
1127 			    struct device_attribute *devattr, char *buf)
1128 {
1129 	struct adt7462_data *data = adt7462_update_device(dev);
1130 	return sprintf(buf, "%d\n", data->pwm_max);
1131 }
1132 
1133 static ssize_t pwm_max_store(struct device *dev,
1134 			     struct device_attribute *devattr,
1135 			     const char *buf, size_t count)
1136 {
1137 	struct adt7462_data *data = dev_get_drvdata(dev);
1138 	struct i2c_client *client = data->client;
1139 	long temp;
1140 
1141 	if (kstrtol(buf, 10, &temp))
1142 		return -EINVAL;
1143 
1144 	temp = clamp_val(temp, 0, 255);
1145 
1146 	mutex_lock(&data->lock);
1147 	data->pwm_max = temp;
1148 	i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MAX, temp);
1149 	mutex_unlock(&data->lock);
1150 
1151 	return count;
1152 }
1153 
1154 static ssize_t pwm_min_show(struct device *dev,
1155 			    struct device_attribute *devattr, char *buf)
1156 {
1157 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1158 	struct adt7462_data *data = adt7462_update_device(dev);
1159 	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
1160 }
1161 
1162 static ssize_t pwm_min_store(struct device *dev,
1163 			     struct device_attribute *devattr,
1164 			     const char *buf, size_t count)
1165 {
1166 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1167 	struct adt7462_data *data = dev_get_drvdata(dev);
1168 	struct i2c_client *client = data->client;
1169 	long temp;
1170 
1171 	if (kstrtol(buf, 10, &temp))
1172 		return -EINVAL;
1173 
1174 	temp = clamp_val(temp, 0, 255);
1175 
1176 	mutex_lock(&data->lock);
1177 	data->pwm_min[attr->index] = temp;
1178 	i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MIN(attr->index),
1179 				  temp);
1180 	mutex_unlock(&data->lock);
1181 
1182 	return count;
1183 }
1184 
1185 static ssize_t pwm_hyst_show(struct device *dev,
1186 			     struct device_attribute *devattr, char *buf)
1187 {
1188 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1189 	struct adt7462_data *data = adt7462_update_device(dev);
1190 	return sprintf(buf, "%d\n", 1000 *
1191 		      (data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK));
1192 }
1193 
1194 static ssize_t pwm_hyst_store(struct device *dev,
1195 			      struct device_attribute *devattr,
1196 			      const char *buf, size_t count)
1197 {
1198 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1199 	struct adt7462_data *data = dev_get_drvdata(dev);
1200 	struct i2c_client *client = data->client;
1201 	long temp;
1202 
1203 	if (kstrtol(buf, 10, &temp))
1204 		return -EINVAL;
1205 
1206 	temp = clamp_val(temp, 0, 15000);
1207 	temp = DIV_ROUND_CLOSEST(temp, 1000);
1208 
1209 	/* package things up */
1210 	temp &= ADT7462_PWM_HYST_MASK;
1211 	temp |= data->pwm_trange[attr->index] & ADT7462_PWM_RANGE_MASK;
1212 
1213 	mutex_lock(&data->lock);
1214 	data->pwm_trange[attr->index] = temp;
1215 	i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1216 				  temp);
1217 	mutex_unlock(&data->lock);
1218 
1219 	return count;
1220 }
1221 
1222 static ssize_t pwm_tmax_show(struct device *dev,
1223 			     struct device_attribute *devattr, char *buf)
1224 {
1225 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1226 	struct adt7462_data *data = adt7462_update_device(dev);
1227 
1228 	/* tmax = tmin + trange */
1229 	int trange = trange_values[data->pwm_trange[attr->index] >>
1230 				   ADT7462_PWM_RANGE_SHIFT];
1231 	int tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1232 
1233 	return sprintf(buf, "%d\n", tmin + trange);
1234 }
1235 
1236 static ssize_t pwm_tmax_store(struct device *dev,
1237 			      struct device_attribute *devattr,
1238 			      const char *buf, size_t count)
1239 {
1240 	int temp;
1241 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1242 	struct adt7462_data *data = dev_get_drvdata(dev);
1243 	struct i2c_client *client = data->client;
1244 	int tmin, trange_value;
1245 	long trange;
1246 
1247 	if (kstrtol(buf, 10, &trange))
1248 		return -EINVAL;
1249 
1250 	/* trange = tmax - tmin */
1251 	tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1252 	trange_value = find_trange_value(trange - tmin);
1253 	if (trange_value < 0)
1254 		return trange_value;
1255 
1256 	temp = trange_value << ADT7462_PWM_RANGE_SHIFT;
1257 	temp |= data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK;
1258 
1259 	mutex_lock(&data->lock);
1260 	data->pwm_trange[attr->index] = temp;
1261 	i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1262 				  temp);
1263 	mutex_unlock(&data->lock);
1264 
1265 	return count;
1266 }
1267 
1268 static ssize_t pwm_tmin_show(struct device *dev,
1269 			     struct device_attribute *devattr, char *buf)
1270 {
1271 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1272 	struct adt7462_data *data = adt7462_update_device(dev);
1273 	return sprintf(buf, "%d\n", 1000 * (data->pwm_tmin[attr->index] - 64));
1274 }
1275 
1276 static ssize_t pwm_tmin_store(struct device *dev,
1277 			      struct device_attribute *devattr,
1278 			      const char *buf, size_t count)
1279 {
1280 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1281 	struct adt7462_data *data = dev_get_drvdata(dev);
1282 	struct i2c_client *client = data->client;
1283 	long temp;
1284 
1285 	if (kstrtol(buf, 10, &temp))
1286 		return -EINVAL;
1287 
1288 	temp = clamp_val(temp, -64000, 191000);
1289 	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
1290 
1291 	mutex_lock(&data->lock);
1292 	data->pwm_tmin[attr->index] = temp;
1293 	i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TMIN(attr->index),
1294 				  temp);
1295 	mutex_unlock(&data->lock);
1296 
1297 	return count;
1298 }
1299 
1300 static ssize_t pwm_auto_show(struct device *dev,
1301 			     struct device_attribute *devattr, char *buf)
1302 {
1303 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1304 	struct adt7462_data *data = adt7462_update_device(dev);
1305 	int cfg = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1306 
1307 	switch (cfg) {
1308 	case 4: /* off */
1309 		return sprintf(buf, "0\n");
1310 	case 7: /* manual */
1311 		return sprintf(buf, "1\n");
1312 	default: /* automatic */
1313 		return sprintf(buf, "2\n");
1314 	}
1315 }
1316 
1317 static void set_pwm_channel(struct i2c_client *client,
1318 			    struct adt7462_data *data,
1319 			    int which,
1320 			    int value)
1321 {
1322 	int temp = data->pwm_cfg[which] & ~ADT7462_PWM_CHANNEL_MASK;
1323 	temp |= value << ADT7462_PWM_CHANNEL_SHIFT;
1324 
1325 	mutex_lock(&data->lock);
1326 	data->pwm_cfg[which] = temp;
1327 	i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_CFG(which), temp);
1328 	mutex_unlock(&data->lock);
1329 }
1330 
1331 static ssize_t pwm_auto_store(struct device *dev,
1332 			      struct device_attribute *devattr,
1333 			      const char *buf, size_t count)
1334 {
1335 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1336 	struct adt7462_data *data = dev_get_drvdata(dev);
1337 	struct i2c_client *client = data->client;
1338 	long temp;
1339 
1340 	if (kstrtol(buf, 10, &temp))
1341 		return -EINVAL;
1342 
1343 	switch (temp) {
1344 	case 0: /* off */
1345 		set_pwm_channel(client, data, attr->index, 4);
1346 		return count;
1347 	case 1: /* manual */
1348 		set_pwm_channel(client, data, attr->index, 7);
1349 		return count;
1350 	default:
1351 		return -EINVAL;
1352 	}
1353 }
1354 
1355 static ssize_t pwm_auto_temp_show(struct device *dev,
1356 				  struct device_attribute *devattr, char *buf)
1357 {
1358 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1359 	struct adt7462_data *data = adt7462_update_device(dev);
1360 	int channel = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1361 
1362 	switch (channel) {
1363 	case 0: /* temp[1234] only */
1364 	case 1:
1365 	case 2:
1366 	case 3:
1367 		return sprintf(buf, "%d\n", (1 << channel));
1368 	case 5: /* temp1 & temp4  */
1369 		return sprintf(buf, "9\n");
1370 	case 6:
1371 		return sprintf(buf, "15\n");
1372 	default:
1373 		return sprintf(buf, "0\n");
1374 	}
1375 }
1376 
1377 static int cvt_auto_temp(int input)
1378 {
1379 	if (input == 0xF)
1380 		return 6;
1381 	if (input == 0x9)
1382 		return 5;
1383 	if (input < 1 || !is_power_of_2(input))
1384 		return -EINVAL;
1385 	return ilog2(input);
1386 }
1387 
1388 static ssize_t pwm_auto_temp_store(struct device *dev,
1389 				   struct device_attribute *devattr,
1390 				   const char *buf, size_t count)
1391 {
1392 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1393 	struct adt7462_data *data = dev_get_drvdata(dev);
1394 	struct i2c_client *client = data->client;
1395 	long temp;
1396 
1397 	if (kstrtol(buf, 10, &temp))
1398 		return -EINVAL;
1399 
1400 	temp = cvt_auto_temp(temp);
1401 	if (temp < 0)
1402 		return temp;
1403 
1404 	set_pwm_channel(client, data, attr->index, temp);
1405 
1406 	return count;
1407 }
1408 
1409 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
1410 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
1411 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
1412 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
1413 
1414 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1415 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1416 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1417 static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
1418 
1419 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1420 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1421 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1422 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
1423 
1424 static SENSOR_DEVICE_ATTR_RO(temp1_label, temp_label, 0);
1425 static SENSOR_DEVICE_ATTR_RO(temp2_label, temp_label, 1);
1426 static SENSOR_DEVICE_ATTR_RO(temp3_label, temp_label, 2);
1427 static SENSOR_DEVICE_ATTR_RO(temp4_label, temp_label, 3);
1428 
1429 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm,
1430 			     ADT7462_ALARM1 | ADT7462_LT_ALARM);
1431 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm,
1432 			     ADT7462_ALARM1 | ADT7462_R1T_ALARM);
1433 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm,
1434 			     ADT7462_ALARM1 | ADT7462_R2T_ALARM);
1435 static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm,
1436 			     ADT7462_ALARM1 | ADT7462_R3T_ALARM);
1437 
1438 static SENSOR_DEVICE_ATTR_RW(in1_max, volt_max, 0);
1439 static SENSOR_DEVICE_ATTR_RW(in2_max, volt_max, 1);
1440 static SENSOR_DEVICE_ATTR_RW(in3_max, volt_max, 2);
1441 static SENSOR_DEVICE_ATTR_RW(in4_max, volt_max, 3);
1442 static SENSOR_DEVICE_ATTR_RW(in5_max, volt_max, 4);
1443 static SENSOR_DEVICE_ATTR_RW(in6_max, volt_max, 5);
1444 static SENSOR_DEVICE_ATTR_RW(in7_max, volt_max, 6);
1445 static SENSOR_DEVICE_ATTR_RW(in8_max, volt_max, 7);
1446 static SENSOR_DEVICE_ATTR_RW(in9_max, volt_max, 8);
1447 static SENSOR_DEVICE_ATTR_RW(in10_max, volt_max, 9);
1448 static SENSOR_DEVICE_ATTR_RW(in11_max, volt_max, 10);
1449 static SENSOR_DEVICE_ATTR_RW(in12_max, volt_max, 11);
1450 static SENSOR_DEVICE_ATTR_RW(in13_max, volt_max, 12);
1451 
1452 static SENSOR_DEVICE_ATTR_RW(in1_min, volt_min, 0);
1453 static SENSOR_DEVICE_ATTR_RW(in2_min, volt_min, 1);
1454 static SENSOR_DEVICE_ATTR_RW(in3_min, volt_min, 2);
1455 static SENSOR_DEVICE_ATTR_RW(in4_min, volt_min, 3);
1456 static SENSOR_DEVICE_ATTR_RW(in5_min, volt_min, 4);
1457 static SENSOR_DEVICE_ATTR_RW(in6_min, volt_min, 5);
1458 static SENSOR_DEVICE_ATTR_RW(in7_min, volt_min, 6);
1459 static SENSOR_DEVICE_ATTR_RW(in8_min, volt_min, 7);
1460 static SENSOR_DEVICE_ATTR_RW(in9_min, volt_min, 8);
1461 static SENSOR_DEVICE_ATTR_RW(in10_min, volt_min, 9);
1462 static SENSOR_DEVICE_ATTR_RW(in11_min, volt_min, 10);
1463 static SENSOR_DEVICE_ATTR_RW(in12_min, volt_min, 11);
1464 static SENSOR_DEVICE_ATTR_RW(in13_min, volt_min, 12);
1465 
1466 static SENSOR_DEVICE_ATTR_RO(in1_input, voltage, 0);
1467 static SENSOR_DEVICE_ATTR_RO(in2_input, voltage, 1);
1468 static SENSOR_DEVICE_ATTR_RO(in3_input, voltage, 2);
1469 static SENSOR_DEVICE_ATTR_RO(in4_input, voltage, 3);
1470 static SENSOR_DEVICE_ATTR_RO(in5_input, voltage, 4);
1471 static SENSOR_DEVICE_ATTR_RO(in6_input, voltage, 5);
1472 static SENSOR_DEVICE_ATTR_RO(in7_input, voltage, 6);
1473 static SENSOR_DEVICE_ATTR_RO(in8_input, voltage, 7);
1474 static SENSOR_DEVICE_ATTR_RO(in9_input, voltage, 8);
1475 static SENSOR_DEVICE_ATTR_RO(in10_input, voltage, 9);
1476 static SENSOR_DEVICE_ATTR_RO(in11_input, voltage, 10);
1477 static SENSOR_DEVICE_ATTR_RO(in12_input, voltage, 11);
1478 static SENSOR_DEVICE_ATTR_RO(in13_input, voltage, 12);
1479 
1480 static SENSOR_DEVICE_ATTR_RO(in1_label, voltage_label, 0);
1481 static SENSOR_DEVICE_ATTR_RO(in2_label, voltage_label, 1);
1482 static SENSOR_DEVICE_ATTR_RO(in3_label, voltage_label, 2);
1483 static SENSOR_DEVICE_ATTR_RO(in4_label, voltage_label, 3);
1484 static SENSOR_DEVICE_ATTR_RO(in5_label, voltage_label, 4);
1485 static SENSOR_DEVICE_ATTR_RO(in6_label, voltage_label, 5);
1486 static SENSOR_DEVICE_ATTR_RO(in7_label, voltage_label, 6);
1487 static SENSOR_DEVICE_ATTR_RO(in8_label, voltage_label, 7);
1488 static SENSOR_DEVICE_ATTR_RO(in9_label, voltage_label, 8);
1489 static SENSOR_DEVICE_ATTR_RO(in10_label, voltage_label, 9);
1490 static SENSOR_DEVICE_ATTR_RO(in11_label, voltage_label, 10);
1491 static SENSOR_DEVICE_ATTR_RO(in12_label, voltage_label, 11);
1492 static SENSOR_DEVICE_ATTR_RO(in13_label, voltage_label, 12);
1493 
1494 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm,
1495 			     ADT7462_ALARM2 | ADT7462_V0_ALARM);
1496 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm,
1497 			     ADT7462_ALARM2 | ADT7462_V7_ALARM);
1498 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm,
1499 			     ADT7462_ALARM2 | ADT7462_V2_ALARM);
1500 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm,
1501 			     ADT7462_ALARM2 | ADT7462_V6_ALARM);
1502 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm,
1503 			     ADT7462_ALARM2 | ADT7462_V5_ALARM);
1504 static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm,
1505 			     ADT7462_ALARM2 | ADT7462_V4_ALARM);
1506 static SENSOR_DEVICE_ATTR_RO(in7_alarm, alarm,
1507 			     ADT7462_ALARM2 | ADT7462_V3_ALARM);
1508 static SENSOR_DEVICE_ATTR_RO(in8_alarm, alarm,
1509 			     ADT7462_ALARM2 | ADT7462_V1_ALARM);
1510 static SENSOR_DEVICE_ATTR_RO(in9_alarm, alarm,
1511 			     ADT7462_ALARM3 | ADT7462_V10_ALARM);
1512 static SENSOR_DEVICE_ATTR_RO(in10_alarm, alarm,
1513 			     ADT7462_ALARM3 | ADT7462_V9_ALARM);
1514 static SENSOR_DEVICE_ATTR_RO(in11_alarm, alarm,
1515 			     ADT7462_ALARM3 | ADT7462_V8_ALARM);
1516 static SENSOR_DEVICE_ATTR_RO(in12_alarm, alarm,
1517 			     ADT7462_ALARM3 | ADT7462_V11_ALARM);
1518 static SENSOR_DEVICE_ATTR_RO(in13_alarm, alarm,
1519 			     ADT7462_ALARM3 | ADT7462_V12_ALARM);
1520 
1521 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1522 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1523 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1524 static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
1525 static SENSOR_DEVICE_ATTR_RW(fan5_min, fan_min, 4);
1526 static SENSOR_DEVICE_ATTR_RW(fan6_min, fan_min, 5);
1527 static SENSOR_DEVICE_ATTR_RW(fan7_min, fan_min, 6);
1528 static SENSOR_DEVICE_ATTR_RW(fan8_min, fan_min, 7);
1529 
1530 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
1531 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
1532 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
1533 static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
1534 static SENSOR_DEVICE_ATTR_RO(fan5_input, fan, 4);
1535 static SENSOR_DEVICE_ATTR_RO(fan6_input, fan, 5);
1536 static SENSOR_DEVICE_ATTR_RO(fan7_input, fan, 6);
1537 static SENSOR_DEVICE_ATTR_RO(fan8_input, fan, 7);
1538 
1539 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm,
1540 			     ADT7462_ALARM4 | ADT7462_F0_ALARM);
1541 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm,
1542 			     ADT7462_ALARM4 | ADT7462_F1_ALARM);
1543 static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm,
1544 			     ADT7462_ALARM4 | ADT7462_F2_ALARM);
1545 static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm,
1546 			     ADT7462_ALARM4 | ADT7462_F3_ALARM);
1547 static SENSOR_DEVICE_ATTR_RO(fan5_alarm, alarm,
1548 			     ADT7462_ALARM4 | ADT7462_F4_ALARM);
1549 static SENSOR_DEVICE_ATTR_RO(fan6_alarm, alarm,
1550 			     ADT7462_ALARM4 | ADT7462_F5_ALARM);
1551 static SENSOR_DEVICE_ATTR_RO(fan7_alarm, alarm,
1552 			     ADT7462_ALARM4 | ADT7462_F6_ALARM);
1553 static SENSOR_DEVICE_ATTR_RO(fan8_alarm, alarm,
1554 			     ADT7462_ALARM4 | ADT7462_F7_ALARM);
1555 
1556 static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
1557 
1558 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1559 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1560 static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
1561 static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
1562 
1563 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
1564 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
1565 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
1566 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
1567 
1568 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
1569 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
1570 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
1571 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
1572 
1573 static SENSOR_DEVICE_ATTR_RW(temp1_auto_point1_hyst, pwm_hyst, 0);
1574 static SENSOR_DEVICE_ATTR_RW(temp2_auto_point1_hyst, pwm_hyst, 1);
1575 static SENSOR_DEVICE_ATTR_RW(temp3_auto_point1_hyst, pwm_hyst, 2);
1576 static SENSOR_DEVICE_ATTR_RW(temp4_auto_point1_hyst, pwm_hyst, 3);
1577 
1578 static SENSOR_DEVICE_ATTR_RW(temp1_auto_point2_hyst, pwm_hyst, 0);
1579 static SENSOR_DEVICE_ATTR_RW(temp2_auto_point2_hyst, pwm_hyst, 1);
1580 static SENSOR_DEVICE_ATTR_RW(temp3_auto_point2_hyst, pwm_hyst, 2);
1581 static SENSOR_DEVICE_ATTR_RW(temp4_auto_point2_hyst, pwm_hyst, 3);
1582 
1583 static SENSOR_DEVICE_ATTR_RW(temp1_auto_point1_temp, pwm_tmin, 0);
1584 static SENSOR_DEVICE_ATTR_RW(temp2_auto_point1_temp, pwm_tmin, 1);
1585 static SENSOR_DEVICE_ATTR_RW(temp3_auto_point1_temp, pwm_tmin, 2);
1586 static SENSOR_DEVICE_ATTR_RW(temp4_auto_point1_temp, pwm_tmin, 3);
1587 
1588 static SENSOR_DEVICE_ATTR_RW(temp1_auto_point2_temp, pwm_tmax, 0);
1589 static SENSOR_DEVICE_ATTR_RW(temp2_auto_point2_temp, pwm_tmax, 1);
1590 static SENSOR_DEVICE_ATTR_RW(temp3_auto_point2_temp, pwm_tmax, 2);
1591 static SENSOR_DEVICE_ATTR_RW(temp4_auto_point2_temp, pwm_tmax, 3);
1592 
1593 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
1594 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
1595 static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
1596 static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
1597 
1598 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
1599 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
1600 static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
1601 static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
1602 
1603 static struct attribute *adt7462_attrs[] = {
1604 	&sensor_dev_attr_temp1_max.dev_attr.attr,
1605 	&sensor_dev_attr_temp2_max.dev_attr.attr,
1606 	&sensor_dev_attr_temp3_max.dev_attr.attr,
1607 	&sensor_dev_attr_temp4_max.dev_attr.attr,
1608 
1609 	&sensor_dev_attr_temp1_min.dev_attr.attr,
1610 	&sensor_dev_attr_temp2_min.dev_attr.attr,
1611 	&sensor_dev_attr_temp3_min.dev_attr.attr,
1612 	&sensor_dev_attr_temp4_min.dev_attr.attr,
1613 
1614 	&sensor_dev_attr_temp1_input.dev_attr.attr,
1615 	&sensor_dev_attr_temp2_input.dev_attr.attr,
1616 	&sensor_dev_attr_temp3_input.dev_attr.attr,
1617 	&sensor_dev_attr_temp4_input.dev_attr.attr,
1618 
1619 	&sensor_dev_attr_temp1_label.dev_attr.attr,
1620 	&sensor_dev_attr_temp2_label.dev_attr.attr,
1621 	&sensor_dev_attr_temp3_label.dev_attr.attr,
1622 	&sensor_dev_attr_temp4_label.dev_attr.attr,
1623 
1624 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1625 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1626 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1627 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
1628 
1629 	&sensor_dev_attr_in1_max.dev_attr.attr,
1630 	&sensor_dev_attr_in2_max.dev_attr.attr,
1631 	&sensor_dev_attr_in3_max.dev_attr.attr,
1632 	&sensor_dev_attr_in4_max.dev_attr.attr,
1633 	&sensor_dev_attr_in5_max.dev_attr.attr,
1634 	&sensor_dev_attr_in6_max.dev_attr.attr,
1635 	&sensor_dev_attr_in7_max.dev_attr.attr,
1636 	&sensor_dev_attr_in8_max.dev_attr.attr,
1637 	&sensor_dev_attr_in9_max.dev_attr.attr,
1638 	&sensor_dev_attr_in10_max.dev_attr.attr,
1639 	&sensor_dev_attr_in11_max.dev_attr.attr,
1640 	&sensor_dev_attr_in12_max.dev_attr.attr,
1641 	&sensor_dev_attr_in13_max.dev_attr.attr,
1642 
1643 	&sensor_dev_attr_in1_min.dev_attr.attr,
1644 	&sensor_dev_attr_in2_min.dev_attr.attr,
1645 	&sensor_dev_attr_in3_min.dev_attr.attr,
1646 	&sensor_dev_attr_in4_min.dev_attr.attr,
1647 	&sensor_dev_attr_in5_min.dev_attr.attr,
1648 	&sensor_dev_attr_in6_min.dev_attr.attr,
1649 	&sensor_dev_attr_in7_min.dev_attr.attr,
1650 	&sensor_dev_attr_in8_min.dev_attr.attr,
1651 	&sensor_dev_attr_in9_min.dev_attr.attr,
1652 	&sensor_dev_attr_in10_min.dev_attr.attr,
1653 	&sensor_dev_attr_in11_min.dev_attr.attr,
1654 	&sensor_dev_attr_in12_min.dev_attr.attr,
1655 	&sensor_dev_attr_in13_min.dev_attr.attr,
1656 
1657 	&sensor_dev_attr_in1_input.dev_attr.attr,
1658 	&sensor_dev_attr_in2_input.dev_attr.attr,
1659 	&sensor_dev_attr_in3_input.dev_attr.attr,
1660 	&sensor_dev_attr_in4_input.dev_attr.attr,
1661 	&sensor_dev_attr_in5_input.dev_attr.attr,
1662 	&sensor_dev_attr_in6_input.dev_attr.attr,
1663 	&sensor_dev_attr_in7_input.dev_attr.attr,
1664 	&sensor_dev_attr_in8_input.dev_attr.attr,
1665 	&sensor_dev_attr_in9_input.dev_attr.attr,
1666 	&sensor_dev_attr_in10_input.dev_attr.attr,
1667 	&sensor_dev_attr_in11_input.dev_attr.attr,
1668 	&sensor_dev_attr_in12_input.dev_attr.attr,
1669 	&sensor_dev_attr_in13_input.dev_attr.attr,
1670 
1671 	&sensor_dev_attr_in1_label.dev_attr.attr,
1672 	&sensor_dev_attr_in2_label.dev_attr.attr,
1673 	&sensor_dev_attr_in3_label.dev_attr.attr,
1674 	&sensor_dev_attr_in4_label.dev_attr.attr,
1675 	&sensor_dev_attr_in5_label.dev_attr.attr,
1676 	&sensor_dev_attr_in6_label.dev_attr.attr,
1677 	&sensor_dev_attr_in7_label.dev_attr.attr,
1678 	&sensor_dev_attr_in8_label.dev_attr.attr,
1679 	&sensor_dev_attr_in9_label.dev_attr.attr,
1680 	&sensor_dev_attr_in10_label.dev_attr.attr,
1681 	&sensor_dev_attr_in11_label.dev_attr.attr,
1682 	&sensor_dev_attr_in12_label.dev_attr.attr,
1683 	&sensor_dev_attr_in13_label.dev_attr.attr,
1684 
1685 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
1686 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
1687 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1688 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1689 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
1690 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
1691 	&sensor_dev_attr_in7_alarm.dev_attr.attr,
1692 	&sensor_dev_attr_in8_alarm.dev_attr.attr,
1693 	&sensor_dev_attr_in9_alarm.dev_attr.attr,
1694 	&sensor_dev_attr_in10_alarm.dev_attr.attr,
1695 	&sensor_dev_attr_in11_alarm.dev_attr.attr,
1696 	&sensor_dev_attr_in12_alarm.dev_attr.attr,
1697 	&sensor_dev_attr_in13_alarm.dev_attr.attr,
1698 
1699 	&sensor_dev_attr_fan1_min.dev_attr.attr,
1700 	&sensor_dev_attr_fan2_min.dev_attr.attr,
1701 	&sensor_dev_attr_fan3_min.dev_attr.attr,
1702 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1703 	&sensor_dev_attr_fan5_min.dev_attr.attr,
1704 	&sensor_dev_attr_fan6_min.dev_attr.attr,
1705 	&sensor_dev_attr_fan7_min.dev_attr.attr,
1706 	&sensor_dev_attr_fan8_min.dev_attr.attr,
1707 
1708 	&sensor_dev_attr_fan1_input.dev_attr.attr,
1709 	&sensor_dev_attr_fan2_input.dev_attr.attr,
1710 	&sensor_dev_attr_fan3_input.dev_attr.attr,
1711 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1712 	&sensor_dev_attr_fan5_input.dev_attr.attr,
1713 	&sensor_dev_attr_fan6_input.dev_attr.attr,
1714 	&sensor_dev_attr_fan7_input.dev_attr.attr,
1715 	&sensor_dev_attr_fan8_input.dev_attr.attr,
1716 
1717 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1718 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1719 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1720 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1721 	&sensor_dev_attr_fan5_alarm.dev_attr.attr,
1722 	&sensor_dev_attr_fan6_alarm.dev_attr.attr,
1723 	&sensor_dev_attr_fan7_alarm.dev_attr.attr,
1724 	&sensor_dev_attr_fan8_alarm.dev_attr.attr,
1725 
1726 	&sensor_dev_attr_force_pwm_max.dev_attr.attr,
1727 	&sensor_dev_attr_pwm1.dev_attr.attr,
1728 	&sensor_dev_attr_pwm2.dev_attr.attr,
1729 	&sensor_dev_attr_pwm3.dev_attr.attr,
1730 	&sensor_dev_attr_pwm4.dev_attr.attr,
1731 
1732 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1733 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1734 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1735 	&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1736 
1737 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1738 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1739 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1740 	&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1741 
1742 	&sensor_dev_attr_temp1_auto_point1_hyst.dev_attr.attr,
1743 	&sensor_dev_attr_temp2_auto_point1_hyst.dev_attr.attr,
1744 	&sensor_dev_attr_temp3_auto_point1_hyst.dev_attr.attr,
1745 	&sensor_dev_attr_temp4_auto_point1_hyst.dev_attr.attr,
1746 
1747 	&sensor_dev_attr_temp1_auto_point2_hyst.dev_attr.attr,
1748 	&sensor_dev_attr_temp2_auto_point2_hyst.dev_attr.attr,
1749 	&sensor_dev_attr_temp3_auto_point2_hyst.dev_attr.attr,
1750 	&sensor_dev_attr_temp4_auto_point2_hyst.dev_attr.attr,
1751 
1752 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1753 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1754 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1755 	&sensor_dev_attr_temp4_auto_point1_temp.dev_attr.attr,
1756 
1757 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1758 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1759 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1760 	&sensor_dev_attr_temp4_auto_point2_temp.dev_attr.attr,
1761 
1762 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
1763 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1764 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
1765 	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
1766 
1767 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1768 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1769 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1770 	&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1771 	NULL
1772 };
1773 
1774 ATTRIBUTE_GROUPS(adt7462);
1775 
1776 /* Return 0 if detection is successful, -ENODEV otherwise */
1777 static int adt7462_detect(struct i2c_client *client,
1778 			  struct i2c_board_info *info)
1779 {
1780 	struct i2c_adapter *adapter = client->adapter;
1781 	int vendor, device, revision;
1782 
1783 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1784 		return -ENODEV;
1785 
1786 	vendor = i2c_smbus_read_byte_data(client, ADT7462_REG_VENDOR);
1787 	if (vendor != ADT7462_VENDOR)
1788 		return -ENODEV;
1789 
1790 	device = i2c_smbus_read_byte_data(client, ADT7462_REG_DEVICE);
1791 	if (device != ADT7462_DEVICE)
1792 		return -ENODEV;
1793 
1794 	revision = i2c_smbus_read_byte_data(client, ADT7462_REG_REVISION);
1795 	if (revision != ADT7462_REVISION)
1796 		return -ENODEV;
1797 
1798 	strlcpy(info->type, "adt7462", I2C_NAME_SIZE);
1799 
1800 	return 0;
1801 }
1802 
1803 static int adt7462_probe(struct i2c_client *client,
1804 			 const struct i2c_device_id *id)
1805 {
1806 	struct device *dev = &client->dev;
1807 	struct adt7462_data *data;
1808 	struct device *hwmon_dev;
1809 
1810 	data = devm_kzalloc(dev, sizeof(struct adt7462_data), GFP_KERNEL);
1811 	if (!data)
1812 		return -ENOMEM;
1813 
1814 	data->client = client;
1815 	mutex_init(&data->lock);
1816 
1817 	dev_info(&client->dev, "%s chip found\n", client->name);
1818 
1819 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1820 							   data,
1821 							   adt7462_groups);
1822 	return PTR_ERR_OR_ZERO(hwmon_dev);
1823 }
1824 
1825 static const struct i2c_device_id adt7462_id[] = {
1826 	{ "adt7462", 0 },
1827 	{ }
1828 };
1829 MODULE_DEVICE_TABLE(i2c, adt7462_id);
1830 
1831 static struct i2c_driver adt7462_driver = {
1832 	.class		= I2C_CLASS_HWMON,
1833 	.driver = {
1834 		.name	= "adt7462",
1835 	},
1836 	.probe		= adt7462_probe,
1837 	.id_table	= adt7462_id,
1838 	.detect		= adt7462_detect,
1839 	.address_list	= normal_i2c,
1840 };
1841 
1842 module_i2c_driver(adt7462_driver);
1843 
1844 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1845 MODULE_DESCRIPTION("ADT7462 driver");
1846 MODULE_LICENSE("GPL");
1847