xref: /linux/drivers/of/platform.c (revision af6074fc9aa4b29517375634189ada1869eea598)
1*af6074fcSRob 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>
2009515ef5SSricharan R #include <linux/of_iommu.h>
219e3288dcSGrant Likely #include <linux/of_irq.h>
223f23de10SStephen Rothwell #include <linux/of_platform.h>
23eca39301SGrant Likely #include <linux/platform_device.h>
24eca39301SGrant Likely 
25cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
26cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
2722869a9eSLinus Walleij 	{ .compatible = "simple-mfd", },
28ecd76edeSPaul Burton 	{ .compatible = "isa", },
29cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
30cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
31cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
32cbb49c26SGrant Likely 	{} /* Empty terminated list */
33cbb49c26SGrant Likely };
34cbb49c26SGrant Likely 
35c6085584SJonas Bonn static int of_dev_node_match(struct device *dev, void *data)
36c6085584SJonas Bonn {
37c6085584SJonas Bonn 	return dev->of_node == data;
38c6085584SJonas Bonn }
39c6085584SJonas Bonn 
40c6085584SJonas Bonn /**
41c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
42c6085584SJonas Bonn  * @np: Pointer to device tree node
43c6085584SJonas Bonn  *
444fb373dfSJohan Hovold  * Takes a reference to the embedded struct device which needs to be dropped
454fb373dfSJohan Hovold  * after use.
464fb373dfSJohan Hovold  *
47c6085584SJonas Bonn  * Returns platform_device pointer, or NULL if not found
48c6085584SJonas Bonn  */
49c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
50c6085584SJonas Bonn {
51c6085584SJonas Bonn 	struct device *dev;
52c6085584SJonas Bonn 
53c6085584SJonas Bonn 	dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
54c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
55c6085584SJonas Bonn }
56c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
57c6085584SJonas Bonn 
58964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS
595fd200f3SGrant Likely /*
605fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
615fd200f3SGrant Likely  * each applicable node.
625fd200f3SGrant Likely  *
635fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
645fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
655fd200f3SGrant Likely  */
665fd200f3SGrant Likely 
675fd200f3SGrant Likely /**
6894c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
6994c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
7094c09319SGrant Likely  *
7107e461cdSGrant Likely  * This routine will first try using the translated bus address to
7207e461cdSGrant Likely  * derive a unique name. If it cannot, then it will prepend names from
7307e461cdSGrant Likely  * parent nodes until a unique name can be derived.
7494c09319SGrant Likely  */
75e553f539SFrank Rowand static void of_device_make_bus_id(struct device *dev)
7694c09319SGrant Likely {
7794c09319SGrant Likely 	struct device_node *node = dev->of_node;
7824fb530fSKim Phillips 	const __be32 *reg;
7994c09319SGrant Likely 	u64 addr;
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) {
8907e461cdSGrant Likely 			dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
9007e461cdSGrant Likely 				     (unsigned long long)addr, node->name,
9107e461cdSGrant Likely 				     dev_name(dev));
9294c09319SGrant Likely 			return;
9394c09319SGrant Likely 		}
9494c09319SGrant Likely 
9507e461cdSGrant Likely 		/* format arguments only used if dev_name() resolves to NULL */
9607e461cdSGrant Likely 		dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
9795e6b1faSRob Herring 			     kbasename(node->full_name), dev_name(dev));
9807e461cdSGrant Likely 		node = node->parent;
9907e461cdSGrant Likely 	}
10094c09319SGrant Likely }
10194c09319SGrant Likely 
10294c09319SGrant Likely /**
10394c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
10494c09319SGrant Likely  * @np: device node to assign to device
10594c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
10694c09319SGrant Likely  * @parent: Parent device.
10794c09319SGrant Likely  */
10894a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
10994c09319SGrant Likely 				  const char *bus_id,
11094c09319SGrant Likely 				  struct device *parent)
11194c09319SGrant Likely {
11294a0cb1fSGrant Likely 	struct platform_device *dev;
11352f6537cSAndres Salomon 	int rc, i, num_reg = 0, num_irq;
114ac80a51eSGrant Likely 	struct resource *res, temp_res;
11594c09319SGrant Likely 
1166d7e3bf8SAndy Shevchenko 	dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
1177096d042SGrant Likely 	if (!dev)
1187096d042SGrant Likely 		return NULL;
1197096d042SGrant Likely 
1207096d042SGrant Likely 	/* count the io and irq resources */
121ac80a51eSGrant Likely 	while (of_address_to_resource(np, num_reg, &temp_res) == 0)
122ac80a51eSGrant Likely 		num_reg++;
12352f6537cSAndres Salomon 	num_irq = of_irq_count(np);
124ac80a51eSGrant Likely 
125ac80a51eSGrant Likely 	/* Populate the resource table */
126ac80a51eSGrant Likely 	if (num_irq || num_reg) {
1277096d042SGrant Likely 		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
1287096d042SGrant Likely 		if (!res) {
1297096d042SGrant Likely 			platform_device_put(dev);
1307096d042SGrant Likely 			return NULL;
1317096d042SGrant Likely 		}
1327096d042SGrant Likely 
133ac80a51eSGrant Likely 		dev->num_resources = num_reg + num_irq;
134ac80a51eSGrant Likely 		dev->resource = res;
135ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
136ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
137ac80a51eSGrant Likely 			WARN_ON(rc);
138ac80a51eSGrant Likely 		}
1399ec36cafSRob Herring 		if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
1409ec36cafSRob Herring 			pr_debug("not all legacy IRQ resources mapped for %s\n",
1419ec36cafSRob Herring 				 np->name);
142ac80a51eSGrant Likely 	}
14394c09319SGrant Likely 
14494c09319SGrant Likely 	dev->dev.of_node = of_node_get(np);
145f94277afSRobin Murphy 	dev->dev.fwnode = &np->fwnode;
14643c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
14794c09319SGrant Likely 
14894c09319SGrant Likely 	if (bus_id)
14994c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
15094c09319SGrant Likely 	else
15194c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
15294c09319SGrant Likely 
15394c09319SGrant Likely 	return dev;
15494c09319SGrant Likely }
15594c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
15694c09319SGrant Likely 
157591c1ee4SSantosh Shilimkar /**
15815c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1595fd200f3SGrant Likely  * @np: pointer to node to create device for
1605fd200f3SGrant Likely  * @bus_id: name to assign device
16115c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1625fd200f3SGrant Likely  * @parent: Linux device model parent device.
163cd1e6504SGrant Likely  *
164cd1e6504SGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
165cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1665fd200f3SGrant Likely  */
167245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
16815c3597dSGrant Likely 					struct device_node *np,
1695fd200f3SGrant Likely 					const char *bus_id,
17015c3597dSGrant Likely 					void *platform_data,
1715fd200f3SGrant Likely 					struct device *parent)
1725fd200f3SGrant Likely {
17394a0cb1fSGrant Likely 	struct platform_device *dev;
1745fd200f3SGrant Likely 
175c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
176c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
177cd1e6504SGrant Likely 		return NULL;
178cd1e6504SGrant Likely 
1795fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
1805fd200f3SGrant Likely 	if (!dev)
181c6e126deSPawel Moll 		goto err_clear_flag;
1825fd200f3SGrant Likely 
183eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
18415c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
185c706c239SMarc Zyngier 	of_msi_configure(&dev->dev, dev->dev.of_node);
1865fd200f3SGrant Likely 
1877096d042SGrant Likely 	if (of_device_add(dev) != 0) {
1887096d042SGrant Likely 		platform_device_put(dev);
189c6e126deSPawel Moll 		goto err_clear_flag;
1905fd200f3SGrant Likely 	}
1915fd200f3SGrant Likely 
1925fd200f3SGrant Likely 	return dev;
193c6e126deSPawel Moll 
194c6e126deSPawel Moll err_clear_flag:
195c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
196c6e126deSPawel Moll 	return NULL;
1975fd200f3SGrant Likely }
19815c3597dSGrant Likely 
19915c3597dSGrant Likely /**
20015c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
20115c3597dSGrant Likely  * @np: pointer to node to create device for
20215c3597dSGrant Likely  * @bus_id: name to assign device
20315c3597dSGrant Likely  * @parent: Linux device model parent device.
20415c3597dSGrant Likely  *
20515c3597dSGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
20615c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
20715c3597dSGrant Likely  */
20815c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
20915c3597dSGrant Likely 					    const char *bus_id,
21015c3597dSGrant Likely 					    struct device *parent)
21115c3597dSGrant Likely {
21215c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
21315c3597dSGrant Likely }
2145fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2155fd200f3SGrant Likely 
2165de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2175de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2185de1540bSGrant Likely 						 const char *bus_id,
2195de1540bSGrant Likely 						 void *platform_data,
2205de1540bSGrant Likely 						 struct device *parent)
2215de1540bSGrant Likely {
2225de1540bSGrant Likely 	struct amba_device *dev;
2235de1540bSGrant Likely 	const void *prop;
2245de1540bSGrant Likely 	int i, ret;
2255de1540bSGrant Likely 
2260d638a07SRob Herring 	pr_debug("Creating amba device %pOF\n", node);
2275de1540bSGrant Likely 
228c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
229c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2305de1540bSGrant Likely 		return NULL;
2315de1540bSGrant Likely 
232c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
233606ad42aSRob Herring 	if (!dev)
234c6e126deSPawel Moll 		goto err_clear_flag;
2355de1540bSGrant Likely 
2365de1540bSGrant Likely 	/* setup generic device info */
2375de1540bSGrant Likely 	dev->dev.of_node = of_node_get(node);
238f94277afSRobin Murphy 	dev->dev.fwnode = &node->fwnode;
23943c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
2405de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2415de1540bSGrant Likely 	if (bus_id)
2425de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
2435de1540bSGrant Likely 	else
2445de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
2455de1540bSGrant Likely 
2465de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
2475de1540bSGrant Likely 	prop = of_get_property(node, "arm,primecell-periphid", NULL);
2485de1540bSGrant Likely 	if (prop)
2495de1540bSGrant Likely 		dev->periphid = of_read_ulong(prop, 1);
2505de1540bSGrant Likely 
2515de1540bSGrant Likely 	/* Decode the IRQs and address ranges */
2525de1540bSGrant Likely 	for (i = 0; i < AMBA_NR_IRQS; i++)
2535de1540bSGrant Likely 		dev->irq[i] = irq_of_parse_and_map(node, i);
2545de1540bSGrant Likely 
2555de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
2562bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2570d638a07SRob Herring 		pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
2580d638a07SRob Herring 		       ret, node);
2595de1540bSGrant Likely 		goto err_free;
2602bc552dfSBartlomiej Zolnierkiewicz 	}
2615de1540bSGrant Likely 
262c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
2632bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
2640d638a07SRob Herring 		pr_err("amba_device_add() failed (%d) for %pOF\n",
2650d638a07SRob Herring 		       ret, node);
2665de1540bSGrant Likely 		goto err_free;
2672bc552dfSBartlomiej Zolnierkiewicz 	}
2685de1540bSGrant Likely 
2695de1540bSGrant Likely 	return dev;
2705de1540bSGrant Likely 
2715de1540bSGrant Likely err_free:
272c0f72f8aSRussell King 	amba_device_put(dev);
273c6e126deSPawel Moll err_clear_flag:
274c6e126deSPawel Moll 	of_node_clear_flag(node, OF_POPULATED);
2755de1540bSGrant Likely 	return NULL;
2765de1540bSGrant Likely }
2775de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
2785de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2795de1540bSGrant Likely 						 const char *bus_id,
2805de1540bSGrant Likely 						 void *platform_data,
2815de1540bSGrant Likely 						 struct device *parent)
2825de1540bSGrant Likely {
2835de1540bSGrant Likely 	return NULL;
2845de1540bSGrant Likely }
2855de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
2865de1540bSGrant Likely 
2875fd200f3SGrant Likely /**
28815c3597dSGrant Likely  * of_devname_lookup() - Given a device node, lookup the preferred Linux name
28915c3597dSGrant Likely  */
29015c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
29115c3597dSGrant Likely 				 struct device_node *np)
29215c3597dSGrant Likely {
293fc5cf80aSTony Lindgren 	const struct of_dev_auxdata *auxdata;
29415c3597dSGrant Likely 	struct resource res;
295fc5cf80aSTony Lindgren 	int compatible = 0;
296303f59d1SOlof Johansson 
297303f59d1SOlof Johansson 	if (!lookup)
298303f59d1SOlof Johansson 		return NULL;
299303f59d1SOlof Johansson 
300fc5cf80aSTony Lindgren 	auxdata = lookup;
301fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
302fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
30315c3597dSGrant Likely 			continue;
304fc5cf80aSTony Lindgren 		compatible++;
30584774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
306fc5cf80aSTony Lindgren 			if (res.start != auxdata->phys_addr)
30715c3597dSGrant Likely 				continue;
3080d638a07SRob Herring 		pr_debug("%pOF: devname=%s\n", np, auxdata->name);
309fc5cf80aSTony Lindgren 		return auxdata;
310fc5cf80aSTony Lindgren 	}
311fc5cf80aSTony Lindgren 
312fc5cf80aSTony Lindgren 	if (!compatible)
313fc5cf80aSTony Lindgren 		return NULL;
314fc5cf80aSTony Lindgren 
315fc5cf80aSTony Lindgren 	/* Try compatible match if no phys_addr and name are specified */
316fc5cf80aSTony Lindgren 	auxdata = lookup;
317fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
318fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
319fc5cf80aSTony Lindgren 			continue;
320fc5cf80aSTony Lindgren 		if (!auxdata->phys_addr && !auxdata->name) {
3210d638a07SRob Herring 			pr_debug("%pOF: compatible match\n", np);
322fc5cf80aSTony Lindgren 			return auxdata;
323fc5cf80aSTony Lindgren 		}
32415c3597dSGrant Likely 	}
325303f59d1SOlof Johansson 
32615c3597dSGrant Likely 	return NULL;
32715c3597dSGrant Likely }
32815c3597dSGrant Likely 
32915c3597dSGrant Likely /**
33038e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3315fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3321eed4c07SGrant Likely  * @matches: match table for bus nodes
333303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
33438e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
335303f59d1SOlof Johansson  * @strict: require compatible property
33638e9e21dSGrant Likely  *
33738e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
33838e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3395fd200f3SGrant Likely  */
34038e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3415fd200f3SGrant Likely 				  const struct of_device_id *matches,
34215c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
34329d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3445fd200f3SGrant Likely {
34515c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3465fd200f3SGrant Likely 	struct device_node *child;
34794a0cb1fSGrant Likely 	struct platform_device *dev;
34815c3597dSGrant Likely 	const char *bus_id = NULL;
34915c3597dSGrant Likely 	void *platform_data = NULL;
3505fd200f3SGrant Likely 	int rc = 0;
3515fd200f3SGrant Likely 
35229d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
35329d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
3540d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, no compatible prop\n",
3550d638a07SRob Herring 			 __func__, bus);
35629d4f8a4SGrant Likely 		return 0;
35729d4f8a4SGrant Likely 	}
35829d4f8a4SGrant Likely 
35944a7185cSKefeng Wang 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
3600d638a07SRob Herring 		pr_debug("%s() - skipping %pOF, already populated\n",
3610d638a07SRob Herring 			__func__, bus);
36244a7185cSKefeng Wang 		return 0;
36344a7185cSKefeng Wang 	}
36444a7185cSKefeng Wang 
36515c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
36615c3597dSGrant Likely 	if (auxdata) {
36715c3597dSGrant Likely 		bus_id = auxdata->name;
36815c3597dSGrant Likely 		platform_data = auxdata->platform_data;
36915c3597dSGrant Likely 	}
37015c3597dSGrant Likely 
3715de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
3722bc552dfSBartlomiej Zolnierkiewicz 		/*
3732bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
3742bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
3752bc552dfSBartlomiej Zolnierkiewicz 		 */
37615c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
3775de1540bSGrant Likely 		return 0;
3785de1540bSGrant Likely 	}
3795de1540bSGrant Likely 
38015c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
38138e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
38238e9e21dSGrant Likely 		return 0;
38338e9e21dSGrant Likely 
3845fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
3850d638a07SRob Herring 		pr_debug("   create child: %pOF\n", child);
38615c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
3875fd200f3SGrant Likely 		if (rc) {
3885fd200f3SGrant Likely 			of_node_put(child);
3895fd200f3SGrant Likely 			break;
3905fd200f3SGrant Likely 		}
3915fd200f3SGrant Likely 	}
39275f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
3935fd200f3SGrant Likely 	return rc;
3945fd200f3SGrant Likely }
3955fd200f3SGrant Likely 
3965fd200f3SGrant Likely /**
39738e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
3985fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
3991eed4c07SGrant Likely  * @matches: match table for bus nodes
4005fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
4015fd200f3SGrant Likely  *
4025fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
4035fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4045fd200f3SGrant Likely  */
4055fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4065fd200f3SGrant Likely 			  const struct of_device_id *matches,
4075fd200f3SGrant Likely 			  struct device *parent)
4085fd200f3SGrant Likely {
4095fd200f3SGrant Likely 	struct device_node *child;
4105fd200f3SGrant Likely 	int rc = 0;
4115fd200f3SGrant Likely 
4121eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4131eed4c07SGrant Likely 	if (!root)
41460d59913SGrant Likely 		return -EINVAL;
4155fd200f3SGrant Likely 
41644a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4170d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
4185fd200f3SGrant Likely 
4191eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4205fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
42115c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
42238e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4235fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4245fd200f3SGrant Likely 			continue;
42515c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
4267fad948aSJulia Lawall 		if (rc) {
4277fad948aSJulia Lawall 			of_node_put(child);
4285fd200f3SGrant Likely 			break;
4295fd200f3SGrant Likely 		}
4307fad948aSJulia Lawall 	}
43138e9e21dSGrant Likely 
4325fd200f3SGrant Likely 	of_node_put(root);
4335fd200f3SGrant Likely 	return rc;
4345fd200f3SGrant Likely }
4355fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
43629d4f8a4SGrant Likely 
43729d4f8a4SGrant Likely /**
43829d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
43929d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
44029d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
44192039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
44229d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
44329d4f8a4SGrant Likely  *
44429d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
44529d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
44629d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
44729d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
44829d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
44929d4f8a4SGrant Likely  * are selected by the @matches argument).
45029d4f8a4SGrant Likely  *
45129d4f8a4SGrant Likely  * New board support should be using this function instead of
45229d4f8a4SGrant Likely  * of_platform_bus_probe().
45329d4f8a4SGrant Likely  *
45429d4f8a4SGrant Likely  * Returns 0 on success, < 0 on failure.
45529d4f8a4SGrant Likely  */
45629d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
45729d4f8a4SGrant Likely 			const struct of_device_id *matches,
45815c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
45929d4f8a4SGrant Likely 			struct device *parent)
46029d4f8a4SGrant Likely {
46129d4f8a4SGrant Likely 	struct device_node *child;
46229d4f8a4SGrant Likely 	int rc = 0;
46329d4f8a4SGrant Likely 
46429d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
46529d4f8a4SGrant Likely 	if (!root)
46629d4f8a4SGrant Likely 		return -EINVAL;
46729d4f8a4SGrant Likely 
46844a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4690d638a07SRob Herring 	pr_debug(" starting at: %pOF\n", root);
47044a7185cSKefeng Wang 
47129d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
47215c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
4737fad948aSJulia Lawall 		if (rc) {
4747fad948aSJulia Lawall 			of_node_put(child);
47529d4f8a4SGrant Likely 			break;
47629d4f8a4SGrant Likely 		}
4777fad948aSJulia Lawall 	}
4782d0747c4SGrant Likely 	of_node_set_flag(root, OF_POPULATED_BUS);
47929d4f8a4SGrant Likely 
48029d4f8a4SGrant Likely 	of_node_put(root);
48129d4f8a4SGrant Likely 	return rc;
48229d4f8a4SGrant Likely }
483e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
484c6e126deSPawel Moll 
48543443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root,
48643443ad6SHauke Mehrtens 				 const struct of_dev_auxdata *lookup,
48743443ad6SHauke Mehrtens 				 struct device *parent)
48843443ad6SHauke Mehrtens {
48943443ad6SHauke Mehrtens 	return of_platform_populate(root, of_default_bus_match_table, lookup,
49043443ad6SHauke Mehrtens 				    parent);
49143443ad6SHauke Mehrtens }
49243443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate);
49343443ad6SHauke Mehrtens 
494fc520f8bSKevin Hao #ifndef CONFIG_PPC
495a50ff19dSBjorn Andersson static const struct of_device_id reserved_mem_matches[] = {
496d1de6d6cSBjorn Andersson 	{ .compatible = "qcom,rmtfs-mem" },
497a50ff19dSBjorn Andersson 	{ .compatible = "ramoops" },
498a50ff19dSBjorn Andersson 	{}
499a50ff19dSBjorn Andersson };
500a50ff19dSBjorn Andersson 
50144a7185cSKefeng Wang static int __init of_platform_default_populate_init(void)
50244a7185cSKefeng Wang {
503529182e2SKees Cook 	struct device_node *node;
504529182e2SKees Cook 
505529182e2SKees Cook 	if (!of_have_populated_dt())
506529182e2SKees Cook 		return -ENODEV;
507529182e2SKees Cook 
508529182e2SKees Cook 	/*
509a50ff19dSBjorn Andersson 	 * Handle certain compatibles explicitly, since we don't want to create
510a50ff19dSBjorn Andersson 	 * platform_devices for every node in /reserved-memory with a
511a50ff19dSBjorn Andersson 	 * "compatible",
512529182e2SKees Cook 	 */
513a50ff19dSBjorn Andersson 	for_each_matching_node(node, reserved_mem_matches)
514529182e2SKees Cook 		of_platform_device_create(node, NULL, NULL);
515529182e2SKees Cook 
516529182e2SKees Cook 	/* Populate everything else. */
51744a7185cSKefeng Wang 	of_platform_default_populate(NULL, NULL, NULL);
51844a7185cSKefeng Wang 
51944a7185cSKefeng Wang 	return 0;
52044a7185cSKefeng Wang }
52144a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init);
522fc520f8bSKevin Hao #endif
52344a7185cSKefeng Wang 
524c2372c20SJan Glauber int of_platform_device_destroy(struct device *dev, void *data)
525c6e126deSPawel Moll {
526c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
52775f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
528c6e126deSPawel Moll 		return 0;
529c6e126deSPawel Moll 
53075f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
53175f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
53275f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
533c6e126deSPawel Moll 
534c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
535c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
536c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
537c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
538c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
539c6e126deSPawel Moll #endif
540c6e126deSPawel Moll 
541c6e126deSPawel Moll 	of_node_clear_flag(dev->of_node, OF_POPULATED);
54275f353b6SGrant Likely 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
543c6e126deSPawel Moll 	return 0;
544c6e126deSPawel Moll }
545c2372c20SJan Glauber EXPORT_SYMBOL_GPL(of_platform_device_destroy);
546c6e126deSPawel Moll 
547c6e126deSPawel Moll /**
548c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
54975f353b6SGrant Likely  * @parent: device which children will be removed
550c6e126deSPawel Moll  *
551c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
552c6e126deSPawel Moll  * of the given device (and, recurrently, their children) that have been
553c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
554c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
555c6e126deSPawel Moll  */
55675f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
557c6e126deSPawel Moll {
5582d0747c4SGrant Likely 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
55975f353b6SGrant Likely 		device_for_each_child(parent, NULL, of_platform_device_destroy);
5602d0747c4SGrant Likely 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
5612d0747c4SGrant Likely 	}
562c6e126deSPawel Moll }
563c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
564c6e126deSPawel Moll 
56538b0b219SBenjamin Gaignard static void devm_of_platform_populate_release(struct device *dev, void *res)
56638b0b219SBenjamin Gaignard {
56738b0b219SBenjamin Gaignard 	of_platform_depopulate(*(struct device **)res);
56838b0b219SBenjamin Gaignard }
56938b0b219SBenjamin Gaignard 
57038b0b219SBenjamin Gaignard /**
57138b0b219SBenjamin Gaignard  * devm_of_platform_populate() - Populate platform_devices from device tree data
57238b0b219SBenjamin Gaignard  * @dev: device that requested to populate from device tree data
57338b0b219SBenjamin Gaignard  *
57438b0b219SBenjamin Gaignard  * Similar to of_platform_populate(), but will automatically call
57538b0b219SBenjamin Gaignard  * of_platform_depopulate() when the device is unbound from the bus.
57638b0b219SBenjamin Gaignard  *
57738b0b219SBenjamin Gaignard  * Returns 0 on success, < 0 on failure.
57838b0b219SBenjamin Gaignard  */
57938b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev)
58038b0b219SBenjamin Gaignard {
58138b0b219SBenjamin Gaignard 	struct device **ptr;
58238b0b219SBenjamin Gaignard 	int ret;
58338b0b219SBenjamin Gaignard 
58438b0b219SBenjamin Gaignard 	if (!dev)
58538b0b219SBenjamin Gaignard 		return -EINVAL;
58638b0b219SBenjamin Gaignard 
58738b0b219SBenjamin Gaignard 	ptr = devres_alloc(devm_of_platform_populate_release,
58838b0b219SBenjamin Gaignard 			   sizeof(*ptr), GFP_KERNEL);
58938b0b219SBenjamin Gaignard 	if (!ptr)
59038b0b219SBenjamin Gaignard 		return -ENOMEM;
59138b0b219SBenjamin Gaignard 
59238b0b219SBenjamin Gaignard 	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
59338b0b219SBenjamin Gaignard 	if (ret) {
59438b0b219SBenjamin Gaignard 		devres_free(ptr);
59538b0b219SBenjamin Gaignard 	} else {
59638b0b219SBenjamin Gaignard 		*ptr = dev;
59738b0b219SBenjamin Gaignard 		devres_add(dev, ptr);
59838b0b219SBenjamin Gaignard 	}
59938b0b219SBenjamin Gaignard 
60038b0b219SBenjamin Gaignard 	return ret;
60138b0b219SBenjamin Gaignard }
60238b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate);
60338b0b219SBenjamin Gaignard 
60438b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data)
60538b0b219SBenjamin Gaignard {
60638b0b219SBenjamin Gaignard 	struct device **ptr = res;
60738b0b219SBenjamin Gaignard 
60838b0b219SBenjamin Gaignard 	if (!ptr) {
60938b0b219SBenjamin Gaignard 		WARN_ON(!ptr);
61038b0b219SBenjamin Gaignard 		return 0;
61138b0b219SBenjamin Gaignard 	}
61238b0b219SBenjamin Gaignard 
61338b0b219SBenjamin Gaignard 	return *ptr == data;
61438b0b219SBenjamin Gaignard }
61538b0b219SBenjamin Gaignard 
61638b0b219SBenjamin Gaignard /**
61738b0b219SBenjamin Gaignard  * devm_of_platform_depopulate() - Remove devices populated from device tree
61838b0b219SBenjamin Gaignard  * @dev: device that requested to depopulate from device tree data
61938b0b219SBenjamin Gaignard  *
62038b0b219SBenjamin Gaignard  * Complementary to devm_of_platform_populate(), this function removes children
62138b0b219SBenjamin Gaignard  * of the given device (and, recurrently, their children) that have been
62238b0b219SBenjamin Gaignard  * created from their respective device tree nodes (and only those,
62338b0b219SBenjamin Gaignard  * leaving others - eg. manually created - unharmed).
62438b0b219SBenjamin Gaignard  */
62538b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev)
62638b0b219SBenjamin Gaignard {
62738b0b219SBenjamin Gaignard 	int ret;
62838b0b219SBenjamin Gaignard 
62938b0b219SBenjamin Gaignard 	ret = devres_release(dev, devm_of_platform_populate_release,
63038b0b219SBenjamin Gaignard 			     devm_of_platform_match, dev);
63138b0b219SBenjamin Gaignard 
63238b0b219SBenjamin Gaignard 	WARN_ON(ret);
63338b0b219SBenjamin Gaignard }
63438b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
63538b0b219SBenjamin Gaignard 
636801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC
637801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb,
638801d728cSPantelis Antoniou 				unsigned long action, void *arg)
639801d728cSPantelis Antoniou {
640801d728cSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
641801d728cSPantelis Antoniou 	struct platform_device *pdev_parent, *pdev;
642801d728cSPantelis Antoniou 	bool children_left;
643801d728cSPantelis Antoniou 
644801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
645801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
646801d728cSPantelis Antoniou 		/* verify that the parent is a bus */
647801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
648801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
649801d728cSPantelis Antoniou 
65015204ab1SPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
65115204ab1SPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
65215204ab1SPantelis Antoniou 			return NOTIFY_OK;
65315204ab1SPantelis Antoniou 
654801d728cSPantelis Antoniou 		/* pdev_parent may be NULL when no bus platform device */
655801d728cSPantelis Antoniou 		pdev_parent = of_find_device_by_node(rd->dn->parent);
656801d728cSPantelis Antoniou 		pdev = of_platform_device_create(rd->dn, NULL,
657801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
658801d728cSPantelis Antoniou 		of_dev_put(pdev_parent);
659801d728cSPantelis Antoniou 
660801d728cSPantelis Antoniou 		if (pdev == NULL) {
6610d638a07SRob Herring 			pr_err("%s: failed to create for '%pOF'\n",
6620d638a07SRob Herring 					__func__, rd->dn);
663801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
664801d728cSPantelis Antoniou 			return notifier_from_errno(-EINVAL);
665801d728cSPantelis Antoniou 		}
666801d728cSPantelis Antoniou 		break;
667801d728cSPantelis Antoniou 
668801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
66915204ab1SPantelis Antoniou 
67015204ab1SPantelis Antoniou 		/* already depopulated? */
67115204ab1SPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
67215204ab1SPantelis Antoniou 			return NOTIFY_OK;
67315204ab1SPantelis Antoniou 
674801d728cSPantelis Antoniou 		/* find our device by node */
675801d728cSPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
676801d728cSPantelis Antoniou 		if (pdev == NULL)
677801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
678801d728cSPantelis Antoniou 
679801d728cSPantelis Antoniou 		/* unregister takes one ref away */
680801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
681801d728cSPantelis Antoniou 
682801d728cSPantelis Antoniou 		/* and put the reference of the find */
683801d728cSPantelis Antoniou 		of_dev_put(pdev);
684801d728cSPantelis Antoniou 		break;
685801d728cSPantelis Antoniou 	}
686801d728cSPantelis Antoniou 
687801d728cSPantelis Antoniou 	return NOTIFY_OK;
688801d728cSPantelis Antoniou }
689801d728cSPantelis Antoniou 
690801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
691801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
692801d728cSPantelis Antoniou };
693801d728cSPantelis Antoniou 
694801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
695801d728cSPantelis Antoniou {
696801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
697801d728cSPantelis Antoniou }
698801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
699801d728cSPantelis Antoniou 
700964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
701