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