xref: /linux/drivers/of/platform.c (revision 3310288f61357b9b54139c93fc7665d60c0288bf)
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>
23*3310288fSJavier Martinez Canillas #include <linux/sysfb.h>
24eca39301SGrant Likely 
2573aca58bSRob Herring #include "of_private.h"
2673aca58bSRob Herring 
27cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
28cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
2922869a9eSLinus Walleij 	{ .compatible = "simple-mfd", },
30ecd76edeSPaul Burton 	{ .compatible = "isa", },
31cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
32cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
33cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
34cbb49c26SGrant Likely 	{} /* Empty terminated list */
35cbb49c26SGrant Likely };
36cbb49c26SGrant Likely 
37c6085584SJonas Bonn /**
38c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
39c6085584SJonas Bonn  * @np: Pointer to device tree node
40c6085584SJonas Bonn  *
414fb373dfSJohan Hovold  * Takes a reference to the embedded struct device which needs to be dropped
424fb373dfSJohan Hovold  * after use.
434fb373dfSJohan Hovold  *
448c8239c2SRob Herring  * Return: platform_device pointer, or NULL if not found
45c6085584SJonas Bonn  */
46c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
47c6085584SJonas Bonn {
48c6085584SJonas Bonn 	struct device *dev;
49c6085584SJonas Bonn 
50cfba5de9SSuzuki K Poulose 	dev = bus_find_device_by_of_node(&platform_bus_type, np);
51c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
52c6085584SJonas Bonn }
53c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
54c6085584SJonas Bonn 
5566a4210bSRob Herring int of_device_add(struct platform_device *ofdev)
5666a4210bSRob Herring {
5766a4210bSRob Herring 	BUG_ON(ofdev->dev.of_node == NULL);
5866a4210bSRob Herring 
5966a4210bSRob Herring 	/* name and id have to be set so that the platform bus doesn't get
6066a4210bSRob Herring 	 * confused on matching */
6166a4210bSRob Herring 	ofdev->name = dev_name(&ofdev->dev);
6266a4210bSRob Herring 	ofdev->id = PLATFORM_DEVID_NONE;
6366a4210bSRob Herring 
6466a4210bSRob Herring 	/*
6566a4210bSRob Herring 	 * If this device has not binding numa node in devicetree, that is
6666a4210bSRob Herring 	 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
6766a4210bSRob Herring 	 * device is on the same node as the parent.
6866a4210bSRob Herring 	 */
6966a4210bSRob Herring 	set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
7066a4210bSRob Herring 
7166a4210bSRob Herring 	return device_add(&ofdev->dev);
7266a4210bSRob Herring }
7366a4210bSRob Herring 
7466a4210bSRob Herring int of_device_register(struct platform_device *pdev)
7566a4210bSRob Herring {
7666a4210bSRob Herring 	device_initialize(&pdev->dev);
7766a4210bSRob Herring 	return of_device_add(pdev);
7866a4210bSRob Herring }
7966a4210bSRob Herring EXPORT_SYMBOL(of_device_register);
8066a4210bSRob Herring 
8166a4210bSRob Herring void of_device_unregister(struct platform_device *ofdev)
8266a4210bSRob Herring {
8366a4210bSRob Herring 	device_unregister(&ofdev->dev);
8466a4210bSRob Herring }
8566a4210bSRob Herring EXPORT_SYMBOL(of_device_unregister);
8666a4210bSRob Herring 
87964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS
88ef04d280SViresh Kumar static const struct of_device_id of_skipped_node_table[] = {
89ef04d280SViresh Kumar 	{ .compatible = "operating-points-v2", },
90ef04d280SViresh Kumar 	{} /* Empty terminated list */
91ef04d280SViresh Kumar };
92ef04d280SViresh Kumar 
935fd200f3SGrant Likely /*
945fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
955fd200f3SGrant Likely  * each applicable node.
965fd200f3SGrant Likely  *
975fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
985fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
995fd200f3SGrant Likely  */
1005fd200f3SGrant Likely 
1015fd200f3SGrant Likely /**
10294c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
10394c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
10494c09319SGrant Likely  *
10507e461cdSGrant Likely  * This routine will first try using the translated bus address to
10607e461cdSGrant Likely  * derive a unique name. If it cannot, then it will prepend names from
10707e461cdSGrant Likely  * parent nodes until a unique name can be derived.
10894c09319SGrant Likely  */
109e553f539SFrank Rowand static void of_device_make_bus_id(struct device *dev)
11094c09319SGrant Likely {
11194c09319SGrant Likely 	struct device_node *node = dev->of_node;
11224fb530fSKim Phillips 	const __be32 *reg;
11394c09319SGrant Likely 	u64 addr;
11468d16195SRob Herring 	u32 mask;
11594c09319SGrant Likely 
11607e461cdSGrant Likely 	/* Construct the name, using parent nodes if necessary to ensure uniqueness */
11707e461cdSGrant Likely 	while (node->parent) {
11894c09319SGrant Likely 		/*
11907e461cdSGrant Likely 		 * If the address can be translated, then that is as much
12007e461cdSGrant Likely 		 * uniqueness as we need. Make it the first component and return
12194c09319SGrant Likely 		 */
12294c09319SGrant Likely 		reg = of_get_property(node, "reg", NULL);
12307e461cdSGrant Likely 		if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
12468d16195SRob Herring 			if (!of_property_read_u32(node, "mask", &mask))
12568d16195SRob Herring 				dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
12668d16195SRob Herring 					     addr, ffs(mask) - 1, node, dev_name(dev));
12768d16195SRob Herring 
12868d16195SRob Herring 			else
129a613b26aSRob Herring 				dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
130d88590dcSGeert Uytterhoeven 					     addr, node, dev_name(dev));
13194c09319SGrant Likely 			return;
13294c09319SGrant Likely 		}
13394c09319SGrant Likely 
13407e461cdSGrant Likely 		/* format arguments only used if dev_name() resolves to NULL */
13507e461cdSGrant Likely 		dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
13695e6b1faSRob Herring 			     kbasename(node->full_name), dev_name(dev));
13707e461cdSGrant Likely 		node = node->parent;
13807e461cdSGrant Likely 	}
13994c09319SGrant Likely }
14094c09319SGrant Likely 
14194c09319SGrant Likely /**
14294c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
14394c09319SGrant Likely  * @np: device node to assign to device
14494c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
14594c09319SGrant Likely  * @parent: Parent device.
14694c09319SGrant Likely  */
14794a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
14894c09319SGrant Likely 				  const char *bus_id,
14994c09319SGrant Likely 				  struct device *parent)
15094c09319SGrant Likely {
15194a0cb1fSGrant Likely 	struct platform_device *dev;
152a1a2b712SLad Prabhakar 	int rc, i, num_reg = 0;
15332e8f9b3SYang Yingliang 	struct resource *res;
15494c09319SGrant Likely 
1556d7e3bf8SAndy Shevchenko 	dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
1567096d042SGrant Likely 	if (!dev)
1577096d042SGrant Likely 		return NULL;
1587096d042SGrant Likely 
159a1a2b712SLad Prabhakar 	/* count the io resources */
16032e8f9b3SYang Yingliang 	num_reg = of_address_count(np);
161ac80a51eSGrant Likely 
162ac80a51eSGrant Likely 	/* Populate the resource table */
163a1a2b712SLad Prabhakar 	if (num_reg) {
164a1a2b712SLad Prabhakar 		res = kcalloc(num_reg, sizeof(*res), GFP_KERNEL);
1657096d042SGrant Likely 		if (!res) {
1667096d042SGrant Likely 			platform_device_put(dev);
1677096d042SGrant Likely 			return NULL;
1687096d042SGrant Likely 		}
1697096d042SGrant Likely 
170a1a2b712SLad Prabhakar 		dev->num_resources = num_reg;
171ac80a51eSGrant Likely 		dev->resource = res;
172ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
173ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
174ac80a51eSGrant Likely 			WARN_ON(rc);
175ac80a51eSGrant Likely 		}
176ac80a51eSGrant Likely 	}
17794c09319SGrant Likely 
1780f8e5651SAndy Shevchenko 	/* setup generic device info */
1797882541cSPeng Fan 	device_set_node(&dev->dev, of_fwnode_handle(of_node_get(np)));
18043c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
18194c09319SGrant Likely 
18294c09319SGrant Likely 	if (bus_id)
18394c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
18494c09319SGrant Likely 	else
18594c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
18694c09319SGrant Likely 
18794c09319SGrant Likely 	return dev;
18894c09319SGrant Likely }
18994c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
19094c09319SGrant Likely 
191591c1ee4SSantosh Shilimkar /**
19215c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1935fd200f3SGrant Likely  * @np: pointer to node to create device for
1945fd200f3SGrant Likely  * @bus_id: name to assign device
19515c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1965fd200f3SGrant Likely  * @parent: Linux device model parent device.
197cd1e6504SGrant Likely  *
1988c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
199cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
2005fd200f3SGrant Likely  */
201245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
20215c3597dSGrant Likely 					struct device_node *np,
2035fd200f3SGrant Likely 					const char *bus_id,
20415c3597dSGrant Likely 					void *platform_data,
2055fd200f3SGrant Likely 					struct device *parent)
2065fd200f3SGrant Likely {
20794a0cb1fSGrant Likely 	struct platform_device *dev;
2085fd200f3SGrant Likely 
209c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
210c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
211cd1e6504SGrant Likely 		return NULL;
212cd1e6504SGrant Likely 
2135fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
2145fd200f3SGrant Likely 	if (!dev)
215c6e126deSPawel Moll 		goto err_clear_flag;
2165fd200f3SGrant Likely 
217a5516219SRobin Murphy 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
218a5516219SRobin Murphy 	if (!dev->dev.dma_mask)
219a5516219SRobin Murphy 		dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
220eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
22115c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
222c706c239SMarc Zyngier 	of_msi_configure(&dev->dev, dev->dev.of_node);
2235fd200f3SGrant Likely 
2247096d042SGrant Likely 	if (of_device_add(dev) != 0) {
2257096d042SGrant Likely 		platform_device_put(dev);
226c6e126deSPawel Moll 		goto err_clear_flag;
2275fd200f3SGrant Likely 	}
2285fd200f3SGrant Likely 
2295fd200f3SGrant Likely 	return dev;
230c6e126deSPawel Moll 
231c6e126deSPawel Moll err_clear_flag:
232c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
233c6e126deSPawel Moll 	return NULL;
2345fd200f3SGrant Likely }
23515c3597dSGrant Likely 
23615c3597dSGrant Likely /**
23715c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
23815c3597dSGrant Likely  * @np: pointer to node to create device for
23915c3597dSGrant Likely  * @bus_id: name to assign device
24015c3597dSGrant Likely  * @parent: Linux device model parent device.
24115c3597dSGrant Likely  *
2428c8239c2SRob Herring  * Return: Pointer to created platform device, or NULL if a device was not
24315c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
24415c3597dSGrant Likely  */
24515c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
24615c3597dSGrant Likely 					    const char *bus_id,
24715c3597dSGrant Likely 					    struct device *parent)
24815c3597dSGrant Likely {
24915c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
25015c3597dSGrant Likely }
2515fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2525fd200f3SGrant Likely 
2535de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2545de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2555de1540bSGrant Likely 						 const char *bus_id,
2565de1540bSGrant Likely 						 void *platform_data,
2575de1540bSGrant Likely 						 struct device *parent)
2585de1540bSGrant Likely {
2595de1540bSGrant Likely 	struct amba_device *dev;
260854f695cSWang Kefeng 	int ret;
2615de1540bSGrant Likely 
2620d638a07SRob Herring 	pr_debug("Creating amba device %pOF\n", node);
2635de1540bSGrant Likely 
264c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
265c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2665de1540bSGrant Likely 		return NULL;
2675de1540bSGrant Likely 
268c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
269606ad42aSRob Herring 	if (!dev)
270c6e126deSPawel Moll 		goto err_clear_flag;
2715de1540bSGrant Likely 
2728c89ef7bSLinus Walleij 	/* AMBA devices only support a single DMA mask */
2738c89ef7bSLinus Walleij 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2748c89ef7bSLinus Walleij 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
2758c89ef7bSLinus Walleij 
2765de1540bSGrant Likely 	/* setup generic device info */
277ca5a75dfSAndy Shevchenko 	device_set_node(&dev->dev, of_fwnode_handle(node));
27843c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
2795de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2805de1540bSGrant Likely 	if (bus_id)
2815de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
2825de1540bSGrant Likely 	else
2835de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
2845de1540bSGrant Likely 
2855de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
28616b0c7caSRob Herring 	of_property_read_u32(node, "arm,primecell-periphid", &dev->periphid);
2875de1540bSGrant Likely 
2885de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
2892bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2900d638a07SRob Herring 		pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
2910d638a07SRob Herring 		       ret, node);
2925de1540bSGrant Likely 		goto err_free;
2932bc552dfSBartlomiej Zolnierkiewicz 	}
2945de1540bSGrant Likely 
295c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
2962bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2970d638a07SRob Herring 		pr_err("amba_device_add() failed (%d) for %pOF\n",
2980d638a07SRob Herring 		       ret, node);
2995de1540bSGrant Likely 		goto err_free;
3002bc552dfSBartlomiej Zolnierkiewicz 	}
3015de1540bSGrant Likely 
3025de1540bSGrant Likely 	return dev;
3035de1540bSGrant Likely 
3045de1540bSGrant Likely err_free:
305c0f72f8aSRussell King 	amba_device_put(dev);
306c6e126deSPawel Moll err_clear_flag:
307c6e126deSPawel Moll 	of_node_clear_flag(node, OF_POPULATED);
3085de1540bSGrant Likely 	return NULL;
3095de1540bSGrant Likely }
3105de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
3115de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
3125de1540bSGrant Likely 						 const char *bus_id,
3135de1540bSGrant Likely 						 void *platform_data,
3145de1540bSGrant Likely 						 struct device *parent)
3155de1540bSGrant Likely {
3165de1540bSGrant Likely 	return NULL;
3175de1540bSGrant Likely }
3185de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
3195de1540bSGrant Likely 
320f3896a7eSLee Jones /*
321f9a529b9SQi Zheng  * of_dev_lookup() - Given a device node, lookup the preferred Linux name
32215c3597dSGrant Likely  */
32315c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
32415c3597dSGrant Likely 				 struct device_node *np)
32515c3597dSGrant Likely {
326fc5cf80aSTony Lindgren 	const struct of_dev_auxdata *auxdata;
32715c3597dSGrant Likely 	struct resource res;
328fc5cf80aSTony Lindgren 	int compatible = 0;
329303f59d1SOlof Johansson 
330303f59d1SOlof Johansson 	if (!lookup)
331303f59d1SOlof Johansson 		return NULL;
332303f59d1SOlof Johansson 
333fc5cf80aSTony Lindgren 	auxdata = lookup;
334fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
335fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
33615c3597dSGrant Likely 			continue;
337fc5cf80aSTony Lindgren 		compatible++;
33884774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
339fc5cf80aSTony Lindgren 			if (res.start != auxdata->phys_addr)
34015c3597dSGrant Likely 				continue;
3410d638a07SRob Herring 		pr_debug("%pOF: devname=%s\n", np, auxdata->name);
342fc5cf80aSTony Lindgren 		return auxdata;
343fc5cf80aSTony Lindgren 	}
344fc5cf80aSTony Lindgren 
345fc5cf80aSTony Lindgren 	if (!compatible)
346fc5cf80aSTony Lindgren 		return NULL;
347fc5cf80aSTony Lindgren 
348fc5cf80aSTony Lindgren 	/* Try compatible match if no phys_addr and name are specified */
349fc5cf80aSTony Lindgren 	auxdata = lookup;
350fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
351fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
352fc5cf80aSTony Lindgren 			continue;
353fc5cf80aSTony Lindgren 		if (!auxdata->phys_addr && !auxdata->name) {
3540d638a07SRob Herring 			pr_debug("%pOF: compatible match\n", np);
355fc5cf80aSTony Lindgren 			return auxdata;
356fc5cf80aSTony Lindgren 		}
35715c3597dSGrant Likely 	}
358303f59d1SOlof Johansson 
35915c3597dSGrant Likely 	return NULL;
36015c3597dSGrant Likely }
36115c3597dSGrant Likely 
36215c3597dSGrant Likely /**
36338e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3645fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3651eed4c07SGrant Likely  * @matches: match table for bus nodes
366303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
36738e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
368303f59d1SOlof Johansson  * @strict: require compatible property
36938e9e21dSGrant Likely  *
37038e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
37138e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3725fd200f3SGrant Likely  */
37338e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3745fd200f3SGrant Likely 				  const struct of_device_id *matches,
37515c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
37629d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3775fd200f3SGrant Likely {
37815c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3795fd200f3SGrant Likely 	struct device_node *child;
38094a0cb1fSGrant Likely 	struct platform_device *dev;
38115c3597dSGrant Likely 	const char *bus_id = NULL;
38215c3597dSGrant Likely 	void *platform_data = NULL;
3835fd200f3SGrant Likely 	int rc = 0;
3845fd200f3SGrant Likely 
38529d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
38629d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
3870d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, no compatible prop\n",
3880d638a07SRob Herring 			 __func__, bus);
38929d4f8a4SGrant Likely 		return 0;
39029d4f8a4SGrant Likely 	}
39129d4f8a4SGrant Likely 
3924550fe63SViresh Kumar 	/* Skip nodes for which we don't want to create devices */
3934550fe63SViresh Kumar 	if (unlikely(of_match_node(of_skipped_node_table, bus))) {
3944550fe63SViresh Kumar 		pr_debug("%s() - skipping %pOF node\n", __func__, bus);
3954550fe63SViresh Kumar 		return 0;
3964550fe63SViresh Kumar 	}
3974550fe63SViresh Kumar 
39844a7185cSKefeng Wang 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
3990d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, already populated\n",
4000d638a07SRob Herring 			__func__, bus);
40144a7185cSKefeng Wang 		return 0;
40244a7185cSKefeng Wang 	}
40344a7185cSKefeng Wang 
40415c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
40515c3597dSGrant Likely 	if (auxdata) {
40615c3597dSGrant Likely 		bus_id = auxdata->name;
40715c3597dSGrant Likely 		platform_data = auxdata->platform_data;
40815c3597dSGrant Likely 	}
40915c3597dSGrant Likely 
4105de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
4112bc552dfSBartlomiej Zolnierkiewicz 		/*
4122bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
4132bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
4142bc552dfSBartlomiej Zolnierkiewicz 		 */
41515c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
4165de1540bSGrant Likely 		return 0;
4175de1540bSGrant Likely 	}
4185de1540bSGrant Likely 
41915c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
42038e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
42138e9e21dSGrant Likely 		return 0;
42238e9e21dSGrant Likely 
4235fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
4240d638a07SRob Herring 		pr_debug("   create child: %pOF\n", child);
42515c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
4265fd200f3SGrant Likely 		if (rc) {
4275fd200f3SGrant Likely 			of_node_put(child);
4285fd200f3SGrant Likely 			break;
4295fd200f3SGrant Likely 		}
4305fd200f3SGrant Likely 	}
43175f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
4325fd200f3SGrant Likely 	return rc;
4335fd200f3SGrant Likely }
4345fd200f3SGrant Likely 
4355fd200f3SGrant Likely /**
43638e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
4375fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
4381eed4c07SGrant Likely  * @matches: match table for bus nodes
4395fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
4405fd200f3SGrant Likely  *
4415fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
4425fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4435fd200f3SGrant Likely  */
4445fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4455fd200f3SGrant Likely 			  const struct of_device_id *matches,
4465fd200f3SGrant Likely 			  struct device *parent)
4475fd200f3SGrant Likely {
4485fd200f3SGrant Likely 	struct device_node *child;
4495fd200f3SGrant Likely 	int rc = 0;
4505fd200f3SGrant Likely 
4511eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4521eed4c07SGrant Likely 	if (!root)
45360d59913SGrant Likely 		return -EINVAL;
4545fd200f3SGrant Likely 
45544a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4560d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
4575fd200f3SGrant Likely 
4581eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4595fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
46015c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
46138e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4625fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4635fd200f3SGrant Likely 			continue;
46415c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
4657fad948aSJulia Lawall 		if (rc) {
4667fad948aSJulia Lawall 			of_node_put(child);
4675fd200f3SGrant Likely 			break;
4685fd200f3SGrant Likely 		}
4697fad948aSJulia Lawall 	}
47038e9e21dSGrant Likely 
4715fd200f3SGrant Likely 	of_node_put(root);
4725fd200f3SGrant Likely 	return rc;
4735fd200f3SGrant Likely }
4745fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
47529d4f8a4SGrant Likely 
47629d4f8a4SGrant Likely /**
47729d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
47829d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
47929d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
48092039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
48129d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
48229d4f8a4SGrant Likely  *
48329d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
48429d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
48529d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
48629d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
48729d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
48829d4f8a4SGrant Likely  * are selected by the @matches argument).
48929d4f8a4SGrant Likely  *
49029d4f8a4SGrant Likely  * New board support should be using this function instead of
49129d4f8a4SGrant Likely  * of_platform_bus_probe().
49229d4f8a4SGrant Likely  *
4938c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
49429d4f8a4SGrant Likely  */
49529d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
49629d4f8a4SGrant Likely 			const struct of_device_id *matches,
49715c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
49829d4f8a4SGrant Likely 			struct device *parent)
49929d4f8a4SGrant Likely {
50029d4f8a4SGrant Likely 	struct device_node *child;
50129d4f8a4SGrant Likely 	int rc = 0;
50229d4f8a4SGrant Likely 
50329d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
50429d4f8a4SGrant Likely 	if (!root)
50529d4f8a4SGrant Likely 		return -EINVAL;
50629d4f8a4SGrant Likely 
50744a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
5080d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
50944a7185cSKefeng Wang 
5105e666938SSaravana Kannan 	device_links_supplier_sync_state_pause();
51129d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
51215c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
5137fad948aSJulia Lawall 		if (rc) {
5147fad948aSJulia Lawall 			of_node_put(child);
51529d4f8a4SGrant Likely 			break;
51629d4f8a4SGrant Likely 		}
5177fad948aSJulia Lawall 	}
5185e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
5195e666938SSaravana Kannan 
5202d0747c4SGrant Likely 	of_node_set_flag(root, OF_POPULATED_BUS);
52129d4f8a4SGrant Likely 
52229d4f8a4SGrant Likely 	of_node_put(root);
52329d4f8a4SGrant Likely 	return rc;
52429d4f8a4SGrant Likely }
525e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
526c6e126deSPawel Moll 
52743443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root,
52843443ad6SHauke Mehrtens 				 const struct of_dev_auxdata *lookup,
52943443ad6SHauke Mehrtens 				 struct device *parent)
53043443ad6SHauke Mehrtens {
53143443ad6SHauke Mehrtens 	return of_platform_populate(root, of_default_bus_match_table, lookup,
53243443ad6SHauke Mehrtens 				    parent);
53343443ad6SHauke Mehrtens }
53443443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate);
53543443ad6SHauke Mehrtens 
536a50ff19dSBjorn Andersson static const struct of_device_id reserved_mem_matches[] = {
5377090d2f1SVincent Whitchurch 	{ .compatible = "phram" },
538d1de6d6cSBjorn Andersson 	{ .compatible = "qcom,rmtfs-mem" },
539312416d9SMahesh Sivasubramanian 	{ .compatible = "qcom,cmd-db" },
540b5af64fcSBjorn Andersson 	{ .compatible = "qcom,smem" },
541a50ff19dSBjorn Andersson 	{ .compatible = "ramoops" },
5425a3fa75aSNicolas Saenz Julienne 	{ .compatible = "nvmem-rmem" },
543f396ededSDavid Brazdil 	{ .compatible = "google,open-dice" },
544a50ff19dSBjorn Andersson 	{}
545a50ff19dSBjorn Andersson };
546a50ff19dSBjorn Andersson 
54744a7185cSKefeng Wang static int __init of_platform_default_populate_init(void)
54844a7185cSKefeng Wang {
549529182e2SKees Cook 	struct device_node *node;
550529182e2SKees Cook 
551ee9b280eSSaravana Kannan 	device_links_supplier_sync_state_pause();
552ee9b280eSSaravana Kannan 
553529182e2SKees Cook 	if (!of_have_populated_dt())
554529182e2SKees Cook 		return -ENODEV;
555529182e2SKees Cook 
55652b1b46cSThomas Zimmermann 	if (IS_ENABLED(CONFIG_PPC)) {
55752b1b46cSThomas Zimmermann 		struct device_node *boot_display = NULL;
55852b1b46cSThomas Zimmermann 		struct platform_device *dev;
559241d2fb5SMichal Suchanek 		int display_number = 0;
56052b1b46cSThomas Zimmermann 		int ret;
56152b1b46cSThomas Zimmermann 
56252b1b46cSThomas Zimmermann 		/* Check if we have a MacOS display without a node spec */
5632f0cb475SRob Herring 		if (of_property_present(of_chosen, "linux,bootx-noscreen")) {
56452b1b46cSThomas Zimmermann 			/*
56552b1b46cSThomas Zimmermann 			 * The old code tried to work out which node was the MacOS
56652b1b46cSThomas Zimmermann 			 * display based on the address. I'm dropping that since the
56752b1b46cSThomas Zimmermann 			 * lack of a node spec only happens with old BootX versions
56852b1b46cSThomas Zimmermann 			 * (users can update) and with this code, they'll still get
56952b1b46cSThomas Zimmermann 			 * a display (just not the palette hacks).
57052b1b46cSThomas Zimmermann 			 */
57152b1b46cSThomas Zimmermann 			dev = platform_device_alloc("bootx-noscreen", 0);
57252b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
57352b1b46cSThomas Zimmermann 				return -ENOMEM;
57452b1b46cSThomas Zimmermann 			ret = platform_device_add(dev);
57552b1b46cSThomas Zimmermann 			if (WARN_ON(ret)) {
57652b1b46cSThomas Zimmermann 				platform_device_put(dev);
57752b1b46cSThomas Zimmermann 				return ret;
57852b1b46cSThomas Zimmermann 			}
57952b1b46cSThomas Zimmermann 		}
58052b1b46cSThomas Zimmermann 
58152b1b46cSThomas Zimmermann 		/*
58252b1b46cSThomas Zimmermann 		 * For OF framebuffers, first create the device for the boot display,
58352b1b46cSThomas Zimmermann 		 * then for the other framebuffers. Only fail for the boot display;
58452b1b46cSThomas Zimmermann 		 * ignore errors for the rest.
58552b1b46cSThomas Zimmermann 		 */
58652b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
58752b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) ||
58852b1b46cSThomas Zimmermann 			    !of_get_property(node, "linux,boot-display", NULL))
58952b1b46cSThomas Zimmermann 				continue;
5900bb8f49cSRob Herring 			dev = of_platform_device_create(node, "of-display", NULL);
591241d2fb5SMichal Suchanek 			of_node_put(node);
59252b1b46cSThomas Zimmermann 			if (WARN_ON(!dev))
59352b1b46cSThomas Zimmermann 				return -ENOMEM;
59452b1b46cSThomas Zimmermann 			boot_display = node;
595241d2fb5SMichal Suchanek 			display_number++;
59652b1b46cSThomas Zimmermann 			break;
59752b1b46cSThomas Zimmermann 		}
59852b1b46cSThomas Zimmermann 		for_each_node_by_type(node, "display") {
599241d2fb5SMichal Suchanek 			char buf[14];
600241d2fb5SMichal Suchanek 			const char *of_display_format = "of-display.%d";
601241d2fb5SMichal Suchanek 
60252b1b46cSThomas Zimmermann 			if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
60352b1b46cSThomas Zimmermann 				continue;
604241d2fb5SMichal Suchanek 			ret = snprintf(buf, sizeof(buf), of_display_format, display_number++);
605241d2fb5SMichal Suchanek 			if (ret < sizeof(buf))
606241d2fb5SMichal Suchanek 				of_platform_device_create(node, buf, NULL);
60752b1b46cSThomas Zimmermann 		}
60852b1b46cSThomas Zimmermann 
60952b1b46cSThomas Zimmermann 	} else {
610529182e2SKees Cook 		/*
611a50ff19dSBjorn Andersson 		 * Handle certain compatibles explicitly, since we don't want to create
612a50ff19dSBjorn Andersson 		 * platform_devices for every node in /reserved-memory with a
613a50ff19dSBjorn Andersson 		 * "compatible",
614529182e2SKees Cook 		 */
615a50ff19dSBjorn Andersson 		for_each_matching_node(node, reserved_mem_matches)
616529182e2SKees Cook 			of_platform_device_create(node, NULL, NULL);
617529182e2SKees Cook 
6183aa0582fSSudeep Holla 		node = of_find_node_by_path("/firmware");
619e2105ca8SSudeep Holla 		if (node) {
6203aa0582fSSudeep Holla 			of_platform_populate(node, NULL, NULL, NULL);
621e2105ca8SSudeep Holla 			of_node_put(node);
622e2105ca8SSudeep Holla 		}
6233aa0582fSSudeep Holla 
6242f92ea21SHector Martin 		node = of_get_compatible_child(of_chosen, "simple-framebuffer");
625*3310288fSJavier Martinez Canillas 		if (node) {
626*3310288fSJavier Martinez Canillas 			/*
627*3310288fSJavier Martinez Canillas 			 * Since a "simple-framebuffer" device is already added
628*3310288fSJavier Martinez Canillas 			 * here, disable the Generic System Framebuffers (sysfb)
629*3310288fSJavier Martinez Canillas 			 * to prevent it from registering another device for the
630*3310288fSJavier Martinez Canillas 			 * system framebuffer later (e.g: using the screen_info
631*3310288fSJavier Martinez Canillas 			 * data that may had been filled as well).
632*3310288fSJavier Martinez Canillas 			 *
633*3310288fSJavier Martinez Canillas 			 * This can happen for example on DT systems that do EFI
634*3310288fSJavier Martinez Canillas 			 * booting and may provide a GOP handle to the EFI stub.
635*3310288fSJavier Martinez Canillas 			 */
636*3310288fSJavier Martinez Canillas 			sysfb_disable();
6372f92ea21SHector Martin 			of_platform_device_create(node, NULL, NULL);
6382f92ea21SHector Martin 			of_node_put(node);
639*3310288fSJavier Martinez Canillas 		}
6402f92ea21SHector Martin 
641529182e2SKees Cook 		/* Populate everything else. */
64244a7185cSKefeng Wang 		of_platform_default_populate(NULL, NULL, NULL);
64352b1b46cSThomas Zimmermann 	}
64444a7185cSKefeng Wang 
64544a7185cSKefeng Wang 	return 0;
64644a7185cSKefeng Wang }
64744a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init);
6485e666938SSaravana Kannan 
6495e666938SSaravana Kannan static int __init of_platform_sync_state_init(void)
6505e666938SSaravana Kannan {
6515e666938SSaravana Kannan 	device_links_supplier_sync_state_resume();
6525e666938SSaravana Kannan 	return 0;
6535e666938SSaravana Kannan }
6545e666938SSaravana Kannan late_initcall_sync(of_platform_sync_state_init);
65544a7185cSKefeng Wang 
656c2372c20SJan Glauber int of_platform_device_destroy(struct device *dev, void *data)
657c6e126deSPawel Moll {
658c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
65975f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
660c6e126deSPawel Moll 		return 0;
661c6e126deSPawel Moll 
66275f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
66375f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
66475f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
665c6e126deSPawel Moll 
666522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED);
667522811e9SSrinivas Kandagatla 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
668522811e9SSrinivas Kandagatla 
669c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
670c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
671c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
672c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
673c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
674c6e126deSPawel Moll #endif
675c6e126deSPawel Moll 
676c6e126deSPawel Moll 	return 0;
677c6e126deSPawel Moll }
678c2372c20SJan Glauber EXPORT_SYMBOL_GPL(of_platform_device_destroy);
679c6e126deSPawel Moll 
680c6e126deSPawel Moll /**
681c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
68275f353b6SGrant Likely  * @parent: device which children will be removed
683c6e126deSPawel Moll  *
684c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
6851080b5c0SJohan Hovold  * of the given device (and, recursively, their children) that have been
686c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
687c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
688c6e126deSPawel Moll  */
68975f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
690c6e126deSPawel Moll {
6912d0747c4SGrant Likely 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
69270a29209SThierry Reding 		device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
6932d0747c4SGrant Likely 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
6942d0747c4SGrant Likely 	}
695c6e126deSPawel Moll }
696c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
697c6e126deSPawel Moll 
69838b0b219SBenjamin Gaignard static void devm_of_platform_populate_release(struct device *dev, void *res)
69938b0b219SBenjamin Gaignard {
70038b0b219SBenjamin Gaignard 	of_platform_depopulate(*(struct device **)res);
70138b0b219SBenjamin Gaignard }
70238b0b219SBenjamin Gaignard 
70338b0b219SBenjamin Gaignard /**
70438b0b219SBenjamin Gaignard  * devm_of_platform_populate() - Populate platform_devices from device tree data
70538b0b219SBenjamin Gaignard  * @dev: device that requested to populate from device tree data
70638b0b219SBenjamin Gaignard  *
70738b0b219SBenjamin Gaignard  * Similar to of_platform_populate(), but will automatically call
70838b0b219SBenjamin Gaignard  * of_platform_depopulate() when the device is unbound from the bus.
70938b0b219SBenjamin Gaignard  *
7108c8239c2SRob Herring  * Return: 0 on success, < 0 on failure.
71138b0b219SBenjamin Gaignard  */
71238b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev)
71338b0b219SBenjamin Gaignard {
71438b0b219SBenjamin Gaignard 	struct device **ptr;
71538b0b219SBenjamin Gaignard 	int ret;
71638b0b219SBenjamin Gaignard 
71738b0b219SBenjamin Gaignard 	if (!dev)
71838b0b219SBenjamin Gaignard 		return -EINVAL;
71938b0b219SBenjamin Gaignard 
72038b0b219SBenjamin Gaignard 	ptr = devres_alloc(devm_of_platform_populate_release,
72138b0b219SBenjamin Gaignard 			   sizeof(*ptr), GFP_KERNEL);
72238b0b219SBenjamin Gaignard 	if (!ptr)
72338b0b219SBenjamin Gaignard 		return -ENOMEM;
72438b0b219SBenjamin Gaignard 
72538b0b219SBenjamin Gaignard 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
72638b0b219SBenjamin Gaignard 	if (ret) {
72738b0b219SBenjamin Gaignard 		devres_free(ptr);
72838b0b219SBenjamin Gaignard 	} else {
72938b0b219SBenjamin Gaignard 		*ptr = dev;
73038b0b219SBenjamin Gaignard 		devres_add(dev, ptr);
73138b0b219SBenjamin Gaignard 	}
73238b0b219SBenjamin Gaignard 
73338b0b219SBenjamin Gaignard 	return ret;
73438b0b219SBenjamin Gaignard }
73538b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate);
73638b0b219SBenjamin Gaignard 
73738b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data)
73838b0b219SBenjamin Gaignard {
73938b0b219SBenjamin Gaignard 	struct device **ptr = res;
74038b0b219SBenjamin Gaignard 
74138b0b219SBenjamin Gaignard 	if (!ptr) {
74238b0b219SBenjamin Gaignard 		WARN_ON(!ptr);
74338b0b219SBenjamin Gaignard 		return 0;
74438b0b219SBenjamin Gaignard 	}
74538b0b219SBenjamin Gaignard 
74638b0b219SBenjamin Gaignard 	return *ptr == data;
74738b0b219SBenjamin Gaignard }
74838b0b219SBenjamin Gaignard 
74938b0b219SBenjamin Gaignard /**
75038b0b219SBenjamin Gaignard  * devm_of_platform_depopulate() - Remove devices populated from device tree
75138b0b219SBenjamin Gaignard  * @dev: device that requested to depopulate from device tree data
75238b0b219SBenjamin Gaignard  *
75338b0b219SBenjamin Gaignard  * Complementary to devm_of_platform_populate(), this function removes children
7541080b5c0SJohan Hovold  * of the given device (and, recursively, their children) that have been
75538b0b219SBenjamin Gaignard  * created from their respective device tree nodes (and only those,
75638b0b219SBenjamin Gaignard  * leaving others - eg. manually created - unharmed).
75738b0b219SBenjamin Gaignard  */
75838b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev)
75938b0b219SBenjamin Gaignard {
76038b0b219SBenjamin Gaignard 	int ret;
76138b0b219SBenjamin Gaignard 
76238b0b219SBenjamin Gaignard 	ret = devres_release(dev, devm_of_platform_populate_release,
76338b0b219SBenjamin Gaignard 			     devm_of_platform_match, dev);
76438b0b219SBenjamin Gaignard 
76538b0b219SBenjamin Gaignard 	WARN_ON(ret);
76638b0b219SBenjamin Gaignard }
76738b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
76838b0b219SBenjamin Gaignard 
769801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC
770801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb,
771801d728cSPantelis Antoniou 				unsigned long action, void *arg)
772801d728cSPantelis Antoniou {
773801d728cSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
774801d728cSPantelis Antoniou 	struct platform_device *pdev_parent, *pdev;
775801d728cSPantelis Antoniou 	bool children_left;
776801d728cSPantelis Antoniou 
777801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
778801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
779801d728cSPantelis Antoniou 		/* verify that the parent is a bus */
780801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
781801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
782801d728cSPantelis Antoniou 
78315204ab1SPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
78415204ab1SPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
78515204ab1SPantelis Antoniou 			return NOTIFY_OK;
78615204ab1SPantelis Antoniou 
7871a50d940SGeert Uytterhoeven 		/*
7881a50d940SGeert Uytterhoeven 		 * Clear the flag before adding the device so that fw_devlink
7891a50d940SGeert Uytterhoeven 		 * doesn't skip adding consumers to this device.
7901a50d940SGeert Uytterhoeven 		 */
7911a50d940SGeert Uytterhoeven 		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
792801d728cSPantelis Antoniou 		/* pdev_parent may be NULL when no bus platform device */
793801d728cSPantelis Antoniou 		pdev_parent = of_find_device_by_node(rd->dn->parent);
794801d728cSPantelis Antoniou 		pdev = of_platform_device_create(rd->dn, NULL,
795801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
79683c4a4eeSRob Herring 		platform_device_put(pdev_parent);
797801d728cSPantelis Antoniou 
798801d728cSPantelis Antoniou 		if (pdev == NULL) {
7990d638a07SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
8000d638a07SRob Herring 					__func__, rd->dn);
801801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
802801d728cSPantelis Antoniou 			return notifier_from_errno(-EINVAL);
803801d728cSPantelis Antoniou 		}
804801d728cSPantelis Antoniou 		break;
805801d728cSPantelis Antoniou 
806801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
80715204ab1SPantelis Antoniou 
80815204ab1SPantelis Antoniou 		/* already depopulated? */
80915204ab1SPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
81015204ab1SPantelis Antoniou 			return NOTIFY_OK;
81115204ab1SPantelis Antoniou 
812801d728cSPantelis Antoniou 		/* find our device by node */
813801d728cSPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
814801d728cSPantelis Antoniou 		if (pdev == NULL)
815801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
816801d728cSPantelis Antoniou 
817801d728cSPantelis Antoniou 		/* unregister takes one ref away */
818801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
819801d728cSPantelis Antoniou 
820801d728cSPantelis Antoniou 		/* and put the reference of the find */
82183c4a4eeSRob Herring 		platform_device_put(pdev);
822801d728cSPantelis Antoniou 		break;
823801d728cSPantelis Antoniou 	}
824801d728cSPantelis Antoniou 
825801d728cSPantelis Antoniou 	return NOTIFY_OK;
826801d728cSPantelis Antoniou }
827801d728cSPantelis Antoniou 
828801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
829801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
830801d728cSPantelis Antoniou };
831801d728cSPantelis Antoniou 
832801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
833801d728cSPantelis Antoniou {
834801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
835801d728cSPantelis Antoniou }
836801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
837801d728cSPantelis Antoniou 
838964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
839