xref: /linux/drivers/of/platform.c (revision 92039ed129b8140be519a9fde561aeba733aecbe)
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", },
28cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA
29cbb49c26SGrant Likely 	{ .compatible = "arm,amba-bus", },
30cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */
31cbb49c26SGrant Likely 	{} /* Empty terminated list */
32cbb49c26SGrant Likely };
33cbb49c26SGrant Likely 
34c6085584SJonas Bonn static int of_dev_node_match(struct device *dev, void *data)
35c6085584SJonas Bonn {
36c6085584SJonas Bonn 	return dev->of_node == data;
37c6085584SJonas Bonn }
38c6085584SJonas Bonn 
39c6085584SJonas Bonn /**
40c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
41c6085584SJonas Bonn  * @np: Pointer to device tree node
42c6085584SJonas Bonn  *
43c6085584SJonas Bonn  * Returns platform_device pointer, or NULL if not found
44c6085584SJonas Bonn  */
45c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
46c6085584SJonas Bonn {
47c6085584SJonas Bonn 	struct device *dev;
48c6085584SJonas Bonn 
49c6085584SJonas Bonn 	dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
50c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
51c6085584SJonas Bonn }
52c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
53c6085584SJonas Bonn 
54596c955cSGrant Likely #if defined(CONFIG_PPC_DCR)
55596c955cSGrant Likely #include <asm/dcr.h>
56596c955cSGrant Likely #endif
57596c955cSGrant Likely 
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  *
7194c09319SGrant Likely  * This routine will first try using either the dcr-reg or the reg property
7294c09319SGrant Likely  * value to derive a unique name.  As a last resort it will use the node
7394c09319SGrant Likely  * name followed by a unique number.
7494c09319SGrant Likely  */
75c6601225SGrant Likely void of_device_make_bus_id(struct device *dev)
7694c09319SGrant Likely {
7794c09319SGrant Likely 	static atomic_t bus_no_reg_magic;
7894c09319SGrant Likely 	struct device_node *node = dev->of_node;
7924fb530fSKim Phillips 	const __be32 *reg;
8094c09319SGrant Likely 	u64 addr;
815d61b165SStephen Warren 	const __be32 *addrp;
8294c09319SGrant Likely 	int magic;
8394c09319SGrant Likely 
8494c09319SGrant Likely #ifdef CONFIG_PPC_DCR
8594c09319SGrant Likely 	/*
8694c09319SGrant Likely 	 * If it's a DCR based device, use 'd' for native DCRs
8794c09319SGrant Likely 	 * and 'D' for MMIO DCRs.
8894c09319SGrant Likely 	 */
8994c09319SGrant Likely 	reg = of_get_property(node, "dcr-reg", NULL);
9094c09319SGrant Likely 	if (reg) {
9194c09319SGrant Likely #ifdef CONFIG_PPC_DCR_NATIVE
9294c09319SGrant Likely 		dev_set_name(dev, "d%x.%s", *reg, node->name);
9394c09319SGrant Likely #else /* CONFIG_PPC_DCR_NATIVE */
9494c09319SGrant Likely 		u64 addr = of_translate_dcr_address(node, *reg, NULL);
9594c09319SGrant Likely 		if (addr != OF_BAD_ADDR) {
9694c09319SGrant Likely 			dev_set_name(dev, "D%llx.%s",
9794c09319SGrant Likely 				     (unsigned long long)addr, node->name);
9894c09319SGrant Likely 			return;
9994c09319SGrant Likely 		}
10094c09319SGrant Likely #endif /* !CONFIG_PPC_DCR_NATIVE */
10194c09319SGrant Likely 	}
10294c09319SGrant Likely #endif /* CONFIG_PPC_DCR */
10394c09319SGrant Likely 
10494c09319SGrant Likely 	/*
10594c09319SGrant Likely 	 * For MMIO, get the physical address
10694c09319SGrant Likely 	 */
10794c09319SGrant Likely 	reg = of_get_property(node, "reg", NULL);
10894c09319SGrant Likely 	if (reg) {
1095d61b165SStephen Warren 		if (of_can_translate_address(node)) {
11094c09319SGrant Likely 			addr = of_translate_address(node, reg);
1115d61b165SStephen Warren 		} else {
1125d61b165SStephen Warren 			addrp = of_get_address(node, 0, NULL, NULL);
1135d61b165SStephen Warren 			if (addrp)
1145d61b165SStephen Warren 				addr = of_read_number(addrp, 1);
1155d61b165SStephen Warren 			else
1165d61b165SStephen Warren 				addr = OF_BAD_ADDR;
1175d61b165SStephen Warren 		}
11894c09319SGrant Likely 		if (addr != OF_BAD_ADDR) {
11994c09319SGrant Likely 			dev_set_name(dev, "%llx.%s",
12094c09319SGrant Likely 				     (unsigned long long)addr, node->name);
12194c09319SGrant Likely 			return;
12294c09319SGrant Likely 		}
12394c09319SGrant Likely 	}
12494c09319SGrant Likely 
12594c09319SGrant Likely 	/*
12694c09319SGrant Likely 	 * No BusID, use the node name and add a globally incremented
12794c09319SGrant Likely 	 * counter (and pray...)
12894c09319SGrant Likely 	 */
12994c09319SGrant Likely 	magic = atomic_add_return(1, &bus_no_reg_magic);
13094c09319SGrant Likely 	dev_set_name(dev, "%s.%d", node->name, magic - 1);
13194c09319SGrant Likely }
13294c09319SGrant Likely 
13394c09319SGrant Likely /**
13494c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
13594c09319SGrant Likely  * @np: device node to assign to device
13694c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
13794c09319SGrant Likely  * @parent: Parent device.
13894c09319SGrant Likely  */
13994a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
14094c09319SGrant Likely 				  const char *bus_id,
14194c09319SGrant Likely 				  struct device *parent)
14294c09319SGrant Likely {
14394a0cb1fSGrant Likely 	struct platform_device *dev;
14452f6537cSAndres Salomon 	int rc, i, num_reg = 0, num_irq;
145ac80a51eSGrant Likely 	struct resource *res, temp_res;
14694c09319SGrant Likely 
1477096d042SGrant Likely 	dev = platform_device_alloc("", -1);
1487096d042SGrant Likely 	if (!dev)
1497096d042SGrant Likely 		return NULL;
1507096d042SGrant Likely 
1517096d042SGrant Likely 	/* count the io and irq resources */
1525d61b165SStephen Warren 	if (of_can_translate_address(np))
153ac80a51eSGrant Likely 		while (of_address_to_resource(np, num_reg, &temp_res) == 0)
154ac80a51eSGrant Likely 			num_reg++;
15552f6537cSAndres Salomon 	num_irq = of_irq_count(np);
156ac80a51eSGrant Likely 
157ac80a51eSGrant Likely 	/* Populate the resource table */
158ac80a51eSGrant Likely 	if (num_irq || num_reg) {
1597096d042SGrant Likely 		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
1607096d042SGrant Likely 		if (!res) {
1617096d042SGrant Likely 			platform_device_put(dev);
1627096d042SGrant Likely 			return NULL;
1637096d042SGrant Likely 		}
1647096d042SGrant Likely 
165ac80a51eSGrant Likely 		dev->num_resources = num_reg + num_irq;
166ac80a51eSGrant Likely 		dev->resource = res;
167ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
168ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
169ac80a51eSGrant Likely 			WARN_ON(rc);
170ac80a51eSGrant Likely 		}
17152f6537cSAndres Salomon 		WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
172ac80a51eSGrant Likely 	}
17394c09319SGrant Likely 
17494c09319SGrant Likely 	dev->dev.of_node = of_node_get(np);
175314b02f5SKumar Gala #if defined(CONFIG_MICROBLAZE)
17694c09319SGrant Likely 	dev->dev.dma_mask = &dev->archdata.dma_mask;
1779e3288dcSGrant Likely #endif
17894c09319SGrant Likely 	dev->dev.parent = parent;
17994c09319SGrant Likely 
18094c09319SGrant Likely 	if (bus_id)
18194c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
18294c09319SGrant Likely 	else
18394c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
18494c09319SGrant Likely 
18594c09319SGrant Likely 	return dev;
18694c09319SGrant Likely }
18794c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
18894c09319SGrant Likely 
18994c09319SGrant Likely /**
19015c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
1915fd200f3SGrant Likely  * @np: pointer to node to create device for
1925fd200f3SGrant Likely  * @bus_id: name to assign device
19315c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
1945fd200f3SGrant Likely  * @parent: Linux device model parent device.
195cd1e6504SGrant Likely  *
196cd1e6504SGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
197cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1985fd200f3SGrant Likely  */
19915c3597dSGrant Likely struct platform_device *of_platform_device_create_pdata(
20015c3597dSGrant Likely 					struct device_node *np,
2015fd200f3SGrant Likely 					const char *bus_id,
20215c3597dSGrant Likely 					void *platform_data,
2035fd200f3SGrant Likely 					struct device *parent)
2045fd200f3SGrant Likely {
20594a0cb1fSGrant Likely 	struct platform_device *dev;
2065fd200f3SGrant Likely 
207cd1e6504SGrant Likely 	if (!of_device_is_available(np))
208cd1e6504SGrant Likely 		return NULL;
209cd1e6504SGrant Likely 
2105fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
2115fd200f3SGrant Likely 	if (!dev)
2125fd200f3SGrant Likely 		return NULL;
2135fd200f3SGrant Likely 
214314b02f5SKumar Gala #if defined(CONFIG_MICROBLAZE)
2155fd200f3SGrant Likely 	dev->archdata.dma_mask = 0xffffffffUL;
2169e3288dcSGrant Likely #endif
2175fd200f3SGrant Likely 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
218eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
21915c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
2205fd200f3SGrant Likely 
2215fd200f3SGrant Likely 	/* We do not fill the DMA ops for platform devices by default.
2225fd200f3SGrant Likely 	 * This is currently the responsibility of the platform code
2235fd200f3SGrant Likely 	 * to do such, possibly using a device notifier
2245fd200f3SGrant Likely 	 */
2255fd200f3SGrant Likely 
2267096d042SGrant Likely 	if (of_device_add(dev) != 0) {
2277096d042SGrant Likely 		platform_device_put(dev);
2285fd200f3SGrant Likely 		return NULL;
2295fd200f3SGrant Likely 	}
2305fd200f3SGrant Likely 
2315fd200f3SGrant Likely 	return dev;
2325fd200f3SGrant Likely }
23315c3597dSGrant Likely 
23415c3597dSGrant Likely /**
23515c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
23615c3597dSGrant Likely  * @np: pointer to node to create device for
23715c3597dSGrant Likely  * @bus_id: name to assign device
23815c3597dSGrant Likely  * @parent: Linux device model parent device.
23915c3597dSGrant Likely  *
24015c3597dSGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
24115c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
24215c3597dSGrant Likely  */
24315c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
24415c3597dSGrant Likely 					    const char *bus_id,
24515c3597dSGrant Likely 					    struct device *parent)
24615c3597dSGrant Likely {
24715c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
24815c3597dSGrant Likely }
2495fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2505fd200f3SGrant Likely 
2515de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2525de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2535de1540bSGrant Likely 						 const char *bus_id,
2545de1540bSGrant Likely 						 void *platform_data,
2555de1540bSGrant Likely 						 struct device *parent)
2565de1540bSGrant Likely {
2575de1540bSGrant Likely 	struct amba_device *dev;
2585de1540bSGrant Likely 	const void *prop;
2595de1540bSGrant Likely 	int i, ret;
2605de1540bSGrant Likely 
2615de1540bSGrant Likely 	pr_debug("Creating amba device %s\n", node->full_name);
2625de1540bSGrant Likely 
2635de1540bSGrant Likely 	if (!of_device_is_available(node))
2645de1540bSGrant Likely 		return NULL;
2655de1540bSGrant Likely 
266c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
2675de1540bSGrant Likely 	if (!dev)
2685de1540bSGrant Likely 		return NULL;
2695de1540bSGrant Likely 
2705de1540bSGrant Likely 	/* setup generic device info */
2715de1540bSGrant Likely 	dev->dev.coherent_dma_mask = ~0;
2725de1540bSGrant Likely 	dev->dev.of_node = of_node_get(node);
2735de1540bSGrant Likely 	dev->dev.parent = parent;
2745de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2755de1540bSGrant Likely 	if (bus_id)
2765de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
2775de1540bSGrant Likely 	else
2785de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
2795de1540bSGrant Likely 
2805de1540bSGrant Likely 	/* setup amba-specific device info */
2815de1540bSGrant Likely 	dev->dma_mask = ~0;
2825de1540bSGrant Likely 
2835de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
2845de1540bSGrant Likely 	prop = of_get_property(node, "arm,primecell-periphid", NULL);
2855de1540bSGrant Likely 	if (prop)
2865de1540bSGrant Likely 		dev->periphid = of_read_ulong(prop, 1);
2875de1540bSGrant Likely 
2885de1540bSGrant Likely 	/* Decode the IRQs and address ranges */
2895de1540bSGrant Likely 	for (i = 0; i < AMBA_NR_IRQS; i++)
2905de1540bSGrant Likely 		dev->irq[i] = irq_of_parse_and_map(node, i);
2915de1540bSGrant Likely 
2925de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
2935de1540bSGrant Likely 	if (ret)
2945de1540bSGrant Likely 		goto err_free;
2955de1540bSGrant Likely 
296c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
2975de1540bSGrant Likely 	if (ret)
2985de1540bSGrant Likely 		goto err_free;
2995de1540bSGrant Likely 
3005de1540bSGrant Likely 	return dev;
3015de1540bSGrant Likely 
3025de1540bSGrant Likely err_free:
303c0f72f8aSRussell King 	amba_device_put(dev);
3045de1540bSGrant Likely 	return NULL;
3055de1540bSGrant Likely }
3065de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
3075de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
3085de1540bSGrant Likely 						 const char *bus_id,
3095de1540bSGrant Likely 						 void *platform_data,
3105de1540bSGrant Likely 						 struct device *parent)
3115de1540bSGrant Likely {
3125de1540bSGrant Likely 	return NULL;
3135de1540bSGrant Likely }
3145de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
3155de1540bSGrant Likely 
3165fd200f3SGrant Likely /**
31715c3597dSGrant Likely  * of_devname_lookup() - Given a device node, lookup the preferred Linux name
31815c3597dSGrant Likely  */
31915c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
32015c3597dSGrant Likely 				 struct device_node *np)
32115c3597dSGrant Likely {
32215c3597dSGrant Likely 	struct resource res;
323303f59d1SOlof Johansson 
324303f59d1SOlof Johansson 	if (!lookup)
325303f59d1SOlof Johansson 		return NULL;
326303f59d1SOlof Johansson 
327f88e1ae8SGrant Likely 	for(; lookup->compatible != NULL; lookup++) {
32815c3597dSGrant Likely 		if (!of_device_is_compatible(np, lookup->compatible))
32915c3597dSGrant Likely 			continue;
33084774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
33115c3597dSGrant Likely 			if (res.start != lookup->phys_addr)
33215c3597dSGrant Likely 				continue;
33315c3597dSGrant Likely 		pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
33415c3597dSGrant Likely 		return lookup;
33515c3597dSGrant Likely 	}
336303f59d1SOlof Johansson 
33715c3597dSGrant Likely 	return NULL;
33815c3597dSGrant Likely }
33915c3597dSGrant Likely 
34015c3597dSGrant Likely /**
34138e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3425fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3431eed4c07SGrant Likely  * @matches: match table for bus nodes
344303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
34538e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
346303f59d1SOlof Johansson  * @strict: require compatible property
34738e9e21dSGrant Likely  *
34838e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
34938e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3505fd200f3SGrant Likely  */
35138e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3525fd200f3SGrant Likely 				  const struct of_device_id *matches,
35315c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
35429d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3555fd200f3SGrant Likely {
35615c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3575fd200f3SGrant Likely 	struct device_node *child;
35894a0cb1fSGrant Likely 	struct platform_device *dev;
35915c3597dSGrant Likely 	const char *bus_id = NULL;
36015c3597dSGrant Likely 	void *platform_data = NULL;
3615fd200f3SGrant Likely 	int rc = 0;
3625fd200f3SGrant Likely 
36329d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
36429d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
36529d4f8a4SGrant Likely 		pr_debug("%s() - skipping %s, no compatible prop\n",
36629d4f8a4SGrant Likely 			 __func__, bus->full_name);
36729d4f8a4SGrant Likely 		return 0;
36829d4f8a4SGrant Likely 	}
36929d4f8a4SGrant Likely 
37015c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
37115c3597dSGrant Likely 	if (auxdata) {
37215c3597dSGrant Likely 		bus_id = auxdata->name;
37315c3597dSGrant Likely 		platform_data = auxdata->platform_data;
37415c3597dSGrant Likely 	}
37515c3597dSGrant Likely 
3765de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
37715c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
3785de1540bSGrant Likely 		return 0;
3795de1540bSGrant Likely 	}
3805de1540bSGrant Likely 
38115c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
38238e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
38338e9e21dSGrant Likely 		return 0;
38438e9e21dSGrant Likely 
3855fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
3865fd200f3SGrant Likely 		pr_debug("   create child: %s\n", child->full_name);
38715c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
3885fd200f3SGrant Likely 		if (rc) {
3895fd200f3SGrant Likely 			of_node_put(child);
3905fd200f3SGrant Likely 			break;
3915fd200f3SGrant Likely 		}
3925fd200f3SGrant Likely 	}
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 
4165fd200f3SGrant Likely 	pr_debug("of_platform_bus_probe()\n");
4175fd200f3SGrant Likely 	pr_debug(" starting at: %s\n", root->full_name);
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);
42638e9e21dSGrant Likely 		if (rc)
4275fd200f3SGrant Likely 			break;
4285fd200f3SGrant Likely 	}
42938e9e21dSGrant Likely 
4305fd200f3SGrant Likely 	of_node_put(root);
4315fd200f3SGrant Likely 	return rc;
4325fd200f3SGrant Likely }
4335fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
43429d4f8a4SGrant Likely 
43529d4f8a4SGrant Likely /**
43629d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
43729d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
43829d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
439*92039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
44029d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
44129d4f8a4SGrant Likely  *
44229d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
44329d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
44429d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
44529d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
44629d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
44729d4f8a4SGrant Likely  * are selected by the @matches argument).
44829d4f8a4SGrant Likely  *
44929d4f8a4SGrant Likely  * New board support should be using this function instead of
45029d4f8a4SGrant Likely  * of_platform_bus_probe().
45129d4f8a4SGrant Likely  *
45229d4f8a4SGrant Likely  * Returns 0 on success, < 0 on failure.
45329d4f8a4SGrant Likely  */
45429d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
45529d4f8a4SGrant Likely 			const struct of_device_id *matches,
45615c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
45729d4f8a4SGrant Likely 			struct device *parent)
45829d4f8a4SGrant Likely {
45929d4f8a4SGrant Likely 	struct device_node *child;
46029d4f8a4SGrant Likely 	int rc = 0;
46129d4f8a4SGrant Likely 
46229d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
46329d4f8a4SGrant Likely 	if (!root)
46429d4f8a4SGrant Likely 		return -EINVAL;
46529d4f8a4SGrant Likely 
46629d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
46715c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
46829d4f8a4SGrant Likely 		if (rc)
46929d4f8a4SGrant Likely 			break;
47029d4f8a4SGrant Likely 	}
47129d4f8a4SGrant Likely 
47229d4f8a4SGrant Likely 	of_node_put(root);
47329d4f8a4SGrant Likely 	return rc;
47429d4f8a4SGrant Likely }
475e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
476964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
477