1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 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);
267 mutex_lock(&dp->lock);
269 ret = drm_edid_connector_add_modes(connector);
271 mutex_unlock(&dp->lock);
276 static enum drm_mode_status
277 cdn_dp_connector_mode_valid(struct drm_connector *connector,
278 struct drm_display_mode *mode)
280 struct cdn_dp_device *dp = connector_to_dp(connector);
281 struct drm_display_info *display_info = &dp->connector.display_info;
282 u32 requested, actual, rate, sink_max, source_max = 0;
285 /* If DP is disconnected, every mode is invalid */
289 switch (display_info->bpc) {
301 requested = mode->clock * bpc * 3 / 1000;
303 source_max = dp->lanes;
304 sink_max = drm_dp_max_lane_count(dp->dpcd);
305 lanes = min(source_max, sink_max);
307 source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
308 sink_max = drm_dp_max_link_rate(dp->dpcd);
309 rate = min(source_max, sink_max);
311 actual = rate * lanes / 100;
313 /* efficiency is about 0.8 */
314 actual = actual * 8 / 10;
316 if (requested > actual) {
317 DRM_DEV_DEBUG_KMS(dp->dev,
318 "requested=%d, actual=%d, clock=%d\n",
319 requested, actual, mode->clock);
320 return MODE_CLOCK_HIGH;
326 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
327 .get_modes = cdn_dp_connector_get_modes,
328 .mode_valid = cdn_dp_connector_mode_valid,
331 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
334 const u32 *iram_data, *dram_data;
335 const struct firmware *fw = dp->fw;
336 const struct cdn_firmware_header *hdr;
338 hdr = (struct cdn_firmware_header *)fw->data;
339 if (fw->size != le32_to_cpu(hdr->size_bytes)) {
340 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
344 iram_data = (const u32 *)(fw->data + hdr->header_size);
345 dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
347 ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
348 dram_data, hdr->dram_size);
352 ret = cdn_dp_set_firmware_active(dp, true);
354 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
358 return cdn_dp_event_config(dp);
361 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
363 const struct drm_display_info *info = &dp->connector.display_info;
366 if (!cdn_dp_check_sink_connection(dp))
369 ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
370 DP_RECEIVER_CAP_SIZE);
372 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
376 drm_edid_free(dp->drm_edid);
377 dp->drm_edid = drm_edid_read_custom(&dp->connector,
378 cdn_dp_get_edid_block, dp);
379 drm_edid_connector_update(&dp->connector, dp->drm_edid);
381 dp->sink_has_audio = info->has_audio;
384 DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
385 info->width_mm / 10, info->height_mm / 10);
390 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
392 union extcon_property_value property;
395 if (!port->phy_enabled) {
396 ret = phy_power_on(port->phy);
398 DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
402 port->phy_enabled = true;
405 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
406 DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
408 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
412 ret = cdn_dp_get_hpd_status(dp);
415 DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
419 ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
420 EXTCON_PROP_USB_TYPEC_POLARITY, &property);
422 DRM_DEV_ERROR(dp->dev, "get property failed\n");
426 port->lanes = cdn_dp_get_port_lanes(port);
427 ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
429 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
434 dp->active_port = port->id;
438 if (phy_power_off(port->phy))
439 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
441 port->phy_enabled = false;
444 cdn_dp_grf_write(dp, GRF_SOC_CON26,
445 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
449 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
450 struct cdn_dp_port *port)
454 if (port->phy_enabled) {
455 ret = phy_power_off(port->phy);
457 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
462 port->phy_enabled = false;
464 dp->active_port = -1;
468 static int cdn_dp_disable(struct cdn_dp_device *dp)
475 for (i = 0; i < dp->ports; i++)
476 cdn_dp_disable_phy(dp, dp->port[i]);
478 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
479 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
481 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
486 cdn_dp_set_firmware_active(dp, false);
487 cdn_dp_clk_disable(dp);
491 if (!dp->connected) {
492 drm_edid_free(dp->drm_edid);
499 static int cdn_dp_enable(struct cdn_dp_device *dp)
502 struct cdn_dp_port *port;
504 port = cdn_dp_connected_port(dp);
506 DRM_DEV_ERROR(dp->dev,
507 "Can't enable without connection\n");
514 ret = cdn_dp_clk_enable(dp);
518 ret = cdn_dp_firmware_init(dp);
520 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
521 goto err_clk_disable;
524 /* only enable the port that connected with downstream device */
525 for (i = port->id; i < dp->ports; i++) {
527 lanes = cdn_dp_get_port_lanes(port);
529 ret = cdn_dp_enable_phy(dp, port);
533 ret = cdn_dp_get_sink_capability(dp);
535 cdn_dp_disable_phy(dp, port);
538 dp->lanes = port->lanes;
545 cdn_dp_clk_disable(dp);
549 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
550 struct drm_display_mode *mode,
551 struct drm_display_mode *adjusted)
553 struct cdn_dp_device *dp = encoder_to_dp(encoder);
554 struct drm_display_info *display_info = &dp->connector.display_info;
555 struct video_info *video = &dp->video_info;
557 switch (display_info->bpc) {
559 video->color_depth = 10;
562 video->color_depth = 6;
565 video->color_depth = 8;
569 video->color_fmt = PXL_RGB;
570 video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
571 video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
573 drm_mode_copy(&dp->mode, adjusted);
576 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
578 u8 link_status[DP_LINK_STATUS_SIZE];
579 struct cdn_dp_port *port = cdn_dp_connected_port(dp);
580 u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
582 if (!port || !dp->max_rate || !dp->max_lanes)
585 if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
586 DP_LINK_STATUS_SIZE)) {
587 DRM_ERROR("Failed to get link status\n");
591 /* if link training is requested we should perform it always */
592 return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
595 static void cdn_dp_audio_handle_plugged_change(struct cdn_dp_device *dp,
599 dp->plugged_cb(dp->codec_dev, plugged);
602 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
604 struct cdn_dp_device *dp = encoder_to_dp(encoder);
607 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
609 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
613 DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
614 (ret) ? "LIT" : "BIG");
616 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
618 val = DP_SEL_VOP_LIT << 16;
620 ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
624 mutex_lock(&dp->lock);
626 ret = cdn_dp_enable(dp);
628 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
632 if (!cdn_dp_check_link_status(dp)) {
633 ret = cdn_dp_train_link(dp);
635 DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
640 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
642 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
646 ret = cdn_dp_config_video(dp);
648 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
652 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
654 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
658 cdn_dp_audio_handle_plugged_change(dp, true);
661 mutex_unlock(&dp->lock);
664 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
666 struct cdn_dp_device *dp = encoder_to_dp(encoder);
669 mutex_lock(&dp->lock);
670 cdn_dp_audio_handle_plugged_change(dp, false);
673 ret = cdn_dp_disable(dp);
675 DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
679 mutex_unlock(&dp->lock);
682 * In the following 2 cases, we need to run the event_work to re-enable
684 * 1. If there is not just one port device is connected, and remove one
685 * device from a port, the DP will be disabled here, at this case,
686 * run the event_work to re-open DP for the other port.
687 * 2. If re-training or re-config failed, the DP will be disabled here.
688 * run the event_work to re-connect it.
690 if (!dp->connected && cdn_dp_connected_port(dp))
691 schedule_work(&dp->event_work);
694 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
695 struct drm_crtc_state *crtc_state,
696 struct drm_connector_state *conn_state)
698 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
700 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
701 s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
706 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
707 .mode_set = cdn_dp_encoder_mode_set,
708 .enable = cdn_dp_encoder_enable,
709 .disable = cdn_dp_encoder_disable,
710 .atomic_check = cdn_dp_encoder_atomic_check,
713 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
715 struct device *dev = dp->dev;
716 struct device_node *np = dev->of_node;
717 struct platform_device *pdev = to_platform_device(dev);
719 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
720 if (IS_ERR(dp->grf)) {
721 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
722 return PTR_ERR(dp->grf);
725 dp->regs = devm_platform_ioremap_resource(pdev, 0);
726 if (IS_ERR(dp->regs)) {
727 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
728 return PTR_ERR(dp->regs);
731 dp->core_clk = devm_clk_get(dev, "core-clk");
732 if (IS_ERR(dp->core_clk)) {
733 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
734 return PTR_ERR(dp->core_clk);
737 dp->pclk = devm_clk_get(dev, "pclk");
738 if (IS_ERR(dp->pclk)) {
739 DRM_DEV_ERROR(dev, "cannot get pclk\n");
740 return PTR_ERR(dp->pclk);
743 dp->spdif_clk = devm_clk_get(dev, "spdif");
744 if (IS_ERR(dp->spdif_clk)) {
745 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
746 return PTR_ERR(dp->spdif_clk);
749 dp->grf_clk = devm_clk_get(dev, "grf");
750 if (IS_ERR(dp->grf_clk)) {
751 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
752 return PTR_ERR(dp->grf_clk);
755 dp->spdif_rst = devm_reset_control_get(dev, "spdif");
756 if (IS_ERR(dp->spdif_rst)) {
757 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
758 return PTR_ERR(dp->spdif_rst);
761 dp->dptx_rst = devm_reset_control_get(dev, "dptx");
762 if (IS_ERR(dp->dptx_rst)) {
763 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
764 return PTR_ERR(dp->dptx_rst);
767 dp->core_rst = devm_reset_control_get(dev, "core");
768 if (IS_ERR(dp->core_rst)) {
769 DRM_DEV_ERROR(dev, "no core reset control found\n");
770 return PTR_ERR(dp->core_rst);
773 dp->apb_rst = devm_reset_control_get(dev, "apb");
774 if (IS_ERR(dp->apb_rst)) {
775 DRM_DEV_ERROR(dev, "no apb reset control found\n");
776 return PTR_ERR(dp->apb_rst);
782 static int cdn_dp_audio_hw_params(struct device *dev, void *data,
783 struct hdmi_codec_daifmt *daifmt,
784 struct hdmi_codec_params *params)
786 struct cdn_dp_device *dp = dev_get_drvdata(dev);
787 struct audio_info audio = {
788 .sample_width = params->sample_width,
789 .sample_rate = params->sample_rate,
790 .channels = params->channels,
794 mutex_lock(&dp->lock);
800 switch (daifmt->fmt) {
802 audio.format = AFMT_I2S;
805 audio.format = AFMT_SPDIF;
808 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
813 ret = cdn_dp_audio_config(dp, &audio);
815 dp->audio_info = audio;
818 mutex_unlock(&dp->lock);
822 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
824 struct cdn_dp_device *dp = dev_get_drvdata(dev);
827 mutex_lock(&dp->lock);
831 ret = cdn_dp_audio_stop(dp, &dp->audio_info);
833 dp->audio_info.format = AFMT_UNUSED;
835 mutex_unlock(&dp->lock);
838 static int cdn_dp_audio_mute_stream(struct device *dev, void *data,
839 bool enable, int direction)
841 struct cdn_dp_device *dp = dev_get_drvdata(dev);
844 mutex_lock(&dp->lock);
850 ret = cdn_dp_audio_mute(dp, enable);
853 mutex_unlock(&dp->lock);
857 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
860 struct cdn_dp_device *dp = dev_get_drvdata(dev);
862 memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
867 static int cdn_dp_audio_hook_plugged_cb(struct device *dev, void *data,
868 hdmi_codec_plugged_cb fn,
869 struct device *codec_dev)
871 struct cdn_dp_device *dp = dev_get_drvdata(dev);
873 mutex_lock(&dp->lock);
875 dp->codec_dev = codec_dev;
876 cdn_dp_audio_handle_plugged_change(dp, dp->connected);
877 mutex_unlock(&dp->lock);
882 static const struct hdmi_codec_ops audio_codec_ops = {
883 .hw_params = cdn_dp_audio_hw_params,
884 .audio_shutdown = cdn_dp_audio_shutdown,
885 .mute_stream = cdn_dp_audio_mute_stream,
886 .get_eld = cdn_dp_audio_get_eld,
887 .hook_plugged_cb = cdn_dp_audio_hook_plugged_cb,
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,
898 .no_capture_mute = 1,
901 dp->audio_pdev = platform_device_register_data(
902 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
903 &codec_data, sizeof(codec_data));
905 return PTR_ERR_OR_ZERO(dp->audio_pdev);
908 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
911 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
912 unsigned long sleep = 1000;
914 WARN_ON(!mutex_is_locked(&dp->lock));
919 /* Drop the lock before getting the firmware to avoid blocking boot */
920 mutex_unlock(&dp->lock);
922 while (time_before(jiffies, timeout)) {
923 ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
924 if (ret == -ENOENT) {
929 DRM_DEV_ERROR(dp->dev,
930 "failed to request firmware: %d\n", ret);
934 dp->fw_loaded = true;
939 DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
942 mutex_lock(&dp->lock);
946 static void cdn_dp_pd_event_work(struct work_struct *work)
948 struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
952 mutex_lock(&dp->lock);
957 ret = cdn_dp_request_firmware(dp);
961 dp->connected = true;
963 /* Not connected, notify userspace to disable the block */
964 if (!cdn_dp_connected_port(dp)) {
965 DRM_DEV_INFO(dp->dev, "Not connected; disabling cdn\n");
966 dp->connected = false;
968 /* Connected but not enabled, enable the block */
969 } else if (!dp->active) {
970 DRM_DEV_INFO(dp->dev, "Connected, not enabled; enabling cdn\n");
971 ret = cdn_dp_enable(dp);
973 DRM_DEV_ERROR(dp->dev, "Enabling dp failed: %d\n", ret);
974 dp->connected = false;
977 /* Enabled and connected to a dongle without a sink, notify userspace */
978 } else if (!cdn_dp_check_sink_connection(dp)) {
979 DRM_DEV_INFO(dp->dev, "Connected without sink; assert hpd\n");
980 dp->connected = false;
982 /* Enabled and connected with a sink, re-train if requested */
983 } else if (!cdn_dp_check_link_status(dp)) {
984 unsigned int rate = dp->max_rate;
985 unsigned int lanes = dp->max_lanes;
986 struct drm_display_mode *mode = &dp->mode;
988 DRM_DEV_INFO(dp->dev, "Connected with sink; re-train link\n");
989 ret = cdn_dp_train_link(dp);
991 dp->connected = false;
992 DRM_DEV_ERROR(dp->dev, "Training link failed: %d\n", ret);
996 /* If training result is changed, update the video config */
998 (rate != dp->max_rate || lanes != dp->max_lanes)) {
999 ret = cdn_dp_config_video(dp);
1001 dp->connected = false;
1002 DRM_DEV_ERROR(dp->dev, "Failed to configure video: %d\n", ret);
1008 mutex_unlock(&dp->lock);
1009 drm_connector_helper_hpd_irq_event(&dp->connector);
1012 static int cdn_dp_pd_event(struct notifier_block *nb,
1013 unsigned long event, void *priv)
1015 struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1017 struct cdn_dp_device *dp = port->dp;
1020 * It would be nice to be able to just do the work inline right here.
1021 * However, we need to make a bunch of calls that might sleep in order
1022 * to turn on the block/phy, so use a worker instead.
1024 schedule_work(&dp->event_work);
1029 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1031 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1032 struct drm_encoder *encoder;
1033 struct drm_connector *connector;
1034 struct cdn_dp_port *port;
1035 struct drm_device *drm_dev = data;
1038 ret = cdn_dp_parse_dt(dp);
1042 dp->drm_dev = drm_dev;
1043 dp->connected = false;
1045 dp->active_port = -1;
1046 dp->fw_loaded = false;
1048 INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1050 encoder = &dp->encoder.encoder;
1052 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1054 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1056 ret = drm_simple_encoder_init(drm_dev, encoder,
1057 DRM_MODE_ENCODER_TMDS);
1059 DRM_ERROR("failed to initialize encoder with drm\n");
1063 drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1065 connector = &dp->connector;
1066 connector->polled = DRM_CONNECTOR_POLL_HPD;
1067 connector->dpms = DRM_MODE_DPMS_OFF;
1069 ret = drm_connector_init(drm_dev, connector,
1070 &cdn_dp_atomic_connector_funcs,
1071 DRM_MODE_CONNECTOR_DisplayPort);
1073 DRM_ERROR("failed to initialize connector with drm\n");
1074 goto err_free_encoder;
1077 drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1079 ret = drm_connector_attach_encoder(connector, encoder);
1081 DRM_ERROR("failed to attach connector and encoder\n");
1082 goto err_free_connector;
1085 for (i = 0; i < dp->ports; i++) {
1088 port->event_nb.notifier_call = cdn_dp_pd_event;
1089 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1094 "register EXTCON_DISP_DP notifier err\n");
1095 goto err_free_connector;
1099 pm_runtime_enable(dev);
1101 schedule_work(&dp->event_work);
1106 drm_connector_cleanup(connector);
1108 drm_encoder_cleanup(encoder);
1112 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1114 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1115 struct drm_encoder *encoder = &dp->encoder.encoder;
1116 struct drm_connector *connector = &dp->connector;
1118 cancel_work_sync(&dp->event_work);
1119 cdn_dp_encoder_disable(encoder);
1120 encoder->funcs->destroy(encoder);
1121 connector->funcs->destroy(connector);
1123 pm_runtime_disable(dev);
1125 release_firmware(dp->fw);
1126 drm_edid_free(dp->drm_edid);
1127 dp->drm_edid = NULL;
1130 static const struct component_ops cdn_dp_component_ops = {
1131 .bind = cdn_dp_bind,
1132 .unbind = cdn_dp_unbind,
1135 static int cdn_dp_suspend(struct device *dev)
1137 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1140 mutex_lock(&dp->lock);
1142 ret = cdn_dp_disable(dp);
1143 dp->suspended = true;
1144 mutex_unlock(&dp->lock);
1149 static __maybe_unused int cdn_dp_resume(struct device *dev)
1151 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1153 mutex_lock(&dp->lock);
1154 dp->suspended = false;
1156 schedule_work(&dp->event_work);
1157 mutex_unlock(&dp->lock);
1162 static int cdn_dp_probe(struct platform_device *pdev)
1164 struct device *dev = &pdev->dev;
1165 const struct of_device_id *match;
1166 struct cdn_dp_data *dp_data;
1167 struct cdn_dp_port *port;
1168 struct cdn_dp_device *dp;
1169 struct extcon_dev *extcon;
1174 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1179 match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1180 dp_data = (struct cdn_dp_data *)match->data;
1182 for (i = 0; i < dp_data->max_phy; i++) {
1183 extcon = extcon_get_edev_by_phandle(dev, i);
1184 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1186 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1187 PTR_ERR(phy) == -EPROBE_DEFER)
1188 return -EPROBE_DEFER;
1190 if (IS_ERR(extcon) || IS_ERR(phy))
1193 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1197 port->extcon = extcon;
1201 dp->port[dp->ports++] = port;
1205 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1209 mutex_init(&dp->lock);
1210 dev_set_drvdata(dev, dp);
1212 ret = cdn_dp_audio_codec_init(dp, dev);
1216 ret = component_add(dev, &cdn_dp_component_ops);
1218 goto err_audio_deinit;
1223 platform_device_unregister(dp->audio_pdev);
1227 static void cdn_dp_remove(struct platform_device *pdev)
1229 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1231 platform_device_unregister(dp->audio_pdev);
1232 cdn_dp_suspend(dp->dev);
1233 component_del(&pdev->dev, &cdn_dp_component_ops);
1236 static void cdn_dp_shutdown(struct platform_device *pdev)
1238 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1240 cdn_dp_suspend(dp->dev);
1243 static const struct dev_pm_ops cdn_dp_pm_ops = {
1244 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1248 struct platform_driver cdn_dp_driver = {
1249 .probe = cdn_dp_probe,
1250 .remove = cdn_dp_remove,
1251 .shutdown = cdn_dp_shutdown,
1254 .of_match_table = cdn_dp_dt_ids,
1255 .pm = &cdn_dp_pm_ops,