1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD7904/AD7914/AD7923/AD7924/AD7908/AD7918/AD7928 SPI ADC driver
5 * Copyright 2011 Analog Devices Inc (from AD7923 Driver)
6 * Copyright 2012 CS Systemes d'Information
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>
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>
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 */
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
45 /* val = value, dec = left shift, bits = number of bits of the mask */
46 #define EXTRACT(val, dec, bits) (((val) >> (dec)) & ((1 << (bits)) - 1))
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;
55 struct regulator *reg;
57 unsigned int settings;
60 * DMA (thus cache coherency maintenance) requires the
61 * transfer buffers to live in their own cache lines.
62 * Ensure rx_buf can be directly used in iio_push_to_buffers_with_timetamp
63 * Length = 8 channels + 4 extra for 8 byte timestamp
65 __be16 rx_buf[12] ____cacheline_aligned;
69 struct ad7923_chip_info {
70 const struct iio_chan_spec *channels;
71 unsigned int num_channels;
83 #define AD7923_V_CHAN(index, bits) \
85 .type = IIO_VOLTAGE, \
88 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
89 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
91 .scan_index = index, \
96 .shift = 12 - (bits), \
97 .endianness = IIO_BE, \
101 #define DECLARE_AD7923_CHANNELS(name, bits) \
102 const struct iio_chan_spec name ## _channels[] = { \
103 AD7923_V_CHAN(0, bits), \
104 AD7923_V_CHAN(1, bits), \
105 AD7923_V_CHAN(2, bits), \
106 AD7923_V_CHAN(3, bits), \
107 IIO_CHAN_SOFT_TIMESTAMP(4), \
110 #define DECLARE_AD7908_CHANNELS(name, bits) \
111 const struct iio_chan_spec name ## _channels[] = { \
112 AD7923_V_CHAN(0, bits), \
113 AD7923_V_CHAN(1, bits), \
114 AD7923_V_CHAN(2, bits), \
115 AD7923_V_CHAN(3, bits), \
116 AD7923_V_CHAN(4, bits), \
117 AD7923_V_CHAN(5, bits), \
118 AD7923_V_CHAN(6, bits), \
119 AD7923_V_CHAN(7, bits), \
120 IIO_CHAN_SOFT_TIMESTAMP(8), \
123 static DECLARE_AD7923_CHANNELS(ad7904, 8);
124 static DECLARE_AD7923_CHANNELS(ad7914, 10);
125 static DECLARE_AD7923_CHANNELS(ad7924, 12);
126 static DECLARE_AD7908_CHANNELS(ad7908, 8);
127 static DECLARE_AD7908_CHANNELS(ad7918, 10);
128 static DECLARE_AD7908_CHANNELS(ad7928, 12);
130 static const struct ad7923_chip_info ad7923_chip_info[] = {
132 .channels = ad7904_channels,
133 .num_channels = ARRAY_SIZE(ad7904_channels),
136 .channels = ad7914_channels,
137 .num_channels = ARRAY_SIZE(ad7914_channels),
140 .channels = ad7924_channels,
141 .num_channels = ARRAY_SIZE(ad7924_channels),
144 .channels = ad7908_channels,
145 .num_channels = ARRAY_SIZE(ad7908_channels),
148 .channels = ad7918_channels,
149 .num_channels = ARRAY_SIZE(ad7918_channels),
152 .channels = ad7928_channels,
153 .num_channels = ARRAY_SIZE(ad7928_channels),
158 * ad7923_update_scan_mode() setup the spi transfer buffer for the new scan mask
160 static int ad7923_update_scan_mode(struct iio_dev *indio_dev,
161 const unsigned long *active_scan_mask)
163 struct ad7923_state *st = iio_priv(indio_dev);
168 * For this driver the last channel is always the software timestamp so
171 for_each_set_bit(i, active_scan_mask, indio_dev->num_channels - 1) {
172 cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(i) |
173 AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
175 cmd <<= AD7923_SHIFT_REGISTER;
176 st->tx_buf[len++] = cpu_to_be16(cmd);
178 /* build spi ring message */
179 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
180 st->ring_xfer[0].len = len;
181 st->ring_xfer[0].cs_change = 1;
183 spi_message_init(&st->ring_msg);
184 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
186 for (i = 0; i < len; i++) {
187 st->ring_xfer[i + 1].rx_buf = &st->rx_buf[i];
188 st->ring_xfer[i + 1].len = 2;
189 st->ring_xfer[i + 1].cs_change = 1;
190 spi_message_add_tail(&st->ring_xfer[i + 1], &st->ring_msg);
192 /* make sure last transfer cs_change is not set */
193 st->ring_xfer[i + 1].cs_change = 0;
199 * ad7923_trigger_handler() bh of trigger launched polling to ring buffer
201 * Currently there is no option in this driver to disable the saving of
202 * timestamps within the ring.
204 static irqreturn_t ad7923_trigger_handler(int irq, void *p)
206 struct iio_poll_func *pf = p;
207 struct iio_dev *indio_dev = pf->indio_dev;
208 struct ad7923_state *st = iio_priv(indio_dev);
211 b_sent = spi_sync(st->spi, &st->ring_msg);
215 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
216 iio_get_time_ns(indio_dev));
219 iio_trigger_notify_done(indio_dev->trig);
224 static int ad7923_scan_direct(struct ad7923_state *st, unsigned int ch)
228 cmd = AD7923_WRITE_CR | AD7923_CHANNEL_WRITE(ch) |
229 AD7923_SEQUENCE_WRITE(AD7923_SEQUENCE_OFF) |
231 cmd <<= AD7923_SHIFT_REGISTER;
232 st->tx_buf[0] = cpu_to_be16(cmd);
234 ret = spi_sync(st->spi, &st->scan_single_msg);
238 return be16_to_cpu(st->rx_buf[0]);
241 static int ad7923_get_range(struct ad7923_state *st)
245 vref = regulator_get_voltage(st->reg);
251 if (!(st->settings & AD7923_RANGE))
257 static int ad7923_read_raw(struct iio_dev *indio_dev,
258 struct iio_chan_spec const *chan,
264 struct ad7923_state *st = iio_priv(indio_dev);
267 case IIO_CHAN_INFO_RAW:
268 ret = iio_device_claim_direct_mode(indio_dev);
271 ret = ad7923_scan_direct(st, chan->address);
272 iio_device_release_direct_mode(indio_dev);
277 if (chan->address == EXTRACT(ret, 12, 4))
278 *val = EXTRACT(ret, chan->scan_type.shift,
279 chan->scan_type.realbits);
284 case IIO_CHAN_INFO_SCALE:
285 ret = ad7923_get_range(st);
289 *val2 = chan->scan_type.realbits;
290 return IIO_VAL_FRACTIONAL_LOG2;
295 static const struct iio_info ad7923_info = {
296 .read_raw = &ad7923_read_raw,
297 .update_scan_mode = ad7923_update_scan_mode,
300 static int ad7923_probe(struct spi_device *spi)
302 struct ad7923_state *st;
303 struct iio_dev *indio_dev;
304 const struct ad7923_chip_info *info;
307 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
311 st = iio_priv(indio_dev);
313 spi_set_drvdata(spi, indio_dev);
316 st->settings = AD7923_CODING | AD7923_RANGE |
317 AD7923_PM_MODE_WRITE(AD7923_PM_MODE_OPS);
319 info = &ad7923_chip_info[spi_get_device_id(spi)->driver_data];
321 indio_dev->name = spi_get_device_id(spi)->name;
322 indio_dev->modes = INDIO_DIRECT_MODE;
323 indio_dev->channels = info->channels;
324 indio_dev->num_channels = info->num_channels;
325 indio_dev->info = &ad7923_info;
327 /* Setup default message */
329 st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
330 st->scan_single_xfer[0].len = 2;
331 st->scan_single_xfer[0].cs_change = 1;
332 st->scan_single_xfer[1].rx_buf = &st->rx_buf[0];
333 st->scan_single_xfer[1].len = 2;
335 spi_message_init(&st->scan_single_msg);
336 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
337 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
339 st->reg = devm_regulator_get(&spi->dev, "refin");
341 return PTR_ERR(st->reg);
343 ret = regulator_enable(st->reg);
347 ret = iio_triggered_buffer_setup(indio_dev, NULL,
348 &ad7923_trigger_handler, NULL);
350 goto error_disable_reg;
352 ret = iio_device_register(indio_dev);
354 goto error_cleanup_ring;
359 iio_triggered_buffer_cleanup(indio_dev);
361 regulator_disable(st->reg);
366 static int ad7923_remove(struct spi_device *spi)
368 struct iio_dev *indio_dev = spi_get_drvdata(spi);
369 struct ad7923_state *st = iio_priv(indio_dev);
371 iio_device_unregister(indio_dev);
372 iio_triggered_buffer_cleanup(indio_dev);
373 regulator_disable(st->reg);
378 static const struct spi_device_id ad7923_id[] = {
388 MODULE_DEVICE_TABLE(spi, ad7923_id);
390 static const struct of_device_id ad7923_of_match[] = {
391 { .compatible = "adi,ad7904", },
392 { .compatible = "adi,ad7914", },
393 { .compatible = "adi,ad7923", },
394 { .compatible = "adi,ad7924", },
395 { .compatible = "adi,ad7908", },
396 { .compatible = "adi,ad7918", },
397 { .compatible = "adi,ad7928", },
400 MODULE_DEVICE_TABLE(of, ad7923_of_match);
402 static struct spi_driver ad7923_driver = {
405 .of_match_table = ad7923_of_match,
407 .probe = ad7923_probe,
408 .remove = ad7923_remove,
409 .id_table = ad7923_id,
411 module_spi_driver(ad7923_driver);
413 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
414 MODULE_AUTHOR("Patrick Vasseur <patrick.vasseur@c-s.fr>");
415 MODULE_DESCRIPTION("Analog Devices AD7923 and similar ADC");
416 MODULE_LICENSE("GPL v2");