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>
6 #include <linux/bitops.h>
7 #include <linux/export.h>
8
9 #include <linux/iio/iio.h>
10 #include <linux/iio/buffer.h>
11 #include <linux/iio/triggered_buffer.h>
12 #include <linux/iio/trigger_consumer.h>
13
14 #include "adis16400.h"
15
adis16400_update_scan_mode(struct iio_dev * indio_dev,const unsigned long * scan_mask)16 int adis16400_update_scan_mode(struct iio_dev *indio_dev,
17 const unsigned long *scan_mask)
18 {
19 struct adis16400_state *st = iio_priv(indio_dev);
20 struct adis *adis = &st->adis;
21 unsigned int burst_length;
22 u8 *tx;
23
24 if (st->variant->flags & ADIS16400_NO_BURST)
25 return adis_update_scan_mode(indio_dev, scan_mask);
26
27 kfree(adis->xfer);
28 kfree(adis->buffer);
29
30 /* All but the timestamp channel */
31 burst_length = (indio_dev->num_channels - 1) * sizeof(u16);
32 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
33 burst_length += sizeof(u16);
34
35 adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
36 if (!adis->xfer)
37 return -ENOMEM;
38
39 adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL);
40 if (!adis->buffer) {
41 kfree(adis->xfer);
42 adis->xfer = NULL;
43 return -ENOMEM;
44 }
45
46 tx = adis->buffer + burst_length;
47 tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD);
48 tx[1] = 0;
49
50 adis->xfer[0].tx_buf = tx;
51 adis->xfer[0].bits_per_word = 8;
52 adis->xfer[0].len = 2;
53 adis->xfer[1].rx_buf = adis->buffer;
54 adis->xfer[1].bits_per_word = 8;
55 adis->xfer[1].len = burst_length;
56
57 spi_message_init(&adis->msg);
58 spi_message_add_tail(&adis->xfer[0], &adis->msg);
59 spi_message_add_tail(&adis->xfer[1], &adis->msg);
60
61 return 0;
62 }
63
adis16400_trigger_handler(int irq,void * p)64 irqreturn_t adis16400_trigger_handler(int irq, void *p)
65 {
66 struct iio_poll_func *pf = p;
67 struct iio_dev *indio_dev = pf->indio_dev;
68 struct adis16400_state *st = iio_priv(indio_dev);
69 struct adis *adis = &st->adis;
70 u32 old_speed_hz = st->adis.spi->max_speed_hz;
71 void *buffer;
72 int ret;
73
74 if (!adis->buffer)
75 return -ENOMEM;
76
77 if (!(st->variant->flags & ADIS16400_NO_BURST) &&
78 st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) {
79 st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST;
80 spi_setup(st->adis.spi);
81 }
82
83 ret = spi_sync(adis->spi, &adis->msg);
84 if (ret)
85 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
86
87 if (!(st->variant->flags & ADIS16400_NO_BURST)) {
88 st->adis.spi->max_speed_hz = old_speed_hz;
89 spi_setup(st->adis.spi);
90 }
91
92 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
93 buffer = adis->buffer + sizeof(u16);
94 else
95 buffer = adis->buffer;
96
97 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
98 pf->timestamp);
99
100 iio_trigger_notify_done(indio_dev->trig);
101
102 return IRQ_HANDLED;
103 }
104