drm_of.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. static void drm_release_of(struct device *dev, void *data)
  9. {
  10. of_node_put(data);
  11. }
  12. /**
  13. * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node
  14. * @dev: DRM device
  15. * @port: port OF node
  16. *
  17. * Given a port OF node, return the possible mask of the corresponding
  18. * CRTC within a device's list of CRTCs. Returns zero if not found.
  19. */
  20. static uint32_t drm_crtc_port_mask(struct drm_device *dev,
  21. struct device_node *port)
  22. {
  23. unsigned int index = 0;
  24. struct drm_crtc *tmp;
  25. drm_for_each_crtc(tmp, dev) {
  26. if (tmp->port == port)
  27. return 1 << index;
  28. index++;
  29. }
  30. return 0;
  31. }
  32. /**
  33. * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port
  34. * @dev: DRM device
  35. * @port: encoder port to scan for endpoints
  36. *
  37. * Scan all endpoints attached to a port, locate their attached CRTCs,
  38. * and generate the DRM mask of CRTCs which may be attached to this
  39. * encoder.
  40. *
  41. * See Documentation/devicetree/bindings/graph.txt for the bindings.
  42. */
  43. uint32_t drm_of_find_possible_crtcs(struct drm_device *dev,
  44. struct device_node *port)
  45. {
  46. struct device_node *remote_port, *ep;
  47. uint32_t possible_crtcs = 0;
  48. for_each_endpoint_of_node(port, ep) {
  49. remote_port = of_graph_get_remote_port(ep);
  50. if (!remote_port) {
  51. of_node_put(ep);
  52. return 0;
  53. }
  54. possible_crtcs |= drm_crtc_port_mask(dev, remote_port);
  55. of_node_put(remote_port);
  56. }
  57. return possible_crtcs;
  58. }
  59. EXPORT_SYMBOL(drm_of_find_possible_crtcs);
  60. /**
  61. * drm_of_component_match_add - Add a component helper OF node match rule
  62. * @master: master device
  63. * @matchptr: component match pointer
  64. * @compare: compare function used for matching component
  65. * @node: of_node
  66. */
  67. void drm_of_component_match_add(struct device *master,
  68. struct component_match **matchptr,
  69. int (*compare)(struct device *, void *),
  70. struct device_node *node)
  71. {
  72. of_node_get(node);
  73. component_match_add_release(master, matchptr, drm_release_of,
  74. compare, node);
  75. }
  76. EXPORT_SYMBOL_GPL(drm_of_component_match_add);
  77. /**
  78. * drm_of_component_probe - Generic probe function for a component based master
  79. * @dev: master device containing the OF node
  80. * @compare_of: compare function used for matching components
  81. * @master_ops: component master ops to be used
  82. *
  83. * Parse the platform device OF node and bind all the components associated
  84. * with the master. Interface ports are added before the encoders in order to
  85. * satisfy their .bind requirements
  86. * See Documentation/devicetree/bindings/graph.txt for the bindings.
  87. *
  88. * Returns zero if successful, or one of the standard error codes if it fails.
  89. */
  90. int drm_of_component_probe(struct device *dev,
  91. int (*compare_of)(struct device *, void *),
  92. const struct component_master_ops *m_ops)
  93. {
  94. struct device_node *ep, *port, *remote;
  95. struct component_match *match = NULL;
  96. int i;
  97. if (!dev->of_node)
  98. return -EINVAL;
  99. /*
  100. * Bind the crtc's ports first, so that drm_of_find_possible_crtcs()
  101. * called from encoder's .bind callbacks works as expected
  102. */
  103. for (i = 0; ; i++) {
  104. port = of_parse_phandle(dev->of_node, "ports", i);
  105. if (!port)
  106. break;
  107. if (!of_device_is_available(port->parent)) {
  108. of_node_put(port);
  109. continue;
  110. }
  111. drm_of_component_match_add(dev, &match, compare_of, port);
  112. of_node_put(port);
  113. }
  114. if (i == 0) {
  115. dev_err(dev, "missing 'ports' property\n");
  116. return -ENODEV;
  117. }
  118. if (!match) {
  119. dev_err(dev, "no available port\n");
  120. return -ENODEV;
  121. }
  122. /*
  123. * For bound crtcs, bind the encoders attached to their remote endpoint
  124. */
  125. for (i = 0; ; i++) {
  126. port = of_parse_phandle(dev->of_node, "ports", i);
  127. if (!port)
  128. break;
  129. if (!of_device_is_available(port->parent)) {
  130. of_node_put(port);
  131. continue;
  132. }
  133. for_each_child_of_node(port, ep) {
  134. remote = of_graph_get_remote_port_parent(ep);
  135. if (!remote || !of_device_is_available(remote)) {
  136. of_node_put(remote);
  137. continue;
  138. } else if (!of_device_is_available(remote->parent)) {
  139. dev_warn(dev, "parent device of %s is not available\n",
  140. remote->full_name);
  141. of_node_put(remote);
  142. continue;
  143. }
  144. drm_of_component_match_add(dev, &match, compare_of,
  145. remote);
  146. of_node_put(remote);
  147. }
  148. of_node_put(port);
  149. }
  150. return component_master_add_with_match(dev, m_ops, match);
  151. }
  152. EXPORT_SYMBOL(drm_of_component_probe);
  153. /*
  154. * drm_of_encoder_active_endpoint - return the active encoder endpoint
  155. * @node: device tree node containing encoder input ports
  156. * @encoder: drm_encoder
  157. *
  158. * Given an encoder device node and a drm_encoder with a connected crtc,
  159. * parse the encoder endpoint connecting to the crtc port.
  160. */
  161. int drm_of_encoder_active_endpoint(struct device_node *node,
  162. struct drm_encoder *encoder,
  163. struct of_endpoint *endpoint)
  164. {
  165. struct device_node *ep;
  166. struct drm_crtc *crtc = encoder->crtc;
  167. struct device_node *port;
  168. int ret;
  169. if (!node || !crtc)
  170. return -EINVAL;
  171. for_each_endpoint_of_node(node, ep) {
  172. port = of_graph_get_remote_port(ep);
  173. of_node_put(port);
  174. if (port == crtc->port) {
  175. ret = of_graph_parse_endpoint(ep, endpoint);
  176. of_node_put(ep);
  177. return ret;
  178. }
  179. }
  180. return -EINVAL;
  181. }
  182. EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint);