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>
23 #include <linux/sysfb.h>
25 #include "of_private.h"
28 * of_find_device_by_node - Find the platform_device associated with a node
29 * @np: Pointer to device tree node
31 * Takes a reference to the embedded struct device which needs to be dropped
34 * Return: platform_device pointer, or NULL if not found
36 struct platform_device
*of_find_device_by_node(struct device_node
*np
)
40 dev
= bus_find_device_by_of_node(&platform_bus_type
, np
);
41 return dev
? to_platform_device(dev
) : NULL
;
43 EXPORT_SYMBOL(of_find_device_by_node
);
45 int of_device_add(struct platform_device
*ofdev
)
47 BUG_ON(ofdev
->dev
.of_node
== NULL
);
49 /* name and id have to be set so that the platform bus doesn't get
50 * confused on matching */
51 ofdev
->name
= dev_name(&ofdev
->dev
);
52 ofdev
->id
= PLATFORM_DEVID_NONE
;
55 * If this device has not binding numa node in devicetree, that is
56 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
57 * device is on the same node as the parent.
59 set_dev_node(&ofdev
->dev
, of_node_to_nid(ofdev
->dev
.of_node
));
61 return device_add(&ofdev
->dev
);
64 int of_device_register(struct platform_device
*pdev
)
66 device_initialize(&pdev
->dev
);
67 return of_device_add(pdev
);
69 EXPORT_SYMBOL(of_device_register
);
71 void of_device_unregister(struct platform_device
*ofdev
)
73 device_unregister(&ofdev
->dev
);
75 EXPORT_SYMBOL(of_device_unregister
);
77 #ifdef CONFIG_OF_ADDRESS
78 static const struct of_device_id of_skipped_node_table
[] = {
79 { .compatible
= "operating-points-v2", },
80 {} /* Empty terminated list */
84 * The following routines scan a subtree and registers a device for
85 * each applicable node.
87 * Note: sparc doesn't use these routines because it has a different
88 * mechanism for creating devices from device tree nodes.
92 * of_device_alloc - Allocate and initialize an of_device
93 * @np: device node to assign to device
94 * @bus_id: Name to assign to the device. May be null to use default name.
95 * @parent: Parent device.
97 struct platform_device
*of_device_alloc(struct device_node
*np
,
99 struct device
*parent
)
101 struct platform_device
*dev
;
102 int rc
, i
, num_reg
= 0;
103 struct resource
*res
;
105 dev
= platform_device_alloc("", PLATFORM_DEVID_NONE
);
109 /* count the io resources */
110 num_reg
= of_address_count(np
);
112 /* Populate the resource table */
114 res
= kcalloc(num_reg
, sizeof(*res
), GFP_KERNEL
);
116 platform_device_put(dev
);
120 dev
->num_resources
= num_reg
;
122 for (i
= 0; i
< num_reg
; i
++, res
++) {
123 rc
= of_address_to_resource(np
, i
, res
);
128 /* setup generic device info */
129 device_set_node(&dev
->dev
, of_fwnode_handle(of_node_get(np
)));
130 dev
->dev
.parent
= parent
? : &platform_bus
;
133 dev_set_name(&dev
->dev
, "%s", bus_id
);
135 of_device_make_bus_id(&dev
->dev
);
139 EXPORT_SYMBOL(of_device_alloc
);
142 * of_platform_device_create_pdata - Alloc, initialize and register an of_device
143 * @np: pointer to node to create device for
144 * @bus_id: name to assign device
145 * @platform_data: pointer to populate platform_data pointer with
146 * @parent: Linux device model parent device.
148 * Return: Pointer to created platform device, or NULL if a device was not
149 * registered. Unavailable devices will not get registered.
151 static struct platform_device
*of_platform_device_create_pdata(
152 struct device_node
*np
,
155 struct device
*parent
)
157 struct platform_device
*dev
;
159 pr_debug("create platform device: %pOF\n", np
);
161 if (!of_device_is_available(np
) ||
162 of_node_test_and_set_flag(np
, OF_POPULATED
))
165 dev
= of_device_alloc(np
, bus_id
, parent
);
169 dev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
170 if (!dev
->dev
.dma_mask
)
171 dev
->dev
.dma_mask
= &dev
->dev
.coherent_dma_mask
;
172 dev
->dev
.bus
= &platform_bus_type
;
173 dev
->dev
.platform_data
= platform_data
;
174 of_msi_configure(&dev
->dev
, dev
->dev
.of_node
);
176 if (of_device_add(dev
) != 0) {
177 platform_device_put(dev
);
184 of_node_clear_flag(np
, OF_POPULATED
);
189 * of_platform_device_create - Alloc, initialize and register an of_device
190 * @np: pointer to node to create device for
191 * @bus_id: name to assign device
192 * @parent: Linux device model parent device.
194 * Return: Pointer to created platform device, or NULL if a device was not
195 * registered. Unavailable devices will not get registered.
197 struct platform_device
*of_platform_device_create(struct device_node
*np
,
199 struct device
*parent
)
201 return of_platform_device_create_pdata(np
, bus_id
, NULL
, parent
);
203 EXPORT_SYMBOL(of_platform_device_create
);
205 #ifdef CONFIG_ARM_AMBA
206 static struct amba_device
*of_amba_device_create(struct device_node
*node
,
209 struct device
*parent
)
211 struct amba_device
*dev
;
214 pr_debug("Creating amba device %pOF\n", node
);
216 if (!of_device_is_available(node
) ||
217 of_node_test_and_set_flag(node
, OF_POPULATED
))
220 dev
= amba_device_alloc(NULL
, 0, 0);
224 /* AMBA devices only support a single DMA mask */
225 dev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
226 dev
->dev
.dma_mask
= &dev
->dev
.coherent_dma_mask
;
228 /* setup generic device info */
229 device_set_node(&dev
->dev
, of_fwnode_handle(node
));
230 dev
->dev
.parent
= parent
? : &platform_bus
;
231 dev
->dev
.platform_data
= platform_data
;
233 dev_set_name(&dev
->dev
, "%s", bus_id
);
235 of_device_make_bus_id(&dev
->dev
);
237 /* Allow the HW Peripheral ID to be overridden */
238 of_property_read_u32(node
, "arm,primecell-periphid", &dev
->periphid
);
240 ret
= of_address_to_resource(node
, 0, &dev
->res
);
242 pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
247 ret
= amba_device_add(dev
, &iomem_resource
);
249 pr_err("amba_device_add() failed (%d) for %pOF\n",
257 amba_device_put(dev
);
259 of_node_clear_flag(node
, OF_POPULATED
);
262 #else /* CONFIG_ARM_AMBA */
263 static struct amba_device
*of_amba_device_create(struct device_node
*node
,
266 struct device
*parent
)
270 #endif /* CONFIG_ARM_AMBA */
273 * of_dev_lookup() - Given a device node, lookup the preferred Linux name
275 static const struct of_dev_auxdata
*of_dev_lookup(const struct of_dev_auxdata
*lookup
,
276 struct device_node
*np
)
278 const struct of_dev_auxdata
*auxdata
;
286 for (; auxdata
->compatible
; auxdata
++) {
287 if (!of_device_is_compatible(np
, auxdata
->compatible
))
290 if (!of_address_to_resource(np
, 0, &res
))
291 if (res
.start
!= auxdata
->phys_addr
)
293 pr_debug("%pOF: devname=%s\n", np
, auxdata
->name
);
300 /* Try compatible match if no phys_addr and name are specified */
302 for (; auxdata
->compatible
; auxdata
++) {
303 if (!of_device_is_compatible(np
, auxdata
->compatible
))
305 if (!auxdata
->phys_addr
&& !auxdata
->name
) {
306 pr_debug("%pOF: compatible match\n", np
);
315 * of_platform_bus_create() - Create a device for a node and its children.
316 * @bus: device node of the bus to instantiate
317 * @matches: match table for bus nodes
318 * @lookup: auxdata table for matching id and platform_data with device nodes
319 * @parent: parent for new device, or NULL for top level.
320 * @strict: require compatible property
322 * Creates a platform_device for the provided device_node, and optionally
323 * recursively create devices for all the child nodes.
325 static int of_platform_bus_create(struct device_node
*bus
,
326 const struct of_device_id
*matches
,
327 const struct of_dev_auxdata
*lookup
,
328 struct device
*parent
, bool strict
)
330 const struct of_dev_auxdata
*auxdata
;
331 struct platform_device
*dev
;
332 const char *bus_id
= NULL
;
333 void *platform_data
= NULL
;
336 /* Make sure it has a compatible property */
337 if (strict
&& (!of_get_property(bus
, "compatible", NULL
))) {
338 pr_debug("%s() - skipping %pOF, no compatible prop\n",
343 /* Skip nodes for which we don't want to create devices */
344 if (unlikely(of_match_node(of_skipped_node_table
, bus
))) {
345 pr_debug("%s() - skipping %pOF node\n", __func__
, bus
);
349 if (of_node_check_flag(bus
, OF_POPULATED_BUS
)) {
350 pr_debug("%s() - skipping %pOF, already populated\n",
355 auxdata
= of_dev_lookup(lookup
, bus
);
357 bus_id
= auxdata
->name
;
358 platform_data
= auxdata
->platform_data
;
361 if (of_device_is_compatible(bus
, "arm,primecell")) {
363 * Don't return an error here to keep compatibility with older
366 of_amba_device_create(bus
, bus_id
, platform_data
, parent
);
370 dev
= of_platform_device_create_pdata(bus
, bus_id
, platform_data
, parent
);
371 if (!dev
|| !of_match_node(matches
, bus
))
374 for_each_child_of_node_scoped(bus
, child
) {
375 pr_debug(" create child: %pOF\n", child
);
376 rc
= of_platform_bus_create(child
, matches
, lookup
, &dev
->dev
, strict
);
380 of_node_set_flag(bus
, OF_POPULATED_BUS
);
385 * of_platform_bus_probe() - Probe the device-tree for platform buses
386 * @root: parent of the first level to probe or NULL for the root of the tree
387 * @matches: match table for bus nodes
388 * @parent: parent to hook devices from, NULL for toplevel
390 * Note that children of the provided root are not instantiated as devices
391 * unless the specified root itself matches the bus list and is not NULL.
393 int of_platform_bus_probe(struct device_node
*root
,
394 const struct of_device_id
*matches
,
395 struct device
*parent
)
397 struct device_node
*child
;
400 root
= root
? of_node_get(root
) : of_find_node_by_path("/");
404 pr_debug("%s()\n", __func__
);
405 pr_debug(" starting at: %pOF\n", root
);
407 /* Do a self check of bus type, if there's a match, create children */
408 if (of_match_node(matches
, root
)) {
409 rc
= of_platform_bus_create(root
, matches
, NULL
, parent
, false);
410 } else for_each_child_of_node(root
, child
) {
411 if (!of_match_node(matches
, child
))
413 rc
= of_platform_bus_create(child
, matches
, NULL
, parent
, false);
423 EXPORT_SYMBOL(of_platform_bus_probe
);
426 * of_platform_populate() - Populate platform_devices from device tree data
427 * @root: parent of the first level to probe or NULL for the root of the tree
428 * @matches: match table, NULL to use the default
429 * @lookup: auxdata table for matching id and platform_data with device nodes
430 * @parent: parent to hook devices from, NULL for toplevel
432 * Similar to of_platform_bus_probe(), this function walks the device tree
433 * and creates devices from nodes. It differs in that it follows the modern
434 * convention of requiring all device nodes to have a 'compatible' property,
435 * and it is suitable for creating devices which are children of the root
436 * node (of_platform_bus_probe will only create children of the root which
437 * are selected by the @matches argument).
439 * New board support should be using this function instead of
440 * of_platform_bus_probe().
442 * Return: 0 on success, < 0 on failure.
444 int of_platform_populate(struct device_node
*root
,
445 const struct of_device_id
*matches
,
446 const struct of_dev_auxdata
*lookup
,
447 struct device
*parent
)
451 root
= root
? of_node_get(root
) : of_find_node_by_path("/");
455 pr_debug("%s()\n", __func__
);
456 pr_debug(" starting at: %pOF\n", root
);
458 device_links_supplier_sync_state_pause();
459 for_each_child_of_node_scoped(root
, child
) {
460 rc
= of_platform_bus_create(child
, matches
, lookup
, parent
, true);
464 device_links_supplier_sync_state_resume();
466 of_node_set_flag(root
, OF_POPULATED_BUS
);
471 EXPORT_SYMBOL_GPL(of_platform_populate
);
473 int of_platform_default_populate(struct device_node
*root
,
474 const struct of_dev_auxdata
*lookup
,
475 struct device
*parent
)
477 static const struct of_device_id match_table
[] = {
478 { .compatible
= "simple-bus", },
479 { .compatible
= "simple-mfd", },
480 { .compatible
= "isa", },
481 #ifdef CONFIG_ARM_AMBA
482 { .compatible
= "arm,amba-bus", },
483 #endif /* CONFIG_ARM_AMBA */
484 {} /* Empty terminated list */
487 return of_platform_populate(root
, match_table
, lookup
, parent
);
489 EXPORT_SYMBOL_GPL(of_platform_default_populate
);
491 static const struct of_device_id reserved_mem_matches
[] = {
492 { .compatible
= "phram" },
493 { .compatible
= "qcom,rmtfs-mem" },
494 { .compatible
= "qcom,cmd-db" },
495 { .compatible
= "qcom,smem" },
496 { .compatible
= "ramoops" },
497 { .compatible
= "nvmem-rmem" },
498 { .compatible
= "google,open-dice" },
502 static int __init
of_platform_default_populate_init(void)
504 struct device_node
*node
;
506 device_links_supplier_sync_state_pause();
508 if (IS_ENABLED(CONFIG_PPC
)) {
509 struct device_node
*boot_display
= NULL
;
510 struct platform_device
*dev
;
511 int display_number
= 0;
514 /* Check if we have a MacOS display without a node spec */
515 if (of_property_present(of_chosen
, "linux,bootx-noscreen")) {
517 * The old code tried to work out which node was the MacOS
518 * display based on the address. I'm dropping that since the
519 * lack of a node spec only happens with old BootX versions
520 * (users can update) and with this code, they'll still get
521 * a display (just not the palette hacks).
523 dev
= platform_device_alloc("bootx-noscreen", 0);
526 ret
= platform_device_add(dev
);
528 platform_device_put(dev
);
534 * For OF framebuffers, first create the device for the boot display,
535 * then for the other framebuffers. Only fail for the boot display;
536 * ignore errors for the rest.
538 for_each_node_by_type(node
, "display") {
539 if (!of_get_property(node
, "linux,opened", NULL
) ||
540 !of_get_property(node
, "linux,boot-display", NULL
))
542 dev
= of_platform_device_create(node
, "of-display", NULL
);
550 for_each_node_by_type(node
, "display") {
552 const char *of_display_format
= "of-display.%d";
554 if (!of_get_property(node
, "linux,opened", NULL
) || node
== boot_display
)
556 ret
= snprintf(buf
, sizeof(buf
), of_display_format
, display_number
++);
557 if (ret
< sizeof(buf
))
558 of_platform_device_create(node
, buf
, NULL
);
563 * Handle certain compatibles explicitly, since we don't want to create
564 * platform_devices for every node in /reserved-memory with a
567 for_each_matching_node(node
, reserved_mem_matches
)
568 of_platform_device_create(node
, NULL
, NULL
);
570 node
= of_find_node_by_path("/firmware");
572 of_platform_populate(node
, NULL
, NULL
, NULL
);
576 node
= of_get_compatible_child(of_chosen
, "simple-framebuffer");
579 * Since a "simple-framebuffer" device is already added
580 * here, disable the Generic System Framebuffers (sysfb)
581 * to prevent it from registering another device for the
582 * system framebuffer later (e.g: using the screen_info
583 * data that may had been filled as well).
585 * This can happen for example on DT systems that do EFI
586 * booting and may provide a GOP handle to the EFI stub.
589 of_platform_device_create(node
, NULL
, NULL
);
593 /* Populate everything else. */
594 of_platform_default_populate(NULL
, NULL
, NULL
);
599 arch_initcall_sync(of_platform_default_populate_init
);
601 static int __init
of_platform_sync_state_init(void)
603 device_links_supplier_sync_state_resume();
606 late_initcall_sync(of_platform_sync_state_init
);
608 int of_platform_device_destroy(struct device
*dev
, void *data
)
610 /* Do not touch devices not populated from the device tree */
611 if (!dev
->of_node
|| !of_node_check_flag(dev
->of_node
, OF_POPULATED
))
614 /* Recurse for any nodes that were treated as busses */
615 if (of_node_check_flag(dev
->of_node
, OF_POPULATED_BUS
))
616 device_for_each_child(dev
, NULL
, of_platform_device_destroy
);
618 of_node_clear_flag(dev
->of_node
, OF_POPULATED
);
619 of_node_clear_flag(dev
->of_node
, OF_POPULATED_BUS
);
621 if (dev
->bus
== &platform_bus_type
)
622 platform_device_unregister(to_platform_device(dev
));
623 #ifdef CONFIG_ARM_AMBA
624 else if (dev
->bus
== &amba_bustype
)
625 amba_device_unregister(to_amba_device(dev
));
630 EXPORT_SYMBOL_GPL(of_platform_device_destroy
);
633 * of_platform_depopulate() - Remove devices populated from device tree
634 * @parent: device which children will be removed
636 * Complementary to of_platform_populate(), this function removes children
637 * of the given device (and, recursively, their children) that have been
638 * created from their respective device tree nodes (and only those,
639 * leaving others - eg. manually created - unharmed).
641 void of_platform_depopulate(struct device
*parent
)
643 if (parent
->of_node
&& of_node_check_flag(parent
->of_node
, OF_POPULATED_BUS
)) {
644 device_for_each_child_reverse(parent
, NULL
, of_platform_device_destroy
);
645 of_node_clear_flag(parent
->of_node
, OF_POPULATED_BUS
);
648 EXPORT_SYMBOL_GPL(of_platform_depopulate
);
650 static void devm_of_platform_populate_release(struct device
*dev
, void *res
)
652 of_platform_depopulate(*(struct device
**)res
);
656 * devm_of_platform_populate() - Populate platform_devices from device tree data
657 * @dev: device that requested to populate from device tree data
659 * Similar to of_platform_populate(), but will automatically call
660 * of_platform_depopulate() when the device is unbound from the bus.
662 * Return: 0 on success, < 0 on failure.
664 int devm_of_platform_populate(struct device
*dev
)
672 ptr
= devres_alloc(devm_of_platform_populate_release
,
673 sizeof(*ptr
), GFP_KERNEL
);
677 ret
= of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
682 devres_add(dev
, ptr
);
687 EXPORT_SYMBOL_GPL(devm_of_platform_populate
);
689 static int devm_of_platform_match(struct device
*dev
, void *res
, void *data
)
691 struct device
**ptr
= res
;
702 * devm_of_platform_depopulate() - Remove devices populated from device tree
703 * @dev: device that requested to depopulate from device tree data
705 * Complementary to devm_of_platform_populate(), this function removes children
706 * of the given device (and, recursively, their children) that have been
707 * created from their respective device tree nodes (and only those,
708 * leaving others - eg. manually created - unharmed).
710 void devm_of_platform_depopulate(struct device
*dev
)
714 ret
= devres_release(dev
, devm_of_platform_populate_release
,
715 devm_of_platform_match
, dev
);
719 EXPORT_SYMBOL_GPL(devm_of_platform_depopulate
);
721 #ifdef CONFIG_OF_DYNAMIC
722 static int of_platform_notify(struct notifier_block
*nb
,
723 unsigned long action
, void *arg
)
725 struct of_reconfig_data
*rd
= arg
;
726 struct platform_device
*pdev_parent
, *pdev
;
728 struct device_node
*parent
;
730 switch (of_reconfig_get_state_change(action
, rd
)) {
731 case OF_RECONFIG_CHANGE_ADD
:
732 parent
= rd
->dn
->parent
;
733 /* verify that the parent is a bus (or the root node) */
734 if (!of_node_is_root(parent
) &&
735 !of_node_check_flag(parent
, OF_POPULATED_BUS
))
736 return NOTIFY_OK
; /* not for us */
738 /* already populated? (driver using of_populate manually) */
739 if (of_node_check_flag(rd
->dn
, OF_POPULATED
))
743 * Clear the flag before adding the device so that fw_devlink
744 * doesn't skip adding consumers to this device.
746 rd
->dn
->fwnode
.flags
&= ~FWNODE_FLAG_NOT_DEVICE
;
747 /* pdev_parent may be NULL when no bus platform device */
748 pdev_parent
= of_find_device_by_node(parent
);
749 pdev
= of_platform_device_create(rd
->dn
, NULL
,
750 pdev_parent
? &pdev_parent
->dev
: NULL
);
751 platform_device_put(pdev_parent
);
754 pr_err("%s: failed to create for '%pOF'\n",
756 /* of_platform_device_create tosses the error code */
757 return notifier_from_errno(-EINVAL
);
761 case OF_RECONFIG_CHANGE_REMOVE
:
763 /* already depopulated? */
764 if (!of_node_check_flag(rd
->dn
, OF_POPULATED
))
767 /* find our device by node */
768 pdev
= of_find_device_by_node(rd
->dn
);
770 return NOTIFY_OK
; /* no? not meant for us */
772 /* unregister takes one ref away */
773 of_platform_device_destroy(&pdev
->dev
, &children_left
);
775 /* and put the reference of the find */
776 platform_device_put(pdev
);
783 static struct notifier_block platform_of_notifier
= {
784 .notifier_call
= of_platform_notify
,
787 void of_platform_register_reconfig_notifier(void)
789 WARN_ON(of_reconfig_notifier_register(&platform_of_notifier
));
791 #endif /* CONFIG_OF_DYNAMIC */
793 #endif /* CONFIG_OF_ADDRESS */