xref: /linux/drivers/iio/adc/ti-ads7138.c (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
1*024b08feSTobias Sperling // SPDX-License-Identifier: GPL-2.0-or-later
2*024b08feSTobias Sperling /*
3*024b08feSTobias Sperling  * ADS7138 - Texas Instruments Analog-to-Digital Converter
4*024b08feSTobias Sperling  */
5*024b08feSTobias Sperling 
6*024b08feSTobias Sperling #include <linux/bitfield.h>
7*024b08feSTobias Sperling #include <linux/cleanup.h>
8*024b08feSTobias Sperling #include <linux/err.h>
9*024b08feSTobias Sperling #include <linux/i2c.h>
10*024b08feSTobias Sperling #include <linux/init.h>
11*024b08feSTobias Sperling #include <linux/interrupt.h>
12*024b08feSTobias Sperling #include <linux/kernel.h>
13*024b08feSTobias Sperling #include <linux/module.h>
14*024b08feSTobias Sperling #include <linux/pm_runtime.h>
15*024b08feSTobias Sperling #include <linux/regulator/consumer.h>
16*024b08feSTobias Sperling #include <linux/unaligned.h>
17*024b08feSTobias Sperling 
18*024b08feSTobias Sperling #include <linux/iio/events.h>
19*024b08feSTobias Sperling #include <linux/iio/iio.h>
20*024b08feSTobias Sperling #include <linux/iio/types.h>
21*024b08feSTobias Sperling 
22*024b08feSTobias Sperling /*
23*024b08feSTobias Sperling  * Always assume 16 bits resolution as HW registers are aligned like that and
24*024b08feSTobias Sperling  * with enabled oversampling/averaging it actually corresponds to 16 bits.
25*024b08feSTobias Sperling  */
26*024b08feSTobias Sperling #define ADS7138_RES_BITS		16
27*024b08feSTobias Sperling 
28*024b08feSTobias Sperling /* ADS7138 operation codes */
29*024b08feSTobias Sperling #define ADS7138_OPCODE_SINGLE_WRITE	0x08
30*024b08feSTobias Sperling #define ADS7138_OPCODE_SET_BIT		0x18
31*024b08feSTobias Sperling #define ADS7138_OPCODE_CLEAR_BIT	0x20
32*024b08feSTobias Sperling #define ADS7138_OPCODE_BLOCK_WRITE	0x28
33*024b08feSTobias Sperling #define ADS7138_OPCODE_BLOCK_READ	0x30
34*024b08feSTobias Sperling 
35*024b08feSTobias Sperling /* ADS7138 registers */
36*024b08feSTobias Sperling #define ADS7138_REG_GENERAL_CFG		0x01
37*024b08feSTobias Sperling #define ADS7138_REG_OSR_CFG		0x03
38*024b08feSTobias Sperling #define ADS7138_REG_OPMODE_CFG		0x04
39*024b08feSTobias Sperling #define ADS7138_REG_SEQUENCE_CFG	0x10
40*024b08feSTobias Sperling #define ADS7138_REG_AUTO_SEQ_CH_SEL	0x12
41*024b08feSTobias Sperling #define ADS7138_REG_ALERT_CH_SEL	0x14
42*024b08feSTobias Sperling #define ADS7138_REG_EVENT_FLAG		0x18
43*024b08feSTobias Sperling #define ADS7138_REG_EVENT_HIGH_FLAG	0x1A
44*024b08feSTobias Sperling #define ADS7138_REG_EVENT_LOW_FLAG	0x1C
45*024b08feSTobias Sperling #define ADS7138_REG_HIGH_TH_HYS_CH(x)	((x) * 4 + 0x20)
46*024b08feSTobias Sperling #define ADS7138_REG_LOW_TH_CNT_CH(x)	((x) * 4 + 0x22)
47*024b08feSTobias Sperling #define ADS7138_REG_MAX_LSB_CH(x)	((x) * 2 + 0x60)
48*024b08feSTobias Sperling #define ADS7138_REG_MIN_LSB_CH(x)	((x) * 2 + 0x80)
49*024b08feSTobias Sperling #define ADS7138_REG_RECENT_LSB_CH(x)	((x) * 2 + 0xA0)
50*024b08feSTobias Sperling 
51*024b08feSTobias Sperling #define ADS7138_GENERAL_CFG_RST		BIT(0)
52*024b08feSTobias Sperling #define ADS7138_GENERAL_CFG_DWC_EN	BIT(4)
53*024b08feSTobias Sperling #define ADS7138_GENERAL_CFG_STATS_EN	BIT(5)
54*024b08feSTobias Sperling #define ADS7138_OSR_CFG_MASK		GENMASK(2, 0)
55*024b08feSTobias Sperling #define ADS7138_OPMODE_CFG_CONV_MODE	BIT(5)
56*024b08feSTobias Sperling #define ADS7138_OPMODE_CFG_FREQ_MASK	GENMASK(4, 0)
57*024b08feSTobias Sperling #define ADS7138_SEQUENCE_CFG_SEQ_MODE	BIT(0)
58*024b08feSTobias Sperling #define ADS7138_SEQUENCE_CFG_SEQ_START	BIT(4)
59*024b08feSTobias Sperling #define ADS7138_THRESHOLD_LSB_MASK	GENMASK(7, 4)
60*024b08feSTobias Sperling 
61*024b08feSTobias Sperling enum ads7138_modes {
62*024b08feSTobias Sperling 	ADS7138_MODE_MANUAL,
63*024b08feSTobias Sperling 	ADS7138_MODE_AUTO,
64*024b08feSTobias Sperling };
65*024b08feSTobias Sperling 
66*024b08feSTobias Sperling struct ads7138_chip_data {
67*024b08feSTobias Sperling 	const char *name;
68*024b08feSTobias Sperling 	const int channel_num;
69*024b08feSTobias Sperling };
70*024b08feSTobias Sperling 
71*024b08feSTobias Sperling struct ads7138_data {
72*024b08feSTobias Sperling 	/* Protects RMW access to the I2C interface */
73*024b08feSTobias Sperling 	struct mutex lock;
74*024b08feSTobias Sperling 	struct i2c_client *client;
75*024b08feSTobias Sperling 	struct regulator *vref_regu;
76*024b08feSTobias Sperling 	const struct ads7138_chip_data *chip_data;
77*024b08feSTobias Sperling };
78*024b08feSTobias Sperling 
79*024b08feSTobias Sperling /*
80*024b08feSTobias Sperling  * 2D array of available sampling frequencies and the corresponding register
81*024b08feSTobias Sperling  * values. Structured like this to be easily usable in read_avail function.
82*024b08feSTobias Sperling  */
83*024b08feSTobias Sperling static const int ads7138_samp_freqs_bits[2][26] = {
84*024b08feSTobias Sperling 	{
85*024b08feSTobias Sperling 		163, 244, 326, 488, 651, 977, 1302, 1953,
86*024b08feSTobias Sperling 		2604, 3906, 5208, 7813, 10417, 15625, 20833, 31250,
87*024b08feSTobias Sperling 		41667, 62500, 83333, 125000, 166667, 250000, 333333, 500000,
88*024b08feSTobias Sperling 		666667, 1000000
89*024b08feSTobias Sperling 	}, {
90*024b08feSTobias Sperling 		0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
91*024b08feSTobias Sperling 		0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
92*024b08feSTobias Sperling 		/* Here is a hole, due to duplicate frequencies */
93*024b08feSTobias Sperling 		0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02,
94*024b08feSTobias Sperling 		0x01, 0x00
95*024b08feSTobias Sperling 	}
96*024b08feSTobias Sperling };
97*024b08feSTobias Sperling 
98*024b08feSTobias Sperling static const int ads7138_oversampling_ratios[] = {
99*024b08feSTobias Sperling 	1, 2, 4, 8, 16, 32, 64, 128
100*024b08feSTobias Sperling };
101*024b08feSTobias Sperling 
102*024b08feSTobias Sperling static int ads7138_i2c_write_block(const struct i2c_client *client, u8 reg,
103*024b08feSTobias Sperling 				   u8 *values, u8 length)
104*024b08feSTobias Sperling {
105*024b08feSTobias Sperling 	int ret;
106*024b08feSTobias Sperling 	int len = length + 2; /* "+ 2" for OPCODE and reg */
107*024b08feSTobias Sperling 
108*024b08feSTobias Sperling 	u8 *buf __free(kfree) = kmalloc(len, GFP_KERNEL);
109*024b08feSTobias Sperling 	if (!buf)
110*024b08feSTobias Sperling 		return -ENOMEM;
111*024b08feSTobias Sperling 
112*024b08feSTobias Sperling 	buf[0] = ADS7138_OPCODE_BLOCK_WRITE;
113*024b08feSTobias Sperling 	buf[1] = reg;
114*024b08feSTobias Sperling 	memcpy(&buf[2], values, length);
115*024b08feSTobias Sperling 
116*024b08feSTobias Sperling 	ret = i2c_master_send(client, buf, len);
117*024b08feSTobias Sperling 	if (ret < 0)
118*024b08feSTobias Sperling 		return ret;
119*024b08feSTobias Sperling 	if (ret != len)
120*024b08feSTobias Sperling 		return -EIO;
121*024b08feSTobias Sperling 
122*024b08feSTobias Sperling 	return 0;
123*024b08feSTobias Sperling }
124*024b08feSTobias Sperling 
125*024b08feSTobias Sperling static int ads7138_i2c_write_with_opcode(const struct i2c_client *client,
126*024b08feSTobias Sperling 					 u8 reg, u8 regval, u8 opcode)
127*024b08feSTobias Sperling {
128*024b08feSTobias Sperling 	u8 buf[3] = { opcode, reg, regval };
129*024b08feSTobias Sperling 	int ret;
130*024b08feSTobias Sperling 
131*024b08feSTobias Sperling 	ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
132*024b08feSTobias Sperling 	if (ret < 0)
133*024b08feSTobias Sperling 		return ret;
134*024b08feSTobias Sperling 	if (ret != ARRAY_SIZE(buf))
135*024b08feSTobias Sperling 		return -EIO;
136*024b08feSTobias Sperling 
137*024b08feSTobias Sperling 	return 0;
138*024b08feSTobias Sperling }
139*024b08feSTobias Sperling 
140*024b08feSTobias Sperling static int ads7138_i2c_write(const struct i2c_client *client, u8 reg, u8 value)
141*024b08feSTobias Sperling {
142*024b08feSTobias Sperling 	return ads7138_i2c_write_with_opcode(client, reg, value,
143*024b08feSTobias Sperling 					     ADS7138_OPCODE_SINGLE_WRITE);
144*024b08feSTobias Sperling }
145*024b08feSTobias Sperling 
146*024b08feSTobias Sperling static int ads7138_i2c_set_bit(const struct i2c_client *client, u8 reg, u8 bits)
147*024b08feSTobias Sperling {
148*024b08feSTobias Sperling 	return ads7138_i2c_write_with_opcode(client, reg, bits,
149*024b08feSTobias Sperling 					     ADS7138_OPCODE_SET_BIT);
150*024b08feSTobias Sperling }
151*024b08feSTobias Sperling 
152*024b08feSTobias Sperling static int ads7138_i2c_clear_bit(const struct i2c_client *client, u8 reg, u8 bits)
153*024b08feSTobias Sperling {
154*024b08feSTobias Sperling 	return ads7138_i2c_write_with_opcode(client, reg, bits,
155*024b08feSTobias Sperling 					     ADS7138_OPCODE_CLEAR_BIT);
156*024b08feSTobias Sperling }
157*024b08feSTobias Sperling 
158*024b08feSTobias Sperling static int ads7138_i2c_read_block(const struct i2c_client *client, u8 reg,
159*024b08feSTobias Sperling 				  u8 *out_values, u8 length)
160*024b08feSTobias Sperling {
161*024b08feSTobias Sperling 	u8 buf[2] = { ADS7138_OPCODE_BLOCK_READ, reg };
162*024b08feSTobias Sperling 	int ret;
163*024b08feSTobias Sperling 	struct i2c_msg msgs[] = {
164*024b08feSTobias Sperling 		{
165*024b08feSTobias Sperling 			.addr = client->addr,
166*024b08feSTobias Sperling 			.len = ARRAY_SIZE(buf),
167*024b08feSTobias Sperling 			.buf = buf,
168*024b08feSTobias Sperling 		},
169*024b08feSTobias Sperling 		{
170*024b08feSTobias Sperling 			.addr = client->addr,
171*024b08feSTobias Sperling 			.flags = I2C_M_RD,
172*024b08feSTobias Sperling 			.len = length,
173*024b08feSTobias Sperling 			.buf = out_values,
174*024b08feSTobias Sperling 		},
175*024b08feSTobias Sperling 	};
176*024b08feSTobias Sperling 
177*024b08feSTobias Sperling 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
178*024b08feSTobias Sperling 	if (ret < 0)
179*024b08feSTobias Sperling 		return ret;
180*024b08feSTobias Sperling 	if (ret != ARRAY_SIZE(msgs))
181*024b08feSTobias Sperling 		return -EIO;
182*024b08feSTobias Sperling 
183*024b08feSTobias Sperling 	return 0;
184*024b08feSTobias Sperling }
185*024b08feSTobias Sperling 
186*024b08feSTobias Sperling static int ads7138_i2c_read(const struct i2c_client *client, u8 reg)
187*024b08feSTobias Sperling {
188*024b08feSTobias Sperling 	u8 value;
189*024b08feSTobias Sperling 	int ret;
190*024b08feSTobias Sperling 
191*024b08feSTobias Sperling 	ret = ads7138_i2c_read_block(client, reg, &value, sizeof(value));
192*024b08feSTobias Sperling 	if (ret)
193*024b08feSTobias Sperling 		return ret;
194*024b08feSTobias Sperling 	return value;
195*024b08feSTobias Sperling }
196*024b08feSTobias Sperling 
197*024b08feSTobias Sperling static int ads7138_freq_to_bits(int freq)
198*024b08feSTobias Sperling {
199*024b08feSTobias Sperling 	int i;
200*024b08feSTobias Sperling 
201*024b08feSTobias Sperling 	for (i = 0; i < ARRAY_SIZE(ads7138_samp_freqs_bits[0]); i++)
202*024b08feSTobias Sperling 		if (freq == ads7138_samp_freqs_bits[0][i])
203*024b08feSTobias Sperling 			return ads7138_samp_freqs_bits[1][i];
204*024b08feSTobias Sperling 
205*024b08feSTobias Sperling 	return -EINVAL;
206*024b08feSTobias Sperling }
207*024b08feSTobias Sperling 
208*024b08feSTobias Sperling static int ads7138_bits_to_freq(int bits)
209*024b08feSTobias Sperling {
210*024b08feSTobias Sperling 	int i;
211*024b08feSTobias Sperling 
212*024b08feSTobias Sperling 	for (i = 0; i < ARRAY_SIZE(ads7138_samp_freqs_bits[1]); i++)
213*024b08feSTobias Sperling 		if (bits == ads7138_samp_freqs_bits[1][i])
214*024b08feSTobias Sperling 			return ads7138_samp_freqs_bits[0][i];
215*024b08feSTobias Sperling 
216*024b08feSTobias Sperling 	return -EINVAL;
217*024b08feSTobias Sperling }
218*024b08feSTobias Sperling 
219*024b08feSTobias Sperling static int ads7138_osr_to_bits(int osr)
220*024b08feSTobias Sperling {
221*024b08feSTobias Sperling 	int i;
222*024b08feSTobias Sperling 
223*024b08feSTobias Sperling 	for (i = 0; i < ARRAY_SIZE(ads7138_oversampling_ratios); i++)
224*024b08feSTobias Sperling 		if (osr == ads7138_oversampling_ratios[i])
225*024b08feSTobias Sperling 			return i;
226*024b08feSTobias Sperling 
227*024b08feSTobias Sperling 	return -EINVAL;
228*024b08feSTobias Sperling }
229*024b08feSTobias Sperling 
230*024b08feSTobias Sperling static int ads7138_read_raw(struct iio_dev *indio_dev,
231*024b08feSTobias Sperling 			    struct iio_chan_spec const *chan, int *val,
232*024b08feSTobias Sperling 			    int *val2, long mask)
233*024b08feSTobias Sperling {
234*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
235*024b08feSTobias Sperling 	int ret, vref, bits;
236*024b08feSTobias Sperling 	u8 values[2];
237*024b08feSTobias Sperling 
238*024b08feSTobias Sperling 	switch (mask) {
239*024b08feSTobias Sperling 	case IIO_CHAN_INFO_RAW:
240*024b08feSTobias Sperling 		ret = ads7138_i2c_read_block(data->client,
241*024b08feSTobias Sperling 					     ADS7138_REG_RECENT_LSB_CH(chan->channel),
242*024b08feSTobias Sperling 					     values, ARRAY_SIZE(values));
243*024b08feSTobias Sperling 		if (ret)
244*024b08feSTobias Sperling 			return ret;
245*024b08feSTobias Sperling 
246*024b08feSTobias Sperling 		*val = get_unaligned_le16(values);
247*024b08feSTobias Sperling 		return IIO_VAL_INT;
248*024b08feSTobias Sperling 	case IIO_CHAN_INFO_PEAK:
249*024b08feSTobias Sperling 		ret = ads7138_i2c_read_block(data->client,
250*024b08feSTobias Sperling 					     ADS7138_REG_MAX_LSB_CH(chan->channel),
251*024b08feSTobias Sperling 					     values, ARRAY_SIZE(values));
252*024b08feSTobias Sperling 		if (ret)
253*024b08feSTobias Sperling 			return ret;
254*024b08feSTobias Sperling 
255*024b08feSTobias Sperling 		*val = get_unaligned_le16(values);
256*024b08feSTobias Sperling 		return IIO_VAL_INT;
257*024b08feSTobias Sperling 	case IIO_CHAN_INFO_TROUGH:
258*024b08feSTobias Sperling 		ret = ads7138_i2c_read_block(data->client,
259*024b08feSTobias Sperling 					     ADS7138_REG_MIN_LSB_CH(chan->channel),
260*024b08feSTobias Sperling 					     values, ARRAY_SIZE(values));
261*024b08feSTobias Sperling 		if (ret)
262*024b08feSTobias Sperling 			return ret;
263*024b08feSTobias Sperling 
264*024b08feSTobias Sperling 		*val = get_unaligned_le16(values);
265*024b08feSTobias Sperling 		return IIO_VAL_INT;
266*024b08feSTobias Sperling 	case IIO_CHAN_INFO_SAMP_FREQ:
267*024b08feSTobias Sperling 		ret = ads7138_i2c_read(data->client, ADS7138_REG_OPMODE_CFG);
268*024b08feSTobias Sperling 		if (ret < 0)
269*024b08feSTobias Sperling 			return ret;
270*024b08feSTobias Sperling 
271*024b08feSTobias Sperling 		bits = FIELD_GET(ADS7138_OPMODE_CFG_FREQ_MASK, ret);
272*024b08feSTobias Sperling 		*val = ads7138_bits_to_freq(bits);
273*024b08feSTobias Sperling 		return IIO_VAL_INT;
274*024b08feSTobias Sperling 	case IIO_CHAN_INFO_SCALE:
275*024b08feSTobias Sperling 		vref = regulator_get_voltage(data->vref_regu);
276*024b08feSTobias Sperling 		if (vref < 0)
277*024b08feSTobias Sperling 			return vref;
278*024b08feSTobias Sperling 		*val = vref / 1000;
279*024b08feSTobias Sperling 		*val2 = ADS7138_RES_BITS;
280*024b08feSTobias Sperling 		return IIO_VAL_FRACTIONAL_LOG2;
281*024b08feSTobias Sperling 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
282*024b08feSTobias Sperling 		ret = ads7138_i2c_read(data->client, ADS7138_REG_OSR_CFG);
283*024b08feSTobias Sperling 		if (ret < 0)
284*024b08feSTobias Sperling 			return ret;
285*024b08feSTobias Sperling 
286*024b08feSTobias Sperling 		bits = FIELD_GET(ADS7138_OSR_CFG_MASK, ret);
287*024b08feSTobias Sperling 		*val = ads7138_oversampling_ratios[bits];
288*024b08feSTobias Sperling 		return IIO_VAL_INT;
289*024b08feSTobias Sperling 	default:
290*024b08feSTobias Sperling 		return -EINVAL;
291*024b08feSTobias Sperling 	}
292*024b08feSTobias Sperling }
293*024b08feSTobias Sperling 
294*024b08feSTobias Sperling static int ads7138_write_raw(struct iio_dev *indio_dev,
295*024b08feSTobias Sperling 			     struct iio_chan_spec const *chan, int val,
296*024b08feSTobias Sperling 			     int val2, long mask)
297*024b08feSTobias Sperling {
298*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
299*024b08feSTobias Sperling 	int bits, ret;
300*024b08feSTobias Sperling 	u8 value;
301*024b08feSTobias Sperling 
302*024b08feSTobias Sperling 	switch (mask) {
303*024b08feSTobias Sperling 	case IIO_CHAN_INFO_SAMP_FREQ: {
304*024b08feSTobias Sperling 		bits = ads7138_freq_to_bits(val);
305*024b08feSTobias Sperling 		if (bits < 0)
306*024b08feSTobias Sperling 			return bits;
307*024b08feSTobias Sperling 
308*024b08feSTobias Sperling 		guard(mutex)(&data->lock);
309*024b08feSTobias Sperling 		ret = ads7138_i2c_read(data->client, ADS7138_REG_OPMODE_CFG);
310*024b08feSTobias Sperling 		if (ret < 0)
311*024b08feSTobias Sperling 			return ret;
312*024b08feSTobias Sperling 
313*024b08feSTobias Sperling 		value = ret & ~ADS7138_OPMODE_CFG_FREQ_MASK;
314*024b08feSTobias Sperling 		value |= FIELD_PREP(ADS7138_OPMODE_CFG_FREQ_MASK, bits);
315*024b08feSTobias Sperling 		return ads7138_i2c_write(data->client, ADS7138_REG_OPMODE_CFG,
316*024b08feSTobias Sperling 					 value);
317*024b08feSTobias Sperling 	}
318*024b08feSTobias Sperling 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
319*024b08feSTobias Sperling 		bits = ads7138_osr_to_bits(val);
320*024b08feSTobias Sperling 		if (bits < 0)
321*024b08feSTobias Sperling 			return bits;
322*024b08feSTobias Sperling 
323*024b08feSTobias Sperling 		return ads7138_i2c_write(data->client, ADS7138_REG_OSR_CFG,
324*024b08feSTobias Sperling 					 bits);
325*024b08feSTobias Sperling 	default:
326*024b08feSTobias Sperling 		return -EINVAL;
327*024b08feSTobias Sperling 	}
328*024b08feSTobias Sperling }
329*024b08feSTobias Sperling 
330*024b08feSTobias Sperling static int ads7138_read_event(struct iio_dev *indio_dev,
331*024b08feSTobias Sperling 			      const struct iio_chan_spec *chan,
332*024b08feSTobias Sperling 			      enum iio_event_type type,
333*024b08feSTobias Sperling 			      enum iio_event_direction dir,
334*024b08feSTobias Sperling 			      enum iio_event_info info, int *val, int *val2)
335*024b08feSTobias Sperling {
336*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
337*024b08feSTobias Sperling 	u8 reg, values[2];
338*024b08feSTobias Sperling 	int ret;
339*024b08feSTobias Sperling 
340*024b08feSTobias Sperling 	switch (info) {
341*024b08feSTobias Sperling 	case IIO_EV_INFO_VALUE:
342*024b08feSTobias Sperling 		reg = (dir == IIO_EV_DIR_RISING) ?
343*024b08feSTobias Sperling 			ADS7138_REG_HIGH_TH_HYS_CH(chan->channel) :
344*024b08feSTobias Sperling 			ADS7138_REG_LOW_TH_CNT_CH(chan->channel);
345*024b08feSTobias Sperling 		ret = ads7138_i2c_read_block(data->client, reg, values,
346*024b08feSTobias Sperling 					     ARRAY_SIZE(values));
347*024b08feSTobias Sperling 		if (ret)
348*024b08feSTobias Sperling 			return ret;
349*024b08feSTobias Sperling 
350*024b08feSTobias Sperling 		*val = ((values[1] << 4) | (values[0] >> 4));
351*024b08feSTobias Sperling 		return IIO_VAL_INT;
352*024b08feSTobias Sperling 	case IIO_EV_INFO_HYSTERESIS:
353*024b08feSTobias Sperling 		ret = ads7138_i2c_read(data->client,
354*024b08feSTobias Sperling 				       ADS7138_REG_HIGH_TH_HYS_CH(chan->channel));
355*024b08feSTobias Sperling 		if (ret < 0)
356*024b08feSTobias Sperling 			return ret;
357*024b08feSTobias Sperling 
358*024b08feSTobias Sperling 		*val = ret & ~ADS7138_THRESHOLD_LSB_MASK;
359*024b08feSTobias Sperling 		return IIO_VAL_INT;
360*024b08feSTobias Sperling 	default:
361*024b08feSTobias Sperling 		return -EINVAL;
362*024b08feSTobias Sperling 	}
363*024b08feSTobias Sperling }
364*024b08feSTobias Sperling 
365*024b08feSTobias Sperling static int ads7138_write_event(struct iio_dev *indio_dev,
366*024b08feSTobias Sperling 			       const struct iio_chan_spec *chan,
367*024b08feSTobias Sperling 			       enum iio_event_type type,
368*024b08feSTobias Sperling 			       enum iio_event_direction dir,
369*024b08feSTobias Sperling 			       enum iio_event_info info, int val, int val2)
370*024b08feSTobias Sperling {
371*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
372*024b08feSTobias Sperling 	u8 reg, values[2];
373*024b08feSTobias Sperling 	int ret;
374*024b08feSTobias Sperling 
375*024b08feSTobias Sperling 	switch (info) {
376*024b08feSTobias Sperling 	case IIO_EV_INFO_VALUE: {
377*024b08feSTobias Sperling 		if (val >= BIT(12) || val < 0)
378*024b08feSTobias Sperling 			return -EINVAL;
379*024b08feSTobias Sperling 
380*024b08feSTobias Sperling 		reg = (dir == IIO_EV_DIR_RISING) ?
381*024b08feSTobias Sperling 			ADS7138_REG_HIGH_TH_HYS_CH(chan->channel) :
382*024b08feSTobias Sperling 			ADS7138_REG_LOW_TH_CNT_CH(chan->channel);
383*024b08feSTobias Sperling 
384*024b08feSTobias Sperling 		guard(mutex)(&data->lock);
385*024b08feSTobias Sperling 		ret = ads7138_i2c_read(data->client, reg);
386*024b08feSTobias Sperling 		if (ret < 0)
387*024b08feSTobias Sperling 			return ret;
388*024b08feSTobias Sperling 
389*024b08feSTobias Sperling 		values[0] = ret & ~ADS7138_THRESHOLD_LSB_MASK;
390*024b08feSTobias Sperling 		values[0] |= FIELD_PREP(ADS7138_THRESHOLD_LSB_MASK, val);
391*024b08feSTobias Sperling 		values[1] = (val >> 4);
392*024b08feSTobias Sperling 		return ads7138_i2c_write_block(data->client, reg, values,
393*024b08feSTobias Sperling 					       ARRAY_SIZE(values));
394*024b08feSTobias Sperling 	}
395*024b08feSTobias Sperling 	case IIO_EV_INFO_HYSTERESIS: {
396*024b08feSTobias Sperling 		if (val >= BIT(4) || val < 0)
397*024b08feSTobias Sperling 			return -EINVAL;
398*024b08feSTobias Sperling 
399*024b08feSTobias Sperling 		reg = ADS7138_REG_HIGH_TH_HYS_CH(chan->channel);
400*024b08feSTobias Sperling 
401*024b08feSTobias Sperling 		guard(mutex)(&data->lock);
402*024b08feSTobias Sperling 		ret = ads7138_i2c_read(data->client, reg);
403*024b08feSTobias Sperling 		if (ret < 0)
404*024b08feSTobias Sperling 			return ret;
405*024b08feSTobias Sperling 
406*024b08feSTobias Sperling 		values[0] = val & ~ADS7138_THRESHOLD_LSB_MASK;
407*024b08feSTobias Sperling 		values[0] |= FIELD_PREP(ADS7138_THRESHOLD_LSB_MASK, ret >> 4);
408*024b08feSTobias Sperling 		return ads7138_i2c_write(data->client, reg, values[0]);
409*024b08feSTobias Sperling 	}
410*024b08feSTobias Sperling 	default:
411*024b08feSTobias Sperling 		return -EINVAL;
412*024b08feSTobias Sperling 	}
413*024b08feSTobias Sperling }
414*024b08feSTobias Sperling 
415*024b08feSTobias Sperling static int ads7138_read_event_config(struct iio_dev *indio_dev,
416*024b08feSTobias Sperling 				     const struct iio_chan_spec *chan,
417*024b08feSTobias Sperling 				     enum iio_event_type type,
418*024b08feSTobias Sperling 				     enum iio_event_direction dir)
419*024b08feSTobias Sperling {
420*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
421*024b08feSTobias Sperling 	int ret;
422*024b08feSTobias Sperling 
423*024b08feSTobias Sperling 	if (dir != IIO_EV_DIR_EITHER)
424*024b08feSTobias Sperling 		return -EINVAL;
425*024b08feSTobias Sperling 
426*024b08feSTobias Sperling 	ret = ads7138_i2c_read(data->client, ADS7138_REG_ALERT_CH_SEL);
427*024b08feSTobias Sperling 	if (ret < 0)
428*024b08feSTobias Sperling 		return ret;
429*024b08feSTobias Sperling 
430*024b08feSTobias Sperling 	return (ret & BIT(chan->channel)) ? 1 : 0;
431*024b08feSTobias Sperling }
432*024b08feSTobias Sperling 
433*024b08feSTobias Sperling static int ads7138_write_event_config(struct iio_dev *indio_dev,
434*024b08feSTobias Sperling 				      const struct iio_chan_spec *chan,
435*024b08feSTobias Sperling 				      enum iio_event_type type,
436*024b08feSTobias Sperling 				      enum iio_event_direction dir, bool state)
437*024b08feSTobias Sperling {
438*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
439*024b08feSTobias Sperling 
440*024b08feSTobias Sperling 	if (dir != IIO_EV_DIR_EITHER)
441*024b08feSTobias Sperling 		return -EINVAL;
442*024b08feSTobias Sperling 
443*024b08feSTobias Sperling 	if (state)
444*024b08feSTobias Sperling 		return ads7138_i2c_set_bit(data->client,
445*024b08feSTobias Sperling 					   ADS7138_REG_ALERT_CH_SEL,
446*024b08feSTobias Sperling 					   BIT(chan->channel));
447*024b08feSTobias Sperling 	else
448*024b08feSTobias Sperling 		return ads7138_i2c_clear_bit(data->client,
449*024b08feSTobias Sperling 					     ADS7138_REG_ALERT_CH_SEL,
450*024b08feSTobias Sperling 					     BIT(chan->channel));
451*024b08feSTobias Sperling }
452*024b08feSTobias Sperling 
453*024b08feSTobias Sperling static int ads7138_read_avail(struct iio_dev *indio_dev,
454*024b08feSTobias Sperling 			      struct iio_chan_spec const *chan,
455*024b08feSTobias Sperling 			      const int **vals, int *type, int *length,
456*024b08feSTobias Sperling 			      long mask)
457*024b08feSTobias Sperling {
458*024b08feSTobias Sperling 	switch (mask) {
459*024b08feSTobias Sperling 	case IIO_CHAN_INFO_SAMP_FREQ:
460*024b08feSTobias Sperling 		*vals = ads7138_samp_freqs_bits[0];
461*024b08feSTobias Sperling 		*length = ARRAY_SIZE(ads7138_samp_freqs_bits[0]);
462*024b08feSTobias Sperling 		*type = IIO_VAL_INT;
463*024b08feSTobias Sperling 
464*024b08feSTobias Sperling 		return IIO_AVAIL_LIST;
465*024b08feSTobias Sperling 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
466*024b08feSTobias Sperling 		*vals = ads7138_oversampling_ratios;
467*024b08feSTobias Sperling 		*length = ARRAY_SIZE(ads7138_oversampling_ratios);
468*024b08feSTobias Sperling 		*type = IIO_VAL_INT;
469*024b08feSTobias Sperling 
470*024b08feSTobias Sperling 		return IIO_AVAIL_LIST;
471*024b08feSTobias Sperling 	default:
472*024b08feSTobias Sperling 		return -EINVAL;
473*024b08feSTobias Sperling 	}
474*024b08feSTobias Sperling }
475*024b08feSTobias Sperling 
476*024b08feSTobias Sperling static const struct iio_info ti_ads7138_info = {
477*024b08feSTobias Sperling 	.read_raw = &ads7138_read_raw,
478*024b08feSTobias Sperling 	.read_avail = &ads7138_read_avail,
479*024b08feSTobias Sperling 	.write_raw = &ads7138_write_raw,
480*024b08feSTobias Sperling 	.read_event_value = &ads7138_read_event,
481*024b08feSTobias Sperling 	.write_event_value = &ads7138_write_event,
482*024b08feSTobias Sperling 	.read_event_config = &ads7138_read_event_config,
483*024b08feSTobias Sperling 	.write_event_config = &ads7138_write_event_config,
484*024b08feSTobias Sperling };
485*024b08feSTobias Sperling 
486*024b08feSTobias Sperling static const struct iio_event_spec ads7138_events[] = {
487*024b08feSTobias Sperling 	{
488*024b08feSTobias Sperling 		.type = IIO_EV_TYPE_THRESH,
489*024b08feSTobias Sperling 		.dir = IIO_EV_DIR_RISING,
490*024b08feSTobias Sperling 		.mask_separate = BIT(IIO_EV_INFO_VALUE)
491*024b08feSTobias Sperling 	}, {
492*024b08feSTobias Sperling 		.type = IIO_EV_TYPE_THRESH,
493*024b08feSTobias Sperling 		.dir = IIO_EV_DIR_FALLING,
494*024b08feSTobias Sperling 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
495*024b08feSTobias Sperling 	}, {
496*024b08feSTobias Sperling 		.type = IIO_EV_TYPE_THRESH,
497*024b08feSTobias Sperling 		.dir = IIO_EV_DIR_EITHER,
498*024b08feSTobias Sperling 		.mask_separate = BIT(IIO_EV_INFO_HYSTERESIS) |
499*024b08feSTobias Sperling 				 BIT(IIO_EV_INFO_ENABLE),
500*024b08feSTobias Sperling 	},
501*024b08feSTobias Sperling };
502*024b08feSTobias Sperling 
503*024b08feSTobias Sperling #define ADS7138_V_CHAN(_chan) {						\
504*024b08feSTobias Sperling 	.type = IIO_VOLTAGE,						\
505*024b08feSTobias Sperling 	.indexed = 1,							\
506*024b08feSTobias Sperling 	.channel = _chan,						\
507*024b08feSTobias Sperling 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |			\
508*024b08feSTobias Sperling 			      BIT(IIO_CHAN_INFO_PEAK) |			\
509*024b08feSTobias Sperling 			      BIT(IIO_CHAN_INFO_TROUGH),		\
510*024b08feSTobias Sperling 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
511*024b08feSTobias Sperling 				    BIT(IIO_CHAN_INFO_SCALE) |		\
512*024b08feSTobias Sperling 				    BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
513*024b08feSTobias Sperling 	.info_mask_shared_by_type_available =				\
514*024b08feSTobias Sperling 				BIT(IIO_CHAN_INFO_SAMP_FREQ) |		\
515*024b08feSTobias Sperling 				BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),	\
516*024b08feSTobias Sperling 	.datasheet_name = "AIN"#_chan,					\
517*024b08feSTobias Sperling 	.event_spec = ads7138_events,					\
518*024b08feSTobias Sperling 	.num_event_specs = ARRAY_SIZE(ads7138_events),			\
519*024b08feSTobias Sperling }
520*024b08feSTobias Sperling 
521*024b08feSTobias Sperling static const struct iio_chan_spec ads7138_channels[] = {
522*024b08feSTobias Sperling 	ADS7138_V_CHAN(0),
523*024b08feSTobias Sperling 	ADS7138_V_CHAN(1),
524*024b08feSTobias Sperling 	ADS7138_V_CHAN(2),
525*024b08feSTobias Sperling 	ADS7138_V_CHAN(3),
526*024b08feSTobias Sperling 	ADS7138_V_CHAN(4),
527*024b08feSTobias Sperling 	ADS7138_V_CHAN(5),
528*024b08feSTobias Sperling 	ADS7138_V_CHAN(6),
529*024b08feSTobias Sperling 	ADS7138_V_CHAN(7),
530*024b08feSTobias Sperling };
531*024b08feSTobias Sperling 
532*024b08feSTobias Sperling static irqreturn_t ads7138_event_handler(int irq, void *priv)
533*024b08feSTobias Sperling {
534*024b08feSTobias Sperling 	struct iio_dev *indio_dev = priv;
535*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
536*024b08feSTobias Sperling 	struct device *dev = &data->client->dev;
537*024b08feSTobias Sperling 	u8 i, events_high, events_low;
538*024b08feSTobias Sperling 	u64 code;
539*024b08feSTobias Sperling 	int ret;
540*024b08feSTobias Sperling 
541*024b08feSTobias Sperling 	/* Check if interrupt was trigger by us */
542*024b08feSTobias Sperling 	ret = ads7138_i2c_read(data->client, ADS7138_REG_EVENT_FLAG);
543*024b08feSTobias Sperling 	if (ret <= 0)
544*024b08feSTobias Sperling 		return IRQ_NONE;
545*024b08feSTobias Sperling 
546*024b08feSTobias Sperling 	ret = ads7138_i2c_read(data->client, ADS7138_REG_EVENT_HIGH_FLAG);
547*024b08feSTobias Sperling 	if (ret < 0) {
548*024b08feSTobias Sperling 		dev_warn(dev, "Failed to read event high flags: %d\n", ret);
549*024b08feSTobias Sperling 		return IRQ_HANDLED;
550*024b08feSTobias Sperling 	}
551*024b08feSTobias Sperling 	events_high = ret;
552*024b08feSTobias Sperling 
553*024b08feSTobias Sperling 	ret = ads7138_i2c_read(data->client, ADS7138_REG_EVENT_LOW_FLAG);
554*024b08feSTobias Sperling 	if (ret < 0) {
555*024b08feSTobias Sperling 		dev_warn(dev, "Failed to read event low flags: %d\n", ret);
556*024b08feSTobias Sperling 		return IRQ_HANDLED;
557*024b08feSTobias Sperling 	}
558*024b08feSTobias Sperling 	events_low = ret;
559*024b08feSTobias Sperling 
560*024b08feSTobias Sperling 	for (i = 0; i < data->chip_data->channel_num; i++) {
561*024b08feSTobias Sperling 		if (events_high & BIT(i)) {
562*024b08feSTobias Sperling 			code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
563*024b08feSTobias Sperling 						    IIO_EV_TYPE_THRESH,
564*024b08feSTobias Sperling 						    IIO_EV_DIR_RISING);
565*024b08feSTobias Sperling 			iio_push_event(indio_dev, code,
566*024b08feSTobias Sperling 				       iio_get_time_ns(indio_dev));
567*024b08feSTobias Sperling 		}
568*024b08feSTobias Sperling 		if (events_low & BIT(i)) {
569*024b08feSTobias Sperling 			code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
570*024b08feSTobias Sperling 						    IIO_EV_TYPE_THRESH,
571*024b08feSTobias Sperling 						    IIO_EV_DIR_FALLING);
572*024b08feSTobias Sperling 			iio_push_event(indio_dev, code,
573*024b08feSTobias Sperling 				       iio_get_time_ns(indio_dev));
574*024b08feSTobias Sperling 		}
575*024b08feSTobias Sperling 	}
576*024b08feSTobias Sperling 
577*024b08feSTobias Sperling 	/* Try to clear all interrupt flags */
578*024b08feSTobias Sperling 	ret = ads7138_i2c_write(data->client, ADS7138_REG_EVENT_HIGH_FLAG, 0xFF);
579*024b08feSTobias Sperling 	if (ret)
580*024b08feSTobias Sperling 		dev_warn(dev, "Failed to clear event high flags: %d\n", ret);
581*024b08feSTobias Sperling 
582*024b08feSTobias Sperling 	ret = ads7138_i2c_write(data->client, ADS7138_REG_EVENT_LOW_FLAG, 0xFF);
583*024b08feSTobias Sperling 	if (ret)
584*024b08feSTobias Sperling 		dev_warn(dev, "Failed to clear event low flags: %d\n", ret);
585*024b08feSTobias Sperling 
586*024b08feSTobias Sperling 	return IRQ_HANDLED;
587*024b08feSTobias Sperling }
588*024b08feSTobias Sperling 
589*024b08feSTobias Sperling static int ads7138_set_conv_mode(struct ads7138_data *data,
590*024b08feSTobias Sperling 				 enum ads7138_modes mode)
591*024b08feSTobias Sperling {
592*024b08feSTobias Sperling 	if (mode == ADS7138_MODE_AUTO)
593*024b08feSTobias Sperling 		return ads7138_i2c_set_bit(data->client, ADS7138_REG_OPMODE_CFG,
594*024b08feSTobias Sperling 					   ADS7138_OPMODE_CFG_CONV_MODE);
595*024b08feSTobias Sperling 	return ads7138_i2c_clear_bit(data->client, ADS7138_REG_OPMODE_CFG,
596*024b08feSTobias Sperling 				     ADS7138_OPMODE_CFG_CONV_MODE);
597*024b08feSTobias Sperling }
598*024b08feSTobias Sperling 
599*024b08feSTobias Sperling static int ads7138_init_hw(struct ads7138_data *data)
600*024b08feSTobias Sperling {
601*024b08feSTobias Sperling 	struct device *dev = &data->client->dev;
602*024b08feSTobias Sperling 	int ret;
603*024b08feSTobias Sperling 
604*024b08feSTobias Sperling 	data->vref_regu = devm_regulator_get(dev, "avdd");
605*024b08feSTobias Sperling 	if (IS_ERR(data->vref_regu))
606*024b08feSTobias Sperling 		return dev_err_probe(dev, PTR_ERR(data->vref_regu),
607*024b08feSTobias Sperling 				     "Failed to get avdd regulator\n");
608*024b08feSTobias Sperling 
609*024b08feSTobias Sperling 	ret = regulator_get_voltage(data->vref_regu);
610*024b08feSTobias Sperling 	if (ret < 0)
611*024b08feSTobias Sperling 		return dev_err_probe(dev, ret, "Failed to get avdd voltage\n");
612*024b08feSTobias Sperling 
613*024b08feSTobias Sperling 	/* Reset the chip to get a defined starting configuration */
614*024b08feSTobias Sperling 	ret = ads7138_i2c_set_bit(data->client, ADS7138_REG_GENERAL_CFG,
615*024b08feSTobias Sperling 				  ADS7138_GENERAL_CFG_RST);
616*024b08feSTobias Sperling 	if (ret)
617*024b08feSTobias Sperling 		return ret;
618*024b08feSTobias Sperling 
619*024b08feSTobias Sperling 	ret = ads7138_set_conv_mode(data, ADS7138_MODE_AUTO);
620*024b08feSTobias Sperling 	if (ret)
621*024b08feSTobias Sperling 		return ret;
622*024b08feSTobias Sperling 
623*024b08feSTobias Sperling 	/* Enable statistics and digital window comparator */
624*024b08feSTobias Sperling 	ret = ads7138_i2c_set_bit(data->client, ADS7138_REG_GENERAL_CFG,
625*024b08feSTobias Sperling 				  ADS7138_GENERAL_CFG_STATS_EN |
626*024b08feSTobias Sperling 				  ADS7138_GENERAL_CFG_DWC_EN);
627*024b08feSTobias Sperling 	if (ret)
628*024b08feSTobias Sperling 		return ret;
629*024b08feSTobias Sperling 
630*024b08feSTobias Sperling 	/* Enable all channels for auto sequencing */
631*024b08feSTobias Sperling 	ret = ads7138_i2c_set_bit(data->client, ADS7138_REG_AUTO_SEQ_CH_SEL, 0xFF);
632*024b08feSTobias Sperling 	if (ret)
633*024b08feSTobias Sperling 		return ret;
634*024b08feSTobias Sperling 
635*024b08feSTobias Sperling 	/* Set auto sequence mode and start sequencing */
636*024b08feSTobias Sperling 	return ads7138_i2c_set_bit(data->client, ADS7138_REG_SEQUENCE_CFG,
637*024b08feSTobias Sperling 				   ADS7138_SEQUENCE_CFG_SEQ_START |
638*024b08feSTobias Sperling 				   ADS7138_SEQUENCE_CFG_SEQ_MODE);
639*024b08feSTobias Sperling }
640*024b08feSTobias Sperling 
641*024b08feSTobias Sperling static int ads7138_probe(struct i2c_client *client)
642*024b08feSTobias Sperling {
643*024b08feSTobias Sperling 	struct device *dev = &client->dev;
644*024b08feSTobias Sperling 	struct iio_dev *indio_dev;
645*024b08feSTobias Sperling 	struct ads7138_data *data;
646*024b08feSTobias Sperling 	int ret;
647*024b08feSTobias Sperling 
648*024b08feSTobias Sperling 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
649*024b08feSTobias Sperling 	if (!indio_dev)
650*024b08feSTobias Sperling 		return -ENOMEM;
651*024b08feSTobias Sperling 
652*024b08feSTobias Sperling 	data = iio_priv(indio_dev);
653*024b08feSTobias Sperling 	data->client = client;
654*024b08feSTobias Sperling 	data->chip_data = i2c_get_match_data(client);
655*024b08feSTobias Sperling 	if (!data->chip_data)
656*024b08feSTobias Sperling 		return -ENODEV;
657*024b08feSTobias Sperling 
658*024b08feSTobias Sperling 	ret = devm_mutex_init(dev, &data->lock);
659*024b08feSTobias Sperling 	if (ret)
660*024b08feSTobias Sperling 		return ret;
661*024b08feSTobias Sperling 
662*024b08feSTobias Sperling 	indio_dev->name = data->chip_data->name;
663*024b08feSTobias Sperling 	indio_dev->modes = INDIO_DIRECT_MODE;
664*024b08feSTobias Sperling 	indio_dev->channels = ads7138_channels;
665*024b08feSTobias Sperling 	indio_dev->num_channels = ARRAY_SIZE(ads7138_channels);
666*024b08feSTobias Sperling 	indio_dev->info = &ti_ads7138_info;
667*024b08feSTobias Sperling 
668*024b08feSTobias Sperling 	i2c_set_clientdata(client, indio_dev);
669*024b08feSTobias Sperling 
670*024b08feSTobias Sperling 	if (client->irq > 0) {
671*024b08feSTobias Sperling 		ret = devm_request_threaded_irq(dev, client->irq,
672*024b08feSTobias Sperling 						NULL, ads7138_event_handler,
673*024b08feSTobias Sperling 						IRQF_TRIGGER_LOW |
674*024b08feSTobias Sperling 						IRQF_ONESHOT | IRQF_SHARED,
675*024b08feSTobias Sperling 						client->name, indio_dev);
676*024b08feSTobias Sperling 		if (ret)
677*024b08feSTobias Sperling 			return ret;
678*024b08feSTobias Sperling 	}
679*024b08feSTobias Sperling 
680*024b08feSTobias Sperling 	ret = ads7138_init_hw(data);
681*024b08feSTobias Sperling 	if (ret)
682*024b08feSTobias Sperling 		return dev_err_probe(dev, ret, "Failed to initialize device\n");
683*024b08feSTobias Sperling 
684*024b08feSTobias Sperling 	ret = devm_iio_device_register(dev, indio_dev);
685*024b08feSTobias Sperling 	if (ret)
686*024b08feSTobias Sperling 		return dev_err_probe(dev, ret, "Failed to register iio device\n");
687*024b08feSTobias Sperling 
688*024b08feSTobias Sperling 	return 0;
689*024b08feSTobias Sperling }
690*024b08feSTobias Sperling 
691*024b08feSTobias Sperling static int ads7138_runtime_suspend(struct device *dev)
692*024b08feSTobias Sperling {
693*024b08feSTobias Sperling 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
694*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
695*024b08feSTobias Sperling 
696*024b08feSTobias Sperling 	return ads7138_set_conv_mode(data, ADS7138_MODE_MANUAL);
697*024b08feSTobias Sperling }
698*024b08feSTobias Sperling 
699*024b08feSTobias Sperling static int ads7138_runtime_resume(struct device *dev)
700*024b08feSTobias Sperling {
701*024b08feSTobias Sperling 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
702*024b08feSTobias Sperling 	struct ads7138_data *data = iio_priv(indio_dev);
703*024b08feSTobias Sperling 
704*024b08feSTobias Sperling 	return ads7138_set_conv_mode(data, ADS7138_MODE_AUTO);
705*024b08feSTobias Sperling }
706*024b08feSTobias Sperling 
707*024b08feSTobias Sperling static DEFINE_RUNTIME_DEV_PM_OPS(ads7138_pm_ops,
708*024b08feSTobias Sperling 				 ads7138_runtime_suspend,
709*024b08feSTobias Sperling 				 ads7138_runtime_resume,
710*024b08feSTobias Sperling 				 NULL);
711*024b08feSTobias Sperling 
712*024b08feSTobias Sperling static const struct ads7138_chip_data ads7128_data = {
713*024b08feSTobias Sperling 	.name = "ads7128",
714*024b08feSTobias Sperling 	.channel_num = 8,
715*024b08feSTobias Sperling };
716*024b08feSTobias Sperling 
717*024b08feSTobias Sperling static const struct ads7138_chip_data ads7138_data = {
718*024b08feSTobias Sperling 	.name = "ads7138",
719*024b08feSTobias Sperling 	.channel_num = 8,
720*024b08feSTobias Sperling };
721*024b08feSTobias Sperling 
722*024b08feSTobias Sperling static const struct of_device_id ads7138_of_match[] = {
723*024b08feSTobias Sperling 	{ .compatible = "ti,ads7128", .data = &ads7128_data },
724*024b08feSTobias Sperling 	{ .compatible = "ti,ads7138", .data = &ads7138_data },
725*024b08feSTobias Sperling 	{ }
726*024b08feSTobias Sperling };
727*024b08feSTobias Sperling MODULE_DEVICE_TABLE(of, ads7138_of_match);
728*024b08feSTobias Sperling 
729*024b08feSTobias Sperling static const struct i2c_device_id ads7138_device_ids[] = {
730*024b08feSTobias Sperling 	{ "ads7128", (kernel_ulong_t)&ads7128_data },
731*024b08feSTobias Sperling 	{ "ads7138", (kernel_ulong_t)&ads7138_data },
732*024b08feSTobias Sperling 	{ }
733*024b08feSTobias Sperling };
734*024b08feSTobias Sperling MODULE_DEVICE_TABLE(i2c, ads7138_device_ids);
735*024b08feSTobias Sperling 
736*024b08feSTobias Sperling static struct i2c_driver ads7138_driver = {
737*024b08feSTobias Sperling 	.driver = {
738*024b08feSTobias Sperling 		.name = "ads7138",
739*024b08feSTobias Sperling 		.of_match_table = ads7138_of_match,
740*024b08feSTobias Sperling 		.pm = pm_ptr(&ads7138_pm_ops),
741*024b08feSTobias Sperling 	},
742*024b08feSTobias Sperling 	.id_table = ads7138_device_ids,
743*024b08feSTobias Sperling 	.probe = ads7138_probe,
744*024b08feSTobias Sperling };
745*024b08feSTobias Sperling module_i2c_driver(ads7138_driver);
746*024b08feSTobias Sperling 
747*024b08feSTobias Sperling MODULE_LICENSE("GPL");
748*024b08feSTobias Sperling MODULE_AUTHOR("Tobias Sperling <tobias.sperling@softing.com>");
749*024b08feSTobias Sperling MODULE_DESCRIPTION("Driver for TI ADS7138 ADCs");
750