xref: /linux/drivers/gpu/drm/msm/dp/dp_display.c (revision 7bdbfb4e36e34eb788e44f27666bf0a2b3b90803)
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/delay.h>
13 #include <drm/display/drm_dp_aux_bus.h>
14 
15 #include "msm_drv.h"
16 #include "msm_kms.h"
17 #include "dp_parser.h"
18 #include "dp_power.h"
19 #include "dp_catalog.h"
20 #include "dp_aux.h"
21 #include "dp_reg.h"
22 #include "dp_link.h"
23 #include "dp_panel.h"
24 #include "dp_ctrl.h"
25 #include "dp_display.h"
26 #include "dp_drm.h"
27 #include "dp_audio.h"
28 #include "dp_debug.h"
29 
30 static bool psr_enabled = false;
31 module_param(psr_enabled, bool, 0);
32 MODULE_PARM_DESC(psr_enabled, "enable PSR for eDP and DP displays");
33 
34 #define HPD_STRING_SIZE 30
35 
36 enum {
37 	ISR_DISCONNECTED,
38 	ISR_CONNECT_PENDING,
39 	ISR_CONNECTED,
40 	ISR_HPD_REPLUG_COUNT,
41 	ISR_IRQ_HPD_PULSE_COUNT,
42 	ISR_HPD_LO_GLITH_COUNT,
43 };
44 
45 /* event thread connection state */
46 enum {
47 	ST_DISCONNECTED,
48 	ST_MAINLINK_READY,
49 	ST_CONNECTED,
50 	ST_DISCONNECT_PENDING,
51 	ST_DISPLAY_OFF,
52 };
53 
54 enum {
55 	EV_NO_EVENT,
56 	/* hpd events */
57 	EV_HPD_PLUG_INT,
58 	EV_IRQ_HPD_INT,
59 	EV_HPD_UNPLUG_INT,
60 	EV_USER_NOTIFICATION,
61 };
62 
63 #define EVENT_TIMEOUT	(HZ/10)	/* 100ms */
64 #define DP_EVENT_Q_MAX	8
65 
66 #define DP_TIMEOUT_NONE		0
67 
68 #define WAIT_FOR_RESUME_TIMEOUT_JIFFIES (HZ / 2)
69 
70 struct dp_event {
71 	u32 event_id;
72 	u32 data;
73 	u32 delay;
74 };
75 
76 struct dp_display_private {
77 	char *name;
78 	int irq;
79 
80 	unsigned int id;
81 
82 	/* state variables */
83 	bool core_initialized;
84 	bool phy_initialized;
85 	bool hpd_irq_on;
86 	bool audio_supported;
87 
88 	struct drm_device *drm_dev;
89 	struct dentry *root;
90 
91 	struct dp_parser  *parser;
92 	struct dp_power   *power;
93 	struct dp_catalog *catalog;
94 	struct drm_dp_aux *aux;
95 	struct dp_link    *link;
96 	struct dp_panel   *panel;
97 	struct dp_ctrl    *ctrl;
98 	struct dp_debug   *debug;
99 
100 	struct dp_display_mode dp_mode;
101 	struct msm_dp dp_display;
102 
103 	/* wait for audio signaling */
104 	struct completion audio_comp;
105 
106 	/* event related only access by event thread */
107 	struct mutex event_mutex;
108 	wait_queue_head_t event_q;
109 	u32 hpd_state;
110 	u32 event_pndx;
111 	u32 event_gndx;
112 	struct task_struct *ev_tsk;
113 	struct dp_event event_list[DP_EVENT_Q_MAX];
114 	spinlock_t event_lock;
115 
116 	bool wide_bus_en;
117 
118 	struct dp_audio *audio;
119 };
120 
121 struct msm_dp_desc {
122 	phys_addr_t io_start;
123 	unsigned int id;
124 	unsigned int connector_type;
125 	bool wide_bus_en;
126 };
127 
128 static const struct msm_dp_desc sc7180_dp_descs[] = {
129 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .connector_type = DRM_MODE_CONNECTOR_DisplayPort },
130 	{}
131 };
132 
133 static const struct msm_dp_desc sc7280_dp_descs[] = {
134 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
135 	{ .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_1, .connector_type = DRM_MODE_CONNECTOR_eDP, .wide_bus_en = true },
136 	{}
137 };
138 
139 static const struct msm_dp_desc sc8180x_dp_descs[] = {
140 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .connector_type = DRM_MODE_CONNECTOR_DisplayPort },
141 	{ .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .connector_type = DRM_MODE_CONNECTOR_DisplayPort },
142 	{ .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .connector_type = DRM_MODE_CONNECTOR_eDP },
143 	{}
144 };
145 
146 static const struct msm_dp_desc sc8280xp_dp_descs[] = {
147 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
148 	{ .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
149 	{ .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
150 	{ .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
151 	{ .io_start = 0x22090000, .id = MSM_DP_CONTROLLER_0, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
152 	{ .io_start = 0x22098000, .id = MSM_DP_CONTROLLER_1, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
153 	{ .io_start = 0x2209a000, .id = MSM_DP_CONTROLLER_2, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
154 	{ .io_start = 0x220a0000, .id = MSM_DP_CONTROLLER_3, .connector_type = DRM_MODE_CONNECTOR_DisplayPort, .wide_bus_en = true },
155 	{}
156 };
157 
158 static const struct msm_dp_desc sc8280xp_edp_descs[] = {
159 	{ .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .connector_type = DRM_MODE_CONNECTOR_eDP, .wide_bus_en = true },
160 	{ .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .connector_type = DRM_MODE_CONNECTOR_eDP, .wide_bus_en = true },
161 	{ .io_start = 0x2209a000, .id = MSM_DP_CONTROLLER_2, .connector_type = DRM_MODE_CONNECTOR_eDP, .wide_bus_en = true },
162 	{ .io_start = 0x220a0000, .id = MSM_DP_CONTROLLER_3, .connector_type = DRM_MODE_CONNECTOR_eDP, .wide_bus_en = true },
163 	{}
164 };
165 
166 static const struct msm_dp_desc sm8350_dp_descs[] = {
167 	{ .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .connector_type = DRM_MODE_CONNECTOR_DisplayPort },
168 	{}
169 };
170 
171 static const struct msm_dp_desc sm8650_dp_descs[] = {
172 	{ .io_start = 0x0af54000, .id = MSM_DP_CONTROLLER_0, .connector_type = DRM_MODE_CONNECTOR_DisplayPort },
173 	{}
174 };
175 
176 static const struct of_device_id dp_dt_match[] = {
177 	{ .compatible = "qcom,sc7180-dp", .data = &sc7180_dp_descs },
178 	{ .compatible = "qcom,sc7280-dp", .data = &sc7280_dp_descs },
179 	{ .compatible = "qcom,sc7280-edp", .data = &sc7280_dp_descs },
180 	{ .compatible = "qcom,sc8180x-dp", .data = &sc8180x_dp_descs },
181 	{ .compatible = "qcom,sc8180x-edp", .data = &sc8180x_dp_descs },
182 	{ .compatible = "qcom,sc8280xp-dp", .data = &sc8280xp_dp_descs },
183 	{ .compatible = "qcom,sc8280xp-edp", .data = &sc8280xp_edp_descs },
184 	{ .compatible = "qcom,sdm845-dp", .data = &sc7180_dp_descs },
185 	{ .compatible = "qcom,sm8350-dp", .data = &sm8350_dp_descs },
186 	{ .compatible = "qcom,sm8650-dp", .data = &sm8650_dp_descs },
187 	{}
188 };
189 
190 static struct dp_display_private *dev_get_dp_display_private(struct device *dev)
191 {
192 	struct msm_dp *dp = dev_get_drvdata(dev);
193 
194 	return container_of(dp, struct dp_display_private, dp_display);
195 }
196 
197 static int dp_add_event(struct dp_display_private *dp_priv, u32 event,
198 						u32 data, u32 delay)
199 {
200 	unsigned long flag;
201 	struct dp_event *todo;
202 	int pndx;
203 
204 	spin_lock_irqsave(&dp_priv->event_lock, flag);
205 	pndx = dp_priv->event_pndx + 1;
206 	pndx %= DP_EVENT_Q_MAX;
207 	if (pndx == dp_priv->event_gndx) {
208 		pr_err("event_q is full: pndx=%d gndx=%d\n",
209 			dp_priv->event_pndx, dp_priv->event_gndx);
210 		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
211 		return -EPERM;
212 	}
213 	todo = &dp_priv->event_list[dp_priv->event_pndx++];
214 	dp_priv->event_pndx %= DP_EVENT_Q_MAX;
215 	todo->event_id = event;
216 	todo->data = data;
217 	todo->delay = delay;
218 	wake_up(&dp_priv->event_q);
219 	spin_unlock_irqrestore(&dp_priv->event_lock, flag);
220 
221 	return 0;
222 }
223 
224 static int dp_del_event(struct dp_display_private *dp_priv, u32 event)
225 {
226 	unsigned long flag;
227 	struct dp_event *todo;
228 	u32	gndx;
229 
230 	spin_lock_irqsave(&dp_priv->event_lock, flag);
231 	if (dp_priv->event_pndx == dp_priv->event_gndx) {
232 		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
233 		return -ENOENT;
234 	}
235 
236 	gndx = dp_priv->event_gndx;
237 	while (dp_priv->event_pndx != gndx) {
238 		todo = &dp_priv->event_list[gndx];
239 		if (todo->event_id == event) {
240 			todo->event_id = EV_NO_EVENT;	/* deleted */
241 			todo->delay = 0;
242 		}
243 		gndx++;
244 		gndx %= DP_EVENT_Q_MAX;
245 	}
246 	spin_unlock_irqrestore(&dp_priv->event_lock, flag);
247 
248 	return 0;
249 }
250 
251 void dp_display_signal_audio_start(struct msm_dp *dp_display)
252 {
253 	struct dp_display_private *dp;
254 
255 	dp = container_of(dp_display, struct dp_display_private, dp_display);
256 
257 	reinit_completion(&dp->audio_comp);
258 }
259 
260 void dp_display_signal_audio_complete(struct msm_dp *dp_display)
261 {
262 	struct dp_display_private *dp;
263 
264 	dp = container_of(dp_display, struct dp_display_private, dp_display);
265 
266 	complete_all(&dp->audio_comp);
267 }
268 
269 static int dp_hpd_event_thread_start(struct dp_display_private *dp_priv);
270 
271 static int dp_display_bind(struct device *dev, struct device *master,
272 			   void *data)
273 {
274 	int rc = 0;
275 	struct dp_display_private *dp = dev_get_dp_display_private(dev);
276 	struct msm_drm_private *priv = dev_get_drvdata(master);
277 	struct drm_device *drm = priv->dev;
278 
279 	dp->dp_display.drm_dev = drm;
280 	priv->dp[dp->id] = &dp->dp_display;
281 
282 
283 
284 	dp->drm_dev = drm;
285 	dp->aux->drm_dev = drm;
286 	rc = dp_aux_register(dp->aux);
287 	if (rc) {
288 		DRM_ERROR("DRM DP AUX register failed\n");
289 		goto end;
290 	}
291 
292 
293 	rc = dp_register_audio_driver(dev, dp->audio);
294 	if (rc) {
295 		DRM_ERROR("Audio registration Dp failed\n");
296 		goto end;
297 	}
298 
299 	rc = dp_hpd_event_thread_start(dp);
300 	if (rc) {
301 		DRM_ERROR("Event thread create failed\n");
302 		goto end;
303 	}
304 
305 	return 0;
306 end:
307 	return rc;
308 }
309 
310 static void dp_display_unbind(struct device *dev, struct device *master,
311 			      void *data)
312 {
313 	struct dp_display_private *dp = dev_get_dp_display_private(dev);
314 	struct msm_drm_private *priv = dev_get_drvdata(master);
315 
316 	kthread_stop(dp->ev_tsk);
317 
318 	of_dp_aux_depopulate_bus(dp->aux);
319 
320 	dp_unregister_audio_driver(dev, dp->audio);
321 	dp_aux_unregister(dp->aux);
322 	dp->drm_dev = NULL;
323 	dp->aux->drm_dev = NULL;
324 	priv->dp[dp->id] = NULL;
325 }
326 
327 static const struct component_ops dp_display_comp_ops = {
328 	.bind = dp_display_bind,
329 	.unbind = dp_display_unbind,
330 };
331 
332 static int dp_display_send_hpd_notification(struct dp_display_private *dp,
333 					    bool hpd)
334 {
335 	struct drm_bridge *bridge = dp->dp_display.bridge;
336 
337 	/* reset video pattern flag on disconnect */
338 	if (!hpd) {
339 		dp->panel->video_test = false;
340 		if (!dp->dp_display.is_edp)
341 			drm_dp_set_subconnector_property(dp->dp_display.connector,
342 							 connector_status_disconnected,
343 							 dp->panel->dpcd,
344 							 dp->panel->downstream_ports);
345 	}
346 
347 	dp->dp_display.link_ready = hpd;
348 
349 	drm_dbg_dp(dp->drm_dev, "type=%d hpd=%d\n",
350 			dp->dp_display.connector_type, hpd);
351 	drm_bridge_hpd_notify(bridge, dp->dp_display.link_ready);
352 
353 	return 0;
354 }
355 
356 static int dp_display_process_hpd_high(struct dp_display_private *dp)
357 {
358 	int rc = 0;
359 	struct edid *edid;
360 
361 	dp->panel->max_dp_lanes = dp->parser->max_dp_lanes;
362 	dp->panel->max_dp_link_rate = dp->parser->max_dp_link_rate;
363 
364 	drm_dbg_dp(dp->drm_dev, "max_lanes=%d max_link_rate=%d\n",
365 		dp->panel->max_dp_lanes, dp->panel->max_dp_link_rate);
366 
367 	rc = dp_panel_read_sink_caps(dp->panel, dp->dp_display.connector);
368 	if (rc)
369 		goto end;
370 
371 	dp_link_process_request(dp->link);
372 
373 	if (!dp->dp_display.is_edp)
374 		drm_dp_set_subconnector_property(dp->dp_display.connector,
375 						 connector_status_connected,
376 						 dp->panel->dpcd,
377 						 dp->panel->downstream_ports);
378 
379 	edid = dp->panel->edid;
380 
381 	dp->dp_display.psr_supported = dp->panel->psr_cap.version && psr_enabled;
382 
383 	dp->audio_supported = drm_detect_monitor_audio(edid);
384 	dp_panel_handle_sink_request(dp->panel);
385 
386 	dp->dp_display.max_dp_lanes = dp->parser->max_dp_lanes;
387 
388 	/*
389 	 * set sink to normal operation mode -- D0
390 	 * before dpcd read
391 	 */
392 	dp_link_psm_config(dp->link, &dp->panel->link_info, false);
393 
394 	dp_link_reset_phy_params_vx_px(dp->link);
395 	rc = dp_ctrl_on_link(dp->ctrl);
396 	if (rc) {
397 		DRM_ERROR("failed to complete DP link training\n");
398 		goto end;
399 	}
400 
401 	dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
402 
403 end:
404 	return rc;
405 }
406 
407 static void dp_display_host_phy_init(struct dp_display_private *dp)
408 {
409 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
410 		dp->dp_display.connector_type, dp->core_initialized,
411 		dp->phy_initialized);
412 
413 	if (!dp->phy_initialized) {
414 		dp_ctrl_phy_init(dp->ctrl);
415 		dp->phy_initialized = true;
416 	}
417 }
418 
419 static void dp_display_host_phy_exit(struct dp_display_private *dp)
420 {
421 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
422 		dp->dp_display.connector_type, dp->core_initialized,
423 		dp->phy_initialized);
424 
425 	if (dp->phy_initialized) {
426 		dp_ctrl_phy_exit(dp->ctrl);
427 		dp->phy_initialized = false;
428 	}
429 }
430 
431 static void dp_display_host_init(struct dp_display_private *dp)
432 {
433 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
434 		dp->dp_display.connector_type, dp->core_initialized,
435 		dp->phy_initialized);
436 
437 	dp_power_init(dp->power);
438 	dp_ctrl_reset_irq_ctrl(dp->ctrl, true);
439 	dp_aux_init(dp->aux);
440 	dp->core_initialized = true;
441 }
442 
443 static void dp_display_host_deinit(struct dp_display_private *dp)
444 {
445 	drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
446 		dp->dp_display.connector_type, dp->core_initialized,
447 		dp->phy_initialized);
448 
449 	dp_ctrl_reset_irq_ctrl(dp->ctrl, false);
450 	dp_aux_deinit(dp->aux);
451 	dp_power_deinit(dp->power);
452 	dp->core_initialized = false;
453 }
454 
455 static int dp_display_usbpd_configure_cb(struct device *dev)
456 {
457 	struct dp_display_private *dp = dev_get_dp_display_private(dev);
458 
459 	dp_display_host_phy_init(dp);
460 
461 	return dp_display_process_hpd_high(dp);
462 }
463 
464 static int dp_display_notify_disconnect(struct device *dev)
465 {
466 	struct dp_display_private *dp = dev_get_dp_display_private(dev);
467 
468 	dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
469 
470 	return 0;
471 }
472 
473 static void dp_display_handle_video_request(struct dp_display_private *dp)
474 {
475 	if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
476 		dp->panel->video_test = true;
477 		dp_link_send_test_response(dp->link);
478 	}
479 }
480 
481 static int dp_display_handle_port_ststus_changed(struct dp_display_private *dp)
482 {
483 	int rc = 0;
484 
485 	if (drm_dp_is_branch(dp->panel->dpcd) && dp->link->sink_count == 0) {
486 		drm_dbg_dp(dp->drm_dev, "sink count is zero, nothing to do\n");
487 		if (dp->hpd_state != ST_DISCONNECTED) {
488 			dp->hpd_state = ST_DISCONNECT_PENDING;
489 			dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
490 		}
491 	} else {
492 		if (dp->hpd_state == ST_DISCONNECTED) {
493 			dp->hpd_state = ST_MAINLINK_READY;
494 			rc = dp_display_process_hpd_high(dp);
495 			if (rc)
496 				dp->hpd_state = ST_DISCONNECTED;
497 		}
498 	}
499 
500 	return rc;
501 }
502 
503 static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
504 {
505 	u32 sink_request = dp->link->sink_request;
506 
507 	drm_dbg_dp(dp->drm_dev, "%d\n", sink_request);
508 	if (dp->hpd_state == ST_DISCONNECTED) {
509 		if (sink_request & DP_LINK_STATUS_UPDATED) {
510 			drm_dbg_dp(dp->drm_dev, "Disconnected sink_request: %d\n",
511 							sink_request);
512 			DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
513 			return -EINVAL;
514 		}
515 	}
516 
517 	dp_ctrl_handle_sink_request(dp->ctrl);
518 
519 	if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
520 		dp_display_handle_video_request(dp);
521 
522 	return 0;
523 }
524 
525 static int dp_display_usbpd_attention_cb(struct device *dev)
526 {
527 	int rc = 0;
528 	u32 sink_request;
529 	struct dp_display_private *dp = dev_get_dp_display_private(dev);
530 
531 	/* check for any test request issued by sink */
532 	rc = dp_link_process_request(dp->link);
533 	if (!rc) {
534 		sink_request = dp->link->sink_request;
535 		drm_dbg_dp(dp->drm_dev, "hpd_state=%d sink_request=%d\n",
536 					dp->hpd_state, sink_request);
537 		if (sink_request & DS_PORT_STATUS_CHANGED)
538 			rc = dp_display_handle_port_ststus_changed(dp);
539 		else
540 			rc = dp_display_handle_irq_hpd(dp);
541 	}
542 
543 	return rc;
544 }
545 
546 static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
547 {
548 	u32 state;
549 	int ret;
550 	struct platform_device *pdev = dp->dp_display.pdev;
551 
552 	mutex_lock(&dp->event_mutex);
553 
554 	state =  dp->hpd_state;
555 	drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
556 			dp->dp_display.connector_type, state);
557 
558 	if (state == ST_DISPLAY_OFF) {
559 		mutex_unlock(&dp->event_mutex);
560 		return 0;
561 	}
562 
563 	if (state == ST_MAINLINK_READY || state == ST_CONNECTED) {
564 		mutex_unlock(&dp->event_mutex);
565 		return 0;
566 	}
567 
568 	if (state == ST_DISCONNECT_PENDING) {
569 		/* wait until ST_DISCONNECTED */
570 		dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
571 		mutex_unlock(&dp->event_mutex);
572 		return 0;
573 	}
574 
575 	ret = pm_runtime_resume_and_get(&pdev->dev);
576 	if (ret) {
577 		DRM_ERROR("failed to pm_runtime_resume\n");
578 		mutex_unlock(&dp->event_mutex);
579 		return ret;
580 	}
581 
582 	ret = dp_display_usbpd_configure_cb(&pdev->dev);
583 	if (ret) {	/* link train failed */
584 		dp->hpd_state = ST_DISCONNECTED;
585 	} else {
586 		dp->hpd_state = ST_MAINLINK_READY;
587 	}
588 
589 	drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
590 			dp->dp_display.connector_type, state);
591 	mutex_unlock(&dp->event_mutex);
592 
593 	/* uevent will complete connection part */
594 	return 0;
595 };
596 
597 static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
598 		bool plugged)
599 {
600 	struct dp_display_private *dp;
601 
602 	dp = container_of(dp_display,
603 			struct dp_display_private, dp_display);
604 
605 	/* notify audio subsystem only if sink supports audio */
606 	if (dp_display->plugged_cb && dp_display->codec_dev &&
607 			dp->audio_supported)
608 		dp_display->plugged_cb(dp_display->codec_dev, plugged);
609 }
610 
611 static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
612 {
613 	u32 state;
614 	struct platform_device *pdev = dp->dp_display.pdev;
615 
616 	mutex_lock(&dp->event_mutex);
617 
618 	state = dp->hpd_state;
619 
620 	drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
621 			dp->dp_display.connector_type, state);
622 
623 	/* unplugged, no more irq_hpd handle */
624 	dp_del_event(dp, EV_IRQ_HPD_INT);
625 
626 	if (state == ST_DISCONNECTED) {
627 		/* triggered by irq_hdp with sink_count = 0 */
628 		if (dp->link->sink_count == 0) {
629 			dp_display_host_phy_exit(dp);
630 		}
631 		dp_display_notify_disconnect(&dp->dp_display.pdev->dev);
632 		mutex_unlock(&dp->event_mutex);
633 		return 0;
634 	} else if (state == ST_DISCONNECT_PENDING) {
635 		mutex_unlock(&dp->event_mutex);
636 		return 0;
637 	} else if (state == ST_MAINLINK_READY) {
638 		dp_ctrl_off_link(dp->ctrl);
639 		dp_display_host_phy_exit(dp);
640 		dp->hpd_state = ST_DISCONNECTED;
641 		dp_display_notify_disconnect(&dp->dp_display.pdev->dev);
642 		mutex_unlock(&dp->event_mutex);
643 		return 0;
644 	}
645 
646 	/*
647 	 * We don't need separate work for disconnect as
648 	 * connect/attention interrupts are disabled
649 	 */
650 	dp_display_notify_disconnect(&dp->dp_display.pdev->dev);
651 
652 	if (state == ST_DISPLAY_OFF) {
653 		dp->hpd_state = ST_DISCONNECTED;
654 	} else {
655 		dp->hpd_state = ST_DISCONNECT_PENDING;
656 	}
657 
658 	/* signal the disconnect event early to ensure proper teardown */
659 	dp_display_handle_plugged_change(&dp->dp_display, false);
660 
661 	drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
662 			dp->dp_display.connector_type, state);
663 
664 	/* uevent will complete disconnection part */
665 	pm_runtime_put_sync(&pdev->dev);
666 	mutex_unlock(&dp->event_mutex);
667 	return 0;
668 }
669 
670 static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
671 {
672 	u32 state;
673 
674 	mutex_lock(&dp->event_mutex);
675 
676 	/* irq_hpd can happen at either connected or disconnected state */
677 	state =  dp->hpd_state;
678 	drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
679 			dp->dp_display.connector_type, state);
680 
681 	if (state == ST_DISPLAY_OFF) {
682 		mutex_unlock(&dp->event_mutex);
683 		return 0;
684 	}
685 
686 	if (state == ST_MAINLINK_READY || state == ST_DISCONNECT_PENDING) {
687 		/* wait until ST_CONNECTED */
688 		dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
689 		mutex_unlock(&dp->event_mutex);
690 		return 0;
691 	}
692 
693 	dp_display_usbpd_attention_cb(&dp->dp_display.pdev->dev);
694 
695 	drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
696 			dp->dp_display.connector_type, state);
697 
698 	mutex_unlock(&dp->event_mutex);
699 
700 	return 0;
701 }
702 
703 static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
704 {
705 	dp_audio_put(dp->audio);
706 	dp_panel_put(dp->panel);
707 	dp_aux_put(dp->aux);
708 }
709 
710 static int dp_init_sub_modules(struct dp_display_private *dp)
711 {
712 	int rc = 0;
713 	struct device *dev = &dp->dp_display.pdev->dev;
714 	struct dp_panel_in panel_in = {
715 		.dev = dev,
716 	};
717 
718 	dp->parser = dp_parser_get(dp->dp_display.pdev);
719 	if (IS_ERR(dp->parser)) {
720 		rc = PTR_ERR(dp->parser);
721 		DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
722 		dp->parser = NULL;
723 		goto error;
724 	}
725 
726 	dp->catalog = dp_catalog_get(dev, &dp->parser->io);
727 	if (IS_ERR(dp->catalog)) {
728 		rc = PTR_ERR(dp->catalog);
729 		DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
730 		dp->catalog = NULL;
731 		goto error;
732 	}
733 
734 	dp->power = dp_power_get(dev, dp->parser);
735 	if (IS_ERR(dp->power)) {
736 		rc = PTR_ERR(dp->power);
737 		DRM_ERROR("failed to initialize power, rc = %d\n", rc);
738 		dp->power = NULL;
739 		goto error;
740 	}
741 
742 	dp->aux = dp_aux_get(dev, dp->catalog, dp->dp_display.is_edp);
743 	if (IS_ERR(dp->aux)) {
744 		rc = PTR_ERR(dp->aux);
745 		DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
746 		dp->aux = NULL;
747 		goto error;
748 	}
749 
750 	dp->link = dp_link_get(dev, dp->aux);
751 	if (IS_ERR(dp->link)) {
752 		rc = PTR_ERR(dp->link);
753 		DRM_ERROR("failed to initialize link, rc = %d\n", rc);
754 		dp->link = NULL;
755 		goto error_link;
756 	}
757 
758 	panel_in.aux = dp->aux;
759 	panel_in.catalog = dp->catalog;
760 	panel_in.link = dp->link;
761 
762 	dp->panel = dp_panel_get(&panel_in);
763 	if (IS_ERR(dp->panel)) {
764 		rc = PTR_ERR(dp->panel);
765 		DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
766 		dp->panel = NULL;
767 		goto error_link;
768 	}
769 
770 	dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
771 			       dp->power, dp->catalog, dp->parser);
772 	if (IS_ERR(dp->ctrl)) {
773 		rc = PTR_ERR(dp->ctrl);
774 		DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
775 		dp->ctrl = NULL;
776 		goto error_ctrl;
777 	}
778 
779 	dp->audio = dp_audio_get(dp->dp_display.pdev, dp->panel, dp->catalog);
780 	if (IS_ERR(dp->audio)) {
781 		rc = PTR_ERR(dp->audio);
782 		pr_err("failed to initialize audio, rc = %d\n", rc);
783 		dp->audio = NULL;
784 		goto error_ctrl;
785 	}
786 
787 	/* populate wide_bus_en to differernt layers */
788 	dp->ctrl->wide_bus_en = dp->wide_bus_en;
789 	dp->catalog->wide_bus_en = dp->wide_bus_en;
790 
791 	return rc;
792 
793 error_ctrl:
794 	dp_panel_put(dp->panel);
795 error_link:
796 	dp_aux_put(dp->aux);
797 error:
798 	return rc;
799 }
800 
801 static int dp_display_set_mode(struct msm_dp *dp_display,
802 			       struct dp_display_mode *mode)
803 {
804 	struct dp_display_private *dp;
805 
806 	dp = container_of(dp_display, struct dp_display_private, dp_display);
807 
808 	drm_mode_copy(&dp->panel->dp_mode.drm_mode, &mode->drm_mode);
809 	dp->panel->dp_mode.bpp = mode->bpp;
810 	dp->panel->dp_mode.capabilities = mode->capabilities;
811 	dp_panel_init_panel_info(dp->panel);
812 	return 0;
813 }
814 
815 static int dp_display_enable(struct dp_display_private *dp, bool force_link_train)
816 {
817 	int rc = 0;
818 	struct msm_dp *dp_display = &dp->dp_display;
819 
820 	drm_dbg_dp(dp->drm_dev, "sink_count=%d\n", dp->link->sink_count);
821 	if (dp_display->power_on) {
822 		drm_dbg_dp(dp->drm_dev, "Link already setup, return\n");
823 		return 0;
824 	}
825 
826 	rc = dp_ctrl_on_stream(dp->ctrl, force_link_train);
827 	if (!rc)
828 		dp_display->power_on = true;
829 
830 	return rc;
831 }
832 
833 static int dp_display_post_enable(struct msm_dp *dp_display)
834 {
835 	struct dp_display_private *dp;
836 	u32 rate;
837 
838 	dp = container_of(dp_display, struct dp_display_private, dp_display);
839 
840 	rate = dp->link->link_params.rate;
841 
842 	if (dp->audio_supported) {
843 		dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
844 		dp->audio->lane_count = dp->link->link_params.num_lanes;
845 	}
846 
847 	/* signal the connect event late to synchronize video and display */
848 	dp_display_handle_plugged_change(dp_display, true);
849 
850 	if (dp_display->psr_supported)
851 		dp_ctrl_config_psr(dp->ctrl);
852 
853 	return 0;
854 }
855 
856 static int dp_display_disable(struct dp_display_private *dp)
857 {
858 	struct msm_dp *dp_display = &dp->dp_display;
859 
860 	if (!dp_display->power_on)
861 		return 0;
862 
863 	/* wait only if audio was enabled */
864 	if (dp_display->audio_enabled) {
865 		/* signal the disconnect event */
866 		dp_display_handle_plugged_change(dp_display, false);
867 		if (!wait_for_completion_timeout(&dp->audio_comp,
868 				HZ * 5))
869 			DRM_ERROR("audio comp timeout\n");
870 	}
871 
872 	dp_display->audio_enabled = false;
873 
874 	if (dp->link->sink_count == 0) {
875 		/*
876 		 * irq_hpd with sink_count = 0
877 		 * hdmi unplugged out of dongle
878 		 */
879 		dp_ctrl_off_link_stream(dp->ctrl);
880 	} else {
881 		/*
882 		 * unplugged interrupt
883 		 * dongle unplugged out of DUT
884 		 */
885 		dp_ctrl_off(dp->ctrl);
886 		dp_display_host_phy_exit(dp);
887 	}
888 
889 	dp_display->power_on = false;
890 
891 	drm_dbg_dp(dp->drm_dev, "sink count: %d\n", dp->link->sink_count);
892 	return 0;
893 }
894 
895 int dp_display_set_plugged_cb(struct msm_dp *dp_display,
896 		hdmi_codec_plugged_cb fn, struct device *codec_dev)
897 {
898 	bool plugged;
899 
900 	dp_display->plugged_cb = fn;
901 	dp_display->codec_dev = codec_dev;
902 	plugged = dp_display->link_ready;
903 	dp_display_handle_plugged_change(dp_display, plugged);
904 
905 	return 0;
906 }
907 
908 /**
909  * dp_bridge_mode_valid - callback to determine if specified mode is valid
910  * @bridge: Pointer to drm bridge structure
911  * @info: display info
912  * @mode: Pointer to drm mode structure
913  * Returns: Validity status for specified mode
914  */
915 enum drm_mode_status dp_bridge_mode_valid(struct drm_bridge *bridge,
916 					  const struct drm_display_info *info,
917 					  const struct drm_display_mode *mode)
918 {
919 	const u32 num_components = 3, default_bpp = 24;
920 	struct dp_display_private *dp_display;
921 	struct dp_link_info *link_info;
922 	u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
923 	struct msm_dp *dp;
924 	int mode_pclk_khz = mode->clock;
925 
926 	dp = to_dp_bridge(bridge)->dp_display;
927 
928 	if (!dp || !mode_pclk_khz || !dp->connector) {
929 		DRM_ERROR("invalid params\n");
930 		return -EINVAL;
931 	}
932 
933 	if (mode->clock > DP_MAX_PIXEL_CLK_KHZ)
934 		return MODE_CLOCK_HIGH;
935 
936 	dp_display = container_of(dp, struct dp_display_private, dp_display);
937 	link_info = &dp_display->panel->link_info;
938 
939 	mode_bpp = dp->connector->display_info.bpc * num_components;
940 	if (!mode_bpp)
941 		mode_bpp = default_bpp;
942 
943 	mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
944 			mode_bpp, mode_pclk_khz);
945 
946 	mode_rate_khz = mode_pclk_khz * mode_bpp;
947 	supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
948 
949 	if (mode_rate_khz > supported_rate_khz)
950 		return MODE_BAD;
951 
952 	return MODE_OK;
953 }
954 
955 int dp_display_get_modes(struct msm_dp *dp)
956 {
957 	struct dp_display_private *dp_display;
958 
959 	if (!dp) {
960 		DRM_ERROR("invalid params\n");
961 		return 0;
962 	}
963 
964 	dp_display = container_of(dp, struct dp_display_private, dp_display);
965 
966 	return dp_panel_get_modes(dp_display->panel,
967 		dp->connector);
968 }
969 
970 bool dp_display_check_video_test(struct msm_dp *dp)
971 {
972 	struct dp_display_private *dp_display;
973 
974 	dp_display = container_of(dp, struct dp_display_private, dp_display);
975 
976 	return dp_display->panel->video_test;
977 }
978 
979 int dp_display_get_test_bpp(struct msm_dp *dp)
980 {
981 	struct dp_display_private *dp_display;
982 
983 	if (!dp) {
984 		DRM_ERROR("invalid params\n");
985 		return 0;
986 	}
987 
988 	dp_display = container_of(dp, struct dp_display_private, dp_display);
989 
990 	return dp_link_bit_depth_to_bpp(
991 		dp_display->link->test_video.test_bit_depth);
992 }
993 
994 void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
995 {
996 	struct dp_display_private *dp_display;
997 
998 	dp_display = container_of(dp, struct dp_display_private, dp_display);
999 
1000 	/*
1001 	 * if we are reading registers we need the link clocks to be on
1002 	 * however till DP cable is connected this will not happen as we
1003 	 * do not know the resolution to power up with. Hence check the
1004 	 * power_on status before dumping DP registers to avoid crash due
1005 	 * to unclocked access
1006 	 */
1007 	mutex_lock(&dp_display->event_mutex);
1008 
1009 	if (!dp->power_on) {
1010 		mutex_unlock(&dp_display->event_mutex);
1011 		return;
1012 	}
1013 
1014 	dp_catalog_snapshot(dp_display->catalog, disp_state);
1015 
1016 	mutex_unlock(&dp_display->event_mutex);
1017 }
1018 
1019 void dp_display_set_psr(struct msm_dp *dp_display, bool enter)
1020 {
1021 	struct dp_display_private *dp;
1022 
1023 	if (!dp_display) {
1024 		DRM_ERROR("invalid params\n");
1025 		return;
1026 	}
1027 
1028 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1029 	dp_ctrl_set_psr(dp->ctrl, enter);
1030 }
1031 
1032 static int hpd_event_thread(void *data)
1033 {
1034 	struct dp_display_private *dp_priv;
1035 	unsigned long flag;
1036 	struct dp_event *todo;
1037 	int timeout_mode = 0;
1038 
1039 	dp_priv = (struct dp_display_private *)data;
1040 
1041 	while (1) {
1042 		if (timeout_mode) {
1043 			wait_event_timeout(dp_priv->event_q,
1044 				(dp_priv->event_pndx == dp_priv->event_gndx) ||
1045 					kthread_should_stop(), EVENT_TIMEOUT);
1046 		} else {
1047 			wait_event_interruptible(dp_priv->event_q,
1048 				(dp_priv->event_pndx != dp_priv->event_gndx) ||
1049 					kthread_should_stop());
1050 		}
1051 
1052 		if (kthread_should_stop())
1053 			break;
1054 
1055 		spin_lock_irqsave(&dp_priv->event_lock, flag);
1056 		todo = &dp_priv->event_list[dp_priv->event_gndx];
1057 		if (todo->delay) {
1058 			struct dp_event *todo_next;
1059 
1060 			dp_priv->event_gndx++;
1061 			dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1062 
1063 			/* re enter delay event into q */
1064 			todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
1065 			dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1066 			todo_next->event_id = todo->event_id;
1067 			todo_next->data = todo->data;
1068 			todo_next->delay = todo->delay - 1;
1069 
1070 			/* clean up older event */
1071 			todo->event_id = EV_NO_EVENT;
1072 			todo->delay = 0;
1073 
1074 			/* switch to timeout mode */
1075 			timeout_mode = 1;
1076 			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1077 			continue;
1078 		}
1079 
1080 		/* timeout with no events in q */
1081 		if (dp_priv->event_pndx == dp_priv->event_gndx) {
1082 			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1083 			continue;
1084 		}
1085 
1086 		dp_priv->event_gndx++;
1087 		dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1088 		timeout_mode = 0;
1089 		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1090 
1091 		switch (todo->event_id) {
1092 		case EV_HPD_PLUG_INT:
1093 			dp_hpd_plug_handle(dp_priv, todo->data);
1094 			break;
1095 		case EV_HPD_UNPLUG_INT:
1096 			dp_hpd_unplug_handle(dp_priv, todo->data);
1097 			break;
1098 		case EV_IRQ_HPD_INT:
1099 			dp_irq_hpd_handle(dp_priv, todo->data);
1100 			break;
1101 		case EV_USER_NOTIFICATION:
1102 			dp_display_send_hpd_notification(dp_priv,
1103 						todo->data);
1104 			break;
1105 		default:
1106 			break;
1107 		}
1108 	}
1109 
1110 	return 0;
1111 }
1112 
1113 static int dp_hpd_event_thread_start(struct dp_display_private *dp_priv)
1114 {
1115 	/* set event q to empty */
1116 	dp_priv->event_gndx = 0;
1117 	dp_priv->event_pndx = 0;
1118 
1119 	dp_priv->ev_tsk = kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1120 	if (IS_ERR(dp_priv->ev_tsk))
1121 		return PTR_ERR(dp_priv->ev_tsk);
1122 
1123 	return 0;
1124 }
1125 
1126 static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1127 {
1128 	struct dp_display_private *dp = dev_id;
1129 	irqreturn_t ret = IRQ_NONE;
1130 	u32 hpd_isr_status;
1131 
1132 	if (!dp) {
1133 		DRM_ERROR("invalid data\n");
1134 		return IRQ_NONE;
1135 	}
1136 
1137 	hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1138 
1139 	if (hpd_isr_status & 0x0F) {
1140 		drm_dbg_dp(dp->drm_dev, "type=%d isr=0x%x\n",
1141 			dp->dp_display.connector_type, hpd_isr_status);
1142 		/* hpd related interrupts */
1143 		if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1144 			dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1145 
1146 		if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1147 			dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1148 		}
1149 
1150 		if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1151 			dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1152 			dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
1153 		}
1154 
1155 		if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1156 			dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1157 
1158 		ret = IRQ_HANDLED;
1159 	}
1160 
1161 	/* DP controller isr */
1162 	ret |= dp_ctrl_isr(dp->ctrl);
1163 
1164 	/* DP aux isr */
1165 	ret |= dp_aux_isr(dp->aux);
1166 
1167 	return ret;
1168 }
1169 
1170 static int dp_display_request_irq(struct dp_display_private *dp)
1171 {
1172 	int rc = 0;
1173 	struct platform_device *pdev = dp->dp_display.pdev;
1174 
1175 	dp->irq = platform_get_irq(pdev, 0);
1176 	if (dp->irq < 0) {
1177 		DRM_ERROR("failed to get irq\n");
1178 		return dp->irq;
1179 	}
1180 
1181 	rc = devm_request_irq(&pdev->dev, dp->irq, dp_display_irq_handler,
1182 			      IRQF_TRIGGER_HIGH|IRQF_NO_AUTOEN,
1183 			      "dp_display_isr", dp);
1184 
1185 	if (rc < 0) {
1186 		DRM_ERROR("failed to request IRQ%u: %d\n",
1187 				dp->irq, rc);
1188 		return rc;
1189 	}
1190 
1191 	return 0;
1192 }
1193 
1194 static const struct msm_dp_desc *dp_display_get_desc(struct platform_device *pdev)
1195 {
1196 	const struct msm_dp_desc *descs = of_device_get_match_data(&pdev->dev);
1197 	struct resource *res;
1198 	int i;
1199 
1200 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1201 	if (!res)
1202 		return NULL;
1203 
1204 	for (i = 0; i < descs[i].io_start; i++) {
1205 		if (descs[i].io_start == res->start)
1206 			return &descs[i];
1207 	}
1208 
1209 	dev_err(&pdev->dev, "unknown displayport instance\n");
1210 	return NULL;
1211 }
1212 
1213 static int dp_display_get_next_bridge(struct msm_dp *dp);
1214 
1215 static int dp_display_probe_tail(struct device *dev)
1216 {
1217 	struct msm_dp *dp = dev_get_drvdata(dev);
1218 	int ret;
1219 
1220 	ret = dp_display_get_next_bridge(dp);
1221 	if (ret)
1222 		return ret;
1223 
1224 	ret = component_add(dev, &dp_display_comp_ops);
1225 	if (ret)
1226 		DRM_ERROR("component add failed, rc=%d\n", ret);
1227 
1228 	return ret;
1229 }
1230 
1231 static int dp_auxbus_done_probe(struct drm_dp_aux *aux)
1232 {
1233 	return dp_display_probe_tail(aux->dev);
1234 }
1235 
1236 static int dp_display_probe(struct platform_device *pdev)
1237 {
1238 	int rc = 0;
1239 	struct dp_display_private *dp;
1240 	const struct msm_dp_desc *desc;
1241 
1242 	if (!pdev || !pdev->dev.of_node) {
1243 		DRM_ERROR("pdev not found\n");
1244 		return -ENODEV;
1245 	}
1246 
1247 	dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1248 	if (!dp)
1249 		return -ENOMEM;
1250 
1251 	desc = dp_display_get_desc(pdev);
1252 	if (!desc)
1253 		return -EINVAL;
1254 
1255 	dp->dp_display.pdev = pdev;
1256 	dp->name = "drm_dp";
1257 	dp->id = desc->id;
1258 	dp->dp_display.connector_type = desc->connector_type;
1259 	dp->wide_bus_en = desc->wide_bus_en;
1260 	dp->dp_display.is_edp =
1261 		(dp->dp_display.connector_type == DRM_MODE_CONNECTOR_eDP);
1262 
1263 	rc = dp_init_sub_modules(dp);
1264 	if (rc) {
1265 		DRM_ERROR("init sub module failed\n");
1266 		return -EPROBE_DEFER;
1267 	}
1268 
1269 	rc = dp->parser->parse(dp->parser);
1270 	if (rc) {
1271 		DRM_ERROR("device tree parsing failed\n");
1272 		goto err;
1273 	}
1274 
1275 	rc = dp_power_client_init(dp->power);
1276 	if (rc) {
1277 		DRM_ERROR("Power client create failed\n");
1278 		goto err;
1279 	}
1280 
1281 	/* setup event q */
1282 	mutex_init(&dp->event_mutex);
1283 	init_waitqueue_head(&dp->event_q);
1284 	spin_lock_init(&dp->event_lock);
1285 
1286 	/* Store DP audio handle inside DP display */
1287 	dp->dp_display.dp_audio = dp->audio;
1288 
1289 	init_completion(&dp->audio_comp);
1290 
1291 	platform_set_drvdata(pdev, &dp->dp_display);
1292 
1293 	rc = devm_pm_runtime_enable(&pdev->dev);
1294 	if (rc)
1295 		goto err;
1296 
1297 	rc = dp_display_request_irq(dp);
1298 	if (rc)
1299 		goto err;
1300 
1301 	if (dp->dp_display.is_edp) {
1302 		rc = devm_of_dp_aux_populate_bus(dp->aux, dp_auxbus_done_probe);
1303 		if (rc) {
1304 			DRM_ERROR("eDP auxbus population failed, rc=%d\n", rc);
1305 			goto err;
1306 		}
1307 	} else {
1308 		rc = dp_display_probe_tail(&pdev->dev);
1309 		if (rc)
1310 			goto err;
1311 	}
1312 
1313 	return rc;
1314 
1315 err:
1316 	dp_display_deinit_sub_modules(dp);
1317 	return rc;
1318 }
1319 
1320 static void dp_display_remove(struct platform_device *pdev)
1321 {
1322 	struct dp_display_private *dp = dev_get_dp_display_private(&pdev->dev);
1323 
1324 	component_del(&pdev->dev, &dp_display_comp_ops);
1325 	dp_display_deinit_sub_modules(dp);
1326 	platform_set_drvdata(pdev, NULL);
1327 }
1328 
1329 static int dp_pm_runtime_suspend(struct device *dev)
1330 {
1331 	struct dp_display_private *dp = dev_get_dp_display_private(dev);
1332 
1333 	disable_irq(dp->irq);
1334 
1335 	if (dp->dp_display.is_edp) {
1336 		dp_display_host_phy_exit(dp);
1337 		dp_catalog_ctrl_hpd_disable(dp->catalog);
1338 	}
1339 	dp_display_host_deinit(dp);
1340 
1341 	return 0;
1342 }
1343 
1344 static int dp_pm_runtime_resume(struct device *dev)
1345 {
1346 	struct dp_display_private *dp = dev_get_dp_display_private(dev);
1347 
1348 	/*
1349 	 * for eDP, host cotroller, HPD block and PHY are enabled here
1350 	 * but with HPD irq disabled
1351 	 *
1352 	 * for DP, only host controller is enabled here.
1353 	 * HPD block is enabled at dp_bridge_hpd_enable()
1354 	 * PHY will be enabled at plugin handler later
1355 	 */
1356 	dp_display_host_init(dp);
1357 	if (dp->dp_display.is_edp) {
1358 		dp_catalog_ctrl_hpd_enable(dp->catalog);
1359 		dp_display_host_phy_init(dp);
1360 	}
1361 
1362 	enable_irq(dp->irq);
1363 	return 0;
1364 }
1365 
1366 static const struct dev_pm_ops dp_pm_ops = {
1367 	SET_RUNTIME_PM_OPS(dp_pm_runtime_suspend, dp_pm_runtime_resume, NULL)
1368 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1369 				pm_runtime_force_resume)
1370 };
1371 
1372 static struct platform_driver dp_display_driver = {
1373 	.probe  = dp_display_probe,
1374 	.remove_new = dp_display_remove,
1375 	.driver = {
1376 		.name = "msm-dp-display",
1377 		.of_match_table = dp_dt_match,
1378 		.suppress_bind_attrs = true,
1379 		.pm = &dp_pm_ops,
1380 	},
1381 };
1382 
1383 int __init msm_dp_register(void)
1384 {
1385 	int ret;
1386 
1387 	ret = platform_driver_register(&dp_display_driver);
1388 	if (ret)
1389 		DRM_ERROR("Dp display driver register failed");
1390 
1391 	return ret;
1392 }
1393 
1394 void __exit msm_dp_unregister(void)
1395 {
1396 	platform_driver_unregister(&dp_display_driver);
1397 }
1398 
1399 bool msm_dp_wide_bus_available(const struct msm_dp *dp_display)
1400 {
1401 	struct dp_display_private *dp;
1402 
1403 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1404 
1405 	return dp->wide_bus_en;
1406 }
1407 
1408 void dp_display_debugfs_init(struct msm_dp *dp_display, struct dentry *root, bool is_edp)
1409 {
1410 	struct dp_display_private *dp;
1411 	struct device *dev;
1412 	int rc;
1413 
1414 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1415 	dev = &dp->dp_display.pdev->dev;
1416 
1417 	dp->debug = dp_debug_get(dev, dp->panel,
1418 					dp->link, dp->dp_display.connector,
1419 					root, is_edp);
1420 	if (IS_ERR(dp->debug)) {
1421 		rc = PTR_ERR(dp->debug);
1422 		DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1423 		dp->debug = NULL;
1424 	}
1425 }
1426 
1427 static int dp_display_get_next_bridge(struct msm_dp *dp)
1428 {
1429 	int rc;
1430 	struct dp_display_private *dp_priv;
1431 
1432 	dp_priv = container_of(dp, struct dp_display_private, dp_display);
1433 
1434 	/*
1435 	 * External bridges are mandatory for eDP interfaces: one has to
1436 	 * provide at least an eDP panel (which gets wrapped into panel-bridge).
1437 	 *
1438 	 * For DisplayPort interfaces external bridges are optional, so
1439 	 * silently ignore an error if one is not present (-ENODEV).
1440 	 */
1441 	rc = devm_dp_parser_find_next_bridge(&dp->pdev->dev, dp_priv->parser);
1442 	if (!dp->is_edp && rc == -ENODEV)
1443 		return 0;
1444 
1445 	if (!rc)
1446 		dp->next_bridge = dp_priv->parser->next_bridge;
1447 
1448 	return rc;
1449 }
1450 
1451 int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1452 			struct drm_encoder *encoder)
1453 {
1454 	struct dp_display_private *dp_priv;
1455 	int ret;
1456 
1457 	dp_display->drm_dev = dev;
1458 
1459 	dp_priv = container_of(dp_display, struct dp_display_private, dp_display);
1460 
1461 	ret = dp_bridge_init(dp_display, dev, encoder);
1462 	if (ret) {
1463 		DRM_DEV_ERROR(dev->dev,
1464 			"failed to create dp bridge: %d\n", ret);
1465 		return ret;
1466 	}
1467 
1468 	dp_display->connector = dp_drm_connector_init(dp_display, encoder);
1469 	if (IS_ERR(dp_display->connector)) {
1470 		ret = PTR_ERR(dp_display->connector);
1471 		DRM_DEV_ERROR(dev->dev,
1472 			"failed to create dp connector: %d\n", ret);
1473 		dp_display->connector = NULL;
1474 		return ret;
1475 	}
1476 
1477 	dp_priv->panel->connector = dp_display->connector;
1478 
1479 	return 0;
1480 }
1481 
1482 void dp_bridge_atomic_enable(struct drm_bridge *drm_bridge,
1483 			     struct drm_bridge_state *old_bridge_state)
1484 {
1485 	struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
1486 	struct msm_dp *dp = dp_bridge->dp_display;
1487 	int rc = 0;
1488 	struct dp_display_private *dp_display;
1489 	u32 state;
1490 	bool force_link_train = false;
1491 
1492 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1493 	if (!dp_display->dp_mode.drm_mode.clock) {
1494 		DRM_ERROR("invalid params\n");
1495 		return;
1496 	}
1497 
1498 	if (dp->is_edp)
1499 		dp_hpd_plug_handle(dp_display, 0);
1500 
1501 	mutex_lock(&dp_display->event_mutex);
1502 	if (pm_runtime_resume_and_get(&dp->pdev->dev)) {
1503 		DRM_ERROR("failed to pm_runtime_resume\n");
1504 		mutex_unlock(&dp_display->event_mutex);
1505 		return;
1506 	}
1507 
1508 	state = dp_display->hpd_state;
1509 	if (state != ST_DISPLAY_OFF && state != ST_MAINLINK_READY) {
1510 		mutex_unlock(&dp_display->event_mutex);
1511 		return;
1512 	}
1513 
1514 	rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1515 	if (rc) {
1516 		DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1517 		mutex_unlock(&dp_display->event_mutex);
1518 		return;
1519 	}
1520 
1521 	state =  dp_display->hpd_state;
1522 
1523 	if (state == ST_DISPLAY_OFF) {
1524 		dp_display_host_phy_init(dp_display);
1525 		force_link_train = true;
1526 	}
1527 
1528 	dp_display_enable(dp_display, force_link_train);
1529 
1530 	rc = dp_display_post_enable(dp);
1531 	if (rc) {
1532 		DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1533 		dp_display_disable(dp_display);
1534 	}
1535 
1536 	/* completed connection */
1537 	dp_display->hpd_state = ST_CONNECTED;
1538 
1539 	drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
1540 	mutex_unlock(&dp_display->event_mutex);
1541 }
1542 
1543 void dp_bridge_atomic_disable(struct drm_bridge *drm_bridge,
1544 			      struct drm_bridge_state *old_bridge_state)
1545 {
1546 	struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
1547 	struct msm_dp *dp = dp_bridge->dp_display;
1548 	struct dp_display_private *dp_display;
1549 
1550 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1551 
1552 	dp_ctrl_push_idle(dp_display->ctrl);
1553 }
1554 
1555 void dp_bridge_atomic_post_disable(struct drm_bridge *drm_bridge,
1556 				   struct drm_bridge_state *old_bridge_state)
1557 {
1558 	struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
1559 	struct msm_dp *dp = dp_bridge->dp_display;
1560 	u32 state;
1561 	struct dp_display_private *dp_display;
1562 
1563 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1564 
1565 	if (dp->is_edp)
1566 		dp_hpd_unplug_handle(dp_display, 0);
1567 
1568 	mutex_lock(&dp_display->event_mutex);
1569 
1570 	state = dp_display->hpd_state;
1571 	if (state != ST_DISCONNECT_PENDING && state != ST_CONNECTED)
1572 		drm_dbg_dp(dp->drm_dev, "type=%d wrong hpd_state=%d\n",
1573 			   dp->connector_type, state);
1574 
1575 	dp_display_disable(dp_display);
1576 
1577 	state =  dp_display->hpd_state;
1578 	if (state == ST_DISCONNECT_PENDING) {
1579 		/* completed disconnection */
1580 		dp_display->hpd_state = ST_DISCONNECTED;
1581 	} else {
1582 		dp_display->hpd_state = ST_DISPLAY_OFF;
1583 	}
1584 
1585 	drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
1586 
1587 	pm_runtime_put_sync(&dp->pdev->dev);
1588 	mutex_unlock(&dp_display->event_mutex);
1589 }
1590 
1591 void dp_bridge_mode_set(struct drm_bridge *drm_bridge,
1592 			const struct drm_display_mode *mode,
1593 			const struct drm_display_mode *adjusted_mode)
1594 {
1595 	struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
1596 	struct msm_dp *dp = dp_bridge->dp_display;
1597 	struct dp_display_private *dp_display;
1598 
1599 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1600 
1601 	memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1602 
1603 	if (dp_display_check_video_test(dp))
1604 		dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1605 	else /* Default num_components per pixel = 3 */
1606 		dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1607 
1608 	if (!dp_display->dp_mode.bpp)
1609 		dp_display->dp_mode.bpp = 24; /* Default bpp */
1610 
1611 	drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1612 
1613 	dp_display->dp_mode.v_active_low =
1614 		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1615 
1616 	dp_display->dp_mode.h_active_low =
1617 		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);
1618 }
1619 
1620 void dp_bridge_hpd_enable(struct drm_bridge *bridge)
1621 {
1622 	struct msm_dp_bridge *dp_bridge = to_dp_bridge(bridge);
1623 	struct msm_dp *dp_display = dp_bridge->dp_display;
1624 	struct dp_display_private *dp = container_of(dp_display, struct dp_display_private, dp_display);
1625 
1626 	/*
1627 	 * this is for external DP with hpd irq enabled case,
1628 	 * step-1: dp_pm_runtime_resume() enable dp host only
1629 	 * step-2: enable hdp block and have hpd irq enabled here
1630 	 * step-3: waiting for plugin irq while phy is not initialized
1631 	 * step-4: DP PHY is initialized at plugin handler before link training
1632 	 *
1633 	 */
1634 	mutex_lock(&dp->event_mutex);
1635 	if (pm_runtime_resume_and_get(&dp_display->pdev->dev)) {
1636 		DRM_ERROR("failed to resume power\n");
1637 		mutex_unlock(&dp->event_mutex);
1638 		return;
1639 	}
1640 
1641 	dp_catalog_ctrl_hpd_enable(dp->catalog);
1642 
1643 	/* enable HDP interrupts */
1644 	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, true);
1645 
1646 	dp_display->internal_hpd = true;
1647 	mutex_unlock(&dp->event_mutex);
1648 }
1649 
1650 void dp_bridge_hpd_disable(struct drm_bridge *bridge)
1651 {
1652 	struct msm_dp_bridge *dp_bridge = to_dp_bridge(bridge);
1653 	struct msm_dp *dp_display = dp_bridge->dp_display;
1654 	struct dp_display_private *dp = container_of(dp_display, struct dp_display_private, dp_display);
1655 
1656 	mutex_lock(&dp->event_mutex);
1657 	/* disable HDP interrupts */
1658 	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, false);
1659 	dp_catalog_ctrl_hpd_disable(dp->catalog);
1660 
1661 	dp_display->internal_hpd = false;
1662 
1663 	pm_runtime_put_sync(&dp_display->pdev->dev);
1664 	mutex_unlock(&dp->event_mutex);
1665 }
1666 
1667 void dp_bridge_hpd_notify(struct drm_bridge *bridge,
1668 			  enum drm_connector_status status)
1669 {
1670 	struct msm_dp_bridge *dp_bridge = to_dp_bridge(bridge);
1671 	struct msm_dp *dp_display = dp_bridge->dp_display;
1672 	struct dp_display_private *dp = container_of(dp_display, struct dp_display_private, dp_display);
1673 
1674 	/* Without next_bridge interrupts are handled by the DP core directly */
1675 	if (dp_display->internal_hpd)
1676 		return;
1677 
1678 	if (!dp_display->link_ready && status == connector_status_connected)
1679 		dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1680 	else if (dp_display->link_ready && status == connector_status_disconnected)
1681 		dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1682 }
1683