drm_of.c 6.5 KB

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