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
)
79 /* setup of_platform_driver to platform_driver adaptors */
80 drv
->platform_driver
.driver
= drv
->driver
;
82 /* Prefix the driver name with 'of:' to avoid namespace collisions
83 * and bogus matches. There are some drivers in the tree that
84 * register both an of_platform_driver and a platform_driver with
85 * the same name. This is a temporary measure until they are all
86 * cleaned up --gcl July 29, 2010 */
87 of_name
= kmalloc(strlen(drv
->driver
.name
) + 5, GFP_KERNEL
);
90 sprintf(of_name
, "of:%s", drv
->driver
.name
);
91 drv
->platform_driver
.driver
.name
= of_name
;
94 drv
->platform_driver
.probe
= platform_driver_probe_shim
;
95 drv
->platform_driver
.remove
= drv
->remove
;
97 drv
->platform_driver
.shutdown
= platform_driver_shutdown_shim
;
98 drv
->platform_driver
.suspend
= drv
->suspend
;
99 drv
->platform_driver
.resume
= drv
->resume
;
101 return platform_driver_register(&drv
->platform_driver
);
103 EXPORT_SYMBOL(of_register_platform_driver
);
105 void of_unregister_platform_driver(struct of_platform_driver
*drv
)
107 platform_driver_unregister(&drv
->platform_driver
);
108 kfree(drv
->platform_driver
.driver
.name
);
109 drv
->platform_driver
.driver
.name
= NULL
;
111 EXPORT_SYMBOL(of_unregister_platform_driver
);
113 #if defined(CONFIG_PPC_DCR)
117 extern struct device_attribute of_platform_device_attrs
[];
119 static int of_platform_bus_match(struct device
*dev
, struct device_driver
*drv
)
121 const struct of_device_id
*matches
= drv
->of_match_table
;
126 return of_match_device(matches
, dev
) != NULL
;
129 static int of_platform_device_probe(struct device
*dev
)
132 struct of_platform_driver
*drv
;
133 struct platform_device
*of_dev
;
134 const struct of_device_id
*match
;
136 drv
= to_of_platform_driver(dev
->driver
);
137 of_dev
= to_platform_device(dev
);
144 match
= of_match_device(drv
->driver
.of_match_table
, dev
);
146 error
= drv
->probe(of_dev
, match
);
153 static int of_platform_device_remove(struct device
*dev
)
155 struct platform_device
*of_dev
= to_platform_device(dev
);
156 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
158 if (dev
->driver
&& drv
->remove
)
163 static void of_platform_device_shutdown(struct device
*dev
)
165 struct platform_device
*of_dev
= to_platform_device(dev
);
166 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
168 if (dev
->driver
&& drv
->shutdown
)
169 drv
->shutdown(of_dev
);
172 #ifdef CONFIG_PM_SLEEP
174 static int of_platform_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
176 struct platform_device
*of_dev
= to_platform_device(dev
);
177 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
180 if (dev
->driver
&& drv
->suspend
)
181 ret
= drv
->suspend(of_dev
, mesg
);
185 static int of_platform_legacy_resume(struct device
*dev
)
187 struct platform_device
*of_dev
= to_platform_device(dev
);
188 struct of_platform_driver
*drv
= to_of_platform_driver(dev
->driver
);
191 if (dev
->driver
&& drv
->resume
)
192 ret
= drv
->resume(of_dev
);
196 static int of_platform_pm_prepare(struct device
*dev
)
198 struct device_driver
*drv
= dev
->driver
;
201 if (drv
&& drv
->pm
&& drv
->pm
->prepare
)
202 ret
= drv
->pm
->prepare(dev
);
207 static void of_platform_pm_complete(struct device
*dev
)
209 struct device_driver
*drv
= dev
->driver
;
211 if (drv
&& drv
->pm
&& drv
->pm
->complete
)
212 drv
->pm
->complete(dev
);
215 #ifdef CONFIG_SUSPEND
217 static int of_platform_pm_suspend(struct device
*dev
)
219 struct device_driver
*drv
= dev
->driver
;
226 if (drv
->pm
->suspend
)
227 ret
= drv
->pm
->suspend(dev
);
229 ret
= of_platform_legacy_suspend(dev
, PMSG_SUSPEND
);
235 static int of_platform_pm_suspend_noirq(struct device
*dev
)
237 struct device_driver
*drv
= dev
->driver
;
244 if (drv
->pm
->suspend_noirq
)
245 ret
= drv
->pm
->suspend_noirq(dev
);
251 static int of_platform_pm_resume(struct device
*dev
)
253 struct device_driver
*drv
= dev
->driver
;
261 ret
= drv
->pm
->resume(dev
);
263 ret
= of_platform_legacy_resume(dev
);
269 static int of_platform_pm_resume_noirq(struct device
*dev
)
271 struct device_driver
*drv
= dev
->driver
;
278 if (drv
->pm
->resume_noirq
)
279 ret
= drv
->pm
->resume_noirq(dev
);
285 #else /* !CONFIG_SUSPEND */
287 #define of_platform_pm_suspend NULL
288 #define of_platform_pm_resume NULL
289 #define of_platform_pm_suspend_noirq NULL
290 #define of_platform_pm_resume_noirq NULL
292 #endif /* !CONFIG_SUSPEND */
294 #ifdef CONFIG_HIBERNATION
296 static int of_platform_pm_freeze(struct device
*dev
)
298 struct device_driver
*drv
= dev
->driver
;
306 ret
= drv
->pm
->freeze(dev
);
308 ret
= of_platform_legacy_suspend(dev
, PMSG_FREEZE
);
314 static int of_platform_pm_freeze_noirq(struct device
*dev
)
316 struct device_driver
*drv
= dev
->driver
;
323 if (drv
->pm
->freeze_noirq
)
324 ret
= drv
->pm
->freeze_noirq(dev
);
330 static int of_platform_pm_thaw(struct device
*dev
)
332 struct device_driver
*drv
= dev
->driver
;
340 ret
= drv
->pm
->thaw(dev
);
342 ret
= of_platform_legacy_resume(dev
);
348 static int of_platform_pm_thaw_noirq(struct device
*dev
)
350 struct device_driver
*drv
= dev
->driver
;
357 if (drv
->pm
->thaw_noirq
)
358 ret
= drv
->pm
->thaw_noirq(dev
);
364 static int of_platform_pm_poweroff(struct device
*dev
)
366 struct device_driver
*drv
= dev
->driver
;
373 if (drv
->pm
->poweroff
)
374 ret
= drv
->pm
->poweroff(dev
);
376 ret
= of_platform_legacy_suspend(dev
, PMSG_HIBERNATE
);
382 static int of_platform_pm_poweroff_noirq(struct device
*dev
)
384 struct device_driver
*drv
= dev
->driver
;
391 if (drv
->pm
->poweroff_noirq
)
392 ret
= drv
->pm
->poweroff_noirq(dev
);
398 static int of_platform_pm_restore(struct device
*dev
)
400 struct device_driver
*drv
= dev
->driver
;
407 if (drv
->pm
->restore
)
408 ret
= drv
->pm
->restore(dev
);
410 ret
= of_platform_legacy_resume(dev
);
416 static int of_platform_pm_restore_noirq(struct device
*dev
)
418 struct device_driver
*drv
= dev
->driver
;
425 if (drv
->pm
->restore_noirq
)
426 ret
= drv
->pm
->restore_noirq(dev
);
432 #else /* !CONFIG_HIBERNATION */
434 #define of_platform_pm_freeze NULL
435 #define of_platform_pm_thaw NULL
436 #define of_platform_pm_poweroff NULL
437 #define of_platform_pm_restore NULL
438 #define of_platform_pm_freeze_noirq NULL
439 #define of_platform_pm_thaw_noirq NULL
440 #define of_platform_pm_poweroff_noirq NULL
441 #define of_platform_pm_restore_noirq NULL
443 #endif /* !CONFIG_HIBERNATION */
445 static struct dev_pm_ops of_platform_dev_pm_ops
= {
446 .prepare
= of_platform_pm_prepare
,
447 .complete
= of_platform_pm_complete
,
448 .suspend
= of_platform_pm_suspend
,
449 .resume
= of_platform_pm_resume
,
450 .freeze
= of_platform_pm_freeze
,
451 .thaw
= of_platform_pm_thaw
,
452 .poweroff
= of_platform_pm_poweroff
,
453 .restore
= of_platform_pm_restore
,
454 .suspend_noirq
= of_platform_pm_suspend_noirq
,
455 .resume_noirq
= of_platform_pm_resume_noirq
,
456 .freeze_noirq
= of_platform_pm_freeze_noirq
,
457 .thaw_noirq
= of_platform_pm_thaw_noirq
,
458 .poweroff_noirq
= of_platform_pm_poweroff_noirq
,
459 .restore_noirq
= of_platform_pm_restore_noirq
,
462 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
464 #else /* !CONFIG_PM_SLEEP */
466 #define OF_PLATFORM_PM_OPS_PTR NULL
468 #endif /* !CONFIG_PM_SLEEP */
470 int of_bus_type_init(struct bus_type
*bus
, const char *name
)
473 bus
->match
= of_platform_bus_match
;
474 bus
->probe
= of_platform_device_probe
;
475 bus
->remove
= of_platform_device_remove
;
476 bus
->shutdown
= of_platform_device_shutdown
;
477 bus
->dev_attrs
= of_platform_device_attrs
;
478 bus
->pm
= OF_PLATFORM_PM_OPS_PTR
;
479 return bus_register(bus
);
482 int of_register_driver(struct of_platform_driver
*drv
, struct bus_type
*bus
)
485 * Temporary: of_platform_bus used to be distinct from the platform
486 * bus. It isn't anymore, and so drivers on the platform bus need
487 * to be registered in a special way.
489 * After all of_platform_bus_type drivers are converted to
490 * platform_drivers, this exception can be removed.
492 if (bus
== &platform_bus_type
)
493 return of_register_platform_driver(drv
);
495 /* register with core */
496 drv
->driver
.bus
= bus
;
497 return driver_register(&drv
->driver
);
499 EXPORT_SYMBOL(of_register_driver
);
501 void of_unregister_driver(struct of_platform_driver
*drv
)
503 if (drv
->driver
.bus
== &platform_bus_type
)
504 of_unregister_platform_driver(drv
);
506 driver_unregister(&drv
->driver
);
508 EXPORT_SYMBOL(of_unregister_driver
);
510 #if !defined(CONFIG_SPARC)
512 * The following routines scan a subtree and registers a device for
513 * each applicable node.
515 * Note: sparc doesn't use these routines because it has a different
516 * mechanism for creating devices from device tree nodes.
520 * of_device_make_bus_id - Use the device node data to assign a unique name
521 * @dev: pointer to device structure that is linked to a device tree node
523 * This routine will first try using either the dcr-reg or the reg property
524 * value to derive a unique name. As a last resort it will use the node
525 * name followed by a unique number.
527 void of_device_make_bus_id(struct device
*dev
)
529 static atomic_t bus_no_reg_magic
;
530 struct device_node
*node
= dev
->of_node
;
535 #ifdef CONFIG_PPC_DCR
537 * If it's a DCR based device, use 'd' for native DCRs
538 * and 'D' for MMIO DCRs.
540 reg
= of_get_property(node
, "dcr-reg", NULL
);
542 #ifdef CONFIG_PPC_DCR_NATIVE
543 dev_set_name(dev
, "d%x.%s", *reg
, node
->name
);
544 #else /* CONFIG_PPC_DCR_NATIVE */
545 u64 addr
= of_translate_dcr_address(node
, *reg
, NULL
);
546 if (addr
!= OF_BAD_ADDR
) {
547 dev_set_name(dev
, "D%llx.%s",
548 (unsigned long long)addr
, node
->name
);
551 #endif /* !CONFIG_PPC_DCR_NATIVE */
553 #endif /* CONFIG_PPC_DCR */
556 * For MMIO, get the physical address
558 reg
= of_get_property(node
, "reg", NULL
);
560 addr
= of_translate_address(node
, reg
);
561 if (addr
!= OF_BAD_ADDR
) {
562 dev_set_name(dev
, "%llx.%s",
563 (unsigned long long)addr
, node
->name
);
569 * No BusID, use the node name and add a globally incremented
570 * counter (and pray...)
572 magic
= atomic_add_return(1, &bus_no_reg_magic
);
573 dev_set_name(dev
, "%s.%d", node
->name
, magic
- 1);
577 * of_device_alloc - Allocate and initialize an of_device
578 * @np: device node to assign to device
579 * @bus_id: Name to assign to the device. May be null to use default name.
580 * @parent: Parent device.
582 struct platform_device
*of_device_alloc(struct device_node
*np
,
584 struct device
*parent
)
586 struct platform_device
*dev
;
587 int rc
, i
, num_reg
= 0, num_irq
;
588 struct resource
*res
, temp_res
;
590 dev
= platform_device_alloc("", -1);
594 /* count the io and irq resources */
595 while (of_address_to_resource(np
, num_reg
, &temp_res
) == 0)
597 num_irq
= of_irq_count(np
);
599 /* Populate the resource table */
600 if (num_irq
|| num_reg
) {
601 res
= kzalloc(sizeof(*res
) * (num_irq
+ num_reg
), GFP_KERNEL
);
603 platform_device_put(dev
);
607 dev
->num_resources
= num_reg
+ num_irq
;
609 for (i
= 0; i
< num_reg
; i
++, res
++) {
610 rc
= of_address_to_resource(np
, i
, res
);
613 WARN_ON(of_irq_to_resource_table(np
, res
, num_irq
) != num_irq
);
616 dev
->dev
.of_node
= of_node_get(np
);
617 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
618 dev
->dev
.dma_mask
= &dev
->archdata
.dma_mask
;
620 dev
->dev
.parent
= parent
;
623 dev_set_name(&dev
->dev
, "%s", bus_id
);
625 of_device_make_bus_id(&dev
->dev
);
629 EXPORT_SYMBOL(of_device_alloc
);
632 * of_platform_device_create - Alloc, initialize and register an of_device
633 * @np: pointer to node to create device for
634 * @bus_id: name to assign device
635 * @parent: Linux device model parent device.
637 struct platform_device
*of_platform_device_create(struct device_node
*np
,
639 struct device
*parent
)
641 struct platform_device
*dev
;
643 dev
= of_device_alloc(np
, bus_id
, parent
);
647 #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
648 dev
->archdata
.dma_mask
= 0xffffffffUL
;
650 dev
->dev
.coherent_dma_mask
= DMA_BIT_MASK(32);
651 dev
->dev
.bus
= &platform_bus_type
;
653 /* We do not fill the DMA ops for platform devices by default.
654 * This is currently the responsibility of the platform code
655 * to do such, possibly using a device notifier
658 if (of_device_add(dev
) != 0) {
659 platform_device_put(dev
);
665 EXPORT_SYMBOL(of_platform_device_create
);
668 * of_platform_bus_create - Create an OF device for a bus node and all its
669 * children. Optionally recursively instantiate matching busses.
670 * @bus: device node of the bus to instantiate
671 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
672 * disallow recursive creation of child busses
674 static int of_platform_bus_create(const struct device_node
*bus
,
675 const struct of_device_id
*matches
,
676 struct device
*parent
)
678 struct device_node
*child
;
679 struct platform_device
*dev
;
682 for_each_child_of_node(bus
, child
) {
683 pr_debug(" create child: %s\n", child
->full_name
);
684 dev
= of_platform_device_create(child
, NULL
, parent
);
687 else if (!of_match_node(matches
, child
))
690 pr_debug(" and sub busses\n");
691 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
702 * of_platform_bus_probe - Probe the device-tree for platform busses
703 * @root: parent of the first level to probe or NULL for the root of the tree
704 * @matches: match table, NULL to use the default
705 * @parent: parent to hook devices from, NULL for toplevel
707 * Note that children of the provided root are not instantiated as devices
708 * unless the specified root itself matches the bus list and is not NULL.
710 int of_platform_bus_probe(struct device_node
*root
,
711 const struct of_device_id
*matches
,
712 struct device
*parent
)
714 struct device_node
*child
;
715 struct platform_device
*dev
;
718 if (WARN_ON(!matches
|| matches
== OF_NO_DEEP_PROBE
))
721 root
= of_find_node_by_path("/");
727 pr_debug("of_platform_bus_probe()\n");
728 pr_debug(" starting at: %s\n", root
->full_name
);
730 /* Do a self check of bus type, if there's a match, create
733 if (of_match_node(matches
, root
)) {
734 pr_debug(" root match, create all sub devices\n");
735 dev
= of_platform_device_create(root
, NULL
, parent
);
740 pr_debug(" create all sub busses\n");
741 rc
= of_platform_bus_create(root
, matches
, &dev
->dev
);
744 for_each_child_of_node(root
, child
) {
745 if (!of_match_node(matches
, child
))
748 pr_debug(" match: %s\n", child
->full_name
);
749 dev
= of_platform_device_create(child
, NULL
, parent
);
753 rc
= of_platform_bus_create(child
, matches
, &dev
->dev
);
763 EXPORT_SYMBOL(of_platform_bus_probe
);
764 #endif /* !CONFIG_SPARC */