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/gpio.h>
12 #include <linux/workqueue.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/list.h>
18 #include <linux/spi/spi.h>
21 #include "../ring_generic.h"
22 #include "../ring_sw.h"
23 #include "../trigger.h"
28 static IIO_SCAN_EL_C(in0, 0, 0, NULL);
29 static IIO_SCAN_EL_TIMESTAMP(1);
30 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
32 static ssize_t ad7476_show_type(struct device *dev,
33 struct device_attribute *attr,
36 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
37 struct iio_dev *indio_dev = ring->indio_dev;
38 struct ad7476_state *st = indio_dev->dev_data;
40 return sprintf(buf, "%c%d/%d>>%d\n", st->chip_info->sign,
41 st->chip_info->bits, st->chip_info->storagebits,
42 st->chip_info->res_shift);
44 static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad7476_show_type, NULL, 0);
46 static struct attribute *ad7476_scan_el_attrs[] = {
47 &iio_scan_el_in0.dev_attr.attr,
48 &iio_const_attr_in0_index.dev_attr.attr,
49 &iio_const_attr_timestamp_index.dev_attr.attr,
50 &iio_scan_el_timestamp.dev_attr.attr,
51 &iio_const_attr_timestamp_type.dev_attr.attr,
52 &iio_dev_attr_in_type.dev_attr.attr,
56 static struct attribute_group ad7476_scan_el_group = {
57 .name = "scan_elements",
58 .attrs = ad7476_scan_el_attrs,
61 int ad7476_scan_from_ring(struct ad7476_state *st)
63 struct iio_ring_buffer *ring = st->indio_dev->ring;
67 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
68 if (ring_data == NULL) {
72 ret = ring->access.read_last(ring, ring_data);
74 goto error_free_ring_data;
76 ret = (ring_data[0] << 8) | ring_data[1];
85 * ad7476_ring_preenable() setup the parameters of the ring before enabling
87 * The complex nature of the setting of the nuber of bytes per datum is due
88 * to this driver currently ensuring that the timestamp is stored at an 8
91 static int ad7476_ring_preenable(struct iio_dev *indio_dev)
93 struct ad7476_state *st = indio_dev->dev_data;
94 struct iio_ring_buffer *ring = indio_dev->ring;
96 st->d_size = ring->scan_count * st->chip_info->storagebits / 8;
98 if (ring->scan_timestamp) {
99 st->d_size += sizeof(s64);
101 if (st->d_size % sizeof(s64))
102 st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
105 if (indio_dev->ring->access.set_bytes_per_datum)
106 indio_dev->ring->access.set_bytes_per_datum(indio_dev->ring,
113 * ad7476_poll_func_th() th of trigger launched polling to ring buffer
115 * As sampling only occurs on i2c comms occurring, leave timestamping until
116 * then. Some triggers will generate their own time stamp. Currently
117 * there is no way of notifying them when no one cares.
119 static void ad7476_poll_func_th(struct iio_dev *indio_dev, s64 time)
121 struct ad7476_state *st = indio_dev->dev_data;
123 schedule_work(&st->poll_work);
127 * ad7476_poll_bh_to_ring() bh of trigger launched polling to ring buffer
128 * @work_s: the work struct through which this was scheduled
130 * Currently there is no option in this driver to disable the saving of
131 * timestamps within the ring.
132 * I think the one copy of this at a time was to avoid problems if the
133 * trigger was set far too high and the reads then locked up the computer.
135 static void ad7476_poll_bh_to_ring(struct work_struct *work_s)
137 struct ad7476_state *st = container_of(work_s, struct ad7476_state,
139 struct iio_dev *indio_dev = st->indio_dev;
140 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
145 /* Ensure only one copy of this function running at a time */
146 if (atomic_inc_return(&st->protect_ring) > 1)
149 rxbuf = kzalloc(st->d_size, GFP_KERNEL);
153 b_sent = spi_read(st->spi, rxbuf, st->chip_info->storagebits / 8);
157 time_ns = iio_get_time_ns();
159 if (indio_dev->ring->scan_timestamp)
160 memcpy(rxbuf + st->d_size - sizeof(s64),
161 &time_ns, sizeof(time_ns));
163 indio_dev->ring->access.store_to(&sw_ring->buf, rxbuf, time_ns);
166 atomic_dec(&st->protect_ring);
169 int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
171 struct ad7476_state *st = indio_dev->dev_data;
174 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
175 if (!indio_dev->ring) {
179 /* Effectively select the ring buffer implementation */
180 iio_ring_sw_register_funcs(&indio_dev->ring->access);
181 ret = iio_alloc_pollfunc(indio_dev, NULL, &ad7476_poll_func_th);
183 goto error_deallocate_sw_rb;
185 /* Ring buffer functions - here trigger setup related */
187 indio_dev->ring->preenable = &ad7476_ring_preenable;
188 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
189 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
190 indio_dev->ring->scan_el_attrs = &ad7476_scan_el_group;
191 indio_dev->ring->scan_timestamp = true;
193 INIT_WORK(&st->poll_work, &ad7476_poll_bh_to_ring);
195 /* Flag that polled ring buffering is possible */
196 indio_dev->modes |= INDIO_RING_TRIGGERED;
198 error_deallocate_sw_rb:
199 iio_sw_rb_free(indio_dev->ring);
204 void ad7476_ring_cleanup(struct iio_dev *indio_dev)
206 /* ensure that the trigger has been detached */
207 if (indio_dev->trig) {
208 iio_put_trigger(indio_dev->trig);
209 iio_trigger_dettach_poll_func(indio_dev->trig,
210 indio_dev->pollfunc);
212 kfree(indio_dev->pollfunc);
213 iio_sw_rb_free(indio_dev->ring);