adis_buffer.c 4.3 KB

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