blob: f5b9bb8f65e09a00d06e7cff770bd2e4b77bf368 [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>
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 "adis16400.h"
19
Barry Song3fd66da2010-06-04 17:19:53 +080020/**
21 * adis16400_spi_read_burst() - read all data registers
22 * @dev: device associated with child of actual device (iio_dev or iio_trig)
23 * @rx: somewhere to pass back the value read (min size is 24 bytes)
24 **/
25static int adis16400_spi_read_burst(struct device *dev, u8 *rx)
26{
27 struct spi_message msg;
28 struct iio_dev *indio_dev = dev_get_drvdata(dev);
29 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
30 u32 old_speed_hz = st->us->max_speed_hz;
31 int ret;
32
33 struct spi_transfer xfers[] = {
34 {
35 .tx_buf = st->tx,
36 .bits_per_word = 8,
37 .len = 2,
Barry Song3fd66da2010-06-04 17:19:53 +080038 }, {
39 .rx_buf = rx,
40 .bits_per_word = 8,
41 .len = 24,
Barry Song3fd66da2010-06-04 17:19:53 +080042 },
43 };
44
45 mutex_lock(&st->buf_lock);
46 st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD);
47 st->tx[1] = 0;
48
49 spi_message_init(&msg);
50 spi_message_add_tail(&xfers[0], &msg);
51 spi_message_add_tail(&xfers[1], &msg);
52
53 st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz);
54 spi_setup(st->us);
55
56 ret = spi_sync(st->us, &msg);
57 if (ret)
58 dev_err(&st->us->dev, "problem when burst reading");
59
60 st->us->max_speed_hz = old_speed_hz;
61 spi_setup(st->us);
62 mutex_unlock(&st->buf_lock);
63 return ret;
64}
65
Barry Songa9d26f02010-05-04 14:43:15 +010066/* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
67 * specific to be rolled into the core.
68 */
Jonathan Camerone7854842011-05-18 14:41:27 +010069static irqreturn_t adis16400_trigger_handler(int irq, void *p)
Barry Songa9d26f02010-05-04 14:43:15 +010070{
Jonathan Camerone7854842011-05-18 14:41:27 +010071 struct iio_poll_func *pf = p;
72 struct iio_dev *indio_dev = pf->private_data;
73 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
74 struct iio_ring_buffer *ring = indio_dev->ring;
Michael Hennerich0fea4d62011-03-21 16:44:38 +010075 int i = 0, j;
Barry Songa9d26f02010-05-04 14:43:15 +010076 s16 *data;
Manuel Stahlbf329632010-08-31 11:32:52 +020077 size_t datasize = ring->access.get_bytes_per_datum(ring);
Michael Hennerich0fea4d62011-03-21 16:44:38 +010078 unsigned long mask = ring->scan_mask;
Barry Songa9d26f02010-05-04 14:43:15 +010079
80 data = kmalloc(datasize , GFP_KERNEL);
81 if (data == NULL) {
82 dev_err(&st->us->dev, "memory alloc failed in ring bh");
Jonathan Camerone7854842011-05-18 14:41:27 +010083 return -ENOMEM;
Barry Songa9d26f02010-05-04 14:43:15 +010084 }
85
Manuel Stahlbf329632010-08-31 11:32:52 +020086 if (ring->scan_count)
Jonathan Camerone7854842011-05-18 14:41:27 +010087 if (adis16400_spi_read_burst(&indio_dev->dev, st->rx) >= 0)
Michael Hennerich0fea4d62011-03-21 16:44:38 +010088 for (; i < ring->scan_count; i++) {
89 j = __ffs(mask);
90 mask &= ~(1 << j);
Jonathan Cameronf4658c82010-07-11 16:39:12 +010091 data[i] = be16_to_cpup(
Michael Hennerich0fea4d62011-03-21 16:44:38 +010092 (__be16 *)&(st->rx[j*2]));
93 }
Barry Songa9d26f02010-05-04 14:43:15 +010094
95 /* Guaranteed to be aligned with 8 byte boundary */
Manuel Stahlbf329632010-08-31 11:32:52 +020096 if (ring->scan_timestamp)
Jonathan Camerone7854842011-05-18 14:41:27 +010097 *((s64 *)(data + ((i + 3)/4)*4)) = pf->timestamp;
98 ring->access.store_to(indio_dev->ring, (u8 *) data, pf->timestamp);
Barry Songa9d26f02010-05-04 14:43:15 +010099
Jonathan Camerone7854842011-05-18 14:41:27 +0100100 iio_trigger_notify_done(indio_dev->trig);
Barry Songa9d26f02010-05-04 14:43:15 +0100101 kfree(data);
102
Jonathan Camerone7854842011-05-18 14:41:27 +0100103 return IRQ_HANDLED;
Barry Songa9d26f02010-05-04 14:43:15 +0100104}
Barry Songa9d26f02010-05-04 14:43:15 +0100105
Barry Songa9d26f02010-05-04 14:43:15 +0100106void adis16400_unconfigure_ring(struct iio_dev *indio_dev)
107{
Jonathan Camerone7854842011-05-18 14:41:27 +0100108 kfree(indio_dev->pollfunc->name);
Barry Songa9d26f02010-05-04 14:43:15 +0100109 kfree(indio_dev->pollfunc);
110 iio_sw_rb_free(indio_dev->ring);
111}
112
113int adis16400_configure_ring(struct iio_dev *indio_dev)
114{
115 int ret = 0;
Barry Songa9d26f02010-05-04 14:43:15 +0100116 struct iio_ring_buffer *ring;
Barry Songa9d26f02010-05-04 14:43:15 +0100117
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 iio_ring_sw_register_funcs(&ring->access);
Jonathan Cameron43c11b42010-07-11 16:39:17 +0100126 ring->bpe = 2;
Manuel Stahlbf329632010-08-31 11:32:52 +0200127 ring->scan_timestamp = true;
Jonathan Cameron43c11b42010-07-11 16:39:17 +0100128 ring->preenable = &iio_sw_ring_preenable;
Jonathan Cameronc3db00c2010-07-11 16:39:10 +0100129 ring->postenable = &iio_triggered_ring_postenable;
130 ring->predisable = &iio_triggered_ring_predisable;
Barry Songa9d26f02010-05-04 14:43:15 +0100131 ring->owner = THIS_MODULE;
132
Manuel Stahlbf329632010-08-31 11:32:52 +0200133 /* Set default scan mode */
Jonathan Camerone7854842011-05-18 14:41:27 +0100134 iio_scan_mask_set(ring, ADIS16400_SCAN_SUPPLY);
135 iio_scan_mask_set(ring, ADIS16400_SCAN_GYRO_X);
136 iio_scan_mask_set(ring, ADIS16400_SCAN_GYRO_Y);
137 iio_scan_mask_set(ring, ADIS16400_SCAN_GYRO_Z);
138 iio_scan_mask_set(ring, ADIS16400_SCAN_ACC_X);
139 iio_scan_mask_set(ring, ADIS16400_SCAN_ACC_Y);
140 iio_scan_mask_set(ring, ADIS16400_SCAN_ACC_Z);
141 iio_scan_mask_set(ring, ADIS16400_SCAN_MAGN_X);
142 iio_scan_mask_set(ring, ADIS16400_SCAN_MAGN_Y);
143 iio_scan_mask_set(ring, ADIS16400_SCAN_MAGN_Z);
144 iio_scan_mask_set(ring, ADIS16400_SCAN_TEMP);
145 iio_scan_mask_set(ring, ADIS16400_SCAN_ADC_0);
Manuel Stahlbf329632010-08-31 11:32:52 +0200146
Jonathan Camerone7854842011-05-18 14:41:27 +0100147 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
148 if (indio_dev->pollfunc == NULL) {
149 ret = -ENOMEM;
Jonathan Cameron15744092010-07-11 16:39:09 +0100150 goto error_iio_sw_rb_free;
Jonathan Camerone7854842011-05-18 14:41:27 +0100151 }
152 indio_dev->pollfunc->private_data = indio_dev;
153 indio_dev->pollfunc->h = &iio_pollfunc_store_time;
154 indio_dev->pollfunc->thread = &adis16400_trigger_handler;
155 indio_dev->pollfunc->type = IRQF_ONESHOT;
156 indio_dev->pollfunc->name =
157 kasprintf(GFP_KERNEL, "adis16400_consumer%d", indio_dev->id);
158 if (ret)
159 goto error_iio_free_pollfunc;
Jonathan Cameron15744092010-07-11 16:39:09 +0100160
Barry Songa9d26f02010-05-04 14:43:15 +0100161 indio_dev->modes |= INDIO_RING_TRIGGERED;
162 return 0;
Jonathan Camerone7854842011-05-18 14:41:27 +0100163error_iio_free_pollfunc:
164 kfree(indio_dev->pollfunc);
Barry Songa9d26f02010-05-04 14:43:15 +0100165error_iio_sw_rb_free:
166 iio_sw_rb_free(indio_dev->ring);
167 return ret;
168}