xref: /linux/drivers/gpu/drm/msm/dp/dp_display.c (revision 22739ca2a80c9c2bbeace4075249efdc950d3ac1)
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/module.h>
7 #include <linux/slab.h>
8 #include <linux/uaccess.h>
9 #include <linux/debugfs.h>
10 #include <linux/component.h>
11 #include <linux/of_irq.h>
12 #include <linux/phy/phy.h>
13 #include <linux/delay.h>
14 #include <linux/string_choices.h>
15 #include <drm/display/drm_dp_aux_bus.h>
16 #include <drm/display/drm_hdmi_audio_helper.h>
17 #include <drm/drm_edid.h>
18 #include <drm/drm_panel.h>
19 
20 #include "msm_drv.h"
21 #include "msm_kms.h"
22 #include "dp_ctrl.h"
23 #include "dp_aux.h"
24 #include "dp_reg.h"
25 #include "dp_link.h"
26 #include "dp_panel.h"
27 #include "dp_display.h"
28 #include "dp_drm.h"
29 #include "dp_audio.h"
30 #include "dp_debug.h"
31 
32 static bool psr_enabled = false;
33 module_param(psr_enabled, bool, 0);
34 MODULE_PARM_DESC(psr_enabled, "enable PSR for eDP and DP displays");
35 
36 #define HPD_STRING_SIZE 30
37 
38 enum {
39 	ISR_DISCONNECTED,
40 	ISR_CONNECT_PENDING,
41 	ISR_CONNECTED,
42 	ISR_HPD_IO_GLITCH_COUNT,
43 	ISR_IRQ_HPD_PULSE_COUNT,
44 	ISR_HPD_REPLUG_COUNT,
45 };
46 
47 struct msm_dp_display_private {
48 	int irq;
49 
50 	unsigned int id;
51 
52 	/* state variables */
53 	bool core_initialized;
54 	bool phy_initialized;
55 	bool audio_supported;
56 
57 	struct mutex plugged_lock;
58 	bool plugged;
59 
60 	struct drm_device *drm_dev;
61 
62 	struct drm_dp_aux *aux;
63 	struct msm_dp_link    *link;
64 	struct msm_dp_panel   *panel;
65 	struct msm_dp_ctrl    *ctrl;
66 
67 	struct msm_dp msm_dp_display;
68 
69 	/* wait for audio signaling */
70 	struct completion audio_comp;
71 
72 	/* HPD IRQ handling */
73 	spinlock_t irq_thread_lock;
74 	u32 hpd_isr_status;
75 
76 	bool wide_bus_supported;
77 
78 	struct msm_dp_audio *audio;
79 
80 	void __iomem *ahb_base;
81 	size_t ahb_len;
82 
83 	void __iomem *aux_base;
84 	size_t aux_len;
85 
86 	void __iomem *link_base;
87 	size_t link_len;
88 
89 	void __iomem *mst2link_base;
90 	size_t mst2link_len;
91 
92 	void __iomem *mst3link_base;
93 	size_t mst3link_len;
94 
95 	void __iomem *pixel_base[DP_STREAM_MAX];
96 	size_t pixel_len[DP_STREAM_MAX];
97 };
98 
99 struct msm_dp_desc {
100 	phys_addr_t io_start;
101 	unsigned int id;
102 	bool wide_bus_supported;
103 };
104 
105 static const struct msm_dp_desc msm_dp_desc_glymur[] = {
106 	{ .io_start = 0x0af54000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
107 	{ .io_start = 0x0af5c000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
108 	{ .io_start = 0x0af64000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
109 	{ .io_start = 0x0af6c000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
110 	{}
111 };
112 
113 static const struct msm_dp_desc msm_dp_desc_sa8775p[] = {
114 	{ .io_start = 0x0af54000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
115 	{ .io_start = 0x0af5c000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
116 	{ .io_start = 0x22154000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
117 	{ .io_start = 0x2215c000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
118 	{}
119 };
120 
121 static const struct msm_dp_desc msm_dp_desc_sdm845[] = {
122 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0 },
123 	{}
124 };
125 
126 static const struct msm_dp_desc msm_dp_desc_sc7180[] = {
127 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
128 	{}
129 };
130 
131 static const struct msm_dp_desc msm_dp_desc_sc7280[] = {
132 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
133 	{ .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
134 	{}
135 };
136 
137 static const struct msm_dp_desc msm_dp_desc_sc8180x[] = {
138 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
139 	{ .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
140 	{ .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
141 	{}
142 };
143 
144 static const struct msm_dp_desc msm_dp_desc_sc8280xp[] = {
145 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
146 	{ .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
147 	{ .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
148 	{ .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
149 	{ .io_start = 0x22090000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
150 	{ .io_start = 0x22098000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
151 	{ .io_start = 0x2209a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
152 	{ .io_start = 0x220a0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
153 	{}
154 };
155 
156 static const struct msm_dp_desc msm_dp_desc_sm8650[] = {
157 	{ .io_start = 0x0af54000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
158 	{}
159 };
160 
161 static const struct msm_dp_desc msm_dp_desc_x1e80100[] = {
162 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
163 	{ .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
164 	{ .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
165 	{ .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
166 	{}
167 };
168 
169 static const struct of_device_id msm_dp_dt_match[] = {
170 	{ .compatible = "qcom,glymur-dp", .data = &msm_dp_desc_glymur },
171 	{ .compatible = "qcom,sa8775p-dp", .data = &msm_dp_desc_sa8775p },
172 	{ .compatible = "qcom,sc7180-dp", .data = &msm_dp_desc_sc7180 },
173 	{ .compatible = "qcom,sc7280-dp", .data = &msm_dp_desc_sc7280 },
174 	{ .compatible = "qcom,sc7280-edp", .data = &msm_dp_desc_sc7280 },
175 	{ .compatible = "qcom,sc8180x-dp", .data = &msm_dp_desc_sc8180x },
176 	{ .compatible = "qcom,sc8180x-edp", .data = &msm_dp_desc_sc8180x },
177 	{ .compatible = "qcom,sc8280xp-dp", .data = &msm_dp_desc_sc8280xp },
178 	{ .compatible = "qcom,sc8280xp-edp", .data = &msm_dp_desc_sc8280xp },
179 	{ .compatible = "qcom,sdm845-dp", .data = &msm_dp_desc_sdm845 },
180 	{ .compatible = "qcom,sm8350-dp", .data = &msm_dp_desc_sc7180 },
181 	{ .compatible = "qcom,sm8650-dp", .data = &msm_dp_desc_sm8650 },
182 	{ .compatible = "qcom,x1e80100-dp", .data = &msm_dp_desc_x1e80100 },
183 	{}
184 };
185 MODULE_DEVICE_TABLE(of, msm_dp_dt_match);
186 
187 static struct msm_dp_display_private *dev_get_dp_display_private(struct device *dev)
188 {
189 	struct msm_dp *dp = dev_get_drvdata(dev);
190 
191 	return container_of(dp, struct msm_dp_display_private, msm_dp_display);
192 }
193 
194 void msm_dp_display_signal_audio_start(struct msm_dp *msm_dp_display)
195 {
196 	struct msm_dp_display_private *dp;
197 
198 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
199 
200 	reinit_completion(&dp->audio_comp);
201 }
202 
203 void msm_dp_display_signal_audio_complete(struct msm_dp *msm_dp_display)
204 {
205 	struct msm_dp_display_private *dp;
206 
207 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
208 
209 	complete_all(&dp->audio_comp);
210 }
211 
212 static int msm_dp_display_bind(struct device *dev, struct device *master,
213 			   void *data)
214 {
215 	int rc = 0;
216 	struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
217 	struct msm_drm_private *priv = dev_get_drvdata(master);
218 	struct drm_device *drm = priv->dev;
219 
220 	dp->msm_dp_display.drm_dev = drm;
221 	priv->kms->dp[dp->id] = &dp->msm_dp_display;
222 
223 	dp->drm_dev = drm;
224 	dp->aux->drm_dev = drm;
225 	rc = msm_dp_aux_register(dp->aux);
226 	if (rc) {
227 		DRM_ERROR("DRM DP AUX register failed\n");
228 		goto end;
229 	}
230 
231 	return 0;
232 end:
233 	return rc;
234 }
235 
236 static void msm_dp_display_unbind(struct device *dev, struct device *master,
237 			      void *data)
238 {
239 	struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
240 	struct msm_drm_private *priv = dev_get_drvdata(master);
241 
242 	of_dp_aux_depopulate_bus(dp->aux);
243 
244 	msm_dp_aux_unregister(dp->aux);
245 	dp->drm_dev = NULL;
246 	dp->aux->drm_dev = NULL;
247 	priv->kms->dp[dp->id] = NULL;
248 }
249 
250 static const struct component_ops msm_dp_display_comp_ops = {
251 	.bind = msm_dp_display_bind,
252 	.unbind = msm_dp_display_unbind,
253 };
254 
255 static int msm_dp_display_lttpr_init(struct msm_dp_display_private *dp, u8 *dpcd)
256 {
257 	int rc, lttpr_count;
258 
259 	if (drm_dp_read_lttpr_common_caps(dp->aux, dpcd, dp->link->lttpr_common_caps))
260 		return 0;
261 
262 	lttpr_count = drm_dp_lttpr_count(dp->link->lttpr_common_caps);
263 	rc = drm_dp_lttpr_init(dp->aux, lttpr_count);
264 	if (rc) {
265 		DRM_ERROR("failed to set LTTPRs transparency mode, rc=%d\n", rc);
266 		return 0;
267 	}
268 
269 	return lttpr_count;
270 }
271 
272 static int msm_dp_display_process_hpd_high(struct msm_dp_display_private *dp)
273 {
274 	struct drm_connector *connector = dp->msm_dp_display.connector;
275 	const struct drm_display_info *info = &connector->display_info;
276 	int rc = 0;
277 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
278 	const struct drm_edid *drm_edid = NULL;
279 
280 	rc = drm_dp_read_dpcd_caps(dp->aux, dpcd);
281 	if (rc)
282 		goto end;
283 
284 	dp->link->lttpr_count = msm_dp_display_lttpr_init(dp, dpcd);
285 
286 	rc = msm_dp_panel_read_link_caps(dp->panel, connector);
287 	if (rc)
288 		goto end;
289 
290 	drm_edid = drm_edid_read_ddc(connector, &dp->aux->ddc);
291 	drm_edid_connector_update(connector, drm_edid);
292 
293 	if (!drm_edid) {
294 		DRM_ERROR("panel edid read failed\n");
295 		/* check edid read fail is due to unplug */
296 		if (!msm_dp_aux_is_link_connected(dp->aux))
297 			return -ETIMEDOUT;
298 	}
299 
300 	msm_dp_link_process_request(dp->link);
301 
302 	if (!dp->msm_dp_display.is_edp)
303 		drm_dp_set_subconnector_property(connector,
304 						 connector_status_connected,
305 						 dp->panel->dpcd,
306 						 dp->panel->downstream_ports);
307 
308 	dp->msm_dp_display.psr_supported = dp->panel->psr_cap.version && psr_enabled;
309 
310 	dp->audio_supported = info->has_audio;
311 	msm_dp_panel_handle_sink_request(dp->panel, drm_edid);
312 
313 	/*
314 	 * set sink to normal operation mode -- D0
315 	 * before dpcd read
316 	 */
317 	msm_dp_link_psm_config(dp->link, &dp->panel->link_info, false);
318 
319 	msm_dp_link_reset_phy_params_vx_px(dp->link);
320 
321 end:
322 	drm_edid_free(drm_edid);
323 	return rc;
324 }
325 
326 /**
327  * msm_dp_display_host_phy_init() - start up DP PHY
328  * @dp: main display data structure
329  *
330  * Prepare DP PHY for the AUX transactions to succeed.
331  *
332  * Returns: true if this call has initliazed the PHY and false if the PHY has
333  * already been setup beforehand.
334  */
335 static bool msm_dp_display_host_phy_init(struct msm_dp_display_private *dp)
336 {
337 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
338 		dp->msm_dp_display.connector_type, dp->core_initialized,
339 		dp->phy_initialized);
340 
341 	if (!dp->phy_initialized) {
342 		msm_dp_ctrl_phy_init(dp->ctrl);
343 		dp->phy_initialized = true;
344 		return true;
345 	}
346 
347 	return false;
348 }
349 
350 static void msm_dp_display_host_phy_exit(struct msm_dp_display_private *dp)
351 {
352 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
353 		dp->msm_dp_display.connector_type, dp->core_initialized,
354 		dp->phy_initialized);
355 
356 	if (dp->phy_initialized) {
357 		msm_dp_ctrl_phy_exit(dp->ctrl);
358 		dp->phy_initialized = false;
359 	}
360 }
361 
362 static void msm_dp_display_host_init(struct msm_dp_display_private *dp)
363 {
364 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
365 		dp->msm_dp_display.connector_type, dp->core_initialized,
366 		dp->phy_initialized);
367 
368 	msm_dp_ctrl_core_clk_enable(dp->ctrl);
369 	msm_dp_ctrl_reset(dp->ctrl, dp->panel);
370 	msm_dp_ctrl_enable_irq(dp->ctrl);
371 	msm_dp_aux_init(dp->aux);
372 	dp->core_initialized = true;
373 }
374 
375 static void msm_dp_display_host_deinit(struct msm_dp_display_private *dp)
376 {
377 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
378 		dp->msm_dp_display.connector_type, dp->core_initialized,
379 		dp->phy_initialized);
380 
381 	msm_dp_ctrl_reset(dp->ctrl, dp->panel);
382 	msm_dp_ctrl_disable_irq(dp->ctrl);
383 	msm_dp_aux_deinit(dp->aux);
384 	msm_dp_ctrl_core_clk_disable(dp->ctrl);
385 	dp->core_initialized = false;
386 }
387 
388 static void msm_dp_display_handle_video_request(struct msm_dp_display_private *dp)
389 {
390 	if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
391 		dp->panel->video_test = true;
392 		msm_dp_link_send_test_response(dp->link);
393 	}
394 }
395 
396 static int msm_dp_display_handle_irq_hpd(struct msm_dp_display_private *dp)
397 {
398 	u32 sink_request = dp->link->sink_request;
399 
400 	drm_dbg_dp(dp->drm_dev, "%d\n", sink_request);
401 
402 	msm_dp_ctrl_handle_sink_request(dp->ctrl, dp->panel);
403 
404 	if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
405 		msm_dp_display_handle_video_request(dp);
406 
407 	return 0;
408 }
409 
410 static int msm_dp_hpd_plug_handle(struct msm_dp_display_private *dp)
411 {
412 	int ret;
413 	struct platform_device *pdev = dp->msm_dp_display.pdev;
414 
415 	drm_dbg_dp(dp->drm_dev, "Before, type=%d sink_count=%d\n",
416 			dp->msm_dp_display.connector_type,
417 			dp->link->sink_count);
418 
419 	guard(mutex)(&dp->plugged_lock);
420 
421 	ret = pm_runtime_resume_and_get(&pdev->dev);
422 	if (ret) {
423 		DRM_ERROR("failed to pm_runtime_resume\n");
424 		return ret;
425 	}
426 
427 	msm_dp_aux_enable_xfers(dp->aux, true);
428 
429 	msm_dp_display_host_phy_init(dp);
430 
431 	ret = msm_dp_display_process_hpd_high(dp);
432 
433 	drm_dbg_dp(dp->drm_dev, "After, type=%d sink_count=%d\n",
434 			dp->msm_dp_display.connector_type,
435 			dp->link->sink_count);
436 
437 	dp->plugged = true;
438 
439 	return ret;
440 };
441 
442 static void msm_dp_display_handle_plugged_change(struct msm_dp *msm_dp_display,
443 		bool plugged)
444 {
445 	struct msm_dp_display_private *dp;
446 
447 	dp = container_of(msm_dp_display,
448 			struct msm_dp_display_private, msm_dp_display);
449 
450 	/* notify audio subsystem only if sink supports audio */
451 	if (dp->audio_supported)
452 		drm_connector_hdmi_audio_plugged_notify(msm_dp_display->connector,
453 							plugged);
454 }
455 
456 static int msm_dp_hpd_unplug_handle(struct msm_dp_display_private *dp)
457 {
458 	struct platform_device *pdev = dp->msm_dp_display.pdev;
459 
460 	guard(mutex)(&dp->plugged_lock);
461 
462 	dp->panel->video_test = false;
463 
464 	msm_dp_aux_enable_xfers(dp->aux, false);
465 
466 	drm_dbg_dp(dp->drm_dev, "Before, type=%d sink_count=%d\n",
467 			dp->msm_dp_display.connector_type,
468 			dp->link->sink_count);
469 
470 	if (!dp->plugged)
471 		return 0;
472 
473 	/* Don't forget modes for eDP */
474 	if (!dp->msm_dp_display.is_edp)
475 		drm_edid_connector_update(dp->msm_dp_display.connector, NULL);
476 
477 	/* triggered by irq_hdp with sink_count = 0 */
478 	if (dp->link->sink_count == 0)
479 		msm_dp_display_host_phy_exit(dp);
480 
481 	/*
482 	 * We don't need separate work for disconnect as
483 	 * connect/attention interrupts are disabled
484 	 */
485 	if (!dp->msm_dp_display.is_edp)
486 		drm_dp_set_subconnector_property(dp->msm_dp_display.connector,
487 						 connector_status_disconnected,
488 						 dp->panel->dpcd,
489 						 dp->panel->downstream_ports);
490 
491 	/* signal the disconnect event early to ensure proper teardown */
492 	msm_dp_display_handle_plugged_change(&dp->msm_dp_display, false);
493 
494 	drm_dbg_dp(dp->drm_dev, "After, type=%d, sink_count=%d\n",
495 			dp->msm_dp_display.connector_type,
496 			dp->link->sink_count);
497 
498 	if (dp->plugged) {
499 		pm_runtime_put_sync(&pdev->dev);
500 		dp->plugged = false;
501 	}
502 
503 	return 0;
504 }
505 
506 static int msm_dp_irq_hpd_handle(struct msm_dp_display_private *dp)
507 {
508 	u32 sink_request;
509 	int rc = 0;
510 
511 	guard(mutex)(&dp->plugged_lock);
512 
513 	/* irq_hpd can happen at either connected or disconnected state */
514 	drm_dbg_dp(dp->drm_dev, "Before, type=%d, sink_count=%d\n",
515 			dp->msm_dp_display.connector_type,
516 			dp->link->sink_count);
517 
518 	/* check for any test request issued by sink */
519 	rc = msm_dp_link_process_request(dp->link);
520 	if (!rc) {
521 		sink_request = dp->link->sink_request;
522 		drm_dbg_dp(dp->drm_dev, "sink_request=%d\n", sink_request);
523 		if (sink_request & DS_PORT_STATUS_CHANGED)
524 			rc = msm_dp_display_process_hpd_high(dp);
525 		else
526 			rc = msm_dp_display_handle_irq_hpd(dp);
527 	}
528 
529 	drm_dbg_dp(dp->drm_dev, "After, type=%d, sink_count=%d\n",
530 			dp->msm_dp_display.connector_type,
531 			dp->link->sink_count);
532 
533 	return rc;
534 }
535 
536 static void msm_dp_display_deinit_sub_modules(struct msm_dp_display_private *dp)
537 {
538 	msm_dp_audio_put(dp->audio);
539 	msm_dp_aux_put(dp->aux);
540 }
541 
542 static int msm_dp_init_sub_modules(struct msm_dp_display_private *dp)
543 {
544 	int rc = 0;
545 	struct device *dev = &dp->msm_dp_display.pdev->dev;
546 	struct phy *phy;
547 
548 	phy = devm_phy_get(dev, "dp");
549 	if (IS_ERR(phy))
550 		return PTR_ERR(phy);
551 
552 	rc = phy_set_mode_ext(phy, PHY_MODE_DP,
553 			      dp->msm_dp_display.is_edp ? PHY_SUBMODE_EDP : PHY_SUBMODE_DP);
554 	if (rc) {
555 		DRM_ERROR("failed to set phy submode, rc = %d\n", rc);
556 		goto error;
557 	}
558 
559 	dp->aux = msm_dp_aux_get(dev, phy, dp->msm_dp_display.is_edp, dp->aux_base);
560 	if (IS_ERR(dp->aux)) {
561 		rc = PTR_ERR(dp->aux);
562 		DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
563 		dp->aux = NULL;
564 		goto error;
565 	}
566 
567 	dp->link = msm_dp_link_get(dev, dp->aux);
568 	if (IS_ERR(dp->link)) {
569 		rc = PTR_ERR(dp->link);
570 		DRM_ERROR("failed to initialize link, rc = %d\n", rc);
571 		dp->link = NULL;
572 		goto error_link;
573 	}
574 
575 	dp->panel = msm_dp_panel_get(dev, dp->aux, dp->link, dp->link_base,
576 				     dp->mst2link_base, dp->mst3link_base, dp->pixel_base[0]);
577 	if (IS_ERR(dp->panel)) {
578 		rc = PTR_ERR(dp->panel);
579 		DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
580 		dp->panel = NULL;
581 		goto error_link;
582 	}
583 
584 	dp->ctrl = msm_dp_ctrl_get(dev, dp->link, dp->aux,
585 			       phy, dp->ahb_base, dp->link_base,
586 			       dp->mst2link_base, dp->mst3link_base);
587 	if (IS_ERR(dp->ctrl)) {
588 		rc = PTR_ERR(dp->ctrl);
589 		DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
590 		dp->ctrl = NULL;
591 		goto error_link;
592 	}
593 
594 	dp->audio = msm_dp_audio_get(dp->msm_dp_display.pdev, dp->link_base);
595 	if (IS_ERR(dp->audio)) {
596 		rc = PTR_ERR(dp->audio);
597 		pr_err("failed to initialize audio, rc = %d\n", rc);
598 		dp->audio = NULL;
599 		goto error_link;
600 	}
601 
602 	return rc;
603 
604 error_link:
605 	msm_dp_aux_put(dp->aux);
606 error:
607 	return rc;
608 }
609 
610 static int msm_dp_display_set_mode(struct msm_dp *msm_dp_display,
611 				   const struct drm_display_mode *adjusted_mode,
612 				   struct msm_dp_panel *msm_dp_panel)
613 {
614 	struct msm_dp_display_private *dp;
615 	u32 bpp;
616 
617 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
618 
619 	if (msm_dp_display_check_video_test(msm_dp_display))
620 		bpp = msm_dp_display_get_test_bpp(msm_dp_display);
621 	else
622 		bpp = msm_dp_panel->connector->display_info.bpc * 3;
623 
624 	msm_dp_panel_init_panel_info(msm_dp_panel, adjusted_mode, bpp ? bpp : 24);
625 
626 	/* populate wide_bus_support to different layers */
627 	dp->ctrl->wide_bus_en =
628 		msm_dp_panel->msm_dp_mode.out_fmt_is_yuv_420 ? false : dp->wide_bus_supported;
629 	return 0;
630 }
631 
632 static int msm_dp_display_prepare_link(struct msm_dp_display_private *dp)
633 {
634 	struct msm_dp *msm_dp_display = &dp->msm_dp_display;
635 	int rc = 0;
636 	bool force_link_train = false;
637 
638 	drm_dbg_dp(dp->drm_dev, "sink_count=%d\n", dp->link->sink_count);
639 
640 	if (msm_dp_display->is_edp)
641 		msm_dp_hpd_plug_handle(dp);
642 
643 	rc = pm_runtime_resume_and_get(&msm_dp_display->pdev->dev);
644 	if (rc) {
645 		DRM_ERROR("failed to pm_runtime_resume\n");
646 		return rc;
647 	}
648 
649 	if (dp->link->sink_count == 0)
650 		return -ENOTCONN;
651 
652 	if (!msm_dp_display->power_on) {
653 		msm_dp_display_host_phy_init(dp);
654 		force_link_train = true;
655 	}
656 
657 	rc = msm_dp_ctrl_on_link(dp->ctrl, dp->panel);
658 	if (rc) {
659 		DRM_ERROR("Failed link training (rc=%d)\n", rc);
660 		// TODO: schedule drm_connector_set_link_status_property()
661 		return rc;
662 	}
663 
664 	return msm_dp_ctrl_prepare_stream_on(dp->ctrl, dp->panel, force_link_train);
665 }
666 
667 static int msm_dp_display_enable(struct msm_dp_display_private *dp,
668 				 struct msm_dp_panel *msm_dp_panel)
669 {
670 	int rc = 0;
671 	struct msm_dp *msm_dp_display = &dp->msm_dp_display;
672 
673 	drm_dbg_dp(dp->drm_dev, "sink_count=%d\n", dp->link->sink_count);
674 	if (msm_dp_display->power_on) {
675 		drm_dbg_dp(dp->drm_dev, "Link already setup, return\n");
676 		return 0;
677 	}
678 
679 	rc = msm_dp_ctrl_on_stream(dp->ctrl, msm_dp_panel);
680 	if (!rc)
681 		msm_dp_display->power_on = true;
682 
683 	return rc;
684 }
685 
686 static int msm_dp_display_post_enable(struct msm_dp *msm_dp_display)
687 {
688 	struct msm_dp_display_private *dp;
689 	u32 rate;
690 
691 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
692 
693 	rate = dp->link->link_params.rate;
694 
695 	if (dp->audio_supported) {
696 		dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
697 		dp->audio->lane_count = dp->link->link_params.num_lanes;
698 	}
699 
700 	/* signal the connect event late to synchronize video and display */
701 	msm_dp_display_handle_plugged_change(msm_dp_display, true);
702 
703 	if (msm_dp_display->psr_supported)
704 		msm_dp_ctrl_config_psr(dp->ctrl, dp->panel);
705 
706 	return 0;
707 }
708 
709 static void msm_dp_display_audio_notify_disable(struct msm_dp_display_private *dp)
710 {
711 	struct msm_dp *msm_dp_display = &dp->msm_dp_display;
712 
713 	/* wait only if audio was enabled */
714 	if (msm_dp_display->audio_enabled) {
715 		/* signal the disconnect event */
716 		msm_dp_display_handle_plugged_change(msm_dp_display, false);
717 		if (!wait_for_completion_timeout(&dp->audio_comp,
718 				HZ * 5))
719 			DRM_ERROR("audio comp timeout\n");
720 	}
721 
722 	msm_dp_display->audio_enabled = false;
723 }
724 
725 static int msm_dp_display_disable(struct msm_dp_display_private *dp,
726 				  struct msm_dp_panel *msm_dp_panel)
727 {
728 	struct msm_dp *msm_dp_display = &dp->msm_dp_display;
729 
730 	if (!msm_dp_display->power_on)
731 		return 0;
732 
733 	msm_dp_panel_disable_vsc_sdp(msm_dp_panel);
734 
735 	msm_dp_ctrl_off_pixel_clk(dp->ctrl, msm_dp_panel->stream_id);
736 
737 	drm_dbg_dp(dp->drm_dev, "sink count: %d\n", dp->link->sink_count);
738 	return 0;
739 }
740 
741 /**
742  * msm_dp_display_mode_valid - callback to determine if specified mode is valid
743  * @dp: Pointer to dp display structure
744  * @info: display info
745  * @mode: Pointer to drm mode structure
746  * Returns: Validity status for specified mode
747  */
748 enum drm_mode_status msm_dp_display_mode_valid(struct msm_dp *dp,
749 					       const struct drm_display_info *info,
750 					       const struct drm_display_mode *mode)
751 {
752 	const u32 num_components = 3, default_bpp = 24;
753 	struct msm_dp_display_private *msm_dp_display;
754 	struct msm_dp_link_info *link_info;
755 	u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
756 	int mode_pclk_khz = mode->clock;
757 	int link_pclk_khz;
758 	bool is_yuv_420;
759 
760 	if (!dp || !mode_pclk_khz || !dp->connector) {
761 		DRM_ERROR("invalid params\n");
762 		return -EINVAL;
763 	}
764 
765 	msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
766 	link_info = &msm_dp_display->panel->link_info;
767 
768 	is_yuv_420 = drm_mode_is_420_only(&dp->connector->display_info, mode);
769 
770 	/*
771 	 * YUV 420 is carried over DP by signalling the colorimetry through a
772 	 * VSC SDP, so a 420-only mode cannot be driven without VSC SDP support.
773 	 */
774 	if (is_yuv_420 && !msm_dp_display->panel->vsc_sdp_supported)
775 		return MODE_NO_420;
776 
777 	link_pclk_khz = is_yuv_420 ? mode_pclk_khz / 2 : mode_pclk_khz;
778 
779 	if (is_yuv_420 || msm_dp_display->wide_bus_supported)
780 		mode_pclk_khz /= 2;
781 
782 	if (mode_pclk_khz > DP_MAX_PIXEL_CLK_KHZ)
783 		return MODE_CLOCK_HIGH;
784 
785 	mode_bpp = dp->connector->display_info.bpc * num_components;
786 	if (!mode_bpp)
787 		mode_bpp = default_bpp;
788 
789 	mode_bpp = msm_dp_panel_get_mode_bpp(msm_dp_display->panel,
790 			mode_bpp, link_pclk_khz);
791 
792 	mode_rate_khz = link_pclk_khz * mode_bpp;
793 	supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
794 
795 	if (mode_rate_khz > supported_rate_khz)
796 		return MODE_BAD;
797 
798 	return MODE_OK;
799 }
800 
801 int msm_dp_display_get_modes(struct msm_dp *dp)
802 {
803 	struct msm_dp_display_private *msm_dp_display;
804 
805 	if (!dp) {
806 		DRM_ERROR("invalid params\n");
807 		return 0;
808 	}
809 
810 	msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
811 
812 	return drm_edid_connector_add_modes(msm_dp_display->panel->connector);
813 }
814 
815 bool msm_dp_display_check_video_test(struct msm_dp *dp)
816 {
817 	struct msm_dp_display_private *msm_dp_display;
818 
819 	msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
820 
821 	return msm_dp_display->panel->video_test;
822 }
823 
824 int msm_dp_display_get_test_bpp(struct msm_dp *dp)
825 {
826 	struct msm_dp_display_private *msm_dp_display;
827 
828 	if (!dp) {
829 		DRM_ERROR("invalid params\n");
830 		return 0;
831 	}
832 
833 	msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
834 
835 	return msm_dp_link_bit_depth_to_bpp(
836 		msm_dp_display->link->test_video.test_bit_depth);
837 }
838 
839 void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
840 {
841 	struct msm_dp_display_private *msm_dp_display;
842 
843 	msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
844 
845 	/*
846 	 * if we are reading registers we need the link clocks to be on
847 	 * however till DP cable is connected this will not happen as we
848 	 * do not know the resolution to power up with. Hence check the
849 	 * power_on status before dumping DP registers to avoid crash due
850 	 * to unclocked access
851 	 */
852 	if (!dp->power_on)
853 		return;
854 
855 	msm_disp_snapshot_add_block(disp_state, msm_dp_display->ahb_len,
856 				    msm_dp_display->ahb_base, "dp_ahb");
857 	msm_disp_snapshot_add_block(disp_state, msm_dp_display->aux_len,
858 				    msm_dp_display->aux_base, "dp_aux");
859 	msm_disp_snapshot_add_block(disp_state, msm_dp_display->link_len,
860 				    msm_dp_display->link_base, "dp_link");
861 	if (msm_dp_display->mst2link_base)
862 		msm_disp_snapshot_add_block(disp_state, msm_dp_display->mst2link_len,
863 					    msm_dp_display->mst2link_base, "dp_mst2link");
864 	if (msm_dp_display->mst3link_base)
865 		msm_disp_snapshot_add_block(disp_state, msm_dp_display->mst3link_len,
866 					    msm_dp_display->mst3link_base, "dp_mst3link");
867 
868 	for (int i = 0; i < DP_STREAM_MAX; i++) {
869 		if (!msm_dp_ctrl_stream_clks_on(msm_dp_display->ctrl, i))
870 			continue;
871 		msm_disp_snapshot_add_block(disp_state, msm_dp_display->pixel_len[i],
872 					    msm_dp_display->pixel_base[i], "dp_p%d", i);
873 	}
874 }
875 
876 void msm_dp_display_set_psr(struct msm_dp *msm_dp_display, bool enter)
877 {
878 	struct msm_dp_display_private *dp;
879 
880 	if (!msm_dp_display) {
881 		DRM_ERROR("invalid params\n");
882 		return;
883 	}
884 
885 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
886 	msm_dp_ctrl_set_psr(dp->ctrl, dp->panel, enter);
887 }
888 
889 /**
890  * msm_dp_bridge_detect - callback to determine if connector is connected
891  *
892  * @bridge: Pointer to drm bridge structure
893  * @connector: Pointer to drm connector structure
894  *
895  * Returns: where there is a display connected to the DPTX (returning
896  * disconnected for branch devices without DP Sinks being connected).
897  */
898 enum drm_connector_status msm_dp_bridge_detect(struct drm_bridge *bridge,
899 					       struct drm_connector *connector)
900 {
901 	struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
902 	struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
903 	int status = connector_status_disconnected;
904 	struct msm_dp_display_private *priv;
905 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
906 	struct drm_dp_desc desc;
907 	bool phy_deinit;
908 	int ret;
909 
910 	dp = to_dp_bridge(bridge)->msm_dp_display;
911 
912 	priv = container_of(dp, struct msm_dp_display_private, msm_dp_display);
913 
914 	guard(mutex)(&priv->plugged_lock);
915 	ret = pm_runtime_resume_and_get(&dp->pdev->dev);
916 	if (ret) {
917 		DRM_ERROR("failed to pm_runtime_resume\n");
918 		return status;
919 	}
920 
921 	phy_deinit = msm_dp_display_host_phy_init(priv);
922 
923 	msm_dp_aux_enable_xfers(priv->aux, true);
924 
925 	ret = msm_dp_aux_is_link_connected(priv->aux);
926 	DRM_DEBUG_DP("aux link status: %x\n", ret);
927 	if (!priv->plugged && !ret) {
928 		DRM_DEBUG_DP("aux not connected\n");
929 		priv->plugged = false;
930 		goto end;
931 	}
932 
933 	ret = drm_dp_read_dpcd_caps(priv->aux, dpcd);
934 	if (ret) {
935 		DRM_DEBUG_DP("failed to read caps\n");
936 		priv->plugged = false;
937 		goto end;
938 	}
939 
940 	ret = drm_dp_read_desc(priv->aux, &desc, drm_dp_is_branch(dpcd));
941 	if (ret) {
942 		DRM_DEBUG_DP("failed to read desc\n");
943 		priv->plugged = false;
944 		goto end;
945 	}
946 
947 	status = connector_status_connected;
948 	priv->plugged = true;
949 
950 	if (drm_dp_read_sink_count_cap(connector, dpcd, &desc)) {
951 		int sink_count = drm_dp_read_sink_count(priv->aux);
952 
953 		drm_dbg_dp(dp->drm_dev, "sink_count = %d\n", sink_count);
954 
955 		if (sink_count <= 0)
956 			status = connector_status_disconnected;
957 	}
958 
959 end:
960 	/*
961 	 * If we detected the DPRX, leave the controller on so that it doesn't
962 	 * lose the state.
963 	 */
964 	if (!priv->plugged) {
965 		if (phy_deinit) {
966 			msm_dp_aux_enable_xfers(priv->aux, false);
967 			msm_dp_display_host_phy_exit(priv);
968 		}
969 
970 		pm_runtime_put_sync(&dp->pdev->dev);
971 	}
972 
973 	return status;
974 }
975 
976 static irqreturn_t msm_dp_display_irq_handler(int irq, void *dev_id)
977 {
978 	struct msm_dp_display_private *dp = dev_id;
979 	u32 hpd_isr_status;
980 	unsigned long flags;
981 	irqreturn_t ret = IRQ_HANDLED;
982 
983 	hpd_isr_status = msm_dp_aux_get_hpd_intr_status(dp->aux);
984 
985 	if (hpd_isr_status & 0x0F) {
986 		drm_dbg_dp(dp->drm_dev, "type=%d isr=0x%x\n",
987 			dp->msm_dp_display.connector_type, hpd_isr_status);
988 
989 		spin_lock_irqsave(&dp->irq_thread_lock, flags);
990 		dp->hpd_isr_status |= hpd_isr_status;
991 		ret = IRQ_WAKE_THREAD;
992 		spin_unlock_irqrestore(&dp->irq_thread_lock, flags);
993 	}
994 
995 	/* DP controller isr */
996 	ret |= msm_dp_ctrl_isr(dp->ctrl, dp->panel);
997 
998 	return ret;
999 }
1000 
1001 static irqreturn_t msm_dp_display_irq_thread(int irq, void *dev_id)
1002 {
1003 	struct msm_dp_display_private *dp = dev_id;
1004 	irqreturn_t ret = IRQ_NONE;
1005 	unsigned long flags;
1006 	u32 hpd_isr_status;
1007 
1008 	spin_lock_irqsave(&dp->irq_thread_lock, flags);
1009 	hpd_isr_status = dp->hpd_isr_status;
1010 	dp->hpd_isr_status = 0;
1011 	spin_unlock_irqrestore(&dp->irq_thread_lock, flags);
1012 
1013 	if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1014 		drm_bridge_hpd_notify(dp->msm_dp_display.bridge,
1015 				      connector_status_disconnected);
1016 
1017 	if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1018 		drm_bridge_hpd_notify(dp->msm_dp_display.bridge,
1019 				      connector_status_connected);
1020 
1021 	/* Send HPD as connected and distinguish it in the notifier */
1022 	if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK)
1023 		drm_bridge_hpd_notify(dp->msm_dp_display.bridge,
1024 				      connector_status_connected);
1025 
1026 	ret = IRQ_HANDLED;
1027 
1028 	return ret;
1029 }
1030 
1031 static int msm_dp_display_request_irq(struct msm_dp_display_private *dp)
1032 {
1033 	int rc = 0;
1034 	struct platform_device *pdev = dp->msm_dp_display.pdev;
1035 
1036 	dp->irq = platform_get_irq(pdev, 0);
1037 	if (dp->irq < 0) {
1038 		DRM_ERROR("failed to get irq\n");
1039 		return dp->irq;
1040 	}
1041 
1042 	spin_lock_init(&dp->irq_thread_lock);
1043 	irq_set_status_flags(dp->irq, IRQ_NOAUTOEN);
1044 	rc = devm_request_threaded_irq(&pdev->dev, dp->irq,
1045 				       msm_dp_display_irq_handler,
1046 				       msm_dp_display_irq_thread,
1047 				       IRQ_TYPE_LEVEL_HIGH,
1048 				       "dp_display_isr", dp);
1049 
1050 	if (rc < 0) {
1051 		DRM_ERROR("failed to request IRQ%u: %d\n",
1052 				dp->irq, rc);
1053 		return rc;
1054 	}
1055 
1056 	return 0;
1057 }
1058 
1059 static const struct msm_dp_desc *msm_dp_display_get_desc(struct platform_device *pdev)
1060 {
1061 	const struct msm_dp_desc *descs = of_device_get_match_data(&pdev->dev);
1062 	struct resource *res;
1063 	int i;
1064 
1065 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1066 	if (!res)
1067 		return NULL;
1068 
1069 	for (i = 0; i < descs[i].io_start; i++) {
1070 		if (descs[i].io_start == res->start)
1071 			return &descs[i];
1072 	}
1073 
1074 	dev_err(&pdev->dev, "unknown displayport instance\n");
1075 	return NULL;
1076 }
1077 
1078 static int msm_dp_display_probe_tail(struct device *dev)
1079 {
1080 	struct msm_dp *dp = dev_get_drvdata(dev);
1081 	int ret;
1082 
1083 	/*
1084 	 * External bridges are mandatory for eDP interfaces: one has to
1085 	 * provide at least an eDP panel (which gets wrapped into panel-bridge).
1086 	 *
1087 	 * For DisplayPort interfaces external bridges are optional, so
1088 	 * silently ignore an error if one is not present (-ENODEV).
1089 	 */
1090 	dp->next_bridge = devm_drm_of_get_bridge(&dp->pdev->dev, dp->pdev->dev.of_node, 1, 0);
1091 	if (IS_ERR(dp->next_bridge)) {
1092 		ret = PTR_ERR(dp->next_bridge);
1093 		dp->next_bridge = NULL;
1094 		if (dp->is_edp || ret != -ENODEV)
1095 			return ret;
1096 	}
1097 
1098 	ret = component_add(dev, &msm_dp_display_comp_ops);
1099 	if (ret)
1100 		DRM_ERROR("component add failed, rc=%d\n", ret);
1101 
1102 	return ret;
1103 }
1104 
1105 static int msm_dp_auxbus_done_probe(struct drm_dp_aux *aux)
1106 {
1107 	return msm_dp_display_probe_tail(aux->dev);
1108 }
1109 
1110 static int msm_dp_display_get_connector_type(struct platform_device *pdev,
1111 					 const struct msm_dp_desc *desc)
1112 {
1113 	struct device_node *node = pdev->dev.of_node;
1114 	struct device_node *aux_bus = of_get_child_by_name(node, "aux-bus");
1115 	struct device_node *panel = of_get_child_by_name(aux_bus, "panel");
1116 	int connector_type;
1117 
1118 	if (panel)
1119 		connector_type = DRM_MODE_CONNECTOR_eDP;
1120 	else
1121 		connector_type = DRM_MODE_SUBCONNECTOR_DisplayPort;
1122 
1123 	of_node_put(panel);
1124 	of_node_put(aux_bus);
1125 
1126 	return connector_type;
1127 }
1128 
1129 static void __iomem *msm_dp_ioremap(struct platform_device *pdev, int idx, size_t *len)
1130 {
1131 	struct resource *res;
1132 	void __iomem *base;
1133 
1134 	base = devm_platform_get_and_ioremap_resource(pdev, idx, &res);
1135 	if (!IS_ERR(base))
1136 		*len = resource_size(res);
1137 
1138 	return base;
1139 }
1140 
1141 static void __iomem *msm_dp_ioremap_optional(struct platform_device *pdev,
1142 					     int idx, size_t *len)
1143 {
1144 	struct resource *res;
1145 	void __iomem *base;
1146 
1147 	if (len)
1148 		*len = 0;
1149 
1150 	res = platform_get_resource(pdev, IORESOURCE_MEM, idx);
1151 	if (!res)
1152 		return NULL;
1153 
1154 	base = devm_ioremap_resource(&pdev->dev, res);
1155 	if (!IS_ERR(base))
1156 		*len = resource_size(res);
1157 
1158 	return base;
1159 }
1160 
1161 #define DP_DEFAULT_AHB_OFFSET	0x0000
1162 #define DP_DEFAULT_AHB_SIZE	0x0200
1163 #define DP_DEFAULT_AUX_OFFSET	0x0200
1164 #define DP_DEFAULT_AUX_SIZE	0x0200
1165 #define DP_DEFAULT_LINK_OFFSET	0x0400
1166 #define DP_DEFAULT_LINK_SIZE	0x0C00
1167 #define DP_DEFAULT_P0_OFFSET	0x1000
1168 #define DP_DEFAULT_P0_SIZE	0x0400
1169 
1170 static int msm_dp_display_get_io(struct msm_dp_display_private *display)
1171 {
1172 	struct platform_device *pdev = display->msm_dp_display.pdev;
1173 	int i;
1174 
1175 	display->ahb_base = msm_dp_ioremap(pdev, 0, &display->ahb_len);
1176 	if (IS_ERR(display->ahb_base))
1177 		return PTR_ERR(display->ahb_base);
1178 
1179 	display->aux_base = msm_dp_ioremap(pdev, 1, &display->aux_len);
1180 	if (IS_ERR(display->aux_base)) {
1181 		if (display->aux_base != ERR_PTR(-EINVAL)) {
1182 			DRM_ERROR("unable to remap aux region: %pe\n", display->aux_base);
1183 			return PTR_ERR(display->aux_base);
1184 		}
1185 
1186 		/*
1187 		 * The initial binding had a single reg, but in order to
1188 		 * support variation in the sub-region sizes this was split.
1189 		 * msm_dp_ioremap() will fail with -EINVAL here if only a single
1190 		 * reg is specified, so fill in the sub-region offsets and
1191 		 * lengths based on this single region.
1192 		 */
1193 		if (display->ahb_len < DP_DEFAULT_P0_OFFSET + DP_DEFAULT_P0_SIZE) {
1194 			DRM_ERROR("legacy memory region not large enough\n");
1195 			return -EINVAL;
1196 		}
1197 
1198 		display->ahb_len = DP_DEFAULT_AHB_SIZE;
1199 		display->aux_base = display->ahb_base + DP_DEFAULT_AUX_OFFSET;
1200 		display->aux_len = DP_DEFAULT_AUX_SIZE;
1201 		display->link_base = display->ahb_base + DP_DEFAULT_LINK_OFFSET;
1202 		display->link_len = DP_DEFAULT_LINK_SIZE;
1203 		display->pixel_base[0] = display->ahb_base + DP_DEFAULT_P0_OFFSET;
1204 		display->pixel_len[DP_STREAM_0] = DP_DEFAULT_P0_SIZE;
1205 
1206 		return 0;
1207 	}
1208 
1209 	display->link_base = msm_dp_ioremap(pdev, 2, &display->link_len);
1210 	if (IS_ERR(display->link_base)) {
1211 		DRM_ERROR("unable to remap link region: %pe\n", display->link_base);
1212 		return PTR_ERR(display->link_base);
1213 	}
1214 
1215 	display->pixel_base[0] = msm_dp_ioremap(pdev, 3,
1216 					  &display->pixel_len[DP_STREAM_0]);
1217 	if (IS_ERR(display->pixel_base[0]))
1218 		return PTR_ERR(display->pixel_base[0]);
1219 
1220 	for (i = DP_STREAM_1; i < DP_STREAM_MAX; i++) {
1221 		display->pixel_base[i] = msm_dp_ioremap_optional(pdev, i + 3,
1222 							   &display->pixel_len[i]);
1223 		if (IS_ERR(display->pixel_base[i]))
1224 			return PTR_ERR(display->pixel_base[i]);
1225 		if (!display->pixel_base[i])
1226 			break;
1227 	}
1228 
1229 	display->mst2link_base = msm_dp_ioremap_optional(pdev, 7, &display->mst2link_len);
1230 	if (IS_ERR(display->mst2link_base))
1231 		return PTR_ERR(display->mst2link_base);
1232 
1233 	display->mst3link_base = msm_dp_ioremap_optional(pdev, 8, &display->mst3link_len);
1234 	if (IS_ERR(display->mst3link_base))
1235 		return PTR_ERR(display->mst3link_base);
1236 
1237 	return 0;
1238 }
1239 
1240 static int msm_dp_display_probe(struct platform_device *pdev)
1241 {
1242 	int rc = 0;
1243 	struct msm_dp_display_private *dp;
1244 	const struct msm_dp_desc *desc;
1245 
1246 	if (!pdev || !pdev->dev.of_node) {
1247 		DRM_ERROR("pdev not found\n");
1248 		return -ENODEV;
1249 	}
1250 
1251 	dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1252 	if (!dp)
1253 		return -ENOMEM;
1254 
1255 	desc = msm_dp_display_get_desc(pdev);
1256 	if (!desc)
1257 		return -EINVAL;
1258 
1259 	dp->msm_dp_display.pdev = pdev;
1260 	dp->id = desc->id;
1261 	dp->msm_dp_display.connector_type = msm_dp_display_get_connector_type(pdev, desc);
1262 	dp->wide_bus_supported = desc->wide_bus_supported;
1263 	dp->msm_dp_display.is_edp =
1264 		(dp->msm_dp_display.connector_type == DRM_MODE_CONNECTOR_eDP);
1265 	dp->hpd_isr_status = 0;
1266 
1267 	mutex_init(&dp->plugged_lock);
1268 
1269 	rc = msm_dp_display_get_io(dp);
1270 	if (rc)
1271 		return rc;
1272 
1273 	rc = msm_dp_init_sub_modules(dp);
1274 	if (rc) {
1275 		DRM_ERROR("init sub module failed\n");
1276 		return -EPROBE_DEFER;
1277 	}
1278 
1279 	/* Store DP audio handle inside DP display */
1280 	dp->msm_dp_display.msm_dp_audio = dp->audio;
1281 
1282 	init_completion(&dp->audio_comp);
1283 
1284 	platform_set_drvdata(pdev, &dp->msm_dp_display);
1285 
1286 	rc = devm_pm_runtime_enable(&pdev->dev);
1287 	if (rc)
1288 		goto err;
1289 
1290 	rc = msm_dp_display_request_irq(dp);
1291 	if (rc)
1292 		goto err;
1293 
1294 	if (dp->msm_dp_display.is_edp) {
1295 		rc = devm_of_dp_aux_populate_bus(dp->aux, msm_dp_auxbus_done_probe);
1296 		if (rc) {
1297 			DRM_ERROR("eDP auxbus population failed, rc=%d\n", rc);
1298 			goto err;
1299 		}
1300 	} else {
1301 		rc = msm_dp_display_probe_tail(&pdev->dev);
1302 		if (rc)
1303 			goto err;
1304 	}
1305 
1306 	return rc;
1307 
1308 err:
1309 	msm_dp_display_deinit_sub_modules(dp);
1310 	return rc;
1311 }
1312 
1313 static void msm_dp_display_remove(struct platform_device *pdev)
1314 {
1315 	struct msm_dp_display_private *dp = dev_get_dp_display_private(&pdev->dev);
1316 
1317 	component_del(&pdev->dev, &msm_dp_display_comp_ops);
1318 	msm_dp_display_deinit_sub_modules(dp);
1319 	platform_set_drvdata(pdev, NULL);
1320 }
1321 
1322 static int msm_dp_pm_runtime_suspend(struct device *dev)
1323 {
1324 	struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
1325 
1326 	disable_irq(dp->irq);
1327 
1328 	if (dp->msm_dp_display.is_edp) {
1329 		msm_dp_display_host_phy_exit(dp);
1330 		msm_dp_aux_hpd_disable(dp->aux);
1331 	}
1332 	msm_dp_display_host_deinit(dp);
1333 
1334 	return 0;
1335 }
1336 
1337 static int msm_dp_pm_runtime_resume(struct device *dev)
1338 {
1339 	struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
1340 
1341 	/*
1342 	 * for eDP, host cotroller, HPD block and PHY are enabled here
1343 	 * but with HPD irq disabled
1344 	 *
1345 	 * for DP, only host controller is enabled here.
1346 	 * HPD block is enabled at msm_dp_bridge_hpd_enable()
1347 	 * PHY will be enabled at plugin handler later
1348 	 */
1349 	msm_dp_display_host_init(dp);
1350 	if (dp->msm_dp_display.is_edp) {
1351 		msm_dp_aux_hpd_enable(dp->aux);
1352 		msm_dp_display_host_phy_init(dp);
1353 	}
1354 
1355 	enable_irq(dp->irq);
1356 	return 0;
1357 }
1358 
1359 static const struct dev_pm_ops msm_dp_pm_ops = {
1360 	SET_RUNTIME_PM_OPS(msm_dp_pm_runtime_suspend, msm_dp_pm_runtime_resume, NULL)
1361 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1362 				pm_runtime_force_resume)
1363 };
1364 
1365 static struct platform_driver msm_dp_display_driver = {
1366 	.probe  = msm_dp_display_probe,
1367 	.remove = msm_dp_display_remove,
1368 	.driver = {
1369 		.name = "msm-dp-display",
1370 		.of_match_table = msm_dp_dt_match,
1371 		.suppress_bind_attrs = true,
1372 		.pm = &msm_dp_pm_ops,
1373 	},
1374 };
1375 
1376 int __init msm_dp_register(void)
1377 {
1378 	int ret;
1379 
1380 	ret = platform_driver_register(&msm_dp_display_driver);
1381 	if (ret)
1382 		DRM_ERROR("Dp display driver register failed");
1383 
1384 	return ret;
1385 }
1386 
1387 void __exit msm_dp_unregister(void)
1388 {
1389 	platform_driver_unregister(&msm_dp_display_driver);
1390 }
1391 
1392 bool msm_dp_needs_periph_flush(const struct msm_dp *msm_dp_display,
1393 			       const struct drm_display_mode *mode)
1394 {
1395 	return drm_mode_is_420_only(&msm_dp_display->connector->display_info, mode);
1396 }
1397 
1398 bool msm_dp_wide_bus_available(const struct msm_dp *msm_dp_display)
1399 {
1400 	struct msm_dp_display_private *dp;
1401 
1402 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1403 
1404 	if (dp->panel->msm_dp_mode.out_fmt_is_yuv_420)
1405 		return false;
1406 
1407 	return dp->wide_bus_supported;
1408 }
1409 
1410 void msm_dp_display_debugfs_init(struct msm_dp *msm_dp_display, struct dentry *root, bool is_edp)
1411 {
1412 	struct msm_dp_display_private *dp;
1413 	struct device *dev;
1414 	int rc;
1415 
1416 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1417 	dev = &dp->msm_dp_display.pdev->dev;
1418 
1419 	rc = msm_dp_debug_init(dev, dp->panel, dp->link, dp->msm_dp_display.connector, root, is_edp);
1420 	if (rc)
1421 		DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1422 }
1423 
1424 int msm_dp_modeset_init(struct msm_dp *msm_dp_display, struct drm_device *dev,
1425 			struct drm_encoder *encoder, bool yuv_supported)
1426 {
1427 	struct msm_dp_display_private *msm_dp_priv;
1428 	int ret;
1429 
1430 	msm_dp_display->drm_dev = dev;
1431 
1432 	msm_dp_priv = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1433 
1434 	ret = msm_dp_bridge_init(msm_dp_display, dev, encoder, yuv_supported);
1435 	if (ret) {
1436 		DRM_DEV_ERROR(dev->dev,
1437 			"failed to create dp bridge: %d\n", ret);
1438 		return ret;
1439 	}
1440 
1441 	msm_dp_display->connector = msm_dp_drm_connector_init(msm_dp_display, encoder);
1442 	if (IS_ERR(msm_dp_display->connector)) {
1443 		ret = PTR_ERR(msm_dp_display->connector);
1444 		DRM_DEV_ERROR(dev->dev,
1445 			"failed to create dp connector: %d\n", ret);
1446 		msm_dp_display->connector = NULL;
1447 		return ret;
1448 	}
1449 
1450 	msm_dp_priv->panel->connector = msm_dp_display->connector;
1451 
1452 	return 0;
1453 }
1454 
1455 void msm_dp_display_atomic_pre_enable(struct msm_dp *msm_dp_display,
1456 				      struct drm_atomic_commit *state)
1457 {
1458 	struct msm_dp_display_private *dp;
1459 	struct drm_crtc *crtc;
1460 	struct drm_crtc_state *crtc_state;
1461 
1462 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1463 
1464 	crtc = drm_atomic_get_new_crtc_for_encoder(state, msm_dp_display->bridge->encoder);
1465 	if (!crtc)
1466 		return;
1467 	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1468 
1469 	/*
1470 	 * The DPU encoder's .atomic_enable() reads the mode's YUV420 / wide bus
1471 	 * state and runs before the bridge's .atomic_enable(), so the mode must
1472 	 * be programmed here, in .atomic_pre_enable().
1473 	 */
1474 	msm_dp_display_set_mode(msm_dp_display, &crtc_state->adjusted_mode, dp->panel);
1475 }
1476 
1477 void msm_dp_display_atomic_enable(struct msm_dp *msm_dp_display,
1478 				  struct drm_atomic_commit *state)
1479 {
1480 	int rc = 0;
1481 	struct msm_dp_display_private *dp;
1482 
1483 	dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1484 
1485 	rc = msm_dp_display_prepare_link(dp);
1486 	if (rc) {
1487 		DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1488 		return;
1489 	}
1490 
1491 	rc = msm_dp_display_enable(dp, dp->panel);
1492 	if (rc)
1493 		DRM_ERROR("DP display enable failed, rc=%d\n", rc);
1494 
1495 	rc = msm_dp_display_post_enable(msm_dp_display);
1496 	if (rc) {
1497 		DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1498 		msm_dp_display_disable(dp, dp->panel);
1499 	}
1500 
1501 	drm_dbg_dp(msm_dp_display->drm_dev, "type=%d Done\n", msm_dp_display->connector_type);
1502 }
1503 
1504 void msm_dp_display_atomic_disable(struct msm_dp *dp)
1505 {
1506 	struct msm_dp_display_private *msm_dp_display;
1507 
1508 	msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1509 
1510 	/*
1511 	 * If .atomic_enable() bailed out - link training failure is the common
1512 	 * case - the mainlink was never brought up and ->power_on stayed false.
1513 	 * Driving the PUSH_IDLE pattern into a controller that was never
1514 	 * enabled times out, and .atomic_post_disable() then drops the
1515 	 * controller's runtime-PM reference without tearing the PHY back down,
1516 	 * because msm_dp_display_disable() returns early on !power_on.  On
1517 	 * glymur (Snapdragon X2 Elite) that combination is answered by a
1518 	 * TrustZone-level SOCCP/ADSP force-stop and a silent SoC reset.
1519 	 * There is nothing to push idle, so leave it alone.
1520 	 */
1521 	if (!dp->power_on)
1522 		return;
1523 
1524 	msm_dp_ctrl_push_idle(msm_dp_display->ctrl);
1525 }
1526 
1527 static void msm_dp_display_unprepare(struct msm_dp_display_private *dp)
1528 {
1529 	struct msm_dp *msm_dp_display = &dp->msm_dp_display;
1530 
1531 	/* dongle is still connected but sinks are disconnected */
1532 	if (dp->link->sink_count == 0)
1533 		msm_dp_link_psm_config(dp->link, &dp->panel->link_info, true);
1534 
1535 	msm_dp_ctrl_off_link(dp->ctrl, dp->panel);
1536 
1537 	if (dp->link->sink_count == 0)
1538 		/* re-init the PHY so that we can listen to Dongle disconnect */
1539 		msm_dp_ctrl_reinit_phy(dp->ctrl);
1540 	else
1541 		msm_dp_display_host_phy_exit(dp);
1542 
1543 	msm_dp_display->power_on = false;
1544 
1545 	pm_runtime_put_sync(&msm_dp_display->pdev->dev);
1546 
1547 	drm_dbg_dp(dp->drm_dev, "type=%d Done\n", msm_dp_display->connector_type);
1548 }
1549 
1550 void msm_dp_display_atomic_post_disable(struct msm_dp *dp)
1551 {
1552 	struct msm_dp_display_private *msm_dp_display;
1553 
1554 	msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1555 
1556 	if (dp->is_edp)
1557 		msm_dp_hpd_unplug_handle(msm_dp_display);
1558 
1559 	msm_dp_display_audio_notify_disable(msm_dp_display);
1560 
1561 	msm_dp_display_disable(msm_dp_display, msm_dp_display->panel);
1562 
1563 	msm_dp_display_unprepare(msm_dp_display);
1564 }
1565 
1566 void msm_dp_bridge_hpd_enable(struct drm_bridge *bridge)
1567 {
1568 	struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1569 	struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1570 	struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1571 
1572 	/*
1573 	 * this is for external DP with hpd irq enabled case,
1574 	 * step-1: msm_dp_pm_runtime_resume() enable dp host only
1575 	 * step-2: enable hdp block and have hpd irq enabled here
1576 	 * step-3: waiting for plugin irq while phy is not initialized
1577 	 * step-4: DP PHY is initialized at plugin handler before link training
1578 	 *
1579 	 */
1580 	if (pm_runtime_resume_and_get(&msm_dp_display->pdev->dev)) {
1581 		DRM_ERROR("failed to resume power\n");
1582 		return;
1583 	}
1584 
1585 	msm_dp_aux_hpd_enable(dp->aux);
1586 	msm_dp_aux_hpd_intr_enable(dp->aux);
1587 }
1588 
1589 void msm_dp_bridge_hpd_disable(struct drm_bridge *bridge)
1590 {
1591 	struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1592 	struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1593 	struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1594 
1595 	msm_dp_aux_hpd_intr_disable(dp->aux);
1596 	msm_dp_aux_hpd_disable(dp->aux);
1597 
1598 	pm_runtime_put_sync(&msm_dp_display->pdev->dev);
1599 }
1600 
1601 void msm_dp_bridge_hpd_notify(struct drm_bridge *bridge,
1602 			      struct drm_connector *connector,
1603 			      enum drm_connector_status status)
1604 {
1605 	struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1606 	struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1607 	struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1608 	u32 hpd_link_status = 0;
1609 
1610 	if (pm_runtime_resume_and_get(&msm_dp_display->pdev->dev)) {
1611 		DRM_ERROR("failed to pm_runtime_resume\n");
1612 		return;
1613 	}
1614 
1615 	hpd_link_status = msm_dp_aux_is_link_connected(dp->aux);
1616 
1617 	drm_dbg_dp(dp->drm_dev, "type=%d link hpd_link_status=0x%x, status=%d\n",
1618 		   msm_dp_display->connector_type, hpd_link_status, status);
1619 
1620 	if (status == connector_status_connected) {
1621 		if (hpd_link_status == ISR_HPD_REPLUG_COUNT) {
1622 			msm_dp_hpd_unplug_handle(dp);
1623 			msm_dp_hpd_plug_handle(dp);
1624 		} else if (hpd_link_status == ISR_IRQ_HPD_PULSE_COUNT) {
1625 			msm_dp_irq_hpd_handle(dp);
1626 		} else {
1627 			msm_dp_hpd_plug_handle(dp);
1628 		}
1629 	} else {
1630 		msm_dp_hpd_unplug_handle(dp);
1631 	}
1632 
1633 	pm_runtime_put_sync(&msm_dp_display->pdev->dev);
1634 }
1635