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