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