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