2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
4 * and Arnd Bergmann, IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
15 #include <linux/string.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/slab.h>
21 #include <linux/pci.h>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
26 #include <linux/errno.h>
27 #include <linux/topology.h>
28 #include <asm/atomic.h>
30 struct bus_type of_platform_bus_type
= {
31 .uevent
= of_device_uevent
,
33 EXPORT_SYMBOL(of_platform_bus_type
);
35 static int __init
of_bus_driver_init(void)
37 return of_bus_type_init(&of_platform_bus_type
, "of_platform");
39 postcore_initcall(of_bus_driver_init
);
41 struct of_device
*of_platform_device_create(struct device_node
*np
,
43 struct device
*parent
)
45 struct of_device
*dev
;
47 dev
= of_device_alloc(np
, bus_id
, parent
);
51 dev
->dma_mask
= 0xffffffffUL
;
52 dev
->dev
.bus
= &of_platform_bus_type
;
54 /* We do not fill the DMA ops for platform devices by default.
55 * This is currently the responsibility of the platform code
56 * to do such, possibly using a device notifier
59 if (of_device_register(dev
) != 0) {
66 EXPORT_SYMBOL(of_platform_device_create
);
69 * of_platform_bus_create - Create an OF device for a bus node and all its
70 * children. Optionally recursively instanciate matching busses.
71 * @bus: device node of the bus to instanciate
72 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
73 * disallow recursive creation of child busses
75 static int of_platform_bus_create(const struct device_node
*bus
,
76 const struct of_device_id
*matches
,
77 struct device
*parent
)
79 struct device_node
*child
;
80 struct of_device
*dev
;
83 for_each_child_of_node(bus
, child
) {
84 pr_debug(" create child: %s\n", child
->full_name
);
85 dev
= of_platform_device_create(child
, NULL
, parent
);
88 else if (!of_match_node(matches
, child
))
91 pr_debug(" and sub busses\n");
92 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
104 * of_platform_bus_probe - Probe the device-tree for platform busses
105 * @root: parent of the first level to probe or NULL for the root of the tree
106 * @matches: match table, NULL to use the default
107 * @parent: parent to hook devices from, NULL for toplevel
109 * Note that children of the provided root are not instanciated as devices
110 * unless the specified root itself matches the bus list and is not NULL.
113 int of_platform_bus_probe(struct device_node
*root
,
114 const struct of_device_id
*matches
,
115 struct device
*parent
)
117 struct device_node
*child
;
118 struct of_device
*dev
;
122 matches
= of_default_bus_ids
;
123 if (matches
== OF_NO_DEEP_PROBE
)
126 root
= of_find_node_by_path("/");
130 pr_debug("of_platform_bus_probe()\n");
131 pr_debug(" starting at: %s\n", root
->full_name
);
133 /* Do a self check of bus type, if there's a match, create
136 if (of_match_node(matches
, root
)) {
137 pr_debug(" root match, create all sub devices\n");
138 dev
= of_platform_device_create(root
, NULL
, parent
);
143 pr_debug(" create all sub busses\n");
144 rc
= of_platform_bus_create(root
, matches
, &dev
->dev
);
147 for_each_child_of_node(root
, child
) {
148 if (!of_match_node(matches
, child
))
151 pr_debug(" match: %s\n", child
->full_name
);
152 dev
= of_platform_device_create(child
, NULL
, parent
);
156 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
166 EXPORT_SYMBOL(of_platform_bus_probe
);
168 static int of_dev_node_match(struct device
*dev
, void *data
)
170 return to_of_device(dev
)->node
== data
;
173 struct of_device
*of_find_device_by_node(struct device_node
*np
)
177 dev
= bus_find_device(&of_platform_bus_type
,
178 NULL
, np
, of_dev_node_match
);
180 return to_of_device(dev
);
183 EXPORT_SYMBOL(of_find_device_by_node
);
185 static int of_dev_phandle_match(struct device
*dev
, void *data
)
188 return to_of_device(dev
)->node
->linux_phandle
== *ph
;
191 struct of_device
*of_find_device_by_phandle(phandle ph
)
195 dev
= bus_find_device(&of_platform_bus_type
,
196 NULL
, &ph
, of_dev_phandle_match
);
198 return to_of_device(dev
);
201 EXPORT_SYMBOL(of_find_device_by_phandle
);