xref: /linux/drivers/hwmon/adt7475.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas 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>
134e2496e4SJavier Martinez Canillas #include <linux/of_device.h>
141c301fc5SJordan Crouse #include <linux/init.h>
151c301fc5SJordan Crouse #include <linux/slab.h>
161c301fc5SJordan Crouse #include <linux/i2c.h>
171c301fc5SJordan Crouse #include <linux/hwmon.h>
181c301fc5SJordan Crouse #include <linux/hwmon-sysfs.h>
1954fe4671SJean Delvare #include <linux/hwmon-vid.h>
201c301fc5SJordan Crouse #include <linux/err.h>
21dcd8f392SJean Delvare #include <linux/jiffies.h>
22e4651640SChris Packham #include <linux/util_macros.h>
231c301fc5SJordan Crouse 
241c301fc5SJordan Crouse /* Indexes for the sysfs hooks */
251c301fc5SJordan Crouse 
261c301fc5SJordan Crouse #define INPUT		0
271c301fc5SJordan Crouse #define MIN		1
281c301fc5SJordan Crouse #define MAX		2
291c301fc5SJordan Crouse #define CONTROL		3
301c301fc5SJordan Crouse #define OFFSET		3
311c301fc5SJordan Crouse #define AUTOMIN		4
321c301fc5SJordan Crouse #define THERM		5
331c301fc5SJordan Crouse #define HYSTERSIS	6
341c301fc5SJordan Crouse 
359ed5bc24SGuenter Roeck /*
369ed5bc24SGuenter Roeck  * These are unique identifiers for the sysfs functions - unlike the
379ed5bc24SGuenter Roeck  * numbers above, these are not also indexes into an array
381c301fc5SJordan Crouse  */
391c301fc5SJordan Crouse 
401c301fc5SJordan Crouse #define ALARM		9
411c301fc5SJordan Crouse #define FAULT		10
421c301fc5SJordan Crouse 
431c301fc5SJordan Crouse /* 7475 Common Registers */
441c301fc5SJordan Crouse 
45d07ca4adSJean Delvare #define REG_DEVREV2		0x12	/* ADT7490 only */
46d07ca4adSJean Delvare 
473d849981SJean Delvare #define REG_VTT			0x1E	/* ADT7490 only */
483d849981SJean Delvare #define REG_EXTEND3		0x1F	/* ADT7490 only */
493d849981SJean Delvare 
50cffb9dd0SJean Delvare #define REG_VOLTAGE_BASE	0x20
511c301fc5SJordan Crouse #define REG_TEMP_BASE		0x25
521c301fc5SJordan Crouse #define REG_TACH_BASE		0x28
531c301fc5SJordan Crouse #define REG_PWM_BASE		0x30
541c301fc5SJordan Crouse #define REG_PWM_MAX_BASE	0x38
551c301fc5SJordan Crouse 
561c301fc5SJordan Crouse #define REG_DEVID		0x3D
571c301fc5SJordan Crouse #define REG_VENDID		0x3E
58d656b6fdSJean Delvare #define REG_DEVID2		0x3F
591c301fc5SJordan Crouse 
604abdf38dSChris Packham #define REG_CONFIG1		0x40
614abdf38dSChris Packham 
621c301fc5SJordan Crouse #define REG_STATUS1		0x41
631c301fc5SJordan Crouse #define REG_STATUS2		0x42
641c301fc5SJordan Crouse 
65d8d2ee07SJean Delvare #define REG_VID			0x43	/* ADT7476 only */
66d8d2ee07SJean Delvare 
67cffb9dd0SJean Delvare #define REG_VOLTAGE_MIN_BASE	0x44
68cffb9dd0SJean Delvare #define REG_VOLTAGE_MAX_BASE	0x45
691c301fc5SJordan Crouse 
701c301fc5SJordan Crouse #define REG_TEMP_MIN_BASE	0x4E
711c301fc5SJordan Crouse #define REG_TEMP_MAX_BASE	0x4F
721c301fc5SJordan Crouse 
731c301fc5SJordan Crouse #define REG_TACH_MIN_BASE	0x54
741c301fc5SJordan Crouse 
751c301fc5SJordan Crouse #define REG_PWM_CONFIG_BASE	0x5C
761c301fc5SJordan Crouse 
771c301fc5SJordan Crouse #define REG_TEMP_TRANGE_BASE	0x5F
781c301fc5SJordan Crouse 
791d58f5efSChris Packham #define REG_ENHANCE_ACOUSTICS1	0x62
801d58f5efSChris Packham #define REG_ENHANCE_ACOUSTICS2	0x63
811d58f5efSChris Packham 
821c301fc5SJordan Crouse #define REG_PWM_MIN_BASE	0x64
831c301fc5SJordan Crouse 
841c301fc5SJordan Crouse #define REG_TEMP_TMIN_BASE	0x67
851c301fc5SJordan Crouse #define REG_TEMP_THERM_BASE	0x6A
861c301fc5SJordan Crouse 
871c301fc5SJordan Crouse #define REG_REMOTE1_HYSTERSIS	0x6D
881c301fc5SJordan Crouse #define REG_REMOTE2_HYSTERSIS	0x6E
891c301fc5SJordan Crouse 
901c301fc5SJordan Crouse #define REG_TEMP_OFFSET_BASE	0x70
911c301fc5SJordan Crouse 
92ebfaf1fbSJean Delvare #define REG_CONFIG2		0x73
93ebfaf1fbSJean Delvare 
941c301fc5SJordan Crouse #define REG_EXTEND1		0x76
951c301fc5SJordan Crouse #define REG_EXTEND2		0x77
96378933c9SJean Delvare 
97378933c9SJean Delvare #define REG_CONFIG3		0x78
981c301fc5SJordan Crouse #define REG_CONFIG5		0x7C
99f99318b2SJean Delvare #define REG_CONFIG4		0x7D
100f99318b2SJean Delvare 
1013d849981SJean Delvare #define REG_STATUS4		0x81	/* ADT7490 only */
1023d849981SJean Delvare 
1033d849981SJean Delvare #define REG_VTT_MIN		0x84	/* ADT7490 only */
1043d849981SJean Delvare #define REG_VTT_MAX		0x86	/* ADT7490 only */
1053d849981SJean Delvare 
106d8d2ee07SJean Delvare #define VID_VIDSEL		0x80	/* ADT7476 only */
107d8d2ee07SJean Delvare 
108ebfaf1fbSJean Delvare #define CONFIG2_ATTN		0x20
109ebfaf1fbSJean Delvare 
110378933c9SJean Delvare #define CONFIG3_SMBALERT	0x01
111378933c9SJean Delvare #define CONFIG3_THERM		0x02
112378933c9SJean Delvare 
113378933c9SJean Delvare #define CONFIG4_PINFUNC		0x03
114f99318b2SJean Delvare #define CONFIG4_MAXDUTY		0x08
115ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN10	0x30
116ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN43	0xC0
1171c301fc5SJordan Crouse 
1181c301fc5SJordan Crouse #define CONFIG5_TWOSCOMP	0x01
1191c301fc5SJordan Crouse #define CONFIG5_TEMPOFFSET	0x02
12054fe4671SJean Delvare #define CONFIG5_VIDGPIO		0x10	/* ADT7476 only */
1211c301fc5SJordan Crouse 
1221c301fc5SJordan Crouse /* ADT7475 Settings */
1231c301fc5SJordan Crouse 
124cffb9dd0SJean Delvare #define ADT7475_VOLTAGE_COUNT	5	/* Not counting Vtt */
1251c301fc5SJordan Crouse #define ADT7475_TEMP_COUNT	3
1261c301fc5SJordan Crouse #define ADT7475_TACH_COUNT	4
1271c301fc5SJordan Crouse #define ADT7475_PWM_COUNT	3
1281c301fc5SJordan Crouse 
1291c301fc5SJordan Crouse /* Macro to read the registers */
1301c301fc5SJordan Crouse 
1311c301fc5SJordan Crouse #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
1321c301fc5SJordan Crouse 
1331c301fc5SJordan Crouse /* Macros to easily index the registers */
1341c301fc5SJordan Crouse 
1351c301fc5SJordan Crouse #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
1361c301fc5SJordan Crouse #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
1371c301fc5SJordan Crouse 
1381c301fc5SJordan Crouse #define PWM_REG(idx) (REG_PWM_BASE + (idx))
1391c301fc5SJordan Crouse #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
1401c301fc5SJordan Crouse #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
1411c301fc5SJordan Crouse #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
1421c301fc5SJordan Crouse 
1431c301fc5SJordan Crouse #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
1441c301fc5SJordan Crouse #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
1451c301fc5SJordan Crouse #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
1461c301fc5SJordan Crouse 
1471c301fc5SJordan Crouse #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
1481c301fc5SJordan Crouse #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
1491c301fc5SJordan Crouse #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
1501c301fc5SJordan Crouse #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
1511c301fc5SJordan Crouse #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
1521c301fc5SJordan Crouse #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
1531c301fc5SJordan Crouse #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
1541c301fc5SJordan Crouse 
155918ee91cSJean Delvare static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1561c301fc5SJordan Crouse 
157e5e9f44cSJean Delvare enum chips { adt7473, adt7475, adt7476, adt7490 };
1581c301fc5SJordan Crouse 
1591c301fc5SJordan Crouse static const struct i2c_device_id adt7475_id[] = {
160b180d050SJean Delvare 	{ "adt7473", adt7473 },
1611c301fc5SJordan Crouse 	{ "adt7475", adt7475 },
162d8d2ee07SJean Delvare 	{ "adt7476", adt7476 },
1633d849981SJean Delvare 	{ "adt7490", adt7490 },
1641c301fc5SJordan Crouse 	{ }
1651c301fc5SJordan Crouse };
1661c301fc5SJordan Crouse MODULE_DEVICE_TABLE(i2c, adt7475_id);
1671c301fc5SJordan Crouse 
168fe339dbfSGuenter Roeck static const struct of_device_id __maybe_unused adt7475_of_match[] = {
1694e2496e4SJavier Martinez Canillas 	{
1704e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7473",
1714e2496e4SJavier Martinez Canillas 		.data = (void *)adt7473
1724e2496e4SJavier Martinez Canillas 	},
1734e2496e4SJavier Martinez Canillas 	{
1744e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7475",
1754e2496e4SJavier Martinez Canillas 		.data = (void *)adt7475
1764e2496e4SJavier Martinez Canillas 	},
1774e2496e4SJavier Martinez Canillas 	{
1784e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7476",
1794e2496e4SJavier Martinez Canillas 		.data = (void *)adt7476
1804e2496e4SJavier Martinez Canillas 	},
1814e2496e4SJavier Martinez Canillas 	{
1824e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7490",
1834e2496e4SJavier Martinez Canillas 		.data = (void *)adt7490
1844e2496e4SJavier Martinez Canillas 	},
1854e2496e4SJavier Martinez Canillas 	{ },
1864e2496e4SJavier Martinez Canillas };
1874e2496e4SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, adt7475_of_match);
1884e2496e4SJavier Martinez Canillas 
1891c301fc5SJordan Crouse struct adt7475_data {
1901c301fc5SJordan Crouse 	struct device *hwmon_dev;
1911c301fc5SJordan Crouse 	struct mutex lock;
1921c301fc5SJordan Crouse 
1931c301fc5SJordan Crouse 	unsigned long measure_updated;
194b36fb171STokunori Ikegami 	bool valid;
1951c301fc5SJordan Crouse 
196f99318b2SJean Delvare 	u8 config4;
1971c301fc5SJordan Crouse 	u8 config5;
198cffb9dd0SJean Delvare 	u8 has_voltage;
199ebfaf1fbSJean Delvare 	u8 bypass_attn;		/* Bypass voltage attenuator */
200378933c9SJean Delvare 	u8 has_pwm2:1;
201378933c9SJean Delvare 	u8 has_fan4:1;
20254fe4671SJean Delvare 	u8 has_vid:1;
2033d849981SJean Delvare 	u32 alarms;
204cffb9dd0SJean Delvare 	u16 voltage[3][6];
2051c301fc5SJordan Crouse 	u16 temp[7][3];
2061c301fc5SJordan Crouse 	u16 tach[2][4];
2071c301fc5SJordan Crouse 	u8 pwm[4][3];
2081c301fc5SJordan Crouse 	u8 range[3];
2091c301fc5SJordan Crouse 	u8 pwmctl[3];
2101c301fc5SJordan Crouse 	u8 pwmchan[3];
2111d58f5efSChris Packham 	u8 enh_acoustics[2];
21254fe4671SJean Delvare 
21354fe4671SJean Delvare 	u8 vid;
21454fe4671SJean Delvare 	u8 vrm;
2151c301fc5SJordan Crouse };
2161c301fc5SJordan Crouse 
2171c301fc5SJordan Crouse static struct i2c_driver adt7475_driver;
2181c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev);
2191c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client);
2201c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index);
2211c301fc5SJordan Crouse 
2221c301fc5SJordan Crouse /* Given a temp value, convert it to register value */
2231c301fc5SJordan Crouse 
2241c301fc5SJordan Crouse static inline u16 temp2reg(struct adt7475_data *data, long val)
2251c301fc5SJordan Crouse {
2261c301fc5SJordan Crouse 	u16 ret;
2271c301fc5SJordan Crouse 
2281c301fc5SJordan Crouse 	if (!(data->config5 & CONFIG5_TWOSCOMP)) {
2292a844c14SGuenter Roeck 		val = clamp_val(val, -64000, 191000);
2301c301fc5SJordan Crouse 		ret = (val + 64500) / 1000;
2311c301fc5SJordan Crouse 	} else {
2322a844c14SGuenter Roeck 		val = clamp_val(val, -128000, 127000);
2331c301fc5SJordan Crouse 		if (val < -500)
2341c301fc5SJordan Crouse 			ret = (256500 + val) / 1000;
2351c301fc5SJordan Crouse 		else
2361c301fc5SJordan Crouse 			ret = (val + 500) / 1000;
2371c301fc5SJordan Crouse 	}
2381c301fc5SJordan Crouse 
2391c301fc5SJordan Crouse 	return ret << 2;
2401c301fc5SJordan Crouse }
2411c301fc5SJordan Crouse 
2421c301fc5SJordan Crouse /* Given a register value, convert it to a real temp value */
2431c301fc5SJordan Crouse 
2441c301fc5SJordan Crouse static inline int reg2temp(struct adt7475_data *data, u16 reg)
2451c301fc5SJordan Crouse {
2461c301fc5SJordan Crouse 	if (data->config5 & CONFIG5_TWOSCOMP) {
2471c301fc5SJordan Crouse 		if (reg >= 512)
2481c301fc5SJordan Crouse 			return (reg - 1024) * 250;
2491c301fc5SJordan Crouse 		else
2501c301fc5SJordan Crouse 			return reg * 250;
2511c301fc5SJordan Crouse 	} else
2521c301fc5SJordan Crouse 		return (reg - 256) * 250;
2531c301fc5SJordan Crouse }
2541c301fc5SJordan Crouse 
2551c301fc5SJordan Crouse static inline int tach2rpm(u16 tach)
2561c301fc5SJordan Crouse {
2571c301fc5SJordan Crouse 	if (tach == 0 || tach == 0xFFFF)
2581c301fc5SJordan Crouse 		return 0;
2591c301fc5SJordan Crouse 
2601c301fc5SJordan Crouse 	return (90000 * 60) / tach;
2611c301fc5SJordan Crouse }
2621c301fc5SJordan Crouse 
2631c301fc5SJordan Crouse static inline u16 rpm2tach(unsigned long rpm)
2641c301fc5SJordan Crouse {
2651c301fc5SJordan Crouse 	if (rpm == 0)
2661c301fc5SJordan Crouse 		return 0;
2671c301fc5SJordan Crouse 
2682a844c14SGuenter Roeck 	return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
2691c301fc5SJordan Crouse }
2701c301fc5SJordan Crouse 
271cffb9dd0SJean Delvare /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
272cffb9dd0SJean Delvare static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
273cffb9dd0SJean Delvare 	{ 45, 94 },	/* +2.5V */
274cffb9dd0SJean Delvare 	{ 175, 525 },	/* Vccp */
275cffb9dd0SJean Delvare 	{ 68, 71 },	/* Vcc */
276cffb9dd0SJean Delvare 	{ 93, 47 },	/* +5V */
277cffb9dd0SJean Delvare 	{ 120, 20 },	/* +12V */
278cffb9dd0SJean Delvare 	{ 45, 45 },	/* Vtt */
279cffb9dd0SJean Delvare };
280cffb9dd0SJean Delvare 
281ebfaf1fbSJean Delvare static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
2821c301fc5SJordan Crouse {
283cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
284cffb9dd0SJean Delvare 
285ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
286ebfaf1fbSJean Delvare 		return DIV_ROUND_CLOSEST(reg * 2250, 1024);
287cffb9dd0SJean Delvare 	return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
2881c301fc5SJordan Crouse }
2891c301fc5SJordan Crouse 
290ebfaf1fbSJean Delvare static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
2911c301fc5SJordan Crouse {
292cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
293cffb9dd0SJean Delvare 	long reg;
2941c301fc5SJordan Crouse 
295ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
296ebfaf1fbSJean Delvare 		reg = (volt * 1024) / 2250;
297ebfaf1fbSJean Delvare 	else
298cffb9dd0SJean Delvare 		reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
2992a844c14SGuenter Roeck 	return clamp_val(reg, 0, 1023) & (0xff << 2);
3001c301fc5SJordan Crouse }
3011c301fc5SJordan Crouse 
302f196dec6SDan Carpenter static int adt7475_read_word(struct i2c_client *client, int reg)
3031c301fc5SJordan Crouse {
304f196dec6SDan Carpenter 	int val1, val2;
3051c301fc5SJordan Crouse 
306f196dec6SDan Carpenter 	val1 = i2c_smbus_read_byte_data(client, reg);
307f196dec6SDan Carpenter 	if (val1 < 0)
308f196dec6SDan Carpenter 		return val1;
309f196dec6SDan Carpenter 	val2 = i2c_smbus_read_byte_data(client, reg + 1);
310f196dec6SDan Carpenter 	if (val2 < 0)
311f196dec6SDan Carpenter 		return val2;
3121c301fc5SJordan Crouse 
313f196dec6SDan Carpenter 	return val1 | (val2 << 8);
3141c301fc5SJordan Crouse }
3151c301fc5SJordan Crouse 
3161c301fc5SJordan Crouse static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
3171c301fc5SJordan Crouse {
3181c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
3191c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, val & 0xFF);
3201c301fc5SJordan Crouse }
3211c301fc5SJordan Crouse 
322c24f9ba9SGuenter Roeck static ssize_t voltage_show(struct device *dev, struct device_attribute *attr,
3231c301fc5SJordan Crouse 			    char *buf)
3241c301fc5SJordan Crouse {
3251c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3261c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3271c301fc5SJordan Crouse 	unsigned short val;
3281c301fc5SJordan Crouse 
3294afec79fSTokunori Ikegami 	if (IS_ERR(data))
3304afec79fSTokunori Ikegami 		return PTR_ERR(data);
3314afec79fSTokunori Ikegami 
3321c301fc5SJordan Crouse 	switch (sattr->nr) {
3331c301fc5SJordan Crouse 	case ALARM:
3341c301fc5SJordan Crouse 		return sprintf(buf, "%d\n",
335cffb9dd0SJean Delvare 			       (data->alarms >> sattr->index) & 1);
3361c301fc5SJordan Crouse 	default:
3371c301fc5SJordan Crouse 		val = data->voltage[sattr->nr][sattr->index];
338ebfaf1fbSJean Delvare 		return sprintf(buf, "%d\n",
339ebfaf1fbSJean Delvare 			       reg2volt(sattr->index, val, data->bypass_attn));
3401c301fc5SJordan Crouse 	}
3411c301fc5SJordan Crouse }
3421c301fc5SJordan Crouse 
343c24f9ba9SGuenter Roeck static ssize_t voltage_store(struct device *dev,
344c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
345c24f9ba9SGuenter Roeck 			     size_t count)
3461c301fc5SJordan Crouse {
3471c301fc5SJordan Crouse 
3481c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3491c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
3501c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
3511c301fc5SJordan Crouse 	unsigned char reg;
3521c301fc5SJordan Crouse 	long val;
3531c301fc5SJordan Crouse 
354179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
3551c301fc5SJordan Crouse 		return -EINVAL;
3561c301fc5SJordan Crouse 
3571c301fc5SJordan Crouse 	mutex_lock(&data->lock);
3581c301fc5SJordan Crouse 
359ebfaf1fbSJean Delvare 	data->voltage[sattr->nr][sattr->index] =
360ebfaf1fbSJean Delvare 				volt2reg(sattr->index, val, data->bypass_attn);
3611c301fc5SJordan Crouse 
3623d849981SJean Delvare 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
3631c301fc5SJordan Crouse 		if (sattr->nr == MIN)
3641c301fc5SJordan Crouse 			reg = VOLTAGE_MIN_REG(sattr->index);
3651c301fc5SJordan Crouse 		else
3661c301fc5SJordan Crouse 			reg = VOLTAGE_MAX_REG(sattr->index);
3673d849981SJean Delvare 	} else {
3683d849981SJean Delvare 		if (sattr->nr == MIN)
3693d849981SJean Delvare 			reg = REG_VTT_MIN;
3703d849981SJean Delvare 		else
3713d849981SJean Delvare 			reg = REG_VTT_MAX;
3723d849981SJean Delvare 	}
3731c301fc5SJordan Crouse 
3741c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
3751c301fc5SJordan Crouse 				  data->voltage[sattr->nr][sattr->index] >> 2);
3761c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
3771c301fc5SJordan Crouse 
3781c301fc5SJordan Crouse 	return count;
3791c301fc5SJordan Crouse }
3801c301fc5SJordan Crouse 
381c24f9ba9SGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
3821c301fc5SJordan Crouse 			 char *buf)
3831c301fc5SJordan Crouse {
3841c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3851c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3861c301fc5SJordan Crouse 	int out;
3871c301fc5SJordan Crouse 
3884afec79fSTokunori Ikegami 	if (IS_ERR(data))
3894afec79fSTokunori Ikegami 		return PTR_ERR(data);
3904afec79fSTokunori Ikegami 
3911c301fc5SJordan Crouse 	switch (sattr->nr) {
3921c301fc5SJordan Crouse 	case HYSTERSIS:
3931c301fc5SJordan Crouse 		mutex_lock(&data->lock);
3941c301fc5SJordan Crouse 		out = data->temp[sattr->nr][sattr->index];
3951c301fc5SJordan Crouse 		if (sattr->index != 1)
3961c301fc5SJordan Crouse 			out = (out >> 4) & 0xF;
3971c301fc5SJordan Crouse 		else
3981c301fc5SJordan Crouse 			out = (out & 0xF);
3999ed5bc24SGuenter Roeck 		/*
4009ed5bc24SGuenter Roeck 		 * Show the value as an absolute number tied to
4019ed5bc24SGuenter Roeck 		 * THERM
4029ed5bc24SGuenter Roeck 		 */
4031c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
4041c301fc5SJordan Crouse 			out * 1000;
4051c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4061c301fc5SJordan Crouse 		break;
4071c301fc5SJordan Crouse 
4081c301fc5SJordan Crouse 	case OFFSET:
4099ed5bc24SGuenter Roeck 		/*
4109ed5bc24SGuenter Roeck 		 * Offset is always 2's complement, regardless of the
4119ed5bc24SGuenter Roeck 		 * setting in CONFIG5
4129ed5bc24SGuenter Roeck 		 */
4131c301fc5SJordan Crouse 		mutex_lock(&data->lock);
4141c301fc5SJordan Crouse 		out = (s8)data->temp[sattr->nr][sattr->index];
4151c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET)
4161c301fc5SJordan Crouse 			out *= 1000;
4171c301fc5SJordan Crouse 		else
4181c301fc5SJordan Crouse 			out *= 500;
4191c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4201c301fc5SJordan Crouse 		break;
4211c301fc5SJordan Crouse 
4221c301fc5SJordan Crouse 	case ALARM:
4231c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 4)) & 1;
4241c301fc5SJordan Crouse 		break;
4251c301fc5SJordan Crouse 
4261c301fc5SJordan Crouse 	case FAULT:
4271c301fc5SJordan Crouse 		/* Note - only for remote1 and remote2 */
428cf312e07SJean Delvare 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
4291c301fc5SJordan Crouse 		break;
4301c301fc5SJordan Crouse 
4311c301fc5SJordan Crouse 	default:
4321c301fc5SJordan Crouse 		/* All other temp values are in the configured format */
4331c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
4341c301fc5SJordan Crouse 	}
4351c301fc5SJordan Crouse 
4361c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
4371c301fc5SJordan Crouse }
4381c301fc5SJordan Crouse 
439c24f9ba9SGuenter Roeck static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
4401c301fc5SJordan Crouse 			  const char *buf, size_t count)
4411c301fc5SJordan Crouse {
4421c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4431c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
4441c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
4451c301fc5SJordan Crouse 	unsigned char reg = 0;
4461c301fc5SJordan Crouse 	u8 out;
4471c301fc5SJordan Crouse 	int temp;
4481c301fc5SJordan Crouse 	long val;
4491c301fc5SJordan Crouse 
450179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
4511c301fc5SJordan Crouse 		return -EINVAL;
4521c301fc5SJordan Crouse 
4531c301fc5SJordan Crouse 	mutex_lock(&data->lock);
4541c301fc5SJordan Crouse 
4551c301fc5SJordan Crouse 	/* We need the config register in all cases for temp <-> reg conv. */
4561c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
4571c301fc5SJordan Crouse 
4581c301fc5SJordan Crouse 	switch (sattr->nr) {
4591c301fc5SJordan Crouse 	case OFFSET:
4601c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET) {
4612a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 127000);
4621c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 1000;
4631c301fc5SJordan Crouse 		} else {
4642a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 64000);
4651c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 500;
4661c301fc5SJordan Crouse 		}
4671c301fc5SJordan Crouse 		break;
4681c301fc5SJordan Crouse 
4691c301fc5SJordan Crouse 	case HYSTERSIS:
4709ed5bc24SGuenter Roeck 		/*
4719ed5bc24SGuenter Roeck 		 * The value will be given as an absolute value, turn it
4729ed5bc24SGuenter Roeck 		 * into an offset based on THERM
4739ed5bc24SGuenter Roeck 		 */
4741c301fc5SJordan Crouse 
4751c301fc5SJordan Crouse 		/* Read fresh THERM and HYSTERSIS values from the chip */
4761c301fc5SJordan Crouse 		data->temp[THERM][sattr->index] =
4771c301fc5SJordan Crouse 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
4781c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
4791c301fc5SJordan Crouse 
4801c301fc5SJordan Crouse 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
4812a844c14SGuenter Roeck 		val = clamp_val(val, temp - 15000, temp);
4821c301fc5SJordan Crouse 		val = (temp - val) / 1000;
4831c301fc5SJordan Crouse 
4841c301fc5SJordan Crouse 		if (sattr->index != 1) {
4851c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
4861c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
4871c301fc5SJordan Crouse 		} else {
4881c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
4891c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
4901c301fc5SJordan Crouse 		}
4911c301fc5SJordan Crouse 
4921c301fc5SJordan Crouse 		out = data->temp[HYSTERSIS][sattr->index];
4931c301fc5SJordan Crouse 		break;
4941c301fc5SJordan Crouse 
4951c301fc5SJordan Crouse 	default:
4961c301fc5SJordan Crouse 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
4971c301fc5SJordan Crouse 
4989ed5bc24SGuenter Roeck 		/*
4999ed5bc24SGuenter Roeck 		 * We maintain an extra 2 digits of precision for simplicity
5009ed5bc24SGuenter Roeck 		 * - shift those back off before writing the value
5019ed5bc24SGuenter Roeck 		 */
5021c301fc5SJordan Crouse 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
5031c301fc5SJordan Crouse 	}
5041c301fc5SJordan Crouse 
5051c301fc5SJordan Crouse 	switch (sattr->nr) {
5061c301fc5SJordan Crouse 	case MIN:
5071c301fc5SJordan Crouse 		reg = TEMP_MIN_REG(sattr->index);
5081c301fc5SJordan Crouse 		break;
5091c301fc5SJordan Crouse 	case MAX:
5101c301fc5SJordan Crouse 		reg = TEMP_MAX_REG(sattr->index);
5111c301fc5SJordan Crouse 		break;
5121c301fc5SJordan Crouse 	case OFFSET:
5131c301fc5SJordan Crouse 		reg = TEMP_OFFSET_REG(sattr->index);
5141c301fc5SJordan Crouse 		break;
5151c301fc5SJordan Crouse 	case AUTOMIN:
5161c301fc5SJordan Crouse 		reg = TEMP_TMIN_REG(sattr->index);
5171c301fc5SJordan Crouse 		break;
5181c301fc5SJordan Crouse 	case THERM:
5191c301fc5SJordan Crouse 		reg = TEMP_THERM_REG(sattr->index);
5201c301fc5SJordan Crouse 		break;
5211c301fc5SJordan Crouse 	case HYSTERSIS:
5221c301fc5SJordan Crouse 		if (sattr->index != 2)
5231c301fc5SJordan Crouse 			reg = REG_REMOTE1_HYSTERSIS;
5241c301fc5SJordan Crouse 		else
5251c301fc5SJordan Crouse 			reg = REG_REMOTE2_HYSTERSIS;
5261c301fc5SJordan Crouse 
5271c301fc5SJordan Crouse 		break;
5281c301fc5SJordan Crouse 	}
5291c301fc5SJordan Crouse 
5301c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, out);
5311c301fc5SJordan Crouse 
5321c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5331c301fc5SJordan Crouse 	return count;
5341c301fc5SJordan Crouse }
5351c301fc5SJordan Crouse 
5368f05bcc3SChris Packham /* Assuming CONFIG6[SLOW] is 0 */
5378f05bcc3SChris Packham static const int ad7475_st_map[] = {
5388f05bcc3SChris Packham 	37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
5398f05bcc3SChris Packham };
5408f05bcc3SChris Packham 
541c24f9ba9SGuenter Roeck static ssize_t temp_st_show(struct device *dev, struct device_attribute *attr,
5428f05bcc3SChris Packham 			    char *buf)
5438f05bcc3SChris Packham {
5448f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5458f05bcc3SChris Packham 	struct i2c_client *client = to_i2c_client(dev);
5468f05bcc3SChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
5478f05bcc3SChris Packham 	long val;
5488f05bcc3SChris Packham 
5498f05bcc3SChris Packham 	switch (sattr->index) {
5508f05bcc3SChris Packham 	case 0:
5518f05bcc3SChris Packham 		val = data->enh_acoustics[0] & 0xf;
5528f05bcc3SChris Packham 		break;
5538f05bcc3SChris Packham 	case 1:
5548f05bcc3SChris Packham 		val = (data->enh_acoustics[1] >> 4) & 0xf;
5558f05bcc3SChris Packham 		break;
5568f05bcc3SChris Packham 	case 2:
5578f05bcc3SChris Packham 	default:
5588f05bcc3SChris Packham 		val = data->enh_acoustics[1] & 0xf;
5598f05bcc3SChris Packham 		break;
5608f05bcc3SChris Packham 	}
5618f05bcc3SChris Packham 
5628f05bcc3SChris Packham 	if (val & 0x8)
5638f05bcc3SChris Packham 		return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
5648f05bcc3SChris Packham 	else
5658f05bcc3SChris Packham 		return sprintf(buf, "0\n");
5668f05bcc3SChris Packham }
5678f05bcc3SChris Packham 
568c24f9ba9SGuenter Roeck static ssize_t temp_st_store(struct device *dev,
569c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
570c24f9ba9SGuenter Roeck 			     size_t count)
5718f05bcc3SChris Packham {
5728f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5738f05bcc3SChris Packham 	struct i2c_client *client = to_i2c_client(dev);
5748f05bcc3SChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
5758f05bcc3SChris Packham 	unsigned char reg;
5768f05bcc3SChris Packham 	int shift, idx;
5778f05bcc3SChris Packham 	ulong val;
5788f05bcc3SChris Packham 
5798f05bcc3SChris Packham 	if (kstrtoul(buf, 10, &val))
5808f05bcc3SChris Packham 		return -EINVAL;
5818f05bcc3SChris Packham 
5828f05bcc3SChris Packham 	switch (sattr->index) {
5838f05bcc3SChris Packham 	case 0:
5848f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS1;
5858f05bcc3SChris Packham 		shift = 0;
5868f05bcc3SChris Packham 		idx = 0;
5878f05bcc3SChris Packham 		break;
5888f05bcc3SChris Packham 	case 1:
5898f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
5908f05bcc3SChris Packham 		shift = 0;
5918f05bcc3SChris Packham 		idx = 1;
5928f05bcc3SChris Packham 		break;
5938f05bcc3SChris Packham 	case 2:
5948f05bcc3SChris Packham 	default:
5958f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
5968f05bcc3SChris Packham 		shift = 4;
5978f05bcc3SChris Packham 		idx = 1;
5988f05bcc3SChris Packham 		break;
5998f05bcc3SChris Packham 	}
6008f05bcc3SChris Packham 
6018f05bcc3SChris Packham 	if (val > 0) {
6028f05bcc3SChris Packham 		val = find_closest_descending(val, ad7475_st_map,
6038f05bcc3SChris Packham 					      ARRAY_SIZE(ad7475_st_map));
6048f05bcc3SChris Packham 		val |= 0x8;
6058f05bcc3SChris Packham 	}
6068f05bcc3SChris Packham 
6078f05bcc3SChris Packham 	mutex_lock(&data->lock);
6088f05bcc3SChris Packham 
6098f05bcc3SChris Packham 	data->enh_acoustics[idx] &= ~(0xf << shift);
6108f05bcc3SChris Packham 	data->enh_acoustics[idx] |= (val << shift);
6118f05bcc3SChris Packham 
6128f05bcc3SChris Packham 	i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
6138f05bcc3SChris Packham 
6148f05bcc3SChris Packham 	mutex_unlock(&data->lock);
6158f05bcc3SChris Packham 
6168f05bcc3SChris Packham 	return count;
6178f05bcc3SChris Packham }
6188f05bcc3SChris Packham 
6199ed5bc24SGuenter Roeck /*
6209ed5bc24SGuenter Roeck  * Table of autorange values - the user will write the value in millidegrees,
6219ed5bc24SGuenter Roeck  * and we'll convert it
6229ed5bc24SGuenter Roeck  */
6231c301fc5SJordan Crouse static const int autorange_table[] = {
6241c301fc5SJordan Crouse 	2000, 2500, 3330, 4000, 5000, 6670, 8000,
6251c301fc5SJordan Crouse 	10000, 13330, 16000, 20000, 26670, 32000, 40000,
6261c301fc5SJordan Crouse 	53330, 80000
6271c301fc5SJordan Crouse };
6281c301fc5SJordan Crouse 
629c24f9ba9SGuenter Roeck static ssize_t point2_show(struct device *dev, struct device_attribute *attr,
6301c301fc5SJordan Crouse 			   char *buf)
6311c301fc5SJordan Crouse {
6321c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6331c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6341c301fc5SJordan Crouse 	int out, val;
6351c301fc5SJordan Crouse 
6364afec79fSTokunori Ikegami 	if (IS_ERR(data))
6374afec79fSTokunori Ikegami 		return PTR_ERR(data);
6384afec79fSTokunori Ikegami 
6391c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6401c301fc5SJordan Crouse 	out = (data->range[sattr->index] >> 4) & 0x0F;
6411c301fc5SJordan Crouse 	val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6421c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6431c301fc5SJordan Crouse 
6441c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", val + autorange_table[out]);
6451c301fc5SJordan Crouse }
6461c301fc5SJordan Crouse 
647c24f9ba9SGuenter Roeck static ssize_t point2_store(struct device *dev, struct device_attribute *attr,
6481c301fc5SJordan Crouse 			    const char *buf, size_t count)
6491c301fc5SJordan Crouse {
6501c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
6511c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
6521c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6531c301fc5SJordan Crouse 	int temp;
6541c301fc5SJordan Crouse 	long val;
6551c301fc5SJordan Crouse 
656179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
6571c301fc5SJordan Crouse 		return -EINVAL;
6581c301fc5SJordan Crouse 
6591c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6601c301fc5SJordan Crouse 
6611c301fc5SJordan Crouse 	/* Get a fresh copy of the needed registers */
6621c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
6631c301fc5SJordan Crouse 	data->temp[AUTOMIN][sattr->index] =
6641c301fc5SJordan Crouse 		adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
6651c301fc5SJordan Crouse 	data->range[sattr->index] =
6661c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
6671c301fc5SJordan Crouse 
6689ed5bc24SGuenter Roeck 	/*
6699ed5bc24SGuenter Roeck 	 * The user will write an absolute value, so subtract the start point
6709ed5bc24SGuenter Roeck 	 * to figure the range
6719ed5bc24SGuenter Roeck 	 */
6721c301fc5SJordan Crouse 	temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6732a844c14SGuenter Roeck 	val = clamp_val(val, temp + autorange_table[0],
6741c301fc5SJordan Crouse 		temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
6751c301fc5SJordan Crouse 	val -= temp;
6761c301fc5SJordan Crouse 
6771c301fc5SJordan Crouse 	/* Find the nearest table entry to what the user wrote */
678e4651640SChris Packham 	val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
6791c301fc5SJordan Crouse 
6801c301fc5SJordan Crouse 	data->range[sattr->index] &= ~0xF0;
6811c301fc5SJordan Crouse 	data->range[sattr->index] |= val << 4;
6821c301fc5SJordan Crouse 
6831c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
6841c301fc5SJordan Crouse 				  data->range[sattr->index]);
6851c301fc5SJordan Crouse 
6861c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6871c301fc5SJordan Crouse 	return count;
6881c301fc5SJordan Crouse }
6891c301fc5SJordan Crouse 
690c24f9ba9SGuenter Roeck static ssize_t tach_show(struct device *dev, struct device_attribute *attr,
6911c301fc5SJordan Crouse 			 char *buf)
6921c301fc5SJordan Crouse {
6931c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6941c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6951c301fc5SJordan Crouse 	int out;
6961c301fc5SJordan Crouse 
6974afec79fSTokunori Ikegami 	if (IS_ERR(data))
6984afec79fSTokunori Ikegami 		return PTR_ERR(data);
6994afec79fSTokunori Ikegami 
7001c301fc5SJordan Crouse 	if (sattr->nr == ALARM)
7011c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 10)) & 1;
7021c301fc5SJordan Crouse 	else
7031c301fc5SJordan Crouse 		out = tach2rpm(data->tach[sattr->nr][sattr->index]);
7041c301fc5SJordan Crouse 
7051c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
7061c301fc5SJordan Crouse }
7071c301fc5SJordan Crouse 
708c24f9ba9SGuenter Roeck static ssize_t tach_store(struct device *dev, struct device_attribute *attr,
7091c301fc5SJordan Crouse 			  const char *buf, size_t count)
7101c301fc5SJordan Crouse {
7111c301fc5SJordan Crouse 
7121c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7131c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7141c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7151c301fc5SJordan Crouse 	unsigned long val;
7161c301fc5SJordan Crouse 
717179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
7181c301fc5SJordan Crouse 		return -EINVAL;
7191c301fc5SJordan Crouse 
7201c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7211c301fc5SJordan Crouse 
7221c301fc5SJordan Crouse 	data->tach[MIN][sattr->index] = rpm2tach(val);
7231c301fc5SJordan Crouse 
7241c301fc5SJordan Crouse 	adt7475_write_word(client, TACH_MIN_REG(sattr->index),
7251c301fc5SJordan Crouse 			   data->tach[MIN][sattr->index]);
7261c301fc5SJordan Crouse 
7271c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7281c301fc5SJordan Crouse 	return count;
7291c301fc5SJordan Crouse }
7301c301fc5SJordan Crouse 
731c24f9ba9SGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
7321c301fc5SJordan Crouse 			char *buf)
7331c301fc5SJordan Crouse {
7341c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7351c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7361c301fc5SJordan Crouse 
7374afec79fSTokunori Ikegami 	if (IS_ERR(data))
7384afec79fSTokunori Ikegami 		return PTR_ERR(data);
7394afec79fSTokunori Ikegami 
7401c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
7411c301fc5SJordan Crouse }
7421c301fc5SJordan Crouse 
743c24f9ba9SGuenter Roeck static ssize_t pwmchan_show(struct device *dev, struct device_attribute *attr,
7441c301fc5SJordan Crouse 			    char *buf)
7451c301fc5SJordan Crouse {
7461c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7471c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7481c301fc5SJordan Crouse 
7494afec79fSTokunori Ikegami 	if (IS_ERR(data))
7504afec79fSTokunori Ikegami 		return PTR_ERR(data);
7514afec79fSTokunori Ikegami 
7521c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
7531c301fc5SJordan Crouse }
7541c301fc5SJordan Crouse 
755c24f9ba9SGuenter Roeck static ssize_t pwmctrl_show(struct device *dev, struct device_attribute *attr,
7561c301fc5SJordan Crouse 			    char *buf)
7571c301fc5SJordan Crouse {
7581c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7591c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7601c301fc5SJordan Crouse 
7614afec79fSTokunori Ikegami 	if (IS_ERR(data))
7624afec79fSTokunori Ikegami 		return PTR_ERR(data);
7634afec79fSTokunori Ikegami 
7641c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
7651c301fc5SJordan Crouse }
7661c301fc5SJordan Crouse 
767c24f9ba9SGuenter Roeck static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
7681c301fc5SJordan Crouse 			 const char *buf, size_t count)
7691c301fc5SJordan Crouse {
7701c301fc5SJordan Crouse 
7711c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7721c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7731c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7741c301fc5SJordan Crouse 	unsigned char reg = 0;
7751c301fc5SJordan Crouse 	long val;
7761c301fc5SJordan Crouse 
777179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
7781c301fc5SJordan Crouse 		return -EINVAL;
7791c301fc5SJordan Crouse 
7801c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7811c301fc5SJordan Crouse 
7821c301fc5SJordan Crouse 	switch (sattr->nr) {
7831c301fc5SJordan Crouse 	case INPUT:
7841c301fc5SJordan Crouse 		/* Get a fresh value for CONTROL */
7851c301fc5SJordan Crouse 		data->pwm[CONTROL][sattr->index] =
7861c301fc5SJordan Crouse 			adt7475_read(PWM_CONFIG_REG(sattr->index));
7871c301fc5SJordan Crouse 
7889ed5bc24SGuenter Roeck 		/*
7899ed5bc24SGuenter Roeck 		 * If we are not in manual mode, then we shouldn't allow
7909ed5bc24SGuenter Roeck 		 * the user to set the pwm speed
7919ed5bc24SGuenter Roeck 		 */
7921c301fc5SJordan Crouse 		if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
7931c301fc5SJordan Crouse 			mutex_unlock(&data->lock);
7941c301fc5SJordan Crouse 			return count;
7951c301fc5SJordan Crouse 		}
7961c301fc5SJordan Crouse 
7971c301fc5SJordan Crouse 		reg = PWM_REG(sattr->index);
7981c301fc5SJordan Crouse 		break;
7991c301fc5SJordan Crouse 
8001c301fc5SJordan Crouse 	case MIN:
8011c301fc5SJordan Crouse 		reg = PWM_MIN_REG(sattr->index);
8021c301fc5SJordan Crouse 		break;
8031c301fc5SJordan Crouse 
8041c301fc5SJordan Crouse 	case MAX:
8051c301fc5SJordan Crouse 		reg = PWM_MAX_REG(sattr->index);
8061c301fc5SJordan Crouse 		break;
8071c301fc5SJordan Crouse 	}
8081c301fc5SJordan Crouse 
8092a844c14SGuenter Roeck 	data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
8101c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
8111c301fc5SJordan Crouse 				  data->pwm[sattr->nr][sattr->index]);
8121d58f5efSChris Packham 	mutex_unlock(&data->lock);
8131d58f5efSChris Packham 
8141d58f5efSChris Packham 	return count;
8151d58f5efSChris Packham }
8161d58f5efSChris Packham 
817c24f9ba9SGuenter Roeck static ssize_t stall_disable_show(struct device *dev,
8181d58f5efSChris Packham 				  struct device_attribute *attr, char *buf)
8191d58f5efSChris Packham {
8201d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8211d58f5efSChris Packham 	struct i2c_client *client = to_i2c_client(dev);
8221d58f5efSChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
8231d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8241d58f5efSChris Packham 
8251d58f5efSChris Packham 	return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
8261d58f5efSChris Packham }
8271d58f5efSChris Packham 
828c24f9ba9SGuenter Roeck static ssize_t stall_disable_store(struct device *dev,
829c24f9ba9SGuenter Roeck 				   struct device_attribute *attr,
830c24f9ba9SGuenter Roeck 				   const char *buf, size_t count)
8311d58f5efSChris Packham {
8321d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8331d58f5efSChris Packham 	struct i2c_client *client = to_i2c_client(dev);
8341d58f5efSChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
8351d58f5efSChris Packham 	long val;
8361d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8371d58f5efSChris Packham 
8381d58f5efSChris Packham 	if (kstrtol(buf, 10, &val))
8391d58f5efSChris Packham 		return -EINVAL;
8401d58f5efSChris Packham 
8411d58f5efSChris Packham 	mutex_lock(&data->lock);
8421d58f5efSChris Packham 
8431d58f5efSChris Packham 	data->enh_acoustics[0] &= ~mask;
8441d58f5efSChris Packham 	if (val)
8451d58f5efSChris Packham 		data->enh_acoustics[0] |= mask;
8461d58f5efSChris Packham 
8471d58f5efSChris Packham 	i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
8481d58f5efSChris Packham 				  data->enh_acoustics[0]);
8491c301fc5SJordan Crouse 
8501c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8511c301fc5SJordan Crouse 
8521c301fc5SJordan Crouse 	return count;
8531c301fc5SJordan Crouse }
8541c301fc5SJordan Crouse 
8551c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */
8561c301fc5SJordan Crouse 
8571c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index,
8581c301fc5SJordan Crouse 		      unsigned int pwmctl, unsigned int pwmchan)
8591c301fc5SJordan Crouse {
8601c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8611c301fc5SJordan Crouse 	long val = 0;
8621c301fc5SJordan Crouse 
8631c301fc5SJordan Crouse 	switch (pwmctl) {
8641c301fc5SJordan Crouse 	case 0:
8651c301fc5SJordan Crouse 		val = 0x03;	/* Run at full speed */
8661c301fc5SJordan Crouse 		break;
8671c301fc5SJordan Crouse 	case 1:
8681c301fc5SJordan Crouse 		val = 0x07;	/* Manual mode */
8691c301fc5SJordan Crouse 		break;
8701c301fc5SJordan Crouse 	case 2:
8711c301fc5SJordan Crouse 		switch (pwmchan) {
8721c301fc5SJordan Crouse 		case 1:
8731c301fc5SJordan Crouse 			/* Remote1 controls PWM */
8741c301fc5SJordan Crouse 			val = 0x00;
8751c301fc5SJordan Crouse 			break;
8761c301fc5SJordan Crouse 		case 2:
8771c301fc5SJordan Crouse 			/* local controls PWM */
8781c301fc5SJordan Crouse 			val = 0x01;
8791c301fc5SJordan Crouse 			break;
8801c301fc5SJordan Crouse 		case 4:
8811c301fc5SJordan Crouse 			/* remote2 controls PWM */
8821c301fc5SJordan Crouse 			val = 0x02;
8831c301fc5SJordan Crouse 			break;
8841c301fc5SJordan Crouse 		case 6:
8851c301fc5SJordan Crouse 			/* local/remote2 control PWM */
8861c301fc5SJordan Crouse 			val = 0x05;
8871c301fc5SJordan Crouse 			break;
8881c301fc5SJordan Crouse 		case 7:
8891c301fc5SJordan Crouse 			/* All three control PWM */
8901c301fc5SJordan Crouse 			val = 0x06;
8911c301fc5SJordan Crouse 			break;
8921c301fc5SJordan Crouse 		default:
8931c301fc5SJordan Crouse 			return -EINVAL;
8941c301fc5SJordan Crouse 		}
8951c301fc5SJordan Crouse 		break;
8961c301fc5SJordan Crouse 	default:
8971c301fc5SJordan Crouse 		return -EINVAL;
8981c301fc5SJordan Crouse 	}
8991c301fc5SJordan Crouse 
9001c301fc5SJordan Crouse 	data->pwmctl[index] = pwmctl;
9011c301fc5SJordan Crouse 	data->pwmchan[index] = pwmchan;
9021c301fc5SJordan Crouse 
9031c301fc5SJordan Crouse 	data->pwm[CONTROL][index] &= ~0xE0;
9041c301fc5SJordan Crouse 	data->pwm[CONTROL][index] |= (val & 7) << 5;
9051c301fc5SJordan Crouse 
9061c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
9071c301fc5SJordan Crouse 				  data->pwm[CONTROL][index]);
9081c301fc5SJordan Crouse 
9091c301fc5SJordan Crouse 	return 0;
9101c301fc5SJordan Crouse }
9111c301fc5SJordan Crouse 
912c24f9ba9SGuenter Roeck static ssize_t pwmchan_store(struct device *dev,
913c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
914c24f9ba9SGuenter Roeck 			     size_t count)
9151c301fc5SJordan Crouse {
9161c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9171c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9181c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9191c301fc5SJordan Crouse 	int r;
9201c301fc5SJordan Crouse 	long val;
9211c301fc5SJordan Crouse 
922179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9231c301fc5SJordan Crouse 		return -EINVAL;
9241c301fc5SJordan Crouse 
9251c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9261c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9271c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9281c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
9291c301fc5SJordan Crouse 	if (r)
9301c301fc5SJordan Crouse 		count = r;
9311c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9321c301fc5SJordan Crouse 
9331c301fc5SJordan Crouse 	return count;
9341c301fc5SJordan Crouse }
9351c301fc5SJordan Crouse 
936c24f9ba9SGuenter Roeck static ssize_t pwmctrl_store(struct device *dev,
937c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
938c24f9ba9SGuenter Roeck 			     size_t count)
9391c301fc5SJordan Crouse {
9401c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9411c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9421c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9431c301fc5SJordan Crouse 	int r;
9441c301fc5SJordan Crouse 	long val;
9451c301fc5SJordan Crouse 
946179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9471c301fc5SJordan Crouse 		return -EINVAL;
9481c301fc5SJordan Crouse 
9491c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9501c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9511c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9521c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
9531c301fc5SJordan Crouse 	if (r)
9541c301fc5SJordan Crouse 		count = r;
9551c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9561c301fc5SJordan Crouse 
9571c301fc5SJordan Crouse 	return count;
9581c301fc5SJordan Crouse }
9591c301fc5SJordan Crouse 
9601c301fc5SJordan Crouse /* List of frequencies for the PWM */
9611c301fc5SJordan Crouse static const int pwmfreq_table[] = {
9623490c92aSChris Packham 	11, 14, 22, 29, 35, 44, 58, 88, 22500
9631c301fc5SJordan Crouse };
9641c301fc5SJordan Crouse 
965c24f9ba9SGuenter Roeck static ssize_t pwmfreq_show(struct device *dev, struct device_attribute *attr,
9661c301fc5SJordan Crouse 			    char *buf)
9671c301fc5SJordan Crouse {
9681c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
9691c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9709d19371dSDan Carpenter 	int idx;
9711c301fc5SJordan Crouse 
9724afec79fSTokunori Ikegami 	if (IS_ERR(data))
9734afec79fSTokunori Ikegami 		return PTR_ERR(data);
9749d19371dSDan Carpenter 	idx = clamp_val(data->range[sattr->index] & 0xf, 0,
9759d19371dSDan Carpenter 			ARRAY_SIZE(pwmfreq_table) - 1);
9764afec79fSTokunori Ikegami 
9779d19371dSDan Carpenter 	return sprintf(buf, "%d\n", pwmfreq_table[idx]);
9781c301fc5SJordan Crouse }
9791c301fc5SJordan Crouse 
980c24f9ba9SGuenter Roeck static ssize_t pwmfreq_store(struct device *dev,
981c24f9ba9SGuenter Roeck 			     struct device_attribute *attr, const char *buf,
982c24f9ba9SGuenter Roeck 			     size_t count)
9831c301fc5SJordan Crouse {
9841c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9851c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9861c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9871c301fc5SJordan Crouse 	int out;
9881c301fc5SJordan Crouse 	long val;
9891c301fc5SJordan Crouse 
990179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9911c301fc5SJordan Crouse 		return -EINVAL;
9921c301fc5SJordan Crouse 
993e4651640SChris Packham 	out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
9941c301fc5SJordan Crouse 
9951c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9961c301fc5SJordan Crouse 
9971c301fc5SJordan Crouse 	data->range[sattr->index] =
9981c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
9993490c92aSChris Packham 	data->range[sattr->index] &= ~0xf;
10001c301fc5SJordan Crouse 	data->range[sattr->index] |= out;
10011c301fc5SJordan Crouse 
10021c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
10031c301fc5SJordan Crouse 				  data->range[sattr->index]);
10041c301fc5SJordan Crouse 
10051c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
10061c301fc5SJordan Crouse 	return count;
10071c301fc5SJordan Crouse }
10081c301fc5SJordan Crouse 
10091d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
10101d05303cSJulia Lawall 					struct device_attribute *devattr,
10111d05303cSJulia Lawall 					char *buf)
1012f99318b2SJean Delvare {
1013f99318b2SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
10149d19371dSDan Carpenter 
10159d19371dSDan Carpenter 	if (IS_ERR(data))
10169d19371dSDan Carpenter 		return PTR_ERR(data);
10179d19371dSDan Carpenter 
1018f99318b2SJean Delvare 	return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1019f99318b2SJean Delvare }
1020f99318b2SJean Delvare 
10211d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1022f99318b2SJean Delvare 					struct device_attribute *devattr,
1023f99318b2SJean Delvare 					const char *buf, size_t count)
1024f99318b2SJean Delvare {
1025f99318b2SJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
1026f99318b2SJean Delvare 	struct adt7475_data *data = i2c_get_clientdata(client);
1027f99318b2SJean Delvare 	long val;
1028f99318b2SJean Delvare 
1029179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
1030f99318b2SJean Delvare 		return -EINVAL;
1031f99318b2SJean Delvare 	if (val != 0 && val != 1)
1032f99318b2SJean Delvare 		return -EINVAL;
1033f99318b2SJean Delvare 
1034f99318b2SJean Delvare 	mutex_lock(&data->lock);
1035f99318b2SJean Delvare 	data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1036f99318b2SJean Delvare 	if (val)
1037f99318b2SJean Delvare 		data->config4 |= CONFIG4_MAXDUTY;
1038f99318b2SJean Delvare 	else
1039f99318b2SJean Delvare 		data->config4 &= ~CONFIG4_MAXDUTY;
1040f99318b2SJean Delvare 	i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1041f99318b2SJean Delvare 	mutex_unlock(&data->lock);
1042f99318b2SJean Delvare 
1043f99318b2SJean Delvare 	return count;
1044f99318b2SJean Delvare }
1045f99318b2SJean Delvare 
10461d05303cSJulia Lawall static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
104754fe4671SJean Delvare 			char *buf)
104854fe4671SJean Delvare {
104954fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
105054fe4671SJean Delvare 	return sprintf(buf, "%d\n", (int)data->vrm);
105154fe4671SJean Delvare }
105254fe4671SJean Delvare 
10531d05303cSJulia Lawall static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
105454fe4671SJean Delvare 			 const char *buf, size_t count)
105554fe4671SJean Delvare {
105654fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
105754fe4671SJean Delvare 	long val;
105854fe4671SJean Delvare 
1059179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
106054fe4671SJean Delvare 		return -EINVAL;
106154fe4671SJean Delvare 	if (val < 0 || val > 255)
106254fe4671SJean Delvare 		return -EINVAL;
106354fe4671SJean Delvare 	data->vrm = val;
106454fe4671SJean Delvare 
106554fe4671SJean Delvare 	return count;
106654fe4671SJean Delvare }
106754fe4671SJean Delvare 
10681d05303cSJulia Lawall static ssize_t cpu0_vid_show(struct device *dev,
10691d05303cSJulia Lawall 			     struct device_attribute *devattr, char *buf)
107054fe4671SJean Delvare {
107154fe4671SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
10724afec79fSTokunori Ikegami 
10734afec79fSTokunori Ikegami 	if (IS_ERR(data))
10744afec79fSTokunori Ikegami 		return PTR_ERR(data);
10754afec79fSTokunori Ikegami 
107654fe4671SJean Delvare 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
107754fe4671SJean Delvare }
107854fe4671SJean Delvare 
1079c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in0_input, voltage, INPUT, 0);
1080c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_max, voltage, MAX, 0);
1081c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in0_min, voltage, MIN, 0);
1082c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in0_alarm, voltage, ALARM, 0);
1083c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_input, voltage, INPUT, 1);
1084c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_max, voltage, MAX, 1);
1085c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in1_min, voltage, MIN, 1);
1086c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in1_alarm, voltage, ALARM, 1);
1087c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_input, voltage, INPUT, 2);
1088c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_max, voltage, MAX, 2);
1089c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in2_min, voltage, MIN, 2);
1090c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, voltage, ALARM, 2);
1091c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_input, voltage, INPUT, 3);
1092c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_max, voltage, MAX, 3);
1093c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in3_min, voltage, MIN, 3);
1094c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in3_alarm, voltage, ALARM, 3);
1095c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_input, voltage, INPUT, 4);
1096c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_max, voltage, MAX, 4);
1097c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in4_min, voltage, MIN, 4);
1098c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, voltage, ALARM, 8);
1099c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in5_input, voltage, INPUT, 5);
1100c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_max, voltage, MAX, 5);
1101c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(in5_min, voltage, MIN, 5);
1102c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(in5_alarm, voltage, ALARM, 31);
1103c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, INPUT, 0);
1104c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_alarm, temp, ALARM, 0);
1105c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp1_fault, temp, FAULT, 0);
1106c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, MAX, 0);
1107c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, MIN, 0);
1108c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_offset, temp, OFFSET, 0);
1109c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point1_temp, temp, AUTOMIN, 0);
1110c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, point2, 0, 0);
1111c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, THERM, 0);
1112c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_crit_hyst, temp, HYSTERSIS, 0);
1113c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp1_smoothing, temp_st, 0, 0);
1114c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, INPUT, 1);
1115c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp2_alarm, temp, ALARM, 1);
1116c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, MAX, 1);
1117c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, MIN, 1);
1118c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, OFFSET, 1);
1119c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp, AUTOMIN, 1);
1120c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, point2, 0, 1);
1121c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, THERM, 1);
1122c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_crit_hyst, temp, HYSTERSIS, 1);
1123c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp2_smoothing, temp_st, 0, 1);
1124c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, INPUT, 2);
1125c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_alarm, temp, ALARM, 2);
1126c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, temp, FAULT, 2);
1127c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, MAX, 2);
1128c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, MIN, 2);
1129c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_offset, temp, OFFSET, 2);
1130c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point1_temp, temp, AUTOMIN, 2);
1131c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_point2_temp, point2, 0, 2);
1132c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, THERM, 2);
1133c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_crit_hyst, temp, HYSTERSIS, 2);
1134c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(temp3_smoothing, temp_st, 0, 2);
1135c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan1_input, tach, INPUT, 0);
1136c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan1_min, tach, MIN, 0);
1137c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan1_alarm, tach, ALARM, 0);
1138c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan2_input, tach, INPUT, 1);
1139c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan2_min, tach, MIN, 1);
1140c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan2_alarm, tach, ALARM, 1);
1141c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan3_input, tach, INPUT, 2);
1142c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan3_min, tach, MIN, 2);
1143c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan3_alarm, tach, ALARM, 2);
1144c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan4_input, tach, INPUT, 3);
1145c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(fan4_min, tach, MIN, 3);
1146c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RO(fan4_alarm, tach, ALARM, 3);
1147c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1, pwm, INPUT, 0);
1148c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_freq, pwmfreq, INPUT, 0);
1149c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_enable, pwmctrl, INPUT, 0);
1150c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_channels_temp, pwmchan, INPUT, 0);
1151c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, pwm, MIN, 0);
1152c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, pwm, MAX, 0);
1153c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm1_stall_disable, stall_disable, 0, 0);
1154c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2, pwm, INPUT, 1);
1155c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_freq, pwmfreq, INPUT, 1);
1156c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_enable, pwmctrl, INPUT, 1);
1157c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_channels_temp, pwmchan, INPUT, 1);
1158c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, pwm, MIN, 1);
1159c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, pwm, MAX, 1);
1160c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm2_stall_disable, stall_disable, 0, 1);
1161c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3, pwm, INPUT, 2);
1162c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_freq, pwmfreq, INPUT, 2);
1163c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_enable, pwmctrl, INPUT, 2);
1164c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_channels_temp, pwmchan, INPUT, 2);
1165c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, pwm, MIN, 2);
1166c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, pwm, MAX, 2);
1167c24f9ba9SGuenter Roeck static SENSOR_DEVICE_ATTR_2_RW(pwm3_stall_disable, stall_disable, 0, 2);
11681c301fc5SJordan Crouse 
1169f99318b2SJean Delvare /* Non-standard name, might need revisiting */
11701d05303cSJulia Lawall static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1171f99318b2SJean Delvare 
11721d05303cSJulia Lawall static DEVICE_ATTR_RW(vrm);
11731d05303cSJulia Lawall static DEVICE_ATTR_RO(cpu0_vid);
117454fe4671SJean Delvare 
11751c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = {
11761c301fc5SJordan Crouse 	&sensor_dev_attr_in1_input.dev_attr.attr,
11771c301fc5SJordan Crouse 	&sensor_dev_attr_in1_max.dev_attr.attr,
11781c301fc5SJordan Crouse 	&sensor_dev_attr_in1_min.dev_attr.attr,
11791c301fc5SJordan Crouse 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
11801c301fc5SJordan Crouse 	&sensor_dev_attr_in2_input.dev_attr.attr,
11811c301fc5SJordan Crouse 	&sensor_dev_attr_in2_max.dev_attr.attr,
11821c301fc5SJordan Crouse 	&sensor_dev_attr_in2_min.dev_attr.attr,
11831c301fc5SJordan Crouse 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
11841c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_input.dev_attr.attr,
11851c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
11861c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
11871c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_max.dev_attr.attr,
11881c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_min.dev_attr.attr,
11891c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
11901c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
11911c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
11921c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
11931c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
11948f05bcc3SChris Packham 	&sensor_dev_attr_temp1_smoothing.dev_attr.attr,
11951c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_input.dev_attr.attr,
11961c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
11971c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_max.dev_attr.attr,
11981c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_min.dev_attr.attr,
11991c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
12001c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
12011c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
12021c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
12031c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
12048f05bcc3SChris Packham 	&sensor_dev_attr_temp2_smoothing.dev_attr.attr,
12051c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_input.dev_attr.attr,
12061c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
12071c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
12081c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_max.dev_attr.attr,
12091c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_min.dev_attr.attr,
12101c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
12111c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
12121c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
12131c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
12141c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
12158f05bcc3SChris Packham 	&sensor_dev_attr_temp3_smoothing.dev_attr.attr,
12161c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_input.dev_attr.attr,
12171c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_min.dev_attr.attr,
12181c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
12191c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_input.dev_attr.attr,
12201c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_min.dev_attr.attr,
12211c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
12221c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_input.dev_attr.attr,
12231c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_min.dev_attr.attr,
12241c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
12251c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1.dev_attr.attr,
12261c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
12271c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
122884d2a314SJean Delvare 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
12291c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
12301c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
12311d58f5efSChris Packham 	&sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
12321c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3.dev_attr.attr,
12331c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
12341c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
123584d2a314SJean Delvare 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
12361c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
12371c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
12381d58f5efSChris Packham 	&sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
1239f99318b2SJean Delvare 	&dev_attr_pwm_use_point2_pwm_at_crit.attr,
12401c301fc5SJordan Crouse 	NULL,
12411c301fc5SJordan Crouse };
12421c301fc5SJordan Crouse 
1243378933c9SJean Delvare static struct attribute *fan4_attrs[] = {
1244378933c9SJean Delvare 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1245378933c9SJean Delvare 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1246378933c9SJean Delvare 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1247378933c9SJean Delvare 	NULL
1248378933c9SJean Delvare };
1249378933c9SJean Delvare 
1250378933c9SJean Delvare static struct attribute *pwm2_attrs[] = {
1251378933c9SJean Delvare 	&sensor_dev_attr_pwm2.dev_attr.attr,
1252378933c9SJean Delvare 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1253378933c9SJean Delvare 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1254378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1255378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1256378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
12571d58f5efSChris Packham 	&sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
1258378933c9SJean Delvare 	NULL
1259378933c9SJean Delvare };
1260378933c9SJean Delvare 
1261378933c9SJean Delvare static struct attribute *in0_attrs[] = {
12623d849981SJean Delvare 	&sensor_dev_attr_in0_input.dev_attr.attr,
12633d849981SJean Delvare 	&sensor_dev_attr_in0_max.dev_attr.attr,
12643d849981SJean Delvare 	&sensor_dev_attr_in0_min.dev_attr.attr,
12653d849981SJean Delvare 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1266378933c9SJean Delvare 	NULL
1267378933c9SJean Delvare };
1268378933c9SJean Delvare 
1269d8d2ee07SJean Delvare static struct attribute *in3_attrs[] = {
12703d849981SJean Delvare 	&sensor_dev_attr_in3_input.dev_attr.attr,
12713d849981SJean Delvare 	&sensor_dev_attr_in3_max.dev_attr.attr,
12723d849981SJean Delvare 	&sensor_dev_attr_in3_min.dev_attr.attr,
12733d849981SJean Delvare 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1274d8d2ee07SJean Delvare 	NULL
1275d8d2ee07SJean Delvare };
1276d8d2ee07SJean Delvare 
1277d8d2ee07SJean Delvare static struct attribute *in4_attrs[] = {
12783d849981SJean Delvare 	&sensor_dev_attr_in4_input.dev_attr.attr,
12793d849981SJean Delvare 	&sensor_dev_attr_in4_max.dev_attr.attr,
12803d849981SJean Delvare 	&sensor_dev_attr_in4_min.dev_attr.attr,
12813d849981SJean Delvare 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1282d8d2ee07SJean Delvare 	NULL
1283d8d2ee07SJean Delvare };
1284d8d2ee07SJean Delvare 
1285d8d2ee07SJean Delvare static struct attribute *in5_attrs[] = {
12863d849981SJean Delvare 	&sensor_dev_attr_in5_input.dev_attr.attr,
12873d849981SJean Delvare 	&sensor_dev_attr_in5_max.dev_attr.attr,
12883d849981SJean Delvare 	&sensor_dev_attr_in5_min.dev_attr.attr,
12893d849981SJean Delvare 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
12903d849981SJean Delvare 	NULL
12913d849981SJean Delvare };
12923d849981SJean Delvare 
129354fe4671SJean Delvare static struct attribute *vid_attrs[] = {
129454fe4671SJean Delvare 	&dev_attr_cpu0_vid.attr,
129554fe4671SJean Delvare 	&dev_attr_vrm.attr,
129654fe4671SJean Delvare 	NULL
129754fe4671SJean Delvare };
129854fe4671SJean Delvare 
1299f5397be8SArvind Yadav static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1300f5397be8SArvind Yadav static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1301f5397be8SArvind Yadav static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1302f5397be8SArvind Yadav static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1303f5397be8SArvind Yadav static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1304f5397be8SArvind Yadav static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1305f5397be8SArvind Yadav static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1306f5397be8SArvind Yadav static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
13071c301fc5SJordan Crouse 
1308310ec792SJean Delvare static int adt7475_detect(struct i2c_client *client,
13091c301fc5SJordan Crouse 			  struct i2c_board_info *info)
13101c301fc5SJordan Crouse {
13111c301fc5SJordan Crouse 	struct i2c_adapter *adapter = client->adapter;
1312d656b6fdSJean Delvare 	int vendid, devid, devid2;
1313b180d050SJean Delvare 	const char *name;
13141c301fc5SJordan Crouse 
13151c301fc5SJordan Crouse 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
13161c301fc5SJordan Crouse 		return -ENODEV;
13171c301fc5SJordan Crouse 
1318b180d050SJean Delvare 	vendid = adt7475_read(REG_VENDID);
1319d656b6fdSJean Delvare 	devid2 = adt7475_read(REG_DEVID2);
1320d656b6fdSJean Delvare 	if (vendid != 0x41 ||		/* Analog Devices */
1321d656b6fdSJean Delvare 	    (devid2 & 0xf8) != 0x68)
1322d656b6fdSJean Delvare 		return -ENODEV;
1323b180d050SJean Delvare 
1324d656b6fdSJean Delvare 	devid = adt7475_read(REG_DEVID);
1325d656b6fdSJean Delvare 	if (devid == 0x73)
1326b180d050SJean Delvare 		name = "adt7473";
1327d656b6fdSJean Delvare 	else if (devid == 0x75 && client->addr == 0x2e)
1328b180d050SJean Delvare 		name = "adt7475";
1329d8d2ee07SJean Delvare 	else if (devid == 0x76)
1330d8d2ee07SJean Delvare 		name = "adt7476";
13313d849981SJean Delvare 	else if ((devid2 & 0xfc) == 0x6c)
13323d849981SJean Delvare 		name = "adt7490";
1333b180d050SJean Delvare 	else {
1334b180d050SJean Delvare 		dev_dbg(&adapter->dev,
1335d8d2ee07SJean Delvare 			"Couldn't detect an ADT7473/75/76/90 part at "
1336b180d050SJean Delvare 			"0x%02x\n", (unsigned int)client->addr);
13371c301fc5SJordan Crouse 		return -ENODEV;
13381c301fc5SJordan Crouse 	}
13391c301fc5SJordan Crouse 
1340b180d050SJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
13411c301fc5SJordan Crouse 
13421c301fc5SJordan Crouse 	return 0;
13431c301fc5SJordan Crouse }
13441c301fc5SJordan Crouse 
13450f14480bSJean Delvare static void adt7475_remove_files(struct i2c_client *client,
13460f14480bSJean Delvare 				 struct adt7475_data *data)
13470f14480bSJean Delvare {
13480f14480bSJean Delvare 	sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1349378933c9SJean Delvare 	if (data->has_fan4)
1350378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1351378933c9SJean Delvare 	if (data->has_pwm2)
1352378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1353378933c9SJean Delvare 	if (data->has_voltage & (1 << 0))
1354378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1355d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3))
1356d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1357d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4))
1358d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1359d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5))
1360d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
136154fe4671SJean Delvare 	if (data->has_vid)
136254fe4671SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
13630f14480bSJean Delvare }
13640f14480bSJean Delvare 
1365702afeadSTokunori Ikegami static int adt7475_update_limits(struct i2c_client *client)
13665cf943edSTokunori Ikegami {
13675cf943edSTokunori Ikegami 	struct adt7475_data *data = i2c_get_clientdata(client);
13685cf943edSTokunori Ikegami 	int i;
1369702afeadSTokunori Ikegami 	int ret;
13705cf943edSTokunori Ikegami 
1371702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG4);
1372702afeadSTokunori Ikegami 	if (ret < 0)
1373702afeadSTokunori Ikegami 		return ret;
1374702afeadSTokunori Ikegami 	data->config4 = ret;
1375702afeadSTokunori Ikegami 
1376702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG5);
1377702afeadSTokunori Ikegami 	if (ret < 0)
1378702afeadSTokunori Ikegami 		return ret;
1379702afeadSTokunori Ikegami 	data->config5 = ret;
13805cf943edSTokunori Ikegami 
13815cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
13825cf943edSTokunori Ikegami 		if (!(data->has_voltage & (1 << i)))
13835cf943edSTokunori Ikegami 			continue;
13845cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1385702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MIN_REG(i));
1386702afeadSTokunori Ikegami 		if (ret < 0)
1387702afeadSTokunori Ikegami 			return ret;
1388702afeadSTokunori Ikegami 		data->voltage[MIN][i] = ret << 2;
1389702afeadSTokunori Ikegami 
1390702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MAX_REG(i));
1391702afeadSTokunori Ikegami 		if (ret < 0)
1392702afeadSTokunori Ikegami 			return ret;
1393702afeadSTokunori Ikegami 		data->voltage[MAX][i] = ret << 2;
13945cf943edSTokunori Ikegami 	}
13955cf943edSTokunori Ikegami 
13965cf943edSTokunori Ikegami 	if (data->has_voltage & (1 << 5)) {
1397702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MIN);
1398702afeadSTokunori Ikegami 		if (ret < 0)
1399702afeadSTokunori Ikegami 			return ret;
1400702afeadSTokunori Ikegami 		data->voltage[MIN][5] = ret << 2;
1401702afeadSTokunori Ikegami 
1402702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MAX);
1403702afeadSTokunori Ikegami 		if (ret < 0)
1404702afeadSTokunori Ikegami 			return ret;
1405702afeadSTokunori Ikegami 		data->voltage[MAX][5] = ret << 2;
14065cf943edSTokunori Ikegami 	}
14075cf943edSTokunori Ikegami 
14085cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
14095cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1410702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MIN_REG(i));
1411702afeadSTokunori Ikegami 		if (ret < 0)
1412702afeadSTokunori Ikegami 			return ret;
1413702afeadSTokunori Ikegami 		data->temp[MIN][i] = ret << 2;
1414702afeadSTokunori Ikegami 
1415702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MAX_REG(i));
1416702afeadSTokunori Ikegami 		if (ret < 0)
1417702afeadSTokunori Ikegami 			return ret;
1418702afeadSTokunori Ikegami 		data->temp[MAX][i] = ret << 2;
1419702afeadSTokunori Ikegami 
1420702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_TMIN_REG(i));
1421702afeadSTokunori Ikegami 		if (ret < 0)
1422702afeadSTokunori Ikegami 			return ret;
1423702afeadSTokunori Ikegami 		data->temp[AUTOMIN][i] = ret << 2;
1424702afeadSTokunori Ikegami 
1425702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_THERM_REG(i));
1426702afeadSTokunori Ikegami 		if (ret < 0)
1427702afeadSTokunori Ikegami 			return ret;
1428702afeadSTokunori Ikegami 		data->temp[THERM][i] = ret << 2;
1429702afeadSTokunori Ikegami 
1430702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_OFFSET_REG(i));
1431702afeadSTokunori Ikegami 		if (ret < 0)
1432702afeadSTokunori Ikegami 			return ret;
1433702afeadSTokunori Ikegami 		data->temp[OFFSET][i] = ret;
14345cf943edSTokunori Ikegami 	}
14355cf943edSTokunori Ikegami 	adt7475_read_hystersis(client);
14365cf943edSTokunori Ikegami 
14375cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
14385cf943edSTokunori Ikegami 		if (i == 3 && !data->has_fan4)
14395cf943edSTokunori Ikegami 			continue;
1440702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_MIN_REG(i));
1441702afeadSTokunori Ikegami 		if (ret < 0)
1442702afeadSTokunori Ikegami 			return ret;
1443702afeadSTokunori Ikegami 		data->tach[MIN][i] = ret;
14445cf943edSTokunori Ikegami 	}
14455cf943edSTokunori Ikegami 
14465cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
14475cf943edSTokunori Ikegami 		if (i == 1 && !data->has_pwm2)
14485cf943edSTokunori Ikegami 			continue;
1449702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MAX_REG(i));
1450702afeadSTokunori Ikegami 		if (ret < 0)
1451702afeadSTokunori Ikegami 			return ret;
1452702afeadSTokunori Ikegami 		data->pwm[MAX][i] = ret;
1453702afeadSTokunori Ikegami 
1454702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MIN_REG(i));
1455702afeadSTokunori Ikegami 		if (ret < 0)
1456702afeadSTokunori Ikegami 			return ret;
1457702afeadSTokunori Ikegami 		data->pwm[MIN][i] = ret;
14585cf943edSTokunori Ikegami 		/* Set the channel and control information */
14595cf943edSTokunori Ikegami 		adt7475_read_pwm(client, i);
14605cf943edSTokunori Ikegami 	}
14615cf943edSTokunori Ikegami 
1462702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(0));
1463702afeadSTokunori Ikegami 	if (ret < 0)
1464702afeadSTokunori Ikegami 		return ret;
1465702afeadSTokunori Ikegami 	data->range[0] = ret;
1466702afeadSTokunori Ikegami 
1467702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(1));
1468702afeadSTokunori Ikegami 	if (ret < 0)
1469702afeadSTokunori Ikegami 		return ret;
1470702afeadSTokunori Ikegami 	data->range[1] = ret;
1471702afeadSTokunori Ikegami 
1472702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(2));
1473702afeadSTokunori Ikegami 	if (ret < 0)
1474702afeadSTokunori Ikegami 		return ret;
1475702afeadSTokunori Ikegami 	data->range[2] = ret;
1476702afeadSTokunori Ikegami 
1477702afeadSTokunori Ikegami 	return 0;
14785cf943edSTokunori Ikegami }
14795cf943edSTokunori Ikegami 
14801c301fc5SJordan Crouse static int adt7475_probe(struct i2c_client *client,
14811c301fc5SJordan Crouse 			 const struct i2c_device_id *id)
14821c301fc5SJordan Crouse {
14834e2496e4SJavier Martinez Canillas 	enum chips chip;
148499b8c83aSFrans Meulenbroeks 	static const char * const names[] = {
1485d07ca4adSJean Delvare 		[adt7473] = "ADT7473",
1486d07ca4adSJean Delvare 		[adt7475] = "ADT7475",
1487d8d2ee07SJean Delvare 		[adt7476] = "ADT7476",
1488d07ca4adSJean Delvare 		[adt7490] = "ADT7490",
1489d07ca4adSJean Delvare 	};
1490d07ca4adSJean Delvare 
14911c301fc5SJordan Crouse 	struct adt7475_data *data;
1492378933c9SJean Delvare 	int i, ret = 0, revision;
1493ebfaf1fbSJean Delvare 	u8 config2, config3;
14941c301fc5SJordan Crouse 
1495e3ecb2eeSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
14961c301fc5SJordan Crouse 	if (data == NULL)
14971c301fc5SJordan Crouse 		return -ENOMEM;
14981c301fc5SJordan Crouse 
14991c301fc5SJordan Crouse 	mutex_init(&data->lock);
15001c301fc5SJordan Crouse 	i2c_set_clientdata(client, data);
15011c301fc5SJordan Crouse 
15024e2496e4SJavier Martinez Canillas 	if (client->dev.of_node)
15034e2496e4SJavier Martinez Canillas 		chip = (enum chips)of_device_get_match_data(&client->dev);
15044e2496e4SJavier Martinez Canillas 	else
15054e2496e4SJavier Martinez Canillas 		chip = id->driver_data;
15064e2496e4SJavier Martinez Canillas 
1507cffb9dd0SJean Delvare 	/* Initialize device-specific values */
15084e2496e4SJavier Martinez Canillas 	switch (chip) {
1509d8d2ee07SJean Delvare 	case adt7476:
1510d8d2ee07SJean Delvare 		data->has_voltage = 0x0e;	/* in1 to in3 */
1511d8d2ee07SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1512d8d2ee07SJean Delvare 		break;
15133d849981SJean Delvare 	case adt7490:
1514378933c9SJean Delvare 		data->has_voltage = 0x3e;	/* in1 to in5 */
1515378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x03;
1516d07ca4adSJean Delvare 		if (revision == 0x03)
1517d07ca4adSJean Delvare 			revision += adt7475_read(REG_DEVREV2);
15183d849981SJean Delvare 		break;
1519cffb9dd0SJean Delvare 	default:
1520cffb9dd0SJean Delvare 		data->has_voltage = 0x06;	/* in1, in2 */
1521378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1522378933c9SJean Delvare 	}
1523378933c9SJean Delvare 
1524378933c9SJean Delvare 	config3 = adt7475_read(REG_CONFIG3);
1525378933c9SJean Delvare 	/* Pin PWM2 may alternatively be used for ALERT output */
1526378933c9SJean Delvare 	if (!(config3 & CONFIG3_SMBALERT))
1527378933c9SJean Delvare 		data->has_pwm2 = 1;
1528378933c9SJean Delvare 	/* Meaning of this bit is inverted for the ADT7473-1 */
1529378933c9SJean Delvare 	if (id->driver_data == adt7473 && revision >= 1)
1530378933c9SJean Delvare 		data->has_pwm2 = !data->has_pwm2;
1531378933c9SJean Delvare 
1532378933c9SJean Delvare 	data->config4 = adt7475_read(REG_CONFIG4);
1533378933c9SJean Delvare 	/* Pin TACH4 may alternatively be used for THERM */
1534378933c9SJean Delvare 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1535378933c9SJean Delvare 		data->has_fan4 = 1;
1536378933c9SJean Delvare 
15379ed5bc24SGuenter Roeck 	/*
15389ed5bc24SGuenter Roeck 	 * THERM configuration is more complex on the ADT7476 and ADT7490,
15399ed5bc24SGuenter Roeck 	 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
15409ed5bc24SGuenter Roeck 	 * this function
15419ed5bc24SGuenter Roeck 	 */
1542378933c9SJean Delvare 	if (id->driver_data == adt7490) {
1543378933c9SJean Delvare 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1544378933c9SJean Delvare 		    !(config3 & CONFIG3_THERM))
1545378933c9SJean Delvare 			data->has_fan4 = 1;
1546d8d2ee07SJean Delvare 	}
1547d8d2ee07SJean Delvare 	if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1548378933c9SJean Delvare 		if (!(config3 & CONFIG3_THERM) ||
1549378933c9SJean Delvare 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1550378933c9SJean Delvare 			data->has_voltage |= (1 << 0);		/* in0 */
1551cffb9dd0SJean Delvare 	}
1552cffb9dd0SJean Delvare 
15539ed5bc24SGuenter Roeck 	/*
15549ed5bc24SGuenter Roeck 	 * On the ADT7476, the +12V input pin may instead be used as VID5,
15559ed5bc24SGuenter Roeck 	 * and VID pins may alternatively be used as GPIO
15569ed5bc24SGuenter Roeck 	 */
1557d8d2ee07SJean Delvare 	if (id->driver_data == adt7476) {
1558d8d2ee07SJean Delvare 		u8 vid = adt7475_read(REG_VID);
1559d8d2ee07SJean Delvare 		if (!(vid & VID_VIDSEL))
1560d8d2ee07SJean Delvare 			data->has_voltage |= (1 << 4);		/* in4 */
156154fe4671SJean Delvare 
156254fe4671SJean Delvare 		data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1563d8d2ee07SJean Delvare 	}
1564d8d2ee07SJean Delvare 
1565ebfaf1fbSJean Delvare 	/* Voltage attenuators can be bypassed, globally or individually */
1566ebfaf1fbSJean Delvare 	config2 = adt7475_read(REG_CONFIG2);
1567ebfaf1fbSJean Delvare 	if (config2 & CONFIG2_ATTN) {
1568ebfaf1fbSJean Delvare 		data->bypass_attn = (0x3 << 3) | 0x3;
1569ebfaf1fbSJean Delvare 	} else {
1570ebfaf1fbSJean Delvare 		data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1571ebfaf1fbSJean Delvare 				    ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1572ebfaf1fbSJean Delvare 	}
1573ebfaf1fbSJean Delvare 	data->bypass_attn &= data->has_voltage;
1574ebfaf1fbSJean Delvare 
15759ed5bc24SGuenter Roeck 	/*
15769ed5bc24SGuenter Roeck 	 * Call adt7475_read_pwm for all pwm's as this will reprogram any
15779ed5bc24SGuenter Roeck 	 * pwm's which are disabled to manual mode with 0% duty cycle
15789ed5bc24SGuenter Roeck 	 */
15791c301fc5SJordan Crouse 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
15801c301fc5SJordan Crouse 		adt7475_read_pwm(client, i);
15811c301fc5SJordan Crouse 
15824abdf38dSChris Packham 	/* Start monitoring */
15834abdf38dSChris Packham 	switch (chip) {
15844abdf38dSChris Packham 	case adt7475:
15854abdf38dSChris Packham 	case adt7476:
15864abdf38dSChris Packham 		i2c_smbus_write_byte_data(client, REG_CONFIG1,
15874abdf38dSChris Packham 					  adt7475_read(REG_CONFIG1) | 0x01);
15884abdf38dSChris Packham 		break;
15894abdf38dSChris Packham 	default:
15904abdf38dSChris Packham 		break;
15914abdf38dSChris Packham 	}
15924abdf38dSChris Packham 
15931c301fc5SJordan Crouse 	ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
15941c301fc5SJordan Crouse 	if (ret)
1595e3ecb2eeSGuenter Roeck 		return ret;
15961c301fc5SJordan Crouse 
1597378933c9SJean Delvare 	/* Features that can be disabled individually */
1598378933c9SJean Delvare 	if (data->has_fan4) {
1599378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1600378933c9SJean Delvare 		if (ret)
1601378933c9SJean Delvare 			goto eremove;
1602378933c9SJean Delvare 	}
1603378933c9SJean Delvare 	if (data->has_pwm2) {
1604378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1605378933c9SJean Delvare 		if (ret)
1606378933c9SJean Delvare 			goto eremove;
1607378933c9SJean Delvare 	}
1608378933c9SJean Delvare 	if (data->has_voltage & (1 << 0)) {
1609378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1610378933c9SJean Delvare 		if (ret)
1611378933c9SJean Delvare 			goto eremove;
1612378933c9SJean Delvare 	}
1613d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3)) {
1614d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1615d8d2ee07SJean Delvare 		if (ret)
1616d8d2ee07SJean Delvare 			goto eremove;
1617d8d2ee07SJean Delvare 	}
1618d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4)) {
1619d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1620d8d2ee07SJean Delvare 		if (ret)
1621d8d2ee07SJean Delvare 			goto eremove;
1622d8d2ee07SJean Delvare 	}
1623d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1624d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1625d8d2ee07SJean Delvare 		if (ret)
1626d8d2ee07SJean Delvare 			goto eremove;
1627d8d2ee07SJean Delvare 	}
162854fe4671SJean Delvare 	if (data->has_vid) {
162954fe4671SJean Delvare 		data->vrm = vid_which_vrm();
163054fe4671SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
163154fe4671SJean Delvare 		if (ret)
163254fe4671SJean Delvare 			goto eremove;
163354fe4671SJean Delvare 	}
1634378933c9SJean Delvare 
16351c301fc5SJordan Crouse 	data->hwmon_dev = hwmon_device_register(&client->dev);
16361c301fc5SJordan Crouse 	if (IS_ERR(data->hwmon_dev)) {
16371c301fc5SJordan Crouse 		ret = PTR_ERR(data->hwmon_dev);
16381c301fc5SJordan Crouse 		goto eremove;
16391c301fc5SJordan Crouse 	}
16401c301fc5SJordan Crouse 
1641d07ca4adSJean Delvare 	dev_info(&client->dev, "%s device, revision %d\n",
1642d07ca4adSJean Delvare 		 names[id->driver_data], revision);
1643d8d2ee07SJean Delvare 	if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
164454fe4671SJean Delvare 		dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1645d07ca4adSJean Delvare 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1646d8d2ee07SJean Delvare 			 (data->has_voltage & (1 << 4)) ? " in4" : "",
1647d07ca4adSJean Delvare 			 data->has_fan4 ? " fan4" : "",
164854fe4671SJean Delvare 			 data->has_pwm2 ? " pwm2" : "",
164954fe4671SJean Delvare 			 data->has_vid ? " vid" : "");
1650ebfaf1fbSJean Delvare 	if (data->bypass_attn)
1651ebfaf1fbSJean Delvare 		dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1652ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1653ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1654ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1655ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1656d07ca4adSJean Delvare 
16575cf943edSTokunori Ikegami 	/* Limits and settings, should never change update more than once */
16584afec79fSTokunori Ikegami 	ret = adt7475_update_limits(client);
16594afec79fSTokunori Ikegami 	if (ret)
16604afec79fSTokunori Ikegami 		goto eremove;
16615cf943edSTokunori Ikegami 
16621c301fc5SJordan Crouse 	return 0;
16631c301fc5SJordan Crouse 
16641c301fc5SJordan Crouse eremove:
16650f14480bSJean Delvare 	adt7475_remove_files(client, data);
16661c301fc5SJordan Crouse 	return ret;
16671c301fc5SJordan Crouse }
16681c301fc5SJordan Crouse 
16691c301fc5SJordan Crouse static int adt7475_remove(struct i2c_client *client)
16701c301fc5SJordan Crouse {
16711c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
16721c301fc5SJordan Crouse 
16731c301fc5SJordan Crouse 	hwmon_device_unregister(data->hwmon_dev);
16740f14480bSJean Delvare 	adt7475_remove_files(client, data);
16751c301fc5SJordan Crouse 
16761c301fc5SJordan Crouse 	return 0;
16771c301fc5SJordan Crouse }
16781c301fc5SJordan Crouse 
16791c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = {
16801c301fc5SJordan Crouse 	.class		= I2C_CLASS_HWMON,
16811c301fc5SJordan Crouse 	.driver = {
16821c301fc5SJordan Crouse 		.name	= "adt7475",
16834e2496e4SJavier Martinez Canillas 		.of_match_table = of_match_ptr(adt7475_of_match),
16841c301fc5SJordan Crouse 	},
16851c301fc5SJordan Crouse 	.probe		= adt7475_probe,
16861c301fc5SJordan Crouse 	.remove		= adt7475_remove,
16871c301fc5SJordan Crouse 	.id_table	= adt7475_id,
16881c301fc5SJordan Crouse 	.detect		= adt7475_detect,
1689c3813d6aSJean Delvare 	.address_list	= normal_i2c,
16901c301fc5SJordan Crouse };
16911c301fc5SJordan Crouse 
16921c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client)
16931c301fc5SJordan Crouse {
16941c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
16951c301fc5SJordan Crouse 
16961c301fc5SJordan Crouse 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
16971c301fc5SJordan Crouse 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
16981c301fc5SJordan Crouse 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
16991c301fc5SJordan Crouse }
17001c301fc5SJordan Crouse 
17011c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index)
17021c301fc5SJordan Crouse {
17031c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17041c301fc5SJordan Crouse 	unsigned int v;
17051c301fc5SJordan Crouse 
17061c301fc5SJordan Crouse 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
17071c301fc5SJordan Crouse 
17089ed5bc24SGuenter Roeck 	/*
17099ed5bc24SGuenter Roeck 	 * Figure out the internal value for pwmctrl and pwmchan
17109ed5bc24SGuenter Roeck 	 * based on the current settings
17119ed5bc24SGuenter Roeck 	 */
17121c301fc5SJordan Crouse 	v = (data->pwm[CONTROL][index] >> 5) & 7;
17131c301fc5SJordan Crouse 
17141c301fc5SJordan Crouse 	if (v == 3)
17151c301fc5SJordan Crouse 		data->pwmctl[index] = 0;
17161c301fc5SJordan Crouse 	else if (v == 7)
17171c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
17181c301fc5SJordan Crouse 	else if (v == 4) {
17199ed5bc24SGuenter Roeck 		/*
17209ed5bc24SGuenter Roeck 		 * The fan is disabled - we don't want to
17219ed5bc24SGuenter Roeck 		 * support that, so change to manual mode and
17229ed5bc24SGuenter Roeck 		 * set the duty cycle to 0 instead
17231c301fc5SJordan Crouse 		 */
17241c301fc5SJordan Crouse 		data->pwm[INPUT][index] = 0;
17251c301fc5SJordan Crouse 		data->pwm[CONTROL][index] &= ~0xE0;
17261c301fc5SJordan Crouse 		data->pwm[CONTROL][index] |= (7 << 5);
17271c301fc5SJordan Crouse 
17281c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
17291c301fc5SJordan Crouse 					  data->pwm[INPUT][index]);
17301c301fc5SJordan Crouse 
17311c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
17321c301fc5SJordan Crouse 					  data->pwm[CONTROL][index]);
17331c301fc5SJordan Crouse 
17341c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
17351c301fc5SJordan Crouse 	} else {
17361c301fc5SJordan Crouse 		data->pwmctl[index] = 2;
17371c301fc5SJordan Crouse 
17381c301fc5SJordan Crouse 		switch (v) {
17391c301fc5SJordan Crouse 		case 0:
17401c301fc5SJordan Crouse 			data->pwmchan[index] = 1;
17411c301fc5SJordan Crouse 			break;
17421c301fc5SJordan Crouse 		case 1:
17431c301fc5SJordan Crouse 			data->pwmchan[index] = 2;
17441c301fc5SJordan Crouse 			break;
17451c301fc5SJordan Crouse 		case 2:
17461c301fc5SJordan Crouse 			data->pwmchan[index] = 4;
17471c301fc5SJordan Crouse 			break;
17481c301fc5SJordan Crouse 		case 5:
17491c301fc5SJordan Crouse 			data->pwmchan[index] = 6;
17501c301fc5SJordan Crouse 			break;
17511c301fc5SJordan Crouse 		case 6:
17521c301fc5SJordan Crouse 			data->pwmchan[index] = 7;
17531c301fc5SJordan Crouse 			break;
17541c301fc5SJordan Crouse 		}
17551c301fc5SJordan Crouse 	}
17561c301fc5SJordan Crouse }
17571c301fc5SJordan Crouse 
1758702afeadSTokunori Ikegami static int adt7475_update_measure(struct device *dev)
17591c301fc5SJordan Crouse {
17601c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
17611c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17623d849981SJean Delvare 	u16 ext;
17631c301fc5SJordan Crouse 	int i;
1764702afeadSTokunori Ikegami 	int ret;
17651c301fc5SJordan Crouse 
1766702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS2);
1767702afeadSTokunori Ikegami 	if (ret < 0)
1768702afeadSTokunori Ikegami 		return ret;
1769702afeadSTokunori Ikegami 	data->alarms = ret << 8;
17701c301fc5SJordan Crouse 
1771702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS1);
1772702afeadSTokunori Ikegami 	if (ret < 0)
1773702afeadSTokunori Ikegami 		return ret;
1774702afeadSTokunori Ikegami 	data->alarms |= ret;
1775702afeadSTokunori Ikegami 
1776702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND2);
1777702afeadSTokunori Ikegami 	if (ret < 0)
1778702afeadSTokunori Ikegami 		return ret;
1779702afeadSTokunori Ikegami 
1780702afeadSTokunori Ikegami 	ext = (ret << 8);
1781702afeadSTokunori Ikegami 
1782702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND1);
1783702afeadSTokunori Ikegami 	if (ret < 0)
1784702afeadSTokunori Ikegami 		return ret;
1785702afeadSTokunori Ikegami 
1786702afeadSTokunori Ikegami 	ext |= ret;
1787702afeadSTokunori Ikegami 
1788cffb9dd0SJean Delvare 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1789cffb9dd0SJean Delvare 		if (!(data->has_voltage & (1 << i)))
1790cffb9dd0SJean Delvare 			continue;
1791702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_REG(i));
1792702afeadSTokunori Ikegami 		if (ret < 0)
1793702afeadSTokunori Ikegami 			return ret;
17941c301fc5SJordan Crouse 		data->voltage[INPUT][i] =
1795702afeadSTokunori Ikegami 			(ret << 2) |
1796cffb9dd0SJean Delvare 			((ext >> (i * 2)) & 3);
1797cffb9dd0SJean Delvare 	}
17981c301fc5SJordan Crouse 
1799702afeadSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1800702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_REG(i));
1801702afeadSTokunori Ikegami 		if (ret < 0)
1802702afeadSTokunori Ikegami 			return ret;
18031c301fc5SJordan Crouse 		data->temp[INPUT][i] =
1804702afeadSTokunori Ikegami 			(ret << 2) |
18053d849981SJean Delvare 			((ext >> ((i + 5) * 2)) & 3);
1806702afeadSTokunori Ikegami 	}
18073d849981SJean Delvare 
18083d849981SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1809702afeadSTokunori Ikegami 		ret = adt7475_read(REG_STATUS4);
1810702afeadSTokunori Ikegami 		if (ret < 0)
1811702afeadSTokunori Ikegami 			return ret;
1812702afeadSTokunori Ikegami 		data->alarms |= ret << 24;
1813702afeadSTokunori Ikegami 
1814702afeadSTokunori Ikegami 		ret = adt7475_read(REG_EXTEND3);
1815702afeadSTokunori Ikegami 		if (ret < 0)
1816702afeadSTokunori Ikegami 			return ret;
1817702afeadSTokunori Ikegami 		ext = ret;
1818702afeadSTokunori Ikegami 
1819702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT);
1820702afeadSTokunori Ikegami 		if (ret < 0)
1821702afeadSTokunori Ikegami 			return ret;
1822702afeadSTokunori Ikegami 		data->voltage[INPUT][5] = ret << 2 |
18233d849981SJean Delvare 			((ext >> 4) & 3);
18243d849981SJean Delvare 	}
18251c301fc5SJordan Crouse 
1826378933c9SJean Delvare 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1827378933c9SJean Delvare 		if (i == 3 && !data->has_fan4)
1828378933c9SJean Delvare 			continue;
1829702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_REG(i));
1830702afeadSTokunori Ikegami 		if (ret < 0)
1831702afeadSTokunori Ikegami 			return ret;
1832702afeadSTokunori Ikegami 		data->tach[INPUT][i] = ret;
1833378933c9SJean Delvare 	}
18341c301fc5SJordan Crouse 
18351c301fc5SJordan Crouse 	/* Updated by hw when in auto mode */
1836378933c9SJean Delvare 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1837378933c9SJean Delvare 		if (i == 1 && !data->has_pwm2)
1838378933c9SJean Delvare 			continue;
1839702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_REG(i));
1840702afeadSTokunori Ikegami 		if (ret < 0)
1841702afeadSTokunori Ikegami 			return ret;
1842702afeadSTokunori Ikegami 		data->pwm[INPUT][i] = ret;
1843378933c9SJean Delvare 	}
18441c301fc5SJordan Crouse 
1845702afeadSTokunori Ikegami 	if (data->has_vid) {
1846702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VID);
1847702afeadSTokunori Ikegami 		if (ret < 0)
1848702afeadSTokunori Ikegami 			return ret;
1849702afeadSTokunori Ikegami 		data->vid = ret & 0x3f;
1850702afeadSTokunori Ikegami 	}
1851702afeadSTokunori Ikegami 
1852702afeadSTokunori Ikegami 	return 0;
18531c301fc5SJordan Crouse }
18541c301fc5SJordan Crouse 
18555cf943edSTokunori Ikegami static struct adt7475_data *adt7475_update_device(struct device *dev)
18565cf943edSTokunori Ikegami {
18575cf943edSTokunori Ikegami 	struct i2c_client *client = to_i2c_client(dev);
18585cf943edSTokunori Ikegami 	struct adt7475_data *data = i2c_get_clientdata(client);
18594afec79fSTokunori Ikegami 	int ret;
18605cf943edSTokunori Ikegami 
18615cf943edSTokunori Ikegami 	mutex_lock(&data->lock);
18625cf943edSTokunori Ikegami 
18635cf943edSTokunori Ikegami 	/* Measurement values update every 2 seconds */
18645cf943edSTokunori Ikegami 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
18651c301fc5SJordan Crouse 	    !data->valid) {
18664afec79fSTokunori Ikegami 		ret = adt7475_update_measure(dev);
18674afec79fSTokunori Ikegami 		if (ret) {
18684afec79fSTokunori Ikegami 			data->valid = false;
18694afec79fSTokunori Ikegami 			mutex_unlock(&data->lock);
18704afec79fSTokunori Ikegami 			return ERR_PTR(ret);
18714afec79fSTokunori Ikegami 		}
18725cf943edSTokunori Ikegami 		data->measure_updated = jiffies;
1873b36fb171STokunori Ikegami 		data->valid = true;
18741c301fc5SJordan Crouse 	}
18751c301fc5SJordan Crouse 
18761c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
18771c301fc5SJordan Crouse 
18781c301fc5SJordan Crouse 	return data;
18791c301fc5SJordan Crouse }
18801c301fc5SJordan Crouse 
1881f0967eeaSAxel Lin module_i2c_driver(adt7475_driver);
18821c301fc5SJordan Crouse 
18831c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc");
18841c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver");
18851c301fc5SJordan Crouse MODULE_LICENSE("GPL");
1886