xref: /linux/drivers/iio/adc/ad7923.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AD7904/AD7914/AD7923/AD7924/AD7908/AD7918/AD7928 SPI ADC driver
4  *
5  * Copyright 2011 Analog Devices Inc (from AD7923 Driver)
6  * Copyright 2012 CS Systemes d'Information
7  */
8 
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/spi/spi.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
25 
26 #define AD7923_WRITE_CR		BIT(11)		/* write control register */
27 #define AD7923_RANGE		BIT(1)		/* range to REFin */
28 #define AD7923_CODING		BIT(0)		/* coding is straight binary */
29 #define AD7923_PM_MODE_AS	(1)		/* auto shutdown */
30 #define AD7923_PM_MODE_FS	(2)		/* full shutdown */
31 #define AD7923_PM_MODE_OPS	(3)		/* normal operation */
32 #define AD7923_SEQUENCE_OFF	(0)		/* no sequence fonction */
33 #define AD7923_SEQUENCE_PROTECT	(2)		/* no interrupt write cycle */
34 #define AD7923_SEQUENCE_ON	(3)		/* continuous sequence */
35 
36 
37 #define AD7923_PM_MODE_WRITE(mode)	((mode) << 4)	 /* write mode */
38 #define AD7923_CHANNEL_WRITE(channel)	((channel) << 6) /* write channel */
39 #define AD7923_SEQUENCE_WRITE(sequence)	((((sequence) & 1) << 3) \
40 					+ (((sequence) & 2) << 9))
41 						/* write sequence fonction */
42 /* left shift for CR : bit 11 transmit in first */
43 #define AD7923_SHIFT_REGISTER	4
44 
45 /* val = value, dec = left shift, bits = number of bits of the mask */
46 #define EXTRACT(val, dec, bits)		(((val) >> (dec)) & ((1 << (bits)) - 1))
47 
48 struct ad7923_state {
49 	struct spi_device		*spi;
50 	struct spi_transfer		ring_xfer[5];
51 	struct spi_transfer		scan_single_xfer[2];
52 	struct spi_message		ring_msg;
53 	struct spi_message		scan_single_msg;
54 
55 	struct regulator		*reg;
56 
57 	unsigned int			settings;
58 
59 	/*
60 	 * DMA (thus cache coherency maintenance) requires the
61 	 * transfer buffers to live in their own cache lines.
62 	 */
63 	__be16				rx_buf[4] ____cacheline_aligned;
64 	__be16				tx_buf[4];
65 };
66 
67 struct ad7923_chip_info {
68 	const struct iio_chan_spec *channels;
69 	unsigned int num_channels;
70 };
71 
72 enum ad7923_id {
73 	AD7904,
74 	AD7914,
75 	AD7924,
76 	AD7908,
77 	AD7918,
78 	AD7928
79 };
80 
81 #define AD7923_V_CHAN(index, bits)					\
82 	{								\
83 		.type = IIO_VOLTAGE,					\
84 		.indexed = 1,						\
85 		.channel = index,					\
86 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
87 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
88 		.address = index,					\
89 		.scan_index = index,					\
90 		.scan_type = {						\
91 			.sign = 'u',					\
92 			.realbits = (bits),				\
93 			.storagebits = 16,				\
94 			.endianness = IIO_BE,				\
95 		},							\
96 	}
97 
98 #define DECLARE_AD7923_CHANNELS(name, bits) \
99 const struct iio_chan_spec name ## _channels[] = { \
100 	AD7923_V_CHAN(0, bits), \
101 	AD7923_V_CHAN(1, bits), \
102 	AD7923_V_CHAN(2, bits), \
103 	AD7923_V_CHAN(3, bits), \
104 	IIO_CHAN_SOFT_TIMESTAMP(4), \
105 }
106 
107 #define DECLARE_AD7908_CHANNELS(name, bits) \
108 const struct iio_chan_spec name ## _channels[] = { \
109 	AD7923_V_CHAN(0, bits), \
110 	AD7923_V_CHAN(1, bits), \
111 	AD7923_V_CHAN(2, bits), \
112 	AD7923_V_CHAN(3, bits), \
113 	AD7923_V_CHAN(4, bits), \
114 	AD7923_V_CHAN(5, bits), \
115 	AD7923_V_CHAN(6, bits), \
116 	AD7923_V_CHAN(7, bits), \
117 	IIO_CHAN_SOFT_TIMESTAMP(8), \
118 }
119 
120 static DECLARE_AD7923_CHANNELS(ad7904, 8);
121 static DECLARE_AD7923_CHANNELS(ad7914, 10);
122 static DECLARE_AD7923_CHANNELS(ad7924, 12);
123 static DECLARE_AD7908_CHANNELS(ad7908, 8);
124 static DECLARE_AD7908_CHANNELS(ad7918, 10);
125 static DECLARE_AD7908_CHANNELS(ad7928, 12);
126 
127 static const struct ad7923_chip_info ad7923_chip_info[] = {
128 	[AD7904] = {
129 		.channels = ad7904_channels,
130 		.num_channels = ARRAY_SIZE(ad7904_channels),
131 	},
132 	[AD7914] = {
133 		.channels = ad7914_channels,
134 		.num_channels = ARRAY_SIZE(ad7914_channels),
135 	},
136 	[AD7924] = {
137 		.channels = ad7924_channels,
138 		.num_channels = ARRAY_SIZE(ad7924_channels),
139 	},
140 	[AD7908] = {
141 		.channels = ad7908_channels,
142 		.num_channels = ARRAY_SIZE(ad7908_channels),
143 	},
144 	[AD7918] = {
145 		.channels = ad7918_channels,
146 		.num_channels = ARRAY_SIZE(ad7918_channels),
147 	},
148 	[AD7928] = {
149 		.channels = ad7928_channels,
150 		.num_channels = ARRAY_SIZE(ad7928_channels),
151 	},
152 };
153 
154 /*
155  * ad7923_update_scan_mode() setup the spi transfer buffer for the new scan mask
156  */
157 static int ad7923_update_scan_mode(struct iio_dev *indio_dev,
158 				   const unsigned long *active_scan_mask)
159 {
160 	struct ad7923_state *st = iio_priv(indio_dev);
161 	int i, cmd, len;
162 
163 	len = 0;
164 	/*
165 	 * For this driver the last channel is always the software timestamp so
166 	 * skip that one.
167 	 */
168 	for_each_set_bit(i, active_scan_mask, indio_dev->num_channels - 1) {
169 		cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(i) |
170 			AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
171 			st->settings;
172 		cmd <<= AD7923_SHIFT_REGISTER;
173 		st->tx_buf[len++] = cpu_to_be16(cmd);
174 	}
175 	/* build spi ring message */
176 	st->ring_xfer[0].tx_buf = &st->tx_buf[0];
177 	st->ring_xfer[0].len = len;
178 	st->ring_xfer[0].cs_change = 1;
179 
180 	spi_message_init(&st->ring_msg);
181 	spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
182 
183 	for (i = 0; i < len; i++) {
184 		st->ring_xfer[i + 1].rx_buf = &st->rx_buf[i];
185 		st->ring_xfer[i + 1].len = 2;
186 		st->ring_xfer[i + 1].cs_change = 1;
187 		spi_message_add_tail(&st->ring_xfer[i + 1], &st->ring_msg);
188 	}
189 	/* make sure last transfer cs_change is not set */
190 	st->ring_xfer[i + 1].cs_change = 0;
191 
192 	return 0;
193 }
194 
195 static irqreturn_t ad7923_trigger_handler(int irq, void *p)
196 {
197 	struct iio_poll_func *pf = p;
198 	struct iio_dev *indio_dev = pf->indio_dev;
199 	struct ad7923_state *st = iio_priv(indio_dev);
200 	int b_sent;
201 
202 	b_sent = spi_sync(st->spi, &st->ring_msg);
203 	if (b_sent)
204 		goto done;
205 
206 	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
207 					   iio_get_time_ns(indio_dev));
208 
209 done:
210 	iio_trigger_notify_done(indio_dev->trig);
211 
212 	return IRQ_HANDLED;
213 }
214 
215 static int ad7923_scan_direct(struct ad7923_state *st, unsigned int ch)
216 {
217 	int ret, cmd;
218 
219 	cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(ch) |
220 		AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
221 		st->settings;
222 	cmd <<= AD7923_SHIFT_REGISTER;
223 	st->tx_buf[0] = cpu_to_be16(cmd);
224 
225 	ret = spi_sync(st->spi, &st->scan_single_msg);
226 	if (ret)
227 		return ret;
228 
229 	return be16_to_cpu(st->rx_buf[0]);
230 }
231 
232 static int ad7923_get_range(struct ad7923_state *st)
233 {
234 	int vref;
235 
236 	vref = regulator_get_voltage(st->reg);
237 	if (vref < 0)
238 		return vref;
239 
240 	vref /= 1000;
241 
242 	if (!(st->settings & AD7923_RANGE))
243 		vref *= 2;
244 
245 	return vref;
246 }
247 
248 static int ad7923_read_raw(struct iio_dev *indio_dev,
249 			   struct iio_chan_spec const *chan,
250 			   int *val,
251 			   int *val2,
252 			   long m)
253 {
254 	int ret;
255 	struct ad7923_state *st = iio_priv(indio_dev);
256 
257 	switch (m) {
258 	case IIO_CHAN_INFO_RAW:
259 		ret = iio_device_claim_direct_mode(indio_dev);
260 		if (ret)
261 			return ret;
262 		ret = ad7923_scan_direct(st, chan->address);
263 		iio_device_release_direct_mode(indio_dev);
264 
265 		if (ret < 0)
266 			return ret;
267 
268 		if (chan->address == EXTRACT(ret, 12, 4))
269 			*val = EXTRACT(ret, 0, 12);
270 		else
271 			return -EIO;
272 
273 		return IIO_VAL_INT;
274 	case IIO_CHAN_INFO_SCALE:
275 		ret = ad7923_get_range(st);
276 		if (ret < 0)
277 			return ret;
278 		*val = ret;
279 		*val2 = chan->scan_type.realbits;
280 		return IIO_VAL_FRACTIONAL_LOG2;
281 	}
282 	return -EINVAL;
283 }
284 
285 static const struct iio_info ad7923_info = {
286 	.read_raw = &ad7923_read_raw,
287 	.update_scan_mode = ad7923_update_scan_mode,
288 };
289 
290 static void ad7923_regulator_disable(void *data)
291 {
292 	struct ad7923_state *st = data;
293 
294 	regulator_disable(st->reg);
295 }
296 
297 static int ad7923_probe(struct spi_device *spi)
298 {
299 	struct ad7923_state *st;
300 	struct iio_dev *indio_dev;
301 	const struct ad7923_chip_info *info;
302 	int ret;
303 
304 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
305 	if (!indio_dev)
306 		return -ENOMEM;
307 
308 	st = iio_priv(indio_dev);
309 
310 	st->spi = spi;
311 	st->settings = AD7923_CODING | AD7923_RANGE |
312 			AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS);
313 
314 	info = &ad7923_chip_info[spi_get_device_id(spi)->driver_data];
315 
316 	indio_dev->name = spi_get_device_id(spi)->name;
317 	indio_dev->modes = INDIO_DIRECT_MODE;
318 	indio_dev->channels = info->channels;
319 	indio_dev->num_channels = info->num_channels;
320 	indio_dev->info = &ad7923_info;
321 
322 	/* Setup default message */
323 
324 	st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
325 	st->scan_single_xfer[0].len = 2;
326 	st->scan_single_xfer[0].cs_change = 1;
327 	st->scan_single_xfer[1].rx_buf = &st->rx_buf[0];
328 	st->scan_single_xfer[1].len = 2;
329 
330 	spi_message_init(&st->scan_single_msg);
331 	spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
332 	spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
333 
334 	st->reg = devm_regulator_get(&spi->dev, "refin");
335 	if (IS_ERR(st->reg))
336 		return PTR_ERR(st->reg);
337 
338 	ret = regulator_enable(st->reg);
339 	if (ret)
340 		return ret;
341 
342 	ret = devm_add_action_or_reset(&spi->dev, ad7923_regulator_disable, st);
343 	if (ret)
344 		return ret;
345 
346 	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
347 					      &ad7923_trigger_handler, NULL);
348 	if (ret)
349 		return ret;
350 
351 	return devm_iio_device_register(&spi->dev, indio_dev);
352 }
353 
354 static const struct spi_device_id ad7923_id[] = {
355 	{"ad7904", AD7904},
356 	{"ad7914", AD7914},
357 	{"ad7923", AD7924},
358 	{"ad7924", AD7924},
359 	{"ad7908", AD7908},
360 	{"ad7918", AD7918},
361 	{"ad7928", AD7928},
362 	{}
363 };
364 MODULE_DEVICE_TABLE(spi, ad7923_id);
365 
366 static const struct of_device_id ad7923_of_match[] = {
367 	{ .compatible = "adi,ad7904", },
368 	{ .compatible = "adi,ad7914", },
369 	{ .compatible = "adi,ad7923", },
370 	{ .compatible = "adi,ad7924", },
371 	{ .compatible = "adi,ad7908", },
372 	{ .compatible = "adi,ad7918", },
373 	{ .compatible = "adi,ad7928", },
374 	{ },
375 };
376 MODULE_DEVICE_TABLE(of, ad7923_of_match);
377 
378 static struct spi_driver ad7923_driver = {
379 	.driver = {
380 		.name	= "ad7923",
381 		.of_match_table = ad7923_of_match,
382 	},
383 	.probe		= ad7923_probe,
384 	.id_table	= ad7923_id,
385 };
386 module_spi_driver(ad7923_driver);
387 
388 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
389 MODULE_AUTHOR("Patrick Vasseur <patrick.vasseur@c-s.fr>");
390 MODULE_DESCRIPTION("Analog Devices AD7923 and similar ADC");
391 MODULE_LICENSE("GPL v2");
392