adis16400_buffer.c 2.5 KB

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