industrialio-triggered-event.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Cogent Embedded, Inc.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/export.h>
  7. #include <linux/module.h>
  8. #include <linux/iio/iio.h>
  9. #include <linux/iio/triggered_event.h>
  10. #include <linux/iio/trigger_consumer.h>
  11. /**
  12. * iio_triggered_event_setup() - Setup pollfunc_event for triggered event
  13. * @indio_dev: IIO device structure
  14. * @h: Function which will be used as pollfunc_event top half
  15. * @thread: Function which will be used as pollfunc_event bottom half
  16. *
  17. * This function combines some common tasks which will normally be performed
  18. * when setting up a triggered event. It will allocate the pollfunc_event and
  19. * set mode to use it for triggered event.
  20. *
  21. * Before calling this function the indio_dev structure should already be
  22. * completely initialized, but not yet registered. In practice this means that
  23. * this function should be called right before iio_device_register().
  24. *
  25. * To free the resources allocated by this function call
  26. * iio_triggered_event_cleanup().
  27. */
  28. int iio_triggered_event_setup(struct iio_dev *indio_dev,
  29. irqreturn_t (*h)(int irq, void *p),
  30. irqreturn_t (*thread)(int irq, void *p))
  31. {
  32. indio_dev->pollfunc_event = iio_alloc_pollfunc(h,
  33. thread,
  34. IRQF_ONESHOT,
  35. indio_dev,
  36. "%s_consumer%d",
  37. indio_dev->name,
  38. indio_dev->id);
  39. if (indio_dev->pollfunc_event == NULL)
  40. return -ENOMEM;
  41. /* Flag that events polling is possible */
  42. indio_dev->modes |= INDIO_EVENT_TRIGGERED;
  43. return 0;
  44. }
  45. EXPORT_SYMBOL(iio_triggered_event_setup);
  46. /**
  47. * iio_triggered_event_cleanup() - Free resources allocated by iio_triggered_event_setup()
  48. * @indio_dev: IIO device structure
  49. */
  50. void iio_triggered_event_cleanup(struct iio_dev *indio_dev)
  51. {
  52. indio_dev->modes &= ~INDIO_EVENT_TRIGGERED;
  53. iio_dealloc_pollfunc(indio_dev->pollfunc_event);
  54. }
  55. EXPORT_SYMBOL(iio_triggered_event_cleanup);
  56. MODULE_AUTHOR("Vladimir Barinov");
  57. MODULE_DESCRIPTION("IIO helper functions for setting up triggered events");
  58. MODULE_LICENSE("GPL");