xref: /linux/drivers/hwmon/adt7470.c (revision 42291a5aaed748a88dc69c0854a8101cf8f3b3eb)
16f9703d0SDarrick J. Wong /*
26f9703d0SDarrick J. Wong  * A hwmon driver for the Analog Devices ADT7470
36f9703d0SDarrick J. Wong  * Copyright (C) 2007 IBM
46f9703d0SDarrick J. Wong  *
55407e051SDarrick J. Wong  * Author: Darrick J. Wong <darrick.wong@oracle.com>
66f9703d0SDarrick J. Wong  *
76f9703d0SDarrick J. Wong  * This program is free software; you can redistribute it and/or modify
86f9703d0SDarrick J. Wong  * it under the terms of the GNU General Public License as published by
96f9703d0SDarrick J. Wong  * the Free Software Foundation; either version 2 of the License, or
106f9703d0SDarrick J. Wong  * (at your option) any later version.
116f9703d0SDarrick J. Wong  *
126f9703d0SDarrick J. Wong  * This program is distributed in the hope that it will be useful,
136f9703d0SDarrick J. Wong  * but WITHOUT ANY WARRANTY; without even the implied warranty of
146f9703d0SDarrick J. Wong  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
156f9703d0SDarrick J. Wong  * GNU General Public License for more details.
166f9703d0SDarrick J. Wong  *
176f9703d0SDarrick J. Wong  * You should have received a copy of the GNU General Public License
186f9703d0SDarrick J. Wong  * along with this program; if not, write to the Free Software
196f9703d0SDarrick J. Wong  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
206f9703d0SDarrick J. Wong  */
216f9703d0SDarrick J. Wong 
222e991201SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
232e991201SJoe Perches 
246f9703d0SDarrick J. Wong #include <linux/module.h>
256f9703d0SDarrick J. Wong #include <linux/jiffies.h>
266f9703d0SDarrick J. Wong #include <linux/i2c.h>
276f9703d0SDarrick J. Wong #include <linux/hwmon.h>
286f9703d0SDarrick J. Wong #include <linux/hwmon-sysfs.h>
296f9703d0SDarrick J. Wong #include <linux/err.h>
306f9703d0SDarrick J. Wong #include <linux/mutex.h>
316f9703d0SDarrick J. Wong #include <linux/delay.h>
326f9703d0SDarrick J. Wong #include <linux/log2.h>
3389fac11cSDarrick J. Wong #include <linux/kthread.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
35aa18cc91SJoshua Scott #include <linux/util_macros.h>
366f9703d0SDarrick J. Wong 
376f9703d0SDarrick J. Wong /* Addresses to scan */
3825e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = { 0x2C, 0x2E, 0x2F, I2C_CLIENT_END };
396f9703d0SDarrick J. Wong 
406f9703d0SDarrick J. Wong /* ADT7470 registers */
416f9703d0SDarrick J. Wong #define ADT7470_REG_BASE_ADDR			0x20
426f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_BASE_ADDR		0x20
436f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_MAX_ADDR		0x29
446f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_BASE_ADDR		0x2A
456f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX_ADDR		0x31
466f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_BASE_ADDR		0x32
476f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX_ADDR		0x35
486f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX_BASE_ADDR		0x38
496f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX_MAX_ADDR		0x3B
506f9703d0SDarrick J. Wong #define ADT7470_REG_CFG				0x40
516f9703d0SDarrick J. Wong #define		ADT7470_FSPD_MASK		0x04
526f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM1			0x41
53fe03f28cSDarrick J. Wong #define		ADT7470_R1T_ALARM		0x01
54fe03f28cSDarrick J. Wong #define		ADT7470_R2T_ALARM		0x02
55fe03f28cSDarrick J. Wong #define		ADT7470_R3T_ALARM		0x04
56fe03f28cSDarrick J. Wong #define		ADT7470_R4T_ALARM		0x08
57fe03f28cSDarrick J. Wong #define		ADT7470_R5T_ALARM		0x10
58fe03f28cSDarrick J. Wong #define		ADT7470_R6T_ALARM		0x20
59fe03f28cSDarrick J. Wong #define		ADT7470_R7T_ALARM		0x40
60fe03f28cSDarrick J. Wong #define		ADT7470_OOL_ALARM		0x80
616f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM2			0x42
62fe03f28cSDarrick J. Wong #define		ADT7470_R8T_ALARM		0x01
63fe03f28cSDarrick J. Wong #define		ADT7470_R9T_ALARM		0x02
64fe03f28cSDarrick J. Wong #define		ADT7470_R10T_ALARM		0x04
65fe03f28cSDarrick J. Wong #define		ADT7470_FAN1_ALARM		0x10
66fe03f28cSDarrick J. Wong #define		ADT7470_FAN2_ALARM		0x20
67fe03f28cSDarrick J. Wong #define		ADT7470_FAN3_ALARM		0x40
68fe03f28cSDarrick J. Wong #define		ADT7470_FAN4_ALARM		0x80
696f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_LIMITS_BASE_ADDR	0x44
706f9703d0SDarrick J. Wong #define ADT7470_REG_TEMP_LIMITS_MAX_ADDR	0x57
716f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MIN_BASE_ADDR		0x58
726f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MIN_MAX_ADDR		0x5F
736f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX_BASE_ADDR		0x60
746f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX_MAX_ADDR		0x67
756f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_CFG_BASE_ADDR		0x68
766f9703d0SDarrick J. Wong #define ADT7470_REG_PWM12_CFG			0x68
776f9703d0SDarrick J. Wong #define		ADT7470_PWM2_AUTO_MASK		0x40
786f9703d0SDarrick J. Wong #define		ADT7470_PWM1_AUTO_MASK		0x80
792e75a4b7SDarrick J. Wong #define		ADT7470_PWM_AUTO_MASK		0xC0
806f9703d0SDarrick J. Wong #define ADT7470_REG_PWM34_CFG			0x69
816f9703d0SDarrick J. Wong #define		ADT7470_PWM3_AUTO_MASK		0x40
826f9703d0SDarrick J. Wong #define		ADT7470_PWM4_AUTO_MASK		0x80
836f9703d0SDarrick J. Wong #define	ADT7470_REG_PWM_MIN_BASE_ADDR		0x6A
846f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MIN_MAX_ADDR		0x6D
856f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR	0x6E
866f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_TEMP_MIN_MAX_ADDR	0x71
87aa18cc91SJoshua Scott #define ADT7470_REG_CFG_2			0x74
886f9703d0SDarrick J. Wong #define ADT7470_REG_ACOUSTICS12			0x75
896f9703d0SDarrick J. Wong #define ADT7470_REG_ACOUSTICS34			0x76
906f9703d0SDarrick J. Wong #define ADT7470_REG_DEVICE			0x3D
916f9703d0SDarrick J. Wong #define ADT7470_REG_VENDOR			0x3E
926f9703d0SDarrick J. Wong #define ADT7470_REG_REVISION			0x3F
936f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM1_MASK			0x72
946f9703d0SDarrick J. Wong #define ADT7470_REG_ALARM2_MASK			0x73
956f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR	0x7C
966f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_AUTO_TEMP_MAX_ADDR	0x7D
976f9703d0SDarrick J. Wong #define ADT7470_REG_MAX_ADDR			0x81
986f9703d0SDarrick J. Wong 
996f9703d0SDarrick J. Wong #define ADT7470_TEMP_COUNT	10
1006f9703d0SDarrick J. Wong #define ADT7470_TEMP_REG(x)	(ADT7470_REG_TEMP_BASE_ADDR + (x))
1016f9703d0SDarrick J. Wong #define ADT7470_TEMP_MIN_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
1026f9703d0SDarrick J. Wong #define ADT7470_TEMP_MAX_REG(x) (ADT7470_REG_TEMP_LIMITS_BASE_ADDR + \
1036f9703d0SDarrick J. Wong 				((x) * 2) + 1)
1046f9703d0SDarrick J. Wong 
1056f9703d0SDarrick J. Wong #define ADT7470_FAN_COUNT	4
1066f9703d0SDarrick J. Wong #define ADT7470_REG_FAN(x)	(ADT7470_REG_FAN_BASE_ADDR + ((x) * 2))
1076f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MIN(x)	(ADT7470_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
1086f9703d0SDarrick J. Wong #define ADT7470_REG_FAN_MAX(x)	(ADT7470_REG_FAN_MAX_BASE_ADDR + ((x) * 2))
1096f9703d0SDarrick J. Wong 
1106f9703d0SDarrick J. Wong #define ADT7470_PWM_COUNT	4
1116f9703d0SDarrick J. Wong #define ADT7470_REG_PWM(x)	(ADT7470_REG_PWM_BASE_ADDR + (x))
1126f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MAX(x)	(ADT7470_REG_PWM_MAX_BASE_ADDR + (x))
1136f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_MIN(x)	(ADT7470_REG_PWM_MIN_BASE_ADDR + (x))
1146f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_TMIN(x)	(ADT7470_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
1156f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_CFG(x)	(ADT7470_REG_PWM_CFG_BASE_ADDR + ((x) / 2))
1166f9703d0SDarrick J. Wong #define ADT7470_REG_PWM_AUTO_TEMP(x)	(ADT7470_REG_PWM_AUTO_TEMP_BASE_ADDR + \
1176f9703d0SDarrick J. Wong 					((x) / 2))
1186f9703d0SDarrick J. Wong 
119fe03f28cSDarrick J. Wong #define ALARM2(x)		((x) << 8)
120fe03f28cSDarrick J. Wong 
1216f9703d0SDarrick J. Wong #define ADT7470_VENDOR		0x41
1226f9703d0SDarrick J. Wong #define ADT7470_DEVICE		0x70
1236f9703d0SDarrick J. Wong /* datasheet only mentions a revision 2 */
1246f9703d0SDarrick J. Wong #define ADT7470_REVISION	0x02
1256f9703d0SDarrick J. Wong 
1266f9703d0SDarrick J. Wong /* "all temps" according to hwmon sysfs interface spec */
1276f9703d0SDarrick J. Wong #define ADT7470_PWM_ALL_TEMPS	0x3FF
1286f9703d0SDarrick J. Wong 
1296f9703d0SDarrick J. Wong /* How often do we reread sensors values? (In jiffies) */
1306f9703d0SDarrick J. Wong #define SENSOR_REFRESH_INTERVAL	(5 * HZ)
1316f9703d0SDarrick J. Wong 
1326f9703d0SDarrick J. Wong /* How often do we reread sensor limit values? (In jiffies) */
1336f9703d0SDarrick J. Wong #define LIMIT_REFRESH_INTERVAL	(60 * HZ)
1346f9703d0SDarrick J. Wong 
1352f22d5dfSDarrick J. Wong /* Wait at least 200ms per sensor for 10 sensors */
1362f22d5dfSDarrick J. Wong #define TEMP_COLLECTION_TIME	2000
1376f9703d0SDarrick J. Wong 
13889fac11cSDarrick J. Wong /* auto update thing won't fire more than every 2s */
13989fac11cSDarrick J. Wong #define AUTO_UPDATE_INTERVAL	2000
14089fac11cSDarrick J. Wong 
1416f9703d0SDarrick J. Wong /* datasheet says to divide this number by the fan reading to get fan rpm */
1426f9703d0SDarrick J. Wong #define FAN_PERIOD_TO_RPM(x)	((90000 * 60) / (x))
1436f9703d0SDarrick J. Wong #define FAN_RPM_TO_PERIOD	FAN_PERIOD_TO_RPM
1446f9703d0SDarrick J. Wong #define FAN_PERIOD_INVALID	65535
1456f9703d0SDarrick J. Wong #define FAN_DATA_VALID(x)	((x) && (x) != FAN_PERIOD_INVALID)
1466f9703d0SDarrick J. Wong 
147aa18cc91SJoshua Scott /* Config registers 1 and 2 include fields for selecting the PWM frequency */
148aa18cc91SJoshua Scott #define ADT7470_CFG_LF		0x40
149aa18cc91SJoshua Scott #define ADT7470_FREQ_MASK	0x70
150aa18cc91SJoshua Scott #define ADT7470_FREQ_SHIFT	4
151aa18cc91SJoshua Scott 
1526f9703d0SDarrick J. Wong struct adt7470_data {
15330485776SAxel Lin 	struct i2c_client	*client;
1546f9703d0SDarrick J. Wong 	struct mutex		lock;
1556f9703d0SDarrick J. Wong 	char			sensors_valid;
1566f9703d0SDarrick J. Wong 	char			limits_valid;
1576f9703d0SDarrick J. Wong 	unsigned long		sensors_last_updated;	/* In jiffies */
1586f9703d0SDarrick J. Wong 	unsigned long		limits_last_updated;	/* In jiffies */
1596f9703d0SDarrick J. Wong 
1602f22d5dfSDarrick J. Wong 	int			num_temp_sensors;	/* -1 = probe */
16189fac11cSDarrick J. Wong 	int			temperatures_probed;
1622f22d5dfSDarrick J. Wong 
1636f9703d0SDarrick J. Wong 	s8			temp[ADT7470_TEMP_COUNT];
1646f9703d0SDarrick J. Wong 	s8			temp_min[ADT7470_TEMP_COUNT];
1656f9703d0SDarrick J. Wong 	s8			temp_max[ADT7470_TEMP_COUNT];
1666f9703d0SDarrick J. Wong 	u16			fan[ADT7470_FAN_COUNT];
1676f9703d0SDarrick J. Wong 	u16			fan_min[ADT7470_FAN_COUNT];
1686f9703d0SDarrick J. Wong 	u16			fan_max[ADT7470_FAN_COUNT];
169fe03f28cSDarrick J. Wong 	u16			alarm;
170fe03f28cSDarrick J. Wong 	u16			alarms_mask;
1716f9703d0SDarrick J. Wong 	u8			force_pwm_max;
1726f9703d0SDarrick J. Wong 	u8			pwm[ADT7470_PWM_COUNT];
1736f9703d0SDarrick J. Wong 	u8			pwm_max[ADT7470_PWM_COUNT];
1746f9703d0SDarrick J. Wong 	u8			pwm_automatic[ADT7470_PWM_COUNT];
1756f9703d0SDarrick J. Wong 	u8			pwm_min[ADT7470_PWM_COUNT];
1766f9703d0SDarrick J. Wong 	s8			pwm_tmin[ADT7470_PWM_COUNT];
1776f9703d0SDarrick J. Wong 	u8			pwm_auto_temp[ADT7470_PWM_COUNT];
17889fac11cSDarrick J. Wong 
17989fac11cSDarrick J. Wong 	struct task_struct	*auto_update;
18089fac11cSDarrick J. Wong 	unsigned int		auto_update_interval;
1816f9703d0SDarrick J. Wong };
1826f9703d0SDarrick J. Wong 
1836f9703d0SDarrick J. Wong /*
1846f9703d0SDarrick J. Wong  * 16-bit registers on the ADT7470 are low-byte first.  The data sheet says
1856f9703d0SDarrick J. Wong  * that the low byte must be read before the high byte.
1866f9703d0SDarrick J. Wong  */
1876f9703d0SDarrick J. Wong static inline int adt7470_read_word_data(struct i2c_client *client, u8 reg)
1886f9703d0SDarrick J. Wong {
1896f9703d0SDarrick J. Wong 	u16 foo;
1906f9703d0SDarrick J. Wong 	foo = i2c_smbus_read_byte_data(client, reg);
1916f9703d0SDarrick J. Wong 	foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
1926f9703d0SDarrick J. Wong 	return foo;
1936f9703d0SDarrick J. Wong }
1946f9703d0SDarrick J. Wong 
1956f9703d0SDarrick J. Wong static inline int adt7470_write_word_data(struct i2c_client *client, u8 reg,
1966f9703d0SDarrick J. Wong 					  u16 value)
1976f9703d0SDarrick J. Wong {
1986f9703d0SDarrick J. Wong 	return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
19993d783bcSCurt Brune 	       || i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
2006f9703d0SDarrick J. Wong }
2016f9703d0SDarrick J. Wong 
20289fac11cSDarrick J. Wong /* Probe for temperature sensors.  Assumes lock is held */
20389fac11cSDarrick J. Wong static int adt7470_read_temperatures(struct i2c_client *client,
20489fac11cSDarrick J. Wong 				     struct adt7470_data *data)
2056f9703d0SDarrick J. Wong {
20689fac11cSDarrick J. Wong 	unsigned long res;
2076f9703d0SDarrick J. Wong 	int i;
20889fac11cSDarrick J. Wong 	u8 cfg, pwm[4], pwm_cfg[2];
2096f9703d0SDarrick J. Wong 
2102e75a4b7SDarrick J. Wong 	/* save pwm[1-4] config register */
2112e75a4b7SDarrick J. Wong 	pwm_cfg[0] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(0));
2122e75a4b7SDarrick J. Wong 	pwm_cfg[1] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM_CFG(2));
2132e75a4b7SDarrick J. Wong 
2142e75a4b7SDarrick J. Wong 	/* set manual pwm to whatever it is set to now */
2152e75a4b7SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
2162e75a4b7SDarrick J. Wong 		pwm[i] = i2c_smbus_read_byte_data(client, ADT7470_REG_PWM(i));
2172e75a4b7SDarrick J. Wong 
2182e75a4b7SDarrick J. Wong 	/* put pwm in manual mode */
2192e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0),
2202e75a4b7SDarrick J. Wong 		pwm_cfg[0] & ~(ADT7470_PWM_AUTO_MASK));
2212e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2),
2222e75a4b7SDarrick J. Wong 		pwm_cfg[1] & ~(ADT7470_PWM_AUTO_MASK));
2232e75a4b7SDarrick J. Wong 
2242e75a4b7SDarrick J. Wong 	/* write pwm control to whatever it was */
2252e75a4b7SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
2262e75a4b7SDarrick J. Wong 		i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(i), pwm[i]);
2272e75a4b7SDarrick J. Wong 
2286f9703d0SDarrick J. Wong 	/* start reading temperature sensors */
2296f9703d0SDarrick J. Wong 	cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
2306f9703d0SDarrick J. Wong 	cfg |= 0x80;
2316f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
2326f9703d0SDarrick J. Wong 
2332f22d5dfSDarrick J. Wong 	/* Delay is 200ms * number of temp sensors. */
23489fac11cSDarrick J. Wong 	res = msleep_interruptible((data->num_temp_sensors >= 0 ?
2352f22d5dfSDarrick J. Wong 				    data->num_temp_sensors * 200 :
2362f22d5dfSDarrick J. Wong 				    TEMP_COLLECTION_TIME));
2376f9703d0SDarrick J. Wong 
2386f9703d0SDarrick J. Wong 	/* done reading temperature sensors */
2396f9703d0SDarrick J. Wong 	cfg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
2406f9703d0SDarrick J. Wong 	cfg &= ~0x80;
2416f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, cfg);
2426f9703d0SDarrick J. Wong 
2432e75a4b7SDarrick J. Wong 	/* restore pwm[1-4] config registers */
2442e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(0), pwm_cfg[0]);
2452e75a4b7SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_CFG(2), pwm_cfg[1]);
2462e75a4b7SDarrick J. Wong 
24789fac11cSDarrick J. Wong 	if (res) {
2482e991201SJoe Perches 		pr_err("ha ha, interrupted\n");
24989fac11cSDarrick J. Wong 		return -EAGAIN;
25089fac11cSDarrick J. Wong 	}
25189fac11cSDarrick J. Wong 
25289fac11cSDarrick J. Wong 	/* Only count fans if we have to */
25389fac11cSDarrick J. Wong 	if (data->num_temp_sensors >= 0)
25489fac11cSDarrick J. Wong 		return 0;
25589fac11cSDarrick J. Wong 
25689fac11cSDarrick J. Wong 	for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
25789fac11cSDarrick J. Wong 		data->temp[i] = i2c_smbus_read_byte_data(client,
25889fac11cSDarrick J. Wong 						ADT7470_TEMP_REG(i));
25989fac11cSDarrick J. Wong 		if (data->temp[i])
26089fac11cSDarrick J. Wong 			data->num_temp_sensors = i + 1;
26189fac11cSDarrick J. Wong 	}
26289fac11cSDarrick J. Wong 	data->temperatures_probed = 1;
26389fac11cSDarrick J. Wong 	return 0;
26489fac11cSDarrick J. Wong }
26589fac11cSDarrick J. Wong 
26689fac11cSDarrick J. Wong static int adt7470_update_thread(void *p)
26789fac11cSDarrick J. Wong {
26889fac11cSDarrick J. Wong 	struct i2c_client *client = p;
26989fac11cSDarrick J. Wong 	struct adt7470_data *data = i2c_get_clientdata(client);
27089fac11cSDarrick J. Wong 
27189fac11cSDarrick J. Wong 	while (!kthread_should_stop()) {
27289fac11cSDarrick J. Wong 		mutex_lock(&data->lock);
27389fac11cSDarrick J. Wong 		adt7470_read_temperatures(client, data);
27489fac11cSDarrick J. Wong 		mutex_unlock(&data->lock);
27593cacfd4SJoshua Scott 
27693cacfd4SJoshua Scott 		set_current_state(TASK_INTERRUPTIBLE);
27789fac11cSDarrick J. Wong 		if (kthread_should_stop())
27889fac11cSDarrick J. Wong 			break;
27993cacfd4SJoshua Scott 
28093cacfd4SJoshua Scott 		schedule_timeout(msecs_to_jiffies(data->auto_update_interval));
28189fac11cSDarrick J. Wong 	}
28289fac11cSDarrick J. Wong 
28389fac11cSDarrick J. Wong 	return 0;
28489fac11cSDarrick J. Wong }
28589fac11cSDarrick J. Wong 
28689fac11cSDarrick J. Wong static struct adt7470_data *adt7470_update_device(struct device *dev)
28789fac11cSDarrick J. Wong {
28830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
28930485776SAxel Lin 	struct i2c_client *client = data->client;
29089fac11cSDarrick J. Wong 	unsigned long local_jiffies = jiffies;
29189fac11cSDarrick J. Wong 	u8 cfg;
29289fac11cSDarrick J. Wong 	int i;
29389fac11cSDarrick J. Wong 	int need_sensors = 1;
29489fac11cSDarrick J. Wong 	int need_limits = 1;
29589fac11cSDarrick J. Wong 
29689fac11cSDarrick J. Wong 	/*
29789fac11cSDarrick J. Wong 	 * Figure out if we need to update the shadow registers.
29889fac11cSDarrick J. Wong 	 * Lockless means that we may occasionally report out of
29989fac11cSDarrick J. Wong 	 * date data.
30089fac11cSDarrick J. Wong 	 */
30189fac11cSDarrick J. Wong 	if (time_before(local_jiffies, data->sensors_last_updated +
30289fac11cSDarrick J. Wong 			SENSOR_REFRESH_INTERVAL) &&
30389fac11cSDarrick J. Wong 	    data->sensors_valid)
30489fac11cSDarrick J. Wong 		need_sensors = 0;
30589fac11cSDarrick J. Wong 
30689fac11cSDarrick J. Wong 	if (time_before(local_jiffies, data->limits_last_updated +
30789fac11cSDarrick J. Wong 			LIMIT_REFRESH_INTERVAL) &&
30889fac11cSDarrick J. Wong 	    data->limits_valid)
30989fac11cSDarrick J. Wong 		need_limits = 0;
31089fac11cSDarrick J. Wong 
31189fac11cSDarrick J. Wong 	if (!need_sensors && !need_limits)
31289fac11cSDarrick J. Wong 		return data;
31389fac11cSDarrick J. Wong 
31489fac11cSDarrick J. Wong 	mutex_lock(&data->lock);
31589fac11cSDarrick J. Wong 	if (!need_sensors)
31689fac11cSDarrick J. Wong 		goto no_sensor_update;
31789fac11cSDarrick J. Wong 
31889fac11cSDarrick J. Wong 	if (!data->temperatures_probed)
31989fac11cSDarrick J. Wong 		adt7470_read_temperatures(client, data);
32089fac11cSDarrick J. Wong 	else
3216f9703d0SDarrick J. Wong 		for (i = 0; i < ADT7470_TEMP_COUNT; i++)
3226f9703d0SDarrick J. Wong 			data->temp[i] = i2c_smbus_read_byte_data(client,
3236f9703d0SDarrick J. Wong 						ADT7470_TEMP_REG(i));
3246f9703d0SDarrick J. Wong 
3256f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++)
3266f9703d0SDarrick J. Wong 		data->fan[i] = adt7470_read_word_data(client,
3276f9703d0SDarrick J. Wong 						ADT7470_REG_FAN(i));
3286f9703d0SDarrick J. Wong 
3296f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
3306f9703d0SDarrick J. Wong 		int reg;
3316f9703d0SDarrick J. Wong 		int reg_mask;
3326f9703d0SDarrick J. Wong 
3336f9703d0SDarrick J. Wong 		data->pwm[i] = i2c_smbus_read_byte_data(client,
3346f9703d0SDarrick J. Wong 						ADT7470_REG_PWM(i));
3356f9703d0SDarrick J. Wong 
3366f9703d0SDarrick J. Wong 		if (i % 2)
3376f9703d0SDarrick J. Wong 			reg_mask = ADT7470_PWM2_AUTO_MASK;
3386f9703d0SDarrick J. Wong 		else
3396f9703d0SDarrick J. Wong 			reg_mask = ADT7470_PWM1_AUTO_MASK;
3406f9703d0SDarrick J. Wong 
3416f9703d0SDarrick J. Wong 		reg = ADT7470_REG_PWM_CFG(i);
3426f9703d0SDarrick J. Wong 		if (i2c_smbus_read_byte_data(client, reg) & reg_mask)
3436f9703d0SDarrick J. Wong 			data->pwm_automatic[i] = 1;
3446f9703d0SDarrick J. Wong 		else
3456f9703d0SDarrick J. Wong 			data->pwm_automatic[i] = 0;
3466f9703d0SDarrick J. Wong 
3476f9703d0SDarrick J. Wong 		reg = ADT7470_REG_PWM_AUTO_TEMP(i);
3486f9703d0SDarrick J. Wong 		cfg = i2c_smbus_read_byte_data(client, reg);
3496f9703d0SDarrick J. Wong 		if (!(i % 2))
3506f9703d0SDarrick J. Wong 			data->pwm_auto_temp[i] = cfg >> 4;
3516f9703d0SDarrick J. Wong 		else
3526f9703d0SDarrick J. Wong 			data->pwm_auto_temp[i] = cfg & 0xF;
3536f9703d0SDarrick J. Wong 	}
3546f9703d0SDarrick J. Wong 
3556f9703d0SDarrick J. Wong 	if (i2c_smbus_read_byte_data(client, ADT7470_REG_CFG) &
3566f9703d0SDarrick J. Wong 	    ADT7470_FSPD_MASK)
3576f9703d0SDarrick J. Wong 		data->force_pwm_max = 1;
3586f9703d0SDarrick J. Wong 	else
3596f9703d0SDarrick J. Wong 		data->force_pwm_max = 0;
3606f9703d0SDarrick J. Wong 
361fe03f28cSDarrick J. Wong 	data->alarm = i2c_smbus_read_byte_data(client, ADT7470_REG_ALARM1);
362fe03f28cSDarrick J. Wong 	if (data->alarm & ADT7470_OOL_ALARM)
363fe03f28cSDarrick J. Wong 		data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
364fe03f28cSDarrick J. Wong 							ADT7470_REG_ALARM2));
3656f9703d0SDarrick J. Wong 	data->alarms_mask = adt7470_read_word_data(client,
3666f9703d0SDarrick J. Wong 						   ADT7470_REG_ALARM1_MASK);
3676f9703d0SDarrick J. Wong 
3686f9703d0SDarrick J. Wong 	data->sensors_last_updated = local_jiffies;
3696f9703d0SDarrick J. Wong 	data->sensors_valid = 1;
3706f9703d0SDarrick J. Wong 
3716f9703d0SDarrick J. Wong no_sensor_update:
37289fac11cSDarrick J. Wong 	if (!need_limits)
3736f9703d0SDarrick J. Wong 		goto out;
3746f9703d0SDarrick J. Wong 
3756f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_TEMP_COUNT; i++) {
3766f9703d0SDarrick J. Wong 		data->temp_min[i] = i2c_smbus_read_byte_data(client,
3776f9703d0SDarrick J. Wong 						ADT7470_TEMP_MIN_REG(i));
3786f9703d0SDarrick J. Wong 		data->temp_max[i] = i2c_smbus_read_byte_data(client,
3796f9703d0SDarrick J. Wong 						ADT7470_TEMP_MAX_REG(i));
3806f9703d0SDarrick J. Wong 	}
3816f9703d0SDarrick J. Wong 
3826f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_FAN_COUNT; i++) {
3836f9703d0SDarrick J. Wong 		data->fan_min[i] = adt7470_read_word_data(client,
3846f9703d0SDarrick J. Wong 						ADT7470_REG_FAN_MIN(i));
3856f9703d0SDarrick J. Wong 		data->fan_max[i] = adt7470_read_word_data(client,
3866f9703d0SDarrick J. Wong 						ADT7470_REG_FAN_MAX(i));
3876f9703d0SDarrick J. Wong 	}
3886f9703d0SDarrick J. Wong 
3896f9703d0SDarrick J. Wong 	for (i = 0; i < ADT7470_PWM_COUNT; i++) {
3906f9703d0SDarrick J. Wong 		data->pwm_max[i] = i2c_smbus_read_byte_data(client,
3916f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_MAX(i));
3926f9703d0SDarrick J. Wong 		data->pwm_min[i] = i2c_smbus_read_byte_data(client,
3936f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_MIN(i));
3946f9703d0SDarrick J. Wong 		data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
3956f9703d0SDarrick J. Wong 						ADT7470_REG_PWM_TMIN(i));
3966f9703d0SDarrick J. Wong 	}
3976f9703d0SDarrick J. Wong 
3986f9703d0SDarrick J. Wong 	data->limits_last_updated = local_jiffies;
3996f9703d0SDarrick J. Wong 	data->limits_valid = 1;
4006f9703d0SDarrick J. Wong 
4016f9703d0SDarrick J. Wong out:
4026f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
4036f9703d0SDarrick J. Wong 	return data;
4046f9703d0SDarrick J. Wong }
4056f9703d0SDarrick J. Wong 
406808fc6c2SJulia Lawall static ssize_t auto_update_interval_show(struct device *dev,
40789fac11cSDarrick J. Wong 					 struct device_attribute *devattr,
40889fac11cSDarrick J. Wong 					 char *buf)
40989fac11cSDarrick J. Wong {
41089fac11cSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
41189fac11cSDarrick J. Wong 	return sprintf(buf, "%d\n", data->auto_update_interval);
41289fac11cSDarrick J. Wong }
41389fac11cSDarrick J. Wong 
414808fc6c2SJulia Lawall static ssize_t auto_update_interval_store(struct device *dev,
41589fac11cSDarrick J. Wong 					  struct device_attribute *devattr,
416808fc6c2SJulia Lawall 					  const char *buf, size_t count)
41789fac11cSDarrick J. Wong {
41830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
41989fac11cSDarrick J. Wong 	long temp;
42089fac11cSDarrick J. Wong 
421179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
42289fac11cSDarrick J. Wong 		return -EINVAL;
42389fac11cSDarrick J. Wong 
4242a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 60000);
42589fac11cSDarrick J. Wong 
42689fac11cSDarrick J. Wong 	mutex_lock(&data->lock);
42789fac11cSDarrick J. Wong 	data->auto_update_interval = temp;
42889fac11cSDarrick J. Wong 	mutex_unlock(&data->lock);
42989fac11cSDarrick J. Wong 
43089fac11cSDarrick J. Wong 	return count;
43189fac11cSDarrick J. Wong }
43289fac11cSDarrick J. Wong 
433808fc6c2SJulia Lawall static ssize_t num_temp_sensors_show(struct device *dev,
4342f22d5dfSDarrick J. Wong 				     struct device_attribute *devattr,
4352f22d5dfSDarrick J. Wong 				     char *buf)
4362f22d5dfSDarrick J. Wong {
4372f22d5dfSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
4382f22d5dfSDarrick J. Wong 	return sprintf(buf, "%d\n", data->num_temp_sensors);
4392f22d5dfSDarrick J. Wong }
4402f22d5dfSDarrick J. Wong 
441808fc6c2SJulia Lawall static ssize_t num_temp_sensors_store(struct device *dev,
4422f22d5dfSDarrick J. Wong 				      struct device_attribute *devattr,
443808fc6c2SJulia Lawall 				      const char *buf, size_t count)
4442f22d5dfSDarrick J. Wong {
44530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
4462f22d5dfSDarrick J. Wong 	long temp;
4472f22d5dfSDarrick J. Wong 
448179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
4492f22d5dfSDarrick J. Wong 		return -EINVAL;
4502f22d5dfSDarrick J. Wong 
4512a844c14SGuenter Roeck 	temp = clamp_val(temp, -1, 10);
4522f22d5dfSDarrick J. Wong 
4532f22d5dfSDarrick J. Wong 	mutex_lock(&data->lock);
4542f22d5dfSDarrick J. Wong 	data->num_temp_sensors = temp;
45589fac11cSDarrick J. Wong 	if (temp < 0)
45689fac11cSDarrick J. Wong 		data->temperatures_probed = 0;
4572f22d5dfSDarrick J. Wong 	mutex_unlock(&data->lock);
4582f22d5dfSDarrick J. Wong 
4592f22d5dfSDarrick J. Wong 	return count;
4602f22d5dfSDarrick J. Wong }
4612f22d5dfSDarrick J. Wong 
462*42291a5aSGuenter Roeck static ssize_t temp_min_show(struct device *dev,
463*42291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
4646f9703d0SDarrick J. Wong {
4656f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
4666f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
4676f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
4686f9703d0SDarrick J. Wong }
4696f9703d0SDarrick J. Wong 
470*42291a5aSGuenter Roeck static ssize_t temp_min_store(struct device *dev,
4716f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
472*42291a5aSGuenter Roeck 			      const char *buf, size_t count)
4736f9703d0SDarrick J. Wong {
4746f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
47530485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
47630485776SAxel Lin 	struct i2c_client *client = data->client;
47705a9bd46SDarrick J. Wong 	long temp;
47805a9bd46SDarrick J. Wong 
479179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
48005a9bd46SDarrick J. Wong 		return -EINVAL;
48105a9bd46SDarrick J. Wong 
48264bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
4838f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
4846f9703d0SDarrick J. Wong 
4856f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
4866f9703d0SDarrick J. Wong 	data->temp_min[attr->index] = temp;
4876f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
4886f9703d0SDarrick J. Wong 				  temp);
4896f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
4906f9703d0SDarrick J. Wong 
4916f9703d0SDarrick J. Wong 	return count;
4926f9703d0SDarrick J. Wong }
4936f9703d0SDarrick J. Wong 
494*42291a5aSGuenter Roeck static ssize_t temp_max_show(struct device *dev,
495*42291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
4966f9703d0SDarrick J. Wong {
4976f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
4986f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
4996f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
5006f9703d0SDarrick J. Wong }
5016f9703d0SDarrick J. Wong 
502*42291a5aSGuenter Roeck static ssize_t temp_max_store(struct device *dev,
5036f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
504*42291a5aSGuenter Roeck 			      const char *buf, size_t count)
5056f9703d0SDarrick J. Wong {
5066f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
50730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
50830485776SAxel Lin 	struct i2c_client *client = data->client;
50905a9bd46SDarrick J. Wong 	long temp;
51005a9bd46SDarrick J. Wong 
511179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
51205a9bd46SDarrick J. Wong 		return -EINVAL;
51305a9bd46SDarrick J. Wong 
51464bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
5158f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
5166f9703d0SDarrick J. Wong 
5176f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5186f9703d0SDarrick J. Wong 	data->temp_max[attr->index] = temp;
5196f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
5206f9703d0SDarrick J. Wong 				  temp);
5216f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
5226f9703d0SDarrick J. Wong 
5236f9703d0SDarrick J. Wong 	return count;
5246f9703d0SDarrick J. Wong }
5256f9703d0SDarrick J. Wong 
526*42291a5aSGuenter Roeck static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
5276f9703d0SDarrick J. Wong 			 char *buf)
5286f9703d0SDarrick J. Wong {
5296f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5306f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5316f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
5326f9703d0SDarrick J. Wong }
5336f9703d0SDarrick J. Wong 
534808fc6c2SJulia Lawall static ssize_t alarm_mask_show(struct device *dev,
5356f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
5366f9703d0SDarrick J. Wong 			   char *buf)
5376f9703d0SDarrick J. Wong {
5386f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5396f9703d0SDarrick J. Wong 
5406f9703d0SDarrick J. Wong 	return sprintf(buf, "%x\n", data->alarms_mask);
5416f9703d0SDarrick J. Wong }
5426f9703d0SDarrick J. Wong 
543808fc6c2SJulia Lawall static ssize_t alarm_mask_store(struct device *dev,
544feca3132SJoshua Scott 				struct device_attribute *devattr,
545808fc6c2SJulia Lawall 				const char *buf, size_t count)
546feca3132SJoshua Scott {
547feca3132SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
548feca3132SJoshua Scott 	long mask;
549feca3132SJoshua Scott 
550feca3132SJoshua Scott 	if (kstrtoul(buf, 0, &mask))
551feca3132SJoshua Scott 		return -EINVAL;
552feca3132SJoshua Scott 
553feca3132SJoshua Scott 	if (mask & ~0xffff)
554feca3132SJoshua Scott 		return -EINVAL;
555feca3132SJoshua Scott 
556feca3132SJoshua Scott 	mutex_lock(&data->lock);
557feca3132SJoshua Scott 	data->alarms_mask = mask;
558feca3132SJoshua Scott 	adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
559feca3132SJoshua Scott 	mutex_unlock(&data->lock);
560feca3132SJoshua Scott 
561feca3132SJoshua Scott 	return count;
562feca3132SJoshua Scott }
563feca3132SJoshua Scott 
564*42291a5aSGuenter Roeck static ssize_t fan_max_show(struct device *dev,
565*42291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
5666f9703d0SDarrick J. Wong {
5676f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5686f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5696f9703d0SDarrick J. Wong 
5706f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_max[attr->index]))
5716f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
5726f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
5736f9703d0SDarrick J. Wong 	else
5746f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
5756f9703d0SDarrick J. Wong }
5766f9703d0SDarrick J. Wong 
577*42291a5aSGuenter Roeck static ssize_t fan_max_store(struct device *dev,
5786f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
5796f9703d0SDarrick J. Wong 			     const char *buf, size_t count)
5806f9703d0SDarrick J. Wong {
5816f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
58230485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
58330485776SAxel Lin 	struct i2c_client *client = data->client;
58405a9bd46SDarrick J. Wong 	long temp;
5856f9703d0SDarrick J. Wong 
586179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
5876f9703d0SDarrick J. Wong 		return -EINVAL;
58805a9bd46SDarrick J. Wong 
5896f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
5902a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
5916f9703d0SDarrick J. Wong 
5926f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5936f9703d0SDarrick J. Wong 	data->fan_max[attr->index] = temp;
5946f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
5956f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
5966f9703d0SDarrick J. Wong 
5976f9703d0SDarrick J. Wong 	return count;
5986f9703d0SDarrick J. Wong }
5996f9703d0SDarrick J. Wong 
600*42291a5aSGuenter Roeck static ssize_t fan_min_show(struct device *dev,
601*42291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
6026f9703d0SDarrick J. Wong {
6036f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6046f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6056f9703d0SDarrick J. Wong 
6066f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_min[attr->index]))
6076f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6086f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
6096f9703d0SDarrick J. Wong 	else
6106f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6116f9703d0SDarrick J. Wong }
6126f9703d0SDarrick J. Wong 
613*42291a5aSGuenter Roeck static ssize_t fan_min_store(struct device *dev,
6146f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
6156f9703d0SDarrick J. Wong 			     const char *buf, size_t count)
6166f9703d0SDarrick J. Wong {
6176f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
61830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
61930485776SAxel Lin 	struct i2c_client *client = data->client;
62005a9bd46SDarrick J. Wong 	long temp;
6216f9703d0SDarrick J. Wong 
622179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
6236f9703d0SDarrick J. Wong 		return -EINVAL;
62405a9bd46SDarrick J. Wong 
6256f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
6262a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
6276f9703d0SDarrick J. Wong 
6286f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6296f9703d0SDarrick J. Wong 	data->fan_min[attr->index] = temp;
6306f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
6316f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6326f9703d0SDarrick J. Wong 
6336f9703d0SDarrick J. Wong 	return count;
6346f9703d0SDarrick J. Wong }
6356f9703d0SDarrick J. Wong 
636*42291a5aSGuenter Roeck static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
6376f9703d0SDarrick J. Wong 			char *buf)
6386f9703d0SDarrick J. Wong {
6396f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6406f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6416f9703d0SDarrick J. Wong 
6426f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan[attr->index]))
6436f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6446f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
6456f9703d0SDarrick J. Wong 	else
6466f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6476f9703d0SDarrick J. Wong }
6486f9703d0SDarrick J. Wong 
649*42291a5aSGuenter Roeck static ssize_t force_pwm_max_show(struct device *dev,
650*42291a5aSGuenter Roeck 				  struct device_attribute *devattr, char *buf)
6516f9703d0SDarrick J. Wong {
6526f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6536f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->force_pwm_max);
6546f9703d0SDarrick J. Wong }
6556f9703d0SDarrick J. Wong 
656*42291a5aSGuenter Roeck static ssize_t force_pwm_max_store(struct device *dev,
6576f9703d0SDarrick J. Wong 				   struct device_attribute *devattr,
658*42291a5aSGuenter Roeck 				   const char *buf, size_t count)
6596f9703d0SDarrick J. Wong {
66030485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
66130485776SAxel Lin 	struct i2c_client *client = data->client;
66205a9bd46SDarrick J. Wong 	long temp;
6636f9703d0SDarrick J. Wong 	u8 reg;
6646f9703d0SDarrick J. Wong 
665179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
66605a9bd46SDarrick J. Wong 		return -EINVAL;
66705a9bd46SDarrick J. Wong 
6686f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6696f9703d0SDarrick J. Wong 	data->force_pwm_max = temp;
6706f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
6716f9703d0SDarrick J. Wong 	if (temp)
6726f9703d0SDarrick J. Wong 		reg |= ADT7470_FSPD_MASK;
6736f9703d0SDarrick J. Wong 	else
6746f9703d0SDarrick J. Wong 		reg &= ~ADT7470_FSPD_MASK;
6756f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
6766f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6776f9703d0SDarrick J. Wong 
6786f9703d0SDarrick J. Wong 	return count;
6796f9703d0SDarrick J. Wong }
6806f9703d0SDarrick J. Wong 
681*42291a5aSGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *devattr,
6826f9703d0SDarrick J. Wong 			char *buf)
6836f9703d0SDarrick J. Wong {
6846f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6856f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6866f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm[attr->index]);
6876f9703d0SDarrick J. Wong }
6886f9703d0SDarrick J. Wong 
689*42291a5aSGuenter Roeck static ssize_t pwm_store(struct device *dev, struct device_attribute *devattr,
6906f9703d0SDarrick J. Wong 			 const char *buf, size_t count)
6916f9703d0SDarrick J. Wong {
6926f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
69330485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
69430485776SAxel Lin 	struct i2c_client *client = data->client;
69505a9bd46SDarrick J. Wong 	long temp;
69605a9bd46SDarrick J. Wong 
697179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
69805a9bd46SDarrick J. Wong 		return -EINVAL;
69905a9bd46SDarrick J. Wong 
7002a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
7016f9703d0SDarrick J. Wong 
7026f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
7036f9703d0SDarrick J. Wong 	data->pwm[attr->index] = temp;
7046f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
7056f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
7066f9703d0SDarrick J. Wong 
7076f9703d0SDarrick J. Wong 	return count;
7086f9703d0SDarrick J. Wong }
7096f9703d0SDarrick J. Wong 
710aa18cc91SJoshua Scott /* These are the valid PWM frequencies to the nearest Hz */
711aa18cc91SJoshua Scott static const int adt7470_freq_map[] = {
712aa18cc91SJoshua Scott 	11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
713aa18cc91SJoshua Scott };
714aa18cc91SJoshua Scott 
715808fc6c2SJulia Lawall static ssize_t pwm1_freq_show(struct device *dev,
716aa18cc91SJoshua Scott 			      struct device_attribute *devattr, char *buf)
717aa18cc91SJoshua Scott {
718aa18cc91SJoshua Scott 	struct adt7470_data *data = adt7470_update_device(dev);
719aa18cc91SJoshua Scott 	unsigned char cfg_reg_1;
720aa18cc91SJoshua Scott 	unsigned char cfg_reg_2;
721aa18cc91SJoshua Scott 	int index;
722aa18cc91SJoshua Scott 
723aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
724aa18cc91SJoshua Scott 	cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
725aa18cc91SJoshua Scott 	cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
726aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
727aa18cc91SJoshua Scott 
728aa18cc91SJoshua Scott 	index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
729aa18cc91SJoshua Scott 	if (!(cfg_reg_1 & ADT7470_CFG_LF))
730aa18cc91SJoshua Scott 		index += 8;
731aa18cc91SJoshua Scott 	if (index >= ARRAY_SIZE(adt7470_freq_map))
732aa18cc91SJoshua Scott 		index = ARRAY_SIZE(adt7470_freq_map) - 1;
733aa18cc91SJoshua Scott 
734aa18cc91SJoshua Scott 	return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
735aa18cc91SJoshua Scott }
736aa18cc91SJoshua Scott 
737808fc6c2SJulia Lawall static ssize_t pwm1_freq_store(struct device *dev,
738aa18cc91SJoshua Scott 			       struct device_attribute *devattr,
739aa18cc91SJoshua Scott 			       const char *buf, size_t count)
740aa18cc91SJoshua Scott {
741aa18cc91SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
742aa18cc91SJoshua Scott 	struct i2c_client *client = data->client;
743aa18cc91SJoshua Scott 	long freq;
744aa18cc91SJoshua Scott 	int index;
745aa18cc91SJoshua Scott 	int low_freq = ADT7470_CFG_LF;
746aa18cc91SJoshua Scott 	unsigned char val;
747aa18cc91SJoshua Scott 
748aa18cc91SJoshua Scott 	if (kstrtol(buf, 10, &freq))
749aa18cc91SJoshua Scott 		return -EINVAL;
750aa18cc91SJoshua Scott 
751aa18cc91SJoshua Scott 	/* Round the user value given to the closest available frequency */
752aa18cc91SJoshua Scott 	index = find_closest(freq, adt7470_freq_map,
753aa18cc91SJoshua Scott 			     ARRAY_SIZE(adt7470_freq_map));
754aa18cc91SJoshua Scott 
755aa18cc91SJoshua Scott 	if (index >= 8) {
756aa18cc91SJoshua Scott 		index -= 8;
757aa18cc91SJoshua Scott 		low_freq = 0;
758aa18cc91SJoshua Scott 	}
759aa18cc91SJoshua Scott 
760aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
761aa18cc91SJoshua Scott 	/* Configuration Register 1 */
762aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
763aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
764aa18cc91SJoshua Scott 				  (val & ~ADT7470_CFG_LF) | low_freq);
765aa18cc91SJoshua Scott 	/* Configuration Register 2 */
766aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
767aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
768aa18cc91SJoshua Scott 		(val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
769aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
770aa18cc91SJoshua Scott 
771aa18cc91SJoshua Scott 	return count;
772aa18cc91SJoshua Scott }
773aa18cc91SJoshua Scott 
774*42291a5aSGuenter Roeck static ssize_t pwm_max_show(struct device *dev,
775*42291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
7766f9703d0SDarrick J. Wong {
7776f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
7786f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
7796f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
7806f9703d0SDarrick J. Wong }
7816f9703d0SDarrick J. Wong 
782*42291a5aSGuenter Roeck static ssize_t pwm_max_store(struct device *dev,
7836f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
784*42291a5aSGuenter Roeck 			     const char *buf, size_t count)
7856f9703d0SDarrick J. Wong {
7866f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
78730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
78830485776SAxel Lin 	struct i2c_client *client = data->client;
78905a9bd46SDarrick J. Wong 	long temp;
79005a9bd46SDarrick J. Wong 
791179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
79205a9bd46SDarrick J. Wong 		return -EINVAL;
79305a9bd46SDarrick J. Wong 
7942a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
7956f9703d0SDarrick J. Wong 
7966f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
7976f9703d0SDarrick J. Wong 	data->pwm_max[attr->index] = temp;
7986f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
7996f9703d0SDarrick J. Wong 				  temp);
8006f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8016f9703d0SDarrick J. Wong 
8026f9703d0SDarrick J. Wong 	return count;
8036f9703d0SDarrick J. Wong }
8046f9703d0SDarrick J. Wong 
805*42291a5aSGuenter Roeck static ssize_t pwm_min_show(struct device *dev,
806*42291a5aSGuenter Roeck 			    struct device_attribute *devattr, char *buf)
8076f9703d0SDarrick J. Wong {
8086f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8096f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8106f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
8116f9703d0SDarrick J. Wong }
8126f9703d0SDarrick J. Wong 
813*42291a5aSGuenter Roeck static ssize_t pwm_min_store(struct device *dev,
8146f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
815*42291a5aSGuenter Roeck 			     const char *buf, size_t count)
8166f9703d0SDarrick J. Wong {
8176f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
81830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
81930485776SAxel Lin 	struct i2c_client *client = data->client;
82005a9bd46SDarrick J. Wong 	long temp;
82105a9bd46SDarrick J. Wong 
822179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
82305a9bd46SDarrick J. Wong 		return -EINVAL;
82405a9bd46SDarrick J. Wong 
8252a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
8266f9703d0SDarrick J. Wong 
8276f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8286f9703d0SDarrick J. Wong 	data->pwm_min[attr->index] = temp;
8296f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
8306f9703d0SDarrick J. Wong 				  temp);
8316f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8326f9703d0SDarrick J. Wong 
8336f9703d0SDarrick J. Wong 	return count;
8346f9703d0SDarrick J. Wong }
8356f9703d0SDarrick J. Wong 
836*42291a5aSGuenter Roeck static ssize_t pwm_tmax_show(struct device *dev,
837*42291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
8386f9703d0SDarrick J. Wong {
8396f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8406f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8416f9703d0SDarrick J. Wong 	/* the datasheet says that tmax = tmin + 20C */
8426f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
8436f9703d0SDarrick J. Wong }
8446f9703d0SDarrick J. Wong 
845*42291a5aSGuenter Roeck static ssize_t pwm_tmin_show(struct device *dev,
846*42291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
8476f9703d0SDarrick J. Wong {
8486f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8496f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8506f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
8516f9703d0SDarrick J. Wong }
8526f9703d0SDarrick J. Wong 
853*42291a5aSGuenter Roeck static ssize_t pwm_tmin_store(struct device *dev,
8546f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
855*42291a5aSGuenter Roeck 			      const char *buf, size_t count)
8566f9703d0SDarrick J. Wong {
8576f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
85830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
85930485776SAxel Lin 	struct i2c_client *client = data->client;
86005a9bd46SDarrick J. Wong 	long temp;
86105a9bd46SDarrick J. Wong 
862179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
86305a9bd46SDarrick J. Wong 		return -EINVAL;
86405a9bd46SDarrick J. Wong 
86564bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
8668f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
8676f9703d0SDarrick J. Wong 
8686f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8696f9703d0SDarrick J. Wong 	data->pwm_tmin[attr->index] = temp;
8706f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
8716f9703d0SDarrick J. Wong 				  temp);
8726f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8736f9703d0SDarrick J. Wong 
8746f9703d0SDarrick J. Wong 	return count;
8756f9703d0SDarrick J. Wong }
8766f9703d0SDarrick J. Wong 
877*42291a5aSGuenter Roeck static ssize_t pwm_auto_show(struct device *dev,
878*42291a5aSGuenter Roeck 			     struct device_attribute *devattr, char *buf)
8796f9703d0SDarrick J. Wong {
8806f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8816f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8826f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
8836f9703d0SDarrick J. Wong }
8846f9703d0SDarrick J. Wong 
885*42291a5aSGuenter Roeck static ssize_t pwm_auto_store(struct device *dev,
8866f9703d0SDarrick J. Wong 			      struct device_attribute *devattr,
887*42291a5aSGuenter Roeck 			      const char *buf, size_t count)
8886f9703d0SDarrick J. Wong {
8896f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
89030485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
89130485776SAxel Lin 	struct i2c_client *client = data->client;
8926f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
8936f9703d0SDarrick J. Wong 	int pwm_auto_reg_mask;
89405a9bd46SDarrick J. Wong 	long temp;
8956f9703d0SDarrick J. Wong 	u8 reg;
8966f9703d0SDarrick J. Wong 
897179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
89805a9bd46SDarrick J. Wong 		return -EINVAL;
89905a9bd46SDarrick J. Wong 
9006f9703d0SDarrick J. Wong 	if (attr->index % 2)
9016f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
9026f9703d0SDarrick J. Wong 	else
9036f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
9046f9703d0SDarrick J. Wong 
9056f9703d0SDarrick J. Wong 	if (temp != 2 && temp != 1)
9066f9703d0SDarrick J. Wong 		return -EINVAL;
9076f9703d0SDarrick J. Wong 	temp--;
9086f9703d0SDarrick J. Wong 
9096f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
9106f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
9116f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
9126f9703d0SDarrick J. Wong 	if (temp)
9136f9703d0SDarrick J. Wong 		reg |= pwm_auto_reg_mask;
9146f9703d0SDarrick J. Wong 	else
9156f9703d0SDarrick J. Wong 		reg &= ~pwm_auto_reg_mask;
9166f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
9176f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9186f9703d0SDarrick J. Wong 
9196f9703d0SDarrick J. Wong 	return count;
9206f9703d0SDarrick J. Wong }
9216f9703d0SDarrick J. Wong 
922*42291a5aSGuenter Roeck static ssize_t pwm_auto_temp_show(struct device *dev,
923*42291a5aSGuenter Roeck 				  struct device_attribute *devattr, char *buf)
9246f9703d0SDarrick J. Wong {
9256f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9266f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
9276f9703d0SDarrick J. Wong 	u8 ctrl = data->pwm_auto_temp[attr->index];
9286f9703d0SDarrick J. Wong 
9296f9703d0SDarrick J. Wong 	if (ctrl)
9306f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", 1 << (ctrl - 1));
9316f9703d0SDarrick J. Wong 	else
9326f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
9336f9703d0SDarrick J. Wong }
9346f9703d0SDarrick J. Wong 
9356f9703d0SDarrick J. Wong static int cvt_auto_temp(int input)
9366f9703d0SDarrick J. Wong {
9376f9703d0SDarrick J. Wong 	if (input == ADT7470_PWM_ALL_TEMPS)
9386f9703d0SDarrick J. Wong 		return 0;
939ce9c2f44SRobert P. J. Day 	if (input < 1 || !is_power_of_2(input))
9406f9703d0SDarrick J. Wong 		return -EINVAL;
9416f9703d0SDarrick J. Wong 	return ilog2(input) + 1;
9426f9703d0SDarrick J. Wong }
9436f9703d0SDarrick J. Wong 
944*42291a5aSGuenter Roeck static ssize_t pwm_auto_temp_store(struct device *dev,
9456f9703d0SDarrick J. Wong 				   struct device_attribute *devattr,
946*42291a5aSGuenter Roeck 				   const char *buf, size_t count)
9476f9703d0SDarrick J. Wong {
9486f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
94930485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
95030485776SAxel Lin 	struct i2c_client *client = data->client;
9516f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
95205a9bd46SDarrick J. Wong 	long temp;
9536f9703d0SDarrick J. Wong 	u8 reg;
9546f9703d0SDarrick J. Wong 
955179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
95605a9bd46SDarrick J. Wong 		return -EINVAL;
95705a9bd46SDarrick J. Wong 
95805a9bd46SDarrick J. Wong 	temp = cvt_auto_temp(temp);
9596f9703d0SDarrick J. Wong 	if (temp < 0)
9606f9703d0SDarrick J. Wong 		return temp;
9616f9703d0SDarrick J. Wong 
9626f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
9636f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
9646f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
9656f9703d0SDarrick J. Wong 
9666f9703d0SDarrick J. Wong 	if (!(attr->index % 2)) {
9676f9703d0SDarrick J. Wong 		reg &= 0xF;
9686f9703d0SDarrick J. Wong 		reg |= (temp << 4) & 0xF0;
9696f9703d0SDarrick J. Wong 	} else {
9706f9703d0SDarrick J. Wong 		reg &= 0xF0;
9716f9703d0SDarrick J. Wong 		reg |= temp & 0xF;
9726f9703d0SDarrick J. Wong 	}
9736f9703d0SDarrick J. Wong 
9746f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
9756f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9766f9703d0SDarrick J. Wong 
9776f9703d0SDarrick J. Wong 	return count;
9786f9703d0SDarrick J. Wong }
9796f9703d0SDarrick J. Wong 
980*42291a5aSGuenter Roeck static ssize_t alarm_show(struct device *dev,
981*42291a5aSGuenter Roeck 			  struct device_attribute *devattr, char *buf)
982fe03f28cSDarrick J. Wong {
983fe03f28cSDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
984fe03f28cSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
985fe03f28cSDarrick J. Wong 
986fe03f28cSDarrick J. Wong 	if (data->alarm & attr->index)
987fe03f28cSDarrick J. Wong 		return sprintf(buf, "1\n");
988fe03f28cSDarrick J. Wong 	else
989fe03f28cSDarrick J. Wong 		return sprintf(buf, "0\n");
990fe03f28cSDarrick J. Wong }
991fe03f28cSDarrick J. Wong 
992808fc6c2SJulia Lawall static DEVICE_ATTR_RW(alarm_mask);
993808fc6c2SJulia Lawall static DEVICE_ATTR_RW(num_temp_sensors);
994808fc6c2SJulia Lawall static DEVICE_ATTR_RW(auto_update_interval);
9956f9703d0SDarrick J. Wong 
996*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
997*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
998*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
999*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
1000*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp5_max, temp_max, 4);
1001*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp6_max, temp_max, 5);
1002*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp7_max, temp_max, 6);
1003*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp8_max, temp_max, 7);
1004*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp9_max, temp_max, 8);
1005*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp10_max, temp_max, 9);
10066f9703d0SDarrick J. Wong 
1007*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1008*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1009*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1010*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
1011*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp5_min, temp_min, 4);
1012*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp6_min, temp_min, 5);
1013*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp7_min, temp_min, 6);
1014*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp8_min, temp_min, 7);
1015*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp9_min, temp_min, 8);
1016*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp10_min, temp_min, 9);
10176f9703d0SDarrick J. Wong 
1018*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1019*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1020*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1021*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
1022*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_input, temp, 4);
1023*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_input, temp, 5);
1024*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_input, temp, 6);
1025*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_input, temp, 7);
1026*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_input, temp, 8);
1027*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_input, temp, 9);
10286f9703d0SDarrick J. Wong 
1029*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, ADT7470_R1T_ALARM);
1030*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, ADT7470_R2T_ALARM);
1031*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, ADT7470_R3T_ALARM);
1032*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp4_alarm, alarm, ADT7470_R4T_ALARM);
1033*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp5_alarm, alarm, ADT7470_R5T_ALARM);
1034*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp6_alarm, alarm, ADT7470_R6T_ALARM);
1035*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp7_alarm, alarm, ADT7470_R7T_ALARM);
1036*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp8_alarm, alarm, ALARM2(ADT7470_R8T_ALARM));
1037*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp9_alarm, alarm, ALARM2(ADT7470_R9T_ALARM));
1038*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp10_alarm, alarm, ALARM2(ADT7470_R10T_ALARM));
1039fe03f28cSDarrick J. Wong 
1040*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_max, fan_max, 0);
1041*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_max, fan_max, 1);
1042*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan3_max, fan_max, 2);
1043*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan4_max, fan_max, 3);
10446f9703d0SDarrick J. Wong 
1045*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1046*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1047*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1048*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
10496f9703d0SDarrick J. Wong 
1050*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
1051*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
1052*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_input, fan, 2);
1053*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_input, fan, 3);
10546f9703d0SDarrick J. Wong 
1055*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, ALARM2(ADT7470_FAN1_ALARM));
1056*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, ALARM2(ADT7470_FAN2_ALARM));
1057*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan3_alarm, alarm, ALARM2(ADT7470_FAN3_ALARM));
1058*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan4_alarm, alarm, ALARM2(ADT7470_FAN4_ALARM));
1059fe03f28cSDarrick J. Wong 
1060*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(force_pwm_max, force_pwm_max, 0);
10616f9703d0SDarrick J. Wong 
1062*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1063*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1064*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3, pwm, 2);
1065*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4, pwm, 3);
10666f9703d0SDarrick J. Wong 
1067808fc6c2SJulia Lawall static DEVICE_ATTR_RW(pwm1_freq);
1068aa18cc91SJoshua Scott 
1069*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_pwm, pwm_min, 0);
1070*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_pwm, pwm_min, 1);
1071*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_pwm, pwm_min, 2);
1072*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_pwm, pwm_min, 3);
10736f9703d0SDarrick J. Wong 
1074*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm_max, 0);
1075*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point2_pwm, pwm_max, 1);
1076*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point2_pwm, pwm_max, 2);
1077*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point2_pwm, pwm_max, 3);
10786f9703d0SDarrick J. Wong 
1079*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_tmin, 0);
1080*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_point1_temp, pwm_tmin, 1);
1081*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_point1_temp, pwm_tmin, 2);
1082*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_point1_temp, pwm_tmin, 3);
10836f9703d0SDarrick J. Wong 
1084*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_temp, pwm_tmax, 0);
1085*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm2_auto_point2_temp, pwm_tmax, 1);
1086*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm3_auto_point2_temp, pwm_tmax, 2);
1087*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(pwm4_auto_point2_temp, pwm_tmax, 3);
10886f9703d0SDarrick J. Wong 
1089*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_auto, 0);
1090*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_auto, 1);
1091*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_enable, pwm_auto, 2);
1092*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_enable, pwm_auto, 3);
10936f9703d0SDarrick J. Wong 
1094*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels_temp, pwm_auto_temp, 0);
1095*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels_temp, pwm_auto_temp, 1);
1096*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm3_auto_channels_temp, pwm_auto_temp, 2);
1097*42291a5aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm4_auto_channels_temp, pwm_auto_temp, 3);
10986f9703d0SDarrick J. Wong 
109930485776SAxel Lin static struct attribute *adt7470_attrs[] = {
1100fe03f28cSDarrick J. Wong 	&dev_attr_alarm_mask.attr,
11012f22d5dfSDarrick J. Wong 	&dev_attr_num_temp_sensors.attr,
110289fac11cSDarrick J. Wong 	&dev_attr_auto_update_interval.attr,
11036f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_max.dev_attr.attr,
11046f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_max.dev_attr.attr,
11056f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_max.dev_attr.attr,
11066f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_max.dev_attr.attr,
11076f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_max.dev_attr.attr,
11086f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_max.dev_attr.attr,
11096f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_max.dev_attr.attr,
11106f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_max.dev_attr.attr,
11116f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_max.dev_attr.attr,
11126f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_max.dev_attr.attr,
11136f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_min.dev_attr.attr,
11146f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_min.dev_attr.attr,
11156f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_min.dev_attr.attr,
11166f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_min.dev_attr.attr,
11176f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_min.dev_attr.attr,
11186f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_min.dev_attr.attr,
11196f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_min.dev_attr.attr,
11206f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_min.dev_attr.attr,
11216f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_min.dev_attr.attr,
11226f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_min.dev_attr.attr,
11236f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_input.dev_attr.attr,
11246f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_input.dev_attr.attr,
11256f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_input.dev_attr.attr,
11266f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_input.dev_attr.attr,
11276f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_input.dev_attr.attr,
11286f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_input.dev_attr.attr,
11296f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_input.dev_attr.attr,
11306f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_input.dev_attr.attr,
11316f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_input.dev_attr.attr,
11326f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_input.dev_attr.attr,
1133fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1134fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1135fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1136fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
1137fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
1138fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
1139fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
1140fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
1141fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
1142fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
11436f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_max.dev_attr.attr,
11446f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_max.dev_attr.attr,
11456f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_max.dev_attr.attr,
11466f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_max.dev_attr.attr,
11476f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_min.dev_attr.attr,
11486f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_min.dev_attr.attr,
11496f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_min.dev_attr.attr,
11506f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_min.dev_attr.attr,
11516f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_input.dev_attr.attr,
11526f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_input.dev_attr.attr,
11536f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_input.dev_attr.attr,
11546f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1155fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1156fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1157fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1158fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
11596f9703d0SDarrick J. Wong 	&sensor_dev_attr_force_pwm_max.dev_attr.attr,
11606f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1.dev_attr.attr,
1161aa18cc91SJoshua Scott 	&dev_attr_pwm1_freq.attr,
11626f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2.dev_attr.attr,
11636f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3.dev_attr.attr,
11646f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4.dev_attr.attr,
11656f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
11666f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
11676f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
11686f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
11696f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
11706f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
11716f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
11726f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
11736f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
11746f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
11756f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
11766f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
11776f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
11786f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
11796f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
11806f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
11816f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
11826f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
11836f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
11846f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
11856f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
11866f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
11876f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
11886f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
11896f9703d0SDarrick J. Wong 	NULL
11906f9703d0SDarrick J. Wong };
11916f9703d0SDarrick J. Wong 
119230485776SAxel Lin ATTRIBUTE_GROUPS(adt7470);
119330485776SAxel Lin 
1194008f1ca5SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
1195310ec792SJean Delvare static int adt7470_detect(struct i2c_client *client,
1196008f1ca5SJean Delvare 			  struct i2c_board_info *info)
11976f9703d0SDarrick J. Wong {
1198008f1ca5SJean Delvare 	struct i2c_adapter *adapter = client->adapter;
119952df6440SJean Delvare 	int vendor, device, revision;
12006f9703d0SDarrick J. Wong 
12016f9703d0SDarrick J. Wong 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1202008f1ca5SJean Delvare 		return -ENODEV;
12036f9703d0SDarrick J. Wong 
12046f9703d0SDarrick J. Wong 	vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1205008f1ca5SJean Delvare 	if (vendor != ADT7470_VENDOR)
1206008f1ca5SJean Delvare 		return -ENODEV;
12076f9703d0SDarrick J. Wong 
12086f9703d0SDarrick J. Wong 	device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1209008f1ca5SJean Delvare 	if (device != ADT7470_DEVICE)
1210008f1ca5SJean Delvare 		return -ENODEV;
12116f9703d0SDarrick J. Wong 
121252df6440SJean Delvare 	revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1213008f1ca5SJean Delvare 	if (revision != ADT7470_REVISION)
1214008f1ca5SJean Delvare 		return -ENODEV;
12156f9703d0SDarrick J. Wong 
1216008f1ca5SJean Delvare 	strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
12176f9703d0SDarrick J. Wong 
1218008f1ca5SJean Delvare 	return 0;
1219008f1ca5SJean Delvare }
1220008f1ca5SJean Delvare 
12219027d933SAxel Lin static void adt7470_init_client(struct i2c_client *client)
12229027d933SAxel Lin {
12239027d933SAxel Lin 	int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
12249027d933SAxel Lin 
12259027d933SAxel Lin 	if (reg < 0) {
12269027d933SAxel Lin 		dev_err(&client->dev, "cannot read configuration register\n");
12279027d933SAxel Lin 	} else {
12289027d933SAxel Lin 		/* start monitoring (and do a self-test) */
12299027d933SAxel Lin 		i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
12309027d933SAxel Lin 	}
12319027d933SAxel Lin }
12329027d933SAxel Lin 
1233008f1ca5SJean Delvare static int adt7470_probe(struct i2c_client *client,
1234008f1ca5SJean Delvare 			 const struct i2c_device_id *id)
1235008f1ca5SJean Delvare {
123630485776SAxel Lin 	struct device *dev = &client->dev;
1237008f1ca5SJean Delvare 	struct adt7470_data *data;
123830485776SAxel Lin 	struct device *hwmon_dev;
1239008f1ca5SJean Delvare 
124030485776SAxel Lin 	data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
12419cc7dcc5SGuenter Roeck 	if (!data)
12429cc7dcc5SGuenter Roeck 		return -ENOMEM;
1243008f1ca5SJean Delvare 
12442f22d5dfSDarrick J. Wong 	data->num_temp_sensors = -1;
124589fac11cSDarrick J. Wong 	data->auto_update_interval = AUTO_UPDATE_INTERVAL;
12462f22d5dfSDarrick J. Wong 
1247008f1ca5SJean Delvare 	i2c_set_clientdata(client, data);
124830485776SAxel Lin 	data->client = client;
1249008f1ca5SJean Delvare 	mutex_init(&data->lock);
12506f9703d0SDarrick J. Wong 
12516f9703d0SDarrick J. Wong 	dev_info(&client->dev, "%s chip found\n", client->name);
12526f9703d0SDarrick J. Wong 
12536f9703d0SDarrick J. Wong 	/* Initialize the ADT7470 chip */
12546f9703d0SDarrick J. Wong 	adt7470_init_client(client);
12556f9703d0SDarrick J. Wong 
12566f9703d0SDarrick J. Wong 	/* Register sysfs hooks */
125730485776SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
125830485776SAxel Lin 							   data,
125930485776SAxel Lin 							   adt7470_groups);
12606f9703d0SDarrick J. Wong 
126130485776SAxel Lin 	if (IS_ERR(hwmon_dev))
126230485776SAxel Lin 		return PTR_ERR(hwmon_dev);
12636f9703d0SDarrick J. Wong 
1264f170168bSKees Cook 	data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
126530485776SAxel Lin 					dev_name(hwmon_dev));
1266f7334b4cSAxel Lin 	if (IS_ERR(data->auto_update)) {
126730485776SAxel Lin 		return PTR_ERR(data->auto_update);
1268f7334b4cSAxel Lin 	}
126989fac11cSDarrick J. Wong 
12706f9703d0SDarrick J. Wong 	return 0;
12716f9703d0SDarrick J. Wong }
12726f9703d0SDarrick J. Wong 
1273008f1ca5SJean Delvare static int adt7470_remove(struct i2c_client *client)
12746f9703d0SDarrick J. Wong {
12756f9703d0SDarrick J. Wong 	struct adt7470_data *data = i2c_get_clientdata(client);
12766f9703d0SDarrick J. Wong 
127789fac11cSDarrick J. Wong 	kthread_stop(data->auto_update);
12786f9703d0SDarrick J. Wong 	return 0;
12796f9703d0SDarrick J. Wong }
12806f9703d0SDarrick J. Wong 
12819027d933SAxel Lin static const struct i2c_device_id adt7470_id[] = {
12829027d933SAxel Lin 	{ "adt7470", 0 },
12839027d933SAxel Lin 	{ }
12849027d933SAxel Lin };
12859027d933SAxel Lin MODULE_DEVICE_TABLE(i2c, adt7470_id);
12869027d933SAxel Lin 
12879027d933SAxel Lin static struct i2c_driver adt7470_driver = {
12889027d933SAxel Lin 	.class		= I2C_CLASS_HWMON,
12899027d933SAxel Lin 	.driver = {
12909027d933SAxel Lin 		.name	= "adt7470",
12919027d933SAxel Lin 	},
12929027d933SAxel Lin 	.probe		= adt7470_probe,
12939027d933SAxel Lin 	.remove		= adt7470_remove,
12949027d933SAxel Lin 	.id_table	= adt7470_id,
12959027d933SAxel Lin 	.detect		= adt7470_detect,
12969027d933SAxel Lin 	.address_list	= normal_i2c,
12979027d933SAxel Lin };
12989027d933SAxel Lin 
1299f0967eeaSAxel Lin module_i2c_driver(adt7470_driver);
13006f9703d0SDarrick J. Wong 
13015407e051SDarrick J. Wong MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
13026f9703d0SDarrick J. Wong MODULE_DESCRIPTION("ADT7470 driver");
13036f9703d0SDarrick J. Wong MODULE_LICENSE("GPL");
1304