drm_of.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <linux/component.h>
  2. #include <linux/export.h>
  3. #include <linux/list.h>
  4. #include <linux/of_graph.h>
  5. #include <drm/drmP.h>
  6. #include <drm/drm_crtc.h>
  7. #include <drm/drm_of.h>
  8. /**
  9. * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
  10. * @dev: DRM device
  11. * @port: port OF node
  12. *
  13. * Given a port OF node, return the possible mask of the corresponding
  14. * CRTC within a device's list of CRTCs. Returns zero if not found.
  15. */
  16. static uint32_t drm_crtc_port_mask(struct drm_device *dev,
  17. struct device_node *port)
  18. {
  19. unsigned int index = 0;
  20. struct drm_crtc *tmp;
  21. drm_for_each_crtc(tmp, dev) {
  22. if (tmp->port == port)
  23. return 1 << index;
  24. index++;
  25. }
  26. return 0;
  27. }
  28. /**
  29. * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
  30. * @dev: DRM device
  31. * @port: encoder port to scan for endpoints
  32. *
  33. * Scan all endpoints attached to a port, locate their attached CRTCs,
  34. * and generate the DRM mask of CRTCs which may be attached to this
  35. * encoder.
  36. *
  37. * See Documentation/devicetree/bindings/graph.txt for the bindings.
  38. */
  39. uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
  40. struct device_node *port)
  41. {
  42. struct device_node *remote_port, *ep;
  43. uint32_t possible_crtcs = 0;
  44. for_each_endpoint_of_node(port, ep) {
  45. remote_port = of_graph_get_remote_port(ep);
  46. if (!remote_port) {
  47. of_node_put(ep);
  48. return 0;
  49. }
  50. possible_crtcs |= drm_crtc_port_mask(dev, remote_port);
  51. of_node_put(remote_port);
  52. }
  53. return possible_crtcs;
  54. }
  55. EXPORT_SYMBOL(drm_of_find_possible_crtcs);
  56. /**
  57. * drm_of_component_probe - Generic probe function for a component based master
  58. * @dev: master device containing the OF node
  59. * @compare_of: compare function used for matching components
  60. * @master_ops: component master ops to be used
  61. *
  62. * Parse the platform device OF node and bind all the components associated
  63. * with the master. Interface ports are added before the encoders in order to
  64. * satisfy their .bind requirements
  65. * See Documentation/devicetree/bindings/graph.txt for the bindings.
  66. *
  67. * Returns zero if successful, or one of the standard error codes if it fails.
  68. */
  69. int drm_of_component_probe(struct device *dev,
  70. int (*compare_of)(struct device *, void *),
  71. const struct component_master_ops *m_ops)
  72. {
  73. struct device_node *ep, *port, *remote;
  74. struct component_match *match = NULL;
  75. int i;
  76. if (!dev->of_node)
  77. return -EINVAL;
  78. /*
  79. * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
  80. * called from encoder's .bind callbacks works as expected
  81. */
  82. for (i = 0; ; i++) {
  83. port = of_parse_phandle(dev->of_node, "ports", i);
  84. if (!port)
  85. break;
  86. if (!of_device_is_available(port->parent)) {
  87. of_node_put(port);
  88. continue;
  89. }
  90. component_match_add(dev, &match, compare_of, port);
  91. of_node_put(port);
  92. }
  93. if (i == 0) {
  94. dev_err(dev, "missing 'ports' property\n");
  95. return -ENODEV;
  96. }
  97. if (!match) {
  98. dev_err(dev, "no available port\n");
  99. return -ENODEV;
  100. }
  101. /*
  102. * For bound crtcs, bind the encoders attached to their remote endpoint
  103. */
  104. for (i = 0; ; i++) {
  105. port = of_parse_phandle(dev->of_node, "ports", i);
  106. if (!port)
  107. break;
  108. if (!of_device_is_available(port->parent)) {
  109. of_node_put(port);
  110. continue;
  111. }
  112. for_each_child_of_node(port, ep) {
  113. remote = of_graph_get_remote_port_parent(ep);
  114. if (!remote || !of_device_is_available(remote)) {
  115. of_node_put(remote);
  116. continue;
  117. } else if (!of_device_is_available(remote->parent)) {
  118. dev_warn(dev, "parent device of %s is not available\n",
  119. remote->full_name);
  120. of_node_put(remote);
  121. continue;
  122. }
  123. component_match_add(dev, &match, compare_of, remote);
  124. of_node_put(remote);
  125. }
  126. of_node_put(port);
  127. }
  128. return component_master_add_with_match(dev, m_ops, match);
  129. }
  130. EXPORT_SYMBOL(drm_of_component_probe);