Thomas Gleixner | 52a65ff | 2018-03-14 22:15:19 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 2 | #include <linux/module.h> |
| 3 | #include <linux/interrupt.h> |
Al Viro | 0af3678 | 2007-07-27 14:24:33 +0100 | [diff] [blame] | 4 | #include <linux/device.h> |
Robert P. J. Day | 1aeb272 | 2008-04-29 00:59:25 -0700 | [diff] [blame] | 5 | #include <linux/gfp.h> |
Bartosz Golaszewski | 2b5e773 | 2017-02-10 13:23:23 +0100 | [diff] [blame] | 6 | #include <linux/irq.h> |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 7 | |
Bartosz Golaszewski | 1c3e363 | 2017-05-31 18:06:59 +0200 | [diff] [blame] | 8 | #include "internals.h" |
| 9 | |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 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 | /** |
Arjan van de Ven | 935bd5b | 2009-03-23 18:28:16 +0100 | [diff] [blame] | 33 | * devm_request_threaded_irq - allocate an interrupt line for a managed device |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 34 | * @dev: device to request interrupt for |
| 35 | * @irq: Interrupt line to allocate |
| 36 | * @handler: Function to be called when the IRQ occurs |
Arjan van de Ven | 935bd5b | 2009-03-23 18:28:16 +0100 | [diff] [blame] | 37 | * @thread_fn: function to be called in a threaded interrupt context. NULL |
| 38 | * for devices which handle everything in @handler |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 39 | * @irqflags: Interrupt type flags |
Heiner Kallweit | 899b5fb | 2017-02-12 16:31:44 +0100 | [diff] [blame] | 40 | * @devname: An ascii name for the claiming device, dev_name(dev) if NULL |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 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 |
Emilio López | 307b28b | 2014-07-01 16:47:35 -0300 | [diff] [blame] | 45 | * request_threaded_irq(). IRQs requested with this function will be |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 46 | * automatically freed on driver detach. |
| 47 | * |
| 48 | * If an IRQ allocated with this function needs to be freed |
Jean Delvare | 5c42dc7 | 2010-02-11 15:04:36 +0100 | [diff] [blame] | 49 | * separately, devm_free_irq() must be used. |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 50 | */ |
Arjan van de Ven | 935bd5b | 2009-03-23 18:28:16 +0100 | [diff] [blame] | 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) |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 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 | |
Heiner Kallweit | 899b5fb | 2017-02-12 16:31:44 +0100 | [diff] [blame] | 64 | if (!devname) |
| 65 | devname = dev_name(dev); |
| 66 | |
Arjan van de Ven | 935bd5b | 2009-03-23 18:28:16 +0100 | [diff] [blame] | 67 | rc = request_threaded_irq(irq, handler, thread_fn, irqflags, devname, |
| 68 | dev_id); |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 69 | if (rc) { |
Tejun Heo | 7f30e49 | 2007-04-07 14:59:41 +0900 | [diff] [blame] | 70 | devres_free(dr); |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 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 | } |
Arjan van de Ven | 935bd5b | 2009-03-23 18:28:16 +0100 | [diff] [blame] | 80 | EXPORT_SYMBOL(devm_request_threaded_irq); |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
Stephen Boyd | 0668d30 | 2014-01-02 16:37:32 -0800 | [diff] [blame] | 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 |
Stephen Boyd | 0668d30 | 2014-01-02 16:37:32 -0800 | [diff] [blame] | 87 | * @irqflags: Interrupt type flags |
Heiner Kallweit | 899b5fb | 2017-02-12 16:31:44 +0100 | [diff] [blame] | 88 | * @devname: An ascii name for the claiming device, dev_name(dev) if NULL |
Stephen Boyd | 0668d30 | 2014-01-02 16:37:32 -0800 | [diff] [blame] | 89 | * @dev_id: A cookie passed back to the handler function |
| 90 | * |
| 91 | * Except for the extra @dev argument, this function takes the |
| 92 | * same arguments and performs the same function as |
| 93 | * request_any_context_irq(). IRQs requested with this function will be |
| 94 | * automatically freed on driver detach. |
| 95 | * |
| 96 | * If an IRQ allocated with this function needs to be freed |
| 97 | * separately, devm_free_irq() must be used. |
| 98 | */ |
| 99 | int devm_request_any_context_irq(struct device *dev, unsigned int irq, |
| 100 | irq_handler_t handler, unsigned long irqflags, |
| 101 | const char *devname, void *dev_id) |
| 102 | { |
| 103 | struct irq_devres *dr; |
| 104 | int rc; |
| 105 | |
| 106 | dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres), |
| 107 | GFP_KERNEL); |
| 108 | if (!dr) |
| 109 | return -ENOMEM; |
| 110 | |
Heiner Kallweit | 899b5fb | 2017-02-12 16:31:44 +0100 | [diff] [blame] | 111 | if (!devname) |
| 112 | devname = dev_name(dev); |
| 113 | |
Stephen Boyd | 0668d30 | 2014-01-02 16:37:32 -0800 | [diff] [blame] | 114 | rc = request_any_context_irq(irq, handler, irqflags, devname, dev_id); |
Axel Lin | 6378139 | 2015-05-11 17:02:58 +0800 | [diff] [blame] | 115 | if (rc < 0) { |
Stephen Boyd | 0668d30 | 2014-01-02 16:37:32 -0800 | [diff] [blame] | 116 | devres_free(dr); |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | dr->irq = irq; |
| 121 | dr->dev_id = dev_id; |
| 122 | devres_add(dev, dr); |
| 123 | |
Axel Lin | 6378139 | 2015-05-11 17:02:58 +0800 | [diff] [blame] | 124 | return rc; |
Stephen Boyd | 0668d30 | 2014-01-02 16:37:32 -0800 | [diff] [blame] | 125 | } |
| 126 | EXPORT_SYMBOL(devm_request_any_context_irq); |
| 127 | |
| 128 | /** |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 129 | * devm_free_irq - free an interrupt |
| 130 | * @dev: device to free interrupt for |
| 131 | * @irq: Interrupt line to free |
| 132 | * @dev_id: Device identity to free |
| 133 | * |
| 134 | * Except for the extra @dev argument, this function takes the |
| 135 | * same arguments and performs the same function as free_irq(). |
| 136 | * This function instead of free_irq() should be used to manually |
Baruch Siach | 9ce8e49 | 2010-02-02 08:54:51 +0200 | [diff] [blame] | 137 | * free IRQs allocated with devm_request_irq(). |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 138 | */ |
| 139 | void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id) |
| 140 | { |
| 141 | struct irq_devres match_data = { irq, dev_id }; |
| 142 | |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 143 | WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match, |
| 144 | &match_data)); |
Maxin B John | ae891a1 | 2011-07-25 17:12:59 -0700 | [diff] [blame] | 145 | free_irq(irq, dev_id); |
Al Viro | 5ea8176 | 2007-02-11 15:41:31 +0000 | [diff] [blame] | 146 | } |
| 147 | EXPORT_SYMBOL(devm_free_irq); |
Bartosz Golaszewski | 2b5e773 | 2017-02-10 13:23:23 +0100 | [diff] [blame] | 148 | |
| 149 | struct irq_desc_devres { |
| 150 | unsigned int from; |
| 151 | unsigned int cnt; |
| 152 | }; |
| 153 | |
| 154 | static 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) |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 170 | * @affinity: Optional pointer to an irq_affinity_desc array of size @cnt |
Bartosz Golaszewski | 2b5e773 | 2017-02-10 13:23:23 +0100 | [diff] [blame] | 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 | */ |
| 178 | int __devm_irq_alloc_descs(struct device *dev, int irq, unsigned int from, |
| 179 | unsigned int cnt, int node, struct module *owner, |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 180 | const struct irq_affinity_desc *affinity) |
Bartosz Golaszewski | 2b5e773 | 2017-02-10 13:23:23 +0100 | [diff] [blame] | 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 | } |
| 201 | EXPORT_SYMBOL_GPL(__devm_irq_alloc_descs); |
Bartosz Golaszewski | 1c3e363 | 2017-05-31 18:06:59 +0200 | [diff] [blame] | 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 | */ |
| 217 | struct irq_chip_generic * |
| 218 | devm_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; |
Bartosz Golaszewski | 1c3e363 | 2017-05-31 18:06:59 +0200 | [diff] [blame] | 223 | |
Gustavo A. R. Silva | 2d65c42 | 2019-04-10 12:09:14 -0500 | [diff] [blame] | 224 | gc = devm_kzalloc(dev, struct_size(gc, chip_types, num_ct), GFP_KERNEL); |
Bartosz Golaszewski | 1c3e363 | 2017-05-31 18:06:59 +0200 | [diff] [blame] | 225 | if (gc) |
| 226 | irq_init_generic_chip(gc, name, num_ct, |
| 227 | irq_base, reg_base, handler); |
| 228 | |
| 229 | return gc; |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(devm_irq_alloc_generic_chip); |
Bartosz Golaszewski | 30fd8fc | 2017-05-31 18:07:00 +0200 | [diff] [blame] | 232 | |
| 233 | struct irq_generic_chip_devres { |
| 234 | struct irq_chip_generic *gc; |
| 235 | u32 msk; |
| 236 | unsigned int clr; |
| 237 | unsigned int set; |
| 238 | }; |
| 239 | |
| 240 | static 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 | */ |
| 262 | int 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 | } |
| 283 | EXPORT_SYMBOL_GPL(devm_irq_setup_generic_chip); |
Bartosz Golaszewski | 1c3e363 | 2017-05-31 18:06:59 +0200 | [diff] [blame] | 284 | #endif /* CONFIG_GENERIC_IRQ_CHIP */ |