~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/of/platform.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#include <linux/device.h>
17
17
#include <linux/dma-mapping.h>
18
18
#include <linux/slab.h>
19
 
#include <linux/notifier.h>
20
19
#include <linux/of_address.h>
21
20
#include <linux/of_device.h>
22
21
#include <linux/of_irq.h>
43
42
}
44
43
EXPORT_SYMBOL(of_find_device_by_node);
45
44
 
46
 
static int platform_driver_probe_shim(struct platform_device *pdev)
47
 
{
48
 
        struct platform_driver *pdrv;
49
 
        struct of_platform_driver *ofpdrv;
50
 
        const struct of_device_id *match;
51
 
 
52
 
        pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
53
 
        ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
54
 
 
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;
61
 
}
62
 
 
63
 
static void platform_driver_shutdown_shim(struct platform_device *pdev)
64
 
{
65
 
        struct platform_driver *pdrv;
66
 
        struct of_platform_driver *ofpdrv;
67
 
 
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);
71
 
}
72
 
 
73
 
/**
74
 
 * of_register_platform_driver
75
 
 */
76
 
int of_register_platform_driver(struct of_platform_driver *drv)
77
 
{
78
 
        char *of_name;
79
 
 
80
 
        /* setup of_platform_driver to platform_driver adaptors */
81
 
        drv->platform_driver.driver = drv->driver;
82
 
 
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);
89
 
        if (!of_name)
90
 
                return -ENOMEM;
91
 
        sprintf(of_name, "of:%s", drv->driver.name);
92
 
        drv->platform_driver.driver.name = of_name;
93
 
 
94
 
        if (drv->probe)
95
 
                drv->platform_driver.probe = platform_driver_probe_shim;
96
 
        drv->platform_driver.remove = drv->remove;
97
 
        if (drv->shutdown)
98
 
                drv->platform_driver.shutdown = platform_driver_shutdown_shim;
99
 
        drv->platform_driver.suspend = drv->suspend;
100
 
        drv->platform_driver.resume = drv->resume;
101
 
 
102
 
        return platform_driver_register(&drv->platform_driver);
103
 
}
104
 
EXPORT_SYMBOL(of_register_platform_driver);
105
 
 
106
 
void of_unregister_platform_driver(struct of_platform_driver *drv)
107
 
{
108
 
        platform_driver_unregister(&drv->platform_driver);
109
 
        kfree(drv->platform_driver.driver.name);
110
 
        drv->platform_driver.driver.name = NULL;
111
 
}
112
 
EXPORT_SYMBOL(of_unregister_platform_driver);
113
 
 
114
45
#if defined(CONFIG_PPC_DCR)
115
46
#include <asm/dcr.h>
116
47
#endif
117
48
 
118
 
extern struct device_attribute of_platform_device_attrs[];
119
 
 
120
 
static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
121
 
{
122
 
        const struct of_device_id *matches = drv->of_match_table;
123
 
 
124
 
        if (!matches)
125
 
                return 0;
126
 
 
127
 
        return of_match_device(matches, dev) != NULL;
128
 
}
129
 
 
130
 
static int of_platform_device_probe(struct device *dev)
131
 
{
132
 
        int error = -ENODEV;
133
 
        struct of_platform_driver *drv;
134
 
        struct platform_device *of_dev;
135
 
        const struct of_device_id *match;
136
 
 
137
 
        drv = to_of_platform_driver(dev->driver);
138
 
        of_dev = to_platform_device(dev);
139
 
 
140
 
        if (!drv->probe)
141
 
                return error;
142
 
 
143
 
        of_dev_get(of_dev);
144
 
 
145
 
        match = of_match_device(drv->driver.of_match_table, dev);
146
 
        if (match)
147
 
                error = drv->probe(of_dev, match);
148
 
        if (error)
149
 
                of_dev_put(of_dev);
150
 
 
151
 
        return error;
152
 
}
153
 
 
154
 
