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