1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
5 * and Arnd Bergmann, IBM Corp.
6 * Merged from powerpc/kernel/of_platform.c and
7 * sparc{,64}/kernel/of_device.c by Stephen Rothwell
10 #define pr_fmt(fmt) "OF: " fmt
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/amba/bus.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/slab.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
24 const struct of_device_id of_default_bus_match_table[] = {
25 { .compatible = "simple-bus", },
26 { .compatible = "simple-mfd", },
27 { .compatible = "isa", },
28 #ifdef CONFIG_ARM_AMBA
29 { .compatible = "arm,amba-bus", },
30 #endif /* CONFIG_ARM_AMBA */
31 {} /* Empty terminated list */
34 static const struct of_device_id of_skipped_node_table[] = {
35 { .compatible = "operating-points-v2", },
36 {} /* Empty terminated list */
40 * of_find_device_by_node - Find the platform_device associated with a node
41 * @np: Pointer to device tree node
43 * Takes a reference to the embedded struct device which needs to be dropped
46 * Return: platform_device pointer, or NULL if not found
48 struct platform_device *of_find_device_by_node(struct device_node *np)
52 dev = bus_find_device_by_of_node(&platform_bus_type, np);
53 return dev ? to_platform_device(dev) : NULL;
55 EXPORT_SYMBOL(of_find_device_by_node);
57 #ifdef CONFIG_OF_ADDRESS
59 * The following routines scan a subtree and registers a device for
60 * each applicable node.
62 * Note: sparc doesn't use these routines because it has a different
63 * mechanism for creating devices from device tree nodes.
67 * of_device_make_bus_id - Use the device node data to assign a unique name
68 * @dev: pointer to device structure that is linked to a device tree node
70 * This routine will first try using the translated bus address to
71 * derive a unique name. If it cannot, then it will prepend names from
72 * parent nodes until a unique name can be derived.
74 static void of_device_make_bus_id(struct device *dev)
76 struct device_node *node = dev->of_node;
81 /* Construct the name, using parent nodes if necessary to ensure uniqueness */
82 while (node->parent) {
84 * If the address can be translated, then that is as much
85 * uniqueness as we need. Make it the first component and return
87 reg = of_get_property(node, "reg", NULL);
88 if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
89 if (!of_property_read_u32(node, "mask", &mask))
90 dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
91 addr, ffs(mask) - 1, node, dev_name(dev));
94 dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
95 addr, node, dev_name(dev));
99 /* format arguments only used if dev_name() resolves to NULL */
100 dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
101 kbasename(node->full_name), dev_name(dev));
107 * of_device_alloc - Allocate and initialize an of_device
108 * @np: device node to assign to device
109 * @bus_id: Name to assign to the device. May be null to use default name.
110 * @parent: Parent device.
112 struct platform_device *of_device_alloc(struct device_node *np,
114 struct device *parent)
116 struct platform_device *dev;
117 int rc, i, num_reg = 0, num_irq;
118 struct resource *res, temp_res;
120 dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
124 /* count the io and irq resources */
125 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
127 num_irq = of_irq_count(np);
129 /* Populate the resource table */
130 if (num_irq || num_reg) {
131 res = kcalloc(num_irq + num_reg, sizeof(*res), GFP_KERNEL);
133 platform_device_put(dev);
137 dev->num_resources = num_reg + num_irq;
139 for (i = 0; i < num_reg; i++, res++) {
140 rc = of_address_to_resource(np, i, res);
143 if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
144 pr_debug("not all legacy IRQ resources mapped for %pOFn\n",
148 dev->dev.of_node = of_node_get(np);
149 dev->dev.fwnode = &np->fwnode;
150 dev->dev.parent = parent ? : &platform_bus;
153 dev_set_name(&dev->dev, "%s", bus_id);
155 of_device_make_bus_id(&dev->dev);
159 EXPORT_SYMBOL(of_device_alloc);
162 * of_platform_device_create_pdata - Alloc, initialize and register an of_device
163 * @np: pointer to node to create device for
164 * @bus_id: name to assign device
165 * @platform_data: pointer to populate platform_data pointer with
166 * @parent: Linux device model parent device.
168 * Return: Pointer to created platform device, or NULL if a device was not
169 * registered. Unavailable devices will not get registered.
171 static struct platform_device *of_platform_device_create_pdata(
172 struct device_node *np,
175 struct device *parent)
177 struct platform_device *dev;
179 if (!of_device_is_available(np) ||
180 of_node_test_and_set_flag(np, OF_POPULATED))
183 dev = of_device_alloc(np, bus_id, parent);
187 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
188 if (!dev->dev.dma_mask)
189 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
190 dev->dev.bus = &platform_bus_type;
191 dev->dev.platform_data = platform_data;
192 of_msi_configure(&dev->dev, dev->dev.of_node);
194 if (of_device_add(dev) != 0) {
195 platform_device_put(dev);
202 of_node_clear_flag(np, OF_POPULATED);
207 * of_platform_device_create - Alloc, initialize and register an of_device
208 * @np: pointer to node to create device for
209 * @bus_id: name to assign device
210 * @parent: Linux device model parent device.
212 * Return: Pointer to created platform device, or NULL if a device was not
213 * registered. Unavailable devices will not get registered.
215 struct platform_device *of_platform_device_create(struct device_node *np,
217 struct device *parent)
219 return of_platform_device_create_pdata(np, bus_id, NULL, parent);
221 EXPORT_SYMBOL(of_platform_device_create);
223 #ifdef CONFIG_ARM_AMBA
224 static struct amba_device *of_amba_device_create(struct device_node *node,
227 struct device *parent)
229 struct amba_device *dev;
233 pr_debug("Creating amba device %pOF\n", node);
235 if (!of_device_is_available(node) ||
236 of_node_test_and_set_flag(node, OF_POPULATED))
239 dev = amba_device_alloc(NULL, 0, 0);
243 /* AMBA devices only support a single DMA mask */
244 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
245 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
247 /* setup generic device info */
248 dev->dev.of_node = of_node_get(node);
249 dev->dev.fwnode = &node->fwnode;
250 dev->dev.parent = parent ? : &platform_bus;
251 dev->dev.platform_data = platform_data;
253 dev_set_name(&dev->dev, "%s", bus_id);
255 of_device_make_bus_id(&dev->dev);
257 /* Allow the HW Peripheral ID to be overridden */
258 prop = of_get_property(node, "arm,primecell-periphid", NULL);
260 dev->periphid = of_read_ulong(prop, 1);
262 ret = of_address_to_resource(node, 0, &dev->res);
264 pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
269 ret = amba_device_add(dev, &iomem_resource);
271 pr_err("amba_device_add() failed (%d) for %pOF\n",
279 amba_device_put(dev);
281 of_node_clear_flag(node, OF_POPULATED);
284 #else /* CONFIG_ARM_AMBA */
285 static struct amba_device *of_amba_device_create(struct device_node *node,
288 struct device *parent)
292 #endif /* CONFIG_ARM_AMBA */
295 * of_dev_lookup() - Given a device node, lookup the preferred Linux name
297 static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
298 struct device_node *np)
300 const struct of_dev_auxdata *auxdata;
308 for (; auxdata->compatible; auxdata++) {
309 if (!of_device_is_compatible(np, auxdata->compatible))
312 if (!of_address_to_resource(np, 0, &res))
313 if (res.start != auxdata->phys_addr)
315 pr_debug("%pOF: devname=%s\n", np, auxdata->name);
322 /* Try compatible match if no phys_addr and name are specified */
324 for (; auxdata->compatible; auxdata++) {
325 if (!of_device_is_compatible(np, auxdata->compatible))
327 if (!auxdata->phys_addr && !auxdata->name) {
328 pr_debug("%pOF: compatible match\n", np);
337 * of_platform_bus_create() - Create a device for a node and its children.
338 * @bus: device node of the bus to instantiate
339 * @matches: match table for bus nodes
340 * @lookup: auxdata table for matching id and platform_data with device nodes
341 * @parent: parent for new device, or NULL for top level.
342 * @strict: require compatible property
344 * Creates a platform_device for the provided device_node, and optionally
345 * recursively create devices for all the child nodes.
347 static int of_platform_bus_create(struct device_node *bus,
348 const struct of_device_id *matches,
349 const struct of_dev_auxdata *lookup,
350 struct device *parent, bool strict)
352 const struct of_dev_auxdata *auxdata;
353 struct device_node *child;
354 struct platform_device *dev;
355 const char *bus_id = NULL;
356 void *platform_data = NULL;
359 /* Make sure it has a compatible property */
360 if (strict && (!of_get_property(bus, "compatible", NULL))) {
361 pr_debug("%s() - skipping %pOF, no compatible prop\n",
366 /* Skip nodes for which we don't want to create devices */
367 if (unlikely(of_match_node(of_skipped_node_table, bus))) {
368 pr_debug("%s() - skipping %pOF node\n", __func__, bus);
372 if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
373 pr_debug("%s() - skipping %pOF, already populated\n",
378 auxdata = of_dev_lookup(lookup, bus);
380 bus_id = auxdata->name;
381 platform_data = auxdata->platform_data;
384 if (of_device_is_compatible(bus, "arm,primecell")) {
386 * Don't return an error here to keep compatibility with older
389 of_amba_device_create(bus, bus_id, platform_data, parent);
393 dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
394 if (!dev || !of_match_node(matches, bus))
397 for_each_child_of_node(bus, child) {
398 pr_debug(" create child: %pOF\n", child);
399 rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
405 of_node_set_flag(bus, OF_POPULATED_BUS);
410 * of_platform_bus_probe() - Probe the device-tree for platform buses
411 * @root: parent of the first level to probe or NULL for the root of the tree
412 * @matches: match table for bus nodes
413 * @parent: parent to hook devices from, NULL for toplevel
415 * Note that children of the provided root are not instantiated as devices
416 * unless the specified root itself matches the bus list and is not NULL.
418 int of_platform_bus_probe(struct device_node *root,
419 const struct of_device_id *matches,
420 struct device *parent)
422 struct device_node *child;
425 root = root ? of_node_get(root) : of_find_node_by_path("/");
429 pr_debug("%s()\n", __func__);
430 pr_debug(" starting at: %pOF\n", root);
432 /* Do a self check of bus type, if there's a match, create children */
433 if (of_match_node(matches, root)) {
434 rc = of_platform_bus_create(root, matches, NULL, parent, false);
435 } else for_each_child_of_node(root, child) {
436 if (!of_match_node(matches, child))
438 rc = of_platform_bus_create(child, matches, NULL, parent, false);
448 EXPORT_SYMBOL(of_platform_bus_probe);
451 * of_platform_populate() - Populate platform_devices from device tree data
452 * @root: parent of the first level to probe or NULL for the root of the tree
453 * @matches: match table, NULL to use the default
454 * @lookup: auxdata table for matching id and platform_data with device nodes
455 * @parent: parent to hook devices from, NULL for toplevel
457 * Similar to of_platform_bus_probe(), this function walks the device tree
458 * and creates devices from nodes. It differs in that it follows the modern
459 * convention of requiring all device nodes to have a 'compatible' property,
460 * and it is suitable for creating devices which are children of the root
461 * node (of_platform_bus_probe will only create children of the root which
462 * are selected by the @matches argument).
464 * New board support should be using this function instead of
465 * of_platform_bus_probe().
467 * Return: 0 on success, < 0 on failure.
469 int of_platform_populate(struct device_node *root,
470 const struct of_device_id *matches,
471 const struct of_dev_auxdata *lookup,
472 struct device *parent)
474 struct device_node *child;
477 root = root ? of_node_get(root) : of_find_node_by_path("/");
481 pr_debug("%s()\n", __func__);
482 pr_debug(" starting at: %pOF\n", root);
484 device_links_supplier_sync_state_pause();
485 for_each_child_of_node(root, child) {
486 rc = of_platform_bus_create(child, matches, lookup, parent, true);
492 device_links_supplier_sync_state_resume();
494 of_node_set_flag(root, OF_POPULATED_BUS);
499 EXPORT_SYMBOL_GPL(of_platform_populate);
501 int of_platform_default_populate(struct device_node *root,
502 const struct of_dev_auxdata *lookup,
503 struct device *parent)
505 return of_platform_populate(root, of_default_bus_match_table, lookup,
508 EXPORT_SYMBOL_GPL(of_platform_default_populate);
511 static const struct of_device_id reserved_mem_matches[] = {
512 { .compatible = "qcom,rmtfs-mem" },
513 { .compatible = "qcom,cmd-db" },
514 { .compatible = "qcom,smem" },
515 { .compatible = "ramoops" },
516 { .compatible = "nvmem-rmem" },
520 static int __init of_platform_default_populate_init(void)
522 struct device_node *node;
524 device_links_supplier_sync_state_pause();
526 if (!of_have_populated_dt())
530 * Handle certain compatibles explicitly, since we don't want to create
531 * platform_devices for every node in /reserved-memory with a
534 for_each_matching_node(node, reserved_mem_matches)
535 of_platform_device_create(node, NULL, NULL);
537 node = of_find_node_by_path("/firmware");
539 of_platform_populate(node, NULL, NULL, NULL);
543 node = of_get_compatible_child(of_chosen, "simple-framebuffer");
544 of_platform_device_create(node, NULL, NULL);
547 /* Populate everything else. */
548 of_platform_default_populate(NULL, NULL, NULL);
552 arch_initcall_sync(of_platform_default_populate_init);
554 static int __init of_platform_sync_state_init(void)
556 device_links_supplier_sync_state_resume();
559 late_initcall_sync(of_platform_sync_state_init);
562 int of_platform_device_destroy(struct device *dev, void *data)
564 /* Do not touch devices not populated from the device tree */
565 if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
568 /* Recurse for any nodes that were treated as busses */
569 if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
570 device_for_each_child(dev, NULL, of_platform_device_destroy);
572 of_node_clear_flag(dev->of_node, OF_POPULATED);
573 of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
575 if (dev->bus == &platform_bus_type)
576 platform_device_unregister(to_platform_device(dev));
577 #ifdef CONFIG_ARM_AMBA
578 else if (dev->bus == &amba_bustype)
579 amba_device_unregister(to_amba_device(dev));
584 EXPORT_SYMBOL_GPL(of_platform_device_destroy);
587 * of_platform_depopulate() - Remove devices populated from device tree
588 * @parent: device which children will be removed
590 * Complementary to of_platform_populate(), this function removes children
591 * of the given device (and, recurrently, their children) that have been
592 * created from their respective device tree nodes (and only those,
593 * leaving others - eg. manually created - unharmed).
595 void of_platform_depopulate(struct device *parent)
597 if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
598 device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
599 of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
602 EXPORT_SYMBOL_GPL(of_platform_depopulate);
604 static void devm_of_platform_populate_release(struct device *dev, void *res)
606 of_platform_depopulate(*(struct device **)res);
610 * devm_of_platform_populate() - Populate platform_devices from device tree data
611 * @dev: device that requested to populate from device tree data
613 * Similar to of_platform_populate(), but will automatically call
614 * of_platform_depopulate() when the device is unbound from the bus.
616 * Return: 0 on success, < 0 on failure.
618 int devm_of_platform_populate(struct device *dev)
626 ptr = devres_alloc(devm_of_platform_populate_release,
627 sizeof(*ptr), GFP_KERNEL);
631 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
636 devres_add(dev, ptr);
641 EXPORT_SYMBOL_GPL(devm_of_platform_populate);
643 static int devm_of_platform_match(struct device *dev, void *res, void *data)
645 struct device **ptr = res;
656 * devm_of_platform_depopulate() - Remove devices populated from device tree
657 * @dev: device that requested to depopulate from device tree data
659 * Complementary to devm_of_platform_populate(), this function removes children
660 * of the given device (and, recurrently, their children) that have been
661 * created from their respective device tree nodes (and only those,
662 * leaving others - eg. manually created - unharmed).
664 void devm_of_platform_depopulate(struct device *dev)
668 ret = devres_release(dev, devm_of_platform_populate_release,
669 devm_of_platform_match, dev);
673 EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
675 #ifdef CONFIG_OF_DYNAMIC
676 static int of_platform_notify(struct notifier_block *nb,
677 unsigned long action, void *arg)
679 struct of_reconfig_data *rd = arg;
680 struct platform_device *pdev_parent, *pdev;
683 switch (of_reconfig_get_state_change(action, rd)) {
684 case OF_RECONFIG_CHANGE_ADD:
685 /* verify that the parent is a bus */
686 if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
687 return NOTIFY_OK; /* not for us */
689 /* already populated? (driver using of_populate manually) */
690 if (of_node_check_flag(rd->dn, OF_POPULATED))
693 /* pdev_parent may be NULL when no bus platform device */
694 pdev_parent = of_find_device_by_node(rd->dn->parent);
695 pdev = of_platform_device_create(rd->dn, NULL,
696 pdev_parent ? &pdev_parent->dev : NULL);
697 platform_device_put(pdev_parent);
700 pr_err("%s: failed to create for '%pOF'\n",
702 /* of_platform_device_create tosses the error code */
703 return notifier_from_errno(-EINVAL);
707 case OF_RECONFIG_CHANGE_REMOVE:
709 /* already depopulated? */
710 if (!of_node_check_flag(rd->dn, OF_POPULATED))
713 /* find our device by node */
714 pdev = of_find_device_by_node(rd->dn);
716 return NOTIFY_OK; /* no? not meant for us */
718 /* unregister takes one ref away */
719 of_platform_device_destroy(&pdev->dev, &children_left);
721 /* and put the reference of the find */
722 platform_device_put(pdev);
729 static struct notifier_block platform_of_notifier = {
730 .notifier_call = of_platform_notify,
733 void of_platform_register_reconfig_notifier(void)
735 WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
737 #endif /* CONFIG_OF_DYNAMIC */
739 #endif /* CONFIG_OF_ADDRESS */