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>
24 #if defined(CONFIG_PPC_DCR)
28 extern struct device_attribute of_platform_device_attrs[];
30 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
32 const struct of_device_id *matches = drv->of_match_table;
37 return of_match_device(matches, dev) != NULL;
40 static int of_platform_device_probe(struct device *dev)
43 struct of_platform_driver *drv;
44 struct of_device *of_dev;
45 const struct of_device_id *match;
47 drv = to_of_platform_driver(dev->driver);
48 of_dev = to_of_device(dev);
55 match = of_match_device(drv->driver.of_match_table, dev);
57 error = drv->probe(of_dev, match);
64 static int of_platform_device_remove(struct device *dev)
66 struct of_device *of_dev = to_of_device(dev);
67 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
69 if (dev->driver && drv->remove)
74 static void of_platform_device_shutdown(struct device *dev)
76 struct of_device *of_dev = to_of_device(dev);
77 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
79 if (dev->driver && drv->shutdown)
80 drv->shutdown(of_dev);
83 #ifdef CONFIG_PM_SLEEP
85 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
87 struct of_device *of_dev = to_of_device(dev);
88 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
91 if (dev->driver && drv->suspend)
92 ret = drv->suspend(of_dev, mesg);
96 static int of_platform_legacy_resume(struct device *dev)
98 struct of_device *of_dev = to_of_device(dev);
99 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
102 if (dev->driver && drv->resume)
103 ret = drv->resume(of_dev);
107 static int of_platform_pm_prepare(struct device *dev)
109 struct device_driver *drv = dev->driver;
112 if (drv && drv->pm && drv->pm->prepare)
113 ret = drv->pm->prepare(dev);
118 static void of_platform_pm_complete(struct device *dev)
120 struct device_driver *drv = dev->driver;
122 if (drv && drv->pm && drv->pm->complete)
123 drv->pm->complete(dev);
126 #ifdef CONFIG_SUSPEND
128 static int of_platform_pm_suspend(struct device *dev)
130 struct device_driver *drv = dev->driver;
137 if (drv->pm->suspend)
138 ret = drv->pm->suspend(dev);
140 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
146 static int of_platform_pm_suspend_noirq(struct device *dev)
148 struct device_driver *drv = dev->driver;
155 if (drv->pm->suspend_noirq)
156 ret = drv->pm->suspend_noirq(dev);
162 static int of_platform_pm_resume(struct device *dev)
164 struct device_driver *drv = dev->driver;
172 ret = drv->pm->resume(dev);
174 ret = of_platform_legacy_resume(dev);
180 static int of_platform_pm_resume_noirq(struct device *dev)
182 struct device_driver *drv = dev->driver;
189 if (drv->pm->resume_noirq)
190 ret = drv->pm->resume_noirq(dev);
196 #else /* !CONFIG_SUSPEND */
198 #define of_platform_pm_suspend NULL
199 #define of_platform_pm_resume NULL
200 #define of_platform_pm_suspend_noirq NULL
201 #define of_platform_pm_resume_noirq NULL
203 #endif /* !CONFIG_SUSPEND */
205 #ifdef CONFIG_HIBERNATION
207 static int of_platform_pm_freeze(struct device *dev)
209 struct device_driver *drv = dev->driver;
217 ret = drv->pm->freeze(dev);
219 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
225 static int of_platform_pm_freeze_noirq(struct device *dev)
227 struct device_driver *drv = dev->driver;
234 if (drv->pm->freeze_noirq)
235 ret = drv->pm->freeze_noirq(dev);
241 static int of_platform_pm_thaw(struct device *dev)
243 struct device_driver *drv = dev->driver;
251 ret = drv->pm->thaw(dev);
253 ret = of_platform_legacy_resume(dev);
259 static int of_platform_pm_thaw_noirq(struct device *dev)
261 struct device_driver *drv = dev->driver;
268 if (drv->pm->thaw_noirq)
269 ret = drv->pm->thaw_noirq(dev);
275 static int of_platform_pm_poweroff(struct device *dev)
277 struct device_driver *drv = dev->driver;
284 if (drv->pm->poweroff)
285 ret = drv->pm->poweroff(dev);
287 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
293 static int of_platform_pm_poweroff_noirq(struct device *dev)
295 struct device_driver *drv = dev->driver;
302 if (drv->pm->poweroff_noirq)
303 ret = drv->pm->poweroff_noirq(dev);
309 static int of_platform_pm_restore(struct device *dev)
311 struct device_driver *drv = dev->driver;
318 if (drv->pm->restore)
319 ret = drv->pm->restore(dev);
321 ret = of_platform_legacy_resume(dev);
327 static int of_platform_pm_restore_noirq(struct device *dev)
329 struct device_driver *drv = dev->driver;
336 if (drv->pm->restore_noirq)
337 ret = drv->pm->restore_noirq(dev);
343 #else /* !CONFIG_HIBERNATION */
345 #define of_platform_pm_freeze NULL
346 #define of_platform_pm_thaw NULL
347 #define of_platform_pm_poweroff NULL
348 #define of_platform_pm_restore NULL
349 #define of_platform_pm_freeze_noirq NULL
350 #define of_platform_pm_thaw_noirq NULL
351 #define of_platform_pm_poweroff_noirq NULL
352 #define of_platform_pm_restore_noirq NULL
354 #endif /* !CONFIG_HIBERNATION */
356 static struct dev_pm_ops of_platform_dev_pm_ops = {
357 .prepare = of_platform_pm_prepare,
358 .complete = of_platform_pm_complete,
359 .suspend = of_platform_pm_suspend,
360 .resume = of_platform_pm_resume,
361 .freeze = of_platform_pm_freeze,
362 .thaw = of_platform_pm_thaw,
363 .poweroff = of_platform_pm_poweroff,
364 .restore = of_platform_pm_restore,
365 .suspend_noirq = of_platform_pm_suspend_noirq,
366 .resume_noirq = of_platform_pm_resume_noirq,
367 .freeze_noirq = of_platform_pm_freeze_noirq,
368 .thaw_noirq = of_platform_pm_thaw_noirq,
369 .poweroff_noirq = of_platform_pm_poweroff_noirq,
370 .restore_noirq = of_platform_pm_restore_noirq,
373 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
375 #else /* !CONFIG_PM_SLEEP */
377 #define OF_PLATFORM_PM_OPS_PTR NULL
379 #endif /* !CONFIG_PM_SLEEP */
381 int of_bus_type_init(struct bus_type *bus, const char *name)
384 bus->match = of_platform_bus_match;
385 bus->probe = of_platform_device_probe;
386 bus->remove = of_platform_device_remove;
387 bus->shutdown = of_platform_device_shutdown;
388 bus->dev_attrs = of_platform_device_attrs;
389 bus->pm = OF_PLATFORM_PM_OPS_PTR;
390 return bus_register(bus);
393 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
395 drv->driver.bus = bus;
397 /* register with core */
398 return driver_register(&drv->driver);
400 EXPORT_SYMBOL(of_register_driver);
402 void of_unregister_driver(struct of_platform_driver *drv)
404 driver_unregister(&drv->driver);
406 EXPORT_SYMBOL(of_unregister_driver);
408 #if !defined(CONFIG_SPARC)
410 * The following routines scan a subtree and registers a device for
411 * each applicable node.
413 * Note: sparc doesn't use these routines because it has a different
414 * mechanism for creating devices from device tree nodes.
418 * of_device_make_bus_id - Use the device node data to assign a unique name
419 * @dev: pointer to device structure that is linked to a device tree node
421 * This routine will first try using either the dcr-reg or the reg property
422 * value to derive a unique name. As a last resort it will use the node
423 * name followed by a unique number.
425 static void of_device_make_bus_id(struct device *dev)
427 static atomic_t bus_no_reg_magic;
428 struct device_node *node = dev->of_node;
433 #ifdef CONFIG_PPC_DCR
435 * If it's a DCR based device, use 'd' for native DCRs
436 * and 'D' for MMIO DCRs.
438 reg = of_get_property(node, "dcr-reg", NULL);
440 #ifdef CONFIG_PPC_DCR_NATIVE
441 dev_set_name(dev, "d%x.%s", *reg, node->name);
442 #else /* CONFIG_PPC_DCR_NATIVE */
443 u64 addr = of_translate_dcr_address(node, *reg, NULL);
444 if (addr != OF_BAD_ADDR) {
445 dev_set_name(dev, "D%llx.%s",
446 (unsigned long long)addr, node->name);
449 #endif /* !CONFIG_PPC_DCR_NATIVE */
451 #endif /* CONFIG_PPC_DCR */
454 * For MMIO, get the physical address
456 reg = of_get_property(node, "reg", NULL);
458 addr = of_translate_address(node, reg);
459 if (addr != OF_BAD_ADDR) {
460 dev_set_name(dev, "%llx.%s",
461 (unsigned long long)addr, node->name);
467 * No BusID, use the node name and add a globally incremented
468 * counter (and pray...)
470 magic = atomic_add_return(1, &bus_no_reg_magic);
471 dev_set_name(dev, "%s.%d", node->name, magic - 1);
475 * of_device_alloc - Allocate and initialize an of_device
476 * @np: device node to assign to device
477 * @bus_id: Name to assign to the device. May be null to use default name.
478 * @parent: Parent device.
480 struct of_device *of_device_alloc(struct device_node *np,
482 struct device *parent)
484 struct of_device *dev;
485 int rc, i, num_reg = 0, num_irq = 0;
486 struct resource *res, temp_res;
488 /* First count how many resources are needed */
489 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
491 while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
494 /* Allocate memory for both the struct device and the resource table */
495 dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
499 res = (struct resource *) &dev[1];
501 /* Populate the resource table */
502 if (num_irq || num_reg) {
503 dev->num_resources = num_reg + num_irq;
505 for (i = 0; i < num_reg; i++, res++) {
506 rc = of_address_to_resource(np, i, res);
509 for (i = 0; i < num_irq; i++, res++) {
510 rc = of_irq_to_resource(np, i, res);
511 WARN_ON(rc == NO_IRQ);
515 dev->dev.of_node = of_node_get(np);
516 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
517 dev->dev.dma_mask = &dev->archdata.dma_mask;
519 dev->dev.parent = parent;
520 dev->dev.release = of_release_dev;
523 dev_set_name(&dev->dev, "%s", bus_id);
525 of_device_make_bus_id(&dev->dev);
529 EXPORT_SYMBOL(of_device_alloc);
532 * of_platform_device_create - Alloc, initialize and register an of_device
533 * @np: pointer to node to create device for
534 * @bus_id: name to assign device
535 * @parent: Linux device model parent device.
537 struct of_device *of_platform_device_create(struct device_node *np,
539 struct device *parent)
541 struct of_device *dev;
543 dev = of_device_alloc(np, bus_id, parent);
547 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
548 dev->archdata.dma_mask = 0xffffffffUL;
550 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
551 dev->dev.bus = &of_platform_bus_type;
553 /* We do not fill the DMA ops for platform devices by default.
554 * This is currently the responsibility of the platform code
555 * to do such, possibly using a device notifier
558 if (of_device_register(dev) != 0) {
565 EXPORT_SYMBOL(of_platform_device_create);
568 * of_platform_bus_create - Create an OF device for a bus node and all its
569 * children. Optionally recursively instantiate matching busses.
570 * @bus: device node of the bus to instantiate
571 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
572 * disallow recursive creation of child busses
574 static int of_platform_bus_create(const struct device_node *bus,
575 const struct of_device_id *matches,
576 struct device *parent)
578 struct device_node *child;
579 struct of_device *dev;
582 for_each_child_of_node(bus, child) {
583 pr_debug(" create child: %s\n", child->full_name);
584 dev = of_platform_device_create(child, NULL, parent);
587 else if (!of_match_node(matches, child))
590 pr_debug(" and sub busses\n");
591 rc = of_platform_bus_create(child, matches, &dev->dev);
602 * of_platform_bus_probe - Probe the device-tree for platform busses
603 * @root: parent of the first level to probe or NULL for the root of the tree
604 * @matches: match table, NULL to use the default
605 * @parent: parent to hook devices from, NULL for toplevel
607 * Note that children of the provided root are not instantiated as devices
608 * unless the specified root itself matches the bus list and is not NULL.
610 int of_platform_bus_probe(struct device_node *root,
611 const struct of_device_id *matches,
612 struct device *parent)
614 struct device_node *child;
615 struct of_device *dev;
619 matches = of_default_bus_ids;
620 if (matches == OF_NO_DEEP_PROBE)
623 root = of_find_node_by_path("/");
629 pr_debug("of_platform_bus_probe()\n");
630 pr_debug(" starting at: %s\n", root->full_name);
632 /* Do a self check of bus type, if there's a match, create
635 if (of_match_node(matches, root)) {
636 pr_debug(" root match, create all sub devices\n");
637 dev = of_platform_device_create(root, NULL, parent);
642 pr_debug(" create all sub busses\n");
643 rc = of_platform_bus_create(root, matches, &dev->dev);
646 for_each_child_of_node(root, child) {
647 if (!of_match_node(matches, child))
650 pr_debug(" match: %s\n", child->full_name);
651 dev = of_platform_device_create(child, NULL, parent);
655 rc = of_platform_bus_create(child, matches, &dev->dev);
665 EXPORT_SYMBOL(of_platform_bus_probe);
666 #endif /* !CONFIG_SPARC */