]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/adc/ad7887_ring.c
staging:iio:pollfunc: Make explicit that private data is always pointer to a struct...
[~andy/linux] / drivers / staging / iio / adc / ad7887_ring.c
1 /*
2  * Copyright 2010-2011 Analog Devices Inc.
3  * Copyright (C) 2008 Jonathan Cameron
4  *
5  * Licensed under the GPL-2.
6  *
7  * ad7887_ring.c
8  */
9
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14
15 #include "../iio.h"
16 #include "../ring_generic.h"
17 #include "../ring_sw.h"
18 #include "../trigger.h"
19
20 #include "ad7887.h"
21
22 int ad7887_scan_from_ring(struct ad7887_state *st, long mask)
23 {
24         struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
25         int count = 0, ret;
26         u16 *ring_data;
27
28         if (!(ring->scan_mask & mask)) {
29                 ret = -EBUSY;
30                 goto error_ret;
31         }
32
33         ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
34                             GFP_KERNEL);
35         if (ring_data == NULL) {
36                 ret = -ENOMEM;
37                 goto error_ret;
38         }
39         ret = ring->access->read_last(ring, (u8 *) ring_data);
40         if (ret)
41                 goto error_free_ring_data;
42
43         /* for single channel scan the result is stored with zero offset */
44         if ((ring->scan_mask == ((1 << 1) | (1 << 0))) && (mask == (1 << 1)))
45                 count = 1;
46
47         ret = be16_to_cpu(ring_data[count]);
48
49 error_free_ring_data:
50         kfree(ring_data);
51 error_ret:
52         return ret;
53 }
54
55 /**
56  * ad7887_ring_preenable() setup the parameters of the ring before enabling
57  *
58  * The complex nature of the setting of the nuber of bytes per datum is due
59  * to this driver currently ensuring that the timestamp is stored at an 8
60  * byte boundary.
61  **/
62 static int ad7887_ring_preenable(struct iio_dev *indio_dev)
63 {
64         struct ad7887_state *st = iio_priv(indio_dev);
65         struct iio_ring_buffer *ring = indio_dev->ring;
66
67         st->d_size = ring->scan_count *
68                 st->chip_info->channel[0].scan_type.storagebits / 8;
69
70         if (ring->scan_timestamp) {
71                 st->d_size += sizeof(s64);
72
73                 if (st->d_size % sizeof(s64))
74                         st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
75         }
76
77         if (indio_dev->ring->access->set_bytes_per_datum)
78                 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
79                                                             st->d_size);
80
81         switch (ring->scan_mask) {
82         case (1 << 0):
83                 st->ring_msg = &st->msg[AD7887_CH0];
84                 break;
85         case (1 << 1):
86                 st->ring_msg = &st->msg[AD7887_CH1];
87                 /* Dummy read: push CH1 setting down to hardware */
88                 spi_sync(st->spi, st->ring_msg);
89                 break;
90         case ((1 << 1) | (1 << 0)):
91                 st->ring_msg = &st->msg[AD7887_CH0_CH1];
92                 break;
93         }
94
95         return 0;
96 }
97
98 static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
99 {
100         struct ad7887_state *st = iio_priv(indio_dev);
101
102         /* dummy read: restore default CH0 settin */
103         return spi_sync(st->spi, &st->msg[AD7887_CH0]);
104 }
105
106 /**
107  * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
108  *
109  * Currently there is no option in this driver to disable the saving of
110  * timestamps within the ring.
111  **/
112 static irqreturn_t ad7887_trigger_handler(int irq, void *p)
113 {
114         struct iio_poll_func *pf = p;
115         struct iio_dev *indio_dev = pf->indio_dev;
116         struct ad7887_state *st = iio_priv(indio_dev);
117         struct iio_ring_buffer *ring = indio_dev->ring;
118         s64 time_ns;
119         __u8 *buf;
120         int b_sent;
121
122         unsigned int bytes = ring->scan_count *
123                 st->chip_info->channel[0].scan_type.storagebits / 8;
124
125         buf = kzalloc(st->d_size, GFP_KERNEL);
126         if (buf == NULL)
127                 return -ENOMEM;
128
129         b_sent = spi_sync(st->spi, st->ring_msg);
130         if (b_sent)
131                 goto done;
132
133         time_ns = iio_get_time_ns();
134
135         memcpy(buf, st->data, bytes);
136         if (ring->scan_timestamp)
137                 memcpy(buf + st->d_size - sizeof(s64),
138                        &time_ns, sizeof(time_ns));
139
140         indio_dev->ring->access->store_to(indio_dev->ring, buf, time_ns);
141 done:
142         kfree(buf);
143         iio_trigger_notify_done(indio_dev->trig);
144
145         return IRQ_HANDLED;
146 }
147
148 static const struct iio_ring_setup_ops ad7887_ring_setup_ops = {
149         .preenable = &ad7887_ring_preenable,
150         .postenable = &iio_triggered_ring_postenable,
151         .predisable = &iio_triggered_ring_predisable,
152         .postdisable = &ad7887_ring_postdisable,
153 };
154
155 int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
156 {
157         int ret;
158
159         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
160         if (!indio_dev->ring) {
161                 ret = -ENOMEM;
162                 goto error_ret;
163         }
164         /* Effectively select the ring buffer implementation */
165         indio_dev->ring->access = &ring_sw_access_funcs;
166         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
167                                                  &ad7887_trigger_handler,
168                                                  IRQF_ONESHOT,
169                                                  indio_dev,
170                                                  "ad7887_consumer%d",
171                                                  indio_dev->id);
172         if (indio_dev->pollfunc == NULL) {
173                 ret = -ENOMEM;
174                 goto error_deallocate_sw_rb;
175         }
176         /* Ring buffer functions - here trigger setup related */
177         indio_dev->ring->setup_ops = &ad7887_ring_setup_ops;
178
179         /* Flag that polled ring buffering is possible */
180         indio_dev->modes |= INDIO_RING_TRIGGERED;
181         return 0;
182
183 error_deallocate_sw_rb:
184         iio_sw_rb_free(indio_dev->ring);
185 error_ret:
186         return ret;
187 }
188
189 void ad7887_ring_cleanup(struct iio_dev *indio_dev)
190 {
191         /* ensure that the trigger has been detached */
192         if (indio_dev->trig) {
193                 iio_put_trigger(indio_dev->trig);
194                 iio_trigger_dettach_poll_func(indio_dev->trig,
195                                               indio_dev->pollfunc);
196         }
197         iio_dealloc_pollfunc(indio_dev->pollfunc);
198         iio_sw_rb_free(indio_dev->ring);
199 }