irqchip: Add IRQCHIP_PLATFORM_DRIVER_BEGIN/END and IRQCHIP_MATCH helper macros
authorSaravana Kannan <saravanak@google.com>
Sat, 18 Jul 2020 00:06:34 +0000 (17:06 -0700)
committerMarc Zyngier <maz@kernel.org>
Mon, 27 Jul 2020 07:55:04 +0000 (08:55 +0100)
Compiling an irqchip driver as a platform driver needs to bunch of
things to be done right:
- Making sure the parent domain is initialized first
- Making sure the device can't be unbound from sysfs
- Disallowing module unload if it's built as a module
- Finding the parent node
- Etc.

Instead of trying to make sure all future irqchip platform drivers get
this right, provide boilerplate macros that take care of all of this.

An example use would look something like this. Where acme_foo_init and
acme_bar_init are similar to what would be passed to IRQCHIP_DECLARE.

IRQCHIP_PLATFORM_DRIVER_BEGIN(acme_irq)
IRQCHIP_MATCH("acme,foo", acme_foo_init)
IRQCHIP_MATCH("acme,bar", acme_bar_init)
IRQCHIP_PLATFORM_DRIVER_END(acme_irq)

Signed-off-by: Saravana Kannan <saravanak@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: https://lore.kernel.org/r/20200718000637.3632841-2-saravanak@google.com
drivers/irqchip/irqchip.c
include/linux/irqchip.h

index 2b35e68bea825c3a5d132d42a2a52b2740ec1353..1bb0e36c2bf3dbd5ee07f9fa1588d956deb60177 100644 (file)
 
 #include <linux/acpi.h>
 #include <linux/init.h>
+#include <linux/of_device.h>
 #include <linux/of_irq.h>
 #include <linux/irqchip.h>
+#include <linux/platform_device.h>
 
 /*
  * This special of_device_id is the sentinel at the end of the
@@ -29,3 +31,30 @@ void __init irqchip_init(void)
        of_irq_init(__irqchip_of_table);
        acpi_probe_device_table(irqchip);
 }
+
+int platform_irqchip_probe(struct platform_device *pdev)
+{
+       struct device_node *np = pdev->dev.of_node;
+       struct device_node *par_np = of_irq_find_parent(np);
+       of_irq_init_cb_t irq_init_cb = of_device_get_match_data(&pdev->dev);
+
+       if (!irq_init_cb)
+               return -EINVAL;
+
+       if (par_np == np)
+               par_np = NULL;
+
+       /*
+        * If there's a parent interrupt controller and  none of the parent irq
+        * domains have been registered, that means the parent interrupt
+        * controller has not been initialized yet.  it's not time for this
+        * interrupt controller to initialize. So, defer probe of this
+        * interrupt controller. The actual initialization callback of this
+        * interrupt controller can check for specific domains as necessary.
+        */
+       if (par_np && !irq_find_matching_host(np, DOMAIN_BUS_ANY))
+               return -EPROBE_DEFER;
+
+       return irq_init_cb(np, par_np);
+}
+EXPORT_SYMBOL_GPL(platform_irqchip_probe);
index 447f22880a69580e750f5e4e9429f3c3b10ea2a2..8e754d8b81557b443b23979191cae2d7b099c8bc 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <linux/acpi.h>
 #include <linux/of.h>
+#include <linux/platform_device.h>
 
 /*
  * This macro must be used by the different irqchip drivers to declare
  */
 #define IRQCHIP_DECLARE(name, compat, fn) OF_DECLARE_2(irqchip, name, compat, fn)
 
+extern int platform_irqchip_probe(struct platform_device *pdev);
+
+#define IRQCHIP_PLATFORM_DRIVER_BEGIN(drv_name) \
+static const struct of_device_id drv_name##_irqchip_match_table[] = {
+
+#define IRQCHIP_MATCH(compat, fn) { .compatible = compat, .data = fn },
+
+#define IRQCHIP_PLATFORM_DRIVER_END(drv_name)                          \
+       {},                                                             \
+};                                                                     \
+MODULE_DEVICE_TABLE(of, drv_name##_irqchip_match_table);               \
+static struct platform_driver drv_name##_driver = {            \
+       .probe  = platform_irqchip_probe,                               \
+       .driver = {                                                     \
+               .name = #drv_name,                                      \
+               .owner = THIS_MODULE,                                   \
+               .of_match_table = drv_name##_irqchip_match_table,       \
+               .suppress_bind_attrs = true,                            \
+       },                                                              \
+};                                                                     \
+builtin_platform_driver(drv_name##_driver)
+
 /*
  * This macro must be used by the different irqchip drivers to declare
  * the association between their version and their initialization function.