2 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
3 * <benh@kernel.crashing.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/slab.h>
21 #include <asm/errno.h>
23 #include <asm/of_device.h>
24 #include <asm/of_platform.h>
25 #include <asm/topology.h>
28 * The list of OF IDs below is used for matching bus types in the
29 * system whose devices are to be exposed as of_platform_devices.
31 * This is the default list valid for most platforms. This file provides
32 * functions who can take an explicit list if necessary though
34 * The search is always performed recursively looking for children of
35 * the provided device_node and recursively if such a children matches
36 * a bus type in the list
39 static struct of_device_id of_default_bus_ids[] = {
41 { .compatible = "soc", },
42 { .type = "spider", },
52 * OF platform device type definition & base infrastructure
56 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
58 struct of_device * of_dev = to_of_device(dev);
59 struct of_platform_driver * of_drv = to_of_platform_driver(drv);
60 const struct of_device_id * matches = of_drv->match_table;
65 return of_match_device(matches, of_dev) != NULL;
68 static int of_platform_device_probe(struct device *dev)
71 struct of_platform_driver *drv;
72 struct of_device *of_dev;
73 const struct of_device_id *match;
75 drv = to_of_platform_driver(dev->driver);
76 of_dev = to_of_device(dev);
83 match = of_match_device(drv->match_table, of_dev);
85 error = drv->probe(of_dev, match);
92 static int of_platform_device_remove(struct device *dev)
94 struct of_device * of_dev = to_of_device(dev);
95 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
97 if (dev->driver && drv->remove)
102 static int of_platform_device_suspend(struct device *dev, pm_message_t state)
104 struct of_device * of_dev = to_of_device(dev);
105 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
108 if (dev->driver && drv->suspend)
109 error = drv->suspend(of_dev, state);
113 static int of_platform_device_resume(struct device * dev)
115 struct of_device * of_dev = to_of_device(dev);
116 struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
119 if (dev->driver && drv->resume)
120 error = drv->resume(of_dev);
124 struct bus_type of_platform_bus_type = {
125 .name = "of_platform",
126 .match = of_platform_bus_match,
127 .probe = of_platform_device_probe,
128 .remove = of_platform_device_remove,
129 .suspend = of_platform_device_suspend,
130 .resume = of_platform_device_resume,
132 EXPORT_SYMBOL(of_platform_bus_type);
134 static int __init of_bus_driver_init(void)
136 return bus_register(&of_platform_bus_type);
139 postcore_initcall(of_bus_driver_init);
141 int of_register_platform_driver(struct of_platform_driver *drv)
143 /* initialize common driver fields */
144 drv->driver.name = drv->name;
145 drv->driver.bus = &of_platform_bus_type;
147 /* register with core */
148 return driver_register(&drv->driver);
150 EXPORT_SYMBOL(of_register_platform_driver);
152 void of_unregister_platform_driver(struct of_platform_driver *drv)
154 driver_unregister(&drv->driver);
156 EXPORT_SYMBOL(of_unregister_platform_driver);
158 static void of_platform_make_bus_id(struct of_device *dev)
160 struct device_node *node = dev->node;
161 char *name = dev->dev.bus_id;
166 * If it's a DCR based device, use 'd' for native DCRs
167 * and 'D' for MMIO DCRs.
169 #ifdef CONFIG_PPC_DCR
170 reg = get_property(node, "dcr-reg", NULL);
172 #ifdef CONFIG_PPC_DCR_NATIVE
173 snprintf(name, BUS_ID_SIZE, "d%x.%s",
175 #else /* CONFIG_PPC_DCR_NATIVE */
176 addr = of_translate_dcr_address(node, *reg, NULL);
177 if (addr != OF_BAD_ADDR) {
178 snprintf(name, BUS_ID_SIZE,
179 "D%llx.%s", (unsigned long long)addr,
183 #endif /* !CONFIG_PPC_DCR_NATIVE */
185 #endif /* CONFIG_PPC_DCR */
188 * For MMIO, get the physical address
190 reg = get_property(node, "reg", NULL);
192 addr = of_translate_address(node, reg);
193 if (addr != OF_BAD_ADDR) {
194 snprintf(name, BUS_ID_SIZE,
195 "%llx.%s", (unsigned long long)addr,
202 * No BusID, use the node name and pray
204 snprintf(name, BUS_ID_SIZE, "%s", node->name);
207 struct of_device* of_platform_device_create(struct device_node *np,
209 struct device *parent)
211 struct of_device *dev;
213 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
216 memset(dev, 0, sizeof(*dev));
218 dev->node = of_node_get(np);
219 dev->dma_mask = 0xffffffffUL;
220 dev->dev.dma_mask = &dev->dma_mask;
221 dev->dev.parent = parent;
222 dev->dev.bus = &of_platform_bus_type;
223 dev->dev.release = of_release_dev;
224 dev->dev.archdata.of_node = np;
225 dev->dev.archdata.numa_node = of_node_to_nid(np);
227 /* We do not fill the DMA ops for platform devices by default.
228 * This is currently the responsibility of the platform code
229 * to do such, possibly using a device notifier
233 strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
235 of_platform_make_bus_id(dev);
237 if (of_device_register(dev) != 0) {
244 EXPORT_SYMBOL(of_platform_device_create);
249 * of_platform_bus_create - Create an OF device for a bus node and all its
250 * children. Optionally recursively instanciate matching busses.
251 * @bus: device node of the bus to instanciate
252 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
253 * disallow recursive creation of child busses
255 static int of_platform_bus_create(struct device_node *bus,
256 struct of_device_id *matches,
257 struct device *parent)
259 struct device_node *child;
260 struct of_device *dev;
263 for (child = NULL; (child = of_get_next_child(bus, child)); ) {
264 pr_debug(" create child: %s\n", child->full_name);
265 dev = of_platform_device_create(child, NULL, parent);
268 else if (!of_match_node(matches, child))
271 pr_debug(" and sub busses\n");
272 rc = of_platform_bus_create(child, matches, &dev->dev);
282 * of_platform_bus_probe - Probe the device-tree for platform busses
283 * @root: parent of the first level to probe or NULL for the root of the tree
284 * @matches: match table, NULL to use the default
285 * @parent: parent to hook devices from, NULL for toplevel
287 * Note that children of the provided root are not instanciated as devices
288 * unless the specified root itself matches the bus list and is not NULL.
291 int of_platform_bus_probe(struct device_node *root,
292 struct of_device_id *matches,
293 struct device *parent)
295 struct device_node *child;
296 struct of_device *dev;
300 matches = of_default_bus_ids;
301 if (matches == OF_NO_DEEP_PROBE)
304 root = of_find_node_by_path("/");
308 pr_debug("of_platform_bus_probe()\n");
309 pr_debug(" starting at: %s\n", root->full_name);
311 /* Do a self check of bus type, if there's a match, create
314 if (of_match_node(matches, root)) {
315 pr_debug(" root match, create all sub devices\n");
316 dev = of_platform_device_create(root, NULL, parent);
321 pr_debug(" create all sub busses\n");
322 rc = of_platform_bus_create(root, matches, &dev->dev);
325 for (child = NULL; (child = of_get_next_child(root, child)); ) {
326 if (!of_match_node(matches, child))
329 pr_debug(" match: %s\n", child->full_name);
330 dev = of_platform_device_create(child, NULL, parent);
334 rc = of_platform_bus_create(child, matches, &dev->dev);
344 EXPORT_SYMBOL(of_platform_bus_probe);
346 static int of_dev_node_match(struct device *dev, void *data)
348 return to_of_device(dev)->node == data;
351 struct of_device *of_find_device_by_node(struct device_node *np)
355 dev = bus_find_device(&of_platform_bus_type,
356 NULL, np, of_dev_node_match);
358 return to_of_device(dev);
361 EXPORT_SYMBOL(of_find_device_by_node);
363 static int of_dev_phandle_match(struct device *dev, void *data)
366 return to_of_device(dev)->node->linux_phandle == *ph;
369 struct of_device *of_find_device_by_phandle(phandle ph)
373 dev = bus_find_device(&of_platform_bus_type,
374 NULL, &ph, of_dev_phandle_match);
376 return to_of_device(dev);
379 EXPORT_SYMBOL(of_find_device_by_phandle);