xref: /linux/drivers/hwmon/adt7475.c (revision d07ca4ad2f67ebb1bf48a2308a7a0cd81ef8f70a)
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>
63d849981SJean Delvare  * Copyright (C) 2009 Jean Delvare <khali@linux-fr.org>
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>
161c301fc5SJordan Crouse #include <linux/init.h>
171c301fc5SJordan Crouse #include <linux/slab.h>
181c301fc5SJordan Crouse #include <linux/i2c.h>
191c301fc5SJordan Crouse #include <linux/hwmon.h>
201c301fc5SJordan Crouse #include <linux/hwmon-sysfs.h>
211c301fc5SJordan Crouse #include <linux/err.h>
221c301fc5SJordan Crouse 
231c301fc5SJordan Crouse /* Indexes for the sysfs hooks */
241c301fc5SJordan Crouse 
251c301fc5SJordan Crouse #define INPUT		0
261c301fc5SJordan Crouse #define MIN		1
271c301fc5SJordan Crouse #define MAX		2
281c301fc5SJordan Crouse #define CONTROL		3
291c301fc5SJordan Crouse #define OFFSET		3
301c301fc5SJordan Crouse #define AUTOMIN		4
311c301fc5SJordan Crouse #define THERM		5
321c301fc5SJordan Crouse #define HYSTERSIS	6
331c301fc5SJordan Crouse 
341c301fc5SJordan Crouse /* These are unique identifiers for the sysfs functions - unlike the
351c301fc5SJordan Crouse    numbers above, these are not also indexes into an array
361c301fc5SJordan Crouse */
371c301fc5SJordan Crouse 
381c301fc5SJordan Crouse #define ALARM		9
391c301fc5SJordan Crouse #define FAULT		10
401c301fc5SJordan Crouse 
411c301fc5SJordan Crouse /* 7475 Common Registers */
421c301fc5SJordan Crouse 
43*d07ca4adSJean Delvare #define REG_DEVREV2		0x12	/* ADT7490 only */
44*d07ca4adSJean Delvare 
453d849981SJean Delvare #define REG_VTT			0x1E	/* ADT7490 only */
463d849981SJean Delvare #define REG_EXTEND3		0x1F	/* ADT7490 only */
473d849981SJean Delvare 
48cffb9dd0SJean Delvare #define REG_VOLTAGE_BASE	0x20
491c301fc5SJordan Crouse #define REG_TEMP_BASE		0x25
501c301fc5SJordan Crouse #define REG_TACH_BASE		0x28
511c301fc5SJordan Crouse #define REG_PWM_BASE		0x30
521c301fc5SJordan Crouse #define REG_PWM_MAX_BASE	0x38
531c301fc5SJordan Crouse 
541c301fc5SJordan Crouse #define REG_DEVID		0x3D
551c301fc5SJordan Crouse #define REG_VENDID		0x3E
56d656b6fdSJean Delvare #define REG_DEVID2		0x3F
571c301fc5SJordan Crouse 
581c301fc5SJordan Crouse #define REG_STATUS1		0x41
591c301fc5SJordan Crouse #define REG_STATUS2		0x42
601c301fc5SJordan Crouse 
61cffb9dd0SJean Delvare #define REG_VOLTAGE_MIN_BASE	0x44
62cffb9dd0SJean Delvare #define REG_VOLTAGE_MAX_BASE	0x45
631c301fc5SJordan Crouse 
641c301fc5SJordan Crouse #define REG_TEMP_MIN_BASE	0x4E
651c301fc5SJordan Crouse #define REG_TEMP_MAX_BASE	0x4F
661c301fc5SJordan Crouse 
671c301fc5SJordan Crouse #define REG_TACH_MIN_BASE	0x54
681c301fc5SJordan Crouse 
691c301fc5SJordan Crouse #define REG_PWM_CONFIG_BASE	0x5C
701c301fc5SJordan Crouse 
711c301fc5SJordan Crouse #define REG_TEMP_TRANGE_BASE	0x5F
721c301fc5SJordan Crouse 
731c301fc5SJordan Crouse #define REG_PWM_MIN_BASE	0x64
741c301fc5SJordan Crouse 
751c301fc5SJordan Crouse #define REG_TEMP_TMIN_BASE	0x67
761c301fc5SJordan Crouse #define REG_TEMP_THERM_BASE	0x6A
771c301fc5SJordan Crouse 
781c301fc5SJordan Crouse #define REG_REMOTE1_HYSTERSIS	0x6D
791c301fc5SJordan Crouse #define REG_REMOTE2_HYSTERSIS	0x6E
801c301fc5SJordan Crouse 
811c301fc5SJordan Crouse #define REG_TEMP_OFFSET_BASE	0x70
821c301fc5SJordan Crouse 
831c301fc5SJordan Crouse #define REG_EXTEND1		0x76
841c301fc5SJordan Crouse #define REG_EXTEND2		0x77
85378933c9SJean Delvare 
86378933c9SJean Delvare #define REG_CONFIG3		0x78
871c301fc5SJordan Crouse #define REG_CONFIG5		0x7C
88f99318b2SJean Delvare #define REG_CONFIG4		0x7D
89f99318b2SJean Delvare 
903d849981SJean Delvare #define REG_STATUS4		0x81	/* ADT7490 only */
913d849981SJean Delvare 
923d849981SJean Delvare #define REG_VTT_MIN		0x84	/* ADT7490 only */
933d849981SJean Delvare #define REG_VTT_MAX		0x86	/* ADT7490 only */
943d849981SJean Delvare 
95378933c9SJean Delvare #define CONFIG3_SMBALERT	0x01
96378933c9SJean Delvare #define CONFIG3_THERM		0x02
97378933c9SJean Delvare 
98378933c9SJean Delvare #define CONFIG4_PINFUNC		0x03
99f99318b2SJean Delvare #define CONFIG4_MAXDUTY		0x08
1001c301fc5SJordan Crouse 
1011c301fc5SJordan Crouse #define CONFIG5_TWOSCOMP	0x01
1021c301fc5SJordan Crouse #define CONFIG5_TEMPOFFSET	0x02
1031c301fc5SJordan Crouse 
1041c301fc5SJordan Crouse /* ADT7475 Settings */
1051c301fc5SJordan Crouse 
106cffb9dd0SJean Delvare #define ADT7475_VOLTAGE_COUNT	5	/* Not counting Vtt */
1071c301fc5SJordan Crouse #define ADT7475_TEMP_COUNT	3
1081c301fc5SJordan Crouse #define ADT7475_TACH_COUNT	4
1091c301fc5SJordan Crouse #define ADT7475_PWM_COUNT	3
1101c301fc5SJordan Crouse 
1111c301fc5SJordan Crouse /* Macro to read the registers */
1121c301fc5SJordan Crouse 
1131c301fc5SJordan Crouse #define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
1141c301fc5SJordan Crouse 
1151c301fc5SJordan Crouse /* Macros to easily index the registers */
1161c301fc5SJordan Crouse 
1171c301fc5SJordan Crouse #define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
1181c301fc5SJordan Crouse #define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
1191c301fc5SJordan Crouse 
1201c301fc5SJordan Crouse #define PWM_REG(idx) (REG_PWM_BASE + (idx))
1211c301fc5SJordan Crouse #define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
1221c301fc5SJordan Crouse #define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
1231c301fc5SJordan Crouse #define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
1241c301fc5SJordan Crouse 
1251c301fc5SJordan Crouse #define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
1261c301fc5SJordan Crouse #define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
1271c301fc5SJordan Crouse #define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
1281c301fc5SJordan Crouse 
1291c301fc5SJordan Crouse #define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
1301c301fc5SJordan Crouse #define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
1311c301fc5SJordan Crouse #define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
1321c301fc5SJordan Crouse #define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
1331c301fc5SJordan Crouse #define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
1341c301fc5SJordan Crouse #define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
1351c301fc5SJordan Crouse #define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
1361c301fc5SJordan Crouse 
137b180d050SJean Delvare static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
1381c301fc5SJordan Crouse 
1393d849981SJean Delvare I2C_CLIENT_INSMOD_3(adt7473, adt7475, adt7490);
1401c301fc5SJordan Crouse 
1411c301fc5SJordan Crouse static const struct i2c_device_id adt7475_id[] = {
142b180d050SJean Delvare 	{ "adt7473", adt7473 },
1431c301fc5SJordan Crouse 	{ "adt7475", adt7475 },
1443d849981SJean Delvare 	{ "adt7490", adt7490 },
1451c301fc5SJordan Crouse 	{ }
1461c301fc5SJordan Crouse };
1471c301fc5SJordan Crouse MODULE_DEVICE_TABLE(i2c, adt7475_id);
1481c301fc5SJordan Crouse 
1491c301fc5SJordan Crouse struct adt7475_data {
1501c301fc5SJordan Crouse 	struct device *hwmon_dev;
1511c301fc5SJordan Crouse 	struct mutex lock;
1521c301fc5SJordan Crouse 
1531c301fc5SJordan Crouse 	unsigned long measure_updated;
1541c301fc5SJordan Crouse 	unsigned long limits_updated;
1551c301fc5SJordan Crouse 	char valid;
1561c301fc5SJordan Crouse 
157f99318b2SJean Delvare 	u8 config4;
1581c301fc5SJordan Crouse 	u8 config5;
159cffb9dd0SJean Delvare 	u8 has_voltage;
160378933c9SJean Delvare 	u8 has_pwm2:1;
161378933c9SJean Delvare 	u8 has_fan4:1;
1623d849981SJean Delvare 	u32 alarms;
163cffb9dd0SJean Delvare 	u16 voltage[3][6];
1641c301fc5SJordan Crouse 	u16 temp[7][3];
1651c301fc5SJordan Crouse 	u16 tach[2][4];
1661c301fc5SJordan Crouse 	u8 pwm[4][3];
1671c301fc5SJordan Crouse 	u8 range[3];
1681c301fc5SJordan Crouse 	u8 pwmctl[3];
1691c301fc5SJordan Crouse 	u8 pwmchan[3];
1701c301fc5SJordan Crouse };
1711c301fc5SJordan Crouse 
1721c301fc5SJordan Crouse static struct i2c_driver adt7475_driver;
1731c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev);
1741c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client);
1751c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index);
1761c301fc5SJordan Crouse 
1771c301fc5SJordan Crouse /* Given a temp value, convert it to register value */
1781c301fc5SJordan Crouse 
1791c301fc5SJordan Crouse static inline u16 temp2reg(struct adt7475_data *data, long val)
1801c301fc5SJordan Crouse {
1811c301fc5SJordan Crouse 	u16 ret;
1821c301fc5SJordan Crouse 
1831c301fc5SJordan Crouse 	if (!(data->config5 & CONFIG5_TWOSCOMP)) {
1841c301fc5SJordan Crouse 		val = SENSORS_LIMIT(val, -64000, 191000);
1851c301fc5SJordan Crouse 		ret = (val + 64500) / 1000;
1861c301fc5SJordan Crouse 	} else {
1871c301fc5SJordan Crouse 		val = SENSORS_LIMIT(val, -128000, 127000);
1881c301fc5SJordan Crouse 		if (val < -500)
1891c301fc5SJordan Crouse 			ret = (256500 + val) / 1000;
1901c301fc5SJordan Crouse 		else
1911c301fc5SJordan Crouse 			ret = (val + 500) / 1000;
1921c301fc5SJordan Crouse 	}
1931c301fc5SJordan Crouse 
1941c301fc5SJordan Crouse 	return ret << 2;
1951c301fc5SJordan Crouse }
1961c301fc5SJordan Crouse 
1971c301fc5SJordan Crouse /* Given a register value, convert it to a real temp value */
1981c301fc5SJordan Crouse 
1991c301fc5SJordan Crouse static inline int reg2temp(struct adt7475_data *data, u16 reg)
2001c301fc5SJordan Crouse {
2011c301fc5SJordan Crouse 	if (data->config5 & CONFIG5_TWOSCOMP) {
2021c301fc5SJordan Crouse 		if (reg >= 512)
2031c301fc5SJordan Crouse 			return (reg - 1024) * 250;
2041c301fc5SJordan Crouse 		else
2051c301fc5SJordan Crouse 			return reg * 250;
2061c301fc5SJordan Crouse 	} else
2071c301fc5SJordan Crouse 		return (reg - 256) * 250;
2081c301fc5SJordan Crouse }
2091c301fc5SJordan Crouse 
2101c301fc5SJordan Crouse static inline int tach2rpm(u16 tach)
2111c301fc5SJordan Crouse {
2121c301fc5SJordan Crouse 	if (tach == 0 || tach == 0xFFFF)
2131c301fc5SJordan Crouse 		return 0;
2141c301fc5SJordan Crouse 
2151c301fc5SJordan Crouse 	return (90000 * 60) / tach;
2161c301fc5SJordan Crouse }
2171c301fc5SJordan Crouse 
2181c301fc5SJordan Crouse static inline u16 rpm2tach(unsigned long rpm)
2191c301fc5SJordan Crouse {
2201c301fc5SJordan Crouse 	if (rpm == 0)
2211c301fc5SJordan Crouse 		return 0;
2221c301fc5SJordan Crouse 
2231c301fc5SJordan Crouse 	return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
2241c301fc5SJordan Crouse }
2251c301fc5SJordan Crouse 
226cffb9dd0SJean Delvare /* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
227cffb9dd0SJean Delvare static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
228cffb9dd0SJean Delvare 	{ 45, 94 },	/* +2.5V */
229cffb9dd0SJean Delvare 	{ 175, 525 },	/* Vccp */
230cffb9dd0SJean Delvare 	{ 68, 71 },	/* Vcc */
231cffb9dd0SJean Delvare 	{ 93, 47 },	/* +5V */
232cffb9dd0SJean Delvare 	{ 120, 20 },	/* +12V */
233cffb9dd0SJean Delvare 	{ 45, 45 },	/* Vtt */
234cffb9dd0SJean Delvare };
235cffb9dd0SJean Delvare 
236cffb9dd0SJean Delvare static inline int reg2volt(int channel, u16 reg)
2371c301fc5SJordan Crouse {
238cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
239cffb9dd0SJean Delvare 
240cffb9dd0SJean Delvare 	return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
2411c301fc5SJordan Crouse }
2421c301fc5SJordan Crouse 
243cffb9dd0SJean Delvare static inline u16 volt2reg(int channel, long volt)
2441c301fc5SJordan Crouse {
245cffb9dd0SJean Delvare 	const int *r = adt7473_in_scaling[channel];
246cffb9dd0SJean Delvare 	long reg;
2471c301fc5SJordan Crouse 
248cffb9dd0SJean Delvare 	reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
249cffb9dd0SJean Delvare 	return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
2501c301fc5SJordan Crouse }
2511c301fc5SJordan Crouse 
2521c301fc5SJordan Crouse static u16 adt7475_read_word(struct i2c_client *client, int reg)
2531c301fc5SJordan Crouse {
2541c301fc5SJordan Crouse 	u16 val;
2551c301fc5SJordan Crouse 
2561c301fc5SJordan Crouse 	val = i2c_smbus_read_byte_data(client, reg);
2571c301fc5SJordan Crouse 	val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
2581c301fc5SJordan Crouse 
2591c301fc5SJordan Crouse 	return val;
2601c301fc5SJordan Crouse }
2611c301fc5SJordan Crouse 
2621c301fc5SJordan Crouse static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
2631c301fc5SJordan Crouse {
2641c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
2651c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, val & 0xFF);
2661c301fc5SJordan Crouse }
2671c301fc5SJordan Crouse 
2681c301fc5SJordan Crouse /* Find the nearest value in a table - used for pwm frequency and
2691c301fc5SJordan Crouse    auto temp range */
2701c301fc5SJordan Crouse static int find_nearest(long val, const int *array, int size)
2711c301fc5SJordan Crouse {
2721c301fc5SJordan Crouse 	int i;
2731c301fc5SJordan Crouse 
2741c301fc5SJordan Crouse 	if (val < array[0])
2751c301fc5SJordan Crouse 		return 0;
2761c301fc5SJordan Crouse 
2771c301fc5SJordan Crouse 	if (val > array[size - 1])
2781c301fc5SJordan Crouse 		return size - 1;
2791c301fc5SJordan Crouse 
2801c301fc5SJordan Crouse 	for (i = 0; i < size - 1; i++) {
2811c301fc5SJordan Crouse 		int a, b;
2821c301fc5SJordan Crouse 
2831c301fc5SJordan Crouse 		if (val > array[i + 1])
2841c301fc5SJordan Crouse 			continue;
2851c301fc5SJordan Crouse 
2861c301fc5SJordan Crouse 		a = val - array[i];
2871c301fc5SJordan Crouse 		b = array[i + 1] - val;
2881c301fc5SJordan Crouse 
2891c301fc5SJordan Crouse 		return (a <= b) ? i : i + 1;
2901c301fc5SJordan Crouse 	}
2911c301fc5SJordan Crouse 
2921c301fc5SJordan Crouse 	return 0;
2931c301fc5SJordan Crouse }
2941c301fc5SJordan Crouse 
2951c301fc5SJordan Crouse static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
2961c301fc5SJordan Crouse 			    char *buf)
2971c301fc5SJordan Crouse {
2981c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
2991c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3001c301fc5SJordan Crouse 	unsigned short val;
3011c301fc5SJordan Crouse 
3021c301fc5SJordan Crouse 	switch (sattr->nr) {
3031c301fc5SJordan Crouse 	case ALARM:
3041c301fc5SJordan Crouse 		return sprintf(buf, "%d\n",
305cffb9dd0SJean Delvare 			       (data->alarms >> sattr->index) & 1);
3061c301fc5SJordan Crouse 	default:
3071c301fc5SJordan Crouse 		val = data->voltage[sattr->nr][sattr->index];
308cffb9dd0SJean Delvare 		return sprintf(buf, "%d\n", reg2volt(sattr->index, val));
3091c301fc5SJordan Crouse 	}
3101c301fc5SJordan Crouse }
3111c301fc5SJordan Crouse 
3121c301fc5SJordan Crouse static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
3131c301fc5SJordan Crouse 			   const char *buf, size_t count)
3141c301fc5SJordan Crouse {
3151c301fc5SJordan Crouse 
3161c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3171c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
3181c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
3191c301fc5SJordan Crouse 	unsigned char reg;
3201c301fc5SJordan Crouse 	long val;
3211c301fc5SJordan Crouse 
3221c301fc5SJordan Crouse 	if (strict_strtol(buf, 10, &val))
3231c301fc5SJordan Crouse 		return -EINVAL;
3241c301fc5SJordan Crouse 
3251c301fc5SJordan Crouse 	mutex_lock(&data->lock);
3261c301fc5SJordan Crouse 
327cffb9dd0SJean Delvare 	data->voltage[sattr->nr][sattr->index] = volt2reg(sattr->index, val);
3281c301fc5SJordan Crouse 
3293d849981SJean Delvare 	if (sattr->index < ADT7475_VOLTAGE_COUNT) {
3301c301fc5SJordan Crouse 		if (sattr->nr == MIN)
3311c301fc5SJordan Crouse 			reg = VOLTAGE_MIN_REG(sattr->index);
3321c301fc5SJordan Crouse 		else
3331c301fc5SJordan Crouse 			reg = VOLTAGE_MAX_REG(sattr->index);
3343d849981SJean Delvare 	} else {
3353d849981SJean Delvare 		if (sattr->nr == MIN)
3363d849981SJean Delvare 			reg = REG_VTT_MIN;
3373d849981SJean Delvare 		else
3383d849981SJean Delvare 			reg = REG_VTT_MAX;
3393d849981SJean Delvare 	}
3401c301fc5SJordan Crouse 
3411c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
3421c301fc5SJordan Crouse 				  data->voltage[sattr->nr][sattr->index] >> 2);
3431c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
3441c301fc5SJordan Crouse 
3451c301fc5SJordan Crouse 	return count;
3461c301fc5SJordan Crouse }
3471c301fc5SJordan Crouse 
3481c301fc5SJordan Crouse static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
3491c301fc5SJordan Crouse 			 char *buf)
3501c301fc5SJordan Crouse {
3511c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
3521c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
3531c301fc5SJordan Crouse 	int out;
3541c301fc5SJordan Crouse 
3551c301fc5SJordan Crouse 	switch (sattr->nr) {
3561c301fc5SJordan Crouse 	case HYSTERSIS:
3571c301fc5SJordan Crouse 		mutex_lock(&data->lock);
3581c301fc5SJordan Crouse 		out = data->temp[sattr->nr][sattr->index];
3591c301fc5SJordan Crouse 		if (sattr->index != 1)
3601c301fc5SJordan Crouse 			out = (out >> 4) & 0xF;
3611c301fc5SJordan Crouse 		else
3621c301fc5SJordan Crouse 			out = (out & 0xF);
3631c301fc5SJordan Crouse 		/* Show the value as an absolute number tied to
3641c301fc5SJordan Crouse 		 * THERM */
3651c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[THERM][sattr->index]) -
3661c301fc5SJordan Crouse 			out * 1000;
3671c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
3681c301fc5SJordan Crouse 		break;
3691c301fc5SJordan Crouse 
3701c301fc5SJordan Crouse 	case OFFSET:
3711c301fc5SJordan Crouse 		/* Offset is always 2's complement, regardless of the
3721c301fc5SJordan Crouse 		 * setting in CONFIG5 */
3731c301fc5SJordan Crouse 		mutex_lock(&data->lock);
3741c301fc5SJordan Crouse 		out = (s8)data->temp[sattr->nr][sattr->index];
3751c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET)
3761c301fc5SJordan Crouse 			out *= 1000;
3771c301fc5SJordan Crouse 		else
3781c301fc5SJordan Crouse 			out *= 500;
3791c301fc5SJordan Crouse 		mutex_unlock(&data->lock);
3801c301fc5SJordan Crouse 		break;
3811c301fc5SJordan Crouse 
3821c301fc5SJordan Crouse 	case ALARM:
3831c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 4)) & 1;
3841c301fc5SJordan Crouse 		break;
3851c301fc5SJordan Crouse 
3861c301fc5SJordan Crouse 	case FAULT:
3871c301fc5SJordan Crouse 		/* Note - only for remote1 and remote2 */
388cf312e07SJean Delvare 		out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
3891c301fc5SJordan Crouse 		break;
3901c301fc5SJordan Crouse 
3911c301fc5SJordan Crouse 	default:
3921c301fc5SJordan Crouse 		/* All other temp values are in the configured format */
3931c301fc5SJordan Crouse 		out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
3941c301fc5SJordan Crouse 	}
3951c301fc5SJordan Crouse 
3961c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
3971c301fc5SJordan Crouse }
3981c301fc5SJordan Crouse 
3991c301fc5SJordan Crouse static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
4001c301fc5SJordan Crouse 			const char *buf, size_t count)
4011c301fc5SJordan Crouse {
4021c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
4031c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
4041c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
4051c301fc5SJordan Crouse 	unsigned char reg = 0;
4061c301fc5SJordan Crouse 	u8 out;
4071c301fc5SJordan Crouse 	int temp;
4081c301fc5SJordan Crouse 	long val;
4091c301fc5SJordan Crouse 
4101c301fc5SJordan Crouse 	if (strict_strtol(buf, 10, &val))
4111c301fc5SJordan Crouse 		return -EINVAL;
4121c301fc5SJordan Crouse 
4131c301fc5SJordan Crouse 	mutex_lock(&data->lock);
4141c301fc5SJordan Crouse 
4151c301fc5SJordan Crouse 	/* We need the config register in all cases for temp <-> reg conv. */
4161c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
4171c301fc5SJordan Crouse 
4181c301fc5SJordan Crouse 	switch (sattr->nr) {
4191c301fc5SJordan Crouse 	case OFFSET:
4201c301fc5SJordan Crouse 		if (data->config5 & CONFIG5_TEMPOFFSET) {
4211c301fc5SJordan Crouse 			val = SENSORS_LIMIT(val, -63000, 127000);
4221c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 1000;
4231c301fc5SJordan Crouse 		} else {
4241c301fc5SJordan Crouse 			val = SENSORS_LIMIT(val, -63000, 64000);
4251c301fc5SJordan Crouse 			out = data->temp[OFFSET][sattr->index] = val / 500;
4261c301fc5SJordan Crouse 		}
4271c301fc5SJordan Crouse 		break;
4281c301fc5SJordan Crouse 
4291c301fc5SJordan Crouse 	case HYSTERSIS:
4301c301fc5SJordan Crouse 		/* The value will be given as an absolute value, turn it
4311c301fc5SJordan Crouse 		   into an offset based on THERM */
4321c301fc5SJordan Crouse 
4331c301fc5SJordan Crouse 		/* Read fresh THERM and HYSTERSIS values from the chip */
4341c301fc5SJordan Crouse 		data->temp[THERM][sattr->index] =
4351c301fc5SJordan Crouse 			adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
4361c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
4371c301fc5SJordan Crouse 
4381c301fc5SJordan Crouse 		temp = reg2temp(data, data->temp[THERM][sattr->index]);
4391c301fc5SJordan Crouse 		val = SENSORS_LIMIT(val, temp - 15000, temp);
4401c301fc5SJordan Crouse 		val = (temp - val) / 1000;
4411c301fc5SJordan Crouse 
4421c301fc5SJordan Crouse 		if (sattr->index != 1) {
4431c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0xF0;
4441c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
4451c301fc5SJordan Crouse 		} else {
4461c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] &= 0x0F;
4471c301fc5SJordan Crouse 			data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
4481c301fc5SJordan Crouse 		}
4491c301fc5SJordan Crouse 
4501c301fc5SJordan Crouse 		out = data->temp[HYSTERSIS][sattr->index];
4511c301fc5SJordan Crouse 		break;
4521c301fc5SJordan Crouse 
4531c301fc5SJordan Crouse 	default:
4541c301fc5SJordan Crouse 		data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
4551c301fc5SJordan Crouse 
4561c301fc5SJordan Crouse 		/* We maintain an extra 2 digits of precision for simplicity
4571c301fc5SJordan Crouse 		 * - shift those back off before writing the value */
4581c301fc5SJordan Crouse 		out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
4591c301fc5SJordan Crouse 	}
4601c301fc5SJordan Crouse 
4611c301fc5SJordan Crouse 	switch (sattr->nr) {
4621c301fc5SJordan Crouse 	case MIN:
4631c301fc5SJordan Crouse 		reg = TEMP_MIN_REG(sattr->index);
4641c301fc5SJordan Crouse 		break;
4651c301fc5SJordan Crouse 	case MAX:
4661c301fc5SJordan Crouse 		reg = TEMP_MAX_REG(sattr->index);
4671c301fc5SJordan Crouse 		break;
4681c301fc5SJordan Crouse 	case OFFSET:
4691c301fc5SJordan Crouse 		reg = TEMP_OFFSET_REG(sattr->index);
4701c301fc5SJordan Crouse 		break;
4711c301fc5SJordan Crouse 	case AUTOMIN:
4721c301fc5SJordan Crouse 		reg = TEMP_TMIN_REG(sattr->index);
4731c301fc5SJordan Crouse 		break;
4741c301fc5SJordan Crouse 	case THERM:
4751c301fc5SJordan Crouse 		reg = TEMP_THERM_REG(sattr->index);
4761c301fc5SJordan Crouse 		break;
4771c301fc5SJordan Crouse 	case HYSTERSIS:
4781c301fc5SJordan Crouse 		if (sattr->index != 2)
4791c301fc5SJordan Crouse 			reg = REG_REMOTE1_HYSTERSIS;
4801c301fc5SJordan Crouse 		else
4811c301fc5SJordan Crouse 			reg = REG_REMOTE2_HYSTERSIS;
4821c301fc5SJordan Crouse 
4831c301fc5SJordan Crouse 		break;
4841c301fc5SJordan Crouse 	}
4851c301fc5SJordan Crouse 
4861c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg, out);
4871c301fc5SJordan Crouse 
4881c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
4891c301fc5SJordan Crouse 	return count;
4901c301fc5SJordan Crouse }
4911c301fc5SJordan Crouse 
4921c301fc5SJordan Crouse /* Table of autorange values - the user will write the value in millidegrees,
4931c301fc5SJordan Crouse    and we'll convert it */
4941c301fc5SJordan Crouse static const int autorange_table[] = {
4951c301fc5SJordan Crouse 	2000, 2500, 3330, 4000, 5000, 6670, 8000,
4961c301fc5SJordan Crouse 	10000, 13330, 16000, 20000, 26670, 32000, 40000,
4971c301fc5SJordan Crouse 	53330, 80000
4981c301fc5SJordan Crouse };
4991c301fc5SJordan Crouse 
5001c301fc5SJordan Crouse static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
5011c301fc5SJordan Crouse 			   char *buf)
5021c301fc5SJordan Crouse {
5031c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
5041c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5051c301fc5SJordan Crouse 	int out, val;
5061c301fc5SJordan Crouse 
5071c301fc5SJordan Crouse 	mutex_lock(&data->lock);
5081c301fc5SJordan Crouse 	out = (data->range[sattr->index] >> 4) & 0x0F;
5091c301fc5SJordan Crouse 	val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
5101c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5111c301fc5SJordan Crouse 
5121c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", val + autorange_table[out]);
5131c301fc5SJordan Crouse }
5141c301fc5SJordan Crouse 
5151c301fc5SJordan Crouse static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
5161c301fc5SJordan Crouse 			  const char *buf, size_t count)
5171c301fc5SJordan Crouse {
5181c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
5191c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
5201c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5211c301fc5SJordan Crouse 	int temp;
5221c301fc5SJordan Crouse 	long val;
5231c301fc5SJordan Crouse 
5241c301fc5SJordan Crouse 	if (strict_strtol(buf, 10, &val))
5251c301fc5SJordan Crouse 		return -EINVAL;
5261c301fc5SJordan Crouse 
5271c301fc5SJordan Crouse 	mutex_lock(&data->lock);
5281c301fc5SJordan Crouse 
5291c301fc5SJordan Crouse 	/* Get a fresh copy of the needed registers */
5301c301fc5SJordan Crouse 	data->config5 = adt7475_read(REG_CONFIG5);
5311c301fc5SJordan Crouse 	data->temp[AUTOMIN][sattr->index] =
5321c301fc5SJordan Crouse 		adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
5331c301fc5SJordan Crouse 	data->range[sattr->index] =
5341c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
5351c301fc5SJordan Crouse 
5361c301fc5SJordan Crouse 	/* The user will write an absolute value, so subtract the start point
5371c301fc5SJordan Crouse 	   to figure the range */
5381c301fc5SJordan Crouse 	temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
5391c301fc5SJordan Crouse 	val = SENSORS_LIMIT(val, temp + autorange_table[0],
5401c301fc5SJordan Crouse 		temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
5411c301fc5SJordan Crouse 	val -= temp;
5421c301fc5SJordan Crouse 
5431c301fc5SJordan Crouse 	/* Find the nearest table entry to what the user wrote */
5441c301fc5SJordan Crouse 	val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
5451c301fc5SJordan Crouse 
5461c301fc5SJordan Crouse 	data->range[sattr->index] &= ~0xF0;
5471c301fc5SJordan Crouse 	data->range[sattr->index] |= val << 4;
5481c301fc5SJordan Crouse 
5491c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
5501c301fc5SJordan Crouse 				  data->range[sattr->index]);
5511c301fc5SJordan Crouse 
5521c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5531c301fc5SJordan Crouse 	return count;
5541c301fc5SJordan Crouse }
5551c301fc5SJordan Crouse 
5561c301fc5SJordan Crouse static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
5571c301fc5SJordan Crouse 			 char *buf)
5581c301fc5SJordan Crouse {
5591c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
5601c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5611c301fc5SJordan Crouse 	int out;
5621c301fc5SJordan Crouse 
5631c301fc5SJordan Crouse 	if (sattr->nr == ALARM)
5641c301fc5SJordan Crouse 		out = (data->alarms >> (sattr->index + 10)) & 1;
5651c301fc5SJordan Crouse 	else
5661c301fc5SJordan Crouse 		out = tach2rpm(data->tach[sattr->nr][sattr->index]);
5671c301fc5SJordan Crouse 
5681c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", out);
5691c301fc5SJordan Crouse }
5701c301fc5SJordan Crouse 
5711c301fc5SJordan Crouse static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
5721c301fc5SJordan Crouse 			const char *buf, size_t count)
5731c301fc5SJordan Crouse {
5741c301fc5SJordan Crouse 
5751c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5761c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
5771c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
5781c301fc5SJordan Crouse 	unsigned long val;
5791c301fc5SJordan Crouse 
5801c301fc5SJordan Crouse 	if (strict_strtoul(buf, 10, &val))
5811c301fc5SJordan Crouse 		return -EINVAL;
5821c301fc5SJordan Crouse 
5831c301fc5SJordan Crouse 	mutex_lock(&data->lock);
5841c301fc5SJordan Crouse 
5851c301fc5SJordan Crouse 	data->tach[MIN][sattr->index] = rpm2tach(val);
5861c301fc5SJordan Crouse 
5871c301fc5SJordan Crouse 	adt7475_write_word(client, TACH_MIN_REG(sattr->index),
5881c301fc5SJordan Crouse 			   data->tach[MIN][sattr->index]);
5891c301fc5SJordan Crouse 
5901c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
5911c301fc5SJordan Crouse 	return count;
5921c301fc5SJordan Crouse }
5931c301fc5SJordan Crouse 
5941c301fc5SJordan Crouse static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
5951c301fc5SJordan Crouse 			char *buf)
5961c301fc5SJordan Crouse {
5971c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
5981c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
5991c301fc5SJordan Crouse 
6001c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
6011c301fc5SJordan Crouse }
6021c301fc5SJordan Crouse 
6031c301fc5SJordan Crouse static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
6041c301fc5SJordan Crouse 			    char *buf)
6051c301fc5SJordan Crouse {
6061c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6071c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6081c301fc5SJordan Crouse 
6091c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
6101c301fc5SJordan Crouse }
6111c301fc5SJordan Crouse 
6121c301fc5SJordan Crouse static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
6131c301fc5SJordan Crouse 			    char *buf)
6141c301fc5SJordan Crouse {
6151c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
6161c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6171c301fc5SJordan Crouse 
6181c301fc5SJordan Crouse 	return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
6191c301fc5SJordan Crouse }
6201c301fc5SJordan Crouse 
6211c301fc5SJordan Crouse static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
6221c301fc5SJordan Crouse 		       const char *buf, size_t count)
6231c301fc5SJordan Crouse {
6241c301fc5SJordan Crouse 
6251c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
6261c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
6271c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
6281c301fc5SJordan Crouse 	unsigned char reg = 0;
6291c301fc5SJordan Crouse 	long val;
6301c301fc5SJordan Crouse 
6311c301fc5SJordan Crouse 	if (strict_strtol(buf, 10, &val))
6321c301fc5SJordan Crouse 		return -EINVAL;
6331c301fc5SJordan Crouse 
6341c301fc5SJordan Crouse 	mutex_lock(&data->lock);
6351c301fc5SJordan Crouse 
6361c301fc5SJordan Crouse 	switch (sattr->nr) {
6371c301fc5SJordan Crouse 	case INPUT:
6381c301fc5SJordan Crouse 		/* Get a fresh value for CONTROL */
6391c301fc5SJordan Crouse 		data->pwm[CONTROL][sattr->index] =
6401c301fc5SJordan Crouse 			adt7475_read(PWM_CONFIG_REG(sattr->index));
6411c301fc5SJordan Crouse 
6421c301fc5SJordan Crouse 		/* If we are not in manual mode, then we shouldn't allow
6431c301fc5SJordan Crouse 		 * the user to set the pwm speed */
6441c301fc5SJordan Crouse 		if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
6451c301fc5SJordan Crouse 			mutex_unlock(&data->lock);
6461c301fc5SJordan Crouse 			return count;
6471c301fc5SJordan Crouse 		}
6481c301fc5SJordan Crouse 
6491c301fc5SJordan Crouse 		reg = PWM_REG(sattr->index);
6501c301fc5SJordan Crouse 		break;
6511c301fc5SJordan Crouse 
6521c301fc5SJordan Crouse 	case MIN:
6531c301fc5SJordan Crouse 		reg = PWM_MIN_REG(sattr->index);
6541c301fc5SJordan Crouse 		break;
6551c301fc5SJordan Crouse 
6561c301fc5SJordan Crouse 	case MAX:
6571c301fc5SJordan Crouse 		reg = PWM_MAX_REG(sattr->index);
6581c301fc5SJordan Crouse 		break;
6591c301fc5SJordan Crouse 	}
6601c301fc5SJordan Crouse 
6611c301fc5SJordan Crouse 	data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
6621c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, reg,
6631c301fc5SJordan Crouse 				  data->pwm[sattr->nr][sattr->index]);
6641c301fc5SJordan Crouse 
6651c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
6661c301fc5SJordan Crouse 
6671c301fc5SJordan Crouse 	return count;
6681c301fc5SJordan Crouse }
6691c301fc5SJordan Crouse 
6701c301fc5SJordan Crouse /* Called by set_pwmctrl and set_pwmchan */
6711c301fc5SJordan Crouse 
6721c301fc5SJordan Crouse static int hw_set_pwm(struct i2c_client *client, int index,
6731c301fc5SJordan Crouse 		      unsigned int pwmctl, unsigned int pwmchan)
6741c301fc5SJordan Crouse {
6751c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
6761c301fc5SJordan Crouse 	long val = 0;
6771c301fc5SJordan Crouse 
6781c301fc5SJordan Crouse 	switch (pwmctl) {
6791c301fc5SJordan Crouse 	case 0:
6801c301fc5SJordan Crouse 		val = 0x03;	/* Run at full speed */
6811c301fc5SJordan Crouse 		break;
6821c301fc5SJordan Crouse 	case 1:
6831c301fc5SJordan Crouse 		val = 0x07;	/* Manual mode */
6841c301fc5SJordan Crouse 		break;
6851c301fc5SJordan Crouse 	case 2:
6861c301fc5SJordan Crouse 		switch (pwmchan) {
6871c301fc5SJordan Crouse 		case 1:
6881c301fc5SJordan Crouse 			/* Remote1 controls PWM */
6891c301fc5SJordan Crouse 			val = 0x00;
6901c301fc5SJordan Crouse 			break;
6911c301fc5SJordan Crouse 		case 2:
6921c301fc5SJordan Crouse 			/* local controls PWM */
6931c301fc5SJordan Crouse 			val = 0x01;
6941c301fc5SJordan Crouse 			break;
6951c301fc5SJordan Crouse 		case 4:
6961c301fc5SJordan Crouse 			/* remote2 controls PWM */
6971c301fc5SJordan Crouse 			val = 0x02;
6981c301fc5SJordan Crouse 			break;
6991c301fc5SJordan Crouse 		case 6:
7001c301fc5SJordan Crouse 			/* local/remote2 control PWM */
7011c301fc5SJordan Crouse 			val = 0x05;
7021c301fc5SJordan Crouse 			break;
7031c301fc5SJordan Crouse 		case 7:
7041c301fc5SJordan Crouse 			/* All three control PWM */
7051c301fc5SJordan Crouse 			val = 0x06;
7061c301fc5SJordan Crouse 			break;
7071c301fc5SJordan Crouse 		default:
7081c301fc5SJordan Crouse 			return -EINVAL;
7091c301fc5SJordan Crouse 		}
7101c301fc5SJordan Crouse 		break;
7111c301fc5SJordan Crouse 	default:
7121c301fc5SJordan Crouse 		return -EINVAL;
7131c301fc5SJordan Crouse 	}
7141c301fc5SJordan Crouse 
7151c301fc5SJordan Crouse 	data->pwmctl[index] = pwmctl;
7161c301fc5SJordan Crouse 	data->pwmchan[index] = pwmchan;
7171c301fc5SJordan Crouse 
7181c301fc5SJordan Crouse 	data->pwm[CONTROL][index] &= ~0xE0;
7191c301fc5SJordan Crouse 	data->pwm[CONTROL][index] |= (val & 7) << 5;
7201c301fc5SJordan Crouse 
7211c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
7221c301fc5SJordan Crouse 				  data->pwm[CONTROL][index]);
7231c301fc5SJordan Crouse 
7241c301fc5SJordan Crouse 	return 0;
7251c301fc5SJordan Crouse }
7261c301fc5SJordan Crouse 
7271c301fc5SJordan Crouse static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
7281c301fc5SJordan Crouse 			   const char *buf, size_t count)
7291c301fc5SJordan Crouse {
7301c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7311c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7321c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7331c301fc5SJordan Crouse 	int r;
7341c301fc5SJordan Crouse 	long val;
7351c301fc5SJordan Crouse 
7361c301fc5SJordan Crouse 	if (strict_strtol(buf, 10, &val))
7371c301fc5SJordan Crouse 		return -EINVAL;
7381c301fc5SJordan Crouse 
7391c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7401c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
7411c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
7421c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
7431c301fc5SJordan Crouse 	if (r)
7441c301fc5SJordan Crouse 		count = r;
7451c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7461c301fc5SJordan Crouse 
7471c301fc5SJordan Crouse 	return count;
7481c301fc5SJordan Crouse }
7491c301fc5SJordan Crouse 
7501c301fc5SJordan Crouse static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
7511c301fc5SJordan Crouse 			   const char *buf, size_t count)
7521c301fc5SJordan Crouse {
7531c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7541c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7551c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7561c301fc5SJordan Crouse 	int r;
7571c301fc5SJordan Crouse 	long val;
7581c301fc5SJordan Crouse 
7591c301fc5SJordan Crouse 	if (strict_strtol(buf, 10, &val))
7601c301fc5SJordan Crouse 		return -EINVAL;
7611c301fc5SJordan Crouse 
7621c301fc5SJordan Crouse 	mutex_lock(&data->lock);
7631c301fc5SJordan Crouse 	/* Read Modify Write PWM values */
7641c301fc5SJordan Crouse 	adt7475_read_pwm(client, sattr->index);
7651c301fc5SJordan Crouse 	r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
7661c301fc5SJordan Crouse 	if (r)
7671c301fc5SJordan Crouse 		count = r;
7681c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
7691c301fc5SJordan Crouse 
7701c301fc5SJordan Crouse 	return count;
7711c301fc5SJordan Crouse }
7721c301fc5SJordan Crouse 
7731c301fc5SJordan Crouse /* List of frequencies for the PWM */
7741c301fc5SJordan Crouse static const int pwmfreq_table[] = {
7751c301fc5SJordan Crouse 	11, 14, 22, 29, 35, 44, 58, 88
7761c301fc5SJordan Crouse };
7771c301fc5SJordan Crouse 
7781c301fc5SJordan Crouse static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
7791c301fc5SJordan Crouse 			    char *buf)
7801c301fc5SJordan Crouse {
7811c301fc5SJordan Crouse 	struct adt7475_data *data = adt7475_update_device(dev);
7821c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7831c301fc5SJordan Crouse 
7841c301fc5SJordan Crouse 	return sprintf(buf, "%d\n",
7851c301fc5SJordan Crouse 		       pwmfreq_table[data->range[sattr->index] & 7]);
7861c301fc5SJordan Crouse }
7871c301fc5SJordan Crouse 
7881c301fc5SJordan Crouse static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
7891c301fc5SJordan Crouse 			   const char *buf, size_t count)
7901c301fc5SJordan Crouse {
7911c301fc5SJordan Crouse 	struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
7921c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
7931c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
7941c301fc5SJordan Crouse 	int out;
7951c301fc5SJordan Crouse 	long val;
7961c301fc5SJordan Crouse 
7971c301fc5SJordan Crouse 	if (strict_strtol(buf, 10, &val))
7981c301fc5SJordan Crouse 		return -EINVAL;
7991c301fc5SJordan Crouse 
8001c301fc5SJordan Crouse 	out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
8011c301fc5SJordan Crouse 
8021c301fc5SJordan Crouse 	mutex_lock(&data->lock);
8031c301fc5SJordan Crouse 
8041c301fc5SJordan Crouse 	data->range[sattr->index] =
8051c301fc5SJordan Crouse 		adt7475_read(TEMP_TRANGE_REG(sattr->index));
8061c301fc5SJordan Crouse 	data->range[sattr->index] &= ~7;
8071c301fc5SJordan Crouse 	data->range[sattr->index] |= out;
8081c301fc5SJordan Crouse 
8091c301fc5SJordan Crouse 	i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
8101c301fc5SJordan Crouse 				  data->range[sattr->index]);
8111c301fc5SJordan Crouse 
8121c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
8131c301fc5SJordan Crouse 	return count;
8141c301fc5SJordan Crouse }
8151c301fc5SJordan Crouse 
816f99318b2SJean Delvare static ssize_t show_pwm_at_crit(struct device *dev,
817f99318b2SJean Delvare 				struct device_attribute *devattr, char *buf)
818f99318b2SJean Delvare {
819f99318b2SJean Delvare 	struct adt7475_data *data = adt7475_update_device(dev);
820f99318b2SJean Delvare 	return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
821f99318b2SJean Delvare }
822f99318b2SJean Delvare 
823f99318b2SJean Delvare static ssize_t set_pwm_at_crit(struct device *dev,
824f99318b2SJean Delvare 			       struct device_attribute *devattr,
825f99318b2SJean Delvare 			       const char *buf, size_t count)
826f99318b2SJean Delvare {
827f99318b2SJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
828f99318b2SJean Delvare 	struct adt7475_data *data = i2c_get_clientdata(client);
829f99318b2SJean Delvare 	long val;
830f99318b2SJean Delvare 
831f99318b2SJean Delvare 	if (strict_strtol(buf, 10, &val))
832f99318b2SJean Delvare 		return -EINVAL;
833f99318b2SJean Delvare 	if (val != 0 && val != 1)
834f99318b2SJean Delvare 		return -EINVAL;
835f99318b2SJean Delvare 
836f99318b2SJean Delvare 	mutex_lock(&data->lock);
837f99318b2SJean Delvare 	data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
838f99318b2SJean Delvare 	if (val)
839f99318b2SJean Delvare 		data->config4 |= CONFIG4_MAXDUTY;
840f99318b2SJean Delvare 	else
841f99318b2SJean Delvare 		data->config4 &= ~CONFIG4_MAXDUTY;
842f99318b2SJean Delvare 	i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
843f99318b2SJean Delvare 	mutex_unlock(&data->lock);
844f99318b2SJean Delvare 
845f99318b2SJean Delvare 	return count;
846f99318b2SJean Delvare }
847f99318b2SJean Delvare 
8483d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_voltage, NULL, INPUT, 0);
8493d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_voltage,
8503d849981SJean Delvare 			    set_voltage, MAX, 0);
8513d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_voltage,
8523d849981SJean Delvare 			    set_voltage, MIN, 0);
8533d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_voltage, NULL, ALARM, 0);
854cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
8551c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
8561c301fc5SJordan Crouse 			    set_voltage, MAX, 1);
857cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
8581c301fc5SJordan Crouse 			    set_voltage, MIN, 1);
859cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
860cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
861cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
862cffb9dd0SJean Delvare 			    set_voltage, MAX, 2);
863cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
864cffb9dd0SJean Delvare 			    set_voltage, MIN, 2);
865cffb9dd0SJean Delvare static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
8663d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_voltage, NULL, INPUT, 3);
8673d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_voltage,
8683d849981SJean Delvare 			    set_voltage, MAX, 3);
8693d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_voltage,
8703d849981SJean Delvare 			    set_voltage, MIN, 3);
8713d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_voltage, NULL, ALARM, 3);
8723d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_voltage, NULL, INPUT, 4);
8733d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_voltage,
8743d849981SJean Delvare 			    set_voltage, MAX, 4);
8753d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_voltage,
8763d849981SJean Delvare 			    set_voltage, MIN, 4);
8773d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_voltage, NULL, ALARM, 8);
8783d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_voltage, NULL, INPUT, 5);
8793d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_voltage,
8803d849981SJean Delvare 			    set_voltage, MAX, 5);
8813d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_voltage,
8823d849981SJean Delvare 			    set_voltage, MIN, 5);
8833d849981SJean Delvare static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, show_voltage, NULL, ALARM, 31);
8841c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
8851c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
8861c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
8871c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
8881c301fc5SJordan Crouse 			    MAX, 0);
8891c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
8901c301fc5SJordan Crouse 			    MIN, 0);
8911c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
8921c301fc5SJordan Crouse 			    set_temp, OFFSET, 0);
8931c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
8941c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 0);
8951c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
8961c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 0);
8971c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
8981c301fc5SJordan Crouse 			    THERM, 0);
8991c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
9001c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 0);
9011c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
9021c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
9031c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
9041c301fc5SJordan Crouse 			    MAX, 1);
9051c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
9061c301fc5SJordan Crouse 			    MIN, 1);
9071c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
9081c301fc5SJordan Crouse 			    set_temp, OFFSET, 1);
9091c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
9101c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 1);
9111c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
9121c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 1);
9131c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
9141c301fc5SJordan Crouse 			    THERM, 1);
9151c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
9161c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 1);
9171c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
9181c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
9191c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
9201c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
9211c301fc5SJordan Crouse 			    MAX, 2);
9221c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
9231c301fc5SJordan Crouse 			    MIN, 2);
9241c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
9251c301fc5SJordan Crouse 			    set_temp, OFFSET, 2);
9261c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
9271c301fc5SJordan Crouse 			    show_temp, set_temp, AUTOMIN, 2);
9281c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
9291c301fc5SJordan Crouse 			    show_point2, set_point2, 0, 2);
9301c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
9311c301fc5SJordan Crouse 			    THERM, 2);
9321c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
9331c301fc5SJordan Crouse 			    set_temp, HYSTERSIS, 2);
9341c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
9351c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
9361c301fc5SJordan Crouse 			    MIN, 0);
9371c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
9381c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
9391c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
9401c301fc5SJordan Crouse 			    MIN, 1);
9411c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
9421c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
9431c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
9441c301fc5SJordan Crouse 			    MIN, 2);
9451c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
9461c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
9471c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
9481c301fc5SJordan Crouse 			    MIN, 3);
9491c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
9501c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
9511c301fc5SJordan Crouse 			    0);
9521c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
9531c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 0);
9541c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
9551c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 0);
95684d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
9571c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 0);
9581c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
9591c301fc5SJordan Crouse 			    set_pwm, MIN, 0);
9601c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
9611c301fc5SJordan Crouse 			    set_pwm, MAX, 0);
9621c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
9631c301fc5SJordan Crouse 			    1);
9641c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
9651c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 1);
9661c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
9671c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 1);
96884d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
9691c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 1);
9701c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
9711c301fc5SJordan Crouse 			    set_pwm, MIN, 1);
9721c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
9731c301fc5SJordan Crouse 			    set_pwm, MAX, 1);
9741c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
9751c301fc5SJordan Crouse 			    2);
9761c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
9771c301fc5SJordan Crouse 			    set_pwmfreq, INPUT, 2);
9781c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
9791c301fc5SJordan Crouse 			    set_pwmctrl, INPUT, 2);
98084d2a314SJean Delvare static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
9811c301fc5SJordan Crouse 			    show_pwmchan, set_pwmchan, INPUT, 2);
9821c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
9831c301fc5SJordan Crouse 			    set_pwm, MIN, 2);
9841c301fc5SJordan Crouse static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
9851c301fc5SJordan Crouse 			    set_pwm, MAX, 2);
9861c301fc5SJordan Crouse 
987f99318b2SJean Delvare /* Non-standard name, might need revisiting */
988f99318b2SJean Delvare static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
989f99318b2SJean Delvare 		   show_pwm_at_crit, set_pwm_at_crit);
990f99318b2SJean Delvare 
9911c301fc5SJordan Crouse static struct attribute *adt7475_attrs[] = {
9921c301fc5SJordan Crouse 	&sensor_dev_attr_in1_input.dev_attr.attr,
9931c301fc5SJordan Crouse 	&sensor_dev_attr_in1_max.dev_attr.attr,
9941c301fc5SJordan Crouse 	&sensor_dev_attr_in1_min.dev_attr.attr,
9951c301fc5SJordan Crouse 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
9961c301fc5SJordan Crouse 	&sensor_dev_attr_in2_input.dev_attr.attr,
9971c301fc5SJordan Crouse 	&sensor_dev_attr_in2_max.dev_attr.attr,
9981c301fc5SJordan Crouse 	&sensor_dev_attr_in2_min.dev_attr.attr,
9991c301fc5SJordan Crouse 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
10001c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_input.dev_attr.attr,
10011c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
10021c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
10031c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_max.dev_attr.attr,
10041c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_min.dev_attr.attr,
10051c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_offset.dev_attr.attr,
10061c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
10071c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
10081c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
10091c301fc5SJordan Crouse 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
10101c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_input.dev_attr.attr,
10111c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
10121c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_max.dev_attr.attr,
10131c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_min.dev_attr.attr,
10141c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
10151c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
10161c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
10171c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
10181c301fc5SJordan Crouse 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
10191c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_input.dev_attr.attr,
10201c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
10211c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
10221c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_max.dev_attr.attr,
10231c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_min.dev_attr.attr,
10241c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
10251c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
10261c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
10271c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
10281c301fc5SJordan Crouse 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
10291c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_input.dev_attr.attr,
10301c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_min.dev_attr.attr,
10311c301fc5SJordan Crouse 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
10321c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_input.dev_attr.attr,
10331c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_min.dev_attr.attr,
10341c301fc5SJordan Crouse 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
10351c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_input.dev_attr.attr,
10361c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_min.dev_attr.attr,
10371c301fc5SJordan Crouse 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
10381c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1.dev_attr.attr,
10391c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_freq.dev_attr.attr,
10401c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
104184d2a314SJean Delvare 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
10421c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
10431c301fc5SJordan Crouse 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
10441c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3.dev_attr.attr,
10451c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_freq.dev_attr.attr,
10461c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
104784d2a314SJean Delvare 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
10481c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
10491c301fc5SJordan Crouse 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1050f99318b2SJean Delvare 	&dev_attr_pwm_use_point2_pwm_at_crit.attr,
10511c301fc5SJordan Crouse 	NULL,
10521c301fc5SJordan Crouse };
10531c301fc5SJordan Crouse 
1054378933c9SJean Delvare static struct attribute *fan4_attrs[] = {
1055378933c9SJean Delvare 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1056378933c9SJean Delvare 	&sensor_dev_attr_fan4_min.dev_attr.attr,
1057378933c9SJean Delvare 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1058378933c9SJean Delvare 	NULL
1059378933c9SJean Delvare };
1060378933c9SJean Delvare 
1061378933c9SJean Delvare static struct attribute *pwm2_attrs[] = {
1062378933c9SJean Delvare 	&sensor_dev_attr_pwm2.dev_attr.attr,
1063378933c9SJean Delvare 	&sensor_dev_attr_pwm2_freq.dev_attr.attr,
1064378933c9SJean Delvare 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
1065378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1066378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1067378933c9SJean Delvare 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1068378933c9SJean Delvare 	NULL
1069378933c9SJean Delvare };
1070378933c9SJean Delvare 
10713d849981SJean Delvare /* Attributes specific to the ADT7490 */
1072378933c9SJean Delvare static struct attribute *in0_attrs[] = {
10733d849981SJean Delvare 	&sensor_dev_attr_in0_input.dev_attr.attr,
10743d849981SJean Delvare 	&sensor_dev_attr_in0_max.dev_attr.attr,
10753d849981SJean Delvare 	&sensor_dev_attr_in0_min.dev_attr.attr,
10763d849981SJean Delvare 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1077378933c9SJean Delvare 	NULL
1078378933c9SJean Delvare };
1079378933c9SJean Delvare 
1080378933c9SJean Delvare static struct attribute *adt7490_attrs[] = {
10813d849981SJean Delvare 	&sensor_dev_attr_in3_input.dev_attr.attr,
10823d849981SJean Delvare 	&sensor_dev_attr_in3_max.dev_attr.attr,
10833d849981SJean Delvare 	&sensor_dev_attr_in3_min.dev_attr.attr,
10843d849981SJean Delvare 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
10853d849981SJean Delvare 	&sensor_dev_attr_in4_input.dev_attr.attr,
10863d849981SJean Delvare 	&sensor_dev_attr_in4_max.dev_attr.attr,
10873d849981SJean Delvare 	&sensor_dev_attr_in4_min.dev_attr.attr,
10883d849981SJean Delvare 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
10893d849981SJean Delvare 	&sensor_dev_attr_in5_input.dev_attr.attr,
10903d849981SJean Delvare 	&sensor_dev_attr_in5_max.dev_attr.attr,
10913d849981SJean Delvare 	&sensor_dev_attr_in5_min.dev_attr.attr,
10923d849981SJean Delvare 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
10933d849981SJean Delvare 	NULL
10943d849981SJean Delvare };
10953d849981SJean Delvare 
109654ecb9e3SJean Delvare static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
1097378933c9SJean Delvare static struct attribute_group fan4_attr_group = { .attrs = fan4_attrs };
1098378933c9SJean Delvare static struct attribute_group pwm2_attr_group = { .attrs = pwm2_attrs };
1099378933c9SJean Delvare static struct attribute_group in0_attr_group = { .attrs = in0_attrs };
11003d849981SJean Delvare static struct attribute_group adt7490_attr_group = { .attrs = adt7490_attrs };
11011c301fc5SJordan Crouse 
11021c301fc5SJordan Crouse static int adt7475_detect(struct i2c_client *client, int kind,
11031c301fc5SJordan Crouse 			  struct i2c_board_info *info)
11041c301fc5SJordan Crouse {
11051c301fc5SJordan Crouse 	struct i2c_adapter *adapter = client->adapter;
1106d656b6fdSJean Delvare 	int vendid, devid, devid2;
1107b180d050SJean Delvare 	const char *name;
11081c301fc5SJordan Crouse 
11091c301fc5SJordan Crouse 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
11101c301fc5SJordan Crouse 		return -ENODEV;
11111c301fc5SJordan Crouse 
1112b180d050SJean Delvare 	vendid = adt7475_read(REG_VENDID);
1113d656b6fdSJean Delvare 	devid2 = adt7475_read(REG_DEVID2);
1114d656b6fdSJean Delvare 	if (vendid != 0x41 ||		/* Analog Devices */
1115d656b6fdSJean Delvare 	    (devid2 & 0xf8) != 0x68)
1116d656b6fdSJean Delvare 		return -ENODEV;
1117b180d050SJean Delvare 
1118d656b6fdSJean Delvare 	devid = adt7475_read(REG_DEVID);
1119d656b6fdSJean Delvare 	if (devid == 0x73)
1120b180d050SJean Delvare 		name = "adt7473";
1121d656b6fdSJean Delvare 	else if (devid == 0x75 && client->addr == 0x2e)
1122b180d050SJean Delvare 		name = "adt7475";
11233d849981SJean Delvare 	else if ((devid2 & 0xfc) == 0x6c)
11243d849981SJean Delvare 		name = "adt7490";
1125b180d050SJean Delvare 	else {
1126b180d050SJean Delvare 		dev_dbg(&adapter->dev,
11273d849981SJean Delvare 			"Couldn't detect an ADT7473/75/90 part at "
1128b180d050SJean Delvare 			"0x%02x\n", (unsigned int)client->addr);
11291c301fc5SJordan Crouse 		return -ENODEV;
11301c301fc5SJordan Crouse 	}
11311c301fc5SJordan Crouse 
1132b180d050SJean Delvare 	strlcpy(info->type, name, I2C_NAME_SIZE);
11331c301fc5SJordan Crouse 
11341c301fc5SJordan Crouse 	return 0;
11351c301fc5SJordan Crouse }
11361c301fc5SJordan Crouse 
11370f14480bSJean Delvare static void adt7475_remove_files(struct i2c_client *client,
11380f14480bSJean Delvare 				 struct adt7475_data *data)
11390f14480bSJean Delvare {
11400f14480bSJean Delvare 	sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
11410f14480bSJean Delvare 	if (data->has_voltage & 0x39)
11420f14480bSJean Delvare 		sysfs_remove_group(&client->dev.kobj, &adt7490_attr_group);
1143378933c9SJean Delvare 	if (data->has_fan4)
1144378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &fan4_attr_group);
1145378933c9SJean Delvare 	if (data->has_pwm2)
1146378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &pwm2_attr_group);
1147378933c9SJean Delvare 	if (data->has_voltage & (1 << 0))
1148378933c9SJean Delvare 		sysfs_remove_group(&client->dev.kobj, &in0_attr_group);
11490f14480bSJean Delvare }
11500f14480bSJean Delvare 
11511c301fc5SJordan Crouse static int adt7475_probe(struct i2c_client *client,
11521c301fc5SJordan Crouse 			 const struct i2c_device_id *id)
11531c301fc5SJordan Crouse {
1154*d07ca4adSJean Delvare 	static const char *names[] = {
1155*d07ca4adSJean Delvare 		[adt7473] = "ADT7473",
1156*d07ca4adSJean Delvare 		[adt7475] = "ADT7475",
1157*d07ca4adSJean Delvare 		[adt7490] = "ADT7490",
1158*d07ca4adSJean Delvare 	};
1159*d07ca4adSJean Delvare 
11601c301fc5SJordan Crouse 	struct adt7475_data *data;
1161378933c9SJean Delvare 	int i, ret = 0, revision;
1162378933c9SJean Delvare 	u8 config3;
11631c301fc5SJordan Crouse 
11641c301fc5SJordan Crouse 	data = kzalloc(sizeof(*data), GFP_KERNEL);
11651c301fc5SJordan Crouse 	if (data == NULL)
11661c301fc5SJordan Crouse 		return -ENOMEM;
11671c301fc5SJordan Crouse 
11681c301fc5SJordan Crouse 	mutex_init(&data->lock);
11691c301fc5SJordan Crouse 	i2c_set_clientdata(client, data);
11701c301fc5SJordan Crouse 
1171cffb9dd0SJean Delvare 	/* Initialize device-specific values */
1172cffb9dd0SJean Delvare 	switch (id->driver_data) {
11733d849981SJean Delvare 	case adt7490:
1174378933c9SJean Delvare 		data->has_voltage = 0x3e;	/* in1 to in5 */
1175378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x03;
1176*d07ca4adSJean Delvare 		if (revision == 0x03)
1177*d07ca4adSJean Delvare 			revision += adt7475_read(REG_DEVREV2);
11783d849981SJean Delvare 		break;
1179cffb9dd0SJean Delvare 	default:
1180cffb9dd0SJean Delvare 		data->has_voltage = 0x06;	/* in1, in2 */
1181378933c9SJean Delvare 		revision = adt7475_read(REG_DEVID2) & 0x07;
1182378933c9SJean Delvare 	}
1183378933c9SJean Delvare 
1184378933c9SJean Delvare 	config3 = adt7475_read(REG_CONFIG3);
1185378933c9SJean Delvare 	/* Pin PWM2 may alternatively be used for ALERT output */
1186378933c9SJean Delvare 	if (!(config3 & CONFIG3_SMBALERT))
1187378933c9SJean Delvare 		data->has_pwm2 = 1;
1188378933c9SJean Delvare 	/* Meaning of this bit is inverted for the ADT7473-1 */
1189378933c9SJean Delvare 	if (id->driver_data == adt7473 && revision >= 1)
1190378933c9SJean Delvare 		data->has_pwm2 = !data->has_pwm2;
1191378933c9SJean Delvare 
1192378933c9SJean Delvare 	data->config4 = adt7475_read(REG_CONFIG4);
1193378933c9SJean Delvare 	/* Pin TACH4 may alternatively be used for THERM */
1194378933c9SJean Delvare 	if ((data->config4 & CONFIG4_PINFUNC) == 0x0)
1195378933c9SJean Delvare 		data->has_fan4 = 1;
1196378933c9SJean Delvare 
1197378933c9SJean Delvare 	/* THERM configuration is more complex on the ADT7490, because 2
1198378933c9SJean Delvare 	   different pins (TACH4 and +2.5 Vin) can be used for this function */
1199378933c9SJean Delvare 	if (id->driver_data == adt7490) {
1200378933c9SJean Delvare 		if ((data->config4 & CONFIG4_PINFUNC) == 0x1 &&
1201378933c9SJean Delvare 		    !(config3 & CONFIG3_THERM))
1202378933c9SJean Delvare 			data->has_fan4 = 1;
1203378933c9SJean Delvare 		if (!(config3 & CONFIG3_THERM) ||
1204378933c9SJean Delvare 		    (data->config4 & CONFIG4_PINFUNC) == 0x1)
1205378933c9SJean Delvare 			data->has_voltage |= (1 << 0);		/* in0 */
1206cffb9dd0SJean Delvare 	}
1207cffb9dd0SJean Delvare 
12081c301fc5SJordan Crouse 	/* Call adt7475_read_pwm for all pwm's as this will reprogram any
12091c301fc5SJordan Crouse 	   pwm's which are disabled to manual mode with 0% duty cycle */
12101c301fc5SJordan Crouse 	for (i = 0; i < ADT7475_PWM_COUNT; i++)
12111c301fc5SJordan Crouse 		adt7475_read_pwm(client, i);
12121c301fc5SJordan Crouse 
12131c301fc5SJordan Crouse 	ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
12141c301fc5SJordan Crouse 	if (ret)
12151c301fc5SJordan Crouse 		goto efree;
12161c301fc5SJordan Crouse 
12173d849981SJean Delvare 	if (id->driver_data == adt7490) {
12183d849981SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj,
12193d849981SJean Delvare 					 &adt7490_attr_group);
12203d849981SJean Delvare 		if (ret)
12213d849981SJean Delvare 			goto eremove;
12223d849981SJean Delvare 	}
12233d849981SJean Delvare 
1224378933c9SJean Delvare 	/* Features that can be disabled individually */
1225378933c9SJean Delvare 	if (data->has_fan4) {
1226378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &fan4_attr_group);
1227378933c9SJean Delvare 		if (ret)
1228378933c9SJean Delvare 			goto eremove;
1229378933c9SJean Delvare 	}
1230378933c9SJean Delvare 	if (data->has_pwm2) {
1231378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &pwm2_attr_group);
1232378933c9SJean Delvare 		if (ret)
1233378933c9SJean Delvare 			goto eremove;
1234378933c9SJean Delvare 	}
1235378933c9SJean Delvare 	if (data->has_voltage & (1 << 0)) {
1236378933c9SJean Delvare 		ret = sysfs_create_group(&client->dev.kobj, &in0_attr_group);
1237378933c9SJean Delvare 		if (ret)
1238378933c9SJean Delvare 			goto eremove;
1239378933c9SJean Delvare 	}
1240378933c9SJean Delvare 
12411c301fc5SJordan Crouse 	data->hwmon_dev = hwmon_device_register(&client->dev);
12421c301fc5SJordan Crouse 	if (IS_ERR(data->hwmon_dev)) {
12431c301fc5SJordan Crouse 		ret = PTR_ERR(data->hwmon_dev);
12441c301fc5SJordan Crouse 		goto eremove;
12451c301fc5SJordan Crouse 	}
12461c301fc5SJordan Crouse 
1247*d07ca4adSJean Delvare 	dev_info(&client->dev, "%s device, revision %d\n",
1248*d07ca4adSJean Delvare 		 names[id->driver_data], revision);
1249*d07ca4adSJean Delvare 	if ((data->has_voltage & (1 << 0)) || data->has_fan4 || data->has_pwm2)
1250*d07ca4adSJean Delvare 		dev_info(&client->dev, "Optional features:%s%s%s\n",
1251*d07ca4adSJean Delvare 			 (data->has_voltage & (1 << 0)) ? " in0" : "",
1252*d07ca4adSJean Delvare 			 data->has_fan4 ? " fan4" : "",
1253*d07ca4adSJean Delvare 			 data->has_pwm2 ? " pwm2" : "");
1254*d07ca4adSJean Delvare 
12551c301fc5SJordan Crouse 	return 0;
12561c301fc5SJordan Crouse 
12571c301fc5SJordan Crouse eremove:
12580f14480bSJean Delvare 	adt7475_remove_files(client, data);
12591c301fc5SJordan Crouse efree:
12601c301fc5SJordan Crouse 	kfree(data);
12611c301fc5SJordan Crouse 	return ret;
12621c301fc5SJordan Crouse }
12631c301fc5SJordan Crouse 
12641c301fc5SJordan Crouse static int adt7475_remove(struct i2c_client *client)
12651c301fc5SJordan Crouse {
12661c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
12671c301fc5SJordan Crouse 
12681c301fc5SJordan Crouse 	hwmon_device_unregister(data->hwmon_dev);
12690f14480bSJean Delvare 	adt7475_remove_files(client, data);
12701c301fc5SJordan Crouse 	kfree(data);
12711c301fc5SJordan Crouse 
12721c301fc5SJordan Crouse 	return 0;
12731c301fc5SJordan Crouse }
12741c301fc5SJordan Crouse 
12751c301fc5SJordan Crouse static struct i2c_driver adt7475_driver = {
12761c301fc5SJordan Crouse 	.class		= I2C_CLASS_HWMON,
12771c301fc5SJordan Crouse 	.driver = {
12781c301fc5SJordan Crouse 		.name	= "adt7475",
12791c301fc5SJordan Crouse 	},
12801c301fc5SJordan Crouse 	.probe		= adt7475_probe,
12811c301fc5SJordan Crouse 	.remove		= adt7475_remove,
12821c301fc5SJordan Crouse 	.id_table	= adt7475_id,
12831c301fc5SJordan Crouse 	.detect		= adt7475_detect,
12841c301fc5SJordan Crouse 	.address_data	= &addr_data,
12851c301fc5SJordan Crouse };
12861c301fc5SJordan Crouse 
12871c301fc5SJordan Crouse static void adt7475_read_hystersis(struct i2c_client *client)
12881c301fc5SJordan Crouse {
12891c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
12901c301fc5SJordan Crouse 
12911c301fc5SJordan Crouse 	data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
12921c301fc5SJordan Crouse 	data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
12931c301fc5SJordan Crouse 	data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
12941c301fc5SJordan Crouse }
12951c301fc5SJordan Crouse 
12961c301fc5SJordan Crouse static void adt7475_read_pwm(struct i2c_client *client, int index)
12971c301fc5SJordan Crouse {
12981c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
12991c301fc5SJordan Crouse 	unsigned int v;
13001c301fc5SJordan Crouse 
13011c301fc5SJordan Crouse 	data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
13021c301fc5SJordan Crouse 
13031c301fc5SJordan Crouse 	/* Figure out the internal value for pwmctrl and pwmchan
13041c301fc5SJordan Crouse 	   based on the current settings */
13051c301fc5SJordan Crouse 	v = (data->pwm[CONTROL][index] >> 5) & 7;
13061c301fc5SJordan Crouse 
13071c301fc5SJordan Crouse 	if (v == 3)
13081c301fc5SJordan Crouse 		data->pwmctl[index] = 0;
13091c301fc5SJordan Crouse 	else if (v == 7)
13101c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
13111c301fc5SJordan Crouse 	else if (v == 4) {
13121c301fc5SJordan Crouse 		/* The fan is disabled - we don't want to
13131c301fc5SJordan Crouse 		   support that, so change to manual mode and
13141c301fc5SJordan Crouse 		   set the duty cycle to 0 instead
13151c301fc5SJordan Crouse 		*/
13161c301fc5SJordan Crouse 		data->pwm[INPUT][index] = 0;
13171c301fc5SJordan Crouse 		data->pwm[CONTROL][index] &= ~0xE0;
13181c301fc5SJordan Crouse 		data->pwm[CONTROL][index] |= (7 << 5);
13191c301fc5SJordan Crouse 
13201c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
13211c301fc5SJordan Crouse 					  data->pwm[INPUT][index]);
13221c301fc5SJordan Crouse 
13231c301fc5SJordan Crouse 		i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
13241c301fc5SJordan Crouse 					  data->pwm[CONTROL][index]);
13251c301fc5SJordan Crouse 
13261c301fc5SJordan Crouse 		data->pwmctl[index] = 1;
13271c301fc5SJordan Crouse 	} else {
13281c301fc5SJordan Crouse 		data->pwmctl[index] = 2;
13291c301fc5SJordan Crouse 
13301c301fc5SJordan Crouse 		switch (v) {
13311c301fc5SJordan Crouse 		case 0:
13321c301fc5SJordan Crouse 			data->pwmchan[index] = 1;
13331c301fc5SJordan Crouse 			break;
13341c301fc5SJordan Crouse 		case 1:
13351c301fc5SJordan Crouse 			data->pwmchan[index] = 2;
13361c301fc5SJordan Crouse 			break;
13371c301fc5SJordan Crouse 		case 2:
13381c301fc5SJordan Crouse 			data->pwmchan[index] = 4;
13391c301fc5SJordan Crouse 			break;
13401c301fc5SJordan Crouse 		case 5:
13411c301fc5SJordan Crouse 			data->pwmchan[index] = 6;
13421c301fc5SJordan Crouse 			break;
13431c301fc5SJordan Crouse 		case 6:
13441c301fc5SJordan Crouse 			data->pwmchan[index] = 7;
13451c301fc5SJordan Crouse 			break;
13461c301fc5SJordan Crouse 		}
13471c301fc5SJordan Crouse 	}
13481c301fc5SJordan Crouse }
13491c301fc5SJordan Crouse 
13501c301fc5SJordan Crouse static struct adt7475_data *adt7475_update_device(struct device *dev)
13511c301fc5SJordan Crouse {
13521c301fc5SJordan Crouse 	struct i2c_client *client = to_i2c_client(dev);
13531c301fc5SJordan Crouse 	struct adt7475_data *data = i2c_get_clientdata(client);
13543d849981SJean Delvare 	u16 ext;
13551c301fc5SJordan Crouse 	int i;
13561c301fc5SJordan Crouse 
13571c301fc5SJordan Crouse 	mutex_lock(&data->lock);
13581c301fc5SJordan Crouse 
13591c301fc5SJordan Crouse 	/* Measurement values update every 2 seconds */
13601c301fc5SJordan Crouse 	if (time_after(jiffies, data->measure_updated + HZ * 2) ||
13611c301fc5SJordan Crouse 	    !data->valid) {
13621c301fc5SJordan Crouse 		data->alarms = adt7475_read(REG_STATUS2) << 8;
13631c301fc5SJordan Crouse 		data->alarms |= adt7475_read(REG_STATUS1);
13641c301fc5SJordan Crouse 
13653d849981SJean Delvare 		ext = (adt7475_read(REG_EXTEND2) << 8) |
13663d849981SJean Delvare 			adt7475_read(REG_EXTEND1);
1367cffb9dd0SJean Delvare 		for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1368cffb9dd0SJean Delvare 			if (!(data->has_voltage & (1 << i)))
1369cffb9dd0SJean Delvare 				continue;
13701c301fc5SJordan Crouse 			data->voltage[INPUT][i] =
13711c301fc5SJordan Crouse 				(adt7475_read(VOLTAGE_REG(i)) << 2) |
1372cffb9dd0SJean Delvare 				((ext >> (i * 2)) & 3);
1373cffb9dd0SJean Delvare 		}
13741c301fc5SJordan Crouse 
13751c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_TEMP_COUNT; i++)
13761c301fc5SJordan Crouse 			data->temp[INPUT][i] =
13771c301fc5SJordan Crouse 				(adt7475_read(TEMP_REG(i)) << 2) |
13783d849981SJean Delvare 				((ext >> ((i + 5) * 2)) & 3);
13793d849981SJean Delvare 
13803d849981SJean Delvare 		if (data->has_voltage & (1 << 5)) {
13813d849981SJean Delvare 			data->alarms |= adt7475_read(REG_STATUS4) << 24;
13823d849981SJean Delvare 			ext = adt7475_read(REG_EXTEND3);
13833d849981SJean Delvare 			data->voltage[INPUT][5] = adt7475_read(REG_VTT) << 2 |
13843d849981SJean Delvare 				((ext >> 4) & 3);
13853d849981SJean Delvare 		}
13861c301fc5SJordan Crouse 
1387378933c9SJean Delvare 		for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1388378933c9SJean Delvare 			if (i == 3 && !data->has_fan4)
1389378933c9SJean Delvare 				continue;
13901c301fc5SJordan Crouse 			data->tach[INPUT][i] =
13911c301fc5SJordan Crouse 				adt7475_read_word(client, TACH_REG(i));
1392378933c9SJean Delvare 		}
13931c301fc5SJordan Crouse 
13941c301fc5SJordan Crouse 		/* Updated by hw when in auto mode */
1395378933c9SJean Delvare 		for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1396378933c9SJean Delvare 			if (i == 1 && !data->has_pwm2)
1397378933c9SJean Delvare 				continue;
13981c301fc5SJordan Crouse 			data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
1399378933c9SJean Delvare 		}
14001c301fc5SJordan Crouse 
14011c301fc5SJordan Crouse 		data->measure_updated = jiffies;
14021c301fc5SJordan Crouse 	}
14031c301fc5SJordan Crouse 
14041c301fc5SJordan Crouse 	/* Limits and settings, should never change update every 60 seconds */
140556e35eeeSJean Delvare 	if (time_after(jiffies, data->limits_updated + HZ * 60) ||
14061c301fc5SJordan Crouse 	    !data->valid) {
1407f99318b2SJean Delvare 		data->config4 = adt7475_read(REG_CONFIG4);
14081c301fc5SJordan Crouse 		data->config5 = adt7475_read(REG_CONFIG5);
14091c301fc5SJordan Crouse 
14101c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1411cffb9dd0SJean Delvare 			if (!(data->has_voltage & (1 << i)))
1412cffb9dd0SJean Delvare 				continue;
14131c301fc5SJordan Crouse 			/* Adjust values so they match the input precision */
14141c301fc5SJordan Crouse 			data->voltage[MIN][i] =
14151c301fc5SJordan Crouse 				adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
14161c301fc5SJordan Crouse 			data->voltage[MAX][i] =
14171c301fc5SJordan Crouse 				adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
14181c301fc5SJordan Crouse 		}
14191c301fc5SJordan Crouse 
14203d849981SJean Delvare 		if (data->has_voltage & (1 << 5)) {
14213d849981SJean Delvare 			data->voltage[MIN][5] = adt7475_read(REG_VTT_MIN) << 2;
14223d849981SJean Delvare 			data->voltage[MAX][5] = adt7475_read(REG_VTT_MAX) << 2;
14233d849981SJean Delvare 		}
14243d849981SJean Delvare 
14251c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
14261c301fc5SJordan Crouse 			/* Adjust values so they match the input precision */
14271c301fc5SJordan Crouse 			data->temp[MIN][i] =
14281c301fc5SJordan Crouse 				adt7475_read(TEMP_MIN_REG(i)) << 2;
14291c301fc5SJordan Crouse 			data->temp[MAX][i] =
14301c301fc5SJordan Crouse 				adt7475_read(TEMP_MAX_REG(i)) << 2;
14311c301fc5SJordan Crouse 			data->temp[AUTOMIN][i] =
14321c301fc5SJordan Crouse 				adt7475_read(TEMP_TMIN_REG(i)) << 2;
14331c301fc5SJordan Crouse 			data->temp[THERM][i] =
14341c301fc5SJordan Crouse 				adt7475_read(TEMP_THERM_REG(i)) << 2;
14351c301fc5SJordan Crouse 			data->temp[OFFSET][i] =
14361c301fc5SJordan Crouse 				adt7475_read(TEMP_OFFSET_REG(i));
14371c301fc5SJordan Crouse 		}
14381c301fc5SJordan Crouse 		adt7475_read_hystersis(client);
14391c301fc5SJordan Crouse 
1440378933c9SJean Delvare 		for (i = 0; i < ADT7475_TACH_COUNT; i++) {
1441378933c9SJean Delvare 			if (i == 3 && !data->has_fan4)
1442378933c9SJean Delvare 				continue;
14431c301fc5SJordan Crouse 			data->tach[MIN][i] =
14441c301fc5SJordan Crouse 				adt7475_read_word(client, TACH_MIN_REG(i));
1445378933c9SJean Delvare 		}
14461c301fc5SJordan Crouse 
14471c301fc5SJordan Crouse 		for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1448378933c9SJean Delvare 			if (i == 1 && !data->has_pwm2)
1449378933c9SJean Delvare 				continue;
14501c301fc5SJordan Crouse 			data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
14511c301fc5SJordan Crouse 			data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
14521c301fc5SJordan Crouse 			/* Set the channel and control information */
14531c301fc5SJordan Crouse 			adt7475_read_pwm(client, i);
14541c301fc5SJordan Crouse 		}
14551c301fc5SJordan Crouse 
14561c301fc5SJordan Crouse 		data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
14571c301fc5SJordan Crouse 		data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
14581c301fc5SJordan Crouse 		data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
14591c301fc5SJordan Crouse 
14601c301fc5SJordan Crouse 		data->limits_updated = jiffies;
14611c301fc5SJordan Crouse 		data->valid = 1;
14621c301fc5SJordan Crouse 	}
14631c301fc5SJordan Crouse 
14641c301fc5SJordan Crouse 	mutex_unlock(&data->lock);
14651c301fc5SJordan Crouse 
14661c301fc5SJordan Crouse 	return data;
14671c301fc5SJordan Crouse }
14681c301fc5SJordan Crouse 
14691c301fc5SJordan Crouse static int __init sensors_adt7475_init(void)
14701c301fc5SJordan Crouse {
14711c301fc5SJordan Crouse 	return i2c_add_driver(&adt7475_driver);
14721c301fc5SJordan Crouse }
14731c301fc5SJordan Crouse 
14741c301fc5SJordan Crouse static void __exit sensors_adt7475_exit(void)
14751c301fc5SJordan Crouse {
14761c301fc5SJordan Crouse 	i2c_del_driver(&adt7475_driver);
14771c301fc5SJordan Crouse }
14781c301fc5SJordan Crouse 
14791c301fc5SJordan Crouse MODULE_AUTHOR("Advanced Micro Devices, Inc");
14801c301fc5SJordan Crouse MODULE_DESCRIPTION("adt7475 driver");
14811c301fc5SJordan Crouse MODULE_LICENSE("GPL");
14821c301fc5SJordan Crouse 
14831c301fc5SJordan Crouse module_init(sensors_adt7475_init);
14841c301fc5SJordan Crouse module_exit(sensors_adt7475_exit);
1485