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