static int of_platform_device_remove(struct device *dev)
155
 
{
156
 
        struct platform_device *of_dev = to_platform_device(dev);
157
 
        struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
158
 
 
159
 
        if (dev->driver && drv->remove)
160
 
                drv->remove(of_dev);
161
 
        return 0;
162
 
}
163
 
 
164
 
static void of_platform_device_shutdown(struct device *dev)
165
 
{
166
 
        struct platform_device *of_dev = to_platform_device(dev);
167
 
        struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
168
 
 
169
 
        if (dev->driver && drv->shutdown)
170
 
                drv->shutdown(of_dev);
171
 
}
172
 
 
173
 
#ifdef CONFIG_PM_SLEEP
174
 
 
175
 
static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
176
 
{
177
 
        struct platform_device *of_dev = to_platform_device(dev);
178
 
        struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
179
 
        int ret = 0;
180
 
 
181
 
        if (dev->driver && drv->suspend)
182
 
                ret = drv->suspend(of_dev, mesg);
183
 
        return ret;
184
 
}
185
 
 
186
 
static int of_platform_legacy_resume(struct device *dev)
187
 
{
188
 
        struct platform_device *of_dev = to_platform_device(dev);
189
 
        struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
190
 
        int ret = 0;
191
 
 
192
 
        if (dev->driver && drv->resume)
193
 
                ret = drv->resume(of_dev);
194
 
        return ret;
195
 
}
196
 
 
197
 
static int of_platform_pm_prepare(struct device *dev)
198
 
{
199
 
        struct device_driver *drv = dev->driver;
200
 
        int ret = 0;
201
 
 
202
 
        if (drv && drv->pm && drv->pm->prepare)
203
 
                ret = drv->pm->prepare(dev);
204
 
 
205
 
        return ret;
206
 
}
207
 
 
208
 
static void of_platform_pm_complete(struct device *dev)
209
 
{
210
 
        struct device_driver *drv = dev->driver;
211
 
 
212
 
        if (drv && drv->pm && drv->pm->complete)
213
 
                drv->pm->complete(dev);
214
 
}
215
 
 
216
 
#ifdef CONFIG_SUSPEND
217
 
 
218
 
static int of_platform_pm_suspend(struct device *dev)
219
 
{
220
 
        struct device_driver *drv = dev->driver;
221
 
        int ret = 0;
222
 
 
223
 
        if (!drv)
224
 
                return 0;
225
 
 
226
 
        if (drv->pm) {
227
 
                if (drv->pm->suspend)
228
 
                        ret = drv->pm->suspend(dev);
229
 
        } else {
230
 
                ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
231
 
        }
232
 
 
233
 
        return ret;
234
 
}
235
 
 
236
 
static int of_platform_pm_suspend_noirq(struct device *dev)
237
 
{
238
 
        struct device_driver *drv = dev->driver;
239
 
        int ret = 0;
240
 
 
241
 
        if (!drv)
242
 
                return 0;
243
 
 
244
 
        if (drv->pm) {
245
 
                if (drv->pm->suspend_noirq)
246
 
                        ret = drv->pm->suspend_noirq(dev);
247
 
        }
248
 
 
249
 
        return ret;
250
 
}
251
 
 
252
 
static int of_platform_pm_resume(struct device *dev)
253
 
{
254
 
        struct device_driver *drv = dev->driver;
255
 
        int ret = 0;
256
 
 
257
 
        if (!drv)
258
 
                return 0;
259
 
 
260
 
        if (drv->pm) {
261
 
                if (drv->pm->resume)
262
 
                        ret = drv->pm->resume(dev);
263
 
        } else {
264
 
                ret = of_platform_legacy_resume(dev);
265
 
        }
266
 
 
267
 
        return ret;
268
 
}
269
 
 
270
 
static int of_platform_pm_resume_noirq(struct device *dev)
271
 
