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 rc = dp_aux_register(dp->aux);
221 DRM_ERROR("DRM DP AUX register failed\n");
225 rc = dp_power_client_init(dp->power);
227 DRM_ERROR("Power client create failed\n");
231 rc = dp_register_audio_driver(dev, dp->audio);
233 DRM_ERROR("Audio registration Dp failed\n");
239 static void dp_display_unbind(struct device *dev, struct device *master,
242 struct dp_display_private *dp;
243 struct drm_device *drm = dev_get_drvdata(master);
244 struct msm_drm_private *priv = drm->dev_private;
246 dp = container_of(g_dp_display,
247 struct dp_display_private, dp_display);
249 dp_power_client_deinit(dp->power);
250 dp_aux_unregister(dp->aux);
254 static const struct component_ops dp_display_comp_ops = {
255 .bind = dp_display_bind,
256 .unbind = dp_display_unbind,
259 static bool dp_display_is_ds_bridge(struct dp_panel *panel)
261 return (panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
262 DP_DWN_STRM_PORT_PRESENT);
265 static bool dp_display_is_sink_count_zero(struct dp_display_private *dp)
267 DRM_DEBUG_DP("present=%#x sink_count=%d\n", dp->panel->dpcd[DP_DOWNSTREAMPORT_PRESENT],
268 dp->link->sink_count);
269 return dp_display_is_ds_bridge(dp->panel) &&
270 (dp->link->sink_count == 0);
273 static void dp_display_send_hpd_event(struct msm_dp *dp_display)
275 struct dp_display_private *dp;
276 struct drm_connector *connector;
278 dp = container_of(dp_display, struct dp_display_private, dp_display);
280 connector = dp->dp_display.connector;
281 drm_helper_hpd_irq_event(connector->dev);
285 static int dp_display_send_hpd_notification(struct dp_display_private *dp,
288 if ((hpd && dp->dp_display.is_connected) ||
289 (!hpd && !dp->dp_display.is_connected)) {
290 DRM_DEBUG_DP("HPD already %s\n", (hpd ? "on" : "off"));
294 /* reset video pattern flag on disconnect */
296 dp->panel->video_test = false;
298 dp->dp_display.is_connected = hpd;
300 DRM_DEBUG_DP("hpd=%d\n", hpd);
301 dp_display_send_hpd_event(&dp->dp_display);
306 static int dp_display_process_hpd_high(struct dp_display_private *dp)
311 dp->panel->max_dp_lanes = dp->parser->max_dp_lanes;
313 rc = dp_panel_read_sink_caps(dp->panel, dp->dp_display.connector);
317 dp_link_process_request(dp->link);
319 edid = dp->panel->edid;
321 dp->audio_supported = drm_detect_monitor_audio(edid);
322 dp_panel_handle_sink_request(dp->panel);
324 dp->dp_display.max_pclk_khz = DP_MAX_PIXEL_CLK_KHZ;
325 dp->dp_display.max_dp_lanes = dp->parser->max_dp_lanes;
328 * set sink to normal operation mode -- D0
331 dp_link_psm_config(dp->link, &dp->panel->link_info, false);
333 dp_link_reset_phy_params_vx_px(dp->link);
334 rc = dp_ctrl_on_link(dp->ctrl);
336 DRM_ERROR("failed to complete DP link training\n");
340 dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
346 static void dp_display_host_init(struct dp_display_private *dp, int reset)
350 DRM_DEBUG_DP("core_initialized=%d\n", dp->core_initialized);
351 if (dp->core_initialized) {
352 DRM_DEBUG_DP("DP core already initialized\n");
356 if (dp->usbpd->orientation == ORIENTATION_CC2)
359 dp_power_init(dp->power, flip);
360 dp_ctrl_host_init(dp->ctrl, flip, reset);
361 dp_aux_init(dp->aux);
362 dp->core_initialized = true;
365 static void dp_display_host_deinit(struct dp_display_private *dp)
367 if (!dp->core_initialized) {
368 DRM_DEBUG_DP("DP core not initialized\n");
372 dp_ctrl_host_deinit(dp->ctrl);
373 dp_aux_deinit(dp->aux);
374 dp_power_deinit(dp->power);
376 dp->core_initialized = false;
379 static int dp_display_usbpd_configure_cb(struct device *dev)
382 struct dp_display_private *dp;
385 DRM_ERROR("invalid dev\n");
390 dp = container_of(g_dp_display,
391 struct dp_display_private, dp_display);
393 dp_display_host_init(dp, false);
395 rc = dp_display_process_hpd_high(dp);
400 static int dp_display_usbpd_disconnect_cb(struct device *dev)
403 struct dp_display_private *dp;
406 DRM_ERROR("invalid dev\n");
411 dp = container_of(g_dp_display,
412 struct dp_display_private, dp_display);
414 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
419 static void dp_display_handle_video_request(struct dp_display_private *dp)
421 if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
422 dp->panel->video_test = true;
423 dp_link_send_test_response(dp->link);
427 static int dp_display_handle_port_ststus_changed(struct dp_display_private *dp)
431 if (dp_display_is_sink_count_zero(dp)) {
432 DRM_DEBUG_DP("sink count is zero, nothing to do\n");
433 if (dp->hpd_state != ST_DISCONNECTED) {
434 dp->hpd_state = ST_DISCONNECT_PENDING;
435 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
438 if (dp->hpd_state == ST_DISCONNECTED) {
439 dp->hpd_state = ST_CONNECT_PENDING;
440 rc = dp_display_process_hpd_high(dp);
442 dp->hpd_state = ST_DISCONNECTED;
449 static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
451 u32 sink_request = dp->link->sink_request;
453 DRM_DEBUG_DP("%d\n", sink_request);
454 if (dp->hpd_state == ST_DISCONNECTED) {
455 if (sink_request & DP_LINK_STATUS_UPDATED) {
456 DRM_DEBUG_DP("Disconnected sink_request: %d\n", sink_request);
457 DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
462 dp_ctrl_handle_sink_request(dp->ctrl);
464 if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
465 dp_display_handle_video_request(dp);
470 static int dp_display_usbpd_attention_cb(struct device *dev)
474 struct dp_display_private *dp;
477 DRM_ERROR("invalid dev\n");
481 dp = container_of(g_dp_display,
482 struct dp_display_private, dp_display);
484 /* check for any test request issued by sink */
485 rc = dp_link_process_request(dp->link);
487 sink_request = dp->link->sink_request;
488 DRM_DEBUG_DP("hpd_state=%d sink_request=%d\n", dp->hpd_state, sink_request);
489 if (sink_request & DS_PORT_STATUS_CHANGED)
490 rc = dp_display_handle_port_ststus_changed(dp);
492 rc = dp_display_handle_irq_hpd(dp);
498 static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
500 struct dp_usbpd *hpd = dp->usbpd;
502 u32 tout = DP_TIMEOUT_5_SECOND;
508 mutex_lock(&dp->event_mutex);
510 state = dp->hpd_state;
511 DRM_DEBUG_DP("hpd_state=%d\n", state);
512 if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
513 mutex_unlock(&dp->event_mutex);
517 if (state == ST_CONNECT_PENDING || state == ST_CONNECTED) {
518 mutex_unlock(&dp->event_mutex);
522 if (state == ST_DISCONNECT_PENDING) {
523 /* wait until ST_DISCONNECTED */
524 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
525 mutex_unlock(&dp->event_mutex);
529 dp->hpd_state = ST_CONNECT_PENDING;
533 ret = dp_display_usbpd_configure_cb(&dp->pdev->dev);
534 if (ret) { /* link train failed */
536 dp->hpd_state = ST_DISCONNECTED;
538 if (ret == -ECONNRESET) { /* cable unplugged */
539 dp->core_initialized = false;
543 /* start sentinel checking in case of missing uevent */
544 dp_add_event(dp, EV_CONNECT_PENDING_TIMEOUT, 0, tout);
547 /* enable HDP irq_hpd/replug interrupt */
548 dp_catalog_hpd_config_intr(dp->catalog,
549 DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, true);
551 mutex_unlock(&dp->event_mutex);
553 /* uevent will complete connection part */
557 static int dp_display_enable(struct dp_display_private *dp, u32 data);
558 static int dp_display_disable(struct dp_display_private *dp, u32 data);
560 static int dp_connect_pending_timeout(struct dp_display_private *dp, u32 data)
564 mutex_lock(&dp->event_mutex);
566 state = dp->hpd_state;
567 if (state == ST_CONNECT_PENDING)
568 dp->hpd_state = ST_CONNECTED;
570 mutex_unlock(&dp->event_mutex);
575 static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
578 struct dp_display_private *dp;
580 dp = container_of(dp_display,
581 struct dp_display_private, dp_display);
583 /* notify audio subsystem only if sink supports audio */
584 if (dp_display->plugged_cb && dp_display->codec_dev &&
586 dp_display->plugged_cb(dp_display->codec_dev, plugged);
589 static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
591 struct dp_usbpd *hpd = dp->usbpd;
597 mutex_lock(&dp->event_mutex);
599 state = dp->hpd_state;
601 /* disable irq_hpd/replug interrupts */
602 dp_catalog_hpd_config_intr(dp->catalog,
603 DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, false);
605 /* unplugged, no more irq_hpd handle */
606 dp_del_event(dp, EV_IRQ_HPD_INT);
608 if (state == ST_DISCONNECTED) {
609 /* triggered by irq_hdp with sink_count = 0 */
610 if (dp->link->sink_count == 0) {
611 dp_ctrl_off_phy(dp->ctrl);
613 dp->core_initialized = false;
615 mutex_unlock(&dp->event_mutex);
619 if (state == ST_DISCONNECT_PENDING) {
620 mutex_unlock(&dp->event_mutex);
624 if (state == ST_CONNECT_PENDING) {
625 /* wait until CONNECTED */
626 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 1); /* delay = 1 */
627 mutex_unlock(&dp->event_mutex);
631 dp->hpd_state = ST_DISCONNECT_PENDING;
633 /* disable HPD plug interrupts */
634 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, false);
639 * We don't need separate work for disconnect as
640 * connect/attention interrupts are disabled
642 dp_display_usbpd_disconnect_cb(&dp->pdev->dev);
644 /* start sentinel checking in case of missing uevent */
645 dp_add_event(dp, EV_DISCONNECT_PENDING_TIMEOUT, 0, DP_TIMEOUT_5_SECOND);
647 DRM_DEBUG_DP("hpd_state=%d\n", state);
648 /* signal the disconnect event early to ensure proper teardown */
649 dp_display_handle_plugged_change(g_dp_display, false);
651 /* enable HDP plug interrupt to prepare for next plugin */
652 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, true);
654 /* uevent will complete disconnection part */
655 mutex_unlock(&dp->event_mutex);
659 static int dp_disconnect_pending_timeout(struct dp_display_private *dp, u32 data)
663 mutex_lock(&dp->event_mutex);
665 state = dp->hpd_state;
666 if (state == ST_DISCONNECT_PENDING)
667 dp->hpd_state = ST_DISCONNECTED;
669 mutex_unlock(&dp->event_mutex);
674 static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
679 mutex_lock(&dp->event_mutex);
681 /* irq_hpd can happen at either connected or disconnected state */
682 state = dp->hpd_state;
683 if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
684 mutex_unlock(&dp->event_mutex);
688 if (state == ST_CONNECT_PENDING) {
689 /* wait until ST_CONNECTED */
690 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
691 mutex_unlock(&dp->event_mutex);
695 if (state == ST_CONNECT_PENDING || state == ST_DISCONNECT_PENDING) {
696 /* wait until ST_CONNECTED */
697 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
698 mutex_unlock(&dp->event_mutex);
702 ret = dp_display_usbpd_attention_cb(&dp->pdev->dev);
703 if (ret == -ECONNRESET) { /* cable unplugged */
704 dp->core_initialized = false;
706 DRM_DEBUG_DP("hpd_state=%d\n", state);
708 mutex_unlock(&dp->event_mutex);
713 static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
715 dp_debug_put(dp->debug);
716 dp_panel_put(dp->panel);
718 dp_audio_put(dp->audio);
721 static int dp_init_sub_modules(struct dp_display_private *dp)
724 struct device *dev = &dp->pdev->dev;
725 struct dp_usbpd_cb *cb = &dp->usbpd_cb;
726 struct dp_panel_in panel_in = {
730 /* Callback APIs used for cable status change event */
731 cb->configure = dp_display_usbpd_configure_cb;
732 cb->disconnect = dp_display_usbpd_disconnect_cb;
733 cb->attention = dp_display_usbpd_attention_cb;
735 dp->usbpd = dp_hpd_get(dev, cb);
736 if (IS_ERR(dp->usbpd)) {
737 rc = PTR_ERR(dp->usbpd);
738 DRM_ERROR("failed to initialize hpd, rc = %d\n", rc);
743 dp->parser = dp_parser_get(dp->pdev);
744 if (IS_ERR(dp->parser)) {
745 rc = PTR_ERR(dp->parser);
746 DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
751 dp->catalog = dp_catalog_get(dev, &dp->parser->io);
752 if (IS_ERR(dp->catalog)) {
753 rc = PTR_ERR(dp->catalog);
754 DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
759 dp->power = dp_power_get(dev, dp->parser);
760 if (IS_ERR(dp->power)) {
761 rc = PTR_ERR(dp->power);
762 DRM_ERROR("failed to initialize power, rc = %d\n", rc);
767 dp->aux = dp_aux_get(dev, dp->catalog);
768 if (IS_ERR(dp->aux)) {
769 rc = PTR_ERR(dp->aux);
770 DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
775 dp->link = dp_link_get(dev, dp->aux);
776 if (IS_ERR(dp->link)) {
777 rc = PTR_ERR(dp->link);
778 DRM_ERROR("failed to initialize link, rc = %d\n", rc);
783 panel_in.aux = dp->aux;
784 panel_in.catalog = dp->catalog;
785 panel_in.link = dp->link;
787 dp->panel = dp_panel_get(&panel_in);
788 if (IS_ERR(dp->panel)) {
789 rc = PTR_ERR(dp->panel);
790 DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
795 dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
796 dp->power, dp->catalog, dp->parser);
797 if (IS_ERR(dp->ctrl)) {
798 rc = PTR_ERR(dp->ctrl);
799 DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
804 dp->audio = dp_audio_get(dp->pdev, dp->panel, dp->catalog);
805 if (IS_ERR(dp->audio)) {
806 rc = PTR_ERR(dp->audio);
807 pr_err("failed to initialize audio, rc = %d\n", rc);
815 dp_panel_put(dp->panel);
822 static int dp_display_set_mode(struct msm_dp *dp_display,
823 struct dp_display_mode *mode)
825 struct dp_display_private *dp;
827 dp = container_of(dp_display, struct dp_display_private, dp_display);
829 dp->panel->dp_mode.drm_mode = mode->drm_mode;
830 dp->panel->dp_mode.bpp = mode->bpp;
831 dp->panel->dp_mode.capabilities = mode->capabilities;
832 dp_panel_init_panel_info(dp->panel);
836 static int dp_display_prepare(struct msm_dp *dp)
841 static int dp_display_enable(struct dp_display_private *dp, u32 data)
844 struct msm_dp *dp_display;
846 dp_display = g_dp_display;
848 DRM_DEBUG_DP("sink_count=%d\n", dp->link->sink_count);
849 if (dp_display->power_on) {
850 DRM_DEBUG_DP("Link already setup, return\n");
854 rc = dp_ctrl_on_stream(dp->ctrl);
856 dp_display->power_on = true;
861 static int dp_display_post_enable(struct msm_dp *dp_display)
863 struct dp_display_private *dp;
866 dp = container_of(dp_display, struct dp_display_private, dp_display);
868 rate = dp->link->link_params.rate;
870 if (dp->audio_supported) {
871 dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
872 dp->audio->lane_count = dp->link->link_params.num_lanes;
875 /* signal the connect event late to synchronize video and display */
876 dp_display_handle_plugged_change(dp_display, true);
880 static int dp_display_disable(struct dp_display_private *dp, u32 data)
882 struct msm_dp *dp_display;
884 dp_display = g_dp_display;
886 if (!dp_display->power_on)
889 /* wait only if audio was enabled */
890 if (dp_display->audio_enabled) {
891 /* signal the disconnect event */
892 dp_display_handle_plugged_change(dp_display, false);
893 if (!wait_for_completion_timeout(&dp->audio_comp,
895 DRM_ERROR("audio comp timeout\n");
898 dp_display->audio_enabled = false;
900 /* triggered by irq_hpd with sink_count = 0 */
901 if (dp->link->sink_count == 0) {
902 dp_ctrl_off_link_stream(dp->ctrl);
904 dp_ctrl_off(dp->ctrl);
905 dp->core_initialized = false;
908 dp_display->power_on = false;
910 DRM_DEBUG_DP("sink count: %d\n", dp->link->sink_count);
914 static int dp_display_unprepare(struct msm_dp *dp)
919 int dp_display_set_plugged_cb(struct msm_dp *dp_display,
920 hdmi_codec_plugged_cb fn, struct device *codec_dev)
924 dp_display->plugged_cb = fn;
925 dp_display->codec_dev = codec_dev;
926 plugged = dp_display->is_connected;
927 dp_display_handle_plugged_change(dp_display, plugged);
932 int dp_display_validate_mode(struct msm_dp *dp, u32 mode_pclk_khz)
934 const u32 num_components = 3, default_bpp = 24;
935 struct dp_display_private *dp_display;
936 struct dp_link_info *link_info;
937 u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
939 if (!dp || !mode_pclk_khz || !dp->connector) {
940 DRM_ERROR("invalid params\n");
944 dp_display = container_of(dp, struct dp_display_private, dp_display);
945 link_info = &dp_display->panel->link_info;
947 mode_bpp = dp->connector->display_info.bpc * num_components;
949 mode_bpp = default_bpp;
951 mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
952 mode_bpp, mode_pclk_khz);
954 mode_rate_khz = mode_pclk_khz * mode_bpp;
955 supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
957 if (mode_rate_khz > supported_rate_khz)
963 int dp_display_get_modes(struct msm_dp *dp,
964 struct dp_display_mode *dp_mode)
966 struct dp_display_private *dp_display;
970 DRM_ERROR("invalid params\n");
974 dp_display = container_of(dp, struct dp_display_private, dp_display);
976 ret = dp_panel_get_modes(dp_display->panel,
977 dp->connector, dp_mode);
978 if (dp_mode->drm_mode.clock)
979 dp->max_pclk_khz = dp_mode->drm_mode.clock;
983 bool dp_display_check_video_test(struct msm_dp *dp)
985 struct dp_display_private *dp_display;
987 dp_display = container_of(dp, struct dp_display_private, dp_display);
989 return dp_display->panel->video_test;
992 int dp_display_get_test_bpp(struct msm_dp *dp)
994 struct dp_display_private *dp_display;
997 DRM_ERROR("invalid params\n");
1001 dp_display = container_of(dp, struct dp_display_private, dp_display);
1003 return dp_link_bit_depth_to_bpp(
1004 dp_display->link->test_video.test_bit_depth);
1007 void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
1009 struct dp_display_private *dp_display;
1011 dp_display = container_of(dp, struct dp_display_private, dp_display);
1014 * if we are reading registers we need the link clocks to be on
1015 * however till DP cable is connected this will not happen as we
1016 * do not know the resolution to power up with. Hence check the
1017 * power_on status before dumping DP registers to avoid crash due
1018 * to unclocked access
1020 mutex_lock(&dp_display->event_mutex);
1022 if (!dp->power_on) {
1023 mutex_unlock(&dp_display->event_mutex);
1027 dp_catalog_snapshot(dp_display->catalog, disp_state);
1029 mutex_unlock(&dp_display->event_mutex);
1032 static void dp_display_config_hpd(struct dp_display_private *dp)
1035 dp_display_host_init(dp, true);
1036 dp_catalog_ctrl_hpd_config(dp->catalog);
1038 /* Enable interrupt first time
1039 * we are leaving dp clocks on during disconnect
1040 * and never disable interrupt
1042 enable_irq(dp->irq);
1045 static int hpd_event_thread(void *data)
1047 struct dp_display_private *dp_priv;
1049 struct dp_event *todo;
1050 int timeout_mode = 0;
1052 dp_priv = (struct dp_display_private *)data;
1056 wait_event_timeout(dp_priv->event_q,
1057 (dp_priv->event_pndx == dp_priv->event_gndx),
1060 wait_event_interruptible(dp_priv->event_q,
1061 (dp_priv->event_pndx != dp_priv->event_gndx));
1063 spin_lock_irqsave(&dp_priv->event_lock, flag);
1064 todo = &dp_priv->event_list[dp_priv->event_gndx];
1066 struct dp_event *todo_next;
1068 dp_priv->event_gndx++;
1069 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1071 /* re enter delay event into q */
1072 todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
1073 dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1074 todo_next->event_id = todo->event_id;
1075 todo_next->data = todo->data;
1076 todo_next->delay = todo->delay - 1;
1078 /* clean up older event */
1079 todo->event_id = EV_NO_EVENT;
1082 /* switch to timeout mode */
1084 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1088 /* timeout with no events in q */
1089 if (dp_priv->event_pndx == dp_priv->event_gndx) {
1090 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1094 dp_priv->event_gndx++;
1095 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1097 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1099 switch (todo->event_id) {
1100 case EV_HPD_INIT_SETUP:
1101 dp_display_config_hpd(dp_priv);
1103 case EV_HPD_PLUG_INT:
1104 dp_hpd_plug_handle(dp_priv, todo->data);
1106 case EV_HPD_UNPLUG_INT:
1107 dp_hpd_unplug_handle(dp_priv, todo->data);
1109 case EV_IRQ_HPD_INT:
1110 dp_irq_hpd_handle(dp_priv, todo->data);
1112 case EV_USER_NOTIFICATION:
1113 dp_display_send_hpd_notification(dp_priv,
1116 case EV_CONNECT_PENDING_TIMEOUT:
1117 dp_connect_pending_timeout(dp_priv,
1120 case EV_DISCONNECT_PENDING_TIMEOUT:
1121 dp_disconnect_pending_timeout(dp_priv,
1132 static void dp_hpd_event_setup(struct dp_display_private *dp_priv)
1134 init_waitqueue_head(&dp_priv->event_q);
1135 spin_lock_init(&dp_priv->event_lock);
1137 kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1140 static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1142 struct dp_display_private *dp = dev_id;
1143 irqreturn_t ret = IRQ_HANDLED;
1147 DRM_ERROR("invalid data\n");
1151 hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1153 DRM_DEBUG_DP("hpd isr status=%#x\n", hpd_isr_status);
1154 if (hpd_isr_status & 0x0F) {
1155 /* hpd related interrupts */
1156 if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1157 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1159 if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1160 /* stop sentinel connect pending checking */
1161 dp_del_event(dp, EV_CONNECT_PENDING_TIMEOUT);
1162 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1165 if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1166 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1167 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
1170 if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1171 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1174 /* DP controller isr */
1175 dp_ctrl_isr(dp->ctrl);
1178 dp_aux_isr(dp->aux);
1183 int dp_display_request_irq(struct msm_dp *dp_display)
1186 struct dp_display_private *dp;
1189 DRM_ERROR("invalid input\n");
1193 dp = container_of(dp_display, struct dp_display_private, dp_display);
1195 dp->irq = irq_of_parse_and_map(dp->pdev->dev.of_node, 0);
1198 DRM_ERROR("failed to get irq: %d\n", rc);
1202 rc = devm_request_irq(&dp->pdev->dev, dp->irq,
1203 dp_display_irq_handler,
1204 IRQF_TRIGGER_HIGH, "dp_display_isr", dp);
1206 DRM_ERROR("failed to request IRQ%u: %d\n",
1210 disable_irq(dp->irq);
1215 static int dp_display_probe(struct platform_device *pdev)
1218 struct dp_display_private *dp;
1220 if (!pdev || !pdev->dev.of_node) {
1221 DRM_ERROR("pdev not found\n");
1225 dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1230 dp->name = "drm_dp";
1232 rc = dp_init_sub_modules(dp);
1234 DRM_ERROR("init sub module failed\n");
1235 return -EPROBE_DEFER;
1238 mutex_init(&dp->event_mutex);
1239 g_dp_display = &dp->dp_display;
1241 /* Store DP audio handle inside DP display */
1242 g_dp_display->dp_audio = dp->audio;
1244 init_completion(&dp->audio_comp);
1246 platform_set_drvdata(pdev, g_dp_display);
1248 rc = component_add(&pdev->dev, &dp_display_comp_ops);
1250 DRM_ERROR("component add failed, rc=%d\n", rc);
1251 dp_display_deinit_sub_modules(dp);
1257 static int dp_display_remove(struct platform_device *pdev)
1259 struct dp_display_private *dp;
1261 dp = container_of(g_dp_display,
1262 struct dp_display_private, dp_display);
1264 dp_display_deinit_sub_modules(dp);
1266 component_del(&pdev->dev, &dp_display_comp_ops);
1267 platform_set_drvdata(pdev, NULL);
1272 static int dp_pm_resume(struct device *dev)
1274 struct platform_device *pdev = to_platform_device(dev);
1275 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1276 struct dp_display_private *dp;
1279 dp = container_of(dp_display, struct dp_display_private, dp_display);
1281 mutex_lock(&dp->event_mutex);
1283 DRM_DEBUG_DP("Before, core_inited=%d power_on=%d\n",
1284 dp->core_initialized, dp_display->power_on);
1286 /* start from disconnected state */
1287 dp->hpd_state = ST_DISCONNECTED;
1289 /* turn on dp ctrl/phy */
1290 dp_display_host_init(dp, true);
1292 dp_catalog_ctrl_hpd_config(dp->catalog);
1295 * set sink to normal operation mode -- D0
1298 dp_link_psm_config(dp->link, &dp->panel->link_info, false);
1300 if (dp_catalog_link_is_connected(dp->catalog)) {
1301 sink_count = drm_dp_read_sink_count(dp->aux);
1306 dp->link->sink_count = sink_count;
1308 * can not declared display is connected unless
1309 * HDMI cable is plugged in and sink_count of
1312 if (dp->link->sink_count)
1313 dp->dp_display.is_connected = true;
1315 dp->dp_display.is_connected = false;
1317 DRM_DEBUG_DP("After, sink_count=%d is_connected=%d core_inited=%d power_on=%d\n",
1318 dp->link->sink_count, dp->dp_display.is_connected,
1319 dp->core_initialized, dp_display->power_on);
1321 mutex_unlock(&dp->event_mutex);
1326 static int dp_pm_suspend(struct device *dev)
1328 struct platform_device *pdev = to_platform_device(dev);
1329 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1330 struct dp_display_private *dp;
1332 dp = container_of(dp_display, struct dp_display_private, dp_display);
1334 mutex_lock(&dp->event_mutex);
1336 DRM_DEBUG_DP("Before, core_inited=%d power_on=%d\n",
1337 dp->core_initialized, dp_display->power_on);
1339 if (dp->core_initialized == true) {
1340 /* mainlink enabled */
1341 if (dp_power_clk_status(dp->power, DP_CTRL_PM))
1342 dp_ctrl_off_link_stream(dp->ctrl);
1344 dp_display_host_deinit(dp);
1347 dp->hpd_state = ST_SUSPENDED;
1349 /* host_init will be called at pm_resume */
1350 dp->core_initialized = false;
1352 DRM_DEBUG_DP("After, core_inited=%d power_on=%d\n",
1353 dp->core_initialized, dp_display->power_on);
1355 mutex_unlock(&dp->event_mutex);
1360 static int dp_pm_prepare(struct device *dev)
1365 static void dp_pm_complete(struct device *dev)
1370 static const struct dev_pm_ops dp_pm_ops = {
1371 .suspend = dp_pm_suspend,
1372 .resume = dp_pm_resume,
1373 .prepare = dp_pm_prepare,
1374 .complete = dp_pm_complete,
1377 static struct platform_driver dp_display_driver = {
1378 .probe = dp_display_probe,
1379 .remove = dp_display_remove,
1381 .name = "msm-dp-display",
1382 .of_match_table = dp_dt_match,
1383 .suppress_bind_attrs = true,
1388 int __init msm_dp_register(void)
1392 ret = platform_driver_register(&dp_display_driver);
1394 DRM_ERROR("Dp display driver register failed");
1399 void __exit msm_dp_unregister(void)
1401 platform_driver_unregister(&dp_display_driver);
1404 void msm_dp_irq_postinstall(struct msm_dp *dp_display)
1406 struct dp_display_private *dp;
1411 dp = container_of(dp_display, struct dp_display_private, dp_display);
1413 dp_hpd_event_setup(dp);
1415 dp_add_event(dp, EV_HPD_INIT_SETUP, 0, 100);
1418 void msm_dp_debugfs_init(struct msm_dp *dp_display, struct drm_minor *minor)
1420 struct dp_display_private *dp;
1424 dp = container_of(dp_display, struct dp_display_private, dp_display);
1425 dev = &dp->pdev->dev;
1427 dp->debug = dp_debug_get(dev, dp->panel, dp->usbpd,
1428 dp->link, &dp->dp_display.connector,
1430 if (IS_ERR(dp->debug)) {
1431 rc = PTR_ERR(dp->debug);
1432 DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1437 int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1438 struct drm_encoder *encoder)
1440 struct msm_drm_private *priv;
1443 if (WARN_ON(!encoder) || WARN_ON(!dp_display) || WARN_ON(!dev))
1446 priv = dev->dev_private;
1447 dp_display->drm_dev = dev;
1449 ret = dp_display_request_irq(dp_display);
1451 DRM_ERROR("request_irq failed, ret=%d\n", ret);
1455 dp_display->encoder = encoder;
1457 dp_display->connector = dp_drm_connector_init(dp_display);
1458 if (IS_ERR(dp_display->connector)) {
1459 ret = PTR_ERR(dp_display->connector);
1460 DRM_DEV_ERROR(dev->dev,
1461 "failed to create dp connector: %d\n", ret);
1462 dp_display->connector = NULL;
1466 priv->connectors[priv->num_connectors++] = dp_display->connector;
1470 int msm_dp_display_enable(struct msm_dp *dp, struct drm_encoder *encoder)
1473 struct dp_display_private *dp_display;
1476 dp_display = container_of(dp, struct dp_display_private, dp_display);
1477 if (!dp_display->dp_mode.drm_mode.clock) {
1478 DRM_ERROR("invalid params\n");
1482 mutex_lock(&dp_display->event_mutex);
1484 /* stop sentinel checking */
1485 dp_del_event(dp_display, EV_CONNECT_PENDING_TIMEOUT);
1487 rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1489 DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1490 mutex_unlock(&dp_display->event_mutex);
1494 rc = dp_display_prepare(dp);
1496 DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1497 mutex_unlock(&dp_display->event_mutex);
1501 state = dp_display->hpd_state;
1503 if (state == ST_DISPLAY_OFF)
1504 dp_display_host_init(dp_display, true);
1506 dp_display_enable(dp_display, 0);
1508 rc = dp_display_post_enable(dp);
1510 DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1511 dp_display_disable(dp_display, 0);
1512 dp_display_unprepare(dp);
1515 /* manual kick off plug event to train link */
1516 if (state == ST_DISPLAY_OFF)
1517 dp_add_event(dp_display, EV_IRQ_HPD_INT, 0, 0);
1519 /* completed connection */
1520 dp_display->hpd_state = ST_CONNECTED;
1522 mutex_unlock(&dp_display->event_mutex);
1527 int msm_dp_display_pre_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 dp_ctrl_push_idle(dp_display->ctrl);
1538 int msm_dp_display_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1542 struct dp_display_private *dp_display;
1544 dp_display = container_of(dp, struct dp_display_private, dp_display);
1546 mutex_lock(&dp_display->event_mutex);
1548 /* stop sentinel checking */
1549 dp_del_event(dp_display, EV_DISCONNECT_PENDING_TIMEOUT);
1551 dp_display_disable(dp_display, 0);
1553 rc = dp_display_unprepare(dp);
1555 DRM_ERROR("DP display unprepare failed, rc=%d\n", rc);
1557 state = dp_display->hpd_state;
1558 if (state == ST_DISCONNECT_PENDING) {
1559 /* completed disconnection */
1560 dp_display->hpd_state = ST_DISCONNECTED;
1562 dp_display->hpd_state = ST_DISPLAY_OFF;
1565 mutex_unlock(&dp_display->event_mutex);
1569 void msm_dp_display_mode_set(struct msm_dp *dp, struct drm_encoder *encoder,
1570 struct drm_display_mode *mode,
1571 struct drm_display_mode *adjusted_mode)
1573 struct dp_display_private *dp_display;
1575 dp_display = container_of(dp, struct dp_display_private, dp_display);
1577 memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1579 if (dp_display_check_video_test(dp))
1580 dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1581 else /* Default num_components per pixel = 3 */
1582 dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1584 if (!dp_display->dp_mode.bpp)
1585 dp_display->dp_mode.bpp = 24; /* Default bpp */
1587 drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1589 dp_display->dp_mode.v_active_low =
1590 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1592 dp_display->dp_mode.h_active_low =
1593 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);