blob: 90c24a23c679b8001e31cdff48b098cacb872682 [file] [log] [blame]
Barry Songa9d26f02010-05-04 14:43:15 +01001#include <linux/interrupt.h>
Barry Songa9d26f02010-05-04 14:43:15 +01002#include <linux/mutex.h>
Barry Songa9d26f02010-05-04 14:43:15 +01003#include <linux/kernel.h>
4#include <linux/spi/spi.h>
Mike Frysinger1cb6c1f2010-05-23 03:10:35 -04005#include <linux/slab.h>
Jonathan Cameron2a29a902011-05-18 14:41:28 +01006#include <linux/bitops.h>
Paul Gortmaker8e336a72011-07-10 13:09:12 -04007#include <linux/export.h>
Barry Songa9d26f02010-05-04 14:43:15 +01008
Jonathan Cameron06458e22012-04-25 15:54:58 +01009#include <linux/iio/iio.h>
Lars-Peter Clausen1ffe2e72013-01-16 12:48:00 +000010#include <linux/iio/buffer.h>
11#include <linux/iio/triggered_buffer.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010012#include <linux/iio/trigger_consumer.h>
Lars-Peter Clausencd888a172013-01-16 12:48:00 +000013
Barry Songa9d26f02010-05-04 14:43:15 +010014#include "adis16400.h"
15
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000016int adis16400_update_scan_mode(struct iio_dev *indio_dev,
17 const unsigned long *scan_mask)
Jonathan Cameron2a29a902011-05-18 14:41:28 +010018{
Jonathan Cameron38d15f02011-05-18 14:42:23 +010019 struct adis16400_state *st = iio_priv(indio_dev);
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000020 struct adis *adis = &st->adis;
Paul Cercueil9df56032015-05-15 17:18:37 +020021 unsigned int burst_length;
22 u8 *tx;
Jonathan Cameron2a29a902011-05-18 14:41:28 +010023
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000024 if (st->variant->flags & ADIS16400_NO_BURST)
25 return adis_update_scan_mode(indio_dev, scan_mask);
Jonathan Cameron2a29a902011-05-18 14:41:28 +010026
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000027 kfree(adis->xfer);
28 kfree(adis->buffer);
29
Paul Cercueil9df56032015-05-15 17:18:37 +020030 /* All but the timestamp channel */
31 burst_length = (indio_dev->num_channels - 1) * sizeof(u16);
Lars-Peter Clausend046ba22015-05-15 17:18:38 +020032 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
33 burst_length += sizeof(u16);
Paul Cercueil9df56032015-05-15 17:18:37 +020034
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000035 adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
36 if (!adis->xfer)
Jonathan Cameron2a29a902011-05-18 14:41:28 +010037 return -ENOMEM;
38
Paul Cercueil9df56032015-05-15 17:18:37 +020039 adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL);
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000040 if (!adis->buffer)
41 return -ENOMEM;
Jonathan Cameron2a29a902011-05-18 14:41:28 +010042
Paul Cercueil9df56032015-05-15 17:18:37 +020043 tx = adis->buffer + burst_length;
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000044 tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD);
45 tx[1] = 0;
Jonathan Cameron2a29a902011-05-18 14:41:28 +010046
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000047 adis->xfer[0].tx_buf = tx;
48 adis->xfer[0].bits_per_word = 8;
49 adis->xfer[0].len = 2;
Paul Cercueil9df56032015-05-15 17:18:37 +020050 adis->xfer[1].rx_buf = adis->buffer;
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000051 adis->xfer[1].bits_per_word = 8;
Paul Cercueil9df56032015-05-15 17:18:37 +020052 adis->xfer[1].len = burst_length;
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000053
54 spi_message_init(&adis->msg);
55 spi_message_add_tail(&adis->xfer[0], &adis->msg);
56 spi_message_add_tail(&adis->xfer[1], &adis->msg);
57
58 return 0;
Jonathan Cameron2a29a902011-05-18 14:41:28 +010059}
60
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000061irqreturn_t adis16400_trigger_handler(int irq, void *p)
Barry Songa9d26f02010-05-04 14:43:15 +010062{
Jonathan Camerone7854842011-05-18 14:41:27 +010063 struct iio_poll_func *pf = p;
Jonathan Camerone65bc6a2011-08-24 17:28:36 +010064 struct iio_dev *indio_dev = pf->indio_dev;
Jonathan Cameron38d15f02011-05-18 14:42:23 +010065 struct adis16400_state *st = iio_priv(indio_dev);
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000066 struct adis *adis = &st->adis;
67 u32 old_speed_hz = st->adis.spi->max_speed_hz;
Lars-Peter Clausend046ba22015-05-15 17:18:38 +020068 void *buffer;
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000069 int ret;
Jonathan Cameron420fe2e2012-04-21 10:09:35 +010070
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000071 if (!adis->buffer)
72 return -ENOMEM;
73
74 if (!(st->variant->flags & ADIS16400_NO_BURST) &&
75 st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) {
76 st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST;
77 spi_setup(st->adis.spi);
Barry Songa9d26f02010-05-04 14:43:15 +010078 }
79
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000080 ret = spi_sync(adis->spi, &adis->msg);
81 if (ret)
82 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
83
84 if (!(st->variant->flags & ADIS16400_NO_BURST)) {
85 st->adis.spi->max_speed_hz = old_speed_hz;
86 spi_setup(st->adis.spi);
Jonathan Cameron2a29a902011-05-18 14:41:28 +010087 }
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000088
Lars-Peter Clausend046ba22015-05-15 17:18:38 +020089 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
90 buffer = adis->buffer + sizeof(u16);
91 else
92 buffer = adis->buffer;
93
94 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
Lars-Peter Clausen8c7b0322013-09-19 13:59:00 +010095 pf->timestamp);
Lars-Peter Clausen5eda3552013-01-16 12:48:00 +000096
Jonathan Camerone7854842011-05-18 14:41:27 +010097 iio_trigger_notify_done(indio_dev->trig);
Barry Songa9d26f02010-05-04 14:43:15 +010098
Jonathan Camerone7854842011-05-18 14:41:27 +010099 return IRQ_HANDLED;
Barry Songa9d26f02010-05-04 14:43:15 +0100100}