]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/accel/adis16203_ring.c
staging:iio: rationalization of different buffer implementation hooks.
[karo-tx-linux.git] / drivers / staging / iio / accel / adis16203_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
13 #include "../iio.h"
14 #include "../sysfs.h"
15 #include "../ring_sw.h"
16 #include "accel.h"
17 #include "../trigger.h"
18 #include "adis16203.h"
19
20 /**
21  * adis16203_read_ring_data() read data registers which will be placed into ring
22  * @dev: device associated with child of actual device (iio_dev or iio_trig)
23  * @rx: somewhere to pass back the value read
24  **/
25 static int adis16203_read_ring_data(struct device *dev, u8 *rx)
26 {
27         struct spi_message msg;
28         struct iio_dev *indio_dev = dev_get_drvdata(dev);
29         struct adis16203_state *st = iio_dev_get_devdata(indio_dev);
30         struct spi_transfer xfers[ADIS16203_OUTPUTS + 1];
31         int ret;
32         int i;
33
34         mutex_lock(&st->buf_lock);
35
36         spi_message_init(&msg);
37
38         memset(xfers, 0, sizeof(xfers));
39         for (i = 0; i <= ADIS16203_OUTPUTS; i++) {
40                 xfers[i].bits_per_word = 8;
41                 xfers[i].cs_change = 1;
42                 xfers[i].len = 2;
43                 xfers[i].delay_usecs = 20;
44                 xfers[i].tx_buf = st->tx + 2 * i;
45                 if (i < 1) /* SUPPLY_OUT: 0x02, AUX_ADC: 0x08 */
46                         st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i);
47                 else
48                         st->tx[2 * i] = ADIS16203_READ_REG(ADIS16203_SUPPLY_OUT + 2 * i + 6);
49                 st->tx[2 * i + 1] = 0;
50                 if (i >= 1)
51                         xfers[i].rx_buf = rx + 2 * (i - 1);
52                 spi_message_add_tail(&xfers[i], &msg);
53         }
54
55         ret = spi_sync(st->us, &msg);
56         if (ret)
57                 dev_err(&st->us->dev, "problem when burst reading");
58
59         mutex_unlock(&st->buf_lock);
60
61         return ret;
62 }
63
64 /* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
65  * specific to be rolled into the core.
66  */
67 static irqreturn_t adis16203_trigger_handler(int irq, void *p)
68 {
69         struct iio_poll_func *pf = p;
70         struct iio_dev *indio_dev = pf->private_data;
71         struct adis16203_state *st = iio_dev_get_devdata(indio_dev);
72         struct iio_ring_buffer *ring = indio_dev->ring;
73
74         int i = 0;
75         s16 *data;
76         size_t datasize = ring->access->get_bytes_per_datum(ring);
77
78         data = kmalloc(datasize, GFP_KERNEL);
79         if (data == NULL) {
80                 dev_err(&st->us->dev, "memory alloc failed in ring bh");
81                 return -ENOMEM;
82         }
83
84         if (ring->scan_count)
85                 if (adis16203_read_ring_data(&st->indio_dev->dev, st->rx) >= 0)
86                         for (; i < ring->scan_count; i++)
87                                 data[i] = be16_to_cpup(
88                                         (__be16 *)&(st->rx[i*2]));
89
90         /* Guaranteed to be aligned with 8 byte boundary */
91         if (ring->scan_timestamp)
92                 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
93
94         ring->access->store_to(ring,
95                               (u8 *)data,
96                               pf->timestamp);
97
98         iio_trigger_notify_done(st->indio_dev->trig);
99         kfree(data);
100
101         return IRQ_HANDLED;
102 }
103
104 void adis16203_unconfigure_ring(struct iio_dev *indio_dev)
105 {
106         kfree(indio_dev->pollfunc->name);
107         kfree(indio_dev->pollfunc);
108         iio_sw_rb_free(indio_dev->ring);
109 }
110
111 static const struct iio_ring_setup_ops adis16203_ring_setup_ops = {
112         .preenable = &iio_sw_ring_preenable,
113         .postenable = &iio_triggered_ring_postenable,
114         .predisable = &iio_triggered_ring_predisable,
115 };
116
117 int adis16203_configure_ring(struct iio_dev *indio_dev)
118 {
119         int ret = 0;
120         struct iio_ring_buffer *ring;
121
122         ring = iio_sw_rb_allocate(indio_dev);
123         if (!ring) {
124                 ret = -ENOMEM;
125                 return ret;
126         }
127         indio_dev->ring = ring;
128         /* Effectively select the ring buffer implementation */
129         ring->bpe = 2;
130         ring->scan_timestamp = true;
131         ring->access = &ring_sw_access_funcs;
132         ring->setup_ops = &adis16203_ring_setup_ops;
133         ring->owner = THIS_MODULE;
134
135         /* Set default scan mode */
136         iio_scan_mask_set(ring, ADIS16203_SCAN_SUPPLY);
137         iio_scan_mask_set(ring, ADIS16203_SCAN_TEMP);
138         iio_scan_mask_set(ring, ADIS16203_SCAN_AUX_ADC);
139         iio_scan_mask_set(ring, ADIS16203_SCAN_INCLI_X);
140         iio_scan_mask_set(ring, ADIS16203_SCAN_INCLI_Y);
141
142         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
143         if (indio_dev->pollfunc == NULL) {
144                 ret = -ENOMEM;
145                 goto error_iio_sw_rb_free;
146         }
147         indio_dev->pollfunc->private_data = indio_dev;
148         indio_dev->pollfunc->h = &iio_pollfunc_store_time;
149         indio_dev->pollfunc->thread = &adis16203_trigger_handler;
150         indio_dev->pollfunc->type = IRQF_ONESHOT;
151         indio_dev->pollfunc->name =
152                 kasprintf(GFP_KERNEL, "adis16203_consumer%d", indio_dev->id);
153         if (indio_dev->pollfunc->name == NULL) {
154                 ret = -ENOMEM;
155                 goto error_free_poll_func;
156         }
157
158         indio_dev->modes |= INDIO_RING_TRIGGERED;
159         return 0;
160 error_free_poll_func:
161         kfree(indio_dev->pollfunc);
162 error_iio_sw_rb_free:
163         iio_sw_rb_free(indio_dev->ring);
164         return ret;
165 }