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