adis_buffer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. 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 int adis_paging_trigger_handler(struct adis *adis)
  107. {
  108. int ret;
  109. guard(mutex)(&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. ret = spi_write(adis->spi, adis->tx, 2);
  114. if (ret) {
  115. dev_err(&adis->spi->dev, "Failed to change device page: %d\n", ret);
  116. return ret;
  117. }
  118. adis->current_page = 0;
  119. }
  120. return spi_sync(adis->spi, &adis->msg);
  121. }
  122. static irqreturn_t adis_trigger_handler(int irq, void *p)
  123. {
  124. struct iio_poll_func *pf = p;
  125. struct iio_dev *indio_dev = pf->indio_dev;
  126. struct adis *adis = iio_device_get_drvdata(indio_dev);
  127. int ret;
  128. if (adis->data->has_paging)
  129. ret = adis_paging_trigger_handler(adis);
  130. else
  131. ret = spi_sync(adis->spi, &adis->msg);
  132. if (ret) {
  133. dev_err(&adis->spi->dev, "Failed to read data: %d", ret);
  134. goto irq_done;
  135. }
  136. iio_push_to_buffers_with_timestamp(indio_dev, adis->buffer,
  137. pf->timestamp);
  138. irq_done:
  139. iio_trigger_notify_done(indio_dev->trig);
  140. return IRQ_HANDLED;
  141. }
  142. static void adis_buffer_cleanup(void *arg)
  143. {
  144. struct adis *adis = arg;
  145. kfree(adis->buffer);
  146. kfree(adis->xfer);
  147. }
  148. /**
  149. * devm_adis_setup_buffer_and_trigger_with_attrs() - Sets up buffer and trigger
  150. * for the managed adis device with buffer attributes.
  151. * @adis: The adis device
  152. * @indio_dev: The IIO device
  153. * @trigger_handler: Trigger handler: should handle the buffer readings.
  154. * @ops: Optional buffer setup functions, may be NULL.
  155. * @buffer_attrs: Extra buffer attributes.
  156. *
  157. * Returns 0 on success, a negative error code otherwise.
  158. *
  159. * This function sets up the buffer (with buffer setup functions and extra
  160. * buffer attributes) and trigger for a adis devices with buffer attributes.
  161. */
  162. int
  163. devm_adis_setup_buffer_and_trigger_with_attrs(struct adis *adis, struct iio_dev *indio_dev,
  164. irq_handler_t trigger_handler,
  165. const struct iio_buffer_setup_ops *ops,
  166. const struct iio_dev_attr **buffer_attrs)
  167. {
  168. int ret;
  169. if (!trigger_handler)
  170. trigger_handler = adis_trigger_handler;
  171. ret = devm_iio_triggered_buffer_setup_ext(&adis->spi->dev, indio_dev,
  172. &iio_pollfunc_store_time,
  173. trigger_handler,
  174. IIO_BUFFER_DIRECTION_IN,
  175. ops,
  176. buffer_attrs);
  177. if (ret)
  178. return ret;
  179. if (adis->spi->irq) {
  180. ret = devm_adis_probe_trigger(adis, indio_dev);
  181. if (ret)
  182. return ret;
  183. }
  184. return devm_add_action_or_reset(&adis->spi->dev, adis_buffer_cleanup,
  185. adis);
  186. }
  187. EXPORT_SYMBOL_NS_GPL(devm_adis_setup_buffer_and_trigger_with_attrs, IIO_ADISLIB);