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);
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\n");
131 cdn_dp_set_fw_clk(dp, rate);
132 cdn_dp_clock_reset(dp);
137 clk_disable_unprepare(dp->core_clk);
139 clk_disable_unprepare(dp->pclk);
144 static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
146 pm_runtime_put_sync(dp->dev);
147 clk_disable_unprepare(dp->pclk);
148 clk_disable_unprepare(dp->core_clk);
151 static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
153 struct extcon_dev *edev = port->extcon;
154 union extcon_property_value property;
158 dptx = extcon_get_state(edev, EXTCON_DISP_DP);
160 extcon_get_property(edev, EXTCON_DISP_DP,
161 EXTCON_PROP_USB_SS, &property);
173 static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
179 ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
183 *sink_count = DP_GET_SINK_COUNT(value);
187 static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
189 struct cdn_dp_port *port;
192 for (i = 0; i < dp->ports; i++) {
194 lanes = cdn_dp_get_port_lanes(port);
201 static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
203 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
204 struct cdn_dp_port *port;
207 if (dp->active_port < 0 || dp->active_port >= dp->ports) {
208 DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
212 port = dp->port[dp->active_port];
215 * Attempt to read sink count, retry in case the sink may not be ready.
217 * Sinks are *supposed* to come up within 1ms from an off state, but
218 * some docks need more time to power up.
220 while (time_before(jiffies, timeout)) {
221 if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
224 if (!cdn_dp_get_sink_count(dp, &sink_count))
225 return sink_count ? true : false;
227 usleep_range(5000, 10000);
230 DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
234 static enum drm_connector_status
235 cdn_dp_connector_detect(struct drm_connector *connector, bool force)
237 struct cdn_dp_device *dp = connector_to_dp(connector);
238 enum drm_connector_status status = connector_status_disconnected;
240 mutex_lock(&dp->lock);
242 status = connector_status_connected;
243 mutex_unlock(&dp->lock);
248 static void cdn_dp_connector_destroy(struct drm_connector *connector)
250 drm_connector_unregister(connector);
251 drm_connector_cleanup(connector);
254 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
255 .dpms = drm_atomic_helper_connector_dpms,
256 .detect = cdn_dp_connector_detect,
257 .destroy = cdn_dp_connector_destroy,
258 .fill_modes = drm_helper_probe_single_connector_modes,
259 .reset = drm_atomic_helper_connector_reset,
260 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
261 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
264 static int cdn_dp_connector_get_modes(struct drm_connector *connector)
266 struct cdn_dp_device *dp = connector_to_dp(connector);
270 mutex_lock(&dp->lock);
273 DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
274 edid->width_cm, edid->height_cm);
276 dp->sink_has_audio = drm_detect_monitor_audio(edid);
277 ret = drm_add_edid_modes(connector, edid);
279 drm_mode_connector_update_edid_property(connector,
281 drm_edid_to_eld(connector, edid);
284 mutex_unlock(&dp->lock);
289 static struct drm_encoder *
290 cdn_dp_connector_best_encoder(struct drm_connector *connector)
292 struct cdn_dp_device *dp = connector_to_dp(connector);
297 static int cdn_dp_connector_mode_valid(struct drm_connector *connector,
298 struct drm_display_mode *mode)
300 struct cdn_dp_device *dp = connector_to_dp(connector);
301 struct drm_display_info *display_info = &dp->connector.display_info;
302 u32 requested, actual, rate, sink_max, source_max = 0;
305 /* If DP is disconnected, every mode is invalid */
309 switch (display_info->bpc) {
321 requested = mode->clock * bpc * 3 / 1000;
323 source_max = dp->lanes;
324 sink_max = drm_dp_max_lane_count(dp->dpcd);
325 lanes = min(source_max, sink_max);
327 source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
328 sink_max = drm_dp_max_link_rate(dp->dpcd);
329 rate = min(source_max, sink_max);
331 actual = rate * lanes / 100;
333 /* efficiency is about 0.8 */
334 actual = actual * 8 / 10;
336 if (requested > actual) {
337 DRM_DEV_DEBUG_KMS(dp->dev,
338 "requested=%d, actual=%d, clock=%d\n",
339 requested, actual, mode->clock);
340 return MODE_CLOCK_HIGH;
346 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
347 .get_modes = cdn_dp_connector_get_modes,
348 .best_encoder = cdn_dp_connector_best_encoder,
349 .mode_valid = cdn_dp_connector_mode_valid,
352 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
355 const u32 *iram_data, *dram_data;
356 const struct firmware *fw = dp->fw;
357 const struct cdn_firmware_header *hdr;
359 hdr = (struct cdn_firmware_header *)fw->data;
360 if (fw->size != le32_to_cpu(hdr->size_bytes)) {
361 DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
365 iram_data = (const u32 *)(fw->data + hdr->header_size);
366 dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
368 ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
369 dram_data, hdr->dram_size);
373 ret = cdn_dp_set_firmware_active(dp, true);
375 DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
379 return cdn_dp_event_config(dp);
382 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
386 if (!cdn_dp_check_sink_connection(dp))
389 ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
390 DP_RECEIVER_CAP_SIZE);
392 DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
397 dp->edid = drm_do_get_edid(&dp->connector,
398 cdn_dp_get_edid_block, dp);
402 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
404 union extcon_property_value property;
407 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
408 (port->id << UPHY_SEL_BIT) | UPHY_SEL_MASK);
412 if (!port->phy_enabled) {
413 ret = phy_power_on(port->phy);
415 DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
419 port->phy_enabled = true;
422 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
423 DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
425 DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
429 ret = cdn_dp_get_hpd_status(dp);
432 DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
436 ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
437 EXTCON_PROP_USB_TYPEC_POLARITY, &property);
439 DRM_DEV_ERROR(dp->dev, "get property failed\n");
443 port->lanes = cdn_dp_get_port_lanes(port);
444 ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
446 DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
451 dp->active_port = port->id;
455 if (phy_power_off(port->phy))
456 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
458 port->phy_enabled = false;
461 cdn_dp_grf_write(dp, GRF_SOC_CON26,
462 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
466 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
467 struct cdn_dp_port *port)
471 if (port->phy_enabled) {
472 ret = phy_power_off(port->phy);
474 DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
479 port->phy_enabled = false;
481 dp->active_port = -1;
485 static int cdn_dp_disable(struct cdn_dp_device *dp)
492 for (i = 0; i < dp->ports; i++)
493 cdn_dp_disable_phy(dp, dp->port[i]);
495 ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
496 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
498 DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
503 cdn_dp_set_firmware_active(dp, false);
504 cdn_dp_clk_disable(dp);
507 dp->link.num_lanes = 0;
508 if (!dp->connected) {
516 static int cdn_dp_enable(struct cdn_dp_device *dp)
519 struct cdn_dp_port *port;
521 port = cdn_dp_connected_port(dp);
523 DRM_DEV_ERROR(dp->dev,
524 "Can't enable without connection\n");
531 ret = cdn_dp_clk_enable(dp);
535 ret = cdn_dp_firmware_init(dp);
537 DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
538 goto err_clk_disable;
541 /* only enable the port that connected with downstream device */
542 for (i = port->id; i < dp->ports; i++) {
544 lanes = cdn_dp_get_port_lanes(port);
546 ret = cdn_dp_enable_phy(dp, port);
550 ret = cdn_dp_get_sink_capability(dp);
552 cdn_dp_disable_phy(dp, port);
555 dp->lanes = port->lanes;
562 cdn_dp_clk_disable(dp);
566 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
567 struct drm_display_mode *mode,
568 struct drm_display_mode *adjusted)
570 struct cdn_dp_device *dp = encoder_to_dp(encoder);
571 struct drm_display_info *display_info = &dp->connector.display_info;
572 struct video_info *video = &dp->video_info;
574 switch (display_info->bpc) {
576 video->color_depth = 10;
579 video->color_depth = 6;
582 video->color_depth = 8;
586 video->color_fmt = PXL_RGB;
587 video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
588 video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
590 memcpy(&dp->mode, adjusted, sizeof(*mode));
593 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
595 u8 link_status[DP_LINK_STATUS_SIZE];
596 struct cdn_dp_port *port = cdn_dp_connected_port(dp);
597 u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
599 if (!port || !dp->link.rate || !dp->link.num_lanes)
602 if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
603 DP_LINK_STATUS_SIZE)) {
604 DRM_ERROR("Failed to get link status\n");
608 /* if link training is requested we should perform it always */
609 return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
612 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
614 struct cdn_dp_device *dp = encoder_to_dp(encoder);
616 struct rockchip_crtc_state *state;
618 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
620 DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
624 DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
625 (ret) ? "LIT" : "BIG");
626 state = to_rockchip_crtc_state(encoder->crtc->state);
628 val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
629 state->output_mode = ROCKCHIP_OUT_MODE_P888;
631 val = DP_SEL_VOP_LIT << 16;
632 state->output_mode = ROCKCHIP_OUT_MODE_AAAA;
635 ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
639 mutex_lock(&dp->lock);
641 ret = cdn_dp_enable(dp);
643 DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
647 if (!cdn_dp_check_link_status(dp)) {
648 ret = cdn_dp_train_link(dp);
650 DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
655 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
657 DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
661 ret = cdn_dp_config_video(dp);
663 DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
667 ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
669 DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
673 mutex_unlock(&dp->lock);
676 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
678 struct cdn_dp_device *dp = encoder_to_dp(encoder);
681 mutex_lock(&dp->lock);
683 ret = cdn_dp_disable(dp);
685 DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
689 mutex_unlock(&dp->lock);
692 * In the following 2 cases, we need to run the event_work to re-enable
694 * 1. If there is not just one port device is connected, and remove one
695 * device from a port, the DP will be disabled here, at this case,
696 * run the event_work to re-open DP for the other port.
697 * 2. If re-training or re-config failed, the DP will be disabled here.
698 * run the event_work to re-connect it.
700 if (!dp->connected && cdn_dp_connected_port(dp))
701 schedule_work(&dp->event_work);
704 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
705 struct drm_crtc_state *crtc_state,
706 struct drm_connector_state *conn_state)
708 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
710 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
711 s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
716 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
717 .mode_set = cdn_dp_encoder_mode_set,
718 .enable = cdn_dp_encoder_enable,
719 .disable = cdn_dp_encoder_disable,
720 .atomic_check = cdn_dp_encoder_atomic_check,
723 static const struct drm_encoder_funcs cdn_dp_encoder_funcs = {
724 .destroy = drm_encoder_cleanup,
727 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
729 struct device *dev = dp->dev;
730 struct device_node *np = dev->of_node;
731 struct platform_device *pdev = to_platform_device(dev);
732 struct resource *res;
734 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
735 if (IS_ERR(dp->grf)) {
736 DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
737 return PTR_ERR(dp->grf);
740 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
741 dp->regs = devm_ioremap_resource(dev, res);
742 if (IS_ERR(dp->regs)) {
743 DRM_DEV_ERROR(dev, "ioremap reg failed\n");
744 return PTR_ERR(dp->regs);
747 dp->core_clk = devm_clk_get(dev, "core-clk");
748 if (IS_ERR(dp->core_clk)) {
749 DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
750 return PTR_ERR(dp->core_clk);
753 dp->pclk = devm_clk_get(dev, "pclk");
754 if (IS_ERR(dp->pclk)) {
755 DRM_DEV_ERROR(dev, "cannot get pclk\n");
756 return PTR_ERR(dp->pclk);
759 dp->spdif_clk = devm_clk_get(dev, "spdif");
760 if (IS_ERR(dp->spdif_clk)) {
761 DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
762 return PTR_ERR(dp->spdif_clk);
765 dp->grf_clk = devm_clk_get(dev, "grf");
766 if (IS_ERR(dp->grf_clk)) {
767 DRM_DEV_ERROR(dev, "cannot get grf clk\n");
768 return PTR_ERR(dp->grf_clk);
771 dp->spdif_rst = devm_reset_control_get(dev, "spdif");
772 if (IS_ERR(dp->spdif_rst)) {
773 DRM_DEV_ERROR(dev, "no spdif reset control found\n");
774 return PTR_ERR(dp->spdif_rst);
777 dp->dptx_rst = devm_reset_control_get(dev, "dptx");
778 if (IS_ERR(dp->dptx_rst)) {
779 DRM_DEV_ERROR(dev, "no uphy reset control found\n");
780 return PTR_ERR(dp->dptx_rst);
783 dp->core_rst = devm_reset_control_get(dev, "core");
784 if (IS_ERR(dp->core_rst)) {
785 DRM_DEV_ERROR(dev, "no core reset control found\n");
786 return PTR_ERR(dp->core_rst);
789 dp->apb_rst = devm_reset_control_get(dev, "apb");
790 if (IS_ERR(dp->apb_rst)) {
791 DRM_DEV_ERROR(dev, "no apb reset control found\n");
792 return PTR_ERR(dp->apb_rst);
798 static int cdn_dp_audio_hw_params(struct device *dev, void *data,
799 struct hdmi_codec_daifmt *daifmt,
800 struct hdmi_codec_params *params)
802 struct cdn_dp_device *dp = dev_get_drvdata(dev);
803 struct audio_info audio = {
804 .sample_width = params->sample_width,
805 .sample_rate = params->sample_rate,
806 .channels = params->channels,
810 mutex_lock(&dp->lock);
816 switch (daifmt->fmt) {
818 audio.format = AFMT_I2S;
821 audio.format = AFMT_SPDIF;
824 DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
829 ret = cdn_dp_audio_config(dp, &audio);
831 dp->audio_info = audio;
834 mutex_unlock(&dp->lock);
838 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
840 struct cdn_dp_device *dp = dev_get_drvdata(dev);
843 mutex_lock(&dp->lock);
847 ret = cdn_dp_audio_stop(dp, &dp->audio_info);
849 dp->audio_info.format = AFMT_UNUSED;
851 mutex_unlock(&dp->lock);
854 static int cdn_dp_audio_digital_mute(struct device *dev, void *data,
857 struct cdn_dp_device *dp = dev_get_drvdata(dev);
860 mutex_lock(&dp->lock);
866 ret = cdn_dp_audio_mute(dp, enable);
869 mutex_unlock(&dp->lock);
873 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
876 struct cdn_dp_device *dp = dev_get_drvdata(dev);
878 memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
883 static const struct hdmi_codec_ops audio_codec_ops = {
884 .hw_params = cdn_dp_audio_hw_params,
885 .audio_shutdown = cdn_dp_audio_shutdown,
886 .digital_mute = cdn_dp_audio_digital_mute,
887 .get_eld = cdn_dp_audio_get_eld,
890 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
893 struct hdmi_codec_pdata codec_data = {
896 .ops = &audio_codec_ops,
897 .max_i2s_channels = 8,
900 dp->audio_pdev = platform_device_register_data(
901 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
902 &codec_data, sizeof(codec_data));
904 return PTR_ERR_OR_ZERO(dp->audio_pdev);
907 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
910 unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
911 unsigned long sleep = 1000;
913 WARN_ON(!mutex_is_locked(&dp->lock));
918 /* Drop the lock before getting the firmware to avoid blocking boot */
919 mutex_unlock(&dp->lock);
921 while (time_before(jiffies, timeout)) {
922 ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
923 if (ret == -ENOENT) {
928 DRM_DEV_ERROR(dp->dev,
929 "failed to request firmware: %d\n", ret);
933 dp->fw_loaded = true;
938 DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
941 mutex_lock(&dp->lock);
945 static void cdn_dp_pd_event_work(struct work_struct *work)
947 struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
949 struct drm_connector *connector = &dp->connector;
950 enum drm_connector_status old_status;
954 mutex_lock(&dp->lock);
959 ret = cdn_dp_request_firmware(dp);
963 dp->connected = true;
965 /* Not connected, notify userspace to disable the block */
966 if (!cdn_dp_connected_port(dp)) {
967 DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
968 dp->connected = false;
970 /* Connected but not enabled, enable the block */
971 } else if (!dp->active) {
972 DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
973 ret = cdn_dp_enable(dp);
975 DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
976 dp->connected = false;
979 /* Enabled and connected to a dongle without a sink, notify userspace */
980 } else if (!cdn_dp_check_sink_connection(dp)) {
981 DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
982 dp->connected = false;
984 /* Enabled and connected with a sink, re-train if requested */
985 } else if (!cdn_dp_check_link_status(dp)) {
986 unsigned int rate = dp->link.rate;
987 unsigned int lanes = dp->link.num_lanes;
988 struct drm_display_mode *mode = &dp->mode;
990 DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
991 ret = cdn_dp_train_link(dp);
993 dp->connected = false;
994 DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
998 /* If training result is changed, update the video config */
1000 (rate != dp->link.rate || lanes != dp->link.num_lanes)) {
1001 ret = cdn_dp_config_video(dp);
1003 dp->connected = false;
1004 DRM_DEV_ERROR(dp->dev,
1005 "Failed to config video %d\n",
1012 mutex_unlock(&dp->lock);
1014 old_status = connector->status;
1015 connector->status = connector->funcs->detect(connector, false);
1016 if (old_status != connector->status)
1017 drm_kms_helper_hotplug_event(dp->drm_dev);
1020 static int cdn_dp_pd_event(struct notifier_block *nb,
1021 unsigned long event, void *priv)
1023 struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1025 struct cdn_dp_device *dp = port->dp;
1028 * It would be nice to be able to just do the work inline right here.
1029 * However, we need to make a bunch of calls that might sleep in order
1030 * to turn on the block/phy, so use a worker instead.
1032 schedule_work(&dp->event_work);
1037 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1039 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1040 struct drm_encoder *encoder;
1041 struct drm_connector *connector;
1042 struct cdn_dp_port *port;
1043 struct drm_device *drm_dev = data;
1046 ret = cdn_dp_parse_dt(dp);
1050 dp->drm_dev = drm_dev;
1051 dp->connected = false;
1053 dp->active_port = -1;
1055 INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1057 encoder = &dp->encoder;
1059 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1061 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1063 ret = drm_encoder_init(drm_dev, encoder, &cdn_dp_encoder_funcs,
1064 DRM_MODE_ENCODER_TMDS, NULL);
1066 DRM_ERROR("failed to initialize encoder with drm\n");
1070 drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1072 connector = &dp->connector;
1073 connector->polled = DRM_CONNECTOR_POLL_HPD;
1074 connector->dpms = DRM_MODE_DPMS_OFF;
1076 ret = drm_connector_init(drm_dev, connector,
1077 &cdn_dp_atomic_connector_funcs,
1078 DRM_MODE_CONNECTOR_DisplayPort);
1080 DRM_ERROR("failed to initialize connector with drm\n");
1081 goto err_free_encoder;
1084 drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1086 ret = drm_mode_connector_attach_encoder(connector, encoder);
1088 DRM_ERROR("failed to attach connector and encoder\n");
1089 goto err_free_connector;
1092 cdn_dp_audio_codec_init(dp, dev);
1094 for (i = 0; i < dp->ports; i++) {
1097 port->event_nb.notifier_call = cdn_dp_pd_event;
1098 ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1103 "register EXTCON_DISP_DP notifier err\n");
1104 goto err_free_connector;
1108 pm_runtime_enable(dev);
1110 schedule_work(&dp->event_work);
1115 drm_connector_cleanup(connector);
1117 drm_encoder_cleanup(encoder);
1121 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1123 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1124 struct drm_encoder *encoder = &dp->encoder;
1125 struct drm_connector *connector = &dp->connector;
1127 cancel_work_sync(&dp->event_work);
1128 platform_device_unregister(dp->audio_pdev);
1129 cdn_dp_encoder_disable(encoder);
1130 encoder->funcs->destroy(encoder);
1131 connector->funcs->destroy(connector);
1133 pm_runtime_disable(dev);
1134 release_firmware(dp->fw);
1139 static const struct component_ops cdn_dp_component_ops = {
1140 .bind = cdn_dp_bind,
1141 .unbind = cdn_dp_unbind,
1144 int cdn_dp_suspend(struct device *dev)
1146 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1149 mutex_lock(&dp->lock);
1151 ret = cdn_dp_disable(dp);
1152 dp->suspended = true;
1153 mutex_unlock(&dp->lock);
1158 int cdn_dp_resume(struct device *dev)
1160 struct cdn_dp_device *dp = dev_get_drvdata(dev);
1162 mutex_lock(&dp->lock);
1163 dp->suspended = false;
1165 schedule_work(&dp->event_work);
1166 mutex_unlock(&dp->lock);
1171 static int cdn_dp_probe(struct platform_device *pdev)
1173 struct device *dev = &pdev->dev;
1174 const struct of_device_id *match;
1175 struct cdn_dp_data *dp_data;
1176 struct cdn_dp_port *port;
1177 struct cdn_dp_device *dp;
1178 struct extcon_dev *extcon;
1182 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1187 match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1188 dp_data = (struct cdn_dp_data *)match->data;
1190 for (i = 0; i < dp_data->max_phy; i++) {
1191 extcon = extcon_get_edev_by_phandle(dev, i);
1192 phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1194 if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1195 PTR_ERR(phy) == -EPROBE_DEFER)
1196 return -EPROBE_DEFER;
1198 if (IS_ERR(extcon) || IS_ERR(phy))
1201 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1205 port->extcon = extcon;
1209 dp->port[dp->ports++] = port;
1213 DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1217 mutex_init(&dp->lock);
1218 dev_set_drvdata(dev, dp);
1220 return component_add(dev, &cdn_dp_component_ops);
1223 static int cdn_dp_remove(struct platform_device *pdev)
1225 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1227 cdn_dp_suspend(dp->dev);
1228 component_del(&pdev->dev, &cdn_dp_component_ops);
1233 static void cdn_dp_shutdown(struct platform_device *pdev)
1235 struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1237 cdn_dp_suspend(dp->dev);
1240 static const struct dev_pm_ops cdn_dp_pm_ops = {
1241 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1245 static struct platform_driver cdn_dp_driver = {
1246 .probe = cdn_dp_probe,
1247 .remove = cdn_dp_remove,
1248 .shutdown = cdn_dp_shutdown,
1251 .owner = THIS_MODULE,
1252 .of_match_table = of_match_ptr(cdn_dp_dt_ids),
1253 .pm = &cdn_dp_pm_ops,
1257 module_platform_driver(cdn_dp_driver);
1260 MODULE_DESCRIPTION("cdn DP Driver");
1261 MODULE_LICENSE("GPL v2");