xref: /linux/drivers/of/platform.c (revision 0f8e5651095bcf6e60330c8d558fd1227e0d71b3)
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 
24cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
25cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
2622869a9eSLinus Walleij 	{ .compatible = "simple-mfd", },
27ecd76edeSPaul Burton 	{ .compatible = "isa", },
28cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
29cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
30cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
31cbb49c26SGrant Likely 	{} /* Empty terminated list */
32cbb49c26SGrant Likely };
33cbb49c26SGrant Likely 
344550fe63SViresh Kumar static const struct of_device_id of_skipped_node_table[] = {
354550fe63SViresh Kumar 	{ .compatible = "operating-points-v2", },
364550fe63SViresh Kumar 	{} /* Empty terminated list */
374550fe63SViresh Kumar };
384550fe63SViresh Kumar 
39c6085584SJonas Bonn /**
40c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
41c6085584SJonas Bonn  * @np: Pointer to device tree node
42c6085584SJonas Bonn  *
434fb373dfSJohan Hovold  * Takes a reference to the embedded struct device which needs to be dropped
444fb373dfSJohan Hovold  * after use.
454fb373dfSJohan Hovold  *
468c8239c2SRob Herring  * Return: platform_device pointer, or NULL if not found
47c6085584SJonas Bonn  */
48c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
49c6085584SJonas Bonn {
50c6085584SJonas Bonn 	struct device *dev;
51c6085584SJonas Bonn 
52cfba5de9SSuzuki K Poulose 	dev = bus_find_device_by_of_node(&platform_bus_type, np);
53c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
54c6085584SJonas Bonn }
55c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
56c6085584SJonas Bonn 
57964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS
585fd200f3SGrant Likely /*
595fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
605fd200f3SGrant Likely  * each applicable node.
615fd200f3SGrant Likely  *
625fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
635fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
645fd200f3SGrant Likely  */
655fd200f3SGrant Likely 
665fd200f3SGrant Likely /**
6794c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
6894c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
6994c09319SGrant Likely  *
7007e461cdSGrant Likely  * This routine will first try using the translated bus address to
7107e461cdSGrant Likely  * derive a unique name. If it cannot, then it will prepend names from
7207e461cdSGrant Likely  * parent nodes until a unique name can be derived.
7394c09319SGrant Likely  */
74e553f539SFrank Rowand static void of_device_make_bus_id(struct device *dev)
7594c09319SGrant Likely {
7694c09319SGrant Likely 	struct device_node *node = dev->of_node;
7724fb530fSKim Phillips 	const __be32 *reg;
7894c09319SGrant Likely 	u64 addr;
7968d16195SRob Herring 	u32 mask;
8094c09319SGrant Likely 
8107e461cdSGrant Likely 	/* Construct the name, using parent nodes if necessary to ensure uniqueness */
8207e461cdSGrant Likely 	while (node->parent) {
8394c09319SGrant Likely 		/*
8407e461cdSGrant Likely 		 * If the address can be translated, then that is as much
8507e461cdSGrant Likely 		 * uniqueness as we need. Make it the first component and return
8694c09319SGrant Likely 		 */
8794c09319SGrant Likely 		reg = of_get_property(node, "reg", NULL);
8807e461cdSGrant Likely 		if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
8968d16195SRob Herring 			if (!of_property_read_u32(node, "mask", &mask))
9068d16195SRob Herring 				dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
9168d16195SRob Herring 					     addr, ffs(mask) - 1, node, dev_name(dev));
9268d16195SRob Herring 
9368d16195SRob Herring 			else
94a613b26aSRob Herring 				dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
95d88590dcSGeert Uytterhoeven 					     addr, node, dev_name(dev));
9694c09319SGrant Likely 			return;
9794c09319SGrant Likely 		}
9894c09319SGrant Likely 
9907e461cdSGrant Likely 		/* format arguments only used if dev_name() resolves to NULL */
10007e461cdSGrant Likely 		dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
10195e6b1faSRob Herring 			     kbasename(node->full_name), dev_name(dev));
10207e461cdSGrant Likely 		node = node->parent;
10307e461cdSGrant Likely 	}
10494c09319SGrant Likely }
10594c09319SGrant Likely 
10694c09319SGrant Likely /**
10794c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
10894c09319SGrant Likely  * @np: device node to assign to device
10994c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
11094c09319SGrant Likely  * @parent: Parent device.
11194c09319SGrant Likely  */
11294a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
11394c09319SGrant Likely 				  const char *bus_id,
11494c09319SGrant Likely 				  struct device *parent)
11594c09319SGrant Likely {
11694a0cb1fSGrant Likely 	struct platform_device *dev;
117a1a2b712SLad Prabhakar 	int rc, i, num_reg = 0;
11832e8f9b3SYang Yingliang 	struct resource *res;
11994c09319SGrant Likely 
1206d7e3bf8SAndy Shevchenko 	dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
1217096d042SGrant Likely 	if (!dev)
1227096d042SGrant Likely 		return NULL;
1237096d042SGrant Likely 
124a1a2b712SLad Prabhakar 	/* count the io resources */
12532e8f9b3SYang Yingliang 	num_reg = of_address_count(np);
126ac80a51eSGrant Likely 
127ac80a51eSGrant Likely 	/* Populate the resource table */
128a1a2b712SLad Prabhakar 	if (num_reg) {
129a1a2b712SLad Prabhakar 		res = kcalloc(num_reg, sizeof(*res), GFP_KERNEL);
1307096d042SGrant Likely 		if (!res) {
1317096d042SGrant Likely 			platform_device_put(dev);
1327096d042SGrant Likely 			return NULL;
1337096d042SGrant Likely 		}
1347096d042SGrant Likely 
135a1a2b712SLad Prabhakar 		dev->num_resources = num_reg;
136ac80a51eSGrant Likely 		dev->resource = res;
137ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
138ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
139ac80a51eSGrant Likely 			WARN_ON(rc);
140ac80a51eSGrant Likely 		}
141ac80a51eSGrant Likely 	}
14294c09319SGrant Likely 
143*0f8e5651SAndy Shevchenko 	/* setup generic device info */
144*0f8e5651SAndy Shevchenko 	device_set_node(&dev->dev, of_fwnode_handle(np));
14543c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
14694c09319SGrant Likely 
14794c09319SGrant Likely 	if (bus_id)
14894c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
14994c09319SGrant Likely 	else
15094c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
15194c09319SGrant Likely 
15294c09319SGrant Likely 	return dev;
15394c09319SGrant Likely }
15494c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
15594c09319SGrant Likely 
156591c1ee4SSantosh Shilimkar /**
15715c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1585fd200f3SGrant Likely  * @np: pointer to node to create device for
1595fd200f3SGrant Likely  * @bus_id: name to assign device
16015c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1615fd200f3SGrant Likely  * @parent: Linux device model parent device.
162cd1e6504SGrant Likely  *
1638c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
164cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1655fd200f3SGrant Likely  */
166245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
16715c3597dSGrant Likely 					struct device_node *np,
1685fd200f3SGrant Likely 					const char *bus_id,
16915c3597dSGrant Likely 					void *platform_data,
1705fd200f3SGrant Likely 					struct device *parent)
1715fd200f3SGrant Likely {
17294a0cb1fSGrant Likely 	struct platform_device *dev;
1735fd200f3SGrant Likely 
174c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
175c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
176cd1e6504SGrant Likely 		return NULL;
177cd1e6504SGrant Likely 
1785fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
1795fd200f3SGrant Likely 	if (!dev)
180c6e126deSPawel Moll 		goto err_clear_flag;
1815fd200f3SGrant Likely 
182a5516219SRobin Murphy 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
183a5516219SRobin Murphy 	if (!dev->dev.dma_mask)
184a5516219SRobin Murphy 		dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
185eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
18615c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
187c706c239SMarc Zyngier 	of_msi_configure(&dev->dev, dev->dev.of_node);
1885fd200f3SGrant Likely 
1897096d042SGrant Likely 	if (of_device_add(dev) != 0) {
1907096d042SGrant Likely 		platform_device_put(dev);
191c6e126deSPawel Moll 		goto err_clear_flag;
1925fd200f3SGrant Likely 	}
1935fd200f3SGrant Likely 
1945fd200f3SGrant Likely 	return dev;
195c6e126deSPawel Moll 
196c6e126deSPawel Moll err_clear_flag:
197c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
198c6e126deSPawel Moll 	return NULL;
1995fd200f3SGrant Likely }
20015c3597dSGrant Likely 
20115c3597dSGrant Likely /**
20215c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
20315c3597dSGrant Likely  * @np: pointer to node to create device for
20415c3597dSGrant Likely  * @bus_id: name to assign device
20515c3597dSGrant Likely  * @parent: Linux device model parent device.
20615c3597dSGrant Likely  *
2078c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
20815c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
20915c3597dSGrant Likely  */
21015c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
21115c3597dSGrant Likely 					    const char *bus_id,
21215c3597dSGrant Likely 					    struct device *parent)
21315c3597dSGrant Likely {
21415c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
21515c3597dSGrant Likely }
2165fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2175fd200f3SGrant Likely 
2185de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2195de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2205de1540bSGrant Likely 						 const char *bus_id,
2215de1540bSGrant Likely 						 void *platform_data,
2225de1540bSGrant Likely 						 struct device *parent)
2235de1540bSGrant Likely {
2245de1540bSGrant Likely 	struct amba_device *dev;
225854f695cSWang Kefeng 	int ret;
2265de1540bSGrant Likely 
2270d638a07SRob Herring 	pr_debug("Creating amba device %pOF\n", node);
2285de1540bSGrant Likely 
229c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
230c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2315de1540bSGrant Likely 		return NULL;
2325de1540bSGrant Likely 
233c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
234606ad42aSRob Herring 	if (!dev)
235c6e126deSPawel Moll 		goto err_clear_flag;
2365de1540bSGrant Likely 
2378c89ef7bSLinus Walleij 	/* AMBA devices only support a single DMA mask */
2388c89ef7bSLinus Walleij 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2398c89ef7bSLinus Walleij 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
2408c89ef7bSLinus Walleij 
2415de1540bSGrant Likely 	/* setup generic device info */
242*0f8e5651SAndy Shevchenko 	device_set_node(&dev->dev, of_fwnode_handle(node));
24343c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
2445de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2455de1540bSGrant Likely 	if (bus_id)
2465de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
2475de1540bSGrant Likely 	else
2485de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
2495de1540bSGrant Likely 
2505de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
25116b0c7caSRob Herring 	of_property_read_u32(node, "arm,primecell-periphid", &dev->periphid);
2525de1540bSGrant Likely 
2535de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
2542bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2550d638a07SRob Herring 		pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
2560d638a07SRob Herring 		       ret, node);
2575de1540bSGrant Likely 		goto err_free;
2582bc552dfSBartlomiej Zolnierkiewicz 	}
2595de1540bSGrant Likely 
260c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
2612bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2620d638a07SRob Herring 		pr_err("amba_device_add() failed (%d) for %pOF\n",
2630d638a07SRob Herring 		       ret, node);
2645de1540bSGrant Likely 		goto err_free;
2652bc552dfSBartlomiej Zolnierkiewicz 	}
2665de1540bSGrant Likely 
2675de1540bSGrant Likely 	return dev;
2685de1540bSGrant Likely 
2695de1540bSGrant Likely err_free:
270c0f72f8aSRussell King 	amba_device_put(dev);
271c6e126deSPawel Moll err_clear_flag:
272c6e126deSPawel Moll 	of_node_clear_flag(node, OF_POPULATED);
2735de1540bSGrant Likely 	return NULL;
2745de1540bSGrant Likely }
2755de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
2765de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2775de1540bSGrant Likely 						 const char *bus_id,
2785de1540bSGrant Likely 						 void *platform_data,
2795de1540bSGrant Likely 						 struct device *parent)
2805de1540bSGrant Likely {
2815de1540bSGrant Likely 	return NULL;
2825de1540bSGrant Likely }
2835de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
2845de1540bSGrant Likely 
285f3896a7eSLee Jones /*
286f9a529b9SQi Zheng  * of_dev_lookup() - Given a device node, lookup the preferred Linux name
28715c3597dSGrant Likely  */
28815c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
28915c3597dSGrant Likely 				 struct device_node *np)
29015c3597dSGrant Likely {
291fc5cf80aSTony Lindgren 	const struct of_dev_auxdata *auxdata;
29215c3597dSGrant Likely 	struct resource res;
293fc5cf80aSTony Lindgren 	int compatible = 0;
294303f59d1SOlof Johansson 
295303f59d1SOlof Johansson 	if (!lookup)
296303f59d1SOlof Johansson 		return NULL;
297303f59d1SOlof Johansson 
298fc5cf80aSTony Lindgren 	auxdata = lookup;
299fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
300fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
30115c3597dSGrant Likely 			continue;
302fc5cf80aSTony Lindgren 		compatible++;
30384774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
304fc5cf80aSTony Lindgren 			if (res.start != auxdata->phys_addr)
30515c3597dSGrant Likely 				continue;
3060d638a07SRob Herring 		pr_debug("%pOF: devname=%s\n", np, auxdata->name);
307fc5cf80aSTony Lindgren 		return auxdata;
308fc5cf80aSTony Lindgren 	}
309fc5cf80aSTony Lindgren 
310fc5cf80aSTony Lindgren 	if (!compatible)
311fc5cf80aSTony Lindgren 		return NULL;
312fc5cf80aSTony Lindgren 
313fc5cf80aSTony Lindgren 	/* Try compatible match if no phys_addr and name are specified */
314fc5cf80aSTony Lindgren 	auxdata = lookup;
315fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
316fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
317fc5cf80aSTony Lindgren 			continue;
318fc5cf80aSTony Lindgren 		if (!auxdata->phys_addr && !auxdata->name) {
3190d638a07SRob Herring 			pr_debug("%pOF: compatible match\n", np);
320fc5cf80aSTony Lindgren 			return auxdata;
321fc5cf80aSTony Lindgren 		}
32215c3597dSGrant Likely 	}
323303f59d1SOlof Johansson 
32415c3597dSGrant Likely 	return NULL;
32515c3597dSGrant Likely }
32615c3597dSGrant Likely 
32715c3597dSGrant Likely /**
32838e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3295fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3301eed4c07SGrant Likely  * @matches: match table for bus nodes
331303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
33238e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
333303f59d1SOlof Johansson  * @strict: require compatible property
33438e9e21dSGrant Likely  *
33538e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
33638e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3375fd200f3SGrant Likely  */
33838e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3395fd200f3SGrant Likely 				  const struct of_device_id *matches,
34015c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
34129d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3425fd200f3SGrant Likely {
34315c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3445fd200f3SGrant Likely 	struct device_node *child;
34594a0cb1fSGrant Likely 	struct platform_device *dev;
34615c3597dSGrant Likely 	const char *bus_id = NULL;
34715c3597dSGrant Likely 	void *platform_data = NULL;
3485fd200f3SGrant Likely 	int rc = 0;
3495fd200f3SGrant Likely 
35029d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
35129d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
3520d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, no compatible prop\n",
3530d638a07SRob Herring 			 __func__, bus);
35429d4f8a4SGrant Likely 		return 0;
35529d4f8a4SGrant Likely 	}
35629d4f8a4SGrant Likely 
3574550fe63SViresh Kumar 	/* Skip nodes for which we don't want to create devices */
3584550fe63SViresh Kumar 	if (unlikely(of_match_node(of_skipped_node_table, bus))) {
3594550fe63SViresh Kumar 		pr_debug("%s() - skipping %pOF node\n", __func__, bus);
3604550fe63SViresh Kumar 		return 0;
3614550fe63SViresh Kumar 	}
3624550fe63SViresh Kumar 
36344a7185cSKefeng Wang 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
3640d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, already populated\n",
3650d638a07SRob Herring 			__func__, bus);
36644a7185cSKefeng Wang 		return 0;
36744a7185cSKefeng Wang 	}
36844a7185cSKefeng Wang 
36915c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
37015c3597dSGrant Likely 	if (auxdata) {
37115c3597dSGrant Likely 		bus_id = auxdata->name;
37215c3597dSGrant Likely 		platform_data = auxdata->platform_data;
37315c3597dSGrant Likely 	}
37415c3597dSGrant Likely 
3755de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
3762bc552dfSBartlomiej Zolnierkiewicz 		/*
3772bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
3782bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
3792bc552dfSBartlomiej Zolnierkiewicz 		 */
38015c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
3815de1540bSGrant Likely 		return 0;
3825de1540bSGrant Likely 	}
3835de1540bSGrant Likely 
38415c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
38538e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
38638e9e21dSGrant Likely 		return 0;
38738e9e21dSGrant Likely 
3885fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
3890d638a07SRob Herring 		pr_debug("   create child: %pOF\n", child);
39015c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
3915fd200f3SGrant Likely 		if (rc) {
3925fd200f3SGrant Likely 			of_node_put(child);
3935fd200f3SGrant Likely 			break;
3945fd200f3SGrant Likely 		}
3955fd200f3SGrant Likely 	}
39675f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
3975fd200f3SGrant Likely 	return rc;
3985fd200f3SGrant Likely }
3995fd200f3SGrant Likely 
4005fd200f3SGrant Likely /**
40138e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
4025fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
4031eed4c07SGrant Likely  * @matches: match table for bus nodes
4045fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
4055fd200f3SGrant Likely  *
4065fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
4075fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4085fd200f3SGrant Likely  */
4095fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4105fd200f3SGrant Likely 			  const struct of_device_id *matches,
4115fd200f3SGrant Likely 			  struct device *parent)
4125fd200f3SGrant Likely {
4135fd200f3SGrant Likely 	struct device_node *child;
4145fd200f3SGrant Likely 	int rc = 0;
4155fd200f3SGrant Likely 
4161eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4171eed4c07SGrant Likely 	if (!root)
41860d59913SGrant Likely 		return -EINVAL;
4195fd200f3SGrant Likely 
42044a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4210d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
4225fd200f3SGrant Likely 
4231eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4245fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
42515c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
42638e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4275fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4285fd200f3SGrant Likely 			continue;
42915c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
4307fad948aSJulia Lawall 		if (rc) {
4317fad948aSJulia Lawall 			of_node_put(child);
4325fd200f3SGrant Likely 			break;
4335fd200f3SGrant Likely 		}
4347fad948aSJulia Lawall 	}
43538e9e21dSGrant Likely 
4365fd200f3SGrant Likely 	of_node_put(root);
4375fd200f3SGrant Likely 	return rc;
4385fd200f3SGrant Likely }
4395fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
44029d4f8a4SGrant Likely 
44129d4f8a4SGrant Likely /**
44229d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
44329d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
44429d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
44592039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
44629d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
44729d4f8a4SGrant Likely  *
44829d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
44929d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
45029d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
45129d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
45229d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
45329d4f8a4SGrant Likely  * are selected by the @matches argument).
45429d4f8a4SGrant Likely  *
45529d4f8a4SGrant Likely  * New board support should be using this function instead of
45629d4f8a4SGrant Likely  * of_platform_bus_probe().
45729d4f8a4SGrant Likely  *
4588c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
45929d4f8a4SGrant Likely  */
46029d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
46129d4f8a4SGrant Likely 			const struct of_device_id *matches,
46215c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
46329d4f8a4SGrant Likely 			struct device *parent)
46429d4f8a4SGrant Likely {
46529d4f8a4SGrant Likely 	struct device_node *child;
46629d4f8a4SGrant Likely 	int rc = 0;
46729d4f8a4SGrant Likely 
46829d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
46929d4f8a4SGrant Likely 	if (!root)
47029d4f8a4SGrant Likely 		return -EINVAL;
47129d4f8a4SGrant Likely 
47244a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4730d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
47444a7185cSKefeng Wang 
4755e666938SSaravana Kannan 	device_links_supplier_sync_state_pause();
47629d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
47715c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
4787fad948aSJulia Lawall 		if (rc) {
4797fad948aSJulia Lawall 			of_node_put(child);
48029d4f8a4SGrant Likely 			break;
48129d4f8a4SGrant Likely 		}
4827fad948aSJulia Lawall 	}
4835e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
4845e666938SSaravana Kannan 
4852d0747c4SGrant Likely 	of_node_set_flag(root, OF_POPULATED_BUS);
48629d4f8a4SGrant Likely 
48729d4f8a4SGrant Likely 	of_node_put(root);
48829d4f8a4SGrant Likely 	return rc;
48929d4f8a4SGrant Likely }
490e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
491c6e126deSPawel Moll 
49243443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root,
49343443ad6SHauke Mehrtens 				 const struct of_dev_auxdata *lookup,
49443443ad6SHauke Mehrtens 				 struct device *parent)
49543443ad6SHauke Mehrtens {
49643443ad6SHauke Mehrtens 	return of_platform_populate(root, of_default_bus_match_table, lookup,
49743443ad6SHauke Mehrtens 				    parent);
49843443ad6SHauke Mehrtens }
49943443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate);
50043443ad6SHauke Mehrtens 
501a50ff19dSBjorn Andersson static const struct of_device_id reserved_mem_matches[] = {
5027090d2f1SVincent Whitchurch 	{ .compatible = "phram" },
503d1de6d6cSBjorn Andersson 	{ .compatible = "qcom,rmtfs-mem" },
504312416d9SMahesh Sivasubramanian 	{ .compatible = "qcom,cmd-db" },
505b5af64fcSBjorn Andersson 	{ .compatible = "qcom,smem" },
506a50ff19dSBjorn Andersson 	{ .compatible = "ramoops" },
5075a3fa75aSNicolas Saenz Julienne 	{ .compatible = "nvmem-rmem" },
508f396ededSDavid Brazdil 	{ .compatible = "google,open-dice" },
509a50ff19dSBjorn Andersson 	{}
510a50ff19dSBjorn Andersson };
511a50ff19dSBjorn Andersson 
51244a7185cSKefeng Wang static int __init of_platform_default_populate_init(void)
51344a7185cSKefeng Wang {
514529182e2SKees Cook 	struct device_node *node;
515529182e2SKees Cook 
516ee9b280eSSaravana Kannan 	device_links_supplier_sync_state_pause();
517ee9b280eSSaravana Kannan 
518529182e2SKees Cook 	if (!of_have_populated_dt())
519529182e2SKees Cook 		return -ENODEV;
520529182e2SKees Cook 
52152b1b46cSThomas Zimmermann 	if (IS_ENABLED(CONFIG_PPC)) {
52252b1b46cSThomas Zimmermann 		struct device_node *boot_display = NULL;
52352b1b46cSThomas Zimmermann 		struct platform_device *dev;
524241d2fb5SMichal Suchanek 		int display_number = 0;
52552b1b46cSThomas Zimmermann 		int ret;
52652b1b46cSThomas Zimmermann 
52752b1b46cSThomas Zimmermann 		/* Check if we have a MacOS display without a node spec */
5282f0cb475SRob Herring 		if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
52952b1b46cSThomas Zimmermann 			/*
53052b1b46cSThomas Zimmermann 			 * The old code tried to work out which node was the MacOS
53152b1b46cSThomas Zimmermann 			 * display based on the address. I'm dropping that since the
53252b1b46cSThomas Zimmermann 			 * lack of a node spec only happens with old BootX versions
53352b1b46cSThomas Zimmermann 			 * (users can update) and with this code, they'll still get
53452b1b46cSThomas Zimmermann 			 * a display (just not the palette hacks).
53552b1b46cSThomas Zimmermann 			 */
53652b1b46cSThomas Zimmermann 			dev = platform_device_alloc("bootx-noscreen", 0);
53752b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
53852b1b46cSThomas Zimmermann 				return -ENOMEM;
53952b1b46cSThomas Zimmermann 			ret = platform_device_add(dev);
54052b1b46cSThomas Zimmermann 			if (WARN_ON(ret)) {
54152b1b46cSThomas Zimmermann 				platform_device_put(dev);
54252b1b46cSThomas Zimmermann 				return ret;
54352b1b46cSThomas Zimmermann 			}
54452b1b46cSThomas Zimmermann 		}
54552b1b46cSThomas Zimmermann 
54652b1b46cSThomas Zimmermann 		/*
54752b1b46cSThomas Zimmermann 		 * For OF framebuffers, first create the device for the boot display,
54852b1b46cSThomas Zimmermann 		 * then for the other framebuffers. Only fail for the boot display;
54952b1b46cSThomas Zimmermann 		 * ignore errors for the rest.
55052b1b46cSThomas Zimmermann 		 */
55152b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
55252b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) ||
55352b1b46cSThomas Zimmermann 			    !of_get_property(node, "linux,boot-display", NULL))
55452b1b46cSThomas Zimmermann 				continue;
555241d2fb5SMichal Suchanek 			dev = of_platform_device_create(node, "of-display.0", NULL);
556241d2fb5SMichal Suchanek 			of_node_put(node);
55752b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
55852b1b46cSThomas Zimmermann 				return -ENOMEM;
55952b1b46cSThomas Zimmermann 			boot_display = node;
560241d2fb5SMichal Suchanek 			display_number++;
56152b1b46cSThomas Zimmermann 			break;
56252b1b46cSThomas Zimmermann 		}
56352b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
564241d2fb5SMichal Suchanek 			char buf[14];
565241d2fb5SMichal Suchanek 			const char *of_display_format = "of-display.%d";
566241d2fb5SMichal Suchanek 
56752b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
56852b1b46cSThomas Zimmermann 				continue;
569241d2fb5SMichal Suchanek 			ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
570241d2fb5SMichal Suchanek 			if (ret < sizeof(buf))
571241d2fb5SMichal Suchanek 				of_platform_device_create(node, buf, NULL);
57252b1b46cSThomas Zimmermann 		}
57352b1b46cSThomas Zimmermann 
57452b1b46cSThomas Zimmermann 	} else {
575529182e2SKees Cook 		/*
576a50ff19dSBjorn Andersson 		 * Handle certain compatibles explicitly, since we don't want to create
577a50ff19dSBjorn Andersson 		 * platform_devices for every node in /reserved-memory with a
578a50ff19dSBjorn Andersson 		 * "compatible",
579529182e2SKees Cook 		 */
580a50ff19dSBjorn Andersson 		for_each_matching_node(node, reserved_mem_matches)
581529182e2SKees Cook 			of_platform_device_create(node, NULL, NULL);
582529182e2SKees Cook 
5833aa0582fSSudeep Holla 		node = of_find_node_by_path("/firmware");
584e2105ca8SSudeep Holla 		if (node) {
5853aa0582fSSudeep Holla 			of_platform_populate(node, NULL, NULL, NULL);
586e2105ca8SSudeep Holla 			of_node_put(node);
587e2105ca8SSudeep Holla 		}
5883aa0582fSSudeep Holla 
5892f92ea21SHector Martin 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
5902f92ea21SHector Martin 		of_platform_device_create(node, NULL, NULL);
5912f92ea21SHector Martin 		of_node_put(node);
5922f92ea21SHector Martin 
593529182e2SKees Cook 		/* Populate everything else. */
59444a7185cSKefeng Wang 		of_platform_default_populate(NULL, NULL, NULL);
59552b1b46cSThomas Zimmermann 	}
59644a7185cSKefeng Wang 
59744a7185cSKefeng Wang 	return 0;
59844a7185cSKefeng Wang }
59944a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init);
6005e666938SSaravana Kannan 
6015e666938SSaravana Kannan static int __init of_platform_sync_state_init(void)
6025e666938SSaravana Kannan {
6035e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
6045e666938SSaravana Kannan 	return 0;
6055e666938SSaravana Kannan }
6065e666938SSaravana Kannan late_initcall_sync(of_platform_sync_state_init);
60744a7185cSKefeng Wang 
608c2372c20SJan Glauber int of_platform_device_destroy(struct device *dev, void *data)
609c6e126deSPawel Moll {
610c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
61175f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
612c6e126deSPawel Moll 		return 0;
613c6e126deSPawel Moll 
61475f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
61575f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
61675f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
617c6e126deSPawel Moll 
618522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED);
619522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
620522811e9SSrinivas Kandagatla 
621c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
622c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
623c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
624c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
625c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
626c6e126deSPawel Moll #endif
627c6e126deSPawel Moll 
628c6e126deSPawel Moll 	return 0;
629c6e126deSPawel Moll }
630c2372c20SJan Glauber EXPORT_SYMBOL_GPL(of_platform_device_destroy);
631c6e126deSPawel Moll 
632c6e126deSPawel Moll /**
633c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
63475f353b6SGrant Likely  * @parent: device which children will be removed
635c6e126deSPawel Moll  *
636c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
637c6e126deSPawel Moll  * of the given device (and, recurrently, their children) that have been
638c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
639c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
640c6e126deSPawel Moll  */
64175f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
642c6e126deSPawel Moll {
6432d0747c4SGrant Likely 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
64470a29209SThierry Reding 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
6452d0747c4SGrant Likely 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
6462d0747c4SGrant Likely 	}
647c6e126deSPawel Moll }
648c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
649c6e126deSPawel Moll 
65038b0b219SBenjamin Gaignard static void devm_of_platform_populate_release(struct device *dev, void *res)
65138b0b219SBenjamin Gaignard {
65238b0b219SBenjamin Gaignard 	of_platform_depopulate(*(struct device **)res);
65338b0b219SBenjamin Gaignard }
65438b0b219SBenjamin Gaignard 
65538b0b219SBenjamin Gaignard /**
65638b0b219SBenjamin Gaignard  * devm_of_platform_populate() - Populate platform_devices from device tree data
65738b0b219SBenjamin Gaignard  * @dev: device that requested to populate from device tree data
65838b0b219SBenjamin Gaignard  *
65938b0b219SBenjamin Gaignard  * Similar to of_platform_populate(), but will automatically call
66038b0b219SBenjamin Gaignard  * of_platform_depopulate() when the device is unbound from the bus.
66138b0b219SBenjamin Gaignard  *
6628c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
66338b0b219SBenjamin Gaignard  */
66438b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev)
66538b0b219SBenjamin Gaignard {
66638b0b219SBenjamin Gaignard 	struct device **ptr;
66738b0b219SBenjamin Gaignard 	int ret;
66838b0b219SBenjamin Gaignard 
66938b0b219SBenjamin Gaignard 	if (!dev)
67038b0b219SBenjamin Gaignard 		return -EINVAL;
67138b0b219SBenjamin Gaignard 
67238b0b219SBenjamin Gaignard 	ptr = devres_alloc(devm_of_platform_populate_release,
67338b0b219SBenjamin Gaignard 			   sizeof(*ptr), GFP_KERNEL);
67438b0b219SBenjamin Gaignard 	if (!ptr)
67538b0b219SBenjamin Gaignard 		return -ENOMEM;
67638b0b219SBenjamin Gaignard 
67738b0b219SBenjamin Gaignard 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
67838b0b219SBenjamin Gaignard 	if (ret) {
67938b0b219SBenjamin Gaignard 		devres_free(ptr);
68038b0b219SBenjamin Gaignard 	} else {
68138b0b219SBenjamin Gaignard 		*ptr = dev;
68238b0b219SBenjamin Gaignard 		devres_add(dev, ptr);
68338b0b219SBenjamin Gaignard 	}
68438b0b219SBenjamin Gaignard 
68538b0b219SBenjamin Gaignard 	return ret;
68638b0b219SBenjamin Gaignard }
68738b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate);
68838b0b219SBenjamin Gaignard 
68938b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data)
69038b0b219SBenjamin Gaignard {
69138b0b219SBenjamin Gaignard 	struct device **ptr = res;
69238b0b219SBenjamin Gaignard 
69338b0b219SBenjamin Gaignard 	if (!ptr) {
69438b0b219SBenjamin Gaignard 		WARN_ON(!ptr);
69538b0b219SBenjamin Gaignard 		return 0;
69638b0b219SBenjamin Gaignard 	}
69738b0b219SBenjamin Gaignard 
69838b0b219SBenjamin Gaignard 	return *ptr == data;
69938b0b219SBenjamin Gaignard }
70038b0b219SBenjamin Gaignard 
70138b0b219SBenjamin Gaignard /**
70238b0b219SBenjamin Gaignard  * devm_of_platform_depopulate() - Remove devices populated from device tree
70338b0b219SBenjamin Gaignard  * @dev: device that requested to depopulate from device tree data
70438b0b219SBenjamin Gaignard  *
70538b0b219SBenjamin Gaignard  * Complementary to devm_of_platform_populate(), this function removes children
70638b0b219SBenjamin Gaignard  * of the given device (and, recurrently, their children) that have been
70738b0b219SBenjamin Gaignard  * created from their respective device tree nodes (and only those,
70838b0b219SBenjamin Gaignard  * leaving others - eg. manually created - unharmed).
70938b0b219SBenjamin Gaignard  */
71038b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev)
71138b0b219SBenjamin Gaignard {
71238b0b219SBenjamin Gaignard 	int ret;
71338b0b219SBenjamin Gaignard 
71438b0b219SBenjamin Gaignard 	ret = devres_release(dev, devm_of_platform_populate_release,
71538b0b219SBenjamin Gaignard 			     devm_of_platform_match, dev);
71638b0b219SBenjamin Gaignard 
71738b0b219SBenjamin Gaignard 	WARN_ON(ret);
71838b0b219SBenjamin Gaignard }
71938b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
72038b0b219SBenjamin Gaignard 
721801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC
722801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb,
723801d728cSPantelis Antoniou 				unsigned long action, void *arg)
724801d728cSPantelis Antoniou {
725801d728cSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
726801d728cSPantelis Antoniou 	struct platform_device *pdev_parent, *pdev;
727801d728cSPantelis Antoniou 	bool children_left;
728801d728cSPantelis Antoniou 
729801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
730801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
731801d728cSPantelis Antoniou 		/* verify that the parent is a bus */
732801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
733801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
734801d728cSPantelis Antoniou 
73515204ab1SPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
73615204ab1SPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
73715204ab1SPantelis Antoniou 			return NOTIFY_OK;
73815204ab1SPantelis Antoniou 
7391a50d940SGeert Uytterhoeven 		/*
7401a50d940SGeert Uytterhoeven 		 * Clear the flag before adding the device so that fw_devlink
7411a50d940SGeert Uytterhoeven 		 * doesn't skip adding consumers to this device.
7421a50d940SGeert Uytterhoeven 		 */
7431a50d940SGeert Uytterhoeven 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
744801d728cSPantelis Antoniou 		/* pdev_parent may be NULL when no bus platform device */
745801d728cSPantelis Antoniou 		pdev_parent = of_find_device_by_node(rd->dn->parent);
746801d728cSPantelis Antoniou 		pdev = of_platform_device_create(rd->dn, NULL,
747801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
74883c4a4eeSRob Herring 		platform_device_put(pdev_parent);
749801d728cSPantelis Antoniou 
750801d728cSPantelis Antoniou 		if (pdev == NULL) {
7510d638a07SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
7520d638a07SRob Herring 					__func__, rd->dn);
753801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
754801d728cSPantelis Antoniou 			return notifier_from_errno(-EINVAL);
755801d728cSPantelis Antoniou 		}
756801d728cSPantelis Antoniou 		break;
757801d728cSPantelis Antoniou 
758801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
75915204ab1SPantelis Antoniou 
76015204ab1SPantelis Antoniou 		/* already depopulated? */
76115204ab1SPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
76215204ab1SPantelis Antoniou 			return NOTIFY_OK;
76315204ab1SPantelis Antoniou 
764801d728cSPantelis Antoniou 		/* find our device by node */
765801d728cSPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
766801d728cSPantelis Antoniou 		if (pdev == NULL)
767801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
768801d728cSPantelis Antoniou 
769801d728cSPantelis Antoniou 		/* unregister takes one ref away */
770801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
771801d728cSPantelis Antoniou 
772801d728cSPantelis Antoniou 		/* and put the reference of the find */
77383c4a4eeSRob Herring 		platform_device_put(pdev);
774801d728cSPantelis Antoniou 		break;
775801d728cSPantelis Antoniou 	}
776801d728cSPantelis Antoniou 
777801d728cSPantelis Antoniou 	return NOTIFY_OK;
778801d728cSPantelis Antoniou }
779801d728cSPantelis Antoniou 
780801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
781801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
782801d728cSPantelis Antoniou };
783801d728cSPantelis Antoniou 
784801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
785801d728cSPantelis Antoniou {
786801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
787801d728cSPantelis Antoniou }
788801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
789801d728cSPantelis Antoniou 
790964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
791