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