of_device.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/slab.h>
  7. #include <asm/errno.h>
  8. #include <asm/of_device.h>
  9. /**
  10. * of_match_device - Tell if an of_device structure has a matching
  11. * of_match structure
  12. * @ids: array of of device match structures to search in
  13. * @dev: the of device structure to match against
  14. *
  15. * Used by a driver to check whether an of_device present in the
  16. * system is in its list of supported devices.
  17. */
  18. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  19. const struct of_device *dev)
  20. {
  21. if (!dev->node)
  22. return NULL;
  23. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  24. int match = 1;
  25. if (matches->name[0])
  26. match &= dev->node->name
  27. && !strcmp(matches->name, dev->node->name);
  28. if (matches->type[0])
  29. match &= dev->node->type
  30. && !strcmp(matches->type, dev->node->type);
  31. if (matches->compatible[0])
  32. match &= device_is_compatible(dev->node,
  33. matches->compatible);
  34. if (match)
  35. return matches;
  36. matches++;
  37. }
  38. return NULL;
  39. }
  40. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  41. {
  42. struct of_device * of_dev = to_of_device(dev);
  43. struct of_platform_driver * of_drv = to_of_platform_driver(drv);
  44. const struct of_device_id * matches = of_drv->match_table;
  45. if (!matches)
  46. return 0;
  47. return of_match_device(matches, of_dev) != NULL;
  48. }
  49. struct of_device *of_dev_get(struct of_device *dev)
  50. {
  51. struct device *tmp;
  52. if (!dev)
  53. return NULL;
  54. tmp = get_device(&dev->dev);
  55. if (tmp)
  56. return to_of_device(tmp);
  57. else
  58. return NULL;
  59. }
  60. void of_dev_put(struct of_device *dev)
  61. {
  62. if (dev)
  63. put_device(&dev->dev);
  64. }
  65. static int of_device_probe(struct device *dev)
  66. {
  67. int error = -ENODEV;
  68. struct of_platform_driver *drv;
  69. struct of_device *of_dev;
  70. const struct of_device_id *match;
  71. drv = to_of_platform_driver(dev->driver);
  72. of_dev = to_of_device(dev);
  73. if (!drv->probe)
  74. return error;
  75. of_dev_get(of_dev);
  76. match = of_match_device(drv->match_table, of_dev);
  77. if (match)
  78. error = drv->probe(of_dev, match);
  79. if (error)
  80. of_dev_put(of_dev);
  81. return error;
  82. }
  83. static int of_device_remove(struct device *dev)
  84. {
  85. struct of_device * of_dev = to_of_device(dev);
  86. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  87. if (dev->driver && drv->remove)
  88. drv->remove(of_dev);
  89. return 0;
  90. }
  91. static int of_device_suspend(struct device *dev, pm_message_t state)
  92. {
  93. struct of_device * of_dev = to_of_device(dev);
  94. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  95. int error = 0;
  96. if (dev->driver && drv->suspend)
  97. error = drv->suspend(of_dev, state);
  98. return error;
  99. }
  100. static int of_device_resume(struct device * dev)
  101. {
  102. struct of_device * of_dev = to_of_device(dev);
  103. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  104. int error = 0;
  105. if (dev->driver && drv->resume)
  106. error = drv->resume(of_dev);
  107. return error;
  108. }
  109. struct bus_type of_platform_bus_type = {
  110. .name = "of_platform",
  111. .match = of_platform_bus_match,
  112. .probe = of_device_probe,
  113. .remove = of_device_remove,
  114. .suspend = of_device_suspend,
  115. .resume = of_device_resume,
  116. };
  117. static int __init of_bus_driver_init(void)
  118. {
  119. return bus_register(&of_platform_bus_type);
  120. }
  121. postcore_initcall(of_bus_driver_init);
  122. int of_register_driver(struct of_platform_driver *drv)
  123. {
  124. /* initialize common driver fields */
  125. drv->driver.name = drv->name;
  126. drv->driver.bus = &of_platform_bus_type;
  127. /* register with core */
  128. return driver_register(&drv->driver);
  129. }
  130. void of_unregister_driver(struct of_platform_driver *drv)
  131. {
  132. driver_unregister(&drv->driver);
  133. }
  134. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  135. {
  136. struct of_device *ofdev;
  137. ofdev = to_of_device(dev);
  138. return sprintf(buf, "%s", ofdev->node->full_name);
  139. }
  140. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  141. /**
  142. * of_release_dev - free an of device structure when all users of it are finished.
  143. * @dev: device that's been disconnected
  144. *
  145. * Will be called only by the device core when all users of this of device are
  146. * done.
  147. */
  148. void of_release_dev(struct device *dev)
  149. {
  150. struct of_device *ofdev;
  151. ofdev = to_of_device(dev);
  152. of_node_put(ofdev->node);
  153. kfree(ofdev);
  154. }
  155. int of_device_register(struct of_device *ofdev)
  156. {
  157. int rc;
  158. BUG_ON(ofdev->node == NULL);
  159. rc = device_register(&ofdev->dev);
  160. if (rc)
  161. return rc;
  162. device_create_file(&ofdev->dev, &dev_attr_devspec);
  163. return 0;
  164. }
  165. void of_device_unregister(struct of_device *ofdev)
  166. {
  167. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  168. device_unregister(&ofdev->dev);
  169. }
  170. struct of_device* of_platform_device_create(struct device_node *np,
  171. const char *bus_id,
  172. struct device *parent)
  173. {
  174. struct of_device *dev;
  175. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  176. if (!dev)
  177. return NULL;
  178. memset(dev, 0, sizeof(*dev));
  179. dev->node = of_node_get(np);
  180. dev->dma_mask = 0xffffffffUL;
  181. dev->dev.dma_mask = &dev->dma_mask;
  182. dev->dev.parent = parent;
  183. dev->dev.bus = &of_platform_bus_type;
  184. dev->dev.release = of_release_dev;
  185. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  186. if (of_device_register(dev) != 0) {
  187. kfree(dev);
  188. return NULL;
  189. }
  190. return dev;
  191. }
  192. EXPORT_SYMBOL(of_match_device);
  193. EXPORT_SYMBOL(of_platform_bus_type);
  194. EXPORT_SYMBOL(of_register_driver);
  195. EXPORT_SYMBOL(of_unregister_driver);
  196. EXPORT_SYMBOL(of_device_register);
  197. EXPORT_SYMBOL(of_device_unregister);
  198. EXPORT_SYMBOL(of_dev_get);
  199. EXPORT_SYMBOL(of_dev_put);
  200. EXPORT_SYMBOL(of_platform_device_create);
  201. EXPORT_SYMBOL(of_release_dev);