2 * Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
3 * Copyright (C) 2008-2010 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
12 #include <linux/interrupt.h>
13 #include <linux/workqueue.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <linux/i2c.h>
20 #include <linux/bitops.h>
23 #include "../ring_generic.h"
24 #include "../ring_sw.h"
25 #include "../trigger.h"
30 int ad799x_single_channel_from_ring(struct ad799x_state *st, long mask)
32 struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
36 if (!(ring->scan_mask & mask)) {
41 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
42 if (ring_data == NULL) {
46 ret = ring->access.read_last(ring, (u8 *) ring_data);
48 goto error_free_ring_data;
49 /* Need a count of channels prior to this one */
52 if (mask & ring->scan_mask)
57 ret = be16_to_cpu(ring_data[count]);
66 * ad799x_ring_preenable() setup the parameters of the ring before enabling
68 * The complex nature of the setting of the nuber of bytes per datum is due
69 * to this driver currently ensuring that the timestamp is stored at an 8
72 static int ad799x_ring_preenable(struct iio_dev *indio_dev)
74 struct iio_ring_buffer *ring = indio_dev->ring;
75 struct ad799x_state *st = iio_dev_get_devdata(indio_dev);
78 * Need to figure out the current mode based upon the requested
79 * scan mask in iio_dev
82 if (st->id == ad7997 || st->id == ad7998)
83 ad7997_8_set_scan_mode(st, ring->scan_mask);
85 st->d_size = ring->scan_count * 2;
87 if (ring->scan_timestamp) {
88 st->d_size += sizeof(s64);
90 if (st->d_size % sizeof(s64))
91 st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
94 if (indio_dev->ring->access.set_bytes_per_datum)
95 indio_dev->ring->access.set_bytes_per_datum(indio_dev->ring,
102 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
104 * Currently there is no option in this driver to disable the saving of
105 * timestamps within the ring.
108 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
110 struct iio_poll_func *pf = p;
111 struct iio_dev *indio_dev = pf->private_data;
112 struct ad799x_state *st = iio_dev_get_devdata(indio_dev);
113 struct iio_ring_buffer *ring = indio_dev->ring;
114 struct iio_sw_ring_buffer *ring_sw = iio_to_sw_ring(indio_dev->ring);
120 rxbuf = kmalloc(st->d_size, GFP_KERNEL);
128 cmd = st->config | (ring->scan_mask << AD799X_CHANNEL_SHIFT);
133 cmd = (ring->scan_mask << AD799X_CHANNEL_SHIFT) |
138 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
144 b_sent = i2c_smbus_read_i2c_block_data(st->client,
145 cmd, ring->scan_count * 2, rxbuf);
149 time_ns = iio_get_time_ns();
151 if (ring->scan_timestamp)
152 memcpy(rxbuf + st->d_size - sizeof(s64),
153 &time_ns, sizeof(time_ns));
155 ring->access.store_to(&ring_sw->buf, rxbuf, time_ns);
161 iio_trigger_notify_done(indio_dev->trig);
167 int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
171 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
172 if (!indio_dev->ring) {
176 /* Effectively select the ring buffer implementation */
177 iio_ring_sw_register_funcs(&indio_dev->ring->access);
178 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
179 if (indio_dev->pollfunc == NULL) {
181 goto error_deallocate_sw_rb;
183 indio_dev->pollfunc->private_data = indio_dev;
184 indio_dev->pollfunc->thread = &ad799x_trigger_handler;
185 indio_dev->pollfunc->type = IRQF_ONESHOT;
186 indio_dev->pollfunc->name =
187 kasprintf(GFP_KERNEL, "%s_consumer%d", indio_dev->name,
189 if (indio_dev->pollfunc->name == NULL) {
191 goto error_free_poll_func;
193 /* Ring buffer functions - here trigger setup related */
195 indio_dev->ring->preenable = &ad799x_ring_preenable;
196 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
197 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
198 indio_dev->ring->scan_timestamp = true;
200 /* Flag that polled ring buffering is possible */
201 indio_dev->modes |= INDIO_RING_TRIGGERED;
203 error_free_poll_func:
204 kfree(indio_dev->pollfunc);
205 error_deallocate_sw_rb:
206 iio_sw_rb_free(indio_dev->ring);
211 void ad799x_ring_cleanup(struct iio_dev *indio_dev)
213 /* ensure that the trigger has been detached */
214 if (indio_dev->trig) {
215 iio_put_trigger(indio_dev->trig);
216 iio_trigger_dettach_poll_func(indio_dev->trig,
217 indio_dev->pollfunc);
219 kfree(indio_dev->pollfunc->name);
220 kfree(indio_dev->pollfunc);
221 iio_sw_rb_free(indio_dev->ring);