adis_buffer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if (chan->scan_type.storagebits == 32)
  57. *tx++ = cpu_to_be16((chan->address + 2) << 8);
  58. *tx++ = cpu_to_be16(chan->address << 8);
  59. }
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(adis_update_scan_mode);
  63. static irqreturn_t adis_trigger_handler(int irq, void *p)
  64. {
  65. struct iio_poll_func *pf = p;
  66. struct iio_dev *indio_dev = pf->indio_dev;
  67. struct adis *adis = iio_device_get_drvdata(indio_dev);
  68. int ret;
  69. if (!adis->buffer)
  70. return -ENOMEM;
  71. if (adis->data->has_paging) {
  72. mutex_lock(&adis->txrx_lock);
  73. if (adis->current_page != 0) {
  74. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  75. adis->tx[1] = 0;
  76. spi_write(adis->spi, adis->tx, 2);
  77. }
  78. }
  79. ret = spi_sync(adis->spi, &adis->msg);
  80. if (ret)
  81. dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
  82. if (adis->data->has_paging) {
  83. adis->current_page = 0;
  84. mutex_unlock(&adis->txrx_lock);
  85. }
  86. /* Guaranteed to be aligned with 8 byte boundary */
  87. if (indio_dev->scan_timestamp) {
  88. void *b = adis->buffer + indio_dev->scan_bytes - sizeof(s64);
  89. *(s64 *)b = pf->timestamp;
  90. }
  91. iio_push_to_buffers(indio_dev, adis->buffer);
  92. iio_trigger_notify_done(indio_dev->trig);
  93. return IRQ_HANDLED;
  94. }
  95. /**
  96. * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device
  97. * @adis: The adis device.
  98. * @indio_dev: The IIO device.
  99. * @trigger_handler: Optional trigger handler, may be NULL.
  100. *
  101. * Returns 0 on success, a negative error code otherwise.
  102. *
  103. * This function sets up the buffer and trigger for a adis devices. If
  104. * 'trigger_handler' is NULL the default trigger handler will be used. The
  105. * default trigger handler will simply read the registers assigned to the
  106. * currently active channels.
  107. *
  108. * adis_cleanup_buffer_and_trigger() should be called to free the resources
  109. * allocated by this function.
  110. */
  111. int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
  112. irqreturn_t (*trigger_handler)(int, void *))
  113. {
  114. int ret;
  115. if (!trigger_handler)
  116. trigger_handler = adis_trigger_handler;
  117. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  118. trigger_handler, NULL);
  119. if (ret)
  120. return ret;
  121. if (adis->spi->irq) {
  122. ret = adis_probe_trigger(adis, indio_dev);
  123. if (ret)
  124. goto error_buffer_cleanup;
  125. }
  126. return 0;
  127. error_buffer_cleanup:
  128. iio_triggered_buffer_cleanup(indio_dev);
  129. return ret;
  130. }
  131. EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger);
  132. /**
  133. * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
  134. * @adis: The adis device.
  135. * @indio_dev: The IIO device.
  136. *
  137. * Frees resources allocated by adis_setup_buffer_and_trigger()
  138. */
  139. void adis_cleanup_buffer_and_trigger(struct adis *adis,
  140. struct iio_dev *indio_dev)
  141. {
  142. if (adis->spi->irq)
  143. adis_remove_trigger(adis);
  144. kfree(adis->buffer);
  145. kfree(adis->xfer);
  146. iio_triggered_buffer_cleanup(indio_dev);
  147. }
  148. EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);