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
);
34 /* There is an unlikely chance that an of_platform driver might match
35 * on a non-OF platform device. If so, then of_match_device() will
36 * come up empty. Return -EINVAL in this case so other drivers get
37 * the chance to bind. */
38 match
= of_match_device(pdev
->dev
.driver
->of_match_table
, &pdev
->dev
);
39 return match
? ofpdrv
->probe(pdev
, match
) : -EINVAL
;
42 static void platform_driver_shutdown_shim(struct platform_device
*pdev
)
44 struct platform_driver
*pdrv
;
45 struct of_platform_driver
*ofpdrv
;
47 pdrv
= container_of(pdev
->dev
.driver
, struct platform_driver
, driver
);
48 ofpdrv
= container_of(pdrv
, struct of_platform_driver
, platform_driver
);
49 ofpdrv
->shutdown(pdev
);
53 * of_register_platform_driver
55 int of_register_platform_driver(struct of_platform_driver
*drv
)
57 /* setup of_platform_driver to platform_driver adaptors */
58 drv
->platform_driver
.driver
= drv
->driver
;
60 drv
->platform_driver
.probe
= platform_driver_probe_shim
;
61 drv
->platform_driver
.remove
= drv
->remove
;
63 drv
->platform_driver
.shutdown
= platform_driver_shutdown_shim
;
64 drv
->platform_driver
.suspend
= drv
->suspend
;
65 drv
->platform_driver
.resume
= drv
->resume
;
67 return platform_driver_register(&drv
->platform_driver
);
69 EXPORT_SYMBOL(of_register_platform_driver
);
71 void of_unregister_platform_driver(struct of_platform_driver
*drv
)
73 platform_driver_unregister(&drv
->platform_driver
);
75 EXPORT_SYMBOL(of_unregister_platform_driver
);
77 #if defined(CONFIG_PPC_DCR)
81 extern struct device_attribute of_platform_device_attrs
[];
83 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
85 const struct of_device_id
*matches
= drv
->of_match_table
;
90 return of_match_device(matches
, dev
) != NULL
;
93 static int of_platform_device_probe(struct device
*dev
)
96 struct of_platform_driver
*drv
;
97 struct of_device
*of_dev
;
98 const struct of_device_id
*match
;
100 drv
= to_of_platform_driver(dev
->driver
);
101 of_dev
= to_of_device(dev
);
108 match
= of_match_device(drv
->driver
.of_match_table
, dev
);
110 error
= drv
->probe(of_dev
, match
);
117 static int of_platform_device_remove(struct device
*dev
)
119 struct of_device
*of_dev
= to_of_device(dev
);
120 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
122 if (dev
->driver
&& drv
->remove
)
127 static void of_platform_device_shutdown(struct device
*dev
)
129 struct of_device
*of_dev
= to_of_device(dev
);
130 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
132 if (dev
->driver
&& drv
->shutdown
)
133 drv
->shutdown(of_dev
);
136 #ifdef CONFIG_PM_SLEEP
138 static int of_platform_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
140 struct of_device
*of_dev
= to_of_device(dev
);
141 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
144 if (dev
->driver
&& drv
->suspend
)
145 ret
= drv
->suspend(of_dev
, mesg
);
149 static int of_platform_legacy_resume(struct device
*dev
)
151 struct of_device
*of_dev
= to_of_device(dev
);
152 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
155 if (dev
->driver
&& drv
->resume
)
156 ret
= drv
->resume(of_dev
);
160 static int of_platform_pm_prepare(struct device
*dev
)
162 struct device_driver
*drv
= dev
->driver
;
165 if (drv
&& drv
->pm
&& drv
->pm
->prepare
)
166 ret
= drv
->pm
->prepare(dev
);
171 static void of_platform_pm_complete(struct device
*dev
)
173 struct device_driver
*drv
= dev
->driver
;
175 if (drv
&& drv
->pm
&& drv
->pm
->complete
)
176 drv
->pm
->complete(dev
);
179 #ifdef CONFIG_SUSPEND
181 static int of_platform_pm_suspend(struct device
*dev
)
183 struct device_driver
*drv
= dev
->driver
;
190 if (drv
->pm
->suspend
)
191 ret
= drv
->pm
->suspend(dev
);
193 ret
= of_platform_legacy_suspend(dev
, PMSG_SUSPEND
);
199 static int of_platform_pm_suspend_noirq(struct device
*dev
)
201 struct device_driver
*drv
= dev
->driver
;
208 if (drv
->pm
->suspend_noirq
)
209 ret
= drv
->pm
->suspend_noirq(dev
);
215 static int of_platform_pm_resume(struct device
*dev
)
217 struct device_driver
*drv
= dev
->driver
;
225 ret
= drv
->pm
->resume(dev
);
227 ret
= of_platform_legacy_resume(dev
);
233 static int of_platform_pm_resume_noirq(struct device
*dev
)
235 struct device_driver
*drv
= dev
->driver
;
242 if (drv
->pm
->resume_noirq
)
243 ret
= drv
->pm
->resume_noirq(dev
);
249 #else /* !CONFIG_SUSPEND */
251 #define of_platform_pm_suspend NULL
252 #define of_platform_pm_resume NULL
253 #define of_platform_pm_suspend_noirq NULL
254 #define of_platform_pm_resume_noirq NULL
256 #endif /* !CONFIG_SUSPEND */
258 #ifdef CONFIG_HIBERNATION
260 static int of_platform_pm_freeze(struct device
*dev
)
262 struct device_driver
*drv
= dev
->driver
;
270 ret
= drv
->pm
->freeze(dev
);
272 ret
= of_platform_legacy_suspend(dev
, PMSG_FREEZE
);
278 static int of_platform_pm_freeze_noirq(struct device
*dev
)
280 struct device_driver
*drv
= dev
->driver
;
287 if (drv
->pm
->freeze_noirq
)
288 ret
= drv
->pm
->freeze_noirq(dev
);
294 static int of_platform_pm_thaw(struct device
*dev
)
296 struct device_driver
*drv
= dev
->driver
;
304 ret
= drv
->pm
->thaw(dev
);
306 ret
= of_platform_legacy_resume(dev
);
312 static int of_platform_pm_thaw_noirq(struct device
*dev
)
314 struct device_driver
*drv
= dev
->driver
;
321 if (drv
->pm
->thaw_noirq
)
322 ret
= drv
->pm
->thaw_noirq(dev
);
328 static int of_platform_pm_poweroff(struct device
*dev
)
330 struct device_driver
*drv
= dev
->driver
;
337 if (drv
->pm
->poweroff
)
338 ret
= drv
->pm
->poweroff(dev
);
340 ret
= of_platform_legacy_suspend(dev
, PMSG_HIBERNATE
);
346 static int of_platform_pm_poweroff_noirq(struct device
*dev
)
348 struct device_driver
*drv
= dev
->driver
;
355 if (drv
->pm
->poweroff_noirq
)
356 ret
= drv
->pm
->poweroff_noirq(dev
);
362 static int of_platform_pm_restore(struct device
*dev
)
364 struct device_driver
*drv
= dev
->driver
;
371 if (drv
->pm
->restore
)
372 ret
= drv
->pm
->restore(dev
);
374 ret
= of_platform_legacy_resume(dev
);
380 static int of_platform_pm_restore_noirq(struct device
*dev
)
382 struct device_driver
*drv
= dev
->driver
;
389 if (drv
->pm
->restore_noirq
)
390 ret
= drv
->pm
->restore_noirq(dev
);
396 #else /* !CONFIG_HIBERNATION */
398 #define of_platform_pm_freeze NULL
399 #define of_platform_pm_thaw NULL
400 #define of_platform_pm_poweroff NULL
401 #define of_platform_pm_restore NULL
402 #define of_platform_pm_freeze_noirq NULL
403 #define of_platform_pm_thaw_noirq NULL
404 #define of_platform_pm_poweroff_noirq NULL
405 #define of_platform_pm_restore_noirq NULL
407 #endif /* !CONFIG_HIBERNATION */
409 static struct dev_pm_ops of_platform_dev_pm_ops
= {
410 .prepare
= of_platform_pm_prepare
,
411 .complete
= of_platform_pm_complete
,
412 .suspend
= of_platform_pm_suspend
,
413 .resume
= of_platform_pm_resume
,
414 .freeze
= of_platform_pm_freeze
,
415 .thaw
= of_platform_pm_thaw
,
416 .poweroff
= of_platform_pm_poweroff
,
417 .restore
= of_platform_pm_restore
,
418 .suspend_noirq
= of_platform_pm_suspend_noirq
,
419 .resume_noirq
= of_platform_pm_resume_noirq
,
420 .freeze_noirq
= of_platform_pm_freeze_noirq
,
421 .thaw_noirq
= of_platform_pm_thaw_noirq
,
422 .poweroff_noirq
= of_platform_pm_poweroff_noirq
,
423 .restore_noirq
= of_platform_pm_restore_noirq
,
426 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
428 #else /* !CONFIG_PM_SLEEP */
430 #define OF_PLATFORM_PM_OPS_PTR NULL
432 #endif /* !CONFIG_PM_SLEEP */
434 int of_bus_type_init(struct bus_type
*bus
, const char *name
)
437 bus
->match
= of_platform_bus_match
;
438 bus
->probe
= of_platform_device_probe
;
439 bus
->remove
= of_platform_device_remove
;
440 bus
->shutdown
= of_platform_device_shutdown
;
441 bus
->dev_attrs
= of_platform_device_attrs
;
442 bus
->pm
= OF_PLATFORM_PM_OPS_PTR
;
443 return bus_register(bus
);
446 int of_register_driver(struct of_platform_driver
*drv
, struct bus_type
*bus
)
449 * Temporary: of_platform_bus used to be distinct from the platform
450 * bus. It isn't anymore, and so drivers on the platform bus need
451 * to be registered in a special way.
453 * After all of_platform_bus_type drivers are converted to
454 * platform_drivers, this exception can be removed.
456 if (bus
== &platform_bus_type
)
457 return of_register_platform_driver(drv
);
459 /* register with core */
460 drv
->driver
.bus
= bus
;
461 return driver_register(&drv
->driver
);
463 EXPORT_SYMBOL(of_register_driver
);
465 void of_unregister_driver(struct of_platform_driver
*drv
)
467 if (drv
->driver
.bus
== &platform_bus_type
)
468 of_unregister_platform_driver(drv
);
470 driver_unregister(&drv
->driver
);
472 EXPORT_SYMBOL(of_unregister_driver
);
474 #if !defined(CONFIG_SPARC)
476 * The following routines scan a subtree and registers a device for
477 * each applicable node.
479 * Note: sparc doesn't use these routines because it has a different
480 * mechanism for creating devices from device tree nodes.
484 * of_device_make_bus_id - Use the device node data to assign a unique name
485 * @dev: pointer to device structure that is linked to a device tree node
487 * This routine will first try using either the dcr-reg or the reg property
488 * value to derive a unique name. As a last resort it will use the node
489 * name followed by a unique number.
491 static void of_device_make_bus_id(struct device
*dev
)
493 static atomic_t bus_no_reg_magic
;
494 struct device_node
*node
= dev
->of_node
;
499 #ifdef CONFIG_PPC_DCR
501 * If it's a DCR based device, use 'd' for native DCRs
502 * and 'D' for MMIO DCRs.
504 reg
= of_get_property(node
, "dcr-reg", NULL
);
506 #ifdef CONFIG_PPC_DCR_NATIVE
507 dev_set_name(dev
, "d%x.%s", *reg
, node
->name
);
508 #else /* CONFIG_PPC_DCR_NATIVE */
509 u64 addr
= of_translate_dcr_address(node
, *reg
, NULL
);
510 if (addr
!= OF_BAD_ADDR
) {
511 dev_set_name(dev
, "D%llx.%s",
512 (unsigned long long)addr
, node
->name
);
515 #endif /* !CONFIG_PPC_DCR_NATIVE */
517 #endif /* CONFIG_PPC_DCR */
520 * For MMIO, get the physical address
522 reg
= of_get_property(node
, "reg", NULL
);
524 addr
= of_translate_address(node
, reg
);
525 if (addr
!= OF_BAD_ADDR
) {
526 dev_set_name(dev
, "%llx.%s",
527 (unsigned long long)addr
, node
->name
);
533 * No BusID, use the node name and add a globally incremented
534 * counter (and pray...)
536 magic
= atomic_add_return(1, &bus_no_reg_magic
);
537 dev_set_name(dev
, "%s.%d", node
->name
, magic
- 1);
541 * of_device_alloc - Allocate and initialize an of_device
542 * @np: device node to assign to device
543 * @bus_id: Name to assign to the device. May be null to use default name.
544 * @parent: Parent device.
546 struct of_device
*of_device_alloc(struct device_node
*np
,
548 struct device
*parent
)
550 struct of_device
*dev
;
551 int rc
, i
, num_reg
= 0, num_irq
= 0;
552 struct resource
*res
, temp_res
;
554 /* First count how many resources are needed */
555 while (of_address_to_resource(np
, num_reg
, &temp_res
) == 0)
557 while (of_irq_to_resource(np
, num_irq
, &temp_res
) != NO_IRQ
)
560 /* Allocate memory for both the struct device and the resource table */
561 dev
= kzalloc(sizeof(*dev
) + (sizeof(*res
) * (num_reg
+ num_irq
)),
565 res
= (struct resource
*) &dev
[1];
567 /* Populate the resource table */
568 if (num_irq
|| num_reg
) {
569 dev
->num_resources
= num_reg
+ num_irq
;
571 for (i
= 0; i
< num_reg
; i
++, res
++) {
572 rc
= of_address_to_resource(np
, i
, res
);
575 for (i
= 0; i
< num_irq
; i
++, res
++) {
576 rc
= of_irq_to_resource(np
, i
, res
);
577 WARN_ON(rc
== NO_IRQ
);
581 dev
->dev
.of_node
= of_node_get(np
);
582 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
583 dev
->dev
.dma_mask
= &dev
->archdata
.dma_mask
;
585 dev
->dev
.parent
= parent
;
586 dev
->dev
.release
= of_release_dev
;
589 dev_set_name(&dev
->dev
, "%s", bus_id
);
591 of_device_make_bus_id(&dev
->dev
);
595 EXPORT_SYMBOL(of_device_alloc
);
598 * of_platform_device_create - Alloc, initialize and register an of_device
599 * @np: pointer to node to create device for
600 * @bus_id: name to assign device
601 * @parent: Linux device model parent device.
603 struct of_device
*of_platform_device_create(struct device_node
*np
,
605 struct device
*parent
)
607 struct of_device
*dev
;
609 dev
= of_device_alloc(np
, bus_id
, parent
);
613 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
614 dev
->archdata
.dma_mask
= 0xffffffffUL
;
616 dev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
617 dev
->dev
.bus
= &platform_bus_type
;
619 /* We do not fill the DMA ops for platform devices by default.
620 * This is currently the responsibility of the platform code
621 * to do such, possibly using a device notifier
624 if (of_device_register(dev
) != 0) {
631 EXPORT_SYMBOL(of_platform_device_create
);
634 * of_platform_bus_create - Create an OF device for a bus node and all its
635 * children. Optionally recursively instantiate matching busses.
636 * @bus: device node of the bus to instantiate
637 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
638 * disallow recursive creation of child busses
640 static int of_platform_bus_create(const struct device_node
*bus
,
641 const struct of_device_id
*matches
,
642 struct device
*parent
)
644 struct device_node
*child
;
645 struct of_device
*dev
;
648 for_each_child_of_node(bus
, child
) {
649 pr_debug(" create child: %s\n", child
->full_name
);
650 dev
= of_platform_device_create(child
, NULL
, parent
);
653 else if (!of_match_node(matches
, child
))
656 pr_debug(" and sub busses\n");
657 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
668 * of_platform_bus_probe - Probe the device-tree for platform busses
669 * @root: parent of the first level to probe or NULL for the root of the tree
670 * @matches: match table, NULL to use the default
671 * @parent: parent to hook devices from, NULL for toplevel
673 * Note that children of the provided root are not instantiated as devices
674 * unless the specified root itself matches the bus list and is not NULL.
676 int of_platform_bus_probe(struct device_node
*root
,
677 const struct of_device_id
*matches
,
678 struct device
*parent
)
680 struct device_node
*child
;
681 struct of_device
*dev
;
685 matches
= of_default_bus_ids
;
686 if (matches
== OF_NO_DEEP_PROBE
)
689 root
= of_find_node_by_path("/");
695 pr_debug("of_platform_bus_probe()\n");
696 pr_debug(" starting at: %s\n", root
->full_name
);
698 /* Do a self check of bus type, if there's a match, create
701 if (of_match_node(matches
, root
)) {
702 pr_debug(" root match, create all sub devices\n");
703 dev
= of_platform_device_create(root
, NULL
, parent
);
708 pr_debug(" create all sub busses\n");
709 rc
= of_platform_bus_create(root
, matches
, &dev
->dev
);
712 for_each_child_of_node(root
, child
) {
713 if (!of_match_node(matches
, child
))
716 pr_debug(" match: %s\n", child
->full_name
);
717 dev
= of_platform_device_create(child
, NULL
, parent
);
721 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
731 EXPORT_SYMBOL(of_platform_bus_probe
);
732 #endif /* !CONFIG_SPARC */