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