devres.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/device.h>
  5. #include <linux/gfp.h>
  6. #include <linux/irq.h>
  7. #include "internals.h"
  8. /*
  9. * Device resource management aware IRQ request/free implementation.
  10. */
  11. struct irq_devres {
  12. unsigned int irq;
  13. void *dev_id;
  14. };
  15. static void devm_irq_release(struct device *dev, void *res)
  16. {
  17. struct irq_devres *this = res;
  18. free_irq(this->irq, this->dev_id);
  19. }
  20. static int devm_irq_match(struct device *dev, void *res, void *data)
  21. {
  22. struct irq_devres *this = res, *match = data;
  23. return this->irq == match->irq && this->dev_id == match->dev_id;
  24. }
  25. /**
  26. * devm_request_threaded_irq - allocate an interrupt line for a managed device
  27. * @dev: device to request interrupt for
  28. * @irq: Interrupt line to allocate
  29. * @handler: Function to be called when the IRQ occurs
  30. * @thread_fn: function to be called in a threaded interrupt context. NULL
  31. * for devices which handle everything in @handler
  32. * @irqflags: Interrupt type flags
  33. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  34. * @dev_id: A cookie passed back to the handler function
  35. *
  36. * Except for the extra @dev argument, this function takes the
  37. * same arguments and performs the same function as
  38. * request_threaded_irq(). IRQs requested with this function will be
  39. * automatically freed on driver detach.
  40. *
  41. * If an IRQ allocated with this function needs to be freed
  42. * separately, devm_free_irq() must be used.
  43. */
  44. int devm_request_threaded_irq(struct device *dev, unsigned int irq,
  45. irq_handler_t handler, irq_handler_t thread_fn,
  46. unsigned long irqflags, const char *devname,
  47. void *dev_id)
  48. {
  49. struct irq_devres *dr;
  50. int rc;
  51. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  52. GFP_KERNEL);
  53. if (!dr)
  54. return -ENOMEM;
  55. if (!devname)
  56. devname = dev_name(dev);
  57. rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname,
  58. dev_id);
  59. if (rc) {
  60. devres_free(dr);
  61. return rc;
  62. }
  63. dr->irq = irq;
  64. dr->dev_id = dev_id;
  65. devres_add(dev, dr);
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(devm_request_threaded_irq);
  69. /**
  70. * devm_request_any_context_irq - allocate an interrupt line for a managed device
  71. * @dev: device to request interrupt for
  72. * @irq: Interrupt line to allocate
  73. * @handler: Function to be called when the IRQ occurs
  74. * @irqflags: Interrupt type flags
  75. * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
  76. * @dev_id: A cookie passed back to the handler function
  77. *
  78. * Except for the extra @dev argument, this function takes the
  79. * same arguments and performs the same function as
  80. * request_any_context_irq(). IRQs requested with this function will be
  81. * automatically freed on driver detach.
  82. *
  83. * If an IRQ allocated with this function needs to be freed
  84. * separately, devm_free_irq() must be used.
  85. */
  86. int devm_request_any_context_irq(struct device *dev, unsigned int irq,
  87. irq_handler_t handler, unsigned long irqflags,
  88. const char *devname, void *dev_id)
  89. {
  90. struct irq_devres *dr;
  91. int rc;
  92. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  93. GFP_KERNEL);
  94. if (!dr)
  95. return -ENOMEM;
  96. if (!devname)
  97. devname = dev_name(dev);
  98. rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id);
  99. if (rc < 0) {
  100. devres_free(dr);
  101. return rc;
  102. }
  103. dr->irq = irq;
  104. dr->dev_id = dev_id;
  105. devres_add(dev, dr);
  106. return rc;
  107. }
  108. EXPORT_SYMBOL(devm_request_any_context_irq);
  109. /**
  110. * devm_free_irq - free an interrupt
  111. * @dev: device to free interrupt for
  112. * @irq: Interrupt line to free
  113. * @dev_id: Device identity to free
  114. *
  115. * Except for the extra @dev argument, this function takes the
  116. * same arguments and performs the same function as free_irq().
  117. * This function instead of free_irq() should be used to manually
  118. * free IRQs allocated with devm_request_irq().
  119. */
  120. void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
  121. {
  122. struct irq_devres match_data = { irq, dev_id };
  123. WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
  124. &match_data));
  125. free_irq(irq, dev_id);
  126. }
  127. EXPORT_SYMBOL(devm_free_irq);
  128. struct irq_desc_devres {
  129. unsigned int from;
  130. unsigned int cnt;
  131. };
  132. static void devm_irq_desc_release(struct device *dev, void *res)
  133. {
  134. struct irq_desc_devres *this = res;
  135. irq_free_descs(this->from, this->cnt);
  136. }
  137. /**
  138. * __devm_irq_alloc_descs - Allocate and initialize a range of irq descriptors
  139. * for a managed device
  140. * @dev: Device to allocate the descriptors for
  141. * @irq: Allocate for specific irq number if irq >= 0
  142. * @from: Start the search from this irq number
  143. * @cnt: Number of consecutive irqs to allocate
  144. * @node: Preferred node on which the irq descriptor should be allocated
  145. * @owner: Owning module (can be NULL)
  146. * @affinity: Optional pointer to an irq_affinity_desc array of size @cnt
  147. * which hints where the irq descriptors should be allocated
  148. * and which default affinities to use
  149. *
  150. * Returns the first irq number or error code.
  151. *
  152. * Note: Use the provided wrappers (devm_irq_alloc_desc*) for simplicity.
  153. */
  154. int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
  155. unsigned int cnt, int node, struct module *owner,
  156. const struct irq_affinity_desc *affinity)
  157. {
  158. struct irq_desc_devres *dr;
  159. int base;
  160. dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL);
  161. if (!dr)
  162. return -ENOMEM;
  163. base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity);
  164. if (base < 0) {
  165. devres_free(dr);
  166. return base;
  167. }
  168. dr->from = base;
  169. dr->cnt = cnt;
  170. devres_add(dev, dr);
  171. return base;
  172. }
  173. EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs);
  174. #ifdef CONFIG_GENERIC_IRQ_CHIP
  175. /**
  176. * devm_irq_alloc_generic_chip - Allocate and initialize a generic chip
  177. * for a managed device
  178. * @dev: Device to allocate the generic chip for
  179. * @name: Name of the irq chip
  180. * @num_ct: Number of irq_chip_type instances associated with this
  181. * @irq_base: Interrupt base nr for this chip
  182. * @reg_base: Register base address (virtual)
  183. * @handler: Default flow handler associated with this chip
  184. *
  185. * Returns an initialized irq_chip_generic structure. The chip defaults
  186. * to the primary (index 0) irq_chip_type and @handler
  187. */
  188. struct irq_chip_generic *
  189. devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct,
  190. unsigned int irq_base, void __iomem *reg_base,
  191. irq_flow_handler_t handler)
  192. {
  193. struct irq_chip_generic *gc;
  194. gc = devm_kzalloc(dev, struct_size(gc, chip_types, num_ct), GFP_KERNEL);
  195. if (gc)
  196. irq_init_generic_chip(gc, name, num_ct,
  197. irq_base, reg_base, handler);
  198. return gc;
  199. }
  200. EXPORT_SYMBOL_GPL(devm_irq_alloc_generic_chip);
  201. struct irq_generic_chip_devres {
  202. struct irq_chip_generic *gc;
  203. u32 msk;
  204. unsigned int clr;
  205. unsigned int set;
  206. };
  207. static void devm_irq_remove_generic_chip(struct device *dev, void *res)
  208. {
  209. struct irq_generic_chip_devres *this = res;
  210. irq_remove_generic_chip(this->gc, this->msk, this->clr, this->set);
  211. }
  212. /**
  213. * devm_irq_setup_generic_chip - Setup a range of interrupts with a generic
  214. * chip for a managed device
  215. *
  216. * @dev: Device to setup the generic chip for
  217. * @gc: Generic irq chip holding all data
  218. * @msk: Bitmask holding the irqs to initialize relative to gc->irq_base
  219. * @flags: Flags for initialization
  220. * @clr: IRQ_* bits to clear
  221. * @set: IRQ_* bits to set
  222. *
  223. * Set up max. 32 interrupts starting from gc->irq_base. Note, this
  224. * initializes all interrupts to the primary irq_chip_type and its
  225. * associated handler.
  226. */
  227. int devm_irq_setup_generic_chip(struct device *dev, struct irq_chip_generic *gc,
  228. u32 msk, enum irq_gc_flags flags,
  229. unsigned int clr, unsigned int set)
  230. {
  231. struct irq_generic_chip_devres *dr;
  232. dr = devres_alloc(devm_irq_remove_generic_chip,
  233. sizeof(*dr), GFP_KERNEL);
  234. if (!dr)
  235. return -ENOMEM;
  236. irq_setup_generic_chip(gc, msk, flags, clr, set);
  237. dr->gc = gc;
  238. dr->msk = msk;
  239. dr->clr = clr;
  240. dr->set = set;
  241. devres_add(dev, dr);
  242. return 0;
  243. }
  244. EXPORT_SYMBOL_GPL(devm_irq_setup_generic_chip);
  245. #endif /* CONFIG_GENERIC_IRQ_CHIP */