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