staging:iio: treewide rename iio_triggered_ring_* to iio_triggered_buffer_*
[pandora-kernel.git] / drivers / staging / iio / accel / adis16201_ring.c
1 #include <linux/interrupt.h>
2 #include <linux/mutex.h>
3 #include <linux/kernel.h>
4 #include <linux/spi/spi.h>
5 #include <linux/slab.h>
6
7 #include "../iio.h"
8 #include "../ring_sw.h"
9 #include "../trigger_consumer.h"
10 #include "adis16201.h"
11
12
13 /**
14  * adis16201_read_ring_data() read data registers which will be placed into ring
15  * @dev: device associated with child of actual device (iio_dev or iio_trig)
16  * @rx: somewhere to pass back the value read
17  **/
18 static int adis16201_read_ring_data(struct iio_dev *indio_dev, u8 *rx)
19 {
20         struct spi_message msg;
21         struct adis16201_state *st = iio_priv(indio_dev);
22         struct spi_transfer xfers[ADIS16201_OUTPUTS + 1];
23         int ret;
24         int i;
25
26         mutex_lock(&st->buf_lock);
27
28         spi_message_init(&msg);
29
30         memset(xfers, 0, sizeof(xfers));
31         for (i = 0; i <= ADIS16201_OUTPUTS; i++) {
32                 xfers[i].bits_per_word = 8;
33                 xfers[i].cs_change = 1;
34                 xfers[i].len = 2;
35                 xfers[i].delay_usecs = 20;
36                 if (i < ADIS16201_OUTPUTS) {
37                         xfers[i].tx_buf = st->tx + 2 * i;
38                         st->tx[2 * i] = ADIS16201_READ_REG(ADIS16201_SUPPLY_OUT +
39                                                            2 * i);
40                         st->tx[2 * i + 1] = 0;
41                 }
42                 if (i >= 1)
43                         xfers[i].rx_buf = rx + 2 * (i - 1);
44                 spi_message_add_tail(&xfers[i], &msg);
45         }
46
47         ret = spi_sync(st->us, &msg);
48         if (ret)
49                 dev_err(&st->us->dev, "problem when burst reading");
50
51         mutex_unlock(&st->buf_lock);
52
53         return ret;
54 }
55
56 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
57  * specific to be rolled into the core.
58  */
59 static irqreturn_t adis16201_trigger_handler(int irq, void *p)
60 {
61         struct iio_poll_func *pf = p;
62         struct iio_dev *indio_dev = pf->indio_dev;
63         struct adis16201_state *st = iio_priv(indio_dev);
64         struct iio_ring_buffer *ring = indio_dev->ring;
65
66         int i = 0;
67         s16 *data;
68         size_t datasize = ring->access->get_bytes_per_datum(ring);
69
70         data = kmalloc(datasize, GFP_KERNEL);
71         if (data == NULL) {
72                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
73                 return -ENOMEM;
74         }
75
76         if (ring->scan_count)
77                 if (adis16201_read_ring_data(indio_dev, st->rx) >= 0)
78                         for (; i < ring->scan_count; i++)
79                                 data[i] = be16_to_cpup(
80                                         (__be16 *)&(st->rx[i*2]));
81
82         /* Guaranteed to be aligned with 8 byte boundary */
83         if (ring->scan_timestamp)
84                 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
85
86         ring->access->store_to(ring, (u8 *)data, pf->timestamp);
87
88         iio_trigger_notify_done(indio_dev->trig);
89         kfree(data);
90
91         return IRQ_HANDLED;
92 }
93
94 void adis16201_unconfigure_ring(struct iio_dev *indio_dev)
95 {
96         iio_dealloc_pollfunc(indio_dev->pollfunc);
97         iio_sw_rb_free(indio_dev->ring);
98 }
99
100 static const struct iio_ring_setup_ops adis16201_ring_setup_ops = {
101         .preenable = &iio_sw_ring_preenable,
102         .postenable = &iio_triggered_buffer_postenable,
103         .predisable = &iio_triggered_buffer_predisable,
104 };
105
106 int adis16201_configure_ring(struct iio_dev *indio_dev)
107 {
108         int ret = 0;
109         struct iio_ring_buffer *ring;
110
111         ring = iio_sw_rb_allocate(indio_dev);
112         if (!ring) {
113                 ret = -ENOMEM;
114                 return ret;
115         }
116         indio_dev->ring = ring;
117         /* Effectively select the ring buffer implementation */
118         ring->bpe = 2;
119         ring->scan_timestamp = true;
120         ring->access = &ring_sw_access_funcs;
121         ring->setup_ops = &adis16201_ring_setup_ops;
122         ring->owner = THIS_MODULE;
123
124         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
125                                                  &adis16201_trigger_handler,
126                                                  IRQF_ONESHOT,
127                                                  indio_dev,
128                                                  "adis16201_consumer%d",
129                                                  indio_dev->id);
130         if (indio_dev->pollfunc == NULL) {
131                 ret = -ENOMEM;
132                 goto error_iio_sw_rb_free;
133         }
134
135         indio_dev->modes |= INDIO_RING_TRIGGERED;
136         return 0;
137 error_iio_sw_rb_free:
138         iio_sw_rb_free(indio_dev->ring);
139         return ret;
140 }