blob: 271fe1d7c52d93a99458a664fc236b74587fcff8 [file] [log] [blame]
Barry Songa9d26f02010-05-04 14:43:15 +01001#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>
Mike Frysinger1cb6c1f2010-05-23 03:10:35 -04009#include <linux/slab.h>
Barry Songa9d26f02010-05-04 14:43:15 +010010#include <linux/sysfs.h>
11#include <linux/list.h>
Jonathan Cameron2a29a902011-05-18 14:41:28 +010012#include <linux/bitops.h>
Barry Songa9d26f02010-05-04 14:43:15 +010013
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
Barry Song3fd66da2010-06-04 17:19:53 +080021/**
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 **/
26static 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);
Jonathan Cameron38d15f02011-05-18 14:42:23 +010030 struct adis16400_state *st = iio_priv(indio_dev);
Barry Song3fd66da2010-06-04 17:19:53 +080031 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,
Barry Song3fd66da2010-06-04 17:19:53 +080039 }, {
40 .rx_buf = rx,
41 .bits_per_word = 8,
42 .len = 24,
Barry Song3fd66da2010-06-04 17:19:53 +080043 },
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
Jonathan Cameron2a29a902011-05-18 14:41:28 +010067static 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
81static int adis16350_spi_read_all(struct device *dev, u8 *rx)
82{
83 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameron38d15f02011-05-18 14:42:23 +010084 struct adis16400_state *st = iio_priv(indio_dev);
Jonathan Cameron2a29a902011-05-18 14:41:28 +010085
86 struct spi_message msg;
87 int i, j = 0, ret;
88 struct spi_transfer *xfers;
89
Jonathan Cameron38d15f02011-05-18 14:42:23 +010090 xfers = kzalloc(sizeof(*xfers)*indio_dev->ring->scan_count + 1,
91 GFP_KERNEL);
Jonathan Cameron2a29a902011-05-18 14:41:28 +010092 if (xfers == NULL)
93 return -ENOMEM;
94
95 for (i = 0; i < ARRAY_SIZE(read_all_tx_array); i++)
Jonathan Cameron38d15f02011-05-18 14:42:23 +010096 if (indio_dev->ring->scan_mask & (1 << i)) {
Jonathan Cameron2a29a902011-05-18 14:41:28 +010097 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);
Jonathan Cameron38d15f02011-05-18 14:42:23 +0100107 for (j = 0; j < indio_dev->ring->scan_count + 1; j++)
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100108 spi_message_add_tail(&xfers[j], &msg);
109
110 ret = spi_sync(st->us, &msg);
111 kfree(xfers);
112
113 return ret;
114}
115
Barry Songa9d26f02010-05-04 14:43:15 +0100116/* 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 */
Jonathan Camerone7854842011-05-18 14:41:27 +0100119static irqreturn_t adis16400_trigger_handler(int irq, void *p)
Barry Songa9d26f02010-05-04 14:43:15 +0100120{
Jonathan Camerone7854842011-05-18 14:41:27 +0100121 struct iio_poll_func *pf = p;
122 struct iio_dev *indio_dev = pf->private_data;
Jonathan Cameron38d15f02011-05-18 14:42:23 +0100123 struct adis16400_state *st = iio_priv(indio_dev);
Jonathan Camerone7854842011-05-18 14:41:27 +0100124 struct iio_ring_buffer *ring = indio_dev->ring;
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100125 int i = 0, j, ret = 0;
Barry Songa9d26f02010-05-04 14:43:15 +0100126 s16 *data;
Manuel Stahlbf329632010-08-31 11:32:52 +0200127 size_t datasize = ring->access.get_bytes_per_datum(ring);
Michael Hennerich0fea4d62011-03-21 16:44:38 +0100128 unsigned long mask = ring->scan_mask;
Barry Songa9d26f02010-05-04 14:43:15 +0100129
130 data = kmalloc(datasize , GFP_KERNEL);
131 if (data == NULL) {
132 dev_err(&st->us->dev, "memory alloc failed in ring bh");
Jonathan Camerone7854842011-05-18 14:41:27 +0100133 return -ENOMEM;
Barry Songa9d26f02010-05-04 14:43:15 +0100134 }
135
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100136 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++) {
Michael Hennerich0fea4d62011-03-21 16:44:38 +0100148 j = __ffs(mask);
149 mask &= ~(1 << j);
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100150 data[i] = be16_to_cpup(
Michael Hennerich0fea4d62011-03-21 16:44:38 +0100151 (__be16 *)&(st->rx[j*2]));
152 }
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100153 }
154 }
Barry Songa9d26f02010-05-04 14:43:15 +0100155 /* Guaranteed to be aligned with 8 byte boundary */
Manuel Stahlbf329632010-08-31 11:32:52 +0200156 if (ring->scan_timestamp)
Jonathan Camerone7854842011-05-18 14:41:27 +0100157 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
158 ring->access.store_to(indio_dev->ring, (u8 *) data, pf->timestamp);
Barry Songa9d26f02010-05-04 14:43:15 +0100159
Jonathan Camerone7854842011-05-18 14:41:27 +0100160 iio_trigger_notify_done(indio_dev->trig);
Barry Songa9d26f02010-05-04 14:43:15 +0100161 kfree(data);
162
Jonathan Camerone7854842011-05-18 14:41:27 +0100163 return IRQ_HANDLED;
Barry Songa9d26f02010-05-04 14:43:15 +0100164}
Barry Songa9d26f02010-05-04 14:43:15 +0100165
Barry Songa9d26f02010-05-04 14:43:15 +0100166void adis16400_unconfigure_ring(struct iio_dev *indio_dev)
167{
Jonathan Camerone7854842011-05-18 14:41:27 +0100168 kfree(indio_dev->pollfunc->name);
Barry Songa9d26f02010-05-04 14:43:15 +0100169 kfree(indio_dev->pollfunc);
170 iio_sw_rb_free(indio_dev->ring);
171}
172
173int adis16400_configure_ring(struct iio_dev *indio_dev)
174{
175 int ret = 0;
Jonathan Cameron38d15f02011-05-18 14:42:23 +0100176 struct adis16400_state *st = iio_priv(indio_dev);
Barry Songa9d26f02010-05-04 14:43:15 +0100177 struct iio_ring_buffer *ring;
Barry Songa9d26f02010-05-04 14:43:15 +0100178
179 ring = iio_sw_rb_allocate(indio_dev);
180 if (!ring) {
181 ret = -ENOMEM;
182 return ret;
183 }
184 indio_dev->ring = ring;
185 /* Effectively select the ring buffer implementation */
186 iio_ring_sw_register_funcs(&ring->access);
Jonathan Cameron43c11b42010-07-11 16:39:17 +0100187 ring->bpe = 2;
Manuel Stahlbf329632010-08-31 11:32:52 +0200188 ring->scan_timestamp = true;
Jonathan Cameron43c11b42010-07-11 16:39:17 +0100189 ring->preenable = &iio_sw_ring_preenable;
Jonathan Cameronc3db00c2010-07-11 16:39:10 +0100190 ring->postenable = &iio_triggered_ring_postenable;
191 ring->predisable = &iio_triggered_ring_predisable;
Barry Songa9d26f02010-05-04 14:43:15 +0100192 ring->owner = THIS_MODULE;
Jonathan Cameron2a29a902011-05-18 14:41:28 +0100193 ring->scan_mask = st->variant->default_scan_mask;
194 ring->scan_count = hweight_long(st->variant->default_scan_mask);
Manuel Stahlbf329632010-08-31 11:32:52 +0200195 /* Set default scan mode */
Manuel Stahlbf329632010-08-31 11:32:52 +0200196
Jonathan Camerone7854842011-05-18 14:41:27 +0100197 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
198 if (indio_dev->pollfunc == NULL) {
199 ret = -ENOMEM;
Jonathan Cameron15744092010-07-11 16:39:09 +0100200 goto error_iio_sw_rb_free;
Jonathan Camerone7854842011-05-18 14:41:27 +0100201 }
202 indio_dev->pollfunc->private_data = indio_dev;
203 indio_dev->pollfunc->h = &iio_pollfunc_store_time;
204 indio_dev->pollfunc->thread = &adis16400_trigger_handler;
205 indio_dev->pollfunc->type = IRQF_ONESHOT;
206 indio_dev->pollfunc->name =
207 kasprintf(GFP_KERNEL, "adis16400_consumer%d", indio_dev->id);
208 if (ret)
209 goto error_iio_free_pollfunc;
Jonathan Cameron15744092010-07-11 16:39:09 +0100210
Barry Songa9d26f02010-05-04 14:43:15 +0100211 indio_dev->modes |= INDIO_RING_TRIGGERED;
212 return 0;
Jonathan Camerone7854842011-05-18 14:41:27 +0100213error_iio_free_pollfunc:
214 kfree(indio_dev->pollfunc);
Barry Songa9d26f02010-05-04 14:43:15 +0100215error_iio_sw_rb_free:
216 iio_sw_rb_free(indio_dev->ring);
217 return ret;
218}