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