xref: /openbmc/linux/drivers/hwmon/adt7475.c (revision 2169e6da)
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 device *hwmon_dev;
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 };
216 
217 static struct i2c_driver adt7475_driver;
218 static struct adt7475_data *adt7475_update_device(struct device *dev);
219 static void adt7475_read_hystersis(struct i2c_client *client);
220 static void adt7475_read_pwm(struct i2c_client *client, int index);
221 
222 /* Given a temp value, convert it to register value */
223 
224 static inline u16 temp2reg(struct adt7475_data *data, long val)
225 {
226 	u16 ret;
227 
228 	if (!(data->config5 & CONFIG5_TWOSCOMP)) {
229 		val = clamp_val(val, -64000, 191000);
230 		ret = (val + 64500) / 1000;
231 	} else {
232 		val = clamp_val(val, -128000, 127000);
233 		if (val < -500)
234 			ret = (256500 + val) / 1000;
235 		else
236 			ret = (val + 500) / 1000;
237 	}
238 
239 	return ret << 2;
240 }
241 
242 /* Given a register value, convert it to a real temp value */
243 
244 static inline int reg2temp(struct adt7475_data *data, u16 reg)
245 {
246 	if (data->config5 & CONFIG5_TWOSCOMP) {
247 		if (reg >= 512)
248 			return (reg - 1024) * 250;
249 		else
250 			return reg * 250;
251 	} else
252 		return (reg - 256) * 250;
253 }
254 
255 static inline int tach2rpm(u16 tach)
256 {
257 	if (tach == 0 || tach == 0xFFFF)
258 		return 0;
259 
260 	return (90000 * 60) / tach;
261 }
262 
263 static inline u16 rpm2tach(unsigned long rpm)
264 {
265 	if (rpm == 0)
266 		return 0;
267 
268 	return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
269 }
270 
271 /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
272 static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
273 	{ 45, 94 },	/* +2.5V */
274 	{ 175, 525 },	/* Vccp */
275 	{ 68, 71 },	/* Vcc */
276 	{ 93, 47 },	/* +5V */
277 	{ 120, 20 },	/* +12V */
278 	{ 45, 45 },	/* Vtt */
279 };
280 
281 static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
282 {
283 	const int *r = adt7473_in_scaling[channel];
284 
285 	if (bypass_attn & (1 << channel))
286 		return DIV_ROUND_CLOSEST(reg * 2250, 1024);
287 	return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
288 }
289 
290 static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
291 {
292 	const int *r = adt7473_in_scaling[channel];
293 	long reg;
294 
295 	if (bypass_attn & (1 << channel))
296 		reg = (volt * 1024) / 2250;
297 	else
298 		reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
299 	return clamp_val(reg, 0, 1023) & (0xff << 2);
300 }
301 
302 static int adt7475_read_word(struct i2c_client *client, int reg)
303 {
304 	int val1, val2;
305 
306 	val1 = i2c_smbus_read_byte_data(client, reg);
307 	if (val1 < 0)
308 		return val1;
309 	val2 = i2c_smbus_read_byte_data(client, reg + 1);
310 	if (val2 < 0)
311 		return val2;
312 
313 	return val1 | (val2 << 8);
314 }
315 
316 static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
317 {
318 	i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
319 	i2c_smbus_write_byte_data(client, reg, val & 0xFF);
320 }
321 
322 static ssize_t voltage_show(struct device *dev, struct device_attribute *attr,
323 			    char *buf)
324 {
325 	struct adt7475_data *data = adt7475_update_device(dev);
326 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
327 	unsigned short val;
328 
329 	if (IS_ERR(data))
330 		return PTR_ERR(data);
331 
332 	switch (sattr->nr) {
333 	case ALARM:
334 		return sprintf(buf, "%d\n",
335 			       (data->alarms >> sattr->index) & 1);
336 	default:
337 		val = data->voltage[sattr->nr][sattr->index];
338 		return sprintf(buf, "%d\n",
339 			       reg2volt(sattr->index, val, data->bypass_attn));
340 	}
341 }
342 
343 static ssize_t voltage_store(struct device *dev,
344 			     struct device_attribute *attr, const char *buf,
345 			     size_t count)
346 {
347 
348 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
349 	struct i2c_client *client = to_i2c_client(dev);
350 	struct adt7475_data *data = i2c_get_clientdata(client);
351 	unsigned char reg;
352 	long val;
353 
354 	if (kstrtol(buf, 10, &val))
355 		return -EINVAL;
356 
357 	mutex_lock(&data->lock);
358 
359 	data->voltage[sattr->nr][sattr->index] =
360 				volt2reg(sattr->index, val, data->bypass_attn);
361 
362 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
363 		if (sattr->nr == MIN)
364 			reg = VOLTAGE_MIN_REG(sattr->index);
365 		else
366 			reg = VOLTAGE_MAX_REG(sattr->index);
367 	} else {
368 		if (sattr->nr == MIN)
369 			reg = REG_VTT_MIN;
370 		else
371 			reg = REG_VTT_MAX;
372 	}
373 
374 	i2c_smbus_write_byte_data(client, reg,
375 				  data->voltage[sattr->nr][sattr->index] >> 2);
376 	mutex_unlock(&data->lock);
377 
378 	return count;
379 }
380 
381 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
382 			 char *buf)
383 {
384 	struct adt7475_data *data = adt7475_update_device(dev);
385 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
386 	int out;
387 
388 	if (IS_ERR(data))
389 		return PTR_ERR(data);
390 
391 	switch (sattr->nr) {
392 	case HYSTERSIS:
393 		mutex_lock(&data->lock);
394 		out = data->temp[sattr->nr][sattr->index];
395 		if (sattr->index != 1)
396 			out = (out >> 4) & 0xF;
397 		else
398 			out = (out & 0xF);
399 		/*
400 		 * Show the value as an absolute number tied to
401 		 * THERM
402 		 */
403 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
404 			out * 1000;
405 		mutex_unlock(&data->lock);
406 		break;
407 
408 	case OFFSET:
409 		/*
410 		 * Offset is always 2's complement, regardless of the
411 		 * setting in CONFIG5
412 		 */
413 		mutex_lock(&data->lock);
414 		out = (s8)data->temp[sattr->nr][sattr->index];
415 		if (data->config5 & CONFIG5_TEMPOFFSET)
416 			out *= 1000;
417 		else
418 			out *= 500;
419 		mutex_unlock(&data->lock);
420 		break;
421 
422 	case ALARM:
423 		out = (data->alarms >> (sattr->index + 4)) & 1;
424 		break;
425 
426 	case FAULT:
427 		/* Note - only for remote1 and remote2 */
428 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
429 		break;
430 
431 	default:
432 		/* All other temp values are in the configured format */
433 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
434 	}
435 
436 	return sprintf(buf, "%d\n", out);
437 }
438 
439 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
440 			  const char *buf, size_t count)
441 {
442 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
443 	struct i2c_client *client = to_i2c_client(dev);
444 	struct adt7475_data *data = i2c_get_clientdata(client);
445 	unsigned char reg = 0;
446 	u8 out;
447 	int temp;
448 	long val;
449 
450 	if (kstrtol(buf, 10, &val))
451 		return -EINVAL;
452 
453 	mutex_lock(&data->lock);
454 
455 	/* We need the config register in all cases for temp <-> reg conv. */
456 	data->config5 = adt7475_read(REG_CONFIG5);
457 
458 	switch (sattr->nr) {
459 	case OFFSET:
460 		if (data->config5 & CONFIG5_TEMPOFFSET) {
461 			val = clamp_val(val, -63000, 127000);
462 			out = data->temp[OFFSET][sattr->index] = val / 1000;
463 		} else {
464 			val = clamp_val(val, -63000, 64000);
465 			out = data->temp[OFFSET][sattr->index] = val / 500;
466 		}
467 		break;
468 
469 	case HYSTERSIS:
470 		/*
471 		 * The value will be given as an absolute value, turn it
472 		 * into an offset based on THERM
473 		 */
474 
475 		/* Read fresh THERM and HYSTERSIS values from the chip */
476 		data->temp[THERM][sattr->index] =
477 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
478 		adt7475_read_hystersis(client);
479 
480 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
481 		val = clamp_val(val, temp - 15000, temp);
482 		val = (temp - val) / 1000;
483 
484 		if (sattr->index != 1) {
485 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
486 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
487 		} else {
488 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
489 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
490 		}
491 
492 		out = data->temp[HYSTERSIS][sattr->index];
493 		break;
494 
495 	default:
496 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
497 
498 		/*
499 		 * We maintain an extra 2 digits of precision for simplicity
500 		 * - shift those back off before writing the value
501 		 */
502 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
503 	}
504 
505 	switch (sattr->nr) {
506 	case MIN:
507 		reg = TEMP_MIN_REG(sattr->index);
508 		break;
509 	case MAX:
510 		reg = TEMP_MAX_REG(sattr->index);
511 		break;
512 	case OFFSET:
513 		reg = TEMP_OFFSET_REG(sattr->index);
514 		break;
515 	case AUTOMIN:
516 		reg = TEMP_TMIN_REG(sattr->index);
517 		break;
518 	case THERM:
519 		reg = TEMP_THERM_REG(sattr->index);
520 		break;
521 	case HYSTERSIS:
522 		if (sattr->index != 2)
523 			reg = REG_REMOTE1_HYSTERSIS;
524 		else
525 			reg = REG_REMOTE2_HYSTERSIS;
526 
527 		break;
528 	}
529 
530 	i2c_smbus_write_byte_data(client, reg, out);
531 
532 	mutex_unlock(&data->lock);
533 	return count;
534 }
535 
536 /* Assuming CONFIG6[SLOW] is 0 */
537 static const int ad7475_st_map[] = {
538 	37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
539 };
540 
541 static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr,
542 			    char *buf)
543 {
544 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
545 	struct i2c_client *client = to_i2c_client(dev);
546 	struct adt7475_data *data = i2c_get_clientdata(client);
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 i2c_client *client = to_i2c_client(dev);
574 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
651 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
714 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
773 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
822 	struct adt7475_data *data = i2c_get_clientdata(client);
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 i2c_client *client = to_i2c_client(dev);
834 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
918 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
942 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
986 	struct adt7475_data *data = i2c_get_clientdata(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 i2c_client *client = to_i2c_client(dev);
1026 	struct adt7475_data *data = i2c_get_clientdata(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 void adt7475_remove_files(struct i2c_client *client,
1346 				 struct adt7475_data *data)
1347 {
1348 	sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1349 	if (data->has_fan4)
1350 		sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1351 	if (data->has_pwm2)
1352 		sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1353 	if (data->has_voltage & (1 << 0))
1354 		sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1355 	if (data->has_voltage & (1 << 3))
1356 		sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1357 	if (data->has_voltage & (1 << 4))
1358 		sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1359 	if (data->has_voltage & (1 << 5))
1360 		sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
1361 	if (data->has_vid)
1362 		sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
1363 }
1364 
1365 static int adt7475_update_limits(struct i2c_client *client)
1366 {
1367 	struct adt7475_data *data = i2c_get_clientdata(client);
1368 	int i;
1369 	int ret;
1370 
1371 	ret = adt7475_read(REG_CONFIG4);
1372 	if (ret < 0)
1373 		return ret;
1374 	data->config4 = ret;
1375 
1376 	ret = adt7475_read(REG_CONFIG5);
1377 	if (ret < 0)
1378 		return ret;
1379 	data->config5 = ret;
1380 
1381 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1382 		if (!(data->has_voltage & (1 << i)))
1383 			continue;
1384 		/* Adjust values so they match the input precision */
1385 		ret = adt7475_read(VOLTAGE_MIN_REG(i));
1386 		if (ret < 0)
1387 			return ret;
1388 		data->voltage[MIN][i] = ret << 2;
1389 
1390 		ret = adt7475_read(VOLTAGE_MAX_REG(i));
1391 		if (ret < 0)
1392 			return ret;
1393 		data->voltage[MAX][i] = ret << 2;
1394 	}
1395 
1396 	if (data->has_voltage & (1 << 5)) {
1397 		ret = adt7475_read(REG_VTT_MIN);
1398 		if (ret < 0)
1399 			return ret;
1400 		data->voltage[MIN][5] = ret << 2;
1401 
1402 		ret = adt7475_read(REG_VTT_MAX);
1403 		if (ret < 0)
1404 			return ret;
1405 		data->voltage[MAX][5] = ret << 2;
1406 	}
1407 
1408 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1409 		/* Adjust values so they match the input precision */
1410 		ret = adt7475_read(TEMP_MIN_REG(i));
1411 		if (ret < 0)
1412 			return ret;
1413 		data->temp[MIN][i] = ret << 2;
1414 
1415 		ret = adt7475_read(TEMP_MAX_REG(i));
1416 		if (ret < 0)
1417 			return ret;
1418 		data->temp[MAX][i] = ret << 2;
1419 
1420 		ret = adt7475_read(TEMP_TMIN_REG(i));
1421 		if (ret < 0)
1422 			return ret;
1423 		data->temp[AUTOMIN][i] = ret << 2;
1424 
1425 		ret = adt7475_read(TEMP_THERM_REG(i));
1426 		if (ret < 0)
1427 			return ret;
1428 		data->temp[THERM][i] = ret << 2;
1429 
1430 		ret = adt7475_read(TEMP_OFFSET_REG(i));
1431 		if (ret < 0)
1432 			return ret;
1433 		data->temp[OFFSET][i] = ret;
1434 	}
1435 	adt7475_read_hystersis(client);
1436 
1437 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1438 		if (i == 3 && !data->has_fan4)
1439 			continue;
1440 		ret = adt7475_read_word(client, TACH_MIN_REG(i));
1441 		if (ret < 0)
1442 			return ret;
1443 		data->tach[MIN][i] = ret;
1444 	}
1445 
1446 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1447 		if (i == 1 && !data->has_pwm2)
1448 			continue;
1449 		ret = adt7475_read(PWM_MAX_REG(i));
1450 		if (ret < 0)
1451 			return ret;
1452 		data->pwm[MAX][i] = ret;
1453 
1454 		ret = adt7475_read(PWM_MIN_REG(i));
1455 		if (ret < 0)
1456 			return ret;
1457 		data->pwm[MIN][i] = ret;
1458 		/* Set the channel and control information */
1459 		adt7475_read_pwm(client, i);
1460 	}
1461 
1462 	ret = adt7475_read(TEMP_TRANGE_REG(0));
1463 	if (ret < 0)
1464 		return ret;
1465 	data->range[0] = ret;
1466 
1467 	ret = adt7475_read(TEMP_TRANGE_REG(1));
1468 	if (ret < 0)
1469 		return ret;
1470 	data->range[1] = ret;
1471 
1472 	ret = adt7475_read(TEMP_TRANGE_REG(2));
1473 	if (ret < 0)
1474 		return ret;
1475 	data->range[2] = ret;
1476 
1477 	return 0;
1478 }
1479 
1480 static int adt7475_probe(struct i2c_client *client,
1481 			 const struct i2c_device_id *id)
1482 {
1483 	enum chips chip;
1484 	static const char * const names[] = {
1485 		[adt7473] = "ADT7473",
1486 		[adt7475] = "ADT7475",
1487 		[adt7476] = "ADT7476",
1488 		[adt7490] = "ADT7490",
1489 	};
1490 
1491 	struct adt7475_data *data;
1492 	int i, ret = 0, revision;
1493 	u8 config2, config3;
1494 
1495 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1496 	if (data == NULL)
1497 		return -ENOMEM;
1498 
1499 	mutex_init(&data->lock);
1500 	i2c_set_clientdata(client, data);
1501 
1502 	if (client->dev.of_node)
1503 		chip = (enum chips)of_device_get_match_data(&client->dev);
1504 	else
1505 		chip = id->driver_data;
1506 
1507 	/* Initialize device-specific values */
1508 	switch (chip) {
1509 	case adt7476:
1510 		data->has_voltage = 0x0e;	/* in1 to in3 */
1511 		revision = adt7475_read(REG_DEVID2) & 0x07;
1512 		break;
1513 	case adt7490:
1514 		data->has_voltage = 0x3e;	/* in1 to in5 */
1515 		revision = adt7475_read(REG_DEVID2) & 0x03;
1516 		if (revision == 0x03)
1517 			revision += adt7475_read(REG_DEVREV2);
1518 		break;
1519 	default:
1520 		data->has_voltage = 0x06;	/* in1, in2 */
1521 		revision = adt7475_read(REG_DEVID2) & 0x07;
1522 	}
1523 
1524 	config3 = adt7475_read(REG_CONFIG3);
1525 	/* Pin PWM2 may alternatively be used for ALERT output */
1526 	if (!(config3 & CONFIG3_SMBALERT))
1527 		data->has_pwm2 = 1;
1528 	/* Meaning of this bit is inverted for the ADT7473-1 */
1529 	if (id->driver_data == adt7473 && revision >= 1)
1530 		data->has_pwm2 = !data->has_pwm2;
1531 
1532 	data->config4 = adt7475_read(REG_CONFIG4);
1533 	/* Pin TACH4 may alternatively be used for THERM */
1534 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1535 		data->has_fan4 = 1;
1536 
1537 	/*
1538 	 * THERM configuration is more complex on the ADT7476 and ADT7490,
1539 	 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
1540 	 * this function
1541 	 */
1542 	if (id->driver_data == adt7490) {
1543 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1544 		    !(config3 & CONFIG3_THERM))
1545 			data->has_fan4 = 1;
1546 	}
1547 	if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1548 		if (!(config3 & CONFIG3_THERM) ||
1549 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1550 			data->has_voltage |= (1 << 0);		/* in0 */
1551 	}
1552 
1553 	/*
1554 	 * On the ADT7476, the +12V input pin may instead be used as VID5,
1555 	 * and VID pins may alternatively be used as GPIO
1556 	 */
1557 	if (id->driver_data == adt7476) {
1558 		u8 vid = adt7475_read(REG_VID);
1559 		if (!(vid & VID_VIDSEL))
1560 			data->has_voltage |= (1 << 4);		/* in4 */
1561 
1562 		data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1563 	}
1564 
1565 	/* Voltage attenuators can be bypassed, globally or individually */
1566 	config2 = adt7475_read(REG_CONFIG2);
1567 	if (config2 & CONFIG2_ATTN) {
1568 		data->bypass_attn = (0x3 << 3) | 0x3;
1569 	} else {
1570 		data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1571 				    ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1572 	}
1573 	data->bypass_attn &= data->has_voltage;
1574 
1575 	/*
1576 	 * Call adt7475_read_pwm for all pwm's as this will reprogram any
1577 	 * pwm's which are disabled to manual mode with 0% duty cycle
1578 	 */
1579 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
1580 		adt7475_read_pwm(client, i);
1581 
1582 	/* Start monitoring */
1583 	switch (chip) {
1584 	case adt7475:
1585 	case adt7476:
1586 		i2c_smbus_write_byte_data(client, REG_CONFIG1,
1587 					  adt7475_read(REG_CONFIG1) | 0x01);
1588 		break;
1589 	default:
1590 		break;
1591 	}
1592 
1593 	ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1594 	if (ret)
1595 		return ret;
1596 
1597 	/* Features that can be disabled individually */
1598 	if (data->has_fan4) {
1599 		ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1600 		if (ret)
1601 			goto eremove;
1602 	}
1603 	if (data->has_pwm2) {
1604 		ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1605 		if (ret)
1606 			goto eremove;
1607 	}
1608 	if (data->has_voltage & (1 << 0)) {
1609 		ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1610 		if (ret)
1611 			goto eremove;
1612 	}
1613 	if (data->has_voltage & (1 << 3)) {
1614 		ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1615 		if (ret)
1616 			goto eremove;
1617 	}
1618 	if (data->has_voltage & (1 << 4)) {
1619 		ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1620 		if (ret)
1621 			goto eremove;
1622 	}
1623 	if (data->has_voltage & (1 << 5)) {
1624 		ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1625 		if (ret)
1626 			goto eremove;
1627 	}
1628 	if (data->has_vid) {
1629 		data->vrm = vid_which_vrm();
1630 		ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
1631 		if (ret)
1632 			goto eremove;
1633 	}
1634 
1635 	data->hwmon_dev = hwmon_device_register(&client->dev);
1636 	if (IS_ERR(data->hwmon_dev)) {
1637 		ret = PTR_ERR(data->hwmon_dev);
1638 		goto eremove;
1639 	}
1640 
1641 	dev_info(&client->dev, "%s device, revision %d\n",
1642 		 names[id->driver_data], revision);
1643 	if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
1644 		dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1645 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1646 			 (data->has_voltage & (1 << 4)) ? " in4" : "",
1647 			 data->has_fan4 ? " fan4" : "",
1648 			 data->has_pwm2 ? " pwm2" : "",
1649 			 data->has_vid ? " vid" : "");
1650 	if (data->bypass_attn)
1651 		dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1652 			 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1653 			 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1654 			 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1655 			 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1656 
1657 	/* Limits and settings, should never change update more than once */
1658 	ret = adt7475_update_limits(client);
1659 	if (ret)
1660 		goto eremove;
1661 
1662 	return 0;
1663 
1664 eremove:
1665 	adt7475_remove_files(client, data);
1666 	return ret;
1667 }
1668 
1669 static int adt7475_remove(struct i2c_client *client)
1670 {
1671 	struct adt7475_data *data = i2c_get_clientdata(client);
1672 
1673 	hwmon_device_unregister(data->hwmon_dev);
1674 	adt7475_remove_files(client, data);
1675 
1676 	return 0;
1677 }
1678 
1679 static struct i2c_driver adt7475_driver = {
1680 	.class		= I2C_CLASS_HWMON,
1681 	.driver = {
1682 		.name	= "adt7475",
1683 		.of_match_table = of_match_ptr(adt7475_of_match),
1684 	},
1685 	.probe		= adt7475_probe,
1686 	.remove		= adt7475_remove,
1687 	.id_table	= adt7475_id,
1688 	.detect		= adt7475_detect,
1689 	.address_list	= normal_i2c,
1690 };
1691 
1692 static void adt7475_read_hystersis(struct i2c_client *client)
1693 {
1694 	struct adt7475_data *data = i2c_get_clientdata(client);
1695 
1696 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1697 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1698 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1699 }
1700 
1701 static void adt7475_read_pwm(struct i2c_client *client, int index)
1702 {
1703 	struct adt7475_data *data = i2c_get_clientdata(client);
1704 	unsigned int v;
1705 
1706 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1707 
1708 	/*
1709 	 * Figure out the internal value for pwmctrl and pwmchan
1710 	 * based on the current settings
1711 	 */
1712 	v = (data->pwm[CONTROL][index] >> 5) & 7;
1713 
1714 	if (v == 3)
1715 		data->pwmctl[index] = 0;
1716 	else if (v == 7)
1717 		data->pwmctl[index] = 1;
1718 	else if (v == 4) {
1719 		/*
1720 		 * The fan is disabled - we don't want to
1721 		 * support that, so change to manual mode and
1722 		 * set the duty cycle to 0 instead
1723 		 */
1724 		data->pwm[INPUT][index] = 0;
1725 		data->pwm[CONTROL][index] &= ~0xE0;
1726 		data->pwm[CONTROL][index] |= (7 << 5);
1727 
1728 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1729 					  data->pwm[INPUT][index]);
1730 
1731 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1732 					  data->pwm[CONTROL][index]);
1733 
1734 		data->pwmctl[index] = 1;
1735 	} else {
1736 		data->pwmctl[index] = 2;
1737 
1738 		switch (v) {
1739 		case 0:
1740 			data->pwmchan[index] = 1;
1741 			break;
1742 		case 1:
1743 			data->pwmchan[index] = 2;
1744 			break;
1745 		case 2:
1746 			data->pwmchan[index] = 4;
1747 			break;
1748 		case 5:
1749 			data->pwmchan[index] = 6;
1750 			break;
1751 		case 6:
1752 			data->pwmchan[index] = 7;
1753 			break;
1754 		}
1755 	}
1756 }
1757 
1758 static int adt7475_update_measure(struct device *dev)
1759 {
1760 	struct i2c_client *client = to_i2c_client(dev);
1761 	struct adt7475_data *data = i2c_get_clientdata(client);
1762 	u16 ext;
1763 	int i;
1764 	int ret;
1765 
1766 	ret = adt7475_read(REG_STATUS2);
1767 	if (ret < 0)
1768 		return ret;
1769 	data->alarms = ret << 8;
1770 
1771 	ret = adt7475_read(REG_STATUS1);
1772 	if (ret < 0)
1773 		return ret;
1774 	data->alarms |= ret;
1775 
1776 	ret = adt7475_read(REG_EXTEND2);
1777 	if (ret < 0)
1778 		return ret;
1779 
1780 	ext = (ret << 8);
1781 
1782 	ret = adt7475_read(REG_EXTEND1);
1783 	if (ret < 0)
1784 		return ret;
1785 
1786 	ext |= ret;
1787 
1788 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1789 		if (!(data->has_voltage & (1 << i)))
1790 			continue;
1791 		ret = adt7475_read(VOLTAGE_REG(i));
1792 		if (ret < 0)
1793 			return ret;
1794 		data->voltage[INPUT][i] =
1795 			(ret << 2) |
1796 			((ext >> (i * 2)) & 3);
1797 	}
1798 
1799 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1800 		ret = adt7475_read(TEMP_REG(i));
1801 		if (ret < 0)
1802 			return ret;
1803 		data->temp[INPUT][i] =
1804 			(ret << 2) |
1805 			((ext >> ((i + 5) * 2)) & 3);
1806 	}
1807 
1808 	if (data->has_voltage & (1 << 5)) {
1809 		ret = adt7475_read(REG_STATUS4);
1810 		if (ret < 0)
1811 			return ret;
1812 		data->alarms |= ret << 24;
1813 
1814 		ret = adt7475_read(REG_EXTEND3);
1815 		if (ret < 0)
1816 			return ret;
1817 		ext = ret;
1818 
1819 		ret = adt7475_read(REG_VTT);
1820 		if (ret < 0)
1821 			return ret;
1822 		data->voltage[INPUT][5] = ret << 2 |
1823 			((ext >> 4) & 3);
1824 	}
1825 
1826 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1827 		if (i == 3 && !data->has_fan4)
1828 			continue;
1829 		ret = adt7475_read_word(client, TACH_REG(i));
1830 		if (ret < 0)
1831 			return ret;
1832 		data->tach[INPUT][i] = ret;
1833 	}
1834 
1835 	/* Updated by hw when in auto mode */
1836 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1837 		if (i == 1 && !data->has_pwm2)
1838 			continue;
1839 		ret = adt7475_read(PWM_REG(i));
1840 		if (ret < 0)
1841 			return ret;
1842 		data->pwm[INPUT][i] = ret;
1843 	}
1844 
1845 	if (data->has_vid) {
1846 		ret = adt7475_read(REG_VID);
1847 		if (ret < 0)
1848 			return ret;
1849 		data->vid = ret & 0x3f;
1850 	}
1851 
1852 	return 0;
1853 }
1854 
1855 static struct adt7475_data *adt7475_update_device(struct device *dev)
1856 {
1857 	struct i2c_client *client = to_i2c_client(dev);
1858 	struct adt7475_data *data = i2c_get_clientdata(client);
1859 	int ret;
1860 
1861 	mutex_lock(&data->lock);
1862 
1863 	/* Measurement values update every 2 seconds */
1864 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1865 	    !data->valid) {
1866 		ret = adt7475_update_measure(dev);
1867 		if (ret) {
1868 			data->valid = false;
1869 			mutex_unlock(&data->lock);
1870 			return ERR_PTR(ret);
1871 		}
1872 		data->measure_updated = jiffies;
1873 		data->valid = true;
1874 	}
1875 
1876 	mutex_unlock(&data->lock);
1877 
1878 	return data;
1879 }
1880 
1881 module_i2c_driver(adt7475_driver);
1882 
1883 MODULE_AUTHOR("Advanced Micro Devices, Inc");
1884 MODULE_DESCRIPTION("adt7475 driver");
1885 MODULE_LICENSE("GPL");
1886