xref: /linux/drivers/of/platform.c (revision 336157be7e93394901a8752354562449fcd3ee0f)
1af6074fcSRob Herring // SPDX-License-Identifier: GPL-2.0+
23f23de10SStephen Rothwell /*
33f23de10SStephen Rothwell  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
43f23de10SStephen Rothwell  *			 <benh@kernel.crashing.org>
53f23de10SStephen Rothwell  *    and		 Arnd Bergmann, IBM Corp.
63f23de10SStephen Rothwell  *    Merged from powerpc/kernel/of_platform.c and
73f23de10SStephen Rothwell  *    sparc{,64}/kernel/of_device.c by Stephen Rothwell
83f23de10SStephen Rothwell  */
9606ad42aSRob Herring 
10606ad42aSRob Herring #define pr_fmt(fmt)	"OF: " fmt
11606ad42aSRob Herring 
123f23de10SStephen Rothwell #include <linux/errno.h>
135c457083SStephen Rothwell #include <linux/module.h>
145de1540bSGrant Likely #include <linux/amba/bus.h>
153f23de10SStephen Rothwell #include <linux/device.h>
165fd200f3SGrant Likely #include <linux/dma-mapping.h>
1750ef5284SGrant Likely #include <linux/slab.h>
189e3288dcSGrant Likely #include <linux/of_address.h>
193f23de10SStephen Rothwell #include <linux/of_device.h>
209e3288dcSGrant Likely #include <linux/of_irq.h>
213f23de10SStephen Rothwell #include <linux/of_platform.h>
22eca39301SGrant Likely #include <linux/platform_device.h>
233310288fSJavier Martinez Canillas #include <linux/sysfb.h>
24eca39301SGrant Likely 
2573aca58bSRob Herring #include "of_private.h"
2673aca58bSRob Herring 
27cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
28cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
2922869a9eSLinus Walleij 	{ .compatible = "simple-mfd", },
30ecd76edeSPaul Burton 	{ .compatible = "isa", },
31cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
32cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
33cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
34cbb49c26SGrant Likely 	{} /* Empty terminated list */
35cbb49c26SGrant Likely };
36cbb49c26SGrant Likely 
37c6085584SJonas Bonn /**
38c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
39c6085584SJonas Bonn  * @np: Pointer to device tree node
40c6085584SJonas Bonn  *
414fb373dfSJohan Hovold  * Takes a reference to the embedded struct device which needs to be dropped
424fb373dfSJohan Hovold  * after use.
434fb373dfSJohan Hovold  *
448c8239c2SRob Herring  * Return: platform_device pointer, or NULL if not found
45c6085584SJonas Bonn  */
46c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
47c6085584SJonas Bonn {
48c6085584SJonas Bonn 	struct device *dev;
49c6085584SJonas Bonn 
50cfba5de9SSuzuki K Poulose 	dev = bus_find_device_by_of_node(&platform_bus_type, np);
51c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
52c6085584SJonas Bonn }
53c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
54c6085584SJonas Bonn 
5566a4210bSRob Herring int of_device_add(struct platform_device *ofdev)
5666a4210bSRob Herring {
5766a4210bSRob Herring 	BUG_ON(ofdev->dev.of_node == NULL);
5866a4210bSRob Herring 
5966a4210bSRob Herring 	/* name and id have to be set so that the platform bus doesn't get
6066a4210bSRob Herring 	 * confused on matching */
6166a4210bSRob Herring 	ofdev->name = dev_name(&ofdev->dev);
6266a4210bSRob Herring 	ofdev->id = PLATFORM_DEVID_NONE;
6366a4210bSRob Herring 
6466a4210bSRob Herring 	/*
6566a4210bSRob Herring 	 * If this device has not binding numa node in devicetree, that is
6666a4210bSRob Herring 	 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
6766a4210bSRob Herring 	 * device is on the same node as the parent.
6866a4210bSRob Herring 	 */
6966a4210bSRob Herring 	set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
7066a4210bSRob Herring 
7166a4210bSRob Herring 	return device_add(&ofdev->dev);
7266a4210bSRob Herring }
7366a4210bSRob Herring 
7466a4210bSRob Herring int of_device_register(struct platform_device *pdev)
7566a4210bSRob Herring {
7666a4210bSRob Herring 	device_initialize(&pdev->dev);
7766a4210bSRob Herring 	return of_device_add(pdev);
7866a4210bSRob Herring }
7966a4210bSRob Herring EXPORT_SYMBOL(of_device_register);
8066a4210bSRob Herring 
8166a4210bSRob Herring void of_device_unregister(struct platform_device *ofdev)
8266a4210bSRob Herring {
8366a4210bSRob Herring 	device_unregister(&ofdev->dev);
8466a4210bSRob Herring }
8566a4210bSRob Herring EXPORT_SYMBOL(of_device_unregister);
8666a4210bSRob Herring 
87964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS
88ef04d280SViresh Kumar static const struct of_device_id of_skipped_node_table[] = {
89ef04d280SViresh Kumar 	{ .compatible = "operating-points-v2", },
90ef04d280SViresh Kumar 	{} /* Empty terminated list */
91ef04d280SViresh Kumar };
92ef04d280SViresh Kumar 
935fd200f3SGrant Likely /*
945fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
955fd200f3SGrant Likely  * each applicable node.
965fd200f3SGrant Likely  *
975fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
985fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
995fd200f3SGrant Likely  */
1005fd200f3SGrant Likely 
1015fd200f3SGrant Likely /**
10294c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
10394c09319SGrant Likely  * @np: device node to assign to device
10494c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
10594c09319SGrant Likely  * @parent: Parent device.
10694c09319SGrant Likely  */
10794a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
10894c09319SGrant Likely 				  const char *bus_id,
10994c09319SGrant Likely 				  struct device *parent)
11094c09319SGrant Likely {
11194a0cb1fSGrant Likely 	struct platform_device *dev;
112a1a2b712SLad Prabhakar 	int rc, i, num_reg = 0;
11332e8f9b3SYang Yingliang 	struct resource *res;
11494c09319SGrant Likely 
1156d7e3bf8SAndy Shevchenko 	dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
1167096d042SGrant Likely 	if (!dev)
1177096d042SGrant Likely 		return NULL;
1187096d042SGrant Likely 
119a1a2b712SLad Prabhakar 	/* count the io resources */
12032e8f9b3SYang Yingliang 	num_reg = of_address_count(np);
121ac80a51eSGrant Likely 
122ac80a51eSGrant Likely 	/* Populate the resource table */
123a1a2b712SLad Prabhakar 	if (num_reg) {
124a1a2b712SLad Prabhakar 		res = kcalloc(num_reg, sizeof(*res), GFP_KERNEL);
1257096d042SGrant Likely 		if (!res) {
1267096d042SGrant Likely 			platform_device_put(dev);
1277096d042SGrant Likely 			return NULL;
1287096d042SGrant Likely 		}
1297096d042SGrant Likely 
130a1a2b712SLad Prabhakar 		dev->num_resources = num_reg;
131ac80a51eSGrant Likely 		dev->resource = res;
132ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
133ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
134ac80a51eSGrant Likely 			WARN_ON(rc);
135ac80a51eSGrant Likely 		}
136ac80a51eSGrant Likely 	}
13794c09319SGrant Likely 
1380f8e5651SAndy Shevchenko 	/* setup generic device info */
1397882541cSPeng Fan 	device_set_node(&dev->dev, of_fwnode_handle(of_node_get(np)));
14043c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
14194c09319SGrant Likely 
14294c09319SGrant Likely 	if (bus_id)
14394c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
14494c09319SGrant Likely 	else
14594c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
14694c09319SGrant Likely 
14794c09319SGrant Likely 	return dev;
14894c09319SGrant Likely }
14994c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
15094c09319SGrant Likely 
151591c1ee4SSantosh Shilimkar /**
15215c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1535fd200f3SGrant Likely  * @np: pointer to node to create device for
1545fd200f3SGrant Likely  * @bus_id: name to assign device
15515c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1565fd200f3SGrant Likely  * @parent: Linux device model parent device.
157cd1e6504SGrant Likely  *
1588c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
159cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1605fd200f3SGrant Likely  */
161245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
16215c3597dSGrant Likely 					struct device_node *np,
1635fd200f3SGrant Likely 					const char *bus_id,
16415c3597dSGrant Likely 					void *platform_data,
1655fd200f3SGrant Likely 					struct device *parent)
1665fd200f3SGrant Likely {
16794a0cb1fSGrant Likely 	struct platform_device *dev;
1685fd200f3SGrant Likely 
169*336157beSUwe Kleine-König 	pr_debug("create platform device: %pOF\n", np);
170*336157beSUwe Kleine-König 
171c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
172c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
173cd1e6504SGrant Likely 		return NULL;
174cd1e6504SGrant Likely 
1755fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
1765fd200f3SGrant Likely 	if (!dev)
177c6e126deSPawel Moll 		goto err_clear_flag;
1785fd200f3SGrant Likely 
179a5516219SRobin Murphy 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
180a5516219SRobin Murphy 	if (!dev->dev.dma_mask)
181a5516219SRobin Murphy 		dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
182eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
18315c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
184c706c239SMarc Zyngier 	of_msi_configure(&dev->dev, dev->dev.of_node);
1855fd200f3SGrant Likely 
1867096d042SGrant Likely 	if (of_device_add(dev) != 0) {
1877096d042SGrant Likely 		platform_device_put(dev);
188c6e126deSPawel Moll 		goto err_clear_flag;
1895fd200f3SGrant Likely 	}
1905fd200f3SGrant Likely 
1915fd200f3SGrant Likely 	return dev;
192c6e126deSPawel Moll 
193c6e126deSPawel Moll err_clear_flag:
194c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
195c6e126deSPawel Moll 	return NULL;
1965fd200f3SGrant Likely }
19715c3597dSGrant Likely 
19815c3597dSGrant Likely /**
19915c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
20015c3597dSGrant Likely  * @np: pointer to node to create device for
20115c3597dSGrant Likely  * @bus_id: name to assign device
20215c3597dSGrant Likely  * @parent: Linux device model parent device.
20315c3597dSGrant Likely  *
2048c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
20515c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
20615c3597dSGrant Likely  */
20715c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
20815c3597dSGrant Likely 					    const char *bus_id,
20915c3597dSGrant Likely 					    struct device *parent)
21015c3597dSGrant Likely {
21115c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
21215c3597dSGrant Likely }
2135fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2145fd200f3SGrant Likely 
2155de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2165de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2175de1540bSGrant Likely 						 const char *bus_id,
2185de1540bSGrant Likely 						 void *platform_data,
2195de1540bSGrant Likely 						 struct device *parent)
2205de1540bSGrant Likely {
2215de1540bSGrant Likely 	struct amba_device *dev;
222854f695cSWang Kefeng 	int ret;
2235de1540bSGrant Likely 
2240d638a07SRob Herring 	pr_debug("Creating amba device %pOF\n", node);
2255de1540bSGrant Likely 
226c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
227c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2285de1540bSGrant Likely 		return NULL;
2295de1540bSGrant Likely 
230c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
231606ad42aSRob Herring 	if (!dev)
232c6e126deSPawel Moll 		goto err_clear_flag;
2335de1540bSGrant Likely 
2348c89ef7bSLinus Walleij 	/* AMBA devices only support a single DMA mask */
2358c89ef7bSLinus Walleij 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2368c89ef7bSLinus Walleij 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
2378c89ef7bSLinus Walleij 
2385de1540bSGrant Likely 	/* setup generic device info */
239ca5a75dfSAndy Shevchenko 	device_set_node(&dev->dev, of_fwnode_handle(node));
24043c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
2415de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2425de1540bSGrant Likely 	if (bus_id)
2435de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
2445de1540bSGrant Likely 	else
2455de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
2465de1540bSGrant Likely 
2475de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
24816b0c7caSRob Herring 	of_property_read_u32(node, "arm,primecell-periphid", &dev->periphid);
2495de1540bSGrant Likely 
2505de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
2512bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2520d638a07SRob Herring 		pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
2530d638a07SRob Herring 		       ret, node);
2545de1540bSGrant Likely 		goto err_free;
2552bc552dfSBartlomiej Zolnierkiewicz 	}
2565de1540bSGrant Likely 
257c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
2582bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2590d638a07SRob Herring 		pr_err("amba_device_add() failed (%d) for %pOF\n",
2600d638a07SRob Herring 		       ret, node);
2615de1540bSGrant Likely 		goto err_free;
2622bc552dfSBartlomiej Zolnierkiewicz 	}
2635de1540bSGrant Likely 
2645de1540bSGrant Likely 	return dev;
2655de1540bSGrant Likely 
2665de1540bSGrant Likely err_free:
267c0f72f8aSRussell King 	amba_device_put(dev);
268c6e126deSPawel Moll err_clear_flag:
269c6e126deSPawel Moll 	of_node_clear_flag(node, OF_POPULATED);
2705de1540bSGrant Likely 	return NULL;
2715de1540bSGrant Likely }
2725de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
2735de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2745de1540bSGrant Likely 						 const char *bus_id,
2755de1540bSGrant Likely 						 void *platform_data,
2765de1540bSGrant Likely 						 struct device *parent)
2775de1540bSGrant Likely {
2785de1540bSGrant Likely 	return NULL;
2795de1540bSGrant Likely }
2805de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
2815de1540bSGrant Likely 
282f3896a7eSLee Jones /*
283f9a529b9SQi Zheng  * of_dev_lookup() - Given a device node, lookup the preferred Linux name
28415c3597dSGrant Likely  */
28515c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
28615c3597dSGrant Likely 				 struct device_node *np)
28715c3597dSGrant Likely {
288fc5cf80aSTony Lindgren 	const struct of_dev_auxdata *auxdata;
28915c3597dSGrant Likely 	struct resource res;
290fc5cf80aSTony Lindgren 	int compatible = 0;
291303f59d1SOlof Johansson 
292303f59d1SOlof Johansson 	if (!lookup)
293303f59d1SOlof Johansson 		return NULL;
294303f59d1SOlof Johansson 
295fc5cf80aSTony Lindgren 	auxdata = lookup;
296fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
297fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
29815c3597dSGrant Likely 			continue;
299fc5cf80aSTony Lindgren 		compatible++;
30084774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
301fc5cf80aSTony Lindgren 			if (res.start != auxdata->phys_addr)
30215c3597dSGrant Likely 				continue;
3030d638a07SRob Herring 		pr_debug("%pOF: devname=%s\n", np, auxdata->name);
304fc5cf80aSTony Lindgren 		return auxdata;
305fc5cf80aSTony Lindgren 	}
306fc5cf80aSTony Lindgren 
307fc5cf80aSTony Lindgren 	if (!compatible)
308fc5cf80aSTony Lindgren 		return NULL;
309fc5cf80aSTony Lindgren 
310fc5cf80aSTony Lindgren 	/* Try compatible match if no phys_addr and name are specified */
311fc5cf80aSTony Lindgren 	auxdata = lookup;
312fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
313fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
314fc5cf80aSTony Lindgren 			continue;
315fc5cf80aSTony Lindgren 		if (!auxdata->phys_addr && !auxdata->name) {
3160d638a07SRob Herring 			pr_debug("%pOF: compatible match\n", np);
317fc5cf80aSTony Lindgren 			return auxdata;
318fc5cf80aSTony Lindgren 		}
31915c3597dSGrant Likely 	}
320303f59d1SOlof Johansson 
32115c3597dSGrant Likely 	return NULL;
32215c3597dSGrant Likely }
32315c3597dSGrant Likely 
32415c3597dSGrant Likely /**
32538e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3265fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3271eed4c07SGrant Likely  * @matches: match table for bus nodes
328303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
32938e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
330303f59d1SOlof Johansson  * @strict: require compatible property
33138e9e21dSGrant Likely  *
33238e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
33338e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3345fd200f3SGrant Likely  */
33538e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3365fd200f3SGrant Likely 				  const struct of_device_id *matches,
33715c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
33829d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3395fd200f3SGrant Likely {
34015c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3415fd200f3SGrant Likely 	struct device_node *child;
34294a0cb1fSGrant Likely 	struct platform_device *dev;
34315c3597dSGrant Likely 	const char *bus_id = NULL;
34415c3597dSGrant Likely 	void *platform_data = NULL;
3455fd200f3SGrant Likely 	int rc = 0;
3465fd200f3SGrant Likely 
34729d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
34829d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
3490d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, no compatible prop\n",
3500d638a07SRob Herring 			 __func__, bus);
35129d4f8a4SGrant Likely 		return 0;
35229d4f8a4SGrant Likely 	}
35329d4f8a4SGrant Likely 
3544550fe63SViresh Kumar 	/* Skip nodes for which we don't want to create devices */
3554550fe63SViresh Kumar 	if (unlikely(of_match_node(of_skipped_node_table, bus))) {
3564550fe63SViresh Kumar 		pr_debug("%s() - skipping %pOF node\n", __func__, bus);
3574550fe63SViresh Kumar 		return 0;
3584550fe63SViresh Kumar 	}
3594550fe63SViresh Kumar 
36044a7185cSKefeng Wang 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
3610d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, already populated\n",
3620d638a07SRob Herring 			__func__, bus);
36344a7185cSKefeng Wang 		return 0;
36444a7185cSKefeng Wang 	}
36544a7185cSKefeng Wang 
36615c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
36715c3597dSGrant Likely 	if (auxdata) {
36815c3597dSGrant Likely 		bus_id = auxdata->name;
36915c3597dSGrant Likely 		platform_data = auxdata->platform_data;
37015c3597dSGrant Likely 	}
37115c3597dSGrant Likely 
3725de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
3732bc552dfSBartlomiej Zolnierkiewicz 		/*
3742bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
3752bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
3762bc552dfSBartlomiej Zolnierkiewicz 		 */
37715c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
3785de1540bSGrant Likely 		return 0;
3795de1540bSGrant Likely 	}
3805de1540bSGrant Likely 
38115c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
38238e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
38338e9e21dSGrant Likely 		return 0;
38438e9e21dSGrant Likely 
3855fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
3860d638a07SRob Herring 		pr_debug("   create child: %pOF\n", child);
38715c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
3885fd200f3SGrant Likely 		if (rc) {
3895fd200f3SGrant Likely 			of_node_put(child);
3905fd200f3SGrant Likely 			break;
3915fd200f3SGrant Likely 		}
3925fd200f3SGrant Likely 	}
39375f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
3945fd200f3SGrant Likely 	return rc;
3955fd200f3SGrant Likely }
3965fd200f3SGrant Likely 
3975fd200f3SGrant Likely /**
39838e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
3995fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
4001eed4c07SGrant Likely  * @matches: match table for bus nodes
4015fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
4025fd200f3SGrant Likely  *
4035fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
4045fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4055fd200f3SGrant Likely  */
4065fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4075fd200f3SGrant Likely 			  const struct of_device_id *matches,
4085fd200f3SGrant Likely 			  struct device *parent)
4095fd200f3SGrant Likely {
4105fd200f3SGrant Likely 	struct device_node *child;
4115fd200f3SGrant Likely 	int rc = 0;
4125fd200f3SGrant Likely 
4131eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4141eed4c07SGrant Likely 	if (!root)
41560d59913SGrant Likely 		return -EINVAL;
4165fd200f3SGrant Likely 
41744a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4180d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
4195fd200f3SGrant Likely 
4201eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4215fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
42215c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
42338e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4245fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4255fd200f3SGrant Likely 			continue;
42615c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
4277fad948aSJulia Lawall 		if (rc) {
4287fad948aSJulia Lawall 			of_node_put(child);
4295fd200f3SGrant Likely 			break;
4305fd200f3SGrant Likely 		}
4317fad948aSJulia Lawall 	}
43238e9e21dSGrant Likely 
4335fd200f3SGrant Likely 	of_node_put(root);
4345fd200f3SGrant Likely 	return rc;
4355fd200f3SGrant Likely }
4365fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
43729d4f8a4SGrant Likely 
43829d4f8a4SGrant Likely /**
43929d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
44029d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
44129d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
44292039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
44329d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
44429d4f8a4SGrant Likely  *
44529d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
44629d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
44729d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
44829d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
44929d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
45029d4f8a4SGrant Likely  * are selected by the @matches argument).
45129d4f8a4SGrant Likely  *
45229d4f8a4SGrant Likely  * New board support should be using this function instead of
45329d4f8a4SGrant Likely  * of_platform_bus_probe().
45429d4f8a4SGrant Likely  *
4558c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
45629d4f8a4SGrant Likely  */
45729d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
45829d4f8a4SGrant Likely 			const struct of_device_id *matches,
45915c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
46029d4f8a4SGrant Likely 			struct device *parent)
46129d4f8a4SGrant Likely {
46229d4f8a4SGrant Likely 	struct device_node *child;
46329d4f8a4SGrant Likely 	int rc = 0;
46429d4f8a4SGrant Likely 
46529d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
46629d4f8a4SGrant Likely 	if (!root)
46729d4f8a4SGrant Likely 		return -EINVAL;
46829d4f8a4SGrant Likely 
46944a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4700d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
47144a7185cSKefeng Wang 
4725e666938SSaravana Kannan 	device_links_supplier_sync_state_pause();
47329d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
47415c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
4757fad948aSJulia Lawall 		if (rc) {
4767fad948aSJulia Lawall 			of_node_put(child);
47729d4f8a4SGrant Likely 			break;
47829d4f8a4SGrant Likely 		}
4797fad948aSJulia Lawall 	}
4805e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
4815e666938SSaravana Kannan 
4822d0747c4SGrant Likely 	of_node_set_flag(root, OF_POPULATED_BUS);
48329d4f8a4SGrant Likely 
48429d4f8a4SGrant Likely 	of_node_put(root);
48529d4f8a4SGrant Likely 	return rc;
48629d4f8a4SGrant Likely }
487e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
488c6e126deSPawel Moll 
48943443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root,
49043443ad6SHauke Mehrtens 				 const struct of_dev_auxdata *lookup,
49143443ad6SHauke Mehrtens 				 struct device *parent)
49243443ad6SHauke Mehrtens {
49343443ad6SHauke Mehrtens 	return of_platform_populate(root, of_default_bus_match_table, lookup,
49443443ad6SHauke Mehrtens 				    parent);
49543443ad6SHauke Mehrtens }
49643443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate);
49743443ad6SHauke Mehrtens 
498a50ff19dSBjorn Andersson static const struct of_device_id reserved_mem_matches[] = {
4997090d2f1SVincent Whitchurch 	{ .compatible = "phram" },
500d1de6d6cSBjorn Andersson 	{ .compatible = "qcom,rmtfs-mem" },
501312416d9SMahesh Sivasubramanian 	{ .compatible = "qcom,cmd-db" },
502b5af64fcSBjorn Andersson 	{ .compatible = "qcom,smem" },
503a50ff19dSBjorn Andersson 	{ .compatible = "ramoops" },
5045a3fa75aSNicolas Saenz Julienne 	{ .compatible = "nvmem-rmem" },
505f396ededSDavid Brazdil 	{ .compatible = "google,open-dice" },
506a50ff19dSBjorn Andersson 	{}
507a50ff19dSBjorn Andersson };
508a50ff19dSBjorn Andersson 
50944a7185cSKefeng Wang static int __init of_platform_default_populate_init(void)
51044a7185cSKefeng Wang {
511529182e2SKees Cook 	struct device_node *node;
512529182e2SKees Cook 
513ee9b280eSSaravana Kannan 	device_links_supplier_sync_state_pause();
514ee9b280eSSaravana Kannan 
515529182e2SKees Cook 	if (!of_have_populated_dt())
516529182e2SKees Cook 		return -ENODEV;
517529182e2SKees Cook 
51852b1b46cSThomas Zimmermann 	if (IS_ENABLED(CONFIG_PPC)) {
51952b1b46cSThomas Zimmermann 		struct device_node *boot_display = NULL;
52052b1b46cSThomas Zimmermann 		struct platform_device *dev;
521241d2fb5SMichal Suchanek 		int display_number = 0;
52252b1b46cSThomas Zimmermann 		int ret;
52352b1b46cSThomas Zimmermann 
52452b1b46cSThomas Zimmermann 		/* Check if we have a MacOS display without a node spec */
5252f0cb475SRob Herring 		if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
52652b1b46cSThomas Zimmermann 			/*
52752b1b46cSThomas Zimmermann 			 * The old code tried to work out which node was the MacOS
52852b1b46cSThomas Zimmermann 			 * display based on the address. I'm dropping that since the
52952b1b46cSThomas Zimmermann 			 * lack of a node spec only happens with old BootX versions
53052b1b46cSThomas Zimmermann 			 * (users can update) and with this code, they'll still get
53152b1b46cSThomas Zimmermann 			 * a display (just not the palette hacks).
53252b1b46cSThomas Zimmermann 			 */
53352b1b46cSThomas Zimmermann 			dev = platform_device_alloc("bootx-noscreen", 0);
53452b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
53552b1b46cSThomas Zimmermann 				return -ENOMEM;
53652b1b46cSThomas Zimmermann 			ret = platform_device_add(dev);
53752b1b46cSThomas Zimmermann 			if (WARN_ON(ret)) {
53852b1b46cSThomas Zimmermann 				platform_device_put(dev);
53952b1b46cSThomas Zimmermann 				return ret;
54052b1b46cSThomas Zimmermann 			}
54152b1b46cSThomas Zimmermann 		}
54252b1b46cSThomas Zimmermann 
54352b1b46cSThomas Zimmermann 		/*
54452b1b46cSThomas Zimmermann 		 * For OF framebuffers, first create the device for the boot display,
54552b1b46cSThomas Zimmermann 		 * then for the other framebuffers. Only fail for the boot display;
54652b1b46cSThomas Zimmermann 		 * ignore errors for the rest.
54752b1b46cSThomas Zimmermann 		 */
54852b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
54952b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) ||
55052b1b46cSThomas Zimmermann 			    !of_get_property(node, "linux,boot-display", NULL))
55152b1b46cSThomas Zimmermann 				continue;
5520bb8f49cSRob Herring 			dev = of_platform_device_create(node, "of-display", NULL);
553241d2fb5SMichal Suchanek 			of_node_put(node);
55452b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
55552b1b46cSThomas Zimmermann 				return -ENOMEM;
55652b1b46cSThomas Zimmermann 			boot_display = node;
557241d2fb5SMichal Suchanek 			display_number++;
55852b1b46cSThomas Zimmermann 			break;
55952b1b46cSThomas Zimmermann 		}
56052b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
561241d2fb5SMichal Suchanek 			char buf[14];
562241d2fb5SMichal Suchanek 			const char *of_display_format = "of-display.%d";
563241d2fb5SMichal Suchanek 
56452b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
56552b1b46cSThomas Zimmermann 				continue;
566241d2fb5SMichal Suchanek 			ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
567241d2fb5SMichal Suchanek 			if (ret < sizeof(buf))
568241d2fb5SMichal Suchanek 				of_platform_device_create(node, buf, NULL);
56952b1b46cSThomas Zimmermann 		}
57052b1b46cSThomas Zimmermann 
57152b1b46cSThomas Zimmermann 	} else {
572529182e2SKees Cook 		/*
573a50ff19dSBjorn Andersson 		 * Handle certain compatibles explicitly, since we don't want to create
574a50ff19dSBjorn Andersson 		 * platform_devices for every node in /reserved-memory with a
575a50ff19dSBjorn Andersson 		 * "compatible",
576529182e2SKees Cook 		 */
577a50ff19dSBjorn Andersson 		for_each_matching_node(node, reserved_mem_matches)
578529182e2SKees Cook 			of_platform_device_create(node, NULL, NULL);
579529182e2SKees Cook 
5803aa0582fSSudeep Holla 		node = of_find_node_by_path("/firmware");
581e2105ca8SSudeep Holla 		if (node) {
5823aa0582fSSudeep Holla 			of_platform_populate(node, NULL, NULL, NULL);
583e2105ca8SSudeep Holla 			of_node_put(node);
584e2105ca8SSudeep Holla 		}
5853aa0582fSSudeep Holla 
5862f92ea21SHector Martin 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
5873310288fSJavier Martinez Canillas 		if (node) {
5883310288fSJavier Martinez Canillas 			/*
5893310288fSJavier Martinez Canillas 			 * Since a "simple-framebuffer" device is already added
5903310288fSJavier Martinez Canillas 			 * here, disable the Generic System Framebuffers (sysfb)
5913310288fSJavier Martinez Canillas 			 * to prevent it from registering another device for the
5923310288fSJavier Martinez Canillas 			 * system framebuffer later (e.g: using the screen_info
5933310288fSJavier Martinez Canillas 			 * data that may had been filled as well).
5943310288fSJavier Martinez Canillas 			 *
5953310288fSJavier Martinez Canillas 			 * This can happen for example on DT systems that do EFI
5963310288fSJavier Martinez Canillas 			 * booting and may provide a GOP handle to the EFI stub.
5973310288fSJavier Martinez Canillas 			 */
5983310288fSJavier Martinez Canillas 			sysfb_disable();
5992f92ea21SHector Martin 			of_platform_device_create(node, NULL, NULL);
6002f92ea21SHector Martin 			of_node_put(node);
6013310288fSJavier Martinez Canillas 		}
6022f92ea21SHector Martin 
603529182e2SKees Cook 		/* Populate everything else. */
60444a7185cSKefeng Wang 		of_platform_default_populate(NULL, NULL, NULL);
60552b1b46cSThomas Zimmermann 	}
60644a7185cSKefeng Wang 
60744a7185cSKefeng Wang 	return 0;
60844a7185cSKefeng Wang }
60944a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init);
6105e666938SSaravana Kannan 
6115e666938SSaravana Kannan static int __init of_platform_sync_state_init(void)
6125e666938SSaravana Kannan {
6135e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
6145e666938SSaravana Kannan 	return 0;
6155e666938SSaravana Kannan }
6165e666938SSaravana Kannan late_initcall_sync(of_platform_sync_state_init);
61744a7185cSKefeng Wang 
618c2372c20SJan Glauber int of_platform_device_destroy(struct device *dev, void *data)
619c6e126deSPawel Moll {
620c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
62175f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
622c6e126deSPawel Moll 		return 0;
623c6e126deSPawel Moll 
62475f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
62575f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
62675f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
627c6e126deSPawel Moll 
628522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED);
629522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
630522811e9SSrinivas Kandagatla 
631c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
632c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
633c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
634c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
635c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
636c6e126deSPawel Moll #endif
637c6e126deSPawel Moll 
638c6e126deSPawel Moll 	return 0;
639c6e126deSPawel Moll }
640c2372c20SJan Glauber EXPORT_SYMBOL_GPL(of_platform_device_destroy);
641c6e126deSPawel Moll 
642c6e126deSPawel Moll /**
643c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
64475f353b6SGrant Likely  * @parent: device which children will be removed
645c6e126deSPawel Moll  *
646c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
6471080b5c0SJohan Hovold  * of the given device (and, recursively, their children) that have been
648c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
649c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
650c6e126deSPawel Moll  */
65175f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
652c6e126deSPawel Moll {
6532d0747c4SGrant Likely 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
65470a29209SThierry Reding 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
6552d0747c4SGrant Likely 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
6562d0747c4SGrant Likely 	}
657c6e126deSPawel Moll }
658c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
659c6e126deSPawel Moll 
66038b0b219SBenjamin Gaignard static void devm_of_platform_populate_release(struct device *dev, void *res)
66138b0b219SBenjamin Gaignard {
66238b0b219SBenjamin Gaignard 	of_platform_depopulate(*(struct device **)res);
66338b0b219SBenjamin Gaignard }
66438b0b219SBenjamin Gaignard 
66538b0b219SBenjamin Gaignard /**
66638b0b219SBenjamin Gaignard  * devm_of_platform_populate() - Populate platform_devices from device tree data
66738b0b219SBenjamin Gaignard  * @dev: device that requested to populate from device tree data
66838b0b219SBenjamin Gaignard  *
66938b0b219SBenjamin Gaignard  * Similar to of_platform_populate(), but will automatically call
67038b0b219SBenjamin Gaignard  * of_platform_depopulate() when the device is unbound from the bus.
67138b0b219SBenjamin Gaignard  *
6728c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
67338b0b219SBenjamin Gaignard  */
67438b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev)
67538b0b219SBenjamin Gaignard {
67638b0b219SBenjamin Gaignard 	struct device **ptr;
67738b0b219SBenjamin Gaignard 	int ret;
67838b0b219SBenjamin Gaignard 
67938b0b219SBenjamin Gaignard 	if (!dev)
68038b0b219SBenjamin Gaignard 		return -EINVAL;
68138b0b219SBenjamin Gaignard 
68238b0b219SBenjamin Gaignard 	ptr = devres_alloc(devm_of_platform_populate_release,
68338b0b219SBenjamin Gaignard 			   sizeof(*ptr), GFP_KERNEL);
68438b0b219SBenjamin Gaignard 	if (!ptr)
68538b0b219SBenjamin Gaignard 		return -ENOMEM;
68638b0b219SBenjamin Gaignard 
68738b0b219SBenjamin Gaignard 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
68838b0b219SBenjamin Gaignard 	if (ret) {
68938b0b219SBenjamin Gaignard 		devres_free(ptr);
69038b0b219SBenjamin Gaignard 	} else {
69138b0b219SBenjamin Gaignard 		*ptr = dev;
69238b0b219SBenjamin Gaignard 		devres_add(dev, ptr);
69338b0b219SBenjamin Gaignard 	}
69438b0b219SBenjamin Gaignard 
69538b0b219SBenjamin Gaignard 	return ret;
69638b0b219SBenjamin Gaignard }
69738b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate);
69838b0b219SBenjamin Gaignard 
69938b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data)
70038b0b219SBenjamin Gaignard {
70138b0b219SBenjamin Gaignard 	struct device **ptr = res;
70238b0b219SBenjamin Gaignard 
70338b0b219SBenjamin Gaignard 	if (!ptr) {
70438b0b219SBenjamin Gaignard 		WARN_ON(!ptr);
70538b0b219SBenjamin Gaignard 		return 0;
70638b0b219SBenjamin Gaignard 	}
70738b0b219SBenjamin Gaignard 
70838b0b219SBenjamin Gaignard 	return *ptr == data;
70938b0b219SBenjamin Gaignard }
71038b0b219SBenjamin Gaignard 
71138b0b219SBenjamin Gaignard /**
71238b0b219SBenjamin Gaignard  * devm_of_platform_depopulate() - Remove devices populated from device tree
71338b0b219SBenjamin Gaignard  * @dev: device that requested to depopulate from device tree data
71438b0b219SBenjamin Gaignard  *
71538b0b219SBenjamin Gaignard  * Complementary to devm_of_platform_populate(), this function removes children
7161080b5c0SJohan Hovold  * of the given device (and, recursively, their children) that have been
71738b0b219SBenjamin Gaignard  * created from their respective device tree nodes (and only those,
71838b0b219SBenjamin Gaignard  * leaving others - eg. manually created - unharmed).
71938b0b219SBenjamin Gaignard  */
72038b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev)
72138b0b219SBenjamin Gaignard {
72238b0b219SBenjamin Gaignard 	int ret;
72338b0b219SBenjamin Gaignard 
72438b0b219SBenjamin Gaignard 	ret = devres_release(dev, devm_of_platform_populate_release,
72538b0b219SBenjamin Gaignard 			     devm_of_platform_match, dev);
72638b0b219SBenjamin Gaignard 
72738b0b219SBenjamin Gaignard 	WARN_ON(ret);
72838b0b219SBenjamin Gaignard }
72938b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
73038b0b219SBenjamin Gaignard 
731801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC
732801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb,
733801d728cSPantelis Antoniou 				unsigned long action, void *arg)
734801d728cSPantelis Antoniou {
735801d728cSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
736801d728cSPantelis Antoniou 	struct platform_device *pdev_parent, *pdev;
737801d728cSPantelis Antoniou 	bool children_left;
738801d728cSPantelis Antoniou 
739801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
740801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
741801d728cSPantelis Antoniou 		/* verify that the parent is a bus */
742801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
743801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
744801d728cSPantelis Antoniou 
74515204ab1SPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
74615204ab1SPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
74715204ab1SPantelis Antoniou 			return NOTIFY_OK;
74815204ab1SPantelis Antoniou 
7491a50d940SGeert Uytterhoeven 		/*
7501a50d940SGeert Uytterhoeven 		 * Clear the flag before adding the device so that fw_devlink
7511a50d940SGeert Uytterhoeven 		 * doesn't skip adding consumers to this device.
7521a50d940SGeert Uytterhoeven 		 */
7531a50d940SGeert Uytterhoeven 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
754801d728cSPantelis Antoniou 		/* pdev_parent may be NULL when no bus platform device */
755801d728cSPantelis Antoniou 		pdev_parent = of_find_device_by_node(rd->dn->parent);
756801d728cSPantelis Antoniou 		pdev = of_platform_device_create(rd->dn, NULL,
757801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
75883c4a4eeSRob Herring 		platform_device_put(pdev_parent);
759801d728cSPantelis Antoniou 
760801d728cSPantelis Antoniou 		if (pdev == NULL) {
7610d638a07SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
7620d638a07SRob Herring 					__func__, rd->dn);
763801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
764801d728cSPantelis Antoniou 			return notifier_from_errno(-EINVAL);
765801d728cSPantelis Antoniou 		}
766801d728cSPantelis Antoniou 		break;
767801d728cSPantelis Antoniou 
768801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
76915204ab1SPantelis Antoniou 
77015204ab1SPantelis Antoniou 		/* already depopulated? */
77115204ab1SPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
77215204ab1SPantelis Antoniou 			return NOTIFY_OK;
77315204ab1SPantelis Antoniou 
774801d728cSPantelis Antoniou 		/* find our device by node */
775801d728cSPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
776801d728cSPantelis Antoniou 		if (pdev == NULL)
777801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
778801d728cSPantelis Antoniou 
779801d728cSPantelis Antoniou 		/* unregister takes one ref away */
780801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
781801d728cSPantelis Antoniou 
782801d728cSPantelis Antoniou 		/* and put the reference of the find */
78383c4a4eeSRob Herring 		platform_device_put(pdev);
784801d728cSPantelis Antoniou 		break;
785801d728cSPantelis Antoniou 	}
786801d728cSPantelis Antoniou 
787801d728cSPantelis Antoniou 	return NOTIFY_OK;
788801d728cSPantelis Antoniou }
789801d728cSPantelis Antoniou 
790801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
791801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
792801d728cSPantelis Antoniou };
793801d728cSPantelis Antoniou 
794801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
795801d728cSPantelis Antoniou {
796801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
797801d728cSPantelis Antoniou }
798801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
799801d728cSPantelis Antoniou 
800964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
801