industrialio-triggered-buffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2012 Analog Devices, Inc.
  3. * Author: Lars-Peter Clausen <lars@metafoo.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/module.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/buffer.h>
  14. #include <linux/iio/kfifo_buf.h>
  15. #include <linux/iio/triggered_buffer.h>
  16. #include <linux/iio/trigger_consumer.h>
  17. static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
  18. .postenable = &iio_triggered_buffer_postenable,
  19. .predisable = &iio_triggered_buffer_predisable,
  20. };
  21. /**
  22. * iio_triggered_buffer_setup() - Setup triggered buffer and pollfunc
  23. * @indio_dev: IIO device structure
  24. * @h: Function which will be used as pollfunc top half
  25. * @thread: Function which will be used as pollfunc bottom half
  26. * @setup_ops: Buffer setup functions to use for this device.
  27. * If NULL the default setup functions for triggered
  28. * buffers will be used.
  29. *
  30. * This function combines some common tasks which will normally be performed
  31. * when setting up a triggered buffer. It will allocate the buffer and the
  32. * pollfunc.
  33. *
  34. * Before calling this function the indio_dev structure should already be
  35. * completely initialized, but not yet registered. In practice this means that
  36. * this function should be called right before iio_device_register().
  37. *
  38. * To free the resources allocated by this function call
  39. * iio_triggered_buffer_cleanup().
  40. */
  41. int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
  42. irqreturn_t (*h)(int irq, void *p),
  43. irqreturn_t (*thread)(int irq, void *p),
  44. const struct iio_buffer_setup_ops *setup_ops)
  45. {
  46. struct iio_buffer *buffer;
  47. int ret;
  48. buffer = iio_kfifo_allocate();
  49. if (!buffer) {
  50. ret = -ENOMEM;
  51. goto error_ret;
  52. }
  53. iio_device_attach_buffer(indio_dev, buffer);
  54. indio_dev->pollfunc = iio_alloc_pollfunc(h,
  55. thread,
  56. IRQF_ONESHOT,
  57. indio_dev,
  58. "%s_consumer%d",
  59. indio_dev->name,
  60. indio_dev->id);
  61. if (indio_dev->pollfunc == NULL) {
  62. ret = -ENOMEM;
  63. goto error_kfifo_free;
  64. }
  65. /* Ring buffer functions - here trigger setup related */
  66. if (setup_ops)
  67. indio_dev->setup_ops = setup_ops;
  68. else
  69. indio_dev->setup_ops = &iio_triggered_buffer_setup_ops;
  70. /* Flag that polled ring buffering is possible */
  71. indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
  72. return 0;
  73. error_kfifo_free:
  74. iio_kfifo_free(indio_dev->buffer);
  75. error_ret:
  76. return ret;
  77. }
  78. EXPORT_SYMBOL(iio_triggered_buffer_setup);
  79. /**
  80. * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup()
  81. * @indio_dev: IIO device structure
  82. */
  83. void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
  84. {
  85. iio_dealloc_pollfunc(indio_dev->pollfunc);
  86. iio_kfifo_free(indio_dev->buffer);
  87. }
  88. EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
  89. static void devm_iio_triggered_buffer_clean(struct device *dev, void *res)
  90. {
  91. iio_triggered_buffer_cleanup(*(struct iio_dev **)res);
  92. }
  93. int devm_iio_triggered_buffer_setup(struct device *dev,
  94. struct iio_dev *indio_dev,
  95. irqreturn_t (*h)(int irq, void *p),
  96. irqreturn_t (*thread)(int irq, void *p),
  97. const struct iio_buffer_setup_ops *ops)
  98. {
  99. struct iio_dev **ptr;
  100. int ret;
  101. ptr = devres_alloc(devm_iio_triggered_buffer_clean, sizeof(*ptr),
  102. GFP_KERNEL);
  103. if (!ptr)
  104. return -ENOMEM;
  105. *ptr = indio_dev;
  106. ret = iio_triggered_buffer_setup(indio_dev, h, thread, ops);
  107. if (!ret)
  108. devres_add(dev, ptr);
  109. else
  110. devres_free(ptr);
  111. return ret;
  112. }
  113. EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup);
  114. void devm_iio_triggered_buffer_cleanup(struct device *dev,
  115. struct iio_dev *indio_dev)
  116. {
  117. int rc;
  118. rc = devres_release(dev, devm_iio_triggered_buffer_clean,
  119. devm_iio_device_match, indio_dev);
  120. WARN_ON(rc);
  121. }
  122. EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_cleanup);
  123. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  124. MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
  125. MODULE_LICENSE("GPL");