xref: /linux/drivers/hwmon/adt7475.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
4  * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
5  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
6  * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
7  * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
8  *
9  * Derived from the lm83 driver by Jean Delvare
10  */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/hwmon-vid.h>
19 #include <linux/err.h>
20 #include <linux/jiffies.h>
21 #include <linux/of.h>
22 #include <linux/util_macros.h>
23 
24 #include <dt-bindings/pwm/pwm.h>
25 
26 /* Indexes for the sysfs hooks */
27 enum adt_sysfs_id {
28 	INPUT		= 0,
29 	MIN		= 1,
30 	MAX		= 2,
31 	CONTROL		= 3,
32 	OFFSET		= 3,	// Dup
33 	AUTOMIN		= 4,
34 	THERM		= 5,
35 	HYSTERSIS	= 6,
36 /*
37  * These are unique identifiers for the sysfs functions - unlike the
38  * numbers above, these are not also indexes into an array
39  */
40 	ALARM		= 9,
41 	FAULT		= 10,
42 };
43 
44 
45 /* 7475 Common Registers */
46 
47 #define REG_DEVREV2		0x12	/* ADT7490 only */
48 #define REG_IMON		0x1D	/* ADT7490 only */
49 
50 #define REG_VTT			0x1E	/* ADT7490 only */
51 #define REG_EXTEND3		0x1F	/* ADT7490 only */
52 
53 #define REG_VOLTAGE_BASE	0x20
54 #define REG_TEMP_BASE		0x25
55 #define REG_TACH_BASE		0x28
56 #define REG_PWM_BASE		0x30
57 #define REG_PWM_MAX_BASE	0x38
58 
59 #define REG_DEVID		0x3D
60 #define REG_VENDID		0x3E
61 #define REG_DEVID2		0x3F
62 
63 #define REG_CONFIG1		0x40
64 
65 #define REG_STATUS1		0x41
66 #define REG_STATUS2		0x42
67 
68 #define REG_VID			0x43	/* ADT7476 only */
69 
70 #define REG_VOLTAGE_MIN_BASE	0x44
71 #define REG_VOLTAGE_MAX_BASE	0x45
72 
73 #define REG_TEMP_MIN_BASE	0x4E
74 #define REG_TEMP_MAX_BASE	0x4F
75 
76 #define REG_TACH_MIN_BASE	0x54
77 
78 #define REG_PWM_CONFIG_BASE	0x5C
79 
80 #define REG_TEMP_TRANGE_BASE	0x5F
81 
82 #define REG_ENHANCE_ACOUSTICS1	0x62
83 #define REG_ENHANCE_ACOUSTICS2	0x63
84 
85 #define REG_PWM_MIN_BASE	0x64
86 
87 #define REG_TEMP_TMIN_BASE	0x67
88 #define REG_TEMP_THERM_BASE	0x6A
89 
90 #define REG_REMOTE1_HYSTERSIS	0x6D
91 #define REG_REMOTE2_HYSTERSIS	0x6E
92 
93 #define REG_TEMP_OFFSET_BASE	0x70
94 
95 #define REG_CONFIG2		0x73
96 
97 #define REG_EXTEND1		0x76
98 #define REG_EXTEND2		0x77
99 
100 #define REG_CONFIG3		0x78
101 #define REG_CONFIG5		0x7C
102 #define REG_CONFIG4		0x7D
103 
104 #define REG_STATUS4		0x81	/* ADT7490 only */
105 
106 #define REG_VTT_MIN		0x84	/* ADT7490 only */
107 #define REG_VTT_MAX		0x86	/* ADT7490 only */
108 
109 #define REG_IMON_MIN		0x85	/* ADT7490 only */
110 #define REG_IMON_MAX		0x87	/* ADT7490 only */
111 
112 #define VID_VIDSEL		0x80	/* ADT7476 only */
113 
114 #define CONFIG2_ATTN		0x20
115 
116 #define CONFIG3_SMBALERT	0x01
117 #define CONFIG3_THERM		0x02
118 
119 #define CONFIG4_PINFUNC		0x03
120 #define CONFIG4_THERM		0x01
121 #define CONFIG4_SMBALERT	0x02
122 #define CONFIG4_MAXDUTY		0x08
123 #define CONFIG4_ATTN_IN10	0x30
124 #define CONFIG4_ATTN_IN43	0xC0
125 
126 #define CONFIG5_TWOSCOMP	0x01
127 #define CONFIG5_TEMPOFFSET	0x02
128 #define CONFIG5_VIDGPIO		0x10	/* ADT7476 only */
129 
130 /* ADT7475 Settings */
131 
132 #define ADT7475_VOLTAGE_COUNT	5	/* Not counting Vtt or Imon */
133 #define ADT7475_TEMP_COUNT	3
134 #define ADT7475_TACH_COUNT	4
135 #define ADT7475_PWM_COUNT	3
136 
137 /* Macro to read the registers */
138 
139 #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
140 
141 /* Macros to easily index the registers */
142 
143 #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
144 #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
145 
146 #define PWM_REG(idx) (REG_PWM_BASE + (idx))
147 #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
148 #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
149 #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
150 
151 #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
152 #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
153 #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
154 
155 #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
156 #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
157 #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
158 #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
159 #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
160 #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
161 #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
162 
163 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
164 
165 enum chips { adt7473, adt7475, adt7476, adt7490 };
166 
167 static const struct i2c_device_id adt7475_id[] = {
168 	{ "adt7473", adt7473 },
169 	{ "adt7475", adt7475 },
170 	{ "adt7476", adt7476 },
171 	{ "adt7490", adt7490 },
172 	{ }
173 };
174 MODULE_DEVICE_TABLE(i2c, adt7475_id);
175 
176 static const struct of_device_id __maybe_unused adt7475_of_match[] = {
177 	{
178 		.compatible = "adi,adt7473",
179 		.data = (void *)adt7473
180 	},
181 	{
182 		.compatible = "adi,adt7475",
183 		.data = (void *)adt7475
184 	},
185 	{
186 		.compatible = "adi,adt7476",
187 		.data = (void *)adt7476
188 	},
189 	{
190 		.compatible = "adi,adt7490",
191 		.data = (void *)adt7490
192 	},
193 	{ },
194 };
195 MODULE_DEVICE_TABLE(of, adt7475_of_match);
196 
197 struct adt7475_data {
198 	struct i2c_client *client;
199 	struct mutex lock;
200 
201 	unsigned long measure_updated;
202 	bool valid;
203 
204 	u8 config2;
205 	u8 config4;
206 	u8 config5;
207 	u8 has_voltage;
208 	u8 bypass_attn;		/* Bypass voltage attenuator */
209 	u8 has_pwm2:1;
210 	u8 has_fan4:1;
211 	u8 has_vid:1;
212 	u32 alarms;
213 	u16 voltage[3][7];
214 	u16 temp[7][3];
215 	u16 tach[2][4];
216 	u8 pwm[4][3];
217 	u8 range[3];
218 	u8 pwmctl[3];
219 	u8 pwmchan[3];
220 	u8 enh_acoustics[2];
221 
222 	u8 vid;
223 	u8 vrm;
224 	const struct attribute_group *groups[10];
225 };
226 
227 static struct i2c_driver adt7475_driver;
228 static struct adt7475_data *adt7475_update_device(struct device *dev);
229 static void adt7475_read_hystersis(struct i2c_client *client);
230 static void adt7475_read_pwm(struct i2c_client *client, int index);
231 
232 /* Given a temp value, convert it to register value */
233 
234 static inline u16 temp2reg(struct adt7475_data *data, long val)
235 {
236 	u16 ret;
237 
238 	if (!(data->config5 & CONFIG5_TWOSCOMP)) {
239 		val = clamp_val(val, -64000, 191000);
240 		ret = (val + 64500) / 1000;
241 	} else {
242 		val = clamp_val(val, -128000, 127000);
243 		if (val < -500)
244 			ret = (256500 + val) / 1000;
245 		else
246 			ret = (val + 500) / 1000;
247 	}
248 
249 	return ret << 2;
250 }
251 
252 /* Given a register value, convert it to a real temp value */
253 
254 static inline int reg2temp(struct adt7475_data *data, u16 reg)
255 {
256 	if (data->config5 & CONFIG5_TWOSCOMP) {
257 		if (reg >= 512)
258 			return (reg - 1024) * 250;
259 		else
260 			return reg * 250;
261 	} else
262 		return (reg - 256) * 250;
263 }
264 
265 static inline int tach2rpm(u16 tach)
266 {
267 	if (tach == 0 || tach == 0xFFFF)
268 		return 0;
269 
270 	return (90000 * 60) / tach;
271 }
272 
273 static inline u16 rpm2tach(unsigned long rpm)
274 {
275 	if (rpm == 0)
276 		return 0;
277 
278 	return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
279 }
280 
281 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
282 static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 2][2] = {
283 	{ 45, 94 },	/* +2.5V */
284 	{ 175, 525 },	/* Vccp */
285 	{ 68, 71 },	/* Vcc */
286 	{ 93, 47 },	/* +5V */
287 	{ 120, 20 },	/* +12V */
288 	{ 45, 45 },	/* Vtt */
289 	{ 45, 45 },	/* Imon */
290 };
291 
292 static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
293 {
294 	const int *r = adt7473_in_scaling[channel];
295 
296 	if (bypass_attn & (1 << channel))
297 		return DIV_ROUND_CLOSEST(reg * 2250, 1024);
298 	return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
299 }
300 
301 static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
302 {
303 	const int *r = adt7473_in_scaling[channel];
304 	long reg;
305 
306 	if (bypass_attn & (1 << channel))
307 		reg = DIV_ROUND_CLOSEST(volt * 1024, 2250);
308 	else
309 		reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024,
310 					(r[0] + r[1]) * 2250);
311 	return clamp_val(reg, 0, 1023) & (0xff << 2);
312 }
313 
314 static int adt7475_read_word(struct i2c_client *client, int reg)
315 {
316 	int val1, val2;
317 
318 	val1 = i2c_smbus_read_byte_data(client, reg);
319 	if (val1 < 0)
320 		return val1;
321 	val2 = i2c_smbus_read_byte_data(client, reg + 1);
322 	if (val2 < 0)
323 		return val2;
324 
325 	return val1 | (val2 << 8);
326 }
327 
328 static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
329 {
330 	i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
331 	i2c_smbus_write_byte_data(client, reg, val & 0xFF);
332 }
333 
334 static ssize_t voltage_show(struct device *dev, struct device_attribute *attr,
335 			    char *buf)
336 {
337 	struct adt7475_data *data = adt7475_update_device(dev);
338 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
339 	unsigned short val;
340 
341 	if (IS_ERR(data))
342 		return PTR_ERR(data);
343 
344 	switch (sattr->nr) {
345 	case ALARM:
346 		return sprintf(buf, "%d\n",
347 			       (data->alarms >> sattr->index) & 1);
348 	default:
349 		val = data->voltage[sattr->nr][sattr->index];
350 		return sprintf(buf, "%d\n",
351 			       reg2volt(sattr->index, val, data->bypass_attn));
352 	}
353 }
354 
355 static ssize_t voltage_store(struct device *dev,
356 			     struct device_attribute *attr, const char *buf,
357 			     size_t count)
358 {
359 
360 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
361 	struct adt7475_data *data = dev_get_drvdata(dev);
362 	struct i2c_client *client = data->client;
363 	unsigned char reg;
364 	long val;
365 
366 	if (kstrtol(buf, 10, &val))
367 		return -EINVAL;
368 
369 	mutex_lock(&data->lock);
370 
371 	data->voltage[sattr->nr][sattr->index] =
372 				volt2reg(sattr->index, val, data->bypass_attn);
373 
374 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
375 		if (sattr->nr == MIN)
376 			reg = VOLTAGE_MIN_REG(sattr->index);
377 		else
378 			reg = VOLTAGE_MAX_REG(sattr->index);
379 	} else if (sattr->index == 5) {
380 		if (sattr->nr == MIN)
381 			reg = REG_VTT_MIN;
382 		else
383 			reg = REG_VTT_MAX;
384 	} else {
385 		if (sattr->nr == MIN)
386 			reg = REG_IMON_MIN;
387 		else
388 			reg = REG_IMON_MAX;
389 	}
390 
391 	i2c_smbus_write_byte_data(client, reg,
392 				  data->voltage[sattr->nr][sattr->index] >> 2);
393 	mutex_unlock(&data->lock);
394 
395 	return count;
396 }
397 
398 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
399 			 char *buf)
400 {
401 	struct adt7475_data *data = adt7475_update_device(dev);
402 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
403 	int out;
404 
405 	if (IS_ERR(data))
406 		return PTR_ERR(data);
407 
408 	switch (sattr->nr) {
409 	case HYSTERSIS:
410 		mutex_lock(&data->lock);
411 		out = data->temp[sattr->nr][sattr->index];
412 		if (sattr->index != 1)
413 			out = (out >> 4) & 0xF;
414 		else
415 			out = (out & 0xF);
416 		/*
417 		 * Show the value as an absolute number tied to
418 		 * THERM
419 		 */
420 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
421 			out * 1000;
422 		mutex_unlock(&data->lock);
423 		break;
424 
425 	case OFFSET:
426 		/*
427 		 * Offset is always 2's complement, regardless of the
428 		 * setting in CONFIG5
429 		 */
430 		mutex_lock(&data->lock);
431 		out = (s8)data->temp[sattr->nr][sattr->index];
432 		if (data->config5 & CONFIG5_TEMPOFFSET)
433 			out *= 1000;
434 		else
435 			out *= 500;
436 		mutex_unlock(&data->lock);
437 		break;
438 
439 	case ALARM:
440 		out = (data->alarms >> (sattr->index + 4)) & 1;
441 		break;
442 
443 	case FAULT:
444 		/* Note - only for remote1 and remote2 */
445 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
446 		break;
447 
448 	default:
449 		/* All other temp values are in the configured format */
450 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
451 	}
452 
453 	return sprintf(buf, "%d\n", out);
454 }
455 
456 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
457 			  const char *buf, size_t count)
458 {
459 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
460 	struct adt7475_data *data = dev_get_drvdata(dev);
461 	struct i2c_client *client = data->client;
462 	unsigned char reg = 0;
463 	u8 out;
464 	int temp;
465 	long val;
466 
467 	if (kstrtol(buf, 10, &val))
468 		return -EINVAL;
469 
470 	mutex_lock(&data->lock);
471 
472 	/* We need the config register in all cases for temp <-> reg conv. */
473 	data->config5 = adt7475_read(REG_CONFIG5);
474 
475 	switch (sattr->nr) {
476 	case OFFSET:
477 		if (data->config5 & CONFIG5_TEMPOFFSET) {
478 			val = clamp_val(val, -63000, 127000);
479 			out = data->temp[OFFSET][sattr->index] = val / 1000;
480 		} else {
481 			val = clamp_val(val, -63000, 64000);
482 			out = data->temp[OFFSET][sattr->index] = val / 500;
483 		}
484 		break;
485 
486 	case HYSTERSIS:
487 		/*
488 		 * The value will be given as an absolute value, turn it
489 		 * into an offset based on THERM
490 		 */
491 
492 		/* Read fresh THERM and HYSTERSIS values from the chip */
493 		data->temp[THERM][sattr->index] =
494 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
495 		adt7475_read_hystersis(client);
496 
497 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
498 		val = clamp_val(val, temp - 15000, temp);
499 		val = (temp - val) / 1000;
500 
501 		if (sattr->index != 1) {
502 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
503 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
504 		} else {
505 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
506 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
507 		}
508 
509 		out = data->temp[HYSTERSIS][sattr->index];
510 		break;
511 
512 	default:
513 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
514 
515 		/*
516 		 * We maintain an extra 2 digits of precision for simplicity
517 		 * - shift those back off before writing the value
518 		 */
519 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
520 	}
521 
522 	switch (sattr->nr) {
523 	case MIN:
524 		reg = TEMP_MIN_REG(sattr->index);
525 		break;
526 	case MAX:
527 		reg = TEMP_MAX_REG(sattr->index);
528 		break;
529 	case OFFSET:
530 		reg = TEMP_OFFSET_REG(sattr->index);
531 		break;
532 	case AUTOMIN:
533 		reg = TEMP_TMIN_REG(sattr->index);
534 		break;
535 	case THERM:
536 		reg = TEMP_THERM_REG(sattr->index);
537 		break;
538 	case HYSTERSIS:
539 		if (sattr->index != 2)
540 			reg = REG_REMOTE1_HYSTERSIS;
541 		else
542 			reg = REG_REMOTE2_HYSTERSIS;
543 
544 		break;
545 	}
546 
547 	i2c_smbus_write_byte_data(client, reg, out);
548 
549 	mutex_unlock(&data->lock);
550 	return count;
551 }
552 
553 /* Assuming CONFIG6[SLOW] is 0 */
554 static const int ad7475_st_map[] = {
555 	37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
556 };
557 
558 static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr,
559 			    char *buf)
560 {
561 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
562 	struct adt7475_data *data = dev_get_drvdata(dev);
563 	long val;
564 
565 	switch (sattr->index) {
566 	case 0:
567 		val = data->enh_acoustics[0] & 0xf;
568 		break;
569 	case 1:
570 		val = data->enh_acoustics[1] & 0xf;
571 		break;
572 	case 2:
573 	default:
574 		val = (data->enh_acoustics[1] >> 4) & 0xf;
575 		break;
576 	}
577 
578 	if (val & 0x8)
579 		return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
580 	else
581 		return sprintf(buf, "0\n");
582 }
583 
584 static ssize_t temp_st_store(struct device *dev,
585 			     struct device_attribute *attr, const char *buf,
586 			     size_t count)
587 {
588 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
589 	struct adt7475_data *data = dev_get_drvdata(dev);
590 	struct i2c_client *client = data->client;
591 	unsigned char reg;
592 	int shift, idx;
593 	ulong val;
594 
595 	if (kstrtoul(buf, 10, &val))
596 		return -EINVAL;
597 
598 	switch (sattr->index) {
599 	case 0:
600 		reg = REG_ENHANCE_ACOUSTICS1;
601 		shift = 0;
602 		idx = 0;
603 		break;
604 	case 1:
605 		reg = REG_ENHANCE_ACOUSTICS2;
606 		shift = 0;
607 		idx = 1;
608 		break;
609 	case 2:
610 	default:
611 		reg = REG_ENHANCE_ACOUSTICS2;
612 		shift = 4;
613 		idx = 1;
614 		break;
615 	}
616 
617 	if (val > 0) {
618 		val = find_closest_descending(val, ad7475_st_map,
619 					      ARRAY_SIZE(ad7475_st_map));
620 		val |= 0x8;
621 	}
622 
623 	mutex_lock(&data->lock);
624 
625 	data->enh_acoustics[idx] &= ~(0xf << shift);
626 	data->enh_acoustics[idx] |= (val << shift);
627 
628 	i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
629 
630 	mutex_unlock(&data->lock);
631 
632 	return count;
633 }
634 
635 /*
636  * Table of autorange values - the user will write the value in millidegrees,
637  * and we'll convert it
638  */
639 static const int autorange_table[] = {
640 	2000, 2500, 3330, 4000, 5000, 6670, 8000,
641 	10000, 13330, 16000, 20000, 26670, 32000, 40000,
642 	53330, 80000
643 };
644 
645 static ssize_t point2_show(struct device *dev, struct device_attribute *attr,
646 			   char *buf)
647 {
648 	struct adt7475_data *data = adt7475_update_device(dev);
649 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
650 	int out, val;
651 
652 	if (IS_ERR(data))
653 		return PTR_ERR(data);
654 
655 	mutex_lock(&data->lock);
656 	out = (data->range[sattr->index] >> 4) & 0x0F;
657 	val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
658 	mutex_unlock(&data->lock);
659 
660 	return sprintf(buf, "%d\n", val + autorange_table[out]);
661 }
662 
663 static ssize_t point2_store(struct device *dev, struct device_attribute *attr,
664 			    const char *buf, size_t count)
665 {
666 	struct adt7475_data *data = dev_get_drvdata(dev);
667 	struct i2c_client *client = data->client;
668 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
669 	int temp;
670 	long val;
671 
672 	if (kstrtol(buf, 10, &val))
673 		return -EINVAL;
674 
675 	mutex_lock(&data->lock);
676 
677 	/* Get a fresh copy of the needed registers */
678 	data->config5 = adt7475_read(REG_CONFIG5);
679 	data->temp[AUTOMIN][sattr->index] =
680 		adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
681 	data->range[sattr->index] =
682 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
683 
684 	/*
685 	 * The user will write an absolute value, so subtract the start point
686 	 * to figure the range
687 	 */
688 	temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
689 	val = clamp_val(val, temp + autorange_table[0],
690 		temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
691 	val -= temp;
692 
693 	/* Find the nearest table entry to what the user wrote */
694 	val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
695 
696 	data->range[sattr->index] &= ~0xF0;
697 	data->range[sattr->index] |= val << 4;
698 
699 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
700 				  data->range[sattr->index]);
701 
702 	mutex_unlock(&data->lock);
703 	return count;
704 }
705 
706 static ssize_t tach_show(struct device *dev, struct device_attribute *attr,
707 			 char *buf)
708 {
709 	struct adt7475_data *data = adt7475_update_device(dev);
710 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
711 	int out;
712 
713 	if (IS_ERR(data))
714 		return PTR_ERR(data);
715 
716 	if (sattr->nr == ALARM)
717 		out = (data->alarms >> (sattr->index + 10)) & 1;
718 	else
719 		out = tach2rpm(data->tach[sattr->nr][sattr->index]);
720 
721 	return sprintf(buf, "%d\n", out);
722 }
723 
724 static ssize_t tach_store(struct device *dev, struct device_attribute *attr,
725 			  const char *buf, size_t count)
726 {
727 
728 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
729 	struct adt7475_data *data = dev_get_drvdata(dev);
730 	struct i2c_client *client = data->client;
731 	unsigned long val;
732 
733 	if (kstrtoul(buf, 10, &val))
734 		return -EINVAL;
735 
736 	mutex_lock(&data->lock);
737 
738 	data->tach[MIN][sattr->index] = rpm2tach(val);
739 
740 	adt7475_write_word(client, TACH_MIN_REG(sattr->index),
741 			   data->tach[MIN][sattr->index]);
742 
743 	mutex_unlock(&data->lock);
744 	return count;
745 }
746 
747 static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
748 			char *buf)
749 {
750 	struct adt7475_data *data = adt7475_update_device(dev);
751 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
752 
753 	if (IS_ERR(data))
754 		return PTR_ERR(data);
755 
756 	return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
757 }
758 
759 static ssize_t pwmchan_show(struct device *dev, struct device_attribute *attr,
760 			    char *buf)
761 {
762 	struct adt7475_data *data = adt7475_update_device(dev);
763 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
764 
765 	if (IS_ERR(data))
766 		return PTR_ERR(data);
767 
768 	return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
769 }
770 
771 static ssize_t pwmctrl_show(struct device *dev, struct device_attribute *attr,
772 			    char *buf)
773 {
774 	struct adt7475_data *data = adt7475_update_device(dev);
775 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
776 
777 	if (IS_ERR(data))
778 		return PTR_ERR(data);
779 
780 	return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
781 }
782 
783 static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
784 			 const char *buf, size_t count)
785 {
786 
787 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
788 	struct adt7475_data *data = dev_get_drvdata(dev);
789 	struct i2c_client *client = data->client;
790 	unsigned char reg = 0;
791 	long val;
792 
793 	if (kstrtol(buf, 10, &val))
794 		return -EINVAL;
795 
796 	mutex_lock(&data->lock);
797 
798 	switch (sattr->nr) {
799 	case INPUT:
800 		/* Get a fresh value for CONTROL */
801 		data->pwm[CONTROL][sattr->index] =
802 			adt7475_read(PWM_CONFIG_REG(sattr->index));
803 
804 		/*
805 		 * If we are not in manual mode, then we shouldn't allow
806 		 * the user to set the pwm speed
807 		 */
808 		if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
809 			mutex_unlock(&data->lock);
810 			return count;
811 		}
812 
813 		reg = PWM_REG(sattr->index);
814 		break;
815 
816 	case MIN:
817 		reg = PWM_MIN_REG(sattr->index);
818 		break;
819 
820 	case MAX:
821 		reg = PWM_MAX_REG(sattr->index);
822 		break;
823 	}
824 
825 	data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
826 	i2c_smbus_write_byte_data(client, reg,
827 				  data->pwm[sattr->nr][sattr->index]);
828 	mutex_unlock(&data->lock);
829 
830 	return count;
831 }
832 
833 static ssize_t stall_disable_show(struct device *dev,
834 				  struct device_attribute *attr, char *buf)
835 {
836 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
837 	struct adt7475_data *data = dev_get_drvdata(dev);
838 
839 	u8 mask = BIT(5 + sattr->index);
840 
841 	return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
842 }
843 
844 static ssize_t stall_disable_store(struct device *dev,
845 				   struct device_attribute *attr,
846 				   const char *buf, size_t count)
847 {
848 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
849 	struct adt7475_data *data = dev_get_drvdata(dev);
850 	struct i2c_client *client = data->client;
851 	long val;
852 	u8 mask = BIT(5 + sattr->index);
853 
854 	if (kstrtol(buf, 10, &val))
855 		return -EINVAL;
856 
857 	mutex_lock(&data->lock);
858 
859 	data->enh_acoustics[0] &= ~mask;
860 	if (val)
861 		data->enh_acoustics[0] |= mask;
862 
863 	i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
864 				  data->enh_acoustics[0]);
865 
866 	mutex_unlock(&data->lock);
867 
868 	return count;
869 }
870 
871 /* Called by set_pwmctrl and set_pwmchan */
872 
873 static int hw_set_pwm(struct i2c_client *client, int index,
874 		      unsigned int pwmctl, unsigned int pwmchan)
875 {
876 	struct adt7475_data *data = i2c_get_clientdata(client);
877 	long val = 0;
878 
879 	switch (pwmctl) {
880 	case 0:
881 		val = 0x03;	/* Run at full speed */
882 		break;
883 	case 1:
884 		val = 0x07;	/* Manual mode */
885 		break;
886 	case 2:
887 		switch (pwmchan) {
888 		case 1:
889 			/* Remote1 controls PWM */
890 			val = 0x00;
891 			break;
892 		case 2:
893 			/* local controls PWM */
894 			val = 0x01;
895 			break;
896 		case 4:
897 			/* remote2 controls PWM */
898 			val = 0x02;
899 			break;
900 		case 6:
901 			/* local/remote2 control PWM */
902 			val = 0x05;
903 			break;
904 		case 7:
905 			/* All three control PWM */
906 			val = 0x06;
907 			break;
908 		default:
909 			return -EINVAL;
910 		}
911 		break;
912 	default:
913 		return -EINVAL;
914 	}
915 
916 	data->pwmctl[index] = pwmctl;
917 	data->pwmchan[index] = pwmchan;
918 
919 	data->pwm[CONTROL][index] &= ~0xE0;
920 	data->pwm[CONTROL][index] |= (val & 7) << 5;
921 
922 	i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
923 				  data->pwm[CONTROL][index]);
924 
925 	return 0;
926 }
927 
928 static ssize_t pwmchan_store(struct device *dev,
929 			     struct device_attribute *attr, const char *buf,
930 			     size_t count)
931 {
932 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
933 	struct adt7475_data *data = dev_get_drvdata(dev);
934 	struct i2c_client *client = data->client;
935 	int r;
936 	long val;
937 
938 	if (kstrtol(buf, 10, &val))
939 		return -EINVAL;
940 
941 	mutex_lock(&data->lock);
942 	/* Read Modify Write PWM values */
943 	adt7475_read_pwm(client, sattr->index);
944 	r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
945 	if (r)
946 		count = r;
947 	mutex_unlock(&data->lock);
948 
949 	return count;
950 }
951 
952 static ssize_t pwmctrl_store(struct device *dev,
953 			     struct device_attribute *attr, const char *buf,
954 			     size_t count)
955 {
956 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
957 	struct adt7475_data *data = dev_get_drvdata(dev);
958 	struct i2c_client *client = data->client;
959 	int r;
960 	long val;
961 
962 	if (kstrtol(buf, 10, &val))
963 		return -EINVAL;
964 
965 	mutex_lock(&data->lock);
966 	/* Read Modify Write PWM values */
967 	adt7475_read_pwm(client, sattr->index);
968 	r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
969 	if (r)
970 		count = r;
971 	mutex_unlock(&data->lock);
972 
973 	return count;
974 }
975 
976 /* List of frequencies for the PWM */
977 static const int pwmfreq_table[] = {
978 	11, 14, 22, 29, 35, 44, 58, 88, 22500
979 };
980 
981 static ssize_t pwmfreq_show(struct device *dev, struct device_attribute *attr,
982 			    char *buf)
983 {
984 	struct adt7475_data *data = adt7475_update_device(dev);
985 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
986 	int idx;
987 
988 	if (IS_ERR(data))
989 		return PTR_ERR(data);
990 	idx = clamp_val(data->range[sattr->index] & 0xf, 0,
991 			ARRAY_SIZE(pwmfreq_table) - 1);
992 
993 	return sprintf(buf, "%d\n", pwmfreq_table[idx]);
994 }
995 
996 static ssize_t pwmfreq_store(struct device *dev,
997 			     struct device_attribute *attr, const char *buf,
998 			     size_t count)
999 {
1000 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1001 	struct adt7475_data *data = dev_get_drvdata(dev);
1002 	struct i2c_client *client = data->client;
1003 	int out;
1004 	long val;
1005 
1006 	if (kstrtol(buf, 10, &val))
1007 		return -EINVAL;
1008 
1009 	out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
1010 
1011 	mutex_lock(&data->lock);
1012 
1013 	data->range[sattr->index] =
1014 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
1015 	data->range[sattr->index] &= ~0xf;
1016 	data->range[sattr->index] |= out;
1017 
1018 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
1019 				  data->range[sattr->index]);
1020 
1021 	mutex_unlock(&data->lock);
1022 	return count;
1023 }
1024 
1025 static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
1026 					struct device_attribute *devattr,
1027 					char *buf)
1028 {
1029 	struct adt7475_data *data = adt7475_update_device(dev);
1030 
1031 	if (IS_ERR(data))
1032 		return PTR_ERR(data);
1033 
1034 	return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1035 }
1036 
1037 static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1038 					struct device_attribute *devattr,
1039 					const char *buf, size_t count)
1040 {
1041 	struct adt7475_data *data = dev_get_drvdata(dev);
1042 	struct i2c_client *client = data->client;
1043 	long val;
1044 
1045 	if (kstrtol(buf, 10, &val))
1046 		return -EINVAL;
1047 	if (val != 0 && val != 1)
1048 		return -EINVAL;
1049 
1050 	mutex_lock(&data->lock);
1051 	data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1052 	if (val)
1053 		data->config4 |= CONFIG4_MAXDUTY;
1054 	else
1055 		data->config4 &= ~CONFIG4_MAXDUTY;
1056 	i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1057 	mutex_unlock(&data->lock);
1058 
1059 	return count;
1060 }
1061 
1062 static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
1063 			char *buf)
1064 {
1065 	struct adt7475_data *data = dev_get_drvdata(dev);
1066 	return sprintf(buf, "%d\n", (int)data->vrm);
1067 }
1068 
1069 static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
1070 			 const char *buf, size_t count)
1071 {
1072 	struct adt7475_data *data = dev_get_drvdata(dev);
1073 	long val;
1074 
1075 	if (kstrtol(buf, 10, &val))
1076 		return -EINVAL;
1077 	if (val < 0 || val > 255)
1078 		return -EINVAL;
1079 	data->vrm = val;
1080 
1081 	return count;
1082 }
1083 
1084 static ssize_t cpu0_vid_show(struct device *dev,
1085 			     struct device_attribute *devattr, char *buf)
1086 {
1087 	struct adt7475_data *data = adt7475_update_device(dev);
1088 
1089 	if (IS_ERR(data))
1090 		return PTR_ERR(data);
1091 
1092 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
1093 }
1094 
1095 static SENSOR_DEVICE_ATTR_2_RO(in0_input, voltage, INPUT, 0);
1096 static SENSOR_DEVICE_ATTR_2_RW(in0_max, voltage, MAX, 0);
1097 static SENSOR_DEVICE_ATTR_2_RW(in0_min, voltage, MIN, 0);
1098 static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, voltage, ALARM, 0);
1099 static SENSOR_DEVICE_ATTR_2_RO(in1_input, voltage, INPUT, 1);
1100 static SENSOR_DEVICE_ATTR_2_RW(in1_max, voltage, MAX, 1);
1101 static SENSOR_DEVICE_ATTR_2_RW(in1_min, voltage, MIN, 1);
1102 static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, voltage, ALARM, 1);
1103 static SENSOR_DEVICE_ATTR_2_RO(in2_input, voltage, INPUT, 2);
1104 static SENSOR_DEVICE_ATTR_2_RW(in2_max, voltage, MAX, 2);
1105 static SENSOR_DEVICE_ATTR_2_RW(in2_min, voltage, MIN, 2);
1106 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, voltage, ALARM, 2);
1107 static SENSOR_DEVICE_ATTR_2_RO(in3_input, voltage, INPUT, 3);
1108 static SENSOR_DEVICE_ATTR_2_RW(in3_max, voltage, MAX, 3);
1109 static SENSOR_DEVICE_ATTR_2_RW(in3_min, voltage, MIN, 3);
1110 static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, voltage, ALARM, 3);
1111 static SENSOR_DEVICE_ATTR_2_RO(in4_input, voltage, INPUT, 4);
1112 static SENSOR_DEVICE_ATTR_2_RW(in4_max, voltage, MAX, 4);
1113 static SENSOR_DEVICE_ATTR_2_RW(in4_min, voltage, MIN, 4);
1114 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, voltage, ALARM, 8);
1115 static SENSOR_DEVICE_ATTR_2_RO(in5_input, voltage, INPUT, 5);
1116 static SENSOR_DEVICE_ATTR_2_RW(in5_max, voltage, MAX, 5);
1117 static SENSOR_DEVICE_ATTR_2_RW(in5_min, voltage, MIN, 5);
1118 static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, voltage, ALARM, 31);
1119 static SENSOR_DEVICE_ATTR_2_RO(in6_input, voltage, INPUT, 6);
1120 static SENSOR_DEVICE_ATTR_2_RW(in6_max, voltage, MAX, 6);
1121 static SENSOR_DEVICE_ATTR_2_RW(in6_min, voltage, MIN, 6);
1122 static SENSOR_DEVICE_ATTR_2_RO(in6_alarm, voltage, ALARM, 30);
1123 static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, INPUT, 0);
1124 static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, temp, ALARM, 0);
1125 static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, temp, FAULT, 0);
1126 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, MAX, 0);
1127 static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, MIN, 0);
1128 static SENSOR_DEVICE_ATTR_2_RW(temp1_offset, temp, OFFSET, 0);
1129 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp, temp, AUTOMIN, 0);
1130 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, point2, 0, 0);
1131 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, THERM, 0);
1132 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst, temp, HYSTERSIS, 0);
1133 static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing, temp_st, 0, 0);
1134 static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, INPUT, 1);
1135 static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm, temp, ALARM, 1);
1136 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, MAX, 1);
1137 static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, MIN, 1);
1138 static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, OFFSET, 1);
1139 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp, AUTOMIN, 1);
1140 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, point2, 0, 1);
1141 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, THERM, 1);
1142 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst, temp, HYSTERSIS, 1);
1143 static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing, temp_st, 0, 1);
1144 static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, INPUT, 2);
1145 static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm, temp, ALARM, 2);
1146 static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, temp, FAULT, 2);
1147 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, MAX, 2);
1148 static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, MIN, 2);
1149 static SENSOR_DEVICE_ATTR_2_RW(temp3_offset, temp, OFFSET, 2);
1150 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp, temp, AUTOMIN, 2);
1151 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp, point2, 0, 2);
1152 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, THERM, 2);
1153 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst, temp, HYSTERSIS, 2);
1154 static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing, temp_st, 0, 2);
1155 static SENSOR_DEVICE_ATTR_2_RO(fan1_input, tach, INPUT, 0);
1156 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, tach, MIN, 0);
1157 static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, tach, ALARM, 0);
1158 static SENSOR_DEVICE_ATTR_2_RO(fan2_input, tach, INPUT, 1);
1159 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, tach, MIN, 1);
1160 static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, tach, ALARM, 1);
1161 static SENSOR_DEVICE_ATTR_2_RO(fan3_input, tach, INPUT, 2);
1162 static SENSOR_DEVICE_ATTR_2_RW(fan3_min, tach, MIN, 2);
1163 static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, tach, ALARM, 2);
1164 static SENSOR_DEVICE_ATTR_2_RO(fan4_input, tach, INPUT, 3);
1165 static SENSOR_DEVICE_ATTR_2_RW(fan4_min, tach, MIN, 3);
1166 static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm, tach, ALARM, 3);
1167 static SENSOR_DEVICE_ATTR_2_RW(pwm1, pwm, INPUT, 0);
1168 static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq, pwmfreq, INPUT, 0);
1169 static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable, pwmctrl, INPUT, 0);
1170 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp, pwmchan, INPUT, 0);
1171 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, pwm, MIN, 0);
1172 static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, pwm, MAX, 0);
1173 static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable, stall_disable, 0, 0);
1174 static SENSOR_DEVICE_ATTR_2_RW(pwm2, pwm, INPUT, 1);
1175 static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq, pwmfreq, INPUT, 1);
1176 static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable, pwmctrl, INPUT, 1);
1177 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp, pwmchan, INPUT, 1);
1178 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, pwm, MIN, 1);
1179 static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, pwm, MAX, 1);
1180 static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable, stall_disable, 0, 1);
1181 static SENSOR_DEVICE_ATTR_2_RW(pwm3, pwm, INPUT, 2);
1182 static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq, pwmfreq, INPUT, 2);
1183 static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable, pwmctrl, INPUT, 2);
1184 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp, pwmchan, INPUT, 2);
1185 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, pwm, MIN, 2);
1186 static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, pwm, MAX, 2);
1187 static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable, stall_disable, 0, 2);
1188 
1189 /* Non-standard name, might need revisiting */
1190 static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1191 
1192 static DEVICE_ATTR_RW(vrm);
1193 static DEVICE_ATTR_RO(cpu0_vid);
1194 
1195 static struct attribute *adt7475_attrs[] = {
1196 	&sensor_dev_attr_in1_input.dev_attr.attr,
1197 	&sensor_dev_attr_in1_max.dev_attr.attr,
1198 	&sensor_dev_attr_in1_min.dev_attr.attr,
1199 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
1200 	&sensor_dev_attr_in2_input.dev_attr.attr,
1201 	&sensor_dev_attr_in2_max.dev_attr.attr,
1202 	&sensor_dev_attr_in2_min.dev_attr.attr,
1203 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
1204 	&sensor_dev_attr_temp1_input.dev_attr.attr,
1205 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1206 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
1207 	&sensor_dev_attr_temp1_max.dev_attr.attr,
1208 	&sensor_dev_attr_temp1_min.dev_attr.attr,
1209 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
1210 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1211 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1212 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
1213 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
1214 	&sensor_dev_attr_temp1_smoothing.dev_attr.attr,
1215 	&sensor_dev_attr_temp2_input.dev_attr.attr,
1216 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1217 	&sensor_dev_attr_temp2_max.dev_attr.attr,
1218 	&sensor_dev_attr_temp2_min.dev_attr.attr,
1219 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
1220 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1221 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1222 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
1223 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
1224 	&sensor_dev_attr_temp2_smoothing.dev_attr.attr,
1225 	&sensor_dev_attr_temp3_input.dev_attr.attr,
1226 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
1227 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1228 	&sensor_dev_attr_temp3_max.dev_attr.attr,
1229 	&sensor_dev_attr_temp3_min.dev_attr.attr,
1230 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
1231 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1232 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1233 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
1234 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
1235 	&sensor_dev_attr_temp3_smoothing.dev_attr.attr,
1236 	&sensor_dev_attr_fan1_input.dev_attr.attr,
1237 	&sensor_dev_attr_fan1_min.dev_attr.attr,
1238 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1239 	&sensor_dev_attr_fan2_input.dev_attr.attr,
1240 	&sensor_dev_attr_fan2_min.dev_attr.attr,
1241 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1242 	&sensor_dev_attr_fan3_input.dev_attr.attr,
1243 	&sensor_dev_attr_fan3_min.dev_attr.attr,
1244 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1245 	&sensor_dev_attr_pwm1.dev_attr.attr,
1246 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
1247 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
1248 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1249 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1250 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1251 	&sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
1252 	&sensor_dev_attr_pwm3.dev_attr.attr,
1253 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
1254 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
1255 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1256 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1257 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1258 	&sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
1259 	&dev_attr_pwm_use_point2_pwm_at_crit.attr,
1260 	NULL,
1261 };
1262 
1263 static struct attribute *fan4_attrs[] = {
1264 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1265 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1266 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1267 	NULL
1268 };
1269 
1270 static struct attribute *pwm2_attrs[] = {
1271 	&sensor_dev_attr_pwm2.dev_attr.attr,
1272 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1273 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1274 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1275 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1276 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1277 	&sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
1278 	NULL
1279 };
1280 
1281 static struct attribute *in0_attrs[] = {
1282 	&sensor_dev_attr_in0_input.dev_attr.attr,
1283 	&sensor_dev_attr_in0_max.dev_attr.attr,
1284 	&sensor_dev_attr_in0_min.dev_attr.attr,
1285 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1286 	NULL
1287 };
1288 
1289 static struct attribute *in3_attrs[] = {
1290 	&sensor_dev_attr_in3_input.dev_attr.attr,
1291 	&sensor_dev_attr_in3_max.dev_attr.attr,
1292 	&sensor_dev_attr_in3_min.dev_attr.attr,
1293 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1294 	NULL
1295 };
1296 
1297 static struct attribute *in4_attrs[] = {
1298 	&sensor_dev_attr_in4_input.dev_attr.attr,
1299 	&sensor_dev_attr_in4_max.dev_attr.attr,
1300 	&sensor_dev_attr_in4_min.dev_attr.attr,
1301 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1302 	NULL
1303 };
1304 
1305 static struct attribute *in5_attrs[] = {
1306 	&sensor_dev_attr_in5_input.dev_attr.attr,
1307 	&sensor_dev_attr_in5_max.dev_attr.attr,
1308 	&sensor_dev_attr_in5_min.dev_attr.attr,
1309 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
1310 	NULL
1311 };
1312 
1313 static struct attribute *in6_attrs[] = {
1314 	&sensor_dev_attr_in6_input.dev_attr.attr,
1315 	&sensor_dev_attr_in6_max.dev_attr.attr,
1316 	&sensor_dev_attr_in6_min.dev_attr.attr,
1317 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
1318 	NULL
1319 };
1320 
1321 static struct attribute *vid_attrs[] = {
1322 	&dev_attr_cpu0_vid.attr,
1323 	&dev_attr_vrm.attr,
1324 	NULL
1325 };
1326 
1327 static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1328 static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1329 static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1330 static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1331 static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1332 static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1333 static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1334 static const struct attribute_group in6_attr_group = { .attrs = in6_attrs };
1335 static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
1336 
1337 static int adt7475_detect(struct i2c_client *client,
1338 			  struct i2c_board_info *info)
1339 {
1340 	struct i2c_adapter *adapter = client->adapter;
1341 	int vendid, devid, devid2;
1342 	const char *name;
1343 
1344 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1345 		return -ENODEV;
1346 
1347 	vendid = adt7475_read(REG_VENDID);
1348 	devid2 = adt7475_read(REG_DEVID2);
1349 	if (vendid != 0x41 ||		/* Analog Devices */
1350 	    (devid2 & 0xf8) != 0x68)
1351 		return -ENODEV;
1352 
1353 	devid = adt7475_read(REG_DEVID);
1354 	if (devid == 0x73)
1355 		name = "adt7473";
1356 	else if (devid == 0x75 && client->addr == 0x2e)
1357 		name = "adt7475";
1358 	else if (devid == 0x76)
1359 		name = "adt7476";
1360 	else if ((devid2 & 0xfc) == 0x6c)
1361 		name = "adt7490";
1362 	else {
1363 		dev_dbg(&adapter->dev,
1364 			"Couldn't detect an ADT7473/75/76/90 part at "
1365 			"0x%02x\n", (unsigned int)client->addr);
1366 		return -ENODEV;
1367 	}
1368 
1369 	strscpy(info->type, name, I2C_NAME_SIZE);
1370 
1371 	return 0;
1372 }
1373 
1374 static int adt7475_update_limits(struct i2c_client *client)
1375 {
1376 	struct adt7475_data *data = i2c_get_clientdata(client);
1377 	int i;
1378 	int ret;
1379 
1380 	ret = adt7475_read(REG_CONFIG4);
1381 	if (ret < 0)
1382 		return ret;
1383 	data->config4 = ret;
1384 
1385 	ret = adt7475_read(REG_CONFIG5);
1386 	if (ret < 0)
1387 		return ret;
1388 	data->config5 = ret;
1389 
1390 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1391 		if (!(data->has_voltage & (1 << i)))
1392 			continue;
1393 		/* Adjust values so they match the input precision */
1394 		ret = adt7475_read(VOLTAGE_MIN_REG(i));
1395 		if (ret < 0)
1396 			return ret;
1397 		data->voltage[MIN][i] = ret << 2;
1398 
1399 		ret = adt7475_read(VOLTAGE_MAX_REG(i));
1400 		if (ret < 0)
1401 			return ret;
1402 		data->voltage[MAX][i] = ret << 2;
1403 	}
1404 
1405 	if (data->has_voltage & (1 << 5)) {
1406 		ret = adt7475_read(REG_VTT_MIN);
1407 		if (ret < 0)
1408 			return ret;
1409 		data->voltage[MIN][5] = ret << 2;
1410 
1411 		ret = adt7475_read(REG_VTT_MAX);
1412 		if (ret < 0)
1413 			return ret;
1414 		data->voltage[MAX][5] = ret << 2;
1415 	}
1416 
1417 	if (data->has_voltage & (1 << 6)) {
1418 		ret = adt7475_read(REG_IMON_MIN);
1419 		if (ret < 0)
1420 			return ret;
1421 		data->voltage[MIN][6] = ret << 2;
1422 
1423 		ret = adt7475_read(REG_IMON_MAX);
1424 		if (ret < 0)
1425 			return ret;
1426 		data->voltage[MAX][6] = ret << 2;
1427 	}
1428 
1429 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1430 		/* Adjust values so they match the input precision */
1431 		ret = adt7475_read(TEMP_MIN_REG(i));
1432 		if (ret < 0)
1433 			return ret;
1434 		data->temp[MIN][i] = ret << 2;
1435 
1436 		ret = adt7475_read(TEMP_MAX_REG(i));
1437 		if (ret < 0)
1438 			return ret;
1439 		data->temp[MAX][i] = ret << 2;
1440 
1441 		ret = adt7475_read(TEMP_TMIN_REG(i));
1442 		if (ret < 0)
1443 			return ret;
1444 		data->temp[AUTOMIN][i] = ret << 2;
1445 
1446 		ret = adt7475_read(TEMP_THERM_REG(i));
1447 		if (ret < 0)
1448 			return ret;
1449 		data->temp[THERM][i] = ret << 2;
1450 
1451 		ret = adt7475_read(TEMP_OFFSET_REG(i));
1452 		if (ret < 0)
1453 			return ret;
1454 		data->temp[OFFSET][i] = ret;
1455 	}
1456 	adt7475_read_hystersis(client);
1457 
1458 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1459 		if (i == 3 && !data->has_fan4)
1460 			continue;
1461 		ret = adt7475_read_word(client, TACH_MIN_REG(i));
1462 		if (ret < 0)
1463 			return ret;
1464 		data->tach[MIN][i] = ret;
1465 	}
1466 
1467 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1468 		if (i == 1 && !data->has_pwm2)
1469 			continue;
1470 		ret = adt7475_read(PWM_MAX_REG(i));
1471 		if (ret < 0)
1472 			return ret;
1473 		data->pwm[MAX][i] = ret;
1474 
1475 		ret = adt7475_read(PWM_MIN_REG(i));
1476 		if (ret < 0)
1477 			return ret;
1478 		data->pwm[MIN][i] = ret;
1479 		/* Set the channel and control information */
1480 		adt7475_read_pwm(client, i);
1481 	}
1482 
1483 	ret = adt7475_read(TEMP_TRANGE_REG(0));
1484 	if (ret < 0)
1485 		return ret;
1486 	data->range[0] = ret;
1487 
1488 	ret = adt7475_read(TEMP_TRANGE_REG(1));
1489 	if (ret < 0)
1490 		return ret;
1491 	data->range[1] = ret;
1492 
1493 	ret = adt7475_read(TEMP_TRANGE_REG(2));
1494 	if (ret < 0)
1495 		return ret;
1496 	data->range[2] = ret;
1497 
1498 	return 0;
1499 }
1500 
1501 static int load_config3(const struct i2c_client *client, const char *propname)
1502 {
1503 	const char *function;
1504 	u8 config3;
1505 	int ret;
1506 
1507 	ret = device_property_read_string(&client->dev, propname, &function);
1508 	if (!ret) {
1509 		ret = adt7475_read(REG_CONFIG3);
1510 		if (ret < 0)
1511 			return ret;
1512 
1513 		config3 = ret & ~CONFIG3_SMBALERT;
1514 		if (!strcmp("pwm2", function))
1515 			;
1516 		else if (!strcmp("smbalert#", function))
1517 			config3 |= CONFIG3_SMBALERT;
1518 		else
1519 			return -EINVAL;
1520 
1521 		return i2c_smbus_write_byte_data(client, REG_CONFIG3, config3);
1522 	}
1523 
1524 	return 0;
1525 }
1526 
1527 static int load_config4(const struct i2c_client *client, const char *propname)
1528 {
1529 	const char *function;
1530 	u8 config4;
1531 	int ret;
1532 
1533 	ret = device_property_read_string(&client->dev, propname, &function);
1534 	if (!ret) {
1535 		ret = adt7475_read(REG_CONFIG4);
1536 		if (ret < 0)
1537 			return ret;
1538 
1539 		config4 = ret & ~CONFIG4_PINFUNC;
1540 
1541 		if (!strcmp("tach4", function))
1542 			;
1543 		else if (!strcmp("therm#", function))
1544 			config4 |= CONFIG4_THERM;
1545 		else if (!strcmp("smbalert#", function))
1546 			config4 |= CONFIG4_SMBALERT;
1547 		else if (!strcmp("gpio", function))
1548 			config4 |= CONFIG4_PINFUNC;
1549 		else
1550 			return -EINVAL;
1551 
1552 		return i2c_smbus_write_byte_data(client, REG_CONFIG4, config4);
1553 	}
1554 
1555 	return 0;
1556 }
1557 
1558 static int load_config(const struct i2c_client *client, enum chips chip)
1559 {
1560 	int err;
1561 	const char *prop1, *prop2;
1562 
1563 	switch (chip) {
1564 	case adt7473:
1565 	case adt7475:
1566 		prop1 = "adi,pin5-function";
1567 		prop2 = "adi,pin9-function";
1568 		break;
1569 	case adt7476:
1570 	case adt7490:
1571 		prop1 = "adi,pin10-function";
1572 		prop2 = "adi,pin14-function";
1573 		break;
1574 	}
1575 
1576 	err = load_config3(client, prop1);
1577 	if (err) {
1578 		dev_err(&client->dev, "failed to configure %s\n", prop1);
1579 		return err;
1580 	}
1581 
1582 	err = load_config4(client, prop2);
1583 	if (err) {
1584 		dev_err(&client->dev, "failed to configure %s\n", prop2);
1585 		return err;
1586 	}
1587 
1588 	return 0;
1589 }
1590 
1591 static int set_property_bit(const struct i2c_client *client, char *property,
1592 			    u8 *config, u8 bit_index)
1593 {
1594 	u32 prop_value = 0;
1595 	int ret = device_property_read_u32(&client->dev, property,
1596 					   &prop_value);
1597 
1598 	if (!ret) {
1599 		if (prop_value)
1600 			*config |= (1 << bit_index);
1601 		else
1602 			*config &= ~(1 << bit_index);
1603 	}
1604 
1605 	return ret;
1606 }
1607 
1608 static int load_attenuators(const struct i2c_client *client, enum chips chip,
1609 			    struct adt7475_data *data)
1610 {
1611 	switch (chip) {
1612 	case adt7476:
1613 	case adt7490:
1614 		set_property_bit(client, "adi,bypass-attenuator-in0",
1615 				 &data->config4, 4);
1616 		set_property_bit(client, "adi,bypass-attenuator-in1",
1617 				 &data->config4, 5);
1618 		set_property_bit(client, "adi,bypass-attenuator-in3",
1619 				 &data->config4, 6);
1620 		set_property_bit(client, "adi,bypass-attenuator-in4",
1621 				 &data->config4, 7);
1622 
1623 		return i2c_smbus_write_byte_data(client, REG_CONFIG4,
1624 						 data->config4);
1625 	case adt7473:
1626 	case adt7475:
1627 		set_property_bit(client, "adi,bypass-attenuator-in1",
1628 				 &data->config2, 5);
1629 
1630 		return i2c_smbus_write_byte_data(client, REG_CONFIG2,
1631 						 data->config2);
1632 	}
1633 
1634 	return 0;
1635 }
1636 
1637 static int adt7475_set_pwm_polarity(struct i2c_client *client)
1638 {
1639 	u32 states[ADT7475_PWM_COUNT];
1640 	int ret, i;
1641 	u8 val;
1642 
1643 	ret = device_property_read_u32_array(&client->dev,
1644 					     "adi,pwm-active-state", states,
1645 					     ARRAY_SIZE(states));
1646 	if (ret)
1647 		return ret;
1648 
1649 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1650 		ret = adt7475_read(PWM_CONFIG_REG(i));
1651 		if (ret < 0)
1652 			return ret;
1653 		val = ret;
1654 		if (states[i])
1655 			val &= ~BIT(4);
1656 		else
1657 			val |= BIT(4);
1658 
1659 		ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(i), val);
1660 		if (ret)
1661 			return ret;
1662 	}
1663 
1664 	return 0;
1665 }
1666 
1667 struct adt7475_pwm_config {
1668 	int index;
1669 	int freq;
1670 	int flags;
1671 	int duty;
1672 };
1673 
1674 static int _adt7475_pwm_properties_parse_args(u32 args[4], struct adt7475_pwm_config *cfg)
1675 {
1676 	int freq_hz;
1677 	int duty;
1678 
1679 	if (args[1] == 0)
1680 		return -EINVAL;
1681 
1682 	freq_hz = 1000000000UL / args[1];
1683 	if (args[3] >= args[1])
1684 		duty = 255;
1685 	else
1686 		duty = div_u64(255ULL * args[3], args[1]);
1687 
1688 	cfg->index = args[0];
1689 	cfg->freq = find_closest(freq_hz, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
1690 	cfg->flags = args[2];
1691 	cfg->duty = duty;
1692 
1693 	return 0;
1694 }
1695 
1696 static int adt7475_pwm_properties_parse_reference_args(struct fwnode_handle *fwnode,
1697 						       struct adt7475_pwm_config *cfg)
1698 {
1699 	int ret, i;
1700 	struct fwnode_reference_args rargs = {};
1701 	u32 args[4] = {};
1702 
1703 	ret = fwnode_property_get_reference_args(fwnode, "pwms", "#pwm-cells", 0, 0, &rargs);
1704 	if (ret)
1705 		return ret;
1706 
1707 	if (rargs.nargs != 4) {
1708 		fwnode_handle_put(rargs.fwnode);
1709 		return -EINVAL;
1710 	}
1711 
1712 	for (i = 0; i < 4; i++)
1713 		args[i] = rargs.args[i];
1714 
1715 	ret = _adt7475_pwm_properties_parse_args(args, cfg);
1716 
1717 	fwnode_handle_put(rargs.fwnode);
1718 
1719 	return ret;
1720 }
1721 
1722 static int adt7475_pwm_properties_parse_args(struct fwnode_handle *fwnode,
1723 					     struct adt7475_pwm_config *cfg)
1724 {
1725 	int ret;
1726 	u32 args[4] = {};
1727 
1728 	ret = fwnode_property_read_u32_array(fwnode, "pwms", args, ARRAY_SIZE(args));
1729 	if (ret)
1730 		return ret;
1731 
1732 	return _adt7475_pwm_properties_parse_args(args, cfg);
1733 }
1734 
1735 static int adt7475_fan_pwm_config(struct i2c_client *client)
1736 {
1737 	struct adt7475_data *data = i2c_get_clientdata(client);
1738 	struct adt7475_pwm_config cfg = {};
1739 	int ret;
1740 
1741 	device_for_each_child_node_scoped(&client->dev, child) {
1742 		if (!fwnode_property_present(child, "pwms"))
1743 			continue;
1744 
1745 		if (is_of_node(child))
1746 			ret = adt7475_pwm_properties_parse_reference_args(child, &cfg);
1747 		else
1748 			ret = adt7475_pwm_properties_parse_args(child, &cfg);
1749 
1750 		if (cfg.index >= ADT7475_PWM_COUNT)
1751 			return -EINVAL;
1752 
1753 		ret = adt7475_read(PWM_CONFIG_REG(cfg.index));
1754 		if (ret < 0)
1755 			return ret;
1756 		data->pwm[CONTROL][cfg.index] = ret;
1757 		if (cfg.flags & PWM_POLARITY_INVERTED)
1758 			data->pwm[CONTROL][cfg.index] |= BIT(4);
1759 		else
1760 			data->pwm[CONTROL][cfg.index] &= ~BIT(4);
1761 
1762 		/* Force to manual mode so PWM values take effect */
1763 		data->pwm[CONTROL][cfg.index] &= ~0xE0;
1764 		data->pwm[CONTROL][cfg.index] |= 0x07 << 5;
1765 
1766 		ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(cfg.index),
1767 						data->pwm[CONTROL][cfg.index]);
1768 		if (ret)
1769 			return ret;
1770 
1771 		data->pwm[INPUT][cfg.index] = cfg.duty;
1772 		ret = i2c_smbus_write_byte_data(client, PWM_REG(cfg.index),
1773 						data->pwm[INPUT][cfg.index]);
1774 		if (ret)
1775 			return ret;
1776 
1777 		data->range[cfg.index] = adt7475_read(TEMP_TRANGE_REG(cfg.index));
1778 		data->range[cfg.index] &= ~0xf;
1779 		data->range[cfg.index] |= cfg.freq;
1780 
1781 		ret = i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(cfg.index),
1782 						data->range[cfg.index]);
1783 		if (ret)
1784 			return ret;
1785 	}
1786 
1787 	return 0;
1788 }
1789 
1790 static int adt7475_probe(struct i2c_client *client)
1791 {
1792 	enum chips chip;
1793 	static const char * const names[] = {
1794 		[adt7473] = "ADT7473",
1795 		[adt7475] = "ADT7475",
1796 		[adt7476] = "ADT7476",
1797 		[adt7490] = "ADT7490",
1798 	};
1799 
1800 	struct adt7475_data *data;
1801 	struct device *hwmon_dev;
1802 	int i, ret = 0, revision, group_num = 0;
1803 	u8 config3;
1804 
1805 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1806 	if (data == NULL)
1807 		return -ENOMEM;
1808 
1809 	mutex_init(&data->lock);
1810 	data->client = client;
1811 	i2c_set_clientdata(client, data);
1812 
1813 	chip = (uintptr_t)i2c_get_match_data(client);
1814 
1815 	/* Initialize device-specific values */
1816 	switch (chip) {
1817 	case adt7476:
1818 		data->has_voltage = 0x0e;	/* in1 to in3 */
1819 		revision = adt7475_read(REG_DEVID2) & 0x07;
1820 		break;
1821 	case adt7490:
1822 		data->has_voltage = 0x7e;	/* in1 to in6 */
1823 		revision = adt7475_read(REG_DEVID2) & 0x03;
1824 		if (revision == 0x03)
1825 			revision += adt7475_read(REG_DEVREV2);
1826 		break;
1827 	default:
1828 		data->has_voltage = 0x06;	/* in1, in2 */
1829 		revision = adt7475_read(REG_DEVID2) & 0x07;
1830 	}
1831 
1832 	ret = load_config(client, chip);
1833 	if (ret)
1834 		return ret;
1835 
1836 	config3 = adt7475_read(REG_CONFIG3);
1837 	/* Pin PWM2 may alternatively be used for ALERT output */
1838 	if (!(config3 & CONFIG3_SMBALERT))
1839 		data->has_pwm2 = 1;
1840 	/* Meaning of this bit is inverted for the ADT7473-1 */
1841 	if (chip == adt7473 && revision >= 1)
1842 		data->has_pwm2 = !data->has_pwm2;
1843 
1844 	data->config4 = adt7475_read(REG_CONFIG4);
1845 	/* Pin TACH4 may alternatively be used for THERM */
1846 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1847 		data->has_fan4 = 1;
1848 
1849 	/*
1850 	 * THERM configuration is more complex on the ADT7476 and ADT7490,
1851 	 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1852 	 * this function
1853 	 */
1854 	if (chip == adt7490) {
1855 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1856 		    !(config3 & CONFIG3_THERM))
1857 			data->has_fan4 = 1;
1858 	}
1859 	if (chip == adt7476 || chip == adt7490) {
1860 		if (!(config3 & CONFIG3_THERM) ||
1861 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1862 			data->has_voltage |= (1 << 0);		/* in0 */
1863 	}
1864 
1865 	/*
1866 	 * On the ADT7476, the +12V input pin may instead be used as VID5,
1867 	 * and VID pins may alternatively be used as GPIO
1868 	 */
1869 	if (chip == adt7476) {
1870 		u8 vid = adt7475_read(REG_VID);
1871 		if (!(vid & VID_VIDSEL))
1872 			data->has_voltage |= (1 << 4);		/* in4 */
1873 
1874 		data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1875 	}
1876 
1877 	/* Voltage attenuators can be bypassed, globally or individually */
1878 	data->config2 = adt7475_read(REG_CONFIG2);
1879 	ret = load_attenuators(client, chip, data);
1880 	if (ret)
1881 		dev_warn(&client->dev, "Error configuring attenuator bypass\n");
1882 
1883 	if (data->config2 & CONFIG2_ATTN) {
1884 		data->bypass_attn = (0x3 << 3) | 0x3;
1885 	} else {
1886 		data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1887 				    ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1888 	}
1889 	data->bypass_attn &= data->has_voltage;
1890 
1891 	/*
1892 	 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1893 	 * pwm's which are disabled to manual mode with 0% duty cycle
1894 	 */
1895 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
1896 		adt7475_read_pwm(client, i);
1897 
1898 	ret = adt7475_set_pwm_polarity(client);
1899 	if (ret && ret != -EINVAL)
1900 		dev_warn(&client->dev, "Error configuring pwm polarity\n");
1901 
1902 	ret = adt7475_fan_pwm_config(client);
1903 	if (ret)
1904 		dev_warn(&client->dev, "Error %d configuring fan/pwm\n", ret);
1905 
1906 	/* Start monitoring */
1907 	switch (chip) {
1908 	case adt7475:
1909 	case adt7476:
1910 		i2c_smbus_write_byte_data(client, REG_CONFIG1,
1911 					  adt7475_read(REG_CONFIG1) | 0x01);
1912 		break;
1913 	default:
1914 		break;
1915 	}
1916 
1917 	data->groups[group_num++] = &adt7475_attr_group;
1918 
1919 	/* Features that can be disabled individually */
1920 	if (data->has_fan4) {
1921 		data->groups[group_num++] = &fan4_attr_group;
1922 	}
1923 	if (data->has_pwm2) {
1924 		data->groups[group_num++] = &pwm2_attr_group;
1925 	}
1926 	if (data->has_voltage & (1 << 0)) {
1927 		data->groups[group_num++] = &in0_attr_group;
1928 	}
1929 	if (data->has_voltage & (1 << 3)) {
1930 		data->groups[group_num++] = &in3_attr_group;
1931 	}
1932 	if (data->has_voltage & (1 << 4)) {
1933 		data->groups[group_num++] = &in4_attr_group;
1934 	}
1935 	if (data->has_voltage & (1 << 5)) {
1936 		data->groups[group_num++] = &in5_attr_group;
1937 	}
1938 	if (data->has_voltage & (1 << 6)) {
1939 		data->groups[group_num++] = &in6_attr_group;
1940 	}
1941 	if (data->has_vid) {
1942 		data->vrm = vid_which_vrm();
1943 		data->groups[group_num] = &vid_attr_group;
1944 	}
1945 
1946 	/* register device with all the acquired attributes */
1947 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
1948 							   client->name, data,
1949 							   data->groups);
1950 
1951 	if (IS_ERR(hwmon_dev)) {
1952 		ret = PTR_ERR(hwmon_dev);
1953 		return ret;
1954 	}
1955 
1956 	dev_info(&client->dev, "%s device, revision %d\n",
1957 		 names[chip], revision);
1958 	if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
1959 		dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1960 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1961 			 (data->has_voltage & (1 << 4)) ? " in4" : "",
1962 			 data->has_fan4 ? " fan4" : "",
1963 			 data->has_pwm2 ? " pwm2" : "",
1964 			 data->has_vid ? " vid" : "");
1965 	if (data->bypass_attn)
1966 		dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1967 			 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1968 			 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1969 			 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1970 			 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1971 
1972 	/* Limits and settings, should never change update more than once */
1973 	ret = adt7475_update_limits(client);
1974 	if (ret)
1975 		return ret;
1976 
1977 	return 0;
1978 }
1979 
1980 static struct i2c_driver adt7475_driver = {
1981 	.class		= I2C_CLASS_HWMON,
1982 	.driver = {
1983 		.name	= "adt7475",
1984 		.of_match_table = of_match_ptr(adt7475_of_match),
1985 	},
1986 	.probe		= adt7475_probe,
1987 	.id_table	= adt7475_id,
1988 	.detect		= adt7475_detect,
1989 	.address_list	= normal_i2c,
1990 };
1991 
1992 static void adt7475_read_hystersis(struct i2c_client *client)
1993 {
1994 	struct adt7475_data *data = i2c_get_clientdata(client);
1995 
1996 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1997 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1998 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1999 }
2000 
2001 static void adt7475_read_pwm(struct i2c_client *client, int index)
2002 {
2003 	struct adt7475_data *data = i2c_get_clientdata(client);
2004 	unsigned int v;
2005 
2006 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
2007 
2008 	/*
2009 	 * Figure out the internal value for pwmctrl and pwmchan
2010 	 * based on the current settings
2011 	 */
2012 	v = (data->pwm[CONTROL][index] >> 5) & 7;
2013 
2014 	if (v == 3)
2015 		data->pwmctl[index] = 0;
2016 	else if (v == 7)
2017 		data->pwmctl[index] = 1;
2018 	else if (v == 4) {
2019 		/*
2020 		 * The fan is disabled - we don't want to
2021 		 * support that, so change to manual mode and
2022 		 * set the duty cycle to 0 instead
2023 		 */
2024 		data->pwm[INPUT][index] = 0;
2025 		data->pwm[CONTROL][index] &= ~0xE0;
2026 		data->pwm[CONTROL][index] |= (7 << 5);
2027 
2028 		i2c_smbus_write_byte_data(client, PWM_REG(index),
2029 					  data->pwm[INPUT][index]);
2030 
2031 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
2032 					  data->pwm[CONTROL][index]);
2033 
2034 		data->pwmctl[index] = 1;
2035 	} else {
2036 		data->pwmctl[index] = 2;
2037 
2038 		switch (v) {
2039 		case 0:
2040 			data->pwmchan[index] = 1;
2041 			break;
2042 		case 1:
2043 			data->pwmchan[index] = 2;
2044 			break;
2045 		case 2:
2046 			data->pwmchan[index] = 4;
2047 			break;
2048 		case 5:
2049 			data->pwmchan[index] = 6;
2050 			break;
2051 		case 6:
2052 			data->pwmchan[index] = 7;
2053 			break;
2054 		}
2055 	}
2056 }
2057 
2058 static int adt7475_update_measure(struct device *dev)
2059 {
2060 	struct adt7475_data *data = dev_get_drvdata(dev);
2061 	struct i2c_client *client = data->client;
2062 	u16 ext;
2063 	int i;
2064 	int ret;
2065 
2066 	ret = adt7475_read(REG_STATUS2);
2067 	if (ret < 0)
2068 		return ret;
2069 	data->alarms = ret << 8;
2070 
2071 	ret = adt7475_read(REG_STATUS1);
2072 	if (ret < 0)
2073 		return ret;
2074 	data->alarms |= ret;
2075 
2076 	ret = adt7475_read(REG_EXTEND2);
2077 	if (ret < 0)
2078 		return ret;
2079 
2080 	ext = (ret << 8);
2081 
2082 	ret = adt7475_read(REG_EXTEND1);
2083 	if (ret < 0)
2084 		return ret;
2085 
2086 	ext |= ret;
2087 
2088 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
2089 		if (!(data->has_voltage & (1 << i)))
2090 			continue;
2091 		ret = adt7475_read(VOLTAGE_REG(i));
2092 		if (ret < 0)
2093 			return ret;
2094 		data->voltage[INPUT][i] =
2095 			(ret << 2) |
2096 			((ext >> (i * 2)) & 3);
2097 	}
2098 
2099 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
2100 		ret = adt7475_read(TEMP_REG(i));
2101 		if (ret < 0)
2102 			return ret;
2103 		data->temp[INPUT][i] =
2104 			(ret << 2) |
2105 			((ext >> ((i + 5) * 2)) & 3);
2106 	}
2107 
2108 	if (data->has_voltage & (1 << 5)) {
2109 		ret = adt7475_read(REG_STATUS4);
2110 		if (ret < 0)
2111 			return ret;
2112 		data->alarms |= ret << 24;
2113 
2114 		ret = adt7475_read(REG_EXTEND3);
2115 		if (ret < 0)
2116 			return ret;
2117 		ext = ret;
2118 
2119 		ret = adt7475_read(REG_VTT);
2120 		if (ret < 0)
2121 			return ret;
2122 		data->voltage[INPUT][5] = ret << 2 |
2123 			((ext >> 4) & 3);
2124 	}
2125 
2126 	if (data->has_voltage & (1 << 6)) {
2127 		ret = adt7475_read(REG_STATUS4);
2128 		if (ret < 0)
2129 			return ret;
2130 		data->alarms |= ret << 24;
2131 
2132 		ret = adt7475_read(REG_EXTEND3);
2133 		if (ret < 0)
2134 			return ret;
2135 		ext = ret;
2136 
2137 		ret = adt7475_read(REG_IMON);
2138 		if (ret < 0)
2139 			return ret;
2140 		data->voltage[INPUT][6] = ret << 2 |
2141 			((ext >> 6) & 3);
2142 	}
2143 
2144 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
2145 		if (i == 3 && !data->has_fan4)
2146 			continue;
2147 		ret = adt7475_read_word(client, TACH_REG(i));
2148 		if (ret < 0)
2149 			return ret;
2150 		data->tach[INPUT][i] = ret;
2151 	}
2152 
2153 	/* Updated by hw when in auto mode */
2154 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
2155 		if (i == 1 && !data->has_pwm2)
2156 			continue;
2157 		ret = adt7475_read(PWM_REG(i));
2158 		if (ret < 0)
2159 			return ret;
2160 		data->pwm[INPUT][i] = ret;
2161 	}
2162 
2163 	if (data->has_vid) {
2164 		ret = adt7475_read(REG_VID);
2165 		if (ret < 0)
2166 			return ret;
2167 		data->vid = ret & 0x3f;
2168 	}
2169 
2170 	return 0;
2171 }
2172 
2173 static struct adt7475_data *adt7475_update_device(struct device *dev)
2174 {
2175 	struct adt7475_data *data = dev_get_drvdata(dev);
2176 	int ret;
2177 
2178 	mutex_lock(&data->lock);
2179 
2180 	/* Measurement values update every 2 seconds */
2181 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
2182 	    !data->valid) {
2183 		ret = adt7475_update_measure(dev);
2184 		if (ret) {
2185 			data->valid = false;
2186 			mutex_unlock(&data->lock);
2187 			return ERR_PTR(ret);
2188 		}
2189 		data->measure_updated = jiffies;
2190 		data->valid = true;
2191 	}
2192 
2193 	mutex_unlock(&data->lock);
2194 
2195 	return data;
2196 }
2197 
2198 module_i2c_driver(adt7475_driver);
2199 
2200 MODULE_AUTHOR("Advanced Micro Devices, Inc");
2201 MODULE_DESCRIPTION("adt7475 driver");
2202 MODULE_LICENSE("GPL");
2203