]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/iio/imu/adis16400_ring.c
3fc24037db29f35129fdec3f7f02548e224d92fe
[karo-tx-linux.git] / drivers / staging / iio / imu / adis16400_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 #include <linux/bitops.h>
7
8 #include "../iio.h"
9 #include "../ring_sw.h"
10 #include "../trigger_consumer.h"
11 #include "adis16400.h"
12
13 /**
14  * adis16400_spi_read_burst() - read all data registers
15  * @dev: device associated with child of actual device (iio_dev or iio_trig)
16  * @rx: somewhere to pass back the value read (min size is 24 bytes)
17  **/
18 static int adis16400_spi_read_burst(struct device *dev, u8 *rx)
19 {
20         struct spi_message msg;
21         struct iio_dev *indio_dev = dev_get_drvdata(dev);
22         struct adis16400_state *st = iio_priv(indio_dev);
23         u32 old_speed_hz = st->us->max_speed_hz;
24         int ret;
25
26         struct spi_transfer xfers[] = {
27                 {
28                         .tx_buf = st->tx,
29                         .bits_per_word = 8,
30                         .len = 2,
31                 }, {
32                         .rx_buf = rx,
33                         .bits_per_word = 8,
34                         .len = 24,
35                 },
36         };
37
38         mutex_lock(&st->buf_lock);
39         st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD);
40         st->tx[1] = 0;
41
42         spi_message_init(&msg);
43         spi_message_add_tail(&xfers[0], &msg);
44         spi_message_add_tail(&xfers[1], &msg);
45
46         st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz);
47         spi_setup(st->us);
48
49         ret = spi_sync(st->us, &msg);
50         if (ret)
51                 dev_err(&st->us->dev, "problem when burst reading");
52
53         st->us->max_speed_hz = old_speed_hz;
54         spi_setup(st->us);
55         mutex_unlock(&st->buf_lock);
56         return ret;
57 }
58
59 static const u16 read_all_tx_array[] = {
60         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_SUPPLY_OUT)),
61         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_XGYRO_OUT)),
62         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_YGYRO_OUT)),
63         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_ZGYRO_OUT)),
64         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_XACCL_OUT)),
65         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_YACCL_OUT)),
66         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_ZACCL_OUT)),
67         cpu_to_be16(ADIS16400_READ_REG(ADIS16350_XTEMP_OUT)),
68         cpu_to_be16(ADIS16400_READ_REG(ADIS16350_YTEMP_OUT)),
69         cpu_to_be16(ADIS16400_READ_REG(ADIS16350_ZTEMP_OUT)),
70         cpu_to_be16(ADIS16400_READ_REG(ADIS16400_AUX_ADC)),
71 };
72
73 static int adis16350_spi_read_all(struct device *dev, u8 *rx)
74 {
75         struct iio_dev *indio_dev = dev_get_drvdata(dev);
76         struct adis16400_state *st = iio_priv(indio_dev);
77
78         struct spi_message msg;
79         int i, j = 0, ret;
80         struct spi_transfer *xfers;
81
82         xfers = kzalloc(sizeof(*xfers)*indio_dev->ring->scan_count + 1,
83                         GFP_KERNEL);
84         if (xfers == NULL)
85                 return -ENOMEM;
86
87         for (i = 0; i < ARRAY_SIZE(read_all_tx_array); i++)
88                 if (indio_dev->ring->scan_mask & (1 << i)) {
89                         xfers[j].tx_buf = &read_all_tx_array[i];
90                         xfers[j].bits_per_word = 16;
91                         xfers[j].len = 2;
92                         xfers[j + 1].rx_buf = rx + j*2;
93                         j++;
94                 }
95         xfers[j].bits_per_word = 16;
96         xfers[j].len = 2;
97
98         spi_message_init(&msg);
99         for (j = 0; j < indio_dev->ring->scan_count + 1; j++)
100                 spi_message_add_tail(&xfers[j], &msg);
101
102         ret = spi_sync(st->us, &msg);
103         kfree(xfers);
104
105         return ret;
106 }
107
108 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
109  * specific to be rolled into the core.
110  */
111 static irqreturn_t adis16400_trigger_handler(int irq, void *p)
112 {
113         struct iio_poll_func *pf = p;
114         struct iio_dev *indio_dev = pf->indio_dev;
115         struct adis16400_state *st = iio_priv(indio_dev);
116         struct iio_ring_buffer *ring = indio_dev->ring;
117         int i = 0, j, ret = 0;
118         s16 *data;
119         size_t datasize = ring->access->get_bytes_per_datum(ring);
120         unsigned long mask = ring->scan_mask;
121
122         data = kmalloc(datasize , GFP_KERNEL);
123         if (data == NULL) {
124                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
125                 return -ENOMEM;
126         }
127
128         if (ring->scan_count) {
129                 if (st->variant->flags & ADIS16400_NO_BURST) {
130                         ret = adis16350_spi_read_all(&indio_dev->dev, st->rx);
131                         if (ret < 0)
132                                 goto err;
133                         for (; i < ring->scan_count; i++)
134                                 data[i] = *(s16 *)(st->rx + i*2);
135                 } else {
136                         ret = adis16400_spi_read_burst(&indio_dev->dev, st->rx);
137                         if (ret < 0)
138                                 goto err;
139                         for (; i < indio_dev->ring->scan_count; i++) {
140                                 j = __ffs(mask);
141                                 mask &= ~(1 << j);
142                                 data[i] = be16_to_cpup(
143                                         (__be16 *)&(st->rx[j*2]));
144                         }
145                 }
146         }
147         /* Guaranteed to be aligned with 8 byte boundary */
148         if (ring->scan_timestamp)
149                 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
150         ring->access->store_to(indio_dev->ring, (u8 *) data, pf->timestamp);
151
152         iio_trigger_notify_done(indio_dev->trig);
153
154         kfree(data);
155         return IRQ_HANDLED;
156
157 err:
158         kfree(data);
159         return ret;
160 }
161
162 void adis16400_unconfigure_ring(struct iio_dev *indio_dev)
163 {
164         iio_dealloc_pollfunc(indio_dev->pollfunc);
165         iio_sw_rb_free(indio_dev->ring);
166 }
167
168 static const struct iio_ring_setup_ops adis16400_ring_setup_ops = {
169         .preenable = &iio_sw_ring_preenable,
170         .postenable = &iio_triggered_ring_postenable,
171         .predisable = &iio_triggered_ring_predisable,
172 };
173
174 int adis16400_configure_ring(struct iio_dev *indio_dev)
175 {
176         int ret = 0;
177         struct adis16400_state *st = iio_priv(indio_dev);
178         struct iio_ring_buffer *ring;
179
180         ring = iio_sw_rb_allocate(indio_dev);
181         if (!ring) {
182                 ret = -ENOMEM;
183                 return ret;
184         }
185         indio_dev->ring = ring;
186         /* Effectively select the ring buffer implementation */
187         ring->access = &ring_sw_access_funcs;
188         ring->bpe = 2;
189         ring->scan_timestamp = true;
190         ring->setup_ops = &adis16400_ring_setup_ops;
191         ring->owner = THIS_MODULE;
192         /* Set default scan mode */
193         ring->scan_mask = st->variant->default_scan_mask;
194         ring->scan_count = hweight_long(st->variant->default_scan_mask);
195
196         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
197                                                  &adis16400_trigger_handler,
198                                                  IRQF_ONESHOT,
199                                                  indio_dev,
200                                                  "%s_consumer%d",
201                                                  indio_dev->name,
202                                                  indio_dev->id);
203         if (indio_dev->pollfunc == NULL) {
204                 ret = -ENOMEM;
205                 goto error_iio_sw_rb_free;
206         }
207
208         indio_dev->modes |= INDIO_RING_TRIGGERED;
209         return 0;
210 error_iio_sw_rb_free:
211         iio_sw_rb_free(indio_dev->ring);
212         return ret;
213 }