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_complete(struct msm_dp *dp_display)
183 struct dp_display_private *dp;
185 dp = container_of(dp_display, struct dp_display_private, dp_display);
187 complete_all(&dp->audio_comp);
190 static int dp_display_bind(struct device *dev, struct device *master,
194 struct dp_display_private *dp;
195 struct drm_device *drm;
196 struct msm_drm_private *priv;
198 drm = dev_get_drvdata(master);
200 dp = container_of(g_dp_display,
201 struct dp_display_private, dp_display);
203 DRM_ERROR("DP driver bind failed. Invalid driver data\n");
207 dp->dp_display.drm_dev = drm;
208 priv = drm->dev_private;
209 priv->dp = &(dp->dp_display);
211 rc = dp->parser->parse(dp->parser);
213 DRM_ERROR("device tree parsing failed\n");
217 rc = dp_aux_register(dp->aux);
219 DRM_ERROR("DRM DP AUX register failed\n");
223 rc = dp_power_client_init(dp->power);
225 DRM_ERROR("Power client create failed\n");
229 rc = dp_register_audio_driver(dev, dp->audio);
231 DRM_ERROR("Audio registration Dp failed\n");
237 static void dp_display_unbind(struct device *dev, struct device *master,
240 struct dp_display_private *dp;
241 struct drm_device *drm = dev_get_drvdata(master);
242 struct msm_drm_private *priv = drm->dev_private;
244 dp = container_of(g_dp_display,
245 struct dp_display_private, dp_display);
247 DRM_ERROR("Invalid DP driver data\n");
251 dp_power_client_deinit(dp->power);
252 dp_aux_unregister(dp->aux);
256 static const struct component_ops dp_display_comp_ops = {
257 .bind = dp_display_bind,
258 .unbind = dp_display_unbind,
261 static bool dp_display_is_ds_bridge(struct dp_panel *panel)
263 return (panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
264 DP_DWN_STRM_PORT_PRESENT);
267 static bool dp_display_is_sink_count_zero(struct dp_display_private *dp)
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 void dp_display_set_encoder_mode(struct dp_display_private *dp)
287 struct msm_drm_private *priv = dp->dp_display.drm_dev->dev_private;
288 struct msm_kms *kms = priv->kms;
290 if (!dp->encoder_mode_set && dp->dp_display.encoder &&
291 kms->funcs->set_encoder_mode) {
292 kms->funcs->set_encoder_mode(kms,
293 dp->dp_display.encoder, false);
295 dp->encoder_mode_set = true;
299 static int dp_display_send_hpd_notification(struct dp_display_private *dp,
302 if ((hpd && dp->dp_display.is_connected) ||
303 (!hpd && !dp->dp_display.is_connected)) {
304 DRM_DEBUG_DP("HPD already %s\n", (hpd ? "on" : "off"));
308 /* reset video pattern flag on disconnect */
310 dp->panel->video_test = false;
312 dp->dp_display.is_connected = hpd;
314 dp_display_send_hpd_event(&dp->dp_display);
319 static int dp_display_process_hpd_high(struct dp_display_private *dp)
324 dp->panel->max_dp_lanes = dp->parser->max_dp_lanes;
326 rc = dp_panel_read_sink_caps(dp->panel, dp->dp_display.connector);
330 dp_link_process_request(dp->link);
332 edid = dp->panel->edid;
334 dp->audio_supported = drm_detect_monitor_audio(edid);
335 dp_panel_handle_sink_request(dp->panel);
337 dp->dp_display.max_pclk_khz = DP_MAX_PIXEL_CLK_KHZ;
338 dp->dp_display.max_dp_lanes = dp->parser->max_dp_lanes;
340 dp_link_reset_phy_params_vx_px(dp->link);
341 rc = dp_ctrl_on_link(dp->ctrl);
343 DRM_ERROR("failed to complete DP link training\n");
347 dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
353 static void dp_display_host_init(struct dp_display_private *dp, int reset)
357 if (dp->core_initialized) {
358 DRM_DEBUG_DP("DP core already initialized\n");
362 if (dp->usbpd->orientation == ORIENTATION_CC2)
365 dp_display_set_encoder_mode(dp);
367 dp_power_init(dp->power, flip);
368 dp_ctrl_host_init(dp->ctrl, flip, reset);
369 dp_aux_init(dp->aux);
370 dp->core_initialized = true;
373 static void dp_display_host_deinit(struct dp_display_private *dp)
375 if (!dp->core_initialized) {
376 DRM_DEBUG_DP("DP core not initialized\n");
380 dp_ctrl_host_deinit(dp->ctrl);
381 dp_aux_deinit(dp->aux);
382 dp_power_deinit(dp->power);
384 dp->core_initialized = false;
387 static int dp_display_usbpd_configure_cb(struct device *dev)
390 struct dp_display_private *dp;
393 DRM_ERROR("invalid dev\n");
398 dp = container_of(g_dp_display,
399 struct dp_display_private, dp_display);
401 DRM_ERROR("no driver data found\n");
406 dp_display_host_init(dp, false);
409 * set sink to normal operation mode -- D0
412 dp_link_psm_config(dp->link, &dp->panel->link_info, false);
413 rc = dp_display_process_hpd_high(dp);
418 static int dp_display_usbpd_disconnect_cb(struct device *dev)
421 struct dp_display_private *dp;
424 DRM_ERROR("invalid dev\n");
429 dp = container_of(g_dp_display,
430 struct dp_display_private, dp_display);
432 DRM_ERROR("no driver data found\n");
437 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
442 static void dp_display_handle_video_request(struct dp_display_private *dp)
444 if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
445 dp->panel->video_test = true;
446 dp_link_send_test_response(dp->link);
450 static int dp_display_handle_port_ststus_changed(struct dp_display_private *dp)
454 if (dp_display_is_sink_count_zero(dp)) {
455 DRM_DEBUG_DP("sink count is zero, nothing to do\n");
456 if (dp->hpd_state != ST_DISCONNECTED) {
457 dp->hpd_state = ST_DISCONNECT_PENDING;
458 dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
461 if (dp->hpd_state == ST_DISCONNECTED) {
462 dp->hpd_state = ST_CONNECT_PENDING;
463 rc = dp_display_process_hpd_high(dp);
465 dp->hpd_state = ST_DISCONNECTED;
472 static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
474 u32 sink_request = dp->link->sink_request;
476 if (dp->hpd_state == ST_DISCONNECTED) {
477 if (sink_request & DP_LINK_STATUS_UPDATED) {
478 DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
483 dp_ctrl_handle_sink_request(dp->ctrl);
485 if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
486 dp_display_handle_video_request(dp);
491 static int dp_display_usbpd_attention_cb(struct device *dev)
495 struct dp_display_private *dp;
496 struct dp_usbpd *hpd;
499 DRM_ERROR("invalid dev\n");
503 dp = container_of(g_dp_display,
504 struct dp_display_private, dp_display);
506 DRM_ERROR("no driver data found\n");
512 /* check for any test request issued by sink */
513 rc = dp_link_process_request(dp->link);
515 sink_request = dp->link->sink_request;
516 if (sink_request & DS_PORT_STATUS_CHANGED)
517 rc = dp_display_handle_port_ststus_changed(dp);
519 rc = dp_display_handle_irq_hpd(dp);
525 static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
527 struct dp_usbpd *hpd = dp->usbpd;
529 u32 tout = DP_TIMEOUT_5_SECOND;
535 mutex_lock(&dp->event_mutex);
537 state = dp->hpd_state;
538 if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
539 mutex_unlock(&dp->event_mutex);
543 if (state == ST_CONNECT_PENDING || state == ST_CONNECTED) {
544 mutex_unlock(&dp->event_mutex);
548 if (state == ST_DISCONNECT_PENDING) {
549 /* wait until ST_DISCONNECTED */
550 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
551 mutex_unlock(&dp->event_mutex);
555 dp->hpd_state = ST_CONNECT_PENDING;
559 ret = dp_display_usbpd_configure_cb(&dp->pdev->dev);
560 if (ret) { /* link train failed */
562 dp->hpd_state = ST_DISCONNECTED;
564 if (ret == -ECONNRESET) { /* cable unplugged */
565 dp->core_initialized = false;
569 /* start sentinel checking in case of missing uevent */
570 dp_add_event(dp, EV_CONNECT_PENDING_TIMEOUT, 0, tout);
573 mutex_unlock(&dp->event_mutex);
575 /* uevent will complete connection part */
579 static int dp_display_enable(struct dp_display_private *dp, u32 data);
580 static int dp_display_disable(struct dp_display_private *dp, u32 data);
582 static int dp_connect_pending_timeout(struct dp_display_private *dp, u32 data)
586 mutex_lock(&dp->event_mutex);
588 state = dp->hpd_state;
589 if (state == ST_CONNECT_PENDING)
590 dp->hpd_state = ST_CONNECTED;
592 mutex_unlock(&dp->event_mutex);
597 static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
600 struct dp_display_private *dp;
602 dp = container_of(dp_display,
603 struct dp_display_private, dp_display);
605 /* notify audio subsystem only if sink supports audio */
606 if (dp_display->plugged_cb && dp_display->codec_dev &&
608 dp_display->plugged_cb(dp_display->codec_dev, plugged);
611 static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
613 struct dp_usbpd *hpd = dp->usbpd;
619 mutex_lock(&dp->event_mutex);
621 state = dp->hpd_state;
622 if (state == ST_DISCONNECT_PENDING || state == ST_DISCONNECTED) {
623 mutex_unlock(&dp->event_mutex);
627 if (state == ST_CONNECT_PENDING) {
628 /* wait until CONNECTED */
629 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 1); /* delay = 1 */
630 mutex_unlock(&dp->event_mutex);
634 dp->hpd_state = ST_DISCONNECT_PENDING;
636 /* disable HPD plug interrupt until disconnect is done */
637 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK
638 | DP_DP_IRQ_HPD_INT_MASK, false);
643 * We don't need separate work for disconnect as
644 * connect/attention interrupts are disabled
646 dp_display_usbpd_disconnect_cb(&dp->pdev->dev);
648 /* start sentinel checking in case of missing uevent */
649 dp_add_event(dp, EV_DISCONNECT_PENDING_TIMEOUT, 0, DP_TIMEOUT_5_SECOND);
651 /* signal the disconnect event early to ensure proper teardown */
652 reinit_completion(&dp->audio_comp);
653 dp_display_handle_plugged_change(g_dp_display, false);
655 dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK |
656 DP_DP_IRQ_HPD_INT_MASK, true);
658 /* uevent will complete disconnection part */
659 mutex_unlock(&dp->event_mutex);
663 static int dp_disconnect_pending_timeout(struct dp_display_private *dp, u32 data)
667 mutex_lock(&dp->event_mutex);
669 state = dp->hpd_state;
670 if (state == ST_DISCONNECT_PENDING)
671 dp->hpd_state = ST_DISCONNECTED;
673 mutex_unlock(&dp->event_mutex);
678 static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
683 mutex_lock(&dp->event_mutex);
685 /* irq_hpd can happen at either connected or disconnected state */
686 state = dp->hpd_state;
687 if (state == ST_DISPLAY_OFF) {
688 mutex_unlock(&dp->event_mutex);
692 if (state == ST_CONNECT_PENDING) {
693 /* wait until ST_CONNECTED */
694 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
695 mutex_unlock(&dp->event_mutex);
699 if (state == ST_CONNECT_PENDING || state == ST_DISCONNECT_PENDING) {
700 /* wait until ST_CONNECTED */
701 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
702 mutex_unlock(&dp->event_mutex);
706 ret = dp_display_usbpd_attention_cb(&dp->pdev->dev);
707 if (ret == -ECONNRESET) { /* cable unplugged */
708 dp->core_initialized = false;
711 mutex_unlock(&dp->event_mutex);
716 static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
718 dp_debug_put(dp->debug);
719 dp_ctrl_put(dp->ctrl);
720 dp_panel_put(dp->panel);
722 dp_audio_put(dp->audio);
725 static int dp_init_sub_modules(struct dp_display_private *dp)
728 struct device *dev = &dp->pdev->dev;
729 struct dp_usbpd_cb *cb = &dp->usbpd_cb;
730 struct dp_panel_in panel_in = {
734 /* Callback APIs used for cable status change event */
735 cb->configure = dp_display_usbpd_configure_cb;
736 cb->disconnect = dp_display_usbpd_disconnect_cb;
737 cb->attention = dp_display_usbpd_attention_cb;
739 dp->usbpd = dp_hpd_get(dev, cb);
740 if (IS_ERR(dp->usbpd)) {
741 rc = PTR_ERR(dp->usbpd);
742 DRM_ERROR("failed to initialize hpd, rc = %d\n", rc);
747 dp->parser = dp_parser_get(dp->pdev);
748 if (IS_ERR(dp->parser)) {
749 rc = PTR_ERR(dp->parser);
750 DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
755 dp->catalog = dp_catalog_get(dev, &dp->parser->io);
756 if (IS_ERR(dp->catalog)) {
757 rc = PTR_ERR(dp->catalog);
758 DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
763 dp->power = dp_power_get(dev, dp->parser);
764 if (IS_ERR(dp->power)) {
765 rc = PTR_ERR(dp->power);
766 DRM_ERROR("failed to initialize power, rc = %d\n", rc);
771 dp->aux = dp_aux_get(dev, dp->catalog);
772 if (IS_ERR(dp->aux)) {
773 rc = PTR_ERR(dp->aux);
774 DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
779 dp->link = dp_link_get(dev, dp->aux);
780 if (IS_ERR(dp->link)) {
781 rc = PTR_ERR(dp->link);
782 DRM_ERROR("failed to initialize link, rc = %d\n", rc);
787 panel_in.aux = dp->aux;
788 panel_in.catalog = dp->catalog;
789 panel_in.link = dp->link;
791 dp->panel = dp_panel_get(&panel_in);
792 if (IS_ERR(dp->panel)) {
793 rc = PTR_ERR(dp->panel);
794 DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
799 dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
800 dp->power, dp->catalog, dp->parser);
801 if (IS_ERR(dp->ctrl)) {
802 rc = PTR_ERR(dp->ctrl);
803 DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
808 dp->audio = dp_audio_get(dp->pdev, dp->panel, dp->catalog);
809 if (IS_ERR(dp->audio)) {
810 rc = PTR_ERR(dp->audio);
811 pr_err("failed to initialize audio, rc = %d\n", rc);
819 dp_ctrl_put(dp->ctrl);
821 dp_panel_put(dp->panel);
828 static int dp_display_set_mode(struct msm_dp *dp_display,
829 struct dp_display_mode *mode)
831 struct dp_display_private *dp;
833 dp = container_of(dp_display, struct dp_display_private, dp_display);
835 dp->panel->dp_mode.drm_mode = mode->drm_mode;
836 dp->panel->dp_mode.bpp = mode->bpp;
837 dp->panel->dp_mode.capabilities = mode->capabilities;
838 dp_panel_init_panel_info(dp->panel);
842 static int dp_display_prepare(struct msm_dp *dp)
847 static int dp_display_enable(struct dp_display_private *dp, u32 data)
850 struct msm_dp *dp_display;
852 dp_display = g_dp_display;
854 if (dp_display->power_on) {
855 DRM_DEBUG_DP("Link already setup, return\n");
859 rc = dp_ctrl_on_stream(dp->ctrl);
861 dp_display->power_on = true;
866 static int dp_display_post_enable(struct msm_dp *dp_display)
868 struct dp_display_private *dp;
871 dp = container_of(dp_display, struct dp_display_private, dp_display);
873 rate = dp->link->link_params.rate;
875 if (dp->audio_supported) {
876 dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
877 dp->audio->lane_count = dp->link->link_params.num_lanes;
880 /* signal the connect event late to synchronize video and display */
881 dp_display_handle_plugged_change(dp_display, true);
885 static int dp_display_disable(struct dp_display_private *dp, u32 data)
887 struct msm_dp *dp_display;
889 dp_display = g_dp_display;
891 if (!dp_display->power_on)
894 /* wait only if audio was enabled */
895 if (dp_display->audio_enabled) {
896 /* signal the disconnect event */
897 reinit_completion(&dp->audio_comp);
898 dp_display_handle_plugged_change(dp_display, false);
899 if (!wait_for_completion_timeout(&dp->audio_comp,
901 DRM_ERROR("audio comp timeout\n");
904 dp_display->audio_enabled = false;
906 dp_ctrl_off(dp->ctrl);
908 dp->core_initialized = false;
910 dp_display->power_on = false;
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 static void dp_display_config_hpd(struct dp_display_private *dp)
1011 dp_display_host_init(dp, true);
1012 dp_catalog_ctrl_hpd_config(dp->catalog);
1014 /* Enable interrupt first time
1015 * we are leaving dp clocks on during disconnect
1016 * and never disable interrupt
1018 enable_irq(dp->irq);
1021 static int hpd_event_thread(void *data)
1023 struct dp_display_private *dp_priv;
1025 struct dp_event *todo;
1026 int timeout_mode = 0;
1028 dp_priv = (struct dp_display_private *)data;
1032 wait_event_timeout(dp_priv->event_q,
1033 (dp_priv->event_pndx == dp_priv->event_gndx),
1036 wait_event_interruptible(dp_priv->event_q,
1037 (dp_priv->event_pndx != dp_priv->event_gndx));
1039 spin_lock_irqsave(&dp_priv->event_lock, flag);
1040 todo = &dp_priv->event_list[dp_priv->event_gndx];
1042 struct dp_event *todo_next;
1044 dp_priv->event_gndx++;
1045 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1047 /* re enter delay event into q */
1048 todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
1049 dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1050 todo_next->event_id = todo->event_id;
1051 todo_next->data = todo->data;
1052 todo_next->delay = todo->delay - 1;
1054 /* clean up older event */
1055 todo->event_id = EV_NO_EVENT;
1058 /* switch to timeout mode */
1060 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1064 /* timeout with no events in q */
1065 if (dp_priv->event_pndx == dp_priv->event_gndx) {
1066 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1070 dp_priv->event_gndx++;
1071 dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1073 spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1075 switch (todo->event_id) {
1076 case EV_HPD_INIT_SETUP:
1077 dp_display_config_hpd(dp_priv);
1079 case EV_HPD_PLUG_INT:
1080 dp_hpd_plug_handle(dp_priv, todo->data);
1082 case EV_HPD_UNPLUG_INT:
1083 dp_hpd_unplug_handle(dp_priv, todo->data);
1085 case EV_IRQ_HPD_INT:
1086 dp_irq_hpd_handle(dp_priv, todo->data);
1088 case EV_HPD_REPLUG_INT:
1091 case EV_USER_NOTIFICATION:
1092 dp_display_send_hpd_notification(dp_priv,
1095 case EV_CONNECT_PENDING_TIMEOUT:
1096 dp_connect_pending_timeout(dp_priv,
1099 case EV_DISCONNECT_PENDING_TIMEOUT:
1100 dp_disconnect_pending_timeout(dp_priv,
1111 static void dp_hpd_event_setup(struct dp_display_private *dp_priv)
1113 init_waitqueue_head(&dp_priv->event_q);
1114 spin_lock_init(&dp_priv->event_lock);
1116 kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1119 static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1121 struct dp_display_private *dp = dev_id;
1122 irqreturn_t ret = IRQ_HANDLED;
1126 DRM_ERROR("invalid data\n");
1130 hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1132 if (hpd_isr_status & 0x0F) {
1133 /* hpd related interrupts */
1134 if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK ||
1135 hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1136 dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1139 if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1140 /* stop sentinel connect pending checking */
1141 dp_del_event(dp, EV_CONNECT_PENDING_TIMEOUT);
1142 dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1145 if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK)
1146 dp_add_event(dp, EV_HPD_REPLUG_INT, 0, 0);
1148 if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1149 dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1152 /* DP controller isr */
1153 dp_ctrl_isr(dp->ctrl);
1156 dp_aux_isr(dp->aux);
1161 int dp_display_request_irq(struct msm_dp *dp_display)
1164 struct dp_display_private *dp;
1167 DRM_ERROR("invalid input\n");
1171 dp = container_of(dp_display, struct dp_display_private, dp_display);
1173 dp->irq = irq_of_parse_and_map(dp->pdev->dev.of_node, 0);
1176 DRM_ERROR("failed to get irq: %d\n", rc);
1180 rc = devm_request_irq(&dp->pdev->dev, dp->irq,
1181 dp_display_irq_handler,
1182 IRQF_TRIGGER_HIGH, "dp_display_isr", dp);
1184 DRM_ERROR("failed to request IRQ%u: %d\n",
1188 disable_irq(dp->irq);
1193 static int dp_display_probe(struct platform_device *pdev)
1196 struct dp_display_private *dp;
1198 if (!pdev || !pdev->dev.of_node) {
1199 DRM_ERROR("pdev not found\n");
1203 dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1208 dp->name = "drm_dp";
1210 rc = dp_init_sub_modules(dp);
1212 DRM_ERROR("init sub module failed\n");
1213 return -EPROBE_DEFER;
1216 mutex_init(&dp->event_mutex);
1217 g_dp_display = &dp->dp_display;
1219 /* Store DP audio handle inside DP display */
1220 g_dp_display->dp_audio = dp->audio;
1222 init_completion(&dp->audio_comp);
1224 platform_set_drvdata(pdev, g_dp_display);
1226 rc = component_add(&pdev->dev, &dp_display_comp_ops);
1228 DRM_ERROR("component add failed, rc=%d\n", rc);
1229 dp_display_deinit_sub_modules(dp);
1235 static int dp_display_remove(struct platform_device *pdev)
1237 struct dp_display_private *dp;
1239 dp = container_of(g_dp_display,
1240 struct dp_display_private, dp_display);
1242 dp_display_deinit_sub_modules(dp);
1244 component_del(&pdev->dev, &dp_display_comp_ops);
1245 platform_set_drvdata(pdev, NULL);
1250 static int dp_pm_resume(struct device *dev)
1252 struct platform_device *pdev = to_platform_device(dev);
1253 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1254 struct dp_display_private *dp;
1257 dp = container_of(dp_display, struct dp_display_private, dp_display);
1259 mutex_lock(&dp->event_mutex);
1261 /* start from disconnected state */
1262 dp->hpd_state = ST_DISCONNECTED;
1264 /* turn on dp ctrl/phy */
1265 dp_display_host_init(dp, true);
1267 dp_catalog_ctrl_hpd_config(dp->catalog);
1269 status = dp_catalog_link_is_connected(dp->catalog);
1272 * can not declared display is connected unless
1273 * HDMI cable is plugged in and sink_count of
1276 if (status && dp->link->sink_count)
1277 dp->dp_display.is_connected = true;
1279 dp->dp_display.is_connected = false;
1281 mutex_unlock(&dp->event_mutex);
1286 static int dp_pm_suspend(struct device *dev)
1288 struct platform_device *pdev = to_platform_device(dev);
1289 struct msm_dp *dp_display = platform_get_drvdata(pdev);
1290 struct dp_display_private *dp;
1292 dp = container_of(dp_display, struct dp_display_private, dp_display);
1294 mutex_lock(&dp->event_mutex);
1296 if (dp->core_initialized == true)
1297 dp_display_host_deinit(dp);
1299 dp->hpd_state = ST_SUSPENDED;
1301 /* host_init will be called at pm_resume */
1302 dp->core_initialized = false;
1304 mutex_unlock(&dp->event_mutex);
1309 static int dp_pm_prepare(struct device *dev)
1314 static void dp_pm_complete(struct device *dev)
1319 static const struct dev_pm_ops dp_pm_ops = {
1320 .suspend = dp_pm_suspend,
1321 .resume = dp_pm_resume,
1322 .prepare = dp_pm_prepare,
1323 .complete = dp_pm_complete,
1326 static struct platform_driver dp_display_driver = {
1327 .probe = dp_display_probe,
1328 .remove = dp_display_remove,
1330 .name = "msm-dp-display",
1331 .of_match_table = dp_dt_match,
1332 .suppress_bind_attrs = true,
1337 int __init msm_dp_register(void)
1341 ret = platform_driver_register(&dp_display_driver);
1343 DRM_ERROR("Dp display driver register failed");
1348 void __exit msm_dp_unregister(void)
1350 platform_driver_unregister(&dp_display_driver);
1353 void msm_dp_irq_postinstall(struct msm_dp *dp_display)
1355 struct dp_display_private *dp;
1360 dp = container_of(dp_display, struct dp_display_private, dp_display);
1362 dp_hpd_event_setup(dp);
1364 dp_add_event(dp, EV_HPD_INIT_SETUP, 0, 100);
1367 void msm_dp_debugfs_init(struct msm_dp *dp_display, struct drm_minor *minor)
1369 struct dp_display_private *dp;
1373 dp = container_of(dp_display, struct dp_display_private, dp_display);
1374 dev = &dp->pdev->dev;
1376 dp->debug = dp_debug_get(dev, dp->panel, dp->usbpd,
1377 dp->link, &dp->dp_display.connector,
1379 if (IS_ERR(dp->debug)) {
1380 rc = PTR_ERR(dp->debug);
1381 DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1386 int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1387 struct drm_encoder *encoder)
1389 struct msm_drm_private *priv;
1392 if (WARN_ON(!encoder) || WARN_ON(!dp_display) || WARN_ON(!dev))
1395 priv = dev->dev_private;
1396 dp_display->drm_dev = dev;
1398 ret = dp_display_request_irq(dp_display);
1400 DRM_ERROR("request_irq failed, ret=%d\n", ret);
1404 dp_display->encoder = encoder;
1406 dp_display->connector = dp_drm_connector_init(dp_display);
1407 if (IS_ERR(dp_display->connector)) {
1408 ret = PTR_ERR(dp_display->connector);
1409 DRM_DEV_ERROR(dev->dev,
1410 "failed to create dp connector: %d\n", ret);
1411 dp_display->connector = NULL;
1415 priv->connectors[priv->num_connectors++] = dp_display->connector;
1419 int msm_dp_display_enable(struct msm_dp *dp, struct drm_encoder *encoder)
1422 struct dp_display_private *dp_display;
1425 dp_display = container_of(dp, struct dp_display_private, dp_display);
1426 if (!dp_display->dp_mode.drm_mode.clock) {
1427 DRM_ERROR("invalid params\n");
1431 mutex_lock(&dp_display->event_mutex);
1433 /* stop sentinel checking */
1434 dp_del_event(dp_display, EV_CONNECT_PENDING_TIMEOUT);
1436 rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1438 DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1439 mutex_unlock(&dp_display->event_mutex);
1443 rc = dp_display_prepare(dp);
1445 DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1446 mutex_unlock(&dp_display->event_mutex);
1450 state = dp_display->hpd_state;
1452 if (state == ST_DISPLAY_OFF)
1453 dp_display_host_init(dp_display, true);
1455 dp_display_enable(dp_display, 0);
1457 rc = dp_display_post_enable(dp);
1459 DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1460 dp_display_disable(dp_display, 0);
1461 dp_display_unprepare(dp);
1464 /* manual kick off plug event to train link */
1465 if (state == ST_DISPLAY_OFF)
1466 dp_add_event(dp_display, EV_IRQ_HPD_INT, 0, 0);
1468 /* completed connection */
1469 dp_display->hpd_state = ST_CONNECTED;
1471 mutex_unlock(&dp_display->event_mutex);
1476 int msm_dp_display_pre_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1478 struct dp_display_private *dp_display;
1480 dp_display = container_of(dp, struct dp_display_private, dp_display);
1482 dp_ctrl_push_idle(dp_display->ctrl);
1487 int msm_dp_display_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1491 struct dp_display_private *dp_display;
1493 dp_display = container_of(dp, struct dp_display_private, dp_display);
1495 mutex_lock(&dp_display->event_mutex);
1497 /* stop sentinel checking */
1498 dp_del_event(dp_display, EV_DISCONNECT_PENDING_TIMEOUT);
1500 dp_display_disable(dp_display, 0);
1502 rc = dp_display_unprepare(dp);
1504 DRM_ERROR("DP display unprepare failed, rc=%d\n", rc);
1506 state = dp_display->hpd_state;
1507 if (state == ST_DISCONNECT_PENDING) {
1508 /* completed disconnection */
1509 dp_display->hpd_state = ST_DISCONNECTED;
1511 dp_display->hpd_state = ST_DISPLAY_OFF;
1514 mutex_unlock(&dp_display->event_mutex);
1518 void msm_dp_display_mode_set(struct msm_dp *dp, struct drm_encoder *encoder,
1519 struct drm_display_mode *mode,
1520 struct drm_display_mode *adjusted_mode)
1522 struct dp_display_private *dp_display;
1524 dp_display = container_of(dp, struct dp_display_private, dp_display);
1526 memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1528 if (dp_display_check_video_test(dp))
1529 dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1530 else /* Default num_components per pixel = 3 */
1531 dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1533 if (!dp_display->dp_mode.bpp)
1534 dp_display->dp_mode.bpp = 24; /* Default bpp */
1536 drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1538 dp_display->dp_mode.v_active_low =
1539 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1541 dp_display->dp_mode.h_active_low =
1542 !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);