dp_drm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <drm/drm_atomic_helper.h>
  6. #include <drm/drm_atomic.h>
  7. #include <drm/drm_bridge.h>
  8. #include <drm/drm_bridge_connector.h>
  9. #include <drm/drm_crtc.h>
  10. #include "msm_drv.h"
  11. #include "msm_kms.h"
  12. #include "dp_drm.h"
  13. /**
  14. * dp_bridge_detect - callback to determine if connector is connected
  15. * @bridge: Pointer to drm bridge structure
  16. * Returns: Bridge's 'is connected' status
  17. */
  18. static enum drm_connector_status dp_bridge_detect(struct drm_bridge *bridge)
  19. {
  20. struct msm_dp *dp;
  21. dp = to_dp_bridge(bridge)->dp_display;
  22. drm_dbg_dp(dp->drm_dev, "link_ready = %s\n",
  23. (dp->link_ready) ? "true" : "false");
  24. return (dp->link_ready) ? connector_status_connected :
  25. connector_status_disconnected;
  26. }
  27. static int dp_bridge_atomic_check(struct drm_bridge *bridge,
  28. struct drm_bridge_state *bridge_state,
  29. struct drm_crtc_state *crtc_state,
  30. struct drm_connector_state *conn_state)
  31. {
  32. struct msm_dp *dp;
  33. dp = to_dp_bridge(bridge)->dp_display;
  34. drm_dbg_dp(dp->drm_dev, "link_ready = %s\n",
  35. (dp->link_ready) ? "true" : "false");
  36. /*
  37. * There is no protection in the DRM framework to check if the display
  38. * pipeline has been already disabled before trying to disable it again.
  39. * Hence if the sink is unplugged, the pipeline gets disabled, but the
  40. * crtc->active is still true. Any attempt to set the mode or manually
  41. * disable this encoder will result in the crash.
  42. *
  43. * TODO: add support for telling the DRM subsystem that the pipeline is
  44. * disabled by the hardware and thus all access to it should be forbidden.
  45. * After that this piece of code can be removed.
  46. */
  47. if (bridge->ops & DRM_BRIDGE_OP_HPD)
  48. return (dp->link_ready) ? 0 : -ENOTCONN;
  49. return 0;
  50. }
  51. /**
  52. * dp_bridge_get_modes - callback to add drm modes via drm_mode_probed_add()
  53. * @bridge: Poiner to drm bridge
  54. * @connector: Pointer to drm connector structure
  55. * Returns: Number of modes added
  56. */
  57. static int dp_bridge_get_modes(struct drm_bridge *bridge, struct drm_connector *connector)
  58. {
  59. int rc = 0;
  60. struct msm_dp *dp;
  61. if (!connector)
  62. return 0;
  63. dp = to_dp_bridge(bridge)->dp_display;
  64. /* pluggable case assumes EDID is read when HPD */
  65. if (dp->link_ready) {
  66. rc = dp_display_get_modes(dp);
  67. if (rc <= 0) {
  68. DRM_ERROR("failed to get DP sink modes, rc=%d\n", rc);
  69. return rc;
  70. }
  71. } else {
  72. drm_dbg_dp(connector->dev, "No sink connected\n");
  73. }
  74. return rc;
  75. }
  76. static void dp_bridge_debugfs_init(struct drm_bridge *bridge, struct dentry *root)
  77. {
  78. struct msm_dp *dp = to_dp_bridge(bridge)->dp_display;
  79. dp_display_debugfs_init(dp, root, false);
  80. }
  81. static const struct drm_bridge_funcs dp_bridge_ops = {
  82. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  83. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  84. .atomic_reset = drm_atomic_helper_bridge_reset,
  85. .atomic_enable = dp_bridge_atomic_enable,
  86. .atomic_disable = dp_bridge_atomic_disable,
  87. .atomic_post_disable = dp_bridge_atomic_post_disable,
  88. .mode_set = dp_bridge_mode_set,
  89. .mode_valid = dp_bridge_mode_valid,
  90. .get_modes = dp_bridge_get_modes,
  91. .detect = dp_bridge_detect,
  92. .atomic_check = dp_bridge_atomic_check,
  93. .hpd_enable = dp_bridge_hpd_enable,
  94. .hpd_disable = dp_bridge_hpd_disable,
  95. .hpd_notify = dp_bridge_hpd_notify,
  96. .debugfs_init = dp_bridge_debugfs_init,
  97. };
  98. static int edp_bridge_atomic_check(struct drm_bridge *drm_bridge,
  99. struct drm_bridge_state *bridge_state,
  100. struct drm_crtc_state *crtc_state,
  101. struct drm_connector_state *conn_state)
  102. {
  103. struct msm_dp *dp = to_dp_bridge(drm_bridge)->dp_display;
  104. if (WARN_ON(!conn_state))
  105. return -ENODEV;
  106. conn_state->self_refresh_aware = dp->psr_supported;
  107. if (!conn_state->crtc || !crtc_state)
  108. return 0;
  109. if (crtc_state->self_refresh_active && !dp->psr_supported)
  110. return -EINVAL;
  111. return 0;
  112. }
  113. static void edp_bridge_atomic_enable(struct drm_bridge *drm_bridge,
  114. struct drm_bridge_state *old_bridge_state)
  115. {
  116. struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
  117. struct drm_crtc *crtc;
  118. struct drm_crtc_state *old_crtc_state;
  119. struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
  120. struct msm_dp *dp = dp_bridge->dp_display;
  121. /*
  122. * Check the old state of the crtc to determine if the panel
  123. * was put into psr state previously by the edp_bridge_atomic_disable.
  124. * If the panel is in psr, just exit psr state and skip the full
  125. * bridge enable sequence.
  126. */
  127. crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state,
  128. drm_bridge->encoder);
  129. if (!crtc)
  130. return;
  131. old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
  132. if (old_crtc_state && old_crtc_state->self_refresh_active) {
  133. dp_display_set_psr(dp, false);
  134. return;
  135. }
  136. dp_bridge_atomic_enable(drm_bridge, old_bridge_state);
  137. }
  138. static void edp_bridge_atomic_disable(struct drm_bridge *drm_bridge,
  139. struct drm_bridge_state *old_bridge_state)
  140. {
  141. struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
  142. struct drm_crtc *crtc;
  143. struct drm_crtc_state *new_crtc_state = NULL, *old_crtc_state = NULL;
  144. struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
  145. struct msm_dp *dp = dp_bridge->dp_display;
  146. crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state,
  147. drm_bridge->encoder);
  148. if (!crtc)
  149. goto out;
  150. new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
  151. if (!new_crtc_state)
  152. goto out;
  153. old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
  154. if (!old_crtc_state)
  155. goto out;
  156. /*
  157. * Set self refresh mode if current crtc state is active.
  158. *
  159. * If old crtc state is active, then this is a display disable
  160. * call while the sink is in psr state. So, exit psr here.
  161. * The eDP controller will be disabled in the
  162. * edp_bridge_atomic_post_disable function.
  163. *
  164. * We observed sink is stuck in self refresh if psr exit is skipped
  165. * when display disable occurs while the sink is in psr state.
  166. */
  167. if (new_crtc_state->self_refresh_active) {
  168. dp_display_set_psr(dp, true);
  169. return;
  170. } else if (old_crtc_state->self_refresh_active) {
  171. dp_display_set_psr(dp, false);
  172. return;
  173. }
  174. out:
  175. dp_bridge_atomic_disable(drm_bridge, old_bridge_state);
  176. }
  177. static void edp_bridge_atomic_post_disable(struct drm_bridge *drm_bridge,
  178. struct drm_bridge_state *old_bridge_state)
  179. {
  180. struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
  181. struct drm_crtc *crtc;
  182. struct drm_crtc_state *new_crtc_state = NULL;
  183. crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state,
  184. drm_bridge->encoder);
  185. if (!crtc)
  186. return;
  187. new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
  188. if (!new_crtc_state)
  189. return;
  190. /*
  191. * Self refresh mode is already set in edp_bridge_atomic_disable.
  192. */
  193. if (new_crtc_state->self_refresh_active)
  194. return;
  195. dp_bridge_atomic_post_disable(drm_bridge, old_bridge_state);
  196. }
  197. /**
  198. * edp_bridge_mode_valid - callback to determine if specified mode is valid
  199. * @bridge: Pointer to drm bridge structure
  200. * @info: display info
  201. * @mode: Pointer to drm mode structure
  202. * Returns: Validity status for specified mode
  203. */
  204. static enum drm_mode_status edp_bridge_mode_valid(struct drm_bridge *bridge,
  205. const struct drm_display_info *info,
  206. const struct drm_display_mode *mode)
  207. {
  208. struct msm_dp *dp;
  209. int mode_pclk_khz = mode->clock;
  210. dp = to_dp_bridge(bridge)->dp_display;
  211. if (!dp || !mode_pclk_khz || !dp->connector) {
  212. DRM_ERROR("invalid params\n");
  213. return -EINVAL;
  214. }
  215. if (msm_dp_wide_bus_available(dp))
  216. mode_pclk_khz /= 2;
  217. if (mode_pclk_khz > DP_MAX_PIXEL_CLK_KHZ)
  218. return MODE_CLOCK_HIGH;
  219. /*
  220. * The eDP controller currently does not have a reliable way of
  221. * enabling panel power to read sink capabilities. So, we rely
  222. * on the panel driver to populate only supported modes for now.
  223. */
  224. return MODE_OK;
  225. }
  226. static void edp_bridge_debugfs_init(struct drm_bridge *bridge, struct dentry *root)
  227. {
  228. struct msm_dp *dp = to_dp_bridge(bridge)->dp_display;
  229. dp_display_debugfs_init(dp, root, true);
  230. }
  231. static const struct drm_bridge_funcs edp_bridge_ops = {
  232. .atomic_enable = edp_bridge_atomic_enable,
  233. .atomic_disable = edp_bridge_atomic_disable,
  234. .atomic_post_disable = edp_bridge_atomic_post_disable,
  235. .mode_set = dp_bridge_mode_set,
  236. .mode_valid = edp_bridge_mode_valid,
  237. .atomic_reset = drm_atomic_helper_bridge_reset,
  238. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  239. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  240. .atomic_check = edp_bridge_atomic_check,
  241. .debugfs_init = edp_bridge_debugfs_init,
  242. };
  243. int dp_bridge_init(struct msm_dp *dp_display, struct drm_device *dev,
  244. struct drm_encoder *encoder)
  245. {
  246. int rc;
  247. struct msm_dp_bridge *dp_bridge;
  248. struct drm_bridge *bridge;
  249. dp_bridge = devm_kzalloc(dev->dev, sizeof(*dp_bridge), GFP_KERNEL);
  250. if (!dp_bridge)
  251. return -ENOMEM;
  252. dp_bridge->dp_display = dp_display;
  253. bridge = &dp_bridge->bridge;
  254. bridge->funcs = dp_display->is_edp ? &edp_bridge_ops : &dp_bridge_ops;
  255. bridge->type = dp_display->connector_type;
  256. /*
  257. * Many ops only make sense for DP. Why?
  258. * - Detect/HPD are used by DRM to know if a display is _physically_
  259. * there, not whether the display is powered on / finished initting.
  260. * On eDP we assume the display is always there because you can't
  261. * know until power is applied. If we don't implement the ops DRM will
  262. * assume our display is always there.
  263. * - Currently eDP mode reading is driven by the panel driver. This
  264. * allows the panel driver to properly power itself on to read the
  265. * modes.
  266. */
  267. if (!dp_display->is_edp) {
  268. bridge->ops =
  269. DRM_BRIDGE_OP_DETECT |
  270. DRM_BRIDGE_OP_HPD |
  271. DRM_BRIDGE_OP_MODES;
  272. }
  273. rc = devm_drm_bridge_add(dev->dev, bridge);
  274. if (rc) {
  275. DRM_ERROR("failed to add bridge, rc=%d\n", rc);
  276. return rc;
  277. }
  278. rc = drm_bridge_attach(encoder, bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  279. if (rc) {
  280. DRM_ERROR("failed to attach bridge, rc=%d\n", rc);
  281. return rc;
  282. }
  283. if (dp_display->next_bridge) {
  284. rc = drm_bridge_attach(encoder,
  285. dp_display->next_bridge, bridge,
  286. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  287. if (rc < 0) {
  288. DRM_ERROR("failed to attach panel bridge: %d\n", rc);
  289. return rc;
  290. }
  291. }
  292. return 0;
  293. }
  294. /* connector initialization */
  295. struct drm_connector *dp_drm_connector_init(struct msm_dp *dp_display, struct drm_encoder *encoder,
  296. bool yuv_supported)
  297. {
  298. struct drm_connector *connector = NULL;
  299. connector = drm_bridge_connector_init(dp_display->drm_dev, encoder);
  300. if (IS_ERR(connector))
  301. return connector;
  302. if (!dp_display->is_edp)
  303. drm_connector_attach_dp_subconnector_property(connector);
  304. if (yuv_supported)
  305. connector->ycbcr_420_allowed = true;
  306. drm_connector_attach_encoder(connector, encoder);
  307. return connector;
  308. }