of_platform.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. * and Arnd Bergmann, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #undef DEBUG
  13. #include <linux/string.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/slab.h>
  19. #include <linux/pci.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/errno.h>
  24. #include <linux/topology.h>
  25. #include <asm/atomic.h>
  26. struct bus_type of_platform_bus_type = {
  27. .uevent = of_device_uevent,
  28. };
  29. EXPORT_SYMBOL(of_platform_bus_type);
  30. static int __init of_bus_driver_init(void)
  31. {
  32. return of_bus_type_init(&of_platform_bus_type, "of_platform");
  33. }
  34. postcore_initcall(of_bus_driver_init);
  35. struct of_device *of_platform_device_create(struct device_node *np,
  36. const char *bus_id,
  37. struct device *parent)
  38. {
  39. struct of_device *dev;
  40. dev = of_device_alloc(np, bus_id, parent);
  41. if (!dev)
  42. return NULL;
  43. dev->dma_mask = 0xffffffffUL;
  44. dev->dev.bus = &of_platform_bus_type;
  45. /* We do not fill the DMA ops for platform devices by default.
  46. * This is currently the responsibility of the platform code
  47. * to do such, possibly using a device notifier
  48. */
  49. if (of_device_register(dev) != 0) {
  50. of_device_free(dev);
  51. return NULL;
  52. }
  53. return dev;
  54. }
  55. EXPORT_SYMBOL(of_platform_device_create);
  56. /**
  57. * of_platform_bus_create - Create an OF device for a bus node and all its
  58. * children. Optionally recursively instanciate matching busses.
  59. * @bus: device node of the bus to instanciate
  60. * @matches: match table, NULL to use the default, OF_NO_DEEP_PROBE to
  61. * disallow recursive creation of child busses
  62. */
  63. static int of_platform_bus_create(const struct device_node *bus,
  64. const struct of_device_id *matches,
  65. struct device *parent)
  66. {
  67. struct device_node *child;
  68. struct of_device *dev;
  69. int rc = 0;
  70. for_each_child_of_node(bus, child) {
  71. pr_debug(" create child: %s\n", child->full_name);
  72. dev = of_platform_device_create(child, NULL, parent);
  73. if (dev == NULL)
  74. rc = -ENOMEM;
  75. else if (!of_match_node(matches, child))
  76. continue;
  77. if (rc == 0) {
  78. pr_debug(" and sub busses\n");
  79. rc = of_platform_bus_create(child, matches, &dev->dev);
  80. }
  81. if (rc) {
  82. of_node_put(child);
  83. break;
  84. }
  85. }
  86. return rc;
  87. }
  88. /**
  89. * of_platform_bus_probe - Probe the device-tree for platform busses
  90. * @root: parent of the first level to probe or NULL for the root of the tree
  91. * @matches: match table, NULL to use the default
  92. * @parent: parent to hook devices from, NULL for toplevel
  93. *
  94. * Note that children of the provided root are not instanciated as devices
  95. * unless the specified root itself matches the bus list and is not NULL.
  96. */
  97. int of_platform_bus_probe(struct device_node *root,
  98. const struct of_device_id *matches,
  99. struct device *parent)
  100. {
  101. struct device_node *child;
  102. struct of_device *dev;
  103. int rc = 0;
  104. if (matches == NULL)
  105. matches = of_default_bus_ids;
  106. if (matches == OF_NO_DEEP_PROBE)
  107. return -EINVAL;
  108. if (root == NULL)
  109. root = of_find_node_by_path("/");
  110. else
  111. of_node_get(root);
  112. pr_debug("of_platform_bus_probe()\n");
  113. pr_debug(" starting at: %s\n", root->full_name);
  114. /* Do a self check of bus type, if there's a match, create
  115. * children
  116. */
  117. if (of_match_node(matches, root)) {
  118. pr_debug(" root match, create all sub devices\n");
  119. dev = of_platform_device_create(root, NULL, parent);
  120. if (dev == NULL) {
  121. rc = -ENOMEM;
  122. goto bail;
  123. }
  124. pr_debug(" create all sub busses\n");
  125. rc = of_platform_bus_create(root, matches, &dev->dev);
  126. goto bail;
  127. }
  128. for_each_child_of_node(root, child) {
  129. if (!of_match_node(matches, child))
  130. continue;
  131. pr_debug(" match: %s\n", child->full_name);
  132. dev = of_platform_device_create(child, NULL, parent);
  133. if (dev == NULL)
  134. rc = -ENOMEM;
  135. else
  136. rc = of_platform_bus_create(child, matches, &dev->dev);
  137. if (rc) {
  138. of_node_put(child);
  139. break;
  140. }
  141. }
  142. bail:
  143. of_node_put(root);
  144. return rc;
  145. }
  146. EXPORT_SYMBOL(of_platform_bus_probe);
  147. static int of_dev_node_match(struct device *dev, void *data)
  148. {
  149. return to_of_device(dev)->node == data;
  150. }
  151. struct of_device *of_find_device_by_node(struct device_node *np)
  152. {
  153. struct device *dev;
  154. dev = bus_find_device(&of_platform_bus_type,
  155. NULL, np, of_dev_node_match);
  156. if (dev)
  157. return to_of_device(dev);
  158. return NULL;
  159. }
  160. EXPORT_SYMBOL(of_find_device_by_node);
  161. static int of_dev_phandle_match(struct device *dev, void *data)
  162. {
  163. phandle *ph = data;
  164. return to_of_device(dev)->node->linux_phandle == *ph;
  165. }
  166. struct of_device *of_find_device_by_phandle(phandle ph)
  167. {
  168. struct device *dev;
  169. dev = bus_find_device(&of_platform_bus_type,
  170. NULL, &ph, of_dev_phandle_match);
  171. if (dev)
  172. return to_of_device(dev);
  173. return NULL;
  174. }
  175. EXPORT_SYMBOL(of_find_device_by_phandle);