linux/drivers/iio/imu/adis16400_buffer.c
<<
>>
Prefs
   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>
   9
  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>
  14
  15#include "adis16400.h"
  16
  17int adis16400_update_scan_mode(struct iio_dev *indio_dev,
  18        const unsigned long *scan_mask)
  19{
  20        struct adis16400_state *st = iio_priv(indio_dev);
  21        struct adis *adis = &st->adis;
  22        unsigned int burst_length;
  23        u8 *tx;
  24
  25        if (st->variant->flags & ADIS16400_NO_BURST)
  26                return adis_update_scan_mode(indio_dev, scan_mask);
  27
  28        kfree(adis->xfer);
  29        kfree(adis->buffer);
  30
  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);
  35
  36        adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
  37        if (!adis->xfer)
  38                return -ENOMEM;
  39
  40        adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL);
  41        if (!adis->buffer)
  42                return -ENOMEM;
  43
  44        tx = adis->buffer + burst_length;
  45        tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD);
  46        tx[1] = 0;
  47
  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;
  54
  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);
  58
  59        return 0;
  60}
  61
  62irqreturn_t adis16400_trigger_handler(int irq, void *p)
  63{
  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;
  71
  72        if (!adis->buffer)
  73                return -ENOMEM;
  74
  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);
  79        }
  80
  81        ret = spi_sync(adis->spi, &adis->msg);
  82        if (ret)
  83                dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
  84
  85        if (!(st->variant->flags & ADIS16400_NO_BURST)) {
  86                st->adis.spi->max_speed_hz = old_speed_hz;
  87                spi_setup(st->adis.spi);
  88        }
  89
  90        if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
  91                buffer = adis->buffer + sizeof(u16);
  92        else
  93                buffer = adis->buffer;
  94
  95        iio_push_to_buffers_with_timestamp(indio_dev, buffer,
  96                pf->timestamp);
  97
  98        iio_trigger_notify_done(indio_dev->trig);
  99
 100        return IRQ_HANDLED;
 101}
 102