xref: /openbmc/linux/drivers/hwmon/adt7411.c (revision 1975d167)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d84ca5b3SWolfram Sang /*
3d84ca5b3SWolfram Sang  *  Driver for the ADT7411 (I2C/SPI 8 channel 10 bit ADC & temperature-sensor)
4d84ca5b3SWolfram Sang  *
5d84ca5b3SWolfram Sang  *  Copyright (C) 2008, 2010 Pengutronix
6d84ca5b3SWolfram Sang  *
7a95da110SMichael Walle  *  TODO: SPI, use power-down mode for suspend?, interrupt handling?
8d84ca5b3SWolfram Sang  */
9d84ca5b3SWolfram Sang 
10d84ca5b3SWolfram Sang #include <linux/kernel.h>
11d84ca5b3SWolfram Sang #include <linux/module.h>
12d84ca5b3SWolfram Sang #include <linux/init.h>
13d84ca5b3SWolfram Sang #include <linux/err.h>
14d84ca5b3SWolfram Sang #include <linux/mutex.h>
15d84ca5b3SWolfram Sang #include <linux/jiffies.h>
16d84ca5b3SWolfram Sang #include <linux/i2c.h>
17d84ca5b3SWolfram Sang #include <linux/hwmon.h>
18d84ca5b3SWolfram Sang #include <linux/hwmon-sysfs.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20d84ca5b3SWolfram Sang 
219bb2d47dSMichael Walle #define ADT7411_REG_STAT_1			0x00
229bb2d47dSMichael Walle #define ADT7411_STAT_1_INT_TEMP_HIGH		BIT(0)
239bb2d47dSMichael Walle #define ADT7411_STAT_1_INT_TEMP_LOW		BIT(1)
249bb2d47dSMichael Walle #define ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1	BIT(2)
259bb2d47dSMichael Walle #define ADT7411_STAT_1_EXT_TEMP_LOW		BIT(3)
269bb2d47dSMichael Walle #define ADT7411_STAT_1_EXT_TEMP_FAULT		BIT(4)
279bb2d47dSMichael Walle #define ADT7411_STAT_1_AIN2			BIT(5)
289bb2d47dSMichael Walle #define ADT7411_STAT_1_AIN3			BIT(6)
299bb2d47dSMichael Walle #define ADT7411_STAT_1_AIN4			BIT(7)
309bb2d47dSMichael Walle #define ADT7411_REG_STAT_2			0x01
319bb2d47dSMichael Walle #define ADT7411_STAT_2_AIN5			BIT(0)
329bb2d47dSMichael Walle #define ADT7411_STAT_2_AIN6			BIT(1)
339bb2d47dSMichael Walle #define ADT7411_STAT_2_AIN7			BIT(2)
349bb2d47dSMichael Walle #define ADT7411_STAT_2_AIN8			BIT(3)
359bb2d47dSMichael Walle #define ADT7411_STAT_2_VDD			BIT(4)
36d84ca5b3SWolfram Sang #define ADT7411_REG_INT_TEMP_VDD_LSB		0x03
37d84ca5b3SWolfram Sang #define ADT7411_REG_EXT_TEMP_AIN14_LSB		0x04
38d84ca5b3SWolfram Sang #define ADT7411_REG_VDD_MSB			0x06
39d84ca5b3SWolfram Sang #define ADT7411_REG_INT_TEMP_MSB		0x07
40d84ca5b3SWolfram Sang #define ADT7411_REG_EXT_TEMP_AIN1_MSB		0x08
41d84ca5b3SWolfram Sang 
42d84ca5b3SWolfram Sang #define ADT7411_REG_CFG1			0x18
439bb2d47dSMichael Walle #define ADT7411_CFG1_START_MONITOR		BIT(0)
449bb2d47dSMichael Walle #define ADT7411_CFG1_RESERVED_BIT1		BIT(1)
459bb2d47dSMichael Walle #define ADT7411_CFG1_EXT_TDM			BIT(2)
469bb2d47dSMichael Walle #define ADT7411_CFG1_RESERVED_BIT3		BIT(3)
47d84ca5b3SWolfram Sang 
48d84ca5b3SWolfram Sang #define ADT7411_REG_CFG2			0x19
499bb2d47dSMichael Walle #define ADT7411_CFG2_DISABLE_AVG		BIT(5)
50d84ca5b3SWolfram Sang 
51d84ca5b3SWolfram Sang #define ADT7411_REG_CFG3			0x1a
529bb2d47dSMichael Walle #define ADT7411_CFG3_ADC_CLK_225		BIT(0)
539bb2d47dSMichael Walle #define ADT7411_CFG3_RESERVED_BIT1		BIT(1)
549bb2d47dSMichael Walle #define ADT7411_CFG3_RESERVED_BIT2		BIT(2)
559bb2d47dSMichael Walle #define ADT7411_CFG3_RESERVED_BIT3		BIT(3)
569bb2d47dSMichael Walle #define ADT7411_CFG3_REF_VDD			BIT(4)
579bb2d47dSMichael Walle 
589bb2d47dSMichael Walle #define ADT7411_REG_VDD_HIGH			0x23
599bb2d47dSMichael Walle #define ADT7411_REG_VDD_LOW			0x24
609bb2d47dSMichael Walle #define ADT7411_REG_TEMP_HIGH(nr)		(0x25 + 2 * (nr))
619bb2d47dSMichael Walle #define ADT7411_REG_TEMP_LOW(nr)		(0x26 + 2 * (nr))
629bb2d47dSMichael Walle #define ADT7411_REG_IN_HIGH(nr)		((nr) > 1 \
639bb2d47dSMichael Walle 						  ? 0x2b + 2 * ((nr)-2) \
649bb2d47dSMichael Walle 						  : 0x27)
659bb2d47dSMichael Walle #define ADT7411_REG_IN_LOW(nr)			((nr) > 1 \
669bb2d47dSMichael Walle 						  ? 0x2c + 2 * ((nr)-2) \
679bb2d47dSMichael Walle 						  : 0x28)
68d84ca5b3SWolfram Sang 
69d84ca5b3SWolfram Sang #define ADT7411_REG_DEVICE_ID			0x4d
70d84ca5b3SWolfram Sang #define ADT7411_REG_MANUFACTURER_ID		0x4e
71d84ca5b3SWolfram Sang 
72d84ca5b3SWolfram Sang #define ADT7411_DEVICE_ID			0x2
73d84ca5b3SWolfram Sang #define ADT7411_MANUFACTURER_ID			0x41
74d84ca5b3SWolfram Sang 
75d84ca5b3SWolfram Sang static const unsigned short normal_i2c[] = { 0x48, 0x4a, 0x4b, I2C_CLIENT_END };
76d84ca5b3SWolfram Sang 
779bb2d47dSMichael Walle static const u8 adt7411_in_alarm_reg[] = {
789bb2d47dSMichael Walle 	ADT7411_REG_STAT_2,
799bb2d47dSMichael Walle 	ADT7411_REG_STAT_1,
809bb2d47dSMichael Walle 	ADT7411_REG_STAT_1,
819bb2d47dSMichael Walle 	ADT7411_REG_STAT_1,
829bb2d47dSMichael Walle 	ADT7411_REG_STAT_1,
839bb2d47dSMichael Walle 	ADT7411_REG_STAT_2,
849bb2d47dSMichael Walle 	ADT7411_REG_STAT_2,
859bb2d47dSMichael Walle 	ADT7411_REG_STAT_2,
869bb2d47dSMichael Walle 	ADT7411_REG_STAT_2,
879bb2d47dSMichael Walle };
889bb2d47dSMichael Walle 
899bb2d47dSMichael Walle static const u8 adt7411_in_alarm_bits[] = {
909bb2d47dSMichael Walle 	ADT7411_STAT_2_VDD,
919bb2d47dSMichael Walle 	ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1,
929bb2d47dSMichael Walle 	ADT7411_STAT_1_AIN2,
939bb2d47dSMichael Walle 	ADT7411_STAT_1_AIN3,
949bb2d47dSMichael Walle 	ADT7411_STAT_1_AIN4,
959bb2d47dSMichael Walle 	ADT7411_STAT_2_AIN5,
969bb2d47dSMichael Walle 	ADT7411_STAT_2_AIN6,
979bb2d47dSMichael Walle 	ADT7411_STAT_2_AIN7,
989bb2d47dSMichael Walle 	ADT7411_STAT_2_AIN8,
999bb2d47dSMichael Walle };
1009bb2d47dSMichael Walle 
101d84ca5b3SWolfram Sang struct adt7411_data {
102d84ca5b3SWolfram Sang 	struct mutex device_lock;	/* for "atomic" device accesses */
10323244985SWolfram Sang 	struct mutex update_lock;
104d84ca5b3SWolfram Sang 	unsigned long next_update;
1051b109c49SMichael Walle 	long vref_cached;
106eac83cd9SAxel Lin 	struct i2c_client *client;
107a95da110SMichael Walle 	bool use_ext_temp;
108d84ca5b3SWolfram Sang };
109d84ca5b3SWolfram Sang 
110d84ca5b3SWolfram Sang /*
111d84ca5b3SWolfram Sang  * When reading a register containing (up to 4) lsb, all associated
112d84ca5b3SWolfram Sang  * msb-registers get locked by the hardware. After _one_ of those msb is read,
113d84ca5b3SWolfram Sang  * _all_ are unlocked. In order to use this locking correctly, reading lsb/msb
114d84ca5b3SWolfram Sang  * is protected here with a mutex, too.
115d84ca5b3SWolfram Sang  */
adt7411_read_10_bit(struct i2c_client * client,u8 lsb_reg,u8 msb_reg,u8 lsb_shift)116d84ca5b3SWolfram Sang static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
117d84ca5b3SWolfram Sang 				u8 msb_reg, u8 lsb_shift)
118d84ca5b3SWolfram Sang {
119d84ca5b3SWolfram Sang 	struct adt7411_data *data = i2c_get_clientdata(client);
120d84ca5b3SWolfram Sang 	int val, tmp;
121d84ca5b3SWolfram Sang 
122d84ca5b3SWolfram Sang 	mutex_lock(&data->device_lock);
123d84ca5b3SWolfram Sang 
124d84ca5b3SWolfram Sang 	val = i2c_smbus_read_byte_data(client, lsb_reg);
125d84ca5b3SWolfram Sang 	if (val < 0)
126d84ca5b3SWolfram Sang 		goto exit_unlock;
127d84ca5b3SWolfram Sang 
128d84ca5b3SWolfram Sang 	tmp = (val >> lsb_shift) & 3;
129d84ca5b3SWolfram Sang 	val = i2c_smbus_read_byte_data(client, msb_reg);
130d84ca5b3SWolfram Sang 
131d84ca5b3SWolfram Sang 	if (val >= 0)
132d84ca5b3SWolfram Sang 		val = (val << 2) | tmp;
133d84ca5b3SWolfram Sang 
134d84ca5b3SWolfram Sang  exit_unlock:
135d84ca5b3SWolfram Sang 	mutex_unlock(&data->device_lock);
136d84ca5b3SWolfram Sang 
137d84ca5b3SWolfram Sang 	return val;
138d84ca5b3SWolfram Sang }
139d84ca5b3SWolfram Sang 
adt7411_modify_bit(struct i2c_client * client,u8 reg,u8 bit,bool flag)140d84ca5b3SWolfram Sang static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
141d84ca5b3SWolfram Sang 				bool flag)
142d84ca5b3SWolfram Sang {
143d84ca5b3SWolfram Sang 	struct adt7411_data *data = i2c_get_clientdata(client);
144d84ca5b3SWolfram Sang 	int ret, val;
145d84ca5b3SWolfram Sang 
146d84ca5b3SWolfram Sang 	mutex_lock(&data->device_lock);
147d84ca5b3SWolfram Sang 
148d84ca5b3SWolfram Sang 	ret = i2c_smbus_read_byte_data(client, reg);
149d84ca5b3SWolfram Sang 	if (ret < 0)
150d84ca5b3SWolfram Sang 		goto exit_unlock;
151d84ca5b3SWolfram Sang 
152d84ca5b3SWolfram Sang 	if (flag)
153d84ca5b3SWolfram Sang 		val = ret | bit;
154d84ca5b3SWolfram Sang 	else
155d84ca5b3SWolfram Sang 		val = ret & ~bit;
156d84ca5b3SWolfram Sang 
157d84ca5b3SWolfram Sang 	ret = i2c_smbus_write_byte_data(client, reg, val);
158d84ca5b3SWolfram Sang 
159d84ca5b3SWolfram Sang  exit_unlock:
160d84ca5b3SWolfram Sang 	mutex_unlock(&data->device_lock);
161d84ca5b3SWolfram Sang 	return ret;
162d84ca5b3SWolfram Sang }
163d84ca5b3SWolfram Sang 
adt7411_show_bit(struct device * dev,struct device_attribute * attr,char * buf)164d84ca5b3SWolfram Sang static ssize_t adt7411_show_bit(struct device *dev,
165d84ca5b3SWolfram Sang 				struct device_attribute *attr, char *buf)
166d84ca5b3SWolfram Sang {
167d84ca5b3SWolfram Sang 	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
168eac83cd9SAxel Lin 	struct adt7411_data *data = dev_get_drvdata(dev);
169eac83cd9SAxel Lin 	struct i2c_client *client = data->client;
170d84ca5b3SWolfram Sang 	int ret = i2c_smbus_read_byte_data(client, attr2->index);
171d84ca5b3SWolfram Sang 
172d84ca5b3SWolfram Sang 	return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
173d84ca5b3SWolfram Sang }
174d84ca5b3SWolfram Sang 
adt7411_set_bit(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)175d84ca5b3SWolfram Sang static ssize_t adt7411_set_bit(struct device *dev,
176d84ca5b3SWolfram Sang 			       struct device_attribute *attr, const char *buf,
177d84ca5b3SWolfram Sang 			       size_t count)
178d84ca5b3SWolfram Sang {
179d84ca5b3SWolfram Sang 	struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
180eac83cd9SAxel Lin 	struct adt7411_data *data = dev_get_drvdata(dev);
181eac83cd9SAxel Lin 	struct i2c_client *client = data->client;
182d84ca5b3SWolfram Sang 	int ret;
183d84ca5b3SWolfram Sang 	unsigned long flag;
184d84ca5b3SWolfram Sang 
185179c4fdbSFrans Meulenbroeks 	ret = kstrtoul(buf, 0, &flag);
186d84ca5b3SWolfram Sang 	if (ret || flag > 1)
187d84ca5b3SWolfram Sang 		return -EINVAL;
188d84ca5b3SWolfram Sang 
189d84ca5b3SWolfram Sang 	ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
190d84ca5b3SWolfram Sang 
191d84ca5b3SWolfram Sang 	/* force update */
19223244985SWolfram Sang 	mutex_lock(&data->update_lock);
193d84ca5b3SWolfram Sang 	data->next_update = jiffies;
19423244985SWolfram Sang 	mutex_unlock(&data->update_lock);
195d84ca5b3SWolfram Sang 
196d84ca5b3SWolfram Sang 	return ret < 0 ? ret : count;
197d84ca5b3SWolfram Sang }
198d84ca5b3SWolfram Sang 
199d84ca5b3SWolfram Sang #define ADT7411_BIT_ATTR(__name, __reg, __bit) \
200d84ca5b3SWolfram Sang 	SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
201d84ca5b3SWolfram Sang 	adt7411_set_bit, __bit, __reg)
202d84ca5b3SWolfram Sang 
203d84ca5b3SWolfram Sang static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
204d84ca5b3SWolfram Sang static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
205d84ca5b3SWolfram Sang static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);
206d84ca5b3SWolfram Sang 
207d84ca5b3SWolfram Sang static struct attribute *adt7411_attrs[] = {
208d84ca5b3SWolfram Sang 	&sensor_dev_attr_no_average.dev_attr.attr,
209d84ca5b3SWolfram Sang 	&sensor_dev_attr_fast_sampling.dev_attr.attr,
210d84ca5b3SWolfram Sang 	&sensor_dev_attr_adc_ref_vdd.dev_attr.attr,
211d84ca5b3SWolfram Sang 	NULL
212d84ca5b3SWolfram Sang };
2131b109c49SMichael Walle ATTRIBUTE_GROUPS(adt7411);
214d84ca5b3SWolfram Sang 
adt7411_read_in_alarm(struct device * dev,int channel,long * val)2159bb2d47dSMichael Walle static int adt7411_read_in_alarm(struct device *dev, int channel, long *val)
2169bb2d47dSMichael Walle {
2179bb2d47dSMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
2189bb2d47dSMichael Walle 	struct i2c_client *client = data->client;
2199bb2d47dSMichael Walle 	int ret;
2209bb2d47dSMichael Walle 
2219bb2d47dSMichael Walle 	ret = i2c_smbus_read_byte_data(client, adt7411_in_alarm_reg[channel]);
2229bb2d47dSMichael Walle 	if (ret < 0)
2239bb2d47dSMichael Walle 		return ret;
2249bb2d47dSMichael Walle 	*val = !!(ret & adt7411_in_alarm_bits[channel]);
2259bb2d47dSMichael Walle 	return 0;
2269bb2d47dSMichael Walle }
2279bb2d47dSMichael Walle 
adt7411_read_in_vdd(struct device * dev,u32 attr,long * val)2281b109c49SMichael Walle static int adt7411_read_in_vdd(struct device *dev, u32 attr, long *val)
229a95da110SMichael Walle {
230a95da110SMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
2311b109c49SMichael Walle 	struct i2c_client *client = data->client;
2321b109c49SMichael Walle 	int ret;
233a95da110SMichael Walle 
2341b109c49SMichael Walle 	switch (attr) {
2351b109c49SMichael Walle 	case hwmon_in_input:
2361b109c49SMichael Walle 		ret = adt7411_read_10_bit(client, ADT7411_REG_INT_TEMP_VDD_LSB,
2371b109c49SMichael Walle 					  ADT7411_REG_VDD_MSB, 2);
2381b109c49SMichael Walle 		if (ret < 0)
2391b109c49SMichael Walle 			return ret;
2401b109c49SMichael Walle 		*val = ret * 7000 / 1024;
2411b109c49SMichael Walle 		return 0;
2429bb2d47dSMichael Walle 	case hwmon_in_min:
2439bb2d47dSMichael Walle 		ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_LOW);
2449bb2d47dSMichael Walle 		if (ret < 0)
2459bb2d47dSMichael Walle 			return ret;
2469bb2d47dSMichael Walle 		*val = ret * 7000 / 256;
2479bb2d47dSMichael Walle 		return 0;
2489bb2d47dSMichael Walle 	case hwmon_in_max:
2499bb2d47dSMichael Walle 		ret = i2c_smbus_read_byte_data(client, ADT7411_REG_VDD_HIGH);
2509bb2d47dSMichael Walle 		if (ret < 0)
2519bb2d47dSMichael Walle 			return ret;
2529bb2d47dSMichael Walle 		*val = ret * 7000 / 256;
2539bb2d47dSMichael Walle 		return 0;
2549bb2d47dSMichael Walle 	case hwmon_in_alarm:
2559bb2d47dSMichael Walle 		return adt7411_read_in_alarm(dev, 0, val);
2561b109c49SMichael Walle 	default:
2571b109c49SMichael Walle 		return -EOPNOTSUPP;
2581b109c49SMichael Walle 	}
259a95da110SMichael Walle }
260a95da110SMichael Walle 
adt7411_update_vref(struct device * dev)2619bb2d47dSMichael Walle static int adt7411_update_vref(struct device *dev)
2629bb2d47dSMichael Walle {
2639bb2d47dSMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
2649bb2d47dSMichael Walle 	struct i2c_client *client = data->client;
2659bb2d47dSMichael Walle 	int val;
2669bb2d47dSMichael Walle 
2679bb2d47dSMichael Walle 	if (time_after_eq(jiffies, data->next_update)) {
2689bb2d47dSMichael Walle 		val = i2c_smbus_read_byte_data(client, ADT7411_REG_CFG3);
2699bb2d47dSMichael Walle 		if (val < 0)
2709bb2d47dSMichael Walle 			return val;
2719bb2d47dSMichael Walle 
2729bb2d47dSMichael Walle 		if (val & ADT7411_CFG3_REF_VDD) {
2739bb2d47dSMichael Walle 			val = adt7411_read_in_vdd(dev, hwmon_in_input,
2749bb2d47dSMichael Walle 						  &data->vref_cached);
2759bb2d47dSMichael Walle 			if (val < 0)
2769bb2d47dSMichael Walle 				return val;
2779bb2d47dSMichael Walle 		} else {
2789bb2d47dSMichael Walle 			data->vref_cached = 2250;
2799bb2d47dSMichael Walle 		}
2809bb2d47dSMichael Walle 
2819bb2d47dSMichael Walle 		data->next_update = jiffies + HZ;
2829bb2d47dSMichael Walle 	}
2839bb2d47dSMichael Walle 
2849bb2d47dSMichael Walle 	return 0;
2859bb2d47dSMichael Walle }
2869bb2d47dSMichael Walle 
adt7411_read_in_chan(struct device * dev,u32 attr,int channel,long * val)2871b109c49SMichael Walle static int adt7411_read_in_chan(struct device *dev, u32 attr, int channel,
2881b109c49SMichael Walle 				long *val)
2891b109c49SMichael Walle {
2901b109c49SMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
2911b109c49SMichael Walle 	struct i2c_client *client = data->client;
2921b109c49SMichael Walle 
2931b109c49SMichael Walle 	int ret;
2949bb2d47dSMichael Walle 	int reg, lsb_reg, lsb_shift;
2951b109c49SMichael Walle 	int nr = channel - 1;
2961b109c49SMichael Walle 
2971b109c49SMichael Walle 	mutex_lock(&data->update_lock);
2989bb2d47dSMichael Walle 	ret = adt7411_update_vref(dev);
2991b109c49SMichael Walle 	if (ret < 0)
3001b109c49SMichael Walle 		goto exit_unlock;
3011b109c49SMichael Walle 
3021b109c49SMichael Walle 	switch (attr) {
3031b109c49SMichael Walle 	case hwmon_in_input:
3041b109c49SMichael Walle 		lsb_reg = ADT7411_REG_EXT_TEMP_AIN14_LSB + (nr >> 2);
3051b109c49SMichael Walle 		lsb_shift = 2 * (nr & 0x03);
3061b109c49SMichael Walle 		ret = adt7411_read_10_bit(client, lsb_reg,
3071b109c49SMichael Walle 					  ADT7411_REG_EXT_TEMP_AIN1_MSB + nr,
3081b109c49SMichael Walle 					  lsb_shift);
3091b109c49SMichael Walle 		if (ret < 0)
3101b109c49SMichael Walle 			goto exit_unlock;
3111b109c49SMichael Walle 		*val = ret * data->vref_cached / 1024;
3121b109c49SMichael Walle 		ret = 0;
3131b109c49SMichael Walle 		break;
3149bb2d47dSMichael Walle 	case hwmon_in_min:
3159bb2d47dSMichael Walle 	case hwmon_in_max:
3169bb2d47dSMichael Walle 		reg = (attr == hwmon_in_min)
3179bb2d47dSMichael Walle 			? ADT7411_REG_IN_LOW(channel)
3189bb2d47dSMichael Walle 			: ADT7411_REG_IN_HIGH(channel);
3199bb2d47dSMichael Walle 		ret = i2c_smbus_read_byte_data(client, reg);
3209bb2d47dSMichael Walle 		if (ret < 0)
3219bb2d47dSMichael Walle 			goto exit_unlock;
3229bb2d47dSMichael Walle 		*val = ret * data->vref_cached / 256;
3239bb2d47dSMichael Walle 		ret = 0;
3249bb2d47dSMichael Walle 		break;
3259bb2d47dSMichael Walle 	case hwmon_in_alarm:
3269bb2d47dSMichael Walle 		ret = adt7411_read_in_alarm(dev, channel, val);
3279bb2d47dSMichael Walle 		break;
3281b109c49SMichael Walle 	default:
3291b109c49SMichael Walle 		ret = -EOPNOTSUPP;
3301b109c49SMichael Walle 		break;
3311b109c49SMichael Walle 	}
3321b109c49SMichael Walle  exit_unlock:
3331b109c49SMichael Walle 	mutex_unlock(&data->update_lock);
3341b109c49SMichael Walle 	return ret;
3351b109c49SMichael Walle }
3361b109c49SMichael Walle 
adt7411_read_in(struct device * dev,u32 attr,int channel,long * val)3371b109c49SMichael Walle static int adt7411_read_in(struct device *dev, u32 attr, int channel,
3381b109c49SMichael Walle 			   long *val)
3391b109c49SMichael Walle {
3401b109c49SMichael Walle 	if (channel == 0)
3411b109c49SMichael Walle 		return adt7411_read_in_vdd(dev, attr, val);
3421b109c49SMichael Walle 	else
3431b109c49SMichael Walle 		return adt7411_read_in_chan(dev, attr, channel, val);
3441b109c49SMichael Walle }
3451b109c49SMichael Walle 
3469bb2d47dSMichael Walle 
adt7411_read_temp_alarm(struct device * dev,u32 attr,int channel,long * val)3479bb2d47dSMichael Walle static int adt7411_read_temp_alarm(struct device *dev, u32 attr, int channel,
3489bb2d47dSMichael Walle 				   long *val)
3499bb2d47dSMichael Walle {
3509bb2d47dSMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
3519bb2d47dSMichael Walle 	struct i2c_client *client = data->client;
3529bb2d47dSMichael Walle 	int ret, bit;
3539bb2d47dSMichael Walle 
3549bb2d47dSMichael Walle 	ret = i2c_smbus_read_byte_data(client, ADT7411_REG_STAT_1);
3559bb2d47dSMichael Walle 	if (ret < 0)
3569bb2d47dSMichael Walle 		return ret;
3579bb2d47dSMichael Walle 
3589bb2d47dSMichael Walle 	switch (attr) {
3599bb2d47dSMichael Walle 	case hwmon_temp_min_alarm:
3609bb2d47dSMichael Walle 		bit = channel ? ADT7411_STAT_1_EXT_TEMP_LOW
3619bb2d47dSMichael Walle 			      : ADT7411_STAT_1_INT_TEMP_LOW;
3629bb2d47dSMichael Walle 		break;
3639bb2d47dSMichael Walle 	case hwmon_temp_max_alarm:
3649bb2d47dSMichael Walle 		bit = channel ? ADT7411_STAT_1_EXT_TEMP_HIGH_AIN1
3659bb2d47dSMichael Walle 			      : ADT7411_STAT_1_INT_TEMP_HIGH;
3669bb2d47dSMichael Walle 		break;
3679bb2d47dSMichael Walle 	case hwmon_temp_fault:
3689bb2d47dSMichael Walle 		bit = ADT7411_STAT_1_EXT_TEMP_FAULT;
3699bb2d47dSMichael Walle 		break;
3709bb2d47dSMichael Walle 	default:
3719bb2d47dSMichael Walle 		return -EOPNOTSUPP;
3729bb2d47dSMichael Walle 	}
3739bb2d47dSMichael Walle 
3749bb2d47dSMichael Walle 	*val = !!(ret & bit);
3759bb2d47dSMichael Walle 	return 0;
3769bb2d47dSMichael Walle }
3779bb2d47dSMichael Walle 
adt7411_read_temp(struct device * dev,u32 attr,int channel,long * val)3781b109c49SMichael Walle static int adt7411_read_temp(struct device *dev, u32 attr, int channel,
3791b109c49SMichael Walle 			     long *val)
3801b109c49SMichael Walle {
3811b109c49SMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
3821b109c49SMichael Walle 	struct i2c_client *client = data->client;
3839bb2d47dSMichael Walle 	int ret, reg, regl, regh;
3841b109c49SMichael Walle 
3851b109c49SMichael Walle 	switch (attr) {
3861b109c49SMichael Walle 	case hwmon_temp_input:
3871b109c49SMichael Walle 		regl = channel ? ADT7411_REG_EXT_TEMP_AIN14_LSB :
3881b109c49SMichael Walle 				 ADT7411_REG_INT_TEMP_VDD_LSB;
3891b109c49SMichael Walle 		regh = channel ? ADT7411_REG_EXT_TEMP_AIN1_MSB :
3901b109c49SMichael Walle 				 ADT7411_REG_INT_TEMP_MSB;
3911b109c49SMichael Walle 		ret = adt7411_read_10_bit(client, regl, regh, 0);
3921b109c49SMichael Walle 		if (ret < 0)
3931b109c49SMichael Walle 			return ret;
3941b109c49SMichael Walle 		ret = ret & 0x200 ? ret - 0x400 : ret; /* 10 bit signed */
3951b109c49SMichael Walle 		*val = ret * 250;
3961b109c49SMichael Walle 		return 0;
3979bb2d47dSMichael Walle 	case hwmon_temp_min:
3989bb2d47dSMichael Walle 	case hwmon_temp_max:
3999bb2d47dSMichael Walle 		reg = (attr == hwmon_temp_min)
4009bb2d47dSMichael Walle 			? ADT7411_REG_TEMP_LOW(channel)
4019bb2d47dSMichael Walle 			: ADT7411_REG_TEMP_HIGH(channel);
4029bb2d47dSMichael Walle 		ret = i2c_smbus_read_byte_data(client, reg);
4039bb2d47dSMichael Walle 		if (ret < 0)
4049bb2d47dSMichael Walle 			return ret;
4059bb2d47dSMichael Walle 		ret = ret & 0x80 ? ret - 0x100 : ret; /* 8 bit signed */
4069bb2d47dSMichael Walle 		*val = ret * 1000;
4079bb2d47dSMichael Walle 		return 0;
4089bb2d47dSMichael Walle 	case hwmon_temp_min_alarm:
4099bb2d47dSMichael Walle 	case hwmon_temp_max_alarm:
4109bb2d47dSMichael Walle 	case hwmon_temp_fault:
4119bb2d47dSMichael Walle 		return adt7411_read_temp_alarm(dev, attr, channel, val);
4121b109c49SMichael Walle 	default:
4131b109c49SMichael Walle 		return -EOPNOTSUPP;
4141b109c49SMichael Walle 	}
4151b109c49SMichael Walle }
4161b109c49SMichael Walle 
adt7411_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)4171b109c49SMichael Walle static int adt7411_read(struct device *dev, enum hwmon_sensor_types type,
4181b109c49SMichael Walle 			u32 attr, int channel, long *val)
4191b109c49SMichael Walle {
4201b109c49SMichael Walle 	switch (type) {
4211b109c49SMichael Walle 	case hwmon_in:
4221b109c49SMichael Walle 		return adt7411_read_in(dev, attr, channel, val);
4231b109c49SMichael Walle 	case hwmon_temp:
4241b109c49SMichael Walle 		return adt7411_read_temp(dev, attr, channel, val);
4251b109c49SMichael Walle 	default:
4261b109c49SMichael Walle 		return -EOPNOTSUPP;
4271b109c49SMichael Walle 	}
4281b109c49SMichael Walle }
4291b109c49SMichael Walle 
adt7411_write_in_vdd(struct device * dev,u32 attr,long val)4309bb2d47dSMichael Walle static int adt7411_write_in_vdd(struct device *dev, u32 attr, long val)
4319bb2d47dSMichael Walle {
4329bb2d47dSMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
4339bb2d47dSMichael Walle 	struct i2c_client *client = data->client;
4349bb2d47dSMichael Walle 	int reg;
4359bb2d47dSMichael Walle 
4369bb2d47dSMichael Walle 	val = clamp_val(val, 0, 255 * 7000 / 256);
4379bb2d47dSMichael Walle 	val = DIV_ROUND_CLOSEST(val * 256, 7000);
4389bb2d47dSMichael Walle 
4399bb2d47dSMichael Walle 	switch (attr) {
4409bb2d47dSMichael Walle 	case hwmon_in_min:
4419bb2d47dSMichael Walle 		reg = ADT7411_REG_VDD_LOW;
4429bb2d47dSMichael Walle 		break;
4439bb2d47dSMichael Walle 	case hwmon_in_max:
4449bb2d47dSMichael Walle 		reg = ADT7411_REG_VDD_HIGH;
4459bb2d47dSMichael Walle 		break;
4469bb2d47dSMichael Walle 	default:
4479bb2d47dSMichael Walle 		return -EOPNOTSUPP;
4489bb2d47dSMichael Walle 	}
4499bb2d47dSMichael Walle 
4509bb2d47dSMichael Walle 	return i2c_smbus_write_byte_data(client, reg, val);
4519bb2d47dSMichael Walle }
4529bb2d47dSMichael Walle 
adt7411_write_in_chan(struct device * dev,u32 attr,int channel,long val)4539bb2d47dSMichael Walle static int adt7411_write_in_chan(struct device *dev, u32 attr, int channel,
4549bb2d47dSMichael Walle 				 long val)
4559bb2d47dSMichael Walle {
4569bb2d47dSMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
4579bb2d47dSMichael Walle 	struct i2c_client *client = data->client;
4589bb2d47dSMichael Walle 	int ret, reg;
4599bb2d47dSMichael Walle 
4609bb2d47dSMichael Walle 	mutex_lock(&data->update_lock);
4619bb2d47dSMichael Walle 	ret = adt7411_update_vref(dev);
4629bb2d47dSMichael Walle 	if (ret < 0)
4639bb2d47dSMichael Walle 		goto exit_unlock;
4649bb2d47dSMichael Walle 	val = clamp_val(val, 0, 255 * data->vref_cached / 256);
4659bb2d47dSMichael Walle 	val = DIV_ROUND_CLOSEST(val * 256, data->vref_cached);
4669bb2d47dSMichael Walle 
4679bb2d47dSMichael Walle 	switch (attr) {
4689bb2d47dSMichael Walle 	case hwmon_in_min:
4699bb2d47dSMichael Walle 		reg = ADT7411_REG_IN_LOW(channel);
4709bb2d47dSMichael Walle 		break;
4719bb2d47dSMichael Walle 	case hwmon_in_max:
4729bb2d47dSMichael Walle 		reg = ADT7411_REG_IN_HIGH(channel);
4739bb2d47dSMichael Walle 		break;
4749bb2d47dSMichael Walle 	default:
4759bb2d47dSMichael Walle 		ret = -EOPNOTSUPP;
4769bb2d47dSMichael Walle 		goto exit_unlock;
4779bb2d47dSMichael Walle 	}
4789bb2d47dSMichael Walle 
4799bb2d47dSMichael Walle 	ret = i2c_smbus_write_byte_data(client, reg, val);
4809bb2d47dSMichael Walle  exit_unlock:
4819bb2d47dSMichael Walle 	mutex_unlock(&data->update_lock);
4829bb2d47dSMichael Walle 	return ret;
4839bb2d47dSMichael Walle }
4849bb2d47dSMichael Walle 
adt7411_write_in(struct device * dev,u32 attr,int channel,long val)4859bb2d47dSMichael Walle static int adt7411_write_in(struct device *dev, u32 attr, int channel,
4869bb2d47dSMichael Walle 			    long val)
4879bb2d47dSMichael Walle {
4889bb2d47dSMichael Walle 	if (channel == 0)
4899bb2d47dSMichael Walle 		return adt7411_write_in_vdd(dev, attr, val);
4909bb2d47dSMichael Walle 	else
4919bb2d47dSMichael Walle 		return adt7411_write_in_chan(dev, attr, channel, val);
4929bb2d47dSMichael Walle }
4939bb2d47dSMichael Walle 
adt7411_write_temp(struct device * dev,u32 attr,int channel,long val)4949bb2d47dSMichael Walle static int adt7411_write_temp(struct device *dev, u32 attr, int channel,
4959bb2d47dSMichael Walle 			      long val)
4969bb2d47dSMichael Walle {
4979bb2d47dSMichael Walle 	struct adt7411_data *data = dev_get_drvdata(dev);
4989bb2d47dSMichael Walle 	struct i2c_client *client = data->client;
4999bb2d47dSMichael Walle 	int reg;
5009bb2d47dSMichael Walle 
5019bb2d47dSMichael Walle 	val = clamp_val(val, -128000, 127000);
5029bb2d47dSMichael Walle 	val = DIV_ROUND_CLOSEST(val, 1000);
5039bb2d47dSMichael Walle 
5049bb2d47dSMichael Walle 	switch (attr) {
5059bb2d47dSMichael Walle 	case hwmon_temp_min:
5069bb2d47dSMichael Walle 		reg = ADT7411_REG_TEMP_LOW(channel);
5079bb2d47dSMichael Walle 		break;
5089bb2d47dSMichael Walle 	case hwmon_temp_max:
5099bb2d47dSMichael Walle 		reg = ADT7411_REG_TEMP_HIGH(channel);
5109bb2d47dSMichael Walle 		break;
5119bb2d47dSMichael Walle 	default:
5129bb2d47dSMichael Walle 		return -EOPNOTSUPP;
5139bb2d47dSMichael Walle 	}
5149bb2d47dSMichael Walle 
5159bb2d47dSMichael Walle 	return i2c_smbus_write_byte_data(client, reg, val);
5169bb2d47dSMichael Walle }
5179bb2d47dSMichael Walle 
adt7411_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)5189bb2d47dSMichael Walle static int adt7411_write(struct device *dev, enum hwmon_sensor_types type,
5199bb2d47dSMichael Walle 			 u32 attr, int channel, long val)
5209bb2d47dSMichael Walle {
5219bb2d47dSMichael Walle 	switch (type) {
5229bb2d47dSMichael Walle 	case hwmon_in:
5239bb2d47dSMichael Walle 		return adt7411_write_in(dev, attr, channel, val);
5249bb2d47dSMichael Walle 	case hwmon_temp:
5259bb2d47dSMichael Walle 		return adt7411_write_temp(dev, attr, channel, val);
5269bb2d47dSMichael Walle 	default:
5279bb2d47dSMichael Walle 		return -EOPNOTSUPP;
5289bb2d47dSMichael Walle 	}
5299bb2d47dSMichael Walle }
5309bb2d47dSMichael Walle 
adt7411_is_visible(const void * _data,enum hwmon_sensor_types type,u32 attr,int channel)5311b109c49SMichael Walle static umode_t adt7411_is_visible(const void *_data,
5321b109c49SMichael Walle 				  enum hwmon_sensor_types type,
5331b109c49SMichael Walle 				  u32 attr, int channel)
5341b109c49SMichael Walle {
5351b109c49SMichael Walle 	const struct adt7411_data *data = _data;
5369bb2d47dSMichael Walle 	bool visible;
5371b109c49SMichael Walle 
5381b109c49SMichael Walle 	switch (type) {
5391b109c49SMichael Walle 	case hwmon_in:
5409bb2d47dSMichael Walle 		visible = channel == 0 || channel >= 3 || !data->use_ext_temp;
5419bb2d47dSMichael Walle 		switch (attr) {
5429bb2d47dSMichael Walle 		case hwmon_in_input:
5439bb2d47dSMichael Walle 		case hwmon_in_alarm:
5449bb2d47dSMichael Walle 			return visible ? S_IRUGO : 0;
5459bb2d47dSMichael Walle 		case hwmon_in_min:
5469bb2d47dSMichael Walle 		case hwmon_in_max:
5479bb2d47dSMichael Walle 			return visible ? S_IRUGO | S_IWUSR : 0;
5481b109c49SMichael Walle 		}
5499bb2d47dSMichael Walle 		break;
5509bb2d47dSMichael Walle 	case hwmon_temp:
5519bb2d47dSMichael Walle 		visible = channel == 0 || data->use_ext_temp;
5529bb2d47dSMichael Walle 		switch (attr) {
5539bb2d47dSMichael Walle 		case hwmon_temp_input:
5549bb2d47dSMichael Walle 		case hwmon_temp_min_alarm:
5559bb2d47dSMichael Walle 		case hwmon_temp_max_alarm:
5569bb2d47dSMichael Walle 		case hwmon_temp_fault:
5579bb2d47dSMichael Walle 			return visible ? S_IRUGO : 0;
5589bb2d47dSMichael Walle 		case hwmon_temp_min:
5599bb2d47dSMichael Walle 		case hwmon_temp_max:
5609bb2d47dSMichael Walle 			return visible ? S_IRUGO | S_IWUSR : 0;
5619bb2d47dSMichael Walle 		}
5629bb2d47dSMichael Walle 		break;
5639bb2d47dSMichael Walle 	default:
5649bb2d47dSMichael Walle 		break;
5659bb2d47dSMichael Walle 	}
5669bb2d47dSMichael Walle 	return 0;
5671b109c49SMichael Walle }
568d84ca5b3SWolfram Sang 
adt7411_detect(struct i2c_client * client,struct i2c_board_info * info)569d84ca5b3SWolfram Sang static int adt7411_detect(struct i2c_client *client,
570d84ca5b3SWolfram Sang 			  struct i2c_board_info *info)
571d84ca5b3SWolfram Sang {
572d84ca5b3SWolfram Sang 	int val;
573d84ca5b3SWolfram Sang 
574d84ca5b3SWolfram Sang 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
575d84ca5b3SWolfram Sang 		return -ENODEV;
576d84ca5b3SWolfram Sang 
577d84ca5b3SWolfram Sang 	val = i2c_smbus_read_byte_data(client, ADT7411_REG_MANUFACTURER_ID);
578d84ca5b3SWolfram Sang 	if (val < 0 || val != ADT7411_MANUFACTURER_ID) {
579b55f3757SGuenter Roeck 		dev_dbg(&client->dev,
580b55f3757SGuenter Roeck 			"Wrong manufacturer ID. Got %d, expected %d\n",
581b55f3757SGuenter Roeck 			val, ADT7411_MANUFACTURER_ID);
582d84ca5b3SWolfram Sang 		return -ENODEV;
583d84ca5b3SWolfram Sang 	}
584d84ca5b3SWolfram Sang 
585d84ca5b3SWolfram Sang 	val = i2c_smbus_read_byte_data(client, ADT7411_REG_DEVICE_ID);
586d84ca5b3SWolfram Sang 	if (val < 0 || val != ADT7411_DEVICE_ID) {
587b55f3757SGuenter Roeck 		dev_dbg(&client->dev,
588b55f3757SGuenter Roeck 			"Wrong device ID. Got %d, expected %d\n",
589b55f3757SGuenter Roeck 			val, ADT7411_DEVICE_ID);
590d84ca5b3SWolfram Sang 		return -ENODEV;
591d84ca5b3SWolfram Sang 	}
592d84ca5b3SWolfram Sang 
593f2f394dbSWolfram Sang 	strscpy(info->type, "adt7411", I2C_NAME_SIZE);
594d84ca5b3SWolfram Sang 
595d84ca5b3SWolfram Sang 	return 0;
596d84ca5b3SWolfram Sang }
597d84ca5b3SWolfram Sang 
adt7411_init_device(struct adt7411_data * data)598601807bbSMichael Walle static int adt7411_init_device(struct adt7411_data *data)
599601807bbSMichael Walle {
600601807bbSMichael Walle 	int ret;
601601807bbSMichael Walle 	u8 val;
602601807bbSMichael Walle 
603601807bbSMichael Walle 	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG3);
604601807bbSMichael Walle 	if (ret < 0)
605601807bbSMichael Walle 		return ret;
606601807bbSMichael Walle 
607601807bbSMichael Walle 	/*
608601807bbSMichael Walle 	 * We must only write zero to bit 1 and bit 2 and only one to bit 3
609601807bbSMichael Walle 	 * according to the datasheet.
610601807bbSMichael Walle 	 */
611601807bbSMichael Walle 	val = ret;
612601807bbSMichael Walle 	val &= ~(ADT7411_CFG3_RESERVED_BIT1 | ADT7411_CFG3_RESERVED_BIT2);
613601807bbSMichael Walle 	val |= ADT7411_CFG3_RESERVED_BIT3;
614601807bbSMichael Walle 
615601807bbSMichael Walle 	ret = i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG3, val);
616601807bbSMichael Walle 	if (ret < 0)
617601807bbSMichael Walle 		return ret;
618601807bbSMichael Walle 
619601807bbSMichael Walle 	ret = i2c_smbus_read_byte_data(data->client, ADT7411_REG_CFG1);
620601807bbSMichael Walle 	if (ret < 0)
621601807bbSMichael Walle 		return ret;
622601807bbSMichael Walle 
623a95da110SMichael Walle 	data->use_ext_temp = ret & ADT7411_CFG1_EXT_TDM;
624a95da110SMichael Walle 
625601807bbSMichael Walle 	/*
626601807bbSMichael Walle 	 * We must only write zero to bit 1 and only one to bit 3 according to
627601807bbSMichael Walle 	 * the datasheet.
628601807bbSMichael Walle 	 */
629601807bbSMichael Walle 	val = ret;
630601807bbSMichael Walle 	val &= ~ADT7411_CFG1_RESERVED_BIT1;
631601807bbSMichael Walle 	val |= ADT7411_CFG1_RESERVED_BIT3;
632601807bbSMichael Walle 
633601807bbSMichael Walle 	/* enable monitoring */
634601807bbSMichael Walle 	val |= ADT7411_CFG1_START_MONITOR;
635601807bbSMichael Walle 
636601807bbSMichael Walle 	return i2c_smbus_write_byte_data(data->client, ADT7411_REG_CFG1, val);
637601807bbSMichael Walle }
638601807bbSMichael Walle 
63907aa164aSKrzysztof Kozlowski static const struct hwmon_channel_info * const adt7411_info[] = {
640ff56121eSGuenter Roeck 	HWMON_CHANNEL_INFO(in,
6419bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
6429bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
6439bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
6449bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
6459bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
6469bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
6479bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
6489bb2d47dSMichael Walle 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM,
649ff56121eSGuenter Roeck 			   HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_ALARM),
650ff56121eSGuenter Roeck 	HWMON_CHANNEL_INFO(temp,
6519bb2d47dSMichael Walle 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
6529bb2d47dSMichael Walle 			   HWMON_T_MAX | HWMON_T_MAX_ALARM,
6539bb2d47dSMichael Walle 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MIN_ALARM |
654ff56121eSGuenter Roeck 			   HWMON_T_MAX | HWMON_T_MAX_ALARM | HWMON_T_FAULT),
6551b109c49SMichael Walle 	NULL
6561b109c49SMichael Walle };
6571b109c49SMichael Walle 
6581b109c49SMichael Walle static const struct hwmon_ops adt7411_hwmon_ops = {
6591b109c49SMichael Walle 	.is_visible = adt7411_is_visible,
6601b109c49SMichael Walle 	.read = adt7411_read,
6619bb2d47dSMichael Walle 	.write = adt7411_write,
6621b109c49SMichael Walle };
6631b109c49SMichael Walle 
6641b109c49SMichael Walle static const struct hwmon_chip_info adt7411_chip_info = {
6651b109c49SMichael Walle 	.ops = &adt7411_hwmon_ops,
6661b109c49SMichael Walle 	.info = adt7411_info,
6671b109c49SMichael Walle };
6681b109c49SMichael Walle 
adt7411_probe(struct i2c_client * client)66967487038SStephen Kitt static int adt7411_probe(struct i2c_client *client)
670d84ca5b3SWolfram Sang {
671eac83cd9SAxel Lin 	struct device *dev = &client->dev;
672d84ca5b3SWolfram Sang 	struct adt7411_data *data;
673eac83cd9SAxel Lin 	struct device *hwmon_dev;
674d84ca5b3SWolfram Sang 	int ret;
675d84ca5b3SWolfram Sang 
676eac83cd9SAxel Lin 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
677d84ca5b3SWolfram Sang 	if (!data)
678d84ca5b3SWolfram Sang 		return -ENOMEM;
679d84ca5b3SWolfram Sang 
680d84ca5b3SWolfram Sang 	i2c_set_clientdata(client, data);
681eac83cd9SAxel Lin 	data->client = client;
682d84ca5b3SWolfram Sang 	mutex_init(&data->device_lock);
68323244985SWolfram Sang 	mutex_init(&data->update_lock);
684d84ca5b3SWolfram Sang 
685601807bbSMichael Walle 	ret = adt7411_init_device(data);
686d84ca5b3SWolfram Sang 	if (ret < 0)
68765ec17b0SGuenter Roeck 		return ret;
688d84ca5b3SWolfram Sang 
689d84ca5b3SWolfram Sang 	/* force update on first occasion */
690d84ca5b3SWolfram Sang 	data->next_update = jiffies;
691d84ca5b3SWolfram Sang 
6921b109c49SMichael Walle 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
693eac83cd9SAxel Lin 							 data,
6941b109c49SMichael Walle 							 &adt7411_chip_info,
695eac83cd9SAxel Lin 							 adt7411_groups);
696eac83cd9SAxel Lin 	return PTR_ERR_OR_ZERO(hwmon_dev);
697d84ca5b3SWolfram Sang }
698d84ca5b3SWolfram Sang 
699d84ca5b3SWolfram Sang static const struct i2c_device_id adt7411_id[] = {
700d84ca5b3SWolfram Sang 	{ "adt7411", 0 },
701d84ca5b3SWolfram Sang 	{ }
702d84ca5b3SWolfram Sang };
703a7254d68Saxel lin MODULE_DEVICE_TABLE(i2c, adt7411_id);
704d84ca5b3SWolfram Sang 
705d84ca5b3SWolfram Sang static struct i2c_driver adt7411_driver = {
706d84ca5b3SWolfram Sang 	.driver		= {
707d84ca5b3SWolfram Sang 		.name		= "adt7411",
708d84ca5b3SWolfram Sang 	},
709*1975d167SUwe Kleine-König 	.probe = adt7411_probe,
710d84ca5b3SWolfram Sang 	.id_table = adt7411_id,
711d84ca5b3SWolfram Sang 	.detect = adt7411_detect,
712d84ca5b3SWolfram Sang 	.address_list = normal_i2c,
713d84ca5b3SWolfram Sang 	.class = I2C_CLASS_HWMON,
714d84ca5b3SWolfram Sang };
715d84ca5b3SWolfram Sang 
716f0967eeaSAxel Lin module_i2c_driver(adt7411_driver);
717d84ca5b3SWolfram Sang 
7186e0498ddSWolfram Sang MODULE_AUTHOR("Sascha Hauer, Wolfram Sang <kernel@pengutronix.de>");
719d84ca5b3SWolfram Sang MODULE_DESCRIPTION("ADT7411 driver");
720d84ca5b3SWolfram Sang MODULE_LICENSE("GPL v2");
721