2 * Copyright 2011 Analog Devices Inc.
4 * Licensed under the GPL-2.
8 #include <linux/interrupt.h>
9 #include <linux/gpio.h>
10 #include <linux/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
17 #include "../ring_generic.h"
18 #include "../ring_sw.h"
19 #include "../trigger.h"
24 int ad7606_scan_from_ring(struct ad7606_state *st, unsigned ch)
26 struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
30 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
32 if (ring_data == NULL) {
36 ret = ring->access->read_last(ring, (u8 *) ring_data);
38 goto error_free_ring_data;
49 * ad7606_ring_preenable() setup the parameters of the ring before enabling
51 * The complex nature of the setting of the nuber of bytes per datum is due
52 * to this driver currently ensuring that the timestamp is stored at an 8
55 static int ad7606_ring_preenable(struct iio_dev *indio_dev)
57 struct ad7606_state *st = indio_dev->dev_data;
58 struct iio_ring_buffer *ring = indio_dev->ring;
61 d_size = st->chip_info->num_channels *
62 st->chip_info->channels[0].scan_type.storagebits / 8;
64 if (ring->scan_timestamp) {
65 d_size += sizeof(s64);
67 if (d_size % sizeof(s64))
68 d_size += sizeof(s64) - (d_size % sizeof(s64));
71 if (ring->access->set_bytes_per_datum)
72 ring->access->set_bytes_per_datum(ring, d_size);
80 * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
83 static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p)
85 struct iio_poll_func *pf = p;
86 struct iio_dev *indio_dev = pf->private_data;
87 struct ad7606_state *st = indio_dev->dev_data;
89 gpio_set_value(st->pdata->gpio_convst, 1);
95 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
96 * @work_s: the work struct through which this was scheduled
98 * Currently there is no option in this driver to disable the saving of
99 * timestamps within the ring.
100 * I think the one copy of this at a time was to avoid problems if the
101 * trigger was set far too high and the reads then locked up the computer.
103 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
105 struct ad7606_state *st = container_of(work_s, struct ad7606_state,
107 struct iio_dev *indio_dev = iio_priv_to_dev(st);
108 struct iio_ring_buffer *ring = indio_dev->ring;
113 buf = kzalloc(st->d_size, GFP_KERNEL);
117 if (st->have_frstdata) {
118 ret = st->bops->read_block(st->dev, 1, buf);
121 if (!gpio_get_value(st->pdata->gpio_frstdata)) {
122 /* This should never happen. However
123 * some signal glitch caused by bad PCB desgin or
124 * electrostatic discharge, could cause an extra read
125 * or clock. This allows recovery.
130 ret = st->bops->read_block(st->dev,
131 st->chip_info->num_channels - 1, buf + 2);
135 ret = st->bops->read_block(st->dev,
136 st->chip_info->num_channels, buf);
141 time_ns = iio_get_time_ns();
143 if (ring->scan_timestamp)
144 memcpy(buf + st->d_size - sizeof(s64),
145 &time_ns, sizeof(time_ns));
147 ring->access->store_to(indio_dev->ring, buf, time_ns);
149 gpio_set_value(st->pdata->gpio_convst, 0);
150 iio_trigger_notify_done(indio_dev->trig);
154 static const struct iio_ring_setup_ops ad7606_ring_setup_ops = {
155 .preenable = &ad7606_ring_preenable,
156 .postenable = &iio_triggered_ring_postenable,
157 .predisable = &iio_triggered_ring_predisable,
160 int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
162 struct ad7606_state *st = indio_dev->dev_data;
165 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
166 if (!indio_dev->ring) {
171 /* Effectively select the ring buffer implementation */
172 indio_dev->ring->access = &ring_sw_access_funcs;
173 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
174 if (indio_dev->pollfunc == NULL) {
176 goto error_deallocate_sw_rb;
179 indio_dev->pollfunc->private_data = indio_dev;
180 indio_dev->pollfunc->h = &ad7606_trigger_handler_th_bh;
181 indio_dev->pollfunc->thread = &ad7606_trigger_handler_th_bh;
182 indio_dev->pollfunc->name =
183 kasprintf(GFP_KERNEL, "%s_consumer%d", indio_dev->name,
185 if (indio_dev->pollfunc->name == NULL) {
187 goto error_free_poll_func;
189 /* Ring buffer functions - here trigger setup related */
191 indio_dev->ring->setup_ops = &ad7606_ring_setup_ops;
192 indio_dev->ring->scan_timestamp = true ;
194 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
196 /* Flag that polled ring buffering is possible */
197 indio_dev->modes |= INDIO_RING_TRIGGERED;
199 error_free_poll_func:
200 kfree(indio_dev->pollfunc);
201 error_deallocate_sw_rb:
202 iio_sw_rb_free(indio_dev->ring);
207 void ad7606_ring_cleanup(struct iio_dev *indio_dev)
209 if (indio_dev->trig) {
210 iio_put_trigger(indio_dev->trig);
211 iio_trigger_dettach_poll_func(indio_dev->trig,
212 indio_dev->pollfunc);
214 kfree(indio_dev->pollfunc->name);
215 kfree(indio_dev->pollfunc);
216 iio_sw_rb_free(indio_dev->ring);