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