blob: 4aba42f92359ae683257b10a94a3760d0a8031da [file] [log] [blame]
Stephen Rothwell3f23de12007-05-03 02:38:57 +10001/*
2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 * and Arnd Bergmann, IBM Corp.
5 * Merged from powerpc/kernel/of_platform.c and
6 * sparc{,64}/kernel/of_device.c by Stephen Rothwell
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 */
Rob Herring606ad422016-06-15 08:32:18 -050014
15#define pr_fmt(fmt) "OF: " fmt
16
Stephen Rothwell3f23de12007-05-03 02:38:57 +100017#include <linux/errno.h>
Stephen Rothwell5c457082007-10-17 21:17:42 -070018#include <linux/module.h>
Grant Likely5de15402011-06-21 10:59:34 -060019#include <linux/amba/bus.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100020#include <linux/device.h>
Grant Likely5fd200f2010-06-08 07:48:13 -060021#include <linux/dma-mapping.h>
Grant Likely50ef5282010-06-08 07:48:20 -060022#include <linux/slab.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060023#include <linux/of_address.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100024#include <linux/of_device.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060025#include <linux/of_irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100026#include <linux/of_platform.h>
David Keitel8d04fbc2016-02-04 10:12:25 -080027#include <linux/of_reserved_mem.h>
Grant Likelyeca39302010-06-08 07:48:21 -060028#include <linux/platform_device.h>
29
Grant Likelycbb49c22011-06-21 10:59:34 -060030const struct of_device_id of_default_bus_match_table[] = {
31 { .compatible = "simple-bus", },
Linus Walleij22869a92015-03-03 09:52:20 +010032 { .compatible = "simple-mfd", },
Paul Burtonecd76ed2016-09-19 22:21:24 +010033 { .compatible = "isa", },
Grant Likelycbb49c22011-06-21 10:59:34 -060034#ifdef CONFIG_ARM_AMBA
35 { .compatible = "arm,amba-bus", },
36#endif /* CONFIG_ARM_AMBA */
37 {} /* Empty terminated list */
38};
39
Jonas Bonnc6085582010-07-23 19:19:35 +020040static int of_dev_node_match(struct device *dev, void *data)
41{
42 return dev->of_node == data;
43}
44
45/**
46 * of_find_device_by_node - Find the platform_device associated with a node
47 * @np: Pointer to device tree node
48 *
49 * Returns platform_device pointer, or NULL if not found
50 */
51struct platform_device *of_find_device_by_node(struct device_node *np)
52{
53 struct device *dev;
54
55 dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
56 return dev ? to_platform_device(dev) : NULL;
57}
58EXPORT_SYMBOL(of_find_device_by_node);
59
Grant Likely964dba22012-02-24 14:58:34 -070060#ifdef CONFIG_OF_ADDRESS
Grant Likely5fd200f2010-06-08 07:48:13 -060061/*
62 * The following routines scan a subtree and registers a device for
63 * each applicable node.
64 *
65 * Note: sparc doesn't use these routines because it has a different
66 * mechanism for creating devices from device tree nodes.
67 */
68
69/**
Grant Likely94c09312010-06-08 07:48:14 -060070 * of_device_make_bus_id - Use the device node data to assign a unique name
71 * @dev: pointer to device structure that is linked to a device tree node
72 *
Grant Likely07e461c2014-05-21 15:40:31 +090073 * This routine will first try using the translated bus address to
74 * derive a unique name. If it cannot, then it will prepend names from
75 * parent nodes until a unique name can be derived.
Grant Likely94c09312010-06-08 07:48:14 -060076 */
Grant Likelyc6601222010-07-23 15:04:01 -060077void of_device_make_bus_id(struct device *dev)
Grant Likely94c09312010-06-08 07:48:14 -060078{
Grant Likely94c09312010-06-08 07:48:14 -060079 struct device_node *node = dev->of_node;
Kim Phillips24fb5302012-10-08 19:42:11 -050080 const __be32 *reg;
Grant Likely94c09312010-06-08 07:48:14 -060081 u64 addr;
Grant Likely94c09312010-06-08 07:48:14 -060082
Grant Likely07e461c2014-05-21 15:40:31 +090083 /* Construct the name, using parent nodes if necessary to ensure uniqueness */
84 while (node->parent) {
85 /*
86 * If the address can be translated, then that is as much
87 * uniqueness as we need. Make it the first component and return
88 */
89 reg = of_get_property(node, "reg", NULL);
90 if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
91 dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
92 (unsigned long long)addr, node->name,
93 dev_name(dev));
Grant Likely94c09312010-06-08 07:48:14 -060094 return;
95 }
Grant Likely94c09312010-06-08 07:48:14 -060096
Grant Likely07e461c2014-05-21 15:40:31 +090097 /* format arguments only used if dev_name() resolves to NULL */
98 dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
99 strrchr(node->full_name, '/') + 1, dev_name(dev));
100 node = node->parent;
Grant Likely94c09312010-06-08 07:48:14 -0600101 }
Grant Likely94c09312010-06-08 07:48:14 -0600102}
103
104/**
105 * of_device_alloc - Allocate and initialize an of_device
106 * @np: device node to assign to device
107 * @bus_id: Name to assign to the device. May be null to use default name.
108 * @parent: Parent device.
109 */
Grant Likely94a0cb12010-07-22 13:59:23 -0600110struct platform_device *of_device_alloc(struct device_node *np,
Grant Likely94c09312010-06-08 07:48:14 -0600111 const char *bus_id,
112 struct device *parent)
113{
Grant Likely94a0cb12010-07-22 13:59:23 -0600114 struct platform_device *dev;
Andres Salomon52f65372010-10-10 21:35:05 -0600115 int rc, i, num_reg = 0, num_irq;
Grant Likelyac80a512010-06-08 07:48:15 -0600116 struct resource *res, temp_res;
Grant Likely94c09312010-06-08 07:48:14 -0600117
Grant Likely7096d042010-10-20 11:45:13 -0600118 dev = platform_device_alloc("", -1);
119 if (!dev)
120 return NULL;
121
122 /* count the io and irq resources */
Rob Herringd9c68662014-05-07 15:23:56 -0500123 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
124 num_reg++;
Andres Salomon52f65372010-10-10 21:35:05 -0600125 num_irq = of_irq_count(np);
Grant Likelyac80a512010-06-08 07:48:15 -0600126
Grant Likelyac80a512010-06-08 07:48:15 -0600127 /* Populate the resource table */
128 if (num_irq || num_reg) {
Grant Likely7096d042010-10-20 11:45:13 -0600129 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
130 if (!res) {
131 platform_device_put(dev);
132 return NULL;
133 }
134
Grant Likelyac80a512010-06-08 07:48:15 -0600135 dev->num_resources = num_reg + num_irq;
136 dev->resource = res;
137 for (i = 0; i < num_reg; i++, res++) {
138 rc = of_address_to_resource(np, i, res);
139 WARN_ON(rc);
140 }
Rob Herring9ec36ca2014-04-23 17:57:41 -0500141 if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
142 pr_debug("not all legacy IRQ resources mapped for %s\n",
143 np->name);
Grant Likelyac80a512010-06-08 07:48:15 -0600144 }
Grant Likely94c09312010-06-08 07:48:14 -0600145
146 dev->dev.of_node = of_node_get(np);
Robin Murphyf94277a2016-09-14 16:01:24 +0100147 dev->dev.fwnode = &np->fwnode;
Grant Likely43c07672014-11-04 10:26:26 +0000148 dev->dev.parent = parent ? : &platform_bus;
Grant Likely94c09312010-06-08 07:48:14 -0600149
150 if (bus_id)
151 dev_set_name(&dev->dev, "%s", bus_id);
152 else
153 of_device_make_bus_id(&dev->dev);
154
155 return dev;
156}
157EXPORT_SYMBOL(of_device_alloc);
158
Will Deacon97890ba2014-08-27 16:24:20 +0100159static void of_dma_deconfigure(struct device *dev)
160{
161 arch_teardown_dma_ops(dev);
Santosh Shilimkar591c1ee2014-04-24 11:30:04 -0400162}
163
164/**
Grant Likely15c35972011-06-21 10:59:35 -0600165 * of_platform_device_create_pdata - Alloc, initialize and register an of_device
Grant Likely5fd200f2010-06-08 07:48:13 -0600166 * @np: pointer to node to create device for
167 * @bus_id: name to assign device
Grant Likely15c35972011-06-21 10:59:35 -0600168 * @platform_data: pointer to populate platform_data pointer with
Grant Likely5fd200f2010-06-08 07:48:13 -0600169 * @parent: Linux device model parent device.
Grant Likelycd1e6502011-01-03 15:51:11 -0700170 *
171 * Returns pointer to created platform device, or NULL if a device was not
172 * registered. Unavailable devices will not get registered.
Grant Likely5fd200f2010-06-08 07:48:13 -0600173 */
Mark Brown245d9642013-07-01 20:26:52 +0100174static struct platform_device *of_platform_device_create_pdata(
Grant Likely15c35972011-06-21 10:59:35 -0600175 struct device_node *np,
176 const char *bus_id,
177 void *platform_data,
178 struct device *parent)
Grant Likely5fd200f2010-06-08 07:48:13 -0600179{
Grant Likely94a0cb12010-07-22 13:59:23 -0600180 struct platform_device *dev;
Grant Likely5fd200f2010-06-08 07:48:13 -0600181
Pawel Mollc6e126d2014-05-15 16:55:24 +0100182 if (!of_device_is_available(np) ||
183 of_node_test_and_set_flag(np, OF_POPULATED))
Grant Likelycd1e6502011-01-03 15:51:11 -0700184 return NULL;
185
Grant Likely5fd200f2010-06-08 07:48:13 -0600186 dev = of_device_alloc(np, bus_id, parent);
187 if (!dev)
Pawel Mollc6e126d2014-05-15 16:55:24 +0100188 goto err_clear_flag;
Grant Likely5fd200f2010-06-08 07:48:13 -0600189
Grant Likelyeca39302010-06-08 07:48:21 -0600190 dev->dev.bus = &platform_bus_type;
Grant Likely15c35972011-06-21 10:59:35 -0600191 dev->dev.platform_data = platform_data;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500192 of_dma_configure(&dev->dev, dev->dev.of_node);
Marc Zyngierc706c232015-07-28 14:46:15 +0100193 of_msi_configure(&dev->dev, dev->dev.of_node);
David Keitel8d04fbc2016-02-04 10:12:25 -0800194 of_reserved_mem_device_init_by_idx(&dev->dev, dev->dev.of_node, 0);
Grant Likely5fd200f2010-06-08 07:48:13 -0600195
Grant Likely7096d042010-10-20 11:45:13 -0600196 if (of_device_add(dev) != 0) {
Will Deacon97890ba2014-08-27 16:24:20 +0100197 of_dma_deconfigure(&dev->dev);
Grant Likely7096d042010-10-20 11:45:13 -0600198 platform_device_put(dev);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100199 goto err_clear_flag;
Grant Likely5fd200f2010-06-08 07:48:13 -0600200 }
201
202 return dev;
Pawel Mollc6e126d2014-05-15 16:55:24 +0100203
204err_clear_flag:
205 of_node_clear_flag(np, OF_POPULATED);
206 return NULL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600207}
Grant Likely15c35972011-06-21 10:59:35 -0600208
209/**
210 * of_platform_device_create - Alloc, initialize and register an of_device
211 * @np: pointer to node to create device for
212 * @bus_id: name to assign device
213 * @parent: Linux device model parent device.
214 *
215 * Returns pointer to created platform device, or NULL if a device was not
216 * registered. Unavailable devices will not get registered.
217 */
218struct platform_device *of_platform_device_create(struct device_node *np,
219 const char *bus_id,
220 struct device *parent)
221{
222 return of_platform_device_create_pdata(np, bus_id, NULL, parent);
223}
Grant Likely5fd200f2010-06-08 07:48:13 -0600224EXPORT_SYMBOL(of_platform_device_create);
225
Grant Likely5de15402011-06-21 10:59:34 -0600226#ifdef CONFIG_ARM_AMBA
227static struct amba_device *of_amba_device_create(struct device_node *node,
228 const char *bus_id,
229 void *platform_data,
230 struct device *parent)
231{
232 struct amba_device *dev;
233 const void *prop;
234 int i, ret;
235
236 pr_debug("Creating amba device %s\n", node->full_name);
237
Pawel Mollc6e126d2014-05-15 16:55:24 +0100238 if (!of_device_is_available(node) ||
239 of_node_test_and_set_flag(node, OF_POPULATED))
Grant Likely5de15402011-06-21 10:59:34 -0600240 return NULL;
241
Russell Kingc0f72f82011-12-18 11:45:17 +0000242 dev = amba_device_alloc(NULL, 0, 0);
Rob Herring606ad422016-06-15 08:32:18 -0500243 if (!dev)
Pawel Mollc6e126d2014-05-15 16:55:24 +0100244 goto err_clear_flag;
Grant Likely5de15402011-06-21 10:59:34 -0600245
246 /* setup generic device info */
Grant Likely5de15402011-06-21 10:59:34 -0600247 dev->dev.of_node = of_node_get(node);
Robin Murphyf94277a2016-09-14 16:01:24 +0100248 dev->dev.fwnode = &node->fwnode;
Grant Likely43c07672014-11-04 10:26:26 +0000249 dev->dev.parent = parent ? : &platform_bus;
Grant Likely5de15402011-06-21 10:59:34 -0600250 dev->dev.platform_data = platform_data;
251 if (bus_id)
252 dev_set_name(&dev->dev, "%s", bus_id);
253 else
254 of_device_make_bus_id(&dev->dev);
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500255 of_dma_configure(&dev->dev, dev->dev.of_node);
Grant Likely5de15402011-06-21 10:59:34 -0600256
Grant Likely5de15402011-06-21 10:59:34 -0600257 /* Allow the HW Peripheral ID to be overridden */
258 prop = of_get_property(node, "arm,primecell-periphid", NULL);
259 if (prop)
260 dev->periphid = of_read_ulong(prop, 1);
261
262 /* Decode the IRQs and address ranges */
263 for (i = 0; i < AMBA_NR_IRQS; i++)
264 dev->irq[i] = irq_of_parse_and_map(node, i);
265
266 ret = of_address_to_resource(node, 0, &dev->res);
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200267 if (ret) {
Rob Herring606ad422016-06-15 08:32:18 -0500268 pr_err("amba: of_address_to_resource() failed (%d) for %s\n",
269 ret, node->full_name);
Grant Likely5de15402011-06-21 10:59:34 -0600270 goto err_free;
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200271 }
Grant Likely5de15402011-06-21 10:59:34 -0600272
Russell Kingc0f72f82011-12-18 11:45:17 +0000273 ret = amba_device_add(dev, &iomem_resource);
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200274 if (ret) {
Rob Herring606ad422016-06-15 08:32:18 -0500275 pr_err("amba_device_add() failed (%d) for %s\n",
276 ret, node->full_name);
Grant Likely5de15402011-06-21 10:59:34 -0600277 goto err_free;
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200278 }
Grant Likely5de15402011-06-21 10:59:34 -0600279
280 return dev;
281
282err_free:
Russell Kingc0f72f82011-12-18 11:45:17 +0000283 amba_device_put(dev);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100284err_clear_flag:
285 of_node_clear_flag(node, OF_POPULATED);
Grant Likely5de15402011-06-21 10:59:34 -0600286 return NULL;
287}
288#else /* CONFIG_ARM_AMBA */
289static struct amba_device *of_amba_device_create(struct device_node *node,
290 const char *bus_id,
291 void *platform_data,
292 struct device *parent)
293{
294 return NULL;
295}
296#endif /* CONFIG_ARM_AMBA */
297
Grant Likely5fd200f2010-06-08 07:48:13 -0600298/**
Grant Likely15c35972011-06-21 10:59:35 -0600299 * of_devname_lookup() - Given a device node, lookup the preferred Linux name
300 */
301static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
302 struct device_node *np)
303{
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700304 const struct of_dev_auxdata *auxdata;
Grant Likely15c35972011-06-21 10:59:35 -0600305 struct resource res;
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700306 int compatible = 0;
Olof Johansson303f59d2011-11-02 22:07:29 -0700307
308 if (!lookup)
309 return NULL;
310
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700311 auxdata = lookup;
312 for (; auxdata->compatible; auxdata++) {
313 if (!of_device_is_compatible(np, auxdata->compatible))
Olof Johansson303f59d2011-11-02 22:07:29 -0700314 continue;
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700315 compatible++;
Lee Jones84774e62012-07-05 15:15:36 +0100316 if (!of_address_to_resource(np, 0, &res))
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700317 if (res.start != auxdata->phys_addr)
Lee Jones84774e62012-07-05 15:15:36 +0100318 continue;
Tony Lindgrenfc5cf802016-04-15 08:45:32 -0700319 pr_debug("%s: devname=%s\n", np->full_name, auxdata->name);
320 return auxdata;
321 }
322
323 if (!compatible)
324 return NULL;
325
326 /* Try compatible match if no phys_addr and name are specified */
327 auxdata = lookup;
328 for (; auxdata->compatible; auxdata++) {
329 if (!of_device_is_compatible(np, auxdata->compatible))
330 continue;
331 if (!auxdata->phys_addr && !auxdata->name) {
332 pr_debug("%s: compatible match\n", np->full_name);
333 return auxdata;
334 }
Grant Likely15c35972011-06-21 10:59:35 -0600335 }
Olof Johansson303f59d2011-11-02 22:07:29 -0700336
Grant Likely15c35972011-06-21 10:59:35 -0600337 return NULL;
338}
339
340/**
Grant Likely38e9e212011-03-18 10:21:28 -0600341 * of_platform_bus_create() - Create a device for a node and its children.
Grant Likely5fd200f2010-06-08 07:48:13 -0600342 * @bus: device node of the bus to instantiate
Grant Likely1eed4c02011-03-18 10:21:29 -0600343 * @matches: match table for bus nodes
Olof Johansson303f59d2011-11-02 22:07:29 -0700344 * @lookup: auxdata table for matching id and platform_data with device nodes
Grant Likely38e9e212011-03-18 10:21:28 -0600345 * @parent: parent for new device, or NULL for top level.
Olof Johansson303f59d2011-11-02 22:07:29 -0700346 * @strict: require compatible property
Grant Likely38e9e212011-03-18 10:21:28 -0600347 *
348 * Creates a platform_device for the provided device_node, and optionally
349 * recursively create devices for all the child nodes.
Grant Likely5fd200f2010-06-08 07:48:13 -0600350 */
Grant Likely38e9e212011-03-18 10:21:28 -0600351static int of_platform_bus_create(struct device_node *bus,
Grant Likely5fd200f2010-06-08 07:48:13 -0600352 const struct of_device_id *matches,
Grant Likely15c35972011-06-21 10:59:35 -0600353 const struct of_dev_auxdata *lookup,
Grant Likely29d4f8a2011-06-21 10:59:34 -0600354 struct device *parent, bool strict)
Grant Likely5fd200f2010-06-08 07:48:13 -0600355{
Grant Likely15c35972011-06-21 10:59:35 -0600356 const struct of_dev_auxdata *auxdata;
Grant Likely5fd200f2010-06-08 07:48:13 -0600357 struct device_node *child;
Grant Likely94a0cb12010-07-22 13:59:23 -0600358 struct platform_device *dev;
Grant Likely15c35972011-06-21 10:59:35 -0600359 const char *bus_id = NULL;
360 void *platform_data = NULL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600361 int rc = 0;
362
Grant Likely29d4f8a2011-06-21 10:59:34 -0600363 /* Make sure it has a compatible property */
364 if (strict && (!of_get_property(bus, "compatible", NULL))) {
365 pr_debug("%s() - skipping %s, no compatible prop\n",
366 __func__, bus->full_name);
367 return 0;
368 }
369
Kefeng Wang44a71852016-06-01 14:52:54 +0800370 if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
371 pr_debug("%s() - skipping %s, already populated\n",
372 __func__, bus->full_name);
373 return 0;
374 }
375
Grant Likely15c35972011-06-21 10:59:35 -0600376 auxdata = of_dev_lookup(lookup, bus);
377 if (auxdata) {
378 bus_id = auxdata->name;
379 platform_data = auxdata->platform_data;
380 }
381
Grant Likely5de15402011-06-21 10:59:34 -0600382 if (of_device_is_compatible(bus, "arm,primecell")) {
Bartlomiej Zolnierkiewicz2bc552d2013-08-30 13:17:29 +0200383 /*
384 * Don't return an error here to keep compatibility with older
385 * device tree files.
386 */
Grant Likely15c35972011-06-21 10:59:35 -0600387 of_amba_device_create(bus, bus_id, platform_data, parent);
Grant Likely5de15402011-06-21 10:59:34 -0600388 return 0;
389 }
390
Grant Likely15c35972011-06-21 10:59:35 -0600391 dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
Grant Likely38e9e212011-03-18 10:21:28 -0600392 if (!dev || !of_match_node(matches, bus))
393 return 0;
394
Grant Likely5fd200f2010-06-08 07:48:13 -0600395 for_each_child_of_node(bus, child) {
396 pr_debug(" create child: %s\n", child->full_name);
Grant Likely15c35972011-06-21 10:59:35 -0600397 rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
Grant Likely5fd200f2010-06-08 07:48:13 -0600398 if (rc) {
399 of_node_put(child);
400 break;
401 }
402 }
Grant Likely75f353b2014-06-24 16:13:47 +0100403 of_node_set_flag(bus, OF_POPULATED_BUS);
Grant Likely5fd200f2010-06-08 07:48:13 -0600404 return rc;
405}
406
407/**
Grant Likely38e9e212011-03-18 10:21:28 -0600408 * of_platform_bus_probe() - Probe the device-tree for platform buses
Grant Likely5fd200f2010-06-08 07:48:13 -0600409 * @root: parent of the first level to probe or NULL for the root of the tree
Grant Likely1eed4c02011-03-18 10:21:29 -0600410 * @matches: match table for bus nodes
Grant Likely5fd200f2010-06-08 07:48:13 -0600411 * @parent: parent to hook devices from, NULL for toplevel
412 *
413 * Note that children of the provided root are not instantiated as devices
414 * unless the specified root itself matches the bus list and is not NULL.
415 */
416int of_platform_bus_probe(struct device_node *root,
417 const struct of_device_id *matches,
418 struct device *parent)
419{
420 struct device_node *child;
Grant Likely5fd200f2010-06-08 07:48:13 -0600421 int rc = 0;
422
Grant Likely1eed4c02011-03-18 10:21:29 -0600423 root = root ? of_node_get(root) : of_find_node_by_path("/");
424 if (!root)
Grant Likely60d59912010-06-08 07:48:25 -0600425 return -EINVAL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600426
Kefeng Wang44a71852016-06-01 14:52:54 +0800427 pr_debug("%s()\n", __func__);
Grant Likely5fd200f2010-06-08 07:48:13 -0600428 pr_debug(" starting at: %s\n", root->full_name);
429
Grant Likely1eed4c02011-03-18 10:21:29 -0600430 /* Do a self check of bus type, if there's a match, create children */
Grant Likely5fd200f2010-06-08 07:48:13 -0600431 if (of_match_node(matches, root)) {
Grant Likely15c35972011-06-21 10:59:35 -0600432 rc = of_platform_bus_create(root, matches, NULL, parent, false);
Grant Likely38e9e212011-03-18 10:21:28 -0600433 } else for_each_child_of_node(root, child) {
Grant Likely5fd200f2010-06-08 07:48:13 -0600434 if (!of_match_node(matches, child))
435 continue;
Grant Likely15c35972011-06-21 10:59:35 -0600436 rc = of_platform_bus_create(child, matches, NULL, parent, false);
Julia Lawall7fad948a2015-10-22 11:02:49 +0200437 if (rc) {
438 of_node_put(child);
Grant Likely5fd200f2010-06-08 07:48:13 -0600439 break;
Julia Lawall7fad948a2015-10-22 11:02:49 +0200440 }
Grant Likely5fd200f2010-06-08 07:48:13 -0600441 }
Grant Likely38e9e212011-03-18 10:21:28 -0600442
Grant Likely5fd200f2010-06-08 07:48:13 -0600443 of_node_put(root);
444 return rc;
445}
446EXPORT_SYMBOL(of_platform_bus_probe);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600447
448/**
449 * of_platform_populate() - Populate platform_devices from device tree data
450 * @root: parent of the first level to probe or NULL for the root of the tree
451 * @matches: match table, NULL to use the default
Javi Merino92039ed2012-11-23 16:25:08 +0000452 * @lookup: auxdata table for matching id and platform_data with device nodes
Grant Likely29d4f8a2011-06-21 10:59:34 -0600453 * @parent: parent to hook devices from, NULL for toplevel
454 *
455 * Similar to of_platform_bus_probe(), this function walks the device tree
456 * and creates devices from nodes. It differs in that it follows the modern
457 * convention of requiring all device nodes to have a 'compatible' property,
458 * and it is suitable for creating devices which are children of the root
459 * node (of_platform_bus_probe will only create children of the root which
460 * are selected by the @matches argument).
461 *
462 * New board support should be using this function instead of
463 * of_platform_bus_probe().
464 *
465 * Returns 0 on success, < 0 on failure.
466 */
467int of_platform_populate(struct device_node *root,
468 const struct of_device_id *matches,
Grant Likely15c35972011-06-21 10:59:35 -0600469 const struct of_dev_auxdata *lookup,
Grant Likely29d4f8a2011-06-21 10:59:34 -0600470 struct device *parent)
471{
472 struct device_node *child;
473 int rc = 0;
474
475 root = root ? of_node_get(root) : of_find_node_by_path("/");
476 if (!root)
477 return -EINVAL;
478
Kefeng Wang44a71852016-06-01 14:52:54 +0800479 pr_debug("%s()\n", __func__);
480 pr_debug(" starting at: %s\n", root->full_name);
481
Grant Likely29d4f8a2011-06-21 10:59:34 -0600482 for_each_child_of_node(root, child) {
Grant Likely15c35972011-06-21 10:59:35 -0600483 rc = of_platform_bus_create(child, matches, lookup, parent, true);
Julia Lawall7fad948a2015-10-22 11:02:49 +0200484 if (rc) {
485 of_node_put(child);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600486 break;
Julia Lawall7fad948a2015-10-22 11:02:49 +0200487 }
Grant Likely29d4f8a2011-06-21 10:59:34 -0600488 }
Grant Likely2d0747c2014-11-19 22:35:39 +0000489 of_node_set_flag(root, OF_POPULATED_BUS);
Grant Likely29d4f8a2011-06-21 10:59:34 -0600490
491 of_node_put(root);
492 return rc;
493}
Stephen Warrene001f1c2012-06-07 17:57:36 -0600494EXPORT_SYMBOL_GPL(of_platform_populate);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100495
Hauke Mehrtens43443ad2015-08-02 19:44:43 +0200496int of_platform_default_populate(struct device_node *root,
497 const struct of_dev_auxdata *lookup,
498 struct device *parent)
499{
500 return of_platform_populate(root, of_default_bus_match_table, lookup,
501 parent);
502}
503EXPORT_SYMBOL_GPL(of_platform_default_populate);
504
Kevin Haofc520f82016-08-12 13:49:34 +0800505#ifndef CONFIG_PPC
Kefeng Wang44a71852016-06-01 14:52:54 +0800506static int __init of_platform_default_populate_init(void)
507{
Kees Cook529182e2016-07-29 18:11:32 -0700508 struct device_node *node;
509
510 if (!of_have_populated_dt())
511 return -ENODEV;
512
513 /*
514 * Handle ramoops explicitly, since it is inside /reserved-memory,
515 * which lacks a "compatible" property.
516 */
517 node = of_find_node_by_path("/reserved-memory");
518 if (node) {
519 node = of_find_compatible_node(node, NULL, "ramoops");
520 if (node)
521 of_platform_device_create(node, NULL, NULL);
522 }
523
524 /* Populate everything else. */
525 of_platform_default_populate(NULL, NULL, NULL);
Kefeng Wang44a71852016-06-01 14:52:54 +0800526
527 return 0;
528}
529arch_initcall_sync(of_platform_default_populate_init);
Kevin Haofc520f82016-08-12 13:49:34 +0800530#endif
Kefeng Wang44a71852016-06-01 14:52:54 +0800531
Pawel Mollc6e126d2014-05-15 16:55:24 +0100532static int of_platform_device_destroy(struct device *dev, void *data)
533{
Pawel Mollc6e126d2014-05-15 16:55:24 +0100534 /* Do not touch devices not populated from the device tree */
Grant Likely75f353b2014-06-24 16:13:47 +0100535 if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
Pawel Mollc6e126d2014-05-15 16:55:24 +0100536 return 0;
Pawel Mollc6e126d2014-05-15 16:55:24 +0100537
Grant Likely75f353b2014-06-24 16:13:47 +0100538 /* Recurse for any nodes that were treated as busses */
539 if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
540 device_for_each_child(dev, NULL, of_platform_device_destroy);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100541
542 if (dev->bus == &platform_bus_type)
543 platform_device_unregister(to_platform_device(dev));
544#ifdef CONFIG_ARM_AMBA
545 else if (dev->bus == &amba_bustype)
546 amba_device_unregister(to_amba_device(dev));
547#endif
Pawel Mollc6e126d2014-05-15 16:55:24 +0100548
Will Deacon0495cb72015-01-16 17:04:56 +0000549 of_dma_deconfigure(dev);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100550 of_node_clear_flag(dev->of_node, OF_POPULATED);
Grant Likely75f353b2014-06-24 16:13:47 +0100551 of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
Pawel Mollc6e126d2014-05-15 16:55:24 +0100552 return 0;
553}
554
555/**
556 * of_platform_depopulate() - Remove devices populated from device tree
Grant Likely75f353b2014-06-24 16:13:47 +0100557 * @parent: device which children will be removed
Pawel Mollc6e126d2014-05-15 16:55:24 +0100558 *
559 * Complementary to of_platform_populate(), this function removes children
560 * of the given device (and, recurrently, their children) that have been
561 * created from their respective device tree nodes (and only those,
562 * leaving others - eg. manually created - unharmed).
563 *
564 * Returns 0 when all children devices have been removed or
565 * -EBUSY when some children remained.
566 */
Grant Likely75f353b2014-06-24 16:13:47 +0100567void of_platform_depopulate(struct device *parent)
Pawel Mollc6e126d2014-05-15 16:55:24 +0100568{
Grant Likely2d0747c2014-11-19 22:35:39 +0000569 if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
570 device_for_each_child(parent, NULL, of_platform_device_destroy);
571 of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
572 }
Pawel Mollc6e126d2014-05-15 16:55:24 +0100573}
574EXPORT_SYMBOL_GPL(of_platform_depopulate);
575
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200576#ifdef CONFIG_OF_DYNAMIC
577static int of_platform_notify(struct notifier_block *nb,
578 unsigned long action, void *arg)
579{
580 struct of_reconfig_data *rd = arg;
581 struct platform_device *pdev_parent, *pdev;
582 bool children_left;
583
584 switch (of_reconfig_get_state_change(action, rd)) {
585 case OF_RECONFIG_CHANGE_ADD:
586 /* verify that the parent is a bus */
587 if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
588 return NOTIFY_OK; /* not for us */
589
Pantelis Antoniou15204ab2014-12-16 19:45:26 +0200590 /* already populated? (driver using of_populate manually) */
591 if (of_node_check_flag(rd->dn, OF_POPULATED))
592 return NOTIFY_OK;
593
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200594 /* pdev_parent may be NULL when no bus platform device */
595 pdev_parent = of_find_device_by_node(rd->dn->parent);
596 pdev = of_platform_device_create(rd->dn, NULL,
597 pdev_parent ? &pdev_parent->dev : NULL);
598 of_dev_put(pdev_parent);
599
600 if (pdev == NULL) {
601 pr_err("%s: failed to create for '%s'\n",
602 __func__, rd->dn->full_name);
603 /* of_platform_device_create tosses the error code */
604 return notifier_from_errno(-EINVAL);
605 }
606 break;
607
608 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou15204ab2014-12-16 19:45:26 +0200609
610 /* already depopulated? */
611 if (!of_node_check_flag(rd->dn, OF_POPULATED))
612 return NOTIFY_OK;
613
Pantelis Antoniou801d7282014-10-28 22:36:01 +0200614 /* find our device by node */
615 pdev = of_find_device_by_node(rd->dn);
616 if (pdev == NULL)
617 return NOTIFY_OK; /* no? not meant for us */
618
619 /* unregister takes one ref away */
620 of_platform_device_destroy(&pdev->dev, &children_left);
621
622 /* and put the reference of the find */
623 of_dev_put(pdev);
624 break;
625 }
626
627 return NOTIFY_OK;
628}
629
630static struct notifier_block platform_of_notifier = {
631 .notifier_call = of_platform_notify,
632};
633
634void of_platform_register_reconfig_notifier(void)
635{
636 WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
637}
638#endif /* CONFIG_OF_DYNAMIC */
639
Grant Likely964dba22012-02-24 14:58:34 -0700640#endif /* CONFIG_OF_ADDRESS */