industrialio-triggered-event.c 2.3 KB

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