{
272
 
        struct device_driver *drv = dev->driver;
273
 
        int ret = 0;
274
 
 
275
 
        if (!drv)
276
 
                return 0;
277
 
 
278
 
        if (drv->pm) {
279
 
                if (drv->pm->resume_noirq)
280
 
                        ret = drv->pm->resume_noirq(dev);
281
 
        }
282
 
 
283
 
        return ret;
284
 
}
285
 
 
286
 
#else /* !CONFIG_SUSPEND */
287
 
 
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
292
 
 
293
 
#endif /* !CONFIG_SUSPEND */
294
 
 
295
 
#ifdef CONFIG_HIBERNATION
296
 
 
297
 
static int of_platform_pm_freeze(struct device *dev)
298
 
{
299
 
        struct device_driver *drv = dev->driver;
300
 
        int ret = 0;
301
 
 
302
 
        if (!drv)
303
 
                return 0;
304
 
 
305
 
        if (drv->pm) {
306
 
                if (drv->pm->freeze)
307
 
                        ret = drv->pm->freeze(dev);
308
 
        } else {
309
 
                ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
310
 
        }
311
 
 
312
 
        return ret;
313
 
}
314
 
 
315
 
static int of_platform_pm_freeze_noirq(struct device *dev)
316
 
{
317
 
        struct device_driver *drv = dev->driver;
318
 
        int ret = 0;
319
 
 
320
 
        if (!drv)
321
 
                return 0;
322
 
 
323
 
        if (drv->pm) {
324
 
                if (drv->pm->freeze_noirq)
325
 
                        ret = drv->pm->freeze_noirq(dev);
326
 
        }
327
 
 
328
 
        return ret;
329
 
}
330
 
 
331
 
static int of_platform_pm_thaw(struct device *dev)
332
 
{
333
 
        struct device_driver *drv = dev->driver;
334
 
        int ret = 0;
335
 
 
336
 
        if (!drv)
337
 
                return 0;
338
 
 
339
 
        if (drv->pm) {
340
 
                if (drv->pm->thaw)
341
 
                        ret = drv->pm->thaw(dev);
342
 
        } else {
343
 
                ret = of_platform_legacy_resume(dev);
344
 
        }
345
 
 
346
 
        return ret;
347
 
}
348
 
 
349
 
static int of_platform_pm_thaw_noirq(struct device *dev)
350
 
{
351
 
        struct device_driver *drv = dev->driver;
352
 
        int ret = 0;
353
 
 
354
 
        if (!drv)
355
 
                return 0;
356
 
 
357
 
        if (drv->pm) {
358
 
                if (drv->pm->thaw_noirq)
359
 
                        ret = drv->pm->thaw_noirq(dev);
360
 
        }
361
 
 
362
 
        return ret;
363
 
}
364
 
 
365
 
static int of_platform_pm_poweroff(struct device *dev)
366
 
{
367
 
        struct device_driver *drv = dev->driver;
368
 
        int ret = 0;
369
 
 
370
 
        if (!drv)
371
 
                return 0;
372
 
 
373
 
        if (drv->pm) {
374
 
                if (drv->pm->poweroff)
375
 
                        ret = drv->pm->poweroff(dev);
376
 
        } else {
377
 
                ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
378
 
        }
379
 
 
380
 
        return ret;
381
 
}
382
 
 
383
 
static int of_platform_pm_poweroff_noirq(struct device *dev)
384
 
{
385
 
        struct device_driver *drv = dev->driver;
386
 
        int ret = 0;
387
 
 
388
 
        if (!drv)
389
 
                return 0;
390
 
 
391
 
        if (drv->pm) {
392
 
                if (drv->pm->poweroff_noirq)
393
 
                        ret = drv->pm->poweroff_noirq(dev);
394
 
        }
395
 
 
396
 
        return ret;
397
 
}
398
 
 
399
 
static int of_platform_pm_restore(struct device *dev)
400
 
