xref: /linux/drivers/hwmon/adt7470.c (revision ad00a02e34b481396938c5fa62ee642bff7fbb08)
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 
273*ad00a02eSChris Packham static int adt7470_update_sensors(struct adt7470_data *data)
27489fac11cSDarrick J. Wong {
27530485776SAxel Lin 	struct i2c_client *client = data->client;
27689fac11cSDarrick J. Wong 	u8 cfg;
27789fac11cSDarrick J. Wong 	int i;
27889fac11cSDarrick J. Wong 
27989fac11cSDarrick J. Wong 	if (!data->temperatures_probed)
28089fac11cSDarrick J. Wong 		adt7470_read_temperatures(client, data);
28189fac11cSDarrick J. Wong 	else
2826f9703d0SDarrick J. Wong 		for (i = 0; i < ADT7470_TEMP_COUNT; i++)
2836f9703d0SDarrick J. Wong 			data->temp[i] = i2c_smbus_read_byte_data(client,
2846f9703d0SDarrick J. Wong 						ADT7470_TEMP_REG(i));
2856f9703d0SDarrick J. Wong 
2866f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
2876f9703d0SDarrick J. Wong 		data->fan[i] = adt7470_read_word_data(client,
2886f9703d0SDarrick J. Wong 						ADT7470_REG_FAN(i));
2896f9703d0SDarrick J. Wong 
2906f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
2916f9703d0SDarrick J. Wong 		int reg;
2926f9703d0SDarrick J. Wong 		int reg_mask;
2936f9703d0SDarrick J. Wong 
2946f9703d0SDarrick J. Wong 		data->pwm[i] = i2c_smbus_read_byte_data(client,
2956f9703d0SDarrick J. Wong 						ADT7470_REG_PWM(i));
2966f9703d0SDarrick J. Wong 
2976f9703d0SDarrick J. Wong 		if (i % 2)
2986f9703d0SDarrick J. Wong 			reg_mask = ADT7470_PWM2_AUTO_MASK;
2996f9703d0SDarrick J. Wong 		else
3006f9703d0SDarrick J. Wong 			reg_mask = ADT7470_PWM1_AUTO_MASK;
3016f9703d0SDarrick J. Wong 
3026f9703d0SDarrick J. Wong 		reg = ADT7470_REG_PWM_CFG(i);
3036f9703d0SDarrick J. Wong 		if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
3046f9703d0SDarrick J. Wong 			data->pwm_automatic[i] = 1;
3056f9703d0SDarrick J. Wong 		else
3066f9703d0SDarrick J. Wong 			data->pwm_automatic[i] = 0;
3076f9703d0SDarrick J. Wong 
3086f9703d0SDarrick J. Wong 		reg = ADT7470_REG_PWM_AUTO_TEMP(i);
3096f9703d0SDarrick J. Wong 		cfg = i2c_smbus_read_byte_data(client, reg);
3106f9703d0SDarrick J. Wong 		if (!(i % 2))
3116f9703d0SDarrick J. Wong 			data->pwm_auto_temp[i] = cfg >> 4;
3126f9703d0SDarrick J. Wong 		else
3136f9703d0SDarrick J. Wong 			data->pwm_auto_temp[i] = cfg & 0xF;
3146f9703d0SDarrick J. Wong 	}
3156f9703d0SDarrick J. Wong 
3166f9703d0SDarrick J. Wong 	if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
3176f9703d0SDarrick J. Wong 	    ADT7470_FSPD_MASK)
3186f9703d0SDarrick J. Wong 		data->force_pwm_max = 1;
3196f9703d0SDarrick J. Wong 	else
3206f9703d0SDarrick J. Wong 		data->force_pwm_max = 0;
3216f9703d0SDarrick J. Wong 
322fe03f28cSDarrick J. Wong 	data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
323fe03f28cSDarrick J. Wong 	if (data->alarm & ADT7470_OOL_ALARM)
324fe03f28cSDarrick J. Wong 		data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
325fe03f28cSDarrick J. Wong 							ADT7470_REG_ALARM2));
3266f9703d0SDarrick J. Wong 	data->alarms_mask = adt7470_read_word_data(client,
3276f9703d0SDarrick J. Wong 						   ADT7470_REG_ALARM1_MASK);
3286f9703d0SDarrick J. Wong 
329*ad00a02eSChris Packham 	return 0;
330*ad00a02eSChris Packham }
3316f9703d0SDarrick J. Wong 
332*ad00a02eSChris Packham static int adt7470_update_limits(struct adt7470_data *data)
333*ad00a02eSChris Packham {
334*ad00a02eSChris Packham 	struct i2c_client *client = data->client;
335*ad00a02eSChris Packham 	int i;
3366f9703d0SDarrick J. Wong 
3376f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
3386f9703d0SDarrick J. Wong 		data->temp_min[i] = i2c_smbus_read_byte_data(client,
3396f9703d0SDarrick J. Wong 						ADT7470_TEMP_MIN_REG(i));
3406f9703d0SDarrick J. Wong 		data->temp_max[i] = i2c_smbus_read_byte_data(client,
3416f9703d0SDarrick J. Wong 						ADT7470_TEMP_MAX_REG(i));
3426f9703d0SDarrick J. Wong 	}
3436f9703d0SDarrick J. Wong 
3446f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++) {
3456f9703d0SDarrick J. Wong 		data->fan_min[i] = adt7470_read_word_data(client,
3466f9703d0SDarrick J. Wong 						ADT7470_REG_FAN_MIN(i));
3476f9703d0SDarrick J. Wong 		data->fan_max[i] = adt7470_read_word_data(client,
3486f9703d0SDarrick J. Wong 						ADT7470_REG_FAN_MAX(i));
3496f9703d0SDarrick J. Wong 	}
3506f9703d0SDarrick J. Wong 
3516f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
3526f9703d0SDarrick J. Wong 		data->pwm_max[i] = i2c_smbus_read_byte_data(client,
3536f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_MAX(i));
3546f9703d0SDarrick J. Wong 		data->pwm_min[i] = i2c_smbus_read_byte_data(client,
3556f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_MIN(i));
3566f9703d0SDarrick J. Wong 		data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
3576f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_TMIN(i));
3586f9703d0SDarrick J. Wong 	}
3596f9703d0SDarrick J. Wong 
360*ad00a02eSChris Packham 	return 0;
361*ad00a02eSChris Packham }
362*ad00a02eSChris Packham 
363*ad00a02eSChris Packham static struct adt7470_data *adt7470_update_device(struct device *dev)
364*ad00a02eSChris Packham {
365*ad00a02eSChris Packham 	struct adt7470_data *data = dev_get_drvdata(dev);
366*ad00a02eSChris Packham 	unsigned long local_jiffies = jiffies;
367*ad00a02eSChris Packham 	int need_sensors = 1;
368*ad00a02eSChris Packham 	int need_limits = 1;
369*ad00a02eSChris Packham 	int err;
370*ad00a02eSChris Packham 
371*ad00a02eSChris Packham 	/*
372*ad00a02eSChris Packham 	 * Figure out if we need to update the shadow registers.
373*ad00a02eSChris Packham 	 * Lockless means that we may occasionally report out of
374*ad00a02eSChris Packham 	 * date data.
375*ad00a02eSChris Packham 	 */
376*ad00a02eSChris Packham 	if (time_before(local_jiffies, data->sensors_last_updated +
377*ad00a02eSChris Packham 			SENSOR_REFRESH_INTERVAL) &&
378*ad00a02eSChris Packham 	    data->sensors_valid)
379*ad00a02eSChris Packham 		need_sensors = 0;
380*ad00a02eSChris Packham 
381*ad00a02eSChris Packham 	if (time_before(local_jiffies, data->limits_last_updated +
382*ad00a02eSChris Packham 			LIMIT_REFRESH_INTERVAL) &&
383*ad00a02eSChris Packham 	    data->limits_valid)
384*ad00a02eSChris Packham 		need_limits = 0;
385*ad00a02eSChris Packham 
386*ad00a02eSChris Packham 	if (!need_sensors && !need_limits)
387*ad00a02eSChris Packham 		return data;
388*ad00a02eSChris Packham 
389*ad00a02eSChris Packham 	mutex_lock(&data->lock);
390*ad00a02eSChris Packham 	if (need_sensors) {
391*ad00a02eSChris Packham 		err = adt7470_update_sensors(data);
392*ad00a02eSChris Packham 		if (err < 0)
393*ad00a02eSChris Packham 			goto out;
394*ad00a02eSChris Packham 		data->sensors_last_updated = local_jiffies;
395*ad00a02eSChris Packham 		data->sensors_valid = 1;
396*ad00a02eSChris Packham 	}
397*ad00a02eSChris Packham 
398*ad00a02eSChris Packham 	if (need_limits) {
399*ad00a02eSChris Packham 		err = adt7470_update_limits(data);
400*ad00a02eSChris Packham 		if (err < 0)
401*ad00a02eSChris Packham 			goto out;
4026f9703d0SDarrick J. Wong 		data->limits_last_updated = local_jiffies;
4036f9703d0SDarrick J. Wong 		data->limits_valid = 1;
404*ad00a02eSChris Packham 	}
4056f9703d0SDarrick J. Wong out:
4066f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
407*ad00a02eSChris Packham 
408*ad00a02eSChris Packham 	return err < 0 ? ERR_PTR(err) : data;
4096f9703d0SDarrick J. Wong }
4106f9703d0SDarrick J. Wong 
411808fc6c2SJulia Lawall static ssize_t auto_update_interval_show(struct device *dev,
41289fac11cSDarrick J. Wong 					 struct device_attribute *devattr,
41389fac11cSDarrick J. Wong 					 char *buf)
41489fac11cSDarrick J. Wong {
41589fac11cSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
416*ad00a02eSChris Packham 
417*ad00a02eSChris Packham 	if (IS_ERR(data))
418*ad00a02eSChris Packham 		return PTR_ERR(data);
419*ad00a02eSChris Packham 
42089fac11cSDarrick J. Wong 	return sprintf(buf, "%d\n", data->auto_update_interval);
42189fac11cSDarrick J. Wong }
42289fac11cSDarrick J. Wong 
423808fc6c2SJulia Lawall static ssize_t auto_update_interval_store(struct device *dev,
42489fac11cSDarrick J. Wong 					  struct device_attribute *devattr,
425808fc6c2SJulia Lawall 					  const char *buf, size_t count)
42689fac11cSDarrick J. Wong {
42730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
42889fac11cSDarrick J. Wong 	long temp;
42989fac11cSDarrick J. Wong 
430179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
43189fac11cSDarrick J. Wong 		return -EINVAL;
43289fac11cSDarrick J. Wong 
4332a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 60000);
43489fac11cSDarrick J. Wong 
43589fac11cSDarrick J. Wong 	mutex_lock(&data->lock);
43689fac11cSDarrick J. Wong 	data->auto_update_interval = temp;
43789fac11cSDarrick J. Wong 	mutex_unlock(&data->lock);
43889fac11cSDarrick J. Wong 
43989fac11cSDarrick J. Wong 	return count;
44089fac11cSDarrick J. Wong }
44189fac11cSDarrick J. Wong 
442808fc6c2SJulia Lawall static ssize_t num_temp_sensors_show(struct device *dev,
4432f22d5dfSDarrick J. Wong 				     struct device_attribute *devattr,
4442f22d5dfSDarrick J. Wong 				     char *buf)
4452f22d5dfSDarrick J. Wong {
4462f22d5dfSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
447*ad00a02eSChris Packham 
448*ad00a02eSChris Packham 	if (IS_ERR(data))
449*ad00a02eSChris Packham 		return PTR_ERR(data);
450*ad00a02eSChris Packham 
4512f22d5dfSDarrick J. Wong 	return sprintf(buf, "%d\n", data->num_temp_sensors);
4522f22d5dfSDarrick J. Wong }
4532f22d5dfSDarrick J. Wong 
454808fc6c2SJulia Lawall static ssize_t num_temp_sensors_store(struct device *dev,
4552f22d5dfSDarrick J. Wong 				      struct device_attribute *devattr,
456808fc6c2SJulia Lawall 				      const char *buf, size_t count)
4572f22d5dfSDarrick J. Wong {
45830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
4592f22d5dfSDarrick J. Wong 	long temp;
4602f22d5dfSDarrick J. Wong 
461179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
4622f22d5dfSDarrick J. Wong 		return -EINVAL;
4632f22d5dfSDarrick J. Wong 
4642a844c14SGuenter Roeck 	temp = clamp_val(temp, -1, 10);
4652f22d5dfSDarrick J. Wong 
4662f22d5dfSDarrick J. Wong 	mutex_lock(&data->lock);
4672f22d5dfSDarrick J. Wong 	data->num_temp_sensors = temp;
46889fac11cSDarrick J. Wong 	if (temp < 0)
46989fac11cSDarrick J. Wong 		data->temperatures_probed = 0;
4702f22d5dfSDarrick J. Wong 	mutex_unlock(&data->lock);
4712f22d5dfSDarrick J. Wong 
4722f22d5dfSDarrick J. Wong 	return count;
4732f22d5dfSDarrick J. Wong }
4742f22d5dfSDarrick J. Wong 
47542291a5aSGuenter Roeck static ssize_t temp_min_show(struct device *dev,
47642291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
4776f9703d0SDarrick J. Wong {
4786f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
4796f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
480*ad00a02eSChris Packham 
481*ad00a02eSChris Packham 	if (IS_ERR(data))
482*ad00a02eSChris Packham 		return PTR_ERR(data);
483*ad00a02eSChris Packham 
4846f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
4856f9703d0SDarrick J. Wong }
4866f9703d0SDarrick J. Wong 
48742291a5aSGuenter Roeck static ssize_t temp_min_store(struct device *dev,
4886f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
48942291a5aSGuenter Roeck 			      const char *buf, size_t count)
4906f9703d0SDarrick J. Wong {
4916f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
49230485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
49330485776SAxel Lin 	struct i2c_client *client = data->client;
49405a9bd46SDarrick J. Wong 	long temp;
49505a9bd46SDarrick J. Wong 
496179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
49705a9bd46SDarrick J. Wong 		return -EINVAL;
49805a9bd46SDarrick J. Wong 
49964bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
5008f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
5016f9703d0SDarrick J. Wong 
5026f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5036f9703d0SDarrick J. Wong 	data->temp_min[attr->index] = temp;
5046f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
5056f9703d0SDarrick J. Wong 				  temp);
5066f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
5076f9703d0SDarrick J. Wong 
5086f9703d0SDarrick J. Wong 	return count;
5096f9703d0SDarrick J. Wong }
5106f9703d0SDarrick J. Wong 
51142291a5aSGuenter Roeck static ssize_t temp_max_show(struct device *dev,
51242291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
5136f9703d0SDarrick J. Wong {
5146f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5156f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
516*ad00a02eSChris Packham 
517*ad00a02eSChris Packham 	if (IS_ERR(data))
518*ad00a02eSChris Packham 		return PTR_ERR(data);
519*ad00a02eSChris Packham 
5206f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
5216f9703d0SDarrick J. Wong }
5226f9703d0SDarrick J. Wong 
52342291a5aSGuenter Roeck static ssize_t temp_max_store(struct device *dev,
5246f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
52542291a5aSGuenter Roeck 			      const char *buf, size_t count)
5266f9703d0SDarrick J. Wong {
5276f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
52830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
52930485776SAxel Lin 	struct i2c_client *client = data->client;
53005a9bd46SDarrick J. Wong 	long temp;
53105a9bd46SDarrick J. Wong 
532179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
53305a9bd46SDarrick J. Wong 		return -EINVAL;
53405a9bd46SDarrick J. Wong 
53564bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
5368f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
5376f9703d0SDarrick J. Wong 
5386f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5396f9703d0SDarrick J. Wong 	data->temp_max[attr->index] = temp;
5406f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
5416f9703d0SDarrick J. Wong 				  temp);
5426f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
5436f9703d0SDarrick J. Wong 
5446f9703d0SDarrick J. Wong 	return count;
5456f9703d0SDarrick J. Wong }
5466f9703d0SDarrick J. Wong 
54742291a5aSGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
5486f9703d0SDarrick J. Wong 			 char *buf)
5496f9703d0SDarrick J. Wong {
5506f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5516f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
552*ad00a02eSChris Packham 
553*ad00a02eSChris Packham 	if (IS_ERR(data))
554*ad00a02eSChris Packham 		return PTR_ERR(data);
555*ad00a02eSChris Packham 
5566f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
5576f9703d0SDarrick J. Wong }
5586f9703d0SDarrick J. Wong 
559808fc6c2SJulia Lawall static ssize_t alarm_mask_show(struct device *dev,
5606f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
5616f9703d0SDarrick J. Wong 			   char *buf)
5626f9703d0SDarrick J. Wong {
5636f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5646f9703d0SDarrick J. Wong 
565*ad00a02eSChris Packham 	if (IS_ERR(data))
566*ad00a02eSChris Packham 		return PTR_ERR(data);
567*ad00a02eSChris Packham 
5686f9703d0SDarrick J. Wong 	return sprintf(buf, "%x\n", data->alarms_mask);
5696f9703d0SDarrick J. Wong }
5706f9703d0SDarrick J. Wong 
571808fc6c2SJulia Lawall static ssize_t alarm_mask_store(struct device *dev,
572feca3132SJoshua Scott 				struct device_attribute *devattr,
573808fc6c2SJulia Lawall 				const char *buf, size_t count)
574feca3132SJoshua Scott {
575feca3132SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
576feca3132SJoshua Scott 	long mask;
577feca3132SJoshua Scott 
578feca3132SJoshua Scott 	if (kstrtoul(buf, 0, &mask))
579feca3132SJoshua Scott 		return -EINVAL;
580feca3132SJoshua Scott 
581feca3132SJoshua Scott 	if (mask & ~0xffff)
582feca3132SJoshua Scott 		return -EINVAL;
583feca3132SJoshua Scott 
584feca3132SJoshua Scott 	mutex_lock(&data->lock);
585feca3132SJoshua Scott 	data->alarms_mask = mask;
586feca3132SJoshua Scott 	adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
587feca3132SJoshua Scott 	mutex_unlock(&data->lock);
588feca3132SJoshua Scott 
589feca3132SJoshua Scott 	return count;
590feca3132SJoshua Scott }
591feca3132SJoshua Scott 
59242291a5aSGuenter Roeck static ssize_t fan_max_show(struct device *dev,
59342291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
5946f9703d0SDarrick J. Wong {
5956f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5966f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5976f9703d0SDarrick J. Wong 
598*ad00a02eSChris Packham 	if (IS_ERR(data))
599*ad00a02eSChris Packham 		return PTR_ERR(data);
600*ad00a02eSChris Packham 
6016f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_max[attr->index]))
6026f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6036f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
6046f9703d0SDarrick J. Wong 	else
6056f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6066f9703d0SDarrick J. Wong }
6076f9703d0SDarrick J. Wong 
60842291a5aSGuenter Roeck static ssize_t fan_max_store(struct device *dev,
6096f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
6106f9703d0SDarrick J. Wong 			     const char *buf, size_t count)
6116f9703d0SDarrick J. Wong {
6126f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
61330485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
61430485776SAxel Lin 	struct i2c_client *client = data->client;
61505a9bd46SDarrick J. Wong 	long temp;
6166f9703d0SDarrick J. Wong 
617179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
6186f9703d0SDarrick J. Wong 		return -EINVAL;
61905a9bd46SDarrick J. Wong 
6206f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
6212a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
6226f9703d0SDarrick J. Wong 
6236f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6246f9703d0SDarrick J. Wong 	data->fan_max[attr->index] = temp;
6256f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
6266f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6276f9703d0SDarrick J. Wong 
6286f9703d0SDarrick J. Wong 	return count;
6296f9703d0SDarrick J. Wong }
6306f9703d0SDarrick J. Wong 
63142291a5aSGuenter Roeck static ssize_t fan_min_show(struct device *dev,
63242291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
6336f9703d0SDarrick J. Wong {
6346f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6356f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6366f9703d0SDarrick J. Wong 
637*ad00a02eSChris Packham 	if (IS_ERR(data))
638*ad00a02eSChris Packham 		return PTR_ERR(data);
639*ad00a02eSChris Packham 
6406f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_min[attr->index]))
6416f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6426f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
6436f9703d0SDarrick J. Wong 	else
6446f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6456f9703d0SDarrick J. Wong }
6466f9703d0SDarrick J. Wong 
64742291a5aSGuenter Roeck static ssize_t fan_min_store(struct device *dev,
6486f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
6496f9703d0SDarrick J. Wong 			     const char *buf, size_t count)
6506f9703d0SDarrick J. Wong {
6516f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
65230485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
65330485776SAxel Lin 	struct i2c_client *client = data->client;
65405a9bd46SDarrick J. Wong 	long temp;
6556f9703d0SDarrick J. Wong 
656179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
6576f9703d0SDarrick J. Wong 		return -EINVAL;
65805a9bd46SDarrick J. Wong 
6596f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
6602a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
6616f9703d0SDarrick J. Wong 
6626f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6636f9703d0SDarrick J. Wong 	data->fan_min[attr->index] = temp;
6646f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
6656f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6666f9703d0SDarrick J. Wong 
6676f9703d0SDarrick J. Wong 	return count;
6686f9703d0SDarrick J. Wong }
6696f9703d0SDarrick J. Wong 
67042291a5aSGuenter Roeck static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
6716f9703d0SDarrick J. Wong 			char *buf)
6726f9703d0SDarrick J. Wong {
6736f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6746f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6756f9703d0SDarrick J. Wong 
676*ad00a02eSChris Packham 	if (IS_ERR(data))
677*ad00a02eSChris Packham 		return PTR_ERR(data);
678*ad00a02eSChris Packham 
6796f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan[attr->index]))
6806f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6816f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
6826f9703d0SDarrick J. Wong 	else
6836f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6846f9703d0SDarrick J. Wong }
6856f9703d0SDarrick J. Wong 
68642291a5aSGuenter Roeck static ssize_t force_pwm_max_show(struct device *dev,
68742291a5aSGuenter Roeck 				  struct device_attribute *devattr, char *buf)
6886f9703d0SDarrick J. Wong {
6896f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
690*ad00a02eSChris Packham 
691*ad00a02eSChris Packham 	if (IS_ERR(data))
692*ad00a02eSChris Packham 		return PTR_ERR(data);
693*ad00a02eSChris Packham 
6946f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->force_pwm_max);
6956f9703d0SDarrick J. Wong }
6966f9703d0SDarrick J. Wong 
69742291a5aSGuenter Roeck static ssize_t force_pwm_max_store(struct device *dev,
6986f9703d0SDarrick J. Wong 				   struct device_attribute *devattr,
69942291a5aSGuenter Roeck 				   const char *buf, size_t count)
7006f9703d0SDarrick J. Wong {
70130485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
70230485776SAxel Lin 	struct i2c_client *client = data->client;
70305a9bd46SDarrick J. Wong 	long temp;
7046f9703d0SDarrick J. Wong 	u8 reg;
7056f9703d0SDarrick J. Wong 
706179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
70705a9bd46SDarrick J. Wong 		return -EINVAL;
70805a9bd46SDarrick J. Wong 
7096f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
7106f9703d0SDarrick J. Wong 	data->force_pwm_max = temp;
7116f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
7126f9703d0SDarrick J. Wong 	if (temp)
7136f9703d0SDarrick J. Wong 		reg |= ADT7470_FSPD_MASK;
7146f9703d0SDarrick J. Wong 	else
7156f9703d0SDarrick J. Wong 		reg &= ~ADT7470_FSPD_MASK;
7166f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
7176f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
7186f9703d0SDarrick J. Wong 
7196f9703d0SDarrick J. Wong 	return count;
7206f9703d0SDarrick J. Wong }
7216f9703d0SDarrick J. Wong 
72242291a5aSGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
7236f9703d0SDarrick J. Wong 			char *buf)
7246f9703d0SDarrick J. Wong {
7256f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
7266f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
727*ad00a02eSChris Packham 
728*ad00a02eSChris Packham 	if (IS_ERR(data))
729*ad00a02eSChris Packham 		return PTR_ERR(data);
730*ad00a02eSChris Packham 
7316f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm[attr->index]);
7326f9703d0SDarrick J. Wong }
7336f9703d0SDarrick J. Wong 
73442291a5aSGuenter Roeck static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
7356f9703d0SDarrick J. Wong 			 const char *buf, size_t count)
7366f9703d0SDarrick J. Wong {
7376f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
73830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
73930485776SAxel Lin 	struct i2c_client *client = data->client;
74005a9bd46SDarrick J. Wong 	long temp;
74105a9bd46SDarrick J. Wong 
742179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
74305a9bd46SDarrick J. Wong 		return -EINVAL;
74405a9bd46SDarrick J. Wong 
7452a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
7466f9703d0SDarrick J. Wong 
7476f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
7486f9703d0SDarrick J. Wong 	data->pwm[attr->index] = temp;
7496f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
7506f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
7516f9703d0SDarrick J. Wong 
7526f9703d0SDarrick J. Wong 	return count;
7536f9703d0SDarrick J. Wong }
7546f9703d0SDarrick J. Wong 
755aa18cc91SJoshua Scott /* These are the valid PWM frequencies to the nearest Hz */
756aa18cc91SJoshua Scott static const int adt7470_freq_map[] = {
757aa18cc91SJoshua Scott 	11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
758aa18cc91SJoshua Scott };
759aa18cc91SJoshua Scott 
760808fc6c2SJulia Lawall static ssize_t pwm1_freq_show(struct device *dev,
761aa18cc91SJoshua Scott 			      struct device_attribute *devattr, char *buf)
762aa18cc91SJoshua Scott {
763aa18cc91SJoshua Scott 	struct adt7470_data *data = adt7470_update_device(dev);
764aa18cc91SJoshua Scott 	unsigned char cfg_reg_1;
765aa18cc91SJoshua Scott 	unsigned char cfg_reg_2;
766aa18cc91SJoshua Scott 	int index;
767aa18cc91SJoshua Scott 
768aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
769aa18cc91SJoshua Scott 	cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
770aa18cc91SJoshua Scott 	cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
771aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
772aa18cc91SJoshua Scott 
773aa18cc91SJoshua Scott 	index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
774aa18cc91SJoshua Scott 	if (!(cfg_reg_1 & ADT7470_CFG_LF))
775aa18cc91SJoshua Scott 		index += 8;
776aa18cc91SJoshua Scott 	if (index >= ARRAY_SIZE(adt7470_freq_map))
777aa18cc91SJoshua Scott 		index = ARRAY_SIZE(adt7470_freq_map) - 1;
778aa18cc91SJoshua Scott 
779aa18cc91SJoshua Scott 	return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
780aa18cc91SJoshua Scott }
781aa18cc91SJoshua Scott 
782808fc6c2SJulia Lawall static ssize_t pwm1_freq_store(struct device *dev,
783aa18cc91SJoshua Scott 			       struct device_attribute *devattr,
784aa18cc91SJoshua Scott 			       const char *buf, size_t count)
785aa18cc91SJoshua Scott {
786aa18cc91SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
787aa18cc91SJoshua Scott 	struct i2c_client *client = data->client;
788aa18cc91SJoshua Scott 	long freq;
789aa18cc91SJoshua Scott 	int index;
790aa18cc91SJoshua Scott 	int low_freq = ADT7470_CFG_LF;
791aa18cc91SJoshua Scott 	unsigned char val;
792aa18cc91SJoshua Scott 
793aa18cc91SJoshua Scott 	if (kstrtol(buf, 10, &freq))
794aa18cc91SJoshua Scott 		return -EINVAL;
795aa18cc91SJoshua Scott 
796aa18cc91SJoshua Scott 	/* Round the user value given to the closest available frequency */
797aa18cc91SJoshua Scott 	index = find_closest(freq, adt7470_freq_map,
798aa18cc91SJoshua Scott 			     ARRAY_SIZE(adt7470_freq_map));
799aa18cc91SJoshua Scott 
800aa18cc91SJoshua Scott 	if (index >= 8) {
801aa18cc91SJoshua Scott 		index -= 8;
802aa18cc91SJoshua Scott 		low_freq = 0;
803aa18cc91SJoshua Scott 	}
804aa18cc91SJoshua Scott 
805aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
806aa18cc91SJoshua Scott 	/* Configuration Register 1 */
807aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
808aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
809aa18cc91SJoshua Scott 				  (val & ~ADT7470_CFG_LF) | low_freq);
810aa18cc91SJoshua Scott 	/* Configuration Register 2 */
811aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
812aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
813aa18cc91SJoshua Scott 		(val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
814aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
815aa18cc91SJoshua Scott 
816aa18cc91SJoshua Scott 	return count;
817aa18cc91SJoshua Scott }
818aa18cc91SJoshua Scott 
81942291a5aSGuenter Roeck static ssize_t pwm_max_show(struct device *dev,
82042291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
8216f9703d0SDarrick J. Wong {
8226f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8236f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
824*ad00a02eSChris Packham 
825*ad00a02eSChris Packham 	if (IS_ERR(data))
826*ad00a02eSChris Packham 		return PTR_ERR(data);
827*ad00a02eSChris Packham 
8286f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
8296f9703d0SDarrick J. Wong }
8306f9703d0SDarrick J. Wong 
83142291a5aSGuenter Roeck static ssize_t pwm_max_store(struct device *dev,
8326f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
83342291a5aSGuenter Roeck 			     const char *buf, size_t count)
8346f9703d0SDarrick J. Wong {
8356f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
83630485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
83730485776SAxel Lin 	struct i2c_client *client = data->client;
83805a9bd46SDarrick J. Wong 	long temp;
83905a9bd46SDarrick J. Wong 
840179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
84105a9bd46SDarrick J. Wong 		return -EINVAL;
84205a9bd46SDarrick J. Wong 
8432a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
8446f9703d0SDarrick J. Wong 
8456f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8466f9703d0SDarrick J. Wong 	data->pwm_max[attr->index] = temp;
8476f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
8486f9703d0SDarrick J. Wong 				  temp);
8496f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8506f9703d0SDarrick J. Wong 
8516f9703d0SDarrick J. Wong 	return count;
8526f9703d0SDarrick J. Wong }
8536f9703d0SDarrick J. Wong 
85442291a5aSGuenter Roeck static ssize_t pwm_min_show(struct device *dev,
85542291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
8566f9703d0SDarrick J. Wong {
8576f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8586f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
859*ad00a02eSChris Packham 
860*ad00a02eSChris Packham 	if (IS_ERR(data))
861*ad00a02eSChris Packham 		return PTR_ERR(data);
862*ad00a02eSChris Packham 
8636f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
8646f9703d0SDarrick J. Wong }
8656f9703d0SDarrick J. Wong 
86642291a5aSGuenter Roeck static ssize_t pwm_min_store(struct device *dev,
8676f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
86842291a5aSGuenter Roeck 			     const char *buf, size_t count)
8696f9703d0SDarrick J. Wong {
8706f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
87130485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
87230485776SAxel Lin 	struct i2c_client *client = data->client;
87305a9bd46SDarrick J. Wong 	long temp;
87405a9bd46SDarrick J. Wong 
875179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
87605a9bd46SDarrick J. Wong 		return -EINVAL;
87705a9bd46SDarrick J. Wong 
8782a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
8796f9703d0SDarrick J. Wong 
8806f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8816f9703d0SDarrick J. Wong 	data->pwm_min[attr->index] = temp;
8826f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
8836f9703d0SDarrick J. Wong 				  temp);
8846f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8856f9703d0SDarrick J. Wong 
8866f9703d0SDarrick J. Wong 	return count;
8876f9703d0SDarrick J. Wong }
8886f9703d0SDarrick J. Wong 
88942291a5aSGuenter Roeck static ssize_t pwm_tmax_show(struct device *dev,
89042291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
8916f9703d0SDarrick J. Wong {
8926f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8936f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
894*ad00a02eSChris Packham 
895*ad00a02eSChris Packham 	if (IS_ERR(data))
896*ad00a02eSChris Packham 		return PTR_ERR(data);
897*ad00a02eSChris Packham 
8986f9703d0SDarrick J. Wong 	/* the datasheet says that tmax = tmin + 20C */
8996f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
9006f9703d0SDarrick J. Wong }
9016f9703d0SDarrick J. Wong 
90242291a5aSGuenter Roeck static ssize_t pwm_tmin_show(struct device *dev,
90342291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
9046f9703d0SDarrick J. Wong {
9056f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9066f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
907*ad00a02eSChris Packham 
908*ad00a02eSChris Packham 	if (IS_ERR(data))
909*ad00a02eSChris Packham 		return PTR_ERR(data);
910*ad00a02eSChris Packham 
9116f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
9126f9703d0SDarrick J. Wong }
9136f9703d0SDarrick J. Wong 
91442291a5aSGuenter Roeck static ssize_t pwm_tmin_store(struct device *dev,
9156f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
91642291a5aSGuenter Roeck 			      const char *buf, size_t count)
9176f9703d0SDarrick J. Wong {
9186f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
91930485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
92030485776SAxel Lin 	struct i2c_client *client = data->client;
92105a9bd46SDarrick J. Wong 	long temp;
92205a9bd46SDarrick J. Wong 
923179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
92405a9bd46SDarrick J. Wong 		return -EINVAL;
92505a9bd46SDarrick J. Wong 
92664bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
9278f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
9286f9703d0SDarrick J. Wong 
9296f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
9306f9703d0SDarrick J. Wong 	data->pwm_tmin[attr->index] = temp;
9316f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
9326f9703d0SDarrick J. Wong 				  temp);
9336f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9346f9703d0SDarrick J. Wong 
9356f9703d0SDarrick J. Wong 	return count;
9366f9703d0SDarrick J. Wong }
9376f9703d0SDarrick J. Wong 
93842291a5aSGuenter Roeck static ssize_t pwm_auto_show(struct device *dev,
93942291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
9406f9703d0SDarrick J. Wong {
9416f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9426f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
943*ad00a02eSChris Packham 
944*ad00a02eSChris Packham 	if (IS_ERR(data))
945*ad00a02eSChris Packham 		return PTR_ERR(data);
946*ad00a02eSChris Packham 
9476f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
9486f9703d0SDarrick J. Wong }
9496f9703d0SDarrick J. Wong 
95042291a5aSGuenter Roeck static ssize_t pwm_auto_store(struct device *dev,
9516f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
95242291a5aSGuenter Roeck 			      const char *buf, size_t count)
9536f9703d0SDarrick J. Wong {
9546f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
95530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
95630485776SAxel Lin 	struct i2c_client *client = data->client;
9576f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
9586f9703d0SDarrick J. Wong 	int pwm_auto_reg_mask;
95905a9bd46SDarrick J. Wong 	long temp;
9606f9703d0SDarrick J. Wong 	u8 reg;
9616f9703d0SDarrick J. Wong 
962179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
96305a9bd46SDarrick J. Wong 		return -EINVAL;
96405a9bd46SDarrick J. Wong 
9656f9703d0SDarrick J. Wong 	if (attr->index % 2)
9666f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
9676f9703d0SDarrick J. Wong 	else
9686f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
9696f9703d0SDarrick J. Wong 
9706f9703d0SDarrick J. Wong 	if (temp != 2 && temp != 1)
9716f9703d0SDarrick J. Wong 		return -EINVAL;
9726f9703d0SDarrick J. Wong 	temp--;
9736f9703d0SDarrick J. Wong 
9746f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
9756f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
9766f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
9776f9703d0SDarrick J. Wong 	if (temp)
9786f9703d0SDarrick J. Wong 		reg |= pwm_auto_reg_mask;
9796f9703d0SDarrick J. Wong 	else
9806f9703d0SDarrick J. Wong 		reg &= ~pwm_auto_reg_mask;
9816f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
9826f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9836f9703d0SDarrick J. Wong 
9846f9703d0SDarrick J. Wong 	return count;
9856f9703d0SDarrick J. Wong }
9866f9703d0SDarrick J. Wong 
98742291a5aSGuenter Roeck static ssize_t pwm_auto_temp_show(struct device *dev,
98842291a5aSGuenter Roeck 				  struct device_attribute *devattr, char *buf)
9896f9703d0SDarrick J. Wong {
9906f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9916f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
992*ad00a02eSChris Packham 	u8 ctrl;
9936f9703d0SDarrick J. Wong 
994*ad00a02eSChris Packham 	if (IS_ERR(data))
995*ad00a02eSChris Packham 		return PTR_ERR(data);
996*ad00a02eSChris Packham 
997*ad00a02eSChris Packham 	ctrl = data->pwm_auto_temp[attr->index];
9986f9703d0SDarrick J. Wong 	if (ctrl)
9996f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", 1 << (ctrl - 1));
10006f9703d0SDarrick J. Wong 	else
10016f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
10026f9703d0SDarrick J. Wong }
10036f9703d0SDarrick J. Wong 
10046f9703d0SDarrick J. Wong static int cvt_auto_temp(int input)
10056f9703d0SDarrick J. Wong {
10066f9703d0SDarrick J. Wong 	if (input == ADT7470_PWM_ALL_TEMPS)
10076f9703d0SDarrick J. Wong 		return 0;
1008ce9c2f44SRobert P. J. Day 	if (input < 1 || !is_power_of_2(input))
10096f9703d0SDarrick J. Wong 		return -EINVAL;
10106f9703d0SDarrick J. Wong 	return ilog2(input) + 1;
10116f9703d0SDarrick J. Wong }
10126f9703d0SDarrick J. Wong 
101342291a5aSGuenter Roeck static ssize_t pwm_auto_temp_store(struct device *dev,
10146f9703d0SDarrick J. Wong 				   struct device_attribute *devattr,
101542291a5aSGuenter Roeck 				   const char *buf, size_t count)
10166f9703d0SDarrick J. Wong {
10176f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
101830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
101930485776SAxel Lin 	struct i2c_client *client = data->client;
10206f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
102105a9bd46SDarrick J. Wong 	long temp;
10226f9703d0SDarrick J. Wong 	u8 reg;
10236f9703d0SDarrick J. Wong 
1024179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
102505a9bd46SDarrick J. Wong 		return -EINVAL;
102605a9bd46SDarrick J. Wong 
102705a9bd46SDarrick J. Wong 	temp = cvt_auto_temp(temp);
10286f9703d0SDarrick J. Wong 	if (temp < 0)
10296f9703d0SDarrick J. Wong 		return temp;
10306f9703d0SDarrick J. Wong 
10316f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
10326f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
10336f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
10346f9703d0SDarrick J. Wong 
10356f9703d0SDarrick J. Wong 	if (!(attr->index % 2)) {
10366f9703d0SDarrick J. Wong 		reg &= 0xF;
10376f9703d0SDarrick J. Wong 		reg |= (temp << 4) & 0xF0;
10386f9703d0SDarrick J. Wong 	} else {
10396f9703d0SDarrick J. Wong 		reg &= 0xF0;
10406f9703d0SDarrick J. Wong 		reg |= temp & 0xF;
10416f9703d0SDarrick J. Wong 	}
10426f9703d0SDarrick J. Wong 
10436f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
10446f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
10456f9703d0SDarrick J. Wong 
10466f9703d0SDarrick J. Wong 	return count;
10476f9703d0SDarrick J. Wong }
10486f9703d0SDarrick J. Wong 
104942291a5aSGuenter Roeck static ssize_t alarm_show(struct device *dev,
105042291a5aSGuenter Roeck 			  struct device_attribute *devattr, char *buf)
1051fe03f28cSDarrick J. Wong {
1052fe03f28cSDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1053fe03f28cSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
1054fe03f28cSDarrick J. Wong 
1055fe03f28cSDarrick J. Wong 	if (data->alarm & attr->index)
1056fe03f28cSDarrick J. Wong 		return sprintf(buf, "1\n");
1057fe03f28cSDarrick J. Wong 	else
1058fe03f28cSDarrick J. Wong 		return sprintf(buf, "0\n");
1059fe03f28cSDarrick J. Wong }
1060fe03f28cSDarrick J. Wong 
1061808fc6c2SJulia Lawall static DEVICE_ATTR_RW(alarm_mask);
1062808fc6c2SJulia Lawall static DEVICE_ATTR_RW(num_temp_sensors);
1063808fc6c2SJulia Lawall static DEVICE_ATTR_RW(auto_update_interval);
10646f9703d0SDarrick J. Wong 
106542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
106642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
106742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
106842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
106942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
107042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
107142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp7_max, temp_max, 6);
107242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp8_max, temp_max, 7);
107342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp9_max, temp_max, 8);
107442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp10_max, temp_max, 9);
10756f9703d0SDarrick J. Wong 
107642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
107742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
107842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
107942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
108042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp5_min, temp_min, 4);
108142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp6_min, temp_min, 5);
108242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp7_min, temp_min, 6);
108342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp8_min, temp_min, 7);
108442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp9_min, temp_min, 8);
108542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp10_min, temp_min, 9);
10866f9703d0SDarrick J. Wong 
108742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
108842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
108942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
109042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
109142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
109242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
109342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6);
109442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7);
109542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_input, temp, 8);
109642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_input, temp, 9);
10976f9703d0SDarrick J. Wong 
109842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, ADT7470_R1T_ALARM);
109942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, ADT7470_R2T_ALARM);
110042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, ADT7470_R3T_ALARM);
110142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, ADT7470_R4T_ALARM);
110242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, ADT7470_R5T_ALARM);
110342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, ADT7470_R6T_ALARM);
110442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_alarm, alarm, ADT7470_R7T_ALARM);
110542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_alarm, alarm, ALARM2(ADT7470_R8T_ALARM));
110642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_alarm, alarm, ALARM2(ADT7470_R9T_ALARM));
110742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_alarm, alarm, ALARM2(ADT7470_R10T_ALARM));
1108fe03f28cSDarrick J. Wong 
110942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_max, fan_max, 0);
111042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_max, fan_max, 1);
111142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan3_max, fan_max, 2);
111242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan4_max, fan_max, 3);
11136f9703d0SDarrick J. Wong 
111442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
111542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
111642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
111742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
11186f9703d0SDarrick J. Wong 
111942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
112042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
112142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
112242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
11236f9703d0SDarrick J. Wong 
112442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, ALARM2(ADT7470_FAN1_ALARM));
112542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, ALARM2(ADT7470_FAN2_ALARM));
112642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, ALARM2(ADT7470_FAN3_ALARM));
112742291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, ALARM2(ADT7470_FAN4_ALARM));
1128fe03f28cSDarrick J. Wong 
112942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
11306f9703d0SDarrick J. Wong 
113142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
113242291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
113342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
113442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
11356f9703d0SDarrick J. Wong 
1136808fc6c2SJulia Lawall static DEVICE_ATTR_RW(pwm1_freq);
1137aa18cc91SJoshua Scott 
113842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
113942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
114042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
114142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
11426f9703d0SDarrick J. Wong 
114342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
114442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
114542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
114642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
11476f9703d0SDarrick J. Wong 
114842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0);
114942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1);
115042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2);
115142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3);
11526f9703d0SDarrick J. Wong 
115342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0);
115442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1);
115542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2);
115642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3);
11576f9703d0SDarrick J. Wong 
115842291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
115942291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
116042291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
116142291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
11626f9703d0SDarrick J. Wong 
116342291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
116442291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
116542291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
116642291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
11676f9703d0SDarrick J. Wong 
116830485776SAxel Lin static struct attribute *adt7470_attrs[] = {
1169fe03f28cSDarrick J. Wong 	&dev_attr_alarm_mask.attr,
11702f22d5dfSDarrick J. Wong 	&dev_attr_num_temp_sensors.attr,
117189fac11cSDarrick J. Wong 	&dev_attr_auto_update_interval.attr,
11726f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_max.dev_attr.attr,
11736f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_max.dev_attr.attr,
11746f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_max.dev_attr.attr,
11756f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_max.dev_attr.attr,
11766f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_max.dev_attr.attr,
11776f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_max.dev_attr.attr,
11786f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_max.dev_attr.attr,
11796f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_max.dev_attr.attr,
11806f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_max.dev_attr.attr,
11816f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_max.dev_attr.attr,
11826f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_min.dev_attr.attr,
11836f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_min.dev_attr.attr,
11846f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_min.dev_attr.attr,
11856f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_min.dev_attr.attr,
11866f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_min.dev_attr.attr,
11876f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_min.dev_attr.attr,
11886f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_min.dev_attr.attr,
11896f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_min.dev_attr.attr,
11906f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_min.dev_attr.attr,
11916f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_min.dev_attr.attr,
11926f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_input.dev_attr.attr,
11936f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_input.dev_attr.attr,
11946f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_input.dev_attr.attr,
11956f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_input.dev_attr.attr,
11966f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_input.dev_attr.attr,
11976f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_input.dev_attr.attr,
11986f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_input.dev_attr.attr,
11996f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_input.dev_attr.attr,
12006f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_input.dev_attr.attr,
12016f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_input.dev_attr.attr,
1202fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1203fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1204fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1205fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
1206fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
1207fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
1208fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
1209fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
1210fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
1211fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
12126f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_max.dev_attr.attr,
12136f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_max.dev_attr.attr,
12146f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_max.dev_attr.attr,
12156f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_max.dev_attr.attr,
12166f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_min.dev_attr.attr,
12176f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_min.dev_attr.attr,
12186f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_min.dev_attr.attr,
12196f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_min.dev_attr.attr,
12206f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_input.dev_attr.attr,
12216f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_input.dev_attr.attr,
12226f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_input.dev_attr.attr,
12236f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1224fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1225fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1226fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1227fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
12286f9703d0SDarrick J. Wong 	&sensor_dev_attr_force_pwm_max.dev_attr.attr,
12296f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1.dev_attr.attr,
1230aa18cc91SJoshua Scott 	&dev_attr_pwm1_freq.attr,
12316f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2.dev_attr.attr,
12326f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3.dev_attr.attr,
12336f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4.dev_attr.attr,
12346f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
12356f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
12366f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
12376f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
12386f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
12396f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
12406f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
12416f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
12426f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
12436f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
12446f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
12456f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
12466f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
12476f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
12486f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
12496f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
12506f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
12516f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
12526f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
12536f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
12546f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
12556f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
12566f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
12576f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
12586f9703d0SDarrick J. Wong 	NULL
12596f9703d0SDarrick J. Wong };
12606f9703d0SDarrick J. Wong 
126130485776SAxel Lin ATTRIBUTE_GROUPS(adt7470);
126230485776SAxel Lin 
1263008f1ca5SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
1264310ec792SJean Delvare static int adt7470_detect(struct i2c_client *client,
1265008f1ca5SJean Delvare 			  struct i2c_board_info *info)
12666f9703d0SDarrick J. Wong {
1267008f1ca5SJean Delvare 	struct i2c_adapter *adapter = client->adapter;
126852df6440SJean Delvare 	int vendor, device, revision;
12696f9703d0SDarrick J. Wong 
12706f9703d0SDarrick J. Wong 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1271008f1ca5SJean Delvare 		return -ENODEV;
12726f9703d0SDarrick J. Wong 
12736f9703d0SDarrick J. Wong 	vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1274008f1ca5SJean Delvare 	if (vendor != ADT7470_VENDOR)
1275008f1ca5SJean Delvare 		return -ENODEV;
12766f9703d0SDarrick J. Wong 
12776f9703d0SDarrick J. Wong 	device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1278008f1ca5SJean Delvare 	if (device != ADT7470_DEVICE)
1279008f1ca5SJean Delvare 		return -ENODEV;
12806f9703d0SDarrick J. Wong 
128152df6440SJean Delvare 	revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1282008f1ca5SJean Delvare 	if (revision != ADT7470_REVISION)
1283008f1ca5SJean Delvare 		return -ENODEV;
12846f9703d0SDarrick J. Wong 
1285008f1ca5SJean Delvare 	strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
12866f9703d0SDarrick J. Wong 
1287008f1ca5SJean Delvare 	return 0;
1288008f1ca5SJean Delvare }
1289008f1ca5SJean Delvare 
12909027d933SAxel Lin static void adt7470_init_client(struct i2c_client *client)
12919027d933SAxel Lin {
12929027d933SAxel Lin 	int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
12939027d933SAxel Lin 
12949027d933SAxel Lin 	if (reg < 0) {
12959027d933SAxel Lin 		dev_err(&client->dev, "cannot read configuration register\n");
12969027d933SAxel Lin 	} else {
12979027d933SAxel Lin 		/* start monitoring (and do a self-test) */
12989027d933SAxel Lin 		i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
12999027d933SAxel Lin 	}
13009027d933SAxel Lin }
13019027d933SAxel Lin 
130267487038SStephen Kitt static int adt7470_probe(struct i2c_client *client)
1303008f1ca5SJean Delvare {
130430485776SAxel Lin 	struct device *dev = &client->dev;
1305008f1ca5SJean Delvare 	struct adt7470_data *data;
130630485776SAxel Lin 	struct device *hwmon_dev;
1307008f1ca5SJean Delvare 
130830485776SAxel Lin 	data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
13099cc7dcc5SGuenter Roeck 	if (!data)
13109cc7dcc5SGuenter Roeck 		return -ENOMEM;
1311008f1ca5SJean Delvare 
13122f22d5dfSDarrick J. Wong 	data->num_temp_sensors = -1;
131389fac11cSDarrick J. Wong 	data->auto_update_interval = AUTO_UPDATE_INTERVAL;
13142f22d5dfSDarrick J. Wong 
1315008f1ca5SJean Delvare 	i2c_set_clientdata(client, data);
131630485776SAxel Lin 	data->client = client;
1317008f1ca5SJean Delvare 	mutex_init(&data->lock);
13186f9703d0SDarrick J. Wong 
13196f9703d0SDarrick J. Wong 	dev_info(&client->dev, "%s chip found\n", client->name);
13206f9703d0SDarrick J. Wong 
13216f9703d0SDarrick J. Wong 	/* Initialize the ADT7470 chip */
13226f9703d0SDarrick J. Wong 	adt7470_init_client(client);
13236f9703d0SDarrick J. Wong 
13246f9703d0SDarrick J. Wong 	/* Register sysfs hooks */
132530485776SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
132630485776SAxel Lin 							   data,
132730485776SAxel Lin 							   adt7470_groups);
13286f9703d0SDarrick J. Wong 
132930485776SAxel Lin 	if (IS_ERR(hwmon_dev))
133030485776SAxel Lin 		return PTR_ERR(hwmon_dev);
13316f9703d0SDarrick J. Wong 
1332f170168bSKees Cook 	data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
133330485776SAxel Lin 					dev_name(hwmon_dev));
1334f7334b4cSAxel Lin 	if (IS_ERR(data->auto_update)) {
133530485776SAxel Lin 		return PTR_ERR(data->auto_update);
1336f7334b4cSAxel Lin 	}
133789fac11cSDarrick J. Wong 
13386f9703d0SDarrick J. Wong 	return 0;
13396f9703d0SDarrick J. Wong }
13406f9703d0SDarrick J. Wong 
1341008f1ca5SJean Delvare static int adt7470_remove(struct i2c_client *client)
13426f9703d0SDarrick J. Wong {
13436f9703d0SDarrick J. Wong 	struct adt7470_data *data = i2c_get_clientdata(client);
13446f9703d0SDarrick J. Wong 
134589fac11cSDarrick J. Wong 	kthread_stop(data->auto_update);
13466f9703d0SDarrick J. Wong 	return 0;
13476f9703d0SDarrick J. Wong }
13486f9703d0SDarrick J. Wong 
13499027d933SAxel Lin static const struct i2c_device_id adt7470_id[] = {
13509027d933SAxel Lin 	{ "adt7470", 0 },
13519027d933SAxel Lin 	{ }
13529027d933SAxel Lin };
13539027d933SAxel Lin MODULE_DEVICE_TABLE(i2c, adt7470_id);
13549027d933SAxel Lin 
13559027d933SAxel Lin static struct i2c_driver adt7470_driver = {
13569027d933SAxel Lin 	.class		= I2C_CLASS_HWMON,
13579027d933SAxel Lin 	.driver = {
13589027d933SAxel Lin 		.name	= "adt7470",
13599027d933SAxel Lin 	},
136067487038SStephen Kitt 	.probe_new	= adt7470_probe,
13619027d933SAxel Lin 	.remove		= adt7470_remove,
13629027d933SAxel Lin 	.id_table	= adt7470_id,
13639027d933SAxel Lin 	.detect		= adt7470_detect,
13649027d933SAxel Lin 	.address_list	= normal_i2c,
13659027d933SAxel Lin };
13669027d933SAxel Lin 
1367f0967eeaSAxel Lin module_i2c_driver(adt7470_driver);
13686f9703d0SDarrick J. Wong 
13695407e051SDarrick J. Wong MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
13706f9703d0SDarrick J. Wong MODULE_DESCRIPTION("ADT7470 driver");
13716f9703d0SDarrick J. Wong MODULE_LICENSE("GPL");
1372