staging:iio: treewide rename iio_triggered_ring_* to iio_triggered_buffer_*
[pandora-kernel.git] / drivers / staging / iio / adc / ad7298_ring.c
1 /*
2  * AD7298 SPI ADC driver
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/spi/spi.h>
13
14 #include "../iio.h"
15 #include "../ring_generic.h"
16 #include "../ring_sw.h"
17 #include "../trigger_consumer.h"
18
19 #include "ad7298.h"
20
21 int ad7298_scan_from_ring(struct iio_dev *dev_info, long ch)
22 {
23         struct iio_ring_buffer *ring = dev_info->ring;
24         int ret;
25         u16 *ring_data;
26
27         if (!(test_bit(ch, ring->scan_mask))) {
28                 ret = -EBUSY;
29                 goto error_ret;
30         }
31
32         ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
33                             GFP_KERNEL);
34         if (ring_data == NULL) {
35                 ret = -ENOMEM;
36                 goto error_ret;
37         }
38         ret = ring->access->read_last(ring, (u8 *) ring_data);
39         if (ret)
40                 goto error_free_ring_data;
41
42         ret = be16_to_cpu(ring_data[ch]);
43
44 error_free_ring_data:
45         kfree(ring_data);
46 error_ret:
47         return ret;
48 }
49
50 /**
51  * ad7298_ring_preenable() setup the parameters of the ring before enabling
52  *
53  * The complex nature of the setting of the number of bytes per datum is due
54  * to this driver currently ensuring that the timestamp is stored at an 8
55  * byte boundary.
56  **/
57 static int ad7298_ring_preenable(struct iio_dev *indio_dev)
58 {
59         struct ad7298_state *st = iio_priv(indio_dev);
60         struct iio_ring_buffer *ring = indio_dev->ring;
61         size_t d_size;
62         int i, m;
63         unsigned short command;
64
65         d_size = ring->scan_count * (AD7298_STORAGE_BITS / 8);
66
67         if (ring->scan_timestamp) {
68                 d_size += sizeof(s64);
69
70                 if (d_size % sizeof(s64))
71                         d_size += sizeof(s64) - (d_size % sizeof(s64));
72         }
73
74         if (ring->access->set_bytes_per_datum)
75                 ring->access->set_bytes_per_datum(ring, d_size);
76
77         st->d_size = d_size;
78
79         command = AD7298_WRITE | st->ext_ref;
80
81         for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
82                 if (test_bit(i, ring->scan_mask))
83                         command |= m;
84
85         st->tx_buf[0] = cpu_to_be16(command);
86
87         /* build spi ring message */
88         st->ring_xfer[0].tx_buf = &st->tx_buf[0];
89         st->ring_xfer[0].len = 2;
90         st->ring_xfer[0].cs_change = 1;
91         st->ring_xfer[1].tx_buf = &st->tx_buf[1];
92         st->ring_xfer[1].len = 2;
93         st->ring_xfer[1].cs_change = 1;
94
95         spi_message_init(&st->ring_msg);
96         spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
97         spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
98
99         for (i = 0; i < ring->scan_count; i++) {
100                 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
101                 st->ring_xfer[i + 2].len = 2;
102                 st->ring_xfer[i + 2].cs_change = 1;
103                 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
104         }
105         /* make sure last transfer cs_change is not set */
106         st->ring_xfer[i + 1].cs_change = 0;
107
108         return 0;
109 }
110
111 /**
112  * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
113  *
114  * Currently there is no option in this driver to disable the saving of
115  * timestamps within the ring.
116  **/
117 static irqreturn_t ad7298_trigger_handler(int irq, void *p)
118 {
119         struct iio_poll_func *pf = p;
120         struct iio_dev *indio_dev = pf->indio_dev;
121         struct ad7298_state *st = iio_priv(indio_dev);
122         struct iio_ring_buffer *ring = indio_dev->ring;
123         s64 time_ns;
124         __u16 buf[16];
125         int b_sent, i;
126
127         b_sent = spi_sync(st->spi, &st->ring_msg);
128         if (b_sent)
129                 return b_sent;
130
131         if (ring->scan_timestamp) {
132                 time_ns = iio_get_time_ns();
133                 memcpy((u8 *)buf + st->d_size - sizeof(s64),
134                         &time_ns, sizeof(time_ns));
135         }
136
137         for (i = 0; i < ring->scan_count; i++)
138                 buf[i] = be16_to_cpu(st->rx_buf[i]);
139
140         indio_dev->ring->access->store_to(ring, (u8 *)buf, time_ns);
141         iio_trigger_notify_done(indio_dev->trig);
142
143         return IRQ_HANDLED;
144 }
145
146 static const struct iio_ring_setup_ops ad7298_ring_setup_ops = {
147         .preenable = &ad7298_ring_preenable,
148         .postenable = &iio_triggered_buffer_postenable,
149         .predisable = &iio_triggered_buffer_predisable,
150 };
151
152 int ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev)
153 {
154         int ret;
155
156         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
157         if (!indio_dev->ring) {
158                 ret = -ENOMEM;
159                 goto error_ret;
160         }
161         /* Effectively select the ring buffer implementation */
162         indio_dev->ring->access = &ring_sw_access_funcs;
163
164         indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
165                                                  &ad7298_trigger_handler,
166                                                  IRQF_ONESHOT,
167                                                  indio_dev,
168                                                  "ad7298_consumer%d",
169                                                  indio_dev->id);
170
171         if (indio_dev->pollfunc == NULL) {
172                 ret = -ENOMEM;
173                 goto error_deallocate_sw_rb;
174         }
175
176         /* Ring buffer functions - here trigger setup related */
177         indio_dev->ring->setup_ops = &ad7298_ring_setup_ops;
178         indio_dev->ring->scan_timestamp = true;
179
180         /* Flag that polled ring buffering is possible */
181         indio_dev->modes |= INDIO_RING_TRIGGERED;
182         return 0;
183
184 error_deallocate_sw_rb:
185         iio_sw_rb_free(indio_dev->ring);
186 error_ret:
187         return ret;
188 }
189
190 void ad7298_ring_cleanup(struct iio_dev *indio_dev)
191 {
192         iio_dealloc_pollfunc(indio_dev->pollfunc);
193         iio_sw_rb_free(indio_dev->ring);
194 }