1 /*
2 * AD7298 SPI ADC driver
3 *
4 * Copyright 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2.
7 */
8
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spi/spi.h>
13
14 #include "../iio.h"
15 #include "../buffer.h"
16 #include "../ring_sw.h"
17 #include "../trigger_consumer.h"
18
19 #include "ad7298.h"
20
21 /**
22 * ad7298_ring_preenable() setup the parameters of the ring before enabling
23 *
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
26 * byte boundary.
27 **/
ad7298_ring_preenable(struct iio_dev * indio_dev)28 static int ad7298_ring_preenable(struct iio_dev *indio_dev)
29 {
30 struct ad7298_state *st = iio_priv(indio_dev);
31 struct iio_buffer *ring = indio_dev->buffer;
32 size_t d_size;
33 int i, m;
34 unsigned short command;
35 int scan_count = bitmap_weight(indio_dev->active_scan_mask,
36 indio_dev->masklength);
37 d_size = scan_count * (AD7298_STORAGE_BITS / 8);
38
39 if (ring->scan_timestamp) {
40 d_size += sizeof(s64);
41
42 if (d_size % sizeof(s64))
43 d_size += sizeof(s64) - (d_size % sizeof(s64));
44 }
45
46 if (ring->access->set_bytes_per_datum)
47 ring->access->set_bytes_per_datum(ring, d_size);
48
49 st->d_size = d_size;
50
51 command = AD7298_WRITE | st->ext_ref;
52
53 for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
54 if (test_bit(i, indio_dev->active_scan_mask))
55 command |= m;
56
57 st->tx_buf[0] = cpu_to_be16(command);
58
59 /* build spi ring message */
60 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
61 st->ring_xfer[0].len = 2;
62 st->ring_xfer[0].cs_change = 1;
63 st->ring_xfer[1].tx_buf = &st->tx_buf[1];
64 st->ring_xfer[1].len = 2;
65 st->ring_xfer[1].cs_change = 1;
66
67 spi_message_init(&st->ring_msg);
68 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
69 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
70
71 for (i = 0; i < scan_count; i++) {
72 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
73 st->ring_xfer[i + 2].len = 2;
74 st->ring_xfer[i + 2].cs_change = 1;
75 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
76 }
77 /* make sure last transfer cs_change is not set */
78 st->ring_xfer[i + 1].cs_change = 0;
79
80 return 0;
81 }
82
83 /**
84 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
85 *
86 * Currently there is no option in this driver to disable the saving of
87 * timestamps within the ring.
88 **/
ad7298_trigger_handler(int irq,void * p)89 static irqreturn_t ad7298_trigger_handler(int irq, void *p)
90 {
91 struct iio_poll_func *pf = p;
92 struct iio_dev *indio_dev = pf->indio_dev;
93 struct ad7298_state *st = iio_priv(indio_dev);
94 struct iio_buffer *ring = indio_dev->buffer;
95 s64 time_ns;
96 __u16 buf[16];
97 int b_sent, i;
98
99 b_sent = spi_sync(st->spi, &st->ring_msg);
100 if (b_sent)
101 return b_sent;
102
103 if (ring->scan_timestamp) {
104 time_ns = iio_get_time_ns();
105 memcpy((u8 *)buf + st->d_size - sizeof(s64),
106 &time_ns, sizeof(time_ns));
107 }
108
109 for (i = 0; i < bitmap_weight(indio_dev->active_scan_mask,
110 indio_dev->masklength); i++)
111 buf[i] = be16_to_cpu(st->rx_buf[i]);
112
113 indio_dev->buffer->access->store_to(ring, (u8 *)buf, time_ns);
114 iio_trigger_notify_done(indio_dev->trig);
115
116 return IRQ_HANDLED;
117 }
118
119 static const struct iio_buffer_setup_ops ad7298_ring_setup_ops = {
120 .preenable = &ad7298_ring_preenable,
121 .postenable = &iio_triggered_buffer_postenable,
122 .predisable = &iio_triggered_buffer_predisable,
123 };
124
ad7298_register_ring_funcs_and_init(struct iio_dev * indio_dev)125 int ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev)
126 {
127 int ret;
128
129 indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
130 if (!indio_dev->buffer) {
131 ret = -ENOMEM;
132 goto error_ret;
133 }
134 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
135 &ad7298_trigger_handler,
136 IRQF_ONESHOT,
137 indio_dev,
138 "ad7298_consumer%d",
139 indio_dev->id);
140
141 if (indio_dev->pollfunc == NULL) {
142 ret = -ENOMEM;
143 goto error_deallocate_sw_rb;
144 }
145
146 /* Ring buffer functions - here trigger setup related */
147 indio_dev->setup_ops = &ad7298_ring_setup_ops;
148 indio_dev->buffer->scan_timestamp = true;
149
150 /* Flag that polled ring buffering is possible */
151 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
152 return 0;
153
154 error_deallocate_sw_rb:
155 iio_sw_rb_free(indio_dev->buffer);
156 error_ret:
157 return ret;
158 }
159
ad7298_ring_cleanup(struct iio_dev * indio_dev)160 void ad7298_ring_cleanup(struct iio_dev *indio_dev)
161 {
162 iio_dealloc_pollfunc(indio_dev->pollfunc);
163 iio_sw_rb_free(indio_dev->buffer);
164 }
165