{
401
 
        struct device_driver *drv = dev->driver;
402
 
        int ret = 0;
403
 
 
404
 
        if (!drv)
405
 
                return 0;
406
 
 
407
 
        if (drv->pm) {
408
 
                if (drv->pm->restore)
409
 
                        ret = drv->pm->restore(dev);
410
 
        } else {
411
 
                ret = of_platform_legacy_resume(dev);
412
 
        }
413
 
 
414
 
        return ret;
415
 
}
416
 
 
417
 
static int of_platform_pm_restore_noirq(struct device *dev)
418
 
{
419
 
        struct device_driver *drv = dev->driver;
420
 
        int ret = 0;
421
 
 
422
 
        if (!drv)
423
 
                return 0;
424
 
 
425
 
        if (drv->pm) {
426
 
                if (drv->pm->restore_noirq)
427
 
                        ret = drv->pm->restore_noirq(dev);
428
 
        }
429
 
 
430
 
        return ret;
431
 
}
432
 
 
433
 
#else /* !CONFIG_HIBERNATION */
434
 
 
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
443
 
 
444
 
#endif /* !CONFIG_HIBERNATION */
445
 
 
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,
461
 
};
462
 
 
463
 
#define OF_PLATFORM_PM_OPS_PTR  (&of_platform_dev_pm_ops)
464
 
 
465
 
#else /* !CONFIG_PM_SLEEP */
466
 
 
467
 
#define OF_PLATFORM_PM_OPS_PTR  NULL
468
 
 
469
 
#endif /* !CONFIG_PM_SLEEP */
470
 
 
471
 
int of_bus_type_init(struct bus_type *bus, const char *name)
472
 
{
473
 
        bus->name = 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);
481
 
}
482
 
 
483
 
int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
484
 
{
485
 
        /*
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.
489
 
         *
490
 
         * After all of_platform_bus_type drivers are converted to
491
 
         * platform_drivers, this exception can be removed.
492
 
         */
493
 
        if (bus == &platform_bus_type)
494
 
                return of_register_platform_driver(drv);
495
 
 
496
 
        /* register with core */
497
 
        drv->driver.bus = bus;
498
 
        return driver_register(&drv->driver);
499
 
}
500
 
EXPORT_SYMBOL(of_register_driver);
501
 
 
502
 
void of_unregister_driver(struct of_platform_driver *drv)
503
 
{
504
 
        if (drv->driver.bus == &platform_bus_type)
505
 
                of_unregister_platform_driver(drv);
506
 
        else
507
 
                driver_unregister(&drv->driver);
508
 
}
509
 
EXPORT_SYMBOL(of_unregister_driver);
510
 
 
511
49
#if !defined(CONFIG_SPARC)
512
50
/*
513
51
 * The following routines scan a subtree and registers a device for
671
209
}
672
210
EXPORT_SYMBOL(of_platform_device_create);
673
211
 
674
 
struct of_platform_prepare_data {
675
 
        struct list_head list;
676
 
        struct device_node *node;
677
 
        struct device *dev;             /* assigned device */
678
 
 
679
 
        int num_resources;
680
 
        struct resource resource[0];
681
 
};
682
 
 
683
 
static LIST_HEAD(of_platform_prepare_list);
684
 
static struct notifier_block of_platform_nb;
685
 
 
686
 
static struct of_platform_prepare_data *of_platform_find_prepare_data(
687
 
                                                struct device_node *node)
688
 
{
689
 
        struct of_platform_prepare_data *prep;
690
 
        list_for_each_entry(prep, &of_platform_prepare_list, list)
691
 
                if (prep->node == node)
692
 
                        return prep;
693
 
        return NULL;
694
 
}
695
 
 
696
 
static bool of_pdev_match_resources(struct platform_device *pdev,
697
 
                        struct of_platform_prepare_data *prep)
698
 
