]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/ad7606_ring.c
43caced22cee5f647eec488b8e05abb6093ffb43
[karo-tx-linux.git] / drivers / staging / iio / adc / ad7606_ring.c
1 /*
2  * Copyright 2011 Analog Devices Inc.
3  *
4  * Licensed under the GPL-2.
5  *
6  */
7
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>
15
16 #include "../iio.h"
17 #include "../ring_generic.h"
18 #include "../ring_sw.h"
19 #include "../trigger.h"
20 #include "../sysfs.h"
21
22 #include "ad7606.h"
23
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);
32
33 static IIO_SCAN_EL_TIMESTAMP(8);
34 static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64);
35
36 static ssize_t ad7606_show_type(struct device *dev,
37                                 struct device_attribute *attr,
38                                 char *buf)
39 {
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;
43
44         return sprintf(buf, "%c%d/%d\n", st->chip_info->sign,
45                        st->chip_info->bits, st->chip_info->bits);
46 }
47 static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad7606_show_type, NULL, 0);
48
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,
70         NULL,
71 };
72
73 static mode_t ad7606_scan_el_attr_is_visible(struct kobject *kobj,
74                                      struct attribute *attr, int n)
75 {
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;
80
81         mode_t mode = attr->mode;
82
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))
88                 mode = 0;
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))
94                 mode = 0;
95
96         return mode;
97 }
98
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,
103 };
104
105 int ad7606_scan_from_ring(struct ad7606_state *st, unsigned ch)
106 {
107         struct iio_ring_buffer *ring = st->indio_dev->ring;
108         int ret;
109         u16 *ring_data;
110
111         ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
112         if (ring_data == NULL) {
113                 ret = -ENOMEM;
114                 goto error_ret;
115         }
116         ret = ring->access.read_last(ring, (u8 *) ring_data);
117         if (ret)
118                 goto error_free_ring_data;
119
120         ret = ring_data[ch];
121
122 error_free_ring_data:
123         kfree(ring_data);
124 error_ret:
125         return ret;
126 }
127
128 /**
129  * ad7606_ring_preenable() setup the parameters of the ring before enabling
130  *
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
133  * byte boundary.
134  **/
135 static int ad7606_ring_preenable(struct iio_dev *indio_dev)
136 {
137         struct ad7606_state *st = indio_dev->dev_data;
138         struct iio_ring_buffer *ring = indio_dev->ring;
139         size_t d_size;
140
141         d_size = st->chip_info->num_channels *
142                  st->chip_info->bits / 8;
143
144         if (ring->scan_timestamp) {
145                 d_size += sizeof(s64);
146
147                 if (d_size % sizeof(s64))
148                         d_size += sizeof(s64) - (d_size % sizeof(s64));
149         }
150
151         if (ring->access.set_bytes_per_datum)
152                 ring->access.set_bytes_per_datum(ring, d_size);
153
154         st->d_size = d_size;
155
156         return 0;
157 }
158
159 /**
160  * ad7606_trigger_handler_th() th of trigger launched polling to ring buffer
161  *
162  **/
163 static irqreturn_t ad7606_trigger_handler_th(int irq, void *p)
164 {
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);
169
170         return IRQ_HANDLED;
171 }
172
173 /**
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
176  *
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.
181  **/
182 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
183 {
184         struct ad7606_state *st = container_of(work_s, struct ad7606_state,
185                                                 poll_work);
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;
189         s64 time_ns;
190         __u8 *buf;
191         int ret;
192
193         /* Ensure only one copy of this function running at a time */
194         if (atomic_inc_return(&st->protect_ring) > 1)
195                 return;
196
197         buf = kzalloc(st->d_size, GFP_KERNEL);
198         if (buf == NULL)
199                 return;
200
201         if (st->have_frstdata) {
202                 ret = st->bops->read_block(st->dev, 1, buf);
203                 if (ret)
204                         goto done;
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.
210                          */
211                         ad7606_reset(st);
212                         goto done;
213                 }
214                 ret = st->bops->read_block(st->dev,
215                         st->chip_info->num_channels - 1, buf + 2);
216                 if (ret)
217                         goto done;
218         } else {
219                 ret = st->bops->read_block(st->dev,
220                         st->chip_info->num_channels, buf);
221                 if (ret)
222                         goto done;
223         }
224
225         time_ns = iio_get_time_ns();
226
227         if (ring->scan_timestamp)
228                 memcpy(buf + st->d_size - sizeof(s64),
229                         &time_ns, sizeof(time_ns));
230
231         ring->access.store_to(&sw_ring->buf, buf, time_ns);
232 done:
233         gpio_set_value(st->pdata->gpio_convst, 0);
234         kfree(buf);
235         atomic_dec(&st->protect_ring);
236 }
237
238 int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
239 {
240         struct ad7606_state *st = indio_dev->dev_data;
241         int ret;
242
243         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
244         if (!indio_dev->ring) {
245                 ret = -ENOMEM;
246                 goto error_ret;
247         }
248
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) {
253                 ret = -ENOMEM;
254                 goto error_deallocate_sw_rb;
255         }
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,
260                           indio_dev->id);
261         if (indio_dev->pollfunc->name == NULL) {
262                 ret = -ENOMEM;
263                 goto error_free_poll_func;
264         }
265         /* Ring buffer functions - here trigger setup related */
266
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 ;
272
273         INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
274
275         /* Flag that polled ring buffering is possible */
276         indio_dev->modes |= INDIO_RING_TRIGGERED;
277         return 0;
278 error_free_poll_func:
279         kfree(indio_dev->pollfunc);
280 error_deallocate_sw_rb:
281         iio_sw_rb_free(indio_dev->ring);
282 error_ret:
283         return ret;
284 }
285
286 void ad7606_ring_cleanup(struct iio_dev *indio_dev)
287 {
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);
292         }
293         kfree(indio_dev->pollfunc->name);
294         kfree(indio_dev->pollfunc);
295         iio_sw_rb_free(indio_dev->ring);
296 }