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