2 * AD7298 SPI ADC driver
4 * Copyright 2011-2012 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spi/spi.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/kfifo_buf.h>
17 #include <linux/iio/trigger_consumer.h>
22 * ad7298_ring_preenable() setup the parameters of the ring before enabling
24 * The complex nature of the setting of the number of bytes per datum is due
25 * to this driver currently ensuring that the timestamp is stored at an 8
28 static int ad7298_ring_preenable(struct iio_dev *indio_dev)
30 struct ad7298_state *st = iio_priv(indio_dev);
32 unsigned short command;
35 ret = iio_sw_buffer_preenable(indio_dev);
39 /* Now compute overall size */
40 scan_count = bitmap_weight(indio_dev->active_scan_mask,
41 indio_dev->masklength);
43 command = AD7298_WRITE | st->ext_ref;
45 for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
46 if (test_bit(i, indio_dev->active_scan_mask))
49 st->tx_buf[0] = cpu_to_be16(command);
51 /* build spi ring message */
52 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
53 st->ring_xfer[0].len = 2;
54 st->ring_xfer[0].cs_change = 1;
55 st->ring_xfer[1].tx_buf = &st->tx_buf[1];
56 st->ring_xfer[1].len = 2;
57 st->ring_xfer[1].cs_change = 1;
59 spi_message_init(&st->ring_msg);
60 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
61 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
63 for (i = 0; i < scan_count; i++) {
64 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
65 st->ring_xfer[i + 2].len = 2;
66 st->ring_xfer[i + 2].cs_change = 1;
67 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
69 /* make sure last transfer cs_change is not set */
70 st->ring_xfer[i + 1].cs_change = 0;
76 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
78 * Currently there is no option in this driver to disable the saving of
79 * timestamps within the ring.
81 static irqreturn_t ad7298_trigger_handler(int irq, void *p)
83 struct iio_poll_func *pf = p;
84 struct iio_dev *indio_dev = pf->indio_dev;
85 struct ad7298_state *st = iio_priv(indio_dev);
86 struct iio_buffer *ring = indio_dev->buffer;
91 b_sent = spi_sync(st->spi, &st->ring_msg);
95 if (indio_dev->scan_timestamp) {
96 time_ns = iio_get_time_ns();
97 memcpy((u8 *)buf + indio_dev->scan_bytes - sizeof(s64),
98 &time_ns, sizeof(time_ns));
101 for (i = 0; i < bitmap_weight(indio_dev->active_scan_mask,
102 indio_dev->masklength); i++)
103 buf[i] = be16_to_cpu(st->rx_buf[i]);
105 indio_dev->buffer->access->store_to(ring, (u8 *)buf, time_ns);
106 iio_trigger_notify_done(indio_dev->trig);
111 static const struct iio_buffer_setup_ops ad7298_ring_setup_ops = {
112 .preenable = &ad7298_ring_preenable,
113 .postenable = &iio_triggered_buffer_postenable,
114 .predisable = &iio_triggered_buffer_predisable,
117 int ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev)
121 indio_dev->buffer = iio_kfifo_allocate(indio_dev);
122 if (!indio_dev->buffer) {
126 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
127 &ad7298_trigger_handler,
133 if (indio_dev->pollfunc == NULL) {
135 goto error_deallocate_kfifo;
138 /* Ring buffer functions - here trigger setup related */
139 indio_dev->setup_ops = &ad7298_ring_setup_ops;
140 indio_dev->buffer->scan_timestamp = true;
142 /* Flag that polled ring buffering is possible */
143 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
146 error_deallocate_kfifo:
147 iio_kfifo_free(indio_dev->buffer);
152 void ad7298_ring_cleanup(struct iio_dev *indio_dev)
154 iio_dealloc_pollfunc(indio_dev->pollfunc);
155 iio_kfifo_free(indio_dev->buffer);