trigger.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * @set_trigger_state: switch on/off the trigger on demand
  23. * @try_reenable: function to reenable the trigger when the
  24. * use count is zero (may be NULL)
  25. * @validate_device: function to validate the device when the
  26. * current trigger gets changed.
  27. *
  28. * This is typically static const within a driver and shared by
  29. * instances of a given device.
  30. **/
  31. struct iio_trigger_ops {
  32. int (*set_trigger_state)(struct iio_trigger *trig, bool state);
  33. int (*try_reenable)(struct iio_trigger *trig);
  34. int (*validate_device)(struct iio_trigger *trig,
  35. struct iio_dev *indio_dev);
  36. };
  37. /**
  38. * struct iio_trigger - industrial I/O trigger device
  39. * @ops: [DRIVER] operations structure
  40. * @owner: [INTERN] owner of this driver module
  41. * @id: [INTERN] unique id number
  42. * @name: [DRIVER] unique name
  43. * @dev: [DRIVER] associated device (if relevant)
  44. * @list: [INTERN] used in maintenance of global trigger list
  45. * @alloc_list: [DRIVER] used for driver specific trigger list
  46. * @use_count: [INTERN] use count for the trigger.
  47. * @subirq_chip: [INTERN] associate 'virtual' irq chip.
  48. * @subirq_base: [INTERN] base number for irqs provided by trigger.
  49. * @subirqs: [INTERN] information about the 'child' irqs.
  50. * @pool: [INTERN] bitmap of irqs currently in use.
  51. * @pool_lock: [INTERN] protection of the irq pool.
  52. * @attached_own_device:[INTERN] if we are using our own device as trigger,
  53. * i.e. if we registered a poll function to the same
  54. * device as the one providing the trigger.
  55. **/
  56. struct iio_trigger {
  57. const struct iio_trigger_ops *ops;
  58. struct module *owner;
  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->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->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. #define iio_trigger_register(trig_info) \
  114. __iio_trigger_register((trig_info), THIS_MODULE)
  115. int __iio_trigger_register(struct iio_trigger *trig_info,
  116. struct module *this_mod);
  117. #define devm_iio_trigger_register(dev, trig_info) \
  118. __devm_iio_trigger_register((dev), (trig_info), THIS_MODULE)
  119. int __devm_iio_trigger_register(struct device *dev,
  120. struct iio_trigger *trig_info,
  121. struct module *this_mod);
  122. /**
  123. * iio_trigger_unregister() - unregister a trigger from the core
  124. * @trig_info: trigger to be unregistered
  125. **/
  126. void iio_trigger_unregister(struct iio_trigger *trig_info);
  127. void devm_iio_trigger_unregister(struct device *dev,
  128. struct iio_trigger *trig_info);
  129. /**
  130. * iio_trigger_set_immutable() - set an immutable trigger on destination
  131. *
  132. * @indio_dev: IIO device structure containing the device
  133. * @trig: trigger to assign to device
  134. *
  135. **/
  136. int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig);
  137. /**
  138. * iio_trigger_poll() - called on a trigger occurring
  139. * @trig: trigger which occurred
  140. *
  141. * Typically called in relevant hardware interrupt handler.
  142. **/
  143. void iio_trigger_poll(struct iio_trigger *trig);
  144. void iio_trigger_poll_chained(struct iio_trigger *trig);
  145. irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private);
  146. __printf(1, 2) struct iio_trigger *iio_trigger_alloc(const char *fmt, ...);
  147. void iio_trigger_free(struct iio_trigger *trig);
  148. /**
  149. * iio_trigger_using_own() - tells us if we use our own HW trigger ourselves
  150. * @indio_dev: device to check
  151. */
  152. bool iio_trigger_using_own(struct iio_dev *indio_dev);
  153. int iio_trigger_validate_own_device(struct iio_trigger *trig,
  154. struct iio_dev *indio_dev);
  155. #else
  156. struct iio_trigger;
  157. struct iio_trigger_ops;
  158. #endif
  159. #endif /* _IIO_TRIGGER_H_ */