]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/ad7606_ring.c
staging:iio:adc:ad7606: Use private data space from iio_allocate_device
[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 int ad7606_scan_from_ring(struct ad7606_state *st, unsigned ch)
25 {
26         struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
27         int ret;
28         u16 *ring_data;
29
30         ring_data = kmalloc(ring->access.get_bytes_per_datum(ring),
31                             GFP_KERNEL);
32         if (ring_data == NULL) {
33                 ret = -ENOMEM;
34                 goto error_ret;
35         }
36         ret = ring->access.read_last(ring, (u8 *) ring_data);
37         if (ret)
38                 goto error_free_ring_data;
39
40         ret = ring_data[ch];
41
42 error_free_ring_data:
43         kfree(ring_data);
44 error_ret:
45         return ret;
46 }
47
48 /**
49  * ad7606_ring_preenable() setup the parameters of the ring before enabling
50  *
51  * The complex nature of the setting of the nuber of bytes per datum is due
52  * to this driver currently ensuring that the timestamp is stored at an 8
53  * byte boundary.
54  **/
55 static int ad7606_ring_preenable(struct iio_dev *indio_dev)
56 {
57         struct ad7606_state *st = indio_dev->dev_data;
58         struct iio_ring_buffer *ring = indio_dev->ring;
59         size_t d_size;
60
61         d_size = st->chip_info->num_channels *
62                  st->chip_info->channels[0].scan_type.storagebits / 8;
63
64         if (ring->scan_timestamp) {
65                 d_size += sizeof(s64);
66
67                 if (d_size % sizeof(s64))
68                         d_size += sizeof(s64) - (d_size % sizeof(s64));
69         }
70
71         if (ring->access.set_bytes_per_datum)
72                 ring->access.set_bytes_per_datum(ring, d_size);
73
74         st->d_size = d_size;
75
76         return 0;
77 }
78
79 /**
80  * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
81  *
82  **/
83 static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p)
84 {
85         struct iio_poll_func *pf = p;
86         struct iio_dev *indio_dev = pf->private_data;
87         struct ad7606_state *st = indio_dev->dev_data;
88
89         gpio_set_value(st->pdata->gpio_convst, 1);
90
91         return IRQ_HANDLED;
92 }
93
94 /**
95  * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
96  * @work_s:     the work struct through which this was scheduled
97  *
98  * Currently there is no option in this driver to disable the saving of
99  * timestamps within the ring.
100  * I think the one copy of this at a time was to avoid problems if the
101  * trigger was set far too high and the reads then locked up the computer.
102  **/
103 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
104 {
105         struct ad7606_state *st = container_of(work_s, struct ad7606_state,
106                                                 poll_work);
107         struct iio_dev *indio_dev = iio_priv_to_dev(st);
108         struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
109         struct iio_ring_buffer *ring = indio_dev->ring;
110         s64 time_ns;
111         __u8 *buf;
112         int ret;
113
114         buf = kzalloc(st->d_size, GFP_KERNEL);
115         if (buf == NULL)
116                 return;
117
118         if (st->have_frstdata) {
119                 ret = st->bops->read_block(st->dev, 1, buf);
120                 if (ret)
121                         goto done;
122                 if (!gpio_get_value(st->pdata->gpio_frstdata)) {
123                         /* This should never happen. However
124                          * some signal glitch caused by bad PCB desgin or
125                          * electrostatic discharge, could cause an extra read
126                          * or clock. This allows recovery.
127                          */
128                         ad7606_reset(st);
129                         goto done;
130                 }
131                 ret = st->bops->read_block(st->dev,
132                         st->chip_info->num_channels - 1, buf + 2);
133                 if (ret)
134                         goto done;
135         } else {
136                 ret = st->bops->read_block(st->dev,
137                         st->chip_info->num_channels, buf);
138                 if (ret)
139                         goto done;
140         }
141
142         time_ns = iio_get_time_ns();
143
144         if (ring->scan_timestamp)
145                 memcpy(buf + st->d_size - sizeof(s64),
146                         &time_ns, sizeof(time_ns));
147
148         ring->access.store_to(&sw_ring->buf, buf, time_ns);
149 done:
150         gpio_set_value(st->pdata->gpio_convst, 0);
151         iio_trigger_notify_done(indio_dev->trig);
152         kfree(buf);
153 }
154
155 int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
156 {
157         struct ad7606_state *st = indio_dev->dev_data;
158         int ret;
159
160         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
161         if (!indio_dev->ring) {
162                 ret = -ENOMEM;
163                 goto error_ret;
164         }
165
166         /* Effectively select the ring buffer implementation */
167         iio_ring_sw_register_funcs(&indio_dev->ring->access);
168         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
169         if (indio_dev->pollfunc == NULL) {
170                 ret = -ENOMEM;
171                 goto error_deallocate_sw_rb;
172         }
173
174         indio_dev->pollfunc->private_data = indio_dev;
175         indio_dev->pollfunc->h = &ad7606_trigger_handler_th_bh;
176         indio_dev->pollfunc->thread = &ad7606_trigger_handler_th_bh;
177         indio_dev->pollfunc->name =
178                 kasprintf(GFP_KERNEL, "%s_consumer%d", indio_dev->name,
179                           indio_dev->id);
180         if (indio_dev->pollfunc->name == NULL) {
181                 ret = -ENOMEM;
182                 goto error_free_poll_func;
183         }
184         /* Ring buffer functions - here trigger setup related */
185
186         indio_dev->ring->preenable = &ad7606_ring_preenable;
187         indio_dev->ring->postenable = &iio_triggered_ring_postenable;
188         indio_dev->ring->predisable = &iio_triggered_ring_predisable;
189         indio_dev->ring->scan_timestamp = true ;
190
191         INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
192
193         /* Flag that polled ring buffering is possible */
194         indio_dev->modes |= INDIO_RING_TRIGGERED;
195         return 0;
196 error_free_poll_func:
197         kfree(indio_dev->pollfunc);
198 error_deallocate_sw_rb:
199         iio_sw_rb_free(indio_dev->ring);
200 error_ret:
201         return ret;
202 }
203
204 void ad7606_ring_cleanup(struct iio_dev *indio_dev)
205 {
206         if (indio_dev->trig) {
207                 iio_put_trigger(indio_dev->trig);
208                 iio_trigger_dettach_poll_func(indio_dev->trig,
209                                               indio_dev->pollfunc);
210         }
211         kfree(indio_dev->pollfunc->name);
212         kfree(indio_dev->pollfunc);
213         iio_sw_rb_free(indio_dev->ring);
214 }