blob: c01cd1ac7617d6a50596691748036c154143074a [file] [log] [blame]
Stephen Rothwell3f23de12007-05-03 02:38:57 +10001/*
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
7 *
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.
12 *
13 */
14#include <linux/errno.h>
Stephen Rothwell5c457082007-10-17 21:17:42 -070015#include <linux/module.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100016#include <linux/device.h>
Grant Likely5fd200f2010-06-08 07:48:13 -060017#include <linux/dma-mapping.h>
Grant Likely50ef5282010-06-08 07:48:20 -060018#include <linux/slab.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060019#include <linux/of_address.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100020#include <linux/of_device.h>
Grant Likely9e3288d2010-06-08 07:48:26 -060021#include <linux/of_irq.h>
Stephen Rothwell3f23de12007-05-03 02:38:57 +100022#include <linux/of_platform.h>
Grant Likelyeca39302010-06-08 07:48:21 -060023#include <linux/platform_device.h>
24
Jonas Bonnc6085582010-07-23 19:19:35 +020025static int of_dev_node_match(struct device *dev, void *data)
26{
27 return dev->of_node == data;
28}
29
30/**
31 * of_find_device_by_node - Find the platform_device associated with a node
32 * @np: Pointer to device tree node
33 *
34 * Returns platform_device pointer, or NULL if not found
35 */
36struct platform_device *of_find_device_by_node(struct device_node *np)
37{
38 struct device *dev;
39
40 dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
41 return dev ? to_platform_device(dev) : NULL;
42}
43EXPORT_SYMBOL(of_find_device_by_node);
44
Grant Likelyeca39302010-06-08 07:48:21 -060045static int platform_driver_probe_shim(struct platform_device *pdev)
46{
47 struct platform_driver *pdrv;
48 struct of_platform_driver *ofpdrv;
49 const struct of_device_id *match;
50
51 pdrv = container_of(pdev->dev.driver, struct platform_driver, driver);
52 ofpdrv = container_of(pdrv, struct of_platform_driver, platform_driver);
Grant Likelyc1b6d382010-07-22 10:36:28 -060053
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. */
Grant Likelyeca39302010-06-08 07:48:21 -060058 match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
Grant Likelyc1b6d382010-07-22 10:36:28 -060059 return match ? ofpdrv->probe(pdev, match) : -EINVAL;
Grant Likelyeca39302010-06-08 07:48:21 -060060}
61
62static void platform_driver_shutdown_shim(struct platform_device *pdev)
63{
64 struct platform_driver *pdrv;
65 struct of_platform_driver *ofpdrv;
66
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);
70}
71
72/**
73 * of_register_platform_driver
74 */
75int of_register_platform_driver(struct of_platform_driver *drv)
76{
Grant Likely7fb8f882010-07-29 17:55:50 -060077 char *of_name;
78
Grant Likelyeca39302010-06-08 07:48:21 -060079 /* setup of_platform_driver to platform_driver adaptors */
80 drv->platform_driver.driver = drv->driver;
Grant Likely7fb8f882010-07-29 17:55:50 -060081
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);
88 if (!of_name)
89 return -ENOMEM;
90 sprintf(of_name, "of:%s", drv->driver.name);
91 drv->platform_driver.driver.name = of_name;
92
Grant Likelyeca39302010-06-08 07:48:21 -060093 if (drv->probe)
94 drv->platform_driver.probe = platform_driver_probe_shim;
95 drv->platform_driver.remove = drv->remove;
96 if (drv->shutdown)
97 drv->platform_driver.shutdown = platform_driver_shutdown_shim;
98 drv->platform_driver.suspend = drv->suspend;
99 drv->platform_driver.resume = drv->resume;
100
101 return platform_driver_register(&drv->platform_driver);
102}
103EXPORT_SYMBOL(of_register_platform_driver);
104
105void of_unregister_platform_driver(struct of_platform_driver *drv)
106{
107 platform_driver_unregister(&drv->platform_driver);
Grant Likely7fb8f882010-07-29 17:55:50 -0600108 kfree(drv->platform_driver.driver.name);
109 drv->platform_driver.driver.name = NULL;
Grant Likelyeca39302010-06-08 07:48:21 -0600110}
111EXPORT_SYMBOL(of_unregister_platform_driver);
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000112
Grant Likely596c9552010-07-14 23:51:43 -0600113#if defined(CONFIG_PPC_DCR)
114#include <asm/dcr.h>
115#endif
116
Olaf Hering140b9322008-04-24 23:16:00 +1000117extern struct device_attribute of_platform_device_attrs[];
118
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000119static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
120{
Grant Likely597b9d12010-04-13 16:13:01 -0700121 const struct of_device_id *matches = drv->of_match_table;
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000122
123 if (!matches)
124 return 0;
125
Grant Likely44504b22010-04-13 16:13:22 -0700126 return of_match_device(matches, dev) != NULL;
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000127}
128
129static int of_platform_device_probe(struct device *dev)
130{
131 int error = -ENODEV;
132 struct of_platform_driver *drv;
Grant Likely94a0cb12010-07-22 13:59:23 -0600133 struct platform_device *of_dev;
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000134 const struct of_device_id *match;
135
136 drv = to_of_platform_driver(dev->driver);
Grant Likely94a0cb12010-07-22 13:59:23 -0600137 of_dev = to_platform_device(dev);
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000138
139 if (!drv->probe)
140 return error;
141
142 of_dev_get(of_dev);
143
Grant Likely44504b22010-04-13 16:13:22 -0700144 match = of_match_device(drv->driver.of_match_table, dev);
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000145 if (match)
146 error = drv->probe(of_dev, match);
147 if (error)
148 of_dev_put(of_dev);
149
150 return error;
151}
152
153static int of_platform_device_remove(struct device *dev)
154{
Grant Likely94a0cb12010-07-22 13:59:23 -0600155 struct platform_device *of_dev = to_platform_device(dev);
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000156 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
157
158 if (dev->driver && drv->remove)
159 drv->remove(of_dev);
160 return 0;
161}
162
Michael Ellermana09ad3c2008-01-25 16:59:13 +1100163static void of_platform_device_shutdown(struct device *dev)
164{
Grant Likely94a0cb12010-07-22 13:59:23 -0600165 struct platform_device *of_dev = to_platform_device(dev);
Michael Ellermana09ad3c2008-01-25 16:59:13 +1100166 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
167
168 if (dev->driver && drv->shutdown)
169 drv->shutdown(of_dev);
170}
171
Anton Vorontsovd35ef902009-10-12 05:50:41 +0000172#ifdef CONFIG_PM_SLEEP
173
174static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
175{
Grant Likely94a0cb12010-07-22 13:59:23 -0600176 struct platform_device *of_dev = to_platform_device(dev);
Anton Vorontsovd35ef902009-10-12 05:50:41 +0000177 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
178 int ret = 0;
179
180 if (dev->driver && drv->suspend)
181 ret = drv->suspend(of_dev, mesg);
182 return ret;
183}
184
185static int of_platform_legacy_resume(struct device *dev)
186{
Grant Likely94a0cb12010-07-22 13:59:23 -0600187 struct platform_device *of_dev = to_platform_device(dev);
Anton Vorontsovd35ef902009-10-12 05:50:41 +0000188 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
189 int ret = 0;
190
191 if (dev->driver && drv->resume)
192 ret = drv->resume(of_dev);
193 return ret;
194}
195
196static int of_platform_pm_prepare(struct device *dev)
197{
198 struct device_driver *drv = dev->driver;
199 int ret = 0;
200
201 if (drv && drv->pm && drv->pm->prepare)
202 ret = drv->pm->prepare(dev);
203
204 return ret;
205}
206
207static void of_platform_pm_complete(struct device *dev)
208{
209 struct device_driver *drv = dev->driver;
210
211 if (drv && drv->pm && drv->pm->complete)
212 drv->pm->complete(dev);
213}
214
215#ifdef CONFIG_SUSPEND
216
217static int of_platform_pm_suspend(struct device *dev)
218{
219 struct device_driver *drv = dev->driver;
220 int ret = 0;
221
222 if (!drv)
223 return 0;
224
225 if (drv->pm) {
226 if (drv->pm->suspend)
227 ret = drv->pm->suspend(dev);
228 } else {
229 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
230 }
231
232 return ret;
233}
234
235static int of_platform_pm_suspend_noirq(struct device *dev)
236{
237 struct device_driver *drv = dev->driver;
238 int ret = 0;
239
240 if (!drv)
241 return 0;
242
243 if (drv->pm) {
244 if (drv->pm->suspend_noirq)
245 ret = drv->pm->suspend_noirq(dev);
246 }
247
248 return ret;
249}
250
251static int of_platform_pm_resume(struct device *dev)
252{
253 struct device_driver *drv = dev->driver;
254 int ret = 0;
255
256 if (!drv)
257 return 0;
258
259 if (drv->pm) {
260 if (drv->pm->resume)
261 ret = drv->pm->resume(dev);
262 } else {
263 ret = of_platform_legacy_resume(dev);
264 }
265
266 return ret;
267}
268
269static int of_platform_pm_resume_noirq(struct device *dev)
270{
271 struct device_driver *drv = dev->driver;
272 int ret = 0;
273
274 if (!drv)
275 return 0;
276
277 if (drv->pm) {
278 if (drv->pm->resume_noirq)
279 ret = drv->pm->resume_noirq(dev);
280 }
281
282 return ret;
283}
284
285#else /* !CONFIG_SUSPEND */
286
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
291
292#endif /* !CONFIG_SUSPEND */
293
294#ifdef CONFIG_HIBERNATION
295
296static int of_platform_pm_freeze(struct device *dev)
297{
298 struct device_driver *drv = dev->driver;
299 int ret = 0;
300
301 if (!drv)
302 return 0;
303
304 if (drv->pm) {
305 if (drv->pm->freeze)
306 ret = drv->pm->freeze(dev);
307 } else {
308 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
309 }
310
311 return ret;
312}
313
314static int of_platform_pm_freeze_noirq(struct device *dev)
315{
316 struct device_driver *drv = dev->driver;
317 int ret = 0;
318
319 if (!drv)
320 return 0;
321
322 if (drv->pm) {
323 if (drv->pm->freeze_noirq)
324 ret = drv->pm->freeze_noirq(dev);
325 }
326
327 return ret;
328}
329
330static int of_platform_pm_thaw(struct device *dev)
331{
332 struct device_driver *drv = dev->driver;
333 int ret = 0;
334
335 if (!drv)
336 return 0;
337
338 if (drv->pm) {
339 if (drv->pm->thaw)
340 ret = drv->pm->thaw(dev);
341 } else {
342 ret = of_platform_legacy_resume(dev);
343 }
344
345 return ret;
346}
347
348static int of_platform_pm_thaw_noirq(struct device *dev)
349{
350 struct device_driver *drv = dev->driver;
351 int ret = 0;
352
353 if (!drv)
354 return 0;
355
356 if (drv->pm) {
357 if (drv->pm->thaw_noirq)
358 ret = drv->pm->thaw_noirq(dev);
359 }
360
361 return ret;
362}
363
364static int of_platform_pm_poweroff(struct device *dev)
365{
366 struct device_driver *drv = dev->driver;
367 int ret = 0;
368
369 if (!drv)
370 return 0;
371
372 if (drv->pm) {
373 if (drv->pm->poweroff)
374 ret = drv->pm->poweroff(dev);
375 } else {
376 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
377 }
378
379 return ret;
380}
381
382static int of_platform_pm_poweroff_noirq(struct device *dev)
383{
384 struct device_driver *drv = dev->driver;
385 int ret = 0;
386
387 if (!drv)
388 return 0;
389
390 if (drv->pm) {
391 if (drv->pm->poweroff_noirq)
392 ret = drv->pm->poweroff_noirq(dev);
393 }
394
395 return ret;
396}
397
398static int of_platform_pm_restore(struct device *dev)
399{
400 struct device_driver *drv = dev->driver;
401 int ret = 0;
402
403 if (!drv)
404 return 0;
405
406 if (drv->pm) {
407 if (drv->pm->restore)
408 ret = drv->pm->restore(dev);
409 } else {
410 ret = of_platform_legacy_resume(dev);
411 }
412
413 return ret;
414}
415
416static int of_platform_pm_restore_noirq(struct device *dev)
417{
418 struct device_driver *drv = dev->driver;
419 int ret = 0;
420
421 if (!drv)
422 return 0;
423
424 if (drv->pm) {
425 if (drv->pm->restore_noirq)
426 ret = drv->pm->restore_noirq(dev);
427 }
428
429 return ret;
430}
431
432#else /* !CONFIG_HIBERNATION */
433
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
442
443#endif /* !CONFIG_HIBERNATION */
444
445static 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,
460};
461
462#define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
463
464#else /* !CONFIG_PM_SLEEP */
465
466#define OF_PLATFORM_PM_OPS_PTR NULL
467
468#endif /* !CONFIG_PM_SLEEP */
469
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000470int of_bus_type_init(struct bus_type *bus, const char *name)
471{
472 bus->name = name;
473 bus->match = of_platform_bus_match;
474 bus->probe = of_platform_device_probe;
475 bus->remove = of_platform_device_remove;
Michael Ellermana09ad3c2008-01-25 16:59:13 +1100476 bus->shutdown = of_platform_device_shutdown;
Olaf Hering140b9322008-04-24 23:16:00 +1000477 bus->dev_attrs = of_platform_device_attrs;
Anton Vorontsovd35ef902009-10-12 05:50:41 +0000478 bus->pm = OF_PLATFORM_PM_OPS_PTR;
Stephen Rothwell3f23de12007-05-03 02:38:57 +1000479 return bus_register(bus);
480}
Stephen Rothwell5c457082007-10-17 21:17:42 -0700481
482int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
483{
Grant Likelyeca39302010-06-08 07:48:21 -0600484 /*
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.
488 *
489 * After all of_platform_bus_type drivers are converted to
490 * platform_drivers, this exception can be removed.
491 */
492 if (bus == &platform_bus_type)
493 return of_register_platform_driver(drv);
Stephen Rothwell5c457082007-10-17 21:17:42 -0700494
495 /* register with core */
Grant Likelyeca39302010-06-08 07:48:21 -0600496 drv->driver.bus = bus;
Stephen Rothwell5c457082007-10-17 21:17:42 -0700497 return driver_register(&drv->driver);
498}
499EXPORT_SYMBOL(of_register_driver);
500
501void of_unregister_driver(struct of_platform_driver *drv)
502{
Grant Likelyeca39302010-06-08 07:48:21 -0600503 if (drv->driver.bus == &platform_bus_type)
504 of_unregister_platform_driver(drv);
505 else
506 driver_unregister(&drv->driver);
Stephen Rothwell5c457082007-10-17 21:17:42 -0700507}
508EXPORT_SYMBOL(of_unregister_driver);
Grant Likely5fd200f2010-06-08 07:48:13 -0600509
510#if !defined(CONFIG_SPARC)
511/*
512 * The following routines scan a subtree and registers a device for
513 * each applicable node.
514 *
515 * Note: sparc doesn't use these routines because it has a different
516 * mechanism for creating devices from device tree nodes.
517 */
518
519/**
Grant Likely94c09312010-06-08 07:48:14 -0600520 * 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
522 *
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.
526 */
Grant Likelyc6601222010-07-23 15:04:01 -0600527void of_device_make_bus_id(struct device *dev)
Grant Likely94c09312010-06-08 07:48:14 -0600528{
529 static atomic_t bus_no_reg_magic;
530 struct device_node *node = dev->of_node;
531 const u32 *reg;
532 u64 addr;
533 int magic;
534
535#ifdef CONFIG_PPC_DCR
536 /*
537 * If it's a DCR based device, use 'd' for native DCRs
538 * and 'D' for MMIO DCRs.
539 */
540 reg = of_get_property(node, "dcr-reg", NULL);
541 if (reg) {
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);
549 return;
550 }
551#endif /* !CONFIG_PPC_DCR_NATIVE */
552 }
553#endif /* CONFIG_PPC_DCR */
554
555 /*
556 * For MMIO, get the physical address
557 */
558 reg = of_get_property(node, "reg", NULL);
559 if (reg) {
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);
564 return;
565 }
566 }
567
568 /*
569 * No BusID, use the node name and add a globally incremented
570 * counter (and pray...)
571 */
572 magic = atomic_add_return(1, &bus_no_reg_magic);
573 dev_set_name(dev, "%s.%d", node->name, magic - 1);
574}
575
576/**
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.
581 */
Grant Likely94a0cb12010-07-22 13:59:23 -0600582struct platform_device *of_device_alloc(struct device_node *np,
Grant Likely94c09312010-06-08 07:48:14 -0600583 const char *bus_id,
584 struct device *parent)
585{
Grant Likely94a0cb12010-07-22 13:59:23 -0600586 struct platform_device *dev;
Andres Salomon52f65372010-10-10 21:35:05 -0600587 int rc, i, num_reg = 0, num_irq;
Grant Likelyac80a512010-06-08 07:48:15 -0600588 struct resource *res, temp_res;
Grant Likely94c09312010-06-08 07:48:14 -0600589
Grant Likely7096d042010-10-20 11:45:13 -0600590 dev = platform_device_alloc("", -1);
591 if (!dev)
592 return NULL;
593
594 /* count the io and irq resources */
Grant Likelyac80a512010-06-08 07:48:15 -0600595 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
596 num_reg++;
Andres Salomon52f65372010-10-10 21:35:05 -0600597 num_irq = of_irq_count(np);
Grant Likelyac80a512010-06-08 07:48:15 -0600598
Grant Likelyac80a512010-06-08 07:48:15 -0600599 /* Populate the resource table */
600 if (num_irq || num_reg) {
Grant Likely7096d042010-10-20 11:45:13 -0600601 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
602 if (!res) {
603 platform_device_put(dev);
604 return NULL;
605 }
606
Grant Likelyac80a512010-06-08 07:48:15 -0600607 dev->num_resources = num_reg + num_irq;
608 dev->resource = res;
609 for (i = 0; i < num_reg; i++, res++) {
610 rc = of_address_to_resource(np, i, res);
611 WARN_ON(rc);
612 }
Andres Salomon52f65372010-10-10 21:35:05 -0600613 WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq);
Grant Likelyac80a512010-06-08 07:48:15 -0600614 }
Grant Likely94c09312010-06-08 07:48:14 -0600615
616 dev->dev.of_node = of_node_get(np);
Grant Likely9e3288d2010-06-08 07:48:26 -0600617#if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
Grant Likely94c09312010-06-08 07:48:14 -0600618 dev->dev.dma_mask = &dev->archdata.dma_mask;
Grant Likely9e3288d2010-06-08 07:48:26 -0600619#endif
Grant Likely94c09312010-06-08 07:48:14 -0600620 dev->dev.parent = parent;
Grant Likely94c09312010-06-08 07:48:14 -0600621
622 if (bus_id)
623 dev_set_name(&dev->dev, "%s", bus_id);
624 else
625 of_device_make_bus_id(&dev->dev);
626
627 return dev;
628}
629EXPORT_SYMBOL(of_device_alloc);
630
631/**
Grant Likely5fd200f2010-06-08 07:48:13 -0600632 * 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.
Grant Likelycd1e6502011-01-03 15:51:11 -0700636 *
637 * Returns pointer to created platform device, or NULL if a device was not
638 * registered. Unavailable devices will not get registered.
Grant Likely5fd200f2010-06-08 07:48:13 -0600639 */
Grant Likely94a0cb12010-07-22 13:59:23 -0600640struct platform_device *of_platform_device_create(struct device_node *np,
Grant Likely5fd200f2010-06-08 07:48:13 -0600641 const char *bus_id,
642 struct device *parent)
643{
Grant Likely94a0cb12010-07-22 13:59:23 -0600644 struct platform_device *dev;
Grant Likely5fd200f2010-06-08 07:48:13 -0600645
Grant Likelycd1e6502011-01-03 15:51:11 -0700646 if (!of_device_is_available(np))
647 return NULL;
648
Grant Likely5fd200f2010-06-08 07:48:13 -0600649 dev = of_device_alloc(np, bus_id, parent);
650 if (!dev)
651 return NULL;
652
Grant Likely9e3288d2010-06-08 07:48:26 -0600653#if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE)
Grant Likely5fd200f2010-06-08 07:48:13 -0600654 dev->archdata.dma_mask = 0xffffffffUL;
Grant Likely9e3288d2010-06-08 07:48:26 -0600655#endif
Grant Likely5fd200f2010-06-08 07:48:13 -0600656 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Grant Likelyeca39302010-06-08 07:48:21 -0600657 dev->dev.bus = &platform_bus_type;
Grant Likely5fd200f2010-06-08 07:48:13 -0600658
659 /* We do not fill the DMA ops for platform devices by default.
660 * This is currently the responsibility of the platform code
661 * to do such, possibly using a device notifier
662 */
663
Grant Likely7096d042010-10-20 11:45:13 -0600664 if (of_device_add(dev) != 0) {
665 platform_device_put(dev);
Grant Likely5fd200f2010-06-08 07:48:13 -0600666 return NULL;
667 }
668
669 return dev;
670}
671EXPORT_SYMBOL(of_platform_device_create);
672
673/**
674 * of_platform_bus_create - Create an OF device for a bus node and all its
675 * children. Optionally recursively instantiate matching busses.
676 * @bus: device node of the bus to instantiate
677 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
678 * disallow recursive creation of child busses
679 */
680static int of_platform_bus_create(const struct device_node *bus,
681 const struct of_device_id *matches,
682 struct device *parent)
683{
684 struct device_node *child;
Grant Likely94a0cb12010-07-22 13:59:23 -0600685 struct platform_device *dev;
Grant Likely5fd200f2010-06-08 07:48:13 -0600686 int rc = 0;
687
688 for_each_child_of_node(bus, child) {
689 pr_debug(" create child: %s\n", child->full_name);
690 dev = of_platform_device_create(child, NULL, parent);
691 if (dev == NULL)
Grant Likelycd1e6502011-01-03 15:51:11 -0700692 continue;
693
694 if (!of_match_node(matches, child))
Grant Likely5fd200f2010-06-08 07:48:13 -0600695 continue;
696 if (rc == 0) {
697 pr_debug(" and sub busses\n");
698 rc = of_platform_bus_create(child, matches, &dev->dev);
699 }
700 if (rc) {
701 of_node_put(child);
702 break;
703 }
704 }
705 return rc;
706}
707
708/**
709 * of_platform_bus_probe - Probe the device-tree for platform busses
710 * @root: parent of the first level to probe or NULL for the root of the tree
711 * @matches: match table, NULL to use the default
712 * @parent: parent to hook devices from, NULL for toplevel
713 *
714 * Note that children of the provided root are not instantiated as devices
715 * unless the specified root itself matches the bus list and is not NULL.
716 */
717int of_platform_bus_probe(struct device_node *root,
718 const struct of_device_id *matches,
719 struct device *parent)
720{
721 struct device_node *child;
Grant Likely94a0cb12010-07-22 13:59:23 -0600722 struct platform_device *dev;
Grant Likely5fd200f2010-06-08 07:48:13 -0600723 int rc = 0;
724
Jonas Bonnc0dd3942010-07-23 20:19:24 +0200725 if (WARN_ON(!matches || matches == OF_NO_DEEP_PROBE))
Grant Likely5fd200f2010-06-08 07:48:13 -0600726 return -EINVAL;
727 if (root == NULL)
728 root = of_find_node_by_path("/");
729 else
730 of_node_get(root);
Grant Likely60d59912010-06-08 07:48:25 -0600731 if (root == NULL)
732 return -EINVAL;
Grant Likely5fd200f2010-06-08 07:48:13 -0600733
734 pr_debug("of_platform_bus_probe()\n");
735 pr_debug(" starting at: %s\n", root->full_name);
736
737 /* Do a self check of bus type, if there's a match, create
738 * children
739 */
740 if (of_match_node(matches, root)) {
741 pr_debug(" root match, create all sub devices\n");
742 dev = of_platform_device_create(root, NULL, parent);
Grant Likelycd1e6502011-01-03 15:51:11 -0700743 if (dev == NULL)
Grant Likely5fd200f2010-06-08 07:48:13 -0600744 goto bail;
Grant Likelycd1e6502011-01-03 15:51:11 -0700745
Grant Likely5fd200f2010-06-08 07:48:13 -0600746 pr_debug(" create all sub busses\n");
747 rc = of_platform_bus_create(root, matches, &dev->dev);
748 goto bail;
749 }
750 for_each_child_of_node(root, child) {
751 if (!of_match_node(matches, child))
752 continue;
753
754 pr_debug(" match: %s\n", child->full_name);
755 dev = of_platform_device_create(child, NULL, parent);
756 if (dev == NULL)
Grant Likelycd1e6502011-01-03 15:51:11 -0700757 continue;
758
759 rc = of_platform_bus_create(child, matches, &dev->dev);
Grant Likely5fd200f2010-06-08 07:48:13 -0600760 if (rc) {
761 of_node_put(child);
762 break;
763 }
764 }
765 bail:
766 of_node_put(root);
767 return rc;
768}
769EXPORT_SYMBOL(of_platform_bus_probe);
770#endif /* !CONFIG_SPARC */