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,
60 EV_CONNECT_PENDING_TIMEOUT,
61 EV_DISCONNECT_PENDING_TIMEOUT,
64 #define EVENT_TIMEOUT (HZ/10) /* 100ms */
65 #define DP_EVENT_Q_MAX 8
67 #define DP_TIMEOUT_5_SECOND (5000/EVENT_TIMEOUT)
68 #define DP_TIMEOUT_NONE 0
70 #define WAIT_FOR_RESUME_TIMEOUT_JIFFIES (HZ / 2)
78 struct dp_display_private {
83 bool core_initialized;
87 struct platform_device *pdev;
90 struct dp_usbpd *usbpd;
91 struct dp_parser *parser;
92 struct dp_power *power;
93 struct dp_catalog *catalog;
94 struct drm_dp_aux *aux;
96 struct dp_panel *panel;
98 struct dp_debug *debug;
100 struct dp_usbpd_cb usbpd_cb;
101 struct dp_display_mode dp_mode;
102 struct msm_dp dp_display;
104 /* wait for audio signaling */
105 struct completion audio_comp;
107 /* event related only access by event thread */
108 struct mutex event_mutex;
109 wait_queue_head_t event_q;
113 struct dp_event event_list[DP_EVENT_Q_MAX];
114 spinlock_t event_lock;
116 struct dp_audio *audio;
119 static const struct of_device_id dp_dt_match[] = {
120 {.compatible = "qcom,sc7180-dp"},
124 static int dp_add_event(struct dp_display_private *dp_priv, u32 event,
128 struct dp_event *todo;
131 spin_lock_irqsave(&dp_priv->event_lock, flag);
132 pndx = dp_priv->event_pndx + 1;
133 pndx %= DP_EVENT_Q_MAX;
134 if (pndx == dp_priv->event_gndx) {
135 pr_err("event_q is full: pndx=%d gndx=%d\n",
136 dp_priv->event_pndx, dp_priv->event_gndx);
137 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
140 todo = &dp_priv->event_list[dp_priv->event_pndx++];
141 dp_priv->event_pndx %= DP_EVENT_Q_MAX;
142 todo->event_id = event;
145 wake_up(&dp_priv->event_q);
146 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
151 static int dp_del_event(struct dp_display_private *dp_priv, u32 event)
154 struct dp_event *todo;
157 spin_lock_irqsave(&dp_priv->event_lock, flag);
158 if (dp_priv->event_pndx == dp_priv->event_gndx) {
159 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
163 gndx = dp_priv->event_gndx;
164 while (dp_priv->event_pndx != gndx) {
165 todo = &dp_priv->event_list[gndx];
166 if (todo->event_id == event) {
167 todo->event_id = EV_NO_EVENT; /* deleted */
171 gndx %= DP_EVENT_Q_MAX;
173 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
178 void dp_display_signal_audio_start(struct msm_dp *dp_display)
180 struct dp_display_private *dp;
182 dp = container_of(dp_display, struct dp_display_private, dp_display);
184 reinit_completion(&dp->audio_comp);
187 void dp_display_signal_audio_complete(struct msm_dp *dp_display)
189 struct dp_display_private *dp;
191 dp = container_of(dp_display, struct dp_display_private, dp_display);
193 complete_all(&dp->audio_comp);
196 static int dp_display_bind(struct device *dev, struct device *master,
200 struct dp_display_private *dp;
201 struct drm_device *drm;
202 struct msm_drm_private *priv;
204 drm = dev_get_drvdata(master);
206 dp = container_of(g_dp_display,
207 struct dp_display_private, dp_display);
209 dp->dp_display.drm_dev = drm;
210 priv = drm->dev_private;
211 priv->dp = &(dp->dp_display);
213 rc = dp->parser->parse(dp->parser);
215 DRM_ERROR("device tree parsing failed\n");
219 dp->aux->drm_dev = drm;
220 rc = dp_aux_register(dp->aux);
222 DRM_ERROR("DRM DP AUX register failed\n");
226 rc = dp_power_client_init(dp->power);
228 DRM_ERROR("Power client create failed\n");
232 rc = dp_register_audio_driver(dev, dp->audio);
234 DRM_ERROR("Audio registration Dp failed\n");
240 static void dp_display_unbind(struct device *dev, struct device *master,
243 struct dp_display_private *dp;
244 struct drm_device *drm = dev_get_drvdata(master);
245 struct msm_drm_private *priv = drm->dev_private;
247 dp = container_of(g_dp_display,
248 struct dp_display_private, dp_display);
250 dp_power_client_deinit(dp->power);
251 dp_aux_unregister(dp->aux);
255 static const struct component_ops dp_display_comp_ops = {
256 .bind = dp_display_bind,
257 .unbind = dp_display_unbind,
260 static bool dp_display_is_ds_bridge(struct dp_panel *panel)
262 return (panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
263 DP_DWN_STRM_PORT_PRESENT);
266 static bool dp_display_is_sink_count_zero(struct dp_display_private *dp)
268 DRM_DEBUG_DP("present=%#x sink_count=%d\n", dp->panel->dpcd[DP_DOWNSTREAMPORT_PRESENT],
269 dp->link->sink_count);
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 int dp_display_send_hpd_notification(struct dp_display_private *dp,
289 if ((hpd && dp->dp_display.is_connected) ||
290 (!hpd && !dp->dp_display.is_connected)) {
291 DRM_DEBUG_DP("HPD already %s\n", (hpd ? "on" : "off"));
295 /* reset video pattern flag on disconnect */
297 dp->panel->video_test = false;
299 dp->dp_display.is_connected = hpd;
301 DRM_DEBUG_DP("hpd=%d\n", hpd);
302 dp_display_send_hpd_event(&dp->dp_display);
307 static int dp_display_process_hpd_high(struct dp_display_private *dp)
312 dp->panel->max_dp_lanes = dp->parser->max_dp_lanes;
314 rc = dp_panel_read_sink_caps(dp->panel, dp->dp_display.connector);
318 dp_link_process_request(dp->link);
320 edid = dp->panel->edid;
322 dp->audio_supported = drm_detect_monitor_audio(edid);
323 dp_panel_handle_sink_request(dp->panel);
325 dp->dp_display.max_pclk_khz = DP_MAX_PIXEL_CLK_KHZ;
326 dp->dp_display.max_dp_lanes = dp->parser->max_dp_lanes;
329 * set sink to normal operation mode -- D0
332 dp_link_psm_config(dp->link, &dp->panel->link_info, false);
334 dp_link_reset_phy_params_vx_px(dp->link);
335 rc = dp_ctrl_on_link(dp->ctrl);
337 DRM_ERROR("failed to complete DP link training\n");
341 dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
347 static void dp_display_host_init(struct dp_display_private *dp, int reset)
351 DRM_DEBUG_DP("core_initialized=%d\n", dp->core_initialized);
352 if (dp->core_initialized) {
353 DRM_DEBUG_DP("DP core already initialized\n");
357 if (dp->usbpd->orientation == ORIENTATION_CC2)
360 dp_power_init(dp->power, flip);
361 dp_ctrl_host_init(dp->ctrl, flip, reset);
362 dp_aux_init(dp->aux);
363 dp->core_initialized = true;
366 static void dp_display_host_deinit(struct dp_display_private *dp)
368 if (!dp->core_initialized) {
369 DRM_DEBUG_DP("DP core not initialized\n");
373 dp_ctrl_host_deinit(dp->ctrl);
374 dp_aux_deinit(dp->aux);
375 dp_power_deinit(dp->power);
377 dp->core_initialized = false;
380 static int dp_display_usbpd_configure_cb(struct device *dev)
383 struct dp_display_private *dp;
386 DRM_ERROR("invalid dev\n");
391 dp = container_of(g_dp_display,
392 struct dp_display_private, dp_display);
394 dp_display_host_init(dp, false);
396 rc = dp_display_process_hpd_high(dp);
401 static int dp_display_usbpd_disconnect_cb(struct device *dev)
404 struct dp_display_private *dp;
407 DRM_ERROR("invalid dev\n");
412 dp = container_of(g_dp_display,
413 struct dp_display_private, dp_display);
415 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
420 static void dp_display_handle_video_request(struct dp_display_private *dp)
422 if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
423 dp->panel->video_test = true;
424 dp_link_send_test_response(dp->link);
428 static int dp_display_handle_port_ststus_changed(struct dp_display_private *dp)
432 if (dp_display_is_sink_count_zero(dp)) {
433 DRM_DEBUG_DP("sink count is zero, nothing to do\n");
434 if (dp->hpd_state != ST_DISCONNECTED) {
435 dp->hpd_state = ST_DISCONNECT_PENDING;
436 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
439 if (dp->hpd_state == ST_DISCONNECTED) {
440 dp->hpd_state = ST_CONNECT_PENDING;
441 rc = dp_display_process_hpd_high(dp);
443 dp->hpd_state = ST_DISCONNECTED;
450 static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
452 u32 sink_request = dp->link->sink_request;
454 DRM_DEBUG_DP("%d\n", sink_request);
455 if (dp->hpd_state == ST_DISCONNECTED) {
456 if (sink_request & DP_LINK_STATUS_UPDATED) {
457 DRM_DEBUG_DP("Disconnected sink_request: %d\n", sink_request);
458 DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
463 dp_ctrl_handle_sink_request(dp->ctrl);
465 if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
466 dp_display_handle_video_request(dp);
471 static int dp_display_usbpd_attention_cb(struct device *dev)
475 struct dp_display_private *dp;
478 DRM_ERROR("invalid dev\n");
482 dp = container_of(g_dp_display,
483 struct dp_display_private, dp_display);
485 /* check for any test request issued by sink */
486 rc = dp_link_process_request(dp->link);
488 sink_request = dp->link->sink_request;
489 DRM_DEBUG_DP("hpd_state=%d sink_request=%d\n", dp->hpd_state, sink_request);
490 if (sink_request & DS_PORT_STATUS_CHANGED)
491 rc = dp_display_handle_port_ststus_changed(dp);
493 rc = dp_display_handle_irq_hpd(dp);
499 static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
501 struct dp_usbpd *hpd = dp->usbpd;
503 u32 tout = DP_TIMEOUT_5_SECOND;
509 mutex_lock(&dp->event_mutex);
511 state = dp->hpd_state;
512 DRM_DEBUG_DP("hpd_state=%d\n", state);
513 if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
514 mutex_unlock(&dp->event_mutex);
518 if (state == ST_CONNECT_PENDING || state == ST_CONNECTED) {
519 mutex_unlock(&dp->event_mutex);
523 if (state == ST_DISCONNECT_PENDING) {
524 /* wait until ST_DISCONNECTED */
525 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
526 mutex_unlock(&dp->event_mutex);
530 dp->hpd_state = ST_CONNECT_PENDING;
534 ret = dp_display_usbpd_configure_cb(&dp->pdev->dev);
535 if (ret) { /* link train failed */
537 dp->hpd_state = ST_DISCONNECTED;
539 if (ret == -ECONNRESET) { /* cable unplugged */
540 dp->core_initialized = false;
544 /* start sentinel checking in case of missing uevent */
545 dp_add_event(dp, EV_CONNECT_PENDING_TIMEOUT, 0, tout);
548 /* enable HDP irq_hpd/replug interrupt */
549 dp_catalog_hpd_config_intr(dp->catalog,
550 DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, true);
552 mutex_unlock(&dp->event_mutex);
554 /* uevent will complete connection part */
558 static int dp_display_enable(struct dp_display_private *dp, u32 data);
559 static int dp_display_disable(struct dp_display_private *dp, u32 data);
561 static int dp_connect_pending_timeout(struct dp_display_private *dp, u32 data)
565 mutex_lock(&dp->event_mutex);
567 state = dp->hpd_state;
568 if (state == ST_CONNECT_PENDING)
569 dp->hpd_state = ST_CONNECTED;
571 mutex_unlock(&dp->event_mutex);
576 static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
579 struct dp_display_private *dp;
581 dp = container_of(dp_display,
582 struct dp_display_private, dp_display);
584 /* notify audio subsystem only if sink supports audio */
585 if (dp_display->plugged_cb && dp_display->codec_dev &&
587 dp_display->plugged_cb(dp_display->codec_dev, plugged);
590 static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
592 struct dp_usbpd *hpd = dp->usbpd;
598 mutex_lock(&dp->event_mutex);
600 state = dp->hpd_state;
602 /* disable irq_hpd/replug interrupts */
603 dp_catalog_hpd_config_intr(dp->catalog,
604 DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, false);
606 /* unplugged, no more irq_hpd handle */
607 dp_del_event(dp, EV_IRQ_HPD_INT);
609 if (state == ST_DISCONNECTED) {
610 /* triggered by irq_hdp with sink_count = 0 */
611 if (dp->link->sink_count == 0) {
612 dp_ctrl_off_phy(dp->ctrl);
614 dp->core_initialized = false;
616 mutex_unlock(&dp->event_mutex);
620 if (state == ST_DISCONNECT_PENDING) {
621 mutex_unlock(&dp->event_mutex);
625 if (state == ST_CONNECT_PENDING) {
626 /* wait until CONNECTED */
627 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 1); /* delay = 1 */
628 mutex_unlock(&dp->event_mutex);
632 dp->hpd_state = ST_DISCONNECT_PENDING;
634 /* disable HPD plug interrupts */
635 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, false);
640 * We don't need separate work for disconnect as
641 * connect/attention interrupts are disabled
643 dp_display_usbpd_disconnect_cb(&dp->pdev->dev);
645 /* start sentinel checking in case of missing uevent */
646 dp_add_event(dp, EV_DISCONNECT_PENDING_TIMEOUT, 0, DP_TIMEOUT_5_SECOND);
648 DRM_DEBUG_DP("hpd_state=%d\n", state);
649 /* signal the disconnect event early to ensure proper teardown */
650 dp_display_handle_plugged_change(g_dp_display, false);
652 /* enable HDP plug interrupt to prepare for next plugin */
653 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, true);
655 /* uevent will complete disconnection part */
656 mutex_unlock(&dp->event_mutex);
660 static int dp_disconnect_pending_timeout(struct dp_display_private *dp, u32 data)
664 mutex_lock(&dp->event_mutex);
666 state = dp->hpd_state;
667 if (state == ST_DISCONNECT_PENDING)
668 dp->hpd_state = ST_DISCONNECTED;
670 mutex_unlock(&dp->event_mutex);
675 static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
680 mutex_lock(&dp->event_mutex);
682 /* irq_hpd can happen at either connected or disconnected state */
683 state = dp->hpd_state;
684 if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
685 mutex_unlock(&dp->event_mutex);
689 if (state == ST_CONNECT_PENDING) {
690 /* wait until ST_CONNECTED */
691 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
692 mutex_unlock(&dp->event_mutex);
696 if (state == ST_CONNECT_PENDING || state == ST_DISCONNECT_PENDING) {
697 /* wait until ST_CONNECTED */
698 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
699 mutex_unlock(&dp->event_mutex);
703 ret = dp_display_usbpd_attention_cb(&dp->pdev->dev);
704 if (ret == -ECONNRESET) { /* cable unplugged */
705 dp->core_initialized = false;
707 DRM_DEBUG_DP("hpd_state=%d\n", state);
709 mutex_unlock(&dp->event_mutex);
714 static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
716 dp_debug_put(dp->debug);
717 dp_panel_put(dp->panel);
719 dp_audio_put(dp->audio);
722 static int dp_init_sub_modules(struct dp_display_private *dp)
725 struct device *dev = &dp->pdev->dev;
726 struct dp_usbpd_cb *cb = &dp->usbpd_cb;
727 struct dp_panel_in panel_in = {
731 /* Callback APIs used for cable status change event */
732 cb->configure = dp_display_usbpd_configure_cb;
733 cb->disconnect = dp_display_usbpd_disconnect_cb;
734 cb->attention = dp_display_usbpd_attention_cb;
736 dp->usbpd = dp_hpd_get(dev, cb);
737 if (IS_ERR(dp->usbpd)) {
738 rc = PTR_ERR(dp->usbpd);
739 DRM_ERROR("failed to initialize hpd, rc = %d\n", rc);
744 dp->parser = dp_parser_get(dp->pdev);
745 if (IS_ERR(dp->parser)) {
746 rc = PTR_ERR(dp->parser);
747 DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
752 dp->catalog = dp_catalog_get(dev, &dp->parser->io);
753 if (IS_ERR(dp->catalog)) {
754 rc = PTR_ERR(dp->catalog);
755 DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
760 dp->power = dp_power_get(dev, dp->parser);
761 if (IS_ERR(dp->power)) {
762 rc = PTR_ERR(dp->power);
763 DRM_ERROR("failed to initialize power, rc = %d\n", rc);
768 dp->aux = dp_aux_get(dev, dp->catalog);
769 if (IS_ERR(dp->aux)) {
770 rc = PTR_ERR(dp->aux);
771 DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
776 dp->link = dp_link_get(dev, dp->aux);
777 if (IS_ERR(dp->link)) {
778 rc = PTR_ERR(dp->link);
779 DRM_ERROR("failed to initialize link, rc = %d\n", rc);
784 panel_in.aux = dp->aux;
785 panel_in.catalog = dp->catalog;
786 panel_in.link = dp->link;
788 dp->panel = dp_panel_get(&panel_in);
789 if (IS_ERR(dp->panel)) {
790 rc = PTR_ERR(dp->panel);
791 DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
796 dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
797 dp->power, dp->catalog, dp->parser);
798 if (IS_ERR(dp->ctrl)) {
799 rc = PTR_ERR(dp->ctrl);
800 DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
805 dp->audio = dp_audio_get(dp->pdev, dp->panel, dp->catalog);
806 if (IS_ERR(dp->audio)) {
807 rc = PTR_ERR(dp->audio);
808 pr_err("failed to initialize audio, rc = %d\n", rc);
816 dp_panel_put(dp->panel);
823 static int dp_display_set_mode(struct msm_dp *dp_display,
824 struct dp_display_mode *mode)
826 struct dp_display_private *dp;
828 dp = container_of(dp_display, struct dp_display_private, dp_display);
830 dp->panel->dp_mode.drm_mode = mode->drm_mode;
831 dp->panel->dp_mode.bpp = mode->bpp;
832 dp->panel->dp_mode.capabilities = mode->capabilities;
833 dp_panel_init_panel_info(dp->panel);
837 static int dp_display_prepare(struct msm_dp *dp)
842 static int dp_display_enable(struct dp_display_private *dp, u32 data)
845 struct msm_dp *dp_display;
847 dp_display = g_dp_display;
849 DRM_DEBUG_DP("sink_count=%d\n", dp->link->sink_count);
850 if (dp_display->power_on) {
851 DRM_DEBUG_DP("Link already setup, return\n");
855 rc = dp_ctrl_on_stream(dp->ctrl);
857 dp_display->power_on = true;
862 static int dp_display_post_enable(struct msm_dp *dp_display)
864 struct dp_display_private *dp;
867 dp = container_of(dp_display, struct dp_display_private, dp_display);
869 rate = dp->link->link_params.rate;
871 if (dp->audio_supported) {
872 dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
873 dp->audio->lane_count = dp->link->link_params.num_lanes;
876 /* signal the connect event late to synchronize video and display */
877 dp_display_handle_plugged_change(dp_display, true);
881 static int dp_display_disable(struct dp_display_private *dp, u32 data)
883 struct msm_dp *dp_display;
885 dp_display = g_dp_display;
887 if (!dp_display->power_on)
890 /* wait only if audio was enabled */
891 if (dp_display->audio_enabled) {
892 /* signal the disconnect event */
893 dp_display_handle_plugged_change(dp_display, false);
894 if (!wait_for_completion_timeout(&dp->audio_comp,
896 DRM_ERROR("audio comp timeout\n");
899 dp_display->audio_enabled = false;
901 /* triggered by irq_hpd with sink_count = 0 */
902 if (dp->link->sink_count == 0) {
903 dp_ctrl_off_link_stream(dp->ctrl);
905 dp_ctrl_off(dp->ctrl);
906 dp->core_initialized = false;
909 dp_display->power_on = false;
911 DRM_DEBUG_DP("sink count: %d\n", dp->link->sink_count);
915 static int dp_display_unprepare(struct msm_dp *dp)
920 int dp_display_set_plugged_cb(struct msm_dp *dp_display,
921 hdmi_codec_plugged_cb fn, struct device *codec_dev)
925 dp_display->plugged_cb = fn;
926 dp_display->codec_dev = codec_dev;
927 plugged = dp_display->is_connected;
928 dp_display_handle_plugged_change(dp_display, plugged);
933 int dp_display_validate_mode(struct msm_dp *dp, u32 mode_pclk_khz)
935 const u32 num_components = 3, default_bpp = 24;
936 struct dp_display_private *dp_display;
937 struct dp_link_info *link_info;
938 u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
940 if (!dp || !mode_pclk_khz || !dp->connector) {
941 DRM_ERROR("invalid params\n");
945 dp_display = container_of(dp, struct dp_display_private, dp_display);
946 link_info = &dp_display->panel->link_info;
948 mode_bpp = dp->connector->display_info.bpc * num_components;
950 mode_bpp = default_bpp;
952 mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
953 mode_bpp, mode_pclk_khz);
955 mode_rate_khz = mode_pclk_khz * mode_bpp;
956 supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
958 if (mode_rate_khz > supported_rate_khz)
964 int dp_display_get_modes(struct msm_dp *dp,
965 struct dp_display_mode *dp_mode)
967 struct dp_display_private *dp_display;
971 DRM_ERROR("invalid params\n");
975 dp_display = container_of(dp, struct dp_display_private, dp_display);
977 ret = dp_panel_get_modes(dp_display->panel,
978 dp->connector, dp_mode);
979 if (dp_mode->drm_mode.clock)
980 dp->max_pclk_khz = dp_mode->drm_mode.clock;
984 bool dp_display_check_video_test(struct msm_dp *dp)
986 struct dp_display_private *dp_display;
988 dp_display = container_of(dp, struct dp_display_private, dp_display);
990 return dp_display->panel->video_test;
993 int dp_display_get_test_bpp(struct msm_dp *dp)
995 struct dp_display_private *dp_display;
998 DRM_ERROR("invalid params\n");
1002 dp_display = container_of(dp, struct dp_display_private, dp_display);
1004 return dp_link_bit_depth_to_bpp(
1005 dp_display->link->test_video.test_bit_depth);
1008 void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
1010 struct dp_display_private *dp_display;
1012 dp_display = container_of(dp, struct dp_display_private, dp_display);
1015 * if we are reading registers we need the link clocks to be on
1016 * however till DP cable is connected this will not happen as we
1017 * do not know the resolution to power up with. Hence check the
1018 * power_on status before dumping DP registers to avoid crash due
1019 * to unclocked access
1021 mutex_lock(&dp_display->event_mutex);
1023 if (!dp->power_on) {
1024 mutex_unlock(&dp_display->event_mutex);
1028 dp_catalog_snapshot(dp_display->catalog, disp_state);
1030 mutex_unlock(&dp_display->event_mutex);
1033 static void dp_display_config_hpd(struct dp_display_private *dp)
1036 dp_display_host_init(dp, true);
1037 dp_catalog_ctrl_hpd_config(dp->catalog);
1039 /* Enable interrupt first time
1040 * we are leaving dp clocks on during disconnect
1041 * and never disable interrupt
1043 enable_irq(dp->irq);
1046 static int hpd_event_thread(void *data)
1048 struct dp_display_private *dp_priv;
1050 struct dp_event *todo;
1051 int timeout_mode = 0;
1053 dp_priv = (struct dp_display_private *)data;
1057 wait_event_timeout(dp_priv->event_q,
1058 (dp_priv->event_pndx == dp_priv->event_gndx),
1061 wait_event_interruptible(dp_priv->event_q,
1062 (dp_priv->event_pndx != dp_priv->event_gndx));
1064 spin_lock_irqsave(&dp_priv->event_lock, flag);
1065 todo = &dp_priv->event_list[dp_priv->event_gndx];
1067 struct dp_event *todo_next;
1069 dp_priv->event_gndx++;
1070 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1072 /* re enter delay event into q */
1073 todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
1074 dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1075 todo_next->event_id = todo->event_id;
1076 todo_next->data = todo->data;
1077 todo_next->delay = todo->delay - 1;
1079 /* clean up older event */
1080 todo->event_id = EV_NO_EVENT;
1083 /* switch to timeout mode */
1085 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1089 /* timeout with no events in q */
1090 if (dp_priv->event_pndx == dp_priv->event_gndx) {
1091 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1095 dp_priv->event_gndx++;
1096 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1098 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1100 switch (todo->event_id) {
1101 case EV_HPD_INIT_SETUP:
1102 dp_display_config_hpd(dp_priv);
1104 case EV_HPD_PLUG_INT:
1105 dp_hpd_plug_handle(dp_priv, todo->data);
1107 case EV_HPD_UNPLUG_INT:
1108 dp_hpd_unplug_handle(dp_priv, todo->data);
1110 case EV_IRQ_HPD_INT:
1111 dp_irq_hpd_handle(dp_priv, todo->data);
1113 case EV_USER_NOTIFICATION:
1114 dp_display_send_hpd_notification(dp_priv,
1117 case EV_CONNECT_PENDING_TIMEOUT:
1118 dp_connect_pending_timeout(dp_priv,
1121 case EV_DISCONNECT_PENDING_TIMEOUT:
1122 dp_disconnect_pending_timeout(dp_priv,
1133 static void dp_hpd_event_setup(struct dp_display_private *dp_priv)
1135 init_waitqueue_head(&dp_priv->event_q);
1136 spin_lock_init(&dp_priv->event_lock);
1138 kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1141 static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1143 struct dp_display_private *dp = dev_id;
1144 irqreturn_t ret = IRQ_HANDLED;
1148 DRM_ERROR("invalid data\n");
1152 hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1154 DRM_DEBUG_DP("hpd isr status=%#x\n", hpd_isr_status);
1155 if (hpd_isr_status & 0x0F) {
1156 /* hpd related interrupts */
1157 if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1158 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1160 if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1161 /* stop sentinel connect pending checking */
1162 dp_del_event(dp, EV_CONNECT_PENDING_TIMEOUT);
1163 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1166 if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1167 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1168 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
1171 if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1172 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1175 /* DP controller isr */
1176 dp_ctrl_isr(dp->ctrl);
1179 dp_aux_isr(dp->aux);
1184 int dp_display_request_irq(struct msm_dp *dp_display)
1187 struct dp_display_private *dp;
1190 DRM_ERROR("invalid input\n");
1194 dp = container_of(dp_display, struct dp_display_private, dp_display);
1196 dp->irq = irq_of_parse_and_map(dp->pdev->dev.of_node, 0);
1199 DRM_ERROR("failed to get irq: %d\n", rc);
1203 rc = devm_request_irq(&dp->pdev->dev, dp->irq,
1204 dp_display_irq_handler,
1205 IRQF_TRIGGER_HIGH, "dp_display_isr", dp);
1207 DRM_ERROR("failed to request IRQ%u: %d\n",
1211 disable_irq(dp->irq);
1216 static int dp_display_probe(struct platform_device *pdev)
1219 struct dp_display_private *dp;
1221 if (!pdev || !pdev->dev.of_node) {
1222 DRM_ERROR("pdev not found\n");
1226 dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1231 dp->name = "drm_dp";
1233 rc = dp_init_sub_modules(dp);
1235 DRM_ERROR("init sub module failed\n");
1236 return -EPROBE_DEFER;
1239 mutex_init(&dp->event_mutex);
1240 g_dp_display = &dp->dp_display;
1242 /* Store DP audio handle inside DP display */
1243 g_dp_display->dp_audio = dp->audio;
1245 init_completion(&dp->audio_comp);
1247 platform_set_drvdata(pdev, g_dp_display);
1249 rc = component_add(&pdev->dev, &dp_display_comp_ops);
1251 DRM_ERROR("component add failed, rc=%d\n", rc);
1252 dp_display_deinit_sub_modules(dp);
1258 static int dp_display_remove(struct platform_device *pdev)
1260 struct dp_display_private *dp;
1262 dp = container_of(g_dp_display,
1263 struct dp_display_private, dp_display);
1265 dp_display_deinit_sub_modules(dp);
1267 component_del(&pdev->dev, &dp_display_comp_ops);
1268 platform_set_drvdata(pdev, NULL);
1273 static int dp_pm_resume(struct device *dev)
1275 struct platform_device *pdev = to_platform_device(dev);
1276 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1277 struct dp_display_private *dp;
1280 dp = container_of(dp_display, struct dp_display_private, dp_display);
1282 mutex_lock(&dp->event_mutex);
1284 DRM_DEBUG_DP("Before, core_inited=%d power_on=%d\n",
1285 dp->core_initialized, dp_display->power_on);
1287 /* start from disconnected state */
1288 dp->hpd_state = ST_DISCONNECTED;
1290 /* turn on dp ctrl/phy */
1291 dp_display_host_init(dp, true);
1293 dp_catalog_ctrl_hpd_config(dp->catalog);
1296 * set sink to normal operation mode -- D0
1299 dp_link_psm_config(dp->link, &dp->panel->link_info, false);
1301 if (dp_catalog_link_is_connected(dp->catalog)) {
1302 sink_count = drm_dp_read_sink_count(dp->aux);
1307 dp->link->sink_count = sink_count;
1309 * can not declared display is connected unless
1310 * HDMI cable is plugged in and sink_count of
1313 if (dp->link->sink_count)
1314 dp->dp_display.is_connected = true;
1316 dp->dp_display.is_connected = false;
1318 dp_display_handle_plugged_change(g_dp_display,
1319 dp->dp_display.is_connected);
1321 DRM_DEBUG_DP("After, sink_count=%d is_connected=%d core_inited=%d power_on=%d\n",
1322 dp->link->sink_count, dp->dp_display.is_connected,
1323 dp->core_initialized, dp_display->power_on);
1325 mutex_unlock(&dp->event_mutex);
1330 static int dp_pm_suspend(struct device *dev)
1332 struct platform_device *pdev = to_platform_device(dev);
1333 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1334 struct dp_display_private *dp;
1336 dp = container_of(dp_display, struct dp_display_private, dp_display);
1338 mutex_lock(&dp->event_mutex);
1340 DRM_DEBUG_DP("Before, core_inited=%d power_on=%d\n",
1341 dp->core_initialized, dp_display->power_on);
1343 if (dp->core_initialized == true) {
1344 /* mainlink enabled */
1345 if (dp_power_clk_status(dp->power, DP_CTRL_PM))
1346 dp_ctrl_off_link_stream(dp->ctrl);
1348 dp_display_host_deinit(dp);
1351 dp->hpd_state = ST_SUSPENDED;
1353 /* host_init will be called at pm_resume */
1354 dp->core_initialized = false;
1356 DRM_DEBUG_DP("After, core_inited=%d power_on=%d\n",
1357 dp->core_initialized, dp_display->power_on);
1359 mutex_unlock(&dp->event_mutex);
1364 static int dp_pm_prepare(struct device *dev)
1369 static void dp_pm_complete(struct device *dev)
1374 static const struct dev_pm_ops dp_pm_ops = {
1375 .suspend = dp_pm_suspend,
1376 .resume = dp_pm_resume,
1377 .prepare = dp_pm_prepare,
1378 .complete = dp_pm_complete,
1381 static struct platform_driver dp_display_driver = {
1382 .probe = dp_display_probe,
1383 .remove = dp_display_remove,
1385 .name = "msm-dp-display",
1386 .of_match_table = dp_dt_match,
1387 .suppress_bind_attrs = true,
1392 int __init msm_dp_register(void)
1396 ret = platform_driver_register(&dp_display_driver);
1398 DRM_ERROR("Dp display driver register failed");
1403 void __exit msm_dp_unregister(void)
1405 platform_driver_unregister(&dp_display_driver);
1408 void msm_dp_irq_postinstall(struct msm_dp *dp_display)
1410 struct dp_display_private *dp;
1415 dp = container_of(dp_display, struct dp_display_private, dp_display);
1417 dp_hpd_event_setup(dp);
1419 dp_add_event(dp, EV_HPD_INIT_SETUP, 0, 100);
1422 void msm_dp_debugfs_init(struct msm_dp *dp_display, struct drm_minor *minor)
1424 struct dp_display_private *dp;
1428 dp = container_of(dp_display, struct dp_display_private, dp_display);
1429 dev = &dp->pdev->dev;
1431 dp->debug = dp_debug_get(dev, dp->panel, dp->usbpd,
1432 dp->link, &dp->dp_display.connector,
1434 if (IS_ERR(dp->debug)) {
1435 rc = PTR_ERR(dp->debug);
1436 DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1441 int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1442 struct drm_encoder *encoder)
1444 struct msm_drm_private *priv;
1447 if (WARN_ON(!encoder) || WARN_ON(!dp_display) || WARN_ON(!dev))
1450 priv = dev->dev_private;
1451 dp_display->drm_dev = dev;
1453 ret = dp_display_request_irq(dp_display);
1455 DRM_ERROR("request_irq failed, ret=%d\n", ret);
1459 dp_display->encoder = encoder;
1461 dp_display->connector = dp_drm_connector_init(dp_display);
1462 if (IS_ERR(dp_display->connector)) {
1463 ret = PTR_ERR(dp_display->connector);
1464 DRM_DEV_ERROR(dev->dev,
1465 "failed to create dp connector: %d\n", ret);
1466 dp_display->connector = NULL;
1470 priv->connectors[priv->num_connectors++] = dp_display->connector;
1474 int msm_dp_display_enable(struct msm_dp *dp, struct drm_encoder *encoder)
1477 struct dp_display_private *dp_display;
1480 dp_display = container_of(dp, struct dp_display_private, dp_display);
1481 if (!dp_display->dp_mode.drm_mode.clock) {
1482 DRM_ERROR("invalid params\n");
1486 mutex_lock(&dp_display->event_mutex);
1488 /* stop sentinel checking */
1489 dp_del_event(dp_display, EV_CONNECT_PENDING_TIMEOUT);
1491 rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1493 DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1494 mutex_unlock(&dp_display->event_mutex);
1498 rc = dp_display_prepare(dp);
1500 DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1501 mutex_unlock(&dp_display->event_mutex);
1505 state = dp_display->hpd_state;
1507 if (state == ST_DISPLAY_OFF)
1508 dp_display_host_init(dp_display, true);
1510 dp_display_enable(dp_display, 0);
1512 rc = dp_display_post_enable(dp);
1514 DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1515 dp_display_disable(dp_display, 0);
1516 dp_display_unprepare(dp);
1519 /* manual kick off plug event to train link */
1520 if (state == ST_DISPLAY_OFF)
1521 dp_add_event(dp_display, EV_IRQ_HPD_INT, 0, 0);
1523 /* completed connection */
1524 dp_display->hpd_state = ST_CONNECTED;
1526 mutex_unlock(&dp_display->event_mutex);
1531 int msm_dp_display_pre_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1533 struct dp_display_private *dp_display;
1535 dp_display = container_of(dp, struct dp_display_private, dp_display);
1537 dp_ctrl_push_idle(dp_display->ctrl);
1542 int msm_dp_display_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1546 struct dp_display_private *dp_display;
1548 dp_display = container_of(dp, struct dp_display_private, dp_display);
1550 mutex_lock(&dp_display->event_mutex);
1552 /* stop sentinel checking */
1553 dp_del_event(dp_display, EV_DISCONNECT_PENDING_TIMEOUT);
1555 dp_display_disable(dp_display, 0);
1557 rc = dp_display_unprepare(dp);
1559 DRM_ERROR("DP display unprepare failed, rc=%d\n", rc);
1561 state = dp_display->hpd_state;
1562 if (state == ST_DISCONNECT_PENDING) {
1563 /* completed disconnection */
1564 dp_display->hpd_state = ST_DISCONNECTED;
1566 dp_display->hpd_state = ST_DISPLAY_OFF;
1569 mutex_unlock(&dp_display->event_mutex);
1573 void msm_dp_display_mode_set(struct msm_dp *dp, struct drm_encoder *encoder,
1574 struct drm_display_mode *mode,
1575 struct drm_display_mode *adjusted_mode)
1577 struct dp_display_private *dp_display;
1579 dp_display = container_of(dp, struct dp_display_private, dp_display);
1581 memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1583 if (dp_display_check_video_test(dp))
1584 dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1585 else /* Default num_components per pixel = 3 */
1586 dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1588 if (!dp_display->dp_mode.bpp)
1589 dp_display->dp_mode.bpp = 24; /* Default bpp */
1591 drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1593 dp_display->dp_mode.v_active_low =
1594 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1596 dp_display->dp_mode.h_active_low =
1597 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);