adis16400_buffer.c 2.5 KB

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