xref: /linux/drivers/of/platform.c (revision 66a4210bc82e024e6de0f94298ad9230984a5924)
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>
23eca39301SGrant Likely 
2473aca58bSRob Herring #include "of_private.h"
2573aca58bSRob Herring 
26cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
27cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
2822869a9eSLinus Walleij 	{ .compatible = "simple-mfd", },
29ecd76edeSPaul Burton 	{ .compatible = "isa", },
30cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
31cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
32cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
33cbb49c26SGrant Likely 	{} /* Empty terminated list */
34cbb49c26SGrant Likely };
35cbb49c26SGrant Likely 
364550fe63SViresh Kumar static const struct of_device_id of_skipped_node_table[] = {
374550fe63SViresh Kumar 	{ .compatible = "operating-points-v2", },
384550fe63SViresh Kumar 	{} /* Empty terminated list */
394550fe63SViresh Kumar };
404550fe63SViresh Kumar 
41c6085584SJonas Bonn /**
42c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
43c6085584SJonas Bonn  * @np: Pointer to device tree node
44c6085584SJonas Bonn  *
454fb373dfSJohan Hovold  * Takes a reference to the embedded struct device which needs to be dropped
464fb373dfSJohan Hovold  * after use.
474fb373dfSJohan Hovold  *
488c8239c2SRob Herring  * Return: platform_device pointer, or NULL if not found
49c6085584SJonas Bonn  */
50c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
51c6085584SJonas Bonn {
52c6085584SJonas Bonn 	struct device *dev;
53c6085584SJonas Bonn 
54cfba5de9SSuzuki K Poulose 	dev = bus_find_device_by_of_node(&platform_bus_type, np);
55c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
56c6085584SJonas Bonn }
57c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
58c6085584SJonas Bonn 
59*66a4210bSRob Herring int of_device_add(struct platform_device *ofdev)
60*66a4210bSRob Herring {
61*66a4210bSRob Herring 	BUG_ON(ofdev->dev.of_node == NULL);
62*66a4210bSRob Herring 
63*66a4210bSRob Herring 	/* name and id have to be set so that the platform bus doesn't get
64*66a4210bSRob Herring 	 * confused on matching */
65*66a4210bSRob Herring 	ofdev->name = dev_name(&ofdev->dev);
66*66a4210bSRob Herring 	ofdev->id = PLATFORM_DEVID_NONE;
67*66a4210bSRob Herring 
68*66a4210bSRob Herring 	/*
69*66a4210bSRob Herring 	 * If this device has not binding numa node in devicetree, that is
70*66a4210bSRob Herring 	 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
71*66a4210bSRob Herring 	 * device is on the same node as the parent.
72*66a4210bSRob Herring 	 */
73*66a4210bSRob Herring 	set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
74*66a4210bSRob Herring 
75*66a4210bSRob Herring 	return device_add(&ofdev->dev);
76*66a4210bSRob Herring }
77*66a4210bSRob Herring 
78*66a4210bSRob Herring int of_device_register(struct platform_device *pdev)
79*66a4210bSRob Herring {
80*66a4210bSRob Herring 	device_initialize(&pdev->dev);
81*66a4210bSRob Herring 	return of_device_add(pdev);
82*66a4210bSRob Herring }
83*66a4210bSRob Herring EXPORT_SYMBOL(of_device_register);
84*66a4210bSRob Herring 
85*66a4210bSRob Herring void of_device_unregister(struct platform_device *ofdev)
86*66a4210bSRob Herring {
87*66a4210bSRob Herring 	device_unregister(&ofdev->dev);
88*66a4210bSRob Herring }
89*66a4210bSRob Herring EXPORT_SYMBOL(of_device_unregister);
90*66a4210bSRob Herring 
91964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS
925fd200f3SGrant Likely /*
935fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
945fd200f3SGrant Likely  * each applicable node.
955fd200f3SGrant Likely  *
965fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
975fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
985fd200f3SGrant Likely  */
995fd200f3SGrant Likely 
1005fd200f3SGrant Likely /**
10194c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
10294c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
10394c09319SGrant Likely  *
10407e461cdSGrant Likely  * This routine will first try using the translated bus address to
10507e461cdSGrant Likely  * derive a unique name. If it cannot, then it will prepend names from
10607e461cdSGrant Likely  * parent nodes until a unique name can be derived.
10794c09319SGrant Likely  */
108e553f539SFrank Rowand static void of_device_make_bus_id(struct device *dev)
10994c09319SGrant Likely {
11094c09319SGrant Likely 	struct device_node *node = dev->of_node;
11124fb530fSKim Phillips 	const __be32 *reg;
11294c09319SGrant Likely 	u64 addr;
11368d16195SRob Herring 	u32 mask;
11494c09319SGrant Likely 
11507e461cdSGrant Likely 	/* Construct the name, using parent nodes if necessary to ensure uniqueness */
11607e461cdSGrant Likely 	while (node->parent) {
11794c09319SGrant Likely 		/*
11807e461cdSGrant Likely 		 * If the address can be translated, then that is as much
11907e461cdSGrant Likely 		 * uniqueness as we need. Make it the first component and return
12094c09319SGrant Likely 		 */
12194c09319SGrant Likely 		reg = of_get_property(node, "reg", NULL);
12207e461cdSGrant Likely 		if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
12368d16195SRob Herring 			if (!of_property_read_u32(node, "mask", &mask))
12468d16195SRob Herring 				dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
12568d16195SRob Herring 					     addr, ffs(mask) - 1, node, dev_name(dev));
12668d16195SRob Herring 
12768d16195SRob Herring 			else
128a613b26aSRob Herring 				dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
129d88590dcSGeert Uytterhoeven 					     addr, node, dev_name(dev));
13094c09319SGrant Likely 			return;
13194c09319SGrant Likely 		}
13294c09319SGrant Likely 
13307e461cdSGrant Likely 		/* format arguments only used if dev_name() resolves to NULL */
13407e461cdSGrant Likely 		dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
13595e6b1faSRob Herring 			     kbasename(node->full_name), dev_name(dev));
13607e461cdSGrant Likely 		node = node->parent;
13707e461cdSGrant Likely 	}
13894c09319SGrant Likely }
13994c09319SGrant Likely 
14094c09319SGrant Likely /**
14194c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
14294c09319SGrant Likely  * @np: device node to assign to device
14394c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
14494c09319SGrant Likely  * @parent: Parent device.
14594c09319SGrant Likely  */
14694a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
14794c09319SGrant Likely 				  const char *bus_id,
14894c09319SGrant Likely 				  struct device *parent)
14994c09319SGrant Likely {
15094a0cb1fSGrant Likely 	struct platform_device *dev;
151a1a2b712SLad Prabhakar 	int rc, i, num_reg = 0;
15232e8f9b3SYang Yingliang 	struct resource *res;
15394c09319SGrant Likely 
1546d7e3bf8SAndy Shevchenko 	dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
1557096d042SGrant Likely 	if (!dev)
1567096d042SGrant Likely 		return NULL;
1577096d042SGrant Likely 
158a1a2b712SLad Prabhakar 	/* count the io resources */
15932e8f9b3SYang Yingliang 	num_reg = of_address_count(np);
160ac80a51eSGrant Likely 
161ac80a51eSGrant Likely 	/* Populate the resource table */
162a1a2b712SLad Prabhakar 	if (num_reg) {
163a1a2b712SLad Prabhakar 		res = kcalloc(num_reg, sizeof(*res), GFP_KERNEL);
1647096d042SGrant Likely 		if (!res) {
1657096d042SGrant Likely 			platform_device_put(dev);
1667096d042SGrant Likely 			return NULL;
1677096d042SGrant Likely 		}
1687096d042SGrant Likely 
169a1a2b712SLad Prabhakar 		dev->num_resources = num_reg;
170ac80a51eSGrant Likely 		dev->resource = res;
171ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
172ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
173ac80a51eSGrant Likely 			WARN_ON(rc);
174ac80a51eSGrant Likely 		}
175ac80a51eSGrant Likely 	}
17694c09319SGrant Likely 
1770f8e5651SAndy Shevchenko 	/* setup generic device info */
1780f8e5651SAndy Shevchenko 	device_set_node(&dev->dev, of_fwnode_handle(np));
17943c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
18094c09319SGrant Likely 
18194c09319SGrant Likely 	if (bus_id)
18294c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
18394c09319SGrant Likely 	else
18494c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
18594c09319SGrant Likely 
18694c09319SGrant Likely 	return dev;
18794c09319SGrant Likely }
18894c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
18994c09319SGrant Likely 
190591c1ee4SSantosh Shilimkar /**
19115c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1925fd200f3SGrant Likely  * @np: pointer to node to create device for
1935fd200f3SGrant Likely  * @bus_id: name to assign device
19415c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1955fd200f3SGrant Likely  * @parent: Linux device model parent device.
196cd1e6504SGrant Likely  *
1978c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
198cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1995fd200f3SGrant Likely  */
200245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
20115c3597dSGrant Likely 					struct device_node *np,
2025fd200f3SGrant Likely 					const char *bus_id,
20315c3597dSGrant Likely 					void *platform_data,
2045fd200f3SGrant Likely 					struct device *parent)
2055fd200f3SGrant Likely {
20694a0cb1fSGrant Likely 	struct platform_device *dev;
2075fd200f3SGrant Likely 
208c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
209c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
210cd1e6504SGrant Likely 		return NULL;
211cd1e6504SGrant Likely 
2125fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
2135fd200f3SGrant Likely 	if (!dev)
214c6e126deSPawel Moll 		goto err_clear_flag;
2155fd200f3SGrant Likely 
216a5516219SRobin Murphy 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
217a5516219SRobin Murphy 	if (!dev->dev.dma_mask)
218a5516219SRobin Murphy 		dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
219eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
22015c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
221c706c239SMarc Zyngier 	of_msi_configure(&dev->dev, dev->dev.of_node);
2225fd200f3SGrant Likely 
2237096d042SGrant Likely 	if (of_device_add(dev) != 0) {
2247096d042SGrant Likely 		platform_device_put(dev);
225c6e126deSPawel Moll 		goto err_clear_flag;
2265fd200f3SGrant Likely 	}
2275fd200f3SGrant Likely 
2285fd200f3SGrant Likely 	return dev;
229c6e126deSPawel Moll 
230c6e126deSPawel Moll err_clear_flag:
231c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
232c6e126deSPawel Moll 	return NULL;
2335fd200f3SGrant Likely }
23415c3597dSGrant Likely 
23515c3597dSGrant Likely /**
23615c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
23715c3597dSGrant Likely  * @np: pointer to node to create device for
23815c3597dSGrant Likely  * @bus_id: name to assign device
23915c3597dSGrant Likely  * @parent: Linux device model parent device.
24015c3597dSGrant Likely  *
2418c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
24215c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
24315c3597dSGrant Likely  */
24415c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
24515c3597dSGrant Likely 					    const char *bus_id,
24615c3597dSGrant Likely 					    struct device *parent)
24715c3597dSGrant Likely {
24815c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
24915c3597dSGrant Likely }
2505fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2515fd200f3SGrant Likely 
2525de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2535de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2545de1540bSGrant Likely 						 const char *bus_id,
2555de1540bSGrant Likely 						 void *platform_data,
2565de1540bSGrant Likely 						 struct device *parent)
2575de1540bSGrant Likely {
2585de1540bSGrant Likely 	struct amba_device *dev;
259854f695cSWang Kefeng 	int ret;
2605de1540bSGrant Likely 
2610d638a07SRob Herring 	pr_debug("Creating amba device %pOF\n", node);
2625de1540bSGrant Likely 
263c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
264c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2655de1540bSGrant Likely 		return NULL;
2665de1540bSGrant Likely 
267c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
268606ad42aSRob Herring 	if (!dev)
269c6e126deSPawel Moll 		goto err_clear_flag;
2705de1540bSGrant Likely 
2718c89ef7bSLinus Walleij 	/* AMBA devices only support a single DMA mask */
2728c89ef7bSLinus Walleij 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2738c89ef7bSLinus Walleij 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
2748c89ef7bSLinus Walleij 
2755de1540bSGrant Likely 	/* setup generic device info */
2760f8e5651SAndy Shevchenko 	device_set_node(&dev->dev, of_fwnode_handle(node));
27743c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
2785de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2795de1540bSGrant Likely 	if (bus_id)
2805de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
2815de1540bSGrant Likely 	else
2825de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
2835de1540bSGrant Likely 
2845de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
28516b0c7caSRob Herring 	of_property_read_u32(node, "arm,primecell-periphid", &dev->periphid);
2865de1540bSGrant Likely 
2875de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
2882bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2890d638a07SRob Herring 		pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
2900d638a07SRob Herring 		       ret, node);
2915de1540bSGrant Likely 		goto err_free;
2922bc552dfSBartlomiej Zolnierkiewicz 	}
2935de1540bSGrant Likely 
294c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
2952bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2960d638a07SRob Herring 		pr_err("amba_device_add() failed (%d) for %pOF\n",
2970d638a07SRob Herring 		       ret, node);
2985de1540bSGrant Likely 		goto err_free;
2992bc552dfSBartlomiej Zolnierkiewicz 	}
3005de1540bSGrant Likely 
3015de1540bSGrant Likely 	return dev;
3025de1540bSGrant Likely 
3035de1540bSGrant Likely err_free:
304c0f72f8aSRussell King 	amba_device_put(dev);
305c6e126deSPawel Moll err_clear_flag:
306c6e126deSPawel Moll 	of_node_clear_flag(node, OF_POPULATED);
3075de1540bSGrant Likely 	return NULL;
3085de1540bSGrant Likely }
3095de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
3105de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
3115de1540bSGrant Likely 						 const char *bus_id,
3125de1540bSGrant Likely 						 void *platform_data,
3135de1540bSGrant Likely 						 struct device *parent)
3145de1540bSGrant Likely {
3155de1540bSGrant Likely 	return NULL;
3165de1540bSGrant Likely }
3175de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
3185de1540bSGrant Likely 
319f3896a7eSLee Jones /*
320f9a529b9SQi Zheng  * of_dev_lookup() - Given a device node, lookup the preferred Linux name
32115c3597dSGrant Likely  */
32215c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
32315c3597dSGrant Likely 				 struct device_node *np)
32415c3597dSGrant Likely {
325fc5cf80aSTony Lindgren 	const struct of_dev_auxdata *auxdata;
32615c3597dSGrant Likely 	struct resource res;
327fc5cf80aSTony Lindgren 	int compatible = 0;
328303f59d1SOlof Johansson 
329303f59d1SOlof Johansson 	if (!lookup)
330303f59d1SOlof Johansson 		return NULL;
331303f59d1SOlof Johansson 
332fc5cf80aSTony Lindgren 	auxdata = lookup;
333fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
334fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
33515c3597dSGrant Likely 			continue;
336fc5cf80aSTony Lindgren 		compatible++;
33784774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
338fc5cf80aSTony Lindgren 			if (res.start != auxdata->phys_addr)
33915c3597dSGrant Likely 				continue;
3400d638a07SRob Herring 		pr_debug("%pOF: devname=%s\n", np, auxdata->name);
341fc5cf80aSTony Lindgren 		return auxdata;
342fc5cf80aSTony Lindgren 	}
343fc5cf80aSTony Lindgren 
344fc5cf80aSTony Lindgren 	if (!compatible)
345fc5cf80aSTony Lindgren 		return NULL;
346fc5cf80aSTony Lindgren 
347fc5cf80aSTony Lindgren 	/* Try compatible match if no phys_addr and name are specified */
348fc5cf80aSTony Lindgren 	auxdata = lookup;
349fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
350fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
351fc5cf80aSTony Lindgren 			continue;
352fc5cf80aSTony Lindgren 		if (!auxdata->phys_addr && !auxdata->name) {
3530d638a07SRob Herring 			pr_debug("%pOF: compatible match\n", np);
354fc5cf80aSTony Lindgren 			return auxdata;
355fc5cf80aSTony Lindgren 		}
35615c3597dSGrant Likely 	}
357303f59d1SOlof Johansson 
35815c3597dSGrant Likely 	return NULL;
35915c3597dSGrant Likely }
36015c3597dSGrant Likely 
36115c3597dSGrant Likely /**
36238e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3635fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3641eed4c07SGrant Likely  * @matches: match table for bus nodes
365303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
36638e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
367303f59d1SOlof Johansson  * @strict: require compatible property
36838e9e21dSGrant Likely  *
36938e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
37038e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3715fd200f3SGrant Likely  */
37238e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3735fd200f3SGrant Likely 				  const struct of_device_id *matches,
37415c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
37529d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3765fd200f3SGrant Likely {
37715c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3785fd200f3SGrant Likely 	struct device_node *child;
37994a0cb1fSGrant Likely 	struct platform_device *dev;
38015c3597dSGrant Likely 	const char *bus_id = NULL;
38115c3597dSGrant Likely 	void *platform_data = NULL;
3825fd200f3SGrant Likely 	int rc = 0;
3835fd200f3SGrant Likely 
38429d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
38529d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
3860d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, no compatible prop\n",
3870d638a07SRob Herring 			 __func__, bus);
38829d4f8a4SGrant Likely 		return 0;
38929d4f8a4SGrant Likely 	}
39029d4f8a4SGrant Likely 
3914550fe63SViresh Kumar 	/* Skip nodes for which we don't want to create devices */
3924550fe63SViresh Kumar 	if (unlikely(of_match_node(of_skipped_node_table, bus))) {
3934550fe63SViresh Kumar 		pr_debug("%s() - skipping %pOF node\n", __func__, bus);
3944550fe63SViresh Kumar 		return 0;
3954550fe63SViresh Kumar 	}
3964550fe63SViresh Kumar 
39744a7185cSKefeng Wang 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
3980d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, already populated\n",
3990d638a07SRob Herring 			__func__, bus);
40044a7185cSKefeng Wang 		return 0;
40144a7185cSKefeng Wang 	}
40244a7185cSKefeng Wang 
40315c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
40415c3597dSGrant Likely 	if (auxdata) {
40515c3597dSGrant Likely 		bus_id = auxdata->name;
40615c3597dSGrant Likely 		platform_data = auxdata->platform_data;
40715c3597dSGrant Likely 	}
40815c3597dSGrant Likely 
4095de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
4102bc552dfSBartlomiej Zolnierkiewicz 		/*
4112bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
4122bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
4132bc552dfSBartlomiej Zolnierkiewicz 		 */
41415c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
4155de1540bSGrant Likely 		return 0;
4165de1540bSGrant Likely 	}
4175de1540bSGrant Likely 
41815c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
41938e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
42038e9e21dSGrant Likely 		return 0;
42138e9e21dSGrant Likely 
4225fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
4230d638a07SRob Herring 		pr_debug("   create child: %pOF\n", child);
42415c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
4255fd200f3SGrant Likely 		if (rc) {
4265fd200f3SGrant Likely 			of_node_put(child);
4275fd200f3SGrant Likely 			break;
4285fd200f3SGrant Likely 		}
4295fd200f3SGrant Likely 	}
43075f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
4315fd200f3SGrant Likely 	return rc;
4325fd200f3SGrant Likely }
4335fd200f3SGrant Likely 
4345fd200f3SGrant Likely /**
43538e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
4365fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
4371eed4c07SGrant Likely  * @matches: match table for bus nodes
4385fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
4395fd200f3SGrant Likely  *
4405fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
4415fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4425fd200f3SGrant Likely  */
4435fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4445fd200f3SGrant Likely 			  const struct of_device_id *matches,
4455fd200f3SGrant Likely 			  struct device *parent)
4465fd200f3SGrant Likely {
4475fd200f3SGrant Likely 	struct device_node *child;
4485fd200f3SGrant Likely 	int rc = 0;
4495fd200f3SGrant Likely 
4501eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4511eed4c07SGrant Likely 	if (!root)
45260d59913SGrant Likely 		return -EINVAL;
4535fd200f3SGrant Likely 
45444a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4550d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
4565fd200f3SGrant Likely 
4571eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4585fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
45915c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
46038e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4615fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4625fd200f3SGrant Likely 			continue;
46315c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
4647fad948aSJulia Lawall 		if (rc) {
4657fad948aSJulia Lawall 			of_node_put(child);
4665fd200f3SGrant Likely 			break;
4675fd200f3SGrant Likely 		}
4687fad948aSJulia Lawall 	}
46938e9e21dSGrant Likely 
4705fd200f3SGrant Likely 	of_node_put(root);
4715fd200f3SGrant Likely 	return rc;
4725fd200f3SGrant Likely }
4735fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
47429d4f8a4SGrant Likely 
47529d4f8a4SGrant Likely /**
47629d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
47729d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
47829d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
47992039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
48029d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
48129d4f8a4SGrant Likely  *
48229d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
48329d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
48429d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
48529d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
48629d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
48729d4f8a4SGrant Likely  * are selected by the @matches argument).
48829d4f8a4SGrant Likely  *
48929d4f8a4SGrant Likely  * New board support should be using this function instead of
49029d4f8a4SGrant Likely  * of_platform_bus_probe().
49129d4f8a4SGrant Likely  *
4928c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
49329d4f8a4SGrant Likely  */
49429d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
49529d4f8a4SGrant Likely 			const struct of_device_id *matches,
49615c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
49729d4f8a4SGrant Likely 			struct device *parent)
49829d4f8a4SGrant Likely {
49929d4f8a4SGrant Likely 	struct device_node *child;
50029d4f8a4SGrant Likely 	int rc = 0;
50129d4f8a4SGrant Likely 
50229d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
50329d4f8a4SGrant Likely 	if (!root)
50429d4f8a4SGrant Likely 		return -EINVAL;
50529d4f8a4SGrant Likely 
50644a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
5070d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
50844a7185cSKefeng Wang 
5095e666938SSaravana Kannan 	device_links_supplier_sync_state_pause();
51029d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
51115c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
5127fad948aSJulia Lawall 		if (rc) {
5137fad948aSJulia Lawall 			of_node_put(child);
51429d4f8a4SGrant Likely 			break;
51529d4f8a4SGrant Likely 		}
5167fad948aSJulia Lawall 	}
5175e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
5185e666938SSaravana Kannan 
5192d0747c4SGrant Likely 	of_node_set_flag(root, OF_POPULATED_BUS);
52029d4f8a4SGrant Likely 
52129d4f8a4SGrant Likely 	of_node_put(root);
52229d4f8a4SGrant Likely 	return rc;
52329d4f8a4SGrant Likely }
524e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
525c6e126deSPawel Moll 
52643443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root,
52743443ad6SHauke Mehrtens 				 const struct of_dev_auxdata *lookup,
52843443ad6SHauke Mehrtens 				 struct device *parent)
52943443ad6SHauke Mehrtens {
53043443ad6SHauke Mehrtens 	return of_platform_populate(root, of_default_bus_match_table, lookup,
53143443ad6SHauke Mehrtens 				    parent);
53243443ad6SHauke Mehrtens }
53343443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate);
53443443ad6SHauke Mehrtens 
535a50ff19dSBjorn Andersson static const struct of_device_id reserved_mem_matches[] = {
5367090d2f1SVincent Whitchurch 	{ .compatible = "phram" },
537d1de6d6cSBjorn Andersson 	{ .compatible = "qcom,rmtfs-mem" },
538312416d9SMahesh Sivasubramanian 	{ .compatible = "qcom,cmd-db" },
539b5af64fcSBjorn Andersson 	{ .compatible = "qcom,smem" },
540a50ff19dSBjorn Andersson 	{ .compatible = "ramoops" },
5415a3fa75aSNicolas Saenz Julienne 	{ .compatible = "nvmem-rmem" },
542f396ededSDavid Brazdil 	{ .compatible = "google,open-dice" },
543a50ff19dSBjorn Andersson 	{}
544a50ff19dSBjorn Andersson };
545a50ff19dSBjorn Andersson 
54644a7185cSKefeng Wang static int __init of_platform_default_populate_init(void)
54744a7185cSKefeng Wang {
548529182e2SKees Cook 	struct device_node *node;
549529182e2SKees Cook 
550ee9b280eSSaravana Kannan 	device_links_supplier_sync_state_pause();
551ee9b280eSSaravana Kannan 
552529182e2SKees Cook 	if (!of_have_populated_dt())
553529182e2SKees Cook 		return -ENODEV;
554529182e2SKees Cook 
55552b1b46cSThomas Zimmermann 	if (IS_ENABLED(CONFIG_PPC)) {
55652b1b46cSThomas Zimmermann 		struct device_node *boot_display = NULL;
55752b1b46cSThomas Zimmermann 		struct platform_device *dev;
558241d2fb5SMichal Suchanek 		int display_number = 0;
55952b1b46cSThomas Zimmermann 		int ret;
56052b1b46cSThomas Zimmermann 
56152b1b46cSThomas Zimmermann 		/* Check if we have a MacOS display without a node spec */
5622f0cb475SRob Herring 		if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
56352b1b46cSThomas Zimmermann 			/*
56452b1b46cSThomas Zimmermann 			 * The old code tried to work out which node was the MacOS
56552b1b46cSThomas Zimmermann 			 * display based on the address. I'm dropping that since the
56652b1b46cSThomas Zimmermann 			 * lack of a node spec only happens with old BootX versions
56752b1b46cSThomas Zimmermann 			 * (users can update) and with this code, they'll still get
56852b1b46cSThomas Zimmermann 			 * a display (just not the palette hacks).
56952b1b46cSThomas Zimmermann 			 */
57052b1b46cSThomas Zimmermann 			dev = platform_device_alloc("bootx-noscreen", 0);
57152b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
57252b1b46cSThomas Zimmermann 				return -ENOMEM;
57352b1b46cSThomas Zimmermann 			ret = platform_device_add(dev);
57452b1b46cSThomas Zimmermann 			if (WARN_ON(ret)) {
57552b1b46cSThomas Zimmermann 				platform_device_put(dev);
57652b1b46cSThomas Zimmermann 				return ret;
57752b1b46cSThomas Zimmermann 			}
57852b1b46cSThomas Zimmermann 		}
57952b1b46cSThomas Zimmermann 
58052b1b46cSThomas Zimmermann 		/*
58152b1b46cSThomas Zimmermann 		 * For OF framebuffers, first create the device for the boot display,
58252b1b46cSThomas Zimmermann 		 * then for the other framebuffers. Only fail for the boot display;
58352b1b46cSThomas Zimmermann 		 * ignore errors for the rest.
58452b1b46cSThomas Zimmermann 		 */
58552b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
58652b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) ||
58752b1b46cSThomas Zimmermann 			    !of_get_property(node, "linux,boot-display", NULL))
58852b1b46cSThomas Zimmermann 				continue;
589241d2fb5SMichal Suchanek 			dev = of_platform_device_create(node, "of-display.0", NULL);
590241d2fb5SMichal Suchanek 			of_node_put(node);
59152b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
59252b1b46cSThomas Zimmermann 				return -ENOMEM;
59352b1b46cSThomas Zimmermann 			boot_display = node;
594241d2fb5SMichal Suchanek 			display_number++;
59552b1b46cSThomas Zimmermann 			break;
59652b1b46cSThomas Zimmermann 		}
59752b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
598241d2fb5SMichal Suchanek 			char buf[14];
599241d2fb5SMichal Suchanek 			const char *of_display_format = "of-display.%d";
600241d2fb5SMichal Suchanek 
60152b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
60252b1b46cSThomas Zimmermann 				continue;
603241d2fb5SMichal Suchanek 			ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
604241d2fb5SMichal Suchanek 			if (ret < sizeof(buf))
605241d2fb5SMichal Suchanek 				of_platform_device_create(node, buf, NULL);
60652b1b46cSThomas Zimmermann 		}
60752b1b46cSThomas Zimmermann 
60852b1b46cSThomas Zimmermann 	} else {
609529182e2SKees Cook 		/*
610a50ff19dSBjorn Andersson 		 * Handle certain compatibles explicitly, since we don't want to create
611a50ff19dSBjorn Andersson 		 * platform_devices for every node in /reserved-memory with a
612a50ff19dSBjorn Andersson 		 * "compatible",
613529182e2SKees Cook 		 */
614a50ff19dSBjorn Andersson 		for_each_matching_node(node, reserved_mem_matches)
615529182e2SKees Cook 			of_platform_device_create(node, NULL, NULL);
616529182e2SKees Cook 
6173aa0582fSSudeep Holla 		node = of_find_node_by_path("/firmware");
618e2105ca8SSudeep Holla 		if (node) {
6193aa0582fSSudeep Holla 			of_platform_populate(node, NULL, NULL, NULL);
620e2105ca8SSudeep Holla 			of_node_put(node);
621e2105ca8SSudeep Holla 		}
6223aa0582fSSudeep Holla 
6232f92ea21SHector Martin 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
6242f92ea21SHector Martin 		of_platform_device_create(node, NULL, NULL);
6252f92ea21SHector Martin 		of_node_put(node);
6262f92ea21SHector Martin 
627529182e2SKees Cook 		/* Populate everything else. */
62844a7185cSKefeng Wang 		of_platform_default_populate(NULL, NULL, NULL);
62952b1b46cSThomas Zimmermann 	}
63044a7185cSKefeng Wang 
63144a7185cSKefeng Wang 	return 0;
63244a7185cSKefeng Wang }
63344a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init);
6345e666938SSaravana Kannan 
6355e666938SSaravana Kannan static int __init of_platform_sync_state_init(void)
6365e666938SSaravana Kannan {
6375e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
6385e666938SSaravana Kannan 	return 0;
6395e666938SSaravana Kannan }
6405e666938SSaravana Kannan late_initcall_sync(of_platform_sync_state_init);
64144a7185cSKefeng Wang 
642c2372c20SJan Glauber int of_platform_device_destroy(struct device *dev, void *data)
643c6e126deSPawel Moll {
644c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
64575f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
646c6e126deSPawel Moll 		return 0;
647c6e126deSPawel Moll 
64875f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
64975f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
65075f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
651c6e126deSPawel Moll 
652522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED);
653522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
654522811e9SSrinivas Kandagatla 
655c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
656c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
657c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
658c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
659c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
660c6e126deSPawel Moll #endif
661c6e126deSPawel Moll 
662c6e126deSPawel Moll 	return 0;
663c6e126deSPawel Moll }
664c2372c20SJan Glauber EXPORT_SYMBOL_GPL(of_platform_device_destroy);
665c6e126deSPawel Moll 
666c6e126deSPawel Moll /**
667c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
66875f353b6SGrant Likely  * @parent: device which children will be removed
669c6e126deSPawel Moll  *
670c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
671c6e126deSPawel Moll  * of the given device (and, recurrently, their children) that have been
672c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
673c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
674c6e126deSPawel Moll  */
67575f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
676c6e126deSPawel Moll {
6772d0747c4SGrant Likely 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
67870a29209SThierry Reding 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
6792d0747c4SGrant Likely 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
6802d0747c4SGrant Likely 	}
681c6e126deSPawel Moll }
682c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
683c6e126deSPawel Moll 
68438b0b219SBenjamin Gaignard static void devm_of_platform_populate_release(struct device *dev, void *res)
68538b0b219SBenjamin Gaignard {
68638b0b219SBenjamin Gaignard 	of_platform_depopulate(*(struct device **)res);
68738b0b219SBenjamin Gaignard }
68838b0b219SBenjamin Gaignard 
68938b0b219SBenjamin Gaignard /**
69038b0b219SBenjamin Gaignard  * devm_of_platform_populate() - Populate platform_devices from device tree data
69138b0b219SBenjamin Gaignard  * @dev: device that requested to populate from device tree data
69238b0b219SBenjamin Gaignard  *
69338b0b219SBenjamin Gaignard  * Similar to of_platform_populate(), but will automatically call
69438b0b219SBenjamin Gaignard  * of_platform_depopulate() when the device is unbound from the bus.
69538b0b219SBenjamin Gaignard  *
6968c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
69738b0b219SBenjamin Gaignard  */
69838b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev)
69938b0b219SBenjamin Gaignard {
70038b0b219SBenjamin Gaignard 	struct device **ptr;
70138b0b219SBenjamin Gaignard 	int ret;
70238b0b219SBenjamin Gaignard 
70338b0b219SBenjamin Gaignard 	if (!dev)
70438b0b219SBenjamin Gaignard 		return -EINVAL;
70538b0b219SBenjamin Gaignard 
70638b0b219SBenjamin Gaignard 	ptr = devres_alloc(devm_of_platform_populate_release,
70738b0b219SBenjamin Gaignard 			   sizeof(*ptr), GFP_KERNEL);
70838b0b219SBenjamin Gaignard 	if (!ptr)
70938b0b219SBenjamin Gaignard 		return -ENOMEM;
71038b0b219SBenjamin Gaignard 
71138b0b219SBenjamin Gaignard 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
71238b0b219SBenjamin Gaignard 	if (ret) {
71338b0b219SBenjamin Gaignard 		devres_free(ptr);
71438b0b219SBenjamin Gaignard 	} else {
71538b0b219SBenjamin Gaignard 		*ptr = dev;
71638b0b219SBenjamin Gaignard 		devres_add(dev, ptr);
71738b0b219SBenjamin Gaignard 	}
71838b0b219SBenjamin Gaignard 
71938b0b219SBenjamin Gaignard 	return ret;
72038b0b219SBenjamin Gaignard }
72138b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate);
72238b0b219SBenjamin Gaignard 
72338b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data)
72438b0b219SBenjamin Gaignard {
72538b0b219SBenjamin Gaignard 	struct device **ptr = res;
72638b0b219SBenjamin Gaignard 
72738b0b219SBenjamin Gaignard 	if (!ptr) {
72838b0b219SBenjamin Gaignard 		WARN_ON(!ptr);
72938b0b219SBenjamin Gaignard 		return 0;
73038b0b219SBenjamin Gaignard 	}
73138b0b219SBenjamin Gaignard 
73238b0b219SBenjamin Gaignard 	return *ptr == data;
73338b0b219SBenjamin Gaignard }
73438b0b219SBenjamin Gaignard 
73538b0b219SBenjamin Gaignard /**
73638b0b219SBenjamin Gaignard  * devm_of_platform_depopulate() - Remove devices populated from device tree
73738b0b219SBenjamin Gaignard  * @dev: device that requested to depopulate from device tree data
73838b0b219SBenjamin Gaignard  *
73938b0b219SBenjamin Gaignard  * Complementary to devm_of_platform_populate(), this function removes children
74038b0b219SBenjamin Gaignard  * of the given device (and, recurrently, their children) that have been
74138b0b219SBenjamin Gaignard  * created from their respective device tree nodes (and only those,
74238b0b219SBenjamin Gaignard  * leaving others - eg. manually created - unharmed).
74338b0b219SBenjamin Gaignard  */
74438b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev)
74538b0b219SBenjamin Gaignard {
74638b0b219SBenjamin Gaignard 	int ret;
74738b0b219SBenjamin Gaignard 
74838b0b219SBenjamin Gaignard 	ret = devres_release(dev, devm_of_platform_populate_release,
74938b0b219SBenjamin Gaignard 			     devm_of_platform_match, dev);
75038b0b219SBenjamin Gaignard 
75138b0b219SBenjamin Gaignard 	WARN_ON(ret);
75238b0b219SBenjamin Gaignard }
75338b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
75438b0b219SBenjamin Gaignard 
755801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC
756801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb,
757801d728cSPantelis Antoniou 				unsigned long action, void *arg)
758801d728cSPantelis Antoniou {
759801d728cSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
760801d728cSPantelis Antoniou 	struct platform_device *pdev_parent, *pdev;
761801d728cSPantelis Antoniou 	bool children_left;
762801d728cSPantelis Antoniou 
763801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
764801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
765801d728cSPantelis Antoniou 		/* verify that the parent is a bus */
766801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
767801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
768801d728cSPantelis Antoniou 
76915204ab1SPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
77015204ab1SPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
77115204ab1SPantelis Antoniou 			return NOTIFY_OK;
77215204ab1SPantelis Antoniou 
7731a50d940SGeert Uytterhoeven 		/*
7741a50d940SGeert Uytterhoeven 		 * Clear the flag before adding the device so that fw_devlink
7751a50d940SGeert Uytterhoeven 		 * doesn't skip adding consumers to this device.
7761a50d940SGeert Uytterhoeven 		 */
7771a50d940SGeert Uytterhoeven 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
778801d728cSPantelis Antoniou 		/* pdev_parent may be NULL when no bus platform device */
779801d728cSPantelis Antoniou 		pdev_parent = of_find_device_by_node(rd->dn->parent);
780801d728cSPantelis Antoniou 		pdev = of_platform_device_create(rd->dn, NULL,
781801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
78283c4a4eeSRob Herring 		platform_device_put(pdev_parent);
783801d728cSPantelis Antoniou 
784801d728cSPantelis Antoniou 		if (pdev == NULL) {
7850d638a07SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
7860d638a07SRob Herring 					__func__, rd->dn);
787801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
788801d728cSPantelis Antoniou 			return notifier_from_errno(-EINVAL);
789801d728cSPantelis Antoniou 		}
790801d728cSPantelis Antoniou 		break;
791801d728cSPantelis Antoniou 
792801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
79315204ab1SPantelis Antoniou 
79415204ab1SPantelis Antoniou 		/* already depopulated? */
79515204ab1SPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
79615204ab1SPantelis Antoniou 			return NOTIFY_OK;
79715204ab1SPantelis Antoniou 
798801d728cSPantelis Antoniou 		/* find our device by node */
799801d728cSPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
800801d728cSPantelis Antoniou 		if (pdev == NULL)
801801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
802801d728cSPantelis Antoniou 
803801d728cSPantelis Antoniou 		/* unregister takes one ref away */
804801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
805801d728cSPantelis Antoniou 
806801d728cSPantelis Antoniou 		/* and put the reference of the find */
80783c4a4eeSRob Herring 		platform_device_put(pdev);
808801d728cSPantelis Antoniou 		break;
809801d728cSPantelis Antoniou 	}
810801d728cSPantelis Antoniou 
811801d728cSPantelis Antoniou 	return NOTIFY_OK;
812801d728cSPantelis Antoniou }
813801d728cSPantelis Antoniou 
814801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
815801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
816801d728cSPantelis Antoniou };
817801d728cSPantelis Antoniou 
818801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
819801d728cSPantelis Antoniou {
820801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
821801d728cSPantelis Antoniou }
822801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
823801d728cSPantelis Antoniou 
824964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
825