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