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