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