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 
14 #include "msm_drv.h"
15 #include "msm_kms.h"
16 #include "dp_hpd.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 struct msm_dp *g_dp_display;
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_CONNECT_PENDING,
46 	ST_CONNECTED,
47 	ST_DISCONNECT_PENDING,
48 	ST_SUSPEND_PENDING,
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_REPLUG_INT,
59 	EV_HPD_UNPLUG_INT,
60 	EV_USER_NOTIFICATION,
61 	EV_CONNECT_PENDING_TIMEOUT,
62 	EV_DISCONNECT_PENDING_TIMEOUT,
63 };
64 
65 #define EVENT_TIMEOUT	(HZ/10)	/* 100ms */
66 #define DP_EVENT_Q_MAX	8
67 
68 #define DP_TIMEOUT_5_SECOND	(5000/EVENT_TIMEOUT)
69 #define DP_TIMEOUT_NONE		0
70 
71 #define WAIT_FOR_RESUME_TIMEOUT_JIFFIES (HZ / 2)
72 
73 struct dp_event {
74 	u32 event_id;
75 	u32 data;
76 	u32 delay;
77 };
78 
79 struct dp_display_private {
80 	char *name;
81 	int irq;
82 
83 	/* state variables */
84 	bool core_initialized;
85 	bool hpd_irq_on;
86 	bool audio_supported;
87 
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 	atomic_t hpd_state;
112 	u32 event_pndx;
113 	u32 event_gndx;
114 	struct dp_event event_list[DP_EVENT_Q_MAX];
115 	spinlock_t event_lock;
116 
117 	struct completion resume_comp;
118 
119 	struct dp_audio *audio;
120 };
121 
122 static const struct of_device_id dp_dt_match[] = {
123 	{.compatible = "qcom,sc7180-dp"},
124 	{}
125 };
126 
dp_add_event(struct dp_display_private * dp_priv,u32 event,u32 data,u32 delay)127 static int dp_add_event(struct dp_display_private *dp_priv, u32 event,
128 						u32 data, u32 delay)
129 {
130 	unsigned long flag;
131 	struct dp_event *todo;
132 	int pndx;
133 
134 	spin_lock_irqsave(&dp_priv->event_lock, flag);
135 	pndx = dp_priv->event_pndx + 1;
136 	pndx %= DP_EVENT_Q_MAX;
137 	if (pndx == dp_priv->event_gndx) {
138 		pr_err("event_q is full: pndx=%d gndx=%d\n",
139 			dp_priv->event_pndx, dp_priv->event_gndx);
140 		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
141 		return -EPERM;
142 	}
143 	todo = &dp_priv->event_list[dp_priv->event_pndx++];
144 	dp_priv->event_pndx %= DP_EVENT_Q_MAX;
145 	todo->event_id = event;
146 	todo->data = data;
147 	todo->delay = delay;
148 	wake_up(&dp_priv->event_q);
149 	spin_unlock_irqrestore(&dp_priv->event_lock, flag);
150 
151 	return 0;
152 }
153 
dp_del_event(struct dp_display_private * dp_priv,u32 event)154 static int dp_del_event(struct dp_display_private *dp_priv, u32 event)
155 {
156 	unsigned long flag;
157 	struct dp_event *todo;
158 	u32	gndx;
159 
160 	spin_lock_irqsave(&dp_priv->event_lock, flag);
161 	if (dp_priv->event_pndx == dp_priv->event_gndx) {
162 		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
163 		return -ENOENT;
164 	}
165 
166 	gndx = dp_priv->event_gndx;
167 	while (dp_priv->event_pndx != gndx) {
168 		todo = &dp_priv->event_list[gndx];
169 		if (todo->event_id == event) {
170 			todo->event_id = EV_NO_EVENT;	/* deleted */
171 			todo->delay = 0;
172 		}
173 		gndx++;
174 		gndx %= DP_EVENT_Q_MAX;
175 	}
176 	spin_unlock_irqrestore(&dp_priv->event_lock, flag);
177 
178 	return 0;
179 }
180 
dp_display_signal_audio_complete(struct msm_dp * dp_display)181 void dp_display_signal_audio_complete(struct msm_dp *dp_display)
182 {
183 	struct dp_display_private *dp;
184 
185 	dp = container_of(dp_display, struct dp_display_private, dp_display);
186 
187 	complete_all(&dp->audio_comp);
188 }
189 
dp_display_bind(struct device * dev,struct device * master,void * data)190 static int dp_display_bind(struct device *dev, struct device *master,
191 			   void *data)
192 {
193 	int rc = 0;
194 	struct dp_display_private *dp;
195 	struct drm_device *drm;
196 	struct msm_drm_private *priv;
197 
198 	drm = dev_get_drvdata(master);
199 
200 	dp = container_of(g_dp_display,
201 			struct dp_display_private, dp_display);
202 	if (!dp) {
203 		DRM_ERROR("DP driver bind failed. Invalid driver data\n");
204 		return -EINVAL;
205 	}
206 
207 	dp->dp_display.drm_dev = drm;
208 	priv = drm->dev_private;
209 	priv->dp = &(dp->dp_display);
210 
211 	rc = dp->parser->parse(dp->parser);
212 	if (rc) {
213 		DRM_ERROR("device tree parsing failed\n");
214 		goto end;
215 	}
216 
217 	rc = dp_aux_register(dp->aux);
218 	if (rc) {
219 		DRM_ERROR("DRM DP AUX register failed\n");
220 		goto end;
221 	}
222 
223 	rc = dp_power_client_init(dp->power);
224 	if (rc) {
225 		DRM_ERROR("Power client create failed\n");
226 		goto end;
227 	}
228 
229 	rc = dp_register_audio_driver(dev, dp->audio);
230 	if (rc)
231 		DRM_ERROR("Audio registration Dp failed\n");
232 
233 end:
234 	return rc;
235 }
236 
dp_display_unbind(struct device * dev,struct device * master,void * data)237 static void dp_display_unbind(struct device *dev, struct device *master,
238 			      void *data)
239 {
240 	struct dp_display_private *dp;
241 	struct drm_device *drm = dev_get_drvdata(master);
242 	struct msm_drm_private *priv = drm->dev_private;
243 
244 	dp = container_of(g_dp_display,
245 			struct dp_display_private, dp_display);
246 	if (!dp) {
247 		DRM_ERROR("Invalid DP driver data\n");
248 		return;
249 	}
250 
251 	dp_power_client_deinit(dp->power);
252 	dp_aux_unregister(dp->aux);
253 	priv->dp = NULL;
254 }
255 
256 static const struct component_ops dp_display_comp_ops = {
257 	.bind = dp_display_bind,
258 	.unbind = dp_display_unbind,
259 };
260 
dp_display_is_ds_bridge(struct dp_panel * panel)261 static bool dp_display_is_ds_bridge(struct dp_panel *panel)
262 {
263 	return (panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
264 		DP_DWN_STRM_PORT_PRESENT);
265 }
266 
dp_display_is_sink_count_zero(struct dp_display_private * dp)267 static bool dp_display_is_sink_count_zero(struct dp_display_private *dp)
268 {
269 	return dp_display_is_ds_bridge(dp->panel) &&
270 		(dp->link->sink_count == 0);
271 }
272 
dp_display_send_hpd_event(struct msm_dp * dp_display)273 static void dp_display_send_hpd_event(struct msm_dp *dp_display)
274 {
275 	struct dp_display_private *dp;
276 	struct drm_connector *connector;
277 
278 	dp = container_of(dp_display, struct dp_display_private, dp_display);
279 
280 	connector = dp->dp_display.connector;
281 	drm_helper_hpd_irq_event(connector->dev);
282 }
283 
dp_display_send_hpd_notification(struct dp_display_private * dp,bool hpd)284 static int dp_display_send_hpd_notification(struct dp_display_private *dp,
285 					    bool hpd)
286 {
287 	static bool encoder_mode_set;
288 	struct msm_drm_private *priv = dp->dp_display.drm_dev->dev_private;
289 	struct msm_kms *kms = priv->kms;
290 
291 	if ((hpd && dp->dp_display.is_connected) ||
292 			(!hpd && !dp->dp_display.is_connected)) {
293 		DRM_DEBUG_DP("HPD already %s\n", (hpd ? "on" : "off"));
294 		return 0;
295 	}
296 
297 	/* reset video pattern flag on disconnect */
298 	if (!hpd)
299 		dp->panel->video_test = false;
300 
301 	dp->dp_display.is_connected = hpd;
302 
303 	if (dp->dp_display.is_connected && dp->dp_display.encoder
304 				&& !encoder_mode_set
305 				&& kms->funcs->set_encoder_mode) {
306 		kms->funcs->set_encoder_mode(kms,
307 				dp->dp_display.encoder, false);
308 		DRM_DEBUG_DP("set_encoder_mode() Completed\n");
309 		encoder_mode_set = true;
310 	}
311 
312 	dp_display_send_hpd_event(&dp->dp_display);
313 
314 	return 0;
315 }
316 
dp_display_process_hpd_high(struct dp_display_private * dp)317 static int dp_display_process_hpd_high(struct dp_display_private *dp)
318 {
319 	int rc = 0;
320 	struct edid *edid;
321 
322 	dp->panel->max_dp_lanes = dp->parser->max_dp_lanes;
323 
324 	rc = dp_panel_read_sink_caps(dp->panel, dp->dp_display.connector);
325 	if (rc)
326 		goto end;
327 
328 	dp_link_process_request(dp->link);
329 
330 	edid = dp->panel->edid;
331 
332 	dp->audio_supported = drm_detect_monitor_audio(edid);
333 	dp_panel_handle_sink_request(dp->panel);
334 
335 	dp->dp_display.max_pclk_khz = DP_MAX_PIXEL_CLK_KHZ;
336 	dp->dp_display.max_dp_lanes = dp->parser->max_dp_lanes;
337 
338 	rc = dp_ctrl_on_link(dp->ctrl);
339 	if (rc) {
340 		DRM_ERROR("failed to complete DP link training\n");
341 		goto end;
342 	}
343 
344 	dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
345 
346 
347 end:
348 	return rc;
349 }
350 
dp_display_host_init(struct dp_display_private * dp)351 static void dp_display_host_init(struct dp_display_private *dp)
352 {
353 	bool flip = false;
354 
355 	if (dp->core_initialized) {
356 		DRM_DEBUG_DP("DP core already initialized\n");
357 		return;
358 	}
359 
360 	if (dp->usbpd->orientation == ORIENTATION_CC2)
361 		flip = true;
362 
363 	dp_power_init(dp->power, flip);
364 	dp_ctrl_host_init(dp->ctrl, flip);
365 	dp_aux_init(dp->aux);
366 	dp->core_initialized = true;
367 }
368 
dp_display_usbpd_configure_cb(struct device * dev)369 static int dp_display_usbpd_configure_cb(struct device *dev)
370 {
371 	int rc = 0;
372 	struct dp_display_private *dp;
373 
374 	if (!dev) {
375 		DRM_ERROR("invalid dev\n");
376 		rc = -EINVAL;
377 		goto end;
378 	}
379 
380 	dp = container_of(g_dp_display,
381 			struct dp_display_private, dp_display);
382 	if (!dp) {
383 		DRM_ERROR("no driver data found\n");
384 		rc = -ENODEV;
385 		goto end;
386 	}
387 
388 	dp_display_host_init(dp);
389 
390 	/*
391 	 * set sink to normal operation mode -- D0
392 	 * before dpcd read
393 	 */
394 	dp_link_psm_config(dp->link, &dp->panel->link_info, false);
395 	rc = dp_display_process_hpd_high(dp);
396 end:
397 	return rc;
398 }
399 
dp_display_usbpd_disconnect_cb(struct device * dev)400 static int dp_display_usbpd_disconnect_cb(struct device *dev)
401 {
402 	int rc = 0;
403 	struct dp_display_private *dp;
404 
405 	if (!dev) {
406 		DRM_ERROR("invalid dev\n");
407 		rc = -EINVAL;
408 		return rc;
409 	}
410 
411 	dp = container_of(g_dp_display,
412 			struct dp_display_private, dp_display);
413 	if (!dp) {
414 		DRM_ERROR("no driver data found\n");
415 		rc = -ENODEV;
416 		return rc;
417 	}
418 
419 	dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
420 
421 	return rc;
422 }
423 
dp_display_handle_video_request(struct dp_display_private * dp)424 static void dp_display_handle_video_request(struct dp_display_private *dp)
425 {
426 	if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
427 		dp->panel->video_test = true;
428 		dp_link_send_test_response(dp->link);
429 	}
430 }
431 
dp_display_handle_irq_hpd(struct dp_display_private * dp)432 static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
433 {
434 	u32 sink_request;
435 
436 	sink_request = dp->link->sink_request;
437 
438 	if (sink_request & DS_PORT_STATUS_CHANGED) {
439 		dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
440 		if (dp_display_is_sink_count_zero(dp)) {
441 			DRM_DEBUG_DP("sink count is zero, nothing to do\n");
442 			return 0;
443 		}
444 
445 		return dp_display_process_hpd_high(dp);
446 	}
447 
448 	dp_ctrl_handle_sink_request(dp->ctrl);
449 
450 	if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN)
451 		dp_display_handle_video_request(dp);
452 
453 	return 0;
454 }
455 
dp_display_usbpd_attention_cb(struct device * dev)456 static int dp_display_usbpd_attention_cb(struct device *dev)
457 {
458 	int rc = 0;
459 	struct dp_display_private *dp;
460 
461 	if (!dev) {
462 		DRM_ERROR("invalid dev\n");
463 		return -EINVAL;
464 	}
465 
466 	dp = container_of(g_dp_display,
467 			struct dp_display_private, dp_display);
468 	if (!dp) {
469 		DRM_ERROR("no driver data found\n");
470 		return -ENODEV;
471 	}
472 
473 	/* check for any test request issued by sink */
474 	rc = dp_link_process_request(dp->link);
475 	if (!rc)
476 		dp_display_handle_irq_hpd(dp);
477 
478 	return rc;
479 }
480 
dp_hpd_plug_handle(struct dp_display_private * dp,u32 data)481 static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
482 {
483 	struct dp_usbpd *hpd = dp->usbpd;
484 	u32 state;
485 	u32 tout = DP_TIMEOUT_5_SECOND;
486 	int ret;
487 
488 	if (!hpd)
489 		return 0;
490 
491 	mutex_lock(&dp->event_mutex);
492 
493 	state =  atomic_read(&dp->hpd_state);
494 	if (state == ST_SUSPEND_PENDING) {
495 		mutex_unlock(&dp->event_mutex);
496 		return 0;
497 	}
498 
499 	if (state == ST_CONNECT_PENDING || state == ST_CONNECTED) {
500 		mutex_unlock(&dp->event_mutex);
501 		return 0;
502 	}
503 
504 	if (state == ST_DISCONNECT_PENDING) {
505 		/* wait until ST_DISCONNECTED */
506 		dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
507 		mutex_unlock(&dp->event_mutex);
508 		return 0;
509 	}
510 
511 	if (state == ST_SUSPENDED)
512 		tout = DP_TIMEOUT_NONE;
513 
514 	atomic_set(&dp->hpd_state, ST_CONNECT_PENDING);
515 
516 	hpd->hpd_high = 1;
517 
518 	ret = dp_display_usbpd_configure_cb(&dp->pdev->dev);
519 	if (ret) {	/* failed */
520 		hpd->hpd_high = 0;
521 		atomic_set(&dp->hpd_state, ST_DISCONNECTED);
522 	}
523 
524 	/* start sanity checking */
525 	dp_add_event(dp, EV_CONNECT_PENDING_TIMEOUT, 0, tout);
526 
527 	mutex_unlock(&dp->event_mutex);
528 
529 	/* uevent will complete connection part */
530 	return 0;
531 };
532 
533 static int dp_display_enable(struct dp_display_private *dp, u32 data);
534 static int dp_display_disable(struct dp_display_private *dp, u32 data);
535 
dp_connect_pending_timeout(struct dp_display_private * dp,u32 data)536 static int dp_connect_pending_timeout(struct dp_display_private *dp, u32 data)
537 {
538 	u32 state;
539 
540 	mutex_lock(&dp->event_mutex);
541 
542 	state =  atomic_read(&dp->hpd_state);
543 	if (state == ST_CONNECT_PENDING) {
544 		dp_display_enable(dp, 0);
545 		atomic_set(&dp->hpd_state, ST_CONNECTED);
546 	}
547 
548 	mutex_unlock(&dp->event_mutex);
549 
550 	return 0;
551 }
552 
dp_display_handle_plugged_change(struct msm_dp * dp_display,bool plugged)553 static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
554 		bool plugged)
555 {
556 	if (dp_display->plugged_cb && dp_display->codec_dev)
557 		dp_display->plugged_cb(dp_display->codec_dev, plugged);
558 }
559 
dp_hpd_unplug_handle(struct dp_display_private * dp,u32 data)560 static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
561 {
562 	struct dp_usbpd *hpd = dp->usbpd;
563 	u32 state;
564 
565 	if (!hpd)
566 		return 0;
567 
568 	mutex_lock(&dp->event_mutex);
569 
570 	state = atomic_read(&dp->hpd_state);
571 	if (state == ST_SUSPEND_PENDING) {
572 		mutex_unlock(&dp->event_mutex);
573 		return 0;
574 	}
575 
576 	if (state == ST_DISCONNECT_PENDING || state == ST_DISCONNECTED) {
577 		mutex_unlock(&dp->event_mutex);
578 		return 0;
579 	}
580 
581 	if (state == ST_CONNECT_PENDING) {
582 		/* wait until CONNECTED */
583 		dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 1); /* delay = 1 */
584 		mutex_unlock(&dp->event_mutex);
585 		return 0;
586 	}
587 
588 	atomic_set(&dp->hpd_state, ST_DISCONNECT_PENDING);
589 
590 	/* disable HPD plug interrupt until disconnect is done */
591 	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK
592 				| DP_DP_IRQ_HPD_INT_MASK, false);
593 
594 	hpd->hpd_high = 0;
595 
596 	/*
597 	 * We don't need separate work for disconnect as
598 	 * connect/attention interrupts are disabled
599 	 */
600 	dp_display_usbpd_disconnect_cb(&dp->pdev->dev);
601 
602 	/* start sanity checking */
603 	dp_add_event(dp, EV_DISCONNECT_PENDING_TIMEOUT, 0, DP_TIMEOUT_5_SECOND);
604 
605 	/* signal the disconnect event early to ensure proper teardown */
606 	dp_display_handle_plugged_change(g_dp_display, false);
607 	reinit_completion(&dp->audio_comp);
608 
609 	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK |
610 					DP_DP_IRQ_HPD_INT_MASK, true);
611 
612 	/* uevent will complete disconnection part */
613 	mutex_unlock(&dp->event_mutex);
614 	return 0;
615 }
616 
dp_disconnect_pending_timeout(struct dp_display_private * dp,u32 data)617 static int dp_disconnect_pending_timeout(struct dp_display_private *dp, u32 data)
618 {
619 	u32 state;
620 
621 	mutex_lock(&dp->event_mutex);
622 
623 	state =  atomic_read(&dp->hpd_state);
624 	if (state == ST_DISCONNECT_PENDING) {
625 		dp_display_disable(dp, 0);
626 		atomic_set(&dp->hpd_state, ST_DISCONNECTED);
627 	}
628 
629 	mutex_unlock(&dp->event_mutex);
630 
631 	return 0;
632 }
633 
dp_irq_hpd_handle(struct dp_display_private * dp,u32 data)634 static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
635 {
636 	u32 state;
637 
638 	mutex_lock(&dp->event_mutex);
639 
640 	/* irq_hpd can happen at either connected or disconnected state */
641 	state =  atomic_read(&dp->hpd_state);
642 	if (state == ST_SUSPEND_PENDING) {
643 		mutex_unlock(&dp->event_mutex);
644 		return 0;
645 	}
646 
647 	dp_display_usbpd_attention_cb(&dp->pdev->dev);
648 
649 	mutex_unlock(&dp->event_mutex);
650 
651 	return 0;
652 }
653 
dp_display_deinit_sub_modules(struct dp_display_private * dp)654 static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
655 {
656 	dp_debug_put(dp->debug);
657 	dp_ctrl_put(dp->ctrl);
658 	dp_panel_put(dp->panel);
659 	dp_aux_put(dp->aux);
660 	dp_audio_put(dp->audio);
661 }
662 
dp_init_sub_modules(struct dp_display_private * dp)663 static int dp_init_sub_modules(struct dp_display_private *dp)
664 {
665 	int rc = 0;
666 	struct device *dev = &dp->pdev->dev;
667 	struct dp_usbpd_cb *cb = &dp->usbpd_cb;
668 	struct dp_panel_in panel_in = {
669 		.dev = dev,
670 	};
671 
672 	/* Callback APIs used for cable status change event */
673 	cb->configure  = dp_display_usbpd_configure_cb;
674 	cb->disconnect = dp_display_usbpd_disconnect_cb;
675 	cb->attention  = dp_display_usbpd_attention_cb;
676 
677 	dp->usbpd = dp_hpd_get(dev, cb);
678 	if (IS_ERR(dp->usbpd)) {
679 		rc = PTR_ERR(dp->usbpd);
680 		DRM_ERROR("failed to initialize hpd, rc = %d\n", rc);
681 		dp->usbpd = NULL;
682 		goto error;
683 	}
684 
685 	dp->parser = dp_parser_get(dp->pdev);
686 	if (IS_ERR(dp->parser)) {
687 		rc = PTR_ERR(dp->parser);
688 		DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
689 		dp->parser = NULL;
690 		goto error;
691 	}
692 
693 	dp->catalog = dp_catalog_get(dev, &dp->parser->io);
694 	if (IS_ERR(dp->catalog)) {
695 		rc = PTR_ERR(dp->catalog);
696 		DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
697 		dp->catalog = NULL;
698 		goto error;
699 	}
700 
701 	dp->power = dp_power_get(dp->parser);
702 	if (IS_ERR(dp->power)) {
703 		rc = PTR_ERR(dp->power);
704 		DRM_ERROR("failed to initialize power, rc = %d\n", rc);
705 		dp->power = NULL;
706 		goto error;
707 	}
708 
709 	dp->aux = dp_aux_get(dev, dp->catalog);
710 	if (IS_ERR(dp->aux)) {
711 		rc = PTR_ERR(dp->aux);
712 		DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
713 		dp->aux = NULL;
714 		goto error;
715 	}
716 
717 	dp->link = dp_link_get(dev, dp->aux);
718 	if (IS_ERR(dp->link)) {
719 		rc = PTR_ERR(dp->link);
720 		DRM_ERROR("failed to initialize link, rc = %d\n", rc);
721 		dp->link = NULL;
722 		goto error_link;
723 	}
724 
725 	panel_in.aux = dp->aux;
726 	panel_in.catalog = dp->catalog;
727 	panel_in.link = dp->link;
728 
729 	dp->panel = dp_panel_get(&panel_in);
730 	if (IS_ERR(dp->panel)) {
731 		rc = PTR_ERR(dp->panel);
732 		DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
733 		dp->panel = NULL;
734 		goto error_link;
735 	}
736 
737 	dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
738 			       dp->power, dp->catalog, dp->parser);
739 	if (IS_ERR(dp->ctrl)) {
740 		rc = PTR_ERR(dp->ctrl);
741 		DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
742 		dp->ctrl = NULL;
743 		goto error_ctrl;
744 	}
745 
746 	dp->audio = dp_audio_get(dp->pdev, dp->panel, dp->catalog);
747 	if (IS_ERR(dp->audio)) {
748 		rc = PTR_ERR(dp->audio);
749 		pr_err("failed to initialize audio, rc = %d\n", rc);
750 		dp->audio = NULL;
751 		goto error_audio;
752 	}
753 
754 	return rc;
755 
756 error_audio:
757 	dp_ctrl_put(dp->ctrl);
758 error_ctrl:
759 	dp_panel_put(dp->panel);
760 error_link:
761 	dp_aux_put(dp->aux);
762 error:
763 	return rc;
764 }
765 
dp_display_set_mode(struct msm_dp * dp_display,struct dp_display_mode * mode)766 static int dp_display_set_mode(struct msm_dp *dp_display,
767 			       struct dp_display_mode *mode)
768 {
769 	struct dp_display_private *dp;
770 
771 	dp = container_of(dp_display, struct dp_display_private, dp_display);
772 
773 	dp->panel->dp_mode.drm_mode = mode->drm_mode;
774 	dp->panel->dp_mode.bpp = mode->bpp;
775 	dp->panel->dp_mode.capabilities = mode->capabilities;
776 	dp_panel_init_panel_info(dp->panel);
777 	return 0;
778 }
779 
dp_display_prepare(struct msm_dp * dp)780 static int dp_display_prepare(struct msm_dp *dp)
781 {
782 	return 0;
783 }
784 
dp_display_enable(struct dp_display_private * dp,u32 data)785 static int dp_display_enable(struct dp_display_private *dp, u32 data)
786 {
787 	int rc = 0;
788 	struct msm_dp *dp_display;
789 
790 	dp_display = g_dp_display;
791 
792 	if (dp_display->power_on) {
793 		DRM_DEBUG_DP("Link already setup, return\n");
794 		return 0;
795 	}
796 
797 	rc = dp_ctrl_on_stream(dp->ctrl);
798 	if (!rc)
799 		dp_display->power_on = true;
800 
801 	/* complete resume_comp regardless it is armed or not */
802 	complete(&dp->resume_comp);
803 	return rc;
804 }
805 
dp_display_post_enable(struct msm_dp * dp_display)806 static int dp_display_post_enable(struct msm_dp *dp_display)
807 {
808 	struct dp_display_private *dp;
809 	u32 rate;
810 
811 	dp = container_of(dp_display, struct dp_display_private, dp_display);
812 
813 	rate = dp->link->link_params.rate;
814 
815 	if (dp->audio_supported) {
816 		dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
817 		dp->audio->lane_count = dp->link->link_params.num_lanes;
818 	}
819 
820 	/* signal the connect event late to synchronize video and display */
821 	dp_display_handle_plugged_change(dp_display, true);
822 	return 0;
823 }
824 
dp_display_disable(struct dp_display_private * dp,u32 data)825 static int dp_display_disable(struct dp_display_private *dp, u32 data)
826 {
827 	struct msm_dp *dp_display;
828 
829 	dp_display = g_dp_display;
830 
831 	if (!dp_display->power_on)
832 		return -EINVAL;
833 
834 	/* wait only if audio was enabled */
835 	if (dp_display->audio_enabled) {
836 		if (!wait_for_completion_timeout(&dp->audio_comp,
837 				HZ * 5))
838 			DRM_ERROR("audio comp timeout\n");
839 	}
840 
841 	dp_display->audio_enabled = false;
842 
843 	dp_ctrl_off(dp->ctrl);
844 
845 	dp->core_initialized = false;
846 
847 	dp_display->power_on = false;
848 
849 	return 0;
850 }
851 
dp_display_unprepare(struct msm_dp * dp)852 static int dp_display_unprepare(struct msm_dp *dp)
853 {
854 	return 0;
855 }
856 
dp_display_set_plugged_cb(struct msm_dp * dp_display,hdmi_codec_plugged_cb fn,struct device * codec_dev)857 int dp_display_set_plugged_cb(struct msm_dp *dp_display,
858 		hdmi_codec_plugged_cb fn, struct device *codec_dev)
859 {
860 	bool plugged;
861 
862 	dp_display->plugged_cb = fn;
863 	dp_display->codec_dev = codec_dev;
864 	plugged = dp_display->is_connected;
865 	dp_display_handle_plugged_change(dp_display, plugged);
866 
867 	return 0;
868 }
869 
dp_display_validate_mode(struct msm_dp * dp,u32 mode_pclk_khz)870 int dp_display_validate_mode(struct msm_dp *dp, u32 mode_pclk_khz)
871 {
872 	const u32 num_components = 3, default_bpp = 24;
873 	struct dp_display_private *dp_display;
874 	struct dp_link_info *link_info;
875 	u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
876 
877 	if (!dp || !mode_pclk_khz || !dp->connector) {
878 		DRM_ERROR("invalid params\n");
879 		return -EINVAL;
880 	}
881 
882 	dp_display = container_of(dp, struct dp_display_private, dp_display);
883 	link_info = &dp_display->panel->link_info;
884 
885 	mode_bpp = dp->connector->display_info.bpc * num_components;
886 	if (!mode_bpp)
887 		mode_bpp = default_bpp;
888 
889 	mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
890 			mode_bpp, mode_pclk_khz);
891 
892 	mode_rate_khz = mode_pclk_khz * mode_bpp;
893 	supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
894 
895 	if (mode_rate_khz > supported_rate_khz)
896 		return MODE_BAD;
897 
898 	return MODE_OK;
899 }
900 
dp_display_get_modes(struct msm_dp * dp,struct dp_display_mode * dp_mode)901 int dp_display_get_modes(struct msm_dp *dp,
902 				struct dp_display_mode *dp_mode)
903 {
904 	struct dp_display_private *dp_display;
905 	int ret = 0;
906 
907 	if (!dp) {
908 		DRM_ERROR("invalid params\n");
909 		return 0;
910 	}
911 
912 	dp_display = container_of(dp, struct dp_display_private, dp_display);
913 
914 	ret = dp_panel_get_modes(dp_display->panel,
915 		dp->connector, dp_mode);
916 	if (dp_mode->drm_mode.clock)
917 		dp->max_pclk_khz = dp_mode->drm_mode.clock;
918 	return ret;
919 }
920 
dp_display_check_video_test(struct msm_dp * dp)921 bool dp_display_check_video_test(struct msm_dp *dp)
922 {
923 	struct dp_display_private *dp_display;
924 
925 	dp_display = container_of(dp, struct dp_display_private, dp_display);
926 
927 	return dp_display->panel->video_test;
928 }
929 
dp_display_get_test_bpp(struct msm_dp * dp)930 int dp_display_get_test_bpp(struct msm_dp *dp)
931 {
932 	struct dp_display_private *dp_display;
933 
934 	if (!dp) {
935 		DRM_ERROR("invalid params\n");
936 		return 0;
937 	}
938 
939 	dp_display = container_of(dp, struct dp_display_private, dp_display);
940 
941 	return dp_link_bit_depth_to_bpp(
942 		dp_display->link->test_video.test_bit_depth);
943 }
944 
dp_display_config_hpd(struct dp_display_private * dp)945 static void dp_display_config_hpd(struct dp_display_private *dp)
946 {
947 
948 	dp_display_host_init(dp);
949 	dp_catalog_ctrl_hpd_config(dp->catalog);
950 
951 	/* Enable interrupt first time
952 	 * we are leaving dp clocks on during disconnect
953 	 * and never disable interrupt
954 	 */
955 	enable_irq(dp->irq);
956 }
957 
hpd_event_thread(void * data)958 static int hpd_event_thread(void *data)
959 {
960 	struct dp_display_private *dp_priv;
961 	unsigned long flag;
962 	struct dp_event *todo;
963 	int timeout_mode = 0;
964 
965 	dp_priv = (struct dp_display_private *)data;
966 
967 	while (1) {
968 		if (timeout_mode) {
969 			wait_event_timeout(dp_priv->event_q,
970 				(dp_priv->event_pndx == dp_priv->event_gndx),
971 						EVENT_TIMEOUT);
972 		} else {
973 			wait_event_interruptible(dp_priv->event_q,
974 				(dp_priv->event_pndx != dp_priv->event_gndx));
975 		}
976 		spin_lock_irqsave(&dp_priv->event_lock, flag);
977 		todo = &dp_priv->event_list[dp_priv->event_gndx];
978 		if (todo->delay) {
979 			struct dp_event *todo_next;
980 
981 			dp_priv->event_gndx++;
982 			dp_priv->event_gndx %= DP_EVENT_Q_MAX;
983 
984 			/* re enter delay event into q */
985 			todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
986 			dp_priv->event_pndx %= DP_EVENT_Q_MAX;
987 			todo_next->event_id = todo->event_id;
988 			todo_next->data = todo->data;
989 			todo_next->delay = todo->delay - 1;
990 
991 			/* clean up older event */
992 			todo->event_id = EV_NO_EVENT;
993 			todo->delay = 0;
994 
995 			/* switch to timeout mode */
996 			timeout_mode = 1;
997 			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
998 			continue;
999 		}
1000 
1001 		/* timeout with no events in q */
1002 		if (dp_priv->event_pndx == dp_priv->event_gndx) {
1003 			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1004 			continue;
1005 		}
1006 
1007 		dp_priv->event_gndx++;
1008 		dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1009 		timeout_mode = 0;
1010 		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1011 
1012 		switch (todo->event_id) {
1013 		case EV_HPD_INIT_SETUP:
1014 			dp_display_config_hpd(dp_priv);
1015 			break;
1016 		case EV_HPD_PLUG_INT:
1017 			dp_hpd_plug_handle(dp_priv, todo->data);
1018 			break;
1019 		case EV_HPD_UNPLUG_INT:
1020 			dp_hpd_unplug_handle(dp_priv, todo->data);
1021 			break;
1022 		case EV_IRQ_HPD_INT:
1023 			dp_irq_hpd_handle(dp_priv, todo->data);
1024 			break;
1025 		case EV_HPD_REPLUG_INT:
1026 			/* do nothing */
1027 			break;
1028 		case EV_USER_NOTIFICATION:
1029 			dp_display_send_hpd_notification(dp_priv,
1030 						todo->data);
1031 			break;
1032 		case EV_CONNECT_PENDING_TIMEOUT:
1033 			dp_connect_pending_timeout(dp_priv,
1034 						todo->data);
1035 			break;
1036 		case EV_DISCONNECT_PENDING_TIMEOUT:
1037 			dp_disconnect_pending_timeout(dp_priv,
1038 						todo->data);
1039 			break;
1040 		default:
1041 			break;
1042 		}
1043 	}
1044 
1045 	return 0;
1046 }
1047 
dp_hpd_event_setup(struct dp_display_private * dp_priv)1048 static void dp_hpd_event_setup(struct dp_display_private *dp_priv)
1049 {
1050 	init_waitqueue_head(&dp_priv->event_q);
1051 	spin_lock_init(&dp_priv->event_lock);
1052 
1053 	kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1054 }
1055 
dp_display_irq_handler(int irq,void * dev_id)1056 static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1057 {
1058 	struct dp_display_private *dp = dev_id;
1059 	irqreturn_t ret = IRQ_HANDLED;
1060 	u32 hpd_isr_status;
1061 
1062 	if (!dp) {
1063 		DRM_ERROR("invalid data\n");
1064 		return IRQ_NONE;
1065 	}
1066 
1067 	hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1068 
1069 	if (hpd_isr_status & 0x0F) {
1070 		/* hpd related interrupts */
1071 		if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK ||
1072 			hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1073 			dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1074 		}
1075 
1076 		if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1077 			/* delete connect pending event first */
1078 			dp_del_event(dp, EV_CONNECT_PENDING_TIMEOUT);
1079 			dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1080 		}
1081 
1082 		if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK)
1083 			dp_add_event(dp, EV_HPD_REPLUG_INT, 0, 0);
1084 
1085 		if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1086 			dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1087 	}
1088 
1089 	/* DP controller isr */
1090 	dp_ctrl_isr(dp->ctrl);
1091 
1092 	/* DP aux isr */
1093 	dp_aux_isr(dp->aux);
1094 
1095 	return ret;
1096 }
1097 
dp_display_request_irq(struct msm_dp * dp_display)1098 int dp_display_request_irq(struct msm_dp *dp_display)
1099 {
1100 	int rc = 0;
1101 	struct dp_display_private *dp;
1102 
1103 	if (!dp_display) {
1104 		DRM_ERROR("invalid input\n");
1105 		return -EINVAL;
1106 	}
1107 
1108 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1109 
1110 	dp->irq = irq_of_parse_and_map(dp->pdev->dev.of_node, 0);
1111 	if (dp->irq < 0) {
1112 		rc = dp->irq;
1113 		DRM_ERROR("failed to get irq: %d\n", rc);
1114 		return rc;
1115 	}
1116 
1117 	rc = devm_request_irq(&dp->pdev->dev, dp->irq,
1118 			dp_display_irq_handler,
1119 			IRQF_TRIGGER_HIGH, "dp_display_isr", dp);
1120 	if (rc < 0) {
1121 		DRM_ERROR("failed to request IRQ%u: %d\n",
1122 				dp->irq, rc);
1123 		return rc;
1124 	}
1125 	disable_irq(dp->irq);
1126 
1127 	return 0;
1128 }
1129 
dp_display_probe(struct platform_device * pdev)1130 static int dp_display_probe(struct platform_device *pdev)
1131 {
1132 	int rc = 0;
1133 	struct dp_display_private *dp;
1134 
1135 	if (!pdev || !pdev->dev.of_node) {
1136 		DRM_ERROR("pdev not found\n");
1137 		return -ENODEV;
1138 	}
1139 
1140 	dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1141 	if (!dp)
1142 		return -ENOMEM;
1143 
1144 	dp->pdev = pdev;
1145 	dp->name = "drm_dp";
1146 
1147 	rc = dp_init_sub_modules(dp);
1148 	if (rc) {
1149 		DRM_ERROR("init sub module failed\n");
1150 		return -EPROBE_DEFER;
1151 	}
1152 
1153 	mutex_init(&dp->event_mutex);
1154 
1155 	init_completion(&dp->resume_comp);
1156 
1157 	g_dp_display = &dp->dp_display;
1158 
1159 	/* Store DP audio handle inside DP display */
1160 	g_dp_display->dp_audio = dp->audio;
1161 
1162 	init_completion(&dp->audio_comp);
1163 
1164 	platform_set_drvdata(pdev, g_dp_display);
1165 
1166 	rc = component_add(&pdev->dev, &dp_display_comp_ops);
1167 	if (rc) {
1168 		DRM_ERROR("component add failed, rc=%d\n", rc);
1169 		dp_display_deinit_sub_modules(dp);
1170 	}
1171 
1172 	return rc;
1173 }
1174 
dp_display_remove(struct platform_device * pdev)1175 static int dp_display_remove(struct platform_device *pdev)
1176 {
1177 	struct dp_display_private *dp;
1178 
1179 	dp = container_of(g_dp_display,
1180 			struct dp_display_private, dp_display);
1181 
1182 	dp_display_deinit_sub_modules(dp);
1183 
1184 	component_del(&pdev->dev, &dp_display_comp_ops);
1185 	platform_set_drvdata(pdev, NULL);
1186 
1187 	return 0;
1188 }
1189 
dp_pm_resume(struct device * dev)1190 static int dp_pm_resume(struct device *dev)
1191 {
1192 	return 0;
1193 }
1194 
dp_pm_suspend(struct device * dev)1195 static int dp_pm_suspend(struct device *dev)
1196 {
1197 	struct platform_device *pdev = to_platform_device(dev);
1198 	struct dp_display_private *dp = platform_get_drvdata(pdev);
1199 
1200 	if (!dp) {
1201 		DRM_ERROR("DP driver bind failed. Invalid driver data\n");
1202 		return -EINVAL;
1203 	}
1204 
1205 	atomic_set(&dp->hpd_state, ST_SUSPENDED);
1206 
1207 	return 0;
1208 }
1209 
dp_pm_prepare(struct device * dev)1210 static int dp_pm_prepare(struct device *dev)
1211 {
1212 	return 0;
1213 }
1214 
dp_pm_complete(struct device * dev)1215 static void dp_pm_complete(struct device *dev)
1216 {
1217 
1218 }
1219 
1220 static const struct dev_pm_ops dp_pm_ops = {
1221 	.suspend = dp_pm_suspend,
1222 	.resume =  dp_pm_resume,
1223 	.prepare = dp_pm_prepare,
1224 	.complete = dp_pm_complete,
1225 };
1226 
1227 static struct platform_driver dp_display_driver = {
1228 	.probe  = dp_display_probe,
1229 	.remove = dp_display_remove,
1230 	.driver = {
1231 		.name = "msm-dp-display",
1232 		.of_match_table = dp_dt_match,
1233 		.suppress_bind_attrs = true,
1234 		.pm = &dp_pm_ops,
1235 	},
1236 };
1237 
msm_dp_register(void)1238 int __init msm_dp_register(void)
1239 {
1240 	int ret;
1241 
1242 	ret = platform_driver_register(&dp_display_driver);
1243 	if (ret)
1244 		DRM_ERROR("Dp display driver register failed");
1245 
1246 	return ret;
1247 }
1248 
msm_dp_unregister(void)1249 void __exit msm_dp_unregister(void)
1250 {
1251 	platform_driver_unregister(&dp_display_driver);
1252 }
1253 
msm_dp_irq_postinstall(struct msm_dp * dp_display)1254 void msm_dp_irq_postinstall(struct msm_dp *dp_display)
1255 {
1256 	struct dp_display_private *dp;
1257 
1258 	if (!dp_display)
1259 		return;
1260 
1261 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1262 
1263 	dp_hpd_event_setup(dp);
1264 
1265 	dp_add_event(dp, EV_HPD_INIT_SETUP, 0, 100);
1266 }
1267 
msm_dp_debugfs_init(struct msm_dp * dp_display,struct drm_minor * minor)1268 void msm_dp_debugfs_init(struct msm_dp *dp_display, struct drm_minor *minor)
1269 {
1270 	struct dp_display_private *dp;
1271 	struct device *dev;
1272 	int rc;
1273 
1274 	dp = container_of(dp_display, struct dp_display_private, dp_display);
1275 	dev = &dp->pdev->dev;
1276 
1277 	dp->debug = dp_debug_get(dev, dp->panel, dp->usbpd,
1278 					dp->link, &dp->dp_display.connector,
1279 					minor);
1280 	if (IS_ERR(dp->debug)) {
1281 		rc = PTR_ERR(dp->debug);
1282 		DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1283 		dp->debug = NULL;
1284 	}
1285 }
1286 
msm_dp_modeset_init(struct msm_dp * dp_display,struct drm_device * dev,struct drm_encoder * encoder)1287 int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1288 			struct drm_encoder *encoder)
1289 {
1290 	struct msm_drm_private *priv;
1291 	int ret;
1292 
1293 	if (WARN_ON(!encoder) || WARN_ON(!dp_display) || WARN_ON(!dev))
1294 		return -EINVAL;
1295 
1296 	priv = dev->dev_private;
1297 	dp_display->drm_dev = dev;
1298 
1299 	ret = dp_display_request_irq(dp_display);
1300 	if (ret) {
1301 		DRM_ERROR("request_irq failed, ret=%d\n", ret);
1302 		return ret;
1303 	}
1304 
1305 	dp_display->encoder = encoder;
1306 
1307 	dp_display->connector = dp_drm_connector_init(dp_display);
1308 	if (IS_ERR(dp_display->connector)) {
1309 		ret = PTR_ERR(dp_display->connector);
1310 		DRM_DEV_ERROR(dev->dev,
1311 			"failed to create dp connector: %d\n", ret);
1312 		dp_display->connector = NULL;
1313 		return ret;
1314 	}
1315 
1316 	priv->connectors[priv->num_connectors++] = dp_display->connector;
1317 	return 0;
1318 }
1319 
dp_display_wait4resume_done(struct dp_display_private * dp)1320 static int dp_display_wait4resume_done(struct dp_display_private *dp)
1321 {
1322 	int ret = 0;
1323 
1324 	reinit_completion(&dp->resume_comp);
1325 	if (!wait_for_completion_timeout(&dp->resume_comp,
1326 				WAIT_FOR_RESUME_TIMEOUT_JIFFIES)) {
1327 		DRM_ERROR("wait4resume_done timedout\n");
1328 		ret = -ETIMEDOUT;
1329 	}
1330 	return ret;
1331 }
1332 
msm_dp_display_enable(struct msm_dp * dp,struct drm_encoder * encoder)1333 int msm_dp_display_enable(struct msm_dp *dp, struct drm_encoder *encoder)
1334 {
1335 	int rc = 0;
1336 	struct dp_display_private *dp_display;
1337 	u32 state;
1338 
1339 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1340 	if (!dp_display->dp_mode.drm_mode.clock) {
1341 		DRM_ERROR("invalid params\n");
1342 		return -EINVAL;
1343 	}
1344 
1345 	mutex_lock(&dp_display->event_mutex);
1346 
1347 	rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1348 	if (rc) {
1349 		DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1350 		mutex_unlock(&dp_display->event_mutex);
1351 		return rc;
1352 	}
1353 
1354 	rc = dp_display_prepare(dp);
1355 	if (rc) {
1356 		DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1357 		mutex_unlock(&dp_display->event_mutex);
1358 		return rc;
1359 	}
1360 
1361 	state =  atomic_read(&dp_display->hpd_state);
1362 	if (state == ST_SUSPENDED) {
1363 		/* start link training */
1364 		dp_add_event(dp_display, EV_HPD_PLUG_INT, 0, 0);
1365 		mutex_unlock(&dp_display->event_mutex);
1366 
1367 		/* wait until dp interface is up */
1368 		goto resume_done;
1369 	}
1370 
1371 	dp_display_enable(dp_display, 0);
1372 
1373 	rc = dp_display_post_enable(dp);
1374 	if (rc) {
1375 		DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1376 		dp_display_disable(dp_display, 0);
1377 		dp_display_unprepare(dp);
1378 	}
1379 
1380 	dp_del_event(dp_display, EV_CONNECT_PENDING_TIMEOUT);
1381 
1382 	if (state == ST_SUSPEND_PENDING)
1383 		dp_add_event(dp_display, EV_IRQ_HPD_INT, 0, 0);
1384 
1385 	/* completed connection */
1386 	atomic_set(&dp_display->hpd_state, ST_CONNECTED);
1387 
1388 	mutex_unlock(&dp_display->event_mutex);
1389 
1390 	return rc;
1391 
1392 resume_done:
1393 	dp_display_wait4resume_done(dp_display);
1394 	return rc;
1395 }
1396 
msm_dp_display_pre_disable(struct msm_dp * dp,struct drm_encoder * encoder)1397 int msm_dp_display_pre_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1398 {
1399 	struct dp_display_private *dp_display;
1400 
1401 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1402 
1403 	dp_ctrl_push_idle(dp_display->ctrl);
1404 
1405 	return 0;
1406 }
1407 
msm_dp_display_disable(struct msm_dp * dp,struct drm_encoder * encoder)1408 int msm_dp_display_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1409 {
1410 	int rc = 0;
1411 	u32 state;
1412 	struct dp_display_private *dp_display;
1413 
1414 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1415 
1416 	mutex_lock(&dp_display->event_mutex);
1417 
1418 	dp_display_disable(dp_display, 0);
1419 
1420 	rc = dp_display_unprepare(dp);
1421 	if (rc)
1422 		DRM_ERROR("DP display unprepare failed, rc=%d\n", rc);
1423 
1424 	dp_del_event(dp_display, EV_DISCONNECT_PENDING_TIMEOUT);
1425 
1426 	state =  atomic_read(&dp_display->hpd_state);
1427 	if (state == ST_DISCONNECT_PENDING) {
1428 		/* completed disconnection */
1429 		atomic_set(&dp_display->hpd_state, ST_DISCONNECTED);
1430 	} else {
1431 		atomic_set(&dp_display->hpd_state, ST_SUSPEND_PENDING);
1432 	}
1433 
1434 	mutex_unlock(&dp_display->event_mutex);
1435 	return rc;
1436 }
1437 
msm_dp_display_mode_set(struct msm_dp * dp,struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)1438 void msm_dp_display_mode_set(struct msm_dp *dp, struct drm_encoder *encoder,
1439 				struct drm_display_mode *mode,
1440 				struct drm_display_mode *adjusted_mode)
1441 {
1442 	struct dp_display_private *dp_display;
1443 
1444 	dp_display = container_of(dp, struct dp_display_private, dp_display);
1445 
1446 	memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1447 
1448 	if (dp_display_check_video_test(dp))
1449 		dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1450 	else /* Default num_components per pixel = 3 */
1451 		dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1452 
1453 	if (!dp_display->dp_mode.bpp)
1454 		dp_display->dp_mode.bpp = 24; /* Default bpp */
1455 
1456 	drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1457 
1458 	dp_display->dp_mode.v_active_low =
1459 		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1460 
1461 	dp_display->dp_mode.h_active_low =
1462 		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);
1463 }
1464