adis_buffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Common library for ADIS16XXX devices
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/export.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/mutex.h>
  12. #include <linux/kernel.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/slab.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/buffer.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/iio/imu/adis.h>
  20. int adis_update_scan_mode(struct iio_dev *indio_dev,
  21. const unsigned long *scan_mask)
  22. {
  23. struct adis *adis = iio_device_get_drvdata(indio_dev);
  24. const struct iio_chan_spec *chan;
  25. unsigned int scan_count;
  26. unsigned int i, j;
  27. __be16 *tx, *rx;
  28. kfree(adis->xfer);
  29. kfree(adis->buffer);
  30. scan_count = indio_dev->scan_bytes / 2;
  31. adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
  32. if (!adis->xfer)
  33. return -ENOMEM;
  34. adis->buffer = kzalloc(indio_dev->scan_bytes * 2, GFP_KERNEL);
  35. if (!adis->buffer)
  36. return -ENOMEM;
  37. rx = adis->buffer;
  38. tx = rx + indio_dev->scan_bytes;
  39. spi_message_init(&adis->msg);
  40. for (j = 0; j <= scan_count; j++) {
  41. adis->xfer[j].bits_per_word = 8;
  42. if (j != scan_count)
  43. adis->xfer[j].cs_change = 1;
  44. adis->xfer[j].len = 2;
  45. adis->xfer[j].delay_usecs = adis->data->read_delay;
  46. if (j < scan_count)
  47. adis->xfer[j].tx_buf = &tx[j];
  48. if (j >= 1)
  49. adis->xfer[j].rx_buf = &rx[j - 1];
  50. spi_message_add_tail(&adis->xfer[j], &adis->msg);
  51. }
  52. chan = indio_dev->channels;
  53. for (i = 0; i < indio_dev->num_channels; i++, chan++) {
  54. if (!test_bit(chan->scan_index, scan_mask))
  55. continue;
  56. *tx++ = cpu_to_be16(chan->address << 8);
  57. }
  58. return 0;
  59. }
  60. EXPORT_SYMBOL_GPL(adis_update_scan_mode);
  61. static irqreturn_t adis_trigger_handler(int irq, void *p)
  62. {
  63. struct iio_poll_func *pf = p;
  64. struct iio_dev *indio_dev = pf->indio_dev;
  65. struct adis *adis = iio_device_get_drvdata(indio_dev);
  66. int ret;
  67. if (!adis->buffer)
  68. return -ENOMEM;
  69. ret = spi_sync(adis->spi, &adis->msg);
  70. if (ret)
  71. dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
  72. /* Guaranteed to be aligned with 8 byte boundary */
  73. if (indio_dev->scan_timestamp) {
  74. void *b = adis->buffer + indio_dev->scan_bytes - sizeof(s64);
  75. *(s64 *)b = pf->timestamp;
  76. }
  77. iio_push_to_buffers(indio_dev, adis->buffer);
  78. iio_trigger_notify_done(indio_dev->trig);
  79. return IRQ_HANDLED;
  80. }
  81. /**
  82. * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device
  83. * @adis: The adis device.
  84. * @indio_dev: The IIO device.
  85. * @trigger_handler: Optional trigger handler, may be NULL.
  86. *
  87. * Returns 0 on success, a negative error code otherwise.
  88. *
  89. * This function sets up the buffer and trigger for a adis devices. If
  90. * 'trigger_handler' is NULL the default trigger handler will be used. The
  91. * default trigger handler will simply read the registers assigned to the
  92. * currently active channels.
  93. *
  94. * adis_cleanup_buffer_and_trigger() should be called to free the resources
  95. * allocated by this function.
  96. */
  97. int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
  98. irqreturn_t (*trigger_handler)(int, void *))
  99. {
  100. int ret;
  101. if (!trigger_handler)
  102. trigger_handler = adis_trigger_handler;
  103. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  104. trigger_handler, NULL);
  105. if (ret)
  106. return ret;
  107. if (adis->spi->irq) {
  108. ret = adis_probe_trigger(adis, indio_dev);
  109. if (ret)
  110. goto error_buffer_cleanup;
  111. }
  112. return 0;
  113. error_buffer_cleanup:
  114. iio_triggered_buffer_cleanup(indio_dev);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger);
  118. /**
  119. * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
  120. * @adis: The adis device.
  121. * @indio_dev: The IIO device.
  122. *
  123. * Frees resources allocated by adis_setup_buffer_and_trigger()
  124. */
  125. void adis_cleanup_buffer_and_trigger(struct adis *adis,
  126. struct iio_dev *indio_dev)
  127. {
  128. if (adis->spi->irq)
  129. adis_remove_trigger(adis);
  130. kfree(adis->buffer);
  131. kfree(adis->xfer);
  132. iio_triggered_buffer_cleanup(indio_dev);
  133. }
  134. EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);