staging:iio: treewide rename iio_triggered_ring_* to iio_triggered_buffer_*
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / adc / ad7887_ring.c
blob159aeb17eed231223b06b14b339fce20c6d6a2ac
1 /*
2 * Copyright 2010-2011 Analog Devices Inc.
3 * Copyright (C) 2008 Jonathan Cameron
5 * Licensed under the GPL-2.
7 * ad7887_ring.c
8 */
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
15 #include "../iio.h"
16 #include "../ring_generic.h"
17 #include "../ring_sw.h"
18 #include "../trigger_consumer.h"
20 #include "ad7887.h"
22 int ad7887_scan_from_ring(struct ad7887_state *st, int channum)
24 struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
25 int count = 0, ret;
26 u16 *ring_data;
28 if (!(test_bit(channum, ring->scan_mask))) {
29 ret = -EBUSY;
30 goto error_ret;
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;
39 ret = ring->access->read_last(ring, (u8 *) ring_data);
40 if (ret)
41 goto error_free_ring_data;
43 /* for single channel scan the result is stored with zero offset */
44 if ((test_bit(1, ring->scan_mask) || test_bit(0, ring->scan_mask)) &&
45 (channum == 1))
46 count = 1;
48 ret = be16_to_cpu(ring_data[count]);
50 error_free_ring_data:
51 kfree(ring_data);
52 error_ret:
53 return ret;
56 /**
57 * ad7887_ring_preenable() setup the parameters of the ring before enabling
59 * The complex nature of the setting of the nuber of bytes per datum is due
60 * to this driver currently ensuring that the timestamp is stored at an 8
61 * byte boundary.
62 **/
63 static int ad7887_ring_preenable(struct iio_dev *indio_dev)
65 struct ad7887_state *st = iio_priv(indio_dev);
66 struct iio_ring_buffer *ring = indio_dev->ring;
68 st->d_size = ring->scan_count *
69 st->chip_info->channel[0].scan_type.storagebits / 8;
71 if (ring->scan_timestamp) {
72 st->d_size += sizeof(s64);
74 if (st->d_size % sizeof(s64))
75 st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
78 if (indio_dev->ring->access->set_bytes_per_datum)
79 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
80 st->d_size);
82 /* We know this is a single long so can 'cheat' */
83 switch (*ring->scan_mask) {
84 case (1 << 0):
85 st->ring_msg = &st->msg[AD7887_CH0];
86 break;
87 case (1 << 1):
88 st->ring_msg = &st->msg[AD7887_CH1];
89 /* Dummy read: push CH1 setting down to hardware */
90 spi_sync(st->spi, st->ring_msg);
91 break;
92 case ((1 << 1) | (1 << 0)):
93 st->ring_msg = &st->msg[AD7887_CH0_CH1];
94 break;
97 return 0;
100 static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
102 struct ad7887_state *st = iio_priv(indio_dev);
104 /* dummy read: restore default CH0 settin */
105 return spi_sync(st->spi, &st->msg[AD7887_CH0]);
109 * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
111 * Currently there is no option in this driver to disable the saving of
112 * timestamps within the ring.
114 static irqreturn_t ad7887_trigger_handler(int irq, void *p)
116 struct iio_poll_func *pf = p;
117 struct iio_dev *indio_dev = pf->indio_dev;
118 struct ad7887_state *st = iio_priv(indio_dev);
119 struct iio_ring_buffer *ring = indio_dev->ring;
120 s64 time_ns;
121 __u8 *buf;
122 int b_sent;
124 unsigned int bytes = ring->scan_count *
125 st->chip_info->channel[0].scan_type.storagebits / 8;
127 buf = kzalloc(st->d_size, GFP_KERNEL);
128 if (buf == NULL)
129 return -ENOMEM;
131 b_sent = spi_sync(st->spi, st->ring_msg);
132 if (b_sent)
133 goto done;
135 time_ns = iio_get_time_ns();
137 memcpy(buf, st->data, bytes);
138 if (ring->scan_timestamp)
139 memcpy(buf + st->d_size - sizeof(s64),
140 &time_ns, sizeof(time_ns));
142 indio_dev->ring->access->store_to(indio_dev->ring, buf, time_ns);
143 done:
144 kfree(buf);
145 iio_trigger_notify_done(indio_dev->trig);
147 return IRQ_HANDLED;
150 static const struct iio_ring_setup_ops ad7887_ring_setup_ops = {
151 .preenable = &ad7887_ring_preenable,
152 .postenable = &iio_triggered_buffer_postenable,
153 .predisable = &iio_triggered_buffer_predisable,
154 .postdisable = &ad7887_ring_postdisable,
157 int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
159 int ret;
161 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
162 if (!indio_dev->ring) {
163 ret = -ENOMEM;
164 goto error_ret;
166 /* Effectively select the ring buffer implementation */
167 indio_dev->ring->access = &ring_sw_access_funcs;
168 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
169 &ad7887_trigger_handler,
170 IRQF_ONESHOT,
171 indio_dev,
172 "ad7887_consumer%d",
173 indio_dev->id);
174 if (indio_dev->pollfunc == NULL) {
175 ret = -ENOMEM;
176 goto error_deallocate_sw_rb;
178 /* Ring buffer functions - here trigger setup related */
179 indio_dev->ring->setup_ops = &ad7887_ring_setup_ops;
181 /* Flag that polled ring buffering is possible */
182 indio_dev->modes |= INDIO_RING_TRIGGERED;
183 return 0;
185 error_deallocate_sw_rb:
186 iio_sw_rb_free(indio_dev->ring);
187 error_ret:
188 return ret;
191 void ad7887_ring_cleanup(struct iio_dev *indio_dev)
193 iio_dealloc_pollfunc(indio_dev->pollfunc);
194 iio_sw_rb_free(indio_dev->ring);