2 * Copyright 2010-2012 Analog Devices Inc.
3 * Copyright (C) 2008 Jonathan Cameron
5 * Licensed under the GPL-2.
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/kfifo_buf.h>
18 #include <linux/iio/trigger_consumer.h>
23 * ad7887_ring_preenable() setup the parameters of the ring before enabling
25 * The complex nature of the setting of the nuber of bytes per datum is due
26 * to this driver currently ensuring that the timestamp is stored at an 8
29 static int ad7887_ring_preenable(struct iio_dev *indio_dev)
31 struct ad7887_state *st = iio_priv(indio_dev);
34 ret = iio_sw_buffer_preenable(indio_dev);
38 /* We know this is a single long so can 'cheat' */
39 switch (*indio_dev->active_scan_mask) {
41 st->ring_msg = &st->msg[AD7887_CH0];
44 st->ring_msg = &st->msg[AD7887_CH1];
45 /* Dummy read: push CH1 setting down to hardware */
46 spi_sync(st->spi, st->ring_msg);
48 case ((1 << 1) | (1 << 0)):
49 st->ring_msg = &st->msg[AD7887_CH0_CH1];
56 static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
58 struct ad7887_state *st = iio_priv(indio_dev);
60 /* dummy read: restore default CH0 settin */
61 return spi_sync(st->spi, &st->msg[AD7887_CH0]);
65 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
67 * Currently there is no option in this driver to disable the saving of
68 * timestamps within the ring.
70 static irqreturn_t ad7887_trigger_handler(int irq, void *p)
72 struct iio_poll_func *pf = p;
73 struct iio_dev *indio_dev = pf->indio_dev;
74 struct ad7887_state *st = iio_priv(indio_dev);
79 unsigned int bytes = bitmap_weight(indio_dev->active_scan_mask,
80 indio_dev->masklength) *
81 st->chip_info->channel[0].scan_type.storagebits / 8;
83 buf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
87 b_sent = spi_sync(st->spi, st->ring_msg);
91 time_ns = iio_get_time_ns();
93 memcpy(buf, st->data, bytes);
94 if (indio_dev->scan_timestamp)
95 memcpy(buf + indio_dev->scan_bytes - sizeof(s64),
96 &time_ns, sizeof(time_ns));
98 indio_dev->buffer->access->store_to(indio_dev->buffer, buf, time_ns);
101 iio_trigger_notify_done(indio_dev->trig);
106 static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = {
107 .preenable = &ad7887_ring_preenable,
108 .postenable = &iio_triggered_buffer_postenable,
109 .predisable = &iio_triggered_buffer_predisable,
110 .postdisable = &ad7887_ring_postdisable,
113 int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
117 indio_dev->buffer = iio_kfifo_allocate(indio_dev);
118 if (!indio_dev->buffer) {
122 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
123 &ad7887_trigger_handler,
128 if (indio_dev->pollfunc == NULL) {
130 goto error_deallocate_kfifo;
132 /* Ring buffer functions - here trigger setup related */
133 indio_dev->setup_ops = &ad7887_ring_setup_ops;
135 /* Flag that polled ring buffering is possible */
136 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
139 error_deallocate_kfifo:
140 iio_kfifo_free(indio_dev->buffer);
145 void ad7887_ring_cleanup(struct iio_dev *indio_dev)
147 iio_dealloc_pollfunc(indio_dev->pollfunc);
148 iio_kfifo_free(indio_dev->buffer);