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
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.
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/slab.h>
19 #include <linux/of_device.h>
20 #include <linux/of_platform.h>
22 #if defined(CONFIG_PPC_DCR)
26 extern struct device_attribute of_platform_device_attrs[];
28 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
30 const struct of_device_id *matches = drv->of_match_table;
35 return of_match_device(matches, dev) != NULL;
38 static int of_platform_device_probe(struct device *dev)
41 struct of_platform_driver *drv;
42 struct of_device *of_dev;
43 const struct of_device_id *match;
45 drv = to_of_platform_driver(dev->driver);
46 of_dev = to_of_device(dev);
53 match = of_match_device(drv->driver.of_match_table, dev);
55 error = drv->probe(of_dev, match);
62 static int of_platform_device_remove(struct device *dev)
64 struct of_device *of_dev = to_of_device(dev);
65 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
67 if (dev->driver && drv->remove)
72 static void of_platform_device_shutdown(struct device *dev)
74 struct of_device *of_dev = to_of_device(dev);
75 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
77 if (dev->driver && drv->shutdown)
78 drv->shutdown(of_dev);
81 #ifdef CONFIG_PM_SLEEP
83 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
85 struct of_device *of_dev = to_of_device(dev);
86 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
89 if (dev->driver && drv->suspend)
90 ret = drv->suspend(of_dev, mesg);
94 static int of_platform_legacy_resume(struct device *dev)
96 struct of_device *of_dev = to_of_device(dev);
97 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
100 if (dev->driver && drv->resume)
101 ret = drv->resume(of_dev);
105 static int of_platform_pm_prepare(struct device *dev)
107 struct device_driver *drv = dev->driver;
110 if (drv && drv->pm && drv->pm->prepare)
111 ret = drv->pm->prepare(dev);
116 static void of_platform_pm_complete(struct device *dev)
118 struct device_driver *drv = dev->driver;
120 if (drv && drv->pm && drv->pm->complete)
121 drv->pm->complete(dev);
124 #ifdef CONFIG_SUSPEND
126 static int of_platform_pm_suspend(struct device *dev)
128 struct device_driver *drv = dev->driver;
135 if (drv->pm->suspend)
136 ret = drv->pm->suspend(dev);
138 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
144 static int of_platform_pm_suspend_noirq(struct device *dev)
146 struct device_driver *drv = dev->driver;
153 if (drv->pm->suspend_noirq)
154 ret = drv->pm->suspend_noirq(dev);
160 static int of_platform_pm_resume(struct device *dev)
162 struct device_driver *drv = dev->driver;
170 ret = drv->pm->resume(dev);
172 ret = of_platform_legacy_resume(dev);
178 static int of_platform_pm_resume_noirq(struct device *dev)
180 struct device_driver *drv = dev->driver;
187 if (drv->pm->resume_noirq)
188 ret = drv->pm->resume_noirq(dev);
194 #else /* !CONFIG_SUSPEND */
196 #define of_platform_pm_suspend NULL
197 #define of_platform_pm_resume NULL
198 #define of_platform_pm_suspend_noirq NULL
199 #define of_platform_pm_resume_noirq NULL
201 #endif /* !CONFIG_SUSPEND */
203 #ifdef CONFIG_HIBERNATION
205 static int of_platform_pm_freeze(struct device *dev)
207 struct device_driver *drv = dev->driver;
215 ret = drv->pm->freeze(dev);
217 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
223 static int of_platform_pm_freeze_noirq(struct device *dev)
225 struct device_driver *drv = dev->driver;
232 if (drv->pm->freeze_noirq)
233 ret = drv->pm->freeze_noirq(dev);
239 static int of_platform_pm_thaw(struct device *dev)
241 struct device_driver *drv = dev->driver;
249 ret = drv->pm->thaw(dev);
251 ret = of_platform_legacy_resume(dev);
257 static int of_platform_pm_thaw_noirq(struct device *dev)
259 struct device_driver *drv = dev->driver;
266 if (drv->pm->thaw_noirq)
267 ret = drv->pm->thaw_noirq(dev);
273 static int of_platform_pm_poweroff(struct device *dev)
275 struct device_driver *drv = dev->driver;
282 if (drv->pm->poweroff)
283 ret = drv->pm->poweroff(dev);
285 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
291 static int of_platform_pm_poweroff_noirq(struct device *dev)
293 struct device_driver *drv = dev->driver;
300 if (drv->pm->poweroff_noirq)
301 ret = drv->pm->poweroff_noirq(dev);
307 static int of_platform_pm_restore(struct device *dev)
309 struct device_driver *drv = dev->driver;
316 if (drv->pm->restore)
317 ret = drv->pm->restore(dev);
319 ret = of_platform_legacy_resume(dev);
325 static int of_platform_pm_restore_noirq(struct device *dev)
327 struct device_driver *drv = dev->driver;
334 if (drv->pm->restore_noirq)
335 ret = drv->pm->restore_noirq(dev);
341 #else /* !CONFIG_HIBERNATION */
343 #define of_platform_pm_freeze NULL
344 #define of_platform_pm_thaw NULL
345 #define of_platform_pm_poweroff NULL
346 #define of_platform_pm_restore NULL
347 #define of_platform_pm_freeze_noirq NULL
348 #define of_platform_pm_thaw_noirq NULL
349 #define of_platform_pm_poweroff_noirq NULL
350 #define of_platform_pm_restore_noirq NULL
352 #endif /* !CONFIG_HIBERNATION */
354 static struct dev_pm_ops of_platform_dev_pm_ops = {
355 .prepare = of_platform_pm_prepare,
356 .complete = of_platform_pm_complete,
357 .suspend = of_platform_pm_suspend,
358 .resume = of_platform_pm_resume,
359 .freeze = of_platform_pm_freeze,
360 .thaw = of_platform_pm_thaw,
361 .poweroff = of_platform_pm_poweroff,
362 .restore = of_platform_pm_restore,
363 .suspend_noirq = of_platform_pm_suspend_noirq,
364 .resume_noirq = of_platform_pm_resume_noirq,
365 .freeze_noirq = of_platform_pm_freeze_noirq,
366 .thaw_noirq = of_platform_pm_thaw_noirq,
367 .poweroff_noirq = of_platform_pm_poweroff_noirq,
368 .restore_noirq = of_platform_pm_restore_noirq,
371 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
373 #else /* !CONFIG_PM_SLEEP */
375 #define OF_PLATFORM_PM_OPS_PTR NULL
377 #endif /* !CONFIG_PM_SLEEP */
379 int of_bus_type_init(struct bus_type *bus, const char *name)
382 bus->match = of_platform_bus_match;
383 bus->probe = of_platform_device_probe;
384 bus->remove = of_platform_device_remove;
385 bus->shutdown = of_platform_device_shutdown;
386 bus->dev_attrs = of_platform_device_attrs;
387 bus->pm = OF_PLATFORM_PM_OPS_PTR;
388 return bus_register(bus);
391 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
393 drv->driver.bus = bus;
395 /* register with core */
396 return driver_register(&drv->driver);
398 EXPORT_SYMBOL(of_register_driver);
400 void of_unregister_driver(struct of_platform_driver *drv)
402 driver_unregister(&drv->driver);
404 EXPORT_SYMBOL(of_unregister_driver);
406 #if !defined(CONFIG_SPARC)
408 * The following routines scan a subtree and registers a device for
409 * each applicable node.
411 * Note: sparc doesn't use these routines because it has a different
412 * mechanism for creating devices from device tree nodes.
416 * of_device_make_bus_id - Use the device node data to assign a unique name
417 * @dev: pointer to device structure that is linked to a device tree node
419 * This routine will first try using either the dcr-reg or the reg property
420 * value to derive a unique name. As a last resort it will use the node
421 * name followed by a unique number.
423 static void of_device_make_bus_id(struct device *dev)
425 static atomic_t bus_no_reg_magic;
426 struct device_node *node = dev->of_node;
431 #ifdef CONFIG_PPC_DCR
433 * If it's a DCR based device, use 'd' for native DCRs
434 * and 'D' for MMIO DCRs.
436 reg = of_get_property(node, "dcr-reg", NULL);
438 #ifdef CONFIG_PPC_DCR_NATIVE
439 dev_set_name(dev, "d%x.%s", *reg, node->name);
440 #else /* CONFIG_PPC_DCR_NATIVE */
441 u64 addr = of_translate_dcr_address(node, *reg, NULL);
442 if (addr != OF_BAD_ADDR) {
443 dev_set_name(dev, "D%llx.%s",
444 (unsigned long long)addr, node->name);
447 #endif /* !CONFIG_PPC_DCR_NATIVE */
449 #endif /* CONFIG_PPC_DCR */
452 * For MMIO, get the physical address
454 reg = of_get_property(node, "reg", NULL);
456 addr = of_translate_address(node, reg);
457 if (addr != OF_BAD_ADDR) {
458 dev_set_name(dev, "%llx.%s",
459 (unsigned long long)addr, node->name);
465 * No BusID, use the node name and add a globally incremented
466 * counter (and pray...)
468 magic = atomic_add_return(1, &bus_no_reg_magic);
469 dev_set_name(dev, "%s.%d", node->name, magic - 1);
473 * of_device_alloc - Allocate and initialize an of_device
474 * @np: device node to assign to device
475 * @bus_id: Name to assign to the device. May be null to use default name.
476 * @parent: Parent device.
478 struct of_device *of_device_alloc(struct device_node *np,
480 struct device *parent)
482 struct of_device *dev;
483 int rc, i, num_reg = 0, num_irq = 0;
484 struct resource *res, temp_res;
486 /* First count how many resources are needed */
487 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
489 while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
492 /* Allocate memory for both the struct device and the resource table */
493 dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
497 res = (struct resource *) &dev[1];
499 /* Populate the resource table */
500 if (num_irq || num_reg) {
501 dev->num_resources = num_reg + num_irq;
503 for (i = 0; i < num_reg; i++, res++) {
504 rc = of_address_to_resource(np, i, res);
507 for (i = 0; i < num_irq; i++, res++) {
508 rc = of_irq_to_resource(np, i, res);
509 WARN_ON(rc == NO_IRQ);
513 dev->dev.of_node = of_node_get(np);
514 dev->dev.dma_mask = &dev->archdata.dma_mask;
515 dev->dev.parent = parent;
516 dev->dev.release = of_release_dev;
519 dev_set_name(&dev->dev, "%s", bus_id);
521 of_device_make_bus_id(&dev->dev);
525 EXPORT_SYMBOL(of_device_alloc);
528 * of_platform_device_create - Alloc, initialize and register an of_device
529 * @np: pointer to node to create device for
530 * @bus_id: name to assign device
531 * @parent: Linux device model parent device.
533 struct of_device *of_platform_device_create(struct device_node *np,
535 struct device *parent)
537 struct of_device *dev;
539 dev = of_device_alloc(np, bus_id, parent);
543 dev->archdata.dma_mask = 0xffffffffUL;
544 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
545 dev->dev.bus = &of_platform_bus_type;
547 /* We do not fill the DMA ops for platform devices by default.
548 * This is currently the responsibility of the platform code
549 * to do such, possibly using a device notifier
552 if (of_device_register(dev) != 0) {
559 EXPORT_SYMBOL(of_platform_device_create);
562 * of_platform_bus_create - Create an OF device for a bus node and all its
563 * children. Optionally recursively instantiate matching busses.
564 * @bus: device node of the bus to instantiate
565 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
566 * disallow recursive creation of child busses
568 static int of_platform_bus_create(const struct device_node *bus,
569 const struct of_device_id *matches,
570 struct device *parent)
572 struct device_node *child;
573 struct of_device *dev;
576 for_each_child_of_node(bus, child) {
577 pr_debug(" create child: %s\n", child->full_name);
578 dev = of_platform_device_create(child, NULL, parent);
581 else if (!of_match_node(matches, child))
584 pr_debug(" and sub busses\n");
585 rc = of_platform_bus_create(child, matches, &dev->dev);
596 * of_platform_bus_probe - Probe the device-tree for platform busses
597 * @root: parent of the first level to probe or NULL for the root of the tree
598 * @matches: match table, NULL to use the default
599 * @parent: parent to hook devices from, NULL for toplevel
601 * Note that children of the provided root are not instantiated as devices
602 * unless the specified root itself matches the bus list and is not NULL.
604 int of_platform_bus_probe(struct device_node *root,
605 const struct of_device_id *matches,
606 struct device *parent)
608 struct device_node *child;
609 struct of_device *dev;
613 matches = of_default_bus_ids;
614 if (matches == OF_NO_DEEP_PROBE)
617 root = of_find_node_by_path("/");
623 pr_debug("of_platform_bus_probe()\n");
624 pr_debug(" starting at: %s\n", root->full_name);
626 /* Do a self check of bus type, if there's a match, create
629 if (of_match_node(matches, root)) {
630 pr_debug(" root match, create all sub devices\n");
631 dev = of_platform_device_create(root, NULL, parent);
636 pr_debug(" create all sub busses\n");
637 rc = of_platform_bus_create(root, matches, &dev->dev);
640 for_each_child_of_node(root, child) {
641 if (!of_match_node(matches, child))
644 pr_debug(" match: %s\n", child->full_name);
645 dev = of_platform_device_create(child, NULL, parent);
649 rc = of_platform_bus_create(child, matches, &dev->dev);
659 EXPORT_SYMBOL(of_platform_bus_probe);
660 #endif /* !CONFIG_SPARC */