dp_drm.c 11 KB

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