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_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
25 static int of_dev_node_match(struct device *dev, void *data)
27 return dev->of_node == data;
31 * of_find_device_by_node - Find the platform_device associated with a node
32 * @np: Pointer to device tree node
34 * Returns 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(&platform_bus_type, NULL, np, of_dev_node_match);
41 return dev ? to_platform_device(dev) : NULL;
43 EXPORT_SYMBOL(of_find_device_by_node);
45 static int platform_driver_probe_shim(struct platform_device *pdev)
47 struct platform_driver *pdrv;
48 struct of_platform_driver *ofpdrv;
49 const struct of_device_id *match;
51 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
52 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
54 /* There is an unlikely chance that an of_platform driver might match
55 * on a non-OF platform device. If so, then of_match_device() will
56 * come up empty. Return -EINVAL in this case so other drivers get
57 * the chance to bind. */
58 match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
59 return match ? ofpdrv->probe(pdev, match) : -EINVAL;
62 static void platform_driver_shutdown_shim(struct platform_device *pdev)
64 struct platform_driver *pdrv;
65 struct of_platform_driver *ofpdrv;
67 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
68 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
69 ofpdrv->shutdown(pdev);
73 * of_register_platform_driver
75 int of_register_platform_driver(struct of_platform_driver *drv)
77 /* setup of_platform_driver to platform_driver adaptors */
78 drv->platform_driver.driver = drv->driver;
80 drv->platform_driver.probe = platform_driver_probe_shim;
81 drv->platform_driver.remove = drv->remove;
83 drv->platform_driver.shutdown = platform_driver_shutdown_shim;
84 drv->platform_driver.suspend = drv->suspend;
85 drv->platform_driver.resume = drv->resume;
87 return platform_driver_register(&drv->platform_driver);
89 EXPORT_SYMBOL(of_register_platform_driver);
91 void of_unregister_platform_driver(struct of_platform_driver *drv)
93 platform_driver_unregister(&drv->platform_driver);
95 EXPORT_SYMBOL(of_unregister_platform_driver);
97 #if defined(CONFIG_PPC_DCR)
101 extern struct device_attribute of_platform_device_attrs[];
103 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
105 const struct of_device_id *matches = drv->of_match_table;
110 return of_match_device(matches, dev) != NULL;
113 static int of_platform_device_probe(struct device *dev)
116 struct of_platform_driver *drv;
117 struct platform_device *of_dev;
118 const struct of_device_id *match;
120 drv = to_of_platform_driver(dev->driver);
121 of_dev = to_platform_device(dev);
128 match = of_match_device(drv->driver.of_match_table, dev);
130 error = drv->probe(of_dev, match);
137 static int of_platform_device_remove(struct device *dev)
139 struct platform_device *of_dev = to_platform_device(dev);
140 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
142 if (dev->driver && drv->remove)
147 static void of_platform_device_shutdown(struct device *dev)
149 struct platform_device *of_dev = to_platform_device(dev);
150 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
152 if (dev->driver && drv->shutdown)
153 drv->shutdown(of_dev);
156 #ifdef CONFIG_PM_SLEEP
158 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
160 struct platform_device *of_dev = to_platform_device(dev);
161 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
164 if (dev->driver && drv->suspend)
165 ret = drv->suspend(of_dev, mesg);
169 static int of_platform_legacy_resume(struct device *dev)
171 struct platform_device *of_dev = to_platform_device(dev);
172 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
175 if (dev->driver && drv->resume)
176 ret = drv->resume(of_dev);
180 static int of_platform_pm_prepare(struct device *dev)
182 struct device_driver *drv = dev->driver;
185 if (drv && drv->pm && drv->pm->prepare)
186 ret = drv->pm->prepare(dev);
191 static void of_platform_pm_complete(struct device *dev)
193 struct device_driver *drv = dev->driver;
195 if (drv && drv->pm && drv->pm->complete)
196 drv->pm->complete(dev);
199 #ifdef CONFIG_SUSPEND
201 static int of_platform_pm_suspend(struct device *dev)
203 struct device_driver *drv = dev->driver;
210 if (drv->pm->suspend)
211 ret = drv->pm->suspend(dev);
213 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
219 static int of_platform_pm_suspend_noirq(struct device *dev)
221 struct device_driver *drv = dev->driver;
228 if (drv->pm->suspend_noirq)
229 ret = drv->pm->suspend_noirq(dev);
235 static int of_platform_pm_resume(struct device *dev)
237 struct device_driver *drv = dev->driver;
245 ret = drv->pm->resume(dev);
247 ret = of_platform_legacy_resume(dev);
253 static int of_platform_pm_resume_noirq(struct device *dev)
255 struct device_driver *drv = dev->driver;
262 if (drv->pm->resume_noirq)
263 ret = drv->pm->resume_noirq(dev);
269 #else /* !CONFIG_SUSPEND */
271 #define of_platform_pm_suspend NULL
272 #define of_platform_pm_resume NULL
273 #define of_platform_pm_suspend_noirq NULL
274 #define of_platform_pm_resume_noirq NULL
276 #endif /* !CONFIG_SUSPEND */
278 #ifdef CONFIG_HIBERNATION
280 static int of_platform_pm_freeze(struct device *dev)
282 struct device_driver *drv = dev->driver;
290 ret = drv->pm->freeze(dev);
292 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
298 static int of_platform_pm_freeze_noirq(struct device *dev)
300 struct device_driver *drv = dev->driver;
307 if (drv->pm->freeze_noirq)
308 ret = drv->pm->freeze_noirq(dev);
314 static int of_platform_pm_thaw(struct device *dev)
316 struct device_driver *drv = dev->driver;
324 ret = drv->pm->thaw(dev);
326 ret = of_platform_legacy_resume(dev);
332 static int of_platform_pm_thaw_noirq(struct device *dev)
334 struct device_driver *drv = dev->driver;
341 if (drv->pm->thaw_noirq)
342 ret = drv->pm->thaw_noirq(dev);
348 static int of_platform_pm_poweroff(struct device *dev)
350 struct device_driver *drv = dev->driver;
357 if (drv->pm->poweroff)
358 ret = drv->pm->poweroff(dev);
360 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
366 static int of_platform_pm_poweroff_noirq(struct device *dev)
368 struct device_driver *drv = dev->driver;
375 if (drv->pm->poweroff_noirq)
376 ret = drv->pm->poweroff_noirq(dev);
382 static int of_platform_pm_restore(struct device *dev)
384 struct device_driver *drv = dev->driver;
391 if (drv->pm->restore)
392 ret = drv->pm->restore(dev);
394 ret = of_platform_legacy_resume(dev);
400 static int of_platform_pm_restore_noirq(struct device *dev)
402 struct device_driver *drv = dev->driver;
409 if (drv->pm->restore_noirq)
410 ret = drv->pm->restore_noirq(dev);
416 #else /* !CONFIG_HIBERNATION */
418 #define of_platform_pm_freeze NULL
419 #define of_platform_pm_thaw NULL
420 #define of_platform_pm_poweroff NULL
421 #define of_platform_pm_restore NULL
422 #define of_platform_pm_freeze_noirq NULL
423 #define of_platform_pm_thaw_noirq NULL
424 #define of_platform_pm_poweroff_noirq NULL
425 #define of_platform_pm_restore_noirq NULL
427 #endif /* !CONFIG_HIBERNATION */
429 static struct dev_pm_ops of_platform_dev_pm_ops = {
430 .prepare = of_platform_pm_prepare,
431 .complete = of_platform_pm_complete,
432 .suspend = of_platform_pm_suspend,
433 .resume = of_platform_pm_resume,
434 .freeze = of_platform_pm_freeze,
435 .thaw = of_platform_pm_thaw,
436 .poweroff = of_platform_pm_poweroff,
437 .restore = of_platform_pm_restore,
438 .suspend_noirq = of_platform_pm_suspend_noirq,
439 .resume_noirq = of_platform_pm_resume_noirq,
440 .freeze_noirq = of_platform_pm_freeze_noirq,
441 .thaw_noirq = of_platform_pm_thaw_noirq,
442 .poweroff_noirq = of_platform_pm_poweroff_noirq,
443 .restore_noirq = of_platform_pm_restore_noirq,
446 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
448 #else /* !CONFIG_PM_SLEEP */
450 #define OF_PLATFORM_PM_OPS_PTR NULL
452 #endif /* !CONFIG_PM_SLEEP */
454 int of_bus_type_init(struct bus_type *bus, const char *name)
457 bus->match = of_platform_bus_match;
458 bus->probe = of_platform_device_probe;
459 bus->remove = of_platform_device_remove;
460 bus->shutdown = of_platform_device_shutdown;
461 bus->dev_attrs = of_platform_device_attrs;
462 bus->pm = OF_PLATFORM_PM_OPS_PTR;
463 return bus_register(bus);
466 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
469 * Temporary: of_platform_bus used to be distinct from the platform
470 * bus. It isn't anymore, and so drivers on the platform bus need
471 * to be registered in a special way.
473 * After all of_platform_bus_type drivers are converted to
474 * platform_drivers, this exception can be removed.
476 if (bus == &platform_bus_type)
477 return of_register_platform_driver(drv);
479 /* register with core */
480 drv->driver.bus = bus;
481 return driver_register(&drv->driver);
483 EXPORT_SYMBOL(of_register_driver);
485 void of_unregister_driver(struct of_platform_driver *drv)
487 if (drv->driver.bus == &platform_bus_type)
488 of_unregister_platform_driver(drv);
490 driver_unregister(&drv->driver);
492 EXPORT_SYMBOL(of_unregister_driver);
494 #if !defined(CONFIG_SPARC)
496 * The following routines scan a subtree and registers a device for
497 * each applicable node.
499 * Note: sparc doesn't use these routines because it has a different
500 * mechanism for creating devices from device tree nodes.
504 * of_device_make_bus_id - Use the device node data to assign a unique name
505 * @dev: pointer to device structure that is linked to a device tree node
507 * This routine will first try using either the dcr-reg or the reg property
508 * value to derive a unique name. As a last resort it will use the node
509 * name followed by a unique number.
511 void of_device_make_bus_id(struct device *dev)
513 static atomic_t bus_no_reg_magic;
514 struct device_node *node = dev->of_node;
519 #ifdef CONFIG_PPC_DCR
521 * If it's a DCR based device, use 'd' for native DCRs
522 * and 'D' for MMIO DCRs.
524 reg = of_get_property(node, "dcr-reg", NULL);
526 #ifdef CONFIG_PPC_DCR_NATIVE
527 dev_set_name(dev, "d%x.%s", *reg, node->name);
528 #else /* CONFIG_PPC_DCR_NATIVE */
529 u64 addr = of_translate_dcr_address(node, *reg, NULL);
530 if (addr != OF_BAD_ADDR) {
531 dev_set_name(dev, "D%llx.%s",
532 (unsigned long long)addr, node->name);
535 #endif /* !CONFIG_PPC_DCR_NATIVE */
537 #endif /* CONFIG_PPC_DCR */
540 * For MMIO, get the physical address
542 reg = of_get_property(node, "reg", NULL);
544 addr = of_translate_address(node, reg);
545 if (addr != OF_BAD_ADDR) {
546 dev_set_name(dev, "%llx.%s",
547 (unsigned long long)addr, node->name);
553 * No BusID, use the node name and add a globally incremented
554 * counter (and pray...)
556 magic = atomic_add_return(1, &bus_no_reg_magic);
557 dev_set_name(dev, "%s.%d", node->name, magic - 1);
561 * of_device_alloc - Allocate and initialize an of_device
562 * @np: device node to assign to device
563 * @bus_id: Name to assign to the device. May be null to use default name.
564 * @parent: Parent device.
566 struct platform_device *of_device_alloc(struct device_node *np,
568 struct device *parent)
570 struct platform_device *dev;
571 int rc, i, num_reg = 0, num_irq = 0;
572 struct resource *res, temp_res;
574 /* First count how many resources are needed */
575 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
577 while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
580 /* Allocate memory for both the struct device and the resource table */
581 dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
585 res = (struct resource *) &dev[1];
587 /* Populate the resource table */
588 if (num_irq || num_reg) {
589 dev->num_resources = num_reg + num_irq;
591 for (i = 0; i < num_reg; i++, res++) {
592 rc = of_address_to_resource(np, i, res);
595 for (i = 0; i < num_irq; i++, res++) {
596 rc = of_irq_to_resource(np, i, res);
597 WARN_ON(rc == NO_IRQ);
601 dev->dev.of_node = of_node_get(np);
602 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
603 dev->dev.dma_mask = &dev->archdata.dma_mask;
605 dev->dev.parent = parent;
606 dev->dev.release = of_release_dev;
609 dev_set_name(&dev->dev, "%s", bus_id);
611 of_device_make_bus_id(&dev->dev);
615 EXPORT_SYMBOL(of_device_alloc);
618 * of_platform_device_create - Alloc, initialize and register an of_device
619 * @np: pointer to node to create device for
620 * @bus_id: name to assign device
621 * @parent: Linux device model parent device.
623 struct platform_device *of_platform_device_create(struct device_node *np,
625 struct device *parent)
627 struct platform_device *dev;
629 dev = of_device_alloc(np, bus_id, parent);
633 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
634 dev->archdata.dma_mask = 0xffffffffUL;
636 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
637 dev->dev.bus = &platform_bus_type;
639 /* We do not fill the DMA ops for platform devices by default.
640 * This is currently the responsibility of the platform code
641 * to do such, possibly using a device notifier
644 if (of_device_register(dev) != 0) {
651 EXPORT_SYMBOL(of_platform_device_create);
654 * of_platform_bus_create - Create an OF device for a bus node and all its
655 * children. Optionally recursively instantiate matching busses.
656 * @bus: device node of the bus to instantiate
657 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
658 * disallow recursive creation of child busses
660 static int of_platform_bus_create(const struct device_node *bus,
661 const struct of_device_id *matches,
662 struct device *parent)
664 struct device_node *child;
665 struct platform_device *dev;
668 for_each_child_of_node(bus, child) {
669 pr_debug(" create child: %s\n", child->full_name);
670 dev = of_platform_device_create(child, NULL, parent);
673 else if (!of_match_node(matches, child))
676 pr_debug(" and sub busses\n");
677 rc = of_platform_bus_create(child, matches, &dev->dev);
688 * of_platform_bus_probe - Probe the device-tree for platform busses
689 * @root: parent of the first level to probe or NULL for the root of the tree
690 * @matches: match table, NULL to use the default
691 * @parent: parent to hook devices from, NULL for toplevel
693 * Note that children of the provided root are not instantiated as devices
694 * unless the specified root itself matches the bus list and is not NULL.
696 int of_platform_bus_probe(struct device_node *root,
697 const struct of_device_id *matches,
698 struct device *parent)
700 struct device_node *child;
701 struct platform_device *dev;
704 if (WARN_ON(!matches || matches == OF_NO_DEEP_PROBE))
707 root = of_find_node_by_path("/");
713 pr_debug("of_platform_bus_probe()\n");
714 pr_debug(" starting at: %s\n", root->full_name);
716 /* Do a self check of bus type, if there's a match, create
719 if (of_match_node(matches, root)) {
720 pr_debug(" root match, create all sub devices\n");
721 dev = of_platform_device_create(root, NULL, parent);
726 pr_debug(" create all sub busses\n");
727 rc = of_platform_bus_create(root, matches, &dev->dev);
730 for_each_child_of_node(root, child) {
731 if (!of_match_node(matches, child))
734 pr_debug(" match: %s\n", child->full_name);
735 dev = of_platform_device_create(child, NULL, parent);
739 rc = of_platform_bus_create(child, matches, &dev->dev);
749 EXPORT_SYMBOL(of_platform_bus_probe);
750 #endif /* !CONFIG_SPARC */