of/device: populate platform_device (of_device) resource table on allocation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / of / platform.c
blob5cc9a8e91be56ccd90c3d86735886febb5267abc
1 /*
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/of_device.h>
19 #include <linux/of_platform.h>
21 extern struct device_attribute of_platform_device_attrs[];
23 static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
25 const struct of_device_id *matches = drv->of_match_table;
27 if (!matches)
28 return 0;
30 return of_match_device(matches, dev) != NULL;
33 static int of_platform_device_probe(struct device *dev)
35 int error = -ENODEV;
36 struct of_platform_driver *drv;
37 struct of_device *of_dev;
38 const struct of_device_id *match;
40 drv = to_of_platform_driver(dev->driver);
41 of_dev = to_of_device(dev);
43 if (!drv->probe)
44 return error;
46 of_dev_get(of_dev);
48 match = of_match_device(drv->driver.of_match_table, dev);
49 if (match)
50 error = drv->probe(of_dev, match);
51 if (error)
52 of_dev_put(of_dev);
54 return error;
57 static int of_platform_device_remove(struct device *dev)
59 struct of_device *of_dev = to_of_device(dev);
60 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
62 if (dev->driver && drv->remove)
63 drv->remove(of_dev);
64 return 0;
67 static void of_platform_device_shutdown(struct device *dev)
69 struct of_device *of_dev = to_of_device(dev);
70 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
72 if (dev->driver && drv->shutdown)
73 drv->shutdown(of_dev);
76 #ifdef CONFIG_PM_SLEEP
78 static int of_platform_legacy_suspend(struct device *dev, pm_message_t mesg)
80 struct of_device *of_dev = to_of_device(dev);
81 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
82 int ret = 0;
84 if (dev->driver && drv->suspend)
85 ret = drv->suspend(of_dev, mesg);
86 return ret;
89 static int of_platform_legacy_resume(struct device *dev)
91 struct of_device *of_dev = to_of_device(dev);
92 struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
93 int ret = 0;
95 if (dev->driver && drv->resume)
96 ret = drv->resume(of_dev);
97 return ret;
100 static int of_platform_pm_prepare(struct device *dev)
102 struct device_driver *drv = dev->driver;
103 int ret = 0;
105 if (drv && drv->pm && drv->pm->prepare)
106 ret = drv->pm->prepare(dev);
108 return ret;
111 static void of_platform_pm_complete(struct device *dev)
113 struct device_driver *drv = dev->driver;
115 if (drv && drv->pm && drv->pm->complete)
116 drv->pm->complete(dev);
119 #ifdef CONFIG_SUSPEND
121 static int of_platform_pm_suspend(struct device *dev)
123 struct device_driver *drv = dev->driver;
124 int ret = 0;
126 if (!drv)
127 return 0;
129 if (drv->pm) {
130 if (drv->pm->suspend)
131 ret = drv->pm->suspend(dev);
132 } else {
133 ret = of_platform_legacy_suspend(dev, PMSG_SUSPEND);
136 return ret;
139 static int of_platform_pm_suspend_noirq(struct device *dev)
141 struct device_driver *drv = dev->driver;
142 int ret = 0;
144 if (!drv)
145 return 0;
147 if (drv->pm) {
148 if (drv->pm->suspend_noirq)
149 ret = drv->pm->suspend_noirq(dev);
152 return ret;
155 static int of_platform_pm_resume(struct device *dev)
157 struct device_driver *drv = dev->driver;
158 int ret = 0;
160 if (!drv)
161 return 0;
163 if (drv->pm) {
164 if (drv->pm->resume)
165 ret = drv->pm->resume(dev);
166 } else {
167 ret = of_platform_legacy_resume(dev);
170 return ret;
173 static int of_platform_pm_resume_noirq(struct device *dev)
175 struct device_driver *drv = dev->driver;
176 int ret = 0;
178 if (!drv)
179 return 0;
181 if (drv->pm) {
182 if (drv->pm->resume_noirq)
183 ret = drv->pm->resume_noirq(dev);
186 return ret;
189 #else /* !CONFIG_SUSPEND */
191 #define of_platform_pm_suspend NULL
192 #define of_platform_pm_resume NULL
193 #define of_platform_pm_suspend_noirq NULL
194 #define of_platform_pm_resume_noirq NULL
196 #endif /* !CONFIG_SUSPEND */
198 #ifdef CONFIG_HIBERNATION
200 static int of_platform_pm_freeze(struct device *dev)
202 struct device_driver *drv = dev->driver;
203 int ret = 0;
205 if (!drv)
206 return 0;
208 if (drv->pm) {
209 if (drv->pm->freeze)
210 ret = drv->pm->freeze(dev);
211 } else {
212 ret = of_platform_legacy_suspend(dev, PMSG_FREEZE);
215 return ret;
218 static int of_platform_pm_freeze_noirq(struct device *dev)
220 struct device_driver *drv = dev->driver;
221 int ret = 0;
223 if (!drv)
224 return 0;
226 if (drv->pm) {
227 if (drv->pm->freeze_noirq)
228 ret = drv->pm->freeze_noirq(dev);
231 return ret;
234 static int of_platform_pm_thaw(struct device *dev)
236 struct device_driver *drv = dev->driver;
237 int ret = 0;
239 if (!drv)
240 return 0;
242 if (drv->pm) {
243 if (drv->pm->thaw)
244 ret = drv->pm->thaw(dev);
245 } else {
246 ret = of_platform_legacy_resume(dev);
249 return ret;
252 static int of_platform_pm_thaw_noirq(struct device *dev)
254 struct device_driver *drv = dev->driver;
255 int ret = 0;
257 if (!drv)
258 return 0;
260 if (drv->pm) {
261 if (drv->pm->thaw_noirq)
262 ret = drv->pm->thaw_noirq(dev);
265 return ret;
268 static int of_platform_pm_poweroff(struct device *dev)
270 struct device_driver *drv = dev->driver;
271 int ret = 0;
273 if (!drv)
274 return 0;
276 if (drv->pm) {
277 if (drv->pm->poweroff)
278 ret = drv->pm->poweroff(dev);
279 } else {
280 ret = of_platform_legacy_suspend(dev, PMSG_HIBERNATE);
283 return ret;
286 static int of_platform_pm_poweroff_noirq(struct device *dev)
288 struct device_driver *drv = dev->driver;
289 int ret = 0;
291 if (!drv)
292 return 0;
294 if (drv->pm) {
295 if (drv->pm->poweroff_noirq)
296 ret = drv->pm->poweroff_noirq(dev);
299 return ret;
302 static int of_platform_pm_restore(struct device *dev)
304 struct device_driver *drv = dev->driver;
305 int ret = 0;
307 if (!drv)
308 return 0;
310 if (drv->pm) {
311 if (drv->pm->restore)
312 ret = drv->pm->restore(dev);
313 } else {
314 ret = of_platform_legacy_resume(dev);
317 return ret;
320 static int of_platform_pm_restore_noirq(struct device *dev)
322 struct device_driver *drv = dev->driver;
323 int ret = 0;
325 if (!drv)
326 return 0;
328 if (drv->pm) {
329 if (drv->pm->restore_noirq)
330 ret = drv->pm->restore_noirq(dev);
333 return ret;
336 #else /* !CONFIG_HIBERNATION */
338 #define of_platform_pm_freeze NULL
339 #define of_platform_pm_thaw NULL
340 #define of_platform_pm_poweroff NULL
341 #define of_platform_pm_restore NULL
342 #define of_platform_pm_freeze_noirq NULL
343 #define of_platform_pm_thaw_noirq NULL
344 #define of_platform_pm_poweroff_noirq NULL
345 #define of_platform_pm_restore_noirq NULL
347 #endif /* !CONFIG_HIBERNATION */
349 static struct dev_pm_ops of_platform_dev_pm_ops = {
350 .prepare = of_platform_pm_prepare,
351 .complete = of_platform_pm_complete,
352 .suspend = of_platform_pm_suspend,
353 .resume = of_platform_pm_resume,
354 .freeze = of_platform_pm_freeze,
355 .thaw = of_platform_pm_thaw,
356 .poweroff = of_platform_pm_poweroff,
357 .restore = of_platform_pm_restore,
358 .suspend_noirq = of_platform_pm_suspend_noirq,
359 .resume_noirq = of_platform_pm_resume_noirq,
360 .freeze_noirq = of_platform_pm_freeze_noirq,
361 .thaw_noirq = of_platform_pm_thaw_noirq,
362 .poweroff_noirq = of_platform_pm_poweroff_noirq,
363 .restore_noirq = of_platform_pm_restore_noirq,
366 #define OF_PLATFORM_PM_OPS_PTR (&of_platform_dev_pm_ops)
368 #else /* !CONFIG_PM_SLEEP */
370 #define OF_PLATFORM_PM_OPS_PTR NULL
372 #endif /* !CONFIG_PM_SLEEP */
374 int of_bus_type_init(struct bus_type *bus, const char *name)
376 bus->name = name;
377 bus->match = of_platform_bus_match;
378 bus->probe = of_platform_device_probe;
379 bus->remove = of_platform_device_remove;
380 bus->shutdown = of_platform_device_shutdown;
381 bus->dev_attrs = of_platform_device_attrs;
382 bus->pm = OF_PLATFORM_PM_OPS_PTR;
383 return bus_register(bus);
386 int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
388 drv->driver.bus = bus;
390 /* register with core */
391 return driver_register(&drv->driver);
393 EXPORT_SYMBOL(of_register_driver);
395 void of_unregister_driver(struct of_platform_driver *drv)
397 driver_unregister(&drv->driver);
399 EXPORT_SYMBOL(of_unregister_driver);
401 #if !defined(CONFIG_SPARC)
403 * The following routines scan a subtree and registers a device for
404 * each applicable node.
406 * Note: sparc doesn't use these routines because it has a different
407 * mechanism for creating devices from device tree nodes.
411 * of_device_make_bus_id - Use the device node data to assign a unique name
412 * @dev: pointer to device structure that is linked to a device tree node
414 * This routine will first try using either the dcr-reg or the reg property
415 * value to derive a unique name. As a last resort it will use the node
416 * name followed by a unique number.
418 static void of_device_make_bus_id(struct device *dev)
420 static atomic_t bus_no_reg_magic;
421 struct device_node *node = dev->of_node;
422 const u32 *reg;
423 u64 addr;
424 int magic;
426 #ifdef CONFIG_PPC_DCR
428 * If it's a DCR based device, use 'd' for native DCRs
429 * and 'D' for MMIO DCRs.
431 reg = of_get_property(node, "dcr-reg", NULL);
432 if (reg) {
433 #ifdef CONFIG_PPC_DCR_NATIVE
434 dev_set_name(dev, "d%x.%s", *reg, node->name);
435 #else /* CONFIG_PPC_DCR_NATIVE */
436 u64 addr = of_translate_dcr_address(node, *reg, NULL);
437 if (addr != OF_BAD_ADDR) {
438 dev_set_name(dev, "D%llx.%s",
439 (unsigned long long)addr, node->name);
440 return;
442 #endif /* !CONFIG_PPC_DCR_NATIVE */
444 #endif /* CONFIG_PPC_DCR */
447 * For MMIO, get the physical address
449 reg = of_get_property(node, "reg", NULL);
450 if (reg) {
451 addr = of_translate_address(node, reg);
452 if (addr != OF_BAD_ADDR) {
453 dev_set_name(dev, "%llx.%s",
454 (unsigned long long)addr, node->name);
455 return;
460 * No BusID, use the node name and add a globally incremented
461 * counter (and pray...)
463 magic = atomic_add_return(1, &bus_no_reg_magic);
464 dev_set_name(dev, "%s.%d", node->name, magic - 1);
468 * of_device_alloc - Allocate and initialize an of_device
469 * @np: device node to assign to device
470 * @bus_id: Name to assign to the device. May be null to use default name.
471 * @parent: Parent device.
473 struct of_device *of_device_alloc(struct device_node *np,
474 const char *bus_id,
475 struct device *parent)
477 struct of_device *dev;
478 int rc, i, num_reg = 0, num_irq = 0;
479 struct resource *res, temp_res;
481 /* First count how many resources are needed */
482 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
483 num_reg++;
484 while (of_irq_to_resource(np, num_irq, &temp_res) != NO_IRQ)
485 num_irq++;
487 /* Allocate memory for both the struct device and the resource table */
488 dev = kzalloc(sizeof(*dev) + (sizeof(*res) * (num_reg + num_irq)),
489 GFP_KERNEL);
490 if (!dev)
491 return NULL;
492 res = (struct resource *) &dev[1];
494 /* Populate the resource table */
495 if (num_irq || num_reg) {
496 dev->num_resources = num_reg + num_irq;
497 dev->resource = res;
498 for (i = 0; i < num_reg; i++, res++) {
499 rc = of_address_to_resource(np, i, res);
500 WARN_ON(rc);
502 for (i = 0; i < num_irq; i++, res++) {
503 rc = of_irq_to_resource(np, i, res);
504 WARN_ON(rc == NO_IRQ);
508 dev->dev.of_node = of_node_get(np);
509 dev->dev.dma_mask = &dev->archdata.dma_mask;
510 dev->dev.parent = parent;
511 dev->dev.release = of_release_dev;
513 if (bus_id)
514 dev_set_name(&dev->dev, "%s", bus_id);
515 else
516 of_device_make_bus_id(&dev->dev);
518 return dev;
520 EXPORT_SYMBOL(of_device_alloc);
523 * of_platform_device_create - Alloc, initialize and register an of_device
524 * @np: pointer to node to create device for
525 * @bus_id: name to assign device
526 * @parent: Linux device model parent device.
528 struct of_device *of_platform_device_create(struct device_node *np,
529 const char *bus_id,
530 struct device *parent)
532 struct of_device *dev;
534 dev = of_device_alloc(np, bus_id, parent);
535 if (!dev)
536 return NULL;
538 dev->archdata.dma_mask = 0xffffffffUL;
539 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
540 dev->dev.bus = &of_platform_bus_type;
542 /* We do not fill the DMA ops for platform devices by default.
543 * This is currently the responsibility of the platform code
544 * to do such, possibly using a device notifier
547 if (of_device_register(dev) != 0) {
548 of_device_free(dev);
549 return NULL;
552 return dev;
554 EXPORT_SYMBOL(of_platform_device_create);
557 * of_platform_bus_create - Create an OF device for a bus node and all its
558 * children. Optionally recursively instantiate matching busses.
559 * @bus: device node of the bus to instantiate
560 * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
561 * disallow recursive creation of child busses
563 static int of_platform_bus_create(const struct device_node *bus,
564 const struct of_device_id *matches,
565 struct device *parent)
567 struct device_node *child;
568 struct of_device *dev;
569 int rc = 0;
571 for_each_child_of_node(bus, child) {
572 pr_debug(" create child: %s\n", child->full_name);
573 dev = of_platform_device_create(child, NULL, parent);
574 if (dev == NULL)
575 rc = -ENOMEM;
576 else if (!of_match_node(matches, child))
577 continue;
578 if (rc == 0) {
579 pr_debug(" and sub busses\n");
580 rc = of_platform_bus_create(child, matches, &dev->dev);
582 if (rc) {
583 of_node_put(child);
584 break;
587 return rc;
591 * of_platform_bus_probe - Probe the device-tree for platform busses
592 * @root: parent of the first level to probe or NULL for the root of the tree
593 * @matches: match table, NULL to use the default
594 * @parent: parent to hook devices from, NULL for toplevel
596 * Note that children of the provided root are not instantiated as devices
597 * unless the specified root itself matches the bus list and is not NULL.
599 int of_platform_bus_probe(struct device_node *root,
600 const struct of_device_id *matches,
601 struct device *parent)
603 struct device_node *child;
604 struct of_device *dev;
605 int rc = 0;
607 if (matches == NULL)
608 matches = of_default_bus_ids;
609 if (matches == OF_NO_DEEP_PROBE)
610 return -EINVAL;
611 if (root == NULL)
612 root = of_find_node_by_path("/");
613 else
614 of_node_get(root);
616 pr_debug("of_platform_bus_probe()\n");
617 pr_debug(" starting at: %s\n", root->full_name);
619 /* Do a self check of bus type, if there's a match, create
620 * children
622 if (of_match_node(matches, root)) {
623 pr_debug(" root match, create all sub devices\n");
624 dev = of_platform_device_create(root, NULL, parent);
625 if (dev == NULL) {
626 rc = -ENOMEM;
627 goto bail;
629 pr_debug(" create all sub busses\n");
630 rc = of_platform_bus_create(root, matches, &dev->dev);
631 goto bail;
633 for_each_child_of_node(root, child) {
634 if (!of_match_node(matches, child))
635 continue;
637 pr_debug(" match: %s\n", child->full_name);
638 dev = of_platform_device_create(child, NULL, parent);
639 if (dev == NULL)
640 rc = -ENOMEM;
641 else
642 rc = of_platform_bus_create(child, matches, &dev->dev);
643 if (rc) {
644 of_node_put(child);
645 break;
648 bail:
649 of_node_put(root);
650 return rc;
652 EXPORT_SYMBOL(of_platform_bus_probe);
653 #endif /* !CONFIG_SPARC */