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