staging:iio:various move default scan mask setting after ring register or remove
[pandora-kernel.git] / drivers / staging / iio / accel / adis16209_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 "adis16209.h"
11
12 /**
13  * adis16209_read_ring_data() read data registers which will be placed into ring
14  * @dev: device associated with child of actual device (iio_dev or iio_trig)
15  * @rx: somewhere to pass back the value read
16  **/
17 static int adis16209_read_ring_data(struct device *dev, u8 *rx)
18 {
19         struct spi_message msg;
20         struct iio_dev *indio_dev = dev_get_drvdata(dev);
21         struct adis16209_state *st = iio_priv(indio_dev);
22         struct spi_transfer xfers[ADIS16209_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 <= ADIS16209_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 = 30;
36                 xfers[i].tx_buf = st->tx + 2 * i;
37                 st->tx[2 * i]
38                         = ADIS16209_READ_REG(ADIS16209_SUPPLY_OUT + 2 * i);
39                 st->tx[2 * i + 1] = 0;
40                 if (i >= 1)
41                         xfers[i].rx_buf = rx + 2 * (i - 1);
42                 spi_message_add_tail(&xfers[i], &msg);
43         }
44
45         ret = spi_sync(st->us, &msg);
46         if (ret)
47                 dev_err(&st->us->dev, "problem when burst reading");
48
49         mutex_unlock(&st->buf_lock);
50
51         return ret;
52 }
53
54 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
55  * specific to be rolled into the core.
56  */
57 static irqreturn_t adis16209_trigger_handler(int irq, void *p)
58 {
59         struct iio_poll_func *pf = p;
60         struct iio_dev *indio_dev = pf->indio_dev;
61         struct adis16209_state *st = iio_priv(indio_dev);
62         struct iio_ring_buffer *ring = indio_dev->ring;
63
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             adis16209_read_ring_data(&indio_dev->dev, st->rx) >= 0)
76                 for (; i < ring->scan_count; i++)
77                         data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
78
79         /* Guaranteed to be aligned with 8 byte boundary */
80         if (ring->scan_timestamp)
81                 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
82
83         ring->access->store_to(ring, (u8 *)data, pf->timestamp);
84
85         iio_trigger_notify_done(indio_dev->trig);
86         kfree(data);
87
88         return IRQ_HANDLED;
89 }
90
91 void adis16209_unconfigure_ring(struct iio_dev *indio_dev)
92 {
93         iio_dealloc_pollfunc(indio_dev->pollfunc);
94         iio_sw_rb_free(indio_dev->ring);
95 }
96
97 static const struct iio_ring_setup_ops adis16209_ring_setup_ops = {
98         .preenable = &iio_sw_ring_preenable,
99         .postenable = &iio_triggered_ring_postenable,
100         .predisable = &iio_triggered_ring_predisable,
101 };
102
103 int adis16209_configure_ring(struct iio_dev *indio_dev)
104 {
105         int ret = 0;
106         struct iio_ring_buffer *ring;
107
108         ring = iio_sw_rb_allocate(indio_dev);
109         if (!ring) {
110                 ret = -ENOMEM;
111                 return ret;
112         }
113         indio_dev->ring = ring;
114         /* Effectively select the ring buffer implementation */
115         ring->access = &ring_sw_access_funcs;
116         ring->bpe = 2;
117         ring->scan_timestamp = true;
118         ring->setup_ops = &adis16209_ring_setup_ops;
119         ring->owner = THIS_MODULE;
120
121         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
122                                                  &adis16209_trigger_handler,
123                                                  IRQF_ONESHOT,
124                                                  indio_dev,
125                                                  "%s_consumer%d",
126                                                  indio_dev->name,
127                                                  indio_dev->id);
128         if (indio_dev->pollfunc == NULL) {
129                 ret = -ENOMEM;
130                 goto error_iio_sw_rb_free;
131         }
132
133         indio_dev->modes |= INDIO_RING_TRIGGERED;
134         return 0;
135
136 error_iio_sw_rb_free:
137         iio_sw_rb_free(indio_dev->ring);
138         return ret;
139 }