1 // SPDX-License-Identifier: GPL-2.0
3 // regmap based irq_chip
5 // Copyright 2011 Wolfson Microelectronics plc
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 #include <linux/device.h>
10 #include <linux/export.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
20 struct regmap_irq_chip_data {
22 struct irq_chip irq_chip;
25 const struct regmap_irq_chip *chip;
28 struct irq_domain *domain;
34 unsigned int *main_status_buf;
35 unsigned int *status_buf;
36 unsigned int *mask_buf;
37 unsigned int *mask_buf_def;
38 unsigned int *wake_buf;
39 unsigned int *type_buf;
40 unsigned int *type_buf_def;
41 unsigned int **virt_buf;
43 unsigned int irq_reg_stride;
44 unsigned int type_reg_stride;
49 static int sub_irq_reg(struct regmap_irq_chip_data *data,
50 unsigned int base_reg, int i)
52 const struct regmap_irq_chip *chip = data->chip;
53 struct regmap *map = data->map;
54 struct regmap_irq_sub_irq_map *subreg;
58 if (!chip->sub_reg_offsets || !chip->not_fixed_stride) {
59 /* Assume linear mapping */
60 reg = base_reg + (i * map->reg_stride * data->irq_reg_stride);
62 subreg = &chip->sub_reg_offsets[i];
63 offset = subreg->offset[0];
64 reg = base_reg + offset;
71 struct regmap_irq *irq_to_regmap_irq(struct regmap_irq_chip_data *data,
74 return &data->chip->irqs[irq];
77 static void regmap_irq_lock(struct irq_data *data)
79 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
84 static int regmap_irq_update_bits(struct regmap_irq_chip_data *d,
85 unsigned int reg, unsigned int mask,
88 if (d->chip->mask_writeonly)
89 return regmap_write_bits(d->map, reg, mask, val);
91 return regmap_update_bits(d->map, reg, mask, val);
94 static void regmap_irq_sync_unlock(struct irq_data *data)
96 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
97 struct regmap *map = d->map;
103 if (d->chip->runtime_pm) {
104 ret = pm_runtime_get_sync(map->dev);
106 dev_err(map->dev, "IRQ sync failed to resume: %d\n",
110 if (d->clear_status) {
111 for (i = 0; i < d->chip->num_regs; i++) {
112 reg = sub_irq_reg(d, d->chip->status_base, i);
114 ret = regmap_read(map, reg, &val);
117 "Failed to clear the interrupt status bits\n");
120 d->clear_status = false;
124 * If there's been a change in the mask write it back to the
125 * hardware. We rely on the use of the regmap core cache to
126 * suppress pointless writes.
128 for (i = 0; i < d->chip->num_regs; i++) {
129 if (!d->chip->mask_base)
132 reg = sub_irq_reg(d, d->chip->mask_base, i);
133 if (d->chip->mask_invert) {
134 ret = regmap_irq_update_bits(d, reg,
135 d->mask_buf_def[i], ~d->mask_buf[i]);
136 } else if (d->chip->unmask_base) {
137 /* set mask with mask_base register */
138 ret = regmap_irq_update_bits(d, reg,
139 d->mask_buf_def[i], ~d->mask_buf[i]);
142 "Failed to sync unmasks in %x\n",
144 unmask_offset = d->chip->unmask_base -
146 /* clear mask with unmask_base register */
147 ret = regmap_irq_update_bits(d,
152 ret = regmap_irq_update_bits(d, reg,
153 d->mask_buf_def[i], d->mask_buf[i]);
156 dev_err(d->map->dev, "Failed to sync masks in %x\n",
159 reg = sub_irq_reg(d, d->chip->wake_base, i);
161 if (d->chip->wake_invert)
162 ret = regmap_irq_update_bits(d, reg,
166 ret = regmap_irq_update_bits(d, reg,
171 "Failed to sync wakes in %x: %d\n",
175 if (!d->chip->init_ack_masked)
178 * Ack all the masked interrupts unconditionally,
179 * OR if there is masked interrupt which hasn't been Acked,
180 * it'll be ignored in irq handler, then may introduce irq storm
182 if (d->mask_buf[i] && (d->chip->ack_base || d->chip->use_ack)) {
183 reg = sub_irq_reg(d, d->chip->ack_base, i);
185 /* some chips ack by write 0 */
186 if (d->chip->ack_invert)
187 ret = regmap_write(map, reg, ~d->mask_buf[i]);
189 ret = regmap_write(map, reg, d->mask_buf[i]);
190 if (d->chip->clear_ack) {
191 if (d->chip->ack_invert && !ret)
192 ret = regmap_write(map, reg,
195 ret = regmap_write(map, reg,
199 dev_err(d->map->dev, "Failed to ack 0x%x: %d\n",
204 /* Don't update the type bits if we're using mask bits for irq type. */
205 if (!d->chip->type_in_mask) {
206 for (i = 0; i < d->chip->num_type_reg; i++) {
207 if (!d->type_buf_def[i])
209 reg = sub_irq_reg(d, d->chip->type_base, i);
210 if (d->chip->type_invert)
211 ret = regmap_irq_update_bits(d, reg,
212 d->type_buf_def[i], ~d->type_buf[i]);
214 ret = regmap_irq_update_bits(d, reg,
215 d->type_buf_def[i], d->type_buf[i]);
217 dev_err(d->map->dev, "Failed to sync type in %x\n",
222 if (d->chip->num_virt_regs) {
223 for (i = 0; i < d->chip->num_virt_regs; i++) {
224 for (j = 0; j < d->chip->num_regs; j++) {
225 reg = sub_irq_reg(d, d->chip->virt_reg_base[i],
227 ret = regmap_write(map, reg, d->virt_buf[i][j]);
230 "Failed to write virt 0x%x: %d\n",
236 if (d->chip->runtime_pm)
237 pm_runtime_put(map->dev);
239 /* If we've changed our wakeup count propagate it to the parent */
240 if (d->wake_count < 0)
241 for (i = d->wake_count; i < 0; i++)
242 irq_set_irq_wake(d->irq, 0);
243 else if (d->wake_count > 0)
244 for (i = 0; i < d->wake_count; i++)
245 irq_set_irq_wake(d->irq, 1);
249 mutex_unlock(&d->lock);
252 static void regmap_irq_enable(struct irq_data *data)
254 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
255 struct regmap *map = d->map;
256 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
257 unsigned int mask, type;
259 type = irq_data->type.type_falling_val | irq_data->type.type_rising_val;
262 * The type_in_mask flag means that the underlying hardware uses
263 * separate mask bits for rising and falling edge interrupts, but
264 * we want to make them into a single virtual interrupt with
267 * If the interrupt we're enabling defines the falling or rising
268 * masks then instead of using the regular mask bits for this
269 * interrupt, use the value previously written to the type buffer
270 * at the corresponding offset in regmap_irq_set_type().
272 if (d->chip->type_in_mask && type)
273 mask = d->type_buf[irq_data->reg_offset / map->reg_stride];
275 mask = irq_data->mask;
277 if (d->chip->clear_on_unmask)
278 d->clear_status = true;
280 d->mask_buf[irq_data->reg_offset / map->reg_stride] &= ~mask;
283 static void regmap_irq_disable(struct irq_data *data)
285 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
286 struct regmap *map = d->map;
287 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
289 d->mask_buf[irq_data->reg_offset / map->reg_stride] |= irq_data->mask;
292 static int regmap_irq_set_type(struct irq_data *data, unsigned int type)
294 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
295 struct regmap *map = d->map;
296 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
298 const struct regmap_irq_type *t = &irq_data->type;
300 if ((t->types_supported & type) != type)
303 reg = t->type_reg_offset / map->reg_stride;
305 if (t->type_reg_mask)
306 d->type_buf[reg] &= ~t->type_reg_mask;
308 d->type_buf[reg] &= ~(t->type_falling_val |
310 t->type_level_low_val |
311 t->type_level_high_val);
313 case IRQ_TYPE_EDGE_FALLING:
314 d->type_buf[reg] |= t->type_falling_val;
317 case IRQ_TYPE_EDGE_RISING:
318 d->type_buf[reg] |= t->type_rising_val;
321 case IRQ_TYPE_EDGE_BOTH:
322 d->type_buf[reg] |= (t->type_falling_val |
326 case IRQ_TYPE_LEVEL_HIGH:
327 d->type_buf[reg] |= t->type_level_high_val;
330 case IRQ_TYPE_LEVEL_LOW:
331 d->type_buf[reg] |= t->type_level_low_val;
337 if (d->chip->set_type_virt)
338 return d->chip->set_type_virt(d->virt_buf, type, data->hwirq,
344 static int regmap_irq_set_wake(struct irq_data *data, unsigned int on)
346 struct regmap_irq_chip_data *d = irq_data_get_irq_chip_data(data);
347 struct regmap *map = d->map;
348 const struct regmap_irq *irq_data = irq_to_regmap_irq(d, data->hwirq);
352 d->wake_buf[irq_data->reg_offset / map->reg_stride]
357 d->wake_buf[irq_data->reg_offset / map->reg_stride]
365 static const struct irq_chip regmap_irq_chip = {
366 .irq_bus_lock = regmap_irq_lock,
367 .irq_bus_sync_unlock = regmap_irq_sync_unlock,
368 .irq_disable = regmap_irq_disable,
369 .irq_enable = regmap_irq_enable,
370 .irq_set_type = regmap_irq_set_type,
371 .irq_set_wake = regmap_irq_set_wake,
374 static inline int read_sub_irq_data(struct regmap_irq_chip_data *data,
377 const struct regmap_irq_chip *chip = data->chip;
378 struct regmap *map = data->map;
379 struct regmap_irq_sub_irq_map *subreg;
382 if (!chip->sub_reg_offsets) {
383 /* Assume linear mapping */
384 ret = regmap_read(map, chip->status_base +
385 (b * map->reg_stride * data->irq_reg_stride),
386 &data->status_buf[b]);
388 subreg = &chip->sub_reg_offsets[b];
389 for (i = 0; i < subreg->num_regs; i++) {
390 unsigned int offset = subreg->offset[i];
392 if (chip->not_fixed_stride)
393 ret = regmap_read(map,
394 chip->status_base + offset,
395 &data->status_buf[b]);
397 ret = regmap_read(map,
398 chip->status_base + offset,
399 &data->status_buf[offset]);
408 static irqreturn_t regmap_irq_thread(int irq, void *d)
410 struct regmap_irq_chip_data *data = d;
411 const struct regmap_irq_chip *chip = data->chip;
412 struct regmap *map = data->map;
414 bool handled = false;
417 if (chip->handle_pre_irq)
418 chip->handle_pre_irq(chip->irq_drv_data);
420 if (chip->runtime_pm) {
421 ret = pm_runtime_get_sync(map->dev);
423 dev_err(map->dev, "IRQ thread failed to resume: %d\n",
430 * Read only registers with active IRQs if the chip has 'main status
431 * register'. Else read in the statuses, using a single bulk read if
432 * possible in order to reduce the I/O overheads.
435 if (chip->num_main_regs) {
436 unsigned int max_main_bits;
439 size = chip->num_regs * sizeof(unsigned int);
441 max_main_bits = (chip->num_main_status_bits) ?
442 chip->num_main_status_bits : chip->num_regs;
443 /* Clear the status buf as we don't read all status regs */
444 memset(data->status_buf, 0, size);
446 /* We could support bulk read for main status registers
447 * but I don't expect to see devices with really many main
448 * status registers so let's only support single reads for the
449 * sake of simplicity. and add bulk reads only if needed
451 for (i = 0; i < chip->num_main_regs; i++) {
452 ret = regmap_read(map, chip->main_status +
454 * data->irq_reg_stride),
455 &data->main_status_buf[i]);
458 "Failed to read IRQ status %d\n",
464 /* Read sub registers with active IRQs */
465 for (i = 0; i < chip->num_main_regs; i++) {
467 const unsigned long mreg = data->main_status_buf[i];
469 for_each_set_bit(b, &mreg, map->format.val_bytes * 8) {
470 if (i * map->format.val_bytes * 8 + b >
473 ret = read_sub_irq_data(data, b);
477 "Failed to read IRQ status %d\n",
484 } else if (!map->use_single_read && map->reg_stride == 1 &&
485 data->irq_reg_stride == 1) {
487 u8 *buf8 = data->status_reg_buf;
488 u16 *buf16 = data->status_reg_buf;
489 u32 *buf32 = data->status_reg_buf;
491 BUG_ON(!data->status_reg_buf);
493 ret = regmap_bulk_read(map, chip->status_base,
494 data->status_reg_buf,
497 dev_err(map->dev, "Failed to read IRQ status: %d\n",
502 for (i = 0; i < data->chip->num_regs; i++) {
503 switch (map->format.val_bytes) {
505 data->status_buf[i] = buf8[i];
508 data->status_buf[i] = buf16[i];
511 data->status_buf[i] = buf32[i];
520 for (i = 0; i < data->chip->num_regs; i++) {
521 unsigned int reg = sub_irq_reg(data,
522 data->chip->status_base, i);
523 ret = regmap_read(map, reg, &data->status_buf[i]);
527 "Failed to read IRQ status: %d\n",
535 * Ignore masked IRQs and ack if we need to; we ack early so
536 * there is no race between handling and acknowleding the
537 * interrupt. We assume that typically few of the interrupts
538 * will fire simultaneously so don't worry about overhead from
539 * doing a write per register.
541 for (i = 0; i < data->chip->num_regs; i++) {
542 data->status_buf[i] &= ~data->mask_buf[i];
544 if (data->status_buf[i] && (chip->ack_base || chip->use_ack)) {
545 reg = sub_irq_reg(data, data->chip->ack_base, i);
547 if (chip->ack_invert)
548 ret = regmap_write(map, reg,
549 ~data->status_buf[i]);
551 ret = regmap_write(map, reg,
552 data->status_buf[i]);
553 if (chip->clear_ack) {
554 if (chip->ack_invert && !ret)
555 ret = regmap_write(map, reg,
556 data->status_buf[i]);
558 ret = regmap_write(map, reg,
559 ~data->status_buf[i]);
562 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
567 for (i = 0; i < chip->num_irqs; i++) {
568 if (data->status_buf[chip->irqs[i].reg_offset /
569 map->reg_stride] & chip->irqs[i].mask) {
570 handle_nested_irq(irq_find_mapping(data->domain, i));
576 if (chip->runtime_pm)
577 pm_runtime_put(map->dev);
579 if (chip->handle_post_irq)
580 chip->handle_post_irq(chip->irq_drv_data);
588 static int regmap_irq_map(struct irq_domain *h, unsigned int virq,
591 struct regmap_irq_chip_data *data = h->host_data;
593 irq_set_chip_data(virq, data);
594 irq_set_chip(virq, &data->irq_chip);
595 irq_set_nested_thread(virq, 1);
596 irq_set_parent(virq, data->irq);
597 irq_set_noprobe(virq);
602 static const struct irq_domain_ops regmap_domain_ops = {
603 .map = regmap_irq_map,
604 .xlate = irq_domain_xlate_onetwocell,
608 * regmap_add_irq_chip_fwnode() - Use standard regmap IRQ controller handling
610 * @fwnode: The firmware node where the IRQ domain should be added to.
611 * @map: The regmap for the device.
612 * @irq: The IRQ the device uses to signal interrupts.
613 * @irq_flags: The IRQF_ flags to use for the primary interrupt.
614 * @irq_base: Allocate at specific IRQ number if irq_base > 0.
615 * @chip: Configuration for the interrupt controller.
616 * @data: Runtime data structure for the controller, allocated on success.
618 * Returns 0 on success or an errno on failure.
620 * In order for this to be efficient the chip really should use a
621 * register cache. The chip driver is responsible for restoring the
622 * register values used by the IRQ controller over suspend and resume.
624 int regmap_add_irq_chip_fwnode(struct fwnode_handle *fwnode,
625 struct regmap *map, int irq,
626 int irq_flags, int irq_base,
627 const struct regmap_irq_chip *chip,
628 struct regmap_irq_chip_data **data)
630 struct regmap_irq_chip_data *d;
637 if (chip->num_regs <= 0)
640 if (chip->clear_on_unmask && (chip->ack_base || chip->use_ack))
643 for (i = 0; i < chip->num_irqs; i++) {
644 if (chip->irqs[i].reg_offset % map->reg_stride)
646 if (chip->irqs[i].reg_offset / map->reg_stride >=
651 if (chip->not_fixed_stride) {
652 for (i = 0; i < chip->num_regs; i++)
653 if (chip->sub_reg_offsets[i].num_regs != 1)
658 irq_base = irq_alloc_descs(irq_base, 0, chip->num_irqs, 0);
660 dev_warn(map->dev, "Failed to allocate IRQs: %d\n",
666 d = kzalloc(sizeof(*d), GFP_KERNEL);
670 if (chip->num_main_regs) {
671 d->main_status_buf = kcalloc(chip->num_main_regs,
672 sizeof(unsigned int),
675 if (!d->main_status_buf)
679 d->status_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
684 d->mask_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
689 d->mask_buf_def = kcalloc(chip->num_regs, sizeof(unsigned int),
691 if (!d->mask_buf_def)
694 if (chip->wake_base) {
695 d->wake_buf = kcalloc(chip->num_regs, sizeof(unsigned int),
701 num_type_reg = chip->type_in_mask ? chip->num_regs : chip->num_type_reg;
703 d->type_buf_def = kcalloc(num_type_reg,
704 sizeof(unsigned int), GFP_KERNEL);
705 if (!d->type_buf_def)
708 d->type_buf = kcalloc(num_type_reg, sizeof(unsigned int),
714 if (chip->num_virt_regs) {
716 * Create virt_buf[chip->num_extra_config_regs][chip->num_regs]
718 d->virt_buf = kcalloc(chip->num_virt_regs, sizeof(*d->virt_buf),
723 for (i = 0; i < chip->num_virt_regs; i++) {
724 d->virt_buf[i] = kcalloc(chip->num_regs,
725 sizeof(unsigned int),
732 d->irq_chip = regmap_irq_chip;
733 d->irq_chip.name = chip->name;
737 d->irq_base = irq_base;
739 if (chip->irq_reg_stride)
740 d->irq_reg_stride = chip->irq_reg_stride;
742 d->irq_reg_stride = 1;
744 if (chip->type_reg_stride)
745 d->type_reg_stride = chip->type_reg_stride;
747 d->type_reg_stride = 1;
749 if (!map->use_single_read && map->reg_stride == 1 &&
750 d->irq_reg_stride == 1) {
751 d->status_reg_buf = kmalloc_array(chip->num_regs,
752 map->format.val_bytes,
754 if (!d->status_reg_buf)
758 mutex_init(&d->lock);
760 for (i = 0; i < chip->num_irqs; i++)
761 d->mask_buf_def[chip->irqs[i].reg_offset / map->reg_stride]
762 |= chip->irqs[i].mask;
764 /* Mask all the interrupts by default */
765 for (i = 0; i < chip->num_regs; i++) {
766 d->mask_buf[i] = d->mask_buf_def[i];
767 if (!chip->mask_base)
770 reg = sub_irq_reg(d, d->chip->mask_base, i);
772 if (chip->mask_invert)
773 ret = regmap_irq_update_bits(d, reg,
774 d->mask_buf[i], ~d->mask_buf[i]);
775 else if (d->chip->unmask_base) {
776 unmask_offset = d->chip->unmask_base -
778 ret = regmap_irq_update_bits(d,
783 ret = regmap_irq_update_bits(d, reg,
784 d->mask_buf[i], d->mask_buf[i]);
786 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
791 if (!chip->init_ack_masked)
794 /* Ack masked but set interrupts */
795 reg = sub_irq_reg(d, d->chip->status_base, i);
796 ret = regmap_read(map, reg, &d->status_buf[i]);
798 dev_err(map->dev, "Failed to read IRQ status: %d\n",
803 if (d->status_buf[i] && (chip->ack_base || chip->use_ack)) {
804 reg = sub_irq_reg(d, d->chip->ack_base, i);
805 if (chip->ack_invert)
806 ret = regmap_write(map, reg,
807 ~(d->status_buf[i] & d->mask_buf[i]));
809 ret = regmap_write(map, reg,
810 d->status_buf[i] & d->mask_buf[i]);
811 if (chip->clear_ack) {
812 if (chip->ack_invert && !ret)
813 ret = regmap_write(map, reg,
817 ret = regmap_write(map, reg,
822 dev_err(map->dev, "Failed to ack 0x%x: %d\n",
829 /* Wake is disabled by default */
831 for (i = 0; i < chip->num_regs; i++) {
832 d->wake_buf[i] = d->mask_buf_def[i];
833 reg = sub_irq_reg(d, d->chip->wake_base, i);
835 if (chip->wake_invert)
836 ret = regmap_irq_update_bits(d, reg,
840 ret = regmap_irq_update_bits(d, reg,
844 dev_err(map->dev, "Failed to set masks in 0x%x: %d\n",
851 if (chip->num_type_reg && !chip->type_in_mask) {
852 for (i = 0; i < chip->num_type_reg; ++i) {
853 reg = sub_irq_reg(d, d->chip->type_base, i);
855 ret = regmap_read(map, reg, &d->type_buf_def[i]);
857 if (d->chip->type_invert)
858 d->type_buf_def[i] = ~d->type_buf_def[i];
861 dev_err(map->dev, "Failed to get type defaults at 0x%x: %d\n",
869 d->domain = irq_domain_create_legacy(fwnode, chip->num_irqs,
871 ®map_domain_ops, d);
873 d->domain = irq_domain_create_linear(fwnode, chip->num_irqs,
874 ®map_domain_ops, d);
876 dev_err(map->dev, "Failed to create IRQ domain\n");
881 ret = request_threaded_irq(irq, NULL, regmap_irq_thread,
882 irq_flags | IRQF_ONESHOT,
885 dev_err(map->dev, "Failed to request IRQ %d for %s: %d\n",
886 irq, chip->name, ret);
895 /* Should really dispose of the domain but... */
898 kfree(d->type_buf_def);
900 kfree(d->mask_buf_def);
902 kfree(d->status_buf);
903 kfree(d->status_reg_buf);
905 for (i = 0; i < chip->num_virt_regs; i++)
906 kfree(d->virt_buf[i]);
912 EXPORT_SYMBOL_GPL(regmap_add_irq_chip_fwnode);
915 * regmap_add_irq_chip() - Use standard regmap IRQ controller handling
917 * @map: The regmap for the device.
918 * @irq: The IRQ the device uses to signal interrupts.
919 * @irq_flags: The IRQF_ flags to use for the primary interrupt.
920 * @irq_base: Allocate at specific IRQ number if irq_base > 0.
921 * @chip: Configuration for the interrupt controller.
922 * @data: Runtime data structure for the controller, allocated on success.
924 * Returns 0 on success or an errno on failure.
926 * This is the same as regmap_add_irq_chip_fwnode, except that the firmware
927 * node of the regmap is used.
929 int regmap_add_irq_chip(struct regmap *map, int irq, int irq_flags,
930 int irq_base, const struct regmap_irq_chip *chip,
931 struct regmap_irq_chip_data **data)
933 return regmap_add_irq_chip_fwnode(dev_fwnode(map->dev), map, irq,
934 irq_flags, irq_base, chip, data);
936 EXPORT_SYMBOL_GPL(regmap_add_irq_chip);
939 * regmap_del_irq_chip() - Stop interrupt handling for a regmap IRQ chip
941 * @irq: Primary IRQ for the device
942 * @d: ®map_irq_chip_data allocated by regmap_add_irq_chip()
944 * This function also disposes of all mapped IRQs on the chip.
946 void regmap_del_irq_chip(int irq, struct regmap_irq_chip_data *d)
956 /* Dispose all virtual irq from irq domain before removing it */
957 for (hwirq = 0; hwirq < d->chip->num_irqs; hwirq++) {
958 /* Ignore hwirq if holes in the IRQ list */
959 if (!d->chip->irqs[hwirq].mask)
963 * Find the virtual irq of hwirq on chip and if it is
964 * there then dispose it
966 virq = irq_find_mapping(d->domain, hwirq);
968 irq_dispose_mapping(virq);
971 irq_domain_remove(d->domain);
973 kfree(d->type_buf_def);
975 kfree(d->mask_buf_def);
977 kfree(d->status_reg_buf);
978 kfree(d->status_buf);
981 EXPORT_SYMBOL_GPL(regmap_del_irq_chip);
983 static void devm_regmap_irq_chip_release(struct device *dev, void *res)
985 struct regmap_irq_chip_data *d = *(struct regmap_irq_chip_data **)res;
987 regmap_del_irq_chip(d->irq, d);
990 static int devm_regmap_irq_chip_match(struct device *dev, void *res, void *data)
993 struct regmap_irq_chip_data **r = res;
1003 * devm_regmap_add_irq_chip_fwnode() - Resource managed regmap_add_irq_chip_fwnode()
1005 * @dev: The device pointer on which irq_chip belongs to.
1006 * @fwnode: The firmware node where the IRQ domain should be added to.
1007 * @map: The regmap for the device.
1008 * @irq: The IRQ the device uses to signal interrupts
1009 * @irq_flags: The IRQF_ flags to use for the primary interrupt.
1010 * @irq_base: Allocate at specific IRQ number if irq_base > 0.
1011 * @chip: Configuration for the interrupt controller.
1012 * @data: Runtime data structure for the controller, allocated on success
1014 * Returns 0 on success or an errno on failure.
1016 * The ®map_irq_chip_data will be automatically released when the device is
1019 int devm_regmap_add_irq_chip_fwnode(struct device *dev,
1020 struct fwnode_handle *fwnode,
1021 struct regmap *map, int irq,
1022 int irq_flags, int irq_base,
1023 const struct regmap_irq_chip *chip,
1024 struct regmap_irq_chip_data **data)
1026 struct regmap_irq_chip_data **ptr, *d;
1029 ptr = devres_alloc(devm_regmap_irq_chip_release, sizeof(*ptr),
1034 ret = regmap_add_irq_chip_fwnode(fwnode, map, irq, irq_flags, irq_base,
1042 devres_add(dev, ptr);
1046 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip_fwnode);
1049 * devm_regmap_add_irq_chip() - Resource manager regmap_add_irq_chip()
1051 * @dev: The device pointer on which irq_chip belongs to.
1052 * @map: The regmap for the device.
1053 * @irq: The IRQ the device uses to signal interrupts
1054 * @irq_flags: The IRQF_ flags to use for the primary interrupt.
1055 * @irq_base: Allocate at specific IRQ number if irq_base > 0.
1056 * @chip: Configuration for the interrupt controller.
1057 * @data: Runtime data structure for the controller, allocated on success
1059 * Returns 0 on success or an errno on failure.
1061 * The ®map_irq_chip_data will be automatically released when the device is
1064 int devm_regmap_add_irq_chip(struct device *dev, struct regmap *map, int irq,
1065 int irq_flags, int irq_base,
1066 const struct regmap_irq_chip *chip,
1067 struct regmap_irq_chip_data **data)
1069 return devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(map->dev), map,
1070 irq, irq_flags, irq_base, chip,
1073 EXPORT_SYMBOL_GPL(devm_regmap_add_irq_chip);
1076 * devm_regmap_del_irq_chip() - Resource managed regmap_del_irq_chip()
1078 * @dev: Device for which which resource was allocated.
1079 * @irq: Primary IRQ for the device.
1080 * @data: ®map_irq_chip_data allocated by regmap_add_irq_chip().
1082 * A resource managed version of regmap_del_irq_chip().
1084 void devm_regmap_del_irq_chip(struct device *dev, int irq,
1085 struct regmap_irq_chip_data *data)
1089 WARN_ON(irq != data->irq);
1090 rc = devres_release(dev, devm_regmap_irq_chip_release,
1091 devm_regmap_irq_chip_match, data);
1096 EXPORT_SYMBOL_GPL(devm_regmap_del_irq_chip);
1099 * regmap_irq_chip_get_base() - Retrieve interrupt base for a regmap IRQ chip
1101 * @data: regmap irq controller to operate on.
1103 * Useful for drivers to request their own IRQs.
1105 int regmap_irq_chip_get_base(struct regmap_irq_chip_data *data)
1107 WARN_ON(!data->irq_base);
1108 return data->irq_base;
1110 EXPORT_SYMBOL_GPL(regmap_irq_chip_get_base);
1113 * regmap_irq_get_virq() - Map an interrupt on a chip to a virtual IRQ
1115 * @data: regmap irq controller to operate on.
1116 * @irq: index of the interrupt requested in the chip IRQs.
1118 * Useful for drivers to request their own IRQs.
1120 int regmap_irq_get_virq(struct regmap_irq_chip_data *data, int irq)
1122 /* Handle holes in the IRQ list */
1123 if (!data->chip->irqs[irq].mask)
1126 return irq_create_mapping(data->domain, irq);
1128 EXPORT_SYMBOL_GPL(regmap_irq_get_virq);
1131 * regmap_irq_get_domain() - Retrieve the irq_domain for the chip
1133 * @data: regmap_irq controller to operate on.
1135 * Useful for drivers to request their own IRQs and for integration
1136 * with subsystems. For ease of integration NULL is accepted as a
1137 * domain, allowing devices to just call this even if no domain is
1140 struct irq_domain *regmap_irq_get_domain(struct regmap_irq_chip_data *data)
1143 return data->domain;
1147 EXPORT_SYMBOL_GPL(regmap_irq_get_domain);