adis_buffer.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <[email protected]>
  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. if (adis->data->burst_max_speed_hz)
  46. adis->xfer[0].speed_hz = adis->data->burst_max_speed_hz;
  47. adis->xfer[1].rx_buf = adis->buffer;
  48. adis->xfer[1].bits_per_word = 8;
  49. adis->xfer[1].len = burst_length;
  50. if (adis->data->burst_max_speed_hz)
  51. adis->xfer[1].speed_hz = adis->data->burst_max_speed_hz;
  52. spi_message_init(&adis->msg);
  53. spi_message_add_tail(&adis->xfer[0], &adis->msg);
  54. spi_message_add_tail(&adis->xfer[1], &adis->msg);
  55. return 0;
  56. }
  57. int adis_update_scan_mode(struct iio_dev *indio_dev,
  58. const unsigned long *scan_mask)
  59. {
  60. struct adis *adis = iio_device_get_drvdata(indio_dev);
  61. const struct iio_chan_spec *chan;
  62. unsigned int scan_count;
  63. unsigned int i, j;
  64. __be16 *tx, *rx;
  65. kfree(adis->xfer);
  66. kfree(adis->buffer);
  67. if (adis->data->burst_len)
  68. return adis_update_scan_mode_burst(indio_dev, scan_mask);
  69. scan_count = indio_dev->scan_bytes / 2;
  70. adis->xfer = kcalloc(scan_count + 1, sizeof(*adis->xfer), GFP_KERNEL);
  71. if (!adis->xfer)
  72. return -ENOMEM;
  73. adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
  74. if (!adis->buffer) {
  75. kfree(adis->xfer);
  76. adis->xfer = NULL;
  77. return -ENOMEM;
  78. }
  79. rx = adis->buffer;
  80. tx = rx + scan_count;
  81. spi_message_init(&adis->msg);
  82. for (j = 0; j <= scan_count; j++) {
  83. adis->xfer[j].bits_per_word = 8;
  84. if (j != scan_count)
  85. adis->xfer[j].cs_change = 1;
  86. adis->xfer[j].len = 2;
  87. adis->xfer[j].delay.value = adis->data->read_delay;
  88. adis->xfer[j].delay.unit = SPI_DELAY_UNIT_USECS;
  89. if (j < scan_count)
  90. adis->xfer[j].tx_buf = &tx[j];
  91. if (j >= 1)
  92. adis->xfer[j].rx_buf = &rx[j - 1];
  93. spi_message_add_tail(&adis->xfer[j], &adis->msg);
  94. }
  95. chan = indio_dev->channels;
  96. for (i = 0; i < indio_dev->num_channels; i++, chan++) {
  97. if (!test_bit(chan->scan_index, scan_mask))
  98. continue;
  99. if (chan->scan_type.storagebits == 32)
  100. *tx++ = cpu_to_be16((chan->address + 2) << 8);
  101. *tx++ = cpu_to_be16(chan->address << 8);
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_NS_GPL(adis_update_scan_mode, IIO_ADISLIB);
  106. static irqreturn_t adis_trigger_handler(int irq, void *p)
  107. {
  108. struct iio_poll_func *pf = p;
  109. struct iio_dev *indio_dev = pf->indio_dev;
  110. struct adis *adis = iio_device_get_drvdata(indio_dev);
  111. int ret;
  112. if (adis->data->has_paging) {
  113. mutex_lock(&adis->state_lock);
  114. if (adis->current_page != 0) {
  115. adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
  116. adis->tx[1] = 0;
  117. ret = spi_write(adis->spi, adis->tx, 2);
  118. if (ret) {
  119. dev_err(&adis->spi->dev, "Failed to change device page: %d\n", ret);
  120. mutex_unlock(&adis->state_lock);
  121. goto irq_done;
  122. }
  123. adis->current_page = 0;
  124. }
  125. }
  126. ret = spi_sync(adis->spi, &adis->msg);
  127. if (adis->data->has_paging)
  128. mutex_unlock(&adis->state_lock);
  129. if (ret) {
  130. dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
  131. goto irq_done;
  132. }
  133. iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
  134. pf->timestamp);
  135. irq_done:
  136. iio_trigger_notify_done(indio_dev->trig);
  137. return IRQ_HANDLED;
  138. }
  139. static void adis_buffer_cleanup(void *arg)
  140. {
  141. struct adis *adis = arg;
  142. kfree(adis->buffer);
  143. kfree(adis->xfer);
  144. }
  145. /**
  146. * devm_adis_setup_buffer_and_trigger() - Sets up buffer and trigger for
  147. * the managed adis device
  148. * @adis: The adis device
  149. * @indio_dev: The IIO device
  150. * @trigger_handler: Optional trigger handler, may be NULL.
  151. *
  152. * Returns 0 on success, a negative error code otherwise.
  153. *
  154. * This function sets up the buffer and trigger for a adis devices. If
  155. * 'trigger_handler' is NULL the default trigger handler will be used. The
  156. * default trigger handler will simply read the registers assigned to the
  157. * currently active channels.
  158. */
  159. int
  160. devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
  161. irq_handler_t trigger_handler)
  162. {
  163. int ret;
  164. if (!trigger_handler)
  165. trigger_handler = adis_trigger_handler;
  166. ret = devm_iio_triggered_buffer_setup(&adis->spi->dev, indio_dev,
  167. &iio_pollfunc_store_time,
  168. trigger_handler, NULL);
  169. if (ret)
  170. return ret;
  171. if (adis->spi->irq) {
  172. ret = devm_adis_probe_trigger(adis, indio_dev);
  173. if (ret)
  174. return ret;
  175. }
  176. return devm_add_action_or_reset(&adis->spi->dev, adis_buffer_cleanup,
  177. adis);
  178. }
  179. EXPORT_SYMBOL_NS_GPL(devm_adis_setup_buffer_and_trigger, IIO_ADISLIB);