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