blob: 1613bfd483657ceea438362c1f0c6eb69823785e [file] [log] [blame]
Al Viro5ea81762007-02-11 15:41:31 +00001#include <linux/module.h>
2#include <linux/interrupt.h>
Al Viro0af36782007-07-27 14:24:33 +01003#include <linux/device.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -07004#include <linux/gfp.h>
Bartosz Golaszewski2b5e7732017-02-10 13:23:23 +01005#include <linux/irq.h>
Al Viro5ea81762007-02-11 15:41:31 +00006
7/*
8 * Device resource management aware IRQ request/free implementation.
9 */
10struct irq_devres {
11 unsigned int irq;
12 void *dev_id;
13};
14
15static void devm_irq_release(struct device *dev, void *res)
16{
17 struct irq_devres *this = res;
18
19 free_irq(this->irq, this->dev_id);
20}
21
22static int devm_irq_match(struct device *dev, void *res, void *data)
23{
24 struct irq_devres *this = res, *match = data;
25
26 return this->irq == match->irq && this->dev_id == match->dev_id;
27}
28
29/**
Arjan van de Ven935bd5b2009-03-23 18:28:16 +010030 * devm_request_threaded_irq - allocate an interrupt line for a managed device
Al Viro5ea81762007-02-11 15:41:31 +000031 * @dev: device to request interrupt for
32 * @irq: Interrupt line to allocate
33 * @handler: Function to be called when the IRQ occurs
Arjan van de Ven935bd5b2009-03-23 18:28:16 +010034 * @thread_fn: function to be called in a threaded interrupt context. NULL
35 * for devices which handle everything in @handler
Al Viro5ea81762007-02-11 15:41:31 +000036 * @irqflags: Interrupt type flags
Heiner Kallweit899b5fb2017-02-12 16:31:44 +010037 * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
Al Viro5ea81762007-02-11 15:41:31 +000038 * @dev_id: A cookie passed back to the handler function
39 *
40 * Except for the extra @dev argument, this function takes the
41 * same arguments and performs the same function as
Emilio López307b28b2014-07-01 16:47:35 -030042 * request_threaded_irq(). IRQs requested with this function will be
Al Viro5ea81762007-02-11 15:41:31 +000043 * automatically freed on driver detach.
44 *
45 * If an IRQ allocated with this function needs to be freed
Jean Delvare5c42dc72010-02-11 15:04:36 +010046 * separately, devm_free_irq() must be used.
Al Viro5ea81762007-02-11 15:41:31 +000047 */
Arjan van de Ven935bd5b2009-03-23 18:28:16 +010048int devm_request_threaded_irq(struct device *dev, unsigned int irq,
49 irq_handler_t handler, irq_handler_t thread_fn,
50 unsigned long irqflags, const char *devname,
51 void *dev_id)
Al Viro5ea81762007-02-11 15:41:31 +000052{
53 struct irq_devres *dr;
54 int rc;
55
56 dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
57 GFP_KERNEL);
58 if (!dr)
59 return -ENOMEM;
60
Heiner Kallweit899b5fb2017-02-12 16:31:44 +010061 if (!devname)
62 devname = dev_name(dev);
63
Arjan van de Ven935bd5b2009-03-23 18:28:16 +010064 rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname,
65 dev_id);
Al Viro5ea81762007-02-11 15:41:31 +000066 if (rc) {
Tejun Heo7f30e492007-04-07 14:59:41 +090067 devres_free(dr);
Al Viro5ea81762007-02-11 15:41:31 +000068 return rc;
69 }
70
71 dr->irq = irq;
72 dr->dev_id = dev_id;
73 devres_add(dev, dr);
74
75 return 0;
76}
Arjan van de Ven935bd5b2009-03-23 18:28:16 +010077EXPORT_SYMBOL(devm_request_threaded_irq);
Al Viro5ea81762007-02-11 15:41:31 +000078
79/**
Stephen Boyd0668d302014-01-02 16:37:32 -080080 * devm_request_any_context_irq - allocate an interrupt line for a managed device
81 * @dev: device to request interrupt for
82 * @irq: Interrupt line to allocate
83 * @handler: Function to be called when the IRQ occurs
84 * @thread_fn: function to be called in a threaded interrupt context. NULL
85 * for devices which handle everything in @handler
86 * @irqflags: Interrupt type flags
Heiner Kallweit899b5fb2017-02-12 16:31:44 +010087 * @devname: An ascii name for the claiming device, dev_name(dev) if NULL
Stephen Boyd0668d302014-01-02 16:37:32 -080088 * @dev_id: A cookie passed back to the handler function
89 *
90 * Except for the extra @dev argument, this function takes the
91 * same arguments and performs the same function as
92 * request_any_context_irq(). IRQs requested with this function will be
93 * automatically freed on driver detach.
94 *
95 * If an IRQ allocated with this function needs to be freed
96 * separately, devm_free_irq() must be used.
97 */
98int devm_request_any_context_irq(struct device *dev, unsigned int irq,
99 irq_handler_t handler, unsigned long irqflags,
100 const char *devname, void *dev_id)
101{
102 struct irq_devres *dr;
103 int rc;
104
105 dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
106 GFP_KERNEL);
107 if (!dr)
108 return -ENOMEM;
109
Heiner Kallweit899b5fb2017-02-12 16:31:44 +0100110 if (!devname)
111 devname = dev_name(dev);
112
Stephen Boyd0668d302014-01-02 16:37:32 -0800113 rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id);
Axel Lin63781392015-05-11 17:02:58 +0800114 if (rc < 0) {
Stephen Boyd0668d302014-01-02 16:37:32 -0800115 devres_free(dr);
116 return rc;
117 }
118
119 dr->irq = irq;
120 dr->dev_id = dev_id;
121 devres_add(dev, dr);
122
Axel Lin63781392015-05-11 17:02:58 +0800123 return rc;
Stephen Boyd0668d302014-01-02 16:37:32 -0800124}
125EXPORT_SYMBOL(devm_request_any_context_irq);
126
127/**
Al Viro5ea81762007-02-11 15:41:31 +0000128 * devm_free_irq - free an interrupt
129 * @dev: device to free interrupt for
130 * @irq: Interrupt line to free
131 * @dev_id: Device identity to free
132 *
133 * Except for the extra @dev argument, this function takes the
134 * same arguments and performs the same function as free_irq().
135 * This function instead of free_irq() should be used to manually
Baruch Siach9ce8e492010-02-02 08:54:51 +0200136 * free IRQs allocated with devm_request_irq().
Al Viro5ea81762007-02-11 15:41:31 +0000137 */
138void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
139{
140 struct irq_devres match_data = { irq, dev_id };
141
Al Viro5ea81762007-02-11 15:41:31 +0000142 WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
143 &match_data));
Maxin B Johnae891a12011-07-25 17:12:59 -0700144 free_irq(irq, dev_id);
Al Viro5ea81762007-02-11 15:41:31 +0000145}
146EXPORT_SYMBOL(devm_free_irq);
Bartosz Golaszewski2b5e7732017-02-10 13:23:23 +0100147
148struct irq_desc_devres {
149 unsigned int from;
150 unsigned int cnt;
151};
152
153static void devm_irq_desc_release(struct device *dev, void *res)
154{
155 struct irq_desc_devres *this = res;
156
157 irq_free_descs(this->from, this->cnt);
158}
159
160/**
161 * __devm_irq_alloc_descs - Allocate and initialize a range of irq descriptors
162 * for a managed device
163 * @dev: Device to allocate the descriptors for
164 * @irq: Allocate for specific irq number if irq >= 0
165 * @from: Start the search from this irq number
166 * @cnt: Number of consecutive irqs to allocate
167 * @node: Preferred node on which the irq descriptor should be allocated
168 * @owner: Owning module (can be NULL)
169 * @affinity: Optional pointer to an affinity mask array of size @cnt
170 * which hints where the irq descriptors should be allocated
171 * and which default affinities to use
172 *
173 * Returns the first irq number or error code.
174 *
175 * Note: Use the provided wrappers (devm_irq_alloc_desc*) for simplicity.
176 */
177int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from,
178 unsigned int cnt, int node, struct module *owner,
179 const struct cpumask *affinity)
180{
181 struct irq_desc_devres *dr;
182 int base;
183
184 dr = devres_alloc(devm_irq_desc_release, sizeof(*dr), GFP_KERNEL);
185 if (!dr)
186 return -ENOMEM;
187
188 base = __irq_alloc_descs(irq, from, cnt, node, owner, affinity);
189 if (base < 0) {
190 devres_free(dr);
191 return base;
192 }
193
194 dr->from = base;
195 dr->cnt = cnt;
196 devres_add(dev, dr);
197
198 return base;
199}
200EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs);