2 * Copyright 2010-2011 Analog Devices Inc.
3 * Copyright (C) 2008 Jonathan Cameron
5 * Licensed under the GPL-2.
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 ad7887_scan_from_ring(struct ad7887_state *st, long mask)
27 struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
31 if (!(ring->scan_mask & mask)) {
36 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
38 if (ring_data == NULL) {
42 ret = ring->access->read_last(ring, (u8 *) ring_data);
44 goto error_free_ring_data;
46 /* for single channel scan the result is stored with zero offset */
47 if ((ring->scan_mask == ((1 << 1) | (1 << 0))) && (mask == (1 << 1)))
50 ret = be16_to_cpu(ring_data[count]);
59 * ad7887_ring_preenable() setup the parameters of the ring before enabling
61 * The complex nature of the setting of the nuber of bytes per datum is due
62 * to this driver currently ensuring that the timestamp is stored at an 8
65 static int ad7887_ring_preenable(struct iio_dev *indio_dev)
67 struct ad7887_state *st = indio_dev->dev_data;
68 struct iio_ring_buffer *ring = indio_dev->ring;
70 st->d_size = ring->scan_count *
71 st->chip_info->channel[0].scan_type.storagebits / 8;
73 if (ring->scan_timestamp) {
74 st->d_size += sizeof(s64);
76 if (st->d_size % sizeof(s64))
77 st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
80 if (indio_dev->ring->access->set_bytes_per_datum)
81 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
84 switch (ring->scan_mask) {
86 st->ring_msg = &st->msg[AD7887_CH0];
89 st->ring_msg = &st->msg[AD7887_CH1];
90 /* Dummy read: push CH1 setting down to hardware */
91 spi_sync(st->spi, st->ring_msg);
93 case ((1 << 1) | (1 << 0)):
94 st->ring_msg = &st->msg[AD7887_CH0_CH1];
101 static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
103 struct ad7887_state *st = indio_dev->dev_data;
105 /* dummy read: restore default CH0 settin */
106 return spi_sync(st->spi, &st->msg[AD7887_CH0]);
110 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
112 * Currently there is no option in this driver to disable the saving of
113 * timestamps within the ring.
115 static irqreturn_t ad7887_trigger_handler(int irq, void *p)
117 struct iio_poll_func *pf = p;
118 struct iio_dev *indio_dev = pf->private_data;
119 struct ad7887_state *st = iio_dev_get_devdata(indio_dev);
120 struct iio_ring_buffer *ring = indio_dev->ring;
125 unsigned int bytes = ring->scan_count *
126 st->chip_info->channel[0].scan_type.storagebits / 8;
128 buf = kzalloc(st->d_size, GFP_KERNEL);
132 b_sent = spi_sync(st->spi, st->ring_msg);
136 time_ns = iio_get_time_ns();
138 memcpy(buf, st->data, bytes);
139 if (ring->scan_timestamp)
140 memcpy(buf + st->d_size - sizeof(s64),
141 &time_ns, sizeof(time_ns));
143 indio_dev->ring->access->store_to(indio_dev->ring, buf, time_ns);
146 iio_trigger_notify_done(indio_dev->trig);
151 static const struct iio_ring_setup_ops ad7887_ring_setup_ops = {
152 .preenable = &ad7887_ring_preenable,
153 .postenable = &iio_triggered_ring_postenable,
154 .predisable = &iio_triggered_ring_predisable,
155 .postdisable = &ad7887_ring_postdisable,
158 int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
162 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
163 if (!indio_dev->ring) {
167 /* Effectively select the ring buffer implementation */
168 indio_dev->ring->access = &ring_sw_access_funcs;
169 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
170 if (indio_dev->pollfunc == NULL) {
172 goto error_deallocate_sw_rb;
175 indio_dev->pollfunc->private_data = indio_dev;
176 indio_dev->pollfunc->h = &iio_pollfunc_store_time;
177 indio_dev->pollfunc->thread = &ad7887_trigger_handler;
178 indio_dev->pollfunc->type = IRQF_ONESHOT;
179 indio_dev->pollfunc->name =
180 kasprintf(GFP_KERNEL, "ad7887_consumer%d", indio_dev->id);
181 if (indio_dev->pollfunc->name == NULL) {
183 goto error_free_pollfunc;
185 /* Ring buffer functions - here trigger setup related */
186 indio_dev->ring->setup_ops = &ad7887_ring_setup_ops;
188 /* Flag that polled ring buffering is possible */
189 indio_dev->modes |= INDIO_RING_TRIGGERED;
192 kfree(indio_dev->pollfunc);
193 error_deallocate_sw_rb:
194 iio_sw_rb_free(indio_dev->ring);
199 void ad7887_ring_cleanup(struct iio_dev *indio_dev)
201 /* ensure that the trigger has been detached */
202 if (indio_dev->trig) {
203 iio_put_trigger(indio_dev->trig);
204 iio_trigger_dettach_poll_func(indio_dev->trig,
205 indio_dev->pollfunc);
207 kfree(indio_dev->pollfunc->name);
208 kfree(indio_dev->pollfunc);
209 iio_sw_rb_free(indio_dev->ring);