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