drm_of.c 6.7 KB

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