xref: /linux/drivers/of/platform.c (revision 1eed4c077c31450bfcd0249aebb21d96fc5d5d3c)
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>
163f23de10SStephen Rothwell #include <linux/device.h>
175fd200f3SGrant Likely #include <linux/dma-mapping.h>
1850ef5284SGrant Likely #include <linux/slab.h>
199e3288dcSGrant Likely #include <linux/of_address.h>
203f23de10SStephen Rothwell #include <linux/of_device.h>
219e3288dcSGrant Likely #include <linux/of_irq.h>
223f23de10SStephen Rothwell #include <linux/of_platform.h>
23eca39301SGrant Likely #include <linux/platform_device.h>
24eca39301SGrant Likely 
25c6085584SJonas Bonn static int of_dev_node_match(struct device *dev, void *data)
26c6085584SJonas Bonn {
27c6085584SJonas Bonn 	return dev->of_node == data;
28c6085584SJonas Bonn }
29c6085584SJonas Bonn 
30c6085584SJonas Bonn /**
31c6085584SJonas Bonn  * of_find_device_by_node - Find the platform_device associated with a node
32c6085584SJonas Bonn  * @np: Pointer to device tree node
33c6085584SJonas Bonn  *
34c6085584SJonas Bonn  * Returns platform_device pointer, or NULL if not found
35c6085584SJonas Bonn  */
36c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np)
37c6085584SJonas Bonn {
38c6085584SJonas Bonn 	struct device *dev;
39c6085584SJonas Bonn 
40c6085584SJonas Bonn 	dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
41c6085584SJonas Bonn 	return dev ? to_platform_device(dev) : NULL;
42c6085584SJonas Bonn }
43c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node);
44c6085584SJonas Bonn 
45596c955cSGrant Likely #if defined(CONFIG_PPC_DCR)
46596c955cSGrant Likely #include <asm/dcr.h>
47596c955cSGrant Likely #endif
48596c955cSGrant Likely 
495fd200f3SGrant Likely #if !defined(CONFIG_SPARC)
505fd200f3SGrant Likely /*
515fd200f3SGrant Likely  * The following routines scan a subtree and registers a device for
525fd200f3SGrant Likely  * each applicable node.
535fd200f3SGrant Likely  *
545fd200f3SGrant Likely  * Note: sparc doesn't use these routines because it has a different
555fd200f3SGrant Likely  * mechanism for creating devices from device tree nodes.
565fd200f3SGrant Likely  */
575fd200f3SGrant Likely 
585fd200f3SGrant Likely /**
5994c09319SGrant Likely  * of_device_make_bus_id - Use the device node data to assign a unique name
6094c09319SGrant Likely  * @dev: pointer to device structure that is linked to a device tree node
6194c09319SGrant Likely  *
6294c09319SGrant Likely  * This routine will first try using either the dcr-reg or the reg property
6394c09319SGrant Likely  * value to derive a unique name.  As a last resort it will use the node
6494c09319SGrant Likely  * name followed by a unique number.
6594c09319SGrant Likely  */
66c6601225SGrant Likely void of_device_make_bus_id(struct device *dev)
6794c09319SGrant Likely {
6894c09319SGrant Likely 	static atomic_t bus_no_reg_magic;
6994c09319SGrant Likely 	struct device_node *node = dev->of_node;
7094c09319SGrant Likely 	const u32 *reg;
7194c09319SGrant Likely 	u64 addr;
7294c09319SGrant Likely 	int magic;
7394c09319SGrant Likely 
7494c09319SGrant Likely #ifdef CONFIG_PPC_DCR
7594c09319SGrant Likely 	/*
7694c09319SGrant Likely 	 * If it's a DCR based device, use 'd' for native DCRs
7794c09319SGrant Likely 	 * and 'D' for MMIO DCRs.
7894c09319SGrant Likely 	 */
7994c09319SGrant Likely 	reg = of_get_property(node, "dcr-reg", NULL);
8094c09319SGrant Likely 	if (reg) {
8194c09319SGrant Likely #ifdef CONFIG_PPC_DCR_NATIVE
8294c09319SGrant Likely 		dev_set_name(dev, "d%x.%s", *reg, node->name);
8394c09319SGrant Likely #else /* CONFIG_PPC_DCR_NATIVE */
8494c09319SGrant Likely 		u64 addr = of_translate_dcr_address(node, *reg, NULL);
8594c09319SGrant Likely 		if (addr != OF_BAD_ADDR) {
8694c09319SGrant Likely 			dev_set_name(dev, "D%llx.%s",
8794c09319SGrant Likely 				     (unsigned long long)addr, node->name);
8894c09319SGrant Likely 			return;
8994c09319SGrant Likely 		}
9094c09319SGrant Likely #endif /* !CONFIG_PPC_DCR_NATIVE */
9194c09319SGrant Likely 	}
9294c09319SGrant Likely #endif /* CONFIG_PPC_DCR */
9394c09319SGrant Likely 
9494c09319SGrant Likely 	/*
9594c09319SGrant Likely 	 * For MMIO, get the physical address
9694c09319SGrant Likely 	 */
9794c09319SGrant Likely 	reg = of_get_property(node, "reg", NULL);
9894c09319SGrant Likely 	if (reg) {
9994c09319SGrant Likely 		addr = of_translate_address(node, reg);
10094c09319SGrant Likely 		if (addr != OF_BAD_ADDR) {
10194c09319SGrant Likely 			dev_set_name(dev, "%llx.%s",
10294c09319SGrant Likely 				     (unsigned long long)addr, node->name);
10394c09319SGrant Likely 			return;
10494c09319SGrant Likely 		}
10594c09319SGrant Likely 	}
10694c09319SGrant Likely 
10794c09319SGrant Likely 	/*
10894c09319SGrant Likely 	 * No BusID, use the node name and add a globally incremented
10994c09319SGrant Likely 	 * counter (and pray...)
11094c09319SGrant Likely 	 */
11194c09319SGrant Likely 	magic = atomic_add_return(1, &bus_no_reg_magic);
11294c09319SGrant Likely 	dev_set_name(dev, "%s.%d", node->name, magic - 1);
11394c09319SGrant Likely }
11494c09319SGrant Likely 
11594c09319SGrant Likely /**
11694c09319SGrant Likely  * of_device_alloc - Allocate and initialize an of_device
11794c09319SGrant Likely  * @np: device node to assign to device
11894c09319SGrant Likely  * @bus_id: Name to assign to the device.  May be null to use default name.
11994c09319SGrant Likely  * @parent: Parent device.
12094c09319SGrant Likely  */
12194a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np,
12294c09319SGrant Likely 				  const char *bus_id,
12394c09319SGrant Likely 				  struct device *parent)
12494c09319SGrant Likely {
12594a0cb1fSGrant Likely 	struct platform_device *dev;
12652f6537cSAndres Salomon 	int rc, i, num_reg = 0, num_irq;
127ac80a51eSGrant Likely 	struct resource *res, temp_res;
12894c09319SGrant Likely 
1297096d042SGrant Likely 	dev = platform_device_alloc("", -1);
1307096d042SGrant Likely 	if (!dev)
1317096d042SGrant Likely 		return NULL;
1327096d042SGrant Likely 
1337096d042SGrant Likely 	/* count the io and irq resources */
134ac80a51eSGrant Likely 	while (of_address_to_resource(np, num_reg, &temp_res) == 0)
135ac80a51eSGrant Likely 		num_reg++;
13652f6537cSAndres Salomon 	num_irq = of_irq_count(np);
137ac80a51eSGrant Likely 
138ac80a51eSGrant Likely 	/* Populate the resource table */
139ac80a51eSGrant Likely 	if (num_irq || num_reg) {
1407096d042SGrant Likely 		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
1417096d042SGrant Likely 		if (!res) {
1427096d042SGrant Likely 			platform_device_put(dev);
1437096d042SGrant Likely 			return NULL;
1447096d042SGrant Likely 		}
1457096d042SGrant Likely 
146ac80a51eSGrant Likely 		dev->num_resources = num_reg + num_irq;
147ac80a51eSGrant Likely 		dev->resource = res;
148ac80a51eSGrant Likely 		for (i = 0; i < num_reg; i++, res++) {
149ac80a51eSGrant Likely 			rc = of_address_to_resource(np, i, res);
150ac80a51eSGrant Likely 			WARN_ON(rc);
151ac80a51eSGrant Likely 		}
15252f6537cSAndres Salomon 		WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
153ac80a51eSGrant Likely 	}
15494c09319SGrant Likely 
15594c09319SGrant Likely 	dev->dev.of_node = of_node_get(np);
1569e3288dcSGrant Likely #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
15794c09319SGrant Likely 	dev->dev.dma_mask = &dev->archdata.dma_mask;
1589e3288dcSGrant Likely #endif
15994c09319SGrant Likely 	dev->dev.parent = parent;
16094c09319SGrant Likely 
16194c09319SGrant Likely 	if (bus_id)
16294c09319SGrant Likely 		dev_set_name(&dev->dev, "%s", bus_id);
16394c09319SGrant Likely 	else
16494c09319SGrant Likely 		of_device_make_bus_id(&dev->dev);
16594c09319SGrant Likely 
16694c09319SGrant Likely 	return dev;
16794c09319SGrant Likely }
16894c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc);
16994c09319SGrant Likely 
17094c09319SGrant Likely /**
1715fd200f3SGrant Likely  * of_platform_device_create - Alloc, initialize and register an of_device
1725fd200f3SGrant Likely  * @np: pointer to node to create device for
1735fd200f3SGrant Likely  * @bus_id: name to assign device
1745fd200f3SGrant Likely  * @parent: Linux device model parent device.
175cd1e6504SGrant Likely  *
176cd1e6504SGrant Likely  * Returns pointer to created platform device, or NULL if a device was not
177cd1e6504SGrant Likely  * registered.  Unavailable devices will not get registered.
1785fd200f3SGrant Likely  */
17994a0cb1fSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np,
1805fd200f3SGrant Likely 					    const char *bus_id,
1815fd200f3SGrant Likely 					    struct device *parent)
1825fd200f3SGrant Likely {
18394a0cb1fSGrant Likely 	struct platform_device *dev;
1845fd200f3SGrant Likely 
185cd1e6504SGrant Likely 	if (!of_device_is_available(np))
186cd1e6504SGrant Likely 		return NULL;
187cd1e6504SGrant Likely 
1885fd200f3SGrant Likely 	dev = of_device_alloc(np, bus_id, parent);
1895fd200f3SGrant Likely 	if (!dev)
1905fd200f3SGrant Likely 		return NULL;
1915fd200f3SGrant Likely 
1929e3288dcSGrant Likely #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
1935fd200f3SGrant Likely 	dev->archdata.dma_mask = 0xffffffffUL;
1949e3288dcSGrant Likely #endif
1955fd200f3SGrant Likely 	dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
196eca39301SGrant Likely 	dev->dev.bus = &platform_bus_type;
1975fd200f3SGrant Likely 
1985fd200f3SGrant Likely 	/* We do not fill the DMA ops for platform devices by default.
1995fd200f3SGrant Likely 	 * This is currently the responsibility of the platform code
2005fd200f3SGrant Likely 	 * to do such, possibly using a device notifier
2015fd200f3SGrant Likely 	 */
2025fd200f3SGrant Likely 
2037096d042SGrant Likely 	if (of_device_add(dev) != 0) {
2047096d042SGrant Likely 		platform_device_put(dev);
2055fd200f3SGrant Likely 		return NULL;
2065fd200f3SGrant Likely 	}
2075fd200f3SGrant Likely 
2085fd200f3SGrant Likely 	return dev;
2095fd200f3SGrant Likely }
2105fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create);
2115fd200f3SGrant Likely 
2125fd200f3SGrant Likely /**
21338e9e21dSGrant Likely  * of_platform_bus_create() - Create a device for a node and its children.
2145fd200f3SGrant Likely  * @bus: device node of the bus to instantiate
215*1eed4c07SGrant Likely  * @matches: match table for bus nodes
21638e9e21dSGrant Likely  * disallow recursive creation of child buses
21738e9e21dSGrant Likely  * @parent: parent for new device, or NULL for top level.
21838e9e21dSGrant Likely  *
21938e9e21dSGrant Likely  * Creates a platform_device for the provided device_node, and optionally
22038e9e21dSGrant Likely  * recursively create devices for all the child nodes.
2215fd200f3SGrant Likely  */
22238e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus,
2235fd200f3SGrant Likely 				  const struct of_device_id *matches,
2245fd200f3SGrant Likely 				  struct device *parent)
2255fd200f3SGrant Likely {
2265fd200f3SGrant Likely 	struct device_node *child;
22794a0cb1fSGrant Likely 	struct platform_device *dev;
2285fd200f3SGrant Likely 	int rc = 0;
2295fd200f3SGrant Likely 
23038e9e21dSGrant Likely 	dev = of_platform_device_create(bus, NULL, parent);
23138e9e21dSGrant Likely 	if (!dev || !of_match_node(matches, bus))
23238e9e21dSGrant Likely 		return 0;
23338e9e21dSGrant Likely 
2345fd200f3SGrant Likely 	for_each_child_of_node(bus, child) {
2355fd200f3SGrant Likely 		pr_debug("   create child: %s\n", child->full_name);
2365fd200f3SGrant Likely 		rc = of_platform_bus_create(child, matches, &dev->dev);
2375fd200f3SGrant Likely 		if (rc) {
2385fd200f3SGrant Likely 			of_node_put(child);
2395fd200f3SGrant Likely 			break;
2405fd200f3SGrant Likely 		}
2415fd200f3SGrant Likely 	}
2425fd200f3SGrant Likely 	return rc;
2435fd200f3SGrant Likely }
2445fd200f3SGrant Likely 
2455fd200f3SGrant Likely /**
24638e9e21dSGrant Likely  * of_platform_bus_probe() - Probe the device-tree for platform buses
2475fd200f3SGrant Likely  * @root: parent of the first level to probe or NULL for the root of the tree
248*1eed4c07SGrant Likely  * @matches: match table for bus nodes
2495fd200f3SGrant Likely  * @parent: parent to hook devices from, NULL for toplevel
2505fd200f3SGrant Likely  *
2515fd200f3SGrant Likely  * Note that children of the provided root are not instantiated as devices
2525fd200f3SGrant Likely  * unless the specified root itself matches the bus list and is not NULL.
2535fd200f3SGrant Likely  */
2545fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root,
2555fd200f3SGrant Likely 			  const struct of_device_id *matches,
2565fd200f3SGrant Likely 			  struct device *parent)
2575fd200f3SGrant Likely {
2585fd200f3SGrant Likely 	struct device_node *child;
2595fd200f3SGrant Likely 	int rc = 0;
2605fd200f3SGrant Likely 
261*1eed4c07SGrant Likely 	root = root ? of_node_get(root) : of_find_node_by_path("/");
262*1eed4c07SGrant Likely 	if (!root)
26360d59913SGrant Likely 		return -EINVAL;
2645fd200f3SGrant Likely 
2655fd200f3SGrant Likely 	pr_debug("of_platform_bus_probe()\n");
2665fd200f3SGrant Likely 	pr_debug(" starting at: %s\n", root->full_name);
2675fd200f3SGrant Likely 
268*1eed4c07SGrant Likely 	/* Do a self check of bus type, if there's a match, create children */
2695fd200f3SGrant Likely 	if (of_match_node(matches, root)) {
27038e9e21dSGrant Likely 		rc = of_platform_bus_create(root, matches, parent);
27138e9e21dSGrant Likely 	} else for_each_child_of_node(root, child) {
2725fd200f3SGrant Likely 		if (!of_match_node(matches, child))
2735fd200f3SGrant Likely 			continue;
27438e9e21dSGrant Likely 		rc = of_platform_bus_create(child, matches, parent);
27538e9e21dSGrant Likely 		if (rc)
2765fd200f3SGrant Likely 			break;
2775fd200f3SGrant Likely 	}
27838e9e21dSGrant Likely 
2795fd200f3SGrant Likely 	of_node_put(root);
2805fd200f3SGrant Likely 	return rc;
2815fd200f3SGrant Likely }
2825fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe);
2835fd200f3SGrant Likely #endif /* !CONFIG_SPARC */
284