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