drm_of.c 5.2 KB

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