]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/ad7887_ring.c
6b9cb1f95a1e8f31abc6fa8f4c8b977d3c325b4c
[karo-tx-linux.git] / drivers / staging / iio / adc / ad7887_ring.c
1 /*
2  * Copyright 2010 Analog Devices Inc.
3  * Copyright (C) 2008 Jonathan Cameron
4  *
5  * Licensed under the GPL-2 or later.
6  *
7  * ad7887_ring.c
8  */
9
10 #include <linux/interrupt.h>
11 #include <linux/gpio.h>
12 #include <linux/workqueue.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/list.h>
18 #include <linux/spi/spi.h>
19
20 #include "../iio.h"
21 #include "../ring_generic.h"
22 #include "../ring_sw.h"
23 #include "../trigger.h"
24 #include "../sysfs.h"
25
26 #include "ad7887.h"
27
28 static IIO_SCAN_EL_C(in0, 0, 0, NULL);
29 static IIO_SCAN_EL_C(in1, 1, 0, NULL);
30
31 static ssize_t ad7887_show_type(struct device *dev,
32                                 struct device_attribute *attr,
33                                 char *buf)
34 {
35         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
36         struct iio_dev *indio_dev = ring->indio_dev;
37         struct ad7887_state *st = indio_dev->dev_data;
38
39         return sprintf(buf, "%c%d/%d>>%d\n", st->chip_info->sign,
40                        st->chip_info->bits, st->chip_info->storagebits,
41                        st->chip_info->left_shift);
42 }
43 static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad7887_show_type, NULL, 0);
44
45 static struct attribute *ad7887_scan_el_attrs[] = {
46         &iio_scan_el_in0.dev_attr.attr,
47         &iio_const_attr_in0_index.dev_attr.attr,
48         &iio_scan_el_in1.dev_attr.attr,
49         &iio_const_attr_in1_index.dev_attr.attr,
50         &iio_dev_attr_in_type.dev_attr.attr,
51         NULL,
52 };
53
54 static mode_t ad7887_scan_el_attr_is_visible(struct kobject *kobj,
55                                      struct attribute *attr, int n)
56 {
57         struct device *dev = container_of(kobj, struct device, kobj);
58         struct iio_ring_buffer *ring = dev_get_drvdata(dev);
59         struct iio_dev *indio_dev = ring->indio_dev;
60         struct ad7887_state *st = indio_dev->dev_data;
61
62         mode_t mode = attr->mode;
63
64         if ((attr == &iio_scan_el_in1.dev_attr.attr) ||
65                 (attr == &iio_const_attr_in1_index.dev_attr.attr))
66                 if (!st->en_dual)
67                         mode = 0;
68
69         return mode;
70 }
71
72 static struct attribute_group ad7887_scan_el_group = {
73         .name = "scan_elements",
74         .attrs = ad7887_scan_el_attrs,
75         .is_visible = ad7887_scan_el_attr_is_visible,
76 };
77
78 int ad7887_scan_from_ring(struct ad7887_state *st, long mask)
79 {
80         struct iio_ring_buffer *ring = st->indio_dev->ring;
81         int count = 0, ret;
82         u16 *ring_data;
83
84         if (!(ring->scan_mask & mask)) {
85                 ret = -EBUSY;
86                 goto error_ret;
87         }
88
89         ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
90         if (ring_data == NULL) {
91                 ret = -ENOMEM;
92                 goto error_ret;
93         }
94         ret = ring->access.read_last(ring, (u8 *) ring_data);
95         if (ret)
96                 goto error_free_ring_data;
97
98         /* for single channel scan the result is stored with zero offset */
99         if ((ring->scan_mask == ((1 << 1) | (1 << 0))) && (mask == (1 << 1)))
100                 count = 1;
101
102         ret = be16_to_cpu(ring_data[count]);
103
104 error_free_ring_data:
105         kfree(ring_data);
106 error_ret:
107         return ret;
108 }
109
110 /**
111  * ad7887_ring_preenable() setup the parameters of the ring before enabling
112  *
113  * The complex nature of the setting of the nuber of bytes per datum is due
114  * to this driver currently ensuring that the timestamp is stored at an 8
115  * byte boundary.
116  **/
117 static int ad7887_ring_preenable(struct iio_dev *indio_dev)
118 {
119         struct ad7887_state *st = indio_dev->dev_data;
120         struct iio_ring_buffer *ring = indio_dev->ring;
121         size_t d_size;
122
123         if (indio_dev->ring->access.set_bytes_per_datum) {
124                 d_size = st->chip_info->storagebits / 8 + sizeof(s64);
125                 if (d_size % 8)
126                         d_size += 8 - (d_size % 8);
127                 indio_dev->ring->access.set_bytes_per_datum(indio_dev->ring,
128                                                             d_size);
129         }
130
131         switch (ring->scan_mask) {
132         case (1 << 0):
133                 st->ring_msg = &st->msg[AD7887_CH0];
134                 break;
135         case (1 << 1):
136                 st->ring_msg = &st->msg[AD7887_CH1];
137                 /* Dummy read: push CH1 setting down to hardware */
138                 spi_sync(st->spi, st->ring_msg);
139                 break;
140         case ((1 << 1) | (1 << 0)):
141                 st->ring_msg = &st->msg[AD7887_CH0_CH1];
142                 break;
143         }
144
145         return 0;
146 }
147
148 static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
149 {
150         struct ad7887_state *st = indio_dev->dev_data;
151
152         /* dummy read: restore default CH0 settin */
153         return spi_sync(st->spi, &st->msg[AD7887_CH0]);
154 }
155
156 /**
157  * ad7887_poll_func_th() th of trigger launched polling to ring buffer
158  *
159  * As sampling only occurs on spi comms occuring, leave timestamping until
160  * then.  Some triggers will generate their own time stamp.  Currently
161  * there is no way of notifying them when no one cares.
162  **/
163 static void ad7887_poll_func_th(struct iio_dev *indio_dev, s64 time)
164 {
165         struct ad7887_state *st = indio_dev->dev_data;
166
167         schedule_work(&st->poll_work);
168         return;
169 }
170 /**
171  * ad7887_poll_bh_to_ring() bh of trigger launched polling to ring buffer
172  * @work_s:     the work struct through which this was scheduled
173  *
174  * Currently there is no option in this driver to disable the saving of
175  * timestamps within the ring.
176  * I think the one copy of this at a time was to avoid problems if the
177  * trigger was set far too high and the reads then locked up the computer.
178  **/
179 static void ad7887_poll_bh_to_ring(struct work_struct *work_s)
180 {
181         struct ad7887_state *st = container_of(work_s, struct ad7887_state,
182                                                   poll_work);
183         struct iio_dev *indio_dev = st->indio_dev;
184         struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
185         struct iio_ring_buffer *ring = indio_dev->ring;
186         s64 time_ns;
187         __u8 *buf;
188         int b_sent;
189         size_t d_size;
190
191         unsigned int bytes = ring->scan_count * st->chip_info->storagebits / 8;
192
193         /* Ensure the timestamp is 8 byte aligned */
194         d_size = bytes + sizeof(s64);
195         if (d_size % sizeof(s64))
196                 d_size += sizeof(s64) - (d_size % sizeof(s64));
197
198         /* Ensure only one copy of this function running at a time */
199         if (atomic_inc_return(&st->protect_ring) > 1)
200                 return;
201
202         buf = kzalloc(d_size, GFP_KERNEL);
203         if (buf == NULL)
204                 return;
205
206         b_sent = spi_sync(st->spi, st->ring_msg);
207         if (b_sent)
208                 goto done;
209
210         time_ns = iio_get_time_ns();
211
212         memcpy(buf, st->data, bytes);
213         memcpy(buf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
214
215         indio_dev->ring->access.store_to(&sw_ring->buf, buf, time_ns);
216 done:
217         kfree(buf);
218         atomic_dec(&st->protect_ring);
219 }
220
221 int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
222 {
223         struct ad7887_state *st = indio_dev->dev_data;
224         int ret;
225
226         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
227         if (!indio_dev->ring) {
228                 ret = -ENOMEM;
229                 goto error_ret;
230         }
231         /* Effectively select the ring buffer implementation */
232         iio_ring_sw_register_funcs(&indio_dev->ring->access);
233         ret = iio_alloc_pollfunc(indio_dev, NULL, &ad7887_poll_func_th);
234         if (ret)
235                 goto error_deallocate_sw_rb;
236
237         /* Ring buffer functions - here trigger setup related */
238
239         indio_dev->ring->preenable = &ad7887_ring_preenable;
240         indio_dev->ring->postenable = &iio_triggered_ring_postenable;
241         indio_dev->ring->predisable = &iio_triggered_ring_predisable;
242         indio_dev->ring->postdisable = &ad7887_ring_postdisable;
243         indio_dev->ring->scan_el_attrs = &ad7887_scan_el_group;
244
245         INIT_WORK(&st->poll_work, &ad7887_poll_bh_to_ring);
246
247         /* Flag that polled ring buffering is possible */
248         indio_dev->modes |= INDIO_RING_TRIGGERED;
249         return 0;
250 error_deallocate_sw_rb:
251         iio_sw_rb_free(indio_dev->ring);
252 error_ret:
253         return ret;
254 }
255
256 void ad7887_ring_cleanup(struct iio_dev *indio_dev)
257 {
258         /* ensure that the trigger has been detached */
259         if (indio_dev->trig) {
260                 iio_put_trigger(indio_dev->trig);
261                 iio_trigger_dettach_poll_func(indio_dev->trig,
262                                               indio_dev->pollfunc);
263         }
264         kfree(indio_dev->pollfunc);
265         iio_sw_rb_free(indio_dev->ring);
266 }