xref: /linux/drivers/of/platform.c (revision c706c239af5bc297b5fbf1adc715632e1c222f7a)
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  */
143f23de10SStephen Rothwell #include <linux/errno.h>
155c457083SStephen Rothwell #include <linux/module.h>
165de1540bSGrant Likely #include <linux/amba/bus.h>
173f23de10SStephen Rothwell #include <linux/device.h>
185fd200f3SGrant Likely #include <linux/dma-mapping.h>
1950ef5284SGrant Likely #include <linux/slab.h>
209e3288dcSGrant Likely #include <linux/of_address.h>
213f23de10SStephen Rothwell #include <linux/of_device.h>
229e3288dcSGrant Likely #include <linux/of_irq.h>
233f23de10SStephen Rothwell #include <linux/of_platform.h>
24eca39301SGrant Likely #include <linux/platform_device.h>
25eca39301SGrant Likely 
26cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = {
27cbb49c26SGrant Likely 	{ .compatible = "simple-bus", },
2822869a9eSLinus Walleij 	{ .compatible = "simple-mfd", },
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  *
44c6085584SJonas Bonn  * Returns 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 
50c6085584SJonas Bonn 	dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
51c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
52c6085584SJonas Bonn }
53c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
54c6085584SJonas Bonn 
55964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS
565fd200f3SGrant Likely /*
575fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
585fd200f3SGrant Likely  * each applicable node.
595fd200f3SGrant Likely  *
605fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
615fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
625fd200f3SGrant Likely  */
635fd200f3SGrant Likely 
645fd200f3SGrant Likely /**
6594c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
6694c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
6794c09319SGrant Likely  *
6807e461cdSGrant Likely  * This routine will first try using the translated bus address to
6907e461cdSGrant Likely  * derive a unique name. If it cannot, then it will prepend names from
7007e461cdSGrant Likely  * parent nodes until a unique name can be derived.
7194c09319SGrant Likely  */
72c6601225SGrant Likely void of_device_make_bus_id(struct device *dev)
7394c09319SGrant Likely {
7494c09319SGrant Likely 	struct device_node *node = dev->of_node;
7524fb530fSKim Phillips 	const __be32 *reg;
7694c09319SGrant Likely 	u64 addr;
7794c09319SGrant Likely 
7807e461cdSGrant Likely 	/* Construct the name, using parent nodes if necessary to ensure uniqueness */
7907e461cdSGrant Likely 	while (node->parent) {
8094c09319SGrant Likely 		/*
8107e461cdSGrant Likely 		 * If the address can be translated, then that is as much
8207e461cdSGrant Likely 		 * uniqueness as we need. Make it the first component and return
8394c09319SGrant Likely 		 */
8494c09319SGrant Likely 		reg = of_get_property(node, "reg", NULL);
8507e461cdSGrant Likely 		if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
8607e461cdSGrant Likely 			dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
8707e461cdSGrant Likely 				     (unsigned long long)addr, node->name,
8807e461cdSGrant Likely 				     dev_name(dev));
8994c09319SGrant Likely 			return;
9094c09319SGrant Likely 		}
9194c09319SGrant Likely 
9207e461cdSGrant Likely 		/* format arguments only used if dev_name() resolves to NULL */
9307e461cdSGrant Likely 		dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
9407e461cdSGrant Likely 			     strrchr(node->full_name, '/') + 1, dev_name(dev));
9507e461cdSGrant Likely 		node = node->parent;
9607e461cdSGrant Likely 	}
9794c09319SGrant Likely }
9894c09319SGrant Likely 
9994c09319SGrant Likely /**
10094c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
10194c09319SGrant Likely  * @np: device node to assign to device
10294c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
10394c09319SGrant Likely  * @parent: Parent device.
10494c09319SGrant Likely  */
10594a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
10694c09319SGrant Likely 				  const char *bus_id,
10794c09319SGrant Likely 				  struct device *parent)
10894c09319SGrant Likely {
10994a0cb1fSGrant Likely 	struct platform_device *dev;
11052f6537cSAndres Salomon 	int rc, i, num_reg = 0, num_irq;
111ac80a51eSGrant Likely 	struct resource *res, temp_res;
11294c09319SGrant Likely 
1137096d042SGrant Likely 	dev = platform_device_alloc("", -1);
1147096d042SGrant Likely 	if (!dev)
1157096d042SGrant Likely 		return NULL;
1167096d042SGrant Likely 
1177096d042SGrant Likely 	/* count the io and irq resources */
118ac80a51eSGrant Likely 	while (of_address_to_resource(np, num_reg, &temp_res) == 0)
119ac80a51eSGrant Likely 		num_reg++;
12052f6537cSAndres Salomon 	num_irq = of_irq_count(np);
121ac80a51eSGrant Likely 
122ac80a51eSGrant Likely 	/* Populate the resource table */
123ac80a51eSGrant Likely 	if (num_irq || num_reg) {
1247096d042SGrant Likely 		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
1257096d042SGrant Likely 		if (!res) {
1267096d042SGrant Likely 			platform_device_put(dev);
1277096d042SGrant Likely 			return NULL;
1287096d042SGrant Likely 		}
1297096d042SGrant Likely 
130ac80a51eSGrant Likely 		dev->num_resources = num_reg + num_irq;
131ac80a51eSGrant Likely 		dev->resource = res;
132ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
133ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
134ac80a51eSGrant Likely 			WARN_ON(rc);
135ac80a51eSGrant Likely 		}
1369ec36cafSRob Herring 		if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
1379ec36cafSRob Herring 			pr_debug("not all legacy IRQ resources mapped for %s\n",
1389ec36cafSRob Herring 				 np->name);
139ac80a51eSGrant Likely 	}
14094c09319SGrant Likely 
14194c09319SGrant Likely 	dev->dev.of_node = of_node_get(np);
14243c0767eSGrant Likely 	dev->dev.parent = parent ? : &platform_bus;
14394c09319SGrant Likely 
14494c09319SGrant Likely 	if (bus_id)
14594c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
14694c09319SGrant Likely 	else
14794c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
14894c09319SGrant Likely 
14994c09319SGrant Likely 	return dev;
15094c09319SGrant Likely }
15194c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
15294c09319SGrant Likely 
15397890ba9SWill Deacon static void of_dma_deconfigure(struct device *dev)
15497890ba9SWill Deacon {
15597890ba9SWill Deacon 	arch_teardown_dma_ops(dev);
156591c1ee4SSantosh Shilimkar }
157591c1ee4SSantosh Shilimkar 
158591c1ee4SSantosh Shilimkar /**
15915c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1605fd200f3SGrant Likely  * @np: pointer to node to create device for
1615fd200f3SGrant Likely  * @bus_id: name to assign device
16215c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1635fd200f3SGrant Likely  * @parent: Linux device model parent device.
164cd1e6504SGrant Likely  *
165cd1e6504SGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
166cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1675fd200f3SGrant Likely  */
168245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
16915c3597dSGrant Likely 					struct device_node *np,
1705fd200f3SGrant Likely 					const char *bus_id,
17115c3597dSGrant Likely 					void *platform_data,
1725fd200f3SGrant Likely 					struct device *parent)
1735fd200f3SGrant Likely {
17494a0cb1fSGrant Likely 	struct platform_device *dev;
1755fd200f3SGrant Likely 
176c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
177c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
178cd1e6504SGrant Likely 		return NULL;
179cd1e6504SGrant Likely 
1805fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
1815fd200f3SGrant Likely 	if (!dev)
182c6e126deSPawel Moll 		goto err_clear_flag;
1835fd200f3SGrant Likely 
184eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
18515c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
1861f5c69aaSMurali Karicheri 	of_dma_configure(&dev->dev, dev->dev.of_node);
187*c706c239SMarc Zyngier 	of_msi_configure(&dev->dev, dev->dev.of_node);
1885fd200f3SGrant Likely 
1897096d042SGrant Likely 	if (of_device_add(dev) != 0) {
19097890ba9SWill Deacon 		of_dma_deconfigure(&dev->dev);
1917096d042SGrant Likely 		platform_device_put(dev);
192c6e126deSPawel Moll 		goto err_clear_flag;
1935fd200f3SGrant Likely 	}
1945fd200f3SGrant Likely 
1955fd200f3SGrant Likely 	return dev;
196c6e126deSPawel Moll 
197c6e126deSPawel Moll err_clear_flag:
198c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
199c6e126deSPawel Moll 	return NULL;
2005fd200f3SGrant Likely }
20115c3597dSGrant Likely 
20215c3597dSGrant Likely /**
20315c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
20415c3597dSGrant Likely  * @np: pointer to node to create device for
20515c3597dSGrant Likely  * @bus_id: name to assign device
20615c3597dSGrant Likely  * @parent: Linux device model parent device.
20715c3597dSGrant Likely  *
20815c3597dSGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
20915c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
21015c3597dSGrant Likely  */
21115c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
21215c3597dSGrant Likely 					    const char *bus_id,
21315c3597dSGrant Likely 					    struct device *parent)
21415c3597dSGrant Likely {
21515c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
21615c3597dSGrant Likely }
2175fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2185fd200f3SGrant Likely 
2195de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2205de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2215de1540bSGrant Likely 						 const char *bus_id,
2225de1540bSGrant Likely 						 void *platform_data,
2235de1540bSGrant Likely 						 struct device *parent)
2245de1540bSGrant Likely {
2255de1540bSGrant Likely 	struct amba_device *dev;
2265de1540bSGrant Likely 	const void *prop;
2275de1540bSGrant Likely 	int i, ret;
2285de1540bSGrant Likely 
2295de1540bSGrant Likely 	pr_debug("Creating amba device %s\n", node->full_name);
2305de1540bSGrant Likely 
231c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
232c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2335de1540bSGrant Likely 		return NULL;
2345de1540bSGrant Likely 
235c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
2362bc552dfSBartlomiej Zolnierkiewicz 	if (!dev) {
2372bc552dfSBartlomiej Zolnierkiewicz 		pr_err("%s(): amba_device_alloc() failed for %s\n",
2382bc552dfSBartlomiej Zolnierkiewicz 		       __func__, node->full_name);
239c6e126deSPawel Moll 		goto err_clear_flag;
2402bc552dfSBartlomiej Zolnierkiewicz 	}
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) {
2632bc552dfSBartlomiej Zolnierkiewicz 		pr_err("%s(): of_address_to_resource() failed (%d) for %s\n",
2642bc552dfSBartlomiej Zolnierkiewicz 		       __func__, 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) {
2702bc552dfSBartlomiej Zolnierkiewicz 		pr_err("%s(): amba_device_add() failed (%d) for %s\n",
2712bc552dfSBartlomiej Zolnierkiewicz 		       __func__, 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 {
29915c3597dSGrant Likely 	struct resource res;
300303f59d1SOlof Johansson 
301303f59d1SOlof Johansson 	if (!lookup)
302303f59d1SOlof Johansson 		return NULL;
303303f59d1SOlof Johansson 
304f88e1ae8SGrant Likely 	for(; lookup->compatible != NULL; lookup++) {
30515c3597dSGrant Likely 		if (!of_device_is_compatible(np, lookup->compatible))
30615c3597dSGrant Likely 			continue;
30784774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
30815c3597dSGrant Likely 			if (res.start != lookup->phys_addr)
30915c3597dSGrant Likely 				continue;
31015c3597dSGrant Likely 		pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
31115c3597dSGrant Likely 		return lookup;
31215c3597dSGrant Likely 	}
313303f59d1SOlof Johansson 
31415c3597dSGrant Likely 	return NULL;
31515c3597dSGrant Likely }
31615c3597dSGrant Likely 
31715c3597dSGrant Likely /**
31838e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3195fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3201eed4c07SGrant Likely  * @matches: match table for bus nodes
321303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
32238e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
323303f59d1SOlof Johansson  * @strict: require compatible property
32438e9e21dSGrant Likely  *
32538e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
32638e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3275fd200f3SGrant Likely  */
32838e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3295fd200f3SGrant Likely 				  const struct of_device_id *matches,
33015c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
33129d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3325fd200f3SGrant Likely {
33315c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3345fd200f3SGrant Likely 	struct device_node *child;
33594a0cb1fSGrant Likely 	struct platform_device *dev;
33615c3597dSGrant Likely 	const char *bus_id = NULL;
33715c3597dSGrant Likely 	void *platform_data = NULL;
3385fd200f3SGrant Likely 	int rc = 0;
3395fd200f3SGrant Likely 
34029d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
34129d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
34229d4f8a4SGrant Likely 		pr_debug("%s() - skipping %s, no compatible prop\n",
34329d4f8a4SGrant Likely 			 __func__, bus->full_name);
34429d4f8a4SGrant Likely 		return 0;
34529d4f8a4SGrant Likely 	}
34629d4f8a4SGrant Likely 
34715c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
34815c3597dSGrant Likely 	if (auxdata) {
34915c3597dSGrant Likely 		bus_id = auxdata->name;
35015c3597dSGrant Likely 		platform_data = auxdata->platform_data;
35115c3597dSGrant Likely 	}
35215c3597dSGrant Likely 
3535de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
3542bc552dfSBartlomiej Zolnierkiewicz 		/*
3552bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
3562bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
3572bc552dfSBartlomiej Zolnierkiewicz 		 */
35815c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
3595de1540bSGrant Likely 		return 0;
3605de1540bSGrant Likely 	}
3615de1540bSGrant Likely 
36215c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
36338e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
36438e9e21dSGrant Likely 		return 0;
36538e9e21dSGrant Likely 
3665fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
3675fd200f3SGrant Likely 		pr_debug("   create child: %s\n", child->full_name);
36815c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
3695fd200f3SGrant Likely 		if (rc) {
3705fd200f3SGrant Likely 			of_node_put(child);
3715fd200f3SGrant Likely 			break;
3725fd200f3SGrant Likely 		}
3735fd200f3SGrant Likely 	}
37475f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
3755fd200f3SGrant Likely 	return rc;
3765fd200f3SGrant Likely }
3775fd200f3SGrant Likely 
3785fd200f3SGrant Likely /**
37938e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
3805fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
3811eed4c07SGrant Likely  * @matches: match table for bus nodes
3825fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
3835fd200f3SGrant Likely  *
3845fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
3855fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
3865fd200f3SGrant Likely  */
3875fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
3885fd200f3SGrant Likely 			  const struct of_device_id *matches,
3895fd200f3SGrant Likely 			  struct device *parent)
3905fd200f3SGrant Likely {
3915fd200f3SGrant Likely 	struct device_node *child;
3925fd200f3SGrant Likely 	int rc = 0;
3935fd200f3SGrant Likely 
3941eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
3951eed4c07SGrant Likely 	if (!root)
39660d59913SGrant Likely 		return -EINVAL;
3975fd200f3SGrant Likely 
3985fd200f3SGrant Likely 	pr_debug("of_platform_bus_probe()\n");
3995fd200f3SGrant Likely 	pr_debug(" starting at: %s\n", root->full_name);
4005fd200f3SGrant Likely 
4011eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4025fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
40315c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
40438e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4055fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4065fd200f3SGrant Likely 			continue;
40715c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
40838e9e21dSGrant Likely 		if (rc)
4095fd200f3SGrant Likely 			break;
4105fd200f3SGrant Likely 	}
41138e9e21dSGrant Likely 
4125fd200f3SGrant Likely 	of_node_put(root);
4135fd200f3SGrant Likely 	return rc;
4145fd200f3SGrant Likely }
4155fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
41629d4f8a4SGrant Likely 
41729d4f8a4SGrant Likely /**
41829d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
41929d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
42029d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
42192039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
42229d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
42329d4f8a4SGrant Likely  *
42429d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
42529d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
42629d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
42729d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
42829d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
42929d4f8a4SGrant Likely  * are selected by the @matches argument).
43029d4f8a4SGrant Likely  *
43129d4f8a4SGrant Likely  * New board support should be using this function instead of
43229d4f8a4SGrant Likely  * of_platform_bus_probe().
43329d4f8a4SGrant Likely  *
43429d4f8a4SGrant Likely  * Returns 0 on success, < 0 on failure.
43529d4f8a4SGrant Likely  */
43629d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
43729d4f8a4SGrant Likely 			const struct of_device_id *matches,
43815c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
43929d4f8a4SGrant Likely 			struct device *parent)
44029d4f8a4SGrant Likely {
44129d4f8a4SGrant Likely 	struct device_node *child;
44229d4f8a4SGrant Likely 	int rc = 0;
44329d4f8a4SGrant Likely 
44429d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
44529d4f8a4SGrant Likely 	if (!root)
44629d4f8a4SGrant Likely 		return -EINVAL;
44729d4f8a4SGrant Likely 
44829d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
44915c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
45029d4f8a4SGrant Likely 		if (rc)
45129d4f8a4SGrant Likely 			break;
45229d4f8a4SGrant Likely 	}
4532d0747c4SGrant Likely 	of_node_set_flag(root, OF_POPULATED_BUS);
45429d4f8a4SGrant Likely 
45529d4f8a4SGrant Likely 	of_node_put(root);
45629d4f8a4SGrant Likely 	return rc;
45729d4f8a4SGrant Likely }
458e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
459c6e126deSPawel Moll 
460c6e126deSPawel Moll static int of_platform_device_destroy(struct device *dev, void *data)
461c6e126deSPawel Moll {
462c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
46375f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
464c6e126deSPawel Moll 		return 0;
465c6e126deSPawel Moll 
46675f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
46775f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
46875f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
469c6e126deSPawel Moll 
470c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
471c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
472c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
473c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
474c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
475c6e126deSPawel Moll #endif
476c6e126deSPawel Moll 
4770495cb75SWill Deacon 	of_dma_deconfigure(dev);
478c6e126deSPawel Moll 	of_node_clear_flag(dev->of_node, OF_POPULATED);
47975f353b6SGrant Likely 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
480c6e126deSPawel Moll 	return 0;
481c6e126deSPawel Moll }
482c6e126deSPawel Moll 
483c6e126deSPawel Moll /**
484c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
48575f353b6SGrant Likely  * @parent: device which children will be removed
486c6e126deSPawel Moll  *
487c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
488c6e126deSPawel Moll  * of the given device (and, recurrently, their children) that have been
489c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
490c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
491c6e126deSPawel Moll  *
492c6e126deSPawel Moll  * Returns 0 when all children devices have been removed or
493c6e126deSPawel Moll  * -EBUSY when some children remained.
494c6e126deSPawel Moll  */
49575f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
496c6e126deSPawel Moll {
4972d0747c4SGrant Likely 	if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
49875f353b6SGrant Likely 		device_for_each_child(parent, NULL, of_platform_device_destroy);
4992d0747c4SGrant Likely 		of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
5002d0747c4SGrant Likely 	}
501c6e126deSPawel Moll }
502c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
503c6e126deSPawel Moll 
504801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC
505801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb,
506801d728cSPantelis Antoniou 				unsigned long action, void *arg)
507801d728cSPantelis Antoniou {
508801d728cSPantelis Antoniou 	struct of_reconfig_data *rd = arg;
509801d728cSPantelis Antoniou 	struct platform_device *pdev_parent, *pdev;
510801d728cSPantelis Antoniou 	bool children_left;
511801d728cSPantelis Antoniou 
512801d728cSPantelis Antoniou 	switch (of_reconfig_get_state_change(action, rd)) {
513801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_ADD:
514801d728cSPantelis Antoniou 		/* verify that the parent is a bus */
515801d728cSPantelis Antoniou 		if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
516801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* not for us */
517801d728cSPantelis Antoniou 
51815204ab1SPantelis Antoniou 		/* already populated? (driver using of_populate manually) */
51915204ab1SPantelis Antoniou 		if (of_node_check_flag(rd->dn, OF_POPULATED))
52015204ab1SPantelis Antoniou 			return NOTIFY_OK;
52115204ab1SPantelis Antoniou 
522801d728cSPantelis Antoniou 		/* pdev_parent may be NULL when no bus platform device */
523801d728cSPantelis Antoniou 		pdev_parent = of_find_device_by_node(rd->dn->parent);
524801d728cSPantelis Antoniou 		pdev = of_platform_device_create(rd->dn, NULL,
525801d728cSPantelis Antoniou 				pdev_parent ? &pdev_parent->dev : NULL);
526801d728cSPantelis Antoniou 		of_dev_put(pdev_parent);
527801d728cSPantelis Antoniou 
528801d728cSPantelis Antoniou 		if (pdev == NULL) {
529801d728cSPantelis Antoniou 			pr_err("%s: failed to create for '%s'\n",
530801d728cSPantelis Antoniou 					__func__, rd->dn->full_name);
531801d728cSPantelis Antoniou 			/* of_platform_device_create tosses the error code */
532801d728cSPantelis Antoniou 			return notifier_from_errno(-EINVAL);
533801d728cSPantelis Antoniou 		}
534801d728cSPantelis Antoniou 		break;
535801d728cSPantelis Antoniou 
536801d728cSPantelis Antoniou 	case OF_RECONFIG_CHANGE_REMOVE:
53715204ab1SPantelis Antoniou 
53815204ab1SPantelis Antoniou 		/* already depopulated? */
53915204ab1SPantelis Antoniou 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
54015204ab1SPantelis Antoniou 			return NOTIFY_OK;
54115204ab1SPantelis Antoniou 
542801d728cSPantelis Antoniou 		/* find our device by node */
543801d728cSPantelis Antoniou 		pdev = of_find_device_by_node(rd->dn);
544801d728cSPantelis Antoniou 		if (pdev == NULL)
545801d728cSPantelis Antoniou 			return NOTIFY_OK;	/* no? not meant for us */
546801d728cSPantelis Antoniou 
547801d728cSPantelis Antoniou 		/* unregister takes one ref away */
548801d728cSPantelis Antoniou 		of_platform_device_destroy(&pdev->dev, &children_left);
549801d728cSPantelis Antoniou 
550801d728cSPantelis Antoniou 		/* and put the reference of the find */
551801d728cSPantelis Antoniou 		of_dev_put(pdev);
552801d728cSPantelis Antoniou 		break;
553801d728cSPantelis Antoniou 	}
554801d728cSPantelis Antoniou 
555801d728cSPantelis Antoniou 	return NOTIFY_OK;
556801d728cSPantelis Antoniou }
557801d728cSPantelis Antoniou 
558801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = {
559801d728cSPantelis Antoniou 	.notifier_call = of_platform_notify,
560801d728cSPantelis Antoniou };
561801d728cSPantelis Antoniou 
562801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void)
563801d728cSPantelis Antoniou {
564801d728cSPantelis Antoniou 	WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
565801d728cSPantelis Antoniou }
566801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */
567801d728cSPantelis Antoniou 
568964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
569