drm_of.c 6.5 KB

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