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 static IIO_SCAN_EL_C(in0, 0, 0, NULL);
25 static IIO_SCAN_EL_C(in1, 1, 0, NULL);
26 static IIO_SCAN_EL_C(in2, 2, 0, NULL);
27 static IIO_SCAN_EL_C(in3, 3, 0, NULL);
28 static IIO_SCAN_EL_C(in4, 4, 0, NULL);
29 static IIO_SCAN_EL_C(in5, 5, 0, NULL);
30 static IIO_SCAN_EL_C(in6, 6, 0, NULL);
31 static IIO_SCAN_EL_C(in7, 7, 0, NULL);
33 static IIO_SCAN_EL_TIMESTAMP(8);
34 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
36 static ssize_t ad7606_show_type(struct device *dev,
37 struct device_attribute *attr,
40 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
41 struct iio_dev *indio_dev = ring->indio_dev;
42 struct ad7606_state *st = indio_dev->dev_data;
44 return sprintf(buf, "%c%d/%d\n", st->chip_info->sign,
45 st->chip_info->bits, st->chip_info->bits);
47 static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad7606_show_type, NULL, 0);
49 static struct attribute *ad7606_scan_el_attrs[] = {
50 &iio_scan_el_in0.dev_attr.attr,
51 &iio_const_attr_in0_index.dev_attr.attr,
52 &iio_scan_el_in1.dev_attr.attr,
53 &iio_const_attr_in1_index.dev_attr.attr,
54 &iio_scan_el_in2.dev_attr.attr,
55 &iio_const_attr_in2_index.dev_attr.attr,
56 &iio_scan_el_in3.dev_attr.attr,
57 &iio_const_attr_in3_index.dev_attr.attr,
58 &iio_scan_el_in4.dev_attr.attr,
59 &iio_const_attr_in4_index.dev_attr.attr,
60 &iio_scan_el_in5.dev_attr.attr,
61 &iio_const_attr_in5_index.dev_attr.attr,
62 &iio_scan_el_in6.dev_attr.attr,
63 &iio_const_attr_in6_index.dev_attr.attr,
64 &iio_scan_el_in7.dev_attr.attr,
65 &iio_const_attr_in7_index.dev_attr.attr,
66 &iio_const_attr_timestamp_index.dev_attr.attr,
67 &iio_scan_el_timestamp.dev_attr.attr,
68 &iio_const_attr_timestamp_type.dev_attr.attr,
69 &iio_dev_attr_in_type.dev_attr.attr,
73 static mode_t ad7606_scan_el_attr_is_visible(struct kobject *kobj,
74 struct attribute *attr, int n)
76 struct device *dev = container_of(kobj, struct device, kobj);
77 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
78 struct iio_dev *indio_dev = ring->indio_dev;
79 struct ad7606_state *st = indio_dev->dev_data;
81 mode_t mode = attr->mode;
83 if (st->chip_info->num_channels <= 6 &&
84 (attr == &iio_scan_el_in7.dev_attr.attr ||
85 attr == &iio_const_attr_in7_index.dev_attr.attr ||
86 attr == &iio_scan_el_in6.dev_attr.attr ||
87 attr == &iio_const_attr_in6_index.dev_attr.attr))
89 else if (st->chip_info->num_channels <= 4 &&
90 (attr == &iio_scan_el_in5.dev_attr.attr ||
91 attr == &iio_const_attr_in5_index.dev_attr.attr ||
92 attr == &iio_scan_el_in4.dev_attr.attr ||
93 attr == &iio_const_attr_in4_index.dev_attr.attr))
99 static struct attribute_group ad7606_scan_el_group = {
100 .name = "scan_elements",
101 .attrs = ad7606_scan_el_attrs,
102 .is_visible = ad7606_scan_el_attr_is_visible,
105 int ad7606_scan_from_ring(struct ad7606_state *st, unsigned ch)
107 struct iio_ring_buffer *ring = st->indio_dev->ring;
111 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
112 if (ring_data == NULL) {
116 ret = ring->access.read_last(ring, (u8 *) ring_data);
118 goto error_free_ring_data;
122 error_free_ring_data:
129 * ad7606_ring_preenable() setup the parameters of the ring before enabling
131 * The complex nature of the setting of the nuber of bytes per datum is due
132 * to this driver currently ensuring that the timestamp is stored at an 8
135 static int ad7606_ring_preenable(struct iio_dev *indio_dev)
137 struct ad7606_state *st = indio_dev->dev_data;
138 struct iio_ring_buffer *ring = indio_dev->ring;
141 d_size = st->chip_info->num_channels *
142 st->chip_info->bits / 8;
144 if (ring->scan_timestamp) {
145 d_size += sizeof(s64);
147 if (d_size % sizeof(s64))
148 d_size += sizeof(s64) - (d_size % sizeof(s64));
151 if (ring->access.set_bytes_per_datum)
152 ring->access.set_bytes_per_datum(ring, d_size);
160 * ad7606_trigger_handler_th() th of trigger launched polling to ring buffer
163 static irqreturn_t ad7606_trigger_handler_th(int irq, void *p)
165 struct iio_poll_func *pf = p;
166 struct iio_dev *indio_dev = pf->private_data;
167 struct ad7606_state *st = indio_dev->dev_data;
168 gpio_set_value(st->pdata->gpio_convst, 1);
174 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
175 * @work_s: the work struct through which this was scheduled
177 * Currently there is no option in this driver to disable the saving of
178 * timestamps within the ring.
179 * I think the one copy of this at a time was to avoid problems if the
180 * trigger was set far too high and the reads then locked up the computer.
182 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
184 struct ad7606_state *st = container_of(work_s, struct ad7606_state,
186 struct iio_dev *indio_dev = st->indio_dev;
187 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
188 struct iio_ring_buffer *ring = indio_dev->ring;
193 /* Ensure only one copy of this function running at a time */
194 if (atomic_inc_return(&st->protect_ring) > 1)
197 buf = kzalloc(st->d_size, GFP_KERNEL);
201 if (st->have_frstdata) {
202 ret = st->bops->read_block(st->dev, 1, buf);
205 if (!gpio_get_value(st->pdata->gpio_frstdata)) {
206 /* This should never happen. However
207 * some signal glitch caused by bad PCB desgin or
208 * electrostatic discharge, could cause an extra read
209 * or clock. This allows recovery.
214 ret = st->bops->read_block(st->dev,
215 st->chip_info->num_channels - 1, buf + 2);
219 ret = st->bops->read_block(st->dev,
220 st->chip_info->num_channels, buf);
225 time_ns = iio_get_time_ns();
227 if (ring->scan_timestamp)
228 memcpy(buf + st->d_size - sizeof(s64),
229 &time_ns, sizeof(time_ns));
231 ring->access.store_to(&sw_ring->buf, buf, time_ns);
233 gpio_set_value(st->pdata->gpio_convst, 0);
235 atomic_dec(&st->protect_ring);
238 int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
240 struct ad7606_state *st = indio_dev->dev_data;
243 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
244 if (!indio_dev->ring) {
249 /* Effectively select the ring buffer implementation */
250 iio_ring_sw_register_funcs(&indio_dev->ring->access);
251 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
252 if (indio_dev->pollfunc == NULL) {
254 goto error_deallocate_sw_rb;
256 indio_dev->pollfunc->private_data = indio_dev;
257 indio_dev->pollfunc->h = &ad7606_trigger_handler_th;
258 indio_dev->pollfunc->name =
259 kasprintf(GFP_KERNEL, "%s_consumer%d", indio_dev->name,
261 if (indio_dev->pollfunc->name == NULL) {
263 goto error_free_poll_func;
265 /* Ring buffer functions - here trigger setup related */
267 indio_dev->ring->preenable = &ad7606_ring_preenable;
268 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
269 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
270 indio_dev->ring->scan_el_attrs = &ad7606_scan_el_group;
271 indio_dev->ring->scan_timestamp = true ;
273 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
275 /* Flag that polled ring buffering is possible */
276 indio_dev->modes |= INDIO_RING_TRIGGERED;
278 error_free_poll_func:
279 kfree(indio_dev->pollfunc);
280 error_deallocate_sw_rb:
281 iio_sw_rb_free(indio_dev->ring);
286 void ad7606_ring_cleanup(struct iio_dev *indio_dev)
288 if (indio_dev->trig) {
289 iio_put_trigger(indio_dev->trig);
290 iio_trigger_dettach_poll_func(indio_dev->trig,
291 indio_dev->pollfunc);
293 kfree(indio_dev->pollfunc->name);
294 kfree(indio_dev->pollfunc);
295 iio_sw_rb_free(indio_dev->ring);