adis_buffer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 + scan_count;
  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. iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
  87. pf->timestamp);
  88. iio_trigger_notify_done(indio_dev->trig);
  89. return IRQ_HANDLED;
  90. }
  91. /**
  92. * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device
  93. * @adis: The adis device.
  94. * @indio_dev: The IIO device.
  95. * @trigger_handler: Optional trigger handler, may be NULL.
  96. *
  97. * Returns 0 on success, a negative error code otherwise.
  98. *
  99. * This function sets up the buffer and trigger for a adis devices. If
  100. * 'trigger_handler' is NULL the default trigger handler will be used. The
  101. * default trigger handler will simply read the registers assigned to the
  102. * currently active channels.
  103. *
  104. * adis_cleanup_buffer_and_trigger() should be called to free the resources
  105. * allocated by this function.
  106. */
  107. int adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
  108. irqreturn_t (*trigger_handler)(int, void *))
  109. {
  110. int ret;
  111. if (!trigger_handler)
  112. trigger_handler = adis_trigger_handler;
  113. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  114. trigger_handler, NULL);
  115. if (ret)
  116. return ret;
  117. if (adis->spi->irq) {
  118. ret = adis_probe_trigger(adis, indio_dev);
  119. if (ret)
  120. goto error_buffer_cleanup;
  121. }
  122. return 0;
  123. error_buffer_cleanup:
  124. iio_triggered_buffer_cleanup(indio_dev);
  125. return ret;
  126. }
  127. EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger);
  128. /**
  129. * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
  130. * @adis: The adis device.
  131. * @indio_dev: The IIO device.
  132. *
  133. * Frees resources allocated by adis_setup_buffer_and_trigger()
  134. */
  135. void adis_cleanup_buffer_and_trigger(struct adis *adis,
  136. struct iio_dev *indio_dev)
  137. {
  138. if (adis->spi->irq)
  139. adis_remove_trigger(adis);
  140. kfree(adis->buffer);
  141. kfree(adis->xfer);
  142. iio_triggered_buffer_cleanup(indio_dev);
  143. }
  144. EXPORT_SYMBOL_GPL(adis_cleanup_buffer_and_trigger);