xref: /linux/drivers/hwmon/adt7475.c (revision 0fb09bf715736acfa7419d52f966aea2c0d57e7a)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21c301fc5SJordan Crouse /*
31c301fc5SJordan Crouse  * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
41c301fc5SJordan Crouse  * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
51c301fc5SJordan Crouse  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
61c301fc5SJordan Crouse  * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
77c81c60fSJean Delvare  * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
83d849981SJean Delvare  *
91c301fc5SJordan Crouse  * Derived from the lm83 driver by Jean Delvare
101c301fc5SJordan Crouse  */
111c301fc5SJordan Crouse 
121c301fc5SJordan Crouse #include <linux/module.h>
131c301fc5SJordan Crouse #include <linux/init.h>
141c301fc5SJordan Crouse #include <linux/slab.h>
151c301fc5SJordan Crouse #include <linux/i2c.h>
161c301fc5SJordan Crouse #include <linux/hwmon.h>
171c301fc5SJordan Crouse #include <linux/hwmon-sysfs.h>
1854fe4671SJean Delvare #include <linux/hwmon-vid.h>
191c301fc5SJordan Crouse #include <linux/err.h>
20dcd8f392SJean Delvare #include <linux/jiffies.h>
212ecff397SLogan Shaw #include <linux/of.h>
22e4651640SChris Packham #include <linux/util_macros.h>
231c301fc5SJordan Crouse 
24777c97ffSChris Packham #include <dt-bindings/pwm/pwm.h>
25777c97ffSChris Packham 
261c301fc5SJordan Crouse /* Indexes for the sysfs hooks */
271a251f52SLinus Torvalds enum adt_sysfs_id {
281a251f52SLinus Torvalds 	INPUT		= 0,
291a251f52SLinus Torvalds 	MIN		= 1,
301a251f52SLinus Torvalds 	MAX		= 2,
311a251f52SLinus Torvalds 	CONTROL		= 3,
321a251f52SLinus Torvalds 	OFFSET		= 3,	// Dup
331a251f52SLinus Torvalds 	AUTOMIN		= 4,
341a251f52SLinus Torvalds 	THERM		= 5,
351a251f52SLinus Torvalds 	HYSTERSIS	= 6,
369ed5bc24SGuenter Roeck /*
379ed5bc24SGuenter Roeck  * These are unique identifiers for the sysfs functions - unlike the
389ed5bc24SGuenter Roeck  * numbers above, these are not also indexes into an array
391c301fc5SJordan Crouse  */
401a251f52SLinus Torvalds 	ALARM		= 9,
411a251f52SLinus Torvalds 	FAULT		= 10,
421a251f52SLinus Torvalds };
431c301fc5SJordan Crouse 
441c301fc5SJordan Crouse 
451c301fc5SJordan Crouse /* 7475 Common Registers */
461c301fc5SJordan Crouse 
47d07ca4adSJean Delvare #define REG_DEVREV2		0x12	/* ADT7490 only */
4862c11e46STimothy Pearson #define REG_IMON		0x1D	/* ADT7490 only */
49d07ca4adSJean Delvare 
503d849981SJean Delvare #define REG_VTT			0x1E	/* ADT7490 only */
513d849981SJean Delvare #define REG_EXTEND3		0x1F	/* ADT7490 only */
523d849981SJean Delvare 
53cffb9dd0SJean Delvare #define REG_VOLTAGE_BASE	0x20
541c301fc5SJordan Crouse #define REG_TEMP_BASE		0x25
551c301fc5SJordan Crouse #define REG_TACH_BASE		0x28
561c301fc5SJordan Crouse #define REG_PWM_BASE		0x30
571c301fc5SJordan Crouse #define REG_PWM_MAX_BASE	0x38
581c301fc5SJordan Crouse 
591c301fc5SJordan Crouse #define REG_DEVID		0x3D
601c301fc5SJordan Crouse #define REG_VENDID		0x3E
61d656b6fdSJean Delvare #define REG_DEVID2		0x3F
621c301fc5SJordan Crouse 
634abdf38dSChris Packham #define REG_CONFIG1		0x40
644abdf38dSChris Packham 
651c301fc5SJordan Crouse #define REG_STATUS1		0x41
661c301fc5SJordan Crouse #define REG_STATUS2		0x42
671c301fc5SJordan Crouse 
68d8d2ee07SJean Delvare #define REG_VID			0x43	/* ADT7476 only */
69d8d2ee07SJean Delvare 
70cffb9dd0SJean Delvare #define REG_VOLTAGE_MIN_BASE	0x44
71cffb9dd0SJean Delvare #define REG_VOLTAGE_MAX_BASE	0x45
721c301fc5SJordan Crouse 
731c301fc5SJordan Crouse #define REG_TEMP_MIN_BASE	0x4E
741c301fc5SJordan Crouse #define REG_TEMP_MAX_BASE	0x4F
751c301fc5SJordan Crouse 
761c301fc5SJordan Crouse #define REG_TACH_MIN_BASE	0x54
771c301fc5SJordan Crouse 
781c301fc5SJordan Crouse #define REG_PWM_CONFIG_BASE	0x5C
791c301fc5SJordan Crouse 
801c301fc5SJordan Crouse #define REG_TEMP_TRANGE_BASE	0x5F
811c301fc5SJordan Crouse 
821d58f5efSChris Packham #define REG_ENHANCE_ACOUSTICS1	0x62
831d58f5efSChris Packham #define REG_ENHANCE_ACOUSTICS2	0x63
841d58f5efSChris Packham 
851c301fc5SJordan Crouse #define REG_PWM_MIN_BASE	0x64
861c301fc5SJordan Crouse 
871c301fc5SJordan Crouse #define REG_TEMP_TMIN_BASE	0x67
881c301fc5SJordan Crouse #define REG_TEMP_THERM_BASE	0x6A
891c301fc5SJordan Crouse 
901c301fc5SJordan Crouse #define REG_REMOTE1_HYSTERSIS	0x6D
911c301fc5SJordan Crouse #define REG_REMOTE2_HYSTERSIS	0x6E
921c301fc5SJordan Crouse 
931c301fc5SJordan Crouse #define REG_TEMP_OFFSET_BASE	0x70
941c301fc5SJordan Crouse 
95ebfaf1fbSJean Delvare #define REG_CONFIG2		0x73
96ebfaf1fbSJean Delvare 
971c301fc5SJordan Crouse #define REG_EXTEND1		0x76
981c301fc5SJordan Crouse #define REG_EXTEND2		0x77
99378933c9SJean Delvare 
100378933c9SJean Delvare #define REG_CONFIG3		0x78
1011c301fc5SJordan Crouse #define REG_CONFIG5		0x7C
102f99318b2SJean Delvare #define REG_CONFIG4		0x7D
103f99318b2SJean Delvare 
1043d849981SJean Delvare #define REG_STATUS4		0x81	/* ADT7490 only */
1053d849981SJean Delvare 
1063d849981SJean Delvare #define REG_VTT_MIN		0x84	/* ADT7490 only */
1073d849981SJean Delvare #define REG_VTT_MAX		0x86	/* ADT7490 only */
1083d849981SJean Delvare 
10962c11e46STimothy Pearson #define REG_IMON_MIN		0x85	/* ADT7490 only */
11062c11e46STimothy Pearson #define REG_IMON_MAX		0x87	/* ADT7490 only */
11162c11e46STimothy Pearson 
112d8d2ee07SJean Delvare #define VID_VIDSEL		0x80	/* ADT7476 only */
113d8d2ee07SJean Delvare 
114ebfaf1fbSJean Delvare #define CONFIG2_ATTN		0x20
115ebfaf1fbSJean Delvare 
116378933c9SJean Delvare #define CONFIG3_SMBALERT	0x01
117378933c9SJean Delvare #define CONFIG3_THERM		0x02
118378933c9SJean Delvare 
119378933c9SJean Delvare #define CONFIG4_PINFUNC		0x03
1207b8664f1SChris Packham #define CONFIG4_THERM		0x01
1217b8664f1SChris Packham #define CONFIG4_SMBALERT	0x02
122f99318b2SJean Delvare #define CONFIG4_MAXDUTY		0x08
123ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN10	0x30
124ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN43	0xC0
1251c301fc5SJordan Crouse 
1261c301fc5SJordan Crouse #define CONFIG5_TWOSCOMP	0x01
1271c301fc5SJordan Crouse #define CONFIG5_TEMPOFFSET	0x02
12854fe4671SJean Delvare #define CONFIG5_VIDGPIO		0x10	/* ADT7476 only */
1291c301fc5SJordan Crouse 
1301c301fc5SJordan Crouse /* ADT7475 Settings */
1311c301fc5SJordan Crouse 
13262c11e46STimothy Pearson #define ADT7475_VOLTAGE_COUNT	5	/* Not counting Vtt or Imon */
1331c301fc5SJordan Crouse #define ADT7475_TEMP_COUNT	3
1341c301fc5SJordan Crouse #define ADT7475_TACH_COUNT	4
1351c301fc5SJordan Crouse #define ADT7475_PWM_COUNT	3
1361c301fc5SJordan Crouse 
1371c301fc5SJordan Crouse /* Macro to read the registers */
1381c301fc5SJordan Crouse 
1391c301fc5SJordan Crouse #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
1401c301fc5SJordan Crouse 
1411c301fc5SJordan Crouse /* Macros to easily index the registers */
1421c301fc5SJordan Crouse 
1431c301fc5SJordan Crouse #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
1441c301fc5SJordan Crouse #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
1451c301fc5SJordan Crouse 
1461c301fc5SJordan Crouse #define PWM_REG(idx) (REG_PWM_BASE + (idx))
1471c301fc5SJordan Crouse #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
1481c301fc5SJordan Crouse #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
1491c301fc5SJordan Crouse #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
1501c301fc5SJordan Crouse 
1511c301fc5SJordan Crouse #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
1521c301fc5SJordan Crouse #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
1531c301fc5SJordan Crouse #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
1541c301fc5SJordan Crouse 
1551c301fc5SJordan Crouse #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
1561c301fc5SJordan Crouse #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
1571c301fc5SJordan Crouse #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
1581c301fc5SJordan Crouse #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
1591c301fc5SJordan Crouse #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
1601c301fc5SJordan Crouse #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
1611c301fc5SJordan Crouse #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
1621c301fc5SJordan Crouse 
163918ee91cSJean Delvare static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1641c301fc5SJordan Crouse 
165e5e9f44cSJean Delvare enum chips { adt7473, adt7475, adt7476, adt7490 };
1661c301fc5SJordan Crouse 
1671c301fc5SJordan Crouse static const struct i2c_device_id adt7475_id[] = {
168b180d050SJean Delvare 	{ "adt7473", adt7473 },
1691c301fc5SJordan Crouse 	{ "adt7475", adt7475 },
170d8d2ee07SJean Delvare 	{ "adt7476", adt7476 },
1713d849981SJean Delvare 	{ "adt7490", adt7490 },
1721c301fc5SJordan Crouse 	{ }
1731c301fc5SJordan Crouse };
1741c301fc5SJordan Crouse MODULE_DEVICE_TABLE(i2c, adt7475_id);
1751c301fc5SJordan Crouse 
176fe339dbfSGuenter Roeck static const struct of_device_id __maybe_unused adt7475_of_match[] = {
1774e2496e4SJavier Martinez Canillas 	{
1784e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7473",
1794e2496e4SJavier Martinez Canillas 		.data = (void *)adt7473
1804e2496e4SJavier Martinez Canillas 	},
1814e2496e4SJavier Martinez Canillas 	{
1824e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7475",
1834e2496e4SJavier Martinez Canillas 		.data = (void *)adt7475
1844e2496e4SJavier Martinez Canillas 	},
1854e2496e4SJavier Martinez Canillas 	{
1864e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7476",
1874e2496e4SJavier Martinez Canillas 		.data = (void *)adt7476
1884e2496e4SJavier Martinez Canillas 	},
1894e2496e4SJavier Martinez Canillas 	{
1904e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7490",
1914e2496e4SJavier Martinez Canillas 		.data = (void *)adt7490
1924e2496e4SJavier Martinez Canillas 	},
1934e2496e4SJavier Martinez Canillas 	{ },
1944e2496e4SJavier Martinez Canillas };
1954e2496e4SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, adt7475_of_match);
1964e2496e4SJavier Martinez Canillas 
1971c301fc5SJordan Crouse struct adt7475_data {
198c64fce7fSGrant McEwan 	struct i2c_client *client;
1991c301fc5SJordan Crouse 	struct mutex lock;
2001c301fc5SJordan Crouse 
2011c301fc5SJordan Crouse 	unsigned long measure_updated;
202b36fb171STokunori Ikegami 	bool valid;
2031c301fc5SJordan Crouse 
2042ecff397SLogan Shaw 	u8 config2;
205f99318b2SJean Delvare 	u8 config4;
2061c301fc5SJordan Crouse 	u8 config5;
207cffb9dd0SJean Delvare 	u8 has_voltage;
208ebfaf1fbSJean Delvare 	u8 bypass_attn;		/* Bypass voltage attenuator */
209378933c9SJean Delvare 	u8 has_pwm2:1;
210378933c9SJean Delvare 	u8 has_fan4:1;
21154fe4671SJean Delvare 	u8 has_vid:1;
2123d849981SJean Delvare 	u32 alarms;
21362c11e46STimothy Pearson 	u16 voltage[3][7];
2141c301fc5SJordan Crouse 	u16 temp[7][3];
2151c301fc5SJordan Crouse 	u16 tach[2][4];
2161c301fc5SJordan Crouse 	u8 pwm[4][3];
2171c301fc5SJordan Crouse 	u8 range[3];
2181c301fc5SJordan Crouse 	u8 pwmctl[3];
2191c301fc5SJordan Crouse 	u8 pwmchan[3];
2201d58f5efSChris Packham 	u8 enh_acoustics[2];
22154fe4671SJean Delvare 
22254fe4671SJean Delvare 	u8 vid;
22354fe4671SJean Delvare 	u8 vrm;
22462c11e46STimothy Pearson 	const struct attribute_group *groups[10];
2251c301fc5SJordan Crouse };
2261c301fc5SJordan Crouse 
2271c301fc5SJordan Crouse static struct i2c_driver adt7475_driver;
2281c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev);
2291c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client);
2301c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index);
2311c301fc5SJordan Crouse 
2321c301fc5SJordan Crouse /* Given a temp value, convert it to register value */
2331c301fc5SJordan Crouse 
2341c301fc5SJordan Crouse static inline u16 temp2reg(struct adt7475_data *data, long val)
2351c301fc5SJordan Crouse {
2361c301fc5SJordan Crouse 	u16 ret;
2371c301fc5SJordan Crouse 
2381c301fc5SJordan Crouse 	if (!(data->config5 & CONFIG5_TWOSCOMP)) {
2392a844c14SGuenter Roeck 		val = clamp_val(val, -64000, 191000);
2401c301fc5SJordan Crouse 		ret = (val + 64500) / 1000;
2411c301fc5SJordan Crouse 	} else {
2422a844c14SGuenter Roeck 		val = clamp_val(val, -128000, 127000);
2431c301fc5SJordan Crouse 		if (val < -500)
2441c301fc5SJordan Crouse 			ret = (256500 + val) / 1000;
2451c301fc5SJordan Crouse 		else
2461c301fc5SJordan Crouse 			ret = (val + 500) / 1000;
2471c301fc5SJordan Crouse 	}
2481c301fc5SJordan Crouse 
2491c301fc5SJordan Crouse 	return ret << 2;
2501c301fc5SJordan Crouse }
2511c301fc5SJordan Crouse 
2521c301fc5SJordan Crouse /* Given a register value, convert it to a real temp value */
2531c301fc5SJordan Crouse 
2541c301fc5SJordan Crouse static inline int reg2temp(struct adt7475_data *data, u16 reg)
2551c301fc5SJordan Crouse {
2561c301fc5SJordan Crouse 	if (data->config5 & CONFIG5_TWOSCOMP) {
2571c301fc5SJordan Crouse 		if (reg >= 512)
2581c301fc5SJordan Crouse 			return (reg - 1024) * 250;
2591c301fc5SJordan Crouse 		else
2601c301fc5SJordan Crouse 			return reg * 250;
2611c301fc5SJordan Crouse 	} else
2621c301fc5SJordan Crouse 		return (reg - 256) * 250;
2631c301fc5SJordan Crouse }
2641c301fc5SJordan Crouse 
2651c301fc5SJordan Crouse static inline int tach2rpm(u16 tach)
2661c301fc5SJordan Crouse {
2671c301fc5SJordan Crouse 	if (tach == 0 || tach == 0xFFFF)
2681c301fc5SJordan Crouse 		return 0;
2691c301fc5SJordan Crouse 
2701c301fc5SJordan Crouse 	return (90000 * 60) / tach;
2711c301fc5SJordan Crouse }
2721c301fc5SJordan Crouse 
2731c301fc5SJordan Crouse static inline u16 rpm2tach(unsigned long rpm)
2741c301fc5SJordan Crouse {
2751c301fc5SJordan Crouse 	if (rpm == 0)
2761c301fc5SJordan Crouse 		return 0;
2771c301fc5SJordan Crouse 
2782a844c14SGuenter Roeck 	return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
2791c301fc5SJordan Crouse }
2801c301fc5SJordan Crouse 
281cffb9dd0SJean Delvare /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
28262c11e46STimothy Pearson static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 2][2] = {
283cffb9dd0SJean Delvare 	{ 45, 94 },	/* +2.5V */
284cffb9dd0SJean Delvare 	{ 175, 525 },	/* Vccp */
285cffb9dd0SJean Delvare 	{ 68, 71 },	/* Vcc */
286cffb9dd0SJean Delvare 	{ 93, 47 },	/* +5V */
287cffb9dd0SJean Delvare 	{ 120, 20 },	/* +12V */
288cffb9dd0SJean Delvare 	{ 45, 45 },	/* Vtt */
28962c11e46STimothy Pearson 	{ 45, 45 },	/* Imon */
290cffb9dd0SJean Delvare };
291cffb9dd0SJean Delvare 
292ebfaf1fbSJean Delvare static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
2931c301fc5SJordan Crouse {
294cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
295cffb9dd0SJean Delvare 
296ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
297ebfaf1fbSJean Delvare 		return DIV_ROUND_CLOSEST(reg * 2250, 1024);
298cffb9dd0SJean Delvare 	return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
2991c301fc5SJordan Crouse }
3001c301fc5SJordan Crouse 
301ebfaf1fbSJean Delvare static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
3021c301fc5SJordan Crouse {
303cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
304cffb9dd0SJean Delvare 	long reg;
3051c301fc5SJordan Crouse 
306ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
307cf3ca187SLuuk Paulussen 		reg = DIV_ROUND_CLOSEST(volt * 1024, 2250);
308ebfaf1fbSJean Delvare 	else
309cf3ca187SLuuk Paulussen 		reg = DIV_ROUND_CLOSEST(volt * r[1] * 1024,
310cf3ca187SLuuk Paulussen 					(r[0] + r[1]) * 2250);
3112a844c14SGuenter Roeck 	return clamp_val(reg, 0, 1023) & (0xff << 2);
3121c301fc5SJordan Crouse }
3131c301fc5SJordan Crouse 
314f196dec6SDan Carpenter static int adt7475_read_word(struct i2c_client *client, int reg)
3151c301fc5SJordan Crouse {
316f196dec6SDan Carpenter 	int val1, val2;
3171c301fc5SJordan Crouse 
318f196dec6SDan Carpenter 	val1 = i2c_smbus_read_byte_data(client, reg);
319f196dec6SDan Carpenter 	if (val1 < 0)
320f196dec6SDan Carpenter 		return val1;
321f196dec6SDan Carpenter 	val2 = i2c_smbus_read_byte_data(client, reg + 1);
322f196dec6SDan Carpenter 	if (val2 < 0)
323f196dec6SDan Carpenter 		return val2;
3241c301fc5SJordan Crouse 
325f196dec6SDan Carpenter 	return val1 | (val2 << 8);
3261c301fc5SJordan Crouse }
3271c301fc5SJordan Crouse 
3281c301fc5SJordan Crouse static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
3291c301fc5SJordan Crouse {
3301c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
3311c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, val & 0xFF);
3321c301fc5SJordan Crouse }
3331c301fc5SJordan Crouse 
334c24f9ba9SGuenter Roeck static ssize_t voltage_show(struct device *dev, struct device_attribute *attr,
3351c301fc5SJordan Crouse 			    char *buf)
3361c301fc5SJordan Crouse {
3371c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3381c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3391c301fc5SJordan Crouse 	unsigned short val;
3401c301fc5SJordan Crouse 
3414afec79fSTokunori Ikegami 	if (IS_ERR(data))
3424afec79fSTokunori Ikegami 		return PTR_ERR(data);
3434afec79fSTokunori Ikegami 
3441c301fc5SJordan Crouse 	switch (sattr->nr) {
3451c301fc5SJordan Crouse 	case ALARM:
3461c301fc5SJordan Crouse 		return sprintf(buf, "%d\n",
347cffb9dd0SJean Delvare 			       (data->alarms >> sattr->index) & 1);
3481c301fc5SJordan Crouse 	default:
3491c301fc5SJordan Crouse 		val = data->voltage[sattr->nr][sattr->index];
350ebfaf1fbSJean Delvare 		return sprintf(buf, "%d\n",
351ebfaf1fbSJean Delvare 			       reg2volt(sattr->index, val, data->bypass_attn));
3521c301fc5SJordan Crouse 	}
3531c301fc5SJordan Crouse }
3541c301fc5SJordan Crouse 
355c24f9ba9SGuenter Roeck static ssize_t voltage_store(struct device *dev,
356c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
357c24f9ba9SGuenter Roeck 			     size_t count)
3581c301fc5SJordan Crouse {
3591c301fc5SJordan Crouse 
3601c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
361c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
362c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
3631c301fc5SJordan Crouse 	unsigned char reg;
3641c301fc5SJordan Crouse 	long val;
3651c301fc5SJordan Crouse 
366179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
3671c301fc5SJordan Crouse 		return -EINVAL;
3681c301fc5SJordan Crouse 
3691c301fc5SJordan Crouse 	mutex_lock(&data->lock);
3701c301fc5SJordan Crouse 
371ebfaf1fbSJean Delvare 	data->voltage[sattr->nr][sattr->index] =
372ebfaf1fbSJean Delvare 				volt2reg(sattr->index, val, data->bypass_attn);
3731c301fc5SJordan Crouse 
3743d849981SJean Delvare 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
3751c301fc5SJordan Crouse 		if (sattr->nr == MIN)
3761c301fc5SJordan Crouse 			reg = VOLTAGE_MIN_REG(sattr->index);
3771c301fc5SJordan Crouse 		else
3781c301fc5SJordan Crouse 			reg = VOLTAGE_MAX_REG(sattr->index);
37962c11e46STimothy Pearson 	} else if (sattr->index == 5) {
3803d849981SJean Delvare 		if (sattr->nr == MIN)
3813d849981SJean Delvare 			reg = REG_VTT_MIN;
3823d849981SJean Delvare 		else
3833d849981SJean Delvare 			reg = REG_VTT_MAX;
38462c11e46STimothy Pearson 	} else {
38562c11e46STimothy Pearson 		if (sattr->nr == MIN)
38662c11e46STimothy Pearson 			reg = REG_IMON_MIN;
38762c11e46STimothy Pearson 		else
38862c11e46STimothy Pearson 			reg = REG_IMON_MAX;
3893d849981SJean Delvare 	}
3901c301fc5SJordan Crouse 
3911c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
3921c301fc5SJordan Crouse 				  data->voltage[sattr->nr][sattr->index] >> 2);
3931c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
3941c301fc5SJordan Crouse 
3951c301fc5SJordan Crouse 	return count;
3961c301fc5SJordan Crouse }
3971c301fc5SJordan Crouse 
398c24f9ba9SGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
3991c301fc5SJordan Crouse 			 char *buf)
4001c301fc5SJordan Crouse {
4011c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
4021c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4031c301fc5SJordan Crouse 	int out;
4041c301fc5SJordan Crouse 
4054afec79fSTokunori Ikegami 	if (IS_ERR(data))
4064afec79fSTokunori Ikegami 		return PTR_ERR(data);
4074afec79fSTokunori Ikegami 
4081c301fc5SJordan Crouse 	switch (sattr->nr) {
4091c301fc5SJordan Crouse 	case HYSTERSIS:
4101c301fc5SJordan Crouse 		mutex_lock(&data->lock);
4111c301fc5SJordan Crouse 		out = data->temp[sattr->nr][sattr->index];
4121c301fc5SJordan Crouse 		if (sattr->index != 1)
4131c301fc5SJordan Crouse 			out = (out >> 4) & 0xF;
4141c301fc5SJordan Crouse 		else
4151c301fc5SJordan Crouse 			out = (out & 0xF);
4169ed5bc24SGuenter Roeck 		/*
4179ed5bc24SGuenter Roeck 		 * Show the value as an absolute number tied to
4189ed5bc24SGuenter Roeck 		 * THERM
4199ed5bc24SGuenter Roeck 		 */
4201c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
4211c301fc5SJordan Crouse 			out * 1000;
4221c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4231c301fc5SJordan Crouse 		break;
4241c301fc5SJordan Crouse 
4251c301fc5SJordan Crouse 	case OFFSET:
4269ed5bc24SGuenter Roeck 		/*
4279ed5bc24SGuenter Roeck 		 * Offset is always 2's complement, regardless of the
4289ed5bc24SGuenter Roeck 		 * setting in CONFIG5
4299ed5bc24SGuenter Roeck 		 */
4301c301fc5SJordan Crouse 		mutex_lock(&data->lock);
4311c301fc5SJordan Crouse 		out = (s8)data->temp[sattr->nr][sattr->index];
4321c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET)
4331c301fc5SJordan Crouse 			out *= 1000;
4341c301fc5SJordan Crouse 		else
4351c301fc5SJordan Crouse 			out *= 500;
4361c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4371c301fc5SJordan Crouse 		break;
4381c301fc5SJordan Crouse 
4391c301fc5SJordan Crouse 	case ALARM:
4401c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 4)) & 1;
4411c301fc5SJordan Crouse 		break;
4421c301fc5SJordan Crouse 
4431c301fc5SJordan Crouse 	case FAULT:
4441c301fc5SJordan Crouse 		/* Note - only for remote1 and remote2 */
445cf312e07SJean Delvare 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
4461c301fc5SJordan Crouse 		break;
4471c301fc5SJordan Crouse 
4481c301fc5SJordan Crouse 	default:
4491c301fc5SJordan Crouse 		/* All other temp values are in the configured format */
4501c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
4511c301fc5SJordan Crouse 	}
4521c301fc5SJordan Crouse 
4531c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
4541c301fc5SJordan Crouse }
4551c301fc5SJordan Crouse 
456c24f9ba9SGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
4571c301fc5SJordan Crouse 			  const char *buf, size_t count)
4581c301fc5SJordan Crouse {
4591c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
460c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
461c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
4621c301fc5SJordan Crouse 	unsigned char reg = 0;
4631c301fc5SJordan Crouse 	u8 out;
4641c301fc5SJordan Crouse 	int temp;
4651c301fc5SJordan Crouse 	long val;
4661c301fc5SJordan Crouse 
467179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
4681c301fc5SJordan Crouse 		return -EINVAL;
4691c301fc5SJordan Crouse 
4701c301fc5SJordan Crouse 	mutex_lock(&data->lock);
4711c301fc5SJordan Crouse 
4721c301fc5SJordan Crouse 	/* We need the config register in all cases for temp <-> reg conv. */
4731c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
4741c301fc5SJordan Crouse 
4751c301fc5SJordan Crouse 	switch (sattr->nr) {
4761c301fc5SJordan Crouse 	case OFFSET:
4771c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET) {
4782a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 127000);
4791c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 1000;
4801c301fc5SJordan Crouse 		} else {
4812a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 64000);
4821c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 500;
4831c301fc5SJordan Crouse 		}
4841c301fc5SJordan Crouse 		break;
4851c301fc5SJordan Crouse 
4861c301fc5SJordan Crouse 	case HYSTERSIS:
4879ed5bc24SGuenter Roeck 		/*
4889ed5bc24SGuenter Roeck 		 * The value will be given as an absolute value, turn it
4899ed5bc24SGuenter Roeck 		 * into an offset based on THERM
4909ed5bc24SGuenter Roeck 		 */
4911c301fc5SJordan Crouse 
4921c301fc5SJordan Crouse 		/* Read fresh THERM and HYSTERSIS values from the chip */
4931c301fc5SJordan Crouse 		data->temp[THERM][sattr->index] =
4941c301fc5SJordan Crouse 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
4951c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
4961c301fc5SJordan Crouse 
4971c301fc5SJordan Crouse 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
4982a844c14SGuenter Roeck 		val = clamp_val(val, temp - 15000, temp);
4991c301fc5SJordan Crouse 		val = (temp - val) / 1000;
5001c301fc5SJordan Crouse 
5011c301fc5SJordan Crouse 		if (sattr->index != 1) {
50248e81868STony O'Brien 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
5031c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
5041c301fc5SJordan Crouse 		} else {
50548e81868STony O'Brien 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
5061c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
5071c301fc5SJordan Crouse 		}
5081c301fc5SJordan Crouse 
5091c301fc5SJordan Crouse 		out = data->temp[HYSTERSIS][sattr->index];
5101c301fc5SJordan Crouse 		break;
5111c301fc5SJordan Crouse 
5121c301fc5SJordan Crouse 	default:
5131c301fc5SJordan Crouse 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
5141c301fc5SJordan Crouse 
5159ed5bc24SGuenter Roeck 		/*
5169ed5bc24SGuenter Roeck 		 * We maintain an extra 2 digits of precision for simplicity
5179ed5bc24SGuenter Roeck 		 * - shift those back off before writing the value
5189ed5bc24SGuenter Roeck 		 */
5191c301fc5SJordan Crouse 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
5201c301fc5SJordan Crouse 	}
5211c301fc5SJordan Crouse 
5221c301fc5SJordan Crouse 	switch (sattr->nr) {
5231c301fc5SJordan Crouse 	case MIN:
5241c301fc5SJordan Crouse 		reg = TEMP_MIN_REG(sattr->index);
5251c301fc5SJordan Crouse 		break;
5261c301fc5SJordan Crouse 	case MAX:
5271c301fc5SJordan Crouse 		reg = TEMP_MAX_REG(sattr->index);
5281c301fc5SJordan Crouse 		break;
5291c301fc5SJordan Crouse 	case OFFSET:
5301c301fc5SJordan Crouse 		reg = TEMP_OFFSET_REG(sattr->index);
5311c301fc5SJordan Crouse 		break;
5321c301fc5SJordan Crouse 	case AUTOMIN:
5331c301fc5SJordan Crouse 		reg = TEMP_TMIN_REG(sattr->index);
5341c301fc5SJordan Crouse 		break;
5351c301fc5SJordan Crouse 	case THERM:
5361c301fc5SJordan Crouse 		reg = TEMP_THERM_REG(sattr->index);
5371c301fc5SJordan Crouse 		break;
5381c301fc5SJordan Crouse 	case HYSTERSIS:
5391c301fc5SJordan Crouse 		if (sattr->index != 2)
5401c301fc5SJordan Crouse 			reg = REG_REMOTE1_HYSTERSIS;
5411c301fc5SJordan Crouse 		else
5421c301fc5SJordan Crouse 			reg = REG_REMOTE2_HYSTERSIS;
5431c301fc5SJordan Crouse 
5441c301fc5SJordan Crouse 		break;
5451c301fc5SJordan Crouse 	}
5461c301fc5SJordan Crouse 
5471c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, out);
5481c301fc5SJordan Crouse 
5491c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5501c301fc5SJordan Crouse 	return count;
5511c301fc5SJordan Crouse }
5521c301fc5SJordan Crouse 
5538f05bcc3SChris Packham /* Assuming CONFIG6[SLOW] is 0 */
5548f05bcc3SChris Packham static const int ad7475_st_map[] = {
5558f05bcc3SChris Packham 	37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
5568f05bcc3SChris Packham };
5578f05bcc3SChris Packham 
558c24f9ba9SGuenter Roeck static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr,
5598f05bcc3SChris Packham 			    char *buf)
5608f05bcc3SChris Packham {
5618f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
562c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
5638f05bcc3SChris Packham 	long val;
5648f05bcc3SChris Packham 
5658f05bcc3SChris Packham 	switch (sattr->index) {
5668f05bcc3SChris Packham 	case 0:
5678f05bcc3SChris Packham 		val = data->enh_acoustics[0] & 0xf;
5688f05bcc3SChris Packham 		break;
5698f05bcc3SChris Packham 	case 1:
5705f8d1e3bSTony O'Brien 		val = data->enh_acoustics[1] & 0xf;
5718f05bcc3SChris Packham 		break;
5728f05bcc3SChris Packham 	case 2:
5738f05bcc3SChris Packham 	default:
5745f8d1e3bSTony O'Brien 		val = (data->enh_acoustics[1] >> 4) & 0xf;
5758f05bcc3SChris Packham 		break;
5768f05bcc3SChris Packham 	}
5778f05bcc3SChris Packham 
5788f05bcc3SChris Packham 	if (val & 0x8)
5798f05bcc3SChris Packham 		return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
5808f05bcc3SChris Packham 	else
5818f05bcc3SChris Packham 		return sprintf(buf, "0\n");
5828f05bcc3SChris Packham }
5838f05bcc3SChris Packham 
584c24f9ba9SGuenter Roeck static ssize_t temp_st_store(struct device *dev,
585c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
586c24f9ba9SGuenter Roeck 			     size_t count)
5878f05bcc3SChris Packham {
5888f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
589c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
590c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
5918f05bcc3SChris Packham 	unsigned char reg;
5928f05bcc3SChris Packham 	int shift, idx;
5938f05bcc3SChris Packham 	ulong val;
5948f05bcc3SChris Packham 
5958f05bcc3SChris Packham 	if (kstrtoul(buf, 10, &val))
5968f05bcc3SChris Packham 		return -EINVAL;
5978f05bcc3SChris Packham 
5988f05bcc3SChris Packham 	switch (sattr->index) {
5998f05bcc3SChris Packham 	case 0:
6008f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS1;
6018f05bcc3SChris Packham 		shift = 0;
6028f05bcc3SChris Packham 		idx = 0;
6038f05bcc3SChris Packham 		break;
6048f05bcc3SChris Packham 	case 1:
6058f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
6068f05bcc3SChris Packham 		shift = 0;
6078f05bcc3SChris Packham 		idx = 1;
6088f05bcc3SChris Packham 		break;
6098f05bcc3SChris Packham 	case 2:
6108f05bcc3SChris Packham 	default:
6118f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
6128f05bcc3SChris Packham 		shift = 4;
6138f05bcc3SChris Packham 		idx = 1;
6148f05bcc3SChris Packham 		break;
6158f05bcc3SChris Packham 	}
6168f05bcc3SChris Packham 
6178f05bcc3SChris Packham 	if (val > 0) {
6188f05bcc3SChris Packham 		val = find_closest_descending(val, ad7475_st_map,
6198f05bcc3SChris Packham 					      ARRAY_SIZE(ad7475_st_map));
6208f05bcc3SChris Packham 		val |= 0x8;
6218f05bcc3SChris Packham 	}
6228f05bcc3SChris Packham 
6238f05bcc3SChris Packham 	mutex_lock(&data->lock);
6248f05bcc3SChris Packham 
6258f05bcc3SChris Packham 	data->enh_acoustics[idx] &= ~(0xf << shift);
6268f05bcc3SChris Packham 	data->enh_acoustics[idx] |= (val << shift);
6278f05bcc3SChris Packham 
6288f05bcc3SChris Packham 	i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
6298f05bcc3SChris Packham 
6308f05bcc3SChris Packham 	mutex_unlock(&data->lock);
6318f05bcc3SChris Packham 
6328f05bcc3SChris Packham 	return count;
6338f05bcc3SChris Packham }
6348f05bcc3SChris Packham 
6359ed5bc24SGuenter Roeck /*
6369ed5bc24SGuenter Roeck  * Table of autorange values - the user will write the value in millidegrees,
6379ed5bc24SGuenter Roeck  * and we'll convert it
6389ed5bc24SGuenter Roeck  */
6391c301fc5SJordan Crouse static const int autorange_table[] = {
6401c301fc5SJordan Crouse 	2000, 2500, 3330, 4000, 5000, 6670, 8000,
6411c301fc5SJordan Crouse 	10000, 13330, 16000, 20000, 26670, 32000, 40000,
6421c301fc5SJordan Crouse 	53330, 80000
6431c301fc5SJordan Crouse };
6441c301fc5SJordan Crouse 
645c24f9ba9SGuenter Roeck static ssize_t point2_show(struct device *dev, struct device_attribute *attr,
6461c301fc5SJordan Crouse 			   char *buf)
6471c301fc5SJordan Crouse {
6481c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6491c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6501c301fc5SJordan Crouse 	int out, val;
6511c301fc5SJordan Crouse 
6524afec79fSTokunori Ikegami 	if (IS_ERR(data))
6534afec79fSTokunori Ikegami 		return PTR_ERR(data);
6544afec79fSTokunori Ikegami 
6551c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6561c301fc5SJordan Crouse 	out = (data->range[sattr->index] >> 4) & 0x0F;
6571c301fc5SJordan Crouse 	val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6581c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6591c301fc5SJordan Crouse 
6601c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", val + autorange_table[out]);
6611c301fc5SJordan Crouse }
6621c301fc5SJordan Crouse 
663c24f9ba9SGuenter Roeck static ssize_t point2_store(struct device *dev, struct device_attribute *attr,
6641c301fc5SJordan Crouse 			    const char *buf, size_t count)
6651c301fc5SJordan Crouse {
666c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
667c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
6681c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6691c301fc5SJordan Crouse 	int temp;
6701c301fc5SJordan Crouse 	long val;
6711c301fc5SJordan Crouse 
672179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
6731c301fc5SJordan Crouse 		return -EINVAL;
6741c301fc5SJordan Crouse 
6751c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6761c301fc5SJordan Crouse 
6771c301fc5SJordan Crouse 	/* Get a fresh copy of the needed registers */
6781c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
6791c301fc5SJordan Crouse 	data->temp[AUTOMIN][sattr->index] =
6801c301fc5SJordan Crouse 		adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
6811c301fc5SJordan Crouse 	data->range[sattr->index] =
6821c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
6831c301fc5SJordan Crouse 
6849ed5bc24SGuenter Roeck 	/*
6859ed5bc24SGuenter Roeck 	 * The user will write an absolute value, so subtract the start point
6869ed5bc24SGuenter Roeck 	 * to figure the range
6879ed5bc24SGuenter Roeck 	 */
6881c301fc5SJordan Crouse 	temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6892a844c14SGuenter Roeck 	val = clamp_val(val, temp + autorange_table[0],
6901c301fc5SJordan Crouse 		temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
6911c301fc5SJordan Crouse 	val -= temp;
6921c301fc5SJordan Crouse 
6931c301fc5SJordan Crouse 	/* Find the nearest table entry to what the user wrote */
694e4651640SChris Packham 	val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
6951c301fc5SJordan Crouse 
6961c301fc5SJordan Crouse 	data->range[sattr->index] &= ~0xF0;
6971c301fc5SJordan Crouse 	data->range[sattr->index] |= val << 4;
6981c301fc5SJordan Crouse 
6991c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
7001c301fc5SJordan Crouse 				  data->range[sattr->index]);
7011c301fc5SJordan Crouse 
7021c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7031c301fc5SJordan Crouse 	return count;
7041c301fc5SJordan Crouse }
7051c301fc5SJordan Crouse 
706c24f9ba9SGuenter Roeck static ssize_t tach_show(struct device *dev, struct device_attribute *attr,
7071c301fc5SJordan Crouse 			 char *buf)
7081c301fc5SJordan Crouse {
7091c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7101c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7111c301fc5SJordan Crouse 	int out;
7121c301fc5SJordan Crouse 
7134afec79fSTokunori Ikegami 	if (IS_ERR(data))
7144afec79fSTokunori Ikegami 		return PTR_ERR(data);
7154afec79fSTokunori Ikegami 
7161c301fc5SJordan Crouse 	if (sattr->nr == ALARM)
7171c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 10)) & 1;
7181c301fc5SJordan Crouse 	else
7191c301fc5SJordan Crouse 		out = tach2rpm(data->tach[sattr->nr][sattr->index]);
7201c301fc5SJordan Crouse 
7211c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
7221c301fc5SJordan Crouse }
7231c301fc5SJordan Crouse 
724c24f9ba9SGuenter Roeck static ssize_t tach_store(struct device *dev, struct device_attribute *attr,
7251c301fc5SJordan Crouse 			  const char *buf, size_t count)
7261c301fc5SJordan Crouse {
7271c301fc5SJordan Crouse 
7281c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
729c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
730c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
7311c301fc5SJordan Crouse 	unsigned long val;
7321c301fc5SJordan Crouse 
733179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
7341c301fc5SJordan Crouse 		return -EINVAL;
7351c301fc5SJordan Crouse 
7361c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7371c301fc5SJordan Crouse 
7381c301fc5SJordan Crouse 	data->tach[MIN][sattr->index] = rpm2tach(val);
7391c301fc5SJordan Crouse 
7401c301fc5SJordan Crouse 	adt7475_write_word(client, TACH_MIN_REG(sattr->index),
7411c301fc5SJordan Crouse 			   data->tach[MIN][sattr->index]);
7421c301fc5SJordan Crouse 
7431c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7441c301fc5SJordan Crouse 	return count;
7451c301fc5SJordan Crouse }
7461c301fc5SJordan Crouse 
747c24f9ba9SGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
7481c301fc5SJordan Crouse 			char *buf)
7491c301fc5SJordan Crouse {
7501c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7511c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7521c301fc5SJordan Crouse 
7534afec79fSTokunori Ikegami 	if (IS_ERR(data))
7544afec79fSTokunori Ikegami 		return PTR_ERR(data);
7554afec79fSTokunori Ikegami 
7561c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
7571c301fc5SJordan Crouse }
7581c301fc5SJordan Crouse 
759c24f9ba9SGuenter Roeck static ssize_t pwmchan_show(struct device *dev, struct device_attribute *attr,
7601c301fc5SJordan Crouse 			    char *buf)
7611c301fc5SJordan Crouse {
7621c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7631c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7641c301fc5SJordan Crouse 
7654afec79fSTokunori Ikegami 	if (IS_ERR(data))
7664afec79fSTokunori Ikegami 		return PTR_ERR(data);
7674afec79fSTokunori Ikegami 
7681c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
7691c301fc5SJordan Crouse }
7701c301fc5SJordan Crouse 
771c24f9ba9SGuenter Roeck static ssize_t pwmctrl_show(struct device *dev, struct device_attribute *attr,
7721c301fc5SJordan Crouse 			    char *buf)
7731c301fc5SJordan Crouse {
7741c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7751c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7761c301fc5SJordan Crouse 
7774afec79fSTokunori Ikegami 	if (IS_ERR(data))
7784afec79fSTokunori Ikegami 		return PTR_ERR(data);
7794afec79fSTokunori Ikegami 
7801c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
7811c301fc5SJordan Crouse }
7821c301fc5SJordan Crouse 
783c24f9ba9SGuenter Roeck static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
7841c301fc5SJordan Crouse 			 const char *buf, size_t count)
7851c301fc5SJordan Crouse {
7861c301fc5SJordan Crouse 
7871c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
788c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
789c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
7901c301fc5SJordan Crouse 	unsigned char reg = 0;
7911c301fc5SJordan Crouse 	long val;
7921c301fc5SJordan Crouse 
793179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
7941c301fc5SJordan Crouse 		return -EINVAL;
7951c301fc5SJordan Crouse 
7961c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7971c301fc5SJordan Crouse 
7981c301fc5SJordan Crouse 	switch (sattr->nr) {
7991c301fc5SJordan Crouse 	case INPUT:
8001c301fc5SJordan Crouse 		/* Get a fresh value for CONTROL */
8011c301fc5SJordan Crouse 		data->pwm[CONTROL][sattr->index] =
8021c301fc5SJordan Crouse 			adt7475_read(PWM_CONFIG_REG(sattr->index));
8031c301fc5SJordan Crouse 
8049ed5bc24SGuenter Roeck 		/*
8059ed5bc24SGuenter Roeck 		 * If we are not in manual mode, then we shouldn't allow
8069ed5bc24SGuenter Roeck 		 * the user to set the pwm speed
8079ed5bc24SGuenter Roeck 		 */
8081c301fc5SJordan Crouse 		if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
8091c301fc5SJordan Crouse 			mutex_unlock(&data->lock);
8101c301fc5SJordan Crouse 			return count;
8111c301fc5SJordan Crouse 		}
8121c301fc5SJordan Crouse 
8131c301fc5SJordan Crouse 		reg = PWM_REG(sattr->index);
8141c301fc5SJordan Crouse 		break;
8151c301fc5SJordan Crouse 
8161c301fc5SJordan Crouse 	case MIN:
8171c301fc5SJordan Crouse 		reg = PWM_MIN_REG(sattr->index);
8181c301fc5SJordan Crouse 		break;
8191c301fc5SJordan Crouse 
8201c301fc5SJordan Crouse 	case MAX:
8211c301fc5SJordan Crouse 		reg = PWM_MAX_REG(sattr->index);
8221c301fc5SJordan Crouse 		break;
8231c301fc5SJordan Crouse 	}
8241c301fc5SJordan Crouse 
8252a844c14SGuenter Roeck 	data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
8261c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
8271c301fc5SJordan Crouse 				  data->pwm[sattr->nr][sattr->index]);
8281d58f5efSChris Packham 	mutex_unlock(&data->lock);
8291d58f5efSChris Packham 
8301d58f5efSChris Packham 	return count;
8311d58f5efSChris Packham }
8321d58f5efSChris Packham 
833c24f9ba9SGuenter Roeck static ssize_t stall_disable_show(struct device *dev,
8341d58f5efSChris Packham 				  struct device_attribute *attr, char *buf)
8351d58f5efSChris Packham {
8361d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
837c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
838c64fce7fSGrant McEwan 
8391d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8401d58f5efSChris Packham 
8411d58f5efSChris Packham 	return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
8421d58f5efSChris Packham }
8431d58f5efSChris Packham 
844c24f9ba9SGuenter Roeck static ssize_t stall_disable_store(struct device *dev,
845c24f9ba9SGuenter Roeck 				   struct device_attribute *attr,
846c24f9ba9SGuenter Roeck 				   const char *buf, size_t count)
8471d58f5efSChris Packham {
8481d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
849c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
850c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
8511d58f5efSChris Packham 	long val;
8521d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8531d58f5efSChris Packham 
8541d58f5efSChris Packham 	if (kstrtol(buf, 10, &val))
8551d58f5efSChris Packham 		return -EINVAL;
8561d58f5efSChris Packham 
8571d58f5efSChris Packham 	mutex_lock(&data->lock);
8581d58f5efSChris Packham 
8591d58f5efSChris Packham 	data->enh_acoustics[0] &= ~mask;
8601d58f5efSChris Packham 	if (val)
8611d58f5efSChris Packham 		data->enh_acoustics[0] |= mask;
8621d58f5efSChris Packham 
8631d58f5efSChris Packham 	i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
8641d58f5efSChris Packham 				  data->enh_acoustics[0]);
8651c301fc5SJordan Crouse 
8661c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8671c301fc5SJordan Crouse 
8681c301fc5SJordan Crouse 	return count;
8691c301fc5SJordan Crouse }
8701c301fc5SJordan Crouse 
8711c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */
8721c301fc5SJordan Crouse 
8731c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index,
8741c301fc5SJordan Crouse 		      unsigned int pwmctl, unsigned int pwmchan)
8751c301fc5SJordan Crouse {
8761c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8771c301fc5SJordan Crouse 	long val = 0;
8781c301fc5SJordan Crouse 
8791c301fc5SJordan Crouse 	switch (pwmctl) {
8801c301fc5SJordan Crouse 	case 0:
8811c301fc5SJordan Crouse 		val = 0x03;	/* Run at full speed */
8821c301fc5SJordan Crouse 		break;
8831c301fc5SJordan Crouse 	case 1:
8841c301fc5SJordan Crouse 		val = 0x07;	/* Manual mode */
8851c301fc5SJordan Crouse 		break;
8861c301fc5SJordan Crouse 	case 2:
8871c301fc5SJordan Crouse 		switch (pwmchan) {
8881c301fc5SJordan Crouse 		case 1:
8891c301fc5SJordan Crouse 			/* Remote1 controls PWM */
8901c301fc5SJordan Crouse 			val = 0x00;
8911c301fc5SJordan Crouse 			break;
8921c301fc5SJordan Crouse 		case 2:
8931c301fc5SJordan Crouse 			/* local controls PWM */
8941c301fc5SJordan Crouse 			val = 0x01;
8951c301fc5SJordan Crouse 			break;
8961c301fc5SJordan Crouse 		case 4:
8971c301fc5SJordan Crouse 			/* remote2 controls PWM */
8981c301fc5SJordan Crouse 			val = 0x02;
8991c301fc5SJordan Crouse 			break;
9001c301fc5SJordan Crouse 		case 6:
9011c301fc5SJordan Crouse 			/* local/remote2 control PWM */
9021c301fc5SJordan Crouse 			val = 0x05;
9031c301fc5SJordan Crouse 			break;
9041c301fc5SJordan Crouse 		case 7:
9051c301fc5SJordan Crouse 			/* All three control PWM */
9061c301fc5SJordan Crouse 			val = 0x06;
9071c301fc5SJordan Crouse 			break;
9081c301fc5SJordan Crouse 		default:
9091c301fc5SJordan Crouse 			return -EINVAL;
9101c301fc5SJordan Crouse 		}
9111c301fc5SJordan Crouse 		break;
9121c301fc5SJordan Crouse 	default:
9131c301fc5SJordan Crouse 		return -EINVAL;
9141c301fc5SJordan Crouse 	}
9151c301fc5SJordan Crouse 
9161c301fc5SJordan Crouse 	data->pwmctl[index] = pwmctl;
9171c301fc5SJordan Crouse 	data->pwmchan[index] = pwmchan;
9181c301fc5SJordan Crouse 
9191c301fc5SJordan Crouse 	data->pwm[CONTROL][index] &= ~0xE0;
9201c301fc5SJordan Crouse 	data->pwm[CONTROL][index] |= (val & 7) << 5;
9211c301fc5SJordan Crouse 
9221c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
9231c301fc5SJordan Crouse 				  data->pwm[CONTROL][index]);
9241c301fc5SJordan Crouse 
9251c301fc5SJordan Crouse 	return 0;
9261c301fc5SJordan Crouse }
9271c301fc5SJordan Crouse 
928c24f9ba9SGuenter Roeck static ssize_t pwmchan_store(struct device *dev,
929c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
930c24f9ba9SGuenter Roeck 			     size_t count)
9311c301fc5SJordan Crouse {
9321c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
933c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
934c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
9351c301fc5SJordan Crouse 	int r;
9361c301fc5SJordan Crouse 	long val;
9371c301fc5SJordan Crouse 
938179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9391c301fc5SJordan Crouse 		return -EINVAL;
9401c301fc5SJordan Crouse 
9411c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9421c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9431c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9441c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
9451c301fc5SJordan Crouse 	if (r)
9461c301fc5SJordan Crouse 		count = r;
9471c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9481c301fc5SJordan Crouse 
9491c301fc5SJordan Crouse 	return count;
9501c301fc5SJordan Crouse }
9511c301fc5SJordan Crouse 
952c24f9ba9SGuenter Roeck static ssize_t pwmctrl_store(struct device *dev,
953c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
954c24f9ba9SGuenter Roeck 			     size_t count)
9551c301fc5SJordan Crouse {
9561c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
957c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
958c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
9591c301fc5SJordan Crouse 	int r;
9601c301fc5SJordan Crouse 	long val;
9611c301fc5SJordan Crouse 
962179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9631c301fc5SJordan Crouse 		return -EINVAL;
9641c301fc5SJordan Crouse 
9651c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9661c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9671c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9681c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
9691c301fc5SJordan Crouse 	if (r)
9701c301fc5SJordan Crouse 		count = r;
9711c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9721c301fc5SJordan Crouse 
9731c301fc5SJordan Crouse 	return count;
9741c301fc5SJordan Crouse }
9751c301fc5SJordan Crouse 
9761c301fc5SJordan Crouse /* List of frequencies for the PWM */
9771c301fc5SJordan Crouse static const int pwmfreq_table[] = {
9783490c92aSChris Packham 	11, 14, 22, 29, 35, 44, 58, 88, 22500
9791c301fc5SJordan Crouse };
9801c301fc5SJordan Crouse 
981c24f9ba9SGuenter Roeck static ssize_t pwmfreq_show(struct device *dev, struct device_attribute *attr,
9821c301fc5SJordan Crouse 			    char *buf)
9831c301fc5SJordan Crouse {
9841c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
9851c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9869d19371dSDan Carpenter 	int idx;
9871c301fc5SJordan Crouse 
9884afec79fSTokunori Ikegami 	if (IS_ERR(data))
9894afec79fSTokunori Ikegami 		return PTR_ERR(data);
9909d19371dSDan Carpenter 	idx = clamp_val(data->range[sattr->index] & 0xf, 0,
9919d19371dSDan Carpenter 			ARRAY_SIZE(pwmfreq_table) - 1);
9924afec79fSTokunori Ikegami 
9939d19371dSDan Carpenter 	return sprintf(buf, "%d\n", pwmfreq_table[idx]);
9941c301fc5SJordan Crouse }
9951c301fc5SJordan Crouse 
996c24f9ba9SGuenter Roeck static ssize_t pwmfreq_store(struct device *dev,
997c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
998c24f9ba9SGuenter Roeck 			     size_t count)
9991c301fc5SJordan Crouse {
10001c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
1001c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
1002c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
10031c301fc5SJordan Crouse 	int out;
10041c301fc5SJordan Crouse 	long val;
10051c301fc5SJordan Crouse 
1006179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
10071c301fc5SJordan Crouse 		return -EINVAL;
10081c301fc5SJordan Crouse 
1009e4651640SChris Packham 	out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
10101c301fc5SJordan Crouse 
10111c301fc5SJordan Crouse 	mutex_lock(&data->lock);
10121c301fc5SJordan Crouse 
10131c301fc5SJordan Crouse 	data->range[sattr->index] =
10141c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
10153490c92aSChris Packham 	data->range[sattr->index] &= ~0xf;
10161c301fc5SJordan Crouse 	data->range[sattr->index] |= out;
10171c301fc5SJordan Crouse 
10181c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
10191c301fc5SJordan Crouse 				  data->range[sattr->index]);
10201c301fc5SJordan Crouse 
10211c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
10221c301fc5SJordan Crouse 	return count;
10231c301fc5SJordan Crouse }
10241c301fc5SJordan Crouse 
10251d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
10261d05303cSJulia Lawall 					struct device_attribute *devattr,
10271d05303cSJulia Lawall 					char *buf)
1028f99318b2SJean Delvare {
1029f99318b2SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
10309d19371dSDan Carpenter 
10319d19371dSDan Carpenter 	if (IS_ERR(data))
10329d19371dSDan Carpenter 		return PTR_ERR(data);
10339d19371dSDan Carpenter 
1034f99318b2SJean Delvare 	return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1035f99318b2SJean Delvare }
1036f99318b2SJean Delvare 
10371d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1038f99318b2SJean Delvare 					struct device_attribute *devattr,
1039f99318b2SJean Delvare 					const char *buf, size_t count)
1040f99318b2SJean Delvare {
1041c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
1042c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
1043f99318b2SJean Delvare 	long val;
1044f99318b2SJean Delvare 
1045179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
1046f99318b2SJean Delvare 		return -EINVAL;
1047f99318b2SJean Delvare 	if (val != 0 && val != 1)
1048f99318b2SJean Delvare 		return -EINVAL;
1049f99318b2SJean Delvare 
1050f99318b2SJean Delvare 	mutex_lock(&data->lock);
1051f99318b2SJean Delvare 	data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1052f99318b2SJean Delvare 	if (val)
1053f99318b2SJean Delvare 		data->config4 |= CONFIG4_MAXDUTY;
1054f99318b2SJean Delvare 	else
1055f99318b2SJean Delvare 		data->config4 &= ~CONFIG4_MAXDUTY;
1056f99318b2SJean Delvare 	i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1057f99318b2SJean Delvare 	mutex_unlock(&data->lock);
1058f99318b2SJean Delvare 
1059f99318b2SJean Delvare 	return count;
1060f99318b2SJean Delvare }
1061f99318b2SJean Delvare 
10621d05303cSJulia Lawall static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
106354fe4671SJean Delvare 			char *buf)
106454fe4671SJean Delvare {
106554fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
106654fe4671SJean Delvare 	return sprintf(buf, "%d\n", (int)data->vrm);
106754fe4671SJean Delvare }
106854fe4671SJean Delvare 
10691d05303cSJulia Lawall static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
107054fe4671SJean Delvare 			 const char *buf, size_t count)
107154fe4671SJean Delvare {
107254fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
107354fe4671SJean Delvare 	long val;
107454fe4671SJean Delvare 
1075179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
107654fe4671SJean Delvare 		return -EINVAL;
107754fe4671SJean Delvare 	if (val < 0 || val > 255)
107854fe4671SJean Delvare 		return -EINVAL;
107954fe4671SJean Delvare 	data->vrm = val;
108054fe4671SJean Delvare 
108154fe4671SJean Delvare 	return count;
108254fe4671SJean Delvare }
108354fe4671SJean Delvare 
10841d05303cSJulia Lawall static ssize_t cpu0_vid_show(struct device *dev,
10851d05303cSJulia Lawall 			     struct device_attribute *devattr, char *buf)
108654fe4671SJean Delvare {
108754fe4671SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
10884afec79fSTokunori Ikegami 
10894afec79fSTokunori Ikegami 	if (IS_ERR(data))
10904afec79fSTokunori Ikegami 		return PTR_ERR(data);
10914afec79fSTokunori Ikegami 
109254fe4671SJean Delvare 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
109354fe4671SJean Delvare }
109454fe4671SJean Delvare 
1095c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in0_input, voltage, INPUT, 0);
1096c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_max, voltage, MAX, 0);
1097c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_min, voltage, MIN, 0);
1098c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, voltage, ALARM, 0);
1099c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_input, voltage, INPUT, 1);
1100c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_max, voltage, MAX, 1);
1101c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_min, voltage, MIN, 1);
1102c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, voltage, ALARM, 1);
1103c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_input, voltage, INPUT, 2);
1104c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_max, voltage, MAX, 2);
1105c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_min, voltage, MIN, 2);
1106c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, voltage, ALARM, 2);
1107c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_input, voltage, INPUT, 3);
1108c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_max, voltage, MAX, 3);
1109c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_min, voltage, MIN, 3);
1110c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, voltage, ALARM, 3);
1111c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_input, voltage, INPUT, 4);
1112c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_max, voltage, MAX, 4);
1113c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_min, voltage, MIN, 4);
1114c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, voltage, ALARM, 8);
1115c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in5_input, voltage, INPUT, 5);
1116c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_max, voltage, MAX, 5);
1117c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_min, voltage, MIN, 5);
1118c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, voltage, ALARM, 31);
111962c11e46STimothy Pearson static SENSOR_DEVICE_ATTR_2_RO(in6_input, voltage, INPUT, 6);
112062c11e46STimothy Pearson static SENSOR_DEVICE_ATTR_2_RW(in6_max, voltage, MAX, 6);
112162c11e46STimothy Pearson static SENSOR_DEVICE_ATTR_2_RW(in6_min, voltage, MIN, 6);
112262c11e46STimothy Pearson static SENSOR_DEVICE_ATTR_2_RO(in6_alarm, voltage, ALARM, 30);
1123c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, INPUT, 0);
1124c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, temp, ALARM, 0);
1125c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, temp, FAULT, 0);
1126c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, MAX, 0);
1127c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, MIN, 0);
1128c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_offset, temp, OFFSET, 0);
1129c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp, temp, AUTOMIN, 0);
1130c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, point2, 0, 0);
1131c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, THERM, 0);
1132c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst, temp, HYSTERSIS, 0);
1133c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing, temp_st, 0, 0);
1134c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, INPUT, 1);
1135c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm, temp, ALARM, 1);
1136c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, MAX, 1);
1137c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, MIN, 1);
1138c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, OFFSET, 1);
1139c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp, AUTOMIN, 1);
1140c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, point2, 0, 1);
1141c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, THERM, 1);
1142c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst, temp, HYSTERSIS, 1);
1143c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing, temp_st, 0, 1);
1144c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, INPUT, 2);
1145c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm, temp, ALARM, 2);
1146c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, temp, FAULT, 2);
1147c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, MAX, 2);
1148c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, MIN, 2);
1149c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_offset, temp, OFFSET, 2);
1150c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp, temp, AUTOMIN, 2);
1151c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp, point2, 0, 2);
1152c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, THERM, 2);
1153c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst, temp, HYSTERSIS, 2);
1154c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing, temp_st, 0, 2);
1155c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan1_input, tach, INPUT, 0);
1156c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan1_min, tach, MIN, 0);
1157c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, tach, ALARM, 0);
1158c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan2_input, tach, INPUT, 1);
1159c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan2_min, tach, MIN, 1);
1160c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, tach, ALARM, 1);
1161c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan3_input, tach, INPUT, 2);
1162c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan3_min, tach, MIN, 2);
1163c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, tach, ALARM, 2);
1164c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan4_input, tach, INPUT, 3);
1165c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan4_min, tach, MIN, 3);
1166c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm, tach, ALARM, 3);
1167c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1, pwm, INPUT, 0);
1168c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq, pwmfreq, INPUT, 0);
1169c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable, pwmctrl, INPUT, 0);
1170c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp, pwmchan, INPUT, 0);
1171c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, pwm, MIN, 0);
1172c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, pwm, MAX, 0);
1173c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable, stall_disable, 0, 0);
1174c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2, pwm, INPUT, 1);
1175c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq, pwmfreq, INPUT, 1);
1176c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable, pwmctrl, INPUT, 1);
1177c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp, pwmchan, INPUT, 1);
1178c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, pwm, MIN, 1);
1179c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, pwm, MAX, 1);
1180c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable, stall_disable, 0, 1);
1181c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3, pwm, INPUT, 2);
1182c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq, pwmfreq, INPUT, 2);
1183c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable, pwmctrl, INPUT, 2);
1184c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp, pwmchan, INPUT, 2);
1185c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, pwm, MIN, 2);
1186c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, pwm, MAX, 2);
1187c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable, stall_disable, 0, 2);
11881c301fc5SJordan Crouse 
1189f99318b2SJean Delvare /* Non-standard name, might need revisiting */
11901d05303cSJulia Lawall static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1191f99318b2SJean Delvare 
11921d05303cSJulia Lawall static DEVICE_ATTR_RW(vrm);
11931d05303cSJulia Lawall static DEVICE_ATTR_RO(cpu0_vid);
119454fe4671SJean Delvare 
11951c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = {
11961c301fc5SJordan Crouse 	&sensor_dev_attr_in1_input.dev_attr.attr,
11971c301fc5SJordan Crouse 	&sensor_dev_attr_in1_max.dev_attr.attr,
11981c301fc5SJordan Crouse 	&sensor_dev_attr_in1_min.dev_attr.attr,
11991c301fc5SJordan Crouse 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
12001c301fc5SJordan Crouse 	&sensor_dev_attr_in2_input.dev_attr.attr,
12011c301fc5SJordan Crouse 	&sensor_dev_attr_in2_max.dev_attr.attr,
12021c301fc5SJordan Crouse 	&sensor_dev_attr_in2_min.dev_attr.attr,
12031c301fc5SJordan Crouse 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
12041c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_input.dev_attr.attr,
12051c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
12061c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
12071c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_max.dev_attr.attr,
12081c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_min.dev_attr.attr,
12091c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
12101c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
12111c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
12121c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
12131c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
12148f05bcc3SChris Packham 	&sensor_dev_attr_temp1_smoothing.dev_attr.attr,
12151c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_input.dev_attr.attr,
12161c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
12171c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_max.dev_attr.attr,
12181c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_min.dev_attr.attr,
12191c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
12201c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
12211c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
12221c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
12231c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
12248f05bcc3SChris Packham 	&sensor_dev_attr_temp2_smoothing.dev_attr.attr,
12251c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_input.dev_attr.attr,
12261c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
12271c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
12281c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_max.dev_attr.attr,
12291c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_min.dev_attr.attr,
12301c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
12311c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
12321c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
12331c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
12341c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
12358f05bcc3SChris Packham 	&sensor_dev_attr_temp3_smoothing.dev_attr.attr,
12361c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_input.dev_attr.attr,
12371c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_min.dev_attr.attr,
12381c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
12391c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_input.dev_attr.attr,
12401c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_min.dev_attr.attr,
12411c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
12421c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_input.dev_attr.attr,
12431c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_min.dev_attr.attr,
12441c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
12451c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1.dev_attr.attr,
12461c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
12471c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
124884d2a314SJean Delvare 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
12491c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
12501c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
12511d58f5efSChris Packham 	&sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
12521c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3.dev_attr.attr,
12531c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
12541c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
125584d2a314SJean Delvare 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
12561c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
12571c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
12581d58f5efSChris Packham 	&sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
1259f99318b2SJean Delvare 	&dev_attr_pwm_use_point2_pwm_at_crit.attr,
12601c301fc5SJordan Crouse 	NULL,
12611c301fc5SJordan Crouse };
12621c301fc5SJordan Crouse 
1263378933c9SJean Delvare static struct attribute *fan4_attrs[] = {
1264378933c9SJean Delvare 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1265378933c9SJean Delvare 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1266378933c9SJean Delvare 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1267378933c9SJean Delvare 	NULL
1268378933c9SJean Delvare };
1269378933c9SJean Delvare 
1270378933c9SJean Delvare static struct attribute *pwm2_attrs[] = {
1271378933c9SJean Delvare 	&sensor_dev_attr_pwm2.dev_attr.attr,
1272378933c9SJean Delvare 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1273378933c9SJean Delvare 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1274378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1275378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1276378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
12771d58f5efSChris Packham 	&sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
1278378933c9SJean Delvare 	NULL
1279378933c9SJean Delvare };
1280378933c9SJean Delvare 
1281378933c9SJean Delvare static struct attribute *in0_attrs[] = {
12823d849981SJean Delvare 	&sensor_dev_attr_in0_input.dev_attr.attr,
12833d849981SJean Delvare 	&sensor_dev_attr_in0_max.dev_attr.attr,
12843d849981SJean Delvare 	&sensor_dev_attr_in0_min.dev_attr.attr,
12853d849981SJean Delvare 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1286378933c9SJean Delvare 	NULL
1287378933c9SJean Delvare };
1288378933c9SJean Delvare 
1289d8d2ee07SJean Delvare static struct attribute *in3_attrs[] = {
12903d849981SJean Delvare 	&sensor_dev_attr_in3_input.dev_attr.attr,
12913d849981SJean Delvare 	&sensor_dev_attr_in3_max.dev_attr.attr,
12923d849981SJean Delvare 	&sensor_dev_attr_in3_min.dev_attr.attr,
12933d849981SJean Delvare 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1294d8d2ee07SJean Delvare 	NULL
1295d8d2ee07SJean Delvare };
1296d8d2ee07SJean Delvare 
1297d8d2ee07SJean Delvare static struct attribute *in4_attrs[] = {
12983d849981SJean Delvare 	&sensor_dev_attr_in4_input.dev_attr.attr,
12993d849981SJean Delvare 	&sensor_dev_attr_in4_max.dev_attr.attr,
13003d849981SJean Delvare 	&sensor_dev_attr_in4_min.dev_attr.attr,
13013d849981SJean Delvare 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1302d8d2ee07SJean Delvare 	NULL
1303d8d2ee07SJean Delvare };
1304d8d2ee07SJean Delvare 
1305d8d2ee07SJean Delvare static struct attribute *in5_attrs[] = {
13063d849981SJean Delvare 	&sensor_dev_attr_in5_input.dev_attr.attr,
13073d849981SJean Delvare 	&sensor_dev_attr_in5_max.dev_attr.attr,
13083d849981SJean Delvare 	&sensor_dev_attr_in5_min.dev_attr.attr,
13093d849981SJean Delvare 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
13103d849981SJean Delvare 	NULL
13113d849981SJean Delvare };
13123d849981SJean Delvare 
131362c11e46STimothy Pearson static struct attribute *in6_attrs[] = {
131462c11e46STimothy Pearson 	&sensor_dev_attr_in6_input.dev_attr.attr,
131562c11e46STimothy Pearson 	&sensor_dev_attr_in6_max.dev_attr.attr,
131662c11e46STimothy Pearson 	&sensor_dev_attr_in6_min.dev_attr.attr,
131762c11e46STimothy Pearson 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
131862c11e46STimothy Pearson 	NULL
131962c11e46STimothy Pearson };
132062c11e46STimothy Pearson 
132154fe4671SJean Delvare static struct attribute *vid_attrs[] = {
132254fe4671SJean Delvare 	&dev_attr_cpu0_vid.attr,
132354fe4671SJean Delvare 	&dev_attr_vrm.attr,
132454fe4671SJean Delvare 	NULL
132554fe4671SJean Delvare };
132654fe4671SJean Delvare 
1327f5397be8SArvind Yadav static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1328f5397be8SArvind Yadav static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1329f5397be8SArvind Yadav static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1330f5397be8SArvind Yadav static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1331f5397be8SArvind Yadav static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1332f5397be8SArvind Yadav static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1333f5397be8SArvind Yadav static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
133462c11e46STimothy Pearson static const struct attribute_group in6_attr_group = { .attrs = in6_attrs };
1335f5397be8SArvind Yadav static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
13361c301fc5SJordan Crouse 
1337310ec792SJean Delvare static int adt7475_detect(struct i2c_client *client,
13381c301fc5SJordan Crouse 			  struct i2c_board_info *info)
13391c301fc5SJordan Crouse {
13401c301fc5SJordan Crouse 	struct i2c_adapter *adapter = client->adapter;
1341d656b6fdSJean Delvare 	int vendid, devid, devid2;
1342b180d050SJean Delvare 	const char *name;
13431c301fc5SJordan Crouse 
13441c301fc5SJordan Crouse 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
13451c301fc5SJordan Crouse 		return -ENODEV;
13461c301fc5SJordan Crouse 
1347b180d050SJean Delvare 	vendid = adt7475_read(REG_VENDID);
1348d656b6fdSJean Delvare 	devid2 = adt7475_read(REG_DEVID2);
1349d656b6fdSJean Delvare 	if (vendid != 0x41 ||		/* Analog Devices */
1350d656b6fdSJean Delvare 	    (devid2 & 0xf8) != 0x68)
1351d656b6fdSJean Delvare 		return -ENODEV;
1352b180d050SJean Delvare 
1353d656b6fdSJean Delvare 	devid = adt7475_read(REG_DEVID);
1354d656b6fdSJean Delvare 	if (devid == 0x73)
1355b180d050SJean Delvare 		name = "adt7473";
1356d656b6fdSJean Delvare 	else if (devid == 0x75 && client->addr == 0x2e)
1357b180d050SJean Delvare 		name = "adt7475";
1358d8d2ee07SJean Delvare 	else if (devid == 0x76)
1359d8d2ee07SJean Delvare 		name = "adt7476";
13603d849981SJean Delvare 	else if ((devid2 & 0xfc) == 0x6c)
13613d849981SJean Delvare 		name = "adt7490";
1362b180d050SJean Delvare 	else {
1363b180d050SJean Delvare 		dev_dbg(&adapter->dev,
1364d8d2ee07SJean Delvare 			"Couldn't detect an ADT7473/75/76/90 part at "
1365b180d050SJean Delvare 			"0x%02x\n", (unsigned int)client->addr);
13661c301fc5SJordan Crouse 		return -ENODEV;
13671c301fc5SJordan Crouse 	}
13681c301fc5SJordan Crouse 
1369f2f394dbSWolfram Sang 	strscpy(info->type, name, I2C_NAME_SIZE);
13701c301fc5SJordan Crouse 
13711c301fc5SJordan Crouse 	return 0;
13721c301fc5SJordan Crouse }
13731c301fc5SJordan Crouse 
1374702afeadSTokunori Ikegami static int adt7475_update_limits(struct i2c_client *client)
13755cf943edSTokunori Ikegami {
13765cf943edSTokunori Ikegami 	struct adt7475_data *data = i2c_get_clientdata(client);
13775cf943edSTokunori Ikegami 	int i;
1378702afeadSTokunori Ikegami 	int ret;
13795cf943edSTokunori Ikegami 
1380702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG4);
1381702afeadSTokunori Ikegami 	if (ret < 0)
1382702afeadSTokunori Ikegami 		return ret;
1383702afeadSTokunori Ikegami 	data->config4 = ret;
1384702afeadSTokunori Ikegami 
1385702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG5);
1386702afeadSTokunori Ikegami 	if (ret < 0)
1387702afeadSTokunori Ikegami 		return ret;
1388702afeadSTokunori Ikegami 	data->config5 = ret;
13895cf943edSTokunori Ikegami 
13905cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
13915cf943edSTokunori Ikegami 		if (!(data->has_voltage & (1 << i)))
13925cf943edSTokunori Ikegami 			continue;
13935cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1394702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MIN_REG(i));
1395702afeadSTokunori Ikegami 		if (ret < 0)
1396702afeadSTokunori Ikegami 			return ret;
1397702afeadSTokunori Ikegami 		data->voltage[MIN][i] = ret << 2;
1398702afeadSTokunori Ikegami 
1399702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MAX_REG(i));
1400702afeadSTokunori Ikegami 		if (ret < 0)
1401702afeadSTokunori Ikegami 			return ret;
1402702afeadSTokunori Ikegami 		data->voltage[MAX][i] = ret << 2;
14035cf943edSTokunori Ikegami 	}
14045cf943edSTokunori Ikegami 
14055cf943edSTokunori Ikegami 	if (data->has_voltage & (1 << 5)) {
1406702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MIN);
1407702afeadSTokunori Ikegami 		if (ret < 0)
1408702afeadSTokunori Ikegami 			return ret;
1409702afeadSTokunori Ikegami 		data->voltage[MIN][5] = ret << 2;
1410702afeadSTokunori Ikegami 
1411702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MAX);
1412702afeadSTokunori Ikegami 		if (ret < 0)
1413702afeadSTokunori Ikegami 			return ret;
1414702afeadSTokunori Ikegami 		data->voltage[MAX][5] = ret << 2;
14155cf943edSTokunori Ikegami 	}
14165cf943edSTokunori Ikegami 
141762c11e46STimothy Pearson 	if (data->has_voltage & (1 << 6)) {
141862c11e46STimothy Pearson 		ret = adt7475_read(REG_IMON_MIN);
141962c11e46STimothy Pearson 		if (ret < 0)
142062c11e46STimothy Pearson 			return ret;
142162c11e46STimothy Pearson 		data->voltage[MIN][6] = ret << 2;
142262c11e46STimothy Pearson 
142362c11e46STimothy Pearson 		ret = adt7475_read(REG_IMON_MAX);
142462c11e46STimothy Pearson 		if (ret < 0)
142562c11e46STimothy Pearson 			return ret;
142662c11e46STimothy Pearson 		data->voltage[MAX][6] = ret << 2;
142762c11e46STimothy Pearson 	}
142862c11e46STimothy Pearson 
14295cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
14305cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1431702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MIN_REG(i));
1432702afeadSTokunori Ikegami 		if (ret < 0)
1433702afeadSTokunori Ikegami 			return ret;
1434702afeadSTokunori Ikegami 		data->temp[MIN][i] = ret << 2;
1435702afeadSTokunori Ikegami 
1436702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MAX_REG(i));
1437702afeadSTokunori Ikegami 		if (ret < 0)
1438702afeadSTokunori Ikegami 			return ret;
1439702afeadSTokunori Ikegami 		data->temp[MAX][i] = ret << 2;
1440702afeadSTokunori Ikegami 
1441702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_TMIN_REG(i));
1442702afeadSTokunori Ikegami 		if (ret < 0)
1443702afeadSTokunori Ikegami 			return ret;
1444702afeadSTokunori Ikegami 		data->temp[AUTOMIN][i] = ret << 2;
1445702afeadSTokunori Ikegami 
1446702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_THERM_REG(i));
1447702afeadSTokunori Ikegami 		if (ret < 0)
1448702afeadSTokunori Ikegami 			return ret;
1449702afeadSTokunori Ikegami 		data->temp[THERM][i] = ret << 2;
1450702afeadSTokunori Ikegami 
1451702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_OFFSET_REG(i));
1452702afeadSTokunori Ikegami 		if (ret < 0)
1453702afeadSTokunori Ikegami 			return ret;
1454702afeadSTokunori Ikegami 		data->temp[OFFSET][i] = ret;
14555cf943edSTokunori Ikegami 	}
14565cf943edSTokunori Ikegami 	adt7475_read_hystersis(client);
14575cf943edSTokunori Ikegami 
14585cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
14595cf943edSTokunori Ikegami 		if (i == 3 && !data->has_fan4)
14605cf943edSTokunori Ikegami 			continue;
1461702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_MIN_REG(i));
1462702afeadSTokunori Ikegami 		if (ret < 0)
1463702afeadSTokunori Ikegami 			return ret;
1464702afeadSTokunori Ikegami 		data->tach[MIN][i] = ret;
14655cf943edSTokunori Ikegami 	}
14665cf943edSTokunori Ikegami 
14675cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
14685cf943edSTokunori Ikegami 		if (i == 1 && !data->has_pwm2)
14695cf943edSTokunori Ikegami 			continue;
1470702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MAX_REG(i));
1471702afeadSTokunori Ikegami 		if (ret < 0)
1472702afeadSTokunori Ikegami 			return ret;
1473702afeadSTokunori Ikegami 		data->pwm[MAX][i] = ret;
1474702afeadSTokunori Ikegami 
1475702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MIN_REG(i));
1476702afeadSTokunori Ikegami 		if (ret < 0)
1477702afeadSTokunori Ikegami 			return ret;
1478702afeadSTokunori Ikegami 		data->pwm[MIN][i] = ret;
14795cf943edSTokunori Ikegami 		/* Set the channel and control information */
14805cf943edSTokunori Ikegami 		adt7475_read_pwm(client, i);
14815cf943edSTokunori Ikegami 	}
14825cf943edSTokunori Ikegami 
1483702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(0));
1484702afeadSTokunori Ikegami 	if (ret < 0)
1485702afeadSTokunori Ikegami 		return ret;
1486702afeadSTokunori Ikegami 	data->range[0] = ret;
1487702afeadSTokunori Ikegami 
1488702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(1));
1489702afeadSTokunori Ikegami 	if (ret < 0)
1490702afeadSTokunori Ikegami 		return ret;
1491702afeadSTokunori Ikegami 	data->range[1] = ret;
1492702afeadSTokunori Ikegami 
1493702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(2));
1494702afeadSTokunori Ikegami 	if (ret < 0)
1495702afeadSTokunori Ikegami 		return ret;
1496702afeadSTokunori Ikegami 	data->range[2] = ret;
1497702afeadSTokunori Ikegami 
1498702afeadSTokunori Ikegami 	return 0;
14995cf943edSTokunori Ikegami }
15005cf943edSTokunori Ikegami 
15017b8664f1SChris Packham static int load_config3(const struct i2c_client *client, const char *propname)
15027b8664f1SChris Packham {
15037b8664f1SChris Packham 	const char *function;
15047b8664f1SChris Packham 	u8 config3;
15057b8664f1SChris Packham 	int ret;
15067b8664f1SChris Packham 
15076a01a12dSChris Packham 	ret = device_property_read_string(&client->dev, propname, &function);
15087b8664f1SChris Packham 	if (!ret) {
15097b8664f1SChris Packham 		ret = adt7475_read(REG_CONFIG3);
15107b8664f1SChris Packham 		if (ret < 0)
15117b8664f1SChris Packham 			return ret;
15127b8664f1SChris Packham 
15137b8664f1SChris Packham 		config3 = ret & ~CONFIG3_SMBALERT;
15147b8664f1SChris Packham 		if (!strcmp("pwm2", function))
15157b8664f1SChris Packham 			;
15167b8664f1SChris Packham 		else if (!strcmp("smbalert#", function))
15177b8664f1SChris Packham 			config3 |= CONFIG3_SMBALERT;
15187b8664f1SChris Packham 		else
15197b8664f1SChris Packham 			return -EINVAL;
15207b8664f1SChris Packham 
15217b8664f1SChris Packham 		return i2c_smbus_write_byte_data(client, REG_CONFIG3, config3);
15227b8664f1SChris Packham 	}
15237b8664f1SChris Packham 
15247b8664f1SChris Packham 	return 0;
15257b8664f1SChris Packham }
15267b8664f1SChris Packham 
15277b8664f1SChris Packham static int load_config4(const struct i2c_client *client, const char *propname)
15287b8664f1SChris Packham {
15297b8664f1SChris Packham 	const char *function;
15307b8664f1SChris Packham 	u8 config4;
15317b8664f1SChris Packham 	int ret;
15327b8664f1SChris Packham 
15336a01a12dSChris Packham 	ret = device_property_read_string(&client->dev, propname, &function);
15347b8664f1SChris Packham 	if (!ret) {
15357b8664f1SChris Packham 		ret = adt7475_read(REG_CONFIG4);
15367b8664f1SChris Packham 		if (ret < 0)
15377b8664f1SChris Packham 			return ret;
15387b8664f1SChris Packham 
15397b8664f1SChris Packham 		config4 = ret & ~CONFIG4_PINFUNC;
15407b8664f1SChris Packham 
15417b8664f1SChris Packham 		if (!strcmp("tach4", function))
15427b8664f1SChris Packham 			;
15437b8664f1SChris Packham 		else if (!strcmp("therm#", function))
15447b8664f1SChris Packham 			config4 |= CONFIG4_THERM;
15457b8664f1SChris Packham 		else if (!strcmp("smbalert#", function))
15467b8664f1SChris Packham 			config4 |= CONFIG4_SMBALERT;
15477b8664f1SChris Packham 		else if (!strcmp("gpio", function))
15487b8664f1SChris Packham 			config4 |= CONFIG4_PINFUNC;
15497b8664f1SChris Packham 		else
15507b8664f1SChris Packham 			return -EINVAL;
15517b8664f1SChris Packham 
15527b8664f1SChris Packham 		return i2c_smbus_write_byte_data(client, REG_CONFIG4, config4);
15537b8664f1SChris Packham 	}
15547b8664f1SChris Packham 
15557b8664f1SChris Packham 	return 0;
15567b8664f1SChris Packham }
15577b8664f1SChris Packham 
15587b8664f1SChris Packham static int load_config(const struct i2c_client *client, enum chips chip)
15597b8664f1SChris Packham {
15607b8664f1SChris Packham 	int err;
15617b8664f1SChris Packham 	const char *prop1, *prop2;
15627b8664f1SChris Packham 
15637b8664f1SChris Packham 	switch (chip) {
15647b8664f1SChris Packham 	case adt7473:
15657b8664f1SChris Packham 	case adt7475:
15667b8664f1SChris Packham 		prop1 = "adi,pin5-function";
15677b8664f1SChris Packham 		prop2 = "adi,pin9-function";
15687b8664f1SChris Packham 		break;
15697b8664f1SChris Packham 	case adt7476:
15707b8664f1SChris Packham 	case adt7490:
15717b8664f1SChris Packham 		prop1 = "adi,pin10-function";
15727b8664f1SChris Packham 		prop2 = "adi,pin14-function";
15737b8664f1SChris Packham 		break;
15747b8664f1SChris Packham 	}
15757b8664f1SChris Packham 
15767b8664f1SChris Packham 	err = load_config3(client, prop1);
15777b8664f1SChris Packham 	if (err) {
15787b8664f1SChris Packham 		dev_err(&client->dev, "failed to configure %s\n", prop1);
15797b8664f1SChris Packham 		return err;
15807b8664f1SChris Packham 	}
15817b8664f1SChris Packham 
15827b8664f1SChris Packham 	err = load_config4(client, prop2);
15837b8664f1SChris Packham 	if (err) {
15847b8664f1SChris Packham 		dev_err(&client->dev, "failed to configure %s\n", prop2);
15857b8664f1SChris Packham 		return err;
15867b8664f1SChris Packham 	}
15877b8664f1SChris Packham 
15887b8664f1SChris Packham 	return 0;
15897b8664f1SChris Packham }
15907b8664f1SChris Packham 
15912ecff397SLogan Shaw static int set_property_bit(const struct i2c_client *client, char *property,
15922ecff397SLogan Shaw 			    u8 *config, u8 bit_index)
15932ecff397SLogan Shaw {
15942ecff397SLogan Shaw 	u32 prop_value = 0;
15956a01a12dSChris Packham 	int ret = device_property_read_u32(&client->dev, property,
15962ecff397SLogan Shaw 					   &prop_value);
15972ecff397SLogan Shaw 
15982ecff397SLogan Shaw 	if (!ret) {
15992ecff397SLogan Shaw 		if (prop_value)
16002ecff397SLogan Shaw 			*config |= (1 << bit_index);
16012ecff397SLogan Shaw 		else
16022ecff397SLogan Shaw 			*config &= ~(1 << bit_index);
16032ecff397SLogan Shaw 	}
16042ecff397SLogan Shaw 
16052ecff397SLogan Shaw 	return ret;
16062ecff397SLogan Shaw }
16072ecff397SLogan Shaw 
1608d45cd804SChris Packham static int load_attenuators(const struct i2c_client *client, enum chips chip,
16092ecff397SLogan Shaw 			    struct adt7475_data *data)
16102ecff397SLogan Shaw {
1611d45cd804SChris Packham 	switch (chip) {
1612d45cd804SChris Packham 	case adt7476:
1613d45cd804SChris Packham 	case adt7490:
16142ecff397SLogan Shaw 		set_property_bit(client, "adi,bypass-attenuator-in0",
16152ecff397SLogan Shaw 				 &data->config4, 4);
16162ecff397SLogan Shaw 		set_property_bit(client, "adi,bypass-attenuator-in1",
16172ecff397SLogan Shaw 				 &data->config4, 5);
16182ecff397SLogan Shaw 		set_property_bit(client, "adi,bypass-attenuator-in3",
16192ecff397SLogan Shaw 				 &data->config4, 6);
16202ecff397SLogan Shaw 		set_property_bit(client, "adi,bypass-attenuator-in4",
16212ecff397SLogan Shaw 				 &data->config4, 7);
16222ecff397SLogan Shaw 
1623d45cd804SChris Packham 		return i2c_smbus_write_byte_data(client, REG_CONFIG4,
16242ecff397SLogan Shaw 						 data->config4);
1625d45cd804SChris Packham 	case adt7473:
1626d45cd804SChris Packham 	case adt7475:
16272ecff397SLogan Shaw 		set_property_bit(client, "adi,bypass-attenuator-in1",
16282ecff397SLogan Shaw 				 &data->config2, 5);
16292ecff397SLogan Shaw 
1630d45cd804SChris Packham 		return i2c_smbus_write_byte_data(client, REG_CONFIG2,
16312ecff397SLogan Shaw 						 data->config2);
16322ecff397SLogan Shaw 	}
16332ecff397SLogan Shaw 
16342ecff397SLogan Shaw 	return 0;
16352ecff397SLogan Shaw }
16362ecff397SLogan Shaw 
163786da28eeSChris Packham static int adt7475_set_pwm_polarity(struct i2c_client *client)
163886da28eeSChris Packham {
163986da28eeSChris Packham 	u32 states[ADT7475_PWM_COUNT];
164086da28eeSChris Packham 	int ret, i;
164186da28eeSChris Packham 	u8 val;
164286da28eeSChris Packham 
16432a8e41adSChris Packham 	ret = device_property_read_u32_array(&client->dev,
164486da28eeSChris Packham 					     "adi,pwm-active-state", states,
164586da28eeSChris Packham 					     ARRAY_SIZE(states));
164686da28eeSChris Packham 	if (ret)
164786da28eeSChris Packham 		return ret;
164886da28eeSChris Packham 
164986da28eeSChris Packham 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
165086da28eeSChris Packham 		ret = adt7475_read(PWM_CONFIG_REG(i));
165186da28eeSChris Packham 		if (ret < 0)
165286da28eeSChris Packham 			return ret;
165386da28eeSChris Packham 		val = ret;
165486da28eeSChris Packham 		if (states[i])
165586da28eeSChris Packham 			val &= ~BIT(4);
165686da28eeSChris Packham 		else
165786da28eeSChris Packham 			val |= BIT(4);
165886da28eeSChris Packham 
165986da28eeSChris Packham 		ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(i), val);
166086da28eeSChris Packham 		if (ret)
166186da28eeSChris Packham 			return ret;
166286da28eeSChris Packham 	}
166386da28eeSChris Packham 
166486da28eeSChris Packham 	return 0;
166586da28eeSChris Packham }
166686da28eeSChris Packham 
1667777c97ffSChris Packham struct adt7475_pwm_config {
1668777c97ffSChris Packham 	int index;
1669777c97ffSChris Packham 	int freq;
1670777c97ffSChris Packham 	int flags;
1671777c97ffSChris Packham 	int duty;
1672777c97ffSChris Packham };
1673777c97ffSChris Packham 
1674777c97ffSChris Packham static int _adt7475_pwm_properties_parse_args(u32 args[4], struct adt7475_pwm_config *cfg)
1675777c97ffSChris Packham {
1676777c97ffSChris Packham 	int freq_hz;
1677777c97ffSChris Packham 	int duty;
1678777c97ffSChris Packham 
1679777c97ffSChris Packham 	if (args[1] == 0)
1680777c97ffSChris Packham 		return -EINVAL;
1681777c97ffSChris Packham 
1682777c97ffSChris Packham 	freq_hz = 1000000000UL / args[1];
1683777c97ffSChris Packham 	if (args[3] >= args[1])
1684777c97ffSChris Packham 		duty = 255;
1685777c97ffSChris Packham 	else
1686777c97ffSChris Packham 		duty = div_u64(255ULL * args[3], args[1]);
1687777c97ffSChris Packham 
1688777c97ffSChris Packham 	cfg->index = args[0];
1689777c97ffSChris Packham 	cfg->freq = find_closest(freq_hz, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
1690777c97ffSChris Packham 	cfg->flags = args[2];
1691777c97ffSChris Packham 	cfg->duty = duty;
1692777c97ffSChris Packham 
1693777c97ffSChris Packham 	return 0;
1694777c97ffSChris Packham }
1695777c97ffSChris Packham 
1696777c97ffSChris Packham static int adt7475_pwm_properties_parse_reference_args(struct fwnode_handle *fwnode,
1697777c97ffSChris Packham 						       struct adt7475_pwm_config *cfg)
1698777c97ffSChris Packham {
1699777c97ffSChris Packham 	int ret, i;
1700777c97ffSChris Packham 	struct fwnode_reference_args rargs = {};
1701777c97ffSChris Packham 	u32 args[4] = {};
1702777c97ffSChris Packham 
1703777c97ffSChris Packham 	ret = fwnode_property_get_reference_args(fwnode, "pwms", "#pwm-cells", 0, 0, &rargs);
1704777c97ffSChris Packham 	if (ret)
1705777c97ffSChris Packham 		return ret;
1706777c97ffSChris Packham 
1707777c97ffSChris Packham 	if (rargs.nargs != 4) {
1708777c97ffSChris Packham 		fwnode_handle_put(rargs.fwnode);
1709777c97ffSChris Packham 		return -EINVAL;
1710777c97ffSChris Packham 	}
1711777c97ffSChris Packham 
1712777c97ffSChris Packham 	for (i = 0; i < 4; i++)
1713777c97ffSChris Packham 		args[i] = rargs.args[i];
1714777c97ffSChris Packham 
1715777c97ffSChris Packham 	ret = _adt7475_pwm_properties_parse_args(args, cfg);
1716777c97ffSChris Packham 
1717777c97ffSChris Packham 	fwnode_handle_put(rargs.fwnode);
1718777c97ffSChris Packham 
1719777c97ffSChris Packham 	return ret;
1720777c97ffSChris Packham }
1721777c97ffSChris Packham 
1722777c97ffSChris Packham static int adt7475_pwm_properties_parse_args(struct fwnode_handle *fwnode,
1723777c97ffSChris Packham 					     struct adt7475_pwm_config *cfg)
1724777c97ffSChris Packham {
1725777c97ffSChris Packham 	int ret;
1726777c97ffSChris Packham 	u32 args[4] = {};
1727777c97ffSChris Packham 
1728777c97ffSChris Packham 	ret = fwnode_property_read_u32_array(fwnode, "pwms", args, ARRAY_SIZE(args));
1729777c97ffSChris Packham 	if (ret)
1730777c97ffSChris Packham 		return ret;
1731777c97ffSChris Packham 
1732777c97ffSChris Packham 	return _adt7475_pwm_properties_parse_args(args, cfg);
1733777c97ffSChris Packham }
1734777c97ffSChris Packham 
1735777c97ffSChris Packham static int adt7475_fan_pwm_config(struct i2c_client *client)
1736777c97ffSChris Packham {
1737777c97ffSChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
1738777c97ffSChris Packham 	struct adt7475_pwm_config cfg = {};
1739777c97ffSChris Packham 	int ret;
1740777c97ffSChris Packham 
1741*0fb09bf7SJavier Carrasco 	device_for_each_child_node_scoped(&client->dev, child) {
1742777c97ffSChris Packham 		if (!fwnode_property_present(child, "pwms"))
1743777c97ffSChris Packham 			continue;
1744777c97ffSChris Packham 
1745777c97ffSChris Packham 		if (is_of_node(child))
1746777c97ffSChris Packham 			ret = adt7475_pwm_properties_parse_reference_args(child, &cfg);
1747777c97ffSChris Packham 		else
1748777c97ffSChris Packham 			ret = adt7475_pwm_properties_parse_args(child, &cfg);
1749777c97ffSChris Packham 
1750777c97ffSChris Packham 		if (cfg.index >= ADT7475_PWM_COUNT)
1751777c97ffSChris Packham 			return -EINVAL;
1752777c97ffSChris Packham 
1753777c97ffSChris Packham 		ret = adt7475_read(PWM_CONFIG_REG(cfg.index));
1754777c97ffSChris Packham 		if (ret < 0)
1755777c97ffSChris Packham 			return ret;
1756777c97ffSChris Packham 		data->pwm[CONTROL][cfg.index] = ret;
1757777c97ffSChris Packham 		if (cfg.flags & PWM_POLARITY_INVERTED)
1758777c97ffSChris Packham 			data->pwm[CONTROL][cfg.index] |= BIT(4);
1759777c97ffSChris Packham 		else
1760777c97ffSChris Packham 			data->pwm[CONTROL][cfg.index] &= ~BIT(4);
1761777c97ffSChris Packham 
1762777c97ffSChris Packham 		/* Force to manual mode so PWM values take effect */
1763777c97ffSChris Packham 		data->pwm[CONTROL][cfg.index] &= ~0xE0;
1764777c97ffSChris Packham 		data->pwm[CONTROL][cfg.index] |= 0x07 << 5;
1765777c97ffSChris Packham 
1766777c97ffSChris Packham 		ret = i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(cfg.index),
1767777c97ffSChris Packham 						data->pwm[CONTROL][cfg.index]);
1768777c97ffSChris Packham 		if (ret)
1769777c97ffSChris Packham 			return ret;
1770777c97ffSChris Packham 
1771777c97ffSChris Packham 		data->pwm[INPUT][cfg.index] = cfg.duty;
1772777c97ffSChris Packham 		ret = i2c_smbus_write_byte_data(client, PWM_REG(cfg.index),
1773777c97ffSChris Packham 						data->pwm[INPUT][cfg.index]);
1774777c97ffSChris Packham 		if (ret)
1775777c97ffSChris Packham 			return ret;
1776777c97ffSChris Packham 
1777777c97ffSChris Packham 		data->range[cfg.index] = adt7475_read(TEMP_TRANGE_REG(cfg.index));
1778777c97ffSChris Packham 		data->range[cfg.index] &= ~0xf;
1779777c97ffSChris Packham 		data->range[cfg.index] |= cfg.freq;
1780777c97ffSChris Packham 
1781777c97ffSChris Packham 		ret = i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(cfg.index),
1782777c97ffSChris Packham 						data->range[cfg.index]);
1783777c97ffSChris Packham 		if (ret)
1784777c97ffSChris Packham 			return ret;
1785777c97ffSChris Packham 	}
1786777c97ffSChris Packham 
1787777c97ffSChris Packham 	return 0;
1788777c97ffSChris Packham }
1789777c97ffSChris Packham 
179067487038SStephen Kitt static int adt7475_probe(struct i2c_client *client)
17911c301fc5SJordan Crouse {
17924e2496e4SJavier Martinez Canillas 	enum chips chip;
179399b8c83aSFrans Meulenbroeks 	static const char * const names[] = {
1794d07ca4adSJean Delvare 		[adt7473] = "ADT7473",
1795d07ca4adSJean Delvare 		[adt7475] = "ADT7475",
1796d8d2ee07SJean Delvare 		[adt7476] = "ADT7476",
1797d07ca4adSJean Delvare 		[adt7490] = "ADT7490",
1798d07ca4adSJean Delvare 	};
1799d07ca4adSJean Delvare 
18001c301fc5SJordan Crouse 	struct adt7475_data *data;
1801c64fce7fSGrant McEwan 	struct device *hwmon_dev;
1802c64fce7fSGrant McEwan 	int i, ret = 0, revision, group_num = 0;
18032ecff397SLogan Shaw 	u8 config3;
18041c301fc5SJordan Crouse 
1805e3ecb2eeSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
18061c301fc5SJordan Crouse 	if (data == NULL)
18071c301fc5SJordan Crouse 		return -ENOMEM;
18081c301fc5SJordan Crouse 
18091c301fc5SJordan Crouse 	mutex_init(&data->lock);
1810c64fce7fSGrant McEwan 	data->client = client;
18111c301fc5SJordan Crouse 	i2c_set_clientdata(client, data);
18121c301fc5SJordan Crouse 
1813732d2624SAndrew Davis 	chip = (uintptr_t)i2c_get_match_data(client);
18144e2496e4SJavier Martinez Canillas 
1815cffb9dd0SJean Delvare 	/* Initialize device-specific values */
18164e2496e4SJavier Martinez Canillas 	switch (chip) {
1817d8d2ee07SJean Delvare 	case adt7476:
1818d8d2ee07SJean Delvare 		data->has_voltage = 0x0e;	/* in1 to in3 */
1819d8d2ee07SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1820d8d2ee07SJean Delvare 		break;
18213d849981SJean Delvare 	case adt7490:
182262c11e46STimothy Pearson 		data->has_voltage = 0x7e;	/* in1 to in6 */
1823378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x03;
1824d07ca4adSJean Delvare 		if (revision == 0x03)
1825d07ca4adSJean Delvare 			revision += adt7475_read(REG_DEVREV2);
18263d849981SJean Delvare 		break;
1827cffb9dd0SJean Delvare 	default:
1828cffb9dd0SJean Delvare 		data->has_voltage = 0x06;	/* in1, in2 */
1829378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1830378933c9SJean Delvare 	}
1831378933c9SJean Delvare 
18327b8664f1SChris Packham 	ret = load_config(client, chip);
18337b8664f1SChris Packham 	if (ret)
18347b8664f1SChris Packham 		return ret;
18357b8664f1SChris Packham 
1836378933c9SJean Delvare 	config3 = adt7475_read(REG_CONFIG3);
1837378933c9SJean Delvare 	/* Pin PWM2 may alternatively be used for ALERT output */
1838378933c9SJean Delvare 	if (!(config3 & CONFIG3_SMBALERT))
1839378933c9SJean Delvare 		data->has_pwm2 = 1;
1840378933c9SJean Delvare 	/* Meaning of this bit is inverted for the ADT7473-1 */
1841732d2624SAndrew Davis 	if (chip == adt7473 && revision >= 1)
1842378933c9SJean Delvare 		data->has_pwm2 = !data->has_pwm2;
1843378933c9SJean Delvare 
1844378933c9SJean Delvare 	data->config4 = adt7475_read(REG_CONFIG4);
1845378933c9SJean Delvare 	/* Pin TACH4 may alternatively be used for THERM */
1846378933c9SJean Delvare 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1847378933c9SJean Delvare 		data->has_fan4 = 1;
1848378933c9SJean Delvare 
18499ed5bc24SGuenter Roeck 	/*
18509ed5bc24SGuenter Roeck 	 * THERM configuration is more complex on the ADT7476 and ADT7490,
18519ed5bc24SGuenter Roeck 	 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
18529ed5bc24SGuenter Roeck 	 * this function
18539ed5bc24SGuenter Roeck 	 */
1854732d2624SAndrew Davis 	if (chip == adt7490) {
1855378933c9SJean Delvare 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1856378933c9SJean Delvare 		    !(config3 & CONFIG3_THERM))
1857378933c9SJean Delvare 			data->has_fan4 = 1;
1858d8d2ee07SJean Delvare 	}
1859732d2624SAndrew Davis 	if (chip == adt7476 || chip == adt7490) {
1860378933c9SJean Delvare 		if (!(config3 & CONFIG3_THERM) ||
1861378933c9SJean Delvare 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1862378933c9SJean Delvare 			data->has_voltage |= (1 << 0);		/* in0 */
1863cffb9dd0SJean Delvare 	}
1864cffb9dd0SJean Delvare 
18659ed5bc24SGuenter Roeck 	/*
18669ed5bc24SGuenter Roeck 	 * On the ADT7476, the +12V input pin may instead be used as VID5,
18679ed5bc24SGuenter Roeck 	 * and VID pins may alternatively be used as GPIO
18689ed5bc24SGuenter Roeck 	 */
1869732d2624SAndrew Davis 	if (chip == adt7476) {
1870d8d2ee07SJean Delvare 		u8 vid = adt7475_read(REG_VID);
1871d8d2ee07SJean Delvare 		if (!(vid & VID_VIDSEL))
1872d8d2ee07SJean Delvare 			data->has_voltage |= (1 << 4);		/* in4 */
187354fe4671SJean Delvare 
187454fe4671SJean Delvare 		data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1875d8d2ee07SJean Delvare 	}
1876d8d2ee07SJean Delvare 
1877ebfaf1fbSJean Delvare 	/* Voltage attenuators can be bypassed, globally or individually */
18782ecff397SLogan Shaw 	data->config2 = adt7475_read(REG_CONFIG2);
18792ecff397SLogan Shaw 	ret = load_attenuators(client, chip, data);
18802ecff397SLogan Shaw 	if (ret)
18812ecff397SLogan Shaw 		dev_warn(&client->dev, "Error configuring attenuator bypass\n");
18822ecff397SLogan Shaw 
18832ecff397SLogan Shaw 	if (data->config2 & CONFIG2_ATTN) {
1884ebfaf1fbSJean Delvare 		data->bypass_attn = (0x3 << 3) | 0x3;
1885ebfaf1fbSJean Delvare 	} else {
1886ebfaf1fbSJean Delvare 		data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1887ebfaf1fbSJean Delvare 				    ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1888ebfaf1fbSJean Delvare 	}
1889ebfaf1fbSJean Delvare 	data->bypass_attn &= data->has_voltage;
1890ebfaf1fbSJean Delvare 
18919ed5bc24SGuenter Roeck 	/*
18929ed5bc24SGuenter Roeck 	 * Call adt7475_read_pwm for all pwm's as this will reprogram any
18939ed5bc24SGuenter Roeck 	 * pwm's which are disabled to manual mode with 0% duty cycle
18949ed5bc24SGuenter Roeck 	 */
18951c301fc5SJordan Crouse 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
18961c301fc5SJordan Crouse 		adt7475_read_pwm(client, i);
18971c301fc5SJordan Crouse 
189886da28eeSChris Packham 	ret = adt7475_set_pwm_polarity(client);
189986da28eeSChris Packham 	if (ret && ret != -EINVAL)
190086da28eeSChris Packham 		dev_warn(&client->dev, "Error configuring pwm polarity\n");
190186da28eeSChris Packham 
1902777c97ffSChris Packham 	ret = adt7475_fan_pwm_config(client);
1903777c97ffSChris Packham 	if (ret)
1904777c97ffSChris Packham 		dev_warn(&client->dev, "Error %d configuring fan/pwm\n", ret);
1905777c97ffSChris Packham 
19064abdf38dSChris Packham 	/* Start monitoring */
19074abdf38dSChris Packham 	switch (chip) {
19084abdf38dSChris Packham 	case adt7475:
19094abdf38dSChris Packham 	case adt7476:
19104abdf38dSChris Packham 		i2c_smbus_write_byte_data(client, REG_CONFIG1,
19114abdf38dSChris Packham 					  adt7475_read(REG_CONFIG1) | 0x01);
19124abdf38dSChris Packham 		break;
19134abdf38dSChris Packham 	default:
19144abdf38dSChris Packham 		break;
19154abdf38dSChris Packham 	}
19164abdf38dSChris Packham 
1917c64fce7fSGrant McEwan 	data->groups[group_num++] = &adt7475_attr_group;
19181c301fc5SJordan Crouse 
1919378933c9SJean Delvare 	/* Features that can be disabled individually */
1920378933c9SJean Delvare 	if (data->has_fan4) {
1921c64fce7fSGrant McEwan 		data->groups[group_num++] = &fan4_attr_group;
1922378933c9SJean Delvare 	}
1923378933c9SJean Delvare 	if (data->has_pwm2) {
1924c64fce7fSGrant McEwan 		data->groups[group_num++] = &pwm2_attr_group;
1925378933c9SJean Delvare 	}
1926378933c9SJean Delvare 	if (data->has_voltage & (1 << 0)) {
1927c64fce7fSGrant McEwan 		data->groups[group_num++] = &in0_attr_group;
1928378933c9SJean Delvare 	}
1929d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3)) {
1930c64fce7fSGrant McEwan 		data->groups[group_num++] = &in3_attr_group;
1931d8d2ee07SJean Delvare 	}
1932d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4)) {
1933c64fce7fSGrant McEwan 		data->groups[group_num++] = &in4_attr_group;
1934d8d2ee07SJean Delvare 	}
1935d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1936c64fce7fSGrant McEwan 		data->groups[group_num++] = &in5_attr_group;
1937d8d2ee07SJean Delvare 	}
193862c11e46STimothy Pearson 	if (data->has_voltage & (1 << 6)) {
193962c11e46STimothy Pearson 		data->groups[group_num++] = &in6_attr_group;
194062c11e46STimothy Pearson 	}
194154fe4671SJean Delvare 	if (data->has_vid) {
194254fe4671SJean Delvare 		data->vrm = vid_which_vrm();
1943c64fce7fSGrant McEwan 		data->groups[group_num] = &vid_attr_group;
194454fe4671SJean Delvare 	}
1945378933c9SJean Delvare 
1946c64fce7fSGrant McEwan 	/* register device with all the acquired attributes */
1947c64fce7fSGrant McEwan 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
1948c64fce7fSGrant McEwan 							   client->name, data,
1949c64fce7fSGrant McEwan 							   data->groups);
1950c64fce7fSGrant McEwan 
1951c64fce7fSGrant McEwan 	if (IS_ERR(hwmon_dev)) {
1952c64fce7fSGrant McEwan 		ret = PTR_ERR(hwmon_dev);
1953c64fce7fSGrant McEwan 		return ret;
19541c301fc5SJordan Crouse 	}
19551c301fc5SJordan Crouse 
1956d07ca4adSJean Delvare 	dev_info(&client->dev, "%s device, revision %d\n",
1957732d2624SAndrew Davis 		 names[chip], revision);
1958d8d2ee07SJean Delvare 	if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
195954fe4671SJean Delvare 		dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1960d07ca4adSJean Delvare 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1961d8d2ee07SJean Delvare 			 (data->has_voltage & (1 << 4)) ? " in4" : "",
1962d07ca4adSJean Delvare 			 data->has_fan4 ? " fan4" : "",
196354fe4671SJean Delvare 			 data->has_pwm2 ? " pwm2" : "",
196454fe4671SJean Delvare 			 data->has_vid ? " vid" : "");
1965ebfaf1fbSJean Delvare 	if (data->bypass_attn)
1966ebfaf1fbSJean Delvare 		dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1967ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1968ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1969ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1970ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1971d07ca4adSJean Delvare 
19725cf943edSTokunori Ikegami 	/* Limits and settings, should never change update more than once */
19734afec79fSTokunori Ikegami 	ret = adt7475_update_limits(client);
19744afec79fSTokunori Ikegami 	if (ret)
19751c301fc5SJordan Crouse 		return ret;
19761c301fc5SJordan Crouse 
19771c301fc5SJordan Crouse 	return 0;
19781c301fc5SJordan Crouse }
19791c301fc5SJordan Crouse 
19801c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = {
19811c301fc5SJordan Crouse 	.class		= I2C_CLASS_HWMON,
19821c301fc5SJordan Crouse 	.driver = {
19831c301fc5SJordan Crouse 		.name	= "adt7475",
19844e2496e4SJavier Martinez Canillas 		.of_match_table = of_match_ptr(adt7475_of_match),
19851c301fc5SJordan Crouse 	},
19861975d167SUwe Kleine-König 	.probe		= adt7475_probe,
19871c301fc5SJordan Crouse 	.id_table	= adt7475_id,
19881c301fc5SJordan Crouse 	.detect		= adt7475_detect,
1989c3813d6aSJean Delvare 	.address_list	= normal_i2c,
19901c301fc5SJordan Crouse };
19911c301fc5SJordan Crouse 
19921c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client)
19931c301fc5SJordan Crouse {
19941c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
19951c301fc5SJordan Crouse 
19961c301fc5SJordan Crouse 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
19971c301fc5SJordan Crouse 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
19981c301fc5SJordan Crouse 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
19991c301fc5SJordan Crouse }
20001c301fc5SJordan Crouse 
20011c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index)
20021c301fc5SJordan Crouse {
20031c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
20041c301fc5SJordan Crouse 	unsigned int v;
20051c301fc5SJordan Crouse 
20061c301fc5SJordan Crouse 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
20071c301fc5SJordan Crouse 
20089ed5bc24SGuenter Roeck 	/*
20099ed5bc24SGuenter Roeck 	 * Figure out the internal value for pwmctrl and pwmchan
20109ed5bc24SGuenter Roeck 	 * based on the current settings
20119ed5bc24SGuenter Roeck 	 */
20121c301fc5SJordan Crouse 	v = (data->pwm[CONTROL][index] >> 5) & 7;
20131c301fc5SJordan Crouse 
20141c301fc5SJordan Crouse 	if (v == 3)
20151c301fc5SJordan Crouse 		data->pwmctl[index] = 0;
20161c301fc5SJordan Crouse 	else if (v == 7)
20171c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
20181c301fc5SJordan Crouse 	else if (v == 4) {
20199ed5bc24SGuenter Roeck 		/*
20209ed5bc24SGuenter Roeck 		 * The fan is disabled - we don't want to
20219ed5bc24SGuenter Roeck 		 * support that, so change to manual mode and
20229ed5bc24SGuenter Roeck 		 * set the duty cycle to 0 instead
20231c301fc5SJordan Crouse 		 */
20241c301fc5SJordan Crouse 		data->pwm[INPUT][index] = 0;
20251c301fc5SJordan Crouse 		data->pwm[CONTROL][index] &= ~0xE0;
20261c301fc5SJordan Crouse 		data->pwm[CONTROL][index] |= (7 << 5);
20271c301fc5SJordan Crouse 
202839b24cceSWayne Tung 		i2c_smbus_write_byte_data(client, PWM_REG(index),
20291c301fc5SJordan Crouse 					  data->pwm[INPUT][index]);
20301c301fc5SJordan Crouse 
20311c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
20321c301fc5SJordan Crouse 					  data->pwm[CONTROL][index]);
20331c301fc5SJordan Crouse 
20341c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
20351c301fc5SJordan Crouse 	} else {
20361c301fc5SJordan Crouse 		data->pwmctl[index] = 2;
20371c301fc5SJordan Crouse 
20381c301fc5SJordan Crouse 		switch (v) {
20391c301fc5SJordan Crouse 		case 0:
20401c301fc5SJordan Crouse 			data->pwmchan[index] = 1;
20411c301fc5SJordan Crouse 			break;
20421c301fc5SJordan Crouse 		case 1:
20431c301fc5SJordan Crouse 			data->pwmchan[index] = 2;
20441c301fc5SJordan Crouse 			break;
20451c301fc5SJordan Crouse 		case 2:
20461c301fc5SJordan Crouse 			data->pwmchan[index] = 4;
20471c301fc5SJordan Crouse 			break;
20481c301fc5SJordan Crouse 		case 5:
20491c301fc5SJordan Crouse 			data->pwmchan[index] = 6;
20501c301fc5SJordan Crouse 			break;
20511c301fc5SJordan Crouse 		case 6:
20521c301fc5SJordan Crouse 			data->pwmchan[index] = 7;
20531c301fc5SJordan Crouse 			break;
20541c301fc5SJordan Crouse 		}
20551c301fc5SJordan Crouse 	}
20561c301fc5SJordan Crouse }
20571c301fc5SJordan Crouse 
2058702afeadSTokunori Ikegami static int adt7475_update_measure(struct device *dev)
20591c301fc5SJordan Crouse {
2060c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
2061c64fce7fSGrant McEwan 	struct i2c_client *client = data->client;
20623d849981SJean Delvare 	u16 ext;
20631c301fc5SJordan Crouse 	int i;
2064702afeadSTokunori Ikegami 	int ret;
20651c301fc5SJordan Crouse 
2066702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS2);
2067702afeadSTokunori Ikegami 	if (ret < 0)
2068702afeadSTokunori Ikegami 		return ret;
2069702afeadSTokunori Ikegami 	data->alarms = ret << 8;
20701c301fc5SJordan Crouse 
2071702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS1);
2072702afeadSTokunori Ikegami 	if (ret < 0)
2073702afeadSTokunori Ikegami 		return ret;
2074702afeadSTokunori Ikegami 	data->alarms |= ret;
2075702afeadSTokunori Ikegami 
2076702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND2);
2077702afeadSTokunori Ikegami 	if (ret < 0)
2078702afeadSTokunori Ikegami 		return ret;
2079702afeadSTokunori Ikegami 
2080702afeadSTokunori Ikegami 	ext = (ret << 8);
2081702afeadSTokunori Ikegami 
2082702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND1);
2083702afeadSTokunori Ikegami 	if (ret < 0)
2084702afeadSTokunori Ikegami 		return ret;
2085702afeadSTokunori Ikegami 
2086702afeadSTokunori Ikegami 	ext |= ret;
2087702afeadSTokunori Ikegami 
2088cffb9dd0SJean Delvare 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
2089cffb9dd0SJean Delvare 		if (!(data->has_voltage & (1 << i)))
2090cffb9dd0SJean Delvare 			continue;
2091702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_REG(i));
2092702afeadSTokunori Ikegami 		if (ret < 0)
2093702afeadSTokunori Ikegami 			return ret;
20941c301fc5SJordan Crouse 		data->voltage[INPUT][i] =
2095702afeadSTokunori Ikegami 			(ret << 2) |
2096cffb9dd0SJean Delvare 			((ext >> (i * 2)) & 3);
2097cffb9dd0SJean Delvare 	}
20981c301fc5SJordan Crouse 
2099702afeadSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
2100702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_REG(i));
2101702afeadSTokunori Ikegami 		if (ret < 0)
2102702afeadSTokunori Ikegami 			return ret;
21031c301fc5SJordan Crouse 		data->temp[INPUT][i] =
2104702afeadSTokunori Ikegami 			(ret << 2) |
21053d849981SJean Delvare 			((ext >> ((i + 5) * 2)) & 3);
2106702afeadSTokunori Ikegami 	}
21073d849981SJean Delvare 
21083d849981SJean Delvare 	if (data->has_voltage & (1 << 5)) {
2109702afeadSTokunori Ikegami 		ret = adt7475_read(REG_STATUS4);
2110702afeadSTokunori Ikegami 		if (ret < 0)
2111702afeadSTokunori Ikegami 			return ret;
2112702afeadSTokunori Ikegami 		data->alarms |= ret << 24;
2113702afeadSTokunori Ikegami 
2114702afeadSTokunori Ikegami 		ret = adt7475_read(REG_EXTEND3);
2115702afeadSTokunori Ikegami 		if (ret < 0)
2116702afeadSTokunori Ikegami 			return ret;
2117702afeadSTokunori Ikegami 		ext = ret;
2118702afeadSTokunori Ikegami 
2119702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT);
2120702afeadSTokunori Ikegami 		if (ret < 0)
2121702afeadSTokunori Ikegami 			return ret;
2122702afeadSTokunori Ikegami 		data->voltage[INPUT][5] = ret << 2 |
21233d849981SJean Delvare 			((ext >> 4) & 3);
21243d849981SJean Delvare 	}
21251c301fc5SJordan Crouse 
212662c11e46STimothy Pearson 	if (data->has_voltage & (1 << 6)) {
212762c11e46STimothy Pearson 		ret = adt7475_read(REG_STATUS4);
212862c11e46STimothy Pearson 		if (ret < 0)
212962c11e46STimothy Pearson 			return ret;
213062c11e46STimothy Pearson 		data->alarms |= ret << 24;
213162c11e46STimothy Pearson 
213262c11e46STimothy Pearson 		ret = adt7475_read(REG_EXTEND3);
213362c11e46STimothy Pearson 		if (ret < 0)
213462c11e46STimothy Pearson 			return ret;
213562c11e46STimothy Pearson 		ext = ret;
213662c11e46STimothy Pearson 
213762c11e46STimothy Pearson 		ret = adt7475_read(REG_IMON);
213862c11e46STimothy Pearson 		if (ret < 0)
213962c11e46STimothy Pearson 			return ret;
214062c11e46STimothy Pearson 		data->voltage[INPUT][6] = ret << 2 |
214162c11e46STimothy Pearson 			((ext >> 6) & 3);
214262c11e46STimothy Pearson 	}
214362c11e46STimothy Pearson 
2144378933c9SJean Delvare 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
2145378933c9SJean Delvare 		if (i == 3 && !data->has_fan4)
2146378933c9SJean Delvare 			continue;
2147702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_REG(i));
2148702afeadSTokunori Ikegami 		if (ret < 0)
2149702afeadSTokunori Ikegami 			return ret;
2150702afeadSTokunori Ikegami 		data->tach[INPUT][i] = ret;
2151378933c9SJean Delvare 	}
21521c301fc5SJordan Crouse 
21531c301fc5SJordan Crouse 	/* Updated by hw when in auto mode */
2154378933c9SJean Delvare 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
2155378933c9SJean Delvare 		if (i == 1 && !data->has_pwm2)
2156378933c9SJean Delvare 			continue;
2157702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_REG(i));
2158702afeadSTokunori Ikegami 		if (ret < 0)
2159702afeadSTokunori Ikegami 			return ret;
2160702afeadSTokunori Ikegami 		data->pwm[INPUT][i] = ret;
2161378933c9SJean Delvare 	}
21621c301fc5SJordan Crouse 
2163702afeadSTokunori Ikegami 	if (data->has_vid) {
2164702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VID);
2165702afeadSTokunori Ikegami 		if (ret < 0)
2166702afeadSTokunori Ikegami 			return ret;
2167702afeadSTokunori Ikegami 		data->vid = ret & 0x3f;
2168702afeadSTokunori Ikegami 	}
2169702afeadSTokunori Ikegami 
2170702afeadSTokunori Ikegami 	return 0;
21711c301fc5SJordan Crouse }
21721c301fc5SJordan Crouse 
21735cf943edSTokunori Ikegami static struct adt7475_data *adt7475_update_device(struct device *dev)
21745cf943edSTokunori Ikegami {
2175c64fce7fSGrant McEwan 	struct adt7475_data *data = dev_get_drvdata(dev);
21764afec79fSTokunori Ikegami 	int ret;
21775cf943edSTokunori Ikegami 
21785cf943edSTokunori Ikegami 	mutex_lock(&data->lock);
21795cf943edSTokunori Ikegami 
21805cf943edSTokunori Ikegami 	/* Measurement values update every 2 seconds */
21815cf943edSTokunori Ikegami 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
21821c301fc5SJordan Crouse 	    !data->valid) {
21834afec79fSTokunori Ikegami 		ret = adt7475_update_measure(dev);
21844afec79fSTokunori Ikegami 		if (ret) {
21854afec79fSTokunori Ikegami 			data->valid = false;
21864afec79fSTokunori Ikegami 			mutex_unlock(&data->lock);
21874afec79fSTokunori Ikegami 			return ERR_PTR(ret);
21884afec79fSTokunori Ikegami 		}
21895cf943edSTokunori Ikegami 		data->measure_updated = jiffies;
2190b36fb171STokunori Ikegami 		data->valid = true;
21911c301fc5SJordan Crouse 	}
21921c301fc5SJordan Crouse 
21931c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
21941c301fc5SJordan Crouse 
21951c301fc5SJordan Crouse 	return data;
21961c301fc5SJordan Crouse }
21971c301fc5SJordan Crouse 
2198f0967eeaSAxel Lin module_i2c_driver(adt7475_driver);
21991c301fc5SJordan Crouse 
22001c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc");
22011c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver");
22021c301fc5SJordan Crouse MODULE_LICENSE("GPL");
2203