iio_simple_dummy_buffer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011 Jonathan Cameron
  4. *
  5. * Buffer handling elements of industrial I/O reference driver.
  6. * Uses the kfifo buffer.
  7. *
  8. * To test without hardware use the sysfs trigger.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/bitmap.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/trigger_consumer.h>
  19. #include <linux/iio/triggered_buffer.h>
  20. #include "iio_simple_dummy.h"
  21. /* Some fake data */
  22. static const s16 fakedata[] = {
  23. [DUMMY_INDEX_VOLTAGE_0] = 7,
  24. [DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33,
  25. [DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2,
  26. [DUMMY_INDEX_ACCELX] = 344,
  27. };
  28. struct dummy_scan {
  29. s16 data[ARRAY_SIZE(fakedata)];
  30. aligned_s64 timestamp;
  31. };
  32. /**
  33. * iio_simple_dummy_trigger_h() - the trigger handler function
  34. * @irq: the interrupt number
  35. * @p: private data - always a pointer to the poll func.
  36. *
  37. * This is the guts of buffered capture. On a trigger event occurring,
  38. * if the pollfunc is attached then this handler is called as a threaded
  39. * interrupt (and hence may sleep). It is responsible for grabbing data
  40. * from the device and pushing it into the associated buffer.
  41. */
  42. static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
  43. {
  44. struct iio_poll_func *pf = p;
  45. struct iio_dev *indio_dev = pf->indio_dev;
  46. struct dummy_scan *scan;
  47. int i = 0, j;
  48. /*
  49. * Note that some buses such as SPI require DMA safe buffers which
  50. * cannot be on the stack. Two easy ways to do this:
  51. * - Local kzalloc (as done here)
  52. * - A buffer at the end of the structure accessed via iio_priv()
  53. * that is marked __aligned(IIO_DMA_MINALIGN).
  54. */
  55. scan = kzalloc_obj(*scan);
  56. if (!scan)
  57. goto done;
  58. /*
  59. * Three common options here:
  60. * hardware scans:
  61. * certain combinations of channels make up a fast read. The capture
  62. * will consist of all of them. Hence we just call the grab data
  63. * function and fill the buffer without processing.
  64. * software scans:
  65. * can be considered to be random access so efficient reading is just
  66. * a case of minimal bus transactions.
  67. * software culled hardware scans:
  68. * occasionally a driver may process the nearest hardware scan to avoid
  69. * storing elements that are not desired. This is the fiddliest option
  70. * by far.
  71. * Here let's pretend we have random access. And the values are in the
  72. * constant table fakedata.
  73. */
  74. iio_for_each_active_channel(indio_dev, j)
  75. scan->data[i++] = fakedata[j];
  76. iio_push_to_buffers_with_ts(indio_dev, scan, sizeof(*scan),
  77. iio_get_time_ns(indio_dev));
  78. kfree(scan);
  79. done:
  80. /*
  81. * Tell the core we are done with this trigger and ready for the
  82. * next one.
  83. */
  84. iio_trigger_notify_done(indio_dev->trig);
  85. return IRQ_HANDLED;
  86. }
  87. static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
  88. };
  89. int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
  90. {
  91. return iio_triggered_buffer_setup(indio_dev, NULL,
  92. iio_simple_dummy_trigger_h,
  93. &iio_simple_dummy_buffer_setup_ops);
  94. }
  95. /**
  96. * iio_simple_dummy_unconfigure_buffer() - release buffer resources
  97. * @indio_dev: device instance state
  98. */
  99. void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
  100. {
  101. iio_triggered_buffer_cleanup(indio_dev);
  102. }