44
43
EXPORT_SYMBOL(of_find_device_by_node);
46
static int platform_driver_probe_shim(struct platform_device *pdev)
48
struct platform_driver *pdrv;
49
struct of_platform_driver *ofpdrv;
50
const struct of_device_id *match;
52
pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
53
ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
55
/* There is an unlikely chance that an of_platform driver might match
56
* on a non-OF platform device. If so, then of_match_device() will
57
* come up empty. Return -EINVAL in this case so other drivers get
58
* the chance to bind. */
59
match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
60
return match ? ofpdrv->probe(pdev, match) : -EINVAL;
63
static void platform_driver_shutdown_shim(struct platform_device *pdev)
65
struct platform_driver *pdrv;
66
struct of_platform_driver *ofpdrv;
68
pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
69
ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
70
ofpdrv->shutdown(pdev);
74
* of_register_platform_driver
76
int of_register_platform_driver(struct of_platform_driver *drv)
80
/* setup of_platform_driver to platform_driver adaptors */
81
drv->platform_driver.driver = drv->driver;
83
/* Prefix the driver name with 'of:' to avoid namespace collisions
84
* and bogus matches. There are some drivers in the tree that
85
* register both an of_platform_driver and a platform_driver with
86
* the same name. This is a temporary measure until they are all
87
* cleaned up --gcl July 29, 2010 */
88
of_name = kmalloc(strlen(drv->driver.name) + 5, GFP_KERNEL);
91
sprintf(of_name, "of:%s", drv->driver.name);
92
drv->platform_driver.driver.name = of_name;
95
drv->platform_driver.probe = platform_driver_probe_shim;
96
drv->platform_driver.remove = drv->remove;
98
drv->platform_driver.shutdown = platform_driver_shutdown_shim;
99
drv->platform_driver.suspend = drv->suspend;
100
drv->platform_driver.resume = drv->resume;
102
return platform_driver_register(&drv->platform_driver);
104
EXPORT_SYMBOL(of_register_platform_driver);
106
void of_unregister_platform_driver(struct of_platform_driver *drv)
108
platform_driver_unregister(&drv->platform_driver);
109
kfree(drv->platform_driver.driver.name);
110
drv->platform_driver.driver.name = NULL;
112
EXPORT_SYMBOL(of_unregister_platform_driver);
114
45
#if defined(CONFIG_PPC_DCR)
115
46
#include <asm/dcr.h>
118
extern struct device_attribute of_platform_device_attrs[];
120
static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
122
const struct of_device_id *matches = drv->of_match_table;
127
return of_match_device(matches, dev) != NULL;
130
static int of_platform_device_probe(struct device *dev)
133
struct of_platform_driver *drv;
134
struct platform_device *of_dev;
135
const struct of_device_id *match;
137
drv = to_of_platform_driver(dev->driver);
138
of_dev = to_platform_device(dev);
145
match = of_match_device(drv->driver.of_match_table, dev);
147
error = drv->probe(of_dev, match);
154
static int of_platform_device_remove(struct device *dev)
156
struct platform_device *of_dev = to_platform_device(dev);
157
struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
159
if (dev->driver && drv->remove)
164
static void of_platform_device_shutdown(struct device *dev)
166
struct platform_device *of_dev = to_platform_device(dev);
167
struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
169
if (dev->driver && drv->shutdown)
170
drv->shutdown(of_dev);
173
#ifdef CONFIG_PM_SLEEP
175
static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
177
struct platform_device *of_dev = to_platform_device(dev);
178
struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
181
if (dev->driver && drv->suspend)
182
ret = drv->suspend(of_dev, mesg);
186
static int of_platform_legacy_resume(struct device *dev)
188
struct platform_device *of_dev = to_platform_device(dev);
189
struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
192
if (dev->driver && drv->resume)
193
ret = drv->resume(of_dev);
197
static int of_platform_pm_prepare(struct device *dev)
199
struct device_driver *drv = dev->driver;
202
if (drv && drv->pm && drv->pm->prepare)
203
ret = drv->pm->prepare(dev);
208
static void of_platform_pm_complete(struct device *dev)
210
struct device_driver *drv = dev->driver;
212
if (drv && drv->pm && drv->pm->complete)
213
drv->pm->complete(dev);
216
#ifdef CONFIG_SUSPEND
218
static int of_platform_pm_suspend(struct device *dev)
220
struct device_driver *drv = dev->driver;
227
if (drv->pm->suspend)
228
ret = drv->pm->suspend(dev);
230
ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
236
static int of_platform_pm_suspend_noirq(struct device *dev)
238
struct device_driver *drv = dev->driver;
245
if (drv->pm->suspend_noirq)
246
ret = drv->pm->suspend_noirq(dev);
252
static int of_platform_pm_resume(struct device *dev)
254
struct device_driver *drv = dev->driver;
262
ret = drv->pm->resume(dev);
264
ret = of_platform_legacy_resume(dev);
270
static int of_platform_pm_resume_noirq(struct device *dev)
272
struct device_driver *drv = dev->driver;
279
if (drv->pm->resume_noirq)
280
ret = drv->pm->resume_noirq(dev);
286
#else /* !CONFIG_SUSPEND */
288
#define of_platform_pm_suspend NULL
289
#define of_platform_pm_resume NULL
290
#define of_platform_pm_suspend_noirq NULL
291
#define of_platform_pm_resume_noirq NULL
293
#endif /* !CONFIG_SUSPEND */
295
#ifdef CONFIG_HIBERNATION
297
static int of_platform_pm_freeze(struct device *dev)
299
struct device_driver *drv = dev->driver;
307
ret = drv->pm->freeze(dev);
309
ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
315
static int of_platform_pm_freeze_noirq(struct device *dev)
317
struct device_driver *drv = dev->driver;
324
if (drv->pm->freeze_noirq)
325
ret = drv->pm->freeze_noirq(dev);
331
static int of_platform_pm_thaw(struct device *dev)
333
struct device_driver *drv = dev->driver;
341
ret = drv->pm->thaw(dev);
343
ret = of_platform_legacy_resume(dev);
349
static int of_platform_pm_thaw_noirq(struct device *dev)
351
struct device_driver *drv = dev->driver;
358
if (drv->pm->thaw_noirq)
359
ret = drv->pm->thaw_noirq(dev);
365
static int of_platform_pm_poweroff(struct device *dev)
367
struct device_driver *drv = dev->driver;
374
if (drv->pm->poweroff)
375
ret = drv->pm->poweroff(dev);
377
ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
383
static int of_platform_pm_poweroff_noirq(struct device *dev)
385
struct device_driver *drv = dev->driver;
392
if (drv->pm->poweroff_noirq)
393
ret = drv->pm->poweroff_noirq(dev);
399
static int of_platform_pm_restore(struct device *dev)
401
struct device_driver *drv = dev->driver;
408
if (drv->pm->restore)
409
ret = drv->pm->restore(dev);
411
ret = of_platform_legacy_resume(dev);
417
static int of_platform_pm_restore_noirq(struct device *dev)
419
struct device_driver *drv = dev->driver;
426
if (drv->pm->restore_noirq)
427
ret = drv->pm->restore_noirq(dev);
433
#else /* !CONFIG_HIBERNATION */
435
#define of_platform_pm_freeze NULL
436
#define of_platform_pm_thaw NULL
437
#define of_platform_pm_poweroff NULL
438
#define of_platform_pm_restore NULL
439
#define of_platform_pm_freeze_noirq NULL
440
#define of_platform_pm_thaw_noirq NULL
441
#define of_platform_pm_poweroff_noirq NULL
442
#define of_platform_pm_restore_noirq NULL
444
#endif /* !CONFIG_HIBERNATION */
446
static struct dev_pm_ops of_platform_dev_pm_ops = {
447
.prepare = of_platform_pm_prepare,
448
.complete = of_platform_pm_complete,
449
.suspend = of_platform_pm_suspend,
450
.resume = of_platform_pm_resume,
451
.freeze = of_platform_pm_freeze,
452
.thaw = of_platform_pm_thaw,
453
.poweroff = of_platform_pm_poweroff,
454
.restore = of_platform_pm_restore,
455
.suspend_noirq = of_platform_pm_suspend_noirq,
456
.resume_noirq = of_platform_pm_resume_noirq,
457
.freeze_noirq = of_platform_pm_freeze_noirq,
458
.thaw_noirq = of_platform_pm_thaw_noirq,
459
.poweroff_noirq = of_platform_pm_poweroff_noirq,
460
.restore_noirq = of_platform_pm_restore_noirq,
463
#define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
465
#else /* !CONFIG_PM_SLEEP */
467
#define OF_PLATFORM_PM_OPS_PTR NULL
469
#endif /* !CONFIG_PM_SLEEP */
471
int of_bus_type_init(struct bus_type *bus, const char *name)
474
bus->match = of_platform_bus_match;
475
bus->probe = of_platform_device_probe;
476
bus->remove = of_platform_device_remove;
477
bus->shutdown = of_platform_device_shutdown;
478
bus->dev_attrs = of_platform_device_attrs;
479
bus->pm = OF_PLATFORM_PM_OPS_PTR;
480
return bus_register(bus);
483
int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
486
* Temporary: of_platform_bus used to be distinct from the platform
487
* bus. It isn't anymore, and so drivers on the platform bus need
488
* to be registered in a special way.
490
* After all of_platform_bus_type drivers are converted to
491
* platform_drivers, this exception can be removed.
493
if (bus == &platform_bus_type)
494
return of_register_platform_driver(drv);
496
/* register with core */
497
drv->driver.bus = bus;
498
return driver_register(&drv->driver);
500
EXPORT_SYMBOL(of_register_driver);
502
void of_unregister_driver(struct of_platform_driver *drv)
504
if (drv->driver.bus == &platform_bus_type)
505
of_unregister_platform_driver(drv);
507
driver_unregister(&drv->driver);
509
EXPORT_SYMBOL(of_unregister_driver);
511
49
#if !defined(CONFIG_SPARC)
513
51
* The following routines scan a subtree and registers a device for
672
210
EXPORT_SYMBOL(of_platform_device_create);
674
struct of_platform_prepare_data {
675
struct list_head list;
676
struct device_node *node;
677
struct device *dev; /* assigned device */
680
struct resource resource[0];
683
static LIST_HEAD(of_platform_prepare_list);
684
static struct notifier_block of_platform_nb;
686
static struct of_platform_prepare_data *of_platform_find_prepare_data(
687
struct device_node *node)
689
struct of_platform_prepare_data *prep;
690
list_for_each_entry(prep, &of_platform_prepare_list, list)
691
if (prep->node == node)
696
static bool of_pdev_match_resources(struct platform_device *pdev,
697
struct of_platform_prepare_data *prep)
699
struct resource *node_res = prep->resource;
700
struct resource *pdev_res;
703
if (prep->num_resources == 0 || pdev->num_resources == 0)
706
dev_dbg(&pdev->dev, "compare dt node %s\n", prep->node->full_name);
708
/* Compare both resource tables and make sure every node resource
709
* is represented by the platform device. Here we check that each
710
* resource has corresponding entry with the same type and start
711
* values, and the end value falls inside the range specified
712
* in the device tree node. */
713
for (i = 0; i < prep->num_resources; i++, node_res++) {
714
pr_debug(" node res %2i:%.8x..%.8x[%lx]...\n", i,
715
node_res->start, node_res->end, node_res->flags);
716
pdev_res = pdev->resource;
717
for (j = 0; j < pdev->num_resources; j++, pdev_res++) {
718
pr_debug(" pdev res %2i:%.8x..%.8x[%lx]\n", j,
719
pdev_res->start, pdev_res->end, pdev_res->flags);
720
if ((pdev_res->start == node_res->start) &&
721
(pdev_res->end >= node_res->start) &&
722
(pdev_res->end <= node_res->end) &&
723
(pdev_res->flags == node_res->flags)) {
724
pr_debug(" ...MATCH! :-)\n");
728
if (j >= pdev->num_resources)
734
static int of_platform_device_notifier_call(struct notifier_block *nb,
735
unsigned long event, void *_dev)
737
struct platform_device *pdev = to_platform_device(_dev);
738
struct of_platform_prepare_data *prep;
741
case BUS_NOTIFY_ADD_DEVICE:
742
if (pdev->dev.of_node)
745
list_for_each_entry(prep, &of_platform_prepare_list, list) {
749
if (!of_pdev_match_resources(pdev, prep))
752
/* If disabled, don't let the device bind */
753
if (!of_device_is_available(prep->node)) {
754
char buf[strlen(pdev->name) + 12];
755
dev_info(&pdev->dev, "disabled by dt node %s\n",
756
prep->node->full_name);
757
sprintf(buf, "%s-disabled", pdev->name);
758
pdev->name = kstrdup(buf, GFP_KERNEL);
762
dev_info(&pdev->dev, "attaching dt node %s\n",
763
prep->node->full_name);
764
prep->dev = get_device(&pdev->dev);
765
pdev->dev.of_node = of_node_get(prep->node);
770
case BUS_NOTIFY_DEL_DEVICE:
771
list_for_each_entry(prep, &of_platform_prepare_list, list) {
772
if (prep->dev == &pdev->dev) {
773
dev_info(&pdev->dev, "detaching dt node %s\n",
774
prep->node->full_name);
775
of_node_put(pdev->dev.of_node);
776
put_device(prep->dev);
777
pdev->dev.of_node = NULL;
789
* of_platform_prepare - Flag nodes to be used for creating devices
790
* @root: parent of the first level to probe or NULL for the root of the tree
791
* @bus_match: match table for child bus nodes, or NULL
793
* This function sets up 'snooping' of device tree registrations and
794
* when a device registration is found that matches a node in the
795
* device tree, it populates the platform_device with a pointer to the
798
* A bus notifier is used to implement this behaviour. When this
799
* function is called, it will parse all the child nodes of @root and
800
* create a lookup table of eligible device nodes. A device node is
801
* considered eligible if it:
802
* a) has a compatible property,
803
* b) has memory mapped registers, and
804
* c) has a mappable interrupt.
806
* It will also recursively parse child buses providing
807
* a) the child bus node has a ranges property (children have
808
* memory-mapped registers), and
809
* b) it is compatible with the @matches list.
811
* The lookup table will be used as data for a platform bus notifier
812
* that will compare each new device registration with the table
813
* before a device driver is bound to it. If there is a match, then
814
* the of_node pointer will be added to the device. Therefore it is
815
* important to call this function *before* any platform devices get
818
void of_platform_prepare(struct device_node *root,
819
const struct of_device_id *matches)
821
struct device_node *child;
822
struct of_platform_prepare_data *prep;
824
/* register the notifier if it isn't already */
825
if (!of_platform_nb.notifier_call) {
826
of_platform_nb.notifier_call = of_platform_device_notifier_call;
827
bus_register_notifier(&platform_bus_type, &of_platform_nb);
830
/* If root is null, then start at the root of the tree */
831
root = root ? of_node_get(root) : of_find_node_by_path("/");
835
pr_debug("of_platform_prepare()\n");
836
pr_debug(" starting at: %s\n", root->full_name);
838
/* Loop over children and record the details */
839
for_each_child_of_node(root, child) {
840
struct resource *res;
841
int num_irq, num_reg, i;
843
/* If this is a bus node, recursively inspect the children,
844
* but *don't* prepare it. Prepare only concerns
845
* itself with leaf-nodes. */
846
if (of_match_node(matches, child)) {
847
of_platform_prepare(child, matches);
851
/* Is it already in the list? */
852
if (of_platform_find_prepare_data(child))
855
/* Make sure it has a compatible property */
856
if (!of_get_property(child, "compatible", NULL))
860
* Count the resources. If the device doesn't have any
861
* register ranges, then it gets skipped because there is no
862
* way to match such a device against static registration
864
num_irq = of_irq_count(child);
865
num_reg = of_address_count(child);
869
/* Device node looks valid; record the details */
870
prep = kzalloc(sizeof(*prep) +
871
(sizeof(prep->resource[0]) * (num_irq + num_reg)),
874
return; /* We're screwed if malloc doesn't work. */
876
INIT_LIST_HEAD(&prep->list);
878
res = &prep->resource[0];
879
for (i = 0; i < num_reg; i++, res++)
880
WARN_ON(of_address_to_resource(child, i, res));
881
WARN_ON(of_irq_to_resource_table(child, res, num_irq) != num_irq);
882
prep->num_resources = num_reg + num_irq;
883
prep->node = of_node_get(child);
885
list_add_tail(&prep->list, &of_platform_prepare_list);
887
pr_debug("%s() - %s prepared (%i regs, %i irqs)\n",
888
__func__, prep->node->full_name, num_reg, num_irq);
894
213
* of_platform_bus_create() - Create a device for a node and its children.
895
214
* @bus: device node of the bus to instantiate