1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * RZ/N1 GPIO Interrupt Multiplexer
4 *
5 * Copyright 2025 Schneider Electric
6 * Author: Herve Codina <herve.codina@bootlin.com>
7 */
8
9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_irq.h>
14 #include <linux/platform_device.h>
15 #include <dt-bindings/interrupt-controller/arm-gic.h>
16
17 /*
18 * Up to 8 output lines are connected to GIC SPI interrupt controller
19 * starting at IRQ 103.
20 */
21 #define RZN1_IRQMUX_GIC_SPI_BASE 103
22 #define RZN1_IRQMUX_NUM_OUTPUTS 8
23
rzn1_irqmux_parent_args_to_line_index(struct device * dev,const struct of_phandle_args * parent_args)24 static int rzn1_irqmux_parent_args_to_line_index(struct device *dev,
25 const struct of_phandle_args *parent_args)
26 {
27 /*
28 * The parent interrupt should be one of the GIC controller.
29 * Three arguments must be provided.
30 * - args[0]: GIC_SPI
31 * - args[1]: The GIC interrupt number
32 * - args[2]: The interrupt flags
33 *
34 * We retrieve the line index based on the GIC interrupt number
35 * provided.
36 */
37
38 if (parent_args->args_count != 3 || parent_args->args[0] != GIC_SPI) {
39 dev_err(dev, "Invalid interrupt-map item\n");
40 return -EINVAL;
41 }
42
43 if (parent_args->args[1] < RZN1_IRQMUX_GIC_SPI_BASE ||
44 parent_args->args[1] >= RZN1_IRQMUX_GIC_SPI_BASE + RZN1_IRQMUX_NUM_OUTPUTS) {
45 dev_err(dev, "Invalid GIC interrupt %u\n", parent_args->args[1]);
46 return -EINVAL;
47 }
48
49 return parent_args->args[1] - RZN1_IRQMUX_GIC_SPI_BASE;
50 }
51
rzn1_irqmux_probe(struct platform_device * pdev)52 static int rzn1_irqmux_probe(struct platform_device *pdev)
53 {
54 DECLARE_BITMAP(index_done, RZN1_IRQMUX_NUM_OUTPUTS) = {};
55 struct device *dev = &pdev->dev;
56 struct device_node *np = dev->of_node;
57 struct of_imap_parser imap_parser;
58 struct of_imap_item imap_item;
59 u32 __iomem *regs;
60 int index;
61 int ret;
62 u32 tmp;
63
64 regs = devm_platform_ioremap_resource(pdev, 0);
65 if (IS_ERR(regs))
66 return PTR_ERR(regs);
67
68 /* We support only #interrupt-cells = <1> and #address-cells = <0> */
69 ret = of_property_read_u32(np, "#interrupt-cells", &tmp);
70 if (ret)
71 return ret;
72 if (tmp != 1)
73 return -EINVAL;
74
75 ret = of_property_read_u32(np, "#address-cells", &tmp);
76 if (ret)
77 return ret;
78 if (tmp != 0)
79 return -EINVAL;
80
81 ret = of_imap_parser_init(&imap_parser, np, &imap_item);
82 if (ret)
83 return ret;
84
85 for_each_of_imap_item(&imap_parser, &imap_item) {
86 index = rzn1_irqmux_parent_args_to_line_index(dev, &imap_item.parent_args);
87 if (index < 0) {
88 of_node_put(imap_item.parent_args.np);
89 return index;
90 }
91
92 if (test_and_set_bit(index, index_done)) {
93 of_node_put(imap_item.parent_args.np);
94 dev_err(dev, "Mux output line %d already defined in interrupt-map\n",
95 index);
96 return -EINVAL;
97 }
98
99 /*
100 * The child #address-cells is 0 (already checked). The first
101 * value in imap item is the src hwirq.
102 */
103 writel(imap_item.child_imap[0], regs + index);
104 }
105
106 return 0;
107 }
108
109 static const struct of_device_id rzn1_irqmux_of_match[] = {
110 { .compatible = "renesas,rzn1-gpioirqmux", },
111 { /* sentinel */ }
112 };
113 MODULE_DEVICE_TABLE(of, rzn1_irqmux_of_match);
114
115 static struct platform_driver rzn1_irqmux_driver = {
116 .probe = rzn1_irqmux_probe,
117 .driver = {
118 .name = "rzn1_irqmux",
119 .of_match_table = rzn1_irqmux_of_match,
120 },
121 };
122 module_platform_driver(rzn1_irqmux_driver);
123
124 MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
125 MODULE_DESCRIPTION("Renesas RZ/N1 GPIO IRQ Multiplexer Driver");
126 MODULE_LICENSE("GPL");
127