1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
8 #include <linux/component.h>
9 #include <linux/extcon.h>
10 #include <linux/firmware.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/phy/phy.h>
13 #include <linux/regmap.h>
14 #include <linux/reset.h>
16 #include <sound/hdmi-codec.h>
18 #include <drm/display/drm_dp_helper.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_edid.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_simple_kms_helper.h>
25 #include "cdn-dp-core.h"
26 #include "cdn-dp-reg.h"
28 static inline struct cdn_dp_device *connector_to_dp(struct drm_connector *connector)
30 return container_of(connector, struct cdn_dp_device, connector);
33 static inline struct cdn_dp_device *encoder_to_dp(struct drm_encoder *encoder)
35 struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
37 return container_of(rkencoder, struct cdn_dp_device, encoder);
40 #define GRF_SOC_CON9 0x6224
41 #define DP_SEL_VOP_LIT BIT(12)
42 #define GRF_SOC_CON26 0x6268
43 #define DPTX_HPD_SEL (3 << 12)
44 #define DPTX_HPD_DEL (2 << 12)
45 #define DPTX_HPD_SEL_MASK (3 << 28)
47 #define CDN_FW_TIMEOUT_MS (64 * 1000)
48 #define CDN_DPCD_TIMEOUT_MS 5000
49 #define CDN_DP_FIRMWARE "rockchip/dptx.bin"
50 MODULE_FIRMWARE(CDN_DP_FIRMWARE);
56 static struct cdn_dp_data rk3399_cdn_dp = {
60 static const struct of_device_id cdn_dp_dt_ids[] = {
61 { .compatible = "rockchip,rk3399-cdn-dp",
62 .data = (void *)&rk3399_cdn_dp },
66 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
68 static int cdn_dp_grf_write(struct cdn_dp_device *dp,
69 unsigned int reg, unsigned int val)
73 ret = clk_prepare_enable(dp->grf_clk);
75 DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
79 ret = regmap_write(dp->grf, reg, val);
81 DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
82 clk_disable_unprepare(dp->grf_clk);
86 clk_disable_unprepare(dp->grf_clk);
91 static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
96 ret = clk_prepare_enable(dp->pclk);
98 DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret);
102 ret = clk_prepare_enable(dp->core_clk);
104 DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret);
108 ret = pm_runtime_get_sync(dp->dev);
110 DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret);
111 goto err_pm_runtime_get;
114 reset_control_assert(dp->core_rst);
115 reset_control_assert(dp->dptx_rst);
116 reset_control_assert(dp->apb_rst);
117 reset_control_deassert(dp->core_rst);
118 reset_control_deassert(dp->dptx_rst);
119 reset_control_deassert(dp->apb_rst);
121 rate = clk_get_rate(dp->core_clk);
123 DRM_DEV_ERROR(dp->dev, "get clk rate failed\n");
128 cdn_dp_set_fw_clk(dp, rate);
129 cdn_dp_clock_reset(dp);
134 pm_runtime_put(dp->dev);
136 clk_disable_unprepare(dp->core_clk);
138 clk_disable_unprepare(dp->pclk);
143 static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
145 pm_runtime_put_sync(dp->dev);
146 clk_disable_unprepare(dp->pclk);
147 clk_disable_unprepare(dp->core_clk);
150 static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
152 struct extcon_dev *edev = port->extcon;
153 union extcon_property_value property;
157 dptx = extcon_get_state(edev, EXTCON_DISP_DP);
159 extcon_get_property(edev, EXTCON_DISP_DP,
160 EXTCON_PROP_USB_SS, &property);
172 static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
178 ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
182 *sink_count = DP_GET_SINK_COUNT(value);
186 static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
188 struct cdn_dp_port *port;
191 for (i = 0; i < dp->ports; i++) {
193 lanes = cdn_dp_get_port_lanes(port);
200 static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
202 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
203 struct cdn_dp_port *port;
206 if (dp->active_port < 0 || dp->active_port >= dp->ports) {
207 DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
211 port = dp->port[dp->active_port];
214 * Attempt to read sink count, retry in case the sink may not be ready.
216 * Sinks are *supposed* to come up within 1ms from an off state, but
217 * some docks need more time to power up.
219 while (time_before(jiffies, timeout)) {
220 if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
223 if (!cdn_dp_get_sink_count(dp, &sink_count))
224 return sink_count ? true : false;
226 usleep_range(5000, 10000);
229 DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
233 static enum drm_connector_status
234 cdn_dp_connector_detect(struct drm_connector *connector, bool force)
236 struct cdn_dp_device *dp = connector_to_dp(connector);
237 enum drm_connector_status status = connector_status_disconnected;
239 mutex_lock(&dp->lock);
241 status = connector_status_connected;
242 mutex_unlock(&dp->lock);
247 static void cdn_dp_connector_destroy(struct drm_connector *connector)
249 drm_connector_unregister(connector);
250 drm_connector_cleanup(connector);
253 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
254 .detect = cdn_dp_connector_detect,
255 .destroy = cdn_dp_connector_destroy,
256 .fill_modes = drm_helper_probe_single_connector_modes,
257 .reset = drm_atomic_helper_connector_reset,
258 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
259 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
262 static int cdn_dp_connector_get_modes(struct drm_connector *connector)
264 struct cdn_dp_device *dp = connector_to_dp(connector);
268 mutex_lock(&dp->lock);
271 DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
272 edid->width_cm, edid->height_cm);
274 dp->sink_has_audio = drm_detect_monitor_audio(edid);
276 drm_connector_update_edid_property(connector, edid);
277 ret = drm_add_edid_modes(connector, edid);
279 mutex_unlock(&dp->lock);
284 static enum drm_mode_status
285 cdn_dp_connector_mode_valid(struct drm_connector *connector,
286 struct drm_display_mode *mode)
288 struct cdn_dp_device *dp = connector_to_dp(connector);
289 struct drm_display_info *display_info = &dp->connector.display_info;
290 u32 requested, actual, rate, sink_max, source_max = 0;
293 /* If DP is disconnected, every mode is invalid */
297 switch (display_info->bpc) {
309 requested = mode->clock * bpc * 3 / 1000;
311 source_max = dp->lanes;
312 sink_max = drm_dp_max_lane_count(dp->dpcd);
313 lanes = min(source_max, sink_max);
315 source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
316 sink_max = drm_dp_max_link_rate(dp->dpcd);
317 rate = min(source_max, sink_max);
319 actual = rate * lanes / 100;
321 /* efficiency is about 0.8 */
322 actual = actual * 8 / 10;
324 if (requested > actual) {
325 DRM_DEV_DEBUG_KMS(dp->dev,
326 "requested=%d, actual=%d, clock=%d\n",
327 requested, actual, mode->clock);
328 return MODE_CLOCK_HIGH;
334 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
335 .get_modes = cdn_dp_connector_get_modes,
336 .mode_valid = cdn_dp_connector_mode_valid,
339 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
342 const u32 *iram_data, *dram_data;
343 const struct firmware *fw = dp->fw;
344 const struct cdn_firmware_header *hdr;
346 hdr = (struct cdn_firmware_header *)fw->data;
347 if (fw->size != le32_to_cpu(hdr->size_bytes)) {
348 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
352 iram_data = (const u32 *)(fw->data + hdr->header_size);
353 dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
355 ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
356 dram_data, hdr->dram_size);
360 ret = cdn_dp_set_firmware_active(dp, true);
362 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
366 return cdn_dp_event_config(dp);
369 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
373 if (!cdn_dp_check_sink_connection(dp))
376 ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
377 DP_RECEIVER_CAP_SIZE);
379 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
384 dp->edid = drm_do_get_edid(&dp->connector,
385 cdn_dp_get_edid_block, dp);
389 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
391 union extcon_property_value property;
394 if (!port->phy_enabled) {
395 ret = phy_power_on(port->phy);
397 DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
401 port->phy_enabled = true;
404 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
405 DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
407 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
411 ret = cdn_dp_get_hpd_status(dp);
414 DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
418 ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
419 EXTCON_PROP_USB_TYPEC_POLARITY, &property);
421 DRM_DEV_ERROR(dp->dev, "get property failed\n");
425 port->lanes = cdn_dp_get_port_lanes(port);
426 ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
428 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
433 dp->active_port = port->id;
437 if (phy_power_off(port->phy))
438 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
440 port->phy_enabled = false;
443 cdn_dp_grf_write(dp, GRF_SOC_CON26,
444 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
448 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
449 struct cdn_dp_port *port)
453 if (port->phy_enabled) {
454 ret = phy_power_off(port->phy);
456 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
461 port->phy_enabled = false;
463 dp->active_port = -1;
467 static int cdn_dp_disable(struct cdn_dp_device *dp)
474 for (i = 0; i < dp->ports; i++)
475 cdn_dp_disable_phy(dp, dp->port[i]);
477 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
478 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
480 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
485 cdn_dp_set_firmware_active(dp, false);
486 cdn_dp_clk_disable(dp);
490 if (!dp->connected) {
498 static int cdn_dp_enable(struct cdn_dp_device *dp)
501 struct cdn_dp_port *port;
503 port = cdn_dp_connected_port(dp);
505 DRM_DEV_ERROR(dp->dev,
506 "Can't enable without connection\n");
513 ret = cdn_dp_clk_enable(dp);
517 ret = cdn_dp_firmware_init(dp);
519 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
520 goto err_clk_disable;
523 /* only enable the port that connected with downstream device */
524 for (i = port->id; i < dp->ports; i++) {
526 lanes = cdn_dp_get_port_lanes(port);
528 ret = cdn_dp_enable_phy(dp, port);
532 ret = cdn_dp_get_sink_capability(dp);
534 cdn_dp_disable_phy(dp, port);
537 dp->lanes = port->lanes;
544 cdn_dp_clk_disable(dp);
548 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
549 struct drm_display_mode *mode,
550 struct drm_display_mode *adjusted)
552 struct cdn_dp_device *dp = encoder_to_dp(encoder);
553 struct drm_display_info *display_info = &dp->connector.display_info;
554 struct video_info *video = &dp->video_info;
556 switch (display_info->bpc) {
558 video->color_depth = 10;
561 video->color_depth = 6;
564 video->color_depth = 8;
568 video->color_fmt = PXL_RGB;
569 video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
570 video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
572 drm_mode_copy(&dp->mode, adjusted);
575 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
577 u8 link_status[DP_LINK_STATUS_SIZE];
578 struct cdn_dp_port *port = cdn_dp_connected_port(dp);
579 u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
581 if (!port || !dp->max_rate || !dp->max_lanes)
584 if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
585 DP_LINK_STATUS_SIZE)) {
586 DRM_ERROR("Failed to get link status\n");
590 /* if link training is requested we should perform it always */
591 return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
594 static void cdn_dp_audio_handle_plugged_change(struct cdn_dp_device *dp,
598 dp->plugged_cb(dp->codec_dev, plugged);
601 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
603 struct cdn_dp_device *dp = encoder_to_dp(encoder);
606 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
608 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
612 DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
613 (ret) ? "LIT" : "BIG");
615 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
617 val = DP_SEL_VOP_LIT << 16;
619 ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
623 mutex_lock(&dp->lock);
625 ret = cdn_dp_enable(dp);
627 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
631 if (!cdn_dp_check_link_status(dp)) {
632 ret = cdn_dp_train_link(dp);
634 DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
639 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
641 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
645 ret = cdn_dp_config_video(dp);
647 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
651 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
653 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
657 cdn_dp_audio_handle_plugged_change(dp, true);
660 mutex_unlock(&dp->lock);
663 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
665 struct cdn_dp_device *dp = encoder_to_dp(encoder);
668 mutex_lock(&dp->lock);
669 cdn_dp_audio_handle_plugged_change(dp, false);
672 ret = cdn_dp_disable(dp);
674 DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
678 mutex_unlock(&dp->lock);
681 * In the following 2 cases, we need to run the event_work to re-enable
683 * 1. If there is not just one port device is connected, and remove one
684 * device from a port, the DP will be disabled here, at this case,
685 * run the event_work to re-open DP for the other port.
686 * 2. If re-training or re-config failed, the DP will be disabled here.
687 * run the event_work to re-connect it.
689 if (!dp->connected && cdn_dp_connected_port(dp))
690 schedule_work(&dp->event_work);
693 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
694 struct drm_crtc_state *crtc_state,
695 struct drm_connector_state *conn_state)
697 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
699 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
700 s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
705 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
706 .mode_set = cdn_dp_encoder_mode_set,
707 .enable = cdn_dp_encoder_enable,
708 .disable = cdn_dp_encoder_disable,
709 .atomic_check = cdn_dp_encoder_atomic_check,
712 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
714 struct device *dev = dp->dev;
715 struct device_node *np = dev->of_node;
716 struct platform_device *pdev = to_platform_device(dev);
718 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
719 if (IS_ERR(dp->grf)) {
720 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
721 return PTR_ERR(dp->grf);
724 dp->regs = devm_platform_ioremap_resource(pdev, 0);
725 if (IS_ERR(dp->regs)) {
726 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
727 return PTR_ERR(dp->regs);
730 dp->core_clk = devm_clk_get(dev, "core-clk");
731 if (IS_ERR(dp->core_clk)) {
732 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
733 return PTR_ERR(dp->core_clk);
736 dp->pclk = devm_clk_get(dev, "pclk");
737 if (IS_ERR(dp->pclk)) {
738 DRM_DEV_ERROR(dev, "cannot get pclk\n");
739 return PTR_ERR(dp->pclk);
742 dp->spdif_clk = devm_clk_get(dev, "spdif");
743 if (IS_ERR(dp->spdif_clk)) {
744 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
745 return PTR_ERR(dp->spdif_clk);
748 dp->grf_clk = devm_clk_get(dev, "grf");
749 if (IS_ERR(dp->grf_clk)) {
750 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
751 return PTR_ERR(dp->grf_clk);
754 dp->spdif_rst = devm_reset_control_get(dev, "spdif");
755 if (IS_ERR(dp->spdif_rst)) {
756 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
757 return PTR_ERR(dp->spdif_rst);
760 dp->dptx_rst = devm_reset_control_get(dev, "dptx");
761 if (IS_ERR(dp->dptx_rst)) {
762 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
763 return PTR_ERR(dp->dptx_rst);
766 dp->core_rst = devm_reset_control_get(dev, "core");
767 if (IS_ERR(dp->core_rst)) {
768 DRM_DEV_ERROR(dev, "no core reset control found\n");
769 return PTR_ERR(dp->core_rst);
772 dp->apb_rst = devm_reset_control_get(dev, "apb");
773 if (IS_ERR(dp->apb_rst)) {
774 DRM_DEV_ERROR(dev, "no apb reset control found\n");
775 return PTR_ERR(dp->apb_rst);
781 static int cdn_dp_audio_hw_params(struct device *dev, void *data,
782 struct hdmi_codec_daifmt *daifmt,
783 struct hdmi_codec_params *params)
785 struct cdn_dp_device *dp = dev_get_drvdata(dev);
786 struct audio_info audio = {
787 .sample_width = params->sample_width,
788 .sample_rate = params->sample_rate,
789 .channels = params->channels,
793 mutex_lock(&dp->lock);
799 switch (daifmt->fmt) {
801 audio.format = AFMT_I2S;
804 audio.format = AFMT_SPDIF;
807 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
812 ret = cdn_dp_audio_config(dp, &audio);
814 dp->audio_info = audio;
817 mutex_unlock(&dp->lock);
821 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
823 struct cdn_dp_device *dp = dev_get_drvdata(dev);
826 mutex_lock(&dp->lock);
830 ret = cdn_dp_audio_stop(dp, &dp->audio_info);
832 dp->audio_info.format = AFMT_UNUSED;
834 mutex_unlock(&dp->lock);
837 static int cdn_dp_audio_mute_stream(struct device *dev, void *data,
838 bool enable, int direction)
840 struct cdn_dp_device *dp = dev_get_drvdata(dev);
843 mutex_lock(&dp->lock);
849 ret = cdn_dp_audio_mute(dp, enable);
852 mutex_unlock(&dp->lock);
856 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
859 struct cdn_dp_device *dp = dev_get_drvdata(dev);
861 memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
866 static int cdn_dp_audio_hook_plugged_cb(struct device *dev, void *data,
867 hdmi_codec_plugged_cb fn,
868 struct device *codec_dev)
870 struct cdn_dp_device *dp = dev_get_drvdata(dev);
872 mutex_lock(&dp->lock);
874 dp->codec_dev = codec_dev;
875 cdn_dp_audio_handle_plugged_change(dp, dp->connected);
876 mutex_unlock(&dp->lock);
881 static const struct hdmi_codec_ops audio_codec_ops = {
882 .hw_params = cdn_dp_audio_hw_params,
883 .audio_shutdown = cdn_dp_audio_shutdown,
884 .mute_stream = cdn_dp_audio_mute_stream,
885 .get_eld = cdn_dp_audio_get_eld,
886 .hook_plugged_cb = cdn_dp_audio_hook_plugged_cb,
887 .no_capture_mute = 1,
890 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
893 struct hdmi_codec_pdata codec_data = {
896 .ops = &audio_codec_ops,
897 .max_i2s_channels = 8,
900 dp->audio_pdev = platform_device_register_data(
901 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
902 &codec_data, sizeof(codec_data));
904 return PTR_ERR_OR_ZERO(dp->audio_pdev);
907 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
910 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
911 unsigned long sleep = 1000;
913 WARN_ON(!mutex_is_locked(&dp->lock));
918 /* Drop the lock before getting the firmware to avoid blocking boot */
919 mutex_unlock(&dp->lock);
921 while (time_before(jiffies, timeout)) {
922 ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
923 if (ret == -ENOENT) {
928 DRM_DEV_ERROR(dp->dev,
929 "failed to request firmware: %d\n", ret);
933 dp->fw_loaded = true;
938 DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
941 mutex_lock(&dp->lock);
945 static void cdn_dp_pd_event_work(struct work_struct *work)
947 struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
949 struct drm_connector *connector = &dp->connector;
950 enum drm_connector_status old_status;
954 mutex_lock(&dp->lock);
959 ret = cdn_dp_request_firmware(dp);
963 dp->connected = true;
965 /* Not connected, notify userspace to disable the block */
966 if (!cdn_dp_connected_port(dp)) {
967 DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
968 dp->connected = false;
970 /* Connected but not enabled, enable the block */
971 } else if (!dp->active) {
972 DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
973 ret = cdn_dp_enable(dp);
975 DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
976 dp->connected = false;
979 /* Enabled and connected to a dongle without a sink, notify userspace */
980 } else if (!cdn_dp_check_sink_connection(dp)) {
981 DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
982 dp->connected = false;
984 /* Enabled and connected with a sink, re-train if requested */
985 } else if (!cdn_dp_check_link_status(dp)) {
986 unsigned int rate = dp->max_rate;
987 unsigned int lanes = dp->max_lanes;
988 struct drm_display_mode *mode = &dp->mode;
990 DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
991 ret = cdn_dp_train_link(dp);
993 dp->connected = false;
994 DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
998 /* If training result is changed, update the video config */
1000 (rate != dp->max_rate || lanes != dp->max_lanes)) {
1001 ret = cdn_dp_config_video(dp);
1003 dp->connected = false;
1004 DRM_DEV_ERROR(dp->dev,
1005 "Failed to config video %d\n",
1012 mutex_unlock(&dp->lock);
1014 old_status = connector->status;
1015 connector->status = connector->funcs->detect(connector, false);
1016 if (old_status != connector->status)
1017 drm_kms_helper_hotplug_event(dp->drm_dev);
1020 static int cdn_dp_pd_event(struct notifier_block *nb,
1021 unsigned long event, void *priv)
1023 struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1025 struct cdn_dp_device *dp = port->dp;
1028 * It would be nice to be able to just do the work inline right here.
1029 * However, we need to make a bunch of calls that might sleep in order
1030 * to turn on the block/phy, so use a worker instead.
1032 schedule_work(&dp->event_work);
1037 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1039 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1040 struct drm_encoder *encoder;
1041 struct drm_connector *connector;
1042 struct cdn_dp_port *port;
1043 struct drm_device *drm_dev = data;
1046 ret = cdn_dp_parse_dt(dp);
1050 dp->drm_dev = drm_dev;
1051 dp->connected = false;
1053 dp->active_port = -1;
1054 dp->fw_loaded = false;
1056 INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1058 encoder = &dp->encoder.encoder;
1060 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1062 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1064 ret = drm_simple_encoder_init(drm_dev, encoder,
1065 DRM_MODE_ENCODER_TMDS);
1067 DRM_ERROR("failed to initialize encoder with drm\n");
1071 drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1073 connector = &dp->connector;
1074 connector->polled = DRM_CONNECTOR_POLL_HPD;
1075 connector->dpms = DRM_MODE_DPMS_OFF;
1077 ret = drm_connector_init(drm_dev, connector,
1078 &cdn_dp_atomic_connector_funcs,
1079 DRM_MODE_CONNECTOR_DisplayPort);
1081 DRM_ERROR("failed to initialize connector with drm\n");
1082 goto err_free_encoder;
1085 drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1087 ret = drm_connector_attach_encoder(connector, encoder);
1089 DRM_ERROR("failed to attach connector and encoder\n");
1090 goto err_free_connector;
1093 for (i = 0; i < dp->ports; i++) {
1096 port->event_nb.notifier_call = cdn_dp_pd_event;
1097 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1102 "register EXTCON_DISP_DP notifier err\n");
1103 goto err_free_connector;
1107 pm_runtime_enable(dev);
1109 schedule_work(&dp->event_work);
1114 drm_connector_cleanup(connector);
1116 drm_encoder_cleanup(encoder);
1120 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1122 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1123 struct drm_encoder *encoder = &dp->encoder.encoder;
1124 struct drm_connector *connector = &dp->connector;
1126 cancel_work_sync(&dp->event_work);
1127 cdn_dp_encoder_disable(encoder);
1128 encoder->funcs->destroy(encoder);
1129 connector->funcs->destroy(connector);
1131 pm_runtime_disable(dev);
1133 release_firmware(dp->fw);
1138 static const struct component_ops cdn_dp_component_ops = {
1139 .bind = cdn_dp_bind,
1140 .unbind = cdn_dp_unbind,
1143 static int cdn_dp_suspend(struct device *dev)
1145 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1148 mutex_lock(&dp->lock);
1150 ret = cdn_dp_disable(dp);
1151 dp->suspended = true;
1152 mutex_unlock(&dp->lock);
1157 static __maybe_unused int cdn_dp_resume(struct device *dev)
1159 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1161 mutex_lock(&dp->lock);
1162 dp->suspended = false;
1164 schedule_work(&dp->event_work);
1165 mutex_unlock(&dp->lock);
1170 static int cdn_dp_probe(struct platform_device *pdev)
1172 struct device *dev = &pdev->dev;
1173 const struct of_device_id *match;
1174 struct cdn_dp_data *dp_data;
1175 struct cdn_dp_port *port;
1176 struct cdn_dp_device *dp;
1177 struct extcon_dev *extcon;
1182 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1187 match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1188 dp_data = (struct cdn_dp_data *)match->data;
1190 for (i = 0; i < dp_data->max_phy; i++) {
1191 extcon = extcon_get_edev_by_phandle(dev, i);
1192 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1194 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1195 PTR_ERR(phy) == -EPROBE_DEFER)
1196 return -EPROBE_DEFER;
1198 if (IS_ERR(extcon) || IS_ERR(phy))
1201 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1205 port->extcon = extcon;
1209 dp->port[dp->ports++] = port;
1213 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1217 mutex_init(&dp->lock);
1218 dev_set_drvdata(dev, dp);
1220 ret = cdn_dp_audio_codec_init(dp, dev);
1224 ret = component_add(dev, &cdn_dp_component_ops);
1226 goto err_audio_deinit;
1231 platform_device_unregister(dp->audio_pdev);
1235 static void cdn_dp_remove(struct platform_device *pdev)
1237 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1239 platform_device_unregister(dp->audio_pdev);
1240 cdn_dp_suspend(dp->dev);
1241 component_del(&pdev->dev, &cdn_dp_component_ops);
1244 static void cdn_dp_shutdown(struct platform_device *pdev)
1246 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1248 cdn_dp_suspend(dp->dev);
1251 static const struct dev_pm_ops cdn_dp_pm_ops = {
1252 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1256 struct platform_driver cdn_dp_driver = {
1257 .probe = cdn_dp_probe,
1258 .remove_new = cdn_dp_remove,
1259 .shutdown = cdn_dp_shutdown,
1262 .owner = THIS_MODULE,
1263 .of_match_table = cdn_dp_dt_ids,
1264 .pm = &cdn_dp_pm_ops,