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