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