{
699
 
        struct resource *node_res = prep->resource;
700
 
        struct resource *pdev_res;
701
 
        int i, j;
702
 
 
703
 
        if (prep->num_resources == 0 || pdev->num_resources == 0)
704
 
                return false;
705
 
 
706
 
        dev_dbg(&pdev->dev, "compare dt node %s\n", prep->node->full_name);
707
 
 
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");
725
 
                                break;
726
 
                        }
727
 
                }
728
 
                if (j >= pdev->num_resources)
729
 
                        return false;
730
 
        }
731
 
        return true;
732
 
}
733
 
 
734
 
static int of_platform_device_notifier_call(struct notifier_block *nb,
735
 
                                        unsigned long event, void *_dev)
736
 
{
737
 
        struct platform_device *pdev = to_platform_device(_dev);
738
 
        struct of_platform_prepare_data *prep;
739
 
 
740
 
        switch (event) {
741
 
        case BUS_NOTIFY_ADD_DEVICE:
742
 
                if (pdev->dev.of_node)
743
 
                        return NOTIFY_DONE;
744
 
 
745
 
                list_for_each_entry(prep, &of_platform_prepare_list, list) {
746
 
                        if (prep->dev)
747
 
                                continue;
748
 
 
749
 
                        if (!of_pdev_match_resources(pdev, prep))
750
 
                                continue;
751
 
 
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);
759
 
                                continue;
760
 
                        }
761
 
 
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);
766
 
                        return NOTIFY_OK;
767
 
                }
768
 
                break;
769
 
 
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;
778
 
                                prep->dev = NULL;
779
 
                                return NOTIFY_OK;
780
 
                        }
781
 
                }
782
 
                break;
783
 
        }
784
 
 
785
 
        return NOTIFY_DONE;
786
 
}
787
 
 
788
 
/**
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
792
 
 *
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
796
 
 * matching node.
797
 
 *
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.
805
 
 *
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.
810
 
 *
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
816
 
 * registered.
817
 
 */
818
 
void of_platform_prepare(struct device_node *root,
819
 
                         const struct of_device_id *matches)
820
 
{
821
 
        struct device_node *child;
822
 
        struct of_platform_prepare_data *prep;
823
 
 
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);
828
 
        }
829
 
 
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("/");
832
 
        if (!root)
833
 
                return;
834
 
 
835
 
        pr_debug("of_platform_prepare()\n");
836
 
        pr_debug(" starting at: %s\n", root->full_name);
837
 
 
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;
842
 
 
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);
848
 
                        continue;
849
 
                }
850
 
 
851
 
                /* Is it already in the list? */
852
 
                if (of_platform_find_prepare_data(child))
853
 
                        continue;
854
 
 
855
 
                /* Make sure it has a compatible property */
856
 
                if (!of_get_property(child, "compatible", NULL))
857
 
                        continue;
858
 
 
859
 
                /*
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
863
 
                 */
864
 
                num_irq = of_irq_count(child);
865
 
                num_reg = of_address_count(child);
866
 
                if (!num_reg)
867
 
                        continue;
868
 
 
869
 
                /* Device node looks valid; record the details */
870
 
                prep = kzalloc(sizeof(*prep) +
871
 
                        (sizeof(prep->resource[0]) * (num_irq + num_reg)),
872
 
                        GFP_KERNEL);
873
 
                if (!prep)
874
 
                        return; /* We're screwed if malloc doesn't work. */
875
 
 
876
 
                INIT_LIST_HEAD(&prep->list);
877
 
 
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);
884
 
 
885
 
                list_add_tail(&prep->list, &of_platform_prepare_list);
886
 
 
887
 
                pr_debug("%s() - %s prepared (%i regs, %i irqs)\n",
888
 
                        __func__, prep->node->full_name, num_reg, num_irq);
889
 
        }
890
 
 
891
 
}
892
 
 
893
212
/**
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
902
221
 */
903
222
static int of_platform_bus_create(struct device_node *bus,
904
223
                                  const struct of_device_id *matches,
905
 
                                  struct device *parent, bool strict)
 
224
                                  struct device *parent)
906
225
{
907
 
        struct of_platform_prepare_data *prep;
908
226
        struct device_node *child;
909
227
        struct platform_device *dev;
910
228
        int rc = 0;
911
229
 
912
 
        /* Make sure it has a compatible property */
913
 
        if (strict && (!of_get_property(bus, "compatible", NULL))) {
914
 
                pr_debug("%s() - skipping %s, no compatible prop\n",
915
 
                         __func__, bus->full_name);
916
 
                return 0;
917
 
        }
918
 
 
919
 
        /* Has the device already been registered manually? */
920
 
        prep = of_platform_find_prepare_data(bus);
921
 
        if (prep && prep->dev) {
922
 
                pr_debug("%s() - skipping %s, already registered\n",
923
 
                         __func__, bus->full_name);
924
 
                return 0;
925
 
        }
926
 
 
927
230
        dev = of_platform_device_create(bus, NULL, parent);
928
231
        if (!dev || !of_match_node(matches, bus))
929
232
                return 0;
930
233
 
931
234
        for_each_child_of_node(bus, child) {
932
235
                pr_debug("   create child: %s\n", child->full_name);
933
 
                rc = of_platform_bus_create(child, matches, &dev->dev, strict);
 
236
                rc = of_platform_bus_create(child, matches, &dev->dev);
934
237
                if (rc) {
935
238
                        of_node_put(child);
936
239
                        break;
964
267
 
965
268
        /* Do a self check of bus type, if there's a match, create children */
966
269
        if (of_match_node(matches, root)) {
967
 
                rc = of_platform_bus_create(root, matches, parent, false);
 
270
                rc = of_platform_bus_create(root, matches, parent);
968
271
        } else for_each_child_of_node(root, child) {
969
272
                if (!of_match_node(matches, child))
970
273
                        continue;
971
 
                rc = of_platform_bus_create(child, matches, parent, false);
 
274
                rc = of_platform_bus_create(child, matches, parent);
972
275
                if (rc)
973
276
                        break;
974
277
        }
977
280
        return rc;
978
281
}
979
282
EXPORT_SYMBOL(of_platform_bus_probe);
980
 
 
981
 
/**
982
 
 * of_platform_populate() - Populate platform_devices from device tree data
983
 
 * @root: parent of the first level to probe or NULL for the root of the tree
984
 
 * @matches: match table, NULL to use the default
985
 
 * @parent: parent to hook devices from, NULL for toplevel
986
 
 *
987
 
 * Similar to of_platform_bus_probe(), this function walks the device tree
988
 
 * and creates devices from nodes.  It differs in that it follows the modern
989
 
 * convention of requiring all device nodes to have a 'compatible' property,
990
 
 * and it is suitable for creating devices which are children of the root
991
 
 * node (of_platform_bus_probe will only create children of the root which
992
 
 * are selected by the @matches argument).
993
 
 *
994
 
 * New board support should be using this function instead of
995
 
 * of_platform_bus_probe().
996
 
 *
997
 
 * Returns 0 on success, < 0 on failure.
998
 
 */
999
 
int of_platform_populate(struct device_node *root,
1000
 
                        const struct of_device_id *matches,
1001
 
                        struct device *parent)
1002
 
{
1003
 
        struct device_node *child;
1004
 
        int rc = 0;
1005
 
 
1006
 
        root = root ? of_node_get(root) : of_find_node_by_path("/");
1007
 
        if (!root)
1008
 
                return -EINVAL;
1009
 
 
1010
 
        for_each_child_of_node(root, child) {
1011
 
                rc = of_platform_bus_create(child, matches, parent, true);
1012
 
                if (rc)
1013
 
                        break;
1014
 
        }
1015
 
 
1016
 
        of_node_put(root);
1017
 
        return rc;
1018
 
}
1019
283
#endif /* !CONFIG_SPARC */