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);
30 static ssize_t ad7476_show_type(struct device *dev,
31 struct device_attribute *attr,
34 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
35 struct iio_dev *indio_dev = ring->indio_dev;
36 struct ad7476_state *st = indio_dev->dev_data;
38 return sprintf(buf, "%c%d/%d>>%d\n", st->chip_info->sign,
39 st->chip_info->bits, st->chip_info->storagebits,
40 st->chip_info->res_shift);
42 static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad7476_show_type, NULL, 0);
44 static struct attribute *ad7476_scan_el_attrs[] = {
45 &iio_scan_el_in0.dev_attr.attr,
46 &iio_const_attr_in0_index.dev_attr.attr,
47 &iio_dev_attr_in_type.dev_attr.attr,
51 static struct attribute_group ad7476_scan_el_group = {
52 .name = "scan_elements",
53 .attrs = ad7476_scan_el_attrs,
56 int ad7476_scan_from_ring(struct ad7476_state *st)
58 struct iio_ring_buffer *ring = st->indio_dev->ring;
62 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
63 if (ring_data == NULL) {
67 ret = ring->access.read_last(ring, ring_data);
69 goto error_free_ring_data;
71 ret = (ring_data[0] << 8) | ring_data[1];
80 * ad7476_ring_preenable() setup the parameters of the ring before enabling
82 * The complex nature of the setting of the nuber of bytes per datum is due
83 * to this driver currently ensuring that the timestamp is stored at an 8
86 static int ad7476_ring_preenable(struct iio_dev *indio_dev)
88 struct ad7476_state *st = indio_dev->dev_data;
91 if (indio_dev->ring->access.set_bytes_per_datum) {
92 d_size = st->chip_info->storagebits / 8 + sizeof(s64);
94 d_size += 8 - (d_size % 8);
95 indio_dev->ring->access.set_bytes_per_datum(indio_dev->ring,
103 * ad7476_poll_func_th() th of trigger launched polling to ring buffer
105 * As sampling only occurs on i2c comms occuring, leave timestamping until
106 * then. Some triggers will generate their own time stamp. Currently
107 * there is no way of notifying them when no one cares.
109 static void ad7476_poll_func_th(struct iio_dev *indio_dev, s64 time)
111 struct ad7476_state *st = indio_dev->dev_data;
113 schedule_work(&st->poll_work);
117 * ad7476_poll_bh_to_ring() bh of trigger launched polling to ring buffer
118 * @work_s: the work struct through which this was scheduled
120 * Currently there is no option in this driver to disable the saving of
121 * timestamps within the ring.
122 * I think the one copy of this at a time was to avoid problems if the
123 * trigger was set far too high and the reads then locked up the computer.
125 static void ad7476_poll_bh_to_ring(struct work_struct *work_s)
127 struct ad7476_state *st = container_of(work_s, struct ad7476_state,
129 struct iio_dev *indio_dev = st->indio_dev;
130 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
136 /* Ensure the timestamp is 8 byte aligned */
137 d_size = st->chip_info->storagebits / 8 + sizeof(s64);
138 if (d_size % sizeof(s64))
139 d_size += sizeof(s64) - (d_size % sizeof(s64));
141 /* Ensure only one copy of this function running at a time */
142 if (atomic_inc_return(&st->protect_ring) > 1)
145 rxbuf = kzalloc(d_size, GFP_KERNEL);
149 b_sent = spi_read(st->spi, rxbuf, st->chip_info->storagebits / 8);
153 time_ns = iio_get_time_ns();
155 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
157 indio_dev->ring->access.store_to(&sw_ring->buf, rxbuf, time_ns);
160 atomic_dec(&st->protect_ring);
163 int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
165 struct ad7476_state *st = indio_dev->dev_data;
168 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
169 if (!indio_dev->ring) {
173 /* Effectively select the ring buffer implementation */
174 iio_ring_sw_register_funcs(&indio_dev->ring->access);
175 ret = iio_alloc_pollfunc(indio_dev, NULL, &ad7476_poll_func_th);
177 goto error_deallocate_sw_rb;
179 /* Ring buffer functions - here trigger setup related */
181 indio_dev->ring->preenable = &ad7476_ring_preenable;
182 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
183 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
184 indio_dev->ring->scan_el_attrs = &ad7476_scan_el_group;
186 INIT_WORK(&st->poll_work, &ad7476_poll_bh_to_ring);
188 /* Flag that polled ring buffering is possible */
189 indio_dev->modes |= INDIO_RING_TRIGGERED;
191 error_deallocate_sw_rb:
192 iio_sw_rb_free(indio_dev->ring);
197 void ad7476_ring_cleanup(struct iio_dev *indio_dev)
199 /* ensure that the trigger has been detached */
200 if (indio_dev->trig) {
201 iio_put_trigger(indio_dev->trig);
202 iio_trigger_dettach_poll_func(indio_dev->trig,
203 indio_dev->pollfunc);
205 kfree(indio_dev->pollfunc);
206 iio_sw_rb_free(indio_dev->ring);