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