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