2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_dp_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_of.h>
22 #include <linux/clk.h>
23 #include <linux/component.h>
24 #include <linux/extcon.h>
25 #include <linux/firmware.h>
26 #include <linux/regmap.h>
27 #include <linux/reset.h>
28 #include <linux/mfd/syscon.h>
29 #include <linux/phy/phy.h>
31 #include <sound/hdmi-codec.h>
33 #include "cdn-dp-core.h"
34 #include "cdn-dp-reg.h"
35 #include "rockchip_drm_vop.h"
37 #define connector_to_dp(c) \
38 container_of(c, struct cdn_dp_device, connector)
40 #define encoder_to_dp(c) \
41 container_of(c, struct cdn_dp_device, encoder)
43 #define GRF_SOC_CON9 0x6224
44 #define DP_SEL_VOP_LIT BIT(12)
45 #define GRF_SOC_CON26 0x6268
46 #define UPHY_SEL_BIT 3
47 #define UPHY_SEL_MASK BIT(19)
48 #define DPTX_HPD_SEL (3 << 12)
49 #define DPTX_HPD_DEL (2 << 12)
50 #define DPTX_HPD_SEL_MASK (3 << 28)
52 #define CDN_FW_TIMEOUT_MS (64 * 1000)
53 #define CDN_DPCD_TIMEOUT_MS 5000
54 #define CDN_DP_FIRMWARE "rockchip/dptx.bin"
60 struct cdn_dp_data rk3399_cdn_dp = {
64 static const struct of_device_id cdn_dp_dt_ids[] = {
65 { .compatible = "rockchip,rk3399-cdn-dp",
66 .data = (void *)&rk3399_cdn_dp },
70 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
72 static int cdn_dp_grf_write(struct cdn_dp_device *dp,
73 unsigned int reg, unsigned int val)
77 ret = clk_prepare_enable(dp->grf_clk);
79 DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
83 ret = regmap_write(dp->grf, reg, val);
85 DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
89 clk_disable_unprepare(dp->grf_clk);
94 static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
99 ret = clk_prepare_enable(dp->pclk);
101 DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret);
105 ret = clk_prepare_enable(dp->core_clk);
107 DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret);
111 ret = pm_runtime_get_sync(dp->dev);
113 DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret);
114 goto err_pm_runtime_get;
117 reset_control_assert(dp->core_rst);
118 reset_control_assert(dp->dptx_rst);
119 reset_control_assert(dp->apb_rst);
120 reset_control_deassert(dp->core_rst);
121 reset_control_deassert(dp->dptx_rst);
122 reset_control_deassert(dp->apb_rst);
124 rate = clk_get_rate(dp->core_clk);
126 DRM_DEV_ERROR(dp->dev, "get clk rate failed: %d\n", rate);
130 cdn_dp_set_fw_clk(dp, rate);
131 cdn_dp_clock_reset(dp);
136 pm_runtime_put(dp->dev);
138 clk_disable_unprepare(dp->core_clk);
140 clk_disable_unprepare(dp->pclk);
145 static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
147 pm_runtime_put_sync(dp->dev);
148 clk_disable_unprepare(dp->pclk);
149 clk_disable_unprepare(dp->core_clk);
152 static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
154 struct extcon_dev *edev = port->extcon;
155 union extcon_property_value property;
159 dptx = extcon_get_state(edev, EXTCON_DISP_DP);
161 extcon_get_property(edev, EXTCON_DISP_DP,
162 EXTCON_PROP_USB_SS, &property);
174 static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
180 ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
184 *sink_count = DP_GET_SINK_COUNT(value);
188 static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
190 struct cdn_dp_port *port;
193 for (i = 0; i < dp->ports; i++) {
195 lanes = cdn_dp_get_port_lanes(port);
202 static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
204 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
205 struct cdn_dp_port *port;
208 if (dp->active_port < 0 || dp->active_port >= dp->ports) {
209 DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
213 port = dp->port[dp->active_port];
216 * Attempt to read sink count, retry in case the sink may not be ready.
218 * Sinks are *supposed* to come up within 1ms from an off state, but
219 * some docks need more time to power up.
221 while (time_before(jiffies, timeout)) {
222 if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
225 if (!cdn_dp_get_sink_count(dp, &sink_count))
226 return sink_count ? true : false;
228 usleep_range(5000, 10000);
231 DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
235 static enum drm_connector_status
236 cdn_dp_connector_detect(struct drm_connector *connector, bool force)
238 struct cdn_dp_device *dp = connector_to_dp(connector);
239 enum drm_connector_status status = connector_status_disconnected;
241 mutex_lock(&dp->lock);
243 status = connector_status_connected;
244 mutex_unlock(&dp->lock);
249 static void cdn_dp_connector_destroy(struct drm_connector *connector)
251 drm_connector_unregister(connector);
252 drm_connector_cleanup(connector);
255 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
256 .dpms = drm_atomic_helper_connector_dpms,
257 .detect = cdn_dp_connector_detect,
258 .destroy = cdn_dp_connector_destroy,
259 .fill_modes = drm_helper_probe_single_connector_modes,
260 .reset = drm_atomic_helper_connector_reset,
261 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
262 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
265 static int cdn_dp_connector_get_modes(struct drm_connector *connector)
267 struct cdn_dp_device *dp = connector_to_dp(connector);
271 mutex_lock(&dp->lock);
274 DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
275 edid->width_cm, edid->height_cm);
277 dp->sink_has_audio = drm_detect_monitor_audio(edid);
278 ret = drm_add_edid_modes(connector, edid);
280 drm_mode_connector_update_edid_property(connector,
282 drm_edid_to_eld(connector, edid);
285 mutex_unlock(&dp->lock);
290 static struct drm_encoder *
291 cdn_dp_connector_best_encoder(struct drm_connector *connector)
293 struct cdn_dp_device *dp = connector_to_dp(connector);
298 static int cdn_dp_connector_mode_valid(struct drm_connector *connector,
299 struct drm_display_mode *mode)
301 struct cdn_dp_device *dp = connector_to_dp(connector);
302 struct drm_display_info *display_info = &dp->connector.display_info;
303 u32 requested, actual, rate, sink_max, source_max = 0;
306 /* If DP is disconnected, every mode is invalid */
310 switch (display_info->bpc) {
322 requested = mode->clock * bpc * 3 / 1000;
324 source_max = dp->lanes;
325 sink_max = drm_dp_max_lane_count(dp->dpcd);
326 lanes = min(source_max, sink_max);
328 source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
329 sink_max = drm_dp_max_link_rate(dp->dpcd);
330 rate = min(source_max, sink_max);
332 actual = rate * lanes / 100;
334 /* efficiency is about 0.8 */
335 actual = actual * 8 / 10;
337 if (requested > actual) {
338 DRM_DEV_DEBUG_KMS(dp->dev,
339 "requested=%d, actual=%d, clock=%d\n",
340 requested, actual, mode->clock);
341 return MODE_CLOCK_HIGH;
347 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
348 .get_modes = cdn_dp_connector_get_modes,
349 .best_encoder = cdn_dp_connector_best_encoder,
350 .mode_valid = cdn_dp_connector_mode_valid,
353 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
356 const u32 *iram_data, *dram_data;
357 const struct firmware *fw = dp->fw;
358 const struct cdn_firmware_header *hdr;
360 hdr = (struct cdn_firmware_header *)fw->data;
361 if (fw->size != le32_to_cpu(hdr->size_bytes)) {
362 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
366 iram_data = (const u32 *)(fw->data + hdr->header_size);
367 dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
369 ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
370 dram_data, hdr->dram_size);
374 ret = cdn_dp_set_firmware_active(dp, true);
376 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
380 return cdn_dp_event_config(dp);
383 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
387 if (!cdn_dp_check_sink_connection(dp))
390 ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
391 DP_RECEIVER_CAP_SIZE);
393 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
398 dp->edid = drm_do_get_edid(&dp->connector,
399 cdn_dp_get_edid_block, dp);
403 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
405 union extcon_property_value property;
408 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
409 (port->id << UPHY_SEL_BIT) | UPHY_SEL_MASK);
413 if (!port->phy_enabled) {
414 ret = phy_power_on(port->phy);
416 DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
420 port->phy_enabled = true;
423 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
424 DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
426 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
430 ret = cdn_dp_get_hpd_status(dp);
433 DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
437 ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
438 EXTCON_PROP_USB_TYPEC_POLARITY, &property);
440 DRM_DEV_ERROR(dp->dev, "get property failed\n");
444 port->lanes = cdn_dp_get_port_lanes(port);
445 ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
447 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
452 dp->active_port = port->id;
456 if (phy_power_off(port->phy))
457 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
459 port->phy_enabled = false;
462 cdn_dp_grf_write(dp, GRF_SOC_CON26,
463 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
467 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
468 struct cdn_dp_port *port)
472 if (port->phy_enabled) {
473 ret = phy_power_off(port->phy);
475 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
480 port->phy_enabled = false;
482 dp->active_port = -1;
486 static int cdn_dp_disable(struct cdn_dp_device *dp)
493 for (i = 0; i < dp->ports; i++)
494 cdn_dp_disable_phy(dp, dp->port[i]);
496 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
497 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
499 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
504 cdn_dp_set_firmware_active(dp, false);
505 cdn_dp_clk_disable(dp);
508 dp->link.num_lanes = 0;
509 if (!dp->connected) {
517 static int cdn_dp_enable(struct cdn_dp_device *dp)
520 struct cdn_dp_port *port;
522 port = cdn_dp_connected_port(dp);
524 DRM_DEV_ERROR(dp->dev,
525 "Can't enable without connection\n");
532 ret = cdn_dp_clk_enable(dp);
536 ret = cdn_dp_firmware_init(dp);
538 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
539 goto err_clk_disable;
542 /* only enable the port that connected with downstream device */
543 for (i = port->id; i < dp->ports; i++) {
545 lanes = cdn_dp_get_port_lanes(port);
547 ret = cdn_dp_enable_phy(dp, port);
551 ret = cdn_dp_get_sink_capability(dp);
553 cdn_dp_disable_phy(dp, port);
556 dp->lanes = port->lanes;
563 cdn_dp_clk_disable(dp);
567 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
568 struct drm_display_mode *mode,
569 struct drm_display_mode *adjusted)
571 struct cdn_dp_device *dp = encoder_to_dp(encoder);
572 struct drm_display_info *display_info = &dp->connector.display_info;
573 struct video_info *video = &dp->video_info;
575 switch (display_info->bpc) {
577 video->color_depth = 10;
580 video->color_depth = 6;
583 video->color_depth = 8;
587 video->color_fmt = PXL_RGB;
588 video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
589 video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
591 memcpy(&dp->mode, adjusted, sizeof(*mode));
594 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
596 u8 link_status[DP_LINK_STATUS_SIZE];
597 struct cdn_dp_port *port = cdn_dp_connected_port(dp);
598 u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
600 if (!port || !dp->link.rate || !dp->link.num_lanes)
603 if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
604 DP_LINK_STATUS_SIZE)) {
605 DRM_ERROR("Failed to get link status\n");
609 /* if link training is requested we should perform it always */
610 return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
613 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
615 struct cdn_dp_device *dp = encoder_to_dp(encoder);
617 struct rockchip_crtc_state *state;
619 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
621 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
625 DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
626 (ret) ? "LIT" : "BIG");
627 state = to_rockchip_crtc_state(encoder->crtc->state);
629 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
630 state->output_mode = ROCKCHIP_OUT_MODE_P888;
632 val = DP_SEL_VOP_LIT << 16;
633 state->output_mode = ROCKCHIP_OUT_MODE_AAAA;
636 ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
640 mutex_lock(&dp->lock);
642 ret = cdn_dp_enable(dp);
644 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
648 if (!cdn_dp_check_link_status(dp)) {
649 ret = cdn_dp_train_link(dp);
651 DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
656 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
658 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
662 ret = cdn_dp_config_video(dp);
664 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
668 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
670 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
674 mutex_unlock(&dp->lock);
677 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
679 struct cdn_dp_device *dp = encoder_to_dp(encoder);
682 mutex_lock(&dp->lock);
684 ret = cdn_dp_disable(dp);
686 DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
690 mutex_unlock(&dp->lock);
693 * In the following 2 cases, we need to run the event_work to re-enable
695 * 1. If there is not just one port device is connected, and remove one
696 * device from a port, the DP will be disabled here, at this case,
697 * run the event_work to re-open DP for the other port.
698 * 2. If re-training or re-config failed, the DP will be disabled here.
699 * run the event_work to re-connect it.
701 if (!dp->connected && cdn_dp_connected_port(dp))
702 schedule_work(&dp->event_work);
705 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
706 struct drm_crtc_state *crtc_state,
707 struct drm_connector_state *conn_state)
709 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
711 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
712 s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
717 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
718 .mode_set = cdn_dp_encoder_mode_set,
719 .enable = cdn_dp_encoder_enable,
720 .disable = cdn_dp_encoder_disable,
721 .atomic_check = cdn_dp_encoder_atomic_check,
724 static const struct drm_encoder_funcs cdn_dp_encoder_funcs = {
725 .destroy = drm_encoder_cleanup,
728 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
730 struct device *dev = dp->dev;
731 struct device_node *np = dev->of_node;
732 struct platform_device *pdev = to_platform_device(dev);
733 struct resource *res;
735 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
736 if (IS_ERR(dp->grf)) {
737 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
738 return PTR_ERR(dp->grf);
741 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
742 dp->regs = devm_ioremap_resource(dev, res);
743 if (IS_ERR(dp->regs)) {
744 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
745 return PTR_ERR(dp->regs);
748 dp->core_clk = devm_clk_get(dev, "core-clk");
749 if (IS_ERR(dp->core_clk)) {
750 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
751 return PTR_ERR(dp->core_clk);
754 dp->pclk = devm_clk_get(dev, "pclk");
755 if (IS_ERR(dp->pclk)) {
756 DRM_DEV_ERROR(dev, "cannot get pclk\n");
757 return PTR_ERR(dp->pclk);
760 dp->spdif_clk = devm_clk_get(dev, "spdif");
761 if (IS_ERR(dp->spdif_clk)) {
762 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
763 return PTR_ERR(dp->spdif_clk);
766 dp->grf_clk = devm_clk_get(dev, "grf");
767 if (IS_ERR(dp->grf_clk)) {
768 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
769 return PTR_ERR(dp->grf_clk);
772 dp->spdif_rst = devm_reset_control_get(dev, "spdif");
773 if (IS_ERR(dp->spdif_rst)) {
774 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
775 return PTR_ERR(dp->spdif_rst);
778 dp->dptx_rst = devm_reset_control_get(dev, "dptx");
779 if (IS_ERR(dp->dptx_rst)) {
780 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
781 return PTR_ERR(dp->dptx_rst);
784 dp->core_rst = devm_reset_control_get(dev, "core");
785 if (IS_ERR(dp->core_rst)) {
786 DRM_DEV_ERROR(dev, "no core reset control found\n");
787 return PTR_ERR(dp->core_rst);
790 dp->apb_rst = devm_reset_control_get(dev, "apb");
791 if (IS_ERR(dp->apb_rst)) {
792 DRM_DEV_ERROR(dev, "no apb reset control found\n");
793 return PTR_ERR(dp->apb_rst);
799 static int cdn_dp_audio_hw_params(struct device *dev, void *data,
800 struct hdmi_codec_daifmt *daifmt,
801 struct hdmi_codec_params *params)
803 struct cdn_dp_device *dp = dev_get_drvdata(dev);
804 struct audio_info audio = {
805 .sample_width = params->sample_width,
806 .sample_rate = params->sample_rate,
807 .channels = params->channels,
811 mutex_lock(&dp->lock);
817 switch (daifmt->fmt) {
819 audio.format = AFMT_I2S;
822 audio.format = AFMT_SPDIF;
825 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
830 ret = cdn_dp_audio_config(dp, &audio);
832 dp->audio_info = audio;
835 mutex_unlock(&dp->lock);
839 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
841 struct cdn_dp_device *dp = dev_get_drvdata(dev);
844 mutex_lock(&dp->lock);
848 ret = cdn_dp_audio_stop(dp, &dp->audio_info);
850 dp->audio_info.format = AFMT_UNUSED;
852 mutex_unlock(&dp->lock);
855 static int cdn_dp_audio_digital_mute(struct device *dev, void *data,
858 struct cdn_dp_device *dp = dev_get_drvdata(dev);
861 mutex_lock(&dp->lock);
867 ret = cdn_dp_audio_mute(dp, enable);
870 mutex_unlock(&dp->lock);
874 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
877 struct cdn_dp_device *dp = dev_get_drvdata(dev);
879 memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
884 static const struct hdmi_codec_ops audio_codec_ops = {
885 .hw_params = cdn_dp_audio_hw_params,
886 .audio_shutdown = cdn_dp_audio_shutdown,
887 .digital_mute = cdn_dp_audio_digital_mute,
888 .get_eld = cdn_dp_audio_get_eld,
891 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
894 struct hdmi_codec_pdata codec_data = {
897 .ops = &audio_codec_ops,
898 .max_i2s_channels = 8,
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,
950 struct drm_connector *connector = &dp->connector;
951 enum drm_connector_status old_status;
955 mutex_lock(&dp->lock);
960 ret = cdn_dp_request_firmware(dp);
964 dp->connected = true;
966 /* Not connected, notify userspace to disable the block */
967 if (!cdn_dp_connected_port(dp)) {
968 DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
969 dp->connected = false;
971 /* Connected but not enabled, enable the block */
972 } else if (!dp->active) {
973 DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
974 ret = cdn_dp_enable(dp);
976 DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
977 dp->connected = false;
980 /* Enabled and connected to a dongle without a sink, notify userspace */
981 } else if (!cdn_dp_check_sink_connection(dp)) {
982 DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
983 dp->connected = false;
985 /* Enabled and connected with a sink, re-train if requested */
986 } else if (!cdn_dp_check_link_status(dp)) {
987 unsigned int rate = dp->link.rate;
988 unsigned int lanes = dp->link.num_lanes;
989 struct drm_display_mode *mode = &dp->mode;
991 DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
992 ret = cdn_dp_train_link(dp);
994 dp->connected = false;
995 DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
999 /* If training result is changed, update the video config */
1001 (rate != dp->link.rate || lanes != dp->link.num_lanes)) {
1002 ret = cdn_dp_config_video(dp);
1004 dp->connected = false;
1005 DRM_DEV_ERROR(dp->dev,
1006 "Failed to config video %d\n",
1013 mutex_unlock(&dp->lock);
1015 old_status = connector->status;
1016 connector->status = connector->funcs->detect(connector, false);
1017 if (old_status != connector->status)
1018 drm_kms_helper_hotplug_event(dp->drm_dev);
1021 static int cdn_dp_pd_event(struct notifier_block *nb,
1022 unsigned long event, void *priv)
1024 struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1026 struct cdn_dp_device *dp = port->dp;
1029 * It would be nice to be able to just do the work inline right here.
1030 * However, we need to make a bunch of calls that might sleep in order
1031 * to turn on the block/phy, so use a worker instead.
1033 schedule_work(&dp->event_work);
1038 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1040 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1041 struct drm_encoder *encoder;
1042 struct drm_connector *connector;
1043 struct cdn_dp_port *port;
1044 struct drm_device *drm_dev = data;
1047 ret = cdn_dp_parse_dt(dp);
1051 dp->drm_dev = drm_dev;
1052 dp->connected = false;
1054 dp->active_port = -1;
1056 INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1058 encoder = &dp->encoder;
1060 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1062 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1064 ret = drm_encoder_init(drm_dev, encoder, &cdn_dp_encoder_funcs,
1065 DRM_MODE_ENCODER_TMDS, NULL);
1067 DRM_ERROR("failed to initialize encoder with drm\n");
1071 drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1073 connector = &dp->connector;
1074 connector->polled = DRM_CONNECTOR_POLL_HPD;
1075 connector->dpms = DRM_MODE_DPMS_OFF;
1077 ret = drm_connector_init(drm_dev, connector,
1078 &cdn_dp_atomic_connector_funcs,
1079 DRM_MODE_CONNECTOR_DisplayPort);
1081 DRM_ERROR("failed to initialize connector with drm\n");
1082 goto err_free_encoder;
1085 drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1087 ret = drm_mode_connector_attach_encoder(connector, encoder);
1089 DRM_ERROR("failed to attach connector and encoder\n");
1090 goto err_free_connector;
1093 cdn_dp_audio_codec_init(dp, dev);
1095 for (i = 0; i < dp->ports; i++) {
1098 port->event_nb.notifier_call = cdn_dp_pd_event;
1099 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1104 "register EXTCON_DISP_DP notifier err\n");
1105 goto err_free_connector;
1109 pm_runtime_enable(dev);
1111 schedule_work(&dp->event_work);
1116 drm_connector_cleanup(connector);
1118 drm_encoder_cleanup(encoder);
1122 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1124 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1125 struct drm_encoder *encoder = &dp->encoder;
1126 struct drm_connector *connector = &dp->connector;
1128 cancel_work_sync(&dp->event_work);
1129 platform_device_unregister(dp->audio_pdev);
1130 cdn_dp_encoder_disable(encoder);
1131 encoder->funcs->destroy(encoder);
1132 connector->funcs->destroy(connector);
1134 pm_runtime_disable(dev);
1135 release_firmware(dp->fw);
1140 static const struct component_ops cdn_dp_component_ops = {
1141 .bind = cdn_dp_bind,
1142 .unbind = cdn_dp_unbind,
1145 int cdn_dp_suspend(struct device *dev)
1147 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1150 mutex_lock(&dp->lock);
1152 ret = cdn_dp_disable(dp);
1153 dp->suspended = true;
1154 mutex_unlock(&dp->lock);
1159 int cdn_dp_resume(struct device *dev)
1161 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1163 mutex_lock(&dp->lock);
1164 dp->suspended = false;
1166 schedule_work(&dp->event_work);
1167 mutex_unlock(&dp->lock);
1172 static int cdn_dp_probe(struct platform_device *pdev)
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;
1183 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1188 match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1189 dp_data = (struct cdn_dp_data *)match->data;
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);
1195 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1196 PTR_ERR(phy) == -EPROBE_DEFER)
1197 return -EPROBE_DEFER;
1199 if (IS_ERR(extcon) || IS_ERR(phy))
1202 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1206 port->extcon = extcon;
1210 dp->port[dp->ports++] = port;
1214 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1218 mutex_init(&dp->lock);
1219 dev_set_drvdata(dev, dp);
1221 return component_add(dev, &cdn_dp_component_ops);
1224 static int cdn_dp_remove(struct platform_device *pdev)
1226 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1228 cdn_dp_suspend(dp->dev);
1229 component_del(&pdev->dev, &cdn_dp_component_ops);
1234 static void cdn_dp_shutdown(struct platform_device *pdev)
1236 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1238 cdn_dp_suspend(dp->dev);
1241 static const struct dev_pm_ops cdn_dp_pm_ops = {
1242 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1246 static struct platform_driver cdn_dp_driver = {
1247 .probe = cdn_dp_probe,
1248 .remove = cdn_dp_remove,
1249 .shutdown = cdn_dp_shutdown,
1252 .owner = THIS_MODULE,
1253 .of_match_table = of_match_ptr(cdn_dp_dt_ids),
1254 .pm = &cdn_dp_pm_ops,
1258 module_platform_driver(cdn_dp_driver);
1261 MODULE_DESCRIPTION("cdn DP Driver");
1262 MODULE_LICENSE("GPL v2");