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 platform_driver_probe_shim(struct platform_device *pdev)
27 struct platform_driver *pdrv;
28 struct of_platform_driver *ofpdrv;
29 const struct of_device_id *match;
31 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
32 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
33 match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
34 return ofpdrv->probe(pdev, match);
37 static void platform_driver_shutdown_shim(struct platform_device *pdev)
39 struct platform_driver *pdrv;
40 struct of_platform_driver *ofpdrv;
42 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
43 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
44 ofpdrv->shutdown(pdev);
48 * of_register_platform_driver
50 int of_register_platform_driver(struct of_platform_driver *drv)
52 /* setup of_platform_driver to platform_driver adaptors */
53 drv->platform_driver.driver = drv->driver;
55 drv->platform_driver.probe = platform_driver_probe_shim;
56 drv->platform_driver.remove = drv->remove;
58 drv->platform_driver.shutdown = platform_driver_shutdown_shim;
59 drv->platform_driver.suspend = drv->suspend;
60 drv->platform_driver.resume = drv->resume;
62 return platform_driver_register(&drv->platform_driver);
64 EXPORT_SYMBOL(of_register_platform_driver);
66 void of_unregister_platform_driver(struct of_platform_driver *drv)
68 platform_driver_unregister(&drv->platform_driver);
70 EXPORT_SYMBOL(of_unregister_platform_driver);
72 #if defined(CONFIG_PPC_DCR)
76 extern struct device_attribute of_platform_device_attrs[];
78 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
80 const struct of_device_id *matches = drv->of_match_table;
85 return of_match_device(matches, dev) != NULL;
88 static int of_platform_device_probe(struct device *dev)
91 struct of_platform_driver *drv;
92 struct of_device *of_dev;
93 const struct of_device_id *match;
95 drv = to_of_platform_driver(dev->driver);
96 of_dev = to_of_device(dev);
103 match = of_match_device(drv->driver.of_match_table, dev);
105 error = drv->probe(of_dev, match);
112 static int of_platform_device_remove(struct device *dev)
114 struct of_device *of_dev = to_of_device(dev);
115 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
117 if (dev->driver && drv->remove)
122 static void of_platform_device_shutdown(struct device *dev)
124 struct of_device *of_dev = to_of_device(dev);
125 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
127 if (dev->driver && drv->shutdown)
128 drv->shutdown(of_dev);
131 #ifdef CONFIG_PM_SLEEP
133 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
135 struct of_device *of_dev = to_of_device(dev);
136 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
139 if (dev->driver && drv->suspend)
140 ret = drv->suspend(of_dev, mesg);
144 static int of_platform_legacy_resume(struct device *dev)
146 struct of_device *of_dev = to_of_device(dev);
147 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
150 if (dev->driver && drv->resume)
151 ret = drv->resume(of_dev);
155 static int of_platform_pm_prepare(struct device *dev)
157 struct device_driver *drv = dev->driver;
160 if (drv && drv->pm && drv->pm->prepare)
161 ret = drv->pm->prepare(dev);
166 static void of_platform_pm_complete(struct device *dev)
168 struct device_driver *drv = dev->driver;
170 if (drv && drv->pm && drv->pm->complete)
171 drv->pm->complete(dev);
174 #ifdef CONFIG_SUSPEND
176 static int of_platform_pm_suspend(struct device *dev)
178 struct device_driver *drv = dev->driver;
185 if (drv->pm->suspend)
186 ret = drv->pm->suspend(dev);
188 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
194 static int of_platform_pm_suspend_noirq(struct device *dev)
196 struct device_driver *drv = dev->driver;
203 if (drv->pm->suspend_noirq)
204 ret = drv->pm->suspend_noirq(dev);
210 static int of_platform_pm_resume(struct device *dev)
212 struct device_driver *drv = dev->driver;
220 ret = drv->pm->resume(dev);
222 ret = of_platform_legacy_resume(dev);
228 static int of_platform_pm_resume_noirq(struct device *dev)
230 struct device_driver *drv = dev->driver;
237 if (drv->pm->resume_noirq)
238 ret = drv->pm->resume_noirq(dev);
244 #else /* !CONFIG_SUSPEND */
246 #define of_platform_pm_suspend NULL
247 #define of_platform_pm_resume NULL
248 #define of_platform_pm_suspend_noirq NULL
249 #define of_platform_pm_resume_noirq NULL
251 #endif /* !CONFIG_SUSPEND */
253 #ifdef CONFIG_HIBERNATION
255 static int of_platform_pm_freeze(struct device *dev)
257 struct device_driver *drv = dev->driver;
265 ret = drv->pm->freeze(dev);
267 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
273 static int of_platform_pm_freeze_noirq(struct device *dev)
275 struct device_driver *drv = dev->driver;
282 if (drv->pm->freeze_noirq)
283 ret = drv->pm->freeze_noirq(dev);
289 static int of_platform_pm_thaw(struct device *dev)
291 struct device_driver *drv = dev->driver;
299 ret = drv->pm->thaw(dev);
301 ret = of_platform_legacy_resume(dev);
307 static int of_platform_pm_thaw_noirq(struct device *dev)
309 struct device_driver *drv = dev->driver;
316 if (drv->pm->thaw_noirq)
317 ret = drv->pm->thaw_noirq(dev);
323 static int of_platform_pm_poweroff(struct device *dev)
325 struct device_driver *drv = dev->driver;
332 if (drv->pm->poweroff)
333 ret = drv->pm->poweroff(dev);
335 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
341 static int of_platform_pm_poweroff_noirq(struct device *dev)
343 struct device_driver *drv = dev->driver;
350 if (drv->pm->poweroff_noirq)
351 ret = drv->pm->poweroff_noirq(dev);
357 static int of_platform_pm_restore(struct device *dev)
359 struct device_driver *drv = dev->driver;
366 if (drv->pm->restore)
367 ret = drv->pm->restore(dev);
369 ret = of_platform_legacy_resume(dev);
375 static int of_platform_pm_restore_noirq(struct device *dev)
377 struct device_driver *drv = dev->driver;
384 if (drv->pm->restore_noirq)
385 ret = drv->pm->restore_noirq(dev);
391 #else /* !CONFIG_HIBERNATION */
393 #define of_platform_pm_freeze NULL
394 #define of_platform_pm_thaw NULL
395 #define of_platform_pm_poweroff NULL
396 #define of_platform_pm_restore NULL
397 #define of_platform_pm_freeze_noirq NULL
398 #define of_platform_pm_thaw_noirq NULL
399 #define of_platform_pm_poweroff_noirq NULL
400 #define of_platform_pm_restore_noirq NULL
402 #endif /* !CONFIG_HIBERNATION */
404 static struct dev_pm_ops of_platform_dev_pm_ops = {
405 .prepare = of_platform_pm_prepare,
406 .complete = of_platform_pm_complete,
407 .suspend = of_platform_pm_suspend,
408 .resume = of_platform_pm_resume,
409 .freeze = of_platform_pm_freeze,
410 .thaw = of_platform_pm_thaw,
411 .poweroff = of_platform_pm_poweroff,
412 .restore = of_platform_pm_restore,
413 .suspend_noirq = of_platform_pm_suspend_noirq,
414 .resume_noirq = of_platform_pm_resume_noirq,
415 .freeze_noirq = of_platform_pm_freeze_noirq,
416 .thaw_noirq = of_platform_pm_thaw_noirq,
417 .poweroff_noirq = of_platform_pm_poweroff_noirq,
418 .restore_noirq = of_platform_pm_restore_noirq,
421 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
423 #else /* !CONFIG_PM_SLEEP */
425 #define OF_PLATFORM_PM_OPS_PTR NULL
427 #endif /* !CONFIG_PM_SLEEP */
429 int of_bus_type_init(struct bus_type *bus, const char *name)
432 bus->match = of_platform_bus_match;
433 bus->probe = of_platform_device_probe;
434 bus->remove = of_platform_device_remove;
435 bus->shutdown = of_platform_device_shutdown;
436 bus->dev_attrs = of_platform_device_attrs;
437 bus->pm = OF_PLATFORM_PM_OPS_PTR;
438 return bus_register(bus);
441 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
444 * Temporary: of_platform_bus used to be distinct from the platform
445 * bus. It isn't anymore, and so drivers on the platform bus need
446 * to be registered in a special way.
448 * After all of_platform_bus_type drivers are converted to
449 * platform_drivers, this exception can be removed.
451 if (bus == &platform_bus_type)
452 return of_register_platform_driver(drv);
454 /* register with core */
455 drv->driver.bus = bus;
456 return driver_register(&drv->driver);
458 EXPORT_SYMBOL(of_register_driver);
460 void of_unregister_driver(struct of_platform_driver *drv)
462 if (drv->driver.bus == &platform_bus_type)
463 of_unregister_platform_driver(drv);
465 driver_unregister(&drv->driver);
467 EXPORT_SYMBOL(of_unregister_driver);
469 #if !defined(CONFIG_SPARC)
471 * The following routines scan a subtree and registers a device for
472 * each applicable node.
474 * Note: sparc doesn't use these routines because it has a different
475 * mechanism for creating devices from device tree nodes.
479 * of_device_make_bus_id - Use the device node data to assign a unique name
480 * @dev: pointer to device structure that is linked to a device tree node
482 * This routine will first try using either the dcr-reg or the reg property
483 * value to derive a unique name. As a last resort it will use the node
484 * name followed by a unique number.
486 static void of_device_make_bus_id(struct device *dev)
488 static atomic_t bus_no_reg_magic;
489 struct device_node *node = dev->of_node;
494 #ifdef CONFIG_PPC_DCR
496 * If it's a DCR based device, use 'd' for native DCRs
497 * and 'D' for MMIO DCRs.
499 reg = of_get_property(node, "dcr-reg", NULL);
501 #ifdef CONFIG_PPC_DCR_NATIVE
502 dev_set_name(dev, "d%x.%s", *reg, node->name);
503 #else /* CONFIG_PPC_DCR_NATIVE */
504 u64 addr = of_translate_dcr_address(node, *reg, NULL);
505 if (addr != OF_BAD_ADDR) {
506 dev_set_name(dev, "D%llx.%s",
507 (unsigned long long)addr, node->name);
510 #endif /* !CONFIG_PPC_DCR_NATIVE */
512 #endif /* CONFIG_PPC_DCR */
515 * For MMIO, get the physical address
517 reg = of_get_property(node, "reg", NULL);
519 addr = of_translate_address(node, reg);
520 if (addr != OF_BAD_ADDR) {
521 dev_set_name(dev, "%llx.%s",
522 (unsigned long long)addr, node->name);
528 * No BusID, use the node name and add a globally incremented
529 * counter (and pray...)
531 magic = atomic_add_return(1, &bus_no_reg_magic);
532 dev_set_name(dev, "%s.%d", node->name, magic - 1);
536 * of_device_alloc - Allocate and initialize an of_device
537 * @np: device node to assign to device
538 * @bus_id: Name to assign to the device. May be null to use default name.
539 * @parent: Parent device.
541 struct of_device *of_device_alloc(struct device_node *np,
543 struct device *parent)
545 struct of_device *dev;
546 int rc, i, num_reg = 0, num_irq = 0;
547 struct resource *res, temp_res;
549 /* First count how many resources are needed */
550 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
552 while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
555 /* Allocate memory for both the struct device and the resource table */
556 dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
560 res = (struct resource *) &dev[1];
562 /* Populate the resource table */
563 if (num_irq || num_reg) {
564 dev->num_resources = num_reg + num_irq;
566 for (i = 0; i < num_reg; i++, res++) {
567 rc = of_address_to_resource(np, i, res);
570 for (i = 0; i < num_irq; i++, res++) {
571 rc = of_irq_to_resource(np, i, res);
572 WARN_ON(rc == NO_IRQ);
576 dev->dev.of_node = of_node_get(np);
577 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
578 dev->dev.dma_mask = &dev->archdata.dma_mask;
580 dev->dev.parent = parent;
581 dev->dev.release = of_release_dev;
584 dev_set_name(&dev->dev, "%s", bus_id);
586 of_device_make_bus_id(&dev->dev);
590 EXPORT_SYMBOL(of_device_alloc);
593 * of_platform_device_create - Alloc, initialize and register an of_device
594 * @np: pointer to node to create device for
595 * @bus_id: name to assign device
596 * @parent: Linux device model parent device.
598 struct of_device *of_platform_device_create(struct device_node *np,
600 struct device *parent)
602 struct of_device *dev;
604 dev = of_device_alloc(np, bus_id, parent);
608 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
609 dev->archdata.dma_mask = 0xffffffffUL;
611 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
612 dev->dev.bus = &platform_bus_type;
614 /* We do not fill the DMA ops for platform devices by default.
615 * This is currently the responsibility of the platform code
616 * to do such, possibly using a device notifier
619 if (of_device_register(dev) != 0) {
626 EXPORT_SYMBOL(of_platform_device_create);
629 * of_platform_bus_create - Create an OF device for a bus node and all its
630 * children. Optionally recursively instantiate matching busses.
631 * @bus: device node of the bus to instantiate
632 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
633 * disallow recursive creation of child busses
635 static int of_platform_bus_create(const struct device_node *bus,
636 const struct of_device_id *matches,
637 struct device *parent)
639 struct device_node *child;
640 struct of_device *dev;
643 for_each_child_of_node(bus, child) {
644 pr_debug(" create child: %s\n", child->full_name);
645 dev = of_platform_device_create(child, NULL, parent);
648 else if (!of_match_node(matches, child))
651 pr_debug(" and sub busses\n");
652 rc = of_platform_bus_create(child, matches, &dev->dev);
663 * of_platform_bus_probe - Probe the device-tree for platform busses
664 * @root: parent of the first level to probe or NULL for the root of the tree
665 * @matches: match table, NULL to use the default
666 * @parent: parent to hook devices from, NULL for toplevel
668 * Note that children of the provided root are not instantiated as devices
669 * unless the specified root itself matches the bus list and is not NULL.
671 int of_platform_bus_probe(struct device_node *root,
672 const struct of_device_id *matches,
673 struct device *parent)
675 struct device_node *child;
676 struct of_device *dev;
680 matches = of_default_bus_ids;
681 if (matches == OF_NO_DEEP_PROBE)
684 root = of_find_node_by_path("/");
690 pr_debug("of_platform_bus_probe()\n");
691 pr_debug(" starting at: %s\n", root->full_name);
693 /* Do a self check of bus type, if there's a match, create
696 if (of_match_node(matches, root)) {
697 pr_debug(" root match, create all sub devices\n");
698 dev = of_platform_device_create(root, NULL, parent);
703 pr_debug(" create all sub busses\n");
704 rc = of_platform_bus_create(root, matches, &dev->dev);
707 for_each_child_of_node(root, child) {
708 if (!of_match_node(matches, child))
711 pr_debug(" match: %s\n", child->full_name);
712 dev = of_platform_device_create(child, NULL, parent);
716 rc = of_platform_bus_create(child, matches, &dev->dev);
726 EXPORT_SYMBOL(of_platform_bus_probe);
727 #endif /* !CONFIG_SPARC */