staging:iio: treewide rename iio_triggered_ring_* to iio_triggered_buffer_*
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / accel / adis16240_ring.c
blobe52ad2e36553458a3fcbbcccf15e5d0a7a689366
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>
7 #include "../iio.h"
8 #include "../ring_sw.h"
9 #include "../trigger_consumer.h"
10 #include "adis16240.h"
12 /**
13 * adis16240_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 adis16240_read_ring_data(struct device *dev, u8 *rx)
19 struct spi_message msg;
20 struct iio_dev *indio_dev = dev_get_drvdata(dev);
21 struct adis16240_state *st = iio_priv(indio_dev);
22 struct spi_transfer xfers[ADIS16240_OUTPUTS + 1];
23 int ret;
24 int i;
26 mutex_lock(&st->buf_lock);
28 spi_message_init(&msg);
30 memset(xfers, 0, sizeof(xfers));
31 for (i = 0; i <= ADIS16240_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 st->tx[2 * i]
38 = ADIS16240_READ_REG(ADIS16240_SUPPLY_OUT + 2 * i);
39 st->tx[2 * i + 1] = 0;
40 if (i >= 1)
41 xfers[i].rx_buf = rx + 2 * (i - 1);
42 spi_message_add_tail(&xfers[i], &msg);
45 ret = spi_sync(st->us, &msg);
46 if (ret)
47 dev_err(&st->us->dev, "problem when burst reading");
49 mutex_unlock(&st->buf_lock);
51 return ret;
54 static irqreturn_t adis16240_trigger_handler(int irq, void *p)
56 struct iio_poll_func *pf = p;
57 struct iio_dev *indio_dev = pf->indio_dev;
58 struct adis16240_state *st = iio_priv(indio_dev);
59 struct iio_ring_buffer *ring = indio_dev->ring;
61 int i = 0;
62 s16 *data;
63 size_t datasize = ring->access->get_bytes_per_datum(ring);
65 data = kmalloc(datasize, GFP_KERNEL);
66 if (data == NULL) {
67 dev_err(&st->us->dev, "memory alloc failed in ring bh");
68 return -ENOMEM;
71 if (ring->scan_count &&
72 adis16240_read_ring_data(&indio_dev->dev, st->rx) >= 0)
73 for (; i < ring->scan_count; i++)
74 data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
76 /* Guaranteed to be aligned with 8 byte boundary */
77 if (ring->scan_timestamp)
78 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
80 ring->access->store_to(ring, (u8 *)data, pf->timestamp);
82 iio_trigger_notify_done(indio_dev->trig);
83 kfree(data);
85 return IRQ_HANDLED;
88 void adis16240_unconfigure_ring(struct iio_dev *indio_dev)
90 iio_dealloc_pollfunc(indio_dev->pollfunc);
91 iio_sw_rb_free(indio_dev->ring);
94 static const struct iio_ring_setup_ops adis16240_ring_setup_ops = {
95 .preenable = &iio_sw_ring_preenable,
96 .postenable = &iio_triggered_buffer_postenable,
97 .predisable = &iio_triggered_buffer_predisable,
100 int adis16240_configure_ring(struct iio_dev *indio_dev)
102 int ret = 0;
103 struct iio_ring_buffer *ring;
105 ring = iio_sw_rb_allocate(indio_dev);
106 if (!ring) {
107 ret = -ENOMEM;
108 return ret;
110 indio_dev->ring = ring;
111 /* Effectively select the ring buffer implementation */
112 ring->access = &ring_sw_access_funcs;
113 ring->bpe = 2;
114 ring->scan_timestamp = true;
115 ring->setup_ops = &adis16240_ring_setup_ops;
116 ring->owner = THIS_MODULE;
118 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
119 &adis16240_trigger_handler,
120 IRQF_ONESHOT,
121 indio_dev,
122 "%s_consumer%d",
123 indio_dev->name,
124 indio_dev->id);
125 if (indio_dev->pollfunc == NULL) {
126 ret = -ENOMEM;
127 goto error_iio_sw_rb_free;
130 indio_dev->modes |= INDIO_RING_TRIGGERED;
131 return 0;
133 error_iio_sw_rb_free:
134 iio_sw_rb_free(indio_dev->ring);
135 return ret;