xref: /linux/drivers/of/platform.c (revision 42b16d3ac371a2fac9b6f08fd75f23f34ba3955a)
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  */
of_find_device_by_node(struct device_node * np)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 
of_device_add(struct platform_device * ofdev)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 
of_device_register(struct platform_device * pdev)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 
of_device_unregister(struct platform_device * ofdev)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  */
of_device_alloc(struct device_node * np,const char * bus_id,struct device * parent)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  */
of_platform_device_create_pdata(struct device_node * np,const char * bus_id,void * platform_data,struct device * parent)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 
169336157beSUwe Kleine-König 	pr_debug("create platform device: %pOF\n", np);
170336157beSUwe 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  */
of_platform_device_create(struct device_node * np,const char * bus_id,struct device * parent)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
of_amba_device_create(struct device_node * node,const char * bus_id,void * platform_data,struct device * parent)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 */
of_amba_device_create(struct device_node * node,const char * bus_id,void * platform_data,struct device * parent)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  */
of_dev_lookup(const struct of_dev_auxdata * lookup,struct device_node * np)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  */
of_platform_bus_create(struct device_node * bus,const struct of_device_id * matches,const struct of_dev_auxdata * lookup,struct device * parent,bool strict)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 platform_device *dev;
34294a0cb1fSGrant Likely 	const char *bus_id = NULL;
34315c3597dSGrant Likely 	void *platform_data = NULL;
34415c3597dSGrant Likely 	int rc = 0;
3455fd200f3SGrant Likely 
3465fd200f3SGrant Likely 	/* Make sure it has a compatible property */
34729d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
34829d4f8a4SGrant Likely 		pr_debug("%s() - skipping %pOF, no compatible prop\n",
3490d638a07SRob Herring 			 __func__, bus);
3500d638a07SRob Herring 		return 0;
35129d4f8a4SGrant Likely 	}
35229d4f8a4SGrant Likely 
35329d4f8a4SGrant Likely 	/* Skip nodes for which we don't want to create devices */
3544550fe63SViresh Kumar 	if (unlikely(of_match_node(of_skipped_node_table, bus))) {
3554550fe63SViresh Kumar 		pr_debug("%s() - skipping %pOF node\n", __func__, bus);
3564550fe63SViresh Kumar 		return 0;
3574550fe63SViresh Kumar 	}
3584550fe63SViresh Kumar 
3594550fe63SViresh Kumar 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
36044a7185cSKefeng Wang 		pr_debug("%s() - skipping %pOF, already populated\n",
3610d638a07SRob Herring 			__func__, bus);
3620d638a07SRob Herring 		return 0;
36344a7185cSKefeng Wang 	}
36444a7185cSKefeng Wang 
36544a7185cSKefeng Wang 	auxdata = of_dev_lookup(lookup, bus);
36615c3597dSGrant Likely 	if (auxdata) {
36715c3597dSGrant Likely 		bus_id = auxdata->name;
36815c3597dSGrant Likely 		platform_data = auxdata->platform_data;
36915c3597dSGrant Likely 	}
37015c3597dSGrant Likely 
37115c3597dSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
3725de1540bSGrant Likely 		/*
3732bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
3742bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
3752bc552dfSBartlomiej Zolnierkiewicz 		 */
3762bc552dfSBartlomiej Zolnierkiewicz 		of_amba_device_create(bus, bus_id, platform_data, parent);
37715c3597dSGrant Likely 		return 0;
3785de1540bSGrant Likely 	}
3795de1540bSGrant Likely 
3805de1540bSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
38115c3597dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
38238e9e21dSGrant Likely 		return 0;
38338e9e21dSGrant Likely 
38438e9e21dSGrant Likely 	for_each_child_of_node_scoped(bus, child) {
3855fd200f3SGrant Likely 		pr_debug("   create child: %pOF\n", child);
3860d638a07SRob Herring 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
38715c3597dSGrant Likely 		if (rc)
3885fd200f3SGrant Likely 			break;
3895fd200f3SGrant Likely 	}
3905fd200f3SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
3915fd200f3SGrant Likely 	return rc;
3925fd200f3SGrant Likely }
39375f353b6SGrant Likely 
3945fd200f3SGrant Likely /**
3955fd200f3SGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
3965fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
3975fd200f3SGrant Likely  * @matches: match table for bus nodes
39838e9e21dSGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
3995fd200f3SGrant Likely  *
4001eed4c07SGrant Likely  * Note that children of the provided root are not instantiated as devices
4015fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4025fd200f3SGrant Likely  */
of_platform_bus_probe(struct device_node * root,const struct of_device_id * matches,struct device * parent)4035fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4045fd200f3SGrant Likely 			  const struct of_device_id *matches,
4055fd200f3SGrant Likely 			  struct device *parent)
4065fd200f3SGrant Likely {
4075fd200f3SGrant Likely 	struct device_node *child;
4085fd200f3SGrant Likely 	int rc = 0;
4095fd200f3SGrant Likely 
4105fd200f3SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4115fd200f3SGrant Likely 	if (!root)
4125fd200f3SGrant Likely 		return -EINVAL;
4131eed4c07SGrant Likely 
4141eed4c07SGrant Likely 	pr_debug("%s()\n", __func__);
41560d59913SGrant Likely 	pr_debug(" starting at: %pOF\n", root);
4165fd200f3SGrant Likely 
41744a7185cSKefeng Wang 	/* Do a self check of bus type, if there's a match, create children */
4180d638a07SRob Herring 	if (of_match_node(matches, root)) {
4195fd200f3SGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
4201eed4c07SGrant Likely 	} else for_each_child_of_node(root, child) {
4215fd200f3SGrant Likely 		if (!of_match_node(matches, child))
42215c3597dSGrant Likely 			continue;
42338e9e21dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
4245fd200f3SGrant Likely 		if (rc) {
4255fd200f3SGrant Likely 			of_node_put(child);
42615c3597dSGrant Likely 			break;
4277fad948aSJulia Lawall 		}
4287fad948aSJulia Lawall 	}
4295fd200f3SGrant Likely 
4305fd200f3SGrant Likely 	of_node_put(root);
4317fad948aSJulia Lawall 	return rc;
43238e9e21dSGrant Likely }
4335fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
4345fd200f3SGrant Likely 
4355fd200f3SGrant Likely /**
4365fd200f3SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
43729d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
43829d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
43929d4f8a4SGrant Likely  * @lookup: auxdata table for matching id and platform_data with device nodes
44029d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
44129d4f8a4SGrant Likely  *
44292039ed1SJavi Merino  * Similar to of_platform_bus_probe(), this function walks the device tree
44329d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
44429d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
44529d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
44629d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
44729d4f8a4SGrant Likely  * are selected by the @matches argument).
44829d4f8a4SGrant Likely  *
44929d4f8a4SGrant Likely  * New board support should be using this function instead of
45029d4f8a4SGrant Likely  * of_platform_bus_probe().
45129d4f8a4SGrant Likely  *
45229d4f8a4SGrant Likely  * Return: 0 on success, < 0 on failure.
45329d4f8a4SGrant Likely  */
of_platform_populate(struct device_node * root,const struct of_device_id * matches,const struct of_dev_auxdata * lookup,struct device * parent)45429d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
4558c8239c2SRob Herring 			const struct of_device_id *matches,
45629d4f8a4SGrant Likely 			const struct of_dev_auxdata *lookup,
45729d4f8a4SGrant Likely 			struct device *parent)
45829d4f8a4SGrant Likely {
45915c3597dSGrant Likely 	int rc = 0;
46029d4f8a4SGrant Likely 
46129d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
46229d4f8a4SGrant Likely 	if (!root)
46329d4f8a4SGrant Likely 		return -EINVAL;
46429d4f8a4SGrant Likely 
46529d4f8a4SGrant Likely 	pr_debug("%s()\n", __func__);
46629d4f8a4SGrant Likely 	pr_debug(" starting at: %pOF\n", root);
46729d4f8a4SGrant Likely 
46829d4f8a4SGrant Likely 	device_links_supplier_sync_state_pause();
46944a7185cSKefeng Wang 	for_each_child_of_node_scoped(root, child) {
4700d638a07SRob Herring 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
47144a7185cSKefeng Wang 		if (rc)
4725e666938SSaravana Kannan 			break;
47329d4f8a4SGrant Likely 	}
47415c3597dSGrant Likely 	device_links_supplier_sync_state_resume();
4757fad948aSJulia Lawall 
4767fad948aSJulia Lawall 	of_node_set_flag(root, OF_POPULATED_BUS);
47729d4f8a4SGrant Likely 
47829d4f8a4SGrant Likely 	of_node_put(root);
4797fad948aSJulia Lawall 	return rc;
4805e666938SSaravana Kannan }
4815e666938SSaravana Kannan EXPORT_SYMBOL_GPL(of_platform_populate);
4822d0747c4SGrant Likely 
of_platform_default_populate(struct device_node * root,const struct of_dev_auxdata * lookup,struct device * parent)48329d4f8a4SGrant Likely int of_platform_default_populate(struct device_node *root,
48429d4f8a4SGrant Likely 				 const struct of_dev_auxdata *lookup,
48529d4f8a4SGrant Likely 				 struct device *parent)
48629d4f8a4SGrant Likely {
487e001f1c8SStephen Warren 	return of_platform_populate(root, of_default_bus_match_table, lookup,
488c6e126deSPawel Moll 				    parent);
48943443ad6SHauke Mehrtens }
49043443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate);
49143443ad6SHauke Mehrtens 
49243443ad6SHauke Mehrtens static const struct of_device_id reserved_mem_matches[] = {
49343443ad6SHauke Mehrtens 	{ .compatible = "phram" },
49443443ad6SHauke Mehrtens 	{ .compatible = "qcom,rmtfs-mem" },
49543443ad6SHauke Mehrtens 	{ .compatible = "qcom,cmd-db" },
49643443ad6SHauke Mehrtens 	{ .compatible = "qcom,smem" },
49743443ad6SHauke Mehrtens 	{ .compatible = "ramoops" },
498a50ff19dSBjorn Andersson 	{ .compatible = "nvmem-rmem" },
4997090d2f1SVincent Whitchurch 	{ .compatible = "google,open-dice" },
500d1de6d6cSBjorn Andersson 	{}
501312416d9SMahesh Sivasubramanian };
502b5af64fcSBjorn Andersson 
of_platform_default_populate_init(void)503a50ff19dSBjorn Andersson static int __init of_platform_default_populate_init(void)
5045a3fa75aSNicolas Saenz Julienne {
505f396ededSDavid Brazdil 	struct device_node *node;
506a50ff19dSBjorn Andersson 
507a50ff19dSBjorn Andersson 	device_links_supplier_sync_state_pause();
508a50ff19dSBjorn Andersson 
50944a7185cSKefeng Wang 	if (IS_ENABLED(CONFIG_PPC)) {
51044a7185cSKefeng Wang 		struct device_node *boot_display = NULL;
511529182e2SKees Cook 		struct platform_device *dev;
512529182e2SKees Cook 		int display_number = 0;
513ee9b280eSSaravana Kannan 		int ret;
514ee9b280eSSaravana Kannan 
51552b1b46cSThomas Zimmermann 		/* Check if we have a MacOS display without a node spec */
51652b1b46cSThomas Zimmermann 		if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
51752b1b46cSThomas Zimmermann 			/*
518241d2fb5SMichal Suchanek 			 * The old code tried to work out which node was the MacOS
51952b1b46cSThomas Zimmermann 			 * display based on the address. I'm dropping that since the
52052b1b46cSThomas Zimmermann 			 * lack of a node spec only happens with old BootX versions
52152b1b46cSThomas Zimmermann 			 * (users can update) and with this code, they'll still get
5222f0cb475SRob Herring 			 * a display (just not the palette hacks).
52352b1b46cSThomas Zimmermann 			 */
52452b1b46cSThomas Zimmermann 			dev = platform_device_alloc("bootx-noscreen", 0);
52552b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
52652b1b46cSThomas Zimmermann 				return -ENOMEM;
52752b1b46cSThomas Zimmermann 			ret = platform_device_add(dev);
52852b1b46cSThomas Zimmermann 			if (WARN_ON(ret)) {
52952b1b46cSThomas Zimmermann 				platform_device_put(dev);
53052b1b46cSThomas Zimmermann 				return ret;
53152b1b46cSThomas Zimmermann 			}
53252b1b46cSThomas Zimmermann 		}
53352b1b46cSThomas Zimmermann 
53452b1b46cSThomas Zimmermann 		/*
53552b1b46cSThomas Zimmermann 		 * For OF framebuffers, first create the device for the boot display,
53652b1b46cSThomas Zimmermann 		 * then for the other framebuffers. Only fail for the boot display;
53752b1b46cSThomas Zimmermann 		 * ignore errors for the rest.
53852b1b46cSThomas Zimmermann 		 */
53952b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
54052b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) ||
54152b1b46cSThomas Zimmermann 			    !of_get_property(node, "linux,boot-display", NULL))
54252b1b46cSThomas Zimmermann 				continue;
54352b1b46cSThomas Zimmermann 			dev = of_platform_device_create(node, "of-display", NULL);
54452b1b46cSThomas Zimmermann 			of_node_put(node);
54552b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
54652b1b46cSThomas Zimmermann 				return -ENOMEM;
54752b1b46cSThomas Zimmermann 			boot_display = node;
54852b1b46cSThomas Zimmermann 			display_number++;
5490bb8f49cSRob Herring 			break;
550241d2fb5SMichal Suchanek 		}
55152b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
55252b1b46cSThomas Zimmermann 			char buf[14];
55352b1b46cSThomas Zimmermann 			const char *of_display_format = "of-display.%d";
554241d2fb5SMichal Suchanek 
55552b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
55652b1b46cSThomas Zimmermann 				continue;
55752b1b46cSThomas Zimmermann 			ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
558241d2fb5SMichal Suchanek 			if (ret < sizeof(buf))
559241d2fb5SMichal Suchanek 				of_platform_device_create(node, buf, NULL);
560241d2fb5SMichal Suchanek 		}
56152b1b46cSThomas Zimmermann 
56252b1b46cSThomas Zimmermann 	} else {
563241d2fb5SMichal Suchanek 		/*
564241d2fb5SMichal Suchanek 		 * Handle certain compatibles explicitly, since we don't want to create
565241d2fb5SMichal Suchanek 		 * platform_devices for every node in /reserved-memory with a
56652b1b46cSThomas Zimmermann 		 * "compatible",
56752b1b46cSThomas Zimmermann 		 */
56852b1b46cSThomas Zimmermann 		for_each_matching_node(node, reserved_mem_matches)
569529182e2SKees Cook 			of_platform_device_create(node, NULL, NULL);
570a50ff19dSBjorn Andersson 
571a50ff19dSBjorn Andersson 		node = of_find_node_by_path("/firmware");
572a50ff19dSBjorn Andersson 		if (node) {
573529182e2SKees Cook 			of_platform_populate(node, NULL, NULL, NULL);
574a50ff19dSBjorn Andersson 			of_node_put(node);
575529182e2SKees Cook 		}
576529182e2SKees Cook 
5773aa0582fSSudeep Holla 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
578e2105ca8SSudeep Holla 		if (node) {
5793aa0582fSSudeep Holla 			/*
580e2105ca8SSudeep Holla 			 * Since a "simple-framebuffer" device is already added
581e2105ca8SSudeep Holla 			 * here, disable the Generic System Framebuffers (sysfb)
5823aa0582fSSudeep Holla 			 * to prevent it from registering another device for the
5832f92ea21SHector Martin 			 * system framebuffer later (e.g: using the screen_info
5843310288fSJavier Martinez Canillas 			 * data that may had been filled as well).
5853310288fSJavier Martinez Canillas 			 *
5863310288fSJavier Martinez Canillas 			 * This can happen for example on DT systems that do EFI
5873310288fSJavier Martinez Canillas 			 * booting and may provide a GOP handle to the EFI stub.
5883310288fSJavier Martinez Canillas 			 */
5893310288fSJavier Martinez Canillas 			sysfb_disable(NULL);
5903310288fSJavier Martinez Canillas 			of_platform_device_create(node, NULL, NULL);
5913310288fSJavier Martinez Canillas 			of_node_put(node);
5923310288fSJavier Martinez Canillas 		}
5933310288fSJavier Martinez Canillas 
5943310288fSJavier Martinez Canillas 		/* Populate everything else. */
595*b49420d6SAlex Deucher 		of_platform_default_populate(NULL, NULL, NULL);
5962f92ea21SHector Martin 	}
5972f92ea21SHector Martin 
5983310288fSJavier Martinez Canillas 	return 0;
5992f92ea21SHector Martin }
600529182e2SKees Cook arch_initcall_sync(of_platform_default_populate_init);
60144a7185cSKefeng Wang 
of_platform_sync_state_init(void)60252b1b46cSThomas Zimmermann static int __init of_platform_sync_state_init(void)
60344a7185cSKefeng Wang {
60444a7185cSKefeng Wang 	device_links_supplier_sync_state_resume();
60544a7185cSKefeng Wang 	return 0;
60644a7185cSKefeng Wang }
6075e666938SSaravana Kannan late_initcall_sync(of_platform_sync_state_init);
6085e666938SSaravana Kannan 
of_platform_device_destroy(struct device * dev,void * data)6095e666938SSaravana Kannan int of_platform_device_destroy(struct device *dev, void *data)
6105e666938SSaravana Kannan {
6115e666938SSaravana Kannan 	/* Do not touch devices not populated from the device tree */
6125e666938SSaravana Kannan 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
6135e666938SSaravana Kannan 		return 0;
61444a7185cSKefeng Wang 
615c2372c20SJan Glauber 	/* Recurse for any nodes that were treated as busses */
616c6e126deSPawel Moll 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
617c6e126deSPawel Moll 		device_for_each_child(dev, NULL, of_platform_device_destroy);
61875f353b6SGrant Likely 
619c6e126deSPawel Moll 	of_node_clear_flag(dev->of_node, OF_POPULATED);
620c6e126deSPawel Moll 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
62175f353b6SGrant Likely 
62275f353b6SGrant Likely 	if (dev->bus == &platform_bus_type)
62375f353b6SGrant Likely 		platform_device_unregister(to_platform_device(dev));
624c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
625522811e9SSrinivas Kandagatla 	else if (dev->bus == &amba_bustype)
626522811e9SSrinivas Kandagatla 		amba_device_unregister(to_amba_device(dev));
627522811e9SSrinivas Kandagatla #endif
628c6e126deSPawel Moll 
629c6e126deSPawel Moll 	return 0;
630c6e126deSPawel Moll }
631c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_device_destroy);
632c6e126deSPawel Moll 
633c6e126deSPawel Moll /**
634c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
635c6e126deSPawel Moll  * @parent: device which children will be removed
636c6e126deSPawel Moll  *
637c2372c20SJan Glauber  * Complementary to of_platform_populate(), this function removes children
638c6e126deSPawel Moll  * of the given device (and, recursively, their children) that have been
639c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
640c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
64175f353b6SGrant Likely  */
of_platform_depopulate(struct device * parent)642c6e126deSPawel Moll void of_platform_depopulate(struct device *parent)
643c6e126deSPawel Moll {
6441080b5c0SJohan Hovold 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
645c6e126deSPawel Moll 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
646c6e126deSPawel Moll 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
647c6e126deSPawel Moll 	}
64875f353b6SGrant Likely }
649c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
6502d0747c4SGrant Likely 
devm_of_platform_populate_release(struct device * dev,void * res)65170a29209SThierry Reding static void devm_of_platform_populate_release(struct device *dev, void *res)
6522d0747c4SGrant Likely {
6532d0747c4SGrant Likely 	of_platform_depopulate(*(struct device **)res);
654c6e126deSPawel Moll }
655c6e126deSPawel Moll 
656c6e126deSPawel Moll /**
65738b0b219SBenjamin Gaignard  * devm_of_platform_populate() - Populate platform_devices from device tree data
65838b0b219SBenjamin Gaignard  * @dev: device that requested to populate from device tree data
65938b0b219SBenjamin Gaignard  *
66038b0b219SBenjamin Gaignard  * Similar to of_platform_populate(), but will automatically call
66138b0b219SBenjamin Gaignard  * of_platform_depopulate() when the device is unbound from the bus.
66238b0b219SBenjamin Gaignard  *
66338b0b219SBenjamin Gaignard  * Return: 0 on success, < 0 on failure.
66438b0b219SBenjamin Gaignard  */
devm_of_platform_populate(struct device * dev)66538b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev)
66638b0b219SBenjamin Gaignard {
66738b0b219SBenjamin Gaignard 	struct device **ptr;
66838b0b219SBenjamin Gaignard 	int ret;
6698c8239c2SRob Herring 
67038b0b219SBenjamin Gaignard 	if (!dev)
67138b0b219SBenjamin Gaignard 		return -EINVAL;
67238b0b219SBenjamin Gaignard 
67338b0b219SBenjamin Gaignard 	ptr = devres_alloc(devm_of_platform_populate_release,
67438b0b219SBenjamin Gaignard 			   sizeof(*ptr), GFP_KERNEL);
67538b0b219SBenjamin Gaignard 	if (!ptr)
67638b0b219SBenjamin Gaignard 		return -ENOMEM;
67738b0b219SBenjamin Gaignard 
67838b0b219SBenjamin Gaignard 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
67938b0b219SBenjamin Gaignard 	if (ret) {
68038b0b219SBenjamin Gaignard 		devres_free(ptr);
68138b0b219SBenjamin Gaignard 	} else {
68238b0b219SBenjamin Gaignard 		*ptr = dev;
68338b0b219SBenjamin Gaignard 		devres_add(dev, ptr);
68438b0b219SBenjamin Gaignard 	}
68538b0b219SBenjamin Gaignard 
68638b0b219SBenjamin Gaignard 	return ret;
68738b0b219SBenjamin Gaignard }
68838b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate);
68938b0b219SBenjamin Gaignard 
devm_of_platform_match(struct device * dev,void * res,void * data)69038b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data)
69138b0b219SBenjamin Gaignard {
69238b0b219SBenjamin Gaignard 	struct device **ptr = res;
69338b0b219SBenjamin Gaignard 
69438b0b219SBenjamin Gaignard 	if (!ptr) {
69538b0b219SBenjamin Gaignard 		WARN_ON(!ptr);
69638b0b219SBenjamin Gaignard 		return 0;
69738b0b219SBenjamin Gaignard 	}
69838b0b219SBenjamin Gaignard 
69938b0b219SBenjamin Gaignard 	return *ptr == data;
70038b0b219SBenjamin Gaignard }
70138b0b219SBenjamin Gaignard 
70238b0b219SBenjamin Gaignard /**
70338b0b219SBenjamin Gaignard  * devm_of_platform_depopulate() - Remove devices populated from device tree
70438b0b219SBenjamin Gaignard  * @dev: device that requested to depopulate from device tree data
70538b0b219SBenjamin Gaignard  *
70638b0b219SBenjamin Gaignard  * Complementary to devm_of_platform_populate(), this function removes children
70738b0b219SBenjamin Gaignard  * of the given device (and, recursively, their children) that have been
70838b0b219SBenjamin Gaignard  * created from their respective device tree nodes (and only those,
70938b0b219SBenjamin Gaignard  * leaving others - eg. manually created - unharmed).
71038b0b219SBenjamin Gaignard  */
devm_of_platform_depopulate(struct device * dev)71138b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev)
71238b0b219SBenjamin Gaignard {
7131080b5c0SJohan Hovold 	int ret;
71438b0b219SBenjamin Gaignard 
71538b0b219SBenjamin Gaignard 	ret = devres_release(dev, devm_of_platform_populate_release,
71638b0b219SBenjamin Gaignard 			     devm_of_platform_match, dev);
71738b0b219SBenjamin Gaignard 
71838b0b219SBenjamin Gaignard 	WARN_ON(ret);
71938b0b219SBenjamin Gaignard }
72038b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
72138b0b219SBenjamin Gaignard 
72238b0b219SBenjamin Gaignard #ifdef CONFIG_OF_DYNAMIC
of_platform_notify(struct notifier_block * nb,unsigned long action,void * arg)72338b0b219SBenjamin Gaignard static int of_platform_notify(struct notifier_block *nb,
72438b0b219SBenjamin Gaignard 				unsigned long action, void *arg)
72538b0b219SBenjamin Gaignard {
72638b0b219SBenjamin Gaignard 	struct of_reconfig_data *rd = arg;
72738b0b219SBenjamin Gaignard 	struct platform_device *pdev_parent, *pdev;
728801d728cSPantelis Antoniou 	bool children_left;
729801d728cSPantelis Antoniou 	struct device_node *parent;
730801d728cSPantelis Antoniou 
731801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
732801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
733801d728cSPantelis Antoniou 		parent = rd->dn->parent;
734801d728cSPantelis Antoniou 		/* verify that the parent is a bus (or the root node) */
735801d728cSPantelis Antoniou 		if (!of_node_is_root(parent) &&
736801d728cSPantelis Antoniou 		    !of_node_check_flag(parent, OF_POPULATED_BUS))
737801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
738801d728cSPantelis Antoniou 
739801d728cSPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
740801d728cSPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
741801d728cSPantelis Antoniou 			return NOTIFY_OK;
74215204ab1SPantelis Antoniou 
74315204ab1SPantelis Antoniou 		/*
74415204ab1SPantelis Antoniou 		 * Clear the flag before adding the device so that fw_devlink
74515204ab1SPantelis Antoniou 		 * doesn't skip adding consumers to this device.
7461a50d940SGeert Uytterhoeven 		 */
7471a50d940SGeert Uytterhoeven 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
7481a50d940SGeert Uytterhoeven 		/* pdev_parent may be NULL when no bus platform device */
7491a50d940SGeert Uytterhoeven 		pdev_parent = of_find_device_by_node(parent);
7501a50d940SGeert Uytterhoeven 		pdev = of_platform_device_create(rd->dn, NULL,
751801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
752801d728cSPantelis Antoniou 		platform_device_put(pdev_parent);
753801d728cSPantelis Antoniou 
754801d728cSPantelis Antoniou 		if (pdev == NULL) {
75583c4a4eeSRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
756801d728cSPantelis Antoniou 					__func__, rd->dn);
757801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
7580d638a07SRob Herring 			return notifier_from_errno(-EINVAL);
7590d638a07SRob Herring 		}
760801d728cSPantelis Antoniou 		break;
761801d728cSPantelis Antoniou 
762801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
763801d728cSPantelis Antoniou 
764801d728cSPantelis Antoniou 		/* already depopulated? */
765801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
76615204ab1SPantelis Antoniou 			return NOTIFY_OK;
76715204ab1SPantelis Antoniou 
76815204ab1SPantelis Antoniou 		/* find our device by node */
76915204ab1SPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
77015204ab1SPantelis Antoniou 		if (pdev == NULL)
771801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
772801d728cSPantelis Antoniou 
773801d728cSPantelis Antoniou 		/* unregister takes one ref away */
774801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
775801d728cSPantelis Antoniou 
776801d728cSPantelis Antoniou 		/* and put the reference of the find */
777801d728cSPantelis Antoniou 		platform_device_put(pdev);
778801d728cSPantelis Antoniou 		break;
779801d728cSPantelis Antoniou 	}
78083c4a4eeSRob Herring 
781801d728cSPantelis Antoniou 	return NOTIFY_OK;
782801d728cSPantelis Antoniou }
783801d728cSPantelis Antoniou 
784801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
785801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
786801d728cSPantelis Antoniou };
787801d728cSPantelis Antoniou 
of_platform_register_reconfig_notifier(void)788801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
789801d728cSPantelis Antoniou {
790801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
791801d728cSPantelis Antoniou }
792801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
793801d728cSPantelis Antoniou 
794801d728cSPantelis Antoniou #endif /* CONFIG_OF_ADDRESS */
795801d728cSPantelis Antoniou