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