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);
267 mutex_lock(&dp->lock);
270 /* FIXME: get rid of drm_edid_raw() */
271 const struct edid *edid = drm_edid_raw(dp->drm_edid);
273 DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
274 edid->width_cm, edid->height_cm);
278 ret = drm_edid_connector_add_modes(connector);
280 mutex_unlock(&dp->lock);
285 static enum drm_mode_status
286 cdn_dp_connector_mode_valid(struct drm_connector *connector,
287 struct drm_display_mode *mode)
289 struct cdn_dp_device *dp = connector_to_dp(connector);
290 struct drm_display_info *display_info = &dp->connector.display_info;
291 u32 requested, actual, rate, sink_max, source_max = 0;
294 /* If DP is disconnected, every mode is invalid */
298 switch (display_info->bpc) {
310 requested = mode->clock * bpc * 3 / 1000;
312 source_max = dp->lanes;
313 sink_max = drm_dp_max_lane_count(dp->dpcd);
314 lanes = min(source_max, sink_max);
316 source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
317 sink_max = drm_dp_max_link_rate(dp->dpcd);
318 rate = min(source_max, sink_max);
320 actual = rate * lanes / 100;
322 /* efficiency is about 0.8 */
323 actual = actual * 8 / 10;
325 if (requested > actual) {
326 DRM_DEV_DEBUG_KMS(dp->dev,
327 "requested=%d, actual=%d, clock=%d\n",
328 requested, actual, mode->clock);
329 return MODE_CLOCK_HIGH;
335 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
336 .get_modes = cdn_dp_connector_get_modes,
337 .mode_valid = cdn_dp_connector_mode_valid,
340 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
343 const u32 *iram_data, *dram_data;
344 const struct firmware *fw = dp->fw;
345 const struct cdn_firmware_header *hdr;
347 hdr = (struct cdn_firmware_header *)fw->data;
348 if (fw->size != le32_to_cpu(hdr->size_bytes)) {
349 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
353 iram_data = (const u32 *)(fw->data + hdr->header_size);
354 dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
356 ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
357 dram_data, hdr->dram_size);
361 ret = cdn_dp_set_firmware_active(dp, true);
363 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
367 return cdn_dp_event_config(dp);
370 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
374 if (!cdn_dp_check_sink_connection(dp))
377 ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
378 DP_RECEIVER_CAP_SIZE);
380 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
384 drm_edid_free(dp->drm_edid);
385 dp->drm_edid = drm_edid_read_custom(&dp->connector,
386 cdn_dp_get_edid_block, dp);
387 drm_edid_connector_update(&dp->connector, dp->drm_edid);
389 dp->sink_has_audio = dp->connector.display_info.has_audio;
394 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
396 union extcon_property_value property;
399 if (!port->phy_enabled) {
400 ret = phy_power_on(port->phy);
402 DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
406 port->phy_enabled = true;
409 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
410 DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
412 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
416 ret = cdn_dp_get_hpd_status(dp);
419 DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
423 ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
424 EXTCON_PROP_USB_TYPEC_POLARITY, &property);
426 DRM_DEV_ERROR(dp->dev, "get property failed\n");
430 port->lanes = cdn_dp_get_port_lanes(port);
431 ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
433 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
438 dp->active_port = port->id;
442 if (phy_power_off(port->phy))
443 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
445 port->phy_enabled = false;
448 cdn_dp_grf_write(dp, GRF_SOC_CON26,
449 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
453 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
454 struct cdn_dp_port *port)
458 if (port->phy_enabled) {
459 ret = phy_power_off(port->phy);
461 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
466 port->phy_enabled = false;
468 dp->active_port = -1;
472 static int cdn_dp_disable(struct cdn_dp_device *dp)
479 for (i = 0; i < dp->ports; i++)
480 cdn_dp_disable_phy(dp, dp->port[i]);
482 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
483 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
485 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
490 cdn_dp_set_firmware_active(dp, false);
491 cdn_dp_clk_disable(dp);
495 if (!dp->connected) {
496 drm_edid_free(dp->drm_edid);
503 static int cdn_dp_enable(struct cdn_dp_device *dp)
506 struct cdn_dp_port *port;
508 port = cdn_dp_connected_port(dp);
510 DRM_DEV_ERROR(dp->dev,
511 "Can't enable without connection\n");
518 ret = cdn_dp_clk_enable(dp);
522 ret = cdn_dp_firmware_init(dp);
524 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
525 goto err_clk_disable;
528 /* only enable the port that connected with downstream device */
529 for (i = port->id; i < dp->ports; i++) {
531 lanes = cdn_dp_get_port_lanes(port);
533 ret = cdn_dp_enable_phy(dp, port);
537 ret = cdn_dp_get_sink_capability(dp);
539 cdn_dp_disable_phy(dp, port);
542 dp->lanes = port->lanes;
549 cdn_dp_clk_disable(dp);
553 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
554 struct drm_display_mode *mode,
555 struct drm_display_mode *adjusted)
557 struct cdn_dp_device *dp = encoder_to_dp(encoder);
558 struct drm_display_info *display_info = &dp->connector.display_info;
559 struct video_info *video = &dp->video_info;
561 switch (display_info->bpc) {
563 video->color_depth = 10;
566 video->color_depth = 6;
569 video->color_depth = 8;
573 video->color_fmt = PXL_RGB;
574 video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
575 video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
577 drm_mode_copy(&dp->mode, adjusted);
580 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
582 u8 link_status[DP_LINK_STATUS_SIZE];
583 struct cdn_dp_port *port = cdn_dp_connected_port(dp);
584 u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
586 if (!port || !dp->max_rate || !dp->max_lanes)
589 if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
590 DP_LINK_STATUS_SIZE)) {
591 DRM_ERROR("Failed to get link status\n");
595 /* if link training is requested we should perform it always */
596 return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
599 static void cdn_dp_audio_handle_plugged_change(struct cdn_dp_device *dp,
603 dp->plugged_cb(dp->codec_dev, plugged);
606 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
608 struct cdn_dp_device *dp = encoder_to_dp(encoder);
611 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
613 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
617 DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
618 (ret) ? "LIT" : "BIG");
620 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
622 val = DP_SEL_VOP_LIT << 16;
624 ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
628 mutex_lock(&dp->lock);
630 ret = cdn_dp_enable(dp);
632 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
636 if (!cdn_dp_check_link_status(dp)) {
637 ret = cdn_dp_train_link(dp);
639 DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
644 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
646 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
650 ret = cdn_dp_config_video(dp);
652 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
656 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
658 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
662 cdn_dp_audio_handle_plugged_change(dp, true);
665 mutex_unlock(&dp->lock);
668 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
670 struct cdn_dp_device *dp = encoder_to_dp(encoder);
673 mutex_lock(&dp->lock);
674 cdn_dp_audio_handle_plugged_change(dp, false);
677 ret = cdn_dp_disable(dp);
679 DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
683 mutex_unlock(&dp->lock);
686 * In the following 2 cases, we need to run the event_work to re-enable
688 * 1. If there is not just one port device is connected, and remove one
689 * device from a port, the DP will be disabled here, at this case,
690 * run the event_work to re-open DP for the other port.
691 * 2. If re-training or re-config failed, the DP will be disabled here.
692 * run the event_work to re-connect it.
694 if (!dp->connected && cdn_dp_connected_port(dp))
695 schedule_work(&dp->event_work);
698 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
699 struct drm_crtc_state *crtc_state,
700 struct drm_connector_state *conn_state)
702 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
704 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
705 s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
710 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
711 .mode_set = cdn_dp_encoder_mode_set,
712 .enable = cdn_dp_encoder_enable,
713 .disable = cdn_dp_encoder_disable,
714 .atomic_check = cdn_dp_encoder_atomic_check,
717 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
719 struct device *dev = dp->dev;
720 struct device_node *np = dev->of_node;
721 struct platform_device *pdev = to_platform_device(dev);
723 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
724 if (IS_ERR(dp->grf)) {
725 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
726 return PTR_ERR(dp->grf);
729 dp->regs = devm_platform_ioremap_resource(pdev, 0);
730 if (IS_ERR(dp->regs)) {
731 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
732 return PTR_ERR(dp->regs);
735 dp->core_clk = devm_clk_get(dev, "core-clk");
736 if (IS_ERR(dp->core_clk)) {
737 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
738 return PTR_ERR(dp->core_clk);
741 dp->pclk = devm_clk_get(dev, "pclk");
742 if (IS_ERR(dp->pclk)) {
743 DRM_DEV_ERROR(dev, "cannot get pclk\n");
744 return PTR_ERR(dp->pclk);
747 dp->spdif_clk = devm_clk_get(dev, "spdif");
748 if (IS_ERR(dp->spdif_clk)) {
749 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
750 return PTR_ERR(dp->spdif_clk);
753 dp->grf_clk = devm_clk_get(dev, "grf");
754 if (IS_ERR(dp->grf_clk)) {
755 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
756 return PTR_ERR(dp->grf_clk);
759 dp->spdif_rst = devm_reset_control_get(dev, "spdif");
760 if (IS_ERR(dp->spdif_rst)) {
761 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
762 return PTR_ERR(dp->spdif_rst);
765 dp->dptx_rst = devm_reset_control_get(dev, "dptx");
766 if (IS_ERR(dp->dptx_rst)) {
767 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
768 return PTR_ERR(dp->dptx_rst);
771 dp->core_rst = devm_reset_control_get(dev, "core");
772 if (IS_ERR(dp->core_rst)) {
773 DRM_DEV_ERROR(dev, "no core reset control found\n");
774 return PTR_ERR(dp->core_rst);
777 dp->apb_rst = devm_reset_control_get(dev, "apb");
778 if (IS_ERR(dp->apb_rst)) {
779 DRM_DEV_ERROR(dev, "no apb reset control found\n");
780 return PTR_ERR(dp->apb_rst);
786 static int cdn_dp_audio_hw_params(struct device *dev, void *data,
787 struct hdmi_codec_daifmt *daifmt,
788 struct hdmi_codec_params *params)
790 struct cdn_dp_device *dp = dev_get_drvdata(dev);
791 struct audio_info audio = {
792 .sample_width = params->sample_width,
793 .sample_rate = params->sample_rate,
794 .channels = params->channels,
798 mutex_lock(&dp->lock);
804 switch (daifmt->fmt) {
806 audio.format = AFMT_I2S;
809 audio.format = AFMT_SPDIF;
812 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
817 ret = cdn_dp_audio_config(dp, &audio);
819 dp->audio_info = audio;
822 mutex_unlock(&dp->lock);
826 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
828 struct cdn_dp_device *dp = dev_get_drvdata(dev);
831 mutex_lock(&dp->lock);
835 ret = cdn_dp_audio_stop(dp, &dp->audio_info);
837 dp->audio_info.format = AFMT_UNUSED;
839 mutex_unlock(&dp->lock);
842 static int cdn_dp_audio_mute_stream(struct device *dev, void *data,
843 bool enable, int direction)
845 struct cdn_dp_device *dp = dev_get_drvdata(dev);
848 mutex_lock(&dp->lock);
854 ret = cdn_dp_audio_mute(dp, enable);
857 mutex_unlock(&dp->lock);
861 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
864 struct cdn_dp_device *dp = dev_get_drvdata(dev);
866 memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
871 static int cdn_dp_audio_hook_plugged_cb(struct device *dev, void *data,
872 hdmi_codec_plugged_cb fn,
873 struct device *codec_dev)
875 struct cdn_dp_device *dp = dev_get_drvdata(dev);
877 mutex_lock(&dp->lock);
879 dp->codec_dev = codec_dev;
880 cdn_dp_audio_handle_plugged_change(dp, dp->connected);
881 mutex_unlock(&dp->lock);
886 static const struct hdmi_codec_ops audio_codec_ops = {
887 .hw_params = cdn_dp_audio_hw_params,
888 .audio_shutdown = cdn_dp_audio_shutdown,
889 .mute_stream = cdn_dp_audio_mute_stream,
890 .get_eld = cdn_dp_audio_get_eld,
891 .hook_plugged_cb = cdn_dp_audio_hook_plugged_cb,
892 .no_capture_mute = 1,
895 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
898 struct hdmi_codec_pdata codec_data = {
901 .ops = &audio_codec_ops,
902 .max_i2s_channels = 8,
905 dp->audio_pdev = platform_device_register_data(
906 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
907 &codec_data, sizeof(codec_data));
909 return PTR_ERR_OR_ZERO(dp->audio_pdev);
912 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
915 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
916 unsigned long sleep = 1000;
918 WARN_ON(!mutex_is_locked(&dp->lock));
923 /* Drop the lock before getting the firmware to avoid blocking boot */
924 mutex_unlock(&dp->lock);
926 while (time_before(jiffies, timeout)) {
927 ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
928 if (ret == -ENOENT) {
933 DRM_DEV_ERROR(dp->dev,
934 "failed to request firmware: %d\n", ret);
938 dp->fw_loaded = true;
943 DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
946 mutex_lock(&dp->lock);
950 static void cdn_dp_pd_event_work(struct work_struct *work)
952 struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
954 struct drm_connector *connector = &dp->connector;
955 enum drm_connector_status old_status;
959 mutex_lock(&dp->lock);
964 ret = cdn_dp_request_firmware(dp);
968 dp->connected = true;
970 /* Not connected, notify userspace to disable the block */
971 if (!cdn_dp_connected_port(dp)) {
972 DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
973 dp->connected = false;
975 /* Connected but not enabled, enable the block */
976 } else if (!dp->active) {
977 DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
978 ret = cdn_dp_enable(dp);
980 DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
981 dp->connected = false;
984 /* Enabled and connected to a dongle without a sink, notify userspace */
985 } else if (!cdn_dp_check_sink_connection(dp)) {
986 DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
987 dp->connected = false;
989 /* Enabled and connected with a sink, re-train if requested */
990 } else if (!cdn_dp_check_link_status(dp)) {
991 unsigned int rate = dp->max_rate;
992 unsigned int lanes = dp->max_lanes;
993 struct drm_display_mode *mode = &dp->mode;
995 DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
996 ret = cdn_dp_train_link(dp);
998 dp->connected = false;
999 DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
1003 /* If training result is changed, update the video config */
1005 (rate != dp->max_rate || lanes != dp->max_lanes)) {
1006 ret = cdn_dp_config_video(dp);
1008 dp->connected = false;
1009 DRM_DEV_ERROR(dp->dev,
1010 "Failed to config video %d\n",
1017 mutex_unlock(&dp->lock);
1019 old_status = connector->status;
1020 connector->status = connector->funcs->detect(connector, false);
1021 if (old_status != connector->status)
1022 drm_kms_helper_hotplug_event(dp->drm_dev);
1025 static int cdn_dp_pd_event(struct notifier_block *nb,
1026 unsigned long event, void *priv)
1028 struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1030 struct cdn_dp_device *dp = port->dp;
1033 * It would be nice to be able to just do the work inline right here.
1034 * However, we need to make a bunch of calls that might sleep in order
1035 * to turn on the block/phy, so use a worker instead.
1037 schedule_work(&dp->event_work);
1042 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1044 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1045 struct drm_encoder *encoder;
1046 struct drm_connector *connector;
1047 struct cdn_dp_port *port;
1048 struct drm_device *drm_dev = data;
1051 ret = cdn_dp_parse_dt(dp);
1055 dp->drm_dev = drm_dev;
1056 dp->connected = false;
1058 dp->active_port = -1;
1059 dp->fw_loaded = false;
1061 INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1063 encoder = &dp->encoder.encoder;
1065 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1067 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1069 ret = drm_simple_encoder_init(drm_dev, encoder,
1070 DRM_MODE_ENCODER_TMDS);
1072 DRM_ERROR("failed to initialize encoder with drm\n");
1076 drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1078 connector = &dp->connector;
1079 connector->polled = DRM_CONNECTOR_POLL_HPD;
1080 connector->dpms = DRM_MODE_DPMS_OFF;
1082 ret = drm_connector_init(drm_dev, connector,
1083 &cdn_dp_atomic_connector_funcs,
1084 DRM_MODE_CONNECTOR_DisplayPort);
1086 DRM_ERROR("failed to initialize connector with drm\n");
1087 goto err_free_encoder;
1090 drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1092 ret = drm_connector_attach_encoder(connector, encoder);
1094 DRM_ERROR("failed to attach connector and encoder\n");
1095 goto err_free_connector;
1098 for (i = 0; i < dp->ports; i++) {
1101 port->event_nb.notifier_call = cdn_dp_pd_event;
1102 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1107 "register EXTCON_DISP_DP notifier err\n");
1108 goto err_free_connector;
1112 pm_runtime_enable(dev);
1114 schedule_work(&dp->event_work);
1119 drm_connector_cleanup(connector);
1121 drm_encoder_cleanup(encoder);
1125 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1127 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1128 struct drm_encoder *encoder = &dp->encoder.encoder;
1129 struct drm_connector *connector = &dp->connector;
1131 cancel_work_sync(&dp->event_work);
1132 cdn_dp_encoder_disable(encoder);
1133 encoder->funcs->destroy(encoder);
1134 connector->funcs->destroy(connector);
1136 pm_runtime_disable(dev);
1138 release_firmware(dp->fw);
1139 drm_edid_free(dp->drm_edid);
1140 dp->drm_edid = NULL;
1143 static const struct component_ops cdn_dp_component_ops = {
1144 .bind = cdn_dp_bind,
1145 .unbind = cdn_dp_unbind,
1148 static int cdn_dp_suspend(struct device *dev)
1150 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1153 mutex_lock(&dp->lock);
1155 ret = cdn_dp_disable(dp);
1156 dp->suspended = true;
1157 mutex_unlock(&dp->lock);
1162 static __maybe_unused int cdn_dp_resume(struct device *dev)
1164 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1166 mutex_lock(&dp->lock);
1167 dp->suspended = false;
1169 schedule_work(&dp->event_work);
1170 mutex_unlock(&dp->lock);
1175 static int cdn_dp_probe(struct platform_device *pdev)
1177 struct device *dev = &pdev->dev;
1178 const struct of_device_id *match;
1179 struct cdn_dp_data *dp_data;
1180 struct cdn_dp_port *port;
1181 struct cdn_dp_device *dp;
1182 struct extcon_dev *extcon;
1187 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1192 match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1193 dp_data = (struct cdn_dp_data *)match->data;
1195 for (i = 0; i < dp_data->max_phy; i++) {
1196 extcon = extcon_get_edev_by_phandle(dev, i);
1197 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1199 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1200 PTR_ERR(phy) == -EPROBE_DEFER)
1201 return -EPROBE_DEFER;
1203 if (IS_ERR(extcon) || IS_ERR(phy))
1206 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1210 port->extcon = extcon;
1214 dp->port[dp->ports++] = port;
1218 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1222 mutex_init(&dp->lock);
1223 dev_set_drvdata(dev, dp);
1225 ret = cdn_dp_audio_codec_init(dp, dev);
1229 ret = component_add(dev, &cdn_dp_component_ops);
1231 goto err_audio_deinit;
1236 platform_device_unregister(dp->audio_pdev);
1240 static void cdn_dp_remove(struct platform_device *pdev)
1242 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1244 platform_device_unregister(dp->audio_pdev);
1245 cdn_dp_suspend(dp->dev);
1246 component_del(&pdev->dev, &cdn_dp_component_ops);
1249 static void cdn_dp_shutdown(struct platform_device *pdev)
1251 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1253 cdn_dp_suspend(dp->dev);
1256 static const struct dev_pm_ops cdn_dp_pm_ops = {
1257 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1261 struct platform_driver cdn_dp_driver = {
1262 .probe = cdn_dp_probe,
1263 .remove_new = cdn_dp_remove,
1264 .shutdown = cdn_dp_shutdown,
1267 .of_match_table = cdn_dp_dt_ids,
1268 .pm = &cdn_dp_pm_ops,