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