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