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