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