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