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