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