xref: /linux/drivers/hwmon/adt7470.c (revision 6748703856d461f8cd62b72ebca51040e06cb310)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26f9703d0SDarrick J. Wong /*
36f9703d0SDarrick J. Wong  * A hwmon driver for the Analog Devices ADT7470
46f9703d0SDarrick J. Wong  * Copyright (C) 2007 IBM
56f9703d0SDarrick J. Wong  *
65407e051SDarrick J. Wong  * Author: Darrick J. Wong <darrick.wong@oracle.com>
76f9703d0SDarrick J. Wong  */
86f9703d0SDarrick J. Wong 
92e991201SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
102e991201SJoe Perches 
116f9703d0SDarrick J. Wong #include <linux/module.h>
126f9703d0SDarrick J. Wong #include <linux/jiffies.h>
136f9703d0SDarrick J. Wong #include <linux/i2c.h>
146f9703d0SDarrick J. Wong #include <linux/hwmon.h>
156f9703d0SDarrick J. Wong #include <linux/hwmon-sysfs.h>
166f9703d0SDarrick J. Wong #include <linux/err.h>
176f9703d0SDarrick J. Wong #include <linux/mutex.h>
186f9703d0SDarrick J. Wong #include <linux/delay.h>
196f9703d0SDarrick J. Wong #include <linux/log2.h>
2089fac11cSDarrick J. Wong #include <linux/kthread.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
22aa18cc91SJoshua Scott #include <linux/util_macros.h>
236f9703d0SDarrick J. Wong 
246f9703d0SDarrick J. Wong /* Addresses to scan */
2525e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
266f9703d0SDarrick J. Wong 
276f9703d0SDarrick J. Wong /* ADT7470 registers */
286f9703d0SDarrick J. Wong #define ADT7470_REG_BASE_ADDR			0x20
296f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_BASE_ADDR		0x20
306f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_MAX_ADDR		0x29
316f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_BASE_ADDR		0x2A
326f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX_ADDR		0x31
336f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_BASE_ADDR		0x32
346f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX_ADDR		0x35
356f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX_BASE_ADDR		0x38
366f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX_MAX_ADDR		0x3B
376f9703d0SDarrick J. Wong #define ADT7470_REG_CFG				0x40
386f9703d0SDarrick J. Wong #define		ADT7470_FSPD_MASK		0x04
396f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM1			0x41
40fe03f28cSDarrick J. Wong #define		ADT7470_R1T_ALARM		0x01
41fe03f28cSDarrick J. Wong #define		ADT7470_R2T_ALARM		0x02
42fe03f28cSDarrick J. Wong #define		ADT7470_R3T_ALARM		0x04
43fe03f28cSDarrick J. Wong #define		ADT7470_R4T_ALARM		0x08
44fe03f28cSDarrick J. Wong #define		ADT7470_R5T_ALARM		0x10
45fe03f28cSDarrick J. Wong #define		ADT7470_R6T_ALARM		0x20
46fe03f28cSDarrick J. Wong #define		ADT7470_R7T_ALARM		0x40
47fe03f28cSDarrick J. Wong #define		ADT7470_OOL_ALARM		0x80
486f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM2			0x42
49fe03f28cSDarrick J. Wong #define		ADT7470_R8T_ALARM		0x01
50fe03f28cSDarrick J. Wong #define		ADT7470_R9T_ALARM		0x02
51fe03f28cSDarrick J. Wong #define		ADT7470_R10T_ALARM		0x04
52fe03f28cSDarrick J. Wong #define		ADT7470_FAN1_ALARM		0x10
53fe03f28cSDarrick J. Wong #define		ADT7470_FAN2_ALARM		0x20
54fe03f28cSDarrick J. Wong #define		ADT7470_FAN3_ALARM		0x40
55fe03f28cSDarrick J. Wong #define		ADT7470_FAN4_ALARM		0x80
566f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR	0x44
576f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR	0x57
586f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MIN_BASE_ADDR		0x58
596f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MIN_MAX_ADDR		0x5F
606f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX_BASE_ADDR		0x60
616f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX_MAX_ADDR		0x67
626f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_CFG_BASE_ADDR		0x68
636f9703d0SDarrick J. Wong #define ADT7470_REG_PWM12_CFG			0x68
646f9703d0SDarrick J. Wong #define		ADT7470_PWM2_AUTO_MASK		0x40
656f9703d0SDarrick J. Wong #define		ADT7470_PWM1_AUTO_MASK		0x80
662e75a4b7SDarrick J. Wong #define		ADT7470_PWM_AUTO_MASK		0xC0
676f9703d0SDarrick J. Wong #define ADT7470_REG_PWM34_CFG			0x69
686f9703d0SDarrick J. Wong #define		ADT7470_PWM3_AUTO_MASK		0x40
696f9703d0SDarrick J. Wong #define		ADT7470_PWM4_AUTO_MASK		0x80
706f9703d0SDarrick J. Wong #define	ADT7470_REG_PWM_MIN_BASE_ADDR		0x6A
716f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MIN_MAX_ADDR		0x6D
726f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR	0x6E
736f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR	0x71
74aa18cc91SJoshua Scott #define ADT7470_REG_CFG_2			0x74
756f9703d0SDarrick J. Wong #define ADT7470_REG_ACOUSTICS12			0x75
766f9703d0SDarrick J. Wong #define ADT7470_REG_ACOUSTICS34			0x76
776f9703d0SDarrick J. Wong #define ADT7470_REG_DEVICE			0x3D
786f9703d0SDarrick J. Wong #define ADT7470_REG_VENDOR			0x3E
796f9703d0SDarrick J. Wong #define ADT7470_REG_REVISION			0x3F
806f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM1_MASK			0x72
816f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM2_MASK			0x73
826f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR	0x7C
836f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR	0x7D
846f9703d0SDarrick J. Wong #define ADT7470_REG_MAX_ADDR			0x81
856f9703d0SDarrick J. Wong 
866f9703d0SDarrick J. Wong #define ADT7470_TEMP_COUNT	10
876f9703d0SDarrick J. Wong #define ADT7470_TEMP_REG(x)	(ADT7470_REG_TEMP_BASE_ADDR + (x))
886f9703d0SDarrick J. Wong #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
896f9703d0SDarrick J. Wong #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
906f9703d0SDarrick J. Wong 				((x) * 2) + 1)
916f9703d0SDarrick J. Wong 
926f9703d0SDarrick J. Wong #define ADT7470_FAN_COUNT	4
936f9703d0SDarrick J. Wong #define ADT7470_REG_FAN(x)	(ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
946f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MIN(x)	(ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
956f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX(x)	(ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
966f9703d0SDarrick J. Wong 
976f9703d0SDarrick J. Wong #define ADT7470_PWM_COUNT	4
986f9703d0SDarrick J. Wong #define ADT7470_REG_PWM(x)	(ADT7470_REG_PWM_BASE_ADDR + (x))
996f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX(x)	(ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
1006f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MIN(x)	(ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
1016f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_TMIN(x)	(ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
1026f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_CFG(x)	(ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
1036f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_AUTO_TEMP(x)	(ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
1046f9703d0SDarrick J. Wong 					((x) / 2))
1056f9703d0SDarrick J. Wong 
106fe03f28cSDarrick J. Wong #define ALARM2(x)		((x) << 8)
107fe03f28cSDarrick J. Wong 
1086f9703d0SDarrick J. Wong #define ADT7470_VENDOR		0x41
1096f9703d0SDarrick J. Wong #define ADT7470_DEVICE		0x70
1106f9703d0SDarrick J. Wong /* datasheet only mentions a revision 2 */
1116f9703d0SDarrick J. Wong #define ADT7470_REVISION	0x02
1126f9703d0SDarrick J. Wong 
1136f9703d0SDarrick J. Wong /* "all temps" according to hwmon sysfs interface spec */
1146f9703d0SDarrick J. Wong #define ADT7470_PWM_ALL_TEMPS	0x3FF
1156f9703d0SDarrick J. Wong 
1166f9703d0SDarrick J. Wong /* How often do we reread sensors values? (In jiffies) */
1176f9703d0SDarrick J. Wong #define SENSOR_REFRESH_INTERVAL	(5 * HZ)
1186f9703d0SDarrick J. Wong 
1196f9703d0SDarrick J. Wong /* How often do we reread sensor limit values? (In jiffies) */
1206f9703d0SDarrick J. Wong #define LIMIT_REFRESH_INTERVAL	(60 * HZ)
1216f9703d0SDarrick J. Wong 
1222f22d5dfSDarrick J. Wong /* Wait at least 200ms per sensor for 10 sensors */
1232f22d5dfSDarrick J. Wong #define TEMP_COLLECTION_TIME	2000
1246f9703d0SDarrick J. Wong 
12589fac11cSDarrick J. Wong /* auto update thing won't fire more than every 2s */
12689fac11cSDarrick J. Wong #define AUTO_UPDATE_INTERVAL	2000
12789fac11cSDarrick J. Wong 
1286f9703d0SDarrick J. Wong /* datasheet says to divide this number by the fan reading to get fan rpm */
1296f9703d0SDarrick J. Wong #define FAN_PERIOD_TO_RPM(x)	((90000 * 60) / (x))
1306f9703d0SDarrick J. Wong #define FAN_RPM_TO_PERIOD	FAN_PERIOD_TO_RPM
1316f9703d0SDarrick J. Wong #define FAN_PERIOD_INVALID	65535
1326f9703d0SDarrick J. Wong #define FAN_DATA_VALID(x)	((x) && (x) != FAN_PERIOD_INVALID)
1336f9703d0SDarrick J. Wong 
134aa18cc91SJoshua Scott /* Config registers 1 and 2 include fields for selecting the PWM frequency */
135aa18cc91SJoshua Scott #define ADT7470_CFG_LF		0x40
136aa18cc91SJoshua Scott #define ADT7470_FREQ_MASK	0x70
137aa18cc91SJoshua Scott #define ADT7470_FREQ_SHIFT	4
138aa18cc91SJoshua Scott 
1396f9703d0SDarrick J. Wong struct adt7470_data {
14030485776SAxel Lin 	struct i2c_client	*client;
1416f9703d0SDarrick J. Wong 	struct mutex		lock;
1426f9703d0SDarrick J. Wong 	char			sensors_valid;
1436f9703d0SDarrick J. Wong 	char			limits_valid;
1446f9703d0SDarrick J. Wong 	unsigned long		sensors_last_updated;	/* In jiffies */
1456f9703d0SDarrick J. Wong 	unsigned long		limits_last_updated;	/* In jiffies */
1466f9703d0SDarrick J. Wong 
1472f22d5dfSDarrick J. Wong 	int			num_temp_sensors;	/* -1 = probe */
14889fac11cSDarrick J. Wong 	int			temperatures_probed;
1492f22d5dfSDarrick J. Wong 
1506f9703d0SDarrick J. Wong 	s8			temp[ADT7470_TEMP_COUNT];
1516f9703d0SDarrick J. Wong 	s8			temp_min[ADT7470_TEMP_COUNT];
1526f9703d0SDarrick J. Wong 	s8			temp_max[ADT7470_TEMP_COUNT];
1536f9703d0SDarrick J. Wong 	u16			fan[ADT7470_FAN_COUNT];
1546f9703d0SDarrick J. Wong 	u16			fan_min[ADT7470_FAN_COUNT];
1556f9703d0SDarrick J. Wong 	u16			fan_max[ADT7470_FAN_COUNT];
156fe03f28cSDarrick J. Wong 	u16			alarm;
157fe03f28cSDarrick J. Wong 	u16			alarms_mask;
1586f9703d0SDarrick J. Wong 	u8			force_pwm_max;
1596f9703d0SDarrick J. Wong 	u8			pwm[ADT7470_PWM_COUNT];
1606f9703d0SDarrick J. Wong 	u8			pwm_max[ADT7470_PWM_COUNT];
1616f9703d0SDarrick J. Wong 	u8			pwm_automatic[ADT7470_PWM_COUNT];
1626f9703d0SDarrick J. Wong 	u8			pwm_min[ADT7470_PWM_COUNT];
1636f9703d0SDarrick J. Wong 	s8			pwm_tmin[ADT7470_PWM_COUNT];
1646f9703d0SDarrick J. Wong 	u8			pwm_auto_temp[ADT7470_PWM_COUNT];
16589fac11cSDarrick J. Wong 
16689fac11cSDarrick J. Wong 	struct task_struct	*auto_update;
16789fac11cSDarrick J. Wong 	unsigned int		auto_update_interval;
1686f9703d0SDarrick J. Wong };
1696f9703d0SDarrick J. Wong 
1706f9703d0SDarrick J. Wong /*
1716f9703d0SDarrick J. Wong  * 16-bit registers on the ADT7470 are low-byte first.  The data sheet says
1726f9703d0SDarrick J. Wong  * that the low byte must be read before the high byte.
1736f9703d0SDarrick J. Wong  */
1746f9703d0SDarrick J. Wong static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
1756f9703d0SDarrick J. Wong {
1766f9703d0SDarrick J. Wong 	u16 foo;
1776f9703d0SDarrick J. Wong 	foo = i2c_smbus_read_byte_data(client, reg);
1786f9703d0SDarrick J. Wong 	foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
1796f9703d0SDarrick J. Wong 	return foo;
1806f9703d0SDarrick J. Wong }
1816f9703d0SDarrick J. Wong 
1826f9703d0SDarrick J. Wong static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
1836f9703d0SDarrick J. Wong 					  u16 value)
1846f9703d0SDarrick J. Wong {
1856f9703d0SDarrick J. Wong 	return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
18693d783bcSCurt Brune 	       || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
1876f9703d0SDarrick J. Wong }
1886f9703d0SDarrick J. Wong 
18989fac11cSDarrick J. Wong /* Probe for temperature sensors.  Assumes lock is held */
19089fac11cSDarrick J. Wong static int adt7470_read_temperatures(struct i2c_client *client,
19189fac11cSDarrick J. Wong 				     struct adt7470_data *data)
1926f9703d0SDarrick J. Wong {
19389fac11cSDarrick J. Wong 	unsigned long res;
1946f9703d0SDarrick J. Wong 	int i;
19589fac11cSDarrick J. Wong 	u8 cfg, pwm[4], pwm_cfg[2];
1966f9703d0SDarrick J. Wong 
1972e75a4b7SDarrick J. Wong 	/* save pwm[1-4] config register */
1982e75a4b7SDarrick J. Wong 	pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
1992e75a4b7SDarrick J. Wong 	pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
2002e75a4b7SDarrick J. Wong 
2012e75a4b7SDarrick J. Wong 	/* set manual pwm to whatever it is set to now */
2022e75a4b7SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
2032e75a4b7SDarrick J. Wong 		pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
2042e75a4b7SDarrick J. Wong 
2052e75a4b7SDarrick J. Wong 	/* put pwm in manual mode */
2062e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
2072e75a4b7SDarrick J. Wong 		pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
2082e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
2092e75a4b7SDarrick J. Wong 		pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
2102e75a4b7SDarrick J. Wong 
2112e75a4b7SDarrick J. Wong 	/* write pwm control to whatever it was */
2122e75a4b7SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
2132e75a4b7SDarrick J. Wong 		i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
2142e75a4b7SDarrick J. Wong 
2156f9703d0SDarrick J. Wong 	/* start reading temperature sensors */
2166f9703d0SDarrick J. Wong 	cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
2176f9703d0SDarrick J. Wong 	cfg |= 0x80;
2186f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
2196f9703d0SDarrick J. Wong 
2202f22d5dfSDarrick J. Wong 	/* Delay is 200ms * number of temp sensors. */
22189fac11cSDarrick J. Wong 	res = msleep_interruptible((data->num_temp_sensors >= 0 ?
2222f22d5dfSDarrick J. Wong 				    data->num_temp_sensors * 200 :
2232f22d5dfSDarrick J. Wong 				    TEMP_COLLECTION_TIME));
2246f9703d0SDarrick J. Wong 
2256f9703d0SDarrick J. Wong 	/* done reading temperature sensors */
2266f9703d0SDarrick J. Wong 	cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
2276f9703d0SDarrick J. Wong 	cfg &= ~0x80;
2286f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
2296f9703d0SDarrick J. Wong 
2302e75a4b7SDarrick J. Wong 	/* restore pwm[1-4] config registers */
2312e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
2322e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
2332e75a4b7SDarrick J. Wong 
23489fac11cSDarrick J. Wong 	if (res) {
2352e991201SJoe Perches 		pr_err("ha ha, interrupted\n");
23689fac11cSDarrick J. Wong 		return -EAGAIN;
23789fac11cSDarrick J. Wong 	}
23889fac11cSDarrick J. Wong 
23989fac11cSDarrick J. Wong 	/* Only count fans if we have to */
24089fac11cSDarrick J. Wong 	if (data->num_temp_sensors >= 0)
24189fac11cSDarrick J. Wong 		return 0;
24289fac11cSDarrick J. Wong 
24389fac11cSDarrick J. Wong 	for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
24489fac11cSDarrick J. Wong 		data->temp[i] = i2c_smbus_read_byte_data(client,
24589fac11cSDarrick J. Wong 						ADT7470_TEMP_REG(i));
24689fac11cSDarrick J. Wong 		if (data->temp[i])
24789fac11cSDarrick J. Wong 			data->num_temp_sensors = i + 1;
24889fac11cSDarrick J. Wong 	}
24989fac11cSDarrick J. Wong 	data->temperatures_probed = 1;
25089fac11cSDarrick J. Wong 	return 0;
25189fac11cSDarrick J. Wong }
25289fac11cSDarrick J. Wong 
25389fac11cSDarrick J. Wong static int adt7470_update_thread(void *p)
25489fac11cSDarrick J. Wong {
25589fac11cSDarrick J. Wong 	struct i2c_client *client = p;
25689fac11cSDarrick J. Wong 	struct adt7470_data *data = i2c_get_clientdata(client);
25789fac11cSDarrick J. Wong 
25889fac11cSDarrick J. Wong 	while (!kthread_should_stop()) {
25989fac11cSDarrick J. Wong 		mutex_lock(&data->lock);
26089fac11cSDarrick J. Wong 		adt7470_read_temperatures(client, data);
26189fac11cSDarrick J. Wong 		mutex_unlock(&data->lock);
26293cacfd4SJoshua Scott 
26393cacfd4SJoshua Scott 		set_current_state(TASK_INTERRUPTIBLE);
26489fac11cSDarrick J. Wong 		if (kthread_should_stop())
26589fac11cSDarrick J. Wong 			break;
26693cacfd4SJoshua Scott 
26793cacfd4SJoshua Scott 		schedule_timeout(msecs_to_jiffies(data->auto_update_interval));
26889fac11cSDarrick J. Wong 	}
26989fac11cSDarrick J. Wong 
27089fac11cSDarrick J. Wong 	return 0;
27189fac11cSDarrick J. Wong }
27289fac11cSDarrick J. Wong 
27389fac11cSDarrick J. Wong static struct adt7470_data *adt7470_update_device(struct device *dev)
27489fac11cSDarrick J. Wong {
27530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
27630485776SAxel Lin 	struct i2c_client *client = data->client;
27789fac11cSDarrick J. Wong 	unsigned long local_jiffies = jiffies;
27889fac11cSDarrick J. Wong 	u8 cfg;
27989fac11cSDarrick J. Wong 	int i;
28089fac11cSDarrick J. Wong 	int need_sensors = 1;
28189fac11cSDarrick J. Wong 	int need_limits = 1;
28289fac11cSDarrick J. Wong 
28389fac11cSDarrick J. Wong 	/*
28489fac11cSDarrick J. Wong 	 * Figure out if we need to update the shadow registers.
28589fac11cSDarrick J. Wong 	 * Lockless means that we may occasionally report out of
28689fac11cSDarrick J. Wong 	 * date data.
28789fac11cSDarrick J. Wong 	 */
28889fac11cSDarrick J. Wong 	if (time_before(local_jiffies, data->sensors_last_updated +
28989fac11cSDarrick J. Wong 			SENSOR_REFRESH_INTERVAL) &&
29089fac11cSDarrick J. Wong 	    data->sensors_valid)
29189fac11cSDarrick J. Wong 		need_sensors = 0;
29289fac11cSDarrick J. Wong 
29389fac11cSDarrick J. Wong 	if (time_before(local_jiffies, data->limits_last_updated +
29489fac11cSDarrick J. Wong 			LIMIT_REFRESH_INTERVAL) &&
29589fac11cSDarrick J. Wong 	    data->limits_valid)
29689fac11cSDarrick J. Wong 		need_limits = 0;
29789fac11cSDarrick J. Wong 
29889fac11cSDarrick J. Wong 	if (!need_sensors && !need_limits)
29989fac11cSDarrick J. Wong 		return data;
30089fac11cSDarrick J. Wong 
30189fac11cSDarrick J. Wong 	mutex_lock(&data->lock);
30289fac11cSDarrick J. Wong 	if (!need_sensors)
30389fac11cSDarrick J. Wong 		goto no_sensor_update;
30489fac11cSDarrick J. Wong 
30589fac11cSDarrick J. Wong 	if (!data->temperatures_probed)
30689fac11cSDarrick J. Wong 		adt7470_read_temperatures(client, data);
30789fac11cSDarrick J. Wong 	else
3086f9703d0SDarrick J. Wong 		for (i = 0; i < ADT7470_TEMP_COUNT; i++)
3096f9703d0SDarrick J. Wong 			data->temp[i] = i2c_smbus_read_byte_data(client,
3106f9703d0SDarrick J. Wong 						ADT7470_TEMP_REG(i));
3116f9703d0SDarrick J. Wong 
3126f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
3136f9703d0SDarrick J. Wong 		data->fan[i] = adt7470_read_word_data(client,
3146f9703d0SDarrick J. Wong 						ADT7470_REG_FAN(i));
3156f9703d0SDarrick J. Wong 
3166f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
3176f9703d0SDarrick J. Wong 		int reg;
3186f9703d0SDarrick J. Wong 		int reg_mask;
3196f9703d0SDarrick J. Wong 
3206f9703d0SDarrick J. Wong 		data->pwm[i] = i2c_smbus_read_byte_data(client,
3216f9703d0SDarrick J. Wong 						ADT7470_REG_PWM(i));
3226f9703d0SDarrick J. Wong 
3236f9703d0SDarrick J. Wong 		if (i % 2)
3246f9703d0SDarrick J. Wong 			reg_mask = ADT7470_PWM2_AUTO_MASK;
3256f9703d0SDarrick J. Wong 		else
3266f9703d0SDarrick J. Wong 			reg_mask = ADT7470_PWM1_AUTO_MASK;
3276f9703d0SDarrick J. Wong 
3286f9703d0SDarrick J. Wong 		reg = ADT7470_REG_PWM_CFG(i);
3296f9703d0SDarrick J. Wong 		if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
3306f9703d0SDarrick J. Wong 			data->pwm_automatic[i] = 1;
3316f9703d0SDarrick J. Wong 		else
3326f9703d0SDarrick J. Wong 			data->pwm_automatic[i] = 0;
3336f9703d0SDarrick J. Wong 
3346f9703d0SDarrick J. Wong 		reg = ADT7470_REG_PWM_AUTO_TEMP(i);
3356f9703d0SDarrick J. Wong 		cfg = i2c_smbus_read_byte_data(client, reg);
3366f9703d0SDarrick J. Wong 		if (!(i % 2))
3376f9703d0SDarrick J. Wong 			data->pwm_auto_temp[i] = cfg >> 4;
3386f9703d0SDarrick J. Wong 		else
3396f9703d0SDarrick J. Wong 			data->pwm_auto_temp[i] = cfg & 0xF;
3406f9703d0SDarrick J. Wong 	}
3416f9703d0SDarrick J. Wong 
3426f9703d0SDarrick J. Wong 	if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
3436f9703d0SDarrick J. Wong 	    ADT7470_FSPD_MASK)
3446f9703d0SDarrick J. Wong 		data->force_pwm_max = 1;
3456f9703d0SDarrick J. Wong 	else
3466f9703d0SDarrick J. Wong 		data->force_pwm_max = 0;
3476f9703d0SDarrick J. Wong 
348fe03f28cSDarrick J. Wong 	data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
349fe03f28cSDarrick J. Wong 	if (data->alarm & ADT7470_OOL_ALARM)
350fe03f28cSDarrick J. Wong 		data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
351fe03f28cSDarrick J. Wong 							ADT7470_REG_ALARM2));
3526f9703d0SDarrick J. Wong 	data->alarms_mask = adt7470_read_word_data(client,
3536f9703d0SDarrick J. Wong 						   ADT7470_REG_ALARM1_MASK);
3546f9703d0SDarrick J. Wong 
3556f9703d0SDarrick J. Wong 	data->sensors_last_updated = local_jiffies;
3566f9703d0SDarrick J. Wong 	data->sensors_valid = 1;
3576f9703d0SDarrick J. Wong 
3586f9703d0SDarrick J. Wong no_sensor_update:
35989fac11cSDarrick J. Wong 	if (!need_limits)
3606f9703d0SDarrick J. Wong 		goto out;
3616f9703d0SDarrick J. Wong 
3626f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
3636f9703d0SDarrick J. Wong 		data->temp_min[i] = i2c_smbus_read_byte_data(client,
3646f9703d0SDarrick J. Wong 						ADT7470_TEMP_MIN_REG(i));
3656f9703d0SDarrick J. Wong 		data->temp_max[i] = i2c_smbus_read_byte_data(client,
3666f9703d0SDarrick J. Wong 						ADT7470_TEMP_MAX_REG(i));
3676f9703d0SDarrick J. Wong 	}
3686f9703d0SDarrick J. Wong 
3696f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++) {
3706f9703d0SDarrick J. Wong 		data->fan_min[i] = adt7470_read_word_data(client,
3716f9703d0SDarrick J. Wong 						ADT7470_REG_FAN_MIN(i));
3726f9703d0SDarrick J. Wong 		data->fan_max[i] = adt7470_read_word_data(client,
3736f9703d0SDarrick J. Wong 						ADT7470_REG_FAN_MAX(i));
3746f9703d0SDarrick J. Wong 	}
3756f9703d0SDarrick J. Wong 
3766f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
3776f9703d0SDarrick J. Wong 		data->pwm_max[i] = i2c_smbus_read_byte_data(client,
3786f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_MAX(i));
3796f9703d0SDarrick J. Wong 		data->pwm_min[i] = i2c_smbus_read_byte_data(client,
3806f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_MIN(i));
3816f9703d0SDarrick J. Wong 		data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
3826f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_TMIN(i));
3836f9703d0SDarrick J. Wong 	}
3846f9703d0SDarrick J. Wong 
3856f9703d0SDarrick J. Wong 	data->limits_last_updated = local_jiffies;
3866f9703d0SDarrick J. Wong 	data->limits_valid = 1;
3876f9703d0SDarrick J. Wong 
3886f9703d0SDarrick J. Wong out:
3896f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
3906f9703d0SDarrick J. Wong 	return data;
3916f9703d0SDarrick J. Wong }
3926f9703d0SDarrick J. Wong 
393808fc6c2SJulia Lawall static ssize_t auto_update_interval_show(struct device *dev,
39489fac11cSDarrick J. Wong 					 struct device_attribute *devattr,
39589fac11cSDarrick J. Wong 					 char *buf)
39689fac11cSDarrick J. Wong {
39789fac11cSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
39889fac11cSDarrick J. Wong 	return sprintf(buf, "%d\n", data->auto_update_interval);
39989fac11cSDarrick J. Wong }
40089fac11cSDarrick J. Wong 
401808fc6c2SJulia Lawall static ssize_t auto_update_interval_store(struct device *dev,
40289fac11cSDarrick J. Wong 					  struct device_attribute *devattr,
403808fc6c2SJulia Lawall 					  const char *buf, size_t count)
40489fac11cSDarrick J. Wong {
40530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
40689fac11cSDarrick J. Wong 	long temp;
40789fac11cSDarrick J. Wong 
408179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
40989fac11cSDarrick J. Wong 		return -EINVAL;
41089fac11cSDarrick J. Wong 
4112a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 60000);
41289fac11cSDarrick J. Wong 
41389fac11cSDarrick J. Wong 	mutex_lock(&data->lock);
41489fac11cSDarrick J. Wong 	data->auto_update_interval = temp;
41589fac11cSDarrick J. Wong 	mutex_unlock(&data->lock);
41689fac11cSDarrick J. Wong 
41789fac11cSDarrick J. Wong 	return count;
41889fac11cSDarrick J. Wong }
41989fac11cSDarrick J. Wong 
420808fc6c2SJulia Lawall static ssize_t num_temp_sensors_show(struct device *dev,
4212f22d5dfSDarrick J. Wong 				     struct device_attribute *devattr,
4222f22d5dfSDarrick J. Wong 				     char *buf)
4232f22d5dfSDarrick J. Wong {
4242f22d5dfSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
4252f22d5dfSDarrick J. Wong 	return sprintf(buf, "%d\n", data->num_temp_sensors);
4262f22d5dfSDarrick J. Wong }
4272f22d5dfSDarrick J. Wong 
428808fc6c2SJulia Lawall static ssize_t num_temp_sensors_store(struct device *dev,
4292f22d5dfSDarrick J. Wong 				      struct device_attribute *devattr,
430808fc6c2SJulia Lawall 				      const char *buf, size_t count)
4312f22d5dfSDarrick J. Wong {
43230485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
4332f22d5dfSDarrick J. Wong 	long temp;
4342f22d5dfSDarrick J. Wong 
435179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
4362f22d5dfSDarrick J. Wong 		return -EINVAL;
4372f22d5dfSDarrick J. Wong 
4382a844c14SGuenter Roeck 	temp = clamp_val(temp, -1, 10);
4392f22d5dfSDarrick J. Wong 
4402f22d5dfSDarrick J. Wong 	mutex_lock(&data->lock);
4412f22d5dfSDarrick J. Wong 	data->num_temp_sensors = temp;
44289fac11cSDarrick J. Wong 	if (temp < 0)
44389fac11cSDarrick J. Wong 		data->temperatures_probed = 0;
4442f22d5dfSDarrick J. Wong 	mutex_unlock(&data->lock);
4452f22d5dfSDarrick J. Wong 
4462f22d5dfSDarrick J. Wong 	return count;
4472f22d5dfSDarrick J. Wong }
4482f22d5dfSDarrick J. Wong 
44942291a5aSGuenter Roeck static ssize_t temp_min_show(struct device *dev,
45042291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
4516f9703d0SDarrick J. Wong {
4526f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
4536f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
4546f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
4556f9703d0SDarrick J. Wong }
4566f9703d0SDarrick J. Wong 
45742291a5aSGuenter Roeck static ssize_t temp_min_store(struct device *dev,
4586f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
45942291a5aSGuenter Roeck 			      const char *buf, size_t count)
4606f9703d0SDarrick J. Wong {
4616f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
46230485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
46330485776SAxel Lin 	struct i2c_client *client = data->client;
46405a9bd46SDarrick J. Wong 	long temp;
46505a9bd46SDarrick J. Wong 
466179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
46705a9bd46SDarrick J. Wong 		return -EINVAL;
46805a9bd46SDarrick J. Wong 
46964bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
4708f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
4716f9703d0SDarrick J. Wong 
4726f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
4736f9703d0SDarrick J. Wong 	data->temp_min[attr->index] = temp;
4746f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
4756f9703d0SDarrick J. Wong 				  temp);
4766f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
4776f9703d0SDarrick J. Wong 
4786f9703d0SDarrick J. Wong 	return count;
4796f9703d0SDarrick J. Wong }
4806f9703d0SDarrick J. Wong 
48142291a5aSGuenter Roeck static ssize_t temp_max_show(struct device *dev,
48242291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
4836f9703d0SDarrick J. Wong {
4846f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
4856f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
4866f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
4876f9703d0SDarrick J. Wong }
4886f9703d0SDarrick J. Wong 
48942291a5aSGuenter Roeck static ssize_t temp_max_store(struct device *dev,
4906f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
49142291a5aSGuenter Roeck 			      const char *buf, size_t count)
4926f9703d0SDarrick J. Wong {
4936f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
49430485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
49530485776SAxel Lin 	struct i2c_client *client = data->client;
49605a9bd46SDarrick J. Wong 	long temp;
49705a9bd46SDarrick J. Wong 
498179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
49905a9bd46SDarrick J. Wong 		return -EINVAL;
50005a9bd46SDarrick J. Wong 
50164bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
5028f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
5036f9703d0SDarrick J. Wong 
5046f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5056f9703d0SDarrick J. Wong 	data->temp_max[attr->index] = temp;
5066f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
5076f9703d0SDarrick J. Wong 				  temp);
5086f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
5096f9703d0SDarrick J. Wong 
5106f9703d0SDarrick J. Wong 	return count;
5116f9703d0SDarrick J. Wong }
5126f9703d0SDarrick J. Wong 
51342291a5aSGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
5146f9703d0SDarrick J. Wong 			 char *buf)
5156f9703d0SDarrick J. Wong {
5166f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5176f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5186f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
5196f9703d0SDarrick J. Wong }
5206f9703d0SDarrick J. Wong 
521808fc6c2SJulia Lawall static ssize_t alarm_mask_show(struct device *dev,
5226f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
5236f9703d0SDarrick J. Wong 			   char *buf)
5246f9703d0SDarrick J. Wong {
5256f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5266f9703d0SDarrick J. Wong 
5276f9703d0SDarrick J. Wong 	return sprintf(buf, "%x\n", data->alarms_mask);
5286f9703d0SDarrick J. Wong }
5296f9703d0SDarrick J. Wong 
530808fc6c2SJulia Lawall static ssize_t alarm_mask_store(struct device *dev,
531feca3132SJoshua Scott 				struct device_attribute *devattr,
532808fc6c2SJulia Lawall 				const char *buf, size_t count)
533feca3132SJoshua Scott {
534feca3132SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
535feca3132SJoshua Scott 	long mask;
536feca3132SJoshua Scott 
537feca3132SJoshua Scott 	if (kstrtoul(buf, 0, &mask))
538feca3132SJoshua Scott 		return -EINVAL;
539feca3132SJoshua Scott 
540feca3132SJoshua Scott 	if (mask & ~0xffff)
541feca3132SJoshua Scott 		return -EINVAL;
542feca3132SJoshua Scott 
543feca3132SJoshua Scott 	mutex_lock(&data->lock);
544feca3132SJoshua Scott 	data->alarms_mask = mask;
545feca3132SJoshua Scott 	adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
546feca3132SJoshua Scott 	mutex_unlock(&data->lock);
547feca3132SJoshua Scott 
548feca3132SJoshua Scott 	return count;
549feca3132SJoshua Scott }
550feca3132SJoshua Scott 
55142291a5aSGuenter Roeck static ssize_t fan_max_show(struct device *dev,
55242291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
5536f9703d0SDarrick J. Wong {
5546f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5556f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5566f9703d0SDarrick J. Wong 
5576f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_max[attr->index]))
5586f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
5596f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
5606f9703d0SDarrick J. Wong 	else
5616f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
5626f9703d0SDarrick J. Wong }
5636f9703d0SDarrick J. Wong 
56442291a5aSGuenter Roeck static ssize_t fan_max_store(struct device *dev,
5656f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
5666f9703d0SDarrick J. Wong 			     const char *buf, size_t count)
5676f9703d0SDarrick J. Wong {
5686f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
56930485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
57030485776SAxel Lin 	struct i2c_client *client = data->client;
57105a9bd46SDarrick J. Wong 	long temp;
5726f9703d0SDarrick J. Wong 
573179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
5746f9703d0SDarrick J. Wong 		return -EINVAL;
57505a9bd46SDarrick J. Wong 
5766f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
5772a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
5786f9703d0SDarrick J. Wong 
5796f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5806f9703d0SDarrick J. Wong 	data->fan_max[attr->index] = temp;
5816f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
5826f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
5836f9703d0SDarrick J. Wong 
5846f9703d0SDarrick J. Wong 	return count;
5856f9703d0SDarrick J. Wong }
5866f9703d0SDarrick J. Wong 
58742291a5aSGuenter Roeck static ssize_t fan_min_show(struct device *dev,
58842291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
5896f9703d0SDarrick J. Wong {
5906f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5916f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5926f9703d0SDarrick J. Wong 
5936f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_min[attr->index]))
5946f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
5956f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
5966f9703d0SDarrick J. Wong 	else
5976f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
5986f9703d0SDarrick J. Wong }
5996f9703d0SDarrick J. Wong 
60042291a5aSGuenter Roeck static ssize_t fan_min_store(struct device *dev,
6016f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
6026f9703d0SDarrick J. Wong 			     const char *buf, size_t count)
6036f9703d0SDarrick J. Wong {
6046f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
60530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
60630485776SAxel Lin 	struct i2c_client *client = data->client;
60705a9bd46SDarrick J. Wong 	long temp;
6086f9703d0SDarrick J. Wong 
609179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
6106f9703d0SDarrick J. Wong 		return -EINVAL;
61105a9bd46SDarrick J. Wong 
6126f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
6132a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
6146f9703d0SDarrick J. Wong 
6156f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6166f9703d0SDarrick J. Wong 	data->fan_min[attr->index] = temp;
6176f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
6186f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6196f9703d0SDarrick J. Wong 
6206f9703d0SDarrick J. Wong 	return count;
6216f9703d0SDarrick J. Wong }
6226f9703d0SDarrick J. Wong 
62342291a5aSGuenter Roeck static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
6246f9703d0SDarrick J. Wong 			char *buf)
6256f9703d0SDarrick J. Wong {
6266f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6276f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6286f9703d0SDarrick J. Wong 
6296f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan[attr->index]))
6306f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6316f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
6326f9703d0SDarrick J. Wong 	else
6336f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6346f9703d0SDarrick J. Wong }
6356f9703d0SDarrick J. Wong 
63642291a5aSGuenter Roeck static ssize_t force_pwm_max_show(struct device *dev,
63742291a5aSGuenter Roeck 				  struct device_attribute *devattr, char *buf)
6386f9703d0SDarrick J. Wong {
6396f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6406f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->force_pwm_max);
6416f9703d0SDarrick J. Wong }
6426f9703d0SDarrick J. Wong 
64342291a5aSGuenter Roeck static ssize_t force_pwm_max_store(struct device *dev,
6446f9703d0SDarrick J. Wong 				   struct device_attribute *devattr,
64542291a5aSGuenter Roeck 				   const char *buf, size_t count)
6466f9703d0SDarrick J. Wong {
64730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
64830485776SAxel Lin 	struct i2c_client *client = data->client;
64905a9bd46SDarrick J. Wong 	long temp;
6506f9703d0SDarrick J. Wong 	u8 reg;
6516f9703d0SDarrick J. Wong 
652179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
65305a9bd46SDarrick J. Wong 		return -EINVAL;
65405a9bd46SDarrick J. Wong 
6556f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6566f9703d0SDarrick J. Wong 	data->force_pwm_max = temp;
6576f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
6586f9703d0SDarrick J. Wong 	if (temp)
6596f9703d0SDarrick J. Wong 		reg |= ADT7470_FSPD_MASK;
6606f9703d0SDarrick J. Wong 	else
6616f9703d0SDarrick J. Wong 		reg &= ~ADT7470_FSPD_MASK;
6626f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
6636f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6646f9703d0SDarrick J. Wong 
6656f9703d0SDarrick J. Wong 	return count;
6666f9703d0SDarrick J. Wong }
6676f9703d0SDarrick J. Wong 
66842291a5aSGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
6696f9703d0SDarrick J. Wong 			char *buf)
6706f9703d0SDarrick J. Wong {
6716f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6726f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6736f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm[attr->index]);
6746f9703d0SDarrick J. Wong }
6756f9703d0SDarrick J. Wong 
67642291a5aSGuenter Roeck static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
6776f9703d0SDarrick J. Wong 			 const char *buf, size_t count)
6786f9703d0SDarrick J. Wong {
6796f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
68030485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
68130485776SAxel Lin 	struct i2c_client *client = data->client;
68205a9bd46SDarrick J. Wong 	long temp;
68305a9bd46SDarrick J. Wong 
684179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
68505a9bd46SDarrick J. Wong 		return -EINVAL;
68605a9bd46SDarrick J. Wong 
6872a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
6886f9703d0SDarrick J. Wong 
6896f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6906f9703d0SDarrick J. Wong 	data->pwm[attr->index] = temp;
6916f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
6926f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6936f9703d0SDarrick J. Wong 
6946f9703d0SDarrick J. Wong 	return count;
6956f9703d0SDarrick J. Wong }
6966f9703d0SDarrick J. Wong 
697aa18cc91SJoshua Scott /* These are the valid PWM frequencies to the nearest Hz */
698aa18cc91SJoshua Scott static const int adt7470_freq_map[] = {
699aa18cc91SJoshua Scott 	11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
700aa18cc91SJoshua Scott };
701aa18cc91SJoshua Scott 
702808fc6c2SJulia Lawall static ssize_t pwm1_freq_show(struct device *dev,
703aa18cc91SJoshua Scott 			      struct device_attribute *devattr, char *buf)
704aa18cc91SJoshua Scott {
705aa18cc91SJoshua Scott 	struct adt7470_data *data = adt7470_update_device(dev);
706aa18cc91SJoshua Scott 	unsigned char cfg_reg_1;
707aa18cc91SJoshua Scott 	unsigned char cfg_reg_2;
708aa18cc91SJoshua Scott 	int index;
709aa18cc91SJoshua Scott 
710aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
711aa18cc91SJoshua Scott 	cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
712aa18cc91SJoshua Scott 	cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
713aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
714aa18cc91SJoshua Scott 
715aa18cc91SJoshua Scott 	index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
716aa18cc91SJoshua Scott 	if (!(cfg_reg_1 & ADT7470_CFG_LF))
717aa18cc91SJoshua Scott 		index += 8;
718aa18cc91SJoshua Scott 	if (index >= ARRAY_SIZE(adt7470_freq_map))
719aa18cc91SJoshua Scott 		index = ARRAY_SIZE(adt7470_freq_map) - 1;
720aa18cc91SJoshua Scott 
721aa18cc91SJoshua Scott 	return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
722aa18cc91SJoshua Scott }
723aa18cc91SJoshua Scott 
724808fc6c2SJulia Lawall static ssize_t pwm1_freq_store(struct device *dev,
725aa18cc91SJoshua Scott 			       struct device_attribute *devattr,
726aa18cc91SJoshua Scott 			       const char *buf, size_t count)
727aa18cc91SJoshua Scott {
728aa18cc91SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
729aa18cc91SJoshua Scott 	struct i2c_client *client = data->client;
730aa18cc91SJoshua Scott 	long freq;
731aa18cc91SJoshua Scott 	int index;
732aa18cc91SJoshua Scott 	int low_freq = ADT7470_CFG_LF;
733aa18cc91SJoshua Scott 	unsigned char val;
734aa18cc91SJoshua Scott 
735aa18cc91SJoshua Scott 	if (kstrtol(buf, 10, &freq))
736aa18cc91SJoshua Scott 		return -EINVAL;
737aa18cc91SJoshua Scott 
738aa18cc91SJoshua Scott 	/* Round the user value given to the closest available frequency */
739aa18cc91SJoshua Scott 	index = find_closest(freq, adt7470_freq_map,
740aa18cc91SJoshua Scott 			     ARRAY_SIZE(adt7470_freq_map));
741aa18cc91SJoshua Scott 
742aa18cc91SJoshua Scott 	if (index >= 8) {
743aa18cc91SJoshua Scott 		index -= 8;
744aa18cc91SJoshua Scott 		low_freq = 0;
745aa18cc91SJoshua Scott 	}
746aa18cc91SJoshua Scott 
747aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
748aa18cc91SJoshua Scott 	/* Configuration Register 1 */
749aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
750aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
751aa18cc91SJoshua Scott 				  (val & ~ADT7470_CFG_LF) | low_freq);
752aa18cc91SJoshua Scott 	/* Configuration Register 2 */
753aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
754aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
755aa18cc91SJoshua Scott 		(val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
756aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
757aa18cc91SJoshua Scott 
758aa18cc91SJoshua Scott 	return count;
759aa18cc91SJoshua Scott }
760aa18cc91SJoshua Scott 
76142291a5aSGuenter Roeck static ssize_t pwm_max_show(struct device *dev,
76242291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
7636f9703d0SDarrick J. Wong {
7646f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
7656f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
7666f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
7676f9703d0SDarrick J. Wong }
7686f9703d0SDarrick J. Wong 
76942291a5aSGuenter Roeck static ssize_t pwm_max_store(struct device *dev,
7706f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
77142291a5aSGuenter Roeck 			     const char *buf, size_t count)
7726f9703d0SDarrick J. Wong {
7736f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
77430485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
77530485776SAxel Lin 	struct i2c_client *client = data->client;
77605a9bd46SDarrick J. Wong 	long temp;
77705a9bd46SDarrick J. Wong 
778179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
77905a9bd46SDarrick J. Wong 		return -EINVAL;
78005a9bd46SDarrick J. Wong 
7812a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
7826f9703d0SDarrick J. Wong 
7836f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
7846f9703d0SDarrick J. Wong 	data->pwm_max[attr->index] = temp;
7856f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
7866f9703d0SDarrick J. Wong 				  temp);
7876f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
7886f9703d0SDarrick J. Wong 
7896f9703d0SDarrick J. Wong 	return count;
7906f9703d0SDarrick J. Wong }
7916f9703d0SDarrick J. Wong 
79242291a5aSGuenter Roeck static ssize_t pwm_min_show(struct device *dev,
79342291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
7946f9703d0SDarrick J. Wong {
7956f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
7966f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
7976f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
7986f9703d0SDarrick J. Wong }
7996f9703d0SDarrick J. Wong 
80042291a5aSGuenter Roeck static ssize_t pwm_min_store(struct device *dev,
8016f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
80242291a5aSGuenter Roeck 			     const char *buf, size_t count)
8036f9703d0SDarrick J. Wong {
8046f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
80530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
80630485776SAxel Lin 	struct i2c_client *client = data->client;
80705a9bd46SDarrick J. Wong 	long temp;
80805a9bd46SDarrick J. Wong 
809179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
81005a9bd46SDarrick J. Wong 		return -EINVAL;
81105a9bd46SDarrick J. Wong 
8122a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
8136f9703d0SDarrick J. Wong 
8146f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8156f9703d0SDarrick J. Wong 	data->pwm_min[attr->index] = temp;
8166f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
8176f9703d0SDarrick J. Wong 				  temp);
8186f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8196f9703d0SDarrick J. Wong 
8206f9703d0SDarrick J. Wong 	return count;
8216f9703d0SDarrick J. Wong }
8226f9703d0SDarrick J. Wong 
82342291a5aSGuenter Roeck static ssize_t pwm_tmax_show(struct device *dev,
82442291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
8256f9703d0SDarrick J. Wong {
8266f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8276f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8286f9703d0SDarrick J. Wong 	/* the datasheet says that tmax = tmin + 20C */
8296f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
8306f9703d0SDarrick J. Wong }
8316f9703d0SDarrick J. Wong 
83242291a5aSGuenter Roeck static ssize_t pwm_tmin_show(struct device *dev,
83342291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
8346f9703d0SDarrick J. Wong {
8356f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8366f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8376f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
8386f9703d0SDarrick J. Wong }
8396f9703d0SDarrick J. Wong 
84042291a5aSGuenter Roeck static ssize_t pwm_tmin_store(struct device *dev,
8416f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
84242291a5aSGuenter Roeck 			      const char *buf, size_t count)
8436f9703d0SDarrick J. Wong {
8446f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
84530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
84630485776SAxel Lin 	struct i2c_client *client = data->client;
84705a9bd46SDarrick J. Wong 	long temp;
84805a9bd46SDarrick J. Wong 
849179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
85005a9bd46SDarrick J. Wong 		return -EINVAL;
85105a9bd46SDarrick J. Wong 
85264bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
8538f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
8546f9703d0SDarrick J. Wong 
8556f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8566f9703d0SDarrick J. Wong 	data->pwm_tmin[attr->index] = temp;
8576f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
8586f9703d0SDarrick J. Wong 				  temp);
8596f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8606f9703d0SDarrick J. Wong 
8616f9703d0SDarrick J. Wong 	return count;
8626f9703d0SDarrick J. Wong }
8636f9703d0SDarrick J. Wong 
86442291a5aSGuenter Roeck static ssize_t pwm_auto_show(struct device *dev,
86542291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
8666f9703d0SDarrick J. Wong {
8676f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8686f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8696f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
8706f9703d0SDarrick J. Wong }
8716f9703d0SDarrick J. Wong 
87242291a5aSGuenter Roeck static ssize_t pwm_auto_store(struct device *dev,
8736f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
87442291a5aSGuenter Roeck 			      const char *buf, size_t count)
8756f9703d0SDarrick J. Wong {
8766f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
87730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
87830485776SAxel Lin 	struct i2c_client *client = data->client;
8796f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
8806f9703d0SDarrick J. Wong 	int pwm_auto_reg_mask;
88105a9bd46SDarrick J. Wong 	long temp;
8826f9703d0SDarrick J. Wong 	u8 reg;
8836f9703d0SDarrick J. Wong 
884179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
88505a9bd46SDarrick J. Wong 		return -EINVAL;
88605a9bd46SDarrick J. Wong 
8876f9703d0SDarrick J. Wong 	if (attr->index % 2)
8886f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
8896f9703d0SDarrick J. Wong 	else
8906f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
8916f9703d0SDarrick J. Wong 
8926f9703d0SDarrick J. Wong 	if (temp != 2 && temp != 1)
8936f9703d0SDarrick J. Wong 		return -EINVAL;
8946f9703d0SDarrick J. Wong 	temp--;
8956f9703d0SDarrick J. Wong 
8966f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8976f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
8986f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
8996f9703d0SDarrick J. Wong 	if (temp)
9006f9703d0SDarrick J. Wong 		reg |= pwm_auto_reg_mask;
9016f9703d0SDarrick J. Wong 	else
9026f9703d0SDarrick J. Wong 		reg &= ~pwm_auto_reg_mask;
9036f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
9046f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9056f9703d0SDarrick J. Wong 
9066f9703d0SDarrick J. Wong 	return count;
9076f9703d0SDarrick J. Wong }
9086f9703d0SDarrick J. Wong 
90942291a5aSGuenter Roeck static ssize_t pwm_auto_temp_show(struct device *dev,
91042291a5aSGuenter Roeck 				  struct device_attribute *devattr, char *buf)
9116f9703d0SDarrick J. Wong {
9126f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9136f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
9146f9703d0SDarrick J. Wong 	u8 ctrl = data->pwm_auto_temp[attr->index];
9156f9703d0SDarrick J. Wong 
9166f9703d0SDarrick J. Wong 	if (ctrl)
9176f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", 1 << (ctrl - 1));
9186f9703d0SDarrick J. Wong 	else
9196f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
9206f9703d0SDarrick J. Wong }
9216f9703d0SDarrick J. Wong 
9226f9703d0SDarrick J. Wong static int cvt_auto_temp(int input)
9236f9703d0SDarrick J. Wong {
9246f9703d0SDarrick J. Wong 	if (input == ADT7470_PWM_ALL_TEMPS)
9256f9703d0SDarrick J. Wong 		return 0;
926ce9c2f44SRobert P. J. Day 	if (input < 1 || !is_power_of_2(input))
9276f9703d0SDarrick J. Wong 		return -EINVAL;
9286f9703d0SDarrick J. Wong 	return ilog2(input) + 1;
9296f9703d0SDarrick J. Wong }
9306f9703d0SDarrick J. Wong 
93142291a5aSGuenter Roeck static ssize_t pwm_auto_temp_store(struct device *dev,
9326f9703d0SDarrick J. Wong 				   struct device_attribute *devattr,
93342291a5aSGuenter Roeck 				   const char *buf, size_t count)
9346f9703d0SDarrick J. Wong {
9356f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
93630485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
93730485776SAxel Lin 	struct i2c_client *client = data->client;
9386f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
93905a9bd46SDarrick J. Wong 	long temp;
9406f9703d0SDarrick J. Wong 	u8 reg;
9416f9703d0SDarrick J. Wong 
942179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
94305a9bd46SDarrick J. Wong 		return -EINVAL;
94405a9bd46SDarrick J. Wong 
94505a9bd46SDarrick J. Wong 	temp = cvt_auto_temp(temp);
9466f9703d0SDarrick J. Wong 	if (temp < 0)
9476f9703d0SDarrick J. Wong 		return temp;
9486f9703d0SDarrick J. Wong 
9496f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
9506f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
9516f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
9526f9703d0SDarrick J. Wong 
9536f9703d0SDarrick J. Wong 	if (!(attr->index % 2)) {
9546f9703d0SDarrick J. Wong 		reg &= 0xF;
9556f9703d0SDarrick J. Wong 		reg |= (temp << 4) & 0xF0;
9566f9703d0SDarrick J. Wong 	} else {
9576f9703d0SDarrick J. Wong 		reg &= 0xF0;
9586f9703d0SDarrick J. Wong 		reg |= temp & 0xF;
9596f9703d0SDarrick J. Wong 	}
9606f9703d0SDarrick J. Wong 
9616f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
9626f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9636f9703d0SDarrick J. Wong 
9646f9703d0SDarrick J. Wong 	return count;
9656f9703d0SDarrick J. Wong }
9666f9703d0SDarrick J. Wong 
96742291a5aSGuenter Roeck static ssize_t alarm_show(struct device *dev,
96842291a5aSGuenter Roeck 			  struct device_attribute *devattr, char *buf)
969fe03f28cSDarrick J. Wong {
970fe03f28cSDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
971fe03f28cSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
972fe03f28cSDarrick J. Wong 
973fe03f28cSDarrick J. Wong 	if (data->alarm & attr->index)
974fe03f28cSDarrick J. Wong 		return sprintf(buf, "1\n");
975fe03f28cSDarrick J. Wong 	else
976fe03f28cSDarrick J. Wong 		return sprintf(buf, "0\n");
977fe03f28cSDarrick J. Wong }
978fe03f28cSDarrick J. Wong 
979808fc6c2SJulia Lawall static DEVICE_ATTR_RW(alarm_mask);
980808fc6c2SJulia Lawall static DEVICE_ATTR_RW(num_temp_sensors);
981808fc6c2SJulia Lawall static DEVICE_ATTR_RW(auto_update_interval);
9826f9703d0SDarrick J. Wong 
98342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
98442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
98542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
98642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
98742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
98842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
98942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp7_max, temp_max, 6);
99042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp8_max, temp_max, 7);
99142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp9_max, temp_max, 8);
99242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp10_max, temp_max, 9);
9936f9703d0SDarrick J. Wong 
99442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
99542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
99642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
99742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
99842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp5_min, temp_min, 4);
99942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp6_min, temp_min, 5);
100042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp7_min, temp_min, 6);
100142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp8_min, temp_min, 7);
100242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp9_min, temp_min, 8);
100342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp10_min, temp_min, 9);
10046f9703d0SDarrick J. Wong 
100542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
100642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
100742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
100842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
100942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
101042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
101142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6);
101242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7);
101342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_input, temp, 8);
101442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_input, temp, 9);
10156f9703d0SDarrick J. Wong 
101642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, ADT7470_R1T_ALARM);
101742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, ADT7470_R2T_ALARM);
101842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, ADT7470_R3T_ALARM);
101942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, ADT7470_R4T_ALARM);
102042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, ADT7470_R5T_ALARM);
102142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, ADT7470_R6T_ALARM);
102242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_alarm, alarm, ADT7470_R7T_ALARM);
102342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_alarm, alarm, ALARM2(ADT7470_R8T_ALARM));
102442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_alarm, alarm, ALARM2(ADT7470_R9T_ALARM));
102542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_alarm, alarm, ALARM2(ADT7470_R10T_ALARM));
1026fe03f28cSDarrick J. Wong 
102742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_max, fan_max, 0);
102842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_max, fan_max, 1);
102942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan3_max, fan_max, 2);
103042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan4_max, fan_max, 3);
10316f9703d0SDarrick J. Wong 
103242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
103342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
103442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
103542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
10366f9703d0SDarrick J. Wong 
103742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
103842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
103942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
104042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
10416f9703d0SDarrick J. Wong 
104242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, ALARM2(ADT7470_FAN1_ALARM));
104342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, ALARM2(ADT7470_FAN2_ALARM));
104442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, ALARM2(ADT7470_FAN3_ALARM));
104542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, ALARM2(ADT7470_FAN4_ALARM));
1046fe03f28cSDarrick J. Wong 
104742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
10486f9703d0SDarrick J. Wong 
104942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
105042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
105142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
105242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
10536f9703d0SDarrick J. Wong 
1054808fc6c2SJulia Lawall static DEVICE_ATTR_RW(pwm1_freq);
1055aa18cc91SJoshua Scott 
105642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
105742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
105842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
105942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
10606f9703d0SDarrick J. Wong 
106142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
106242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
106342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
106442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
10656f9703d0SDarrick J. Wong 
106642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0);
106742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1);
106842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2);
106942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3);
10706f9703d0SDarrick J. Wong 
107142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0);
107242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1);
107342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2);
107442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3);
10756f9703d0SDarrick J. Wong 
107642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
107742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
107842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
107942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
10806f9703d0SDarrick J. Wong 
108142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
108242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
108342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
108442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
10856f9703d0SDarrick J. Wong 
108630485776SAxel Lin static struct attribute *adt7470_attrs[] = {
1087fe03f28cSDarrick J. Wong 	&dev_attr_alarm_mask.attr,
10882f22d5dfSDarrick J. Wong 	&dev_attr_num_temp_sensors.attr,
108989fac11cSDarrick J. Wong 	&dev_attr_auto_update_interval.attr,
10906f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_max.dev_attr.attr,
10916f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_max.dev_attr.attr,
10926f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_max.dev_attr.attr,
10936f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_max.dev_attr.attr,
10946f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_max.dev_attr.attr,
10956f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_max.dev_attr.attr,
10966f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_max.dev_attr.attr,
10976f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_max.dev_attr.attr,
10986f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_max.dev_attr.attr,
10996f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_max.dev_attr.attr,
11006f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_min.dev_attr.attr,
11016f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_min.dev_attr.attr,
11026f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_min.dev_attr.attr,
11036f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_min.dev_attr.attr,
11046f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_min.dev_attr.attr,
11056f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_min.dev_attr.attr,
11066f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_min.dev_attr.attr,
11076f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_min.dev_attr.attr,
11086f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_min.dev_attr.attr,
11096f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_min.dev_attr.attr,
11106f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_input.dev_attr.attr,
11116f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_input.dev_attr.attr,
11126f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_input.dev_attr.attr,
11136f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_input.dev_attr.attr,
11146f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_input.dev_attr.attr,
11156f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_input.dev_attr.attr,
11166f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_input.dev_attr.attr,
11176f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_input.dev_attr.attr,
11186f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_input.dev_attr.attr,
11196f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_input.dev_attr.attr,
1120fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1121fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1122fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1123fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
1124fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
1125fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
1126fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
1127fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
1128fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
1129fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
11306f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_max.dev_attr.attr,
11316f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_max.dev_attr.attr,
11326f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_max.dev_attr.attr,
11336f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_max.dev_attr.attr,
11346f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_min.dev_attr.attr,
11356f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_min.dev_attr.attr,
11366f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_min.dev_attr.attr,
11376f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_min.dev_attr.attr,
11386f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_input.dev_attr.attr,
11396f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_input.dev_attr.attr,
11406f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_input.dev_attr.attr,
11416f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1142fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1143fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1144fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1145fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
11466f9703d0SDarrick J. Wong 	&sensor_dev_attr_force_pwm_max.dev_attr.attr,
11476f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1.dev_attr.attr,
1148aa18cc91SJoshua Scott 	&dev_attr_pwm1_freq.attr,
11496f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2.dev_attr.attr,
11506f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3.dev_attr.attr,
11516f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4.dev_attr.attr,
11526f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
11536f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
11546f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
11556f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
11566f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
11576f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
11586f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
11596f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
11606f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
11616f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
11626f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
11636f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
11646f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
11656f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
11666f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
11676f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
11686f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
11696f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
11706f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
11716f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
11726f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
11736f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
11746f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
11756f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
11766f9703d0SDarrick J. Wong 	NULL
11776f9703d0SDarrick J. Wong };
11786f9703d0SDarrick J. Wong 
117930485776SAxel Lin ATTRIBUTE_GROUPS(adt7470);
118030485776SAxel Lin 
1181008f1ca5SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
1182310ec792SJean Delvare static int adt7470_detect(struct i2c_client *client,
1183008f1ca5SJean Delvare 			  struct i2c_board_info *info)
11846f9703d0SDarrick J. Wong {
1185008f1ca5SJean Delvare 	struct i2c_adapter *adapter = client->adapter;
118652df6440SJean Delvare 	int vendor, device, revision;
11876f9703d0SDarrick J. Wong 
11886f9703d0SDarrick J. Wong 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1189008f1ca5SJean Delvare 		return -ENODEV;
11906f9703d0SDarrick J. Wong 
11916f9703d0SDarrick J. Wong 	vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1192008f1ca5SJean Delvare 	if (vendor != ADT7470_VENDOR)
1193008f1ca5SJean Delvare 		return -ENODEV;
11946f9703d0SDarrick J. Wong 
11956f9703d0SDarrick J. Wong 	device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1196008f1ca5SJean Delvare 	if (device != ADT7470_DEVICE)
1197008f1ca5SJean Delvare 		return -ENODEV;
11986f9703d0SDarrick J. Wong 
119952df6440SJean Delvare 	revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1200008f1ca5SJean Delvare 	if (revision != ADT7470_REVISION)
1201008f1ca5SJean Delvare 		return -ENODEV;
12026f9703d0SDarrick J. Wong 
1203008f1ca5SJean Delvare 	strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
12046f9703d0SDarrick J. Wong 
1205008f1ca5SJean Delvare 	return 0;
1206008f1ca5SJean Delvare }
1207008f1ca5SJean Delvare 
12089027d933SAxel Lin static void adt7470_init_client(struct i2c_client *client)
12099027d933SAxel Lin {
12109027d933SAxel Lin 	int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
12119027d933SAxel Lin 
12129027d933SAxel Lin 	if (reg < 0) {
12139027d933SAxel Lin 		dev_err(&client->dev, "cannot read configuration register\n");
12149027d933SAxel Lin 	} else {
12159027d933SAxel Lin 		/* start monitoring (and do a self-test) */
12169027d933SAxel Lin 		i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
12179027d933SAxel Lin 	}
12189027d933SAxel Lin }
12199027d933SAxel Lin 
1220*67487038SStephen Kitt static int adt7470_probe(struct i2c_client *client)
1221008f1ca5SJean Delvare {
122230485776SAxel Lin 	struct device *dev = &client->dev;
1223008f1ca5SJean Delvare 	struct adt7470_data *data;
122430485776SAxel Lin 	struct device *hwmon_dev;
1225008f1ca5SJean Delvare 
122630485776SAxel Lin 	data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
12279cc7dcc5SGuenter Roeck 	if (!data)
12289cc7dcc5SGuenter Roeck 		return -ENOMEM;
1229008f1ca5SJean Delvare 
12302f22d5dfSDarrick J. Wong 	data->num_temp_sensors = -1;
123189fac11cSDarrick J. Wong 	data->auto_update_interval = AUTO_UPDATE_INTERVAL;
12322f22d5dfSDarrick J. Wong 
1233008f1ca5SJean Delvare 	i2c_set_clientdata(client, data);
123430485776SAxel Lin 	data->client = client;
1235008f1ca5SJean Delvare 	mutex_init(&data->lock);
12366f9703d0SDarrick J. Wong 
12376f9703d0SDarrick J. Wong 	dev_info(&client->dev, "%s chip found\n", client->name);
12386f9703d0SDarrick J. Wong 
12396f9703d0SDarrick J. Wong 	/* Initialize the ADT7470 chip */
12406f9703d0SDarrick J. Wong 	adt7470_init_client(client);
12416f9703d0SDarrick J. Wong 
12426f9703d0SDarrick J. Wong 	/* Register sysfs hooks */
124330485776SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
124430485776SAxel Lin 							   data,
124530485776SAxel Lin 							   adt7470_groups);
12466f9703d0SDarrick J. Wong 
124730485776SAxel Lin 	if (IS_ERR(hwmon_dev))
124830485776SAxel Lin 		return PTR_ERR(hwmon_dev);
12496f9703d0SDarrick J. Wong 
1250f170168bSKees Cook 	data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
125130485776SAxel Lin 					dev_name(hwmon_dev));
1252f7334b4cSAxel Lin 	if (IS_ERR(data->auto_update)) {
125330485776SAxel Lin 		return PTR_ERR(data->auto_update);
1254f7334b4cSAxel Lin 	}
125589fac11cSDarrick J. Wong 
12566f9703d0SDarrick J. Wong 	return 0;
12576f9703d0SDarrick J. Wong }
12586f9703d0SDarrick J. Wong 
1259008f1ca5SJean Delvare static int adt7470_remove(struct i2c_client *client)
12606f9703d0SDarrick J. Wong {
12616f9703d0SDarrick J. Wong 	struct adt7470_data *data = i2c_get_clientdata(client);
12626f9703d0SDarrick J. Wong 
126389fac11cSDarrick J. Wong 	kthread_stop(data->auto_update);
12646f9703d0SDarrick J. Wong 	return 0;
12656f9703d0SDarrick J. Wong }
12666f9703d0SDarrick J. Wong 
12679027d933SAxel Lin static const struct i2c_device_id adt7470_id[] = {
12689027d933SAxel Lin 	{ "adt7470", 0 },
12699027d933SAxel Lin 	{ }
12709027d933SAxel Lin };
12719027d933SAxel Lin MODULE_DEVICE_TABLE(i2c, adt7470_id);
12729027d933SAxel Lin 
12739027d933SAxel Lin static struct i2c_driver adt7470_driver = {
12749027d933SAxel Lin 	.class		= I2C_CLASS_HWMON,
12759027d933SAxel Lin 	.driver = {
12769027d933SAxel Lin 		.name	= "adt7470",
12779027d933SAxel Lin 	},
1278*67487038SStephen Kitt 	.probe_new	= adt7470_probe,
12799027d933SAxel Lin 	.remove		= adt7470_remove,
12809027d933SAxel Lin 	.id_table	= adt7470_id,
12819027d933SAxel Lin 	.detect		= adt7470_detect,
12829027d933SAxel Lin 	.address_list	= normal_i2c,
12839027d933SAxel Lin };
12849027d933SAxel Lin 
1285f0967eeaSAxel Lin module_i2c_driver(adt7470_driver);
12866f9703d0SDarrick J. Wong 
12875407e051SDarrick J. Wong MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
12886f9703d0SDarrick J. Wong MODULE_DESCRIPTION("ADT7470 driver");
12896f9703d0SDarrick J. Wong MODULE_LICENSE("GPL");
1290