xref: /linux/drivers/of/platform.c (revision fc520f8b4fa35ceb36f0ad031c1a297968788236)
13f23de10SStephen Rothwell /*
23f23de10SStephen Rothwell  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
33f23de10SStephen Rothwell  *			 <benh@kernel.crashing.org>
43f23de10SStephen Rothwell  *    and		 Arnd Bergmann, IBM Corp.
53f23de10SStephen Rothwell  *    Merged from powerpc/kernel/of_platform.c and
63f23de10SStephen Rothwell  *    sparc{,64}/kernel/of_device.c by Stephen Rothwell
73f23de10SStephen Rothwell  *
83f23de10SStephen Rothwell  *  This program is free software; you can redistribute it and/or
93f23de10SStephen Rothwell  *  modify it under the terms of the GNU General Public License
103f23de10SStephen Rothwell  *  as published by the Free Software Foundation; either version
113f23de10SStephen Rothwell  *  2 of the License, or (at your option) any later version.
123f23de10SStephen Rothwell  *
133f23de10SStephen Rothwell  */
14606ad42aSRob Herring 
15606ad42aSRob Herring #define pr_fmt(fmt)	"OF: " fmt
16606ad42aSRob Herring 
173f23de10SStephen Rothwell #include <linux/errno.h>
185c457083SStephen Rothwell #include <linux/module.h>
195de1540bSGrant Likely #include <linux/amba/bus.h>
203f23de10SStephen Rothwell #include <linux/device.h>
215fd200f3SGrant Likely #include <linux/dma-mapping.h>
2250ef5284SGrant Likely #include <linux/slab.h>
239e3288dcSGrant Likely #include <linux/of_address.h>
243f23de10SStephen Rothwell #include <linux/of_device.h>
259e3288dcSGrant Likely #include <linux/of_irq.h>
263f23de10SStephen Rothwell #include <linux/of_platform.h>
27eca39301SGrant Likely #include <linux/platform_device.h>
28eca39301SGrant Likely 
29cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
30cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
3122869a9eSLinus Walleij 	{ .compatible = "simple-mfd", },
32cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
33cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
34cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
35cbb49c26SGrant Likely 	{} /* Empty terminated list */
36cbb49c26SGrant Likely };
37cbb49c26SGrant Likely 
38c6085584SJonas Bonn static int of_dev_node_match(struct device *dev, void *data)
39c6085584SJonas Bonn {
40c6085584SJonas Bonn 	return dev->of_node == data;
41c6085584SJonas Bonn }
42c6085584SJonas Bonn 
43c6085584SJonas Bonn /**
44c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
45c6085584SJonas Bonn  * @np: Pointer to device tree node
46c6085584SJonas Bonn  *
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  */
75c6601225SGrant Likely 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",
9707e461cdSGrant Likely 			     strrchr(node->full_name, '/') + 1, 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 
1167096d042SGrant Likely 	dev = platform_device_alloc("", -1);
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);
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 
15697890ba9SWill Deacon static void of_dma_deconfigure(struct device *dev)
15797890ba9SWill Deacon {
15897890ba9SWill Deacon 	arch_teardown_dma_ops(dev);
159591c1ee4SSantosh Shilimkar }
160591c1ee4SSantosh Shilimkar 
161591c1ee4SSantosh Shilimkar /**
16215c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1635fd200f3SGrant Likely  * @np: pointer to node to create device for
1645fd200f3SGrant Likely  * @bus_id: name to assign device
16515c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1665fd200f3SGrant Likely  * @parent: Linux device model parent device.
167cd1e6504SGrant Likely  *
168cd1e6504SGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
169cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1705fd200f3SGrant Likely  */
171245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
17215c3597dSGrant Likely 					struct device_node *np,
1735fd200f3SGrant Likely 					const char *bus_id,
17415c3597dSGrant Likely 					void *platform_data,
1755fd200f3SGrant Likely 					struct device *parent)
1765fd200f3SGrant Likely {
17794a0cb1fSGrant Likely 	struct platform_device *dev;
1785fd200f3SGrant Likely 
179c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
180c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
181cd1e6504SGrant Likely 		return NULL;
182cd1e6504SGrant Likely 
1835fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
1845fd200f3SGrant Likely 	if (!dev)
185c6e126deSPawel Moll 		goto err_clear_flag;
1865fd200f3SGrant Likely 
187eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
18815c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
1891f5c69aaSMurali Karicheri 	of_dma_configure(&dev->dev, dev->dev.of_node);
190c706c239SMarc Zyngier 	of_msi_configure(&dev->dev, dev->dev.of_node);
1915fd200f3SGrant Likely 
1927096d042SGrant Likely 	if (of_device_add(dev) != 0) {
19397890ba9SWill Deacon 		of_dma_deconfigure(&dev->dev);
1947096d042SGrant Likely 		platform_device_put(dev);
195c6e126deSPawel Moll 		goto err_clear_flag;
1965fd200f3SGrant Likely 	}
1975fd200f3SGrant Likely 
1985fd200f3SGrant Likely 	return dev;
199c6e126deSPawel Moll 
200c6e126deSPawel Moll err_clear_flag:
201c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
202c6e126deSPawel Moll 	return NULL;
2035fd200f3SGrant Likely }
20415c3597dSGrant Likely 
20515c3597dSGrant Likely /**
20615c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
20715c3597dSGrant Likely  * @np: pointer to node to create device for
20815c3597dSGrant Likely  * @bus_id: name to assign device
20915c3597dSGrant Likely  * @parent: Linux device model parent device.
21015c3597dSGrant Likely  *
21115c3597dSGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
21215c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
21315c3597dSGrant Likely  */
21415c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
21515c3597dSGrant Likely 					    const char *bus_id,
21615c3597dSGrant Likely 					    struct device *parent)
21715c3597dSGrant Likely {
21815c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
21915c3597dSGrant Likely }
2205fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2215fd200f3SGrant Likely 
2225de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2235de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2245de1540bSGrant Likely 						 const char *bus_id,
2255de1540bSGrant Likely 						 void *platform_data,
2265de1540bSGrant Likely 						 struct device *parent)
2275de1540bSGrant Likely {
2285de1540bSGrant Likely 	struct amba_device *dev;
2295de1540bSGrant Likely 	const void *prop;
2305de1540bSGrant Likely 	int i, ret;
2315de1540bSGrant Likely 
2325de1540bSGrant Likely 	pr_debug("Creating amba device %s\n", node->full_name);
2335de1540bSGrant Likely 
234c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
235c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2365de1540bSGrant Likely 		return NULL;
2375de1540bSGrant Likely 
238c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
239606ad42aSRob Herring 	if (!dev)
240c6e126deSPawel Moll 		goto err_clear_flag;
2415de1540bSGrant Likely 
2425de1540bSGrant Likely 	/* setup generic device info */
2435de1540bSGrant Likely 	dev->dev.of_node = of_node_get(node);
24443c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
2455de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2465de1540bSGrant Likely 	if (bus_id)
2475de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
2485de1540bSGrant Likely 	else
2495de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
2501f5c69aaSMurali Karicheri 	of_dma_configure(&dev->dev, dev->dev.of_node);
2515de1540bSGrant Likely 
2525de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
2535de1540bSGrant Likely 	prop = of_get_property(node, "arm,primecell-periphid", NULL);
2545de1540bSGrant Likely 	if (prop)
2555de1540bSGrant Likely 		dev->periphid = of_read_ulong(prop, 1);
2565de1540bSGrant Likely 
2575de1540bSGrant Likely 	/* Decode the IRQs and address ranges */
2585de1540bSGrant Likely 	for (i = 0; i < AMBA_NR_IRQS; i++)
2595de1540bSGrant Likely 		dev->irq[i] = irq_of_parse_and_map(node, i);
2605de1540bSGrant Likely 
2615de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
2622bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
263606ad42aSRob Herring 		pr_err("amba: of_address_to_resource() failed (%d) for %s\n",
264606ad42aSRob Herring 		       ret, node->full_name);
2655de1540bSGrant Likely 		goto err_free;
2662bc552dfSBartlomiej Zolnierkiewicz 	}
2675de1540bSGrant Likely 
268c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
2692bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
270606ad42aSRob Herring 		pr_err("amba_device_add() failed (%d) for %s\n",
271606ad42aSRob Herring 		       ret, node->full_name);
2725de1540bSGrant Likely 		goto err_free;
2732bc552dfSBartlomiej Zolnierkiewicz 	}
2745de1540bSGrant Likely 
2755de1540bSGrant Likely 	return dev;
2765de1540bSGrant Likely 
2775de1540bSGrant Likely err_free:
278c0f72f8aSRussell King 	amba_device_put(dev);
279c6e126deSPawel Moll err_clear_flag:
280c6e126deSPawel Moll 	of_node_clear_flag(node, OF_POPULATED);
2815de1540bSGrant Likely 	return NULL;
2825de1540bSGrant Likely }
2835de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
2845de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2855de1540bSGrant Likely 						 const char *bus_id,
2865de1540bSGrant Likely 						 void *platform_data,
2875de1540bSGrant Likely 						 struct device *parent)
2885de1540bSGrant Likely {
2895de1540bSGrant Likely 	return NULL;
2905de1540bSGrant Likely }
2915de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
2925de1540bSGrant Likely 
2935fd200f3SGrant Likely /**
29415c3597dSGrant Likely  * of_devname_lookup() - Given a device node, lookup the preferred Linux name
29515c3597dSGrant Likely  */
29615c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
29715c3597dSGrant Likely 				 struct device_node *np)
29815c3597dSGrant Likely {
299fc5cf80aSTony Lindgren 	const struct of_dev_auxdata *auxdata;
30015c3597dSGrant Likely 	struct resource res;
301fc5cf80aSTony Lindgren 	int compatible = 0;
302303f59d1SOlof Johansson 
303303f59d1SOlof Johansson 	if (!lookup)
304303f59d1SOlof Johansson 		return NULL;
305303f59d1SOlof Johansson 
306fc5cf80aSTony Lindgren 	auxdata = lookup;
307fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
308fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
30915c3597dSGrant Likely 			continue;
310fc5cf80aSTony Lindgren 		compatible++;
31184774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
312fc5cf80aSTony Lindgren 			if (res.start != auxdata->phys_addr)
31315c3597dSGrant Likely 				continue;
314fc5cf80aSTony Lindgren 		pr_debug("%s: devname=%s\n", np->full_name, auxdata->name);
315fc5cf80aSTony Lindgren 		return auxdata;
316fc5cf80aSTony Lindgren 	}
317fc5cf80aSTony Lindgren 
318fc5cf80aSTony Lindgren 	if (!compatible)
319fc5cf80aSTony Lindgren 		return NULL;
320fc5cf80aSTony Lindgren 
321fc5cf80aSTony Lindgren 	/* Try compatible match if no phys_addr and name are specified */
322fc5cf80aSTony Lindgren 	auxdata = lookup;
323fc5cf80aSTony Lindgren 	for (; auxdata->compatible; auxdata++) {
324fc5cf80aSTony Lindgren 		if (!of_device_is_compatible(np, auxdata->compatible))
325fc5cf80aSTony Lindgren 			continue;
326fc5cf80aSTony Lindgren 		if (!auxdata->phys_addr && !auxdata->name) {
327fc5cf80aSTony Lindgren 			pr_debug("%s: compatible match\n", np->full_name);
328fc5cf80aSTony Lindgren 			return auxdata;
329fc5cf80aSTony Lindgren 		}
33015c3597dSGrant Likely 	}
331303f59d1SOlof Johansson 
33215c3597dSGrant Likely 	return NULL;
33315c3597dSGrant Likely }
33415c3597dSGrant Likely 
33515c3597dSGrant Likely /**
33638e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3375fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3381eed4c07SGrant Likely  * @matches: match table for bus nodes
339303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
34038e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
341303f59d1SOlof Johansson  * @strict: require compatible property
34238e9e21dSGrant Likely  *
34338e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
34438e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3455fd200f3SGrant Likely  */
34638e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3475fd200f3SGrant Likely 				  const struct of_device_id *matches,
34815c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
34929d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3505fd200f3SGrant Likely {
35115c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3525fd200f3SGrant Likely 	struct device_node *child;
35394a0cb1fSGrant Likely 	struct platform_device *dev;
35415c3597dSGrant Likely 	const char *bus_id = NULL;
35515c3597dSGrant Likely 	void *platform_data = NULL;
3565fd200f3SGrant Likely 	int rc = 0;
3575fd200f3SGrant Likely 
35829d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
35929d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
36029d4f8a4SGrant Likely 		pr_debug("%s() - skipping %s, no compatible prop\n",
36129d4f8a4SGrant Likely 			 __func__, bus->full_name);
36229d4f8a4SGrant Likely 		return 0;
36329d4f8a4SGrant Likely 	}
36429d4f8a4SGrant Likely 
36544a7185cSKefeng Wang 	if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
36644a7185cSKefeng Wang 		pr_debug("%s() - skipping %s, already populated\n",
36744a7185cSKefeng Wang 			__func__, bus->full_name);
36844a7185cSKefeng Wang 		return 0;
36944a7185cSKefeng Wang 	}
37044a7185cSKefeng Wang 
37115c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
37215c3597dSGrant Likely 	if (auxdata) {
37315c3597dSGrant Likely 		bus_id = auxdata->name;
37415c3597dSGrant Likely 		platform_data = auxdata->platform_data;
37515c3597dSGrant Likely 	}
37615c3597dSGrant Likely 
3775de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
3782bc552dfSBartlomiej Zolnierkiewicz 		/*
3792bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
3802bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
3812bc552dfSBartlomiej Zolnierkiewicz 		 */
38215c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
3835de1540bSGrant Likely 		return 0;
3845de1540bSGrant Likely 	}
3855de1540bSGrant Likely 
38615c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
38738e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
38838e9e21dSGrant Likely 		return 0;
38938e9e21dSGrant Likely 
3905fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
3915fd200f3SGrant Likely 		pr_debug("   create child: %s\n", child->full_name);
39215c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
3935fd200f3SGrant Likely 		if (rc) {
3945fd200f3SGrant Likely 			of_node_put(child);
3955fd200f3SGrant Likely 			break;
3965fd200f3SGrant Likely 		}
3975fd200f3SGrant Likely 	}
39875f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
3995fd200f3SGrant Likely 	return rc;
4005fd200f3SGrant Likely }
4015fd200f3SGrant Likely 
4025fd200f3SGrant Likely /**
40338e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
4045fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
4051eed4c07SGrant Likely  * @matches: match table for bus nodes
4065fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
4075fd200f3SGrant Likely  *
4085fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
4095fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4105fd200f3SGrant Likely  */
4115fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4125fd200f3SGrant Likely 			  const struct of_device_id *matches,
4135fd200f3SGrant Likely 			  struct device *parent)
4145fd200f3SGrant Likely {
4155fd200f3SGrant Likely 	struct device_node *child;
4165fd200f3SGrant Likely 	int rc = 0;
4175fd200f3SGrant Likely 
4181eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4191eed4c07SGrant Likely 	if (!root)
42060d59913SGrant Likely 		return -EINVAL;
4215fd200f3SGrant Likely 
42244a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
4235fd200f3SGrant Likely 	pr_debug(" starting at: %s\n", root->full_name);
4245fd200f3SGrant Likely 
4251eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4265fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
42715c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
42838e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4295fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4305fd200f3SGrant Likely 			continue;
43115c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
4327fad948aSJulia Lawall 		if (rc) {
4337fad948aSJulia Lawall 			of_node_put(child);
4345fd200f3SGrant Likely 			break;
4355fd200f3SGrant Likely 		}
4367fad948aSJulia Lawall 	}
43738e9e21dSGrant Likely 
4385fd200f3SGrant Likely 	of_node_put(root);
4395fd200f3SGrant Likely 	return rc;
4405fd200f3SGrant Likely }
4415fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
44229d4f8a4SGrant Likely 
44329d4f8a4SGrant Likely /**
44429d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
44529d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
44629d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
44792039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
44829d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
44929d4f8a4SGrant Likely  *
45029d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
45129d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
45229d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
45329d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
45429d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
45529d4f8a4SGrant Likely  * are selected by the @matches argument).
45629d4f8a4SGrant Likely  *
45729d4f8a4SGrant Likely  * New board support should be using this function instead of
45829d4f8a4SGrant Likely  * of_platform_bus_probe().
45929d4f8a4SGrant Likely  *
46029d4f8a4SGrant Likely  * Returns 0 on success, < 0 on failure.
46129d4f8a4SGrant Likely  */
46229d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
46329d4f8a4SGrant Likely 			const struct of_device_id *matches,
46415c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
46529d4f8a4SGrant Likely 			struct device *parent)
46629d4f8a4SGrant Likely {
46729d4f8a4SGrant Likely 	struct device_node *child;
46829d4f8a4SGrant Likely 	int rc = 0;
46929d4f8a4SGrant Likely 
47029d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
47129d4f8a4SGrant Likely 	if (!root)
47229d4f8a4SGrant Likely 		return -EINVAL;
47329d4f8a4SGrant Likely 
47444a7185cSKefeng Wang 	pr_debug("%s()\n", __func__);
47544a7185cSKefeng Wang 	pr_debug(" starting at: %s\n", root->full_name);
47644a7185cSKefeng Wang 
47729d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
47815c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
4797fad948aSJulia Lawall 		if (rc) {
4807fad948aSJulia Lawall 			of_node_put(child);
48129d4f8a4SGrant Likely 			break;
48229d4f8a4SGrant Likely 		}
4837fad948aSJulia Lawall 	}
4842d0747c4SGrant Likely 	of_node_set_flag(root, OF_POPULATED_BUS);
48529d4f8a4SGrant Likely 
48629d4f8a4SGrant Likely 	of_node_put(root);
48729d4f8a4SGrant Likely 	return rc;
48829d4f8a4SGrant Likely }
489e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
490c6e126deSPawel Moll 
49143443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root,
49243443ad6SHauke Mehrtens 				 const struct of_dev_auxdata *lookup,
49343443ad6SHauke Mehrtens 				 struct device *parent)
49443443ad6SHauke Mehrtens {
49543443ad6SHauke Mehrtens 	return of_platform_populate(root, of_default_bus_match_table, lookup,
49643443ad6SHauke Mehrtens 				    parent);
49743443ad6SHauke Mehrtens }
49843443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate);
49943443ad6SHauke Mehrtens 
500*fc520f8bSKevin Hao #ifndef CONFIG_PPC
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 	/*
509529182e2SKees Cook 	 * Handle ramoops explicitly, since it is inside /reserved-memory,
510529182e2SKees Cook 	 * which lacks a "compatible" property.
511529182e2SKees Cook 	 */
512529182e2SKees Cook 	node = of_find_node_by_path("/reserved-memory");
513529182e2SKees Cook 	if (node) {
514529182e2SKees Cook 		node = of_find_compatible_node(node, NULL, "ramoops");
515529182e2SKees Cook 		if (node)
516529182e2SKees Cook 			of_platform_device_create(node, NULL, NULL);
517529182e2SKees Cook 	}
518529182e2SKees Cook 
519529182e2SKees Cook 	/* Populate everything else. */
52044a7185cSKefeng Wang 	of_platform_default_populate(NULL, NULL, NULL);
52144a7185cSKefeng Wang 
52244a7185cSKefeng Wang 	return 0;
52344a7185cSKefeng Wang }
52444a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init);
525*fc520f8bSKevin Hao #endif
52644a7185cSKefeng Wang 
527c6e126deSPawel Moll static int of_platform_device_destroy(struct device *dev, void *data)
528c6e126deSPawel Moll {
529c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
53075f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
531c6e126deSPawel Moll 		return 0;
532c6e126deSPawel Moll 
53375f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
53475f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
53575f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
536c6e126deSPawel Moll 
537c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
538c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
539c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
540c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
541c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
542c6e126deSPawel Moll #endif
543c6e126deSPawel Moll 
5440495cb75SWill Deacon 	of_dma_deconfigure(dev);
545c6e126deSPawel Moll 	of_node_clear_flag(dev->of_node, OF_POPULATED);
54675f353b6SGrant Likely 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
547c6e126deSPawel Moll 	return 0;
548c6e126deSPawel Moll }
549c6e126deSPawel Moll 
550c6e126deSPawel Moll /**
551c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
55275f353b6SGrant Likely  * @parent: device which children will be removed
553c6e126deSPawel Moll  *
554c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
555c6e126deSPawel Moll  * of the given device (and, recurrently, their children) that have been
556c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
557c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
558c6e126deSPawel Moll  *
559c6e126deSPawel Moll  * Returns 0 when all children devices have been removed or
560c6e126deSPawel Moll  * -EBUSY when some children remained.
561c6e126deSPawel Moll  */
56275f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
563c6e126deSPawel Moll {
5642d0747c4SGrant Likely 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
56575f353b6SGrant Likely 		device_for_each_child(parent, NULL, of_platform_device_destroy);
5662d0747c4SGrant Likely 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
5672d0747c4SGrant Likely 	}
568c6e126deSPawel Moll }
569c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
570c6e126deSPawel Moll 
571801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC
572801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb,
573801d728cSPantelis Antoniou 				unsigned long action, void *arg)
574801d728cSPantelis Antoniou {
575801d728cSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
576801d728cSPantelis Antoniou 	struct platform_device *pdev_parent, *pdev;
577801d728cSPantelis Antoniou 	bool children_left;
578801d728cSPantelis Antoniou 
579801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
580801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
581801d728cSPantelis Antoniou 		/* verify that the parent is a bus */
582801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
583801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
584801d728cSPantelis Antoniou 
58515204ab1SPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
58615204ab1SPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
58715204ab1SPantelis Antoniou 			return NOTIFY_OK;
58815204ab1SPantelis Antoniou 
589801d728cSPantelis Antoniou 		/* pdev_parent may be NULL when no bus platform device */
590801d728cSPantelis Antoniou 		pdev_parent = of_find_device_by_node(rd->dn->parent);
591801d728cSPantelis Antoniou 		pdev = of_platform_device_create(rd->dn, NULL,
592801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
593801d728cSPantelis Antoniou 		of_dev_put(pdev_parent);
594801d728cSPantelis Antoniou 
595801d728cSPantelis Antoniou 		if (pdev == NULL) {
596801d728cSPantelis Antoniou 			pr_err("%s: failed to create for '%s'\n",
597801d728cSPantelis Antoniou 					__func__, rd->dn->full_name);
598801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
599801d728cSPantelis Antoniou 			return notifier_from_errno(-EINVAL);
600801d728cSPantelis Antoniou 		}
601801d728cSPantelis Antoniou 		break;
602801d728cSPantelis Antoniou 
603801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
60415204ab1SPantelis Antoniou 
60515204ab1SPantelis Antoniou 		/* already depopulated? */
60615204ab1SPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
60715204ab1SPantelis Antoniou 			return NOTIFY_OK;
60815204ab1SPantelis Antoniou 
609801d728cSPantelis Antoniou 		/* find our device by node */
610801d728cSPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
611801d728cSPantelis Antoniou 		if (pdev == NULL)
612801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
613801d728cSPantelis Antoniou 
614801d728cSPantelis Antoniou 		/* unregister takes one ref away */
615801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
616801d728cSPantelis Antoniou 
617801d728cSPantelis Antoniou 		/* and put the reference of the find */
618801d728cSPantelis Antoniou 		of_dev_put(pdev);
619801d728cSPantelis Antoniou 		break;
620801d728cSPantelis Antoniou 	}
621801d728cSPantelis Antoniou 
622801d728cSPantelis Antoniou 	return NOTIFY_OK;
623801d728cSPantelis Antoniou }
624801d728cSPantelis Antoniou 
625801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
626801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
627801d728cSPantelis Antoniou };
628801d728cSPantelis Antoniou 
629801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
630801d728cSPantelis Antoniou {
631801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
632801d728cSPantelis Antoniou }
633801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
634801d728cSPantelis Antoniou 
635964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
636