xref: /linux/drivers/hwmon/adt7475.c (revision 9d19371df50a73301aec66a479b490587e889055)
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 
3284afec79fSTokunori Ikegami 	if (IS_ERR(data))
3294afec79fSTokunori Ikegami 		return PTR_ERR(data);
3304afec79fSTokunori Ikegami 
3311c301fc5SJordan Crouse 	switch (sattr->nr) {
3321c301fc5SJordan Crouse 	case ALARM:
3331c301fc5SJordan Crouse 		return sprintf(buf, "%d\n",
334cffb9dd0SJean Delvare 			       (data->alarms >> sattr->index) & 1);
3351c301fc5SJordan Crouse 	default:
3361c301fc5SJordan Crouse 		val = data->voltage[sattr->nr][sattr->index];
337ebfaf1fbSJean Delvare 		return sprintf(buf, "%d\n",
338ebfaf1fbSJean Delvare 			       reg2volt(sattr->index, val, data->bypass_attn));
3391c301fc5SJordan Crouse 	}
3401c301fc5SJordan Crouse }
3411c301fc5SJordan Crouse 
3421c301fc5SJordan Crouse static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
3431c301fc5SJordan Crouse 			   const char *buf, size_t count)
3441c301fc5SJordan Crouse {
3451c301fc5SJordan Crouse 
3461c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3471c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
3481c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
3491c301fc5SJordan Crouse 	unsigned char reg;
3501c301fc5SJordan Crouse 	long val;
3511c301fc5SJordan Crouse 
352179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
3531c301fc5SJordan Crouse 		return -EINVAL;
3541c301fc5SJordan Crouse 
3551c301fc5SJordan Crouse 	mutex_lock(&data->lock);
3561c301fc5SJordan Crouse 
357ebfaf1fbSJean Delvare 	data->voltage[sattr->nr][sattr->index] =
358ebfaf1fbSJean Delvare 				volt2reg(sattr->index, val, data->bypass_attn);
3591c301fc5SJordan Crouse 
3603d849981SJean Delvare 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
3611c301fc5SJordan Crouse 		if (sattr->nr == MIN)
3621c301fc5SJordan Crouse 			reg = VOLTAGE_MIN_REG(sattr->index);
3631c301fc5SJordan Crouse 		else
3641c301fc5SJordan Crouse 			reg = VOLTAGE_MAX_REG(sattr->index);
3653d849981SJean Delvare 	} else {
3663d849981SJean Delvare 		if (sattr->nr == MIN)
3673d849981SJean Delvare 			reg = REG_VTT_MIN;
3683d849981SJean Delvare 		else
3693d849981SJean Delvare 			reg = REG_VTT_MAX;
3703d849981SJean Delvare 	}
3711c301fc5SJordan Crouse 
3721c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
3731c301fc5SJordan Crouse 				  data->voltage[sattr->nr][sattr->index] >> 2);
3741c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
3751c301fc5SJordan Crouse 
3761c301fc5SJordan Crouse 	return count;
3771c301fc5SJordan Crouse }
3781c301fc5SJordan Crouse 
3791c301fc5SJordan Crouse static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
3801c301fc5SJordan Crouse 			 char *buf)
3811c301fc5SJordan Crouse {
3821c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3831c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3841c301fc5SJordan Crouse 	int out;
3851c301fc5SJordan Crouse 
3864afec79fSTokunori Ikegami 	if (IS_ERR(data))
3874afec79fSTokunori Ikegami 		return PTR_ERR(data);
3884afec79fSTokunori Ikegami 
3891c301fc5SJordan Crouse 	switch (sattr->nr) {
3901c301fc5SJordan Crouse 	case HYSTERSIS:
3911c301fc5SJordan Crouse 		mutex_lock(&data->lock);
3921c301fc5SJordan Crouse 		out = data->temp[sattr->nr][sattr->index];
3931c301fc5SJordan Crouse 		if (sattr->index != 1)
3941c301fc5SJordan Crouse 			out = (out >> 4) & 0xF;
3951c301fc5SJordan Crouse 		else
3961c301fc5SJordan Crouse 			out = (out & 0xF);
3979ed5bc24SGuenter Roeck 		/*
3989ed5bc24SGuenter Roeck 		 * Show the value as an absolute number tied to
3999ed5bc24SGuenter Roeck 		 * THERM
4009ed5bc24SGuenter Roeck 		 */
4011c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
4021c301fc5SJordan Crouse 			out * 1000;
4031c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4041c301fc5SJordan Crouse 		break;
4051c301fc5SJordan Crouse 
4061c301fc5SJordan Crouse 	case OFFSET:
4079ed5bc24SGuenter Roeck 		/*
4089ed5bc24SGuenter Roeck 		 * Offset is always 2's complement, regardless of the
4099ed5bc24SGuenter Roeck 		 * setting in CONFIG5
4109ed5bc24SGuenter Roeck 		 */
4111c301fc5SJordan Crouse 		mutex_lock(&data->lock);
4121c301fc5SJordan Crouse 		out = (s8)data->temp[sattr->nr][sattr->index];
4131c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET)
4141c301fc5SJordan Crouse 			out *= 1000;
4151c301fc5SJordan Crouse 		else
4161c301fc5SJordan Crouse 			out *= 500;
4171c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4181c301fc5SJordan Crouse 		break;
4191c301fc5SJordan Crouse 
4201c301fc5SJordan Crouse 	case ALARM:
4211c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 4)) & 1;
4221c301fc5SJordan Crouse 		break;
4231c301fc5SJordan Crouse 
4241c301fc5SJordan Crouse 	case FAULT:
4251c301fc5SJordan Crouse 		/* Note - only for remote1 and remote2 */
426cf312e07SJean Delvare 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
4271c301fc5SJordan Crouse 		break;
4281c301fc5SJordan Crouse 
4291c301fc5SJordan Crouse 	default:
4301c301fc5SJordan Crouse 		/* All other temp values are in the configured format */
4311c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
4321c301fc5SJordan Crouse 	}
4331c301fc5SJordan Crouse 
4341c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
4351c301fc5SJordan Crouse }
4361c301fc5SJordan Crouse 
4371c301fc5SJordan Crouse static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
4381c301fc5SJordan Crouse 			const char *buf, size_t count)
4391c301fc5SJordan Crouse {
4401c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4411c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
4421c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
4431c301fc5SJordan Crouse 	unsigned char reg = 0;
4441c301fc5SJordan Crouse 	u8 out;
4451c301fc5SJordan Crouse 	int temp;
4461c301fc5SJordan Crouse 	long val;
4471c301fc5SJordan Crouse 
448179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
4491c301fc5SJordan Crouse 		return -EINVAL;
4501c301fc5SJordan Crouse 
4511c301fc5SJordan Crouse 	mutex_lock(&data->lock);
4521c301fc5SJordan Crouse 
4531c301fc5SJordan Crouse 	/* We need the config register in all cases for temp <-> reg conv. */
4541c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
4551c301fc5SJordan Crouse 
4561c301fc5SJordan Crouse 	switch (sattr->nr) {
4571c301fc5SJordan Crouse 	case OFFSET:
4581c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET) {
4592a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 127000);
4601c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 1000;
4611c301fc5SJordan Crouse 		} else {
4622a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 64000);
4631c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 500;
4641c301fc5SJordan Crouse 		}
4651c301fc5SJordan Crouse 		break;
4661c301fc5SJordan Crouse 
4671c301fc5SJordan Crouse 	case HYSTERSIS:
4689ed5bc24SGuenter Roeck 		/*
4699ed5bc24SGuenter Roeck 		 * The value will be given as an absolute value, turn it
4709ed5bc24SGuenter Roeck 		 * into an offset based on THERM
4719ed5bc24SGuenter Roeck 		 */
4721c301fc5SJordan Crouse 
4731c301fc5SJordan Crouse 		/* Read fresh THERM and HYSTERSIS values from the chip */
4741c301fc5SJordan Crouse 		data->temp[THERM][sattr->index] =
4751c301fc5SJordan Crouse 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
4761c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
4771c301fc5SJordan Crouse 
4781c301fc5SJordan Crouse 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
4792a844c14SGuenter Roeck 		val = clamp_val(val, temp - 15000, temp);
4801c301fc5SJordan Crouse 		val = (temp - val) / 1000;
4811c301fc5SJordan Crouse 
4821c301fc5SJordan Crouse 		if (sattr->index != 1) {
4831c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
4841c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
4851c301fc5SJordan Crouse 		} else {
4861c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
4871c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
4881c301fc5SJordan Crouse 		}
4891c301fc5SJordan Crouse 
4901c301fc5SJordan Crouse 		out = data->temp[HYSTERSIS][sattr->index];
4911c301fc5SJordan Crouse 		break;
4921c301fc5SJordan Crouse 
4931c301fc5SJordan Crouse 	default:
4941c301fc5SJordan Crouse 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
4951c301fc5SJordan Crouse 
4969ed5bc24SGuenter Roeck 		/*
4979ed5bc24SGuenter Roeck 		 * We maintain an extra 2 digits of precision for simplicity
4989ed5bc24SGuenter Roeck 		 * - shift those back off before writing the value
4999ed5bc24SGuenter Roeck 		 */
5001c301fc5SJordan Crouse 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
5011c301fc5SJordan Crouse 	}
5021c301fc5SJordan Crouse 
5031c301fc5SJordan Crouse 	switch (sattr->nr) {
5041c301fc5SJordan Crouse 	case MIN:
5051c301fc5SJordan Crouse 		reg = TEMP_MIN_REG(sattr->index);
5061c301fc5SJordan Crouse 		break;
5071c301fc5SJordan Crouse 	case MAX:
5081c301fc5SJordan Crouse 		reg = TEMP_MAX_REG(sattr->index);
5091c301fc5SJordan Crouse 		break;
5101c301fc5SJordan Crouse 	case OFFSET:
5111c301fc5SJordan Crouse 		reg = TEMP_OFFSET_REG(sattr->index);
5121c301fc5SJordan Crouse 		break;
5131c301fc5SJordan Crouse 	case AUTOMIN:
5141c301fc5SJordan Crouse 		reg = TEMP_TMIN_REG(sattr->index);
5151c301fc5SJordan Crouse 		break;
5161c301fc5SJordan Crouse 	case THERM:
5171c301fc5SJordan Crouse 		reg = TEMP_THERM_REG(sattr->index);
5181c301fc5SJordan Crouse 		break;
5191c301fc5SJordan Crouse 	case HYSTERSIS:
5201c301fc5SJordan Crouse 		if (sattr->index != 2)
5211c301fc5SJordan Crouse 			reg = REG_REMOTE1_HYSTERSIS;
5221c301fc5SJordan Crouse 		else
5231c301fc5SJordan Crouse 			reg = REG_REMOTE2_HYSTERSIS;
5241c301fc5SJordan Crouse 
5251c301fc5SJordan Crouse 		break;
5261c301fc5SJordan Crouse 	}
5271c301fc5SJordan Crouse 
5281c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, out);
5291c301fc5SJordan Crouse 
5301c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5311c301fc5SJordan Crouse 	return count;
5321c301fc5SJordan Crouse }
5331c301fc5SJordan Crouse 
5348f05bcc3SChris Packham /* Assuming CONFIG6[SLOW] is 0 */
5358f05bcc3SChris Packham static const int ad7475_st_map[] = {
5368f05bcc3SChris Packham 	37500, 18800, 12500, 7500, 4700, 3100, 1600, 800,
5378f05bcc3SChris Packham };
5388f05bcc3SChris Packham 
5398f05bcc3SChris Packham static ssize_t show_temp_st(struct device *dev, struct device_attribute *attr,
5408f05bcc3SChris Packham 				  char *buf)
5418f05bcc3SChris Packham {
5428f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5438f05bcc3SChris Packham 	struct i2c_client *client = to_i2c_client(dev);
5448f05bcc3SChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
5458f05bcc3SChris Packham 	long val;
5468f05bcc3SChris Packham 
5478f05bcc3SChris Packham 	switch (sattr->index) {
5488f05bcc3SChris Packham 	case 0:
5498f05bcc3SChris Packham 		val = data->enh_acoustics[0] & 0xf;
5508f05bcc3SChris Packham 		break;
5518f05bcc3SChris Packham 	case 1:
5528f05bcc3SChris Packham 		val = (data->enh_acoustics[1] >> 4) & 0xf;
5538f05bcc3SChris Packham 		break;
5548f05bcc3SChris Packham 	case 2:
5558f05bcc3SChris Packham 	default:
5568f05bcc3SChris Packham 		val = data->enh_acoustics[1] & 0xf;
5578f05bcc3SChris Packham 		break;
5588f05bcc3SChris Packham 	}
5598f05bcc3SChris Packham 
5608f05bcc3SChris Packham 	if (val & 0x8)
5618f05bcc3SChris Packham 		return sprintf(buf, "%d\n", ad7475_st_map[val & 0x7]);
5628f05bcc3SChris Packham 	else
5638f05bcc3SChris Packham 		return sprintf(buf, "0\n");
5648f05bcc3SChris Packham }
5658f05bcc3SChris Packham 
5668f05bcc3SChris Packham static ssize_t set_temp_st(struct device *dev, struct device_attribute *attr,
5678f05bcc3SChris Packham 				 const char *buf, size_t count)
5688f05bcc3SChris Packham {
5698f05bcc3SChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5708f05bcc3SChris Packham 	struct i2c_client *client = to_i2c_client(dev);
5718f05bcc3SChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
5728f05bcc3SChris Packham 	unsigned char reg;
5738f05bcc3SChris Packham 	int shift, idx;
5748f05bcc3SChris Packham 	ulong val;
5758f05bcc3SChris Packham 
5768f05bcc3SChris Packham 	if (kstrtoul(buf, 10, &val))
5778f05bcc3SChris Packham 		return -EINVAL;
5788f05bcc3SChris Packham 
5798f05bcc3SChris Packham 	switch (sattr->index) {
5808f05bcc3SChris Packham 	case 0:
5818f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS1;
5828f05bcc3SChris Packham 		shift = 0;
5838f05bcc3SChris Packham 		idx = 0;
5848f05bcc3SChris Packham 		break;
5858f05bcc3SChris Packham 	case 1:
5868f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
5878f05bcc3SChris Packham 		shift = 0;
5888f05bcc3SChris Packham 		idx = 1;
5898f05bcc3SChris Packham 		break;
5908f05bcc3SChris Packham 	case 2:
5918f05bcc3SChris Packham 	default:
5928f05bcc3SChris Packham 		reg = REG_ENHANCE_ACOUSTICS2;
5938f05bcc3SChris Packham 		shift = 4;
5948f05bcc3SChris Packham 		idx = 1;
5958f05bcc3SChris Packham 		break;
5968f05bcc3SChris Packham 	}
5978f05bcc3SChris Packham 
5988f05bcc3SChris Packham 	if (val > 0) {
5998f05bcc3SChris Packham 		val = find_closest_descending(val, ad7475_st_map,
6008f05bcc3SChris Packham 					      ARRAY_SIZE(ad7475_st_map));
6018f05bcc3SChris Packham 		val |= 0x8;
6028f05bcc3SChris Packham 	}
6038f05bcc3SChris Packham 
6048f05bcc3SChris Packham 	mutex_lock(&data->lock);
6058f05bcc3SChris Packham 
6068f05bcc3SChris Packham 	data->enh_acoustics[idx] &= ~(0xf << shift);
6078f05bcc3SChris Packham 	data->enh_acoustics[idx] |= (val << shift);
6088f05bcc3SChris Packham 
6098f05bcc3SChris Packham 	i2c_smbus_write_byte_data(client, reg, data->enh_acoustics[idx]);
6108f05bcc3SChris Packham 
6118f05bcc3SChris Packham 	mutex_unlock(&data->lock);
6128f05bcc3SChris Packham 
6138f05bcc3SChris Packham 	return count;
6148f05bcc3SChris Packham }
6158f05bcc3SChris Packham 
6169ed5bc24SGuenter Roeck /*
6179ed5bc24SGuenter Roeck  * Table of autorange values - the user will write the value in millidegrees,
6189ed5bc24SGuenter Roeck  * and we'll convert it
6199ed5bc24SGuenter Roeck  */
6201c301fc5SJordan Crouse static const int autorange_table[] = {
6211c301fc5SJordan Crouse 	2000, 2500, 3330, 4000, 5000, 6670, 8000,
6221c301fc5SJordan Crouse 	10000, 13330, 16000, 20000, 26670, 32000, 40000,
6231c301fc5SJordan Crouse 	53330, 80000
6241c301fc5SJordan Crouse };
6251c301fc5SJordan Crouse 
6261c301fc5SJordan Crouse static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
6271c301fc5SJordan Crouse 			   char *buf)
6281c301fc5SJordan Crouse {
6291c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6301c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6311c301fc5SJordan Crouse 	int out, val;
6321c301fc5SJordan Crouse 
6334afec79fSTokunori Ikegami 	if (IS_ERR(data))
6344afec79fSTokunori Ikegami 		return PTR_ERR(data);
6354afec79fSTokunori Ikegami 
6361c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6371c301fc5SJordan Crouse 	out = (data->range[sattr->index] >> 4) & 0x0F;
6381c301fc5SJordan Crouse 	val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6391c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6401c301fc5SJordan Crouse 
6411c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", val + autorange_table[out]);
6421c301fc5SJordan Crouse }
6431c301fc5SJordan Crouse 
6441c301fc5SJordan Crouse static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
6451c301fc5SJordan Crouse 			  const char *buf, size_t count)
6461c301fc5SJordan Crouse {
6471c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
6481c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
6491c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6501c301fc5SJordan Crouse 	int temp;
6511c301fc5SJordan Crouse 	long val;
6521c301fc5SJordan Crouse 
653179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
6541c301fc5SJordan Crouse 		return -EINVAL;
6551c301fc5SJordan Crouse 
6561c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6571c301fc5SJordan Crouse 
6581c301fc5SJordan Crouse 	/* Get a fresh copy of the needed registers */
6591c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
6601c301fc5SJordan Crouse 	data->temp[AUTOMIN][sattr->index] =
6611c301fc5SJordan Crouse 		adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
6621c301fc5SJordan Crouse 	data->range[sattr->index] =
6631c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
6641c301fc5SJordan Crouse 
6659ed5bc24SGuenter Roeck 	/*
6669ed5bc24SGuenter Roeck 	 * The user will write an absolute value, so subtract the start point
6679ed5bc24SGuenter Roeck 	 * to figure the range
6689ed5bc24SGuenter Roeck 	 */
6691c301fc5SJordan Crouse 	temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6702a844c14SGuenter Roeck 	val = clamp_val(val, temp + autorange_table[0],
6711c301fc5SJordan Crouse 		temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
6721c301fc5SJordan Crouse 	val -= temp;
6731c301fc5SJordan Crouse 
6741c301fc5SJordan Crouse 	/* Find the nearest table entry to what the user wrote */
675e4651640SChris Packham 	val = find_closest(val, autorange_table, ARRAY_SIZE(autorange_table));
6761c301fc5SJordan Crouse 
6771c301fc5SJordan Crouse 	data->range[sattr->index] &= ~0xF0;
6781c301fc5SJordan Crouse 	data->range[sattr->index] |= val << 4;
6791c301fc5SJordan Crouse 
6801c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
6811c301fc5SJordan Crouse 				  data->range[sattr->index]);
6821c301fc5SJordan Crouse 
6831c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6841c301fc5SJordan Crouse 	return count;
6851c301fc5SJordan Crouse }
6861c301fc5SJordan Crouse 
6871c301fc5SJordan Crouse static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
6881c301fc5SJordan Crouse 			 char *buf)
6891c301fc5SJordan Crouse {
6901c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6911c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6921c301fc5SJordan Crouse 	int out;
6931c301fc5SJordan Crouse 
6944afec79fSTokunori Ikegami 	if (IS_ERR(data))
6954afec79fSTokunori Ikegami 		return PTR_ERR(data);
6964afec79fSTokunori Ikegami 
6971c301fc5SJordan Crouse 	if (sattr->nr == ALARM)
6981c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 10)) & 1;
6991c301fc5SJordan Crouse 	else
7001c301fc5SJordan Crouse 		out = tach2rpm(data->tach[sattr->nr][sattr->index]);
7011c301fc5SJordan Crouse 
7021c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
7031c301fc5SJordan Crouse }
7041c301fc5SJordan Crouse 
7051c301fc5SJordan Crouse static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
7061c301fc5SJordan Crouse 			const char *buf, size_t count)
7071c301fc5SJordan Crouse {
7081c301fc5SJordan Crouse 
7091c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7101c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7111c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7121c301fc5SJordan Crouse 	unsigned long val;
7131c301fc5SJordan Crouse 
714179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
7151c301fc5SJordan Crouse 		return -EINVAL;
7161c301fc5SJordan Crouse 
7171c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7181c301fc5SJordan Crouse 
7191c301fc5SJordan Crouse 	data->tach[MIN][sattr->index] = rpm2tach(val);
7201c301fc5SJordan Crouse 
7211c301fc5SJordan Crouse 	adt7475_write_word(client, TACH_MIN_REG(sattr->index),
7221c301fc5SJordan Crouse 			   data->tach[MIN][sattr->index]);
7231c301fc5SJordan Crouse 
7241c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7251c301fc5SJordan Crouse 	return count;
7261c301fc5SJordan Crouse }
7271c301fc5SJordan Crouse 
7281c301fc5SJordan Crouse static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
7291c301fc5SJordan Crouse 			char *buf)
7301c301fc5SJordan Crouse {
7311c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7321c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7331c301fc5SJordan Crouse 
7344afec79fSTokunori Ikegami 	if (IS_ERR(data))
7354afec79fSTokunori Ikegami 		return PTR_ERR(data);
7364afec79fSTokunori Ikegami 
7371c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
7381c301fc5SJordan Crouse }
7391c301fc5SJordan Crouse 
7401c301fc5SJordan Crouse static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
7411c301fc5SJordan Crouse 			    char *buf)
7421c301fc5SJordan Crouse {
7431c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7441c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7451c301fc5SJordan Crouse 
7464afec79fSTokunori Ikegami 	if (IS_ERR(data))
7474afec79fSTokunori Ikegami 		return PTR_ERR(data);
7484afec79fSTokunori Ikegami 
7491c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
7501c301fc5SJordan Crouse }
7511c301fc5SJordan Crouse 
7521c301fc5SJordan Crouse static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
7531c301fc5SJordan Crouse 			    char *buf)
7541c301fc5SJordan Crouse {
7551c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7561c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7571c301fc5SJordan Crouse 
7584afec79fSTokunori Ikegami 	if (IS_ERR(data))
7594afec79fSTokunori Ikegami 		return PTR_ERR(data);
7604afec79fSTokunori Ikegami 
7611c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
7621c301fc5SJordan Crouse }
7631c301fc5SJordan Crouse 
7641c301fc5SJordan Crouse static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
7651c301fc5SJordan Crouse 		       const char *buf, size_t count)
7661c301fc5SJordan Crouse {
7671c301fc5SJordan Crouse 
7681c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7691c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7701c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7711c301fc5SJordan Crouse 	unsigned char reg = 0;
7721c301fc5SJordan Crouse 	long val;
7731c301fc5SJordan Crouse 
774179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
7751c301fc5SJordan Crouse 		return -EINVAL;
7761c301fc5SJordan Crouse 
7771c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7781c301fc5SJordan Crouse 
7791c301fc5SJordan Crouse 	switch (sattr->nr) {
7801c301fc5SJordan Crouse 	case INPUT:
7811c301fc5SJordan Crouse 		/* Get a fresh value for CONTROL */
7821c301fc5SJordan Crouse 		data->pwm[CONTROL][sattr->index] =
7831c301fc5SJordan Crouse 			adt7475_read(PWM_CONFIG_REG(sattr->index));
7841c301fc5SJordan Crouse 
7859ed5bc24SGuenter Roeck 		/*
7869ed5bc24SGuenter Roeck 		 * If we are not in manual mode, then we shouldn't allow
7879ed5bc24SGuenter Roeck 		 * the user to set the pwm speed
7889ed5bc24SGuenter Roeck 		 */
7891c301fc5SJordan Crouse 		if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
7901c301fc5SJordan Crouse 			mutex_unlock(&data->lock);
7911c301fc5SJordan Crouse 			return count;
7921c301fc5SJordan Crouse 		}
7931c301fc5SJordan Crouse 
7941c301fc5SJordan Crouse 		reg = PWM_REG(sattr->index);
7951c301fc5SJordan Crouse 		break;
7961c301fc5SJordan Crouse 
7971c301fc5SJordan Crouse 	case MIN:
7981c301fc5SJordan Crouse 		reg = PWM_MIN_REG(sattr->index);
7991c301fc5SJordan Crouse 		break;
8001c301fc5SJordan Crouse 
8011c301fc5SJordan Crouse 	case MAX:
8021c301fc5SJordan Crouse 		reg = PWM_MAX_REG(sattr->index);
8031c301fc5SJordan Crouse 		break;
8041c301fc5SJordan Crouse 	}
8051c301fc5SJordan Crouse 
8062a844c14SGuenter Roeck 	data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
8071c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
8081c301fc5SJordan Crouse 				  data->pwm[sattr->nr][sattr->index]);
8091d58f5efSChris Packham 	mutex_unlock(&data->lock);
8101d58f5efSChris Packham 
8111d58f5efSChris Packham 	return count;
8121d58f5efSChris Packham }
8131d58f5efSChris Packham 
8141d58f5efSChris Packham static ssize_t show_stall_disable(struct device *dev,
8151d58f5efSChris Packham 				  struct device_attribute *attr, char *buf)
8161d58f5efSChris Packham {
8171d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8181d58f5efSChris Packham 	struct i2c_client *client = to_i2c_client(dev);
8191d58f5efSChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
8201d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8211d58f5efSChris Packham 
8221d58f5efSChris Packham 	return sprintf(buf, "%d\n", !!(data->enh_acoustics[0] & mask));
8231d58f5efSChris Packham }
8241d58f5efSChris Packham 
8251d58f5efSChris Packham static ssize_t set_stall_disable(struct device *dev,
8261d58f5efSChris Packham 				 struct device_attribute *attr, const char *buf,
8271d58f5efSChris Packham 				 size_t count)
8281d58f5efSChris Packham {
8291d58f5efSChris Packham 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8301d58f5efSChris Packham 	struct i2c_client *client = to_i2c_client(dev);
8311d58f5efSChris Packham 	struct adt7475_data *data = i2c_get_clientdata(client);
8321d58f5efSChris Packham 	long val;
8331d58f5efSChris Packham 	u8 mask = BIT(5 + sattr->index);
8341d58f5efSChris Packham 
8351d58f5efSChris Packham 	if (kstrtol(buf, 10, &val))
8361d58f5efSChris Packham 		return -EINVAL;
8371d58f5efSChris Packham 
8381d58f5efSChris Packham 	mutex_lock(&data->lock);
8391d58f5efSChris Packham 
8401d58f5efSChris Packham 	data->enh_acoustics[0] &= ~mask;
8411d58f5efSChris Packham 	if (val)
8421d58f5efSChris Packham 		data->enh_acoustics[0] |= mask;
8431d58f5efSChris Packham 
8441d58f5efSChris Packham 	i2c_smbus_write_byte_data(client, REG_ENHANCE_ACOUSTICS1,
8451d58f5efSChris Packham 				  data->enh_acoustics[0]);
8461c301fc5SJordan Crouse 
8471c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8481c301fc5SJordan Crouse 
8491c301fc5SJordan Crouse 	return count;
8501c301fc5SJordan Crouse }
8511c301fc5SJordan Crouse 
8521c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */
8531c301fc5SJordan Crouse 
8541c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index,
8551c301fc5SJordan Crouse 		      unsigned int pwmctl, unsigned int pwmchan)
8561c301fc5SJordan Crouse {
8571c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8581c301fc5SJordan Crouse 	long val = 0;
8591c301fc5SJordan Crouse 
8601c301fc5SJordan Crouse 	switch (pwmctl) {
8611c301fc5SJordan Crouse 	case 0:
8621c301fc5SJordan Crouse 		val = 0x03;	/* Run at full speed */
8631c301fc5SJordan Crouse 		break;
8641c301fc5SJordan Crouse 	case 1:
8651c301fc5SJordan Crouse 		val = 0x07;	/* Manual mode */
8661c301fc5SJordan Crouse 		break;
8671c301fc5SJordan Crouse 	case 2:
8681c301fc5SJordan Crouse 		switch (pwmchan) {
8691c301fc5SJordan Crouse 		case 1:
8701c301fc5SJordan Crouse 			/* Remote1 controls PWM */
8711c301fc5SJordan Crouse 			val = 0x00;
8721c301fc5SJordan Crouse 			break;
8731c301fc5SJordan Crouse 		case 2:
8741c301fc5SJordan Crouse 			/* local controls PWM */
8751c301fc5SJordan Crouse 			val = 0x01;
8761c301fc5SJordan Crouse 			break;
8771c301fc5SJordan Crouse 		case 4:
8781c301fc5SJordan Crouse 			/* remote2 controls PWM */
8791c301fc5SJordan Crouse 			val = 0x02;
8801c301fc5SJordan Crouse 			break;
8811c301fc5SJordan Crouse 		case 6:
8821c301fc5SJordan Crouse 			/* local/remote2 control PWM */
8831c301fc5SJordan Crouse 			val = 0x05;
8841c301fc5SJordan Crouse 			break;
8851c301fc5SJordan Crouse 		case 7:
8861c301fc5SJordan Crouse 			/* All three control PWM */
8871c301fc5SJordan Crouse 			val = 0x06;
8881c301fc5SJordan Crouse 			break;
8891c301fc5SJordan Crouse 		default:
8901c301fc5SJordan Crouse 			return -EINVAL;
8911c301fc5SJordan Crouse 		}
8921c301fc5SJordan Crouse 		break;
8931c301fc5SJordan Crouse 	default:
8941c301fc5SJordan Crouse 		return -EINVAL;
8951c301fc5SJordan Crouse 	}
8961c301fc5SJordan Crouse 
8971c301fc5SJordan Crouse 	data->pwmctl[index] = pwmctl;
8981c301fc5SJordan Crouse 	data->pwmchan[index] = pwmchan;
8991c301fc5SJordan Crouse 
9001c301fc5SJordan Crouse 	data->pwm[CONTROL][index] &= ~0xE0;
9011c301fc5SJordan Crouse 	data->pwm[CONTROL][index] |= (val & 7) << 5;
9021c301fc5SJordan Crouse 
9031c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
9041c301fc5SJordan Crouse 				  data->pwm[CONTROL][index]);
9051c301fc5SJordan Crouse 
9061c301fc5SJordan Crouse 	return 0;
9071c301fc5SJordan Crouse }
9081c301fc5SJordan Crouse 
9091c301fc5SJordan Crouse static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
9101c301fc5SJordan Crouse 			   const char *buf, size_t count)
9111c301fc5SJordan Crouse {
9121c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9131c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9141c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9151c301fc5SJordan Crouse 	int r;
9161c301fc5SJordan Crouse 	long val;
9171c301fc5SJordan Crouse 
918179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9191c301fc5SJordan Crouse 		return -EINVAL;
9201c301fc5SJordan Crouse 
9211c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9221c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9231c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9241c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
9251c301fc5SJordan Crouse 	if (r)
9261c301fc5SJordan Crouse 		count = r;
9271c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9281c301fc5SJordan Crouse 
9291c301fc5SJordan Crouse 	return count;
9301c301fc5SJordan Crouse }
9311c301fc5SJordan Crouse 
9321c301fc5SJordan Crouse static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
9331c301fc5SJordan Crouse 			   const char *buf, size_t count)
9341c301fc5SJordan Crouse {
9351c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9361c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9371c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9381c301fc5SJordan Crouse 	int r;
9391c301fc5SJordan Crouse 	long val;
9401c301fc5SJordan Crouse 
941179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9421c301fc5SJordan Crouse 		return -EINVAL;
9431c301fc5SJordan Crouse 
9441c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9451c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
9461c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
9471c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
9481c301fc5SJordan Crouse 	if (r)
9491c301fc5SJordan Crouse 		count = r;
9501c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
9511c301fc5SJordan Crouse 
9521c301fc5SJordan Crouse 	return count;
9531c301fc5SJordan Crouse }
9541c301fc5SJordan Crouse 
9551c301fc5SJordan Crouse /* List of frequencies for the PWM */
9561c301fc5SJordan Crouse static const int pwmfreq_table[] = {
9573490c92aSChris Packham 	11, 14, 22, 29, 35, 44, 58, 88, 22500
9581c301fc5SJordan Crouse };
9591c301fc5SJordan Crouse 
9601c301fc5SJordan Crouse static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
9611c301fc5SJordan Crouse 			    char *buf)
9621c301fc5SJordan Crouse {
9631c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
9641c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
965*9d19371dSDan Carpenter 	int idx;
9661c301fc5SJordan Crouse 
9674afec79fSTokunori Ikegami 	if (IS_ERR(data))
9684afec79fSTokunori Ikegami 		return PTR_ERR(data);
969*9d19371dSDan Carpenter 	idx = clamp_val(data->range[sattr->index] & 0xf, 0,
970*9d19371dSDan Carpenter 			ARRAY_SIZE(pwmfreq_table) - 1);
9714afec79fSTokunori Ikegami 
972*9d19371dSDan Carpenter 	return sprintf(buf, "%d\n", pwmfreq_table[idx]);
9731c301fc5SJordan Crouse }
9741c301fc5SJordan Crouse 
9751c301fc5SJordan Crouse static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
9761c301fc5SJordan Crouse 			   const char *buf, size_t count)
9771c301fc5SJordan Crouse {
9781c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
9791c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
9801c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
9811c301fc5SJordan Crouse 	int out;
9821c301fc5SJordan Crouse 	long val;
9831c301fc5SJordan Crouse 
984179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
9851c301fc5SJordan Crouse 		return -EINVAL;
9861c301fc5SJordan Crouse 
987e4651640SChris Packham 	out = find_closest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
9881c301fc5SJordan Crouse 
9891c301fc5SJordan Crouse 	mutex_lock(&data->lock);
9901c301fc5SJordan Crouse 
9911c301fc5SJordan Crouse 	data->range[sattr->index] =
9921c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
9933490c92aSChris Packham 	data->range[sattr->index] &= ~0xf;
9941c301fc5SJordan Crouse 	data->range[sattr->index] |= out;
9951c301fc5SJordan Crouse 
9961c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
9971c301fc5SJordan Crouse 				  data->range[sattr->index]);
9981c301fc5SJordan Crouse 
9991c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
10001c301fc5SJordan Crouse 	return count;
10011c301fc5SJordan Crouse }
10021c301fc5SJordan Crouse 
10031d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
10041d05303cSJulia Lawall 					struct device_attribute *devattr,
10051d05303cSJulia Lawall 					char *buf)
1006f99318b2SJean Delvare {
1007f99318b2SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
1008*9d19371dSDan Carpenter 
1009*9d19371dSDan Carpenter 	if (IS_ERR(data))
1010*9d19371dSDan Carpenter 		return PTR_ERR(data);
1011*9d19371dSDan Carpenter 
1012f99318b2SJean Delvare 	return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
1013f99318b2SJean Delvare }
1014f99318b2SJean Delvare 
10151d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
1016f99318b2SJean Delvare 					struct device_attribute *devattr,
1017f99318b2SJean Delvare 					const char *buf, size_t count)
1018f99318b2SJean Delvare {
1019f99318b2SJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
1020f99318b2SJean Delvare 	struct adt7475_data *data = i2c_get_clientdata(client);
1021f99318b2SJean Delvare 	long val;
1022f99318b2SJean Delvare 
1023179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
1024f99318b2SJean Delvare 		return -EINVAL;
1025f99318b2SJean Delvare 	if (val != 0 && val != 1)
1026f99318b2SJean Delvare 		return -EINVAL;
1027f99318b2SJean Delvare 
1028f99318b2SJean Delvare 	mutex_lock(&data->lock);
1029f99318b2SJean Delvare 	data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
1030f99318b2SJean Delvare 	if (val)
1031f99318b2SJean Delvare 		data->config4 |= CONFIG4_MAXDUTY;
1032f99318b2SJean Delvare 	else
1033f99318b2SJean Delvare 		data->config4 &= ~CONFIG4_MAXDUTY;
1034f99318b2SJean Delvare 	i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
1035f99318b2SJean Delvare 	mutex_unlock(&data->lock);
1036f99318b2SJean Delvare 
1037f99318b2SJean Delvare 	return count;
1038f99318b2SJean Delvare }
1039f99318b2SJean Delvare 
10401d05303cSJulia Lawall static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
104154fe4671SJean Delvare 			char *buf)
104254fe4671SJean Delvare {
104354fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
104454fe4671SJean Delvare 	return sprintf(buf, "%d\n", (int)data->vrm);
104554fe4671SJean Delvare }
104654fe4671SJean Delvare 
10471d05303cSJulia Lawall static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
104854fe4671SJean Delvare 			 const char *buf, size_t count)
104954fe4671SJean Delvare {
105054fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
105154fe4671SJean Delvare 	long val;
105254fe4671SJean Delvare 
1053179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
105454fe4671SJean Delvare 		return -EINVAL;
105554fe4671SJean Delvare 	if (val < 0 || val > 255)
105654fe4671SJean Delvare 		return -EINVAL;
105754fe4671SJean Delvare 	data->vrm = val;
105854fe4671SJean Delvare 
105954fe4671SJean Delvare 	return count;
106054fe4671SJean Delvare }
106154fe4671SJean Delvare 
10621d05303cSJulia Lawall static ssize_t cpu0_vid_show(struct device *dev,
10631d05303cSJulia Lawall 			     struct device_attribute *devattr, char *buf)
106454fe4671SJean Delvare {
106554fe4671SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
10664afec79fSTokunori Ikegami 
10674afec79fSTokunori Ikegami 	if (IS_ERR(data))
10684afec79fSTokunori Ikegami 		return PTR_ERR(data);
10694afec79fSTokunori Ikegami 
107054fe4671SJean Delvare 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
107154fe4671SJean Delvare }
107254fe4671SJean Delvare 
10733d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
10743d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
10753d849981SJean Delvare 			    set_voltage, MAX, 0);
10763d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
10773d849981SJean Delvare 			    set_voltage, MIN, 0);
10783d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
1079cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
10801c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
10811c301fc5SJordan Crouse 			    set_voltage, MAX, 1);
1082cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
10831c301fc5SJordan Crouse 			    set_voltage, MIN, 1);
1084cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
1085cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
1086cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
1087cffb9dd0SJean Delvare 			    set_voltage, MAX, 2);
1088cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
1089cffb9dd0SJean Delvare 			    set_voltage, MIN, 2);
1090cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
10913d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
10923d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
10933d849981SJean Delvare 			    set_voltage, MAX, 3);
10943d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
10953d849981SJean Delvare 			    set_voltage, MIN, 3);
10963d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
10973d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
10983d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
10993d849981SJean Delvare 			    set_voltage, MAX, 4);
11003d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
11013d849981SJean Delvare 			    set_voltage, MIN, 4);
11023d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
11033d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
11043d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
11053d849981SJean Delvare 			    set_voltage, MAX, 5);
11063d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
11073d849981SJean Delvare 			    set_voltage, MIN, 5);
11083d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
11091c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
11101c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
11111c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
11121c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
11131c301fc5SJordan Crouse 			    MAX, 0);
11141c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
11151c301fc5SJordan Crouse 			    MIN, 0);
11161c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
11171c301fc5SJordan Crouse 			    set_temp, OFFSET, 0);
11181c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
11191c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 0);
11201c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
11211c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 0);
11221c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
11231c301fc5SJordan Crouse 			    THERM, 0);
11241c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
11251c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 0);
11268f05bcc3SChris Packham static SENSOR_DEVICE_ATTR_2(temp1_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
11278f05bcc3SChris Packham 			    set_temp_st, 0, 0);
11281c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
11291c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
11301c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
11311c301fc5SJordan Crouse 			    MAX, 1);
11321c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
11331c301fc5SJordan Crouse 			    MIN, 1);
11341c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
11351c301fc5SJordan Crouse 			    set_temp, OFFSET, 1);
11361c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
11371c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 1);
11381c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
11391c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 1);
11401c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
11411c301fc5SJordan Crouse 			    THERM, 1);
11421c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
11431c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 1);
11448f05bcc3SChris Packham static SENSOR_DEVICE_ATTR_2(temp2_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
11458f05bcc3SChris Packham 			    set_temp_st, 0, 1);
11461c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
11471c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
11481c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
11491c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
11501c301fc5SJordan Crouse 			    MAX, 2);
11511c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
11521c301fc5SJordan Crouse 			    MIN, 2);
11531c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
11541c301fc5SJordan Crouse 			    set_temp, OFFSET, 2);
11551c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
11561c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 2);
11571c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
11581c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 2);
11591c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
11601c301fc5SJordan Crouse 			    THERM, 2);
11611c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
11621c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 2);
11638f05bcc3SChris Packham static SENSOR_DEVICE_ATTR_2(temp3_smoothing, S_IRUGO | S_IWUSR, show_temp_st,
11648f05bcc3SChris Packham 			    set_temp_st, 0, 2);
11651c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
11661c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11671c301fc5SJordan Crouse 			    MIN, 0);
11681c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
11691c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
11701c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11711c301fc5SJordan Crouse 			    MIN, 1);
11721c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
11731c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
11741c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11751c301fc5SJordan Crouse 			    MIN, 2);
11761c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
11771c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
11781c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
11791c301fc5SJordan Crouse 			    MIN, 3);
11801c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
11811c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
11821c301fc5SJordan Crouse 			    0);
11831c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
11841c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 0);
11851c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
11861c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 0);
118784d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
11881c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 0);
11891c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
11901c301fc5SJordan Crouse 			    set_pwm, MIN, 0);
11911c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
11921c301fc5SJordan Crouse 			    set_pwm, MAX, 0);
11931d58f5efSChris Packham static SENSOR_DEVICE_ATTR_2(pwm1_stall_disable, S_IRUGO | S_IWUSR,
11941d58f5efSChris Packham 			    show_stall_disable, set_stall_disable, 0, 0);
11951c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
11961c301fc5SJordan Crouse 			    1);
11971c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
11981c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 1);
11991c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
12001c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 1);
120184d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
12021c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 1);
12031c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
12041c301fc5SJordan Crouse 			    set_pwm, MIN, 1);
12051c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
12061c301fc5SJordan Crouse 			    set_pwm, MAX, 1);
12071d58f5efSChris Packham static SENSOR_DEVICE_ATTR_2(pwm2_stall_disable, S_IRUGO | S_IWUSR,
12081d58f5efSChris Packham 			    show_stall_disable, set_stall_disable, 0, 1);
12091c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
12101c301fc5SJordan Crouse 			    2);
12111c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
12121c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 2);
12131c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
12141c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 2);
121584d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
12161c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 2);
12171c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
12181c301fc5SJordan Crouse 			    set_pwm, MIN, 2);
12191c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
12201c301fc5SJordan Crouse 			    set_pwm, MAX, 2);
12211d58f5efSChris Packham static SENSOR_DEVICE_ATTR_2(pwm3_stall_disable, S_IRUGO | S_IWUSR,
12221d58f5efSChris Packham 			    show_stall_disable, set_stall_disable, 0, 2);
12231c301fc5SJordan Crouse 
1224f99318b2SJean Delvare /* Non-standard name, might need revisiting */
12251d05303cSJulia Lawall static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1226f99318b2SJean Delvare 
12271d05303cSJulia Lawall static DEVICE_ATTR_RW(vrm);
12281d05303cSJulia Lawall static DEVICE_ATTR_RO(cpu0_vid);
122954fe4671SJean Delvare 
12301c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = {
12311c301fc5SJordan Crouse 	&sensor_dev_attr_in1_input.dev_attr.attr,
12321c301fc5SJordan Crouse 	&sensor_dev_attr_in1_max.dev_attr.attr,
12331c301fc5SJordan Crouse 	&sensor_dev_attr_in1_min.dev_attr.attr,
12341c301fc5SJordan Crouse 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
12351c301fc5SJordan Crouse 	&sensor_dev_attr_in2_input.dev_attr.attr,
12361c301fc5SJordan Crouse 	&sensor_dev_attr_in2_max.dev_attr.attr,
12371c301fc5SJordan Crouse 	&sensor_dev_attr_in2_min.dev_attr.attr,
12381c301fc5SJordan Crouse 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
12391c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_input.dev_attr.attr,
12401c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
12411c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
12421c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_max.dev_attr.attr,
12431c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_min.dev_attr.attr,
12441c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
12451c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
12461c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
12471c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
12481c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
12498f05bcc3SChris Packham 	&sensor_dev_attr_temp1_smoothing.dev_attr.attr,
12501c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_input.dev_attr.attr,
12511c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
12521c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_max.dev_attr.attr,
12531c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_min.dev_attr.attr,
12541c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
12551c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
12561c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
12571c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
12581c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
12598f05bcc3SChris Packham 	&sensor_dev_attr_temp2_smoothing.dev_attr.attr,
12601c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_input.dev_attr.attr,
12611c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
12621c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
12631c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_max.dev_attr.attr,
12641c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_min.dev_attr.attr,
12651c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
12661c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
12671c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
12681c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
12691c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
12708f05bcc3SChris Packham 	&sensor_dev_attr_temp3_smoothing.dev_attr.attr,
12711c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_input.dev_attr.attr,
12721c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_min.dev_attr.attr,
12731c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
12741c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_input.dev_attr.attr,
12751c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_min.dev_attr.attr,
12761c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
12771c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_input.dev_attr.attr,
12781c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_min.dev_attr.attr,
12791c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
12801c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1.dev_attr.attr,
12811c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
12821c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
128384d2a314SJean Delvare 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
12841c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
12851c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
12861d58f5efSChris Packham 	&sensor_dev_attr_pwm1_stall_disable.dev_attr.attr,
12871c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3.dev_attr.attr,
12881c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
12891c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
129084d2a314SJean Delvare 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
12911c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
12921c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
12931d58f5efSChris Packham 	&sensor_dev_attr_pwm3_stall_disable.dev_attr.attr,
1294f99318b2SJean Delvare 	&dev_attr_pwm_use_point2_pwm_at_crit.attr,
12951c301fc5SJordan Crouse 	NULL,
12961c301fc5SJordan Crouse };
12971c301fc5SJordan Crouse 
1298378933c9SJean Delvare static struct attribute *fan4_attrs[] = {
1299378933c9SJean Delvare 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1300378933c9SJean Delvare 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1301378933c9SJean Delvare 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1302378933c9SJean Delvare 	NULL
1303378933c9SJean Delvare };
1304378933c9SJean Delvare 
1305378933c9SJean Delvare static struct attribute *pwm2_attrs[] = {
1306378933c9SJean Delvare 	&sensor_dev_attr_pwm2.dev_attr.attr,
1307378933c9SJean Delvare 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1308378933c9SJean Delvare 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1309378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1310378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1311378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
13121d58f5efSChris Packham 	&sensor_dev_attr_pwm2_stall_disable.dev_attr.attr,
1313378933c9SJean Delvare 	NULL
1314378933c9SJean Delvare };
1315378933c9SJean Delvare 
1316378933c9SJean Delvare static struct attribute *in0_attrs[] = {
13173d849981SJean Delvare 	&sensor_dev_attr_in0_input.dev_attr.attr,
13183d849981SJean Delvare 	&sensor_dev_attr_in0_max.dev_attr.attr,
13193d849981SJean Delvare 	&sensor_dev_attr_in0_min.dev_attr.attr,
13203d849981SJean Delvare 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1321378933c9SJean Delvare 	NULL
1322378933c9SJean Delvare };
1323378933c9SJean Delvare 
1324d8d2ee07SJean Delvare static struct attribute *in3_attrs[] = {
13253d849981SJean Delvare 	&sensor_dev_attr_in3_input.dev_attr.attr,
13263d849981SJean Delvare 	&sensor_dev_attr_in3_max.dev_attr.attr,
13273d849981SJean Delvare 	&sensor_dev_attr_in3_min.dev_attr.attr,
13283d849981SJean Delvare 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1329d8d2ee07SJean Delvare 	NULL
1330d8d2ee07SJean Delvare };
1331d8d2ee07SJean Delvare 
1332d8d2ee07SJean Delvare static struct attribute *in4_attrs[] = {
13333d849981SJean Delvare 	&sensor_dev_attr_in4_input.dev_attr.attr,
13343d849981SJean Delvare 	&sensor_dev_attr_in4_max.dev_attr.attr,
13353d849981SJean Delvare 	&sensor_dev_attr_in4_min.dev_attr.attr,
13363d849981SJean Delvare 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1337d8d2ee07SJean Delvare 	NULL
1338d8d2ee07SJean Delvare };
1339d8d2ee07SJean Delvare 
1340d8d2ee07SJean Delvare static struct attribute *in5_attrs[] = {
13413d849981SJean Delvare 	&sensor_dev_attr_in5_input.dev_attr.attr,
13423d849981SJean Delvare 	&sensor_dev_attr_in5_max.dev_attr.attr,
13433d849981SJean Delvare 	&sensor_dev_attr_in5_min.dev_attr.attr,
13443d849981SJean Delvare 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
13453d849981SJean Delvare 	NULL
13463d849981SJean Delvare };
13473d849981SJean Delvare 
134854fe4671SJean Delvare static struct attribute *vid_attrs[] = {
134954fe4671SJean Delvare 	&dev_attr_cpu0_vid.attr,
135054fe4671SJean Delvare 	&dev_attr_vrm.attr,
135154fe4671SJean Delvare 	NULL
135254fe4671SJean Delvare };
135354fe4671SJean Delvare 
1354f5397be8SArvind Yadav static const struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1355f5397be8SArvind Yadav static const struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1356f5397be8SArvind Yadav static const struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1357f5397be8SArvind Yadav static const struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1358f5397be8SArvind Yadav static const struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1359f5397be8SArvind Yadav static const struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1360f5397be8SArvind Yadav static const struct attribute_group in5_attr_group = { .attrs = in5_attrs };
1361f5397be8SArvind Yadav static const struct attribute_group vid_attr_group = { .attrs = vid_attrs };
13621c301fc5SJordan Crouse 
1363310ec792SJean Delvare static int adt7475_detect(struct i2c_client *client,
13641c301fc5SJordan Crouse 			  struct i2c_board_info *info)
13651c301fc5SJordan Crouse {
13661c301fc5SJordan Crouse 	struct i2c_adapter *adapter = client->adapter;
1367d656b6fdSJean Delvare 	int vendid, devid, devid2;
1368b180d050SJean Delvare 	const char *name;
13691c301fc5SJordan Crouse 
13701c301fc5SJordan Crouse 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
13711c301fc5SJordan Crouse 		return -ENODEV;
13721c301fc5SJordan Crouse 
1373b180d050SJean Delvare 	vendid = adt7475_read(REG_VENDID);
1374d656b6fdSJean Delvare 	devid2 = adt7475_read(REG_DEVID2);
1375d656b6fdSJean Delvare 	if (vendid != 0x41 ||		/* Analog Devices */
1376d656b6fdSJean Delvare 	    (devid2 & 0xf8) != 0x68)
1377d656b6fdSJean Delvare 		return -ENODEV;
1378b180d050SJean Delvare 
1379d656b6fdSJean Delvare 	devid = adt7475_read(REG_DEVID);
1380d656b6fdSJean Delvare 	if (devid == 0x73)
1381b180d050SJean Delvare 		name = "adt7473";
1382d656b6fdSJean Delvare 	else if (devid == 0x75 && client->addr == 0x2e)
1383b180d050SJean Delvare 		name = "adt7475";
1384d8d2ee07SJean Delvare 	else if (devid == 0x76)
1385d8d2ee07SJean Delvare 		name = "adt7476";
13863d849981SJean Delvare 	else if ((devid2 & 0xfc) == 0x6c)
13873d849981SJean Delvare 		name = "adt7490";
1388b180d050SJean Delvare 	else {
1389b180d050SJean Delvare 		dev_dbg(&adapter->dev,
1390d8d2ee07SJean Delvare 			"Couldn't detect an ADT7473/75/76/90 part at "
1391b180d050SJean Delvare 			"0x%02x\n", (unsigned int)client->addr);
13921c301fc5SJordan Crouse 		return -ENODEV;
13931c301fc5SJordan Crouse 	}
13941c301fc5SJordan Crouse 
1395b180d050SJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
13961c301fc5SJordan Crouse 
13971c301fc5SJordan Crouse 	return 0;
13981c301fc5SJordan Crouse }
13991c301fc5SJordan Crouse 
14000f14480bSJean Delvare static void adt7475_remove_files(struct i2c_client *client,
14010f14480bSJean Delvare 				 struct adt7475_data *data)
14020f14480bSJean Delvare {
14030f14480bSJean Delvare 	sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1404378933c9SJean Delvare 	if (data->has_fan4)
1405378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1406378933c9SJean Delvare 	if (data->has_pwm2)
1407378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1408378933c9SJean Delvare 	if (data->has_voltage & (1 << 0))
1409378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1410d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3))
1411d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1412d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4))
1413d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1414d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5))
1415d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
141654fe4671SJean Delvare 	if (data->has_vid)
141754fe4671SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
14180f14480bSJean Delvare }
14190f14480bSJean Delvare 
1420702afeadSTokunori Ikegami static int adt7475_update_limits(struct i2c_client *client)
14215cf943edSTokunori Ikegami {
14225cf943edSTokunori Ikegami 	struct adt7475_data *data = i2c_get_clientdata(client);
14235cf943edSTokunori Ikegami 	int i;
1424702afeadSTokunori Ikegami 	int ret;
14255cf943edSTokunori Ikegami 
1426702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG4);
1427702afeadSTokunori Ikegami 	if (ret < 0)
1428702afeadSTokunori Ikegami 		return ret;
1429702afeadSTokunori Ikegami 	data->config4 = ret;
1430702afeadSTokunori Ikegami 
1431702afeadSTokunori Ikegami 	ret = adt7475_read(REG_CONFIG5);
1432702afeadSTokunori Ikegami 	if (ret < 0)
1433702afeadSTokunori Ikegami 		return ret;
1434702afeadSTokunori Ikegami 	data->config5 = ret;
14355cf943edSTokunori Ikegami 
14365cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
14375cf943edSTokunori Ikegami 		if (!(data->has_voltage & (1 << i)))
14385cf943edSTokunori Ikegami 			continue;
14395cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1440702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MIN_REG(i));
1441702afeadSTokunori Ikegami 		if (ret < 0)
1442702afeadSTokunori Ikegami 			return ret;
1443702afeadSTokunori Ikegami 		data->voltage[MIN][i] = ret << 2;
1444702afeadSTokunori Ikegami 
1445702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_MAX_REG(i));
1446702afeadSTokunori Ikegami 		if (ret < 0)
1447702afeadSTokunori Ikegami 			return ret;
1448702afeadSTokunori Ikegami 		data->voltage[MAX][i] = ret << 2;
14495cf943edSTokunori Ikegami 	}
14505cf943edSTokunori Ikegami 
14515cf943edSTokunori Ikegami 	if (data->has_voltage & (1 << 5)) {
1452702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MIN);
1453702afeadSTokunori Ikegami 		if (ret < 0)
1454702afeadSTokunori Ikegami 			return ret;
1455702afeadSTokunori Ikegami 		data->voltage[MIN][5] = ret << 2;
1456702afeadSTokunori Ikegami 
1457702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT_MAX);
1458702afeadSTokunori Ikegami 		if (ret < 0)
1459702afeadSTokunori Ikegami 			return ret;
1460702afeadSTokunori Ikegami 		data->voltage[MAX][5] = ret << 2;
14615cf943edSTokunori Ikegami 	}
14625cf943edSTokunori Ikegami 
14635cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
14645cf943edSTokunori Ikegami 		/* Adjust values so they match the input precision */
1465702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MIN_REG(i));
1466702afeadSTokunori Ikegami 		if (ret < 0)
1467702afeadSTokunori Ikegami 			return ret;
1468702afeadSTokunori Ikegami 		data->temp[MIN][i] = ret << 2;
1469702afeadSTokunori Ikegami 
1470702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_MAX_REG(i));
1471702afeadSTokunori Ikegami 		if (ret < 0)
1472702afeadSTokunori Ikegami 			return ret;
1473702afeadSTokunori Ikegami 		data->temp[MAX][i] = ret << 2;
1474702afeadSTokunori Ikegami 
1475702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_TMIN_REG(i));
1476702afeadSTokunori Ikegami 		if (ret < 0)
1477702afeadSTokunori Ikegami 			return ret;
1478702afeadSTokunori Ikegami 		data->temp[AUTOMIN][i] = ret << 2;
1479702afeadSTokunori Ikegami 
1480702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_THERM_REG(i));
1481702afeadSTokunori Ikegami 		if (ret < 0)
1482702afeadSTokunori Ikegami 			return ret;
1483702afeadSTokunori Ikegami 		data->temp[THERM][i] = ret << 2;
1484702afeadSTokunori Ikegami 
1485702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_OFFSET_REG(i));
1486702afeadSTokunori Ikegami 		if (ret < 0)
1487702afeadSTokunori Ikegami 			return ret;
1488702afeadSTokunori Ikegami 		data->temp[OFFSET][i] = ret;
14895cf943edSTokunori Ikegami 	}
14905cf943edSTokunori Ikegami 	adt7475_read_hystersis(client);
14915cf943edSTokunori Ikegami 
14925cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
14935cf943edSTokunori Ikegami 		if (i == 3 && !data->has_fan4)
14945cf943edSTokunori Ikegami 			continue;
1495702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_MIN_REG(i));
1496702afeadSTokunori Ikegami 		if (ret < 0)
1497702afeadSTokunori Ikegami 			return ret;
1498702afeadSTokunori Ikegami 		data->tach[MIN][i] = ret;
14995cf943edSTokunori Ikegami 	}
15005cf943edSTokunori Ikegami 
15015cf943edSTokunori Ikegami 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
15025cf943edSTokunori Ikegami 		if (i == 1 && !data->has_pwm2)
15035cf943edSTokunori Ikegami 			continue;
1504702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MAX_REG(i));
1505702afeadSTokunori Ikegami 		if (ret < 0)
1506702afeadSTokunori Ikegami 			return ret;
1507702afeadSTokunori Ikegami 		data->pwm[MAX][i] = ret;
1508702afeadSTokunori Ikegami 
1509702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_MIN_REG(i));
1510702afeadSTokunori Ikegami 		if (ret < 0)
1511702afeadSTokunori Ikegami 			return ret;
1512702afeadSTokunori Ikegami 		data->pwm[MIN][i] = ret;
15135cf943edSTokunori Ikegami 		/* Set the channel and control information */
15145cf943edSTokunori Ikegami 		adt7475_read_pwm(client, i);
15155cf943edSTokunori Ikegami 	}
15165cf943edSTokunori Ikegami 
1517702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(0));
1518702afeadSTokunori Ikegami 	if (ret < 0)
1519702afeadSTokunori Ikegami 		return ret;
1520702afeadSTokunori Ikegami 	data->range[0] = ret;
1521702afeadSTokunori Ikegami 
1522702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(1));
1523702afeadSTokunori Ikegami 	if (ret < 0)
1524702afeadSTokunori Ikegami 		return ret;
1525702afeadSTokunori Ikegami 	data->range[1] = ret;
1526702afeadSTokunori Ikegami 
1527702afeadSTokunori Ikegami 	ret = adt7475_read(TEMP_TRANGE_REG(2));
1528702afeadSTokunori Ikegami 	if (ret < 0)
1529702afeadSTokunori Ikegami 		return ret;
1530702afeadSTokunori Ikegami 	data->range[2] = ret;
1531702afeadSTokunori Ikegami 
1532702afeadSTokunori Ikegami 	return 0;
15335cf943edSTokunori Ikegami }
15345cf943edSTokunori Ikegami 
15351c301fc5SJordan Crouse static int adt7475_probe(struct i2c_client *client,
15361c301fc5SJordan Crouse 			 const struct i2c_device_id *id)
15371c301fc5SJordan Crouse {
15384e2496e4SJavier Martinez Canillas 	enum chips chip;
153999b8c83aSFrans Meulenbroeks 	static const char * const names[] = {
1540d07ca4adSJean Delvare 		[adt7473] = "ADT7473",
1541d07ca4adSJean Delvare 		[adt7475] = "ADT7475",
1542d8d2ee07SJean Delvare 		[adt7476] = "ADT7476",
1543d07ca4adSJean Delvare 		[adt7490] = "ADT7490",
1544d07ca4adSJean Delvare 	};
1545d07ca4adSJean Delvare 
15461c301fc5SJordan Crouse 	struct adt7475_data *data;
1547378933c9SJean Delvare 	int i, ret = 0, revision;
1548ebfaf1fbSJean Delvare 	u8 config2, config3;
15491c301fc5SJordan Crouse 
1550e3ecb2eeSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
15511c301fc5SJordan Crouse 	if (data == NULL)
15521c301fc5SJordan Crouse 		return -ENOMEM;
15531c301fc5SJordan Crouse 
15541c301fc5SJordan Crouse 	mutex_init(&data->lock);
15551c301fc5SJordan Crouse 	i2c_set_clientdata(client, data);
15561c301fc5SJordan Crouse 
15574e2496e4SJavier Martinez Canillas 	if (client->dev.of_node)
15584e2496e4SJavier Martinez Canillas 		chip = (enum chips)of_device_get_match_data(&client->dev);
15594e2496e4SJavier Martinez Canillas 	else
15604e2496e4SJavier Martinez Canillas 		chip = id->driver_data;
15614e2496e4SJavier Martinez Canillas 
1562cffb9dd0SJean Delvare 	/* Initialize device-specific values */
15634e2496e4SJavier Martinez Canillas 	switch (chip) {
1564d8d2ee07SJean Delvare 	case adt7476:
1565d8d2ee07SJean Delvare 		data->has_voltage = 0x0e;	/* in1 to in3 */
1566d8d2ee07SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1567d8d2ee07SJean Delvare 		break;
15683d849981SJean Delvare 	case adt7490:
1569378933c9SJean Delvare 		data->has_voltage = 0x3e;	/* in1 to in5 */
1570378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x03;
1571d07ca4adSJean Delvare 		if (revision == 0x03)
1572d07ca4adSJean Delvare 			revision += adt7475_read(REG_DEVREV2);
15733d849981SJean Delvare 		break;
1574cffb9dd0SJean Delvare 	default:
1575cffb9dd0SJean Delvare 		data->has_voltage = 0x06;	/* in1, in2 */
1576378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1577378933c9SJean Delvare 	}
1578378933c9SJean Delvare 
1579378933c9SJean Delvare 	config3 = adt7475_read(REG_CONFIG3);
1580378933c9SJean Delvare 	/* Pin PWM2 may alternatively be used for ALERT output */
1581378933c9SJean Delvare 	if (!(config3 & CONFIG3_SMBALERT))
1582378933c9SJean Delvare 		data->has_pwm2 = 1;
1583378933c9SJean Delvare 	/* Meaning of this bit is inverted for the ADT7473-1 */
1584378933c9SJean Delvare 	if (id->driver_data == adt7473 && revision >= 1)
1585378933c9SJean Delvare 		data->has_pwm2 = !data->has_pwm2;
1586378933c9SJean Delvare 
1587378933c9SJean Delvare 	data->config4 = adt7475_read(REG_CONFIG4);
1588378933c9SJean Delvare 	/* Pin TACH4 may alternatively be used for THERM */
1589378933c9SJean Delvare 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1590378933c9SJean Delvare 		data->has_fan4 = 1;
1591378933c9SJean Delvare 
15929ed5bc24SGuenter Roeck 	/*
15939ed5bc24SGuenter Roeck 	 * THERM configuration is more complex on the ADT7476 and ADT7490,
15949ed5bc24SGuenter Roeck 	 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
15959ed5bc24SGuenter Roeck 	 * this function
15969ed5bc24SGuenter Roeck 	 */
1597378933c9SJean Delvare 	if (id->driver_data == adt7490) {
1598378933c9SJean Delvare 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1599378933c9SJean Delvare 		    !(config3 & CONFIG3_THERM))
1600378933c9SJean Delvare 			data->has_fan4 = 1;
1601d8d2ee07SJean Delvare 	}
1602d8d2ee07SJean Delvare 	if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1603378933c9SJean Delvare 		if (!(config3 & CONFIG3_THERM) ||
1604378933c9SJean Delvare 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1605378933c9SJean Delvare 			data->has_voltage |= (1 << 0);		/* in0 */
1606cffb9dd0SJean Delvare 	}
1607cffb9dd0SJean Delvare 
16089ed5bc24SGuenter Roeck 	/*
16099ed5bc24SGuenter Roeck 	 * On the ADT7476, the +12V input pin may instead be used as VID5,
16109ed5bc24SGuenter Roeck 	 * and VID pins may alternatively be used as GPIO
16119ed5bc24SGuenter Roeck 	 */
1612d8d2ee07SJean Delvare 	if (id->driver_data == adt7476) {
1613d8d2ee07SJean Delvare 		u8 vid = adt7475_read(REG_VID);
1614d8d2ee07SJean Delvare 		if (!(vid & VID_VIDSEL))
1615d8d2ee07SJean Delvare 			data->has_voltage |= (1 << 4);		/* in4 */
161654fe4671SJean Delvare 
161754fe4671SJean Delvare 		data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1618d8d2ee07SJean Delvare 	}
1619d8d2ee07SJean Delvare 
1620ebfaf1fbSJean Delvare 	/* Voltage attenuators can be bypassed, globally or individually */
1621ebfaf1fbSJean Delvare 	config2 = adt7475_read(REG_CONFIG2);
1622ebfaf1fbSJean Delvare 	if (config2 & CONFIG2_ATTN) {
1623ebfaf1fbSJean Delvare 		data->bypass_attn = (0x3 << 3) | 0x3;
1624ebfaf1fbSJean Delvare 	} else {
1625ebfaf1fbSJean Delvare 		data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1626ebfaf1fbSJean Delvare 				    ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1627ebfaf1fbSJean Delvare 	}
1628ebfaf1fbSJean Delvare 	data->bypass_attn &= data->has_voltage;
1629ebfaf1fbSJean Delvare 
16309ed5bc24SGuenter Roeck 	/*
16319ed5bc24SGuenter Roeck 	 * Call adt7475_read_pwm for all pwm's as this will reprogram any
16329ed5bc24SGuenter Roeck 	 * pwm's which are disabled to manual mode with 0% duty cycle
16339ed5bc24SGuenter Roeck 	 */
16341c301fc5SJordan Crouse 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
16351c301fc5SJordan Crouse 		adt7475_read_pwm(client, i);
16361c301fc5SJordan Crouse 
16374abdf38dSChris Packham 	/* Start monitoring */
16384abdf38dSChris Packham 	switch (chip) {
16394abdf38dSChris Packham 	case adt7475:
16404abdf38dSChris Packham 	case adt7476:
16414abdf38dSChris Packham 		i2c_smbus_write_byte_data(client, REG_CONFIG1,
16424abdf38dSChris Packham 					  adt7475_read(REG_CONFIG1) | 0x01);
16434abdf38dSChris Packham 		break;
16444abdf38dSChris Packham 	default:
16454abdf38dSChris Packham 		break;
16464abdf38dSChris Packham 	}
16474abdf38dSChris Packham 
16481c301fc5SJordan Crouse 	ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
16491c301fc5SJordan Crouse 	if (ret)
1650e3ecb2eeSGuenter Roeck 		return ret;
16511c301fc5SJordan Crouse 
1652378933c9SJean Delvare 	/* Features that can be disabled individually */
1653378933c9SJean Delvare 	if (data->has_fan4) {
1654378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1655378933c9SJean Delvare 		if (ret)
1656378933c9SJean Delvare 			goto eremove;
1657378933c9SJean Delvare 	}
1658378933c9SJean Delvare 	if (data->has_pwm2) {
1659378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1660378933c9SJean Delvare 		if (ret)
1661378933c9SJean Delvare 			goto eremove;
1662378933c9SJean Delvare 	}
1663378933c9SJean Delvare 	if (data->has_voltage & (1 << 0)) {
1664378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1665378933c9SJean Delvare 		if (ret)
1666378933c9SJean Delvare 			goto eremove;
1667378933c9SJean Delvare 	}
1668d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3)) {
1669d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1670d8d2ee07SJean Delvare 		if (ret)
1671d8d2ee07SJean Delvare 			goto eremove;
1672d8d2ee07SJean Delvare 	}
1673d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4)) {
1674d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1675d8d2ee07SJean Delvare 		if (ret)
1676d8d2ee07SJean Delvare 			goto eremove;
1677d8d2ee07SJean Delvare 	}
1678d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1679d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1680d8d2ee07SJean Delvare 		if (ret)
1681d8d2ee07SJean Delvare 			goto eremove;
1682d8d2ee07SJean Delvare 	}
168354fe4671SJean Delvare 	if (data->has_vid) {
168454fe4671SJean Delvare 		data->vrm = vid_which_vrm();
168554fe4671SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
168654fe4671SJean Delvare 		if (ret)
168754fe4671SJean Delvare 			goto eremove;
168854fe4671SJean Delvare 	}
1689378933c9SJean Delvare 
16901c301fc5SJordan Crouse 	data->hwmon_dev = hwmon_device_register(&client->dev);
16911c301fc5SJordan Crouse 	if (IS_ERR(data->hwmon_dev)) {
16921c301fc5SJordan Crouse 		ret = PTR_ERR(data->hwmon_dev);
16931c301fc5SJordan Crouse 		goto eremove;
16941c301fc5SJordan Crouse 	}
16951c301fc5SJordan Crouse 
1696d07ca4adSJean Delvare 	dev_info(&client->dev, "%s device, revision %d\n",
1697d07ca4adSJean Delvare 		 names[id->driver_data], revision);
1698d8d2ee07SJean Delvare 	if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
169954fe4671SJean Delvare 		dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1700d07ca4adSJean Delvare 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1701d8d2ee07SJean Delvare 			 (data->has_voltage & (1 << 4)) ? " in4" : "",
1702d07ca4adSJean Delvare 			 data->has_fan4 ? " fan4" : "",
170354fe4671SJean Delvare 			 data->has_pwm2 ? " pwm2" : "",
170454fe4671SJean Delvare 			 data->has_vid ? " vid" : "");
1705ebfaf1fbSJean Delvare 	if (data->bypass_attn)
1706ebfaf1fbSJean Delvare 		dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1707ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1708ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1709ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1710ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1711d07ca4adSJean Delvare 
17125cf943edSTokunori Ikegami 	/* Limits and settings, should never change update more than once */
17134afec79fSTokunori Ikegami 	ret = adt7475_update_limits(client);
17144afec79fSTokunori Ikegami 	if (ret)
17154afec79fSTokunori Ikegami 		goto eremove;
17165cf943edSTokunori Ikegami 
17171c301fc5SJordan Crouse 	return 0;
17181c301fc5SJordan Crouse 
17191c301fc5SJordan Crouse eremove:
17200f14480bSJean Delvare 	adt7475_remove_files(client, data);
17211c301fc5SJordan Crouse 	return ret;
17221c301fc5SJordan Crouse }
17231c301fc5SJordan Crouse 
17241c301fc5SJordan Crouse static int adt7475_remove(struct i2c_client *client)
17251c301fc5SJordan Crouse {
17261c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17271c301fc5SJordan Crouse 
17281c301fc5SJordan Crouse 	hwmon_device_unregister(data->hwmon_dev);
17290f14480bSJean Delvare 	adt7475_remove_files(client, data);
17301c301fc5SJordan Crouse 
17311c301fc5SJordan Crouse 	return 0;
17321c301fc5SJordan Crouse }
17331c301fc5SJordan Crouse 
17341c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = {
17351c301fc5SJordan Crouse 	.class		= I2C_CLASS_HWMON,
17361c301fc5SJordan Crouse 	.driver = {
17371c301fc5SJordan Crouse 		.name	= "adt7475",
17384e2496e4SJavier Martinez Canillas 		.of_match_table = of_match_ptr(adt7475_of_match),
17391c301fc5SJordan Crouse 	},
17401c301fc5SJordan Crouse 	.probe		= adt7475_probe,
17411c301fc5SJordan Crouse 	.remove		= adt7475_remove,
17421c301fc5SJordan Crouse 	.id_table	= adt7475_id,
17431c301fc5SJordan Crouse 	.detect		= adt7475_detect,
1744c3813d6aSJean Delvare 	.address_list	= normal_i2c,
17451c301fc5SJordan Crouse };
17461c301fc5SJordan Crouse 
17471c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client)
17481c301fc5SJordan Crouse {
17491c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17501c301fc5SJordan Crouse 
17511c301fc5SJordan Crouse 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
17521c301fc5SJordan Crouse 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
17531c301fc5SJordan Crouse 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
17541c301fc5SJordan Crouse }
17551c301fc5SJordan Crouse 
17561c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index)
17571c301fc5SJordan Crouse {
17581c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
17591c301fc5SJordan Crouse 	unsigned int v;
17601c301fc5SJordan Crouse 
17611c301fc5SJordan Crouse 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
17621c301fc5SJordan Crouse 
17639ed5bc24SGuenter Roeck 	/*
17649ed5bc24SGuenter Roeck 	 * Figure out the internal value for pwmctrl and pwmchan
17659ed5bc24SGuenter Roeck 	 * based on the current settings
17669ed5bc24SGuenter Roeck 	 */
17671c301fc5SJordan Crouse 	v = (data->pwm[CONTROL][index] >> 5) & 7;
17681c301fc5SJordan Crouse 
17691c301fc5SJordan Crouse 	if (v == 3)
17701c301fc5SJordan Crouse 		data->pwmctl[index] = 0;
17711c301fc5SJordan Crouse 	else if (v == 7)
17721c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
17731c301fc5SJordan Crouse 	else if (v == 4) {
17749ed5bc24SGuenter Roeck 		/*
17759ed5bc24SGuenter Roeck 		 * The fan is disabled - we don't want to
17769ed5bc24SGuenter Roeck 		 * support that, so change to manual mode and
17779ed5bc24SGuenter Roeck 		 * set the duty cycle to 0 instead
17781c301fc5SJordan Crouse 		 */
17791c301fc5SJordan Crouse 		data->pwm[INPUT][index] = 0;
17801c301fc5SJordan Crouse 		data->pwm[CONTROL][index] &= ~0xE0;
17811c301fc5SJordan Crouse 		data->pwm[CONTROL][index] |= (7 << 5);
17821c301fc5SJordan Crouse 
17831c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
17841c301fc5SJordan Crouse 					  data->pwm[INPUT][index]);
17851c301fc5SJordan Crouse 
17861c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
17871c301fc5SJordan Crouse 					  data->pwm[CONTROL][index]);
17881c301fc5SJordan Crouse 
17891c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
17901c301fc5SJordan Crouse 	} else {
17911c301fc5SJordan Crouse 		data->pwmctl[index] = 2;
17921c301fc5SJordan Crouse 
17931c301fc5SJordan Crouse 		switch (v) {
17941c301fc5SJordan Crouse 		case 0:
17951c301fc5SJordan Crouse 			data->pwmchan[index] = 1;
17961c301fc5SJordan Crouse 			break;
17971c301fc5SJordan Crouse 		case 1:
17981c301fc5SJordan Crouse 			data->pwmchan[index] = 2;
17991c301fc5SJordan Crouse 			break;
18001c301fc5SJordan Crouse 		case 2:
18011c301fc5SJordan Crouse 			data->pwmchan[index] = 4;
18021c301fc5SJordan Crouse 			break;
18031c301fc5SJordan Crouse 		case 5:
18041c301fc5SJordan Crouse 			data->pwmchan[index] = 6;
18051c301fc5SJordan Crouse 			break;
18061c301fc5SJordan Crouse 		case 6:
18071c301fc5SJordan Crouse 			data->pwmchan[index] = 7;
18081c301fc5SJordan Crouse 			break;
18091c301fc5SJordan Crouse 		}
18101c301fc5SJordan Crouse 	}
18111c301fc5SJordan Crouse }
18121c301fc5SJordan Crouse 
1813702afeadSTokunori Ikegami static int adt7475_update_measure(struct device *dev)
18141c301fc5SJordan Crouse {
18151c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
18161c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
18173d849981SJean Delvare 	u16 ext;
18181c301fc5SJordan Crouse 	int i;
1819702afeadSTokunori Ikegami 	int ret;
18201c301fc5SJordan Crouse 
1821702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS2);
1822702afeadSTokunori Ikegami 	if (ret < 0)
1823702afeadSTokunori Ikegami 		return ret;
1824702afeadSTokunori Ikegami 	data->alarms = ret << 8;
18251c301fc5SJordan Crouse 
1826702afeadSTokunori Ikegami 	ret = adt7475_read(REG_STATUS1);
1827702afeadSTokunori Ikegami 	if (ret < 0)
1828702afeadSTokunori Ikegami 		return ret;
1829702afeadSTokunori Ikegami 	data->alarms |= ret;
1830702afeadSTokunori Ikegami 
1831702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND2);
1832702afeadSTokunori Ikegami 	if (ret < 0)
1833702afeadSTokunori Ikegami 		return ret;
1834702afeadSTokunori Ikegami 
1835702afeadSTokunori Ikegami 	ext = (ret << 8);
1836702afeadSTokunori Ikegami 
1837702afeadSTokunori Ikegami 	ret = adt7475_read(REG_EXTEND1);
1838702afeadSTokunori Ikegami 	if (ret < 0)
1839702afeadSTokunori Ikegami 		return ret;
1840702afeadSTokunori Ikegami 
1841702afeadSTokunori Ikegami 	ext |= ret;
1842702afeadSTokunori Ikegami 
1843cffb9dd0SJean Delvare 	for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1844cffb9dd0SJean Delvare 		if (!(data->has_voltage & (1 << i)))
1845cffb9dd0SJean Delvare 			continue;
1846702afeadSTokunori Ikegami 		ret = adt7475_read(VOLTAGE_REG(i));
1847702afeadSTokunori Ikegami 		if (ret < 0)
1848702afeadSTokunori Ikegami 			return ret;
18491c301fc5SJordan Crouse 		data->voltage[INPUT][i] =
1850702afeadSTokunori Ikegami 			(ret << 2) |
1851cffb9dd0SJean Delvare 			((ext >> (i * 2)) & 3);
1852cffb9dd0SJean Delvare 	}
18531c301fc5SJordan Crouse 
1854702afeadSTokunori Ikegami 	for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1855702afeadSTokunori Ikegami 		ret = adt7475_read(TEMP_REG(i));
1856702afeadSTokunori Ikegami 		if (ret < 0)
1857702afeadSTokunori Ikegami 			return ret;
18581c301fc5SJordan Crouse 		data->temp[INPUT][i] =
1859702afeadSTokunori Ikegami 			(ret << 2) |
18603d849981SJean Delvare 			((ext >> ((i + 5) * 2)) & 3);
1861702afeadSTokunori Ikegami 	}
18623d849981SJean Delvare 
18633d849981SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1864702afeadSTokunori Ikegami 		ret = adt7475_read(REG_STATUS4);
1865702afeadSTokunori Ikegami 		if (ret < 0)
1866702afeadSTokunori Ikegami 			return ret;
1867702afeadSTokunori Ikegami 		data->alarms |= ret << 24;
1868702afeadSTokunori Ikegami 
1869702afeadSTokunori Ikegami 		ret = adt7475_read(REG_EXTEND3);
1870702afeadSTokunori Ikegami 		if (ret < 0)
1871702afeadSTokunori Ikegami 			return ret;
1872702afeadSTokunori Ikegami 		ext = ret;
1873702afeadSTokunori Ikegami 
1874702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VTT);
1875702afeadSTokunori Ikegami 		if (ret < 0)
1876702afeadSTokunori Ikegami 			return ret;
1877702afeadSTokunori Ikegami 		data->voltage[INPUT][5] = ret << 2 |
18783d849981SJean Delvare 			((ext >> 4) & 3);
18793d849981SJean Delvare 	}
18801c301fc5SJordan Crouse 
1881378933c9SJean Delvare 	for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1882378933c9SJean Delvare 		if (i == 3 && !data->has_fan4)
1883378933c9SJean Delvare 			continue;
1884702afeadSTokunori Ikegami 		ret = adt7475_read_word(client, TACH_REG(i));
1885702afeadSTokunori Ikegami 		if (ret < 0)
1886702afeadSTokunori Ikegami 			return ret;
1887702afeadSTokunori Ikegami 		data->tach[INPUT][i] = ret;
1888378933c9SJean Delvare 	}
18891c301fc5SJordan Crouse 
18901c301fc5SJordan Crouse 	/* Updated by hw when in auto mode */
1891378933c9SJean Delvare 	for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1892378933c9SJean Delvare 		if (i == 1 && !data->has_pwm2)
1893378933c9SJean Delvare 			continue;
1894702afeadSTokunori Ikegami 		ret = adt7475_read(PWM_REG(i));
1895702afeadSTokunori Ikegami 		if (ret < 0)
1896702afeadSTokunori Ikegami 			return ret;
1897702afeadSTokunori Ikegami 		data->pwm[INPUT][i] = ret;
1898378933c9SJean Delvare 	}
18991c301fc5SJordan Crouse 
1900702afeadSTokunori Ikegami 	if (data->has_vid) {
1901702afeadSTokunori Ikegami 		ret = adt7475_read(REG_VID);
1902702afeadSTokunori Ikegami 		if (ret < 0)
1903702afeadSTokunori Ikegami 			return ret;
1904702afeadSTokunori Ikegami 		data->vid = ret & 0x3f;
1905702afeadSTokunori Ikegami 	}
1906702afeadSTokunori Ikegami 
1907702afeadSTokunori Ikegami 	return 0;
19081c301fc5SJordan Crouse }
19091c301fc5SJordan Crouse 
19105cf943edSTokunori Ikegami static struct adt7475_data *adt7475_update_device(struct device *dev)
19115cf943edSTokunori Ikegami {
19125cf943edSTokunori Ikegami 	struct i2c_client *client = to_i2c_client(dev);
19135cf943edSTokunori Ikegami 	struct adt7475_data *data = i2c_get_clientdata(client);
19144afec79fSTokunori Ikegami 	int ret;
19155cf943edSTokunori Ikegami 
19165cf943edSTokunori Ikegami 	mutex_lock(&data->lock);
19175cf943edSTokunori Ikegami 
19185cf943edSTokunori Ikegami 	/* Measurement values update every 2 seconds */
19195cf943edSTokunori Ikegami 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
19201c301fc5SJordan Crouse 	    !data->valid) {
19214afec79fSTokunori Ikegami 		ret = adt7475_update_measure(dev);
19224afec79fSTokunori Ikegami 		if (ret) {
19234afec79fSTokunori Ikegami 			data->valid = false;
19244afec79fSTokunori Ikegami 			mutex_unlock(&data->lock);
19254afec79fSTokunori Ikegami 			return ERR_PTR(ret);
19264afec79fSTokunori Ikegami 		}
19275cf943edSTokunori Ikegami 		data->measure_updated = jiffies;
1928b36fb171STokunori Ikegami 		data->valid = true;
19291c301fc5SJordan Crouse 	}
19301c301fc5SJordan Crouse 
19311c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
19321c301fc5SJordan Crouse 
19331c301fc5SJordan Crouse 	return data;
19341c301fc5SJordan Crouse }
19351c301fc5SJordan Crouse 
1936f0967eeaSAxel Lin module_i2c_driver(adt7475_driver);
19371c301fc5SJordan Crouse 
19381c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc");
19391c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver");
19401c301fc5SJordan Crouse MODULE_LICENSE("GPL");
1941