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