xref: /openbmc/linux/drivers/hwmon/adt7470.c (revision 808fc6c2c3f3fe5ddeb3f2cb9ca7634ba3f4a2c0)
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 
406*808fc6c2SJulia 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 
414*808fc6c2SJulia Lawall static ssize_t auto_update_interval_store(struct device *dev,
41589fac11cSDarrick J. Wong 					  struct device_attribute *devattr,
416*808fc6c2SJulia 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 
433*808fc6c2SJulia 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 
441*808fc6c2SJulia Lawall static ssize_t num_temp_sensors_store(struct device *dev,
4422f22d5dfSDarrick J. Wong 				      struct device_attribute *devattr,
443*808fc6c2SJulia 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 
4626f9703d0SDarrick J. Wong static ssize_t show_temp_min(struct device *dev,
4636f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
4646f9703d0SDarrick J. Wong 			     char *buf)
4656f9703d0SDarrick J. Wong {
4666f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
4676f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
4686f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_min[attr->index]);
4696f9703d0SDarrick J. Wong }
4706f9703d0SDarrick J. Wong 
4716f9703d0SDarrick J. Wong static ssize_t set_temp_min(struct device *dev,
4726f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
4736f9703d0SDarrick J. Wong 			    const char *buf,
4746f9703d0SDarrick J. Wong 			    size_t count)
4756f9703d0SDarrick J. Wong {
4766f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
47730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
47830485776SAxel Lin 	struct i2c_client *client = data->client;
47905a9bd46SDarrick J. Wong 	long temp;
48005a9bd46SDarrick J. Wong 
481179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
48205a9bd46SDarrick J. Wong 		return -EINVAL;
48305a9bd46SDarrick J. Wong 
48464bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
4858f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
4866f9703d0SDarrick J. Wong 
4876f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
4886f9703d0SDarrick J. Wong 	data->temp_min[attr->index] = temp;
4896f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MIN_REG(attr->index),
4906f9703d0SDarrick J. Wong 				  temp);
4916f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
4926f9703d0SDarrick J. Wong 
4936f9703d0SDarrick J. Wong 	return count;
4946f9703d0SDarrick J. Wong }
4956f9703d0SDarrick J. Wong 
4966f9703d0SDarrick J. Wong static ssize_t show_temp_max(struct device *dev,
4976f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
4986f9703d0SDarrick J. Wong 			     char *buf)
4996f9703d0SDarrick J. Wong {
5006f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5016f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5026f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp_max[attr->index]);
5036f9703d0SDarrick J. Wong }
5046f9703d0SDarrick J. Wong 
5056f9703d0SDarrick J. Wong static ssize_t set_temp_max(struct device *dev,
5066f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
5076f9703d0SDarrick J. Wong 			    const char *buf,
5086f9703d0SDarrick J. Wong 			    size_t count)
5096f9703d0SDarrick J. Wong {
5106f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
51130485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
51230485776SAxel Lin 	struct i2c_client *client = data->client;
51305a9bd46SDarrick J. Wong 	long temp;
51405a9bd46SDarrick J. Wong 
515179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
51605a9bd46SDarrick J. Wong 		return -EINVAL;
51705a9bd46SDarrick J. Wong 
51864bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
5198f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
5206f9703d0SDarrick J. Wong 
5216f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5226f9703d0SDarrick J. Wong 	data->temp_max[attr->index] = temp;
5236f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_TEMP_MAX_REG(attr->index),
5246f9703d0SDarrick J. Wong 				  temp);
5256f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
5266f9703d0SDarrick J. Wong 
5276f9703d0SDarrick J. Wong 	return count;
5286f9703d0SDarrick J. Wong }
5296f9703d0SDarrick J. Wong 
5306f9703d0SDarrick J. Wong static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
5316f9703d0SDarrick J. Wong 			 char *buf)
5326f9703d0SDarrick J. Wong {
5336f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5346f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5356f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->temp[attr->index]);
5366f9703d0SDarrick J. Wong }
5376f9703d0SDarrick J. Wong 
538*808fc6c2SJulia Lawall static ssize_t alarm_mask_show(struct device *dev,
5396f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
5406f9703d0SDarrick J. Wong 			   char *buf)
5416f9703d0SDarrick J. Wong {
5426f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5436f9703d0SDarrick J. Wong 
5446f9703d0SDarrick J. Wong 	return sprintf(buf, "%x\n", data->alarms_mask);
5456f9703d0SDarrick J. Wong }
5466f9703d0SDarrick J. Wong 
547*808fc6c2SJulia Lawall static ssize_t alarm_mask_store(struct device *dev,
548feca3132SJoshua Scott 				struct device_attribute *devattr,
549*808fc6c2SJulia Lawall 				const char *buf, size_t count)
550feca3132SJoshua Scott {
551feca3132SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
552feca3132SJoshua Scott 	long mask;
553feca3132SJoshua Scott 
554feca3132SJoshua Scott 	if (kstrtoul(buf, 0, &mask))
555feca3132SJoshua Scott 		return -EINVAL;
556feca3132SJoshua Scott 
557feca3132SJoshua Scott 	if (mask & ~0xffff)
558feca3132SJoshua Scott 		return -EINVAL;
559feca3132SJoshua Scott 
560feca3132SJoshua Scott 	mutex_lock(&data->lock);
561feca3132SJoshua Scott 	data->alarms_mask = mask;
562feca3132SJoshua Scott 	adt7470_write_word_data(data->client, ADT7470_REG_ALARM1_MASK, mask);
563feca3132SJoshua Scott 	mutex_unlock(&data->lock);
564feca3132SJoshua Scott 
565feca3132SJoshua Scott 	return count;
566feca3132SJoshua Scott }
567feca3132SJoshua Scott 
5686f9703d0SDarrick J. Wong static ssize_t show_fan_max(struct device *dev,
5696f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
5706f9703d0SDarrick J. Wong 			    char *buf)
5716f9703d0SDarrick J. Wong {
5726f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
5736f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
5746f9703d0SDarrick J. Wong 
5756f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_max[attr->index]))
5766f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
5776f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
5786f9703d0SDarrick J. Wong 	else
5796f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
5806f9703d0SDarrick J. Wong }
5816f9703d0SDarrick J. Wong 
5826f9703d0SDarrick J. Wong static ssize_t set_fan_max(struct device *dev,
5836f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
5846f9703d0SDarrick J. Wong 			   const char *buf, size_t count)
5856f9703d0SDarrick J. Wong {
5866f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
58730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
58830485776SAxel Lin 	struct i2c_client *client = data->client;
58905a9bd46SDarrick J. Wong 	long temp;
5906f9703d0SDarrick J. Wong 
591179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
5926f9703d0SDarrick J. Wong 		return -EINVAL;
59305a9bd46SDarrick J. Wong 
5946f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
5952a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
5966f9703d0SDarrick J. Wong 
5976f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
5986f9703d0SDarrick J. Wong 	data->fan_max[attr->index] = temp;
5996f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MAX(attr->index), temp);
6006f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6016f9703d0SDarrick J. Wong 
6026f9703d0SDarrick J. Wong 	return count;
6036f9703d0SDarrick J. Wong }
6046f9703d0SDarrick J. Wong 
6056f9703d0SDarrick J. Wong static ssize_t show_fan_min(struct device *dev,
6066f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
6076f9703d0SDarrick J. Wong 			    char *buf)
6086f9703d0SDarrick J. Wong {
6096f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6106f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6116f9703d0SDarrick J. Wong 
6126f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan_min[attr->index]))
6136f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6146f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
6156f9703d0SDarrick J. Wong 	else
6166f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6176f9703d0SDarrick J. Wong }
6186f9703d0SDarrick J. Wong 
6196f9703d0SDarrick J. Wong static ssize_t set_fan_min(struct device *dev,
6206f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
6216f9703d0SDarrick J. Wong 			   const char *buf, size_t count)
6226f9703d0SDarrick J. Wong {
6236f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
62430485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
62530485776SAxel Lin 	struct i2c_client *client = data->client;
62605a9bd46SDarrick J. Wong 	long temp;
6276f9703d0SDarrick J. Wong 
628179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp) || !temp)
6296f9703d0SDarrick J. Wong 		return -EINVAL;
63005a9bd46SDarrick J. Wong 
6316f9703d0SDarrick J. Wong 	temp = FAN_RPM_TO_PERIOD(temp);
6322a844c14SGuenter Roeck 	temp = clamp_val(temp, 1, 65534);
6336f9703d0SDarrick J. Wong 
6346f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6356f9703d0SDarrick J. Wong 	data->fan_min[attr->index] = temp;
6366f9703d0SDarrick J. Wong 	adt7470_write_word_data(client, ADT7470_REG_FAN_MIN(attr->index), temp);
6376f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6386f9703d0SDarrick J. Wong 
6396f9703d0SDarrick J. Wong 	return count;
6406f9703d0SDarrick J. Wong }
6416f9703d0SDarrick J. Wong 
6426f9703d0SDarrick J. Wong static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
6436f9703d0SDarrick J. Wong 			char *buf)
6446f9703d0SDarrick J. Wong {
6456f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6466f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6476f9703d0SDarrick J. Wong 
6486f9703d0SDarrick J. Wong 	if (FAN_DATA_VALID(data->fan[attr->index]))
6496f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n",
6506f9703d0SDarrick J. Wong 			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
6516f9703d0SDarrick J. Wong 	else
6526f9703d0SDarrick J. Wong 		return sprintf(buf, "0\n");
6536f9703d0SDarrick J. Wong }
6546f9703d0SDarrick J. Wong 
6556f9703d0SDarrick J. Wong static ssize_t show_force_pwm_max(struct device *dev,
6566f9703d0SDarrick J. Wong 				  struct device_attribute *devattr,
6576f9703d0SDarrick J. Wong 				  char *buf)
6586f9703d0SDarrick J. Wong {
6596f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6606f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->force_pwm_max);
6616f9703d0SDarrick J. Wong }
6626f9703d0SDarrick J. Wong 
6636f9703d0SDarrick J. Wong static ssize_t set_force_pwm_max(struct device *dev,
6646f9703d0SDarrick J. Wong 				 struct device_attribute *devattr,
6656f9703d0SDarrick J. Wong 				 const char *buf,
6666f9703d0SDarrick J. Wong 				 size_t count)
6676f9703d0SDarrick J. Wong {
66830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
66930485776SAxel Lin 	struct i2c_client *client = data->client;
67005a9bd46SDarrick J. Wong 	long temp;
6716f9703d0SDarrick J. Wong 	u8 reg;
6726f9703d0SDarrick J. Wong 
673179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
67405a9bd46SDarrick J. Wong 		return -EINVAL;
67505a9bd46SDarrick J. Wong 
6766f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
6776f9703d0SDarrick J. Wong 	data->force_pwm_max = temp;
6786f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
6796f9703d0SDarrick J. Wong 	if (temp)
6806f9703d0SDarrick J. Wong 		reg |= ADT7470_FSPD_MASK;
6816f9703d0SDarrick J. Wong 	else
6826f9703d0SDarrick J. Wong 		reg &= ~ADT7470_FSPD_MASK;
6836f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg);
6846f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
6856f9703d0SDarrick J. Wong 
6866f9703d0SDarrick J. Wong 	return count;
6876f9703d0SDarrick J. Wong }
6886f9703d0SDarrick J. Wong 
6896f9703d0SDarrick J. Wong static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
6906f9703d0SDarrick J. Wong 			char *buf)
6916f9703d0SDarrick J. Wong {
6926f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
6936f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
6946f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm[attr->index]);
6956f9703d0SDarrick J. Wong }
6966f9703d0SDarrick J. Wong 
6976f9703d0SDarrick J. Wong static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
6986f9703d0SDarrick J. Wong 			const char *buf, size_t count)
6996f9703d0SDarrick J. Wong {
7006f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
70130485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
70230485776SAxel Lin 	struct i2c_client *client = data->client;
70305a9bd46SDarrick J. Wong 	long temp;
70405a9bd46SDarrick J. Wong 
705179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
70605a9bd46SDarrick J. Wong 		return -EINVAL;
70705a9bd46SDarrick J. Wong 
7082a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
7096f9703d0SDarrick J. Wong 
7106f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
7116f9703d0SDarrick J. Wong 	data->pwm[attr->index] = temp;
7126f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM(attr->index), temp);
7136f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
7146f9703d0SDarrick J. Wong 
7156f9703d0SDarrick J. Wong 	return count;
7166f9703d0SDarrick J. Wong }
7176f9703d0SDarrick J. Wong 
718aa18cc91SJoshua Scott /* These are the valid PWM frequencies to the nearest Hz */
719aa18cc91SJoshua Scott static const int adt7470_freq_map[] = {
720aa18cc91SJoshua Scott 	11, 15, 22, 29, 35, 44, 59, 88, 1400, 22500
721aa18cc91SJoshua Scott };
722aa18cc91SJoshua Scott 
723*808fc6c2SJulia Lawall static ssize_t pwm1_freq_show(struct device *dev,
724aa18cc91SJoshua Scott 			      struct device_attribute *devattr, char *buf)
725aa18cc91SJoshua Scott {
726aa18cc91SJoshua Scott 	struct adt7470_data *data = adt7470_update_device(dev);
727aa18cc91SJoshua Scott 	unsigned char cfg_reg_1;
728aa18cc91SJoshua Scott 	unsigned char cfg_reg_2;
729aa18cc91SJoshua Scott 	int index;
730aa18cc91SJoshua Scott 
731aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
732aa18cc91SJoshua Scott 	cfg_reg_1 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG);
733aa18cc91SJoshua Scott 	cfg_reg_2 = i2c_smbus_read_byte_data(data->client, ADT7470_REG_CFG_2);
734aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
735aa18cc91SJoshua Scott 
736aa18cc91SJoshua Scott 	index = (cfg_reg_2 & ADT7470_FREQ_MASK) >> ADT7470_FREQ_SHIFT;
737aa18cc91SJoshua Scott 	if (!(cfg_reg_1 & ADT7470_CFG_LF))
738aa18cc91SJoshua Scott 		index += 8;
739aa18cc91SJoshua Scott 	if (index >= ARRAY_SIZE(adt7470_freq_map))
740aa18cc91SJoshua Scott 		index = ARRAY_SIZE(adt7470_freq_map) - 1;
741aa18cc91SJoshua Scott 
742aa18cc91SJoshua Scott 	return scnprintf(buf, PAGE_SIZE, "%d\n", adt7470_freq_map[index]);
743aa18cc91SJoshua Scott }
744aa18cc91SJoshua Scott 
745*808fc6c2SJulia Lawall static ssize_t pwm1_freq_store(struct device *dev,
746aa18cc91SJoshua Scott 			       struct device_attribute *devattr,
747aa18cc91SJoshua Scott 			       const char *buf, size_t count)
748aa18cc91SJoshua Scott {
749aa18cc91SJoshua Scott 	struct adt7470_data *data = dev_get_drvdata(dev);
750aa18cc91SJoshua Scott 	struct i2c_client *client = data->client;
751aa18cc91SJoshua Scott 	long freq;
752aa18cc91SJoshua Scott 	int index;
753aa18cc91SJoshua Scott 	int low_freq = ADT7470_CFG_LF;
754aa18cc91SJoshua Scott 	unsigned char val;
755aa18cc91SJoshua Scott 
756aa18cc91SJoshua Scott 	if (kstrtol(buf, 10, &freq))
757aa18cc91SJoshua Scott 		return -EINVAL;
758aa18cc91SJoshua Scott 
759aa18cc91SJoshua Scott 	/* Round the user value given to the closest available frequency */
760aa18cc91SJoshua Scott 	index = find_closest(freq, adt7470_freq_map,
761aa18cc91SJoshua Scott 			     ARRAY_SIZE(adt7470_freq_map));
762aa18cc91SJoshua Scott 
763aa18cc91SJoshua Scott 	if (index >= 8) {
764aa18cc91SJoshua Scott 		index -= 8;
765aa18cc91SJoshua Scott 		low_freq = 0;
766aa18cc91SJoshua Scott 	}
767aa18cc91SJoshua Scott 
768aa18cc91SJoshua Scott 	mutex_lock(&data->lock);
769aa18cc91SJoshua Scott 	/* Configuration Register 1 */
770aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
771aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG,
772aa18cc91SJoshua Scott 				  (val & ~ADT7470_CFG_LF) | low_freq);
773aa18cc91SJoshua Scott 	/* Configuration Register 2 */
774aa18cc91SJoshua Scott 	val = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG_2);
775aa18cc91SJoshua Scott 	i2c_smbus_write_byte_data(client, ADT7470_REG_CFG_2,
776aa18cc91SJoshua Scott 		(val & ~ADT7470_FREQ_MASK) | (index << ADT7470_FREQ_SHIFT));
777aa18cc91SJoshua Scott 	mutex_unlock(&data->lock);
778aa18cc91SJoshua Scott 
779aa18cc91SJoshua Scott 	return count;
780aa18cc91SJoshua Scott }
781aa18cc91SJoshua Scott 
7826f9703d0SDarrick J. Wong static ssize_t show_pwm_max(struct device *dev,
7836f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
7846f9703d0SDarrick J. Wong 			    char *buf)
7856f9703d0SDarrick J. Wong {
7866f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
7876f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
7886f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
7896f9703d0SDarrick J. Wong }
7906f9703d0SDarrick J. Wong 
7916f9703d0SDarrick J. Wong static ssize_t set_pwm_max(struct device *dev,
7926f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
7936f9703d0SDarrick J. Wong 			   const char *buf,
7946f9703d0SDarrick J. Wong 			   size_t count)
7956f9703d0SDarrick J. Wong {
7966f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
79730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
79830485776SAxel Lin 	struct i2c_client *client = data->client;
79905a9bd46SDarrick J. Wong 	long temp;
80005a9bd46SDarrick J. Wong 
801179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
80205a9bd46SDarrick J. Wong 		return -EINVAL;
80305a9bd46SDarrick J. Wong 
8042a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
8056f9703d0SDarrick J. Wong 
8066f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8076f9703d0SDarrick J. Wong 	data->pwm_max[attr->index] = temp;
8086f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MAX(attr->index),
8096f9703d0SDarrick J. Wong 				  temp);
8106f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8116f9703d0SDarrick J. Wong 
8126f9703d0SDarrick J. Wong 	return count;
8136f9703d0SDarrick J. Wong }
8146f9703d0SDarrick J. Wong 
8156f9703d0SDarrick J. Wong static ssize_t show_pwm_min(struct device *dev,
8166f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
8176f9703d0SDarrick J. Wong 			    char *buf)
8186f9703d0SDarrick J. Wong {
8196f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8206f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8216f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
8226f9703d0SDarrick J. Wong }
8236f9703d0SDarrick J. Wong 
8246f9703d0SDarrick J. Wong static ssize_t set_pwm_min(struct device *dev,
8256f9703d0SDarrick J. Wong 			   struct device_attribute *devattr,
8266f9703d0SDarrick J. Wong 			   const char *buf,
8276f9703d0SDarrick J. Wong 			   size_t count)
8286f9703d0SDarrick J. Wong {
8296f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
83030485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
83130485776SAxel Lin 	struct i2c_client *client = data->client;
83205a9bd46SDarrick J. Wong 	long temp;
83305a9bd46SDarrick J. Wong 
834179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
83505a9bd46SDarrick J. Wong 		return -EINVAL;
83605a9bd46SDarrick J. Wong 
8372a844c14SGuenter Roeck 	temp = clamp_val(temp, 0, 255);
8386f9703d0SDarrick J. Wong 
8396f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8406f9703d0SDarrick J. Wong 	data->pwm_min[attr->index] = temp;
8416f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_MIN(attr->index),
8426f9703d0SDarrick J. Wong 				  temp);
8436f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8446f9703d0SDarrick J. Wong 
8456f9703d0SDarrick J. Wong 	return count;
8466f9703d0SDarrick J. Wong }
8476f9703d0SDarrick J. Wong 
8486f9703d0SDarrick J. Wong static ssize_t show_pwm_tmax(struct device *dev,
8496f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
8506f9703d0SDarrick J. Wong 			     char *buf)
8516f9703d0SDarrick J. Wong {
8526f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8536f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8546f9703d0SDarrick J. Wong 	/* the datasheet says that tmax = tmin + 20C */
8556f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * (20 + data->pwm_tmin[attr->index]));
8566f9703d0SDarrick J. Wong }
8576f9703d0SDarrick J. Wong 
8586f9703d0SDarrick J. Wong static ssize_t show_pwm_tmin(struct device *dev,
8596f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
8606f9703d0SDarrick J. Wong 			     char *buf)
8616f9703d0SDarrick J. Wong {
8626f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8636f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8646f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1000 * data->pwm_tmin[attr->index]);
8656f9703d0SDarrick J. Wong }
8666f9703d0SDarrick J. Wong 
8676f9703d0SDarrick J. Wong static ssize_t set_pwm_tmin(struct device *dev,
8686f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
8696f9703d0SDarrick J. Wong 			    const char *buf,
8706f9703d0SDarrick J. Wong 			    size_t count)
8716f9703d0SDarrick J. Wong {
8726f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
87330485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
87430485776SAxel Lin 	struct i2c_client *client = data->client;
87505a9bd46SDarrick J. Wong 	long temp;
87605a9bd46SDarrick J. Wong 
877179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
87805a9bd46SDarrick J. Wong 		return -EINVAL;
87905a9bd46SDarrick J. Wong 
88064bd708aSGuenter Roeck 	temp = clamp_val(temp, -128000, 127000);
8818f8c1fb0SDarrick J. Wong 	temp = DIV_ROUND_CLOSEST(temp, 1000);
8826f9703d0SDarrick J. Wong 
8836f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
8846f9703d0SDarrick J. Wong 	data->pwm_tmin[attr->index] = temp;
8856f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, ADT7470_REG_PWM_TMIN(attr->index),
8866f9703d0SDarrick J. Wong 				  temp);
8876f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
8886f9703d0SDarrick J. Wong 
8896f9703d0SDarrick J. Wong 	return count;
8906f9703d0SDarrick J. Wong }
8916f9703d0SDarrick J. Wong 
8926f9703d0SDarrick J. Wong static ssize_t show_pwm_auto(struct device *dev,
8936f9703d0SDarrick J. Wong 			     struct device_attribute *devattr,
8946f9703d0SDarrick J. Wong 			     char *buf)
8956f9703d0SDarrick J. Wong {
8966f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
8976f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
8986f9703d0SDarrick J. Wong 	return sprintf(buf, "%d\n", 1 + data->pwm_automatic[attr->index]);
8996f9703d0SDarrick J. Wong }
9006f9703d0SDarrick J. Wong 
9016f9703d0SDarrick J. Wong static ssize_t set_pwm_auto(struct device *dev,
9026f9703d0SDarrick J. Wong 			    struct device_attribute *devattr,
9036f9703d0SDarrick J. Wong 			    const char *buf,
9046f9703d0SDarrick J. Wong 			    size_t count)
9056f9703d0SDarrick J. Wong {
9066f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
90730485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
90830485776SAxel Lin 	struct i2c_client *client = data->client;
9096f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_CFG(attr->index);
9106f9703d0SDarrick J. Wong 	int pwm_auto_reg_mask;
91105a9bd46SDarrick J. Wong 	long temp;
9126f9703d0SDarrick J. Wong 	u8 reg;
9136f9703d0SDarrick J. Wong 
914179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
91505a9bd46SDarrick J. Wong 		return -EINVAL;
91605a9bd46SDarrick J. Wong 
9176f9703d0SDarrick J. Wong 	if (attr->index % 2)
9186f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
9196f9703d0SDarrick J. Wong 	else
9206f9703d0SDarrick J. Wong 		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
9216f9703d0SDarrick J. Wong 
9226f9703d0SDarrick J. Wong 	if (temp != 2 && temp != 1)
9236f9703d0SDarrick J. Wong 		return -EINVAL;
9246f9703d0SDarrick J. Wong 	temp--;
9256f9703d0SDarrick J. Wong 
9266f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
9276f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
9286f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
9296f9703d0SDarrick J. Wong 	if (temp)
9306f9703d0SDarrick J. Wong 		reg |= pwm_auto_reg_mask;
9316f9703d0SDarrick J. Wong 	else
9326f9703d0SDarrick J. Wong 		reg &= ~pwm_auto_reg_mask;
9336f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
9346f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9356f9703d0SDarrick J. Wong 
9366f9703d0SDarrick J. Wong 	return count;
9376f9703d0SDarrick J. Wong }
9386f9703d0SDarrick J. Wong 
9396f9703d0SDarrick J. Wong static ssize_t show_pwm_auto_temp(struct device *dev,
9406f9703d0SDarrick J. Wong 				  struct device_attribute *devattr,
9416f9703d0SDarrick J. Wong 				  char *buf)
9426f9703d0SDarrick J. Wong {
9436f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9446f9703d0SDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
9456f9703d0SDarrick J. Wong 	u8 ctrl = data->pwm_auto_temp[attr->index];
9466f9703d0SDarrick J. Wong 
9476f9703d0SDarrick J. Wong 	if (ctrl)
9486f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", 1 << (ctrl - 1));
9496f9703d0SDarrick J. Wong 	else
9506f9703d0SDarrick J. Wong 		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
9516f9703d0SDarrick J. Wong }
9526f9703d0SDarrick J. Wong 
9536f9703d0SDarrick J. Wong static int cvt_auto_temp(int input)
9546f9703d0SDarrick J. Wong {
9556f9703d0SDarrick J. Wong 	if (input == ADT7470_PWM_ALL_TEMPS)
9566f9703d0SDarrick J. Wong 		return 0;
957ce9c2f44SRobert P. J. Day 	if (input < 1 || !is_power_of_2(input))
9586f9703d0SDarrick J. Wong 		return -EINVAL;
9596f9703d0SDarrick J. Wong 	return ilog2(input) + 1;
9606f9703d0SDarrick J. Wong }
9616f9703d0SDarrick J. Wong 
9626f9703d0SDarrick J. Wong static ssize_t set_pwm_auto_temp(struct device *dev,
9636f9703d0SDarrick J. Wong 				 struct device_attribute *devattr,
9646f9703d0SDarrick J. Wong 				 const char *buf,
9656f9703d0SDarrick J. Wong 				 size_t count)
9666f9703d0SDarrick J. Wong {
9676f9703d0SDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
96830485776SAxel Lin 	struct adt7470_data *data = dev_get_drvdata(dev);
96930485776SAxel Lin 	struct i2c_client *client = data->client;
9706f9703d0SDarrick J. Wong 	int pwm_auto_reg = ADT7470_REG_PWM_AUTO_TEMP(attr->index);
97105a9bd46SDarrick J. Wong 	long temp;
9726f9703d0SDarrick J. Wong 	u8 reg;
9736f9703d0SDarrick J. Wong 
974179c4fdbSFrans Meulenbroeks 	if (kstrtol(buf, 10, &temp))
97505a9bd46SDarrick J. Wong 		return -EINVAL;
97605a9bd46SDarrick J. Wong 
97705a9bd46SDarrick J. Wong 	temp = cvt_auto_temp(temp);
9786f9703d0SDarrick J. Wong 	if (temp < 0)
9796f9703d0SDarrick J. Wong 		return temp;
9806f9703d0SDarrick J. Wong 
9816f9703d0SDarrick J. Wong 	mutex_lock(&data->lock);
9826f9703d0SDarrick J. Wong 	data->pwm_automatic[attr->index] = temp;
9836f9703d0SDarrick J. Wong 	reg = i2c_smbus_read_byte_data(client, pwm_auto_reg);
9846f9703d0SDarrick J. Wong 
9856f9703d0SDarrick J. Wong 	if (!(attr->index % 2)) {
9866f9703d0SDarrick J. Wong 		reg &= 0xF;
9876f9703d0SDarrick J. Wong 		reg |= (temp << 4) & 0xF0;
9886f9703d0SDarrick J. Wong 	} else {
9896f9703d0SDarrick J. Wong 		reg &= 0xF0;
9906f9703d0SDarrick J. Wong 		reg |= temp & 0xF;
9916f9703d0SDarrick J. Wong 	}
9926f9703d0SDarrick J. Wong 
9936f9703d0SDarrick J. Wong 	i2c_smbus_write_byte_data(client, pwm_auto_reg, reg);
9946f9703d0SDarrick J. Wong 	mutex_unlock(&data->lock);
9956f9703d0SDarrick J. Wong 
9966f9703d0SDarrick J. Wong 	return count;
9976f9703d0SDarrick J. Wong }
9986f9703d0SDarrick J. Wong 
999fe03f28cSDarrick J. Wong static ssize_t show_alarm(struct device *dev,
1000fe03f28cSDarrick J. Wong 			  struct device_attribute *devattr,
1001fe03f28cSDarrick J. Wong 			  char *buf)
1002fe03f28cSDarrick J. Wong {
1003fe03f28cSDarrick J. Wong 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1004fe03f28cSDarrick J. Wong 	struct adt7470_data *data = adt7470_update_device(dev);
1005fe03f28cSDarrick J. Wong 
1006fe03f28cSDarrick J. Wong 	if (data->alarm & attr->index)
1007fe03f28cSDarrick J. Wong 		return sprintf(buf, "1\n");
1008fe03f28cSDarrick J. Wong 	else
1009fe03f28cSDarrick J. Wong 		return sprintf(buf, "0\n");
1010fe03f28cSDarrick J. Wong }
1011fe03f28cSDarrick J. Wong 
1012*808fc6c2SJulia Lawall static DEVICE_ATTR_RW(alarm_mask);
1013*808fc6c2SJulia Lawall static DEVICE_ATTR_RW(num_temp_sensors);
1014*808fc6c2SJulia Lawall static DEVICE_ATTR_RW(auto_update_interval);
10156f9703d0SDarrick J. Wong 
10166f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
10176f9703d0SDarrick J. Wong 		    set_temp_max, 0);
10186f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
10196f9703d0SDarrick J. Wong 		    set_temp_max, 1);
10206f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
10216f9703d0SDarrick J. Wong 		    set_temp_max, 2);
10226f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
10236f9703d0SDarrick J. Wong 		    set_temp_max, 3);
10246f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_temp_max,
10256f9703d0SDarrick J. Wong 		    set_temp_max, 4);
10266f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp6_max, S_IWUSR | S_IRUGO, show_temp_max,
10276f9703d0SDarrick J. Wong 		    set_temp_max, 5);
10286f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp7_max, S_IWUSR | S_IRUGO, show_temp_max,
10296f9703d0SDarrick J. Wong 		    set_temp_max, 6);
10306f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp8_max, S_IWUSR | S_IRUGO, show_temp_max,
10316f9703d0SDarrick J. Wong 		    set_temp_max, 7);
10326f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp9_max, S_IWUSR | S_IRUGO, show_temp_max,
10336f9703d0SDarrick J. Wong 		    set_temp_max, 8);
10346f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp10_max, S_IWUSR | S_IRUGO, show_temp_max,
10356f9703d0SDarrick J. Wong 		    set_temp_max, 9);
10366f9703d0SDarrick J. Wong 
10376f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
10386f9703d0SDarrick J. Wong 		    set_temp_min, 0);
10396f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
10406f9703d0SDarrick J. Wong 		    set_temp_min, 1);
10416f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
10426f9703d0SDarrick J. Wong 		    set_temp_min, 2);
10436f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
10446f9703d0SDarrick J. Wong 		    set_temp_min, 3);
10456f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp5_min, S_IWUSR | S_IRUGO, show_temp_min,
10466f9703d0SDarrick J. Wong 		    set_temp_min, 4);
10476f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp6_min, S_IWUSR | S_IRUGO, show_temp_min,
10486f9703d0SDarrick J. Wong 		    set_temp_min, 5);
10496f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp7_min, S_IWUSR | S_IRUGO, show_temp_min,
10506f9703d0SDarrick J. Wong 		    set_temp_min, 6);
10516f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp8_min, S_IWUSR | S_IRUGO, show_temp_min,
10526f9703d0SDarrick J. Wong 		    set_temp_min, 7);
10536f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp9_min, S_IWUSR | S_IRUGO, show_temp_min,
10546f9703d0SDarrick J. Wong 		    set_temp_min, 8);
10556f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp10_min, S_IWUSR | S_IRUGO, show_temp_min,
10566f9703d0SDarrick J. Wong 		    set_temp_min, 9);
10576f9703d0SDarrick J. Wong 
10586f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
10596f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
10606f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
10616f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
10626f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
10636f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_temp, NULL, 5);
10646f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_temp, NULL, 6);
10656f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_temp, NULL, 7);
10666f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_temp, NULL, 8);
10676f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, show_temp, NULL, 9);
10686f9703d0SDarrick J. Wong 
1069fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1070fe03f28cSDarrick J. Wong 			  ADT7470_R1T_ALARM);
1071fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1072fe03f28cSDarrick J. Wong 			  ADT7470_R2T_ALARM);
1073fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1074fe03f28cSDarrick J. Wong 			  ADT7470_R3T_ALARM);
1075fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1076fe03f28cSDarrick J. Wong 			  ADT7470_R4T_ALARM);
1077fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL,
1078fe03f28cSDarrick J. Wong 			  ADT7470_R5T_ALARM);
1079fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL,
1080fe03f28cSDarrick J. Wong 			  ADT7470_R6T_ALARM);
1081fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp7_alarm, S_IRUGO, show_alarm, NULL,
1082fe03f28cSDarrick J. Wong 			  ADT7470_R7T_ALARM);
1083fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp8_alarm, S_IRUGO, show_alarm, NULL,
1084fe03f28cSDarrick J. Wong 			  ALARM2(ADT7470_R8T_ALARM));
1085fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp9_alarm, S_IRUGO, show_alarm, NULL,
1086fe03f28cSDarrick J. Wong 			  ALARM2(ADT7470_R9T_ALARM));
1087fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(temp10_alarm, S_IRUGO, show_alarm, NULL,
1088fe03f28cSDarrick J. Wong 			  ALARM2(ADT7470_R10T_ALARM));
1089fe03f28cSDarrick J. Wong 
10906f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan1_max, S_IWUSR | S_IRUGO, show_fan_max,
10916f9703d0SDarrick J. Wong 		    set_fan_max, 0);
10926f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan2_max, S_IWUSR | S_IRUGO, show_fan_max,
10936f9703d0SDarrick J. Wong 		    set_fan_max, 1);
10946f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan3_max, S_IWUSR | S_IRUGO, show_fan_max,
10956f9703d0SDarrick J. Wong 		    set_fan_max, 2);
10966f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan4_max, S_IWUSR | S_IRUGO, show_fan_max,
10976f9703d0SDarrick J. Wong 		    set_fan_max, 3);
10986f9703d0SDarrick J. Wong 
10996f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
11006f9703d0SDarrick J. Wong 		    set_fan_min, 0);
11016f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
11026f9703d0SDarrick J. Wong 		    set_fan_min, 1);
11036f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
11046f9703d0SDarrick J. Wong 		    set_fan_min, 2);
11056f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
11066f9703d0SDarrick J. Wong 		    set_fan_min, 3);
11076f9703d0SDarrick J. Wong 
11086f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
11096f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
11106f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
11116f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
11126f9703d0SDarrick J. Wong 
1113fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
1114fe03f28cSDarrick J. Wong 			  ALARM2(ADT7470_FAN1_ALARM));
1115fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
1116fe03f28cSDarrick J. Wong 			  ALARM2(ADT7470_FAN2_ALARM));
1117fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
1118fe03f28cSDarrick J. Wong 			  ALARM2(ADT7470_FAN3_ALARM));
1119fe03f28cSDarrick J. Wong static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
1120fe03f28cSDarrick J. Wong 			  ALARM2(ADT7470_FAN4_ALARM));
1121fe03f28cSDarrick J. Wong 
11226f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
11236f9703d0SDarrick J. Wong 		    show_force_pwm_max, set_force_pwm_max, 0);
11246f9703d0SDarrick J. Wong 
11256f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
11266f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
11276f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
11286f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
11296f9703d0SDarrick J. Wong 
1130*808fc6c2SJulia Lawall static DEVICE_ATTR_RW(pwm1_freq);
1131aa18cc91SJoshua Scott 
11326f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
11336f9703d0SDarrick J. Wong 		    show_pwm_min, set_pwm_min, 0);
11346f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
11356f9703d0SDarrick J. Wong 		    show_pwm_min, set_pwm_min, 1);
11366f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
11376f9703d0SDarrick J. Wong 		    show_pwm_min, set_pwm_min, 2);
11386f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
11396f9703d0SDarrick J. Wong 		    show_pwm_min, set_pwm_min, 3);
11406f9703d0SDarrick J. Wong 
11416f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
11426f9703d0SDarrick J. Wong 		    show_pwm_max, set_pwm_max, 0);
11436f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
11446f9703d0SDarrick J. Wong 		    show_pwm_max, set_pwm_max, 1);
11456f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
11466f9703d0SDarrick J. Wong 		    show_pwm_max, set_pwm_max, 2);
11476f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
11486f9703d0SDarrick J. Wong 		    show_pwm_max, set_pwm_max, 3);
11496f9703d0SDarrick J. Wong 
11506f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm1_auto_point1_temp, S_IWUSR | S_IRUGO,
11516f9703d0SDarrick J. Wong 		    show_pwm_tmin, set_pwm_tmin, 0);
11526f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm2_auto_point1_temp, S_IWUSR | S_IRUGO,
11536f9703d0SDarrick J. Wong 		    show_pwm_tmin, set_pwm_tmin, 1);
11546f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm3_auto_point1_temp, S_IWUSR | S_IRUGO,
11556f9703d0SDarrick J. Wong 		    show_pwm_tmin, set_pwm_tmin, 2);
11566f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm4_auto_point1_temp, S_IWUSR | S_IRUGO,
11576f9703d0SDarrick J. Wong 		    show_pwm_tmin, set_pwm_tmin, 3);
11586f9703d0SDarrick J. Wong 
11596f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm1_auto_point2_temp, S_IRUGO, show_pwm_tmax,
11606f9703d0SDarrick J. Wong 		    NULL, 0);
11616f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm2_auto_point2_temp, S_IRUGO, show_pwm_tmax,
11626f9703d0SDarrick J. Wong 		    NULL, 1);
11636f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm3_auto_point2_temp, S_IRUGO, show_pwm_tmax,
11646f9703d0SDarrick J. Wong 		    NULL, 2);
11656f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm4_auto_point2_temp, S_IRUGO, show_pwm_tmax,
11666f9703d0SDarrick J. Wong 		    NULL, 3);
11676f9703d0SDarrick J. Wong 
11686f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
11696f9703d0SDarrick J. Wong 		    set_pwm_auto, 0);
11706f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
11716f9703d0SDarrick J. Wong 		    set_pwm_auto, 1);
11726f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
11736f9703d0SDarrick J. Wong 		    set_pwm_auto, 2);
11746f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
11756f9703d0SDarrick J. Wong 		    set_pwm_auto, 3);
11766f9703d0SDarrick J. Wong 
11776f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
11786f9703d0SDarrick J. Wong 		    show_pwm_auto_temp, set_pwm_auto_temp, 0);
11796f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
11806f9703d0SDarrick J. Wong 		    show_pwm_auto_temp, set_pwm_auto_temp, 1);
11816f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
11826f9703d0SDarrick J. Wong 		    show_pwm_auto_temp, set_pwm_auto_temp, 2);
11836f9703d0SDarrick J. Wong static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
11846f9703d0SDarrick J. Wong 		    show_pwm_auto_temp, set_pwm_auto_temp, 3);
11856f9703d0SDarrick J. Wong 
118630485776SAxel Lin static struct attribute *adt7470_attrs[] = {
1187fe03f28cSDarrick J. Wong 	&dev_attr_alarm_mask.attr,
11882f22d5dfSDarrick J. Wong 	&dev_attr_num_temp_sensors.attr,
118989fac11cSDarrick J. Wong 	&dev_attr_auto_update_interval.attr,
11906f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_max.dev_attr.attr,
11916f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_max.dev_attr.attr,
11926f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_max.dev_attr.attr,
11936f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_max.dev_attr.attr,
11946f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_max.dev_attr.attr,
11956f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_max.dev_attr.attr,
11966f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_max.dev_attr.attr,
11976f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_max.dev_attr.attr,
11986f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_max.dev_attr.attr,
11996f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_max.dev_attr.attr,
12006f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_min.dev_attr.attr,
12016f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_min.dev_attr.attr,
12026f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_min.dev_attr.attr,
12036f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_min.dev_attr.attr,
12046f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_min.dev_attr.attr,
12056f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_min.dev_attr.attr,
12066f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_min.dev_attr.attr,
12076f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_min.dev_attr.attr,
12086f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_min.dev_attr.attr,
12096f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_min.dev_attr.attr,
12106f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp1_input.dev_attr.attr,
12116f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp2_input.dev_attr.attr,
12126f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp3_input.dev_attr.attr,
12136f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp4_input.dev_attr.attr,
12146f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp5_input.dev_attr.attr,
12156f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp6_input.dev_attr.attr,
12166f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp7_input.dev_attr.attr,
12176f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp8_input.dev_attr.attr,
12186f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp9_input.dev_attr.attr,
12196f9703d0SDarrick J. Wong 	&sensor_dev_attr_temp10_input.dev_attr.attr,
1220fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1221fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1222fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1223fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp4_alarm.dev_attr.attr,
1224fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp5_alarm.dev_attr.attr,
1225fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp6_alarm.dev_attr.attr,
1226fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp7_alarm.dev_attr.attr,
1227fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp8_alarm.dev_attr.attr,
1228fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp9_alarm.dev_attr.attr,
1229fe03f28cSDarrick J. Wong 	&sensor_dev_attr_temp10_alarm.dev_attr.attr,
12306f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_max.dev_attr.attr,
12316f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_max.dev_attr.attr,
12326f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_max.dev_attr.attr,
12336f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_max.dev_attr.attr,
12346f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_min.dev_attr.attr,
12356f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_min.dev_attr.attr,
12366f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_min.dev_attr.attr,
12376f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_min.dev_attr.attr,
12386f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan1_input.dev_attr.attr,
12396f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan2_input.dev_attr.attr,
12406f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan3_input.dev_attr.attr,
12416f9703d0SDarrick J. Wong 	&sensor_dev_attr_fan4_input.dev_attr.attr,
1242fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1243fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1244fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1245fe03f28cSDarrick J. Wong 	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
12466f9703d0SDarrick J. Wong 	&sensor_dev_attr_force_pwm_max.dev_attr.attr,
12476f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1.dev_attr.attr,
1248aa18cc91SJoshua Scott 	&dev_attr_pwm1_freq.attr,
12496f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2.dev_attr.attr,
12506f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3.dev_attr.attr,
12516f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4.dev_attr.attr,
12526f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
12536f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
12546f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
12556f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
12566f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
12576f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
12586f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
12596f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
12606f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
12616f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
12626f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
12636f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr,
12646f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
12656f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
12666f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
12676f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
12686f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
12696f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_enable.dev_attr.attr,
12706f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_enable.dev_attr.attr,
12716f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_enable.dev_attr.attr,
12726f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
12736f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
12746f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
12756f9703d0SDarrick J. Wong 	&sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
12766f9703d0SDarrick J. Wong 	NULL
12776f9703d0SDarrick J. Wong };
12786f9703d0SDarrick J. Wong 
127930485776SAxel Lin ATTRIBUTE_GROUPS(adt7470);
128030485776SAxel Lin 
1281008f1ca5SJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
1282310ec792SJean Delvare static int adt7470_detect(struct i2c_client *client,
1283008f1ca5SJean Delvare 			  struct i2c_board_info *info)
12846f9703d0SDarrick J. Wong {
1285008f1ca5SJean Delvare 	struct i2c_adapter *adapter = client->adapter;
128652df6440SJean Delvare 	int vendor, device, revision;
12876f9703d0SDarrick J. Wong 
12886f9703d0SDarrick J. Wong 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1289008f1ca5SJean Delvare 		return -ENODEV;
12906f9703d0SDarrick J. Wong 
12916f9703d0SDarrick J. Wong 	vendor = i2c_smbus_read_byte_data(client, ADT7470_REG_VENDOR);
1292008f1ca5SJean Delvare 	if (vendor != ADT7470_VENDOR)
1293008f1ca5SJean Delvare 		return -ENODEV;
12946f9703d0SDarrick J. Wong 
12956f9703d0SDarrick J. Wong 	device = i2c_smbus_read_byte_data(client, ADT7470_REG_DEVICE);
1296008f1ca5SJean Delvare 	if (device != ADT7470_DEVICE)
1297008f1ca5SJean Delvare 		return -ENODEV;
12986f9703d0SDarrick J. Wong 
129952df6440SJean Delvare 	revision = i2c_smbus_read_byte_data(client, ADT7470_REG_REVISION);
1300008f1ca5SJean Delvare 	if (revision != ADT7470_REVISION)
1301008f1ca5SJean Delvare 		return -ENODEV;
13026f9703d0SDarrick J. Wong 
1303008f1ca5SJean Delvare 	strlcpy(info->type, "adt7470", I2C_NAME_SIZE);
13046f9703d0SDarrick J. Wong 
1305008f1ca5SJean Delvare 	return 0;
1306008f1ca5SJean Delvare }
1307008f1ca5SJean Delvare 
13089027d933SAxel Lin static void adt7470_init_client(struct i2c_client *client)
13099027d933SAxel Lin {
13109027d933SAxel Lin 	int reg = i2c_smbus_read_byte_data(client, ADT7470_REG_CFG);
13119027d933SAxel Lin 
13129027d933SAxel Lin 	if (reg < 0) {
13139027d933SAxel Lin 		dev_err(&client->dev, "cannot read configuration register\n");
13149027d933SAxel Lin 	} else {
13159027d933SAxel Lin 		/* start monitoring (and do a self-test) */
13169027d933SAxel Lin 		i2c_smbus_write_byte_data(client, ADT7470_REG_CFG, reg | 3);
13179027d933SAxel Lin 	}
13189027d933SAxel Lin }
13199027d933SAxel Lin 
1320008f1ca5SJean Delvare static int adt7470_probe(struct i2c_client *client,
1321008f1ca5SJean Delvare 			 const struct i2c_device_id *id)
1322008f1ca5SJean Delvare {
132330485776SAxel Lin 	struct device *dev = &client->dev;
1324008f1ca5SJean Delvare 	struct adt7470_data *data;
132530485776SAxel Lin 	struct device *hwmon_dev;
1326008f1ca5SJean Delvare 
132730485776SAxel Lin 	data = devm_kzalloc(dev, sizeof(struct adt7470_data), GFP_KERNEL);
13289cc7dcc5SGuenter Roeck 	if (!data)
13299cc7dcc5SGuenter Roeck 		return -ENOMEM;
1330008f1ca5SJean Delvare 
13312f22d5dfSDarrick J. Wong 	data->num_temp_sensors = -1;
133289fac11cSDarrick J. Wong 	data->auto_update_interval = AUTO_UPDATE_INTERVAL;
13332f22d5dfSDarrick J. Wong 
1334008f1ca5SJean Delvare 	i2c_set_clientdata(client, data);
133530485776SAxel Lin 	data->client = client;
1336008f1ca5SJean Delvare 	mutex_init(&data->lock);
13376f9703d0SDarrick J. Wong 
13386f9703d0SDarrick J. Wong 	dev_info(&client->dev, "%s chip found\n", client->name);
13396f9703d0SDarrick J. Wong 
13406f9703d0SDarrick J. Wong 	/* Initialize the ADT7470 chip */
13416f9703d0SDarrick J. Wong 	adt7470_init_client(client);
13426f9703d0SDarrick J. Wong 
13436f9703d0SDarrick J. Wong 	/* Register sysfs hooks */
134430485776SAxel Lin 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
134530485776SAxel Lin 							   data,
134630485776SAxel Lin 							   adt7470_groups);
13476f9703d0SDarrick J. Wong 
134830485776SAxel Lin 	if (IS_ERR(hwmon_dev))
134930485776SAxel Lin 		return PTR_ERR(hwmon_dev);
13506f9703d0SDarrick J. Wong 
1351f170168bSKees Cook 	data->auto_update = kthread_run(adt7470_update_thread, client, "%s",
135230485776SAxel Lin 					dev_name(hwmon_dev));
1353f7334b4cSAxel Lin 	if (IS_ERR(data->auto_update)) {
135430485776SAxel Lin 		return PTR_ERR(data->auto_update);
1355f7334b4cSAxel Lin 	}
135689fac11cSDarrick J. Wong 
13576f9703d0SDarrick J. Wong 	return 0;
13586f9703d0SDarrick J. Wong }
13596f9703d0SDarrick J. Wong 
1360008f1ca5SJean Delvare static int adt7470_remove(struct i2c_client *client)
13616f9703d0SDarrick J. Wong {
13626f9703d0SDarrick J. Wong 	struct adt7470_data *data = i2c_get_clientdata(client);
13636f9703d0SDarrick J. Wong 
136489fac11cSDarrick J. Wong 	kthread_stop(data->auto_update);
13656f9703d0SDarrick J. Wong 	return 0;
13666f9703d0SDarrick J. Wong }
13676f9703d0SDarrick J. Wong 
13689027d933SAxel Lin static const struct i2c_device_id adt7470_id[] = {
13699027d933SAxel Lin 	{ "adt7470", 0 },
13709027d933SAxel Lin 	{ }
13719027d933SAxel Lin };
13729027d933SAxel Lin MODULE_DEVICE_TABLE(i2c, adt7470_id);
13739027d933SAxel Lin 
13749027d933SAxel Lin static struct i2c_driver adt7470_driver = {
13759027d933SAxel Lin 	.class		= I2C_CLASS_HWMON,
13769027d933SAxel Lin 	.driver = {
13779027d933SAxel Lin 		.name	= "adt7470",
13789027d933SAxel Lin 	},
13799027d933SAxel Lin 	.probe		= adt7470_probe,
13809027d933SAxel Lin 	.remove		= adt7470_remove,
13819027d933SAxel Lin 	.id_table	= adt7470_id,
13829027d933SAxel Lin 	.detect		= adt7470_detect,
13839027d933SAxel Lin 	.address_list	= normal_i2c,
13849027d933SAxel Lin };
13859027d933SAxel Lin 
1386f0967eeaSAxel Lin module_i2c_driver(adt7470_driver);
13876f9703d0SDarrick J. Wong 
13885407e051SDarrick J. Wong MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
13896f9703d0SDarrick J. Wong MODULE_DESCRIPTION("ADT7470 driver");
13906f9703d0SDarrick J. Wong MODULE_LICENSE("GPL");
1391