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_C(in1, 1, 0, NULL);
31 static ssize_t ad7887_show_type(struct device *dev,
32 struct device_attribute *attr,
35 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
36 struct iio_dev *indio_dev = ring->indio_dev;
37 struct ad7887_state *st = indio_dev->dev_data;
39 return sprintf(buf, "%c%d/%d>>%d\n", st->chip_info->sign,
40 st->chip_info->bits, st->chip_info->storagebits,
41 st->chip_info->left_shift);
43 static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad7887_show_type, NULL, 0);
45 static struct attribute *ad7887_scan_el_attrs[] = {
46 &iio_scan_el_in0.dev_attr.attr,
47 &iio_const_attr_in0_index.dev_attr.attr,
48 &iio_scan_el_in1.dev_attr.attr,
49 &iio_const_attr_in1_index.dev_attr.attr,
50 &iio_dev_attr_in_type.dev_attr.attr,
54 static mode_t ad7887_scan_el_attr_is_visible(struct kobject *kobj,
55 struct attribute *attr, int n)
57 struct device *dev = container_of(kobj, struct device, kobj);
58 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
59 struct iio_dev *indio_dev = ring->indio_dev;
60 struct ad7887_state *st = indio_dev->dev_data;
62 mode_t mode = attr->mode;
64 if ((attr == &iio_scan_el_in1.dev_attr.attr) ||
65 (attr == &iio_const_attr_in1_index.dev_attr.attr))
72 static struct attribute_group ad7887_scan_el_group = {
73 .name = "scan_elements",
74 .attrs = ad7887_scan_el_attrs,
75 .is_visible = ad7887_scan_el_attr_is_visible,
78 int ad7887_scan_from_ring(struct ad7887_state *st, long mask)
80 struct iio_ring_buffer *ring = st->indio_dev->ring;
84 if (!(ring->scan_mask & mask)) {
89 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
90 if (ring_data == NULL) {
94 ret = ring->access.read_last(ring, (u8 *) ring_data);
96 goto error_free_ring_data;
98 /* for single channel scan the result is stored with zero offset */
99 if ((ring->scan_mask == ((1 << 1) | (1 << 0))) && (mask == (1 << 1)))
102 ret = be16_to_cpu(ring_data[count]);
104 error_free_ring_data:
111 * ad7887_ring_preenable() setup the parameters of the ring before enabling
113 * The complex nature of the setting of the nuber of bytes per datum is due
114 * to this driver currently ensuring that the timestamp is stored at an 8
117 static int ad7887_ring_preenable(struct iio_dev *indio_dev)
119 struct ad7887_state *st = indio_dev->dev_data;
120 struct iio_ring_buffer *ring = indio_dev->ring;
123 if (indio_dev->ring->access.set_bytes_per_datum) {
124 d_size = st->chip_info->storagebits / 8 + sizeof(s64);
126 d_size += 8 - (d_size % 8);
127 indio_dev->ring->access.set_bytes_per_datum(indio_dev->ring,
131 switch (ring->scan_mask) {
133 st->ring_msg = &st->msg[AD7887_CH0];
136 st->ring_msg = &st->msg[AD7887_CH1];
137 /* Dummy read: push CH1 setting down to hardware */
138 spi_sync(st->spi, st->ring_msg);
140 case ((1 << 1) | (1 << 0)):
141 st->ring_msg = &st->msg[AD7887_CH0_CH1];
148 static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
150 struct ad7887_state *st = indio_dev->dev_data;
152 /* dummy read: restore default CH0 settin */
153 return spi_sync(st->spi, &st->msg[AD7887_CH0]);
157 * ad7887_poll_func_th() th of trigger launched polling to ring buffer
159 * As sampling only occurs on spi comms occuring, leave timestamping until
160 * then. Some triggers will generate their own time stamp. Currently
161 * there is no way of notifying them when no one cares.
163 static void ad7887_poll_func_th(struct iio_dev *indio_dev, s64 time)
165 struct ad7887_state *st = indio_dev->dev_data;
167 schedule_work(&st->poll_work);
171 * ad7887_poll_bh_to_ring() bh of trigger launched polling to ring buffer
172 * @work_s: the work struct through which this was scheduled
174 * Currently there is no option in this driver to disable the saving of
175 * timestamps within the ring.
176 * I think the one copy of this at a time was to avoid problems if the
177 * trigger was set far too high and the reads then locked up the computer.
179 static void ad7887_poll_bh_to_ring(struct work_struct *work_s)
181 struct ad7887_state *st = container_of(work_s, struct ad7887_state,
183 struct iio_dev *indio_dev = st->indio_dev;
184 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
185 struct iio_ring_buffer *ring = indio_dev->ring;
191 unsigned int bytes = ring->scan_count * st->chip_info->storagebits / 8;
193 /* Ensure the timestamp is 8 byte aligned */
194 d_size = bytes + sizeof(s64);
195 if (d_size % sizeof(s64))
196 d_size += sizeof(s64) - (d_size % sizeof(s64));
198 /* Ensure only one copy of this function running at a time */
199 if (atomic_inc_return(&st->protect_ring) > 1)
202 buf = kzalloc(d_size, GFP_KERNEL);
206 b_sent = spi_sync(st->spi, st->ring_msg);
210 time_ns = iio_get_time_ns();
212 memcpy(buf, st->data, bytes);
213 memcpy(buf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
215 indio_dev->ring->access.store_to(&sw_ring->buf, buf, time_ns);
218 atomic_dec(&st->protect_ring);
221 int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
223 struct ad7887_state *st = indio_dev->dev_data;
226 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
227 if (!indio_dev->ring) {
231 /* Effectively select the ring buffer implementation */
232 iio_ring_sw_register_funcs(&indio_dev->ring->access);
233 ret = iio_alloc_pollfunc(indio_dev, NULL, &ad7887_poll_func_th);
235 goto error_deallocate_sw_rb;
237 /* Ring buffer functions - here trigger setup related */
239 indio_dev->ring->preenable = &ad7887_ring_preenable;
240 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
241 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
242 indio_dev->ring->postdisable = &ad7887_ring_postdisable;
243 indio_dev->ring->scan_el_attrs = &ad7887_scan_el_group;
245 INIT_WORK(&st->poll_work, &ad7887_poll_bh_to_ring);
247 /* Flag that polled ring buffering is possible */
248 indio_dev->modes |= INDIO_RING_TRIGGERED;
250 error_deallocate_sw_rb:
251 iio_sw_rb_free(indio_dev->ring);
256 void ad7887_ring_cleanup(struct iio_dev *indio_dev)
258 /* ensure that the trigger has been detached */
259 if (indio_dev->trig) {
260 iio_put_trigger(indio_dev->trig);
261 iio_trigger_dettach_poll_func(indio_dev->trig,
262 indio_dev->pollfunc);
264 kfree(indio_dev->pollfunc);
265 iio_sw_rb_free(indio_dev->ring);