2 * Copyright 2010 Analog Devices Inc.
3 * Copyright (C) 2008 Jonathan Cameron
5 * Licensed under the GPL-2 or later.
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
17 #include "../ring_generic.h"
18 #include "../ring_sw.h"
19 #include "../trigger.h"
23 int ad7476_scan_from_ring(struct iio_dev *indio_dev)
25 struct iio_ring_buffer *ring = indio_dev->ring;
29 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
31 if (ring_data == NULL) {
35 ret = ring->access->read_last(ring, ring_data);
37 goto error_free_ring_data;
39 ret = (ring_data[0] << 8) | ring_data[1];
48 * ad7476_ring_preenable() setup the parameters of the ring before enabling
50 * The complex nature of the setting of the nuber of bytes per datum is due
51 * to this driver currently ensuring that the timestamp is stored at an 8
54 static int ad7476_ring_preenable(struct iio_dev *indio_dev)
56 struct ad7476_state *st = iio_priv(indio_dev);
57 struct iio_ring_buffer *ring = indio_dev->ring;
59 st->d_size = ring->scan_count *
60 st->chip_info->channel[0].scan_type.storagebits / 8;
62 if (ring->scan_timestamp) {
63 st->d_size += sizeof(s64);
65 if (st->d_size % sizeof(s64))
66 st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
69 if (indio_dev->ring->access->set_bytes_per_datum)
70 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
76 static irqreturn_t ad7476_trigger_handler(int irq, void *p)
78 struct iio_poll_func *pf = p;
79 struct iio_dev *indio_dev = pf->indio_dev;
80 struct ad7476_state *st = iio_priv(indio_dev);
85 rxbuf = kzalloc(st->d_size, GFP_KERNEL);
89 b_sent = spi_read(st->spi, rxbuf,
90 st->chip_info->channel[0].scan_type.storagebits / 8);
94 time_ns = iio_get_time_ns();
96 if (indio_dev->ring->scan_timestamp)
97 memcpy(rxbuf + st->d_size - sizeof(s64),
98 &time_ns, sizeof(time_ns));
100 indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
102 iio_trigger_notify_done(indio_dev->trig);
108 static const struct iio_ring_setup_ops ad7476_ring_setup_ops = {
109 .preenable = &ad7476_ring_preenable,
110 .postenable = &iio_triggered_ring_postenable,
111 .predisable = &iio_triggered_ring_predisable,
114 int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
116 struct ad7476_state *st = iio_priv(indio_dev);
119 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
120 if (!indio_dev->ring) {
124 /* Effectively select the ring buffer implementation */
125 indio_dev->ring->access = &ring_sw_access_funcs;
127 = iio_alloc_pollfunc(NULL,
128 &ad7476_trigger_handler,
132 spi_get_device_id(st->spi)->name,
134 if (indio_dev->pollfunc == NULL) {
136 goto error_deallocate_sw_rb;
139 /* Ring buffer functions - here trigger setup related */
140 indio_dev->ring->setup_ops = &ad7476_ring_setup_ops;
141 indio_dev->ring->scan_timestamp = true;
143 /* Flag that polled ring buffering is possible */
144 indio_dev->modes |= INDIO_RING_TRIGGERED;
147 error_deallocate_sw_rb:
148 iio_sw_rb_free(indio_dev->ring);
153 void ad7476_ring_cleanup(struct iio_dev *indio_dev)
155 /* ensure that the trigger has been detached */
156 if (indio_dev->trig) {
157 iio_put_trigger(indio_dev->trig);
158 iio_trigger_dettach_poll_func(indio_dev->trig,
159 indio_dev->pollfunc);
161 iio_dealloc_pollfunc(indio_dev->pollfunc);
162 iio_sw_rb_free(indio_dev->ring);