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/sysfs.h>
15 #include <linux/spi/spi.h>
18 #include "../ring_generic.h"
19 #include "../ring_sw.h"
20 #include "../trigger.h"
25 int ad7476_scan_from_ring(struct ad7476_state *st)
27 struct iio_ring_buffer *ring = st->indio_dev->ring;
31 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
33 if (ring_data == NULL) {
37 ret = ring->access->read_last(ring, ring_data);
39 goto error_free_ring_data;
41 ret = (ring_data[0] << 8) | ring_data[1];
50 * ad7476_ring_preenable() setup the parameters of the ring before enabling
52 * The complex nature of the setting of the nuber of bytes per datum is due
53 * to this driver currently ensuring that the timestamp is stored at an 8
56 static int ad7476_ring_preenable(struct iio_dev *indio_dev)
58 struct ad7476_state *st = indio_dev->dev_data;
59 struct iio_ring_buffer *ring = indio_dev->ring;
61 st->d_size = ring->scan_count *
62 st->chip_info->channel[0].scan_type.storagebits / 8;
64 if (ring->scan_timestamp) {
65 st->d_size += sizeof(s64);
67 if (st->d_size % sizeof(s64))
68 st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
71 if (indio_dev->ring->access->set_bytes_per_datum)
72 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
78 static irqreturn_t ad7476_trigger_handler(int irq, void *p)
80 struct iio_poll_func *pf = p;
81 struct iio_dev *indio_dev = pf->private_data;
82 struct ad7476_state *st = iio_dev_get_devdata(indio_dev);
87 rxbuf = kzalloc(st->d_size, GFP_KERNEL);
91 b_sent = spi_read(st->spi, rxbuf,
92 st->chip_info->channel[0].scan_type.storagebits / 8);
96 time_ns = iio_get_time_ns();
98 if (indio_dev->ring->scan_timestamp)
99 memcpy(rxbuf + st->d_size - sizeof(s64),
100 &time_ns, sizeof(time_ns));
102 indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
104 iio_trigger_notify_done(indio_dev->trig);
110 static const struct iio_ring_setup_ops ad7476_ring_setup_ops = {
111 .preenable = &ad7476_ring_preenable,
112 .postenable = &iio_triggered_ring_postenable,
113 .predisable = &iio_triggered_ring_predisable,
116 int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
118 struct ad7476_state *st = indio_dev->dev_data;
121 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
122 if (!indio_dev->ring) {
126 /* Effectively select the ring buffer implementation */
127 indio_dev->ring->access = &ring_sw_access_funcs;
128 indio_dev->pollfunc = kzalloc(sizeof(indio_dev->pollfunc), GFP_KERNEL);
129 if (indio_dev->pollfunc == NULL) {
131 goto error_deallocate_sw_rb;
133 indio_dev->pollfunc->private_data = indio_dev;
134 indio_dev->pollfunc->thread = &ad7476_trigger_handler;
135 indio_dev->pollfunc->type = IRQF_ONESHOT;
136 indio_dev->pollfunc->name
137 = kasprintf(GFP_KERNEL, "%s_consumer%d",
138 spi_get_device_id(st->spi)->name,
140 if (indio_dev->pollfunc->name == NULL) {
142 goto error_free_pollfunc;
145 /* Ring buffer functions - here trigger setup related */
146 indio_dev->ring->setup_ops = &ad7476_ring_setup_ops;
147 indio_dev->ring->scan_timestamp = true;
149 /* Flag that polled ring buffering is possible */
150 indio_dev->modes |= INDIO_RING_TRIGGERED;
153 kfree(indio_dev->pollfunc);
154 error_deallocate_sw_rb:
155 iio_sw_rb_free(indio_dev->ring);
160 void ad7476_ring_cleanup(struct iio_dev *indio_dev)
162 /* ensure that the trigger has been detached */
163 if (indio_dev->trig) {
164 iio_put_trigger(indio_dev->trig);
165 iio_trigger_dettach_poll_func(indio_dev->trig,
166 indio_dev->pollfunc);
168 kfree(indio_dev->pollfunc->name);
169 kfree(indio_dev->pollfunc);
170 iio_sw_rb_free(indio_dev->ring);