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