xref: /linux/drivers/hwmon/adt7475.c (revision 702afead45e15407bea94ccee16b801bbe9db13e)
11c301fc5SJordan Crouse /*
21c301fc5SJordan Crouse  * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
31c301fc5SJordan Crouse  * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
41c301fc5SJordan Crouse  * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
51c301fc5SJordan Crouse  * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
67c81c60fSJean Delvare  * Copyright (C) 2009 Jean Delvare <jdelvare@suse.de>
73d849981SJean Delvare  *
81c301fc5SJordan Crouse  * Derived from the lm83 driver by Jean Delvare
91c301fc5SJordan Crouse  *
101c301fc5SJordan Crouse  * This program is free software; you can redistribute it and/or modify
111c301fc5SJordan Crouse  * it under the terms of the GNU General Public License version 2 as
121c301fc5SJordan Crouse  * published by the Free Software Foundation.
131c301fc5SJordan Crouse  */
141c301fc5SJordan Crouse 
151c301fc5SJordan Crouse #include <linux/module.h>
164e2496e4SJavier Martinez Canillas #include <linux/of_device.h>
171c301fc5SJordan Crouse #include <linux/init.h>
181c301fc5SJordan Crouse #include <linux/slab.h>
191c301fc5SJordan Crouse #include <linux/i2c.h>
201c301fc5SJordan Crouse #include <linux/hwmon.h>
211c301fc5SJordan Crouse #include <linux/hwmon-sysfs.h>
2254fe4671SJean Delvare #include <linux/hwmon-vid.h>
231c301fc5SJordan Crouse #include <linux/err.h>
24dcd8f392SJean Delvare #include <linux/jiffies.h>
25e4651640SChris Packham #include <linux/util_macros.h>
261c301fc5SJordan Crouse 
271c301fc5SJordan Crouse /* Indexes for the sysfs hooks */
281c301fc5SJordan Crouse 
291c301fc5SJordan Crouse #define INPUT		0
301c301fc5SJordan Crouse #define MIN		1
311c301fc5SJordan Crouse #define MAX		2
321c301fc5SJordan Crouse #define CONTROL		3
331c301fc5SJordan Crouse #define OFFSET		3
341c301fc5SJordan Crouse #define AUTOMIN		4
351c301fc5SJordan Crouse #define THERM		5
361c301fc5SJordan Crouse #define HYSTERSIS	6
371c301fc5SJordan Crouse 
389ed5bc24SGuenter Roeck /*
399ed5bc24SGuenter Roeck  * These are unique identifiers for the sysfs functions - unlike the
409ed5bc24SGuenter Roeck  * numbers above, these are not also indexes into an array
411c301fc5SJordan Crouse  */
421c301fc5SJordan Crouse 
431c301fc5SJordan Crouse #define ALARM		9
441c301fc5SJordan Crouse #define FAULT		10
451c301fc5SJordan Crouse 
461c301fc5SJordan Crouse /* 7475 Common Registers */
471c301fc5SJordan Crouse 
48d07ca4adSJean Delvare #define REG_DEVREV2		0x12	/* ADT7490 only */
49d07ca4adSJean Delvare 
503d849981SJean Delvare #define REG_VTT			0x1E	/* ADT7490 only */
513d849981SJean Delvare #define REG_EXTEND3		0x1F	/* ADT7490 only */
523d849981SJean Delvare 
53cffb9dd0SJean Delvare #define REG_VOLTAGE_BASE	0x20
541c301fc5SJordan Crouse #define REG_TEMP_BASE		0x25
551c301fc5SJordan Crouse #define REG_TACH_BASE		0x28
561c301fc5SJordan Crouse #define REG_PWM_BASE		0x30
571c301fc5SJordan Crouse #define REG_PWM_MAX_BASE	0x38
581c301fc5SJordan Crouse 
591c301fc5SJordan Crouse #define REG_DEVID		0x3D
601c301fc5SJordan Crouse #define REG_VENDID		0x3E
61d656b6fdSJean Delvare #define REG_DEVID2		0x3F
621c301fc5SJordan Crouse 
634abdf38dSChris Packham #define REG_CONFIG1		0x40
644abdf38dSChris Packham 
651c301fc5SJordan Crouse #define REG_STATUS1		0x41
661c301fc5SJordan Crouse #define REG_STATUS2		0x42
671c301fc5SJordan Crouse 
68d8d2ee07SJean Delvare #define REG_VID			0x43	/* ADT7476 only */
69d8d2ee07SJean Delvare 
70cffb9dd0SJean Delvare #define REG_VOLTAGE_MIN_BASE	0x44
71cffb9dd0SJean Delvare #define REG_VOLTAGE_MAX_BASE	0x45
721c301fc5SJordan Crouse 
731c301fc5SJordan Crouse #define REG_TEMP_MIN_BASE	0x4E
741c301fc5SJordan Crouse #define REG_TEMP_MAX_BASE	0x4F
751c301fc5SJordan Crouse 
761c301fc5SJordan Crouse #define REG_TACH_MIN_BASE	0x54
771c301fc5SJordan Crouse 
781c301fc5SJordan Crouse #define REG_PWM_CONFIG_BASE	0x5C
791c301fc5SJordan Crouse 
801c301fc5SJordan Crouse #define REG_TEMP_TRANGE_BASE	0x5F
811c301fc5SJordan Crouse 
821d58f5efSChris Packham #define REG_ENHANCE_ACOUSTICS1	0x62
831d58f5efSChris Packham #define REG_ENHANCE_ACOUSTICS2	0x63
841d58f5efSChris Packham 
851c301fc5SJordan Crouse #define REG_PWM_MIN_BASE	0x64
861c301fc5SJordan Crouse 
871c301fc5SJordan Crouse #define REG_TEMP_TMIN_BASE	0x67
881c301fc5SJordan Crouse #define REG_TEMP_THERM_BASE	0x6A
891c301fc5SJordan Crouse 
901c301fc5SJordan Crouse #define REG_REMOTE1_HYSTERSIS	0x6D
911c301fc5SJordan Crouse #define REG_REMOTE2_HYSTERSIS	0x6E
921c301fc5SJordan Crouse 
931c301fc5SJordan Crouse #define REG_TEMP_OFFSET_BASE	0x70
941c301fc5SJordan Crouse 
95ebfaf1fbSJean Delvare #define REG_CONFIG2		0x73
96ebfaf1fbSJean Delvare 
971c301fc5SJordan Crouse #define REG_EXTEND1		0x76
981c301fc5SJordan Crouse #define REG_EXTEND2		0x77
99378933c9SJean Delvare 
100378933c9SJean Delvare #define REG_CONFIG3		0x78
1011c301fc5SJordan Crouse #define REG_CONFIG5		0x7C
102f99318b2SJean Delvare #define REG_CONFIG4		0x7D
103f99318b2SJean Delvare 
1043d849981SJean Delvare #define REG_STATUS4		0x81	/* ADT7490 only */
1053d849981SJean Delvare 
1063d849981SJean Delvare #define REG_VTT_MIN		0x84	/* ADT7490 only */
1073d849981SJean Delvare #define REG_VTT_MAX		0x86	/* ADT7490 only */
1083d849981SJean Delvare 
109d8d2ee07SJean Delvare #define VID_VIDSEL		0x80	/* ADT7476 only */
110d8d2ee07SJean Delvare 
111ebfaf1fbSJean Delvare #define CONFIG2_ATTN		0x20
112ebfaf1fbSJean Delvare 
113378933c9SJean Delvare #define CONFIG3_SMBALERT	0x01
114378933c9SJean Delvare #define CONFIG3_THERM		0x02
115378933c9SJean Delvare 
116378933c9SJean Delvare #define CONFIG4_PINFUNC		0x03
117f99318b2SJean Delvare #define CONFIG4_MAXDUTY		0x08
118ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN10	0x30
119ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN43	0xC0
1201c301fc5SJordan Crouse 
1211c301fc5SJordan Crouse #define CONFIG5_TWOSCOMP	0x01
1221c301fc5SJordan Crouse #define CONFIG5_TEMPOFFSET	0x02
12354fe4671SJean Delvare #define CONFIG5_VIDGPIO		0x10	/* ADT7476 only */
1241c301fc5SJordan Crouse 
1251c301fc5SJordan Crouse /* ADT7475 Settings */
1261c301fc5SJordan Crouse 
127cffb9dd0SJean Delvare #define ADT7475_VOLTAGE_COUNT	5	/* Not counting Vtt */
1281c301fc5SJordan Crouse #define ADT7475_TEMP_COUNT	3
1291c301fc5SJordan Crouse #define ADT7475_TACH_COUNT	4
1301c301fc5SJordan Crouse #define ADT7475_PWM_COUNT	3
1311c301fc5SJordan Crouse 
1321c301fc5SJordan Crouse /* Macro to read the registers */
1331c301fc5SJordan Crouse 
1341c301fc5SJordan Crouse #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
1351c301fc5SJordan Crouse 
1361c301fc5SJordan Crouse /* Macros to easily index the registers */
1371c301fc5SJordan Crouse 
1381c301fc5SJordan Crouse #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
1391c301fc5SJordan Crouse #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
1401c301fc5SJordan Crouse 
1411c301fc5SJordan Crouse #define PWM_REG(idx) (REG_PWM_BASE + (idx))
1421c301fc5SJordan Crouse #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
1431c301fc5SJordan Crouse #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
1441c301fc5SJordan Crouse #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
1451c301fc5SJordan Crouse 
1461c301fc5SJordan Crouse #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
1471c301fc5SJordan Crouse #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
1481c301fc5SJordan Crouse #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
1491c301fc5SJordan Crouse 
1501c301fc5SJordan Crouse #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
1511c301fc5SJordan Crouse #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
1521c301fc5SJordan Crouse #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
1531c301fc5SJordan Crouse #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
1541c301fc5SJordan Crouse #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
1551c301fc5SJordan Crouse #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
1561c301fc5SJordan Crouse #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
1571c301fc5SJordan Crouse 
158918ee91cSJean Delvare static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1591c301fc5SJordan Crouse 
160e5e9f44cSJean Delvare enum chips { adt7473, adt7475, adt7476, adt7490 };
1611c301fc5SJordan Crouse 
1621c301fc5SJordan Crouse static const struct i2c_device_id adt7475_id[] = {
163b180d050SJean Delvare 	{ "adt7473", adt7473 },
1641c301fc5SJordan Crouse 	{ "adt7475", adt7475 },
165d8d2ee07SJean Delvare 	{ "adt7476", adt7476 },
1663d849981SJean Delvare 	{ "adt7490", adt7490 },
1671c301fc5SJordan Crouse 	{ }
1681c301fc5SJordan Crouse };
1691c301fc5SJordan Crouse MODULE_DEVICE_TABLE(i2c, adt7475_id);
1701c301fc5SJordan Crouse 
1714e2496e4SJavier Martinez Canillas static const struct of_device_id adt7475_of_match[] = {
1724e2496e4SJavier Martinez Canillas 	{
1734e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7473",
1744e2496e4SJavier Martinez Canillas 		.data = (void *)adt7473
1754e2496e4SJavier Martinez Canillas 	},
1764e2496e4SJavier Martinez Canillas 	{
1774e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7475",
1784e2496e4SJavier Martinez Canillas 		.data = (void *)adt7475
1794e2496e4SJavier Martinez Canillas 	},
1804e2496e4SJavier Martinez Canillas 	{
1814e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7476",
1824e2496e4SJavier Martinez Canillas 		.data = (void *)adt7476
1834e2496e4SJavier Martinez Canillas 	},
1844e2496e4SJavier Martinez Canillas 	{
1854e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7490",
1864e2496e4SJavier Martinez Canillas 		.data = (void *)adt7490
1874e2496e4SJavier Martinez Canillas 	},
1884e2496e4SJavier Martinez Canillas 	{ },
1894e2496e4SJavier Martinez Canillas };
1904e2496e4SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, adt7475_of_match);
1914e2496e4SJavier Martinez Canillas 
1921c301fc5SJordan Crouse struct adt7475_data {
1931c301fc5SJordan Crouse 	struct device *hwmon_dev;
1941c301fc5SJordan Crouse 	struct mutex lock;
1951c301fc5SJordan Crouse 
1961c301fc5SJordan Crouse 	unsigned long measure_updated;
197b36fb171STokunori Ikegami 	bool valid;
1981c301fc5SJordan Crouse 
199f99318b2SJean Delvare 	u8 config4;
2001c301fc5SJordan Crouse 	u8 config5;
201cffb9dd0SJean Delvare 	u8 has_voltage;
202ebfaf1fbSJean Delvare 	u8 bypass_attn;		/* Bypass voltage attenuator */
203378933c9SJean Delvare 	u8 has_pwm2:1;
204378933c9SJean Delvare 	u8 has_fan4:1;
20554fe4671SJean Delvare 	u8 has_vid:1;
2063d849981SJean Delvare 	u32 alarms;
207cffb9dd0SJean Delvare 	u16 voltage[3][6];
2081c301fc5SJordan Crouse 	u16 temp[7][3];
2091c301fc5SJordan Crouse 	u16 tach[2][4];
2101c301fc5SJordan Crouse 	u8 pwm[4][3];
2111c301fc5SJordan Crouse 	u8 range[3];
2121c301fc5SJordan Crouse 	u8 pwmctl[3];
2131c301fc5SJordan Crouse 	u8 pwmchan[3];
2141d58f5efSChris Packham 	u8 enh_acoustics[2];
21554fe4671SJean Delvare 
21654fe4671SJean Delvare 	u8 vid;
21754fe4671SJean Delvare 	u8 vrm;
2181c301fc5SJordan Crouse };
2191c301fc5SJordan Crouse 
2201c301fc5SJordan Crouse static struct i2c_driver adt7475_driver;
2211c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev);
2221c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client);
2231c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index);
2241c301fc5SJordan Crouse 
2251c301fc5SJordan Crouse /* Given a temp value, convert it to register value */
2261c301fc5SJordan Crouse 
2271c301fc5SJordan Crouse static inline u16 temp2reg(struct adt7475_data *data, long val)
2281c301fc5SJordan Crouse {
2291c301fc5SJordan Crouse 	u16 ret;
2301c301fc5SJordan Crouse 
2311c301fc5SJordan Crouse 	if (!(data->config5 & CONFIG5_TWOSCOMP)) {
2322a844c14SGuenter Roeck 		val = clamp_val(val, -64000, 191000);
2331c301fc5SJordan Crouse 		ret = (val + 64500) / 1000;
2341c301fc5SJordan Crouse 	} else {
2352a844c14SGuenter Roeck 		val = clamp_val(val, -128000, 127000);
2361c301fc5SJordan Crouse 		if (val < -500)
2371c301fc5SJordan Crouse 			ret = (256500 + val) / 1000;
2381c301fc5SJordan Crouse 		else
2391c301fc5SJordan Crouse 			ret = (val + 500) / 1000;
2401c301fc5SJordan Crouse 	}
2411c301fc5SJordan Crouse 
2421c301fc5SJordan Crouse 	return ret << 2;
2431c301fc5SJordan Crouse }
2441c301fc5SJordan Crouse 
2451c301fc5SJordan Crouse /* Given a register value, convert it to a real temp value */
2461c301fc5SJordan Crouse 
2471c301fc5SJordan Crouse static inline int reg2temp(struct adt7475_data *data, u16 reg)
2481c301fc5SJordan Crouse {
2491c301fc5SJordan Crouse 	if (data->config5 & CONFIG5_TWOSCOMP) {
2501c301fc5SJordan Crouse 		if (reg >= 512)
2511c301fc5SJordan Crouse 			return (reg - 1024) * 250;
2521c301fc5SJordan Crouse 		else
2531c301fc5SJordan Crouse 			return reg * 250;
2541c301fc5SJordan Crouse 	} else
2551c301fc5SJordan Crouse 		return (reg - 256) * 250;
2561c301fc5SJordan Crouse }
2571c301fc5SJordan Crouse 
2581c301fc5SJordan Crouse static inline int tach2rpm(u16 tach)
2591c301fc5SJordan Crouse {
2601c301fc5SJordan Crouse 	if (tach == 0 || tach == 0xFFFF)
2611c301fc5SJordan Crouse 		return 0;
2621c301fc5SJordan Crouse 
2631c301fc5SJordan Crouse 	return (90000 * 60) / tach;
2641c301fc5SJordan Crouse }
2651c301fc5SJordan Crouse 
2661c301fc5SJordan Crouse static inline u16 rpm2tach(unsigned long rpm)
2671c301fc5SJordan Crouse {
2681c301fc5SJordan Crouse 	if (rpm == 0)
2691c301fc5SJordan Crouse 		return 0;
2701c301fc5SJordan Crouse 
2712a844c14SGuenter Roeck 	return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
2721c301fc5SJordan Crouse }
2731c301fc5SJordan Crouse 
274cffb9dd0SJean Delvare /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
275cffb9dd0SJean Delvare static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
276cffb9dd0SJean Delvare 	{ 45, 94 },	/* +2.5V */
277cffb9dd0SJean Delvare 	{ 175, 525 },	/* Vccp */
278cffb9dd0SJean Delvare 	{ 68, 71 },	/* Vcc */
279cffb9dd0SJean Delvare 	{ 93, 47 },	/* +5V */
280cffb9dd0SJean Delvare 	{ 120, 20 },	/* +12V */
281cffb9dd0SJean Delvare 	{ 45, 45 },	/* Vtt */
282cffb9dd0SJean Delvare };
283cffb9dd0SJean Delvare 
284ebfaf1fbSJean Delvare static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
2851c301fc5SJordan Crouse {
286cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
287cffb9dd0SJean Delvare 
288ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
289ebfaf1fbSJean Delvare 		return DIV_ROUND_CLOSEST(reg * 2250, 1024);
290cffb9dd0SJean Delvare 	return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
2911c301fc5SJordan Crouse }
2921c301fc5SJordan Crouse 
293ebfaf1fbSJean Delvare static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
2941c301fc5SJordan Crouse {
295cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
296cffb9dd0SJean Delvare 	long reg;
2971c301fc5SJordan Crouse 
298ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
299ebfaf1fbSJean Delvare 		reg = (volt * 1024) / 2250;
300ebfaf1fbSJean Delvare 	else
301cffb9dd0SJean Delvare 		reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
3022a844c14SGuenter Roeck 	return clamp_val(reg, 0, 1023) & (0xff << 2);
3031c301fc5SJordan Crouse }
3041c301fc5SJordan Crouse 
3051c301fc5SJordan Crouse static u16 adt7475_read_word(struct i2c_client *client, int reg)
3061c301fc5SJordan Crouse {
3071c301fc5SJordan Crouse 	u16 val;
3081c301fc5SJordan Crouse 
3091c301fc5SJordan Crouse 	val = i2c_smbus_read_byte_data(client, reg);
3101c301fc5SJordan Crouse 	val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
3111c301fc5SJordan Crouse 
3121c301fc5SJordan Crouse 	return val;
3131c301fc5SJordan Crouse }
3141c301fc5SJordan Crouse 
3151c301fc5SJordan Crouse static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
3161c301fc5SJordan Crouse {
3171c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
3181c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, val & 0xFF);
3191c301fc5SJordan Crouse }
3201c301fc5SJordan Crouse 
3211c301fc5SJordan Crouse static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
3221c301fc5SJordan Crouse 			    char *buf)
3231c301fc5SJordan Crouse {
3241c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3251c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3261c301fc5SJordan Crouse 	unsigned short val;
3271c301fc5SJordan Crouse 
3281c301fc5SJordan Crouse 	switch (sattr->nr) {
3291c301fc5SJordan Crouse 	case ALARM:
3301c301fc5SJordan Crouse 		return sprintf(buf, "%d\n",
331cffb9dd0SJean Delvare 			       (data->alarms >> sattr->index) & 1);
3321c301fc5SJordan Crouse 	default:
3331c301fc5SJordan Crouse 		val = data->voltage[sattr->nr][sattr->index];
334ebfaf1fbSJean Delvare 		return sprintf(buf, "%d\n",
335ebfaf1fbSJean Delvare 			       reg2volt(sattr->index, val, data->bypass_attn));
3361c301fc5SJordan Crouse 	}
3371c301fc5SJordan Crouse }
3381c301fc5SJordan Crouse 
3391c301fc5SJordan Crouse static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
3401c301fc5SJordan Crouse 			   const char *buf, size_t count)
3411c301fc5SJordan Crouse {
3421c301fc5SJordan Crouse 
3431c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3441c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
3451c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
3461c301fc5SJordan Crouse 	unsigned char reg;
3471c301fc5SJordan Crouse 	long val;
3481c301fc5SJordan Crouse 
349179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
3501c301fc5SJordan Crouse 		return -EINVAL;
3511c301fc5SJordan Crouse 
3521c301fc5SJordan Crouse 	mutex_lock(&data->lock);
3531c301fc5SJordan Crouse 
354ebfaf1fbSJean Delvare 	data->voltage[sattr->nr][sattr->index] =
355ebfaf1fbSJean Delvare 				volt2reg(sattr->index, val, data->bypass_attn);
3561c301fc5SJordan Crouse 
3573d849981SJean Delvare 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
3581c301fc5SJordan Crouse 		if (sattr->nr == MIN)
3591c301fc5SJordan Crouse 			reg = VOLTAGE_MIN_REG(sattr->index);
3601c301fc5SJordan Crouse 		else
3611c301fc5SJordan Crouse 			reg = VOLTAGE_MAX_REG(sattr->index);
3623d849981SJean Delvare 	} else {
3633d849981SJean Delvare 		if (sattr->nr == MIN)
3643d849981SJean Delvare 			reg = REG_VTT_MIN;
3653d849981SJean Delvare 		else
3663d849981SJean Delvare 			reg = REG_VTT_MAX;
3673d849981SJean Delvare 	}
3681c301fc5SJordan Crouse 
3691c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
3701c301fc5SJordan Crouse 				  data->voltage[sattr->nr][sattr->index] >> 2);
3711c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
3721c301fc5SJordan Crouse 
3731c301fc5SJordan Crouse 	return count;
3741c301fc5SJordan Crouse }
3751c301fc5SJordan Crouse 
3761c301fc5SJordan Crouse static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
3771c301fc5SJordan Crouse 			 char *buf)
3781c301fc5SJordan Crouse {
3791c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3801c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3811c301fc5SJordan Crouse 	int out;
3821c301fc5SJordan Crouse 
3831c301fc5SJordan Crouse 	switch (sattr->nr) {
3841c301fc5SJordan Crouse 	case HYSTERSIS:
3851c301fc5SJordan Crouse 		mutex_lock(&data->lock);
3861c301fc5SJordan Crouse 		out = data->temp[sattr->nr][sattr->index];
3871c301fc5SJordan Crouse 		if (sattr->index != 1)
3881c301fc5SJordan Crouse 			out = (out >> 4) & 0xF;
3891c301fc5SJordan Crouse 		else
3901c301fc5SJordan Crouse 			out = (out & 0xF);
3919ed5bc24SGuenter Roeck 		/*
3929ed5bc24SGuenter Roeck 		 * Show the value as an absolute number tied to
3939ed5bc24SGuenter Roeck 		 * THERM
3949ed5bc24SGuenter Roeck 		 */
3951c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
3961c301fc5SJordan Crouse 			out * 1000;
3971c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
3981c301fc5SJordan Crouse 		break;
3991c301fc5SJordan Crouse 
4001c301fc5SJordan Crouse 	case OFFSET:
4019ed5bc24SGuenter Roeck 		/*
4029ed5bc24SGuenter Roeck 		 * Offset is always 2's complement, regardless of the
4039ed5bc24SGuenter Roeck 		 * setting in CONFIG5
4049ed5bc24SGuenter Roeck 		 */
4051c301fc5SJordan Crouse 		mutex_lock(&data->lock);
4061c301fc5SJordan Crouse 		out = (s8)data->temp[sattr->nr][sattr->index];
4071c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET)
4081c301fc5SJordan Crouse 			out *= 1000;
4091c301fc5SJordan Crouse 		else
4101c301fc5SJordan Crouse 			out *= 500;
4111c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4121c301fc5SJordan Crouse 		break;
4131c301fc5SJordan Crouse 
4141c301fc5SJordan Crouse 	case ALARM:
4151c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 4)) & 1;
4161c301fc5SJordan Crouse 		break;
4171c301fc5SJordan Crouse 
4181c301fc5SJordan Crouse 	case FAULT:
4191c301fc5SJordan Crouse 		/* Note - only for remote1 and remote2 */
420cf312e07SJean Delvare 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
4211c301fc5SJordan Crouse 		break;
4221c301fc5SJordan Crouse 
4231c301fc5SJordan Crouse 	default:
4241c301fc5SJordan Crouse 		/* All other temp values are in the configured format */
4251c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
4261c301fc5SJordan Crouse 	}
4271c301fc5SJordan Crouse 
4281c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
4291c301fc5SJordan Crouse }
4301c301fc5SJordan Crouse 
4311c301fc5SJordan Crouse static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
4321c301fc5SJordan Crouse 			const char *buf, size_t count)
4331c301fc5SJordan Crouse {
4341c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4351c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
4361c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
4371c301fc5SJordan Crouse 	unsigned char reg = 0;
4381c301fc5SJordan Crouse 	u8 out;
4391c301fc5SJordan Crouse 	int temp;
4401c301fc5SJordan Crouse 	long val;
4411c301fc5SJordan Crouse 
442179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
4431c301fc5SJordan Crouse 		return -EINVAL;
4441c301fc5SJordan Crouse 
4451c301fc5SJordan Crouse 	mutex_lock(&data->lock);
4461c301fc5SJordan Crouse 
4471c301fc5SJordan Crouse 	/* We need the config register in all cases for temp <-> reg conv. */
4481c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
4491c301fc5SJordan Crouse 
4501c301fc5SJordan Crouse 	switch (sattr->nr) {
4511c301fc5SJordan Crouse 	case OFFSET:
4521c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET) {
4532a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 127000);
4541c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 1000;
4551c301fc5SJordan Crouse 		} else {
4562a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 64000);
4571c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 500;
4581c301fc5SJordan Crouse 		}
4591c301fc5SJordan Crouse 		break;
4601c301fc5SJordan Crouse 
4611c301fc5SJordan Crouse 	case HYSTERSIS:
4629ed5bc24SGuenter Roeck 		/*
4639ed5bc24SGuenter Roeck 		 * The value will be given as an absolute value, turn it
4649ed5bc24SGuenter Roeck 		 * into an offset based on THERM
4659ed5bc24SGuenter Roeck 		 */
4661c301fc5SJordan Crouse 
4671c301fc5SJordan Crouse 		/* Read fresh THERM and HYSTERSIS values from the chip */
4681c301fc5SJordan Crouse 		data->temp[THERM][sattr->index] =
4691c301fc5SJordan Crouse 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
4701c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
4711c301fc5SJordan Crouse 
4721c301fc5SJordan Crouse 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
4732a844c14SGuenter Roeck 		val = clamp_val(val, temp - 15000, temp);
4741c301fc5SJordan Crouse 		val = (temp - val) / 1000;
4751c301fc5SJordan Crouse 
4761c301fc5SJordan Crouse 		if (sattr->index != 1) {
4771c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
4781c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
4791c301fc5SJordan Crouse 		} else {
4801c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
4811c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
4821c301fc5SJordan Crouse 		}
4831c301fc5SJordan Crouse 
4841c301fc5SJordan Crouse 		out = data->temp[HYSTERSIS][sattr->index];
4851c301fc5SJordan Crouse 		break;
4861c301fc5SJordan Crouse 
4871c301fc5SJordan Crouse 	default:
4881c301fc5SJordan Crouse 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
4891c301fc5SJordan Crouse 
4909ed5bc24SGuenter Roeck 		/*
4919ed5bc24SGuenter Roeck 		 * We maintain an extra 2 digits of precision for simplicity
4929ed5bc24SGuenter Roeck 		 * - shift those back off before writing the value
4939ed5bc24SGuenter Roeck 		 */
4941c301fc5SJordan Crouse 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
4951c301fc5SJordan Crouse 	}
4961c301fc5SJordan Crouse 
4971c301fc5SJordan Crouse 	switch (sattr->nr) {
4981c301fc5SJordan Crouse 	case MIN:
4991c301fc5SJordan Crouse 		reg = TEMP_MIN_REG(sattr->index);
5001c301fc5SJordan Crouse 		break;
5011c301fc5SJordan Crouse 	case MAX:
5021c301fc5SJordan Crouse 		reg = TEMP_MAX_REG(sattr->index);
5031c301fc5SJordan Crouse 		break;
5041c301fc5SJordan Crouse 	case OFFSET:
5051c301fc5SJordan Crouse 		reg = TEMP_OFFSET_REG(sattr->index);
5061c301fc5SJordan Crouse 		break;
5071c301fc5SJordan Crouse 	case AUTOMIN:
5081c301fc5SJordan Crouse 		reg = TEMP_TMIN_REG(sattr->index);
5091c301fc5SJordan Crouse 		break;
5101c301fc5SJordan Crouse 	case THERM:
5111c301fc5SJordan Crouse 		reg = TEMP_THERM_REG(sattr->index);
5121c301fc5SJordan Crouse 		break;
5131c301fc5SJordan Crouse 	case HYSTERSIS:
5141c301fc5SJordan Crouse 		if (sattr->index != 2)
5151c301fc5SJordan Crouse 			reg = REG_REMOTE1_HYSTERSIS;
5161c301fc5SJordan Crouse 		else
5171c301fc5SJordan Crouse 			reg = REG_REMOTE2_HYSTERSIS;
5181c301fc5SJordan Crouse 
5191c301fc5SJordan Crouse 		break;
5201c301fc5SJordan Crouse 	}
5211c301fc5SJordan Crouse 
5221c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, out);
5231c301fc5SJordan Crouse 
5241c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5251c301fc5SJordan Crouse 	return count;
5261c301fc5SJordan Crouse }
5271c301fc5SJordan Crouse 
5288f05bcc3SChris Packham /* Assuming CONFIG6[SLOW] is 0 */
5298f05bcc3SChris Packham static const int ad7475_st_map[] = {
5308f05bcc3SChris Packham 	37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
5318f05bcc3SChris Packham };
5328f05bcc3SChris Packham 
5338f05bcc3SChris Packham static ssize_t show_temp_st(struct device *dev, struct device_attribute *attr,
5348f05bcc3SChris Packham 				  char *buf)
5358f05bcc3SChris Packham {
5368f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5378f05bcc3SChris Packham 	struct i2c_client *client = to_i2c_client(dev);
5388f05bcc3SChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
5398f05bcc3SChris Packham 	long val;
5408f05bcc3SChris Packham 
5418f05bcc3SChris Packham 	switch (sattr->index) {
5428f05bcc3SChris Packham 	case 0:
5438f05bcc3SChris Packham 		val = data->enh_acoustics[0] & 0xf;
5448f05bcc3SChris Packham 		break;
5458f05bcc3SChris Packham 	case 1:
5468f05bcc3SChris Packham 		val = (data->enh_acoustics[1] >> 4) & 0xf;
5478f05bcc3SChris Packham 		break;
5488f05bcc3SChris Packham 	case 2:
5498f05bcc3SChris Packham 	default:
5508f05bcc3SChris Packham 		val = data->enh_acoustics[1] & 0xf;
5518f05bcc3SChris Packham 		break;
5528f05bcc3SChris Packham 	}
5538f05bcc3SChris Packham 
5548f05bcc3SChris Packham 	if (val & 0x8)
5558f05bcc3SChris Packham 		return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
5568f05bcc3SChris Packham 	else
5578f05bcc3SChris Packham 		return sprintf(buf, "0\n");
5588f05bcc3SChris Packham }
5598f05bcc3SChris Packham 
5608f05bcc3SChris Packham static ssize_t set_temp_st(struct device *dev, struct device_attribute *attr,
5618f05bcc3SChris Packham 				 const char *buf, size_t count)
5628f05bcc3SChris Packham {
5638f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5648f05bcc3SChris Packham 	struct i2c_client *client = to_i2c_client(dev);
5658f05bcc3SChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
5668f05bcc3SChris Packham 	unsigned char reg;
5678f05bcc3SChris Packham 	int shift, idx;
5688f05bcc3SChris Packham 	ulong val;
5698f05bcc3SChris Packham 
5708f05bcc3SChris Packham 	if (kstrtoul(buf, 10, &val))
5718f05bcc3SChris Packham 		return -EINVAL;
5728f05bcc3SChris Packham 
5738f05bcc3SChris Packham 	switch (sattr->index) {
5748f05bcc3SChris Packham 	case 0:
5758f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS1;
5768f05bcc3SChris Packham 		shift = 0;
5778f05bcc3SChris Packham 		idx = 0;
5788f05bcc3SChris Packham 		break;
5798f05bcc3SChris Packham 	case 1:
5808f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
5818f05bcc3SChris Packham 		shift = 0;
5828f05bcc3SChris Packham 		idx = 1;
5838f05bcc3SChris Packham 		break;
5848f05bcc3SChris Packham 	case 2:
5858f05bcc3SChris Packham 	default:
5868f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
5878f05bcc3SChris Packham 		shift = 4;
5888f05bcc3SChris Packham 		idx = 1;
5898f05bcc3SChris Packham 		break;
5908f05bcc3SChris Packham 	}
5918f05bcc3SChris Packham 
5928f05bcc3SChris Packham 	if (val > 0) {
5938f05bcc3SChris Packham 		val = find_closest_descending(val, ad7475_st_map,
5948f05bcc3SChris Packham 					      ARRAY_SIZE(ad7475_st_map));
5958f05bcc3SChris Packham 		val |= 0x8;
5968f05bcc3SChris Packham 	}
5978f05bcc3SChris Packham 
5988f05bcc3SChris Packham 	mutex_lock(&data->lock);
5998f05bcc3SChris Packham 
6008f05bcc3SChris Packham 	data->enh_acoustics[idx] &= ~(0xf << shift);
6018f05bcc3SChris Packham 	data->enh_acoustics[idx] |= (val << shift);
6028f05bcc3SChris Packham 
6038f05bcc3SChris Packham 	i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
6048f05bcc3SChris Packham 
6058f05bcc3SChris Packham 	mutex_unlock(&data->lock);
6068f05bcc3SChris Packham 
6078f05bcc3SChris Packham 	return count;
6088f05bcc3SChris Packham }
6098f05bcc3SChris Packham 
6109ed5bc24SGuenter Roeck /*
6119ed5bc24SGuenter Roeck  * Table of autorange values - the user will write the value in millidegrees,
6129ed5bc24SGuenter Roeck  * and we'll convert it
6139ed5bc24SGuenter Roeck  */
6141c301fc5SJordan Crouse static const int autorange_table[] = {
6151c301fc5SJordan Crouse 	2000, 2500, 3330, 4000, 5000, 6670, 8000,
6161c301fc5SJordan Crouse 	10000, 13330, 16000, 20000, 26670, 32000, 40000,
6171c301fc5SJordan Crouse 	53330, 80000
6181c301fc5SJordan Crouse };
6191c301fc5SJordan Crouse 
6201c301fc5SJordan Crouse static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
6211c301fc5SJordan Crouse 			   char *buf)
6221c301fc5SJordan Crouse {
6231c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6241c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6251c301fc5SJordan Crouse 	int out, val;
6261c301fc5SJordan Crouse 
6271c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6281c301fc5SJordan Crouse 	out = (data->range[sattr->index] >> 4) & 0x0F;
6291c301fc5SJordan Crouse 	val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6301c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6311c301fc5SJordan Crouse 
6321c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", val + autorange_table[out]);
6331c301fc5SJordan Crouse }
6341c301fc5SJordan Crouse 
6351c301fc5SJordan Crouse static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
6361c301fc5SJordan Crouse 			  const char *buf, size_t count)
6371c301fc5SJordan Crouse {
6381c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
6391c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
6401c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6411c301fc5SJordan Crouse 	int temp;
6421c301fc5SJordan Crouse 	long val;
6431c301fc5SJordan Crouse 
644179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
6451c301fc5SJordan Crouse 		return -EINVAL;
6461c301fc5SJordan Crouse 
6471c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6481c301fc5SJordan Crouse 
6491c301fc5SJordan Crouse 	/* Get a fresh copy of the needed registers */
6501c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
6511c301fc5SJordan Crouse 	data->temp[AUTOMIN][sattr->index] =
6521c301fc5SJordan Crouse 		adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
6531c301fc5SJordan Crouse 	data->range[sattr->index] =
6541c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
6551c301fc5SJordan Crouse 
6569ed5bc24SGuenter Roeck 	/*
6579ed5bc24SGuenter Roeck 	 * The user will write an absolute value, so subtract the start point
6589ed5bc24SGuenter Roeck 	 * to figure the range
6599ed5bc24SGuenter Roeck 	 */
6601c301fc5SJordan Crouse 	temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6612a844c14SGuenter Roeck 	val = clamp_val(val, temp + autorange_table[0],
6621c301fc5SJordan Crouse 		temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
6631c301fc5SJordan Crouse 	val -= temp;
6641c301fc5SJordan Crouse 
6651c301fc5SJordan Crouse 	/* Find the nearest table entry to what the user wrote */
666e4651640SChris Packham 	val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
6671c301fc5SJordan Crouse 
6681c301fc5SJordan Crouse 	data->range[sattr->index] &= ~0xF0;
6691c301fc5SJordan Crouse 	data->range[sattr->index] |= val << 4;
6701c301fc5SJordan Crouse 
6711c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
6721c301fc5SJordan Crouse 				  data->range[sattr->index]);
6731c301fc5SJordan Crouse 
6741c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6751c301fc5SJordan Crouse 	return count;
6761c301fc5SJordan Crouse }
6771c301fc5SJordan Crouse 
6781c301fc5SJordan Crouse static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
6791c301fc5SJordan Crouse 			 char *buf)
6801c301fc5SJordan Crouse {
6811c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6821c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6831c301fc5SJordan Crouse 	int out;
6841c301fc5SJordan Crouse 
6851c301fc5SJordan Crouse 	if (sattr->nr == ALARM)
6861c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 10)) & 1;
6871c301fc5SJordan Crouse 	else
6881c301fc5SJordan Crouse 		out = tach2rpm(data->tach[sattr->nr][sattr->index]);
6891c301fc5SJordan Crouse 
6901c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
6911c301fc5SJordan Crouse }
6921c301fc5SJordan Crouse 
6931c301fc5SJordan Crouse static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
6941c301fc5SJordan Crouse 			const char *buf, size_t count)
6951c301fc5SJordan Crouse {
6961c301fc5SJordan Crouse 
6971c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6981c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
6991c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7001c301fc5SJordan Crouse 	unsigned long val;
7011c301fc5SJordan Crouse 
702179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
7031c301fc5SJordan Crouse 		return -EINVAL;
7041c301fc5SJordan Crouse 
7051c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7061c301fc5SJordan Crouse 
7071c301fc5SJordan Crouse 	data->tach[MIN][sattr->index] = rpm2tach(val);
7081c301fc5SJordan Crouse 
7091c301fc5SJordan Crouse 	adt7475_write_word(client, TACH_MIN_REG(sattr->index),
7101c301fc5SJordan Crouse 			   data->tach[MIN][sattr->index]);
7111c301fc5SJordan Crouse 
7121c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7131c301fc5SJordan Crouse 	return count;
7141c301fc5SJordan Crouse }
7151c301fc5SJordan Crouse 
7161c301fc5SJordan Crouse static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
7171c301fc5SJordan Crouse 			char *buf)
7181c301fc5SJordan Crouse {
7191c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7201c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7211c301fc5SJordan Crouse 
7221c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
7231c301fc5SJordan Crouse }
7241c301fc5SJordan Crouse 
7251c301fc5SJordan Crouse static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
7261c301fc5SJordan Crouse 			    char *buf)
7271c301fc5SJordan Crouse {
7281c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7291c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7301c301fc5SJordan Crouse 
7311c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
7321c301fc5SJordan Crouse }
7331c301fc5SJordan Crouse 
7341c301fc5SJordan Crouse static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
7351c301fc5SJordan Crouse 			    char *buf)
7361c301fc5SJordan Crouse {
7371c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7381c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7391c301fc5SJordan Crouse 
7401c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
7411c301fc5SJordan Crouse }
7421c301fc5SJordan Crouse 
7431c301fc5SJordan Crouse static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
7441c301fc5SJordan Crouse 		       const char *buf, size_t count)
7451c301fc5SJordan Crouse {
7461c301fc5SJordan Crouse 
7471c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7481c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7491c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7501c301fc5SJordan Crouse 	unsigned char reg = 0;
7511c301fc5SJordan Crouse 	long val;
7521c301fc5SJordan Crouse 
753179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
7541c301fc5SJordan Crouse 		return -EINVAL;
7551c301fc5SJordan Crouse 
7561c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7571c301fc5SJordan Crouse 
7581c301fc5SJordan Crouse 	switch (sattr->nr) {
7591c301fc5SJordan Crouse 	case INPUT:
7601c301fc5SJordan Crouse 		/* Get a fresh value for CONTROL */
7611c301fc5SJordan Crouse 		data->pwm[CONTROL][sattr->index] =
7621c301fc5SJordan Crouse 			adt7475_read(PWM_CONFIG_REG(sattr->index));
7631c301fc5SJordan Crouse 
7649ed5bc24SGuenter Roeck 		/*
7659ed5bc24SGuenter Roeck 		 * If we are not in manual mode, then we shouldn't allow
7669ed5bc24SGuenter Roeck 		 * the user to set the pwm speed
7679ed5bc24SGuenter Roeck 		 */
7681c301fc5SJordan Crouse 		if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
7691c301fc5SJordan Crouse 			mutex_unlock(&data->lock);
7701c301fc5SJordan Crouse 			return count;
7711c301fc5SJordan Crouse 		}
7721c301fc5SJordan Crouse 
7731c301fc5SJordan Crouse 		reg = PWM_REG(sattr->index);
7741c301fc5SJordan Crouse 		break;
7751c301fc5SJordan Crouse 
7761c301fc5SJordan Crouse 	case MIN:
7771c301fc5SJordan Crouse 		reg = PWM_MIN_REG(sattr->index);
7781c301fc5SJordan Crouse 		break;
7791c301fc5SJordan Crouse 
7801c301fc5SJordan Crouse 	case MAX:
7811c301fc5SJordan Crouse 		reg = PWM_MAX_REG(sattr->index);
7821c301fc5SJordan Crouse 		break;
7831c301fc5SJordan Crouse 	}
7841c301fc5SJordan Crouse 
7852a844c14SGuenter Roeck 	data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
7861c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
7871c301fc5SJordan Crouse 				  data->pwm[sattr->nr][sattr->index]);
7881d58f5efSChris Packham 	mutex_unlock(&data->lock);
7891d58f5efSChris Packham 
7901d58f5efSChris Packham 	return count;
7911d58f5efSChris Packham }
7921d58f5efSChris Packham 
7931d58f5efSChris Packham static ssize_t show_stall_disable(struct device *dev,
7941d58f5efSChris Packham 				  struct device_attribute *attr, char *buf)
7951d58f5efSChris Packham {
7961d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7971d58f5efSChris Packham 	struct i2c_client *client = to_i2c_client(dev);
7981d58f5efSChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
7991d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8001d58f5efSChris Packham 
8011d58f5efSChris Packham 	return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
8021d58f5efSChris Packham }
8031d58f5efSChris Packham 
8041d58f5efSChris Packham static ssize_t set_stall_disable(struct device *dev,
8051d58f5efSChris Packham 				 struct device_attribute *attr, const char *buf,
8061d58f5efSChris Packham 				 size_t count)
8071d58f5efSChris Packham {
8081d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8091d58f5efSChris Packham 	struct i2c_client *client = to_i2c_client(dev);
8101d58f5efSChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
8111d58f5efSChris Packham 	long val;
8121d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8131d58f5efSChris Packham 
8141d58f5efSChris Packham 	if (kstrtol(buf, 10, &val))
8151d58f5efSChris Packham 		return -EINVAL;
8161d58f5efSChris Packham 
8171d58f5efSChris Packham 	mutex_lock(&data->lock);
8181d58f5efSChris Packham 
8191d58f5efSChris Packham 	data->enh_acoustics[0] &= ~mask;
8201d58f5efSChris Packham 	if (val)
8211d58f5efSChris Packham 		data->enh_acoustics[0] |= mask;
8221d58f5efSChris Packham 
8231d58f5efSChris Packham 	i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
8241d58f5efSChris Packham 				  data->enh_acoustics[0]);
8251c301fc5SJordan Crouse 
8261c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8271c301fc5SJordan Crouse 
8281c301fc5SJordan Crouse 	return count;
8291c301fc5SJordan Crouse }
8301c301fc5SJordan Crouse 
8311c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */
8321c301fc5SJordan Crouse 
8331c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index,
8341c301fc5SJordan Crouse 		      unsigned int pwmctl, unsigned int pwmchan)
8351c301fc5SJordan Crouse {
8361c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8371c301fc5SJordan Crouse 	long val = 0;
8381c301fc5SJordan Crouse 
8391c301fc5SJordan Crouse 	switch (pwmctl) {
8401c301fc5SJordan Crouse 	case 0:
8411c301fc5SJordan Crouse 		val = 0x03;	/* Run at full speed */
8421c301fc5SJordan Crouse 		break;
8431c301fc5SJordan Crouse 	case 1:
8441c301fc5SJordan Crouse 		val = 0x07;	/* Manual mode */
8451c301fc5SJordan Crouse 		break;
8461c301fc5SJordan Crouse 	case 2:
8471c301fc5SJordan Crouse 		switch (pwmchan) {
8481c301fc5SJordan Crouse 		case 1:
8491c301fc5SJordan Crouse 			/* Remote1 controls PWM */
8501c301fc5SJordan Crouse 			val = 0x00;
8511c301fc5SJordan Crouse 			break;
8521c301fc5SJordan Crouse 		case 2:
8531c301fc5SJordan Crouse 			/* local controls PWM */
8541c301fc5SJordan Crouse 			val = 0x01;
8551c301fc5SJordan Crouse 			break;
8561c301fc5SJordan Crouse 		case 4:
8571c301fc5SJordan Crouse 			/* remote2 controls PWM */
8581c301fc5SJordan Crouse 			val = 0x02;
8591c301fc5SJordan Crouse 			break;
8601c301fc5SJordan Crouse 		case 6:
8611c301fc5SJordan Crouse 			/* local/remote2 control PWM */
8621c301fc5SJordan Crouse 			val = 0x05;
8631c301fc5SJordan Crouse 			break;
8641c301fc5SJordan Crouse 		case 7:
8651c301fc5SJordan Crouse 			/* All three control PWM */
8661c301fc5SJordan Crouse 			val = 0x06;
8671c301fc5SJordan Crouse 			break;
8681c301fc5SJordan Crouse 		default:
8691c301fc5SJordan Crouse 			return -EINVAL;
8701c301fc5SJordan Crouse 		}
8711c301fc5SJordan Crouse 		break;
8721c301fc5SJordan Crouse 	default:
8731c301fc5SJordan Crouse 		return -EINVAL;
8741c301fc5SJordan Crouse 	}
8751c301fc5SJordan Crouse 
8761c301fc5SJordan Crouse 	data->pwmctl[index] = pwmctl;
8771c301fc5SJordan Crouse 	data->pwmchan[index] = pwmchan;
8781c301fc5SJordan Crouse 
8791c301fc5SJordan Crouse 	data->pwm[CONTROL][index] &= ~0xE0;
8801c301fc5SJordan Crouse 	data->pwm[CONTROL][index] |= (val & 7) << 5;
8811c301fc5SJordan Crouse 
8821c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
8831c301fc5SJordan Crouse 				  data->pwm[CONTROL][index]);
8841c301fc5SJordan Crouse 
8851c301fc5SJordan Crouse 	return 0;
8861c301fc5SJordan Crouse }
8871c301fc5SJordan Crouse 
8881c301fc5SJordan Crouse static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
8891c301fc5SJordan Crouse 			   const char *buf, size_t count)
8901c301fc5SJordan Crouse {
8911c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8921c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
8931c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8941c301fc5SJordan Crouse 	int r;
8951c301fc5SJordan Crouse 	long val;
8961c301fc5SJordan Crouse 
897179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
8981c301fc5SJordan Crouse 		return -EINVAL;
8991c301fc5SJordan Crouse 
9001c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9011c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9021c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9031c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
9041c301fc5SJordan Crouse 	if (r)
9051c301fc5SJordan Crouse 		count = r;
9061c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9071c301fc5SJordan Crouse 
9081c301fc5SJordan Crouse 	return count;
9091c301fc5SJordan Crouse }
9101c301fc5SJordan Crouse 
9111c301fc5SJordan Crouse static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
9121c301fc5SJordan Crouse 			   const char *buf, size_t count)
9131c301fc5SJordan Crouse {
9141c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9151c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9161c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9171c301fc5SJordan Crouse 	int r;
9181c301fc5SJordan Crouse 	long val;
9191c301fc5SJordan Crouse 
920179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9211c301fc5SJordan Crouse 		return -EINVAL;
9221c301fc5SJordan Crouse 
9231c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9241c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9251c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9261c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
9271c301fc5SJordan Crouse 	if (r)
9281c301fc5SJordan Crouse 		count = r;
9291c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9301c301fc5SJordan Crouse 
9311c301fc5SJordan Crouse 	return count;
9321c301fc5SJordan Crouse }
9331c301fc5SJordan Crouse 
9341c301fc5SJordan Crouse /* List of frequencies for the PWM */
9351c301fc5SJordan Crouse static const int pwmfreq_table[] = {
9363490c92aSChris Packham 	11, 14, 22, 29, 35, 44, 58, 88, 22500
9371c301fc5SJordan Crouse };
9381c301fc5SJordan Crouse 
9391c301fc5SJordan Crouse static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
9401c301fc5SJordan Crouse 			    char *buf)
9411c301fc5SJordan Crouse {
9421c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
9431c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9443490c92aSChris Packham 	int i = clamp_val(data->range[sattr->index] & 0xf, 0,
9453490c92aSChris Packham 			  ARRAY_SIZE(pwmfreq_table) - 1);
9461c301fc5SJordan Crouse 
9473490c92aSChris Packham 	return sprintf(buf, "%d\n", pwmfreq_table[i]);
9481c301fc5SJordan Crouse }
9491c301fc5SJordan Crouse 
9501c301fc5SJordan Crouse static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
9511c301fc5SJordan Crouse 			   const char *buf, size_t count)
9521c301fc5SJordan Crouse {
9531c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9541c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9551c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9561c301fc5SJordan Crouse 	int out;
9571c301fc5SJordan Crouse 	long val;
9581c301fc5SJordan Crouse 
959179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9601c301fc5SJordan Crouse 		return -EINVAL;
9611c301fc5SJordan Crouse 
962e4651640SChris Packham 	out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
9631c301fc5SJordan Crouse 
9641c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9651c301fc5SJordan Crouse 
9661c301fc5SJordan Crouse 	data->range[sattr->index] =
9671c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
9683490c92aSChris Packham 	data->range[sattr->index] &= ~0xf;
9691c301fc5SJordan Crouse 	data->range[sattr->index] |= out;
9701c301fc5SJordan Crouse 
9711c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
9721c301fc5SJordan Crouse 				  data->range[sattr->index]);
9731c301fc5SJordan Crouse 
9741c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9751c301fc5SJordan Crouse 	return count;
9761c301fc5SJordan Crouse }
9771c301fc5SJordan Crouse 
9781d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
9791d05303cSJulia Lawall 					struct device_attribute *devattr,
9801d05303cSJulia Lawall 					char *buf)
981f99318b2SJean Delvare {
982f99318b2SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
983f99318b2SJean Delvare 	return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
984f99318b2SJean Delvare }
985f99318b2SJean Delvare 
9861d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
987f99318b2SJean Delvare 					struct device_attribute *devattr,
988f99318b2SJean Delvare 					const char *buf, size_t count)
989f99318b2SJean Delvare {
990f99318b2SJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
991f99318b2SJean Delvare 	struct adt7475_data *data = i2c_get_clientdata(client);
992f99318b2SJean Delvare 	long val;
993f99318b2SJean Delvare 
994179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
995f99318b2SJean Delvare 		return -EINVAL;
996f99318b2SJean Delvare 	if (val != 0 && val != 1)
997f99318b2SJean Delvare 		return -EINVAL;
998f99318b2SJean Delvare 
999f99318b2SJean Delvare 	mutex_lock(&data->lock);
1000f99318b2SJean Delvare 	data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1001f99318b2SJean Delvare 	if (val)
1002f99318b2SJean Delvare 		data->config4 |= CONFIG4_MAXDUTY;
1003f99318b2SJean Delvare 	else
1004f99318b2SJean Delvare 		data->config4 &= ~CONFIG4_MAXDUTY;
1005f99318b2SJean Delvare 	i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1006f99318b2SJean Delvare 	mutex_unlock(&data->lock);
1007f99318b2SJean Delvare 
1008f99318b2SJean Delvare 	return count;
1009f99318b2SJean Delvare }
1010f99318b2SJean Delvare 
10111d05303cSJulia Lawall static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
101254fe4671SJean Delvare 			char *buf)
101354fe4671SJean Delvare {
101454fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
101554fe4671SJean Delvare 	return sprintf(buf, "%d\n", (int)data->vrm);
101654fe4671SJean Delvare }
101754fe4671SJean Delvare 
10181d05303cSJulia Lawall static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
101954fe4671SJean Delvare 			 const char *buf, size_t count)
102054fe4671SJean Delvare {
102154fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
102254fe4671SJean Delvare 	long val;
102354fe4671SJean Delvare 
1024179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
102554fe4671SJean Delvare 		return -EINVAL;
102654fe4671SJean Delvare 	if (val < 0 || val > 255)
102754fe4671SJean Delvare 		return -EINVAL;
102854fe4671SJean Delvare 	data->vrm = val;
102954fe4671SJean Delvare 
103054fe4671SJean Delvare 	return count;
103154fe4671SJean Delvare }
103254fe4671SJean Delvare 
10331d05303cSJulia Lawall static ssize_t cpu0_vid_show(struct device *dev,
10341d05303cSJulia Lawall 			     struct device_attribute *devattr, char *buf)
103554fe4671SJean Delvare {
103654fe4671SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
103754fe4671SJean Delvare 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
103854fe4671SJean Delvare }
103954fe4671SJean Delvare 
10403d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
10413d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
10423d849981SJean Delvare 			    set_voltage, MAX, 0);
10433d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
10443d849981SJean Delvare 			    set_voltage, MIN, 0);
10453d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
1046cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
10471c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
10481c301fc5SJordan Crouse 			    set_voltage, MAX, 1);
1049cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
10501c301fc5SJordan Crouse 			    set_voltage, MIN, 1);
1051cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
1052cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
1053cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
1054cffb9dd0SJean Delvare 			    set_voltage, MAX, 2);
1055cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
1056cffb9dd0SJean Delvare 			    set_voltage, MIN, 2);
1057cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
10583d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
10593d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
10603d849981SJean Delvare 			    set_voltage, MAX, 3);
10613d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
10623d849981SJean Delvare 			    set_voltage, MIN, 3);
10633d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
10643d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
10653d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
10663d849981SJean Delvare 			    set_voltage, MAX, 4);
10673d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
10683d849981SJean Delvare 			    set_voltage, MIN, 4);
10693d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
10703d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
10713d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
10723d849981SJean Delvare 			    set_voltage, MAX, 5);
10733d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
10743d849981SJean Delvare 			    set_voltage, MIN, 5);
10753d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
10761c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
10771c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
10781c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
10791c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
10801c301fc5SJordan Crouse 			    MAX, 0);
10811c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
10821c301fc5SJordan Crouse 			    MIN, 0);
10831c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
10841c301fc5SJordan Crouse 			    set_temp, OFFSET, 0);
10851c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
10861c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 0);
10871c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
10881c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 0);
10891c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
10901c301fc5SJordan Crouse 			    THERM, 0);
10911c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
10921c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 0);
10938f05bcc3SChris Packham static SENSOR_DEVICE_ATTR_2(temp1_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
10948f05bcc3SChris Packham 			    set_temp_st, 0, 0);
10951c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
10961c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
10971c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
10981c301fc5SJordan Crouse 			    MAX, 1);
10991c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
11001c301fc5SJordan Crouse 			    MIN, 1);
11011c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
11021c301fc5SJordan Crouse 			    set_temp, OFFSET, 1);
11031c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
11041c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 1);
11051c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
11061c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 1);
11071c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
11081c301fc5SJordan Crouse 			    THERM, 1);
11091c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
11101c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 1);
11118f05bcc3SChris Packham static SENSOR_DEVICE_ATTR_2(temp2_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
11128f05bcc3SChris Packham 			    set_temp_st, 0, 1);
11131c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
11141c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
11151c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
11161c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
11171c301fc5SJordan Crouse 			    MAX, 2);
11181c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
11191c301fc5SJordan Crouse 			    MIN, 2);
11201c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
11211c301fc5SJordan Crouse 			    set_temp, OFFSET, 2);
11221c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
11231c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 2);
11241c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
11251c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 2);
11261c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
11271c301fc5SJordan Crouse 			    THERM, 2);
11281c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
11291c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 2);
11308f05bcc3SChris Packham static SENSOR_DEVICE_ATTR_2(temp3_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
11318f05bcc3SChris Packham 			    set_temp_st, 0, 2);
11321c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
11331c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11341c301fc5SJordan Crouse 			    MIN, 0);
11351c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
11361c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
11371c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11381c301fc5SJordan Crouse 			    MIN, 1);
11391c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
11401c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
11411c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11421c301fc5SJordan Crouse 			    MIN, 2);
11431c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
11441c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
11451c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11461c301fc5SJordan Crouse 			    MIN, 3);
11471c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
11481c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
11491c301fc5SJordan Crouse 			    0);
11501c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
11511c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 0);
11521c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
11531c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 0);
115484d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
11551c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 0);
11561c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
11571c301fc5SJordan Crouse 			    set_pwm, MIN, 0);
11581c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
11591c301fc5SJordan Crouse 			    set_pwm, MAX, 0);
11601d58f5efSChris Packham static SENSOR_DEVICE_ATTR_2(pwm1_stall_disable, S_IRUGO | S_IWUSR,
11611d58f5efSChris Packham 			    show_stall_disable, set_stall_disable, 0, 0);
11621c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
11631c301fc5SJordan Crouse 			    1);
11641c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
11651c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 1);
11661c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
11671c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 1);
116884d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
11691c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 1);
11701c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
11711c301fc5SJordan Crouse 			    set_pwm, MIN, 1);
11721c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
11731c301fc5SJordan Crouse 			    set_pwm, MAX, 1);
11741d58f5efSChris Packham static SENSOR_DEVICE_ATTR_2(pwm2_stall_disable, S_IRUGO | S_IWUSR,
11751d58f5efSChris Packham 			    show_stall_disable, set_stall_disable, 0, 1);
11761c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
11771c301fc5SJordan Crouse 			    2);
11781c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
11791c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 2);
11801c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
11811c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 2);
118284d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
11831c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 2);
11841c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
11851c301fc5SJordan Crouse 			    set_pwm, MIN, 2);
11861c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
11871c301fc5SJordan Crouse 			    set_pwm, MAX, 2);
11881d58f5efSChris Packham static SENSOR_DEVICE_ATTR_2(pwm3_stall_disable, S_IRUGO | S_IWUSR,
11891d58f5efSChris Packham 			    show_stall_disable, set_stall_disable, 0, 2);
11901c301fc5SJordan Crouse 
1191f99318b2SJean Delvare /* Non-standard name, might need revisiting */
11921d05303cSJulia Lawall static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1193f99318b2SJean Delvare 
11941d05303cSJulia Lawall static DEVICE_ATTR_RW(vrm);
11951d05303cSJulia Lawall static DEVICE_ATTR_RO(cpu0_vid);
119654fe4671SJean Delvare 
11971c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = {
11981c301fc5SJordan Crouse 	&sensor_dev_attr_in1_input.dev_attr.attr,
11991c301fc5SJordan Crouse 	&sensor_dev_attr_in1_max.dev_attr.attr,
12001c301fc5SJordan Crouse 	&sensor_dev_attr_in1_min.dev_attr.attr,
12011c301fc5SJordan Crouse 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
12021c301fc5SJordan Crouse 	&sensor_dev_attr_in2_input.dev_attr.attr,
12031c301fc5SJordan Crouse 	&sensor_dev_attr_in2_max.dev_attr.attr,
12041c301fc5SJordan Crouse 	&sensor_dev_attr_in2_min.dev_attr.attr,
12051c301fc5SJordan Crouse 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
12061c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_input.dev_attr.attr,
12071c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
12081c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
12091c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_max.dev_attr.attr,
12101c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_min.dev_attr.attr,
12111c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
12121c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
12131c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
12141c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
12151c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
12168f05bcc3SChris Packham 	&sensor_dev_attr_temp1_smoothing.dev_attr.attr,
12171c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_input.dev_attr.attr,
12181c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
12191c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_max.dev_attr.attr,
12201c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_min.dev_attr.attr,
12211c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
12221c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
12231c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
12241c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
12251c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
12268f05bcc3SChris Packham 	&sensor_dev_attr_temp2_smoothing.dev_attr.attr,
12271c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_input.dev_attr.attr,
12281c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
12291c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
12301c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_max.dev_attr.attr,
12311c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_min.dev_attr.attr,
12321c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
12331c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
12341c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
12351c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
12361c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
12378f05bcc3SChris Packham 	&sensor_dev_attr_temp3_smoothing.dev_attr.attr,
12381c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_input.dev_attr.attr,
12391c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_min.dev_attr.attr,
12401c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
12411c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_input.dev_attr.attr,
12421c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_min.dev_attr.attr,
12431c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
12441c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_input.dev_attr.attr,
12451c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_min.dev_attr.attr,
12461c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
12471c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1.dev_attr.attr,
12481c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
12491c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
125084d2a314SJean Delvare 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
12511c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
12521c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
12531d58f5efSChris Packham 	&sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
12541c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3.dev_attr.attr,
12551c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
12561c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
125784d2a314SJean Delvare 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
12581c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
12591c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
12601d58f5efSChris Packham 	&sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
1261f99318b2SJean Delvare 	&dev_attr_pwm_use_point2_pwm_at_crit.attr,
12621c301fc5SJordan Crouse 	NULL,
12631c301fc5SJordan Crouse };
12641c301fc5SJordan Crouse 
1265378933c9SJean Delvare static struct attribute *fan4_attrs[] = {
1266378933c9SJean Delvare 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1267378933c9SJean Delvare 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1268378933c9SJean Delvare 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1269378933c9SJean Delvare 	NULL
1270378933c9SJean Delvare };
1271378933c9SJean Delvare 
1272378933c9SJean Delvare static struct attribute *pwm2_attrs[] = {
1273378933c9SJean Delvare 	&sensor_dev_attr_pwm2.dev_attr.attr,
1274378933c9SJean Delvare 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1275378933c9SJean Delvare 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1276378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1277378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1278378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
12791d58f5efSChris Packham 	&sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
1280378933c9SJean Delvare 	NULL
1281378933c9SJean Delvare };
1282378933c9SJean Delvare 
1283378933c9SJean Delvare static struct attribute *in0_attrs[] = {
12843d849981SJean Delvare 	&sensor_dev_attr_in0_input.dev_attr.attr,
12853d849981SJean Delvare 	&sensor_dev_attr_in0_max.dev_attr.attr,
12863d849981SJean Delvare 	&sensor_dev_attr_in0_min.dev_attr.attr,
12873d849981SJean Delvare 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1288378933c9SJean Delvare 	NULL
1289378933c9SJean Delvare };
1290378933c9SJean Delvare 
1291d8d2ee07SJean Delvare static struct attribute *in3_attrs[] = {
12923d849981SJean Delvare 	&sensor_dev_attr_in3_input.dev_attr.attr,
12933d849981SJean Delvare 	&sensor_dev_attr_in3_max.dev_attr.attr,
12943d849981SJean Delvare 	&sensor_dev_attr_in3_min.dev_attr.attr,
12953d849981SJean Delvare 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1296d8d2ee07SJean Delvare 	NULL
1297d8d2ee07SJean Delvare };
1298d8d2ee07SJean Delvare 
1299d8d2ee07SJean Delvare static struct attribute *in4_attrs[] = {
13003d849981SJean Delvare 	&sensor_dev_attr_in4_input.dev_attr.attr,
13013d849981SJean Delvare 	&sensor_dev_attr_in4_max.dev_attr.attr,
13023d849981SJean Delvare 	&sensor_dev_attr_in4_min.dev_attr.attr,
13033d849981SJean Delvare 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1304d8d2ee07SJean Delvare 	NULL
1305d8d2ee07SJean Delvare };
1306d8d2ee07SJean Delvare 
1307d8d2ee07SJean Delvare static struct attribute *in5_attrs[] = {
13083d849981SJean Delvare 	&sensor_dev_attr_in5_input.dev_attr.attr,
13093d849981SJean Delvare 	&sensor_dev_attr_in5_max.dev_attr.attr,
13103d849981SJean Delvare 	&sensor_dev_attr_in5_min.dev_attr.attr,
13113d849981SJean Delvare 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
13123d849981SJean Delvare 	NULL
13133d849981SJean Delvare };
13143d849981SJean Delvare 
131554fe4671SJean Delvare static struct attribute *vid_attrs[] = {
131654fe4671SJean Delvare 	&dev_attr_cpu0_vid.attr,
131754fe4671SJean Delvare 	&dev_attr_vrm.attr,
131854fe4671SJean Delvare 	NULL
131954fe4671SJean Delvare };
132054fe4671SJean Delvare 
1321f5397be8SArvind Yadav static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1322f5397be8SArvind Yadav static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1323f5397be8SArvind Yadav static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1324f5397be8SArvind Yadav static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1325f5397be8SArvind Yadav static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1326f5397be8SArvind Yadav static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1327f5397be8SArvind Yadav static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1328f5397be8SArvind Yadav static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
13291c301fc5SJordan Crouse 
1330310ec792SJean Delvare static int adt7475_detect(struct i2c_client *client,
13311c301fc5SJordan Crouse 			  struct i2c_board_info *info)
13321c301fc5SJordan Crouse {
13331c301fc5SJordan Crouse 	struct i2c_adapter *adapter = client->adapter;
1334d656b6fdSJean Delvare 	int vendid, devid, devid2;
1335b180d050SJean Delvare 	const char *name;
13361c301fc5SJordan Crouse 
13371c301fc5SJordan Crouse 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
13381c301fc5SJordan Crouse 		return -ENODEV;
13391c301fc5SJordan Crouse 
1340b180d050SJean Delvare 	vendid = adt7475_read(REG_VENDID);
1341d656b6fdSJean Delvare 	devid2 = adt7475_read(REG_DEVID2);
1342d656b6fdSJean Delvare 	if (vendid != 0x41 ||		/* Analog Devices */
1343d656b6fdSJean Delvare 	    (devid2 & 0xf8) != 0x68)
1344d656b6fdSJean Delvare 		return -ENODEV;
1345b180d050SJean Delvare 
1346d656b6fdSJean Delvare 	devid = adt7475_read(REG_DEVID);
1347d656b6fdSJean Delvare 	if (devid == 0x73)
1348b180d050SJean Delvare 		name = "adt7473";
1349d656b6fdSJean Delvare 	else if (devid == 0x75 && client->addr == 0x2e)
1350b180d050SJean Delvare 		name = "adt7475";
1351d8d2ee07SJean Delvare 	else if (devid == 0x76)
1352d8d2ee07SJean Delvare 		name = "adt7476";
13533d849981SJean Delvare 	else if ((devid2 & 0xfc) == 0x6c)
13543d849981SJean Delvare 		name = "adt7490";
1355b180d050SJean Delvare 	else {
1356b180d050SJean Delvare 		dev_dbg(&adapter->dev,
1357d8d2ee07SJean Delvare 			"Couldn't detect an ADT7473/75/76/90 part at "
1358b180d050SJean Delvare 			"0x%02x\n", (unsigned int)client->addr);
13591c301fc5SJordan Crouse 		return -ENODEV;
13601c301fc5SJordan Crouse 	}
13611c301fc5SJordan Crouse 
1362b180d050SJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
13631c301fc5SJordan Crouse 
13641c301fc5SJordan Crouse 	return 0;
13651c301fc5SJordan Crouse }
13661c301fc5SJordan Crouse 
13670f14480bSJean Delvare static void adt7475_remove_files(struct i2c_client *client,
13680f14480bSJean Delvare 				 struct adt7475_data *data)
13690f14480bSJean Delvare {
13700f14480bSJean Delvare 	sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1371378933c9SJean Delvare 	if (data->has_fan4)
1372378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1373378933c9SJean Delvare 	if (data->has_pwm2)
1374378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1375378933c9SJean Delvare 	if (data->has_voltage & (1 << 0))
1376378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1377d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3))
1378d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1379d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4))
1380d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1381d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5))
1382d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
138354fe4671SJean Delvare 	if (data->has_vid)
138454fe4671SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
13850f14480bSJean Delvare }
13860f14480bSJean Delvare 
1387*702afeadSTokunori Ikegami static int adt7475_update_limits(struct i2c_client *client)
13885cf943edSTokunori Ikegami {
13895cf943edSTokunori Ikegami 	struct adt7475_data *data = i2c_get_clientdata(client);
13905cf943edSTokunori Ikegami 	int i;
1391*702afeadSTokunori Ikegami 	int ret;
13925cf943edSTokunori Ikegami 
1393*702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG4);
1394*702afeadSTokunori Ikegami 	if (ret < 0)
1395*702afeadSTokunori Ikegami 		return ret;
1396*702afeadSTokunori Ikegami 	data->config4 = ret;
1397*702afeadSTokunori Ikegami 
1398*702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG5);
1399*702afeadSTokunori Ikegami 	if (ret < 0)
1400*702afeadSTokunori Ikegami 		return ret;
1401*702afeadSTokunori Ikegami 	data->config5 = ret;
14025cf943edSTokunori Ikegami 
14035cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
14045cf943edSTokunori Ikegami 		if (!(data->has_voltage & (1 << i)))
14055cf943edSTokunori Ikegami 			continue;
14065cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1407*702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MIN_REG(i));
1408*702afeadSTokunori Ikegami 		if (ret < 0)
1409*702afeadSTokunori Ikegami 			return ret;
1410*702afeadSTokunori Ikegami 		data->voltage[MIN][i] = ret << 2;
1411*702afeadSTokunori Ikegami 
1412*702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MAX_REG(i));
1413*702afeadSTokunori Ikegami 		if (ret < 0)
1414*702afeadSTokunori Ikegami 			return ret;
1415*702afeadSTokunori Ikegami 		data->voltage[MAX][i] = ret << 2;
14165cf943edSTokunori Ikegami 	}
14175cf943edSTokunori Ikegami 
14185cf943edSTokunori Ikegami 	if (data->has_voltage & (1 << 5)) {
1419*702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MIN);
1420*702afeadSTokunori Ikegami 		if (ret < 0)
1421*702afeadSTokunori Ikegami 			return ret;
1422*702afeadSTokunori Ikegami 		data->voltage[MIN][5] = ret << 2;
1423*702afeadSTokunori Ikegami 
1424*702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MAX);
1425*702afeadSTokunori Ikegami 		if (ret < 0)
1426*702afeadSTokunori Ikegami 			return ret;
1427*702afeadSTokunori Ikegami 		data->voltage[MAX][5] = ret << 2;
14285cf943edSTokunori Ikegami 	}
14295cf943edSTokunori Ikegami 
14305cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
14315cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1432*702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MIN_REG(i));
1433*702afeadSTokunori Ikegami 		if (ret < 0)
1434*702afeadSTokunori Ikegami 			return ret;
1435*702afeadSTokunori Ikegami 		data->temp[MIN][i] = ret << 2;
1436*702afeadSTokunori Ikegami 
1437*702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MAX_REG(i));
1438*702afeadSTokunori Ikegami 		if (ret < 0)
1439*702afeadSTokunori Ikegami 			return ret;
1440*702afeadSTokunori Ikegami 		data->temp[MAX][i] = ret << 2;
1441*702afeadSTokunori Ikegami 
1442*702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_TMIN_REG(i));
1443*702afeadSTokunori Ikegami 		if (ret < 0)
1444*702afeadSTokunori Ikegami 			return ret;
1445*702afeadSTokunori Ikegami 		data->temp[AUTOMIN][i] = ret << 2;
1446*702afeadSTokunori Ikegami 
1447*702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_THERM_REG(i));
1448*702afeadSTokunori Ikegami 		if (ret < 0)
1449*702afeadSTokunori Ikegami 			return ret;
1450*702afeadSTokunori Ikegami 		data->temp[THERM][i] = ret << 2;
1451*702afeadSTokunori Ikegami 
1452*702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_OFFSET_REG(i));
1453*702afeadSTokunori Ikegami 		if (ret < 0)
1454*702afeadSTokunori Ikegami 			return ret;
1455*702afeadSTokunori Ikegami 		data->temp[OFFSET][i] = ret;
14565cf943edSTokunori Ikegami 	}
14575cf943edSTokunori Ikegami 	adt7475_read_hystersis(client);
14585cf943edSTokunori Ikegami 
14595cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
14605cf943edSTokunori Ikegami 		if (i == 3 && !data->has_fan4)
14615cf943edSTokunori Ikegami 			continue;
1462*702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_MIN_REG(i));
1463*702afeadSTokunori Ikegami 		if (ret < 0)
1464*702afeadSTokunori Ikegami 			return ret;
1465*702afeadSTokunori Ikegami 		data->tach[MIN][i] = ret;
14665cf943edSTokunori Ikegami 	}
14675cf943edSTokunori Ikegami 
14685cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
14695cf943edSTokunori Ikegami 		if (i == 1 && !data->has_pwm2)
14705cf943edSTokunori Ikegami 			continue;
1471*702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MAX_REG(i));
1472*702afeadSTokunori Ikegami 		if (ret < 0)
1473*702afeadSTokunori Ikegami 			return ret;
1474*702afeadSTokunori Ikegami 		data->pwm[MAX][i] = ret;
1475*702afeadSTokunori Ikegami 
1476*702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MIN_REG(i));
1477*702afeadSTokunori Ikegami 		if (ret < 0)
1478*702afeadSTokunori Ikegami 			return ret;
1479*702afeadSTokunori Ikegami 		data->pwm[MIN][i] = ret;
14805cf943edSTokunori Ikegami 		/* Set the channel and control information */
14815cf943edSTokunori Ikegami 		adt7475_read_pwm(client, i);
14825cf943edSTokunori Ikegami 	}
14835cf943edSTokunori Ikegami 
1484*702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(0));
1485*702afeadSTokunori Ikegami 	if (ret < 0)
1486*702afeadSTokunori Ikegami 		return ret;
1487*702afeadSTokunori Ikegami 	data->range[0] = ret;
1488*702afeadSTokunori Ikegami 
1489*702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(1));
1490*702afeadSTokunori Ikegami 	if (ret < 0)
1491*702afeadSTokunori Ikegami 		return ret;
1492*702afeadSTokunori Ikegami 	data->range[1] = ret;
1493*702afeadSTokunori Ikegami 
1494*702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(2));
1495*702afeadSTokunori Ikegami 	if (ret < 0)
1496*702afeadSTokunori Ikegami 		return ret;
1497*702afeadSTokunori Ikegami 	data->range[2] = ret;
1498*702afeadSTokunori Ikegami 
1499*702afeadSTokunori Ikegami 	return 0;
15005cf943edSTokunori Ikegami }
15015cf943edSTokunori Ikegami 
15021c301fc5SJordan Crouse static int adt7475_probe(struct i2c_client *client,
15031c301fc5SJordan Crouse 			 const struct i2c_device_id *id)
15041c301fc5SJordan Crouse {
15054e2496e4SJavier Martinez Canillas 	enum chips chip;
150699b8c83aSFrans Meulenbroeks 	static const char * const names[] = {
1507d07ca4adSJean Delvare 		[adt7473] = "ADT7473",
1508d07ca4adSJean Delvare 		[adt7475] = "ADT7475",
1509d8d2ee07SJean Delvare 		[adt7476] = "ADT7476",
1510d07ca4adSJean Delvare 		[adt7490] = "ADT7490",
1511d07ca4adSJean Delvare 	};
1512d07ca4adSJean Delvare 
15131c301fc5SJordan Crouse 	struct adt7475_data *data;
1514378933c9SJean Delvare 	int i, ret = 0, revision;
1515ebfaf1fbSJean Delvare 	u8 config2, config3;
15161c301fc5SJordan Crouse 
1517e3ecb2eeSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
15181c301fc5SJordan Crouse 	if (data == NULL)
15191c301fc5SJordan Crouse 		return -ENOMEM;
15201c301fc5SJordan Crouse 
15211c301fc5SJordan Crouse 	mutex_init(&data->lock);
15221c301fc5SJordan Crouse 	i2c_set_clientdata(client, data);
15231c301fc5SJordan Crouse 
15244e2496e4SJavier Martinez Canillas 	if (client->dev.of_node)
15254e2496e4SJavier Martinez Canillas 		chip = (enum chips)of_device_get_match_data(&client->dev);
15264e2496e4SJavier Martinez Canillas 	else
15274e2496e4SJavier Martinez Canillas 		chip = id->driver_data;
15284e2496e4SJavier Martinez Canillas 
1529cffb9dd0SJean Delvare 	/* Initialize device-specific values */
15304e2496e4SJavier Martinez Canillas 	switch (chip) {
1531d8d2ee07SJean Delvare 	case adt7476:
1532d8d2ee07SJean Delvare 		data->has_voltage = 0x0e;	/* in1 to in3 */
1533d8d2ee07SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1534d8d2ee07SJean Delvare 		break;
15353d849981SJean Delvare 	case adt7490:
1536378933c9SJean Delvare 		data->has_voltage = 0x3e;	/* in1 to in5 */
1537378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x03;
1538d07ca4adSJean Delvare 		if (revision == 0x03)
1539d07ca4adSJean Delvare 			revision += adt7475_read(REG_DEVREV2);
15403d849981SJean Delvare 		break;
1541cffb9dd0SJean Delvare 	default:
1542cffb9dd0SJean Delvare 		data->has_voltage = 0x06;	/* in1, in2 */
1543378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1544378933c9SJean Delvare 	}
1545378933c9SJean Delvare 
1546378933c9SJean Delvare 	config3 = adt7475_read(REG_CONFIG3);
1547378933c9SJean Delvare 	/* Pin PWM2 may alternatively be used for ALERT output */
1548378933c9SJean Delvare 	if (!(config3 & CONFIG3_SMBALERT))
1549378933c9SJean Delvare 		data->has_pwm2 = 1;
1550378933c9SJean Delvare 	/* Meaning of this bit is inverted for the ADT7473-1 */
1551378933c9SJean Delvare 	if (id->driver_data == adt7473 && revision >= 1)
1552378933c9SJean Delvare 		data->has_pwm2 = !data->has_pwm2;
1553378933c9SJean Delvare 
1554378933c9SJean Delvare 	data->config4 = adt7475_read(REG_CONFIG4);
1555378933c9SJean Delvare 	/* Pin TACH4 may alternatively be used for THERM */
1556378933c9SJean Delvare 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1557378933c9SJean Delvare 		data->has_fan4 = 1;
1558378933c9SJean Delvare 
15599ed5bc24SGuenter Roeck 	/*
15609ed5bc24SGuenter Roeck 	 * THERM configuration is more complex on the ADT7476 and ADT7490,
15619ed5bc24SGuenter Roeck 	 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
15629ed5bc24SGuenter Roeck 	 * this function
15639ed5bc24SGuenter Roeck 	 */
1564378933c9SJean Delvare 	if (id->driver_data == adt7490) {
1565378933c9SJean Delvare 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1566378933c9SJean Delvare 		    !(config3 & CONFIG3_THERM))
1567378933c9SJean Delvare 			data->has_fan4 = 1;
1568d8d2ee07SJean Delvare 	}
1569d8d2ee07SJean Delvare 	if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1570378933c9SJean Delvare 		if (!(config3 & CONFIG3_THERM) ||
1571378933c9SJean Delvare 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1572378933c9SJean Delvare 			data->has_voltage |= (1 << 0);		/* in0 */
1573cffb9dd0SJean Delvare 	}
1574cffb9dd0SJean Delvare 
15759ed5bc24SGuenter Roeck 	/*
15769ed5bc24SGuenter Roeck 	 * On the ADT7476, the +12V input pin may instead be used as VID5,
15779ed5bc24SGuenter Roeck 	 * and VID pins may alternatively be used as GPIO
15789ed5bc24SGuenter Roeck 	 */
1579d8d2ee07SJean Delvare 	if (id->driver_data == adt7476) {
1580d8d2ee07SJean Delvare 		u8 vid = adt7475_read(REG_VID);
1581d8d2ee07SJean Delvare 		if (!(vid & VID_VIDSEL))
1582d8d2ee07SJean Delvare 			data->has_voltage |= (1 << 4);		/* in4 */
158354fe4671SJean Delvare 
158454fe4671SJean Delvare 		data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1585d8d2ee07SJean Delvare 	}
1586d8d2ee07SJean Delvare 
1587ebfaf1fbSJean Delvare 	/* Voltage attenuators can be bypassed, globally or individually */
1588ebfaf1fbSJean Delvare 	config2 = adt7475_read(REG_CONFIG2);
1589ebfaf1fbSJean Delvare 	if (config2 & CONFIG2_ATTN) {
1590ebfaf1fbSJean Delvare 		data->bypass_attn = (0x3 << 3) | 0x3;
1591ebfaf1fbSJean Delvare 	} else {
1592ebfaf1fbSJean Delvare 		data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1593ebfaf1fbSJean Delvare 				    ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1594ebfaf1fbSJean Delvare 	}
1595ebfaf1fbSJean Delvare 	data->bypass_attn &= data->has_voltage;
1596ebfaf1fbSJean Delvare 
15979ed5bc24SGuenter Roeck 	/*
15989ed5bc24SGuenter Roeck 	 * Call adt7475_read_pwm for all pwm's as this will reprogram any
15999ed5bc24SGuenter Roeck 	 * pwm's which are disabled to manual mode with 0% duty cycle
16009ed5bc24SGuenter Roeck 	 */
16011c301fc5SJordan Crouse 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
16021c301fc5SJordan Crouse 		adt7475_read_pwm(client, i);
16031c301fc5SJordan Crouse 
16044abdf38dSChris Packham 	/* Start monitoring */
16054abdf38dSChris Packham 	switch (chip) {
16064abdf38dSChris Packham 	case adt7475:
16074abdf38dSChris Packham 	case adt7476:
16084abdf38dSChris Packham 		i2c_smbus_write_byte_data(client, REG_CONFIG1,
16094abdf38dSChris Packham 					  adt7475_read(REG_CONFIG1) | 0x01);
16104abdf38dSChris Packham 		break;
16114abdf38dSChris Packham 	default:
16124abdf38dSChris Packham 		break;
16134abdf38dSChris Packham 	}
16144abdf38dSChris Packham 
16151c301fc5SJordan Crouse 	ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
16161c301fc5SJordan Crouse 	if (ret)
1617e3ecb2eeSGuenter Roeck 		return ret;
16181c301fc5SJordan Crouse 
1619378933c9SJean Delvare 	/* Features that can be disabled individually */
1620378933c9SJean Delvare 	if (data->has_fan4) {
1621378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1622378933c9SJean Delvare 		if (ret)
1623378933c9SJean Delvare 			goto eremove;
1624378933c9SJean Delvare 	}
1625378933c9SJean Delvare 	if (data->has_pwm2) {
1626378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1627378933c9SJean Delvare 		if (ret)
1628378933c9SJean Delvare 			goto eremove;
1629378933c9SJean Delvare 	}
1630378933c9SJean Delvare 	if (data->has_voltage & (1 << 0)) {
1631378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1632378933c9SJean Delvare 		if (ret)
1633378933c9SJean Delvare 			goto eremove;
1634378933c9SJean Delvare 	}
1635d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3)) {
1636d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1637d8d2ee07SJean Delvare 		if (ret)
1638d8d2ee07SJean Delvare 			goto eremove;
1639d8d2ee07SJean Delvare 	}
1640d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4)) {
1641d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1642d8d2ee07SJean Delvare 		if (ret)
1643d8d2ee07SJean Delvare 			goto eremove;
1644d8d2ee07SJean Delvare 	}
1645d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1646d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1647d8d2ee07SJean Delvare 		if (ret)
1648d8d2ee07SJean Delvare 			goto eremove;
1649d8d2ee07SJean Delvare 	}
165054fe4671SJean Delvare 	if (data->has_vid) {
165154fe4671SJean Delvare 		data->vrm = vid_which_vrm();
165254fe4671SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
165354fe4671SJean Delvare 		if (ret)
165454fe4671SJean Delvare 			goto eremove;
165554fe4671SJean Delvare 	}
1656378933c9SJean Delvare 
16571c301fc5SJordan Crouse 	data->hwmon_dev = hwmon_device_register(&client->dev);
16581c301fc5SJordan Crouse 	if (IS_ERR(data->hwmon_dev)) {
16591c301fc5SJordan Crouse 		ret = PTR_ERR(data->hwmon_dev);
16601c301fc5SJordan Crouse 		goto eremove;
16611c301fc5SJordan Crouse 	}
16621c301fc5SJordan Crouse 
1663d07ca4adSJean Delvare 	dev_info(&client->dev, "%s device, revision %d\n",
1664d07ca4adSJean Delvare 		 names[id->driver_data], revision);
1665d8d2ee07SJean Delvare 	if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
166654fe4671SJean Delvare 		dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1667d07ca4adSJean Delvare 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1668d8d2ee07SJean Delvare 			 (data->has_voltage & (1 << 4)) ? " in4" : "",
1669d07ca4adSJean Delvare 			 data->has_fan4 ? " fan4" : "",
167054fe4671SJean Delvare 			 data->has_pwm2 ? " pwm2" : "",
167154fe4671SJean Delvare 			 data->has_vid ? " vid" : "");
1672ebfaf1fbSJean Delvare 	if (data->bypass_attn)
1673ebfaf1fbSJean Delvare 		dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1674ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1675ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1676ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1677ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1678d07ca4adSJean Delvare 
16795cf943edSTokunori Ikegami 	/* Limits and settings, should never change update more than once */
16805cf943edSTokunori Ikegami 	adt7475_update_limits(client);
16815cf943edSTokunori Ikegami 
16821c301fc5SJordan Crouse 	return 0;
16831c301fc5SJordan Crouse 
16841c301fc5SJordan Crouse eremove:
16850f14480bSJean Delvare 	adt7475_remove_files(client, data);
16861c301fc5SJordan Crouse 	return ret;
16871c301fc5SJordan Crouse }
16881c301fc5SJordan Crouse 
16891c301fc5SJordan Crouse static int adt7475_remove(struct i2c_client *client)
16901c301fc5SJordan Crouse {
16911c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
16921c301fc5SJordan Crouse 
16931c301fc5SJordan Crouse 	hwmon_device_unregister(data->hwmon_dev);
16940f14480bSJean Delvare 	adt7475_remove_files(client, data);
16951c301fc5SJordan Crouse 
16961c301fc5SJordan Crouse 	return 0;
16971c301fc5SJordan Crouse }
16981c301fc5SJordan Crouse 
16991c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = {
17001c301fc5SJordan Crouse 	.class		= I2C_CLASS_HWMON,
17011c301fc5SJordan Crouse 	.driver = {
17021c301fc5SJordan Crouse 		.name	= "adt7475",
17034e2496e4SJavier Martinez Canillas 		.of_match_table = of_match_ptr(adt7475_of_match),
17041c301fc5SJordan Crouse 	},
17051c301fc5SJordan Crouse 	.probe		= adt7475_probe,
17061c301fc5SJordan Crouse 	.remove		= adt7475_remove,
17071c301fc5SJordan Crouse 	.id_table	= adt7475_id,
17081c301fc5SJordan Crouse 	.detect		= adt7475_detect,
1709c3813d6aSJean Delvare 	.address_list	= normal_i2c,
17101c301fc5SJordan Crouse };
17111c301fc5SJordan Crouse 
17121c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client)
17131c301fc5SJordan Crouse {
17141c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17151c301fc5SJordan Crouse 
17161c301fc5SJordan Crouse 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
17171c301fc5SJordan Crouse 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
17181c301fc5SJordan Crouse 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
17191c301fc5SJordan Crouse }
17201c301fc5SJordan Crouse 
17211c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index)
17221c301fc5SJordan Crouse {
17231c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17241c301fc5SJordan Crouse 	unsigned int v;
17251c301fc5SJordan Crouse 
17261c301fc5SJordan Crouse 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
17271c301fc5SJordan Crouse 
17289ed5bc24SGuenter Roeck 	/*
17299ed5bc24SGuenter Roeck 	 * Figure out the internal value for pwmctrl and pwmchan
17309ed5bc24SGuenter Roeck 	 * based on the current settings
17319ed5bc24SGuenter Roeck 	 */
17321c301fc5SJordan Crouse 	v = (data->pwm[CONTROL][index] >> 5) & 7;
17331c301fc5SJordan Crouse 
17341c301fc5SJordan Crouse 	if (v == 3)
17351c301fc5SJordan Crouse 		data->pwmctl[index] = 0;
17361c301fc5SJordan Crouse 	else if (v == 7)
17371c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
17381c301fc5SJordan Crouse 	else if (v == 4) {
17399ed5bc24SGuenter Roeck 		/*
17409ed5bc24SGuenter Roeck 		 * The fan is disabled - we don't want to
17419ed5bc24SGuenter Roeck 		 * support that, so change to manual mode and
17429ed5bc24SGuenter Roeck 		 * set the duty cycle to 0 instead
17431c301fc5SJordan Crouse 		 */
17441c301fc5SJordan Crouse 		data->pwm[INPUT][index] = 0;
17451c301fc5SJordan Crouse 		data->pwm[CONTROL][index] &= ~0xE0;
17461c301fc5SJordan Crouse 		data->pwm[CONTROL][index] |= (7 << 5);
17471c301fc5SJordan Crouse 
17481c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
17491c301fc5SJordan Crouse 					  data->pwm[INPUT][index]);
17501c301fc5SJordan Crouse 
17511c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
17521c301fc5SJordan Crouse 					  data->pwm[CONTROL][index]);
17531c301fc5SJordan Crouse 
17541c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
17551c301fc5SJordan Crouse 	} else {
17561c301fc5SJordan Crouse 		data->pwmctl[index] = 2;
17571c301fc5SJordan Crouse 
17581c301fc5SJordan Crouse 		switch (v) {
17591c301fc5SJordan Crouse 		case 0:
17601c301fc5SJordan Crouse 			data->pwmchan[index] = 1;
17611c301fc5SJordan Crouse 			break;
17621c301fc5SJordan Crouse 		case 1:
17631c301fc5SJordan Crouse 			data->pwmchan[index] = 2;
17641c301fc5SJordan Crouse 			break;
17651c301fc5SJordan Crouse 		case 2:
17661c301fc5SJordan Crouse 			data->pwmchan[index] = 4;
17671c301fc5SJordan Crouse 			break;
17681c301fc5SJordan Crouse 		case 5:
17691c301fc5SJordan Crouse 			data->pwmchan[index] = 6;
17701c301fc5SJordan Crouse 			break;
17711c301fc5SJordan Crouse 		case 6:
17721c301fc5SJordan Crouse 			data->pwmchan[index] = 7;
17731c301fc5SJordan Crouse 			break;
17741c301fc5SJordan Crouse 		}
17751c301fc5SJordan Crouse 	}
17761c301fc5SJordan Crouse }
17771c301fc5SJordan Crouse 
1778*702afeadSTokunori Ikegami static int adt7475_update_measure(struct device *dev)
17791c301fc5SJordan Crouse {
17801c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
17811c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17823d849981SJean Delvare 	u16 ext;
17831c301fc5SJordan Crouse 	int i;
1784*702afeadSTokunori Ikegami 	int ret;
17851c301fc5SJordan Crouse 
1786*702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS2);
1787*702afeadSTokunori Ikegami 	if (ret < 0)
1788*702afeadSTokunori Ikegami 		return ret;
1789*702afeadSTokunori Ikegami 	data->alarms = ret << 8;
17901c301fc5SJordan Crouse 
1791*702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS1);
1792*702afeadSTokunori Ikegami 	if (ret < 0)
1793*702afeadSTokunori Ikegami 		return ret;
1794*702afeadSTokunori Ikegami 	data->alarms |= ret;
1795*702afeadSTokunori Ikegami 
1796*702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND2);
1797*702afeadSTokunori Ikegami 	if (ret < 0)
1798*702afeadSTokunori Ikegami 		return ret;
1799*702afeadSTokunori Ikegami 
1800*702afeadSTokunori Ikegami 	ext = (ret << 8);
1801*702afeadSTokunori Ikegami 
1802*702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND1);
1803*702afeadSTokunori Ikegami 	if (ret < 0)
1804*702afeadSTokunori Ikegami 		return ret;
1805*702afeadSTokunori Ikegami 
1806*702afeadSTokunori Ikegami 	ext |= ret;
1807*702afeadSTokunori Ikegami 
1808cffb9dd0SJean Delvare 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1809cffb9dd0SJean Delvare 		if (!(data->has_voltage & (1 << i)))
1810cffb9dd0SJean Delvare 			continue;
1811*702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_REG(i));
1812*702afeadSTokunori Ikegami 		if (ret < 0)
1813*702afeadSTokunori Ikegami 			return ret;
18141c301fc5SJordan Crouse 		data->voltage[INPUT][i] =
1815*702afeadSTokunori Ikegami 			(ret << 2) |
1816cffb9dd0SJean Delvare 			((ext >> (i * 2)) & 3);
1817cffb9dd0SJean Delvare 	}
18181c301fc5SJordan Crouse 
1819*702afeadSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1820*702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_REG(i));
1821*702afeadSTokunori Ikegami 		if (ret < 0)
1822*702afeadSTokunori Ikegami 			return ret;
18231c301fc5SJordan Crouse 		data->temp[INPUT][i] =
1824*702afeadSTokunori Ikegami 			(ret << 2) |
18253d849981SJean Delvare 			((ext >> ((i + 5) * 2)) & 3);
1826*702afeadSTokunori Ikegami 	}
18273d849981SJean Delvare 
18283d849981SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1829*702afeadSTokunori Ikegami 		ret = adt7475_read(REG_STATUS4);
1830*702afeadSTokunori Ikegami 		if (ret < 0)
1831*702afeadSTokunori Ikegami 			return ret;
1832*702afeadSTokunori Ikegami 		data->alarms |= ret << 24;
1833*702afeadSTokunori Ikegami 
1834*702afeadSTokunori Ikegami 		ret = adt7475_read(REG_EXTEND3);
1835*702afeadSTokunori Ikegami 		if (ret < 0)
1836*702afeadSTokunori Ikegami 			return ret;
1837*702afeadSTokunori Ikegami 		ext = ret;
1838*702afeadSTokunori Ikegami 
1839*702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT);
1840*702afeadSTokunori Ikegami 		if (ret < 0)
1841*702afeadSTokunori Ikegami 			return ret;
1842*702afeadSTokunori Ikegami 		data->voltage[INPUT][5] = ret << 2 |
18433d849981SJean Delvare 			((ext >> 4) & 3);
18443d849981SJean Delvare 	}
18451c301fc5SJordan Crouse 
1846378933c9SJean Delvare 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1847378933c9SJean Delvare 		if (i == 3 && !data->has_fan4)
1848378933c9SJean Delvare 			continue;
1849*702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_REG(i));
1850*702afeadSTokunori Ikegami 		if (ret < 0)
1851*702afeadSTokunori Ikegami 			return ret;
1852*702afeadSTokunori Ikegami 		data->tach[INPUT][i] = ret;
1853378933c9SJean Delvare 	}
18541c301fc5SJordan Crouse 
18551c301fc5SJordan Crouse 	/* Updated by hw when in auto mode */
1856378933c9SJean Delvare 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1857378933c9SJean Delvare 		if (i == 1 && !data->has_pwm2)
1858378933c9SJean Delvare 			continue;
1859*702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_REG(i));
1860*702afeadSTokunori Ikegami 		if (ret < 0)
1861*702afeadSTokunori Ikegami 			return ret;
1862*702afeadSTokunori Ikegami 		data->pwm[INPUT][i] = ret;
1863378933c9SJean Delvare 	}
18641c301fc5SJordan Crouse 
1865*702afeadSTokunori Ikegami 	if (data->has_vid) {
1866*702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VID);
1867*702afeadSTokunori Ikegami 		if (ret < 0)
1868*702afeadSTokunori Ikegami 			return ret;
1869*702afeadSTokunori Ikegami 		data->vid = ret & 0x3f;
1870*702afeadSTokunori Ikegami 	}
1871*702afeadSTokunori Ikegami 
1872*702afeadSTokunori Ikegami 	return 0;
18731c301fc5SJordan Crouse }
18741c301fc5SJordan Crouse 
18755cf943edSTokunori Ikegami static struct adt7475_data *adt7475_update_device(struct device *dev)
18765cf943edSTokunori Ikegami {
18775cf943edSTokunori Ikegami 	struct i2c_client *client = to_i2c_client(dev);
18785cf943edSTokunori Ikegami 	struct adt7475_data *data = i2c_get_clientdata(client);
18795cf943edSTokunori Ikegami 
18805cf943edSTokunori Ikegami 	mutex_lock(&data->lock);
18815cf943edSTokunori Ikegami 
18825cf943edSTokunori Ikegami 	/* Measurement values update every 2 seconds */
18835cf943edSTokunori Ikegami 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
18841c301fc5SJordan Crouse 	    !data->valid) {
18855cf943edSTokunori Ikegami 		adt7475_update_measure(dev);
18865cf943edSTokunori Ikegami 		data->measure_updated = jiffies;
1887b36fb171STokunori Ikegami 		data->valid = true;
18881c301fc5SJordan Crouse 	}
18891c301fc5SJordan Crouse 
18901c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
18911c301fc5SJordan Crouse 
18921c301fc5SJordan Crouse 	return data;
18931c301fc5SJordan Crouse }
18941c301fc5SJordan Crouse 
1895f0967eeaSAxel Lin module_i2c_driver(adt7475_driver);
18961c301fc5SJordan Crouse 
18971c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc");
18981c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver");
18991c301fc5SJordan Crouse MODULE_LICENSE("GPL");
1900