Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / imu / adis16400_buffer.c
blobe70a5339acb192006bf84e732a212d5106b26889
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/interrupt.h>
3 #include <linux/mutex.h>
4 #include <linux/kernel.h>
5 #include <linux/spi/spi.h>
6 #include <linux/slab.h>
7 #include <linux/bitops.h>
8 #include <linux/export.h>
10 #include <linux/iio/iio.h>
11 #include <linux/iio/buffer.h>
12 #include <linux/iio/triggered_buffer.h>
13 #include <linux/iio/trigger_consumer.h>
15 #include "adis16400.h"
17 int adis16400_update_scan_mode(struct iio_dev *indio_dev,
18 const unsigned long *scan_mask)
20 struct adis16400_state *st = iio_priv(indio_dev);
21 struct adis *adis = &st->adis;
22 unsigned int burst_length;
23 u8 *tx;
25 if (st->variant->flags & ADIS16400_NO_BURST)
26 return adis_update_scan_mode(indio_dev, scan_mask);
28 kfree(adis->xfer);
29 kfree(adis->buffer);
31 /* All but the timestamp channel */
32 burst_length = (indio_dev->num_channels - 1) * sizeof(u16);
33 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
34 burst_length += sizeof(u16);
36 adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
37 if (!adis->xfer)
38 return -ENOMEM;
40 adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL);
41 if (!adis->buffer)
42 return -ENOMEM;
44 tx = adis->buffer + burst_length;
45 tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD);
46 tx[1] = 0;
48 adis->xfer[0].tx_buf = tx;
49 adis->xfer[0].bits_per_word = 8;
50 adis->xfer[0].len = 2;
51 adis->xfer[1].rx_buf = adis->buffer;
52 adis->xfer[1].bits_per_word = 8;
53 adis->xfer[1].len = burst_length;
55 spi_message_init(&adis->msg);
56 spi_message_add_tail(&adis->xfer[0], &adis->msg);
57 spi_message_add_tail(&adis->xfer[1], &adis->msg);
59 return 0;
62 irqreturn_t adis16400_trigger_handler(int irq, void *p)
64 struct iio_poll_func *pf = p;
65 struct iio_dev *indio_dev = pf->indio_dev;
66 struct adis16400_state *st = iio_priv(indio_dev);
67 struct adis *adis = &st->adis;
68 u32 old_speed_hz = st->adis.spi->max_speed_hz;
69 void *buffer;
70 int ret;
72 if (!adis->buffer)
73 return -ENOMEM;
75 if (!(st->variant->flags & ADIS16400_NO_BURST) &&
76 st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) {
77 st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST;
78 spi_setup(st->adis.spi);
81 ret = spi_sync(adis->spi, &adis->msg);
82 if (ret)
83 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
85 if (!(st->variant->flags & ADIS16400_NO_BURST)) {
86 st->adis.spi->max_speed_hz = old_speed_hz;
87 spi_setup(st->adis.spi);
90 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
91 buffer = adis->buffer + sizeof(u16);
92 else
93 buffer = adis->buffer;
95 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
96 pf->timestamp);
98 iio_trigger_notify_done(indio_dev->trig);
100 return IRQ_HANDLED;