1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Magnus Damm
4 * Copyright (C) 2015 Glider bvba
7 #define pr_fmt(fmt) "board_staging: " fmt
9 #include <linux/clkdev.h>
10 #include <linux/init.h>
11 #include <linux/irq.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_domain.h>
22 static struct device_node *irqc_node __initdata;
23 static unsigned int irqc_base __initdata;
25 static bool find_by_address(u64 base_address)
27 struct device_node *dn = of_find_all_nodes(NULL);
31 if (!of_address_to_resource(dn, 0, &res)) {
32 if (res.start == base_address) {
37 dn = of_find_all_nodes(dn);
43 bool __init board_staging_dt_node_available(const struct resource *resource,
44 unsigned int num_resources)
48 for (i = 0; i < num_resources; i++) {
49 const struct resource *r = resource + i;
51 if (resource_type(r) == IORESOURCE_MEM)
52 if (find_by_address(r->start))
53 return true; /* DT node available */
56 return false; /* Nothing found */
59 int __init board_staging_gic_setup_xlate(const char *gic_match,
64 irqc_node = of_find_compatible_node(NULL, NULL, gic_match);
74 static void __init gic_fixup_resource(struct resource *res)
76 struct of_phandle_args irq_data;
77 unsigned int hwirq = res->start;
80 if (resource_type(res) != IORESOURCE_IRQ || !irqc_node)
83 irq_data.np = irqc_node;
84 irq_data.args_count = 3;
86 irq_data.args[1] = hwirq - irqc_base;
88 (IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE |
89 IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL)) {
90 case IORESOURCE_IRQ_LOWEDGE:
91 irq_data.args[2] = IRQ_TYPE_EDGE_FALLING;
93 case IORESOURCE_IRQ_HIGHEDGE:
94 irq_data.args[2] = IRQ_TYPE_EDGE_RISING;
96 case IORESOURCE_IRQ_LOWLEVEL:
97 irq_data.args[2] = IRQ_TYPE_LEVEL_LOW;
99 case IORESOURCE_IRQ_HIGHLEVEL:
101 irq_data.args[2] = IRQ_TYPE_LEVEL_HIGH;
105 virq = irq_create_of_mapping(&irq_data);
109 pr_debug("hwirq %u -> virq %u\n", hwirq, virq);
113 void __init board_staging_gic_fixup_resources(struct resource *res,
118 for (i = 0; i < nres; i++)
119 gic_fixup_resource(&res[i]);
122 int __init board_staging_register_clock(const struct board_staging_clk *bsc)
126 pr_debug("Aliasing clock %s for con_id %s dev_id %s\n", bsc->clk,
127 bsc->con_id, bsc->dev_id);
128 error = clk_add_alias(bsc->con_id, bsc->dev_id, bsc->clk, NULL);
130 pr_err("Failed to alias clock %s (%d)\n", bsc->clk, error);
135 #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
136 static int board_staging_add_dev_domain(struct platform_device *pdev,
139 struct device *dev = &pdev->dev;
140 struct of_phandle_args pd_args;
141 struct device_node *np;
143 np = of_find_node_by_path(domain);
145 pr_err("Cannot find domain node %s\n", domain);
150 pd_args.args_count = 0;
152 /* Initialization similar to device_pm_init_common() */
153 spin_lock_init(&dev->power.lock);
154 dev->power.early_init = true;
156 return of_genpd_add_device(&pd_args, dev);
159 static inline int board_staging_add_dev_domain(struct platform_device *pdev,
166 int __init board_staging_register_device(const struct board_staging_dev *dev)
168 struct platform_device *pdev = dev->pdev;
172 pr_debug("Trying to register device %s\n", pdev->name);
173 if (board_staging_dt_node_available(pdev->resource,
174 pdev->num_resources)) {
175 pr_warn("Skipping %s, already in DT\n", pdev->name);
179 board_staging_gic_fixup_resources(pdev->resource, pdev->num_resources);
181 for (i = 0; i < dev->nclocks; i++)
182 board_staging_register_clock(&dev->clocks[i]);
185 board_staging_add_dev_domain(pdev, dev->domain);
187 error = platform_device_register(pdev);
189 pr_err("Failed to register device %s (%d)\n", pdev->name,
197 void __init board_staging_register_devices(const struct board_staging_dev *devs,
202 for (i = 0; i < ndevs; i++)
203 board_staging_register_device(&devs[i]);