xref: /openbmc/linux/drivers/gpu/drm/msm/dp/dp_display.c (revision 1fd02f66)
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 	/*
584 	 * add fail safe mode outside event_mutex scope
585 	 * to avoid potiential circular lock with drm thread
586 	 */
587 	dp_panel_add_fail_safe_mode(dp->dp_display.connector);
588 
589 	/* uevent will complete connection part */
590 	return 0;
591 };
592 
593 static int dp_display_enable(struct dp_display_private *dp, u32 data);
594 static int dp_display_disable(struct dp_display_private *dp, u32 data);
595 
596 static int dp_connect_pending_timeout(struct dp_display_private *dp, u32 data)
597 {
598 	u32 state;
599 
600 	mutex_lock(&dp->event_mutex);
601 
602 	state = dp->hpd_state;
603 	if (state == ST_CONNECT_PENDING) {
604 		dp->hpd_state = ST_CONNECTED;
605 		DRM_DEBUG_DP("type=%d\n", dp->dp_display.connector_type);
606 	}
607 
608 	mutex_unlock(&dp->event_mutex);
609 
610 	return 0;
611 }
612 
613 static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
614 		bool plugged)
615 {
616 	struct dp_display_private *dp;
617 
618 	dp = container_of(dp_display,
619 			struct dp_display_private, dp_display);
620 
621 	/* notify audio subsystem only if sink supports audio */
622 	if (dp_display->plugged_cb && dp_display->codec_dev &&
623 			dp->audio_supported)
624 		dp_display->plugged_cb(dp_display->codec_dev, plugged);
625 }
626 
627 static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
628 {
629 	struct dp_usbpd *hpd = dp->usbpd;
630 	u32 state;
631 
632 	if (!hpd)
633 		return 0;
634 
635 	mutex_lock(&dp->event_mutex);
636 
637 	state = dp->hpd_state;
638 
639 	DRM_DEBUG_DP("Before, type=%d hpd_state=%d\n",
640 			dp->dp_display.connector_type, state);
641 
642 	/* disable irq_hpd/replug interrupts */
643 	dp_catalog_hpd_config_intr(dp->catalog,
644 		DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, false);
645 
646 	/* unplugged, no more irq_hpd handle */
647 	dp_del_event(dp, EV_IRQ_HPD_INT);
648 
649 	if (state == ST_DISCONNECTED) {
650 		/* triggered by irq_hdp with sink_count = 0 */
651 		if (dp->link->sink_count == 0) {
652 			dp_display_host_phy_exit(dp);
653 		}
654 		mutex_unlock(&dp->event_mutex);
655 		return 0;
656 	}
657 
658 	if (state == ST_DISCONNECT_PENDING) {
659 		mutex_unlock(&dp->event_mutex);
660 		return 0;
661 	}
662 
663 	if (state == ST_CONNECT_PENDING) {
664 		/* wait until CONNECTED */
665 		dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 1); /* delay = 1 */
666 		mutex_unlock(&dp->event_mutex);
667 		return 0;
668 	}
669 
670 	dp->hpd_state = ST_DISCONNECT_PENDING;
671 
672 	/* disable HPD plug interrupts */
673 	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, false);
674 
675 	/*
676 	 * We don't need separate work for disconnect as
677 	 * connect/attention interrupts are disabled
678 	 */
679 	dp_display_usbpd_disconnect_cb(&dp->pdev->dev);
680 
681 	/* start sentinel checking in case of missing uevent */
682 	dp_add_event(dp, EV_DISCONNECT_PENDING_TIMEOUT, 0, DP_TIMEOUT_5_SECOND);
683 
684 	/* signal the disconnect event early to ensure proper teardown */
685 	dp_display_handle_plugged_change(&dp->dp_display, false);
686 
687 	/* enable HDP plug interrupt to prepare for next plugin */
688 	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, true);
689 
690 	DRM_DEBUG_DP("After, type=%d hpd_state=%d\n",
691 			dp->dp_display.connector_type, state);
692 
693 	/* uevent will complete disconnection part */
694 	mutex_unlock(&dp->event_mutex);
695 	return 0;
696 }
697 
698 static int dp_disconnect_pending_timeout(struct dp_display_private *dp, u32 data)
699 {
700 	u32 state;
701 
702 	mutex_lock(&dp->event_mutex);
703 
704 	state =  dp->hpd_state;
705 	if (state == ST_DISCONNECT_PENDING) {
706 		dp->hpd_state = ST_DISCONNECTED;
707 		DRM_DEBUG_DP("type=%d\n", dp->dp_display.connector_type);
708 	}
709 
710 	mutex_unlock(&dp->event_mutex);
711 
712 	return 0;
713 }
714 
715 static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
716 {
717 	u32 state;
718 
719 	mutex_lock(&dp->event_mutex);
720 
721 	/* irq_hpd can happen at either connected or disconnected state */
722 	state =  dp->hpd_state;
723 	DRM_DEBUG_DP("Before, type=%d hpd_state=%d\n",
724 			dp->dp_display.connector_type, state);
725 
726 	if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
727 		mutex_unlock(&dp->event_mutex);
728 		return 0;
729 	}
730 
731 	if (state == ST_CONNECT_PENDING) {
732 		/* wait until ST_CONNECTED */
733 		dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
734 		mutex_unlock(&dp->event_mutex);
735 		return 0;
736 	}
737 
738 	if (state == ST_CONNECT_PENDING || state == ST_DISCONNECT_PENDING) {
739 		/* wait until ST_CONNECTED */
740 		dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
741 		mutex_unlock(&dp->event_mutex);
742 		return 0;
743 	}
744 
745 	dp_display_usbpd_attention_cb(&dp->pdev->dev);
746 
747 	DRM_DEBUG_DP("After, type=%d hpd_state=%d\n",
748 			dp->dp_display.connector_type, state);
749 
750 	mutex_unlock(&dp->event_mutex);
751 
752 	return 0;
753 }
754 
755 static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
756 {
757 	dp_debug_put(dp->debug);
758 	dp_audio_put(dp->audio);
759 	dp_panel_put(dp->panel);
760 	dp_aux_put(dp->aux);
761 }
762 
763 static int dp_init_sub_modules(struct dp_display_private *dp)
764 {
765 	int rc = 0;
766 	struct device *dev = &dp->pdev->dev;
767 	struct dp_usbpd_cb *cb = &dp->usbpd_cb;
768 	struct dp_panel_in panel_in = {
769 		.dev = dev,
770 	};
771 
772 	/* Callback APIs used for cable status change event */
773 	cb->configure  = dp_display_usbpd_configure_cb;
774 	cb->disconnect = dp_display_usbpd_disconnect_cb;
775 	cb->attention  = dp_display_usbpd_attention_cb;
776 
777 	dp->usbpd = dp_hpd_get(dev, cb);
778 	if (IS_ERR(dp->usbpd)) {
779 		rc = PTR_ERR(dp->usbpd);
780 		DRM_ERROR("failed to initialize hpd, rc = %d\n", rc);
781 		dp->usbpd = NULL;
782 		goto error;
783 	}
784 
785 	dp->parser = dp_parser_get(dp->pdev);
786 	if (IS_ERR(dp->parser)) {
787 		rc = PTR_ERR(dp->parser);
788 		DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
789 		dp->parser = NULL;
790 		goto error;
791 	}
792 
793 	dp->catalog = dp_catalog_get(dev, &dp->parser->io);
794 	if (IS_ERR(dp->catalog)) {
795 		rc = PTR_ERR(dp->catalog);
796 		DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
797 		dp->catalog = NULL;
798 		goto error;
799 	}
800 
801 	dp->power = dp_power_get(dev, dp->parser);
802 	if (IS_ERR(dp->power)) {
803 		rc = PTR_ERR(dp->power);
804 		DRM_ERROR("failed to initialize power, rc = %d\n", rc);
805 		dp->power = NULL;
806 		goto error;
807 	}
808 
809 	dp->aux = dp_aux_get(dev, dp->catalog);
810 	if (IS_ERR(dp->aux)) {
811 		rc = PTR_ERR(dp->aux);
812 		DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
813 		dp->aux = NULL;
814 		goto error;
815 	}
816 
817 	dp->link = dp_link_get(dev, dp->aux);
818 	if (IS_ERR(dp->link)) {
819 		rc = PTR_ERR(dp->link);
820 		DRM_ERROR("failed to initialize link, rc = %d\n", rc);
821 		dp->link = NULL;
822 		goto error_link;
823 	}
824 
825 	panel_in.aux = dp->aux;
826 	panel_in.catalog = dp->catalog;
827 	panel_in.link = dp->link;
828 
829 	dp->panel = dp_panel_get(&panel_in);
830 	if (IS_ERR(dp->panel)) {
831 		rc = PTR_ERR(dp->panel);
832 		DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
833 		dp->panel = NULL;
834 		goto error_link;
835 	}
836 
837 	dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
838 			       dp->power, dp->catalog, dp->parser);
839 	if (IS_ERR(dp->ctrl)) {
840 		rc = PTR_ERR(dp->ctrl);
841 		DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
842 		dp->ctrl = NULL;
843 		goto error_ctrl;
844 	}
845 
846 	dp->audio = dp_audio_get(dp->pdev, dp->panel, dp->catalog);
847 	if (IS_ERR(dp->audio)) {
848 		rc = PTR_ERR(dp->audio);
849 		pr_err("failed to initialize audio, rc = %d\n", rc);
850 		dp->audio = NULL;
851 		goto error_ctrl;
852 	}
853 
854 	return rc;
855 
856 error_ctrl:
857 	dp_panel_put(dp->panel);
858 error_link:
859 	dp_aux_put(dp->aux);
860 error:
861 	return rc;
862 }
863 
864 static int dp_display_set_mode(struct msm_dp *dp_display,
865 			       struct dp_display_mode *mode)
866 {
867 	struct dp_display_private *dp;
868 
869 	dp = container_of(dp_display, struct dp_display_private, dp_display);
870 
871 	dp->panel->dp_mode.drm_mode = mode->drm_mode;
872 	dp->panel->dp_mode.bpp = mode->bpp;
873 	dp->panel->dp_mode.capabilities = mode->capabilities;
874 	dp_panel_init_panel_info(dp->panel);
875 	return 0;
876 }
877 
878 static int dp_display_prepare(struct msm_dp *dp_display)
879 {
880 	return 0;
881 }
882 
883 static int dp_display_enable(struct dp_display_private *dp, u32 data)
884 {
885 	int rc = 0;
886 	struct msm_dp *dp_display = &dp->dp_display;
887 
888 	DRM_DEBUG_DP("sink_count=%d\n", dp->link->sink_count);
889 	if (dp_display->power_on) {
890 		DRM_DEBUG_DP("Link already setup, return\n");
891 		return 0;
892 	}
893 
894 	rc = dp_ctrl_on_stream(dp->ctrl);
895 	if (!rc)
896 		dp_display->power_on = true;
897 
898 	return rc;
899 }
900 
901 static int dp_display_post_enable(struct msm_dp *dp_display)
902 {
903 	struct dp_display_private *dp;
904 	u32 rate;
905 
906 	dp = container_of(dp_display, struct dp_display_private, dp_display);
907 
908 	rate = dp->link->link_params.rate;
909 
910 	if (dp->audio_supported) {
911 		dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
912 		dp->audio->lane_count = dp->link->link_params.num_lanes;
913 	}
914 
915 	/* signal the connect event late to synchronize video and display */
916 	dp_display_handle_plugged_change(dp_display, true);
917 	return 0;
918 }
919 
920 static int dp_display_disable(struct dp_display_private *dp, u32 data)
921 {
922 	struct msm_dp *dp_display = &dp->dp_display;
923 
924 	if (!dp_display->power_on)
925 		return 0;
926 
927 	/* wait only if audio was enabled */
928 	if (dp_display->audio_enabled) {
929 		/* signal the disconnect event */
930 		dp_display_handle_plugged_change(dp_display, false);
931 		if (!wait_for_completion_timeout(&dp->audio_comp,
932 				HZ * 5))
933 			DRM_ERROR("audio comp timeout\n");
934 	}
935 
936 	dp_display->audio_enabled = false;
937 
938 	if (dp->link->sink_count == 0) {
939 		/*
940 		 * irq_hpd with sink_count = 0
941 		 * hdmi unplugged out of dongle
942 		 */
943 		dp_ctrl_off_link_stream(dp->ctrl);
944 	} else {
945 		/*
946 		 * unplugged interrupt
947 		 * dongle unplugged out of DUT
948 		 */
949 		dp_ctrl_off(dp->ctrl);
950 		dp_display_host_phy_exit(dp);
951 	}
952 
953 	dp_display->power_on = false;
954 
955 	DRM_DEBUG_DP("sink count: %d\n", dp->link->sink_count);
956 	return 0;
957 }
958 
959 static int dp_display_unprepare(struct msm_dp *dp_display)
960 {
961 	return 0;
962 }
963 
964 int dp_display_set_plugged_cb(struct msm_dp *dp_display,
965 		hdmi_codec_plugged_cb fn, struct device *codec_dev)
966 {
967 	bool plugged;
968 
969 	dp_display->plugged_cb = fn;
970 	dp_display->codec_dev = codec_dev;
971 	plugged = dp_display->is_connected;
972 	dp_display_handle_plugged_change(dp_display, plugged);
973 
974 	return 0;
975 }
976 
977 int dp_display_validate_mode(struct msm_dp *dp, u32 mode_pclk_khz)
978 {
979 	const u32 num_components = 3, default_bpp = 24;
980 	struct dp_display_private *dp_display;
981 	struct dp_link_info *link_info;
982 	u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
983 
984 	if (!dp || !mode_pclk_khz || !dp->connector) {
985 		DRM_ERROR("invalid params\n");
986 		return -EINVAL;
987 	}
988 
989 	dp_display = container_of(dp, struct dp_display_private, dp_display);
990 	link_info = &dp_display->panel->link_info;
991 
992 	mode_bpp = dp->connector->display_info.bpc * num_components;
993 	if (!mode_bpp)
994 		mode_bpp = default_bpp;
995 
996 	mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
997 			mode_bpp, mode_pclk_khz);
998 
999 	mode_rate_khz = mode_pclk_khz * mode_bpp;
1000 	supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
1001 
1002 	if (mode_rate_khz > supported_rate_khz)
1003 		return MODE_BAD;
1004 
1005 	return MODE_OK;
1006 }
1007 
1008 int dp_display_get_modes(struct msm_dp *dp,
1009 				struct dp_display_mode *dp_mode)
1010 {
1011 	struct dp_display_private *dp_display;
1012 	int ret = 0;
1013 
1014 	if (!dp) {
1015 		DRM_ERROR("invalid params\n");
1016 		return 0;
1017 	}
1018 
1019 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1020 
1021 	ret = dp_panel_get_modes(dp_display->panel,
1022 		dp->connector, dp_mode);
1023 	if (dp_mode->drm_mode.clock)
1024 		dp->max_pclk_khz = dp_mode->drm_mode.clock;
1025 	return ret;
1026 }
1027 
1028 bool dp_display_check_video_test(struct msm_dp *dp)
1029 {
1030 	struct dp_display_private *dp_display;
1031 
1032 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1033 
1034 	return dp_display->panel->video_test;
1035 }
1036 
1037 int dp_display_get_test_bpp(struct msm_dp *dp)
1038 {
1039 	struct dp_display_private *dp_display;
1040 
1041 	if (!dp) {
1042 		DRM_ERROR("invalid params\n");
1043 		return 0;
1044 	}
1045 
1046 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1047 
1048 	return dp_link_bit_depth_to_bpp(
1049 		dp_display->link->test_video.test_bit_depth);
1050 }
1051 
1052 void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
1053 {
1054 	struct dp_display_private *dp_display;
1055 
1056 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1057 
1058 	/*
1059 	 * if we are reading registers we need the link clocks to be on
1060 	 * however till DP cable is connected this will not happen as we
1061 	 * do not know the resolution to power up with. Hence check the
1062 	 * power_on status before dumping DP registers to avoid crash due
1063 	 * to unclocked access
1064 	 */
1065 	mutex_lock(&dp_display->event_mutex);
1066 
1067 	if (!dp->power_on) {
1068 		mutex_unlock(&dp_display->event_mutex);
1069 		return;
1070 	}
1071 
1072 	dp_catalog_snapshot(dp_display->catalog, disp_state);
1073 
1074 	mutex_unlock(&dp_display->event_mutex);
1075 }
1076 
1077 static void dp_display_config_hpd(struct dp_display_private *dp)
1078 {
1079 
1080 	dp_display_host_init(dp);
1081 	dp_catalog_ctrl_hpd_config(dp->catalog);
1082 
1083 	/* Enable interrupt first time
1084 	 * we are leaving dp clocks on during disconnect
1085 	 * and never disable interrupt
1086 	 */
1087 	enable_irq(dp->irq);
1088 }
1089 
1090 static int hpd_event_thread(void *data)
1091 {
1092 	struct dp_display_private *dp_priv;
1093 	unsigned long flag;
1094 	struct dp_event *todo;
1095 	int timeout_mode = 0;
1096 
1097 	dp_priv = (struct dp_display_private *)data;
1098 
1099 	while (1) {
1100 		if (timeout_mode) {
1101 			wait_event_timeout(dp_priv->event_q,
1102 				(dp_priv->event_pndx == dp_priv->event_gndx),
1103 						EVENT_TIMEOUT);
1104 		} else {
1105 			wait_event_interruptible(dp_priv->event_q,
1106 				(dp_priv->event_pndx != dp_priv->event_gndx));
1107 		}
1108 		spin_lock_irqsave(&dp_priv->event_lock, flag);
1109 		todo = &dp_priv->event_list[dp_priv->event_gndx];
1110 		if (todo->delay) {
1111 			struct dp_event *todo_next;
1112 
1113 			dp_priv->event_gndx++;
1114 			dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1115 
1116 			/* re enter delay event into q */
1117 			todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
1118 			dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1119 			todo_next->event_id = todo->event_id;
1120 			todo_next->data = todo->data;
1121 			todo_next->delay = todo->delay - 1;
1122 
1123 			/* clean up older event */
1124 			todo->event_id = EV_NO_EVENT;
1125 			todo->delay = 0;
1126 
1127 			/* switch to timeout mode */
1128 			timeout_mode = 1;
1129 			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1130 			continue;
1131 		}
1132 
1133 		/* timeout with no events in q */
1134 		if (dp_priv->event_pndx == dp_priv->event_gndx) {
1135 			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1136 			continue;
1137 		}
1138 
1139 		dp_priv->event_gndx++;
1140 		dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1141 		timeout_mode = 0;
1142 		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1143 
1144 		switch (todo->event_id) {
1145 		case EV_HPD_INIT_SETUP:
1146 			dp_display_config_hpd(dp_priv);
1147 			break;
1148 		case EV_HPD_PLUG_INT:
1149 			dp_hpd_plug_handle(dp_priv, todo->data);
1150 			break;
1151 		case EV_HPD_UNPLUG_INT:
1152 			dp_hpd_unplug_handle(dp_priv, todo->data);
1153 			break;
1154 		case EV_IRQ_HPD_INT:
1155 			dp_irq_hpd_handle(dp_priv, todo->data);
1156 			break;
1157 		case EV_USER_NOTIFICATION:
1158 			dp_display_send_hpd_notification(dp_priv,
1159 						todo->data);
1160 			break;
1161 		case EV_CONNECT_PENDING_TIMEOUT:
1162 			dp_connect_pending_timeout(dp_priv,
1163 						todo->data);
1164 			break;
1165 		case EV_DISCONNECT_PENDING_TIMEOUT:
1166 			dp_disconnect_pending_timeout(dp_priv,
1167 						todo->data);
1168 			break;
1169 		default:
1170 			break;
1171 		}
1172 	}
1173 
1174 	return 0;
1175 }
1176 
1177 static void dp_hpd_event_setup(struct dp_display_private *dp_priv)
1178 {
1179 	init_waitqueue_head(&dp_priv->event_q);
1180 	spin_lock_init(&dp_priv->event_lock);
1181 
1182 	kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1183 }
1184 
1185 static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1186 {
1187 	struct dp_display_private *dp = dev_id;
1188 	irqreturn_t ret = IRQ_HANDLED;
1189 	u32 hpd_isr_status;
1190 
1191 	if (!dp) {
1192 		DRM_ERROR("invalid data\n");
1193 		return IRQ_NONE;
1194 	}
1195 
1196 	hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1197 
1198 	if (hpd_isr_status & 0x0F) {
1199 		DRM_DEBUG_DP("type=%d isr=0x%x\n",
1200 			dp->dp_display.connector_type, hpd_isr_status);
1201 		/* hpd related interrupts */
1202 		if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1203 			dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1204 
1205 		if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1206 			/* stop sentinel connect pending checking */
1207 			dp_del_event(dp, EV_CONNECT_PENDING_TIMEOUT);
1208 			dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1209 		}
1210 
1211 		if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1212 			dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1213 			dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
1214 		}
1215 
1216 		if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1217 			dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1218 	}
1219 
1220 	/* DP controller isr */
1221 	dp_ctrl_isr(dp->ctrl);
1222 
1223 	/* DP aux isr */
1224 	dp_aux_isr(dp->aux);
1225 
1226 	return ret;
1227 }
1228 
1229 int dp_display_request_irq(struct msm_dp *dp_display)
1230 {
1231 	int rc = 0;
1232 	struct dp_display_private *dp;
1233 
1234 	if (!dp_display) {
1235 		DRM_ERROR("invalid input\n");
1236 		return -EINVAL;
1237 	}
1238 
1239 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1240 
1241 	dp->irq = irq_of_parse_and_map(dp->pdev->dev.of_node, 0);
1242 	if (dp->irq < 0) {
1243 		rc = dp->irq;
1244 		DRM_ERROR("failed to get irq: %d\n", rc);
1245 		return rc;
1246 	}
1247 
1248 	rc = devm_request_irq(&dp->pdev->dev, dp->irq,
1249 			dp_display_irq_handler,
1250 			IRQF_TRIGGER_HIGH, "dp_display_isr", dp);
1251 	if (rc < 0) {
1252 		DRM_ERROR("failed to request IRQ%u: %d\n",
1253 				dp->irq, rc);
1254 		return rc;
1255 	}
1256 	disable_irq(dp->irq);
1257 
1258 	return 0;
1259 }
1260 
1261 static const struct msm_dp_desc *dp_display_get_desc(struct platform_device *pdev,
1262 						     unsigned int *id)
1263 {
1264 	const struct msm_dp_config *cfg = of_device_get_match_data(&pdev->dev);
1265 	struct resource *res;
1266 	int i;
1267 
1268 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1269 	if (!res)
1270 		return NULL;
1271 
1272 	for (i = 0; i < cfg->num_descs; i++) {
1273 		if (cfg->descs[i].io_start == res->start) {
1274 			*id = i;
1275 			return &cfg->descs[i];
1276 		}
1277 	}
1278 
1279 	dev_err(&pdev->dev, "unknown displayport instance\n");
1280 	return NULL;
1281 }
1282 
1283 static int dp_display_probe(struct platform_device *pdev)
1284 {
1285 	int rc = 0;
1286 	struct dp_display_private *dp;
1287 	const struct msm_dp_desc *desc;
1288 
1289 	if (!pdev || !pdev->dev.of_node) {
1290 		DRM_ERROR("pdev not found\n");
1291 		return -ENODEV;
1292 	}
1293 
1294 	dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1295 	if (!dp)
1296 		return -ENOMEM;
1297 
1298 	desc = dp_display_get_desc(pdev, &dp->id);
1299 	if (!desc)
1300 		return -EINVAL;
1301 
1302 	dp->pdev = pdev;
1303 	dp->name = "drm_dp";
1304 	dp->dp_display.connector_type = desc->connector_type;
1305 
1306 	rc = dp_init_sub_modules(dp);
1307 	if (rc) {
1308 		DRM_ERROR("init sub module failed\n");
1309 		return -EPROBE_DEFER;
1310 	}
1311 
1312 	mutex_init(&dp->event_mutex);
1313 
1314 	/* Store DP audio handle inside DP display */
1315 	dp->dp_display.dp_audio = dp->audio;
1316 
1317 	init_completion(&dp->audio_comp);
1318 
1319 	platform_set_drvdata(pdev, &dp->dp_display);
1320 
1321 	rc = component_add(&pdev->dev, &dp_display_comp_ops);
1322 	if (rc) {
1323 		DRM_ERROR("component add failed, rc=%d\n", rc);
1324 		dp_display_deinit_sub_modules(dp);
1325 	}
1326 
1327 	return rc;
1328 }
1329 
1330 static int dp_display_remove(struct platform_device *pdev)
1331 {
1332 	struct dp_display_private *dp = dev_get_dp_display_private(&pdev->dev);
1333 
1334 	dp_display_deinit_sub_modules(dp);
1335 
1336 	component_del(&pdev->dev, &dp_display_comp_ops);
1337 	platform_set_drvdata(pdev, NULL);
1338 
1339 	return 0;
1340 }
1341 
1342 static int dp_pm_resume(struct device *dev)
1343 {
1344 	struct platform_device *pdev = to_platform_device(dev);
1345 	struct msm_dp *dp_display = platform_get_drvdata(pdev);
1346 	struct dp_display_private *dp;
1347 	int sink_count = 0;
1348 
1349 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1350 
1351 	mutex_lock(&dp->event_mutex);
1352 
1353 	DRM_DEBUG_DP("Before, type=%d core_inited=%d phy_inited=%d power_on=%d\n",
1354 		dp->dp_display.connector_type, dp->core_initialized,
1355 		dp->phy_initialized, dp_display->power_on);
1356 
1357 	/* start from disconnected state */
1358 	dp->hpd_state = ST_DISCONNECTED;
1359 
1360 	/* turn on dp ctrl/phy */
1361 	dp_display_host_init(dp);
1362 
1363 	dp_catalog_ctrl_hpd_config(dp->catalog);
1364 
1365 
1366 	if (dp_catalog_link_is_connected(dp->catalog)) {
1367 		/*
1368 		 * set sink to normal operation mode -- D0
1369 		 * before dpcd read
1370 		 */
1371 		dp_display_host_phy_init(dp);
1372 		dp_link_psm_config(dp->link, &dp->panel->link_info, false);
1373 		sink_count = drm_dp_read_sink_count(dp->aux);
1374 		if (sink_count < 0)
1375 			sink_count = 0;
1376 
1377 		dp_display_host_phy_exit(dp);
1378 	}
1379 
1380 	dp->link->sink_count = sink_count;
1381 	/*
1382 	 * can not declared display is connected unless
1383 	 * HDMI cable is plugged in and sink_count of
1384 	 * dongle become 1
1385 	 * also only signal audio when disconnected
1386 	 */
1387 	if (dp->link->sink_count) {
1388 		dp->dp_display.is_connected = true;
1389 	} else {
1390 		dp->dp_display.is_connected = false;
1391 		dp_display_handle_plugged_change(dp_display, false);
1392 	}
1393 
1394 	DRM_DEBUG_DP("After, type=%d sink_count=%d is_connected=%d \
1395 			core_inited=%d phy_inited=%d power_on=%d\n",
1396 		dp->dp_display.connector_type, dp->link->sink_count,
1397 		dp->dp_display.is_connected, dp->core_initialized,
1398 		dp->phy_initialized, dp_display->power_on);
1399 
1400 	mutex_unlock(&dp->event_mutex);
1401 
1402 	return 0;
1403 }
1404 
1405 static int dp_pm_suspend(struct device *dev)
1406 {
1407 	struct platform_device *pdev = to_platform_device(dev);
1408 	struct msm_dp *dp_display = platform_get_drvdata(pdev);
1409 	struct dp_display_private *dp;
1410 
1411 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1412 
1413 	mutex_lock(&dp->event_mutex);
1414 
1415 	DRM_DEBUG_DP("Before, type=%d core_inited=%d phy_inited=%d power_on=%d\n",
1416 		dp->dp_display.connector_type, dp->core_initialized,
1417 		dp->phy_initialized, dp_display->power_on);
1418 
1419 	/* mainlink enabled */
1420 	if (dp_power_clk_status(dp->power, DP_CTRL_PM))
1421 		dp_ctrl_off_link_stream(dp->ctrl);
1422 
1423 	dp_display_host_phy_exit(dp);
1424 
1425 	/* host_init will be called at pm_resume */
1426 	dp_display_host_deinit(dp);
1427 
1428 	dp->hpd_state = ST_SUSPENDED;
1429 
1430 	DRM_DEBUG_DP("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 int dp_pm_prepare(struct device *dev)
1440 {
1441 	return 0;
1442 }
1443 
1444 static void dp_pm_complete(struct device *dev)
1445 {
1446 
1447 }
1448 
1449 static const struct dev_pm_ops dp_pm_ops = {
1450 	.suspend = dp_pm_suspend,
1451 	.resume =  dp_pm_resume,
1452 	.prepare = dp_pm_prepare,
1453 	.complete = dp_pm_complete,
1454 };
1455 
1456 static struct platform_driver dp_display_driver = {
1457 	.probe  = dp_display_probe,
1458 	.remove = dp_display_remove,
1459 	.driver = {
1460 		.name = "msm-dp-display",
1461 		.of_match_table = dp_dt_match,
1462 		.suppress_bind_attrs = true,
1463 		.pm = &dp_pm_ops,
1464 	},
1465 };
1466 
1467 int __init msm_dp_register(void)
1468 {
1469 	int ret;
1470 
1471 	ret = platform_driver_register(&dp_display_driver);
1472 	if (ret)
1473 		DRM_ERROR("Dp display driver register failed");
1474 
1475 	return ret;
1476 }
1477 
1478 void __exit msm_dp_unregister(void)
1479 {
1480 	platform_driver_unregister(&dp_display_driver);
1481 }
1482 
1483 void msm_dp_irq_postinstall(struct msm_dp *dp_display)
1484 {
1485 	struct dp_display_private *dp;
1486 
1487 	if (!dp_display)
1488 		return;
1489 
1490 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1491 
1492 	dp_hpd_event_setup(dp);
1493 
1494 	dp_add_event(dp, EV_HPD_INIT_SETUP, 0, 100);
1495 }
1496 
1497 void msm_dp_debugfs_init(struct msm_dp *dp_display, struct drm_minor *minor)
1498 {
1499 	struct dp_display_private *dp;
1500 	struct device *dev;
1501 	int rc;
1502 
1503 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1504 	dev = &dp->pdev->dev;
1505 
1506 	dp->debug = dp_debug_get(dev, dp->panel, dp->usbpd,
1507 					dp->link, dp->dp_display.connector,
1508 					minor);
1509 	if (IS_ERR(dp->debug)) {
1510 		rc = PTR_ERR(dp->debug);
1511 		DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1512 		dp->debug = NULL;
1513 	}
1514 }
1515 
1516 int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1517 			struct drm_encoder *encoder)
1518 {
1519 	struct msm_drm_private *priv;
1520 	struct dp_display_private *dp_priv;
1521 	int ret;
1522 
1523 	if (WARN_ON(!encoder) || WARN_ON(!dp_display) || WARN_ON(!dev))
1524 		return -EINVAL;
1525 
1526 	priv = dev->dev_private;
1527 	dp_display->drm_dev = dev;
1528 
1529 	dp_priv = container_of(dp_display, struct dp_display_private, dp_display);
1530 
1531 	ret = dp_display_request_irq(dp_display);
1532 	if (ret) {
1533 		DRM_ERROR("request_irq failed, ret=%d\n", ret);
1534 		return ret;
1535 	}
1536 
1537 	dp_display->encoder = encoder;
1538 
1539 	dp_display->connector = dp_drm_connector_init(dp_display);
1540 	if (IS_ERR(dp_display->connector)) {
1541 		ret = PTR_ERR(dp_display->connector);
1542 		DRM_DEV_ERROR(dev->dev,
1543 			"failed to create dp connector: %d\n", ret);
1544 		dp_display->connector = NULL;
1545 		return ret;
1546 	}
1547 
1548 	dp_priv->panel->connector = dp_display->connector;
1549 
1550 	priv->connectors[priv->num_connectors++] = dp_display->connector;
1551 
1552 	dp_display->bridge = msm_dp_bridge_init(dp_display, dev, encoder);
1553 	if (IS_ERR(dp_display->bridge)) {
1554 		ret = PTR_ERR(dp_display->bridge);
1555 		DRM_DEV_ERROR(dev->dev,
1556 			"failed to create dp bridge: %d\n", ret);
1557 		dp_display->bridge = NULL;
1558 		return ret;
1559 	}
1560 
1561 	priv->bridges[priv->num_bridges++] = dp_display->bridge;
1562 
1563 	return 0;
1564 }
1565 
1566 int msm_dp_display_enable(struct msm_dp *dp, struct drm_encoder *encoder)
1567 {
1568 	int rc = 0;
1569 	struct dp_display_private *dp_display;
1570 	u32 state;
1571 
1572 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1573 	if (!dp_display->dp_mode.drm_mode.clock) {
1574 		DRM_ERROR("invalid params\n");
1575 		return -EINVAL;
1576 	}
1577 
1578 	mutex_lock(&dp_display->event_mutex);
1579 
1580 	/* stop sentinel checking */
1581 	dp_del_event(dp_display, EV_CONNECT_PENDING_TIMEOUT);
1582 
1583 	rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1584 	if (rc) {
1585 		DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1586 		mutex_unlock(&dp_display->event_mutex);
1587 		return rc;
1588 	}
1589 
1590 	rc = dp_display_prepare(dp);
1591 	if (rc) {
1592 		DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1593 		mutex_unlock(&dp_display->event_mutex);
1594 		return rc;
1595 	}
1596 
1597 	state =  dp_display->hpd_state;
1598 
1599 	if (state == ST_DISPLAY_OFF)
1600 		dp_display_host_phy_init(dp_display);
1601 
1602 	dp_display_enable(dp_display, 0);
1603 
1604 	rc = dp_display_post_enable(dp);
1605 	if (rc) {
1606 		DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1607 		dp_display_disable(dp_display, 0);
1608 		dp_display_unprepare(dp);
1609 	}
1610 
1611 	/* manual kick off plug event to train link */
1612 	if (state == ST_DISPLAY_OFF)
1613 		dp_add_event(dp_display, EV_IRQ_HPD_INT, 0, 0);
1614 
1615 	/* completed connection */
1616 	dp_display->hpd_state = ST_CONNECTED;
1617 
1618 	mutex_unlock(&dp_display->event_mutex);
1619 
1620 	return rc;
1621 }
1622 
1623 int msm_dp_display_pre_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1624 {
1625 	struct dp_display_private *dp_display;
1626 
1627 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1628 
1629 	dp_ctrl_push_idle(dp_display->ctrl);
1630 
1631 	return 0;
1632 }
1633 
1634 int msm_dp_display_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1635 {
1636 	int rc = 0;
1637 	u32 state;
1638 	struct dp_display_private *dp_display;
1639 
1640 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1641 
1642 	mutex_lock(&dp_display->event_mutex);
1643 
1644 	/* stop sentinel checking */
1645 	dp_del_event(dp_display, EV_DISCONNECT_PENDING_TIMEOUT);
1646 
1647 	dp_display_disable(dp_display, 0);
1648 
1649 	rc = dp_display_unprepare(dp);
1650 	if (rc)
1651 		DRM_ERROR("DP display unprepare failed, rc=%d\n", rc);
1652 
1653 	state =  dp_display->hpd_state;
1654 	if (state == ST_DISCONNECT_PENDING) {
1655 		/* completed disconnection */
1656 		dp_display->hpd_state = ST_DISCONNECTED;
1657 	} else {
1658 		dp_display->hpd_state = ST_DISPLAY_OFF;
1659 	}
1660 
1661 	mutex_unlock(&dp_display->event_mutex);
1662 	return rc;
1663 }
1664 
1665 void msm_dp_display_mode_set(struct msm_dp *dp, struct drm_encoder *encoder,
1666 				const struct drm_display_mode *mode,
1667 				const struct drm_display_mode *adjusted_mode)
1668 {
1669 	struct dp_display_private *dp_display;
1670 
1671 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1672 
1673 	memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1674 
1675 	if (dp_display_check_video_test(dp))
1676 		dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1677 	else /* Default num_components per pixel = 3 */
1678 		dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1679 
1680 	if (!dp_display->dp_mode.bpp)
1681 		dp_display->dp_mode.bpp = 24; /* Default bpp */
1682 
1683 	drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1684 
1685 	dp_display->dp_mode.v_active_low =
1686 		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1687 
1688 	dp_display->dp_mode.h_active_low =
1689 		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);
1690 }
1691