iio_simple_dummy_buffer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  29. * iio_simple_dummy_trigger_h() - the trigger handler function
  30. * @irq: the interrupt number
  31. * @p: private data - always a pointer to the poll func.
  32. *
  33. * This is the guts of buffered capture. On a trigger event occurring,
  34. * if the pollfunc is attached then this handler is called as a threaded
  35. * interrupt (and hence may sleep). It is responsible for grabbing data
  36. * from the device and pushing it into the associated buffer.
  37. */
  38. static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
  39. {
  40. struct iio_poll_func *pf = p;
  41. struct iio_dev *indio_dev = pf->indio_dev;
  42. int i = 0, j;
  43. u16 *data;
  44. data = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
  45. if (!data)
  46. goto done;
  47. /*
  48. * Three common options here:
  49. * hardware scans:
  50. * certain combinations of channels make up a fast read. The capture
  51. * will consist of all of them. Hence we just call the grab data
  52. * function and fill the buffer without processing.
  53. * software scans:
  54. * can be considered to be random access so efficient reading is just
  55. * a case of minimal bus transactions.
  56. * software culled hardware scans:
  57. * occasionally a driver may process the nearest hardware scan to avoid
  58. * storing elements that are not desired. This is the fiddliest option
  59. * by far.
  60. * Here let's pretend we have random access. And the values are in the
  61. * constant table fakedata.
  62. */
  63. iio_for_each_active_channel(indio_dev, j)
  64. data[i++] = fakedata[j];
  65. iio_push_to_buffers_with_timestamp(indio_dev, data,
  66. iio_get_time_ns(indio_dev));
  67. kfree(data);
  68. done:
  69. /*
  70. * Tell the core we are done with this trigger and ready for the
  71. * next one.
  72. */
  73. iio_trigger_notify_done(indio_dev->trig);
  74. return IRQ_HANDLED;
  75. }
  76. static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
  77. };
  78. int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
  79. {
  80. return iio_triggered_buffer_setup(indio_dev, NULL,
  81. iio_simple_dummy_trigger_h,
  82. &iio_simple_dummy_buffer_setup_ops);
  83. }
  84. /**
  85. * iio_simple_dummy_unconfigure_buffer() - release buffer resources
  86. * @indio_dev: device instance state
  87. */
  88. void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
  89. {
  90. iio_triggered_buffer_cleanup(indio_dev);
  91. }