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