trigger.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* The industrial I/O core, trigger handling functions
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  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/irq.h>
  10. #include <linux/module.h>
  11. #include <linux/atomic.h>
  12. #ifndef _IIO_TRIGGER_H_
  13. #define _IIO_TRIGGER_H_
  14. #ifdef CONFIG_IIO_TRIGGER
  15. struct iio_subirq {
  16. bool enabled;
  17. };
  18. struct iio_dev;
  19. struct iio_trigger;
  20. /**
  21. * struct iio_trigger_ops - operations structure for an iio_trigger.
  22. * @owner: used to monitor usage count of the trigger.
  23. * @set_trigger_state: switch on/off the trigger on demand
  24. * @try_reenable: function to reenable the trigger when the
  25. * use count is zero (may be NULL)
  26. * @validate_device: function to validate the device when the
  27. * current trigger gets changed.
  28. *
  29. * This is typically static const within a driver and shared by
  30. * instances of a given device.
  31. **/
  32. struct iio_trigger_ops {
  33. struct module *owner;
  34. int (*set_trigger_state)(struct iio_trigger *trig, bool state);
  35. int (*try_reenable)(struct iio_trigger *trig);
  36. int (*validate_device)(struct iio_trigger *trig,
  37. struct iio_dev *indio_dev);
  38. };
  39. /**
  40. * struct iio_trigger - industrial I/O trigger device
  41. * @ops: [DRIVER] operations structure
  42. * @id: [INTERN] unique id number
  43. * @name: [DRIVER] unique name
  44. * @dev: [DRIVER] associated device (if relevant)
  45. * @list: [INTERN] used in maintenance of global trigger list
  46. * @alloc_list: [DRIVER] used for driver specific trigger list
  47. * @use_count: use count for the trigger
  48. * @subirq_chip: [INTERN] associate 'virtual' irq chip.
  49. * @subirq_base: [INTERN] base number for irqs provided by trigger.
  50. * @subirqs: [INTERN] information about the 'child' irqs.
  51. * @pool: [INTERN] bitmap of irqs currently in use.
  52. * @pool_lock: [INTERN] protection of the irq pool.
  53. * @attached_own_device:[INTERN] if we are using our own device as trigger,
  54. * i.e. if we registered a poll function to the same
  55. * device as the one providing the trigger.
  56. **/
  57. struct iio_trigger {
  58. const struct iio_trigger_ops *ops;
  59. int id;
  60. const char *name;
  61. struct device dev;
  62. struct list_head list;
  63. struct list_head alloc_list;
  64. atomic_t use_count;
  65. struct irq_chip subirq_chip;
  66. int subirq_base;
  67. struct iio_subirq subirqs[CONFIG_IIO_CONSUMERS_PER_TRIGGER];
  68. unsigned long pool[BITS_TO_LONGS(CONFIG_IIO_CONSUMERS_PER_TRIGGER)];
  69. struct mutex pool_lock;
  70. bool attached_own_device;
  71. };
  72. static inline struct iio_trigger *to_iio_trigger(struct device *d)
  73. {
  74. return container_of(d, struct iio_trigger, dev);
  75. }
  76. static inline void iio_trigger_put(struct iio_trigger *trig)
  77. {
  78. module_put(trig->ops->owner);
  79. put_device(&trig->dev);
  80. }
  81. static inline struct iio_trigger *iio_trigger_get(struct iio_trigger *trig)
  82. {
  83. get_device(&trig->dev);
  84. __module_get(trig->ops->owner);
  85. return trig;
  86. }
  87. /**
  88. * iio_device_set_drvdata() - Set trigger driver data
  89. * @trig: IIO trigger structure
  90. * @data: Driver specific data
  91. *
  92. * Allows to attach an arbitrary pointer to an IIO trigger, which can later be
  93. * retrieved by iio_trigger_get_drvdata().
  94. */
  95. static inline void iio_trigger_set_drvdata(struct iio_trigger *trig, void *data)
  96. {
  97. dev_set_drvdata(&trig->dev, data);
  98. }
  99. /**
  100. * iio_trigger_get_drvdata() - Get trigger driver data
  101. * @trig: IIO trigger structure
  102. *
  103. * Returns the data previously set with iio_trigger_set_drvdata()
  104. */
  105. static inline void *iio_trigger_get_drvdata(struct iio_trigger *trig)
  106. {
  107. return dev_get_drvdata(&trig->dev);
  108. }
  109. /**
  110. * iio_trigger_register() - register a trigger with the IIO core
  111. * @trig_info: trigger to be registered
  112. **/
  113. int iio_trigger_register(struct iio_trigger *trig_info);
  114. int devm_iio_trigger_register(struct device *dev,
  115. struct iio_trigger *trig_info);
  116. /**
  117. * iio_trigger_unregister() - unregister a trigger from the core
  118. * @trig_info: trigger to be unregistered
  119. **/
  120. void iio_trigger_unregister(struct iio_trigger *trig_info);
  121. void devm_iio_trigger_unregister(struct device *dev,
  122. struct iio_trigger *trig_info);
  123. /**
  124. * iio_trigger_set_immutable() - set an immutable trigger on destination
  125. *
  126. * @indio_dev - IIO device structure containing the device
  127. * @trig - trigger to assign to device
  128. *
  129. **/
  130. int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig);
  131. /**
  132. * iio_trigger_poll() - called on a trigger occurring
  133. * @trig: trigger which occurred
  134. *
  135. * Typically called in relevant hardware interrupt handler.
  136. **/
  137. void iio_trigger_poll(struct iio_trigger *trig);
  138. void iio_trigger_poll_chained(struct iio_trigger *trig);
  139. irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private);
  140. __printf(1, 2) struct iio_trigger *iio_trigger_alloc(const char *fmt, ...);
  141. void iio_trigger_free(struct iio_trigger *trig);
  142. /**
  143. * iio_trigger_using_own() - tells us if we use our own HW trigger ourselves
  144. * @indio_dev: device to check
  145. */
  146. bool iio_trigger_using_own(struct iio_dev *indio_dev);
  147. #else
  148. struct iio_trigger;
  149. struct iio_trigger_ops;
  150. #endif
  151. #endif /* _IIO_TRIGGER_H_ */