Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.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  * adis16260_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 adis16260_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 adis16260_state *st = iio_priv(indio_dev);
30         struct spi_transfer xfers[ADIS16260_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 <= ADIS16260_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 = 30;
44                 xfers[i].tx_buf = st->tx + 2 * i;
45                 if (i < 2) /* SUPPLY_OUT:0x02 GYRO_OUT:0x04 */
46                         st->tx[2 * i]
47                                 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
48                                                 + 2 * i);
49                 else /* 0x06 to 0x09 is reserved */
50                         st->tx[2 * i]
51                                 = ADIS16260_READ_REG(ADIS16260_SUPPLY_OUT
52                                                 + 2 * i + 4);
53                 st->tx[2 * i + 1] = 0;
54                 if (i >= 1)
55                         xfers[i].rx_buf = rx + 2 * (i - 1);
56                 spi_message_add_tail(&xfers[i], &msg);
57         }
58
59         ret = spi_sync(st->us, &msg);
60         if (ret)
61                 dev_err(&st->us->dev, "problem when burst reading");
62
63         mutex_unlock(&st->buf_lock);
64
65         return ret;
66 }
67
68 static irqreturn_t adis16260_trigger_handler(int irq, void *p)
69 {
70         struct iio_poll_func *pf = p;
71         struct iio_dev *indio_dev = pf->private_data;
72         struct adis16260_state *st = iio_priv(indio_dev);
73         struct iio_ring_buffer *ring = indio_dev->ring;
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             adis16260_read_ring_data(&indio_dev->dev, st->rx) >= 0)
86                 for (; i < ring->scan_count; i++)
87                         data[i] = be16_to_cpup((__be16 *)&(st->rx[i*2]));
88
89         /* Guaranteed to be aligned with 8 byte boundary */
90         if (ring->scan_timestamp)
91                 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
92
93         ring->access->store_to(ring, (u8 *)data, pf->timestamp);
94
95         iio_trigger_notify_done(indio_dev->trig);
96         kfree(data);
97
98         return IRQ_HANDLED;
99 }
100
101 void adis16260_unconfigure_ring(struct iio_dev *indio_dev)
102 {
103         iio_dealloc_pollfunc(indio_dev->pollfunc);
104         iio_sw_rb_free(indio_dev->ring);
105 }
106
107 static const struct iio_ring_setup_ops adis16260_ring_setup_ops = {
108         .preenable = &iio_sw_ring_preenable,
109         .postenable = &iio_triggered_ring_postenable,
110         .predisable = &iio_triggered_ring_predisable,
111 };
112
113 int adis16260_configure_ring(struct iio_dev *indio_dev)
114 {
115         int ret = 0;
116         struct iio_ring_buffer *ring;
117
118         ring = iio_sw_rb_allocate(indio_dev);
119         if (!ring) {
120                 ret = -ENOMEM;
121                 return ret;
122         }
123         indio_dev->ring = ring;
124         /* Effectively select the ring buffer implementation */
125         ring->access = &ring_sw_access_funcs;
126         ring->bpe = 2;
127         ring->scan_timestamp = true;
128         ring->setup_ops = &adis16260_ring_setup_ops;
129         ring->owner = THIS_MODULE;
130
131         /* Set default scan mode */
132         iio_scan_mask_set(ring, ADIS16260_SCAN_SUPPLY);
133         iio_scan_mask_set(ring, ADIS16260_SCAN_GYRO);
134         iio_scan_mask_set(ring, ADIS16260_SCAN_AUX_ADC);
135         iio_scan_mask_set(ring, ADIS16260_SCAN_TEMP);
136         iio_scan_mask_set(ring, ADIS16260_SCAN_ANGL);
137
138         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
139                                                  &adis16260_trigger_handler,
140                                                  IRQF_ONESHOT,
141                                                  indio_dev,
142                                                  "adis16260_consumer%d",
143                                                  indio_dev->id);
144         if (indio_dev->pollfunc == NULL) {
145                 ret = -ENOMEM;
146                 goto error_iio_sw_rb_free;
147         }
148
149         indio_dev->modes |= INDIO_RING_TRIGGERED;
150         return 0;
151
152 error_iio_sw_rb_free:
153         iio_sw_rb_free(indio_dev->ring);
154         return ret;
155 }