xref: /linux/drivers/of/platform.c (revision 75f353b61342b5847c7f6d8499fd6301dce09845)
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 
54964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS
555fd200f3SGrant Likely /*
565fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
575fd200f3SGrant Likely  * each applicable node.
585fd200f3SGrant Likely  *
595fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
605fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
615fd200f3SGrant Likely  */
625fd200f3SGrant Likely 
635fd200f3SGrant Likely /**
6494c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
6594c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
6694c09319SGrant Likely  *
6707e461cdSGrant Likely  * This routine will first try using the translated bus address to
6807e461cdSGrant Likely  * derive a unique name. If it cannot, then it will prepend names from
6907e461cdSGrant Likely  * parent nodes until a unique name can be derived.
7094c09319SGrant Likely  */
71c6601225SGrant Likely void of_device_make_bus_id(struct device *dev)
7294c09319SGrant Likely {
7394c09319SGrant Likely 	struct device_node *node = dev->of_node;
7424fb530fSKim Phillips 	const __be32 *reg;
7594c09319SGrant Likely 	u64 addr;
7694c09319SGrant Likely 
7707e461cdSGrant Likely 	/* Construct the name, using parent nodes if necessary to ensure uniqueness */
7807e461cdSGrant Likely 	while (node->parent) {
7994c09319SGrant Likely 		/*
8007e461cdSGrant Likely 		 * If the address can be translated, then that is as much
8107e461cdSGrant Likely 		 * uniqueness as we need. Make it the first component and return
8294c09319SGrant Likely 		 */
8394c09319SGrant Likely 		reg = of_get_property(node, "reg", NULL);
8407e461cdSGrant Likely 		if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
8507e461cdSGrant Likely 			dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
8607e461cdSGrant Likely 				     (unsigned long long)addr, node->name,
8707e461cdSGrant Likely 				     dev_name(dev));
8894c09319SGrant Likely 			return;
8994c09319SGrant Likely 		}
9094c09319SGrant Likely 
9107e461cdSGrant Likely 		/* format arguments only used if dev_name() resolves to NULL */
9207e461cdSGrant Likely 		dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
9307e461cdSGrant Likely 			     strrchr(node->full_name, '/') + 1, dev_name(dev));
9407e461cdSGrant Likely 		node = node->parent;
9507e461cdSGrant Likely 	}
9694c09319SGrant Likely }
9794c09319SGrant Likely 
9894c09319SGrant Likely /**
9994c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
10094c09319SGrant Likely  * @np: device node to assign to device
10194c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
10294c09319SGrant Likely  * @parent: Parent device.
10394c09319SGrant Likely  */
10494a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
10594c09319SGrant Likely 				  const char *bus_id,
10694c09319SGrant Likely 				  struct device *parent)
10794c09319SGrant Likely {
10894a0cb1fSGrant Likely 	struct platform_device *dev;
10952f6537cSAndres Salomon 	int rc, i, num_reg = 0, num_irq;
110ac80a51eSGrant Likely 	struct resource *res, temp_res;
11194c09319SGrant Likely 
1127096d042SGrant Likely 	dev = platform_device_alloc("", -1);
1137096d042SGrant Likely 	if (!dev)
1147096d042SGrant Likely 		return NULL;
1157096d042SGrant Likely 
1167096d042SGrant Likely 	/* count the io and irq resources */
117ac80a51eSGrant Likely 	while (of_address_to_resource(np, num_reg, &temp_res) == 0)
118ac80a51eSGrant Likely 		num_reg++;
11952f6537cSAndres Salomon 	num_irq = of_irq_count(np);
120ac80a51eSGrant Likely 
121ac80a51eSGrant Likely 	/* Populate the resource table */
122ac80a51eSGrant Likely 	if (num_irq || num_reg) {
1237096d042SGrant Likely 		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
1247096d042SGrant Likely 		if (!res) {
1257096d042SGrant Likely 			platform_device_put(dev);
1267096d042SGrant Likely 			return NULL;
1277096d042SGrant Likely 		}
1287096d042SGrant Likely 
129ac80a51eSGrant Likely 		dev->num_resources = num_reg + num_irq;
130ac80a51eSGrant Likely 		dev->resource = res;
131ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
132ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
133ac80a51eSGrant Likely 			WARN_ON(rc);
134ac80a51eSGrant Likely 		}
1359ec36cafSRob Herring 		if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
1369ec36cafSRob Herring 			pr_debug("not all legacy IRQ resources mapped for %s\n",
1379ec36cafSRob Herring 				 np->name);
138ac80a51eSGrant Likely 	}
13994c09319SGrant Likely 
14094c09319SGrant Likely 	dev->dev.of_node = of_node_get(np);
14194c09319SGrant Likely 	dev->dev.parent = parent;
14294c09319SGrant Likely 
14394c09319SGrant Likely 	if (bus_id)
14494c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
14594c09319SGrant Likely 	else
14694c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
14794c09319SGrant Likely 
14894c09319SGrant Likely 	return dev;
14994c09319SGrant Likely }
15094c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
15194c09319SGrant Likely 
15294c09319SGrant Likely /**
153591c1ee4SSantosh Shilimkar  * of_dma_configure - Setup DMA configuration
154591c1ee4SSantosh Shilimkar  * @dev:	Device to apply DMA configuration
155591c1ee4SSantosh Shilimkar  *
156591c1ee4SSantosh Shilimkar  * Try to get devices's DMA configuration from DT and update it
157591c1ee4SSantosh Shilimkar  * accordingly.
158591c1ee4SSantosh Shilimkar  *
159591c1ee4SSantosh Shilimkar  * In case if platform code need to use own special DMA configuration,it
160591c1ee4SSantosh Shilimkar  * can use Platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE event
161591c1ee4SSantosh Shilimkar  * to fix up DMA configuration.
162591c1ee4SSantosh Shilimkar  */
163591c1ee4SSantosh Shilimkar static void of_dma_configure(struct platform_device *pdev)
164591c1ee4SSantosh Shilimkar {
165591c1ee4SSantosh Shilimkar 	u64 dma_addr, paddr, size;
166591c1ee4SSantosh Shilimkar 	int ret;
167591c1ee4SSantosh Shilimkar 	struct device *dev = &pdev->dev;
168591c1ee4SSantosh Shilimkar 
169591c1ee4SSantosh Shilimkar 	/*
170591c1ee4SSantosh Shilimkar 	 * Set default dma-mask to 32 bit. Drivers are expected to setup
171591c1ee4SSantosh Shilimkar 	 * the correct supported dma_mask.
172591c1ee4SSantosh Shilimkar 	 */
173591c1ee4SSantosh Shilimkar 	dev->coherent_dma_mask = DMA_BIT_MASK(32);
174591c1ee4SSantosh Shilimkar 
175591c1ee4SSantosh Shilimkar 	/*
176591c1ee4SSantosh Shilimkar 	 * Set it to coherent_dma_mask by default if the architecture
177591c1ee4SSantosh Shilimkar 	 * code has not set it.
178591c1ee4SSantosh Shilimkar 	 */
179591c1ee4SSantosh Shilimkar 	if (!dev->dma_mask)
180591c1ee4SSantosh Shilimkar 		dev->dma_mask = &dev->coherent_dma_mask;
181591c1ee4SSantosh Shilimkar 
182591c1ee4SSantosh Shilimkar 	/*
183591c1ee4SSantosh Shilimkar 	 * if dma-coherent property exist, call arch hook to setup
184591c1ee4SSantosh Shilimkar 	 * dma coherent operations.
185591c1ee4SSantosh Shilimkar 	 */
186591c1ee4SSantosh Shilimkar 	if (of_dma_is_coherent(dev->of_node)) {
187591c1ee4SSantosh Shilimkar 		set_arch_dma_coherent_ops(dev);
188591c1ee4SSantosh Shilimkar 		dev_dbg(dev, "device is dma coherent\n");
189591c1ee4SSantosh Shilimkar 	}
190591c1ee4SSantosh Shilimkar 
191591c1ee4SSantosh Shilimkar 	/*
192591c1ee4SSantosh Shilimkar 	 * if dma-ranges property doesn't exist - just return else
193591c1ee4SSantosh Shilimkar 	 * setup the dma offset
194591c1ee4SSantosh Shilimkar 	 */
195591c1ee4SSantosh Shilimkar 	ret = of_dma_get_range(dev->of_node, &dma_addr, &paddr, &size);
196591c1ee4SSantosh Shilimkar 	if (ret < 0) {
197591c1ee4SSantosh Shilimkar 		dev_dbg(dev, "no dma range information to setup\n");
198591c1ee4SSantosh Shilimkar 		return;
199591c1ee4SSantosh Shilimkar 	}
200591c1ee4SSantosh Shilimkar 
201591c1ee4SSantosh Shilimkar 	/* DMA ranges found. Calculate and set dma_pfn_offset */
202591c1ee4SSantosh Shilimkar 	dev->dma_pfn_offset = PFN_DOWN(paddr - dma_addr);
203591c1ee4SSantosh Shilimkar 	dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", dev->dma_pfn_offset);
204591c1ee4SSantosh Shilimkar }
205591c1ee4SSantosh Shilimkar 
206591c1ee4SSantosh Shilimkar /**
20715c3597dSGrant Likely  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
2085fd200f3SGrant Likely  * @np: pointer to node to create device for
2095fd200f3SGrant Likely  * @bus_id: name to assign device
21015c3597dSGrant Likely  * @platform_data: pointer to populate platform_data pointer with
2115fd200f3SGrant Likely  * @parent: Linux device model parent device.
212cd1e6504SGrant Likely  *
213cd1e6504SGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
214cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
2155fd200f3SGrant Likely  */
216245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata(
21715c3597dSGrant Likely 					struct device_node *np,
2185fd200f3SGrant Likely 					const char *bus_id,
21915c3597dSGrant Likely 					void *platform_data,
2205fd200f3SGrant Likely 					struct device *parent)
2215fd200f3SGrant Likely {
22294a0cb1fSGrant Likely 	struct platform_device *dev;
2235fd200f3SGrant Likely 
224c6e126deSPawel Moll 	if (!of_device_is_available(np) ||
225c6e126deSPawel Moll 	    of_node_test_and_set_flag(np, OF_POPULATED))
226cd1e6504SGrant Likely 		return NULL;
227cd1e6504SGrant Likely 
2285fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
2295fd200f3SGrant Likely 	if (!dev)
230c6e126deSPawel Moll 		goto err_clear_flag;
2315fd200f3SGrant Likely 
232591c1ee4SSantosh Shilimkar 	of_dma_configure(dev);
233eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
23415c3597dSGrant Likely 	dev->dev.platform_data = platform_data;
2355fd200f3SGrant Likely 
2365fd200f3SGrant Likely 	/* We do not fill the DMA ops for platform devices by default.
2375fd200f3SGrant Likely 	 * This is currently the responsibility of the platform code
2385fd200f3SGrant Likely 	 * to do such, possibly using a device notifier
2395fd200f3SGrant Likely 	 */
2405fd200f3SGrant Likely 
2417096d042SGrant Likely 	if (of_device_add(dev) != 0) {
2427096d042SGrant Likely 		platform_device_put(dev);
243c6e126deSPawel Moll 		goto err_clear_flag;
2445fd200f3SGrant Likely 	}
2455fd200f3SGrant Likely 
2465fd200f3SGrant Likely 	return dev;
247c6e126deSPawel Moll 
248c6e126deSPawel Moll err_clear_flag:
249c6e126deSPawel Moll 	of_node_clear_flag(np, OF_POPULATED);
250c6e126deSPawel Moll 	return NULL;
2515fd200f3SGrant Likely }
25215c3597dSGrant Likely 
25315c3597dSGrant Likely /**
25415c3597dSGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
25515c3597dSGrant Likely  * @np: pointer to node to create device for
25615c3597dSGrant Likely  * @bus_id: name to assign device
25715c3597dSGrant Likely  * @parent: Linux device model parent device.
25815c3597dSGrant Likely  *
25915c3597dSGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
26015c3597dSGrant Likely  * registered.  Unavailable devices will not get registered.
26115c3597dSGrant Likely  */
26215c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
26315c3597dSGrant Likely 					    const char *bus_id,
26415c3597dSGrant Likely 					    struct device *parent)
26515c3597dSGrant Likely {
26615c3597dSGrant Likely 	return of_platform_device_create_pdata(np, bus_id, NULL, parent);
26715c3597dSGrant Likely }
2685fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2695fd200f3SGrant Likely 
2705de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA
2715de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
2725de1540bSGrant Likely 						 const char *bus_id,
2735de1540bSGrant Likely 						 void *platform_data,
2745de1540bSGrant Likely 						 struct device *parent)
2755de1540bSGrant Likely {
2765de1540bSGrant Likely 	struct amba_device *dev;
2775de1540bSGrant Likely 	const void *prop;
2785de1540bSGrant Likely 	int i, ret;
2795de1540bSGrant Likely 
2805de1540bSGrant Likely 	pr_debug("Creating amba device %s\n", node->full_name);
2815de1540bSGrant Likely 
282c6e126deSPawel Moll 	if (!of_device_is_available(node) ||
283c6e126deSPawel Moll 	    of_node_test_and_set_flag(node, OF_POPULATED))
2845de1540bSGrant Likely 		return NULL;
2855de1540bSGrant Likely 
286c0f72f8aSRussell King 	dev = amba_device_alloc(NULL, 0, 0);
2872bc552dfSBartlomiej Zolnierkiewicz 	if (!dev) {
2882bc552dfSBartlomiej Zolnierkiewicz 		pr_err("%s(): amba_device_alloc() failed for %s\n",
2892bc552dfSBartlomiej Zolnierkiewicz 		       __func__, node->full_name);
290c6e126deSPawel Moll 		goto err_clear_flag;
2912bc552dfSBartlomiej Zolnierkiewicz 	}
2925de1540bSGrant Likely 
2935de1540bSGrant Likely 	/* setup generic device info */
2945de1540bSGrant Likely 	dev->dev.coherent_dma_mask = ~0;
2955de1540bSGrant Likely 	dev->dev.of_node = of_node_get(node);
2965de1540bSGrant Likely 	dev->dev.parent = parent;
2975de1540bSGrant Likely 	dev->dev.platform_data = platform_data;
2985de1540bSGrant Likely 	if (bus_id)
2995de1540bSGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
3005de1540bSGrant Likely 	else
3015de1540bSGrant Likely 		of_device_make_bus_id(&dev->dev);
3025de1540bSGrant Likely 
3035de1540bSGrant Likely 	/* Allow the HW Peripheral ID to be overridden */
3045de1540bSGrant Likely 	prop = of_get_property(node, "arm,primecell-periphid", NULL);
3055de1540bSGrant Likely 	if (prop)
3065de1540bSGrant Likely 		dev->periphid = of_read_ulong(prop, 1);
3075de1540bSGrant Likely 
3085de1540bSGrant Likely 	/* Decode the IRQs and address ranges */
3095de1540bSGrant Likely 	for (i = 0; i < AMBA_NR_IRQS; i++)
3105de1540bSGrant Likely 		dev->irq[i] = irq_of_parse_and_map(node, i);
3115de1540bSGrant Likely 
3125de1540bSGrant Likely 	ret = of_address_to_resource(node, 0, &dev->res);
3132bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
3142bc552dfSBartlomiej Zolnierkiewicz 		pr_err("%s(): of_address_to_resource() failed (%d) for %s\n",
3152bc552dfSBartlomiej Zolnierkiewicz 		       __func__, ret, node->full_name);
3165de1540bSGrant Likely 		goto err_free;
3172bc552dfSBartlomiej Zolnierkiewicz 	}
3185de1540bSGrant Likely 
319c0f72f8aSRussell King 	ret = amba_device_add(dev, &iomem_resource);
3202bc552dfSBartlomiej Zolnierkiewicz 	if (ret) {
3212bc552dfSBartlomiej Zolnierkiewicz 		pr_err("%s(): amba_device_add() failed (%d) for %s\n",
3222bc552dfSBartlomiej Zolnierkiewicz 		       __func__, ret, node->full_name);
3235de1540bSGrant Likely 		goto err_free;
3242bc552dfSBartlomiej Zolnierkiewicz 	}
3255de1540bSGrant Likely 
3265de1540bSGrant Likely 	return dev;
3275de1540bSGrant Likely 
3285de1540bSGrant Likely err_free:
329c0f72f8aSRussell King 	amba_device_put(dev);
330c6e126deSPawel Moll err_clear_flag:
331c6e126deSPawel Moll 	of_node_clear_flag(node, OF_POPULATED);
3325de1540bSGrant Likely 	return NULL;
3335de1540bSGrant Likely }
3345de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */
3355de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node,
3365de1540bSGrant Likely 						 const char *bus_id,
3375de1540bSGrant Likely 						 void *platform_data,
3385de1540bSGrant Likely 						 struct device *parent)
3395de1540bSGrant Likely {
3405de1540bSGrant Likely 	return NULL;
3415de1540bSGrant Likely }
3425de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */
3435de1540bSGrant Likely 
3445fd200f3SGrant Likely /**
34515c3597dSGrant Likely  * of_devname_lookup() - Given a device node, lookup the preferred Linux name
34615c3597dSGrant Likely  */
34715c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
34815c3597dSGrant Likely 				 struct device_node *np)
34915c3597dSGrant Likely {
35015c3597dSGrant Likely 	struct resource res;
351303f59d1SOlof Johansson 
352303f59d1SOlof Johansson 	if (!lookup)
353303f59d1SOlof Johansson 		return NULL;
354303f59d1SOlof Johansson 
355f88e1ae8SGrant Likely 	for(; lookup->compatible != NULL; lookup++) {
35615c3597dSGrant Likely 		if (!of_device_is_compatible(np, lookup->compatible))
35715c3597dSGrant Likely 			continue;
35884774e61SLee Jones 		if (!of_address_to_resource(np, 0, &res))
35915c3597dSGrant Likely 			if (res.start != lookup->phys_addr)
36015c3597dSGrant Likely 				continue;
36115c3597dSGrant Likely 		pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
36215c3597dSGrant Likely 		return lookup;
36315c3597dSGrant Likely 	}
364303f59d1SOlof Johansson 
36515c3597dSGrant Likely 	return NULL;
36615c3597dSGrant Likely }
36715c3597dSGrant Likely 
36815c3597dSGrant Likely /**
36938e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
3705fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
3711eed4c07SGrant Likely  * @matches: match table for bus nodes
372303f59d1SOlof Johansson  * @lookup: auxdata table for matching id and platform_data with device nodes
37338e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
374303f59d1SOlof Johansson  * @strict: require compatible property
37538e9e21dSGrant Likely  *
37638e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
37738e9e21dSGrant Likely  * recursively create devices for all the child nodes.
3785fd200f3SGrant Likely  */
37938e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
3805fd200f3SGrant Likely 				  const struct of_device_id *matches,
38115c3597dSGrant Likely 				  const struct of_dev_auxdata *lookup,
38229d4f8a4SGrant Likely 				  struct device *parent, bool strict)
3835fd200f3SGrant Likely {
38415c3597dSGrant Likely 	const struct of_dev_auxdata *auxdata;
3855fd200f3SGrant Likely 	struct device_node *child;
38694a0cb1fSGrant Likely 	struct platform_device *dev;
38715c3597dSGrant Likely 	const char *bus_id = NULL;
38815c3597dSGrant Likely 	void *platform_data = NULL;
3895fd200f3SGrant Likely 	int rc = 0;
3905fd200f3SGrant Likely 
39129d4f8a4SGrant Likely 	/* Make sure it has a compatible property */
39229d4f8a4SGrant Likely 	if (strict && (!of_get_property(bus, "compatible", NULL))) {
39329d4f8a4SGrant Likely 		pr_debug("%s() - skipping %s, no compatible prop\n",
39429d4f8a4SGrant Likely 			 __func__, bus->full_name);
39529d4f8a4SGrant Likely 		return 0;
39629d4f8a4SGrant Likely 	}
39729d4f8a4SGrant Likely 
39815c3597dSGrant Likely 	auxdata = of_dev_lookup(lookup, bus);
39915c3597dSGrant Likely 	if (auxdata) {
40015c3597dSGrant Likely 		bus_id = auxdata->name;
40115c3597dSGrant Likely 		platform_data = auxdata->platform_data;
40215c3597dSGrant Likely 	}
40315c3597dSGrant Likely 
4045de1540bSGrant Likely 	if (of_device_is_compatible(bus, "arm,primecell")) {
4052bc552dfSBartlomiej Zolnierkiewicz 		/*
4062bc552dfSBartlomiej Zolnierkiewicz 		 * Don't return an error here to keep compatibility with older
4072bc552dfSBartlomiej Zolnierkiewicz 		 * device tree files.
4082bc552dfSBartlomiej Zolnierkiewicz 		 */
40915c3597dSGrant Likely 		of_amba_device_create(bus, bus_id, platform_data, parent);
4105de1540bSGrant Likely 		return 0;
4115de1540bSGrant Likely 	}
4125de1540bSGrant Likely 
41315c3597dSGrant Likely 	dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
41438e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
41538e9e21dSGrant Likely 		return 0;
41638e9e21dSGrant Likely 
4175fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
4185fd200f3SGrant Likely 		pr_debug("   create child: %s\n", child->full_name);
41915c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
4205fd200f3SGrant Likely 		if (rc) {
4215fd200f3SGrant Likely 			of_node_put(child);
4225fd200f3SGrant Likely 			break;
4235fd200f3SGrant Likely 		}
4245fd200f3SGrant Likely 	}
425*75f353b6SGrant Likely 	of_node_set_flag(bus, OF_POPULATED_BUS);
4265fd200f3SGrant Likely 	return rc;
4275fd200f3SGrant Likely }
4285fd200f3SGrant Likely 
4295fd200f3SGrant Likely /**
43038e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
4315fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
4321eed4c07SGrant Likely  * @matches: match table for bus nodes
4335fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
4345fd200f3SGrant Likely  *
4355fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
4365fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
4375fd200f3SGrant Likely  */
4385fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
4395fd200f3SGrant Likely 			  const struct of_device_id *matches,
4405fd200f3SGrant Likely 			  struct device *parent)
4415fd200f3SGrant Likely {
4425fd200f3SGrant Likely 	struct device_node *child;
4435fd200f3SGrant Likely 	int rc = 0;
4445fd200f3SGrant Likely 
4451eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
4461eed4c07SGrant Likely 	if (!root)
44760d59913SGrant Likely 		return -EINVAL;
4485fd200f3SGrant Likely 
4495fd200f3SGrant Likely 	pr_debug("of_platform_bus_probe()\n");
4505fd200f3SGrant Likely 	pr_debug(" starting at: %s\n", root->full_name);
4515fd200f3SGrant Likely 
4521eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
4535fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
45415c3597dSGrant Likely 		rc = of_platform_bus_create(root, matches, NULL, parent, false);
45538e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
4565fd200f3SGrant Likely 		if (!of_match_node(matches, child))
4575fd200f3SGrant Likely 			continue;
45815c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, NULL, parent, false);
45938e9e21dSGrant Likely 		if (rc)
4605fd200f3SGrant Likely 			break;
4615fd200f3SGrant Likely 	}
46238e9e21dSGrant Likely 
4635fd200f3SGrant Likely 	of_node_put(root);
4645fd200f3SGrant Likely 	return rc;
4655fd200f3SGrant Likely }
4665fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
46729d4f8a4SGrant Likely 
46829d4f8a4SGrant Likely /**
46929d4f8a4SGrant Likely  * of_platform_populate() - Populate platform_devices from device tree data
47029d4f8a4SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
47129d4f8a4SGrant Likely  * @matches: match table, NULL to use the default
47292039ed1SJavi Merino  * @lookup: auxdata table for matching id and platform_data with device nodes
47329d4f8a4SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
47429d4f8a4SGrant Likely  *
47529d4f8a4SGrant Likely  * Similar to of_platform_bus_probe(), this function walks the device tree
47629d4f8a4SGrant Likely  * and creates devices from nodes.  It differs in that it follows the modern
47729d4f8a4SGrant Likely  * convention of requiring all device nodes to have a 'compatible' property,
47829d4f8a4SGrant Likely  * and it is suitable for creating devices which are children of the root
47929d4f8a4SGrant Likely  * node (of_platform_bus_probe will only create children of the root which
48029d4f8a4SGrant Likely  * are selected by the @matches argument).
48129d4f8a4SGrant Likely  *
48229d4f8a4SGrant Likely  * New board support should be using this function instead of
48329d4f8a4SGrant Likely  * of_platform_bus_probe().
48429d4f8a4SGrant Likely  *
48529d4f8a4SGrant Likely  * Returns 0 on success, < 0 on failure.
48629d4f8a4SGrant Likely  */
48729d4f8a4SGrant Likely int of_platform_populate(struct device_node *root,
48829d4f8a4SGrant Likely 			const struct of_device_id *matches,
48915c3597dSGrant Likely 			const struct of_dev_auxdata *lookup,
49029d4f8a4SGrant Likely 			struct device *parent)
49129d4f8a4SGrant Likely {
49229d4f8a4SGrant Likely 	struct device_node *child;
49329d4f8a4SGrant Likely 	int rc = 0;
49429d4f8a4SGrant Likely 
49529d4f8a4SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
49629d4f8a4SGrant Likely 	if (!root)
49729d4f8a4SGrant Likely 		return -EINVAL;
49829d4f8a4SGrant Likely 
49929d4f8a4SGrant Likely 	for_each_child_of_node(root, child) {
50015c3597dSGrant Likely 		rc = of_platform_bus_create(child, matches, lookup, parent, true);
50129d4f8a4SGrant Likely 		if (rc)
50229d4f8a4SGrant Likely 			break;
50329d4f8a4SGrant Likely 	}
50429d4f8a4SGrant Likely 
50529d4f8a4SGrant Likely 	of_node_put(root);
50629d4f8a4SGrant Likely 	return rc;
50729d4f8a4SGrant Likely }
508e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate);
509c6e126deSPawel Moll 
510c6e126deSPawel Moll static int of_platform_device_destroy(struct device *dev, void *data)
511c6e126deSPawel Moll {
512c6e126deSPawel Moll 	/* Do not touch devices not populated from the device tree */
513*75f353b6SGrant Likely 	if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
514c6e126deSPawel Moll 		return 0;
515c6e126deSPawel Moll 
516*75f353b6SGrant Likely 	/* Recurse for any nodes that were treated as busses */
517*75f353b6SGrant Likely 	if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
518*75f353b6SGrant Likely 		device_for_each_child(dev, NULL, of_platform_device_destroy);
519c6e126deSPawel Moll 
520c6e126deSPawel Moll 	if (dev->bus == &platform_bus_type)
521c6e126deSPawel Moll 		platform_device_unregister(to_platform_device(dev));
522c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA
523c6e126deSPawel Moll 	else if (dev->bus == &amba_bustype)
524c6e126deSPawel Moll 		amba_device_unregister(to_amba_device(dev));
525c6e126deSPawel Moll #endif
526c6e126deSPawel Moll 
527c6e126deSPawel Moll 	of_node_clear_flag(dev->of_node, OF_POPULATED);
528*75f353b6SGrant Likely 	of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
529c6e126deSPawel Moll 	return 0;
530c6e126deSPawel Moll }
531c6e126deSPawel Moll 
532c6e126deSPawel Moll /**
533c6e126deSPawel Moll  * of_platform_depopulate() - Remove devices populated from device tree
534*75f353b6SGrant Likely  * @parent: device which children will be removed
535c6e126deSPawel Moll  *
536c6e126deSPawel Moll  * Complementary to of_platform_populate(), this function removes children
537c6e126deSPawel Moll  * of the given device (and, recurrently, their children) that have been
538c6e126deSPawel Moll  * created from their respective device tree nodes (and only those,
539c6e126deSPawel Moll  * leaving others - eg. manually created - unharmed).
540c6e126deSPawel Moll  *
541c6e126deSPawel Moll  * Returns 0 when all children devices have been removed or
542c6e126deSPawel Moll  * -EBUSY when some children remained.
543c6e126deSPawel Moll  */
544*75f353b6SGrant Likely void of_platform_depopulate(struct device *parent)
545c6e126deSPawel Moll {
546*75f353b6SGrant Likely 	device_for_each_child(parent, NULL, of_platform_device_destroy);
547c6e126deSPawel Moll }
548c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate);
549c6e126deSPawel Moll 
550964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */
551