xref: /linux/drivers/hwmon/adt7475.c (revision 4abdf38d206fdc22f7a3f7bf79eef78734ec84ab)
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>
251c301fc5SJordan Crouse 
261c301fc5SJordan Crouse /* Indexes for the sysfs hooks */
271c301fc5SJordan Crouse 
281c301fc5SJordan Crouse #define INPUT		0
291c301fc5SJordan Crouse #define MIN		1
301c301fc5SJordan Crouse #define MAX		2
311c301fc5SJordan Crouse #define CONTROL		3
321c301fc5SJordan Crouse #define OFFSET		3
331c301fc5SJordan Crouse #define AUTOMIN		4
341c301fc5SJordan Crouse #define THERM		5
351c301fc5SJordan Crouse #define HYSTERSIS	6
361c301fc5SJordan Crouse 
379ed5bc24SGuenter Roeck /*
389ed5bc24SGuenter Roeck  * These are unique identifiers for the sysfs functions - unlike the
399ed5bc24SGuenter Roeck  * numbers above, these are not also indexes into an array
401c301fc5SJordan Crouse  */
411c301fc5SJordan Crouse 
421c301fc5SJordan Crouse #define ALARM		9
431c301fc5SJordan Crouse #define FAULT		10
441c301fc5SJordan Crouse 
451c301fc5SJordan Crouse /* 7475 Common Registers */
461c301fc5SJordan Crouse 
47d07ca4adSJean Delvare #define REG_DEVREV2		0x12	/* ADT7490 only */
48d07ca4adSJean Delvare 
493d849981SJean Delvare #define REG_VTT			0x1E	/* ADT7490 only */
503d849981SJean Delvare #define REG_EXTEND3		0x1F	/* ADT7490 only */
513d849981SJean Delvare 
52cffb9dd0SJean Delvare #define REG_VOLTAGE_BASE	0x20
531c301fc5SJordan Crouse #define REG_TEMP_BASE		0x25
541c301fc5SJordan Crouse #define REG_TACH_BASE		0x28
551c301fc5SJordan Crouse #define REG_PWM_BASE		0x30
561c301fc5SJordan Crouse #define REG_PWM_MAX_BASE	0x38
571c301fc5SJordan Crouse 
581c301fc5SJordan Crouse #define REG_DEVID		0x3D
591c301fc5SJordan Crouse #define REG_VENDID		0x3E
60d656b6fdSJean Delvare #define REG_DEVID2		0x3F
611c301fc5SJordan Crouse 
62*4abdf38dSChris Packham #define REG_CONFIG1		0x40
63*4abdf38dSChris Packham 
641c301fc5SJordan Crouse #define REG_STATUS1		0x41
651c301fc5SJordan Crouse #define REG_STATUS2		0x42
661c301fc5SJordan Crouse 
67d8d2ee07SJean Delvare #define REG_VID			0x43	/* ADT7476 only */
68d8d2ee07SJean Delvare 
69cffb9dd0SJean Delvare #define REG_VOLTAGE_MIN_BASE	0x44
70cffb9dd0SJean Delvare #define REG_VOLTAGE_MAX_BASE	0x45
711c301fc5SJordan Crouse 
721c301fc5SJordan Crouse #define REG_TEMP_MIN_BASE	0x4E
731c301fc5SJordan Crouse #define REG_TEMP_MAX_BASE	0x4F
741c301fc5SJordan Crouse 
751c301fc5SJordan Crouse #define REG_TACH_MIN_BASE	0x54
761c301fc5SJordan Crouse 
771c301fc5SJordan Crouse #define REG_PWM_CONFIG_BASE	0x5C
781c301fc5SJordan Crouse 
791c301fc5SJordan Crouse #define REG_TEMP_TRANGE_BASE	0x5F
801c301fc5SJordan Crouse 
811c301fc5SJordan Crouse #define REG_PWM_MIN_BASE	0x64
821c301fc5SJordan Crouse 
831c301fc5SJordan Crouse #define REG_TEMP_TMIN_BASE	0x67
841c301fc5SJordan Crouse #define REG_TEMP_THERM_BASE	0x6A
851c301fc5SJordan Crouse 
861c301fc5SJordan Crouse #define REG_REMOTE1_HYSTERSIS	0x6D
871c301fc5SJordan Crouse #define REG_REMOTE2_HYSTERSIS	0x6E
881c301fc5SJordan Crouse 
891c301fc5SJordan Crouse #define REG_TEMP_OFFSET_BASE	0x70
901c301fc5SJordan Crouse 
91ebfaf1fbSJean Delvare #define REG_CONFIG2		0x73
92ebfaf1fbSJean Delvare 
931c301fc5SJordan Crouse #define REG_EXTEND1		0x76
941c301fc5SJordan Crouse #define REG_EXTEND2		0x77
95378933c9SJean Delvare 
96378933c9SJean Delvare #define REG_CONFIG3		0x78
971c301fc5SJordan Crouse #define REG_CONFIG5		0x7C
98f99318b2SJean Delvare #define REG_CONFIG4		0x7D
99f99318b2SJean Delvare 
1003d849981SJean Delvare #define REG_STATUS4		0x81	/* ADT7490 only */
1013d849981SJean Delvare 
1023d849981SJean Delvare #define REG_VTT_MIN		0x84	/* ADT7490 only */
1033d849981SJean Delvare #define REG_VTT_MAX		0x86	/* ADT7490 only */
1043d849981SJean Delvare 
105d8d2ee07SJean Delvare #define VID_VIDSEL		0x80	/* ADT7476 only */
106d8d2ee07SJean Delvare 
107ebfaf1fbSJean Delvare #define CONFIG2_ATTN		0x20
108ebfaf1fbSJean Delvare 
109378933c9SJean Delvare #define CONFIG3_SMBALERT	0x01
110378933c9SJean Delvare #define CONFIG3_THERM		0x02
111378933c9SJean Delvare 
112378933c9SJean Delvare #define CONFIG4_PINFUNC		0x03
113f99318b2SJean Delvare #define CONFIG4_MAXDUTY		0x08
114ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN10	0x30
115ebfaf1fbSJean Delvare #define CONFIG4_ATTN_IN43	0xC0
1161c301fc5SJordan Crouse 
1171c301fc5SJordan Crouse #define CONFIG5_TWOSCOMP	0x01
1181c301fc5SJordan Crouse #define CONFIG5_TEMPOFFSET	0x02
11954fe4671SJean Delvare #define CONFIG5_VIDGPIO		0x10	/* ADT7476 only */
1201c301fc5SJordan Crouse 
1211c301fc5SJordan Crouse /* ADT7475 Settings */
1221c301fc5SJordan Crouse 
123cffb9dd0SJean Delvare #define ADT7475_VOLTAGE_COUNT	5	/* Not counting Vtt */
1241c301fc5SJordan Crouse #define ADT7475_TEMP_COUNT	3
1251c301fc5SJordan Crouse #define ADT7475_TACH_COUNT	4
1261c301fc5SJordan Crouse #define ADT7475_PWM_COUNT	3
1271c301fc5SJordan Crouse 
1281c301fc5SJordan Crouse /* Macro to read the registers */
1291c301fc5SJordan Crouse 
1301c301fc5SJordan Crouse #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
1311c301fc5SJordan Crouse 
1321c301fc5SJordan Crouse /* Macros to easily index the registers */
1331c301fc5SJordan Crouse 
1341c301fc5SJordan Crouse #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
1351c301fc5SJordan Crouse #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
1361c301fc5SJordan Crouse 
1371c301fc5SJordan Crouse #define PWM_REG(idx) (REG_PWM_BASE + (idx))
1381c301fc5SJordan Crouse #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
1391c301fc5SJordan Crouse #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
1401c301fc5SJordan Crouse #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
1411c301fc5SJordan Crouse 
1421c301fc5SJordan Crouse #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
1431c301fc5SJordan Crouse #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
1441c301fc5SJordan Crouse #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
1451c301fc5SJordan Crouse 
1461c301fc5SJordan Crouse #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
1471c301fc5SJordan Crouse #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
1481c301fc5SJordan Crouse #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
1491c301fc5SJordan Crouse #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
1501c301fc5SJordan Crouse #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
1511c301fc5SJordan Crouse #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
1521c301fc5SJordan Crouse #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
1531c301fc5SJordan Crouse 
154918ee91cSJean Delvare static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1551c301fc5SJordan Crouse 
156e5e9f44cSJean Delvare enum chips { adt7473, adt7475, adt7476, adt7490 };
1571c301fc5SJordan Crouse 
1581c301fc5SJordan Crouse static const struct i2c_device_id adt7475_id[] = {
159b180d050SJean Delvare 	{ "adt7473", adt7473 },
1601c301fc5SJordan Crouse 	{ "adt7475", adt7475 },
161d8d2ee07SJean Delvare 	{ "adt7476", adt7476 },
1623d849981SJean Delvare 	{ "adt7490", adt7490 },
1631c301fc5SJordan Crouse 	{ }
1641c301fc5SJordan Crouse };
1651c301fc5SJordan Crouse MODULE_DEVICE_TABLE(i2c, adt7475_id);
1661c301fc5SJordan Crouse 
1674e2496e4SJavier Martinez Canillas static const struct of_device_id adt7475_of_match[] = {
1684e2496e4SJavier Martinez Canillas 	{
1694e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7473",
1704e2496e4SJavier Martinez Canillas 		.data = (void *)adt7473
1714e2496e4SJavier Martinez Canillas 	},
1724e2496e4SJavier Martinez Canillas 	{
1734e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7475",
1744e2496e4SJavier Martinez Canillas 		.data = (void *)adt7475
1754e2496e4SJavier Martinez Canillas 	},
1764e2496e4SJavier Martinez Canillas 	{
1774e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7476",
1784e2496e4SJavier Martinez Canillas 		.data = (void *)adt7476
1794e2496e4SJavier Martinez Canillas 	},
1804e2496e4SJavier Martinez Canillas 	{
1814e2496e4SJavier Martinez Canillas 		.compatible = "adi,adt7490",
1824e2496e4SJavier Martinez Canillas 		.data = (void *)adt7490
1834e2496e4SJavier Martinez Canillas 	},
1844e2496e4SJavier Martinez Canillas 	{ },
1854e2496e4SJavier Martinez Canillas };
1864e2496e4SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, adt7475_of_match);
1874e2496e4SJavier Martinez Canillas 
1881c301fc5SJordan Crouse struct adt7475_data {
1891c301fc5SJordan Crouse 	struct device *hwmon_dev;
1901c301fc5SJordan Crouse 	struct mutex lock;
1911c301fc5SJordan Crouse 
1921c301fc5SJordan Crouse 	unsigned long measure_updated;
1931c301fc5SJordan Crouse 	unsigned long limits_updated;
1941c301fc5SJordan Crouse 	char valid;
1951c301fc5SJordan Crouse 
196f99318b2SJean Delvare 	u8 config4;
1971c301fc5SJordan Crouse 	u8 config5;
198cffb9dd0SJean Delvare 	u8 has_voltage;
199ebfaf1fbSJean Delvare 	u8 bypass_attn;		/* Bypass voltage attenuator */
200378933c9SJean Delvare 	u8 has_pwm2:1;
201378933c9SJean Delvare 	u8 has_fan4:1;
20254fe4671SJean Delvare 	u8 has_vid:1;
2033d849981SJean Delvare 	u32 alarms;
204cffb9dd0SJean Delvare 	u16 voltage[3][6];
2051c301fc5SJordan Crouse 	u16 temp[7][3];
2061c301fc5SJordan Crouse 	u16 tach[2][4];
2071c301fc5SJordan Crouse 	u8 pwm[4][3];
2081c301fc5SJordan Crouse 	u8 range[3];
2091c301fc5SJordan Crouse 	u8 pwmctl[3];
2101c301fc5SJordan Crouse 	u8 pwmchan[3];
21154fe4671SJean Delvare 
21254fe4671SJean Delvare 	u8 vid;
21354fe4671SJean Delvare 	u8 vrm;
2141c301fc5SJordan Crouse };
2151c301fc5SJordan Crouse 
2161c301fc5SJordan Crouse static struct i2c_driver adt7475_driver;
2171c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev);
2181c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client);
2191c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index);
2201c301fc5SJordan Crouse 
2211c301fc5SJordan Crouse /* Given a temp value, convert it to register value */
2221c301fc5SJordan Crouse 
2231c301fc5SJordan Crouse static inline u16 temp2reg(struct adt7475_data *data, long val)
2241c301fc5SJordan Crouse {
2251c301fc5SJordan Crouse 	u16 ret;
2261c301fc5SJordan Crouse 
2271c301fc5SJordan Crouse 	if (!(data->config5 & CONFIG5_TWOSCOMP)) {
2282a844c14SGuenter Roeck 		val = clamp_val(val, -64000, 191000);
2291c301fc5SJordan Crouse 		ret = (val + 64500) / 1000;
2301c301fc5SJordan Crouse 	} else {
2312a844c14SGuenter Roeck 		val = clamp_val(val, -128000, 127000);
2321c301fc5SJordan Crouse 		if (val < -500)
2331c301fc5SJordan Crouse 			ret = (256500 + val) / 1000;
2341c301fc5SJordan Crouse 		else
2351c301fc5SJordan Crouse 			ret = (val + 500) / 1000;
2361c301fc5SJordan Crouse 	}
2371c301fc5SJordan Crouse 
2381c301fc5SJordan Crouse 	return ret << 2;
2391c301fc5SJordan Crouse }
2401c301fc5SJordan Crouse 
2411c301fc5SJordan Crouse /* Given a register value, convert it to a real temp value */
2421c301fc5SJordan Crouse 
2431c301fc5SJordan Crouse static inline int reg2temp(struct adt7475_data *data, u16 reg)
2441c301fc5SJordan Crouse {
2451c301fc5SJordan Crouse 	if (data->config5 & CONFIG5_TWOSCOMP) {
2461c301fc5SJordan Crouse 		if (reg >= 512)
2471c301fc5SJordan Crouse 			return (reg - 1024) * 250;
2481c301fc5SJordan Crouse 		else
2491c301fc5SJordan Crouse 			return reg * 250;
2501c301fc5SJordan Crouse 	} else
2511c301fc5SJordan Crouse 		return (reg - 256) * 250;
2521c301fc5SJordan Crouse }
2531c301fc5SJordan Crouse 
2541c301fc5SJordan Crouse static inline int tach2rpm(u16 tach)
2551c301fc5SJordan Crouse {
2561c301fc5SJordan Crouse 	if (tach == 0 || tach == 0xFFFF)
2571c301fc5SJordan Crouse 		return 0;
2581c301fc5SJordan Crouse 
2591c301fc5SJordan Crouse 	return (90000 * 60) / tach;
2601c301fc5SJordan Crouse }
2611c301fc5SJordan Crouse 
2621c301fc5SJordan Crouse static inline u16 rpm2tach(unsigned long rpm)
2631c301fc5SJordan Crouse {
2641c301fc5SJordan Crouse 	if (rpm == 0)
2651c301fc5SJordan Crouse 		return 0;
2661c301fc5SJordan Crouse 
2672a844c14SGuenter Roeck 	return clamp_val((90000 * 60) / rpm, 1, 0xFFFF);
2681c301fc5SJordan Crouse }
2691c301fc5SJordan Crouse 
270cffb9dd0SJean Delvare /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
271cffb9dd0SJean Delvare static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
272cffb9dd0SJean Delvare 	{ 45, 94 },	/* +2.5V */
273cffb9dd0SJean Delvare 	{ 175, 525 },	/* Vccp */
274cffb9dd0SJean Delvare 	{ 68, 71 },	/* Vcc */
275cffb9dd0SJean Delvare 	{ 93, 47 },	/* +5V */
276cffb9dd0SJean Delvare 	{ 120, 20 },	/* +12V */
277cffb9dd0SJean Delvare 	{ 45, 45 },	/* Vtt */
278cffb9dd0SJean Delvare };
279cffb9dd0SJean Delvare 
280ebfaf1fbSJean Delvare static inline int reg2volt(int channel, u16 reg, u8 bypass_attn)
2811c301fc5SJordan Crouse {
282cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
283cffb9dd0SJean Delvare 
284ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
285ebfaf1fbSJean Delvare 		return DIV_ROUND_CLOSEST(reg * 2250, 1024);
286cffb9dd0SJean Delvare 	return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
2871c301fc5SJordan Crouse }
2881c301fc5SJordan Crouse 
289ebfaf1fbSJean Delvare static inline u16 volt2reg(int channel, long volt, u8 bypass_attn)
2901c301fc5SJordan Crouse {
291cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
292cffb9dd0SJean Delvare 	long reg;
2931c301fc5SJordan Crouse 
294ebfaf1fbSJean Delvare 	if (bypass_attn & (1 << channel))
295ebfaf1fbSJean Delvare 		reg = (volt * 1024) / 2250;
296ebfaf1fbSJean Delvare 	else
297cffb9dd0SJean Delvare 		reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
2982a844c14SGuenter Roeck 	return clamp_val(reg, 0, 1023) & (0xff << 2);
2991c301fc5SJordan Crouse }
3001c301fc5SJordan Crouse 
3011c301fc5SJordan Crouse static u16 adt7475_read_word(struct i2c_client *client, int reg)
3021c301fc5SJordan Crouse {
3031c301fc5SJordan Crouse 	u16 val;
3041c301fc5SJordan Crouse 
3051c301fc5SJordan Crouse 	val = i2c_smbus_read_byte_data(client, reg);
3061c301fc5SJordan Crouse 	val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
3071c301fc5SJordan Crouse 
3081c301fc5SJordan Crouse 	return val;
3091c301fc5SJordan Crouse }
3101c301fc5SJordan Crouse 
3111c301fc5SJordan Crouse static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
3121c301fc5SJordan Crouse {
3131c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
3141c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, val & 0xFF);
3151c301fc5SJordan Crouse }
3161c301fc5SJordan Crouse 
3179ed5bc24SGuenter Roeck /*
3189ed5bc24SGuenter Roeck  * Find the nearest value in a table - used for pwm frequency and
3199ed5bc24SGuenter Roeck  * auto temp range
3209ed5bc24SGuenter Roeck  */
3211c301fc5SJordan Crouse static int find_nearest(long val, const int *array, int size)
3221c301fc5SJordan Crouse {
3231c301fc5SJordan Crouse 	int i;
3241c301fc5SJordan Crouse 
3251c301fc5SJordan Crouse 	if (val < array[0])
3261c301fc5SJordan Crouse 		return 0;
3271c301fc5SJordan Crouse 
3281c301fc5SJordan Crouse 	if (val > array[size - 1])
3291c301fc5SJordan Crouse 		return size - 1;
3301c301fc5SJordan Crouse 
3311c301fc5SJordan Crouse 	for (i = 0; i < size - 1; i++) {
3321c301fc5SJordan Crouse 		int a, b;
3331c301fc5SJordan Crouse 
3341c301fc5SJordan Crouse 		if (val > array[i + 1])
3351c301fc5SJordan Crouse 			continue;
3361c301fc5SJordan Crouse 
3371c301fc5SJordan Crouse 		a = val - array[i];
3381c301fc5SJordan Crouse 		b = array[i + 1] - val;
3391c301fc5SJordan Crouse 
3401c301fc5SJordan Crouse 		return (a <= b) ? i : i + 1;
3411c301fc5SJordan Crouse 	}
3421c301fc5SJordan Crouse 
3431c301fc5SJordan Crouse 	return 0;
3441c301fc5SJordan Crouse }
3451c301fc5SJordan Crouse 
3461c301fc5SJordan Crouse static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
3471c301fc5SJordan Crouse 			    char *buf)
3481c301fc5SJordan Crouse {
3491c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3501c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3511c301fc5SJordan Crouse 	unsigned short val;
3521c301fc5SJordan Crouse 
3531c301fc5SJordan Crouse 	switch (sattr->nr) {
3541c301fc5SJordan Crouse 	case ALARM:
3551c301fc5SJordan Crouse 		return sprintf(buf, "%d\n",
356cffb9dd0SJean Delvare 			       (data->alarms >> sattr->index) & 1);
3571c301fc5SJordan Crouse 	default:
3581c301fc5SJordan Crouse 		val = data->voltage[sattr->nr][sattr->index];
359ebfaf1fbSJean Delvare 		return sprintf(buf, "%d\n",
360ebfaf1fbSJean Delvare 			       reg2volt(sattr->index, val, data->bypass_attn));
3611c301fc5SJordan Crouse 	}
3621c301fc5SJordan Crouse }
3631c301fc5SJordan Crouse 
3641c301fc5SJordan Crouse static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
3651c301fc5SJordan Crouse 			   const char *buf, size_t count)
3661c301fc5SJordan Crouse {
3671c301fc5SJordan Crouse 
3681c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3691c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
3701c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
3711c301fc5SJordan Crouse 	unsigned char reg;
3721c301fc5SJordan Crouse 	long val;
3731c301fc5SJordan Crouse 
374179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
3751c301fc5SJordan Crouse 		return -EINVAL;
3761c301fc5SJordan Crouse 
3771c301fc5SJordan Crouse 	mutex_lock(&data->lock);
3781c301fc5SJordan Crouse 
379ebfaf1fbSJean Delvare 	data->voltage[sattr->nr][sattr->index] =
380ebfaf1fbSJean Delvare 				volt2reg(sattr->index, val, data->bypass_attn);
3811c301fc5SJordan Crouse 
3823d849981SJean Delvare 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
3831c301fc5SJordan Crouse 		if (sattr->nr == MIN)
3841c301fc5SJordan Crouse 			reg = VOLTAGE_MIN_REG(sattr->index);
3851c301fc5SJordan Crouse 		else
3861c301fc5SJordan Crouse 			reg = VOLTAGE_MAX_REG(sattr->index);
3873d849981SJean Delvare 	} else {
3883d849981SJean Delvare 		if (sattr->nr == MIN)
3893d849981SJean Delvare 			reg = REG_VTT_MIN;
3903d849981SJean Delvare 		else
3913d849981SJean Delvare 			reg = REG_VTT_MAX;
3923d849981SJean Delvare 	}
3931c301fc5SJordan Crouse 
3941c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
3951c301fc5SJordan Crouse 				  data->voltage[sattr->nr][sattr->index] >> 2);
3961c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
3971c301fc5SJordan Crouse 
3981c301fc5SJordan Crouse 	return count;
3991c301fc5SJordan Crouse }
4001c301fc5SJordan Crouse 
4011c301fc5SJordan Crouse static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
4021c301fc5SJordan Crouse 			 char *buf)
4031c301fc5SJordan Crouse {
4041c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
4051c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4061c301fc5SJordan Crouse 	int out;
4071c301fc5SJordan Crouse 
4081c301fc5SJordan Crouse 	switch (sattr->nr) {
4091c301fc5SJordan Crouse 	case HYSTERSIS:
4101c301fc5SJordan Crouse 		mutex_lock(&data->lock);
4111c301fc5SJordan Crouse 		out = data->temp[sattr->nr][sattr->index];
4121c301fc5SJordan Crouse 		if (sattr->index != 1)
4131c301fc5SJordan Crouse 			out = (out >> 4) & 0xF;
4141c301fc5SJordan Crouse 		else
4151c301fc5SJordan Crouse 			out = (out & 0xF);
4169ed5bc24SGuenter Roeck 		/*
4179ed5bc24SGuenter Roeck 		 * Show the value as an absolute number tied to
4189ed5bc24SGuenter Roeck 		 * THERM
4199ed5bc24SGuenter Roeck 		 */
4201c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
4211c301fc5SJordan Crouse 			out * 1000;
4221c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4231c301fc5SJordan Crouse 		break;
4241c301fc5SJordan Crouse 
4251c301fc5SJordan Crouse 	case OFFSET:
4269ed5bc24SGuenter Roeck 		/*
4279ed5bc24SGuenter Roeck 		 * Offset is always 2's complement, regardless of the
4289ed5bc24SGuenter Roeck 		 * setting in CONFIG5
4299ed5bc24SGuenter Roeck 		 */
4301c301fc5SJordan Crouse 		mutex_lock(&data->lock);
4311c301fc5SJordan Crouse 		out = (s8)data->temp[sattr->nr][sattr->index];
4321c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET)
4331c301fc5SJordan Crouse 			out *= 1000;
4341c301fc5SJordan Crouse 		else
4351c301fc5SJordan Crouse 			out *= 500;
4361c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
4371c301fc5SJordan Crouse 		break;
4381c301fc5SJordan Crouse 
4391c301fc5SJordan Crouse 	case ALARM:
4401c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 4)) & 1;
4411c301fc5SJordan Crouse 		break;
4421c301fc5SJordan Crouse 
4431c301fc5SJordan Crouse 	case FAULT:
4441c301fc5SJordan Crouse 		/* Note - only for remote1 and remote2 */
445cf312e07SJean Delvare 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
4461c301fc5SJordan Crouse 		break;
4471c301fc5SJordan Crouse 
4481c301fc5SJordan Crouse 	default:
4491c301fc5SJordan Crouse 		/* All other temp values are in the configured format */
4501c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
4511c301fc5SJordan Crouse 	}
4521c301fc5SJordan Crouse 
4531c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
4541c301fc5SJordan Crouse }
4551c301fc5SJordan Crouse 
4561c301fc5SJordan Crouse static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
4571c301fc5SJordan Crouse 			const char *buf, size_t count)
4581c301fc5SJordan Crouse {
4591c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4601c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
4611c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
4621c301fc5SJordan Crouse 	unsigned char reg = 0;
4631c301fc5SJordan Crouse 	u8 out;
4641c301fc5SJordan Crouse 	int temp;
4651c301fc5SJordan Crouse 	long val;
4661c301fc5SJordan Crouse 
467179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
4681c301fc5SJordan Crouse 		return -EINVAL;
4691c301fc5SJordan Crouse 
4701c301fc5SJordan Crouse 	mutex_lock(&data->lock);
4711c301fc5SJordan Crouse 
4721c301fc5SJordan Crouse 	/* We need the config register in all cases for temp <-> reg conv. */
4731c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
4741c301fc5SJordan Crouse 
4751c301fc5SJordan Crouse 	switch (sattr->nr) {
4761c301fc5SJordan Crouse 	case OFFSET:
4771c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET) {
4782a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 127000);
4791c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 1000;
4801c301fc5SJordan Crouse 		} else {
4812a844c14SGuenter Roeck 			val = clamp_val(val, -63000, 64000);
4821c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 500;
4831c301fc5SJordan Crouse 		}
4841c301fc5SJordan Crouse 		break;
4851c301fc5SJordan Crouse 
4861c301fc5SJordan Crouse 	case HYSTERSIS:
4879ed5bc24SGuenter Roeck 		/*
4889ed5bc24SGuenter Roeck 		 * The value will be given as an absolute value, turn it
4899ed5bc24SGuenter Roeck 		 * into an offset based on THERM
4909ed5bc24SGuenter Roeck 		 */
4911c301fc5SJordan Crouse 
4921c301fc5SJordan Crouse 		/* Read fresh THERM and HYSTERSIS values from the chip */
4931c301fc5SJordan Crouse 		data->temp[THERM][sattr->index] =
4941c301fc5SJordan Crouse 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
4951c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
4961c301fc5SJordan Crouse 
4971c301fc5SJordan Crouse 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
4982a844c14SGuenter Roeck 		val = clamp_val(val, temp - 15000, temp);
4991c301fc5SJordan Crouse 		val = (temp - val) / 1000;
5001c301fc5SJordan Crouse 
5011c301fc5SJordan Crouse 		if (sattr->index != 1) {
5021c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
5031c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
5041c301fc5SJordan Crouse 		} else {
5051c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
5061c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
5071c301fc5SJordan Crouse 		}
5081c301fc5SJordan Crouse 
5091c301fc5SJordan Crouse 		out = data->temp[HYSTERSIS][sattr->index];
5101c301fc5SJordan Crouse 		break;
5111c301fc5SJordan Crouse 
5121c301fc5SJordan Crouse 	default:
5131c301fc5SJordan Crouse 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
5141c301fc5SJordan Crouse 
5159ed5bc24SGuenter Roeck 		/*
5169ed5bc24SGuenter Roeck 		 * We maintain an extra 2 digits of precision for simplicity
5179ed5bc24SGuenter Roeck 		 * - shift those back off before writing the value
5189ed5bc24SGuenter Roeck 		 */
5191c301fc5SJordan Crouse 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
5201c301fc5SJordan Crouse 	}
5211c301fc5SJordan Crouse 
5221c301fc5SJordan Crouse 	switch (sattr->nr) {
5231c301fc5SJordan Crouse 	case MIN:
5241c301fc5SJordan Crouse 		reg = TEMP_MIN_REG(sattr->index);
5251c301fc5SJordan Crouse 		break;
5261c301fc5SJordan Crouse 	case MAX:
5271c301fc5SJordan Crouse 		reg = TEMP_MAX_REG(sattr->index);
5281c301fc5SJordan Crouse 		break;
5291c301fc5SJordan Crouse 	case OFFSET:
5301c301fc5SJordan Crouse 		reg = TEMP_OFFSET_REG(sattr->index);
5311c301fc5SJordan Crouse 		break;
5321c301fc5SJordan Crouse 	case AUTOMIN:
5331c301fc5SJordan Crouse 		reg = TEMP_TMIN_REG(sattr->index);
5341c301fc5SJordan Crouse 		break;
5351c301fc5SJordan Crouse 	case THERM:
5361c301fc5SJordan Crouse 		reg = TEMP_THERM_REG(sattr->index);
5371c301fc5SJordan Crouse 		break;
5381c301fc5SJordan Crouse 	case HYSTERSIS:
5391c301fc5SJordan Crouse 		if (sattr->index != 2)
5401c301fc5SJordan Crouse 			reg = REG_REMOTE1_HYSTERSIS;
5411c301fc5SJordan Crouse 		else
5421c301fc5SJordan Crouse 			reg = REG_REMOTE2_HYSTERSIS;
5431c301fc5SJordan Crouse 
5441c301fc5SJordan Crouse 		break;
5451c301fc5SJordan Crouse 	}
5461c301fc5SJordan Crouse 
5471c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, out);
5481c301fc5SJordan Crouse 
5491c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5501c301fc5SJordan Crouse 	return count;
5511c301fc5SJordan Crouse }
5521c301fc5SJordan Crouse 
5539ed5bc24SGuenter Roeck /*
5549ed5bc24SGuenter Roeck  * Table of autorange values - the user will write the value in millidegrees,
5559ed5bc24SGuenter Roeck  * and we'll convert it
5569ed5bc24SGuenter Roeck  */
5571c301fc5SJordan Crouse static const int autorange_table[] = {
5581c301fc5SJordan Crouse 	2000, 2500, 3330, 4000, 5000, 6670, 8000,
5591c301fc5SJordan Crouse 	10000, 13330, 16000, 20000, 26670, 32000, 40000,
5601c301fc5SJordan Crouse 	53330, 80000
5611c301fc5SJordan Crouse };
5621c301fc5SJordan Crouse 
5631c301fc5SJordan Crouse static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
5641c301fc5SJordan Crouse 			   char *buf)
5651c301fc5SJordan Crouse {
5661c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
5671c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5681c301fc5SJordan Crouse 	int out, val;
5691c301fc5SJordan Crouse 
5701c301fc5SJordan Crouse 	mutex_lock(&data->lock);
5711c301fc5SJordan Crouse 	out = (data->range[sattr->index] >> 4) & 0x0F;
5721c301fc5SJordan Crouse 	val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
5731c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5741c301fc5SJordan Crouse 
5751c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", val + autorange_table[out]);
5761c301fc5SJordan Crouse }
5771c301fc5SJordan Crouse 
5781c301fc5SJordan Crouse static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
5791c301fc5SJordan Crouse 			  const char *buf, size_t count)
5801c301fc5SJordan Crouse {
5811c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
5821c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
5831c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5841c301fc5SJordan Crouse 	int temp;
5851c301fc5SJordan Crouse 	long val;
5861c301fc5SJordan Crouse 
587179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
5881c301fc5SJordan Crouse 		return -EINVAL;
5891c301fc5SJordan Crouse 
5901c301fc5SJordan Crouse 	mutex_lock(&data->lock);
5911c301fc5SJordan Crouse 
5921c301fc5SJordan Crouse 	/* Get a fresh copy of the needed registers */
5931c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
5941c301fc5SJordan Crouse 	data->temp[AUTOMIN][sattr->index] =
5951c301fc5SJordan Crouse 		adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
5961c301fc5SJordan Crouse 	data->range[sattr->index] =
5971c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
5981c301fc5SJordan Crouse 
5999ed5bc24SGuenter Roeck 	/*
6009ed5bc24SGuenter Roeck 	 * The user will write an absolute value, so subtract the start point
6019ed5bc24SGuenter Roeck 	 * to figure the range
6029ed5bc24SGuenter Roeck 	 */
6031c301fc5SJordan Crouse 	temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
6042a844c14SGuenter Roeck 	val = clamp_val(val, temp + autorange_table[0],
6051c301fc5SJordan Crouse 		temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
6061c301fc5SJordan Crouse 	val -= temp;
6071c301fc5SJordan Crouse 
6081c301fc5SJordan Crouse 	/* Find the nearest table entry to what the user wrote */
6091c301fc5SJordan Crouse 	val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
6101c301fc5SJordan Crouse 
6111c301fc5SJordan Crouse 	data->range[sattr->index] &= ~0xF0;
6121c301fc5SJordan Crouse 	data->range[sattr->index] |= val << 4;
6131c301fc5SJordan Crouse 
6141c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
6151c301fc5SJordan Crouse 				  data->range[sattr->index]);
6161c301fc5SJordan Crouse 
6171c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6181c301fc5SJordan Crouse 	return count;
6191c301fc5SJordan Crouse }
6201c301fc5SJordan Crouse 
6211c301fc5SJordan Crouse static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
6221c301fc5SJordan Crouse 			 char *buf)
6231c301fc5SJordan Crouse {
6241c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6251c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6261c301fc5SJordan Crouse 	int out;
6271c301fc5SJordan Crouse 
6281c301fc5SJordan Crouse 	if (sattr->nr == ALARM)
6291c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 10)) & 1;
6301c301fc5SJordan Crouse 	else
6311c301fc5SJordan Crouse 		out = tach2rpm(data->tach[sattr->nr][sattr->index]);
6321c301fc5SJordan Crouse 
6331c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
6341c301fc5SJordan Crouse }
6351c301fc5SJordan Crouse 
6361c301fc5SJordan Crouse static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
6371c301fc5SJordan Crouse 			const char *buf, size_t count)
6381c301fc5SJordan Crouse {
6391c301fc5SJordan Crouse 
6401c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6411c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
6421c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
6431c301fc5SJordan Crouse 	unsigned long val;
6441c301fc5SJordan Crouse 
645179c4fdbSFrans Meulenbroeks 	if (kstrtoul(buf, 10, &val))
6461c301fc5SJordan Crouse 		return -EINVAL;
6471c301fc5SJordan Crouse 
6481c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6491c301fc5SJordan Crouse 
6501c301fc5SJordan Crouse 	data->tach[MIN][sattr->index] = rpm2tach(val);
6511c301fc5SJordan Crouse 
6521c301fc5SJordan Crouse 	adt7475_write_word(client, TACH_MIN_REG(sattr->index),
6531c301fc5SJordan Crouse 			   data->tach[MIN][sattr->index]);
6541c301fc5SJordan Crouse 
6551c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6561c301fc5SJordan Crouse 	return count;
6571c301fc5SJordan Crouse }
6581c301fc5SJordan Crouse 
6591c301fc5SJordan Crouse static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
6601c301fc5SJordan Crouse 			char *buf)
6611c301fc5SJordan Crouse {
6621c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6631c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6641c301fc5SJordan Crouse 
6651c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
6661c301fc5SJordan Crouse }
6671c301fc5SJordan Crouse 
6681c301fc5SJordan Crouse static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
6691c301fc5SJordan Crouse 			    char *buf)
6701c301fc5SJordan Crouse {
6711c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6721c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6731c301fc5SJordan Crouse 
6741c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
6751c301fc5SJordan Crouse }
6761c301fc5SJordan Crouse 
6771c301fc5SJordan Crouse static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
6781c301fc5SJordan Crouse 			    char *buf)
6791c301fc5SJordan Crouse {
6801c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6811c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6821c301fc5SJordan Crouse 
6831c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
6841c301fc5SJordan Crouse }
6851c301fc5SJordan Crouse 
6861c301fc5SJordan Crouse static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
6871c301fc5SJordan Crouse 		       const char *buf, size_t count)
6881c301fc5SJordan Crouse {
6891c301fc5SJordan Crouse 
6901c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6911c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
6921c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
6931c301fc5SJordan Crouse 	unsigned char reg = 0;
6941c301fc5SJordan Crouse 	long val;
6951c301fc5SJordan Crouse 
696179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
6971c301fc5SJordan Crouse 		return -EINVAL;
6981c301fc5SJordan Crouse 
6991c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7001c301fc5SJordan Crouse 
7011c301fc5SJordan Crouse 	switch (sattr->nr) {
7021c301fc5SJordan Crouse 	case INPUT:
7031c301fc5SJordan Crouse 		/* Get a fresh value for CONTROL */
7041c301fc5SJordan Crouse 		data->pwm[CONTROL][sattr->index] =
7051c301fc5SJordan Crouse 			adt7475_read(PWM_CONFIG_REG(sattr->index));
7061c301fc5SJordan Crouse 
7079ed5bc24SGuenter Roeck 		/*
7089ed5bc24SGuenter Roeck 		 * If we are not in manual mode, then we shouldn't allow
7099ed5bc24SGuenter Roeck 		 * the user to set the pwm speed
7109ed5bc24SGuenter Roeck 		 */
7111c301fc5SJordan Crouse 		if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
7121c301fc5SJordan Crouse 			mutex_unlock(&data->lock);
7131c301fc5SJordan Crouse 			return count;
7141c301fc5SJordan Crouse 		}
7151c301fc5SJordan Crouse 
7161c301fc5SJordan Crouse 		reg = PWM_REG(sattr->index);
7171c301fc5SJordan Crouse 		break;
7181c301fc5SJordan Crouse 
7191c301fc5SJordan Crouse 	case MIN:
7201c301fc5SJordan Crouse 		reg = PWM_MIN_REG(sattr->index);
7211c301fc5SJordan Crouse 		break;
7221c301fc5SJordan Crouse 
7231c301fc5SJordan Crouse 	case MAX:
7241c301fc5SJordan Crouse 		reg = PWM_MAX_REG(sattr->index);
7251c301fc5SJordan Crouse 		break;
7261c301fc5SJordan Crouse 	}
7271c301fc5SJordan Crouse 
7282a844c14SGuenter Roeck 	data->pwm[sattr->nr][sattr->index] = clamp_val(val, 0, 0xFF);
7291c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
7301c301fc5SJordan Crouse 				  data->pwm[sattr->nr][sattr->index]);
7311c301fc5SJordan Crouse 
7321c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7331c301fc5SJordan Crouse 
7341c301fc5SJordan Crouse 	return count;
7351c301fc5SJordan Crouse }
7361c301fc5SJordan Crouse 
7371c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */
7381c301fc5SJordan Crouse 
7391c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index,
7401c301fc5SJordan Crouse 		      unsigned int pwmctl, unsigned int pwmchan)
7411c301fc5SJordan Crouse {
7421c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7431c301fc5SJordan Crouse 	long val = 0;
7441c301fc5SJordan Crouse 
7451c301fc5SJordan Crouse 	switch (pwmctl) {
7461c301fc5SJordan Crouse 	case 0:
7471c301fc5SJordan Crouse 		val = 0x03;	/* Run at full speed */
7481c301fc5SJordan Crouse 		break;
7491c301fc5SJordan Crouse 	case 1:
7501c301fc5SJordan Crouse 		val = 0x07;	/* Manual mode */
7511c301fc5SJordan Crouse 		break;
7521c301fc5SJordan Crouse 	case 2:
7531c301fc5SJordan Crouse 		switch (pwmchan) {
7541c301fc5SJordan Crouse 		case 1:
7551c301fc5SJordan Crouse 			/* Remote1 controls PWM */
7561c301fc5SJordan Crouse 			val = 0x00;
7571c301fc5SJordan Crouse 			break;
7581c301fc5SJordan Crouse 		case 2:
7591c301fc5SJordan Crouse 			/* local controls PWM */
7601c301fc5SJordan Crouse 			val = 0x01;
7611c301fc5SJordan Crouse 			break;
7621c301fc5SJordan Crouse 		case 4:
7631c301fc5SJordan Crouse 			/* remote2 controls PWM */
7641c301fc5SJordan Crouse 			val = 0x02;
7651c301fc5SJordan Crouse 			break;
7661c301fc5SJordan Crouse 		case 6:
7671c301fc5SJordan Crouse 			/* local/remote2 control PWM */
7681c301fc5SJordan Crouse 			val = 0x05;
7691c301fc5SJordan Crouse 			break;
7701c301fc5SJordan Crouse 		case 7:
7711c301fc5SJordan Crouse 			/* All three control PWM */
7721c301fc5SJordan Crouse 			val = 0x06;
7731c301fc5SJordan Crouse 			break;
7741c301fc5SJordan Crouse 		default:
7751c301fc5SJordan Crouse 			return -EINVAL;
7761c301fc5SJordan Crouse 		}
7771c301fc5SJordan Crouse 		break;
7781c301fc5SJordan Crouse 	default:
7791c301fc5SJordan Crouse 		return -EINVAL;
7801c301fc5SJordan Crouse 	}
7811c301fc5SJordan Crouse 
7821c301fc5SJordan Crouse 	data->pwmctl[index] = pwmctl;
7831c301fc5SJordan Crouse 	data->pwmchan[index] = pwmchan;
7841c301fc5SJordan Crouse 
7851c301fc5SJordan Crouse 	data->pwm[CONTROL][index] &= ~0xE0;
7861c301fc5SJordan Crouse 	data->pwm[CONTROL][index] |= (val & 7) << 5;
7871c301fc5SJordan Crouse 
7881c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
7891c301fc5SJordan Crouse 				  data->pwm[CONTROL][index]);
7901c301fc5SJordan Crouse 
7911c301fc5SJordan Crouse 	return 0;
7921c301fc5SJordan Crouse }
7931c301fc5SJordan Crouse 
7941c301fc5SJordan Crouse static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
7951c301fc5SJordan Crouse 			   const char *buf, size_t count)
7961c301fc5SJordan Crouse {
7971c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7981c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7991c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8001c301fc5SJordan Crouse 	int r;
8011c301fc5SJordan Crouse 	long val;
8021c301fc5SJordan Crouse 
803179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
8041c301fc5SJordan Crouse 		return -EINVAL;
8051c301fc5SJordan Crouse 
8061c301fc5SJordan Crouse 	mutex_lock(&data->lock);
8071c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
8081c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
8091c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
8101c301fc5SJordan Crouse 	if (r)
8111c301fc5SJordan Crouse 		count = r;
8121c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8131c301fc5SJordan Crouse 
8141c301fc5SJordan Crouse 	return count;
8151c301fc5SJordan Crouse }
8161c301fc5SJordan Crouse 
8171c301fc5SJordan Crouse static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
8181c301fc5SJordan Crouse 			   const char *buf, size_t count)
8191c301fc5SJordan Crouse {
8201c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8211c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
8221c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8231c301fc5SJordan Crouse 	int r;
8241c301fc5SJordan Crouse 	long val;
8251c301fc5SJordan Crouse 
826179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
8271c301fc5SJordan Crouse 		return -EINVAL;
8281c301fc5SJordan Crouse 
8291c301fc5SJordan Crouse 	mutex_lock(&data->lock);
8301c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
8311c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
8321c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
8331c301fc5SJordan Crouse 	if (r)
8341c301fc5SJordan Crouse 		count = r;
8351c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8361c301fc5SJordan Crouse 
8371c301fc5SJordan Crouse 	return count;
8381c301fc5SJordan Crouse }
8391c301fc5SJordan Crouse 
8401c301fc5SJordan Crouse /* List of frequencies for the PWM */
8411c301fc5SJordan Crouse static const int pwmfreq_table[] = {
8421c301fc5SJordan Crouse 	11, 14, 22, 29, 35, 44, 58, 88
8431c301fc5SJordan Crouse };
8441c301fc5SJordan Crouse 
8451c301fc5SJordan Crouse static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
8461c301fc5SJordan Crouse 			    char *buf)
8471c301fc5SJordan Crouse {
8481c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
8491c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8501c301fc5SJordan Crouse 
8511c301fc5SJordan Crouse 	return sprintf(buf, "%d\n",
8521c301fc5SJordan Crouse 		       pwmfreq_table[data->range[sattr->index] & 7]);
8531c301fc5SJordan Crouse }
8541c301fc5SJordan Crouse 
8551c301fc5SJordan Crouse static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
8561c301fc5SJordan Crouse 			   const char *buf, size_t count)
8571c301fc5SJordan Crouse {
8581c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
8591c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
8601c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
8611c301fc5SJordan Crouse 	int out;
8621c301fc5SJordan Crouse 	long val;
8631c301fc5SJordan Crouse 
864179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
8651c301fc5SJordan Crouse 		return -EINVAL;
8661c301fc5SJordan Crouse 
8671c301fc5SJordan Crouse 	out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
8681c301fc5SJordan Crouse 
8691c301fc5SJordan Crouse 	mutex_lock(&data->lock);
8701c301fc5SJordan Crouse 
8711c301fc5SJordan Crouse 	data->range[sattr->index] =
8721c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
8731c301fc5SJordan Crouse 	data->range[sattr->index] &= ~7;
8741c301fc5SJordan Crouse 	data->range[sattr->index] |= out;
8751c301fc5SJordan Crouse 
8761c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
8771c301fc5SJordan Crouse 				  data->range[sattr->index]);
8781c301fc5SJordan Crouse 
8791c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8801c301fc5SJordan Crouse 	return count;
8811c301fc5SJordan Crouse }
8821c301fc5SJordan Crouse 
8831d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_show(struct device *dev,
8841d05303cSJulia Lawall 					struct device_attribute *devattr,
8851d05303cSJulia Lawall 					char *buf)
886f99318b2SJean Delvare {
887f99318b2SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
888f99318b2SJean Delvare 	return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
889f99318b2SJean Delvare }
890f99318b2SJean Delvare 
8911d05303cSJulia Lawall static ssize_t pwm_use_point2_pwm_at_crit_store(struct device *dev,
892f99318b2SJean Delvare 					struct device_attribute *devattr,
893f99318b2SJean Delvare 					const char *buf, size_t count)
894f99318b2SJean Delvare {
895f99318b2SJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
896f99318b2SJean Delvare 	struct adt7475_data *data = i2c_get_clientdata(client);
897f99318b2SJean Delvare 	long val;
898f99318b2SJean Delvare 
899179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
900f99318b2SJean Delvare 		return -EINVAL;
901f99318b2SJean Delvare 	if (val != 0 && val != 1)
902f99318b2SJean Delvare 		return -EINVAL;
903f99318b2SJean Delvare 
904f99318b2SJean Delvare 	mutex_lock(&data->lock);
905f99318b2SJean Delvare 	data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
906f99318b2SJean Delvare 	if (val)
907f99318b2SJean Delvare 		data->config4 |= CONFIG4_MAXDUTY;
908f99318b2SJean Delvare 	else
909f99318b2SJean Delvare 		data->config4 &= ~CONFIG4_MAXDUTY;
910f99318b2SJean Delvare 	i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
911f99318b2SJean Delvare 	mutex_unlock(&data->lock);
912f99318b2SJean Delvare 
913f99318b2SJean Delvare 	return count;
914f99318b2SJean Delvare }
915f99318b2SJean Delvare 
9161d05303cSJulia Lawall static ssize_t vrm_show(struct device *dev, struct device_attribute *devattr,
91754fe4671SJean Delvare 			char *buf)
91854fe4671SJean Delvare {
91954fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
92054fe4671SJean Delvare 	return sprintf(buf, "%d\n", (int)data->vrm);
92154fe4671SJean Delvare }
92254fe4671SJean Delvare 
9231d05303cSJulia Lawall static ssize_t vrm_store(struct device *dev, struct device_attribute *devattr,
92454fe4671SJean Delvare 			 const char *buf, size_t count)
92554fe4671SJean Delvare {
92654fe4671SJean Delvare 	struct adt7475_data *data = dev_get_drvdata(dev);
92754fe4671SJean Delvare 	long val;
92854fe4671SJean Delvare 
929179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &val))
93054fe4671SJean Delvare 		return -EINVAL;
93154fe4671SJean Delvare 	if (val < 0 || val > 255)
93254fe4671SJean Delvare 		return -EINVAL;
93354fe4671SJean Delvare 	data->vrm = val;
93454fe4671SJean Delvare 
93554fe4671SJean Delvare 	return count;
93654fe4671SJean Delvare }
93754fe4671SJean Delvare 
9381d05303cSJulia Lawall static ssize_t cpu0_vid_show(struct device *dev,
9391d05303cSJulia Lawall 			     struct device_attribute *devattr, char *buf)
94054fe4671SJean Delvare {
94154fe4671SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
94254fe4671SJean Delvare 	return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
94354fe4671SJean Delvare }
94454fe4671SJean Delvare 
9453d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
9463d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
9473d849981SJean Delvare 			    set_voltage, MAX, 0);
9483d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
9493d849981SJean Delvare 			    set_voltage, MIN, 0);
9503d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
951cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
9521c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
9531c301fc5SJordan Crouse 			    set_voltage, MAX, 1);
954cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
9551c301fc5SJordan Crouse 			    set_voltage, MIN, 1);
956cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
957cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
958cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
959cffb9dd0SJean Delvare 			    set_voltage, MAX, 2);
960cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
961cffb9dd0SJean Delvare 			    set_voltage, MIN, 2);
962cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
9633d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
9643d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
9653d849981SJean Delvare 			    set_voltage, MAX, 3);
9663d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
9673d849981SJean Delvare 			    set_voltage, MIN, 3);
9683d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
9693d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
9703d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
9713d849981SJean Delvare 			    set_voltage, MAX, 4);
9723d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
9733d849981SJean Delvare 			    set_voltage, MIN, 4);
9743d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
9753d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
9763d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
9773d849981SJean Delvare 			    set_voltage, MAX, 5);
9783d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
9793d849981SJean Delvare 			    set_voltage, MIN, 5);
9803d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
9811c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
9821c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
9831c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
9841c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
9851c301fc5SJordan Crouse 			    MAX, 0);
9861c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
9871c301fc5SJordan Crouse 			    MIN, 0);
9881c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
9891c301fc5SJordan Crouse 			    set_temp, OFFSET, 0);
9901c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
9911c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 0);
9921c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
9931c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 0);
9941c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
9951c301fc5SJordan Crouse 			    THERM, 0);
9961c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
9971c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 0);
9981c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
9991c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
10001c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
10011c301fc5SJordan Crouse 			    MAX, 1);
10021c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
10031c301fc5SJordan Crouse 			    MIN, 1);
10041c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
10051c301fc5SJordan Crouse 			    set_temp, OFFSET, 1);
10061c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
10071c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 1);
10081c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
10091c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 1);
10101c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
10111c301fc5SJordan Crouse 			    THERM, 1);
10121c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
10131c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 1);
10141c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
10151c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
10161c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
10171c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
10181c301fc5SJordan Crouse 			    MAX, 2);
10191c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
10201c301fc5SJordan Crouse 			    MIN, 2);
10211c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
10221c301fc5SJordan Crouse 			    set_temp, OFFSET, 2);
10231c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
10241c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 2);
10251c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
10261c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 2);
10271c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
10281c301fc5SJordan Crouse 			    THERM, 2);
10291c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
10301c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 2);
10311c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
10321c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
10331c301fc5SJordan Crouse 			    MIN, 0);
10341c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
10351c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
10361c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
10371c301fc5SJordan Crouse 			    MIN, 1);
10381c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
10391c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
10401c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
10411c301fc5SJordan Crouse 			    MIN, 2);
10421c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
10431c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
10441c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
10451c301fc5SJordan Crouse 			    MIN, 3);
10461c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
10471c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
10481c301fc5SJordan Crouse 			    0);
10491c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
10501c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 0);
10511c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
10521c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 0);
105384d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
10541c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 0);
10551c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
10561c301fc5SJordan Crouse 			    set_pwm, MIN, 0);
10571c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
10581c301fc5SJordan Crouse 			    set_pwm, MAX, 0);
10591c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
10601c301fc5SJordan Crouse 			    1);
10611c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
10621c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 1);
10631c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
10641c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 1);
106584d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
10661c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 1);
10671c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
10681c301fc5SJordan Crouse 			    set_pwm, MIN, 1);
10691c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
10701c301fc5SJordan Crouse 			    set_pwm, MAX, 1);
10711c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
10721c301fc5SJordan Crouse 			    2);
10731c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
10741c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 2);
10751c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
10761c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 2);
107784d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
10781c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 2);
10791c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
10801c301fc5SJordan Crouse 			    set_pwm, MIN, 2);
10811c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
10821c301fc5SJordan Crouse 			    set_pwm, MAX, 2);
10831c301fc5SJordan Crouse 
1084f99318b2SJean Delvare /* Non-standard name, might need revisiting */
10851d05303cSJulia Lawall static DEVICE_ATTR_RW(pwm_use_point2_pwm_at_crit);
1086f99318b2SJean Delvare 
10871d05303cSJulia Lawall static DEVICE_ATTR_RW(vrm);
10881d05303cSJulia Lawall static DEVICE_ATTR_RO(cpu0_vid);
108954fe4671SJean Delvare 
10901c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = {
10911c301fc5SJordan Crouse 	&sensor_dev_attr_in1_input.dev_attr.attr,
10921c301fc5SJordan Crouse 	&sensor_dev_attr_in1_max.dev_attr.attr,
10931c301fc5SJordan Crouse 	&sensor_dev_attr_in1_min.dev_attr.attr,
10941c301fc5SJordan Crouse 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
10951c301fc5SJordan Crouse 	&sensor_dev_attr_in2_input.dev_attr.attr,
10961c301fc5SJordan Crouse 	&sensor_dev_attr_in2_max.dev_attr.attr,
10971c301fc5SJordan Crouse 	&sensor_dev_attr_in2_min.dev_attr.attr,
10981c301fc5SJordan Crouse 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
10991c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_input.dev_attr.attr,
11001c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
11011c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
11021c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_max.dev_attr.attr,
11031c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_min.dev_attr.attr,
11041c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
11051c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
11061c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
11071c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
11081c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
11091c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_input.dev_attr.attr,
11101c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
11111c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_max.dev_attr.attr,
11121c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_min.dev_attr.attr,
11131c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
11141c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
11151c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
11161c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
11171c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
11181c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_input.dev_attr.attr,
11191c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
11201c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
11211c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_max.dev_attr.attr,
11221c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_min.dev_attr.attr,
11231c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
11241c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
11251c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
11261c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
11271c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
11281c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_input.dev_attr.attr,
11291c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_min.dev_attr.attr,
11301c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
11311c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_input.dev_attr.attr,
11321c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_min.dev_attr.attr,
11331c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
11341c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_input.dev_attr.attr,
11351c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_min.dev_attr.attr,
11361c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
11371c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1.dev_attr.attr,
11381c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
11391c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
114084d2a314SJean Delvare 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
11411c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
11421c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
11431c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3.dev_attr.attr,
11441c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
11451c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
114684d2a314SJean Delvare 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
11471c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
11481c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1149f99318b2SJean Delvare 	&dev_attr_pwm_use_point2_pwm_at_crit.attr,
11501c301fc5SJordan Crouse 	NULL,
11511c301fc5SJordan Crouse };
11521c301fc5SJordan Crouse 
1153378933c9SJean Delvare static struct attribute *fan4_attrs[] = {
1154378933c9SJean Delvare 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1155378933c9SJean Delvare 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1156378933c9SJean Delvare 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1157378933c9SJean Delvare 	NULL
1158378933c9SJean Delvare };
1159378933c9SJean Delvare 
1160378933c9SJean Delvare static struct attribute *pwm2_attrs[] = {
1161378933c9SJean Delvare 	&sensor_dev_attr_pwm2.dev_attr.attr,
1162378933c9SJean Delvare 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1163378933c9SJean Delvare 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1164378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1165378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1166378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1167378933c9SJean Delvare 	NULL
1168378933c9SJean Delvare };
1169378933c9SJean Delvare 
1170378933c9SJean Delvare static struct attribute *in0_attrs[] = {
11713d849981SJean Delvare 	&sensor_dev_attr_in0_input.dev_attr.attr,
11723d849981SJean Delvare 	&sensor_dev_attr_in0_max.dev_attr.attr,
11733d849981SJean Delvare 	&sensor_dev_attr_in0_min.dev_attr.attr,
11743d849981SJean Delvare 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1175378933c9SJean Delvare 	NULL
1176378933c9SJean Delvare };
1177378933c9SJean Delvare 
1178d8d2ee07SJean Delvare static struct attribute *in3_attrs[] = {
11793d849981SJean Delvare 	&sensor_dev_attr_in3_input.dev_attr.attr,
11803d849981SJean Delvare 	&sensor_dev_attr_in3_max.dev_attr.attr,
11813d849981SJean Delvare 	&sensor_dev_attr_in3_min.dev_attr.attr,
11823d849981SJean Delvare 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1183d8d2ee07SJean Delvare 	NULL
1184d8d2ee07SJean Delvare };
1185d8d2ee07SJean Delvare 
1186d8d2ee07SJean Delvare static struct attribute *in4_attrs[] = {
11873d849981SJean Delvare 	&sensor_dev_attr_in4_input.dev_attr.attr,
11883d849981SJean Delvare 	&sensor_dev_attr_in4_max.dev_attr.attr,
11893d849981SJean Delvare 	&sensor_dev_attr_in4_min.dev_attr.attr,
11903d849981SJean Delvare 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1191d8d2ee07SJean Delvare 	NULL
1192d8d2ee07SJean Delvare };
1193d8d2ee07SJean Delvare 
1194d8d2ee07SJean Delvare static struct attribute *in5_attrs[] = {
11953d849981SJean Delvare 	&sensor_dev_attr_in5_input.dev_attr.attr,
11963d849981SJean Delvare 	&sensor_dev_attr_in5_max.dev_attr.attr,
11973d849981SJean Delvare 	&sensor_dev_attr_in5_min.dev_attr.attr,
11983d849981SJean Delvare 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
11993d849981SJean Delvare 	NULL
12003d849981SJean Delvare };
12013d849981SJean Delvare 
120254fe4671SJean Delvare static struct attribute *vid_attrs[] = {
120354fe4671SJean Delvare 	&dev_attr_cpu0_vid.attr,
120454fe4671SJean Delvare 	&dev_attr_vrm.attr,
120554fe4671SJean Delvare 	NULL
120654fe4671SJean Delvare };
120754fe4671SJean Delvare 
120854ecb9e3SJean Delvare static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1209378933c9SJean Delvare static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1210378933c9SJean Delvare static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1211378933c9SJean Delvare static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
1212d8d2ee07SJean Delvare static struct attribute_group in3_attr_group = { .attrs = in3_attrs };
1213d8d2ee07SJean Delvare static struct attribute_group in4_attr_group = { .attrs = in4_attrs };
1214d8d2ee07SJean Delvare static struct attribute_group in5_attr_group = { .attrs = in5_attrs };
121554fe4671SJean Delvare static struct attribute_group vid_attr_group = { .attrs = vid_attrs };
12161c301fc5SJordan Crouse 
1217310ec792SJean Delvare static int adt7475_detect(struct i2c_client *client,
12181c301fc5SJordan Crouse 			  struct i2c_board_info *info)
12191c301fc5SJordan Crouse {
12201c301fc5SJordan Crouse 	struct i2c_adapter *adapter = client->adapter;
1221d656b6fdSJean Delvare 	int vendid, devid, devid2;
1222b180d050SJean Delvare 	const char *name;
12231c301fc5SJordan Crouse 
12241c301fc5SJordan Crouse 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
12251c301fc5SJordan Crouse 		return -ENODEV;
12261c301fc5SJordan Crouse 
1227b180d050SJean Delvare 	vendid = adt7475_read(REG_VENDID);
1228d656b6fdSJean Delvare 	devid2 = adt7475_read(REG_DEVID2);
1229d656b6fdSJean Delvare 	if (vendid != 0x41 ||		/* Analog Devices */
1230d656b6fdSJean Delvare 	    (devid2 & 0xf8) != 0x68)
1231d656b6fdSJean Delvare 		return -ENODEV;
1232b180d050SJean Delvare 
1233d656b6fdSJean Delvare 	devid = adt7475_read(REG_DEVID);
1234d656b6fdSJean Delvare 	if (devid == 0x73)
1235b180d050SJean Delvare 		name = "adt7473";
1236d656b6fdSJean Delvare 	else if (devid == 0x75 && client->addr == 0x2e)
1237b180d050SJean Delvare 		name = "adt7475";
1238d8d2ee07SJean Delvare 	else if (devid == 0x76)
1239d8d2ee07SJean Delvare 		name = "adt7476";
12403d849981SJean Delvare 	else if ((devid2 & 0xfc) == 0x6c)
12413d849981SJean Delvare 		name = "adt7490";
1242b180d050SJean Delvare 	else {
1243b180d050SJean Delvare 		dev_dbg(&adapter->dev,
1244d8d2ee07SJean Delvare 			"Couldn't detect an ADT7473/75/76/90 part at "
1245b180d050SJean Delvare 			"0x%02x\n", (unsigned int)client->addr);
12461c301fc5SJordan Crouse 		return -ENODEV;
12471c301fc5SJordan Crouse 	}
12481c301fc5SJordan Crouse 
1249b180d050SJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
12501c301fc5SJordan Crouse 
12511c301fc5SJordan Crouse 	return 0;
12521c301fc5SJordan Crouse }
12531c301fc5SJordan Crouse 
12540f14480bSJean Delvare static void adt7475_remove_files(struct i2c_client *client,
12550f14480bSJean Delvare 				 struct adt7475_data *data)
12560f14480bSJean Delvare {
12570f14480bSJean Delvare 	sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1258378933c9SJean Delvare 	if (data->has_fan4)
1259378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1260378933c9SJean Delvare 	if (data->has_pwm2)
1261378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1262378933c9SJean Delvare 	if (data->has_voltage & (1 << 0))
1263378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
1264d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3))
1265d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in3_attr_group);
1266d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4))
1267d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in4_attr_group);
1268d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5))
1269d8d2ee07SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in5_attr_group);
127054fe4671SJean Delvare 	if (data->has_vid)
127154fe4671SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &vid_attr_group);
12720f14480bSJean Delvare }
12730f14480bSJean Delvare 
12741c301fc5SJordan Crouse static int adt7475_probe(struct i2c_client *client,
12751c301fc5SJordan Crouse 			 const struct i2c_device_id *id)
12761c301fc5SJordan Crouse {
12774e2496e4SJavier Martinez Canillas 	enum chips chip;
127899b8c83aSFrans Meulenbroeks 	static const char * const names[] = {
1279d07ca4adSJean Delvare 		[adt7473] = "ADT7473",
1280d07ca4adSJean Delvare 		[adt7475] = "ADT7475",
1281d8d2ee07SJean Delvare 		[adt7476] = "ADT7476",
1282d07ca4adSJean Delvare 		[adt7490] = "ADT7490",
1283d07ca4adSJean Delvare 	};
1284d07ca4adSJean Delvare 
12851c301fc5SJordan Crouse 	struct adt7475_data *data;
1286378933c9SJean Delvare 	int i, ret = 0, revision;
1287ebfaf1fbSJean Delvare 	u8 config2, config3;
12881c301fc5SJordan Crouse 
1289e3ecb2eeSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
12901c301fc5SJordan Crouse 	if (data == NULL)
12911c301fc5SJordan Crouse 		return -ENOMEM;
12921c301fc5SJordan Crouse 
12931c301fc5SJordan Crouse 	mutex_init(&data->lock);
12941c301fc5SJordan Crouse 	i2c_set_clientdata(client, data);
12951c301fc5SJordan Crouse 
12964e2496e4SJavier Martinez Canillas 	if (client->dev.of_node)
12974e2496e4SJavier Martinez Canillas 		chip = (enum chips)of_device_get_match_data(&client->dev);
12984e2496e4SJavier Martinez Canillas 	else
12994e2496e4SJavier Martinez Canillas 		chip = id->driver_data;
13004e2496e4SJavier Martinez Canillas 
1301cffb9dd0SJean Delvare 	/* Initialize device-specific values */
13024e2496e4SJavier Martinez Canillas 	switch (chip) {
1303d8d2ee07SJean Delvare 	case adt7476:
1304d8d2ee07SJean Delvare 		data->has_voltage = 0x0e;	/* in1 to in3 */
1305d8d2ee07SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1306d8d2ee07SJean Delvare 		break;
13073d849981SJean Delvare 	case adt7490:
1308378933c9SJean Delvare 		data->has_voltage = 0x3e;	/* in1 to in5 */
1309378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x03;
1310d07ca4adSJean Delvare 		if (revision == 0x03)
1311d07ca4adSJean Delvare 			revision += adt7475_read(REG_DEVREV2);
13123d849981SJean Delvare 		break;
1313cffb9dd0SJean Delvare 	default:
1314cffb9dd0SJean Delvare 		data->has_voltage = 0x06;	/* in1, in2 */
1315378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1316378933c9SJean Delvare 	}
1317378933c9SJean Delvare 
1318378933c9SJean Delvare 	config3 = adt7475_read(REG_CONFIG3);
1319378933c9SJean Delvare 	/* Pin PWM2 may alternatively be used for ALERT output */
1320378933c9SJean Delvare 	if (!(config3 & CONFIG3_SMBALERT))
1321378933c9SJean Delvare 		data->has_pwm2 = 1;
1322378933c9SJean Delvare 	/* Meaning of this bit is inverted for the ADT7473-1 */
1323378933c9SJean Delvare 	if (id->driver_data == adt7473 && revision >= 1)
1324378933c9SJean Delvare 		data->has_pwm2 = !data->has_pwm2;
1325378933c9SJean Delvare 
1326378933c9SJean Delvare 	data->config4 = adt7475_read(REG_CONFIG4);
1327378933c9SJean Delvare 	/* Pin TACH4 may alternatively be used for THERM */
1328378933c9SJean Delvare 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1329378933c9SJean Delvare 		data->has_fan4 = 1;
1330378933c9SJean Delvare 
13319ed5bc24SGuenter Roeck 	/*
13329ed5bc24SGuenter Roeck 	 * THERM configuration is more complex on the ADT7476 and ADT7490,
13339ed5bc24SGuenter Roeck 	 * because 2 different pins (TACH4 and +2.5 Vin) can be used for
13349ed5bc24SGuenter Roeck 	 * this function
13359ed5bc24SGuenter Roeck 	 */
1336378933c9SJean Delvare 	if (id->driver_data == adt7490) {
1337378933c9SJean Delvare 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1338378933c9SJean Delvare 		    !(config3 & CONFIG3_THERM))
1339378933c9SJean Delvare 			data->has_fan4 = 1;
1340d8d2ee07SJean Delvare 	}
1341d8d2ee07SJean Delvare 	if (id->driver_data == adt7476 || id->driver_data == adt7490) {
1342378933c9SJean Delvare 		if (!(config3 & CONFIG3_THERM) ||
1343378933c9SJean Delvare 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1344378933c9SJean Delvare 			data->has_voltage |= (1 << 0);		/* in0 */
1345cffb9dd0SJean Delvare 	}
1346cffb9dd0SJean Delvare 
13479ed5bc24SGuenter Roeck 	/*
13489ed5bc24SGuenter Roeck 	 * On the ADT7476, the +12V input pin may instead be used as VID5,
13499ed5bc24SGuenter Roeck 	 * and VID pins may alternatively be used as GPIO
13509ed5bc24SGuenter Roeck 	 */
1351d8d2ee07SJean Delvare 	if (id->driver_data == adt7476) {
1352d8d2ee07SJean Delvare 		u8 vid = adt7475_read(REG_VID);
1353d8d2ee07SJean Delvare 		if (!(vid & VID_VIDSEL))
1354d8d2ee07SJean Delvare 			data->has_voltage |= (1 << 4);		/* in4 */
135554fe4671SJean Delvare 
135654fe4671SJean Delvare 		data->has_vid = !(adt7475_read(REG_CONFIG5) & CONFIG5_VIDGPIO);
1357d8d2ee07SJean Delvare 	}
1358d8d2ee07SJean Delvare 
1359ebfaf1fbSJean Delvare 	/* Voltage attenuators can be bypassed, globally or individually */
1360ebfaf1fbSJean Delvare 	config2 = adt7475_read(REG_CONFIG2);
1361ebfaf1fbSJean Delvare 	if (config2 & CONFIG2_ATTN) {
1362ebfaf1fbSJean Delvare 		data->bypass_attn = (0x3 << 3) | 0x3;
1363ebfaf1fbSJean Delvare 	} else {
1364ebfaf1fbSJean Delvare 		data->bypass_attn = ((data->config4 & CONFIG4_ATTN_IN10) >> 4) |
1365ebfaf1fbSJean Delvare 				    ((data->config4 & CONFIG4_ATTN_IN43) >> 3);
1366ebfaf1fbSJean Delvare 	}
1367ebfaf1fbSJean Delvare 	data->bypass_attn &= data->has_voltage;
1368ebfaf1fbSJean Delvare 
13699ed5bc24SGuenter Roeck 	/*
13709ed5bc24SGuenter Roeck 	 * Call adt7475_read_pwm for all pwm's as this will reprogram any
13719ed5bc24SGuenter Roeck 	 * pwm's which are disabled to manual mode with 0% duty cycle
13729ed5bc24SGuenter Roeck 	 */
13731c301fc5SJordan Crouse 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
13741c301fc5SJordan Crouse 		adt7475_read_pwm(client, i);
13751c301fc5SJordan Crouse 
1376*4abdf38dSChris Packham 	/* Start monitoring */
1377*4abdf38dSChris Packham 	switch (chip) {
1378*4abdf38dSChris Packham 	case adt7475:
1379*4abdf38dSChris Packham 	case adt7476:
1380*4abdf38dSChris Packham 		i2c_smbus_write_byte_data(client, REG_CONFIG1,
1381*4abdf38dSChris Packham 					  adt7475_read(REG_CONFIG1) | 0x01);
1382*4abdf38dSChris Packham 		break;
1383*4abdf38dSChris Packham 	default:
1384*4abdf38dSChris Packham 		break;
1385*4abdf38dSChris Packham 	}
1386*4abdf38dSChris Packham 
13871c301fc5SJordan Crouse 	ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
13881c301fc5SJordan Crouse 	if (ret)
1389e3ecb2eeSGuenter Roeck 		return ret;
13901c301fc5SJordan Crouse 
1391378933c9SJean Delvare 	/* Features that can be disabled individually */
1392378933c9SJean Delvare 	if (data->has_fan4) {
1393378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1394378933c9SJean Delvare 		if (ret)
1395378933c9SJean Delvare 			goto eremove;
1396378933c9SJean Delvare 	}
1397378933c9SJean Delvare 	if (data->has_pwm2) {
1398378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1399378933c9SJean Delvare 		if (ret)
1400378933c9SJean Delvare 			goto eremove;
1401378933c9SJean Delvare 	}
1402378933c9SJean Delvare 	if (data->has_voltage & (1 << 0)) {
1403378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1404378933c9SJean Delvare 		if (ret)
1405378933c9SJean Delvare 			goto eremove;
1406378933c9SJean Delvare 	}
1407d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 3)) {
1408d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in3_attr_group);
1409d8d2ee07SJean Delvare 		if (ret)
1410d8d2ee07SJean Delvare 			goto eremove;
1411d8d2ee07SJean Delvare 	}
1412d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 4)) {
1413d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in4_attr_group);
1414d8d2ee07SJean Delvare 		if (ret)
1415d8d2ee07SJean Delvare 			goto eremove;
1416d8d2ee07SJean Delvare 	}
1417d8d2ee07SJean Delvare 	if (data->has_voltage & (1 << 5)) {
1418d8d2ee07SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in5_attr_group);
1419d8d2ee07SJean Delvare 		if (ret)
1420d8d2ee07SJean Delvare 			goto eremove;
1421d8d2ee07SJean Delvare 	}
142254fe4671SJean Delvare 	if (data->has_vid) {
142354fe4671SJean Delvare 		data->vrm = vid_which_vrm();
142454fe4671SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &vid_attr_group);
142554fe4671SJean Delvare 		if (ret)
142654fe4671SJean Delvare 			goto eremove;
142754fe4671SJean Delvare 	}
1428378933c9SJean Delvare 
14291c301fc5SJordan Crouse 	data->hwmon_dev = hwmon_device_register(&client->dev);
14301c301fc5SJordan Crouse 	if (IS_ERR(data->hwmon_dev)) {
14311c301fc5SJordan Crouse 		ret = PTR_ERR(data->hwmon_dev);
14321c301fc5SJordan Crouse 		goto eremove;
14331c301fc5SJordan Crouse 	}
14341c301fc5SJordan Crouse 
1435d07ca4adSJean Delvare 	dev_info(&client->dev, "%s device, revision %d\n",
1436d07ca4adSJean Delvare 		 names[id->driver_data], revision);
1437d8d2ee07SJean Delvare 	if ((data->has_voltage & 0x11) || data->has_fan4 || data->has_pwm2)
143854fe4671SJean Delvare 		dev_info(&client->dev, "Optional features:%s%s%s%s%s\n",
1439d07ca4adSJean Delvare 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1440d8d2ee07SJean Delvare 			 (data->has_voltage & (1 << 4)) ? " in4" : "",
1441d07ca4adSJean Delvare 			 data->has_fan4 ? " fan4" : "",
144254fe4671SJean Delvare 			 data->has_pwm2 ? " pwm2" : "",
144354fe4671SJean Delvare 			 data->has_vid ? " vid" : "");
1444ebfaf1fbSJean Delvare 	if (data->bypass_attn)
1445ebfaf1fbSJean Delvare 		dev_info(&client->dev, "Bypassing attenuators on:%s%s%s%s\n",
1446ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 0)) ? " in0" : "",
1447ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 1)) ? " in1" : "",
1448ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 3)) ? " in3" : "",
1449ebfaf1fbSJean Delvare 			 (data->bypass_attn & (1 << 4)) ? " in4" : "");
1450d07ca4adSJean Delvare 
14511c301fc5SJordan Crouse 	return 0;
14521c301fc5SJordan Crouse 
14531c301fc5SJordan Crouse eremove:
14540f14480bSJean Delvare 	adt7475_remove_files(client, data);
14551c301fc5SJordan Crouse 	return ret;
14561c301fc5SJordan Crouse }
14571c301fc5SJordan Crouse 
14581c301fc5SJordan Crouse static int adt7475_remove(struct i2c_client *client)
14591c301fc5SJordan Crouse {
14601c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
14611c301fc5SJordan Crouse 
14621c301fc5SJordan Crouse 	hwmon_device_unregister(data->hwmon_dev);
14630f14480bSJean Delvare 	adt7475_remove_files(client, data);
14641c301fc5SJordan Crouse 
14651c301fc5SJordan Crouse 	return 0;
14661c301fc5SJordan Crouse }
14671c301fc5SJordan Crouse 
14681c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = {
14691c301fc5SJordan Crouse 	.class		= I2C_CLASS_HWMON,
14701c301fc5SJordan Crouse 	.driver = {
14711c301fc5SJordan Crouse 		.name	= "adt7475",
14724e2496e4SJavier Martinez Canillas 		.of_match_table = of_match_ptr(adt7475_of_match),
14731c301fc5SJordan Crouse 	},
14741c301fc5SJordan Crouse 	.probe		= adt7475_probe,
14751c301fc5SJordan Crouse 	.remove		= adt7475_remove,
14761c301fc5SJordan Crouse 	.id_table	= adt7475_id,
14771c301fc5SJordan Crouse 	.detect		= adt7475_detect,
1478c3813d6aSJean Delvare 	.address_list	= normal_i2c,
14791c301fc5SJordan Crouse };
14801c301fc5SJordan Crouse 
14811c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client)
14821c301fc5SJordan Crouse {
14831c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
14841c301fc5SJordan Crouse 
14851c301fc5SJordan Crouse 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
14861c301fc5SJordan Crouse 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
14871c301fc5SJordan Crouse 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
14881c301fc5SJordan Crouse }
14891c301fc5SJordan Crouse 
14901c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index)
14911c301fc5SJordan Crouse {
14921c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
14931c301fc5SJordan Crouse 	unsigned int v;
14941c301fc5SJordan Crouse 
14951c301fc5SJordan Crouse 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
14961c301fc5SJordan Crouse 
14979ed5bc24SGuenter Roeck 	/*
14989ed5bc24SGuenter Roeck 	 * Figure out the internal value for pwmctrl and pwmchan
14999ed5bc24SGuenter Roeck 	 * based on the current settings
15009ed5bc24SGuenter Roeck 	 */
15011c301fc5SJordan Crouse 	v = (data->pwm[CONTROL][index] >> 5) & 7;
15021c301fc5SJordan Crouse 
15031c301fc5SJordan Crouse 	if (v == 3)
15041c301fc5SJordan Crouse 		data->pwmctl[index] = 0;
15051c301fc5SJordan Crouse 	else if (v == 7)
15061c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
15071c301fc5SJordan Crouse 	else if (v == 4) {
15089ed5bc24SGuenter Roeck 		/*
15099ed5bc24SGuenter Roeck 		 * The fan is disabled - we don't want to
15109ed5bc24SGuenter Roeck 		 * support that, so change to manual mode and
15119ed5bc24SGuenter Roeck 		 * set the duty cycle to 0 instead
15121c301fc5SJordan Crouse 		 */
15131c301fc5SJordan Crouse 		data->pwm[INPUT][index] = 0;
15141c301fc5SJordan Crouse 		data->pwm[CONTROL][index] &= ~0xE0;
15151c301fc5SJordan Crouse 		data->pwm[CONTROL][index] |= (7 << 5);
15161c301fc5SJordan Crouse 
15171c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
15181c301fc5SJordan Crouse 					  data->pwm[INPUT][index]);
15191c301fc5SJordan Crouse 
15201c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
15211c301fc5SJordan Crouse 					  data->pwm[CONTROL][index]);
15221c301fc5SJordan Crouse 
15231c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
15241c301fc5SJordan Crouse 	} else {
15251c301fc5SJordan Crouse 		data->pwmctl[index] = 2;
15261c301fc5SJordan Crouse 
15271c301fc5SJordan Crouse 		switch (v) {
15281c301fc5SJordan Crouse 		case 0:
15291c301fc5SJordan Crouse 			data->pwmchan[index] = 1;
15301c301fc5SJordan Crouse 			break;
15311c301fc5SJordan Crouse 		case 1:
15321c301fc5SJordan Crouse 			data->pwmchan[index] = 2;
15331c301fc5SJordan Crouse 			break;
15341c301fc5SJordan Crouse 		case 2:
15351c301fc5SJordan Crouse 			data->pwmchan[index] = 4;
15361c301fc5SJordan Crouse 			break;
15371c301fc5SJordan Crouse 		case 5:
15381c301fc5SJordan Crouse 			data->pwmchan[index] = 6;
15391c301fc5SJordan Crouse 			break;
15401c301fc5SJordan Crouse 		case 6:
15411c301fc5SJordan Crouse 			data->pwmchan[index] = 7;
15421c301fc5SJordan Crouse 			break;
15431c301fc5SJordan Crouse 		}
15441c301fc5SJordan Crouse 	}
15451c301fc5SJordan Crouse }
15461c301fc5SJordan Crouse 
15471c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev)
15481c301fc5SJordan Crouse {
15491c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
15501c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
15513d849981SJean Delvare 	u16 ext;
15521c301fc5SJordan Crouse 	int i;
15531c301fc5SJordan Crouse 
15541c301fc5SJordan Crouse 	mutex_lock(&data->lock);
15551c301fc5SJordan Crouse 
15561c301fc5SJordan Crouse 	/* Measurement values update every 2 seconds */
15571c301fc5SJordan Crouse 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
15581c301fc5SJordan Crouse 	    !data->valid) {
15591c301fc5SJordan Crouse 		data->alarms = adt7475_read(REG_STATUS2) << 8;
15601c301fc5SJordan Crouse 		data->alarms |= adt7475_read(REG_STATUS1);
15611c301fc5SJordan Crouse 
15623d849981SJean Delvare 		ext = (adt7475_read(REG_EXTEND2) << 8) |
15633d849981SJean Delvare 			adt7475_read(REG_EXTEND1);
1564cffb9dd0SJean Delvare 		for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1565cffb9dd0SJean Delvare 			if (!(data->has_voltage & (1 << i)))
1566cffb9dd0SJean Delvare 				continue;
15671c301fc5SJordan Crouse 			data->voltage[INPUT][i] =
15681c301fc5SJordan Crouse 				(adt7475_read(VOLTAGE_REG(i)) << 2) |
1569cffb9dd0SJean Delvare 				((ext >> (i * 2)) & 3);
1570cffb9dd0SJean Delvare 		}
15711c301fc5SJordan Crouse 
15721c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_TEMP_COUNT; i++)
15731c301fc5SJordan Crouse 			data->temp[INPUT][i] =
15741c301fc5SJordan Crouse 				(adt7475_read(TEMP_REG(i)) << 2) |
15753d849981SJean Delvare 				((ext >> ((i + 5) * 2)) & 3);
15763d849981SJean Delvare 
15773d849981SJean Delvare 		if (data->has_voltage & (1 << 5)) {
15783d849981SJean Delvare 			data->alarms |= adt7475_read(REG_STATUS4) << 24;
15793d849981SJean Delvare 			ext = adt7475_read(REG_EXTEND3);
15803d849981SJean Delvare 			data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
15813d849981SJean Delvare 				((ext >> 4) & 3);
15823d849981SJean Delvare 		}
15831c301fc5SJordan Crouse 
1584378933c9SJean Delvare 		for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1585378933c9SJean Delvare 			if (i == 3 && !data->has_fan4)
1586378933c9SJean Delvare 				continue;
15871c301fc5SJordan Crouse 			data->tach[INPUT][i] =
15881c301fc5SJordan Crouse 				adt7475_read_word(client, TACH_REG(i));
1589378933c9SJean Delvare 		}
15901c301fc5SJordan Crouse 
15911c301fc5SJordan Crouse 		/* Updated by hw when in auto mode */
1592378933c9SJean Delvare 		for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1593378933c9SJean Delvare 			if (i == 1 && !data->has_pwm2)
1594378933c9SJean Delvare 				continue;
15951c301fc5SJordan Crouse 			data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
1596378933c9SJean Delvare 		}
15971c301fc5SJordan Crouse 
159854fe4671SJean Delvare 		if (data->has_vid)
159954fe4671SJean Delvare 			data->vid = adt7475_read(REG_VID) & 0x3f;
160054fe4671SJean Delvare 
16011c301fc5SJordan Crouse 		data->measure_updated = jiffies;
16021c301fc5SJordan Crouse 	}
16031c301fc5SJordan Crouse 
16041c301fc5SJordan Crouse 	/* Limits and settings, should never change update every 60 seconds */
160556e35eeeSJean Delvare 	if (time_after(jiffies, data->limits_updated + HZ * 60) ||
16061c301fc5SJordan Crouse 	    !data->valid) {
1607f99318b2SJean Delvare 		data->config4 = adt7475_read(REG_CONFIG4);
16081c301fc5SJordan Crouse 		data->config5 = adt7475_read(REG_CONFIG5);
16091c301fc5SJordan Crouse 
16101c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1611cffb9dd0SJean Delvare 			if (!(data->has_voltage & (1 << i)))
1612cffb9dd0SJean Delvare 				continue;
16131c301fc5SJordan Crouse 			/* Adjust values so they match the input precision */
16141c301fc5SJordan Crouse 			data->voltage[MIN][i] =
16151c301fc5SJordan Crouse 				adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
16161c301fc5SJordan Crouse 			data->voltage[MAX][i] =
16171c301fc5SJordan Crouse 				adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
16181c301fc5SJordan Crouse 		}
16191c301fc5SJordan Crouse 
16203d849981SJean Delvare 		if (data->has_voltage & (1 << 5)) {
16213d849981SJean Delvare 			data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
16223d849981SJean Delvare 			data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
16233d849981SJean Delvare 		}
16243d849981SJean Delvare 
16251c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
16261c301fc5SJordan Crouse 			/* Adjust values so they match the input precision */
16271c301fc5SJordan Crouse 			data->temp[MIN][i] =
16281c301fc5SJordan Crouse 				adt7475_read(TEMP_MIN_REG(i)) << 2;
16291c301fc5SJordan Crouse 			data->temp[MAX][i] =
16301c301fc5SJordan Crouse 				adt7475_read(TEMP_MAX_REG(i)) << 2;
16311c301fc5SJordan Crouse 			data->temp[AUTOMIN][i] =
16321c301fc5SJordan Crouse 				adt7475_read(TEMP_TMIN_REG(i)) << 2;
16331c301fc5SJordan Crouse 			data->temp[THERM][i] =
16341c301fc5SJordan Crouse 				adt7475_read(TEMP_THERM_REG(i)) << 2;
16351c301fc5SJordan Crouse 			data->temp[OFFSET][i] =
16361c301fc5SJordan Crouse 				adt7475_read(TEMP_OFFSET_REG(i));
16371c301fc5SJordan Crouse 		}
16381c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
16391c301fc5SJordan Crouse 
1640378933c9SJean Delvare 		for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1641378933c9SJean Delvare 			if (i == 3 && !data->has_fan4)
1642378933c9SJean Delvare 				continue;
16431c301fc5SJordan Crouse 			data->tach[MIN][i] =
16441c301fc5SJordan Crouse 				adt7475_read_word(client, TACH_MIN_REG(i));
1645378933c9SJean Delvare 		}
16461c301fc5SJordan Crouse 
16471c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1648378933c9SJean Delvare 			if (i == 1 && !data->has_pwm2)
1649378933c9SJean Delvare 				continue;
16501c301fc5SJordan Crouse 			data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
16511c301fc5SJordan Crouse 			data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
16521c301fc5SJordan Crouse 			/* Set the channel and control information */
16531c301fc5SJordan Crouse 			adt7475_read_pwm(client, i);
16541c301fc5SJordan Crouse 		}
16551c301fc5SJordan Crouse 
16561c301fc5SJordan Crouse 		data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
16571c301fc5SJordan Crouse 		data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
16581c301fc5SJordan Crouse 		data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
16591c301fc5SJordan Crouse 
16601c301fc5SJordan Crouse 		data->limits_updated = jiffies;
16611c301fc5SJordan Crouse 		data->valid = 1;
16621c301fc5SJordan Crouse 	}
16631c301fc5SJordan Crouse 
16641c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
16651c301fc5SJordan Crouse 
16661c301fc5SJordan Crouse 	return data;
16671c301fc5SJordan Crouse }
16681c301fc5SJordan Crouse 
1669f0967eeaSAxel Lin module_i2c_driver(adt7475_driver);
16701c301fc5SJordan Crouse 
16711c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc");
16721c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver");
16731c301fc5SJordan Crouse MODULE_LICENSE("GPL");
1674