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