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