1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author: Chris Zhong <zyw@rock-chips.com>
8 #include <linux/component.h>
9 #include <linux/extcon.h>
10 #include <linux/firmware.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/phy/phy.h>
13 #include <linux/regmap.h>
14 #include <linux/reset.h>
16 #include <sound/hdmi-codec.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_dp_helper.h>
20 #include <drm/drm_edid.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_probe_helper.h>
24 #include "cdn-dp-core.h"
25 #include "cdn-dp-reg.h"
26 #include "rockchip_drm_vop.h"
28 #define connector_to_dp(c) \
29 container_of(c, struct cdn_dp_device, connector)
31 #define encoder_to_dp(c) \
32 container_of(c, struct cdn_dp_device, encoder)
34 #define GRF_SOC_CON9 0x6224
35 #define DP_SEL_VOP_LIT BIT(12)
36 #define GRF_SOC_CON26 0x6268
37 #define DPTX_HPD_SEL (3 << 12)
38 #define DPTX_HPD_DEL (2 << 12)
39 #define DPTX_HPD_SEL_MASK (3 << 28)
41 #define CDN_FW_TIMEOUT_MS (64 * 1000)
42 #define CDN_DPCD_TIMEOUT_MS 5000
43 #define CDN_DP_FIRMWARE "rockchip/dptx.bin"
49 struct cdn_dp_data rk3399_cdn_dp
= {
53 static const struct of_device_id cdn_dp_dt_ids
[] = {
54 { .compatible
= "rockchip,rk3399-cdn-dp",
55 .data
= (void *)&rk3399_cdn_dp
},
59 MODULE_DEVICE_TABLE(of
, cdn_dp_dt_ids
);
61 static int cdn_dp_grf_write(struct cdn_dp_device
*dp
,
62 unsigned int reg
, unsigned int val
)
66 ret
= clk_prepare_enable(dp
->grf_clk
);
68 DRM_DEV_ERROR(dp
->dev
, "Failed to prepare_enable grf clock\n");
72 ret
= regmap_write(dp
->grf
, reg
, val
);
74 DRM_DEV_ERROR(dp
->dev
, "Could not write to GRF: %d\n", ret
);
78 clk_disable_unprepare(dp
->grf_clk
);
83 static int cdn_dp_clk_enable(struct cdn_dp_device
*dp
)
88 ret
= clk_prepare_enable(dp
->pclk
);
90 DRM_DEV_ERROR(dp
->dev
, "cannot enable dp pclk %d\n", ret
);
94 ret
= clk_prepare_enable(dp
->core_clk
);
96 DRM_DEV_ERROR(dp
->dev
, "cannot enable core_clk %d\n", ret
);
100 ret
= pm_runtime_get_sync(dp
->dev
);
102 DRM_DEV_ERROR(dp
->dev
, "cannot get pm runtime %d\n", ret
);
103 goto err_pm_runtime_get
;
106 reset_control_assert(dp
->core_rst
);
107 reset_control_assert(dp
->dptx_rst
);
108 reset_control_assert(dp
->apb_rst
);
109 reset_control_deassert(dp
->core_rst
);
110 reset_control_deassert(dp
->dptx_rst
);
111 reset_control_deassert(dp
->apb_rst
);
113 rate
= clk_get_rate(dp
->core_clk
);
115 DRM_DEV_ERROR(dp
->dev
, "get clk rate failed\n");
120 cdn_dp_set_fw_clk(dp
, rate
);
121 cdn_dp_clock_reset(dp
);
126 pm_runtime_put(dp
->dev
);
128 clk_disable_unprepare(dp
->core_clk
);
130 clk_disable_unprepare(dp
->pclk
);
135 static void cdn_dp_clk_disable(struct cdn_dp_device
*dp
)
137 pm_runtime_put_sync(dp
->dev
);
138 clk_disable_unprepare(dp
->pclk
);
139 clk_disable_unprepare(dp
->core_clk
);
142 static int cdn_dp_get_port_lanes(struct cdn_dp_port
*port
)
144 struct extcon_dev
*edev
= port
->extcon
;
145 union extcon_property_value property
;
149 dptx
= extcon_get_state(edev
, EXTCON_DISP_DP
);
151 extcon_get_property(edev
, EXTCON_DISP_DP
,
152 EXTCON_PROP_USB_SS
, &property
);
164 static int cdn_dp_get_sink_count(struct cdn_dp_device
*dp
, u8
*sink_count
)
170 ret
= cdn_dp_dpcd_read(dp
, DP_SINK_COUNT
, &value
, 1);
174 *sink_count
= DP_GET_SINK_COUNT(value
);
178 static struct cdn_dp_port
*cdn_dp_connected_port(struct cdn_dp_device
*dp
)
180 struct cdn_dp_port
*port
;
183 for (i
= 0; i
< dp
->ports
; i
++) {
185 lanes
= cdn_dp_get_port_lanes(port
);
192 static bool cdn_dp_check_sink_connection(struct cdn_dp_device
*dp
)
194 unsigned long timeout
= jiffies
+ msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS
);
195 struct cdn_dp_port
*port
;
198 if (dp
->active_port
< 0 || dp
->active_port
>= dp
->ports
) {
199 DRM_DEV_ERROR(dp
->dev
, "active_port is wrong!\n");
203 port
= dp
->port
[dp
->active_port
];
206 * Attempt to read sink count, retry in case the sink may not be ready.
208 * Sinks are *supposed* to come up within 1ms from an off state, but
209 * some docks need more time to power up.
211 while (time_before(jiffies
, timeout
)) {
212 if (!extcon_get_state(port
->extcon
, EXTCON_DISP_DP
))
215 if (!cdn_dp_get_sink_count(dp
, &sink_count
))
216 return sink_count
? true : false;
218 usleep_range(5000, 10000);
221 DRM_DEV_ERROR(dp
->dev
, "Get sink capability timed out\n");
225 static enum drm_connector_status
226 cdn_dp_connector_detect(struct drm_connector
*connector
, bool force
)
228 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
229 enum drm_connector_status status
= connector_status_disconnected
;
231 mutex_lock(&dp
->lock
);
233 status
= connector_status_connected
;
234 mutex_unlock(&dp
->lock
);
239 static void cdn_dp_connector_destroy(struct drm_connector
*connector
)
241 drm_connector_unregister(connector
);
242 drm_connector_cleanup(connector
);
245 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs
= {
246 .detect
= cdn_dp_connector_detect
,
247 .destroy
= cdn_dp_connector_destroy
,
248 .fill_modes
= drm_helper_probe_single_connector_modes
,
249 .reset
= drm_atomic_helper_connector_reset
,
250 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
251 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
254 static int cdn_dp_connector_get_modes(struct drm_connector
*connector
)
256 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
260 mutex_lock(&dp
->lock
);
263 DRM_DEV_DEBUG_KMS(dp
->dev
, "got edid: width[%d] x height[%d]\n",
264 edid
->width_cm
, edid
->height_cm
);
266 dp
->sink_has_audio
= drm_detect_monitor_audio(edid
);
267 ret
= drm_add_edid_modes(connector
, edid
);
269 drm_connector_update_edid_property(connector
,
272 mutex_unlock(&dp
->lock
);
277 static int cdn_dp_connector_mode_valid(struct drm_connector
*connector
,
278 struct drm_display_mode
*mode
)
280 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
281 struct drm_display_info
*display_info
= &dp
->connector
.display_info
;
282 u32 requested
, actual
, rate
, sink_max
, source_max
= 0;
285 /* If DP is disconnected, every mode is invalid */
289 switch (display_info
->bpc
) {
301 requested
= mode
->clock
* bpc
* 3 / 1000;
303 source_max
= dp
->lanes
;
304 sink_max
= drm_dp_max_lane_count(dp
->dpcd
);
305 lanes
= min(source_max
, sink_max
);
307 source_max
= drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE
);
308 sink_max
= drm_dp_max_link_rate(dp
->dpcd
);
309 rate
= min(source_max
, sink_max
);
311 actual
= rate
* lanes
/ 100;
313 /* efficiency is about 0.8 */
314 actual
= actual
* 8 / 10;
316 if (requested
> actual
) {
317 DRM_DEV_DEBUG_KMS(dp
->dev
,
318 "requested=%d, actual=%d, clock=%d\n",
319 requested
, actual
, mode
->clock
);
320 return MODE_CLOCK_HIGH
;
326 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs
= {
327 .get_modes
= cdn_dp_connector_get_modes
,
328 .mode_valid
= cdn_dp_connector_mode_valid
,
331 static int cdn_dp_firmware_init(struct cdn_dp_device
*dp
)
334 const u32
*iram_data
, *dram_data
;
335 const struct firmware
*fw
= dp
->fw
;
336 const struct cdn_firmware_header
*hdr
;
338 hdr
= (struct cdn_firmware_header
*)fw
->data
;
339 if (fw
->size
!= le32_to_cpu(hdr
->size_bytes
)) {
340 DRM_DEV_ERROR(dp
->dev
, "firmware is invalid\n");
344 iram_data
= (const u32
*)(fw
->data
+ hdr
->header_size
);
345 dram_data
= (const u32
*)(fw
->data
+ hdr
->header_size
+ hdr
->iram_size
);
347 ret
= cdn_dp_load_firmware(dp
, iram_data
, hdr
->iram_size
,
348 dram_data
, hdr
->dram_size
);
352 ret
= cdn_dp_set_firmware_active(dp
, true);
354 DRM_DEV_ERROR(dp
->dev
, "active ucpu failed: %d\n", ret
);
358 return cdn_dp_event_config(dp
);
361 static int cdn_dp_get_sink_capability(struct cdn_dp_device
*dp
)
365 if (!cdn_dp_check_sink_connection(dp
))
368 ret
= cdn_dp_dpcd_read(dp
, DP_DPCD_REV
, dp
->dpcd
,
369 DP_RECEIVER_CAP_SIZE
);
371 DRM_DEV_ERROR(dp
->dev
, "Failed to get caps %d\n", ret
);
376 dp
->edid
= drm_do_get_edid(&dp
->connector
,
377 cdn_dp_get_edid_block
, dp
);
381 static int cdn_dp_enable_phy(struct cdn_dp_device
*dp
, struct cdn_dp_port
*port
)
383 union extcon_property_value property
;
386 if (!port
->phy_enabled
) {
387 ret
= phy_power_on(port
->phy
);
389 DRM_DEV_ERROR(dp
->dev
, "phy power on failed: %d\n",
393 port
->phy_enabled
= true;
396 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
397 DPTX_HPD_SEL_MASK
| DPTX_HPD_SEL
);
399 DRM_DEV_ERROR(dp
->dev
, "Failed to write HPD_SEL %d\n", ret
);
403 ret
= cdn_dp_get_hpd_status(dp
);
406 DRM_DEV_ERROR(dp
->dev
, "hpd does not exist\n");
410 ret
= extcon_get_property(port
->extcon
, EXTCON_DISP_DP
,
411 EXTCON_PROP_USB_TYPEC_POLARITY
, &property
);
413 DRM_DEV_ERROR(dp
->dev
, "get property failed\n");
417 port
->lanes
= cdn_dp_get_port_lanes(port
);
418 ret
= cdn_dp_set_host_cap(dp
, port
->lanes
, property
.intval
);
420 DRM_DEV_ERROR(dp
->dev
, "set host capabilities failed: %d\n",
425 dp
->active_port
= port
->id
;
429 if (phy_power_off(port
->phy
))
430 DRM_DEV_ERROR(dp
->dev
, "phy power off failed: %d", ret
);
432 port
->phy_enabled
= false;
435 cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
436 DPTX_HPD_SEL_MASK
| DPTX_HPD_DEL
);
440 static int cdn_dp_disable_phy(struct cdn_dp_device
*dp
,
441 struct cdn_dp_port
*port
)
445 if (port
->phy_enabled
) {
446 ret
= phy_power_off(port
->phy
);
448 DRM_DEV_ERROR(dp
->dev
, "phy power off failed: %d", ret
);
453 port
->phy_enabled
= false;
455 dp
->active_port
= -1;
459 static int cdn_dp_disable(struct cdn_dp_device
*dp
)
466 for (i
= 0; i
< dp
->ports
; i
++)
467 cdn_dp_disable_phy(dp
, dp
->port
[i
]);
469 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
470 DPTX_HPD_SEL_MASK
| DPTX_HPD_DEL
);
472 DRM_DEV_ERROR(dp
->dev
, "Failed to clear hpd sel %d\n",
477 cdn_dp_set_firmware_active(dp
, false);
478 cdn_dp_clk_disable(dp
);
482 if (!dp
->connected
) {
490 static int cdn_dp_enable(struct cdn_dp_device
*dp
)
493 struct cdn_dp_port
*port
;
495 port
= cdn_dp_connected_port(dp
);
497 DRM_DEV_ERROR(dp
->dev
,
498 "Can't enable without connection\n");
505 ret
= cdn_dp_clk_enable(dp
);
509 ret
= cdn_dp_firmware_init(dp
);
511 DRM_DEV_ERROR(dp
->dev
, "firmware init failed: %d", ret
);
512 goto err_clk_disable
;
515 /* only enable the port that connected with downstream device */
516 for (i
= port
->id
; i
< dp
->ports
; i
++) {
518 lanes
= cdn_dp_get_port_lanes(port
);
520 ret
= cdn_dp_enable_phy(dp
, port
);
524 ret
= cdn_dp_get_sink_capability(dp
);
526 cdn_dp_disable_phy(dp
, port
);
529 dp
->lanes
= port
->lanes
;
536 cdn_dp_clk_disable(dp
);
540 static void cdn_dp_encoder_mode_set(struct drm_encoder
*encoder
,
541 struct drm_display_mode
*mode
,
542 struct drm_display_mode
*adjusted
)
544 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
545 struct drm_display_info
*display_info
= &dp
->connector
.display_info
;
546 struct video_info
*video
= &dp
->video_info
;
548 switch (display_info
->bpc
) {
550 video
->color_depth
= 10;
553 video
->color_depth
= 6;
556 video
->color_depth
= 8;
560 video
->color_fmt
= PXL_RGB
;
561 video
->v_sync_polarity
= !!(mode
->flags
& DRM_MODE_FLAG_NVSYNC
);
562 video
->h_sync_polarity
= !!(mode
->flags
& DRM_MODE_FLAG_NHSYNC
);
564 memcpy(&dp
->mode
, adjusted
, sizeof(*mode
));
567 static bool cdn_dp_check_link_status(struct cdn_dp_device
*dp
)
569 u8 link_status
[DP_LINK_STATUS_SIZE
];
570 struct cdn_dp_port
*port
= cdn_dp_connected_port(dp
);
571 u8 sink_lanes
= drm_dp_max_lane_count(dp
->dpcd
);
573 if (!port
|| !dp
->max_rate
|| !dp
->max_lanes
)
576 if (cdn_dp_dpcd_read(dp
, DP_LANE0_1_STATUS
, link_status
,
577 DP_LINK_STATUS_SIZE
)) {
578 DRM_ERROR("Failed to get link status\n");
582 /* if link training is requested we should perform it always */
583 return drm_dp_channel_eq_ok(link_status
, min(port
->lanes
, sink_lanes
));
586 static void cdn_dp_encoder_enable(struct drm_encoder
*encoder
)
588 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
591 ret
= drm_of_encoder_active_endpoint_id(dp
->dev
->of_node
, encoder
);
593 DRM_DEV_ERROR(dp
->dev
, "Could not get vop id, %d", ret
);
597 DRM_DEV_DEBUG_KMS(dp
->dev
, "vop %s output to cdn-dp\n",
598 (ret
) ? "LIT" : "BIG");
600 val
= DP_SEL_VOP_LIT
| (DP_SEL_VOP_LIT
<< 16);
602 val
= DP_SEL_VOP_LIT
<< 16;
604 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON9
, val
);
608 mutex_lock(&dp
->lock
);
610 ret
= cdn_dp_enable(dp
);
612 DRM_DEV_ERROR(dp
->dev
, "Failed to enable encoder %d\n",
616 if (!cdn_dp_check_link_status(dp
)) {
617 ret
= cdn_dp_train_link(dp
);
619 DRM_DEV_ERROR(dp
->dev
, "Failed link train %d\n", ret
);
624 ret
= cdn_dp_set_video_status(dp
, CONTROL_VIDEO_IDLE
);
626 DRM_DEV_ERROR(dp
->dev
, "Failed to idle video %d\n", ret
);
630 ret
= cdn_dp_config_video(dp
);
632 DRM_DEV_ERROR(dp
->dev
, "Failed to config video %d\n", ret
);
636 ret
= cdn_dp_set_video_status(dp
, CONTROL_VIDEO_VALID
);
638 DRM_DEV_ERROR(dp
->dev
, "Failed to valid video %d\n", ret
);
642 mutex_unlock(&dp
->lock
);
645 static void cdn_dp_encoder_disable(struct drm_encoder
*encoder
)
647 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
650 mutex_lock(&dp
->lock
);
652 ret
= cdn_dp_disable(dp
);
654 DRM_DEV_ERROR(dp
->dev
, "Failed to disable encoder %d\n",
658 mutex_unlock(&dp
->lock
);
661 * In the following 2 cases, we need to run the event_work to re-enable
663 * 1. If there is not just one port device is connected, and remove one
664 * device from a port, the DP will be disabled here, at this case,
665 * run the event_work to re-open DP for the other port.
666 * 2. If re-training or re-config failed, the DP will be disabled here.
667 * run the event_work to re-connect it.
669 if (!dp
->connected
&& cdn_dp_connected_port(dp
))
670 schedule_work(&dp
->event_work
);
673 static int cdn_dp_encoder_atomic_check(struct drm_encoder
*encoder
,
674 struct drm_crtc_state
*crtc_state
,
675 struct drm_connector_state
*conn_state
)
677 struct rockchip_crtc_state
*s
= to_rockchip_crtc_state(crtc_state
);
679 s
->output_mode
= ROCKCHIP_OUT_MODE_AAAA
;
680 s
->output_type
= DRM_MODE_CONNECTOR_DisplayPort
;
685 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs
= {
686 .mode_set
= cdn_dp_encoder_mode_set
,
687 .enable
= cdn_dp_encoder_enable
,
688 .disable
= cdn_dp_encoder_disable
,
689 .atomic_check
= cdn_dp_encoder_atomic_check
,
692 static const struct drm_encoder_funcs cdn_dp_encoder_funcs
= {
693 .destroy
= drm_encoder_cleanup
,
696 static int cdn_dp_parse_dt(struct cdn_dp_device
*dp
)
698 struct device
*dev
= dp
->dev
;
699 struct device_node
*np
= dev
->of_node
;
700 struct platform_device
*pdev
= to_platform_device(dev
);
701 struct resource
*res
;
703 dp
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
704 if (IS_ERR(dp
->grf
)) {
705 DRM_DEV_ERROR(dev
, "cdn-dp needs rockchip,grf property\n");
706 return PTR_ERR(dp
->grf
);
709 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
710 dp
->regs
= devm_ioremap_resource(dev
, res
);
711 if (IS_ERR(dp
->regs
)) {
712 DRM_DEV_ERROR(dev
, "ioremap reg failed\n");
713 return PTR_ERR(dp
->regs
);
716 dp
->core_clk
= devm_clk_get(dev
, "core-clk");
717 if (IS_ERR(dp
->core_clk
)) {
718 DRM_DEV_ERROR(dev
, "cannot get core_clk_dp\n");
719 return PTR_ERR(dp
->core_clk
);
722 dp
->pclk
= devm_clk_get(dev
, "pclk");
723 if (IS_ERR(dp
->pclk
)) {
724 DRM_DEV_ERROR(dev
, "cannot get pclk\n");
725 return PTR_ERR(dp
->pclk
);
728 dp
->spdif_clk
= devm_clk_get(dev
, "spdif");
729 if (IS_ERR(dp
->spdif_clk
)) {
730 DRM_DEV_ERROR(dev
, "cannot get spdif_clk\n");
731 return PTR_ERR(dp
->spdif_clk
);
734 dp
->grf_clk
= devm_clk_get(dev
, "grf");
735 if (IS_ERR(dp
->grf_clk
)) {
736 DRM_DEV_ERROR(dev
, "cannot get grf clk\n");
737 return PTR_ERR(dp
->grf_clk
);
740 dp
->spdif_rst
= devm_reset_control_get(dev
, "spdif");
741 if (IS_ERR(dp
->spdif_rst
)) {
742 DRM_DEV_ERROR(dev
, "no spdif reset control found\n");
743 return PTR_ERR(dp
->spdif_rst
);
746 dp
->dptx_rst
= devm_reset_control_get(dev
, "dptx");
747 if (IS_ERR(dp
->dptx_rst
)) {
748 DRM_DEV_ERROR(dev
, "no uphy reset control found\n");
749 return PTR_ERR(dp
->dptx_rst
);
752 dp
->core_rst
= devm_reset_control_get(dev
, "core");
753 if (IS_ERR(dp
->core_rst
)) {
754 DRM_DEV_ERROR(dev
, "no core reset control found\n");
755 return PTR_ERR(dp
->core_rst
);
758 dp
->apb_rst
= devm_reset_control_get(dev
, "apb");
759 if (IS_ERR(dp
->apb_rst
)) {
760 DRM_DEV_ERROR(dev
, "no apb reset control found\n");
761 return PTR_ERR(dp
->apb_rst
);
767 static int cdn_dp_audio_hw_params(struct device
*dev
, void *data
,
768 struct hdmi_codec_daifmt
*daifmt
,
769 struct hdmi_codec_params
*params
)
771 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
772 struct audio_info audio
= {
773 .sample_width
= params
->sample_width
,
774 .sample_rate
= params
->sample_rate
,
775 .channels
= params
->channels
,
779 mutex_lock(&dp
->lock
);
785 switch (daifmt
->fmt
) {
787 audio
.format
= AFMT_I2S
;
790 audio
.format
= AFMT_SPDIF
;
793 DRM_DEV_ERROR(dev
, "Invalid format %d\n", daifmt
->fmt
);
798 ret
= cdn_dp_audio_config(dp
, &audio
);
800 dp
->audio_info
= audio
;
803 mutex_unlock(&dp
->lock
);
807 static void cdn_dp_audio_shutdown(struct device
*dev
, void *data
)
809 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
812 mutex_lock(&dp
->lock
);
816 ret
= cdn_dp_audio_stop(dp
, &dp
->audio_info
);
818 dp
->audio_info
.format
= AFMT_UNUSED
;
820 mutex_unlock(&dp
->lock
);
823 static int cdn_dp_audio_digital_mute(struct device
*dev
, void *data
,
826 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
829 mutex_lock(&dp
->lock
);
835 ret
= cdn_dp_audio_mute(dp
, enable
);
838 mutex_unlock(&dp
->lock
);
842 static int cdn_dp_audio_get_eld(struct device
*dev
, void *data
,
845 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
847 memcpy(buf
, dp
->connector
.eld
, min(sizeof(dp
->connector
.eld
), len
));
852 static const struct hdmi_codec_ops audio_codec_ops
= {
853 .hw_params
= cdn_dp_audio_hw_params
,
854 .audio_shutdown
= cdn_dp_audio_shutdown
,
855 .digital_mute
= cdn_dp_audio_digital_mute
,
856 .get_eld
= cdn_dp_audio_get_eld
,
859 static int cdn_dp_audio_codec_init(struct cdn_dp_device
*dp
,
862 struct hdmi_codec_pdata codec_data
= {
865 .ops
= &audio_codec_ops
,
866 .max_i2s_channels
= 8,
869 dp
->audio_pdev
= platform_device_register_data(
870 dev
, HDMI_CODEC_DRV_NAME
, PLATFORM_DEVID_AUTO
,
871 &codec_data
, sizeof(codec_data
));
873 return PTR_ERR_OR_ZERO(dp
->audio_pdev
);
876 static int cdn_dp_request_firmware(struct cdn_dp_device
*dp
)
879 unsigned long timeout
= jiffies
+ msecs_to_jiffies(CDN_FW_TIMEOUT_MS
);
880 unsigned long sleep
= 1000;
882 WARN_ON(!mutex_is_locked(&dp
->lock
));
887 /* Drop the lock before getting the firmware to avoid blocking boot */
888 mutex_unlock(&dp
->lock
);
890 while (time_before(jiffies
, timeout
)) {
891 ret
= request_firmware(&dp
->fw
, CDN_DP_FIRMWARE
, dp
->dev
);
892 if (ret
== -ENOENT
) {
897 DRM_DEV_ERROR(dp
->dev
,
898 "failed to request firmware: %d\n", ret
);
902 dp
->fw_loaded
= true;
907 DRM_DEV_ERROR(dp
->dev
, "Timed out trying to load firmware\n");
910 mutex_lock(&dp
->lock
);
914 static void cdn_dp_pd_event_work(struct work_struct
*work
)
916 struct cdn_dp_device
*dp
= container_of(work
, struct cdn_dp_device
,
918 struct drm_connector
*connector
= &dp
->connector
;
919 enum drm_connector_status old_status
;
923 mutex_lock(&dp
->lock
);
928 ret
= cdn_dp_request_firmware(dp
);
932 dp
->connected
= true;
934 /* Not connected, notify userspace to disable the block */
935 if (!cdn_dp_connected_port(dp
)) {
936 DRM_DEV_INFO(dp
->dev
, "Not connected. Disabling cdn\n");
937 dp
->connected
= false;
939 /* Connected but not enabled, enable the block */
940 } else if (!dp
->active
) {
941 DRM_DEV_INFO(dp
->dev
, "Connected, not enabled. Enabling cdn\n");
942 ret
= cdn_dp_enable(dp
);
944 DRM_DEV_ERROR(dp
->dev
, "Enable dp failed %d\n", ret
);
945 dp
->connected
= false;
948 /* Enabled and connected to a dongle without a sink, notify userspace */
949 } else if (!cdn_dp_check_sink_connection(dp
)) {
950 DRM_DEV_INFO(dp
->dev
, "Connected without sink. Assert hpd\n");
951 dp
->connected
= false;
953 /* Enabled and connected with a sink, re-train if requested */
954 } else if (!cdn_dp_check_link_status(dp
)) {
955 unsigned int rate
= dp
->max_rate
;
956 unsigned int lanes
= dp
->max_lanes
;
957 struct drm_display_mode
*mode
= &dp
->mode
;
959 DRM_DEV_INFO(dp
->dev
, "Connected with sink. Re-train link\n");
960 ret
= cdn_dp_train_link(dp
);
962 dp
->connected
= false;
963 DRM_DEV_ERROR(dp
->dev
, "Train link failed %d\n", ret
);
967 /* If training result is changed, update the video config */
969 (rate
!= dp
->max_rate
|| lanes
!= dp
->max_lanes
)) {
970 ret
= cdn_dp_config_video(dp
);
972 dp
->connected
= false;
973 DRM_DEV_ERROR(dp
->dev
,
974 "Failed to config video %d\n",
981 mutex_unlock(&dp
->lock
);
983 old_status
= connector
->status
;
984 connector
->status
= connector
->funcs
->detect(connector
, false);
985 if (old_status
!= connector
->status
)
986 drm_kms_helper_hotplug_event(dp
->drm_dev
);
989 static int cdn_dp_pd_event(struct notifier_block
*nb
,
990 unsigned long event
, void *priv
)
992 struct cdn_dp_port
*port
= container_of(nb
, struct cdn_dp_port
,
994 struct cdn_dp_device
*dp
= port
->dp
;
997 * It would be nice to be able to just do the work inline right here.
998 * However, we need to make a bunch of calls that might sleep in order
999 * to turn on the block/phy, so use a worker instead.
1001 schedule_work(&dp
->event_work
);
1006 static int cdn_dp_bind(struct device
*dev
, struct device
*master
, void *data
)
1008 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1009 struct drm_encoder
*encoder
;
1010 struct drm_connector
*connector
;
1011 struct cdn_dp_port
*port
;
1012 struct drm_device
*drm_dev
= data
;
1015 ret
= cdn_dp_parse_dt(dp
);
1019 dp
->drm_dev
= drm_dev
;
1020 dp
->connected
= false;
1022 dp
->active_port
= -1;
1023 dp
->fw_loaded
= false;
1025 INIT_WORK(&dp
->event_work
, cdn_dp_pd_event_work
);
1027 encoder
= &dp
->encoder
;
1029 encoder
->possible_crtcs
= drm_of_find_possible_crtcs(drm_dev
,
1031 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder
->possible_crtcs
);
1033 ret
= drm_encoder_init(drm_dev
, encoder
, &cdn_dp_encoder_funcs
,
1034 DRM_MODE_ENCODER_TMDS
, NULL
);
1036 DRM_ERROR("failed to initialize encoder with drm\n");
1040 drm_encoder_helper_add(encoder
, &cdn_dp_encoder_helper_funcs
);
1042 connector
= &dp
->connector
;
1043 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
1044 connector
->dpms
= DRM_MODE_DPMS_OFF
;
1046 ret
= drm_connector_init(drm_dev
, connector
,
1047 &cdn_dp_atomic_connector_funcs
,
1048 DRM_MODE_CONNECTOR_DisplayPort
);
1050 DRM_ERROR("failed to initialize connector with drm\n");
1051 goto err_free_encoder
;
1054 drm_connector_helper_add(connector
, &cdn_dp_connector_helper_funcs
);
1056 ret
= drm_connector_attach_encoder(connector
, encoder
);
1058 DRM_ERROR("failed to attach connector and encoder\n");
1059 goto err_free_connector
;
1062 for (i
= 0; i
< dp
->ports
; i
++) {
1065 port
->event_nb
.notifier_call
= cdn_dp_pd_event
;
1066 ret
= devm_extcon_register_notifier(dp
->dev
, port
->extcon
,
1071 "register EXTCON_DISP_DP notifier err\n");
1072 goto err_free_connector
;
1076 pm_runtime_enable(dev
);
1078 schedule_work(&dp
->event_work
);
1083 drm_connector_cleanup(connector
);
1085 drm_encoder_cleanup(encoder
);
1089 static void cdn_dp_unbind(struct device
*dev
, struct device
*master
, void *data
)
1091 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1092 struct drm_encoder
*encoder
= &dp
->encoder
;
1093 struct drm_connector
*connector
= &dp
->connector
;
1095 cancel_work_sync(&dp
->event_work
);
1096 cdn_dp_encoder_disable(encoder
);
1097 encoder
->funcs
->destroy(encoder
);
1098 connector
->funcs
->destroy(connector
);
1100 pm_runtime_disable(dev
);
1102 release_firmware(dp
->fw
);
1107 static const struct component_ops cdn_dp_component_ops
= {
1108 .bind
= cdn_dp_bind
,
1109 .unbind
= cdn_dp_unbind
,
1112 int cdn_dp_suspend(struct device
*dev
)
1114 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1117 mutex_lock(&dp
->lock
);
1119 ret
= cdn_dp_disable(dp
);
1120 dp
->suspended
= true;
1121 mutex_unlock(&dp
->lock
);
1126 int cdn_dp_resume(struct device
*dev
)
1128 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1130 mutex_lock(&dp
->lock
);
1131 dp
->suspended
= false;
1133 schedule_work(&dp
->event_work
);
1134 mutex_unlock(&dp
->lock
);
1139 static int cdn_dp_probe(struct platform_device
*pdev
)
1141 struct device
*dev
= &pdev
->dev
;
1142 const struct of_device_id
*match
;
1143 struct cdn_dp_data
*dp_data
;
1144 struct cdn_dp_port
*port
;
1145 struct cdn_dp_device
*dp
;
1146 struct extcon_dev
*extcon
;
1150 dp
= devm_kzalloc(dev
, sizeof(*dp
), GFP_KERNEL
);
1155 match
= of_match_node(cdn_dp_dt_ids
, pdev
->dev
.of_node
);
1156 dp_data
= (struct cdn_dp_data
*)match
->data
;
1158 for (i
= 0; i
< dp_data
->max_phy
; i
++) {
1159 extcon
= extcon_get_edev_by_phandle(dev
, i
);
1160 phy
= devm_of_phy_get_by_index(dev
, dev
->of_node
, i
);
1162 if (PTR_ERR(extcon
) == -EPROBE_DEFER
||
1163 PTR_ERR(phy
) == -EPROBE_DEFER
)
1164 return -EPROBE_DEFER
;
1166 if (IS_ERR(extcon
) || IS_ERR(phy
))
1169 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
1173 port
->extcon
= extcon
;
1177 dp
->port
[dp
->ports
++] = port
;
1181 DRM_DEV_ERROR(dev
, "missing extcon or phy\n");
1185 mutex_init(&dp
->lock
);
1186 dev_set_drvdata(dev
, dp
);
1188 cdn_dp_audio_codec_init(dp
, dev
);
1190 return component_add(dev
, &cdn_dp_component_ops
);
1193 static int cdn_dp_remove(struct platform_device
*pdev
)
1195 struct cdn_dp_device
*dp
= platform_get_drvdata(pdev
);
1197 platform_device_unregister(dp
->audio_pdev
);
1198 cdn_dp_suspend(dp
->dev
);
1199 component_del(&pdev
->dev
, &cdn_dp_component_ops
);
1204 static void cdn_dp_shutdown(struct platform_device
*pdev
)
1206 struct cdn_dp_device
*dp
= platform_get_drvdata(pdev
);
1208 cdn_dp_suspend(dp
->dev
);
1211 static const struct dev_pm_ops cdn_dp_pm_ops
= {
1212 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend
,
1216 struct platform_driver cdn_dp_driver
= {
1217 .probe
= cdn_dp_probe
,
1218 .remove
= cdn_dp_remove
,
1219 .shutdown
= cdn_dp_shutdown
,
1222 .owner
= THIS_MODULE
,
1223 .of_match_table
= of_match_ptr(cdn_dp_dt_ids
),
1224 .pm
= &cdn_dp_pm_ops
,