1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
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>
17 #include "dp_parser.h"
19 #include "dp_catalog.h"
25 #include "dp_display.h"
30 static struct msm_dp *g_dp_display;
31 #define HPD_STRING_SIZE 30
38 ISR_IRQ_HPD_PULSE_COUNT,
39 ISR_HPD_LO_GLITH_COUNT,
42 /* event thread connection state */
47 ST_DISCONNECT_PENDING,
61 EV_CONNECT_PENDING_TIMEOUT,
62 EV_DISCONNECT_PENDING_TIMEOUT,
65 #define EVENT_TIMEOUT (HZ/10) /* 100ms */
66 #define DP_EVENT_Q_MAX 8
68 #define DP_TIMEOUT_5_SECOND (5000/EVENT_TIMEOUT)
69 #define DP_TIMEOUT_NONE 0
71 #define WAIT_FOR_RESUME_TIMEOUT_JIFFIES (HZ / 2)
79 struct dp_display_private {
84 bool core_initialized;
88 struct platform_device *pdev;
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;
97 struct dp_panel *panel;
99 struct dp_debug *debug;
101 struct dp_usbpd_cb usbpd_cb;
102 struct dp_display_mode dp_mode;
103 struct msm_dp dp_display;
105 bool encoder_mode_set;
107 /* wait for audio signaling */
108 struct completion audio_comp;
110 /* event related only access by event thread */
111 struct mutex event_mutex;
112 wait_queue_head_t event_q;
116 struct dp_event event_list[DP_EVENT_Q_MAX];
117 spinlock_t event_lock;
119 struct dp_audio *audio;
122 static const struct of_device_id dp_dt_match[] = {
123 {.compatible = "qcom,sc7180-dp"},
127 static int dp_add_event(struct dp_display_private *dp_priv, u32 event,
131 struct dp_event *todo;
134 spin_lock_irqsave(&dp_priv->event_lock, flag);
135 pndx = dp_priv->event_pndx + 1;
136 pndx %= DP_EVENT_Q_MAX;
137 if (pndx == dp_priv->event_gndx) {
138 pr_err("event_q is full: pndx=%d gndx=%d\n",
139 dp_priv->event_pndx, dp_priv->event_gndx);
140 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
143 todo = &dp_priv->event_list[dp_priv->event_pndx++];
144 dp_priv->event_pndx %= DP_EVENT_Q_MAX;
145 todo->event_id = event;
148 wake_up(&dp_priv->event_q);
149 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
154 static int dp_del_event(struct dp_display_private *dp_priv, u32 event)
157 struct dp_event *todo;
160 spin_lock_irqsave(&dp_priv->event_lock, flag);
161 if (dp_priv->event_pndx == dp_priv->event_gndx) {
162 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
166 gndx = dp_priv->event_gndx;
167 while (dp_priv->event_pndx != gndx) {
168 todo = &dp_priv->event_list[gndx];
169 if (todo->event_id == event) {
170 todo->event_id = EV_NO_EVENT; /* deleted */
174 gndx %= DP_EVENT_Q_MAX;
176 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
181 void dp_display_signal_audio_start(struct msm_dp *dp_display)
183 struct dp_display_private *dp;
185 dp = container_of(dp_display, struct dp_display_private, dp_display);
187 reinit_completion(&dp->audio_comp);
190 void dp_display_signal_audio_complete(struct msm_dp *dp_display)
192 struct dp_display_private *dp;
194 dp = container_of(dp_display, struct dp_display_private, dp_display);
196 complete_all(&dp->audio_comp);
199 static int dp_display_bind(struct device *dev, struct device *master,
203 struct dp_display_private *dp;
204 struct drm_device *drm;
205 struct msm_drm_private *priv;
207 drm = dev_get_drvdata(master);
209 dp = container_of(g_dp_display,
210 struct dp_display_private, dp_display);
212 dp->dp_display.drm_dev = drm;
213 priv = drm->dev_private;
214 priv->dp = &(dp->dp_display);
216 rc = dp->parser->parse(dp->parser);
218 DRM_ERROR("device tree parsing failed\n");
222 rc = dp_aux_register(dp->aux);
224 DRM_ERROR("DRM DP AUX register failed\n");
228 rc = dp_power_client_init(dp->power);
230 DRM_ERROR("Power client create failed\n");
234 rc = dp_register_audio_driver(dev, dp->audio);
236 DRM_ERROR("Audio registration Dp failed\n");
242 static void dp_display_unbind(struct device *dev, struct device *master,
245 struct dp_display_private *dp;
246 struct drm_device *drm = dev_get_drvdata(master);
247 struct msm_drm_private *priv = drm->dev_private;
249 dp = container_of(g_dp_display,
250 struct dp_display_private, dp_display);
252 dp_power_client_deinit(dp->power);
253 dp_aux_unregister(dp->aux);
257 static const struct component_ops dp_display_comp_ops = {
258 .bind = dp_display_bind,
259 .unbind = dp_display_unbind,
262 static bool dp_display_is_ds_bridge(struct dp_panel *panel)
264 return (panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
265 DP_DWN_STRM_PORT_PRESENT);
268 static bool dp_display_is_sink_count_zero(struct dp_display_private *dp)
270 return dp_display_is_ds_bridge(dp->panel) &&
271 (dp->link->sink_count == 0);
274 static void dp_display_send_hpd_event(struct msm_dp *dp_display)
276 struct dp_display_private *dp;
277 struct drm_connector *connector;
279 dp = container_of(dp_display, struct dp_display_private, dp_display);
281 connector = dp->dp_display.connector;
282 drm_helper_hpd_irq_event(connector->dev);
286 static void dp_display_set_encoder_mode(struct dp_display_private *dp)
288 struct msm_drm_private *priv = dp->dp_display.drm_dev->dev_private;
289 struct msm_kms *kms = priv->kms;
291 if (!dp->encoder_mode_set && dp->dp_display.encoder &&
292 kms->funcs->set_encoder_mode) {
293 kms->funcs->set_encoder_mode(kms,
294 dp->dp_display.encoder, false);
296 dp->encoder_mode_set = true;
300 static int dp_display_send_hpd_notification(struct dp_display_private *dp,
303 if ((hpd && dp->dp_display.is_connected) ||
304 (!hpd && !dp->dp_display.is_connected)) {
305 DRM_DEBUG_DP("HPD already %s\n", (hpd ? "on" : "off"));
309 /* reset video pattern flag on disconnect */
311 dp->panel->video_test = false;
313 dp->dp_display.is_connected = hpd;
315 dp_display_send_hpd_event(&dp->dp_display);
320 static int dp_display_process_hpd_high(struct dp_display_private *dp)
325 dp->panel->max_dp_lanes = dp->parser->max_dp_lanes;
327 rc = dp_panel_read_sink_caps(dp->panel, dp->dp_display.connector);
331 dp_link_process_request(dp->link);
333 edid = dp->panel->edid;
335 dp->audio_supported = drm_detect_monitor_audio(edid);
336 dp_panel_handle_sink_request(dp->panel);
338 dp->dp_display.max_pclk_khz = DP_MAX_PIXEL_CLK_KHZ;
339 dp->dp_display.max_dp_lanes = dp->parser->max_dp_lanes;
342 * set sink to normal operation mode -- D0
345 dp_link_psm_config(dp->link, &dp->panel->link_info, false);
347 dp_link_reset_phy_params_vx_px(dp->link);
348 rc = dp_ctrl_on_link(dp->ctrl);
350 DRM_ERROR("failed to complete DP link training\n");
354 dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
360 static void dp_display_host_init(struct dp_display_private *dp, int reset)
364 if (dp->core_initialized) {
365 DRM_DEBUG_DP("DP core already initialized\n");
369 if (dp->usbpd->orientation == ORIENTATION_CC2)
372 dp_display_set_encoder_mode(dp);
374 dp_power_init(dp->power, flip);
375 dp_ctrl_host_init(dp->ctrl, flip, reset);
376 dp_aux_init(dp->aux);
377 dp->core_initialized = true;
380 static void dp_display_host_deinit(struct dp_display_private *dp)
382 if (!dp->core_initialized) {
383 DRM_DEBUG_DP("DP core not initialized\n");
387 dp_ctrl_host_deinit(dp->ctrl);
388 dp_aux_deinit(dp->aux);
389 dp_power_deinit(dp->power);
391 dp->core_initialized = false;
394 static int dp_display_usbpd_configure_cb(struct device *dev)
397 struct dp_display_private *dp;
400 DRM_ERROR("invalid dev\n");
405 dp = container_of(g_dp_display,
406 struct dp_display_private, dp_display);
408 dp_display_host_init(dp, false);
410 rc = dp_display_process_hpd_high(dp);
415 static int dp_display_usbpd_disconnect_cb(struct device *dev)
418 struct dp_display_private *dp;
421 DRM_ERROR("invalid dev\n");
426 dp = container_of(g_dp_display,
427 struct dp_display_private, dp_display);
429 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
434 static void dp_display_handle_video_request(struct dp_display_private *dp)
436 if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
437 dp->panel->video_test = true;
438 dp_link_send_test_response(dp->link);
442 static int dp_display_handle_port_ststus_changed(struct dp_display_private *dp)
446 if (dp_display_is_sink_count_zero(dp)) {
447 DRM_DEBUG_DP("sink count is zero, nothing to do\n");
448 if (dp->hpd_state != ST_DISCONNECTED) {
449 dp->hpd_state = ST_DISCONNECT_PENDING;
450 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
453 if (dp->hpd_state == ST_DISCONNECTED) {
454 dp->hpd_state = ST_CONNECT_PENDING;
455 rc = dp_display_process_hpd_high(dp);
457 dp->hpd_state = ST_DISCONNECTED;
464 static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
466 u32 sink_request = dp->link->sink_request;
468 if (dp->hpd_state == ST_DISCONNECTED) {
469 if (sink_request & DP_LINK_STATUS_UPDATED) {
470 DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
475 dp_ctrl_handle_sink_request(dp->ctrl);
477 if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
478 dp_display_handle_video_request(dp);
483 static int dp_display_usbpd_attention_cb(struct device *dev)
487 struct dp_display_private *dp;
490 DRM_ERROR("invalid dev\n");
494 dp = container_of(g_dp_display,
495 struct dp_display_private, dp_display);
497 /* check for any test request issued by sink */
498 rc = dp_link_process_request(dp->link);
500 sink_request = dp->link->sink_request;
501 if (sink_request & DS_PORT_STATUS_CHANGED)
502 rc = dp_display_handle_port_ststus_changed(dp);
504 rc = dp_display_handle_irq_hpd(dp);
510 static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
512 struct dp_usbpd *hpd = dp->usbpd;
514 u32 tout = DP_TIMEOUT_5_SECOND;
520 mutex_lock(&dp->event_mutex);
522 state = dp->hpd_state;
523 if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
524 mutex_unlock(&dp->event_mutex);
528 if (state == ST_CONNECT_PENDING || state == ST_CONNECTED) {
529 mutex_unlock(&dp->event_mutex);
533 if (state == ST_DISCONNECT_PENDING) {
534 /* wait until ST_DISCONNECTED */
535 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
536 mutex_unlock(&dp->event_mutex);
540 dp->hpd_state = ST_CONNECT_PENDING;
544 ret = dp_display_usbpd_configure_cb(&dp->pdev->dev);
545 if (ret) { /* link train failed */
547 dp->hpd_state = ST_DISCONNECTED;
549 if (ret == -ECONNRESET) { /* cable unplugged */
550 dp->core_initialized = false;
554 /* start sentinel checking in case of missing uevent */
555 dp_add_event(dp, EV_CONNECT_PENDING_TIMEOUT, 0, tout);
558 /* enable HDP irq_hpd/replug interrupt */
559 dp_catalog_hpd_config_intr(dp->catalog,
560 DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, true);
562 mutex_unlock(&dp->event_mutex);
564 /* uevent will complete connection part */
568 static int dp_display_enable(struct dp_display_private *dp, u32 data);
569 static int dp_display_disable(struct dp_display_private *dp, u32 data);
571 static int dp_connect_pending_timeout(struct dp_display_private *dp, u32 data)
575 mutex_lock(&dp->event_mutex);
577 state = dp->hpd_state;
578 if (state == ST_CONNECT_PENDING)
579 dp->hpd_state = ST_CONNECTED;
581 mutex_unlock(&dp->event_mutex);
586 static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
589 struct dp_display_private *dp;
591 dp = container_of(dp_display,
592 struct dp_display_private, dp_display);
594 /* notify audio subsystem only if sink supports audio */
595 if (dp_display->plugged_cb && dp_display->codec_dev &&
597 dp_display->plugged_cb(dp_display->codec_dev, plugged);
600 static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
602 struct dp_usbpd *hpd = dp->usbpd;
608 mutex_lock(&dp->event_mutex);
610 state = dp->hpd_state;
612 /* disable irq_hpd/replug interrupts */
613 dp_catalog_hpd_config_intr(dp->catalog,
614 DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, false);
616 /* unplugged, no more irq_hpd handle */
617 dp_del_event(dp, EV_IRQ_HPD_INT);
619 if (state == ST_DISCONNECTED) {
620 /* triggered by irq_hdp with sink_count = 0 */
621 if (dp->link->sink_count == 0) {
622 dp_ctrl_off_phy(dp->ctrl);
624 dp->core_initialized = false;
626 mutex_unlock(&dp->event_mutex);
630 if (state == ST_DISCONNECT_PENDING) {
631 mutex_unlock(&dp->event_mutex);
635 if (state == ST_CONNECT_PENDING) {
636 /* wait until CONNECTED */
637 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 1); /* delay = 1 */
638 mutex_unlock(&dp->event_mutex);
642 dp->hpd_state = ST_DISCONNECT_PENDING;
644 /* disable HPD plug interrupts */
645 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, false);
650 * We don't need separate work for disconnect as
651 * connect/attention interrupts are disabled
653 dp_display_usbpd_disconnect_cb(&dp->pdev->dev);
655 /* start sentinel checking in case of missing uevent */
656 dp_add_event(dp, EV_DISCONNECT_PENDING_TIMEOUT, 0, DP_TIMEOUT_5_SECOND);
658 /* signal the disconnect event early to ensure proper teardown */
659 dp_display_handle_plugged_change(g_dp_display, false);
661 /* enable HDP plug interrupt to prepare for next plugin */
662 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, true);
664 /* uevent will complete disconnection part */
665 mutex_unlock(&dp->event_mutex);
669 static int dp_disconnect_pending_timeout(struct dp_display_private *dp, u32 data)
673 mutex_lock(&dp->event_mutex);
675 state = dp->hpd_state;
676 if (state == ST_DISCONNECT_PENDING)
677 dp->hpd_state = ST_DISCONNECTED;
679 mutex_unlock(&dp->event_mutex);
684 static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
689 mutex_lock(&dp->event_mutex);
691 /* irq_hpd can happen at either connected or disconnected state */
692 state = dp->hpd_state;
693 if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
694 mutex_unlock(&dp->event_mutex);
698 if (state == ST_CONNECT_PENDING) {
699 /* wait until ST_CONNECTED */
700 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
701 mutex_unlock(&dp->event_mutex);
705 if (state == ST_CONNECT_PENDING || state == ST_DISCONNECT_PENDING) {
706 /* wait until ST_CONNECTED */
707 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
708 mutex_unlock(&dp->event_mutex);
712 ret = dp_display_usbpd_attention_cb(&dp->pdev->dev);
713 if (ret == -ECONNRESET) { /* cable unplugged */
714 dp->core_initialized = false;
717 mutex_unlock(&dp->event_mutex);
722 static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
724 dp_debug_put(dp->debug);
725 dp_panel_put(dp->panel);
727 dp_audio_put(dp->audio);
730 static int dp_init_sub_modules(struct dp_display_private *dp)
733 struct device *dev = &dp->pdev->dev;
734 struct dp_usbpd_cb *cb = &dp->usbpd_cb;
735 struct dp_panel_in panel_in = {
739 /* Callback APIs used for cable status change event */
740 cb->configure = dp_display_usbpd_configure_cb;
741 cb->disconnect = dp_display_usbpd_disconnect_cb;
742 cb->attention = dp_display_usbpd_attention_cb;
744 dp->usbpd = dp_hpd_get(dev, cb);
745 if (IS_ERR(dp->usbpd)) {
746 rc = PTR_ERR(dp->usbpd);
747 DRM_ERROR("failed to initialize hpd, rc = %d\n", rc);
752 dp->parser = dp_parser_get(dp->pdev);
753 if (IS_ERR(dp->parser)) {
754 rc = PTR_ERR(dp->parser);
755 DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
760 dp->catalog = dp_catalog_get(dev, &dp->parser->io);
761 if (IS_ERR(dp->catalog)) {
762 rc = PTR_ERR(dp->catalog);
763 DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
768 dp->power = dp_power_get(dev, dp->parser);
769 if (IS_ERR(dp->power)) {
770 rc = PTR_ERR(dp->power);
771 DRM_ERROR("failed to initialize power, rc = %d\n", rc);
776 dp->aux = dp_aux_get(dev, dp->catalog);
777 if (IS_ERR(dp->aux)) {
778 rc = PTR_ERR(dp->aux);
779 DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
784 dp->link = dp_link_get(dev, dp->aux);
785 if (IS_ERR(dp->link)) {
786 rc = PTR_ERR(dp->link);
787 DRM_ERROR("failed to initialize link, rc = %d\n", rc);
792 panel_in.aux = dp->aux;
793 panel_in.catalog = dp->catalog;
794 panel_in.link = dp->link;
796 dp->panel = dp_panel_get(&panel_in);
797 if (IS_ERR(dp->panel)) {
798 rc = PTR_ERR(dp->panel);
799 DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
804 dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
805 dp->power, dp->catalog, dp->parser);
806 if (IS_ERR(dp->ctrl)) {
807 rc = PTR_ERR(dp->ctrl);
808 DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
813 dp->audio = dp_audio_get(dp->pdev, dp->panel, dp->catalog);
814 if (IS_ERR(dp->audio)) {
815 rc = PTR_ERR(dp->audio);
816 pr_err("failed to initialize audio, rc = %d\n", rc);
824 dp_panel_put(dp->panel);
831 static int dp_display_set_mode(struct msm_dp *dp_display,
832 struct dp_display_mode *mode)
834 struct dp_display_private *dp;
836 dp = container_of(dp_display, struct dp_display_private, dp_display);
838 dp->panel->dp_mode.drm_mode = mode->drm_mode;
839 dp->panel->dp_mode.bpp = mode->bpp;
840 dp->panel->dp_mode.capabilities = mode->capabilities;
841 dp_panel_init_panel_info(dp->panel);
845 static int dp_display_prepare(struct msm_dp *dp)
850 static int dp_display_enable(struct dp_display_private *dp, u32 data)
853 struct msm_dp *dp_display;
855 dp_display = g_dp_display;
857 if (dp_display->power_on) {
858 DRM_DEBUG_DP("Link already setup, return\n");
862 rc = dp_ctrl_on_stream(dp->ctrl);
864 dp_display->power_on = true;
869 static int dp_display_post_enable(struct msm_dp *dp_display)
871 struct dp_display_private *dp;
874 dp = container_of(dp_display, struct dp_display_private, dp_display);
876 rate = dp->link->link_params.rate;
878 if (dp->audio_supported) {
879 dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
880 dp->audio->lane_count = dp->link->link_params.num_lanes;
883 /* signal the connect event late to synchronize video and display */
884 dp_display_handle_plugged_change(dp_display, true);
888 static int dp_display_disable(struct dp_display_private *dp, u32 data)
890 struct msm_dp *dp_display;
892 dp_display = g_dp_display;
894 if (!dp_display->power_on)
897 /* wait only if audio was enabled */
898 if (dp_display->audio_enabled) {
899 /* signal the disconnect event */
900 dp_display_handle_plugged_change(dp_display, false);
901 if (!wait_for_completion_timeout(&dp->audio_comp,
903 DRM_ERROR("audio comp timeout\n");
906 dp_display->audio_enabled = false;
908 /* triggered by irq_hpd with sink_count = 0 */
909 if (dp->link->sink_count == 0) {
910 dp_ctrl_off_link_stream(dp->ctrl);
912 dp_ctrl_off(dp->ctrl);
913 dp->core_initialized = false;
916 dp_display->power_on = false;
921 static int dp_display_unprepare(struct msm_dp *dp)
926 int dp_display_set_plugged_cb(struct msm_dp *dp_display,
927 hdmi_codec_plugged_cb fn, struct device *codec_dev)
931 dp_display->plugged_cb = fn;
932 dp_display->codec_dev = codec_dev;
933 plugged = dp_display->is_connected;
934 dp_display_handle_plugged_change(dp_display, plugged);
939 int dp_display_validate_mode(struct msm_dp *dp, u32 mode_pclk_khz)
941 const u32 num_components = 3, default_bpp = 24;
942 struct dp_display_private *dp_display;
943 struct dp_link_info *link_info;
944 u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
946 if (!dp || !mode_pclk_khz || !dp->connector) {
947 DRM_ERROR("invalid params\n");
951 dp_display = container_of(dp, struct dp_display_private, dp_display);
952 link_info = &dp_display->panel->link_info;
954 mode_bpp = dp->connector->display_info.bpc * num_components;
956 mode_bpp = default_bpp;
958 mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
959 mode_bpp, mode_pclk_khz);
961 mode_rate_khz = mode_pclk_khz * mode_bpp;
962 supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
964 if (mode_rate_khz > supported_rate_khz)
970 int dp_display_get_modes(struct msm_dp *dp,
971 struct dp_display_mode *dp_mode)
973 struct dp_display_private *dp_display;
977 DRM_ERROR("invalid params\n");
981 dp_display = container_of(dp, struct dp_display_private, dp_display);
983 ret = dp_panel_get_modes(dp_display->panel,
984 dp->connector, dp_mode);
985 if (dp_mode->drm_mode.clock)
986 dp->max_pclk_khz = dp_mode->drm_mode.clock;
990 bool dp_display_check_video_test(struct msm_dp *dp)
992 struct dp_display_private *dp_display;
994 dp_display = container_of(dp, struct dp_display_private, dp_display);
996 return dp_display->panel->video_test;
999 int dp_display_get_test_bpp(struct msm_dp *dp)
1001 struct dp_display_private *dp_display;
1004 DRM_ERROR("invalid params\n");
1008 dp_display = container_of(dp, struct dp_display_private, dp_display);
1010 return dp_link_bit_depth_to_bpp(
1011 dp_display->link->test_video.test_bit_depth);
1014 void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
1016 struct dp_display_private *dp_display;
1017 struct drm_device *drm;
1019 dp_display = container_of(dp, struct dp_display_private, dp_display);
1023 * if we are reading registers we need the link clocks to be on
1024 * however till DP cable is connected this will not happen as we
1025 * do not know the resolution to power up with. Hence check the
1026 * power_on status before dumping DP registers to avoid crash due
1027 * to unclocked access
1029 mutex_lock(&dp_display->event_mutex);
1031 if (!dp->power_on) {
1032 mutex_unlock(&dp_display->event_mutex);
1036 dp_catalog_snapshot(dp_display->catalog, disp_state);
1038 mutex_unlock(&dp_display->event_mutex);
1041 static void dp_display_config_hpd(struct dp_display_private *dp)
1044 dp_display_host_init(dp, true);
1045 dp_catalog_ctrl_hpd_config(dp->catalog);
1047 /* Enable interrupt first time
1048 * we are leaving dp clocks on during disconnect
1049 * and never disable interrupt
1051 enable_irq(dp->irq);
1054 static int hpd_event_thread(void *data)
1056 struct dp_display_private *dp_priv;
1058 struct dp_event *todo;
1059 int timeout_mode = 0;
1061 dp_priv = (struct dp_display_private *)data;
1065 wait_event_timeout(dp_priv->event_q,
1066 (dp_priv->event_pndx == dp_priv->event_gndx),
1069 wait_event_interruptible(dp_priv->event_q,
1070 (dp_priv->event_pndx != dp_priv->event_gndx));
1072 spin_lock_irqsave(&dp_priv->event_lock, flag);
1073 todo = &dp_priv->event_list[dp_priv->event_gndx];
1075 struct dp_event *todo_next;
1077 dp_priv->event_gndx++;
1078 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1080 /* re enter delay event into q */
1081 todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
1082 dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1083 todo_next->event_id = todo->event_id;
1084 todo_next->data = todo->data;
1085 todo_next->delay = todo->delay - 1;
1087 /* clean up older event */
1088 todo->event_id = EV_NO_EVENT;
1091 /* switch to timeout mode */
1093 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1097 /* timeout with no events in q */
1098 if (dp_priv->event_pndx == dp_priv->event_gndx) {
1099 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1103 dp_priv->event_gndx++;
1104 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1106 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1108 switch (todo->event_id) {
1109 case EV_HPD_INIT_SETUP:
1110 dp_display_config_hpd(dp_priv);
1112 case EV_HPD_PLUG_INT:
1113 dp_hpd_plug_handle(dp_priv, todo->data);
1115 case EV_HPD_UNPLUG_INT:
1116 dp_hpd_unplug_handle(dp_priv, todo->data);
1118 case EV_IRQ_HPD_INT:
1119 dp_irq_hpd_handle(dp_priv, todo->data);
1121 case EV_HPD_REPLUG_INT:
1124 case EV_USER_NOTIFICATION:
1125 dp_display_send_hpd_notification(dp_priv,
1128 case EV_CONNECT_PENDING_TIMEOUT:
1129 dp_connect_pending_timeout(dp_priv,
1132 case EV_DISCONNECT_PENDING_TIMEOUT:
1133 dp_disconnect_pending_timeout(dp_priv,
1144 static void dp_hpd_event_setup(struct dp_display_private *dp_priv)
1146 init_waitqueue_head(&dp_priv->event_q);
1147 spin_lock_init(&dp_priv->event_lock);
1149 kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1152 static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1154 struct dp_display_private *dp = dev_id;
1155 irqreturn_t ret = IRQ_HANDLED;
1159 DRM_ERROR("invalid data\n");
1163 hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1165 if (hpd_isr_status & 0x0F) {
1166 /* hpd related interrupts */
1167 if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK ||
1168 hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1169 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1172 if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1173 /* stop sentinel connect pending checking */
1174 dp_del_event(dp, EV_CONNECT_PENDING_TIMEOUT);
1175 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1178 if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK)
1179 dp_add_event(dp, EV_HPD_REPLUG_INT, 0, 0);
1181 if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1182 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1185 /* DP controller isr */
1186 dp_ctrl_isr(dp->ctrl);
1189 dp_aux_isr(dp->aux);
1194 int dp_display_request_irq(struct msm_dp *dp_display)
1197 struct dp_display_private *dp;
1200 DRM_ERROR("invalid input\n");
1204 dp = container_of(dp_display, struct dp_display_private, dp_display);
1206 dp->irq = irq_of_parse_and_map(dp->pdev->dev.of_node, 0);
1209 DRM_ERROR("failed to get irq: %d\n", rc);
1213 rc = devm_request_irq(&dp->pdev->dev, dp->irq,
1214 dp_display_irq_handler,
1215 IRQF_TRIGGER_HIGH, "dp_display_isr", dp);
1217 DRM_ERROR("failed to request IRQ%u: %d\n",
1221 disable_irq(dp->irq);
1226 static int dp_display_probe(struct platform_device *pdev)
1229 struct dp_display_private *dp;
1231 if (!pdev || !pdev->dev.of_node) {
1232 DRM_ERROR("pdev not found\n");
1236 dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1241 dp->name = "drm_dp";
1243 rc = dp_init_sub_modules(dp);
1245 DRM_ERROR("init sub module failed\n");
1246 return -EPROBE_DEFER;
1249 mutex_init(&dp->event_mutex);
1250 g_dp_display = &dp->dp_display;
1252 /* Store DP audio handle inside DP display */
1253 g_dp_display->dp_audio = dp->audio;
1255 init_completion(&dp->audio_comp);
1257 platform_set_drvdata(pdev, g_dp_display);
1259 rc = component_add(&pdev->dev, &dp_display_comp_ops);
1261 DRM_ERROR("component add failed, rc=%d\n", rc);
1262 dp_display_deinit_sub_modules(dp);
1268 static int dp_display_remove(struct platform_device *pdev)
1270 struct dp_display_private *dp;
1272 dp = container_of(g_dp_display,
1273 struct dp_display_private, dp_display);
1275 dp_display_deinit_sub_modules(dp);
1277 component_del(&pdev->dev, &dp_display_comp_ops);
1278 platform_set_drvdata(pdev, NULL);
1283 static int dp_pm_resume(struct device *dev)
1285 struct platform_device *pdev = to_platform_device(dev);
1286 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1287 struct dp_display_private *dp;
1290 dp = container_of(dp_display, struct dp_display_private, dp_display);
1292 mutex_lock(&dp->event_mutex);
1294 /* start from disconnected state */
1295 dp->hpd_state = ST_DISCONNECTED;
1297 /* turn on dp ctrl/phy */
1298 dp_display_host_init(dp, true);
1300 dp_catalog_ctrl_hpd_config(dp->catalog);
1302 status = dp_catalog_link_is_connected(dp->catalog);
1305 * can not declared display is connected unless
1306 * HDMI cable is plugged in and sink_count of
1309 if (status && dp->link->sink_count)
1310 dp->dp_display.is_connected = true;
1312 dp->dp_display.is_connected = false;
1314 mutex_unlock(&dp->event_mutex);
1319 static int dp_pm_suspend(struct device *dev)
1321 struct platform_device *pdev = to_platform_device(dev);
1322 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1323 struct dp_display_private *dp;
1325 dp = container_of(dp_display, struct dp_display_private, dp_display);
1327 mutex_lock(&dp->event_mutex);
1329 if (dp->core_initialized == true) {
1330 /* mainlink enabled */
1331 if (dp_power_clk_status(dp->power, DP_CTRL_PM))
1332 dp_ctrl_off_link_stream(dp->ctrl);
1334 dp_display_host_deinit(dp);
1337 dp->hpd_state = ST_SUSPENDED;
1339 /* host_init will be called at pm_resume */
1340 dp->core_initialized = false;
1342 mutex_unlock(&dp->event_mutex);
1347 static int dp_pm_prepare(struct device *dev)
1352 static void dp_pm_complete(struct device *dev)
1357 static const struct dev_pm_ops dp_pm_ops = {
1358 .suspend = dp_pm_suspend,
1359 .resume = dp_pm_resume,
1360 .prepare = dp_pm_prepare,
1361 .complete = dp_pm_complete,
1364 static struct platform_driver dp_display_driver = {
1365 .probe = dp_display_probe,
1366 .remove = dp_display_remove,
1368 .name = "msm-dp-display",
1369 .of_match_table = dp_dt_match,
1370 .suppress_bind_attrs = true,
1375 int __init msm_dp_register(void)
1379 ret = platform_driver_register(&dp_display_driver);
1381 DRM_ERROR("Dp display driver register failed");
1386 void __exit msm_dp_unregister(void)
1388 platform_driver_unregister(&dp_display_driver);
1391 void msm_dp_irq_postinstall(struct msm_dp *dp_display)
1393 struct dp_display_private *dp;
1398 dp = container_of(dp_display, struct dp_display_private, dp_display);
1400 dp_hpd_event_setup(dp);
1402 dp_add_event(dp, EV_HPD_INIT_SETUP, 0, 100);
1405 void msm_dp_debugfs_init(struct msm_dp *dp_display, struct drm_minor *minor)
1407 struct dp_display_private *dp;
1411 dp = container_of(dp_display, struct dp_display_private, dp_display);
1412 dev = &dp->pdev->dev;
1414 dp->debug = dp_debug_get(dev, dp->panel, dp->usbpd,
1415 dp->link, &dp->dp_display.connector,
1417 if (IS_ERR(dp->debug)) {
1418 rc = PTR_ERR(dp->debug);
1419 DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1424 int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1425 struct drm_encoder *encoder)
1427 struct msm_drm_private *priv;
1430 if (WARN_ON(!encoder) || WARN_ON(!dp_display) || WARN_ON(!dev))
1433 priv = dev->dev_private;
1434 dp_display->drm_dev = dev;
1436 ret = dp_display_request_irq(dp_display);
1438 DRM_ERROR("request_irq failed, ret=%d\n", ret);
1442 dp_display->encoder = encoder;
1444 dp_display->connector = dp_drm_connector_init(dp_display);
1445 if (IS_ERR(dp_display->connector)) {
1446 ret = PTR_ERR(dp_display->connector);
1447 DRM_DEV_ERROR(dev->dev,
1448 "failed to create dp connector: %d\n", ret);
1449 dp_display->connector = NULL;
1453 priv->connectors[priv->num_connectors++] = dp_display->connector;
1457 int msm_dp_display_enable(struct msm_dp *dp, struct drm_encoder *encoder)
1460 struct dp_display_private *dp_display;
1463 dp_display = container_of(dp, struct dp_display_private, dp_display);
1464 if (!dp_display->dp_mode.drm_mode.clock) {
1465 DRM_ERROR("invalid params\n");
1469 mutex_lock(&dp_display->event_mutex);
1471 /* stop sentinel checking */
1472 dp_del_event(dp_display, EV_CONNECT_PENDING_TIMEOUT);
1474 rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1476 DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1477 mutex_unlock(&dp_display->event_mutex);
1481 rc = dp_display_prepare(dp);
1483 DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1484 mutex_unlock(&dp_display->event_mutex);
1488 state = dp_display->hpd_state;
1490 if (state == ST_DISPLAY_OFF)
1491 dp_display_host_init(dp_display, true);
1493 dp_display_enable(dp_display, 0);
1495 rc = dp_display_post_enable(dp);
1497 DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1498 dp_display_disable(dp_display, 0);
1499 dp_display_unprepare(dp);
1502 /* manual kick off plug event to train link */
1503 if (state == ST_DISPLAY_OFF)
1504 dp_add_event(dp_display, EV_IRQ_HPD_INT, 0, 0);
1506 /* completed connection */
1507 dp_display->hpd_state = ST_CONNECTED;
1509 mutex_unlock(&dp_display->event_mutex);
1514 int msm_dp_display_pre_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1516 struct dp_display_private *dp_display;
1518 dp_display = container_of(dp, struct dp_display_private, dp_display);
1520 dp_ctrl_push_idle(dp_display->ctrl);
1525 int msm_dp_display_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1529 struct dp_display_private *dp_display;
1531 dp_display = container_of(dp, struct dp_display_private, dp_display);
1533 mutex_lock(&dp_display->event_mutex);
1535 /* stop sentinel checking */
1536 dp_del_event(dp_display, EV_DISCONNECT_PENDING_TIMEOUT);
1538 dp_display_disable(dp_display, 0);
1540 rc = dp_display_unprepare(dp);
1542 DRM_ERROR("DP display unprepare failed, rc=%d\n", rc);
1544 state = dp_display->hpd_state;
1545 if (state == ST_DISCONNECT_PENDING) {
1546 /* completed disconnection */
1547 dp_display->hpd_state = ST_DISCONNECTED;
1549 dp_display->hpd_state = ST_DISPLAY_OFF;
1552 mutex_unlock(&dp_display->event_mutex);
1556 void msm_dp_display_mode_set(struct msm_dp *dp, struct drm_encoder *encoder,
1557 struct drm_display_mode *mode,
1558 struct drm_display_mode *adjusted_mode)
1560 struct dp_display_private *dp_display;
1562 dp_display = container_of(dp, struct dp_display_private, dp_display);
1564 memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1566 if (dp_display_check_video_test(dp))
1567 dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1568 else /* Default num_components per pixel = 3 */
1569 dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1571 if (!dp_display->dp_mode.bpp)
1572 dp_display->dp_mode.bpp = 24; /* Default bpp */
1574 drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1576 dp_display->dp_mode.v_active_low =
1577 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1579 dp_display->dp_mode.h_active_low =
1580 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);