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