Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
0004  * Author: Chris Zhong <zyw@rock-chips.com>
0005  */
0006 
0007 #include <linux/clk.h>
0008 #include <linux/component.h>
0009 #include <linux/extcon.h>
0010 #include <linux/firmware.h>
0011 #include <linux/mfd/syscon.h>
0012 #include <linux/phy/phy.h>
0013 #include <linux/regmap.h>
0014 #include <linux/reset.h>
0015 
0016 #include <sound/hdmi-codec.h>
0017 
0018 #include <drm/display/drm_dp_helper.h>
0019 #include <drm/drm_atomic_helper.h>
0020 #include <drm/drm_edid.h>
0021 #include <drm/drm_of.h>
0022 #include <drm/drm_probe_helper.h>
0023 #include <drm/drm_simple_kms_helper.h>
0024 
0025 #include "cdn-dp-core.h"
0026 #include "cdn-dp-reg.h"
0027 #include "rockchip_drm_vop.h"
0028 
0029 static inline struct cdn_dp_device *connector_to_dp(struct drm_connector *connector)
0030 {
0031     return container_of(connector, struct cdn_dp_device, connector);
0032 }
0033 
0034 static inline struct cdn_dp_device *encoder_to_dp(struct drm_encoder *encoder)
0035 {
0036     struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
0037 
0038     return container_of(rkencoder, struct cdn_dp_device, encoder);
0039 }
0040 
0041 #define GRF_SOC_CON9        0x6224
0042 #define DP_SEL_VOP_LIT      BIT(12)
0043 #define GRF_SOC_CON26       0x6268
0044 #define DPTX_HPD_SEL        (3 << 12)
0045 #define DPTX_HPD_DEL        (2 << 12)
0046 #define DPTX_HPD_SEL_MASK   (3 << 28)
0047 
0048 #define CDN_FW_TIMEOUT_MS   (64 * 1000)
0049 #define CDN_DPCD_TIMEOUT_MS 5000
0050 #define CDN_DP_FIRMWARE     "rockchip/dptx.bin"
0051 MODULE_FIRMWARE(CDN_DP_FIRMWARE);
0052 
0053 struct cdn_dp_data {
0054     u8 max_phy;
0055 };
0056 
0057 static struct cdn_dp_data rk3399_cdn_dp = {
0058     .max_phy = 2,
0059 };
0060 
0061 static const struct of_device_id cdn_dp_dt_ids[] = {
0062     { .compatible = "rockchip,rk3399-cdn-dp",
0063         .data = (void *)&rk3399_cdn_dp },
0064     {}
0065 };
0066 
0067 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
0068 
0069 static int cdn_dp_grf_write(struct cdn_dp_device *dp,
0070                 unsigned int reg, unsigned int val)
0071 {
0072     int ret;
0073 
0074     ret = clk_prepare_enable(dp->grf_clk);
0075     if (ret) {
0076         DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
0077         return ret;
0078     }
0079 
0080     ret = regmap_write(dp->grf, reg, val);
0081     if (ret) {
0082         DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
0083         clk_disable_unprepare(dp->grf_clk);
0084         return ret;
0085     }
0086 
0087     clk_disable_unprepare(dp->grf_clk);
0088 
0089     return 0;
0090 }
0091 
0092 static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
0093 {
0094     int ret;
0095     unsigned long rate;
0096 
0097     ret = clk_prepare_enable(dp->pclk);
0098     if (ret < 0) {
0099         DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret);
0100         goto err_pclk;
0101     }
0102 
0103     ret = clk_prepare_enable(dp->core_clk);
0104     if (ret < 0) {
0105         DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret);
0106         goto err_core_clk;
0107     }
0108 
0109     ret = pm_runtime_get_sync(dp->dev);
0110     if (ret < 0) {
0111         DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret);
0112         goto err_pm_runtime_get;
0113     }
0114 
0115     reset_control_assert(dp->core_rst);
0116     reset_control_assert(dp->dptx_rst);
0117     reset_control_assert(dp->apb_rst);
0118     reset_control_deassert(dp->core_rst);
0119     reset_control_deassert(dp->dptx_rst);
0120     reset_control_deassert(dp->apb_rst);
0121 
0122     rate = clk_get_rate(dp->core_clk);
0123     if (!rate) {
0124         DRM_DEV_ERROR(dp->dev, "get clk rate failed\n");
0125         ret = -EINVAL;
0126         goto err_set_rate;
0127     }
0128 
0129     cdn_dp_set_fw_clk(dp, rate);
0130     cdn_dp_clock_reset(dp);
0131 
0132     return 0;
0133 
0134 err_set_rate:
0135     pm_runtime_put(dp->dev);
0136 err_pm_runtime_get:
0137     clk_disable_unprepare(dp->core_clk);
0138 err_core_clk:
0139     clk_disable_unprepare(dp->pclk);
0140 err_pclk:
0141     return ret;
0142 }
0143 
0144 static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
0145 {
0146     pm_runtime_put_sync(dp->dev);
0147     clk_disable_unprepare(dp->pclk);
0148     clk_disable_unprepare(dp->core_clk);
0149 }
0150 
0151 static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
0152 {
0153     struct extcon_dev *edev = port->extcon;
0154     union extcon_property_value property;
0155     int dptx;
0156     u8 lanes;
0157 
0158     dptx = extcon_get_state(edev, EXTCON_DISP_DP);
0159     if (dptx > 0) {
0160         extcon_get_property(edev, EXTCON_DISP_DP,
0161                     EXTCON_PROP_USB_SS, &property);
0162         if (property.intval)
0163             lanes = 2;
0164         else
0165             lanes = 4;
0166     } else {
0167         lanes = 0;
0168     }
0169 
0170     return lanes;
0171 }
0172 
0173 static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
0174 {
0175     int ret;
0176     u8 value;
0177 
0178     *sink_count = 0;
0179     ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
0180     if (ret)
0181         return ret;
0182 
0183     *sink_count = DP_GET_SINK_COUNT(value);
0184     return 0;
0185 }
0186 
0187 static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
0188 {
0189     struct cdn_dp_port *port;
0190     int i, lanes;
0191 
0192     for (i = 0; i < dp->ports; i++) {
0193         port = dp->port[i];
0194         lanes = cdn_dp_get_port_lanes(port);
0195         if (lanes)
0196             return port;
0197     }
0198     return NULL;
0199 }
0200 
0201 static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
0202 {
0203     unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
0204     struct cdn_dp_port *port;
0205     u8 sink_count = 0;
0206 
0207     if (dp->active_port < 0 || dp->active_port >= dp->ports) {
0208         DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
0209         return false;
0210     }
0211 
0212     port = dp->port[dp->active_port];
0213 
0214     /*
0215      * Attempt to read sink count, retry in case the sink may not be ready.
0216      *
0217      * Sinks are *supposed* to come up within 1ms from an off state, but
0218      * some docks need more time to power up.
0219      */
0220     while (time_before(jiffies, timeout)) {
0221         if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
0222             return false;
0223 
0224         if (!cdn_dp_get_sink_count(dp, &sink_count))
0225             return sink_count ? true : false;
0226 
0227         usleep_range(5000, 10000);
0228     }
0229 
0230     DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
0231     return false;
0232 }
0233 
0234 static enum drm_connector_status
0235 cdn_dp_connector_detect(struct drm_connector *connector, bool force)
0236 {
0237     struct cdn_dp_device *dp = connector_to_dp(connector);
0238     enum drm_connector_status status = connector_status_disconnected;
0239 
0240     mutex_lock(&dp->lock);
0241     if (dp->connected)
0242         status = connector_status_connected;
0243     mutex_unlock(&dp->lock);
0244 
0245     return status;
0246 }
0247 
0248 static void cdn_dp_connector_destroy(struct drm_connector *connector)
0249 {
0250     drm_connector_unregister(connector);
0251     drm_connector_cleanup(connector);
0252 }
0253 
0254 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
0255     .detect = cdn_dp_connector_detect,
0256     .destroy = cdn_dp_connector_destroy,
0257     .fill_modes = drm_helper_probe_single_connector_modes,
0258     .reset = drm_atomic_helper_connector_reset,
0259     .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
0260     .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
0261 };
0262 
0263 static int cdn_dp_connector_get_modes(struct drm_connector *connector)
0264 {
0265     struct cdn_dp_device *dp = connector_to_dp(connector);
0266     struct edid *edid;
0267     int ret = 0;
0268 
0269     mutex_lock(&dp->lock);
0270     edid = dp->edid;
0271     if (edid) {
0272         DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
0273                   edid->width_cm, edid->height_cm);
0274 
0275         dp->sink_has_audio = drm_detect_monitor_audio(edid);
0276         ret = drm_add_edid_modes(connector, edid);
0277         if (ret)
0278             drm_connector_update_edid_property(connector,
0279                                 edid);
0280     }
0281     mutex_unlock(&dp->lock);
0282 
0283     return ret;
0284 }
0285 
0286 static enum drm_mode_status
0287 cdn_dp_connector_mode_valid(struct drm_connector *connector,
0288                 struct drm_display_mode *mode)
0289 {
0290     struct cdn_dp_device *dp = connector_to_dp(connector);
0291     struct drm_display_info *display_info = &dp->connector.display_info;
0292     u32 requested, actual, rate, sink_max, source_max = 0;
0293     u8 lanes, bpc;
0294 
0295     /* If DP is disconnected, every mode is invalid */
0296     if (!dp->connected)
0297         return MODE_BAD;
0298 
0299     switch (display_info->bpc) {
0300     case 10:
0301         bpc = 10;
0302         break;
0303     case 6:
0304         bpc = 6;
0305         break;
0306     default:
0307         bpc = 8;
0308         break;
0309     }
0310 
0311     requested = mode->clock * bpc * 3 / 1000;
0312 
0313     source_max = dp->lanes;
0314     sink_max = drm_dp_max_lane_count(dp->dpcd);
0315     lanes = min(source_max, sink_max);
0316 
0317     source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
0318     sink_max = drm_dp_max_link_rate(dp->dpcd);
0319     rate = min(source_max, sink_max);
0320 
0321     actual = rate * lanes / 100;
0322 
0323     /* efficiency is about 0.8 */
0324     actual = actual * 8 / 10;
0325 
0326     if (requested > actual) {
0327         DRM_DEV_DEBUG_KMS(dp->dev,
0328                   "requested=%d, actual=%d, clock=%d\n",
0329                   requested, actual, mode->clock);
0330         return MODE_CLOCK_HIGH;
0331     }
0332 
0333     return MODE_OK;
0334 }
0335 
0336 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
0337     .get_modes = cdn_dp_connector_get_modes,
0338     .mode_valid = cdn_dp_connector_mode_valid,
0339 };
0340 
0341 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
0342 {
0343     int ret;
0344     const u32 *iram_data, *dram_data;
0345     const struct firmware *fw = dp->fw;
0346     const struct cdn_firmware_header *hdr;
0347 
0348     hdr = (struct cdn_firmware_header *)fw->data;
0349     if (fw->size != le32_to_cpu(hdr->size_bytes)) {
0350         DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
0351         return -EINVAL;
0352     }
0353 
0354     iram_data = (const u32 *)(fw->data + hdr->header_size);
0355     dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
0356 
0357     ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
0358                    dram_data, hdr->dram_size);
0359     if (ret)
0360         return ret;
0361 
0362     ret = cdn_dp_set_firmware_active(dp, true);
0363     if (ret) {
0364         DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
0365         return ret;
0366     }
0367 
0368     return cdn_dp_event_config(dp);
0369 }
0370 
0371 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
0372 {
0373     int ret;
0374 
0375     if (!cdn_dp_check_sink_connection(dp))
0376         return -ENODEV;
0377 
0378     ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
0379                    DP_RECEIVER_CAP_SIZE);
0380     if (ret) {
0381         DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
0382         return ret;
0383     }
0384 
0385     kfree(dp->edid);
0386     dp->edid = drm_do_get_edid(&dp->connector,
0387                    cdn_dp_get_edid_block, dp);
0388     return 0;
0389 }
0390 
0391 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
0392 {
0393     union extcon_property_value property;
0394     int ret;
0395 
0396     if (!port->phy_enabled) {
0397         ret = phy_power_on(port->phy);
0398         if (ret) {
0399             DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
0400                       ret);
0401             goto err_phy;
0402         }
0403         port->phy_enabled = true;
0404     }
0405 
0406     ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
0407                    DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
0408     if (ret) {
0409         DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
0410         goto err_power_on;
0411     }
0412 
0413     ret = cdn_dp_get_hpd_status(dp);
0414     if (ret <= 0) {
0415         if (!ret)
0416             DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
0417         goto err_power_on;
0418     }
0419 
0420     ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
0421                   EXTCON_PROP_USB_TYPEC_POLARITY, &property);
0422     if (ret) {
0423         DRM_DEV_ERROR(dp->dev, "get property failed\n");
0424         goto err_power_on;
0425     }
0426 
0427     port->lanes = cdn_dp_get_port_lanes(port);
0428     ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
0429     if (ret) {
0430         DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
0431                   ret);
0432         goto err_power_on;
0433     }
0434 
0435     dp->active_port = port->id;
0436     return 0;
0437 
0438 err_power_on:
0439     if (phy_power_off(port->phy))
0440         DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
0441     else
0442         port->phy_enabled = false;
0443 
0444 err_phy:
0445     cdn_dp_grf_write(dp, GRF_SOC_CON26,
0446              DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
0447     return ret;
0448 }
0449 
0450 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
0451                   struct cdn_dp_port *port)
0452 {
0453     int ret;
0454 
0455     if (port->phy_enabled) {
0456         ret = phy_power_off(port->phy);
0457         if (ret) {
0458             DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
0459             return ret;
0460         }
0461     }
0462 
0463     port->phy_enabled = false;
0464     port->lanes = 0;
0465     dp->active_port = -1;
0466     return 0;
0467 }
0468 
0469 static int cdn_dp_disable(struct cdn_dp_device *dp)
0470 {
0471     int ret, i;
0472 
0473     if (!dp->active)
0474         return 0;
0475 
0476     for (i = 0; i < dp->ports; i++)
0477         cdn_dp_disable_phy(dp, dp->port[i]);
0478 
0479     ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
0480                    DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
0481     if (ret) {
0482         DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
0483                   ret);
0484         return ret;
0485     }
0486 
0487     cdn_dp_set_firmware_active(dp, false);
0488     cdn_dp_clk_disable(dp);
0489     dp->active = false;
0490     dp->max_lanes = 0;
0491     dp->max_rate = 0;
0492     if (!dp->connected) {
0493         kfree(dp->edid);
0494         dp->edid = NULL;
0495     }
0496 
0497     return 0;
0498 }
0499 
0500 static int cdn_dp_enable(struct cdn_dp_device *dp)
0501 {
0502     int ret, i, lanes;
0503     struct cdn_dp_port *port;
0504 
0505     port = cdn_dp_connected_port(dp);
0506     if (!port) {
0507         DRM_DEV_ERROR(dp->dev,
0508                   "Can't enable without connection\n");
0509         return -ENODEV;
0510     }
0511 
0512     if (dp->active)
0513         return 0;
0514 
0515     ret = cdn_dp_clk_enable(dp);
0516     if (ret)
0517         return ret;
0518 
0519     ret = cdn_dp_firmware_init(dp);
0520     if (ret) {
0521         DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
0522         goto err_clk_disable;
0523     }
0524 
0525     /* only enable the port that connected with downstream device */
0526     for (i = port->id; i < dp->ports; i++) {
0527         port = dp->port[i];
0528         lanes = cdn_dp_get_port_lanes(port);
0529         if (lanes) {
0530             ret = cdn_dp_enable_phy(dp, port);
0531             if (ret)
0532                 continue;
0533 
0534             ret = cdn_dp_get_sink_capability(dp);
0535             if (ret) {
0536                 cdn_dp_disable_phy(dp, port);
0537             } else {
0538                 dp->active = true;
0539                 dp->lanes = port->lanes;
0540                 return 0;
0541             }
0542         }
0543     }
0544 
0545 err_clk_disable:
0546     cdn_dp_clk_disable(dp);
0547     return ret;
0548 }
0549 
0550 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
0551                     struct drm_display_mode *mode,
0552                     struct drm_display_mode *adjusted)
0553 {
0554     struct cdn_dp_device *dp = encoder_to_dp(encoder);
0555     struct drm_display_info *display_info = &dp->connector.display_info;
0556     struct video_info *video = &dp->video_info;
0557 
0558     switch (display_info->bpc) {
0559     case 10:
0560         video->color_depth = 10;
0561         break;
0562     case 6:
0563         video->color_depth = 6;
0564         break;
0565     default:
0566         video->color_depth = 8;
0567         break;
0568     }
0569 
0570     video->color_fmt = PXL_RGB;
0571     video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
0572     video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
0573 
0574     memcpy(&dp->mode, adjusted, sizeof(*mode));
0575 }
0576 
0577 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
0578 {
0579     u8 link_status[DP_LINK_STATUS_SIZE];
0580     struct cdn_dp_port *port = cdn_dp_connected_port(dp);
0581     u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
0582 
0583     if (!port || !dp->max_rate || !dp->max_lanes)
0584         return false;
0585 
0586     if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
0587                  DP_LINK_STATUS_SIZE)) {
0588         DRM_ERROR("Failed to get link status\n");
0589         return false;
0590     }
0591 
0592     /* if link training is requested we should perform it always */
0593     return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
0594 }
0595 
0596 static void cdn_dp_audio_handle_plugged_change(struct cdn_dp_device *dp,
0597                            bool plugged)
0598 {
0599     if (dp->codec_dev)
0600         dp->plugged_cb(dp->codec_dev, plugged);
0601 }
0602 
0603 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
0604 {
0605     struct cdn_dp_device *dp = encoder_to_dp(encoder);
0606     int ret, val;
0607 
0608     ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
0609     if (ret < 0) {
0610         DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
0611         return;
0612     }
0613 
0614     DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
0615               (ret) ? "LIT" : "BIG");
0616     if (ret)
0617         val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
0618     else
0619         val = DP_SEL_VOP_LIT << 16;
0620 
0621     ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
0622     if (ret)
0623         return;
0624 
0625     mutex_lock(&dp->lock);
0626 
0627     ret = cdn_dp_enable(dp);
0628     if (ret) {
0629         DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
0630                   ret);
0631         goto out;
0632     }
0633     if (!cdn_dp_check_link_status(dp)) {
0634         ret = cdn_dp_train_link(dp);
0635         if (ret) {
0636             DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
0637             goto out;
0638         }
0639     }
0640 
0641     ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
0642     if (ret) {
0643         DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
0644         goto out;
0645     }
0646 
0647     ret = cdn_dp_config_video(dp);
0648     if (ret) {
0649         DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
0650         goto out;
0651     }
0652 
0653     ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
0654     if (ret) {
0655         DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
0656         goto out;
0657     }
0658 
0659     cdn_dp_audio_handle_plugged_change(dp, true);
0660 
0661 out:
0662     mutex_unlock(&dp->lock);
0663 }
0664 
0665 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
0666 {
0667     struct cdn_dp_device *dp = encoder_to_dp(encoder);
0668     int ret;
0669 
0670     mutex_lock(&dp->lock);
0671     cdn_dp_audio_handle_plugged_change(dp, false);
0672 
0673     if (dp->active) {
0674         ret = cdn_dp_disable(dp);
0675         if (ret) {
0676             DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
0677                       ret);
0678         }
0679     }
0680     mutex_unlock(&dp->lock);
0681 
0682     /*
0683      * In the following 2 cases, we need to run the event_work to re-enable
0684      * the DP:
0685      * 1. If there is not just one port device is connected, and remove one
0686      *    device from a port, the DP will be disabled here, at this case,
0687      *    run the event_work to re-open DP for the other port.
0688      * 2. If re-training or re-config failed, the DP will be disabled here.
0689      *    run the event_work to re-connect it.
0690      */
0691     if (!dp->connected && cdn_dp_connected_port(dp))
0692         schedule_work(&dp->event_work);
0693 }
0694 
0695 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
0696                        struct drm_crtc_state *crtc_state,
0697                        struct drm_connector_state *conn_state)
0698 {
0699     struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
0700 
0701     s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
0702     s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
0703 
0704     return 0;
0705 }
0706 
0707 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
0708     .mode_set = cdn_dp_encoder_mode_set,
0709     .enable = cdn_dp_encoder_enable,
0710     .disable = cdn_dp_encoder_disable,
0711     .atomic_check = cdn_dp_encoder_atomic_check,
0712 };
0713 
0714 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
0715 {
0716     struct device *dev = dp->dev;
0717     struct device_node *np = dev->of_node;
0718     struct platform_device *pdev = to_platform_device(dev);
0719 
0720     dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
0721     if (IS_ERR(dp->grf)) {
0722         DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
0723         return PTR_ERR(dp->grf);
0724     }
0725 
0726     dp->regs = devm_platform_ioremap_resource(pdev, 0);
0727     if (IS_ERR(dp->regs)) {
0728         DRM_DEV_ERROR(dev, "ioremap reg failed\n");
0729         return PTR_ERR(dp->regs);
0730     }
0731 
0732     dp->core_clk = devm_clk_get(dev, "core-clk");
0733     if (IS_ERR(dp->core_clk)) {
0734         DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
0735         return PTR_ERR(dp->core_clk);
0736     }
0737 
0738     dp->pclk = devm_clk_get(dev, "pclk");
0739     if (IS_ERR(dp->pclk)) {
0740         DRM_DEV_ERROR(dev, "cannot get pclk\n");
0741         return PTR_ERR(dp->pclk);
0742     }
0743 
0744     dp->spdif_clk = devm_clk_get(dev, "spdif");
0745     if (IS_ERR(dp->spdif_clk)) {
0746         DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
0747         return PTR_ERR(dp->spdif_clk);
0748     }
0749 
0750     dp->grf_clk = devm_clk_get(dev, "grf");
0751     if (IS_ERR(dp->grf_clk)) {
0752         DRM_DEV_ERROR(dev, "cannot get grf clk\n");
0753         return PTR_ERR(dp->grf_clk);
0754     }
0755 
0756     dp->spdif_rst = devm_reset_control_get(dev, "spdif");
0757     if (IS_ERR(dp->spdif_rst)) {
0758         DRM_DEV_ERROR(dev, "no spdif reset control found\n");
0759         return PTR_ERR(dp->spdif_rst);
0760     }
0761 
0762     dp->dptx_rst = devm_reset_control_get(dev, "dptx");
0763     if (IS_ERR(dp->dptx_rst)) {
0764         DRM_DEV_ERROR(dev, "no uphy reset control found\n");
0765         return PTR_ERR(dp->dptx_rst);
0766     }
0767 
0768     dp->core_rst = devm_reset_control_get(dev, "core");
0769     if (IS_ERR(dp->core_rst)) {
0770         DRM_DEV_ERROR(dev, "no core reset control found\n");
0771         return PTR_ERR(dp->core_rst);
0772     }
0773 
0774     dp->apb_rst = devm_reset_control_get(dev, "apb");
0775     if (IS_ERR(dp->apb_rst)) {
0776         DRM_DEV_ERROR(dev, "no apb reset control found\n");
0777         return PTR_ERR(dp->apb_rst);
0778     }
0779 
0780     return 0;
0781 }
0782 
0783 static int cdn_dp_audio_hw_params(struct device *dev,  void *data,
0784                   struct hdmi_codec_daifmt *daifmt,
0785                   struct hdmi_codec_params *params)
0786 {
0787     struct cdn_dp_device *dp = dev_get_drvdata(dev);
0788     struct audio_info audio = {
0789         .sample_width = params->sample_width,
0790         .sample_rate = params->sample_rate,
0791         .channels = params->channels,
0792     };
0793     int ret;
0794 
0795     mutex_lock(&dp->lock);
0796     if (!dp->active) {
0797         ret = -ENODEV;
0798         goto out;
0799     }
0800 
0801     switch (daifmt->fmt) {
0802     case HDMI_I2S:
0803         audio.format = AFMT_I2S;
0804         break;
0805     case HDMI_SPDIF:
0806         audio.format = AFMT_SPDIF;
0807         break;
0808     default:
0809         DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
0810         ret = -EINVAL;
0811         goto out;
0812     }
0813 
0814     ret = cdn_dp_audio_config(dp, &audio);
0815     if (!ret)
0816         dp->audio_info = audio;
0817 
0818 out:
0819     mutex_unlock(&dp->lock);
0820     return ret;
0821 }
0822 
0823 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
0824 {
0825     struct cdn_dp_device *dp = dev_get_drvdata(dev);
0826     int ret;
0827 
0828     mutex_lock(&dp->lock);
0829     if (!dp->active)
0830         goto out;
0831 
0832     ret = cdn_dp_audio_stop(dp, &dp->audio_info);
0833     if (!ret)
0834         dp->audio_info.format = AFMT_UNUSED;
0835 out:
0836     mutex_unlock(&dp->lock);
0837 }
0838 
0839 static int cdn_dp_audio_mute_stream(struct device *dev, void *data,
0840                     bool enable, int direction)
0841 {
0842     struct cdn_dp_device *dp = dev_get_drvdata(dev);
0843     int ret;
0844 
0845     mutex_lock(&dp->lock);
0846     if (!dp->active) {
0847         ret = -ENODEV;
0848         goto out;
0849     }
0850 
0851     ret = cdn_dp_audio_mute(dp, enable);
0852 
0853 out:
0854     mutex_unlock(&dp->lock);
0855     return ret;
0856 }
0857 
0858 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
0859                 u8 *buf, size_t len)
0860 {
0861     struct cdn_dp_device *dp = dev_get_drvdata(dev);
0862 
0863     memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
0864 
0865     return 0;
0866 }
0867 
0868 static int cdn_dp_audio_hook_plugged_cb(struct device *dev, void *data,
0869                     hdmi_codec_plugged_cb fn,
0870                     struct device *codec_dev)
0871 {
0872     struct cdn_dp_device *dp = dev_get_drvdata(dev);
0873 
0874     mutex_lock(&dp->lock);
0875     dp->plugged_cb = fn;
0876     dp->codec_dev = codec_dev;
0877     cdn_dp_audio_handle_plugged_change(dp, dp->connected);
0878     mutex_unlock(&dp->lock);
0879 
0880     return 0;
0881 }
0882 
0883 static const struct hdmi_codec_ops audio_codec_ops = {
0884     .hw_params = cdn_dp_audio_hw_params,
0885     .audio_shutdown = cdn_dp_audio_shutdown,
0886     .mute_stream = cdn_dp_audio_mute_stream,
0887     .get_eld = cdn_dp_audio_get_eld,
0888     .hook_plugged_cb = cdn_dp_audio_hook_plugged_cb,
0889     .no_capture_mute = 1,
0890 };
0891 
0892 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
0893                    struct device *dev)
0894 {
0895     struct hdmi_codec_pdata codec_data = {
0896         .i2s = 1,
0897         .spdif = 1,
0898         .ops = &audio_codec_ops,
0899         .max_i2s_channels = 8,
0900     };
0901 
0902     dp->audio_pdev = platform_device_register_data(
0903              dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
0904              &codec_data, sizeof(codec_data));
0905 
0906     return PTR_ERR_OR_ZERO(dp->audio_pdev);
0907 }
0908 
0909 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
0910 {
0911     int ret;
0912     unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
0913     unsigned long sleep = 1000;
0914 
0915     WARN_ON(!mutex_is_locked(&dp->lock));
0916 
0917     if (dp->fw_loaded)
0918         return 0;
0919 
0920     /* Drop the lock before getting the firmware to avoid blocking boot */
0921     mutex_unlock(&dp->lock);
0922 
0923     while (time_before(jiffies, timeout)) {
0924         ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
0925         if (ret == -ENOENT) {
0926             msleep(sleep);
0927             sleep *= 2;
0928             continue;
0929         } else if (ret) {
0930             DRM_DEV_ERROR(dp->dev,
0931                       "failed to request firmware: %d\n", ret);
0932             goto out;
0933         }
0934 
0935         dp->fw_loaded = true;
0936         ret = 0;
0937         goto out;
0938     }
0939 
0940     DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
0941     ret = -ETIMEDOUT;
0942 out:
0943     mutex_lock(&dp->lock);
0944     return ret;
0945 }
0946 
0947 static void cdn_dp_pd_event_work(struct work_struct *work)
0948 {
0949     struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
0950                         event_work);
0951     struct drm_connector *connector = &dp->connector;
0952     enum drm_connector_status old_status;
0953 
0954     int ret;
0955 
0956     mutex_lock(&dp->lock);
0957 
0958     if (dp->suspended)
0959         goto out;
0960 
0961     ret = cdn_dp_request_firmware(dp);
0962     if (ret)
0963         goto out;
0964 
0965     dp->connected = true;
0966 
0967     /* Not connected, notify userspace to disable the block */
0968     if (!cdn_dp_connected_port(dp)) {
0969         DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
0970         dp->connected = false;
0971 
0972     /* Connected but not enabled, enable the block */
0973     } else if (!dp->active) {
0974         DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
0975         ret = cdn_dp_enable(dp);
0976         if (ret) {
0977             DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
0978             dp->connected = false;
0979         }
0980 
0981     /* Enabled and connected to a dongle without a sink, notify userspace */
0982     } else if (!cdn_dp_check_sink_connection(dp)) {
0983         DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
0984         dp->connected = false;
0985 
0986     /* Enabled and connected with a sink, re-train if requested */
0987     } else if (!cdn_dp_check_link_status(dp)) {
0988         unsigned int rate = dp->max_rate;
0989         unsigned int lanes = dp->max_lanes;
0990         struct drm_display_mode *mode = &dp->mode;
0991 
0992         DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
0993         ret = cdn_dp_train_link(dp);
0994         if (ret) {
0995             dp->connected = false;
0996             DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
0997             goto out;
0998         }
0999 
1000         /* If training result is changed, update the video config */
1001         if (mode->clock &&
1002             (rate != dp->max_rate || lanes != dp->max_lanes)) {
1003             ret = cdn_dp_config_video(dp);
1004             if (ret) {
1005                 dp->connected = false;
1006                 DRM_DEV_ERROR(dp->dev,
1007                           "Failed to config video %d\n",
1008                           ret);
1009             }
1010         }
1011     }
1012 
1013 out:
1014     mutex_unlock(&dp->lock);
1015 
1016     old_status = connector->status;
1017     connector->status = connector->funcs->detect(connector, false);
1018     if (old_status != connector->status)
1019         drm_kms_helper_hotplug_event(dp->drm_dev);
1020 }
1021 
1022 static int cdn_dp_pd_event(struct notifier_block *nb,
1023                unsigned long event, void *priv)
1024 {
1025     struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1026                         event_nb);
1027     struct cdn_dp_device *dp = port->dp;
1028 
1029     /*
1030      * It would be nice to be able to just do the work inline right here.
1031      * However, we need to make a bunch of calls that might sleep in order
1032      * to turn on the block/phy, so use a worker instead.
1033      */
1034     schedule_work(&dp->event_work);
1035 
1036     return NOTIFY_DONE;
1037 }
1038 
1039 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1040 {
1041     struct cdn_dp_device *dp = dev_get_drvdata(dev);
1042     struct drm_encoder *encoder;
1043     struct drm_connector *connector;
1044     struct cdn_dp_port *port;
1045     struct drm_device *drm_dev = data;
1046     int ret, i;
1047 
1048     ret = cdn_dp_parse_dt(dp);
1049     if (ret < 0)
1050         return ret;
1051 
1052     dp->drm_dev = drm_dev;
1053     dp->connected = false;
1054     dp->active = false;
1055     dp->active_port = -1;
1056     dp->fw_loaded = false;
1057 
1058     INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1059 
1060     encoder = &dp->encoder.encoder;
1061 
1062     encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1063                                  dev->of_node);
1064     DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1065 
1066     ret = drm_simple_encoder_init(drm_dev, encoder,
1067                       DRM_MODE_ENCODER_TMDS);
1068     if (ret) {
1069         DRM_ERROR("failed to initialize encoder with drm\n");
1070         return ret;
1071     }
1072 
1073     drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1074 
1075     connector = &dp->connector;
1076     connector->polled = DRM_CONNECTOR_POLL_HPD;
1077     connector->dpms = DRM_MODE_DPMS_OFF;
1078 
1079     ret = drm_connector_init(drm_dev, connector,
1080                  &cdn_dp_atomic_connector_funcs,
1081                  DRM_MODE_CONNECTOR_DisplayPort);
1082     if (ret) {
1083         DRM_ERROR("failed to initialize connector with drm\n");
1084         goto err_free_encoder;
1085     }
1086 
1087     drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1088 
1089     ret = drm_connector_attach_encoder(connector, encoder);
1090     if (ret) {
1091         DRM_ERROR("failed to attach connector and encoder\n");
1092         goto err_free_connector;
1093     }
1094 
1095     for (i = 0; i < dp->ports; i++) {
1096         port = dp->port[i];
1097 
1098         port->event_nb.notifier_call = cdn_dp_pd_event;
1099         ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1100                             EXTCON_DISP_DP,
1101                             &port->event_nb);
1102         if (ret) {
1103             DRM_DEV_ERROR(dev,
1104                       "register EXTCON_DISP_DP notifier err\n");
1105             goto err_free_connector;
1106         }
1107     }
1108 
1109     pm_runtime_enable(dev);
1110 
1111     schedule_work(&dp->event_work);
1112 
1113     return 0;
1114 
1115 err_free_connector:
1116     drm_connector_cleanup(connector);
1117 err_free_encoder:
1118     drm_encoder_cleanup(encoder);
1119     return ret;
1120 }
1121 
1122 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1123 {
1124     struct cdn_dp_device *dp = dev_get_drvdata(dev);
1125     struct drm_encoder *encoder = &dp->encoder.encoder;
1126     struct drm_connector *connector = &dp->connector;
1127 
1128     cancel_work_sync(&dp->event_work);
1129     cdn_dp_encoder_disable(encoder);
1130     encoder->funcs->destroy(encoder);
1131     connector->funcs->destroy(connector);
1132 
1133     pm_runtime_disable(dev);
1134     if (dp->fw_loaded)
1135         release_firmware(dp->fw);
1136     kfree(dp->edid);
1137     dp->edid = NULL;
1138 }
1139 
1140 static const struct component_ops cdn_dp_component_ops = {
1141     .bind = cdn_dp_bind,
1142     .unbind = cdn_dp_unbind,
1143 };
1144 
1145 static int cdn_dp_suspend(struct device *dev)
1146 {
1147     struct cdn_dp_device *dp = dev_get_drvdata(dev);
1148     int ret = 0;
1149 
1150     mutex_lock(&dp->lock);
1151     if (dp->active)
1152         ret = cdn_dp_disable(dp);
1153     dp->suspended = true;
1154     mutex_unlock(&dp->lock);
1155 
1156     return ret;
1157 }
1158 
1159 static __maybe_unused int cdn_dp_resume(struct device *dev)
1160 {
1161     struct cdn_dp_device *dp = dev_get_drvdata(dev);
1162 
1163     mutex_lock(&dp->lock);
1164     dp->suspended = false;
1165     if (dp->fw_loaded)
1166         schedule_work(&dp->event_work);
1167     mutex_unlock(&dp->lock);
1168 
1169     return 0;
1170 }
1171 
1172 static int cdn_dp_probe(struct platform_device *pdev)
1173 {
1174     struct device *dev = &pdev->dev;
1175     const struct of_device_id *match;
1176     struct cdn_dp_data *dp_data;
1177     struct cdn_dp_port *port;
1178     struct cdn_dp_device *dp;
1179     struct extcon_dev *extcon;
1180     struct phy *phy;
1181     int i;
1182 
1183     dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1184     if (!dp)
1185         return -ENOMEM;
1186     dp->dev = dev;
1187 
1188     match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1189     dp_data = (struct cdn_dp_data *)match->data;
1190 
1191     for (i = 0; i < dp_data->max_phy; i++) {
1192         extcon = extcon_get_edev_by_phandle(dev, i);
1193         phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1194 
1195         if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1196             PTR_ERR(phy) == -EPROBE_DEFER)
1197             return -EPROBE_DEFER;
1198 
1199         if (IS_ERR(extcon) || IS_ERR(phy))
1200             continue;
1201 
1202         port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1203         if (!port)
1204             return -ENOMEM;
1205 
1206         port->extcon = extcon;
1207         port->phy = phy;
1208         port->dp = dp;
1209         port->id = i;
1210         dp->port[dp->ports++] = port;
1211     }
1212 
1213     if (!dp->ports) {
1214         DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1215         return -EINVAL;
1216     }
1217 
1218     mutex_init(&dp->lock);
1219     dev_set_drvdata(dev, dp);
1220 
1221     cdn_dp_audio_codec_init(dp, dev);
1222 
1223     return component_add(dev, &cdn_dp_component_ops);
1224 }
1225 
1226 static int cdn_dp_remove(struct platform_device *pdev)
1227 {
1228     struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1229 
1230     platform_device_unregister(dp->audio_pdev);
1231     cdn_dp_suspend(dp->dev);
1232     component_del(&pdev->dev, &cdn_dp_component_ops);
1233 
1234     return 0;
1235 }
1236 
1237 static void cdn_dp_shutdown(struct platform_device *pdev)
1238 {
1239     struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1240 
1241     cdn_dp_suspend(dp->dev);
1242 }
1243 
1244 static const struct dev_pm_ops cdn_dp_pm_ops = {
1245     SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1246                 cdn_dp_resume)
1247 };
1248 
1249 struct platform_driver cdn_dp_driver = {
1250     .probe = cdn_dp_probe,
1251     .remove = cdn_dp_remove,
1252     .shutdown = cdn_dp_shutdown,
1253     .driver = {
1254            .name = "cdn-dp",
1255            .owner = THIS_MODULE,
1256            .of_match_table = of_match_ptr(cdn_dp_dt_ids),
1257            .pm = &cdn_dp_pm_ops,
1258     },
1259 };