xref: /linux/drivers/iio/adc/ad7791.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AD7787/AD7788/AD7789/AD7790/AD7791 SPI ADC driver
4  *
5  * Copyright 2012 Analog Devices Inc.
6  *  Author: Lars-Peter Clausen <lars@metafoo.de>
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/delay.h>
19 #include <linux/module.h>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/adc/ad_sigma_delta.h>
28 
29 #include <linux/platform_data/ad7791.h>
30 
31 #define AD7791_REG_COMM			0x0 /* For writes */
32 #define AD7791_REG_STATUS		0x0 /* For reads */
33 #define AD7791_REG_MODE			0x1
34 #define AD7791_REG_FILTER		0x2
35 #define AD7791_REG_DATA			0x3
36 
37 #define AD7791_MODE_CONTINUOUS		0x00
38 #define AD7791_MODE_SINGLE		0x02
39 #define AD7791_MODE_POWERDOWN		0x03
40 
41 #define AD7791_CH_AIN1P_AIN1N		0x00
42 #define AD7791_CH_AIN2			0x01
43 #define AD7791_CH_AIN1N_AIN1N		0x02
44 #define AD7791_CH_AVDD_MONITOR		0x03
45 
46 #define AD7791_FILTER_CLK_DIV_1		(0x0 << 4)
47 #define AD7791_FILTER_CLK_DIV_2		(0x1 << 4)
48 #define AD7791_FILTER_CLK_DIV_4		(0x2 << 4)
49 #define AD7791_FILTER_CLK_DIV_8		(0x3 << 4)
50 #define AD7791_FILTER_CLK_MASK		(0x3 << 4)
51 #define AD7791_FILTER_RATE_120		0x0
52 #define AD7791_FILTER_RATE_100		0x1
53 #define AD7791_FILTER_RATE_33_3		0x2
54 #define AD7791_FILTER_RATE_20		0x3
55 #define AD7791_FILTER_RATE_16_6		0x4
56 #define AD7791_FILTER_RATE_16_7		0x5
57 #define AD7791_FILTER_RATE_13_3		0x6
58 #define AD7791_FILTER_RATE_9_5		0x7
59 #define AD7791_FILTER_RATE_MASK		0x7
60 
61 #define AD7791_MODE_BUFFER		BIT(1)
62 #define AD7791_MODE_UNIPOLAR		BIT(2)
63 #define AD7791_MODE_BURNOUT		BIT(3)
64 #define AD7791_MODE_SEL_MASK		(0x3 << 6)
65 #define AD7791_MODE_SEL(x)		((x) << 6)
66 
67 #define DECLARE_AD7787_CHANNELS(name, bits, storagebits) \
68 const struct iio_chan_spec name[] = { \
69 	AD_SD_DIFF_CHANNEL(0, 0, 0, AD7791_CH_AIN1P_AIN1N, \
70 		(bits), (storagebits), 0), \
71 	AD_SD_CHANNEL(1, 1, AD7791_CH_AIN2, (bits), (storagebits), 0), \
72 	AD_SD_SHORTED_CHANNEL(2, 0, AD7791_CH_AIN1N_AIN1N, \
73 		(bits), (storagebits), 0), \
74 	AD_SD_SUPPLY_CHANNEL(3, 2, AD7791_CH_AVDD_MONITOR,  \
75 		(bits), (storagebits), 0), \
76 	IIO_CHAN_SOFT_TIMESTAMP(4), \
77 }
78 
79 #define DECLARE_AD7791_CHANNELS(name, bits, storagebits) \
80 const struct iio_chan_spec name[] = { \
81 	AD_SD_DIFF_CHANNEL(0, 0, 0, AD7791_CH_AIN1P_AIN1N, \
82 		(bits), (storagebits), 0), \
83 	AD_SD_SHORTED_CHANNEL(1, 0, AD7791_CH_AIN1N_AIN1N, \
84 		(bits), (storagebits), 0), \
85 	AD_SD_SUPPLY_CHANNEL(2, 1, AD7791_CH_AVDD_MONITOR, \
86 		(bits), (storagebits), 0), \
87 	IIO_CHAN_SOFT_TIMESTAMP(3), \
88 }
89 
90 static DECLARE_AD7787_CHANNELS(ad7787_channels, 24, 32);
91 static DECLARE_AD7791_CHANNELS(ad7790_channels, 16, 16);
92 static DECLARE_AD7791_CHANNELS(ad7791_channels, 24, 32);
93 
94 enum {
95 	AD7787,
96 	AD7788,
97 	AD7789,
98 	AD7790,
99 	AD7791,
100 };
101 
102 enum ad7791_chip_info_flags {
103 	AD7791_FLAG_HAS_FILTER		= (1 << 0),
104 	AD7791_FLAG_HAS_BUFFER		= (1 << 1),
105 	AD7791_FLAG_HAS_UNIPOLAR	= (1 << 2),
106 	AD7791_FLAG_HAS_BURNOUT		= (1 << 3),
107 };
108 
109 struct ad7791_chip_info {
110 	const struct iio_chan_spec *channels;
111 	unsigned int num_channels;
112 	enum ad7791_chip_info_flags flags;
113 };
114 
115 static const struct ad7791_chip_info ad7791_chip_infos[] = {
116 	[AD7787] = {
117 		.channels = ad7787_channels,
118 		.num_channels = ARRAY_SIZE(ad7787_channels),
119 		.flags = AD7791_FLAG_HAS_FILTER | AD7791_FLAG_HAS_BUFFER |
120 			AD7791_FLAG_HAS_UNIPOLAR | AD7791_FLAG_HAS_BURNOUT,
121 	},
122 	[AD7788] = {
123 		.channels = ad7790_channels,
124 		.num_channels = ARRAY_SIZE(ad7790_channels),
125 		.flags = AD7791_FLAG_HAS_UNIPOLAR,
126 	},
127 	[AD7789] = {
128 		.channels = ad7791_channels,
129 		.num_channels = ARRAY_SIZE(ad7791_channels),
130 		.flags = AD7791_FLAG_HAS_UNIPOLAR,
131 	},
132 	[AD7790] = {
133 		.channels = ad7790_channels,
134 		.num_channels = ARRAY_SIZE(ad7790_channels),
135 		.flags = AD7791_FLAG_HAS_FILTER | AD7791_FLAG_HAS_BUFFER |
136 			AD7791_FLAG_HAS_BURNOUT,
137 	},
138 	[AD7791] = {
139 		.channels = ad7791_channels,
140 		.num_channels = ARRAY_SIZE(ad7791_channels),
141 		.flags = AD7791_FLAG_HAS_FILTER | AD7791_FLAG_HAS_BUFFER |
142 			AD7791_FLAG_HAS_UNIPOLAR | AD7791_FLAG_HAS_BURNOUT,
143 	},
144 };
145 
146 struct ad7791_state {
147 	struct ad_sigma_delta sd;
148 	uint8_t mode;
149 	uint8_t filter;
150 
151 	struct regulator *reg;
152 	const struct ad7791_chip_info *info;
153 };
154 
155 static const int ad7791_sample_freq_avail[8][2] = {
156 	[AD7791_FILTER_RATE_120] =  { 120, 0 },
157 	[AD7791_FILTER_RATE_100] =  { 100, 0 },
158 	[AD7791_FILTER_RATE_33_3] = { 33,  300000 },
159 	[AD7791_FILTER_RATE_20] =   { 20,  0 },
160 	[AD7791_FILTER_RATE_16_6] = { 16,  600000 },
161 	[AD7791_FILTER_RATE_16_7] = { 16,  700000 },
162 	[AD7791_FILTER_RATE_13_3] = { 13,  300000 },
163 	[AD7791_FILTER_RATE_9_5] =  { 9,   500000 },
164 };
165 
166 static struct ad7791_state *ad_sigma_delta_to_ad7791(struct ad_sigma_delta *sd)
167 {
168 	return container_of(sd, struct ad7791_state, sd);
169 }
170 
171 static int ad7791_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
172 {
173 	ad_sd_set_comm(sd, channel);
174 
175 	return 0;
176 }
177 
178 static int ad7791_set_mode(struct ad_sigma_delta *sd,
179 	enum ad_sigma_delta_mode mode)
180 {
181 	struct ad7791_state *st = ad_sigma_delta_to_ad7791(sd);
182 
183 	switch (mode) {
184 	case AD_SD_MODE_CONTINUOUS:
185 		mode = AD7791_MODE_CONTINUOUS;
186 		break;
187 	case AD_SD_MODE_SINGLE:
188 		mode = AD7791_MODE_SINGLE;
189 		break;
190 	case AD_SD_MODE_IDLE:
191 	case AD_SD_MODE_POWERDOWN:
192 		mode = AD7791_MODE_POWERDOWN;
193 		break;
194 	}
195 
196 	st->mode &= ~AD7791_MODE_SEL_MASK;
197 	st->mode |= AD7791_MODE_SEL(mode);
198 
199 	return ad_sd_write_reg(sd, AD7791_REG_MODE, sizeof(st->mode), st->mode);
200 }
201 
202 static const struct ad_sigma_delta_info ad7791_sigma_delta_info = {
203 	.set_channel = ad7791_set_channel,
204 	.set_mode = ad7791_set_mode,
205 	.has_registers = true,
206 	.addr_shift = 4,
207 	.read_mask = BIT(3),
208 };
209 
210 static int ad7791_read_raw(struct iio_dev *indio_dev,
211 	const struct iio_chan_spec *chan, int *val, int *val2, long info)
212 {
213 	struct ad7791_state *st = iio_priv(indio_dev);
214 	bool unipolar = !!(st->mode & AD7791_MODE_UNIPOLAR);
215 	unsigned int rate;
216 
217 	switch (info) {
218 	case IIO_CHAN_INFO_RAW:
219 		return ad_sigma_delta_single_conversion(indio_dev, chan, val);
220 	case IIO_CHAN_INFO_OFFSET:
221 		/**
222 		 * Unipolar: 0 to VREF
223 		 * Bipolar -VREF to VREF
224 		 **/
225 		if (unipolar)
226 			*val = 0;
227 		else
228 			*val = -(1 << (chan->scan_type.realbits - 1));
229 		return IIO_VAL_INT;
230 	case IIO_CHAN_INFO_SCALE:
231 		/* The monitor channel uses an internal reference. */
232 		if (chan->address == AD7791_CH_AVDD_MONITOR) {
233 			/*
234 			 * The signal is attenuated by a factor of 5 and
235 			 * compared against a 1.17V internal reference.
236 			 */
237 			*val = 1170 * 5;
238 		} else {
239 			int voltage_uv;
240 
241 			voltage_uv = regulator_get_voltage(st->reg);
242 			if (voltage_uv < 0)
243 				return voltage_uv;
244 
245 			*val = voltage_uv / 1000;
246 		}
247 		if (unipolar)
248 			*val2 = chan->scan_type.realbits;
249 		else
250 			*val2 = chan->scan_type.realbits - 1;
251 
252 		return IIO_VAL_FRACTIONAL_LOG2;
253 	case IIO_CHAN_INFO_SAMP_FREQ:
254 		rate = st->filter & AD7791_FILTER_RATE_MASK;
255 		*val = ad7791_sample_freq_avail[rate][0];
256 		*val2 = ad7791_sample_freq_avail[rate][1];
257 		return IIO_VAL_INT_PLUS_MICRO;
258 	}
259 
260 	return -EINVAL;
261 }
262 
263 static int ad7791_write_raw(struct iio_dev *indio_dev,
264 	struct iio_chan_spec const *chan, int val, int val2, long mask)
265 {
266 	struct ad7791_state *st = iio_priv(indio_dev);
267 	int ret, i;
268 
269 	ret = iio_device_claim_direct_mode(indio_dev);
270 	if (ret)
271 		return ret;
272 
273 	switch (mask) {
274 	case IIO_CHAN_INFO_SAMP_FREQ:
275 		for (i = 0; i < ARRAY_SIZE(ad7791_sample_freq_avail); i++) {
276 			if (ad7791_sample_freq_avail[i][0] == val &&
277 			    ad7791_sample_freq_avail[i][1] == val2)
278 				break;
279 		}
280 
281 		if (i == ARRAY_SIZE(ad7791_sample_freq_avail)) {
282 			ret = -EINVAL;
283 			break;
284 		}
285 
286 		st->filter &= ~AD7791_FILTER_RATE_MASK;
287 		st->filter |= i;
288 		ad_sd_write_reg(&st->sd, AD7791_REG_FILTER,
289 				sizeof(st->filter),
290 				st->filter);
291 		break;
292 	default:
293 		ret = -EINVAL;
294 	}
295 
296 	iio_device_release_direct_mode(indio_dev);
297 	return ret;
298 }
299 
300 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("120 100 33.3 20 16.7 16.6 13.3 9.5");
301 
302 static struct attribute *ad7791_attributes[] = {
303 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
304 	NULL
305 };
306 
307 static const struct attribute_group ad7791_attribute_group = {
308 	.attrs = ad7791_attributes,
309 };
310 
311 static const struct iio_info ad7791_info = {
312 	.read_raw = &ad7791_read_raw,
313 	.write_raw = &ad7791_write_raw,
314 	.attrs = &ad7791_attribute_group,
315 	.validate_trigger = ad_sd_validate_trigger,
316 };
317 
318 static const struct iio_info ad7791_no_filter_info = {
319 	.read_raw = &ad7791_read_raw,
320 	.write_raw = &ad7791_write_raw,
321 	.validate_trigger = ad_sd_validate_trigger,
322 };
323 
324 static int ad7791_setup(struct ad7791_state *st,
325 			struct ad7791_platform_data *pdata)
326 {
327 	/* Set to poweron-reset default values */
328 	st->mode = AD7791_MODE_BUFFER;
329 	st->filter = AD7791_FILTER_RATE_16_6;
330 
331 	if (!pdata)
332 		return 0;
333 
334 	if ((st->info->flags & AD7791_FLAG_HAS_BUFFER) && !pdata->buffered)
335 		st->mode &= ~AD7791_MODE_BUFFER;
336 
337 	if ((st->info->flags & AD7791_FLAG_HAS_BURNOUT) &&
338 		pdata->burnout_current)
339 		st->mode |= AD7791_MODE_BURNOUT;
340 
341 	if ((st->info->flags & AD7791_FLAG_HAS_UNIPOLAR) && pdata->unipolar)
342 		st->mode |= AD7791_MODE_UNIPOLAR;
343 
344 	return ad_sd_write_reg(&st->sd, AD7791_REG_MODE, sizeof(st->mode),
345 		st->mode);
346 }
347 
348 static int ad7791_probe(struct spi_device *spi)
349 {
350 	struct ad7791_platform_data *pdata = spi->dev.platform_data;
351 	struct iio_dev *indio_dev;
352 	struct ad7791_state *st;
353 	int ret;
354 
355 	if (!spi->irq) {
356 		dev_err(&spi->dev, "Missing IRQ.\n");
357 		return -ENXIO;
358 	}
359 
360 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
361 	if (!indio_dev)
362 		return -ENOMEM;
363 
364 	st = iio_priv(indio_dev);
365 
366 	st->reg = devm_regulator_get(&spi->dev, "refin");
367 	if (IS_ERR(st->reg))
368 		return PTR_ERR(st->reg);
369 
370 	ret = regulator_enable(st->reg);
371 	if (ret)
372 		return ret;
373 
374 	st->info = &ad7791_chip_infos[spi_get_device_id(spi)->driver_data];
375 	ad_sd_init(&st->sd, indio_dev, spi, &ad7791_sigma_delta_info);
376 
377 	spi_set_drvdata(spi, indio_dev);
378 
379 	indio_dev->dev.parent = &spi->dev;
380 	indio_dev->dev.of_node = spi->dev.of_node;
381 	indio_dev->name = spi_get_device_id(spi)->name;
382 	indio_dev->modes = INDIO_DIRECT_MODE;
383 	indio_dev->channels = st->info->channels;
384 	indio_dev->num_channels = st->info->num_channels;
385 	if (st->info->flags & AD7791_FLAG_HAS_FILTER)
386 		indio_dev->info = &ad7791_info;
387 	else
388 		indio_dev->info = &ad7791_no_filter_info;
389 
390 	ret = ad_sd_setup_buffer_and_trigger(indio_dev);
391 	if (ret)
392 		goto error_disable_reg;
393 
394 	ret = ad7791_setup(st, pdata);
395 	if (ret)
396 		goto error_remove_trigger;
397 
398 	ret = iio_device_register(indio_dev);
399 	if (ret)
400 		goto error_remove_trigger;
401 
402 	return 0;
403 
404 error_remove_trigger:
405 	ad_sd_cleanup_buffer_and_trigger(indio_dev);
406 error_disable_reg:
407 	regulator_disable(st->reg);
408 
409 	return ret;
410 }
411 
412 static int ad7791_remove(struct spi_device *spi)
413 {
414 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
415 	struct ad7791_state *st = iio_priv(indio_dev);
416 
417 	iio_device_unregister(indio_dev);
418 	ad_sd_cleanup_buffer_and_trigger(indio_dev);
419 
420 	regulator_disable(st->reg);
421 
422 	return 0;
423 }
424 
425 static const struct spi_device_id ad7791_spi_ids[] = {
426 	{ "ad7787", AD7787 },
427 	{ "ad7788", AD7788 },
428 	{ "ad7789", AD7789 },
429 	{ "ad7790", AD7790 },
430 	{ "ad7791", AD7791 },
431 	{}
432 };
433 MODULE_DEVICE_TABLE(spi, ad7791_spi_ids);
434 
435 static struct spi_driver ad7791_driver = {
436 	.driver = {
437 		.name	= "ad7791",
438 	},
439 	.probe		= ad7791_probe,
440 	.remove		= ad7791_remove,
441 	.id_table	= ad7791_spi_ids,
442 };
443 module_spi_driver(ad7791_driver);
444 
445 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
446 MODULE_DESCRIPTION("Analog Device AD7787/AD7788/AD7789/AD7790/AD7791 ADC driver");
447 MODULE_LICENSE("GPL v2");
448