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