Linux Audio

Check our new training course

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