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