staging:iio:various move default scan mask setting after ring register or remove
[pandora-kernel.git] / drivers / staging / iio / gyro / adis16260_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 "adis16260.h"
11
12 /**
13  * adis16260_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 adis16260_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 adis16260_state *st = iio_priv(indio_dev);
22         struct spi_transfer xfers[ADIS16260_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 <= ADIS16260_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                 if (i < 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */
38                         st->tx[2 * i]
39                                 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
40                                                 + 2 * i);
41                 else /* 0x06 to 0x09 is reserved */
42                         st->tx[2 * i]
43                                 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
44                                                 + 2 * i + 4);
45                 st->tx[2 * i + 1] = 0;
46                 if (i >= 1)
47                         xfers[i].rx_buf = rx + 2 * (i - 1);
48                 spi_message_add_tail(&xfers[i], &msg);
49         }
50
51         ret = spi_sync(st->us, &msg);
52         if (ret)
53                 dev_err(&st->us->dev, "problem when burst reading");
54
55         mutex_unlock(&st->buf_lock);
56
57         return ret;
58 }
59
60 static irqreturn_t adis16260_trigger_handler(int irq, void *p)
61 {
62         struct iio_poll_func *pf = p;
63         struct iio_dev *indio_dev = pf->indio_dev;
64         struct adis16260_state *st = iio_priv(indio_dev);
65         struct iio_ring_buffer *ring = indio_dev->ring;
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             adis16260_read_ring_data(&indio_dev->dev, st->rx) >= 0)
78                 for (; i < ring->scan_count; i++)
79                         data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
80
81         /* Guaranteed to be aligned with 8 byte boundary */
82         if (ring->scan_timestamp)
83                 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
84
85         ring->access->store_to(ring, (u8 *)data, pf->timestamp);
86
87         iio_trigger_notify_done(indio_dev->trig);
88         kfree(data);
89
90         return IRQ_HANDLED;
91 }
92
93 void adis16260_unconfigure_ring(struct iio_dev *indio_dev)
94 {
95         iio_dealloc_pollfunc(indio_dev->pollfunc);
96         iio_sw_rb_free(indio_dev->ring);
97 }
98
99 static const struct iio_ring_setup_ops adis16260_ring_setup_ops = {
100         .preenable = &iio_sw_ring_preenable,
101         .postenable = &iio_triggered_ring_postenable,
102         .predisable = &iio_triggered_ring_predisable,
103 };
104
105 int adis16260_configure_ring(struct iio_dev *indio_dev)
106 {
107         int ret = 0;
108         struct iio_ring_buffer *ring;
109
110         ring = iio_sw_rb_allocate(indio_dev);
111         if (!ring) {
112                 ret = -ENOMEM;
113                 return ret;
114         }
115         indio_dev->ring = ring;
116         /* Effectively select the ring buffer implementation */
117         ring->access = &ring_sw_access_funcs;
118         ring->bpe = 2;
119         ring->scan_timestamp = true;
120         ring->setup_ops = &adis16260_ring_setup_ops;
121         ring->owner = THIS_MODULE;
122
123         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
124                                                  &adis16260_trigger_handler,
125                                                  IRQF_ONESHOT,
126                                                  indio_dev,
127                                                  "adis16260_consumer%d",
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_RING_TRIGGERED;
135         return 0;
136
137 error_iio_sw_rb_free:
138         iio_sw_rb_free(indio_dev->ring);
139         return ret;
140 }