adis_buffer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common library for ADIS16XXX devices
  4. *
  5. * Copyright 2012 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. */
  8. #include <linux/export.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mutex.h>
  11. #include <linux/kernel.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/slab.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/buffer.h>
  16. #include <linux/iio/trigger_consumer.h>
  17. #include <linux/iio/triggered_buffer.h>
  18. #include <linux/iio/imu/adis.h>
  19. static int adis_update_scan_mode_burst(struct iio_dev *indio_dev,
  20. const unsigned long *scan_mask)
  21. {
  22. struct adis *adis = iio_device_get_drvdata(indio_dev);
  23. unsigned int burst_length, burst_max_length;
  24. u8 *tx;
  25. burst_length = adis->data->burst_len + adis->burst_extra_len;
  26. if (adis->data->burst_max_len)
  27. burst_max_length = adis->data->burst_max_len;
  28. else
  29. burst_max_length = burst_length;
  30. adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL);
  31. if (!adis->xfer)
  32. return -ENOMEM;
  33. adis->buffer = kzalloc(burst_max_length + sizeof(u16), GFP_KERNEL);
  34. if (!adis->buffer) {
  35. kfree(adis->xfer);
  36. adis->xfer = NULL;
  37. return -ENOMEM;
  38. }
  39. tx = adis->buffer + burst_max_length;
  40. tx[0] = ADIS_READ_REG(adis->data->burst_reg_cmd);
  41. tx[1] = 0;
  42. adis->xfer[0].tx_buf = tx;
  43. adis->xfer[0].bits_per_word = 8;
  44. adis->xfer[0].len = 2;
  45. adis->xfer[1].rx_buf = adis->buffer;
  46. adis->xfer[1].bits_per_word = 8;
  47. adis->xfer[1].len = burst_length;
  48. spi_message_init(&adis->msg);
  49. spi_message_add_tail(&adis->xfer[0], &adis->msg);
  50. spi_message_add_tail(&adis->xfer[1], &adis->msg);
  51. return 0;
  52. }
  53. int adis_update_scan_mode(struct iio_dev *indio_dev,
  54. const unsigned long *scan_mask)
  55. {
  56. struct adis *adis = iio_device_get_drvdata(indio_dev);
  57. const struct iio_chan_spec *chan;
  58. unsigned int scan_count;
  59. unsigned int i, j;
  60. __be16 *tx, *rx;
  61. kfree(adis->xfer);
  62. kfree(adis->buffer);
  63. if (adis->data->burst_len)
  64. return adis_update_scan_mode_burst(indio_dev, scan_mask);
  65. scan_count = indio_dev->scan_bytes / 2;
  66. adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
  67. if (!adis->xfer)
  68. return -ENOMEM;
  69. adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
  70. if (!adis->buffer) {
  71. kfree(adis->xfer);
  72. adis->xfer = NULL;
  73. return -ENOMEM;
  74. }
  75. rx = adis->buffer;
  76. tx = rx + scan_count;
  77. spi_message_init(&adis->msg);
  78. for (j = 0; j <= scan_count; j++) {
  79. adis->xfer[j].bits_per_word = 8;
  80. if (j != scan_count)
  81. adis->xfer[j].cs_change = 1;
  82. adis->xfer[j].len = 2;
  83. adis->xfer[j].delay.value = adis->data->read_delay;
  84. adis->xfer[j].delay.unit = SPI_DELAY_UNIT_USECS;
  85. if (j < scan_count)
  86. adis->xfer[j].tx_buf = &tx[j];
  87. if (j >= 1)
  88. adis->xfer[j].rx_buf = &rx[j - 1];
  89. spi_message_add_tail(&adis->xfer[j], &adis->msg);
  90. }
  91. chan = indio_dev->channels;
  92. for (i = 0; i < indio_dev->num_channels; i++, chan++) {
  93. if (!test_bit(chan->scan_index, scan_mask))
  94. continue;
  95. if (chan->scan_type.storagebits == 32)
  96. *tx++ = cpu_to_be16((chan->address + 2) << 8);
  97. *tx++ = cpu_to_be16(chan->address << 8);
  98. }
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(adis_update_scan_mode);
  102. static irqreturn_t adis_trigger_handler(int irq, void *p)
  103. {
  104. struct iio_poll_func *pf = p;
  105. struct iio_dev *indio_dev = pf->indio_dev;
  106. struct adis *adis = iio_device_get_drvdata(indio_dev);
  107. int ret;
  108. if (adis->data->has_paging) {
  109. mutex_lock(&adis->state_lock);
  110. if (adis->current_page != 0) {
  111. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  112. adis->tx[1] = 0;
  113. spi_write(adis->spi, adis->tx, 2);
  114. }
  115. }
  116. ret = spi_sync(adis->spi, &adis->msg);
  117. if (ret)
  118. dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
  119. if (adis->data->has_paging) {
  120. adis->current_page = 0;
  121. mutex_unlock(&adis->state_lock);
  122. }
  123. iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
  124. pf->timestamp);
  125. iio_trigger_notify_done(indio_dev->trig);
  126. return IRQ_HANDLED;
  127. }
  128. static void adis_buffer_cleanup(void *arg)
  129. {
  130. struct adis *adis = arg;
  131. kfree(adis->buffer);
  132. kfree(adis->xfer);
  133. }
  134. /**
  135. * devm_adis_setup_buffer_and_trigger() - Sets up buffer and trigger for
  136. * the managed adis device
  137. * @adis: The adis device
  138. * @indio_dev: The IIO device
  139. * @trigger_handler: Optional trigger handler, may be NULL.
  140. *
  141. * Returns 0 on success, a negative error code otherwise.
  142. *
  143. * This function sets up the buffer and trigger for a adis devices. If
  144. * 'trigger_handler' is NULL the default trigger handler will be used. The
  145. * default trigger handler will simply read the registers assigned to the
  146. * currently active channels.
  147. */
  148. int
  149. devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
  150. irq_handler_t trigger_handler)
  151. {
  152. int ret;
  153. if (!trigger_handler)
  154. trigger_handler = adis_trigger_handler;
  155. ret = devm_iio_triggered_buffer_setup(&adis->spi->dev, indio_dev,
  156. &iio_pollfunc_store_time,
  157. trigger_handler, NULL);
  158. if (ret)
  159. return ret;
  160. if (adis->spi->irq) {
  161. ret = devm_adis_probe_trigger(adis, indio_dev);
  162. if (ret)
  163. return ret;
  164. }
  165. return devm_add_action_or_reset(&adis->spi->dev, adis_buffer_cleanup,
  166. adis);
  167. }
  168. EXPORT_SYMBOL_GPL(devm_adis_setup_buffer_and_trigger);