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