2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author: Chris Zhong <zyw@rock-chips.com>
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 DPTX_HPD_SEL (3 << 12)
47 #define DPTX_HPD_DEL (2 << 12)
48 #define DPTX_HPD_SEL_MASK (3 << 28)
50 #define CDN_FW_TIMEOUT_MS (64 * 1000)
51 #define CDN_DPCD_TIMEOUT_MS 5000
52 #define CDN_DP_FIRMWARE "rockchip/dptx.bin"
58 struct cdn_dp_data rk3399_cdn_dp
= {
62 static const struct of_device_id cdn_dp_dt_ids
[] = {
63 { .compatible
= "rockchip,rk3399-cdn-dp",
64 .data
= (void *)&rk3399_cdn_dp
},
68 MODULE_DEVICE_TABLE(of
, cdn_dp_dt_ids
);
70 static int cdn_dp_grf_write(struct cdn_dp_device
*dp
,
71 unsigned int reg
, unsigned int val
)
75 ret
= clk_prepare_enable(dp
->grf_clk
);
77 DRM_DEV_ERROR(dp
->dev
, "Failed to prepare_enable grf clock\n");
81 ret
= regmap_write(dp
->grf
, reg
, val
);
83 DRM_DEV_ERROR(dp
->dev
, "Could not write to GRF: %d\n", ret
);
87 clk_disable_unprepare(dp
->grf_clk
);
92 static int cdn_dp_clk_enable(struct cdn_dp_device
*dp
)
97 ret
= clk_prepare_enable(dp
->pclk
);
99 DRM_DEV_ERROR(dp
->dev
, "cannot enable dp pclk %d\n", ret
);
103 ret
= clk_prepare_enable(dp
->core_clk
);
105 DRM_DEV_ERROR(dp
->dev
, "cannot enable core_clk %d\n", ret
);
109 ret
= pm_runtime_get_sync(dp
->dev
);
111 DRM_DEV_ERROR(dp
->dev
, "cannot get pm runtime %d\n", ret
);
112 goto err_pm_runtime_get
;
115 reset_control_assert(dp
->core_rst
);
116 reset_control_assert(dp
->dptx_rst
);
117 reset_control_assert(dp
->apb_rst
);
118 reset_control_deassert(dp
->core_rst
);
119 reset_control_deassert(dp
->dptx_rst
);
120 reset_control_deassert(dp
->apb_rst
);
122 rate
= clk_get_rate(dp
->core_clk
);
124 DRM_DEV_ERROR(dp
->dev
, "get clk rate failed\n");
129 cdn_dp_set_fw_clk(dp
, rate
);
130 cdn_dp_clock_reset(dp
);
135 pm_runtime_put(dp
->dev
);
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 .detect
= cdn_dp_connector_detect
,
256 .destroy
= cdn_dp_connector_destroy
,
257 .fill_modes
= drm_helper_probe_single_connector_modes
,
258 .reset
= drm_atomic_helper_connector_reset
,
259 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
260 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
263 static int cdn_dp_connector_get_modes(struct drm_connector
*connector
)
265 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
269 mutex_lock(&dp
->lock
);
272 DRM_DEV_DEBUG_KMS(dp
->dev
, "got edid: width[%d] x height[%d]\n",
273 edid
->width_cm
, edid
->height_cm
);
275 dp
->sink_has_audio
= drm_detect_monitor_audio(edid
);
276 ret
= drm_add_edid_modes(connector
, edid
);
278 drm_connector_update_edid_property(connector
,
281 mutex_unlock(&dp
->lock
);
286 static int cdn_dp_connector_mode_valid(struct drm_connector
*connector
,
287 struct drm_display_mode
*mode
)
289 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
290 struct drm_display_info
*display_info
= &dp
->connector
.display_info
;
291 u32 requested
, actual
, rate
, sink_max
, source_max
= 0;
294 /* If DP is disconnected, every mode is invalid */
298 switch (display_info
->bpc
) {
310 requested
= mode
->clock
* bpc
* 3 / 1000;
312 source_max
= dp
->lanes
;
313 sink_max
= drm_dp_max_lane_count(dp
->dpcd
);
314 lanes
= min(source_max
, sink_max
);
316 source_max
= drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE
);
317 sink_max
= drm_dp_max_link_rate(dp
->dpcd
);
318 rate
= min(source_max
, sink_max
);
320 actual
= rate
* lanes
/ 100;
322 /* efficiency is about 0.8 */
323 actual
= actual
* 8 / 10;
325 if (requested
> actual
) {
326 DRM_DEV_DEBUG_KMS(dp
->dev
,
327 "requested=%d, actual=%d, clock=%d\n",
328 requested
, actual
, mode
->clock
);
329 return MODE_CLOCK_HIGH
;
335 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs
= {
336 .get_modes
= cdn_dp_connector_get_modes
,
337 .mode_valid
= cdn_dp_connector_mode_valid
,
340 static int cdn_dp_firmware_init(struct cdn_dp_device
*dp
)
343 const u32
*iram_data
, *dram_data
;
344 const struct firmware
*fw
= dp
->fw
;
345 const struct cdn_firmware_header
*hdr
;
347 hdr
= (struct cdn_firmware_header
*)fw
->data
;
348 if (fw
->size
!= le32_to_cpu(hdr
->size_bytes
)) {
349 DRM_DEV_ERROR(dp
->dev
, "firmware is invalid\n");
353 iram_data
= (const u32
*)(fw
->data
+ hdr
->header_size
);
354 dram_data
= (const u32
*)(fw
->data
+ hdr
->header_size
+ hdr
->iram_size
);
356 ret
= cdn_dp_load_firmware(dp
, iram_data
, hdr
->iram_size
,
357 dram_data
, hdr
->dram_size
);
361 ret
= cdn_dp_set_firmware_active(dp
, true);
363 DRM_DEV_ERROR(dp
->dev
, "active ucpu failed: %d\n", ret
);
367 return cdn_dp_event_config(dp
);
370 static int cdn_dp_get_sink_capability(struct cdn_dp_device
*dp
)
374 if (!cdn_dp_check_sink_connection(dp
))
377 ret
= cdn_dp_dpcd_read(dp
, DP_DPCD_REV
, dp
->dpcd
,
378 DP_RECEIVER_CAP_SIZE
);
380 DRM_DEV_ERROR(dp
->dev
, "Failed to get caps %d\n", ret
);
385 dp
->edid
= drm_do_get_edid(&dp
->connector
,
386 cdn_dp_get_edid_block
, dp
);
390 static int cdn_dp_enable_phy(struct cdn_dp_device
*dp
, struct cdn_dp_port
*port
)
392 union extcon_property_value property
;
395 if (!port
->phy_enabled
) {
396 ret
= phy_power_on(port
->phy
);
398 DRM_DEV_ERROR(dp
->dev
, "phy power on failed: %d\n",
402 port
->phy_enabled
= true;
405 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
406 DPTX_HPD_SEL_MASK
| DPTX_HPD_SEL
);
408 DRM_DEV_ERROR(dp
->dev
, "Failed to write HPD_SEL %d\n", ret
);
412 ret
= cdn_dp_get_hpd_status(dp
);
415 DRM_DEV_ERROR(dp
->dev
, "hpd does not exist\n");
419 ret
= extcon_get_property(port
->extcon
, EXTCON_DISP_DP
,
420 EXTCON_PROP_USB_TYPEC_POLARITY
, &property
);
422 DRM_DEV_ERROR(dp
->dev
, "get property failed\n");
426 port
->lanes
= cdn_dp_get_port_lanes(port
);
427 ret
= cdn_dp_set_host_cap(dp
, port
->lanes
, property
.intval
);
429 DRM_DEV_ERROR(dp
->dev
, "set host capabilities failed: %d\n",
434 dp
->active_port
= port
->id
;
438 if (phy_power_off(port
->phy
))
439 DRM_DEV_ERROR(dp
->dev
, "phy power off failed: %d", ret
);
441 port
->phy_enabled
= false;
444 cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
445 DPTX_HPD_SEL_MASK
| DPTX_HPD_DEL
);
449 static int cdn_dp_disable_phy(struct cdn_dp_device
*dp
,
450 struct cdn_dp_port
*port
)
454 if (port
->phy_enabled
) {
455 ret
= phy_power_off(port
->phy
);
457 DRM_DEV_ERROR(dp
->dev
, "phy power off failed: %d", ret
);
462 port
->phy_enabled
= false;
464 dp
->active_port
= -1;
468 static int cdn_dp_disable(struct cdn_dp_device
*dp
)
475 for (i
= 0; i
< dp
->ports
; i
++)
476 cdn_dp_disable_phy(dp
, dp
->port
[i
]);
478 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
479 DPTX_HPD_SEL_MASK
| DPTX_HPD_DEL
);
481 DRM_DEV_ERROR(dp
->dev
, "Failed to clear hpd sel %d\n",
486 cdn_dp_set_firmware_active(dp
, false);
487 cdn_dp_clk_disable(dp
);
490 dp
->link
.num_lanes
= 0;
491 if (!dp
->connected
) {
499 static int cdn_dp_enable(struct cdn_dp_device
*dp
)
502 struct cdn_dp_port
*port
;
504 port
= cdn_dp_connected_port(dp
);
506 DRM_DEV_ERROR(dp
->dev
,
507 "Can't enable without connection\n");
514 ret
= cdn_dp_clk_enable(dp
);
518 ret
= cdn_dp_firmware_init(dp
);
520 DRM_DEV_ERROR(dp
->dev
, "firmware init failed: %d", ret
);
521 goto err_clk_disable
;
524 /* only enable the port that connected with downstream device */
525 for (i
= port
->id
; i
< dp
->ports
; i
++) {
527 lanes
= cdn_dp_get_port_lanes(port
);
529 ret
= cdn_dp_enable_phy(dp
, port
);
533 ret
= cdn_dp_get_sink_capability(dp
);
535 cdn_dp_disable_phy(dp
, port
);
538 dp
->lanes
= port
->lanes
;
545 cdn_dp_clk_disable(dp
);
549 static void cdn_dp_encoder_mode_set(struct drm_encoder
*encoder
,
550 struct drm_display_mode
*mode
,
551 struct drm_display_mode
*adjusted
)
553 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
554 struct drm_display_info
*display_info
= &dp
->connector
.display_info
;
555 struct video_info
*video
= &dp
->video_info
;
557 switch (display_info
->bpc
) {
559 video
->color_depth
= 10;
562 video
->color_depth
= 6;
565 video
->color_depth
= 8;
569 video
->color_fmt
= PXL_RGB
;
570 video
->v_sync_polarity
= !!(mode
->flags
& DRM_MODE_FLAG_NVSYNC
);
571 video
->h_sync_polarity
= !!(mode
->flags
& DRM_MODE_FLAG_NHSYNC
);
573 memcpy(&dp
->mode
, adjusted
, sizeof(*mode
));
576 static bool cdn_dp_check_link_status(struct cdn_dp_device
*dp
)
578 u8 link_status
[DP_LINK_STATUS_SIZE
];
579 struct cdn_dp_port
*port
= cdn_dp_connected_port(dp
);
580 u8 sink_lanes
= drm_dp_max_lane_count(dp
->dpcd
);
582 if (!port
|| !dp
->link
.rate
|| !dp
->link
.num_lanes
)
585 if (cdn_dp_dpcd_read(dp
, DP_LANE0_1_STATUS
, link_status
,
586 DP_LINK_STATUS_SIZE
)) {
587 DRM_ERROR("Failed to get link status\n");
591 /* if link training is requested we should perform it always */
592 return drm_dp_channel_eq_ok(link_status
, min(port
->lanes
, sink_lanes
));
595 static void cdn_dp_encoder_enable(struct drm_encoder
*encoder
)
597 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
600 ret
= drm_of_encoder_active_endpoint_id(dp
->dev
->of_node
, encoder
);
602 DRM_DEV_ERROR(dp
->dev
, "Could not get vop id, %d", ret
);
606 DRM_DEV_DEBUG_KMS(dp
->dev
, "vop %s output to cdn-dp\n",
607 (ret
) ? "LIT" : "BIG");
609 val
= DP_SEL_VOP_LIT
| (DP_SEL_VOP_LIT
<< 16);
611 val
= DP_SEL_VOP_LIT
<< 16;
613 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON9
, val
);
617 mutex_lock(&dp
->lock
);
619 ret
= cdn_dp_enable(dp
);
621 DRM_DEV_ERROR(dp
->dev
, "Failed to enable encoder %d\n",
625 if (!cdn_dp_check_link_status(dp
)) {
626 ret
= cdn_dp_train_link(dp
);
628 DRM_DEV_ERROR(dp
->dev
, "Failed link train %d\n", ret
);
633 ret
= cdn_dp_set_video_status(dp
, CONTROL_VIDEO_IDLE
);
635 DRM_DEV_ERROR(dp
->dev
, "Failed to idle video %d\n", ret
);
639 ret
= cdn_dp_config_video(dp
);
641 DRM_DEV_ERROR(dp
->dev
, "Failed to config video %d\n", ret
);
645 ret
= cdn_dp_set_video_status(dp
, CONTROL_VIDEO_VALID
);
647 DRM_DEV_ERROR(dp
->dev
, "Failed to valid video %d\n", ret
);
651 mutex_unlock(&dp
->lock
);
654 static void cdn_dp_encoder_disable(struct drm_encoder
*encoder
)
656 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
659 mutex_lock(&dp
->lock
);
661 ret
= cdn_dp_disable(dp
);
663 DRM_DEV_ERROR(dp
->dev
, "Failed to disable encoder %d\n",
667 mutex_unlock(&dp
->lock
);
670 * In the following 2 cases, we need to run the event_work to re-enable
672 * 1. If there is not just one port device is connected, and remove one
673 * device from a port, the DP will be disabled here, at this case,
674 * run the event_work to re-open DP for the other port.
675 * 2. If re-training or re-config failed, the DP will be disabled here.
676 * run the event_work to re-connect it.
678 if (!dp
->connected
&& cdn_dp_connected_port(dp
))
679 schedule_work(&dp
->event_work
);
682 static int cdn_dp_encoder_atomic_check(struct drm_encoder
*encoder
,
683 struct drm_crtc_state
*crtc_state
,
684 struct drm_connector_state
*conn_state
)
686 struct rockchip_crtc_state
*s
= to_rockchip_crtc_state(crtc_state
);
688 s
->output_mode
= ROCKCHIP_OUT_MODE_AAAA
;
689 s
->output_type
= DRM_MODE_CONNECTOR_DisplayPort
;
694 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs
= {
695 .mode_set
= cdn_dp_encoder_mode_set
,
696 .enable
= cdn_dp_encoder_enable
,
697 .disable
= cdn_dp_encoder_disable
,
698 .atomic_check
= cdn_dp_encoder_atomic_check
,
701 static const struct drm_encoder_funcs cdn_dp_encoder_funcs
= {
702 .destroy
= drm_encoder_cleanup
,
705 static int cdn_dp_parse_dt(struct cdn_dp_device
*dp
)
707 struct device
*dev
= dp
->dev
;
708 struct device_node
*np
= dev
->of_node
;
709 struct platform_device
*pdev
= to_platform_device(dev
);
710 struct resource
*res
;
712 dp
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
713 if (IS_ERR(dp
->grf
)) {
714 DRM_DEV_ERROR(dev
, "cdn-dp needs rockchip,grf property\n");
715 return PTR_ERR(dp
->grf
);
718 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
719 dp
->regs
= devm_ioremap_resource(dev
, res
);
720 if (IS_ERR(dp
->regs
)) {
721 DRM_DEV_ERROR(dev
, "ioremap reg failed\n");
722 return PTR_ERR(dp
->regs
);
725 dp
->core_clk
= devm_clk_get(dev
, "core-clk");
726 if (IS_ERR(dp
->core_clk
)) {
727 DRM_DEV_ERROR(dev
, "cannot get core_clk_dp\n");
728 return PTR_ERR(dp
->core_clk
);
731 dp
->pclk
= devm_clk_get(dev
, "pclk");
732 if (IS_ERR(dp
->pclk
)) {
733 DRM_DEV_ERROR(dev
, "cannot get pclk\n");
734 return PTR_ERR(dp
->pclk
);
737 dp
->spdif_clk
= devm_clk_get(dev
, "spdif");
738 if (IS_ERR(dp
->spdif_clk
)) {
739 DRM_DEV_ERROR(dev
, "cannot get spdif_clk\n");
740 return PTR_ERR(dp
->spdif_clk
);
743 dp
->grf_clk
= devm_clk_get(dev
, "grf");
744 if (IS_ERR(dp
->grf_clk
)) {
745 DRM_DEV_ERROR(dev
, "cannot get grf clk\n");
746 return PTR_ERR(dp
->grf_clk
);
749 dp
->spdif_rst
= devm_reset_control_get(dev
, "spdif");
750 if (IS_ERR(dp
->spdif_rst
)) {
751 DRM_DEV_ERROR(dev
, "no spdif reset control found\n");
752 return PTR_ERR(dp
->spdif_rst
);
755 dp
->dptx_rst
= devm_reset_control_get(dev
, "dptx");
756 if (IS_ERR(dp
->dptx_rst
)) {
757 DRM_DEV_ERROR(dev
, "no uphy reset control found\n");
758 return PTR_ERR(dp
->dptx_rst
);
761 dp
->core_rst
= devm_reset_control_get(dev
, "core");
762 if (IS_ERR(dp
->core_rst
)) {
763 DRM_DEV_ERROR(dev
, "no core reset control found\n");
764 return PTR_ERR(dp
->core_rst
);
767 dp
->apb_rst
= devm_reset_control_get(dev
, "apb");
768 if (IS_ERR(dp
->apb_rst
)) {
769 DRM_DEV_ERROR(dev
, "no apb reset control found\n");
770 return PTR_ERR(dp
->apb_rst
);
776 static int cdn_dp_audio_hw_params(struct device
*dev
, void *data
,
777 struct hdmi_codec_daifmt
*daifmt
,
778 struct hdmi_codec_params
*params
)
780 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
781 struct audio_info audio
= {
782 .sample_width
= params
->sample_width
,
783 .sample_rate
= params
->sample_rate
,
784 .channels
= params
->channels
,
788 mutex_lock(&dp
->lock
);
794 switch (daifmt
->fmt
) {
796 audio
.format
= AFMT_I2S
;
799 audio
.format
= AFMT_SPDIF
;
802 DRM_DEV_ERROR(dev
, "Invalid format %d\n", daifmt
->fmt
);
807 ret
= cdn_dp_audio_config(dp
, &audio
);
809 dp
->audio_info
= audio
;
812 mutex_unlock(&dp
->lock
);
816 static void cdn_dp_audio_shutdown(struct device
*dev
, void *data
)
818 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
821 mutex_lock(&dp
->lock
);
825 ret
= cdn_dp_audio_stop(dp
, &dp
->audio_info
);
827 dp
->audio_info
.format
= AFMT_UNUSED
;
829 mutex_unlock(&dp
->lock
);
832 static int cdn_dp_audio_digital_mute(struct device
*dev
, void *data
,
835 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
838 mutex_lock(&dp
->lock
);
844 ret
= cdn_dp_audio_mute(dp
, enable
);
847 mutex_unlock(&dp
->lock
);
851 static int cdn_dp_audio_get_eld(struct device
*dev
, void *data
,
854 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
856 memcpy(buf
, dp
->connector
.eld
, min(sizeof(dp
->connector
.eld
), len
));
861 static const struct hdmi_codec_ops audio_codec_ops
= {
862 .hw_params
= cdn_dp_audio_hw_params
,
863 .audio_shutdown
= cdn_dp_audio_shutdown
,
864 .digital_mute
= cdn_dp_audio_digital_mute
,
865 .get_eld
= cdn_dp_audio_get_eld
,
868 static int cdn_dp_audio_codec_init(struct cdn_dp_device
*dp
,
871 struct hdmi_codec_pdata codec_data
= {
874 .ops
= &audio_codec_ops
,
875 .max_i2s_channels
= 8,
878 dp
->audio_pdev
= platform_device_register_data(
879 dev
, HDMI_CODEC_DRV_NAME
, PLATFORM_DEVID_AUTO
,
880 &codec_data
, sizeof(codec_data
));
882 return PTR_ERR_OR_ZERO(dp
->audio_pdev
);
885 static int cdn_dp_request_firmware(struct cdn_dp_device
*dp
)
888 unsigned long timeout
= jiffies
+ msecs_to_jiffies(CDN_FW_TIMEOUT_MS
);
889 unsigned long sleep
= 1000;
891 WARN_ON(!mutex_is_locked(&dp
->lock
));
896 /* Drop the lock before getting the firmware to avoid blocking boot */
897 mutex_unlock(&dp
->lock
);
899 while (time_before(jiffies
, timeout
)) {
900 ret
= request_firmware(&dp
->fw
, CDN_DP_FIRMWARE
, dp
->dev
);
901 if (ret
== -ENOENT
) {
906 DRM_DEV_ERROR(dp
->dev
,
907 "failed to request firmware: %d\n", ret
);
911 dp
->fw_loaded
= true;
916 DRM_DEV_ERROR(dp
->dev
, "Timed out trying to load firmware\n");
919 mutex_lock(&dp
->lock
);
923 static void cdn_dp_pd_event_work(struct work_struct
*work
)
925 struct cdn_dp_device
*dp
= container_of(work
, struct cdn_dp_device
,
927 struct drm_connector
*connector
= &dp
->connector
;
928 enum drm_connector_status old_status
;
932 mutex_lock(&dp
->lock
);
937 ret
= cdn_dp_request_firmware(dp
);
941 dp
->connected
= true;
943 /* Not connected, notify userspace to disable the block */
944 if (!cdn_dp_connected_port(dp
)) {
945 DRM_DEV_INFO(dp
->dev
, "Not connected. Disabling cdn\n");
946 dp
->connected
= false;
948 /* Connected but not enabled, enable the block */
949 } else if (!dp
->active
) {
950 DRM_DEV_INFO(dp
->dev
, "Connected, not enabled. Enabling cdn\n");
951 ret
= cdn_dp_enable(dp
);
953 DRM_DEV_ERROR(dp
->dev
, "Enable dp failed %d\n", ret
);
954 dp
->connected
= false;
957 /* Enabled and connected to a dongle without a sink, notify userspace */
958 } else if (!cdn_dp_check_sink_connection(dp
)) {
959 DRM_DEV_INFO(dp
->dev
, "Connected without sink. Assert hpd\n");
960 dp
->connected
= false;
962 /* Enabled and connected with a sink, re-train if requested */
963 } else if (!cdn_dp_check_link_status(dp
)) {
964 unsigned int rate
= dp
->link
.rate
;
965 unsigned int lanes
= dp
->link
.num_lanes
;
966 struct drm_display_mode
*mode
= &dp
->mode
;
968 DRM_DEV_INFO(dp
->dev
, "Connected with sink. Re-train link\n");
969 ret
= cdn_dp_train_link(dp
);
971 dp
->connected
= false;
972 DRM_DEV_ERROR(dp
->dev
, "Train link failed %d\n", ret
);
976 /* If training result is changed, update the video config */
978 (rate
!= dp
->link
.rate
|| lanes
!= dp
->link
.num_lanes
)) {
979 ret
= cdn_dp_config_video(dp
);
981 dp
->connected
= false;
982 DRM_DEV_ERROR(dp
->dev
,
983 "Failed to config video %d\n",
990 mutex_unlock(&dp
->lock
);
992 old_status
= connector
->status
;
993 connector
->status
= connector
->funcs
->detect(connector
, false);
994 if (old_status
!= connector
->status
)
995 drm_kms_helper_hotplug_event(dp
->drm_dev
);
998 static int cdn_dp_pd_event(struct notifier_block
*nb
,
999 unsigned long event
, void *priv
)
1001 struct cdn_dp_port
*port
= container_of(nb
, struct cdn_dp_port
,
1003 struct cdn_dp_device
*dp
= port
->dp
;
1006 * It would be nice to be able to just do the work inline right here.
1007 * However, we need to make a bunch of calls that might sleep in order
1008 * to turn on the block/phy, so use a worker instead.
1010 schedule_work(&dp
->event_work
);
1015 static int cdn_dp_bind(struct device
*dev
, struct device
*master
, void *data
)
1017 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1018 struct drm_encoder
*encoder
;
1019 struct drm_connector
*connector
;
1020 struct cdn_dp_port
*port
;
1021 struct drm_device
*drm_dev
= data
;
1024 ret
= cdn_dp_parse_dt(dp
);
1028 dp
->drm_dev
= drm_dev
;
1029 dp
->connected
= false;
1031 dp
->active_port
= -1;
1032 dp
->fw_loaded
= false;
1034 INIT_WORK(&dp
->event_work
, cdn_dp_pd_event_work
);
1036 encoder
= &dp
->encoder
;
1038 encoder
->possible_crtcs
= drm_of_find_possible_crtcs(drm_dev
,
1040 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder
->possible_crtcs
);
1042 ret
= drm_encoder_init(drm_dev
, encoder
, &cdn_dp_encoder_funcs
,
1043 DRM_MODE_ENCODER_TMDS
, NULL
);
1045 DRM_ERROR("failed to initialize encoder with drm\n");
1049 drm_encoder_helper_add(encoder
, &cdn_dp_encoder_helper_funcs
);
1051 connector
= &dp
->connector
;
1052 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
1053 connector
->dpms
= DRM_MODE_DPMS_OFF
;
1055 ret
= drm_connector_init(drm_dev
, connector
,
1056 &cdn_dp_atomic_connector_funcs
,
1057 DRM_MODE_CONNECTOR_DisplayPort
);
1059 DRM_ERROR("failed to initialize connector with drm\n");
1060 goto err_free_encoder
;
1063 drm_connector_helper_add(connector
, &cdn_dp_connector_helper_funcs
);
1065 ret
= drm_connector_attach_encoder(connector
, encoder
);
1067 DRM_ERROR("failed to attach connector and encoder\n");
1068 goto err_free_connector
;
1071 for (i
= 0; i
< dp
->ports
; i
++) {
1074 port
->event_nb
.notifier_call
= cdn_dp_pd_event
;
1075 ret
= devm_extcon_register_notifier(dp
->dev
, port
->extcon
,
1080 "register EXTCON_DISP_DP notifier err\n");
1081 goto err_free_connector
;
1085 pm_runtime_enable(dev
);
1087 schedule_work(&dp
->event_work
);
1092 drm_connector_cleanup(connector
);
1094 drm_encoder_cleanup(encoder
);
1098 static void cdn_dp_unbind(struct device
*dev
, struct device
*master
, void *data
)
1100 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1101 struct drm_encoder
*encoder
= &dp
->encoder
;
1102 struct drm_connector
*connector
= &dp
->connector
;
1104 cancel_work_sync(&dp
->event_work
);
1105 cdn_dp_encoder_disable(encoder
);
1106 encoder
->funcs
->destroy(encoder
);
1107 connector
->funcs
->destroy(connector
);
1109 pm_runtime_disable(dev
);
1111 release_firmware(dp
->fw
);
1116 static const struct component_ops cdn_dp_component_ops
= {
1117 .bind
= cdn_dp_bind
,
1118 .unbind
= cdn_dp_unbind
,
1121 int cdn_dp_suspend(struct device
*dev
)
1123 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1126 mutex_lock(&dp
->lock
);
1128 ret
= cdn_dp_disable(dp
);
1129 dp
->suspended
= true;
1130 mutex_unlock(&dp
->lock
);
1135 int cdn_dp_resume(struct device
*dev
)
1137 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1139 mutex_lock(&dp
->lock
);
1140 dp
->suspended
= false;
1142 schedule_work(&dp
->event_work
);
1143 mutex_unlock(&dp
->lock
);
1148 static int cdn_dp_probe(struct platform_device
*pdev
)
1150 struct device
*dev
= &pdev
->dev
;
1151 const struct of_device_id
*match
;
1152 struct cdn_dp_data
*dp_data
;
1153 struct cdn_dp_port
*port
;
1154 struct cdn_dp_device
*dp
;
1155 struct extcon_dev
*extcon
;
1159 dp
= devm_kzalloc(dev
, sizeof(*dp
), GFP_KERNEL
);
1164 match
= of_match_node(cdn_dp_dt_ids
, pdev
->dev
.of_node
);
1165 dp_data
= (struct cdn_dp_data
*)match
->data
;
1167 for (i
= 0; i
< dp_data
->max_phy
; i
++) {
1168 extcon
= extcon_get_edev_by_phandle(dev
, i
);
1169 phy
= devm_of_phy_get_by_index(dev
, dev
->of_node
, i
);
1171 if (PTR_ERR(extcon
) == -EPROBE_DEFER
||
1172 PTR_ERR(phy
) == -EPROBE_DEFER
)
1173 return -EPROBE_DEFER
;
1175 if (IS_ERR(extcon
) || IS_ERR(phy
))
1178 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
1182 port
->extcon
= extcon
;
1186 dp
->port
[dp
->ports
++] = port
;
1190 DRM_DEV_ERROR(dev
, "missing extcon or phy\n");
1194 mutex_init(&dp
->lock
);
1195 dev_set_drvdata(dev
, dp
);
1197 cdn_dp_audio_codec_init(dp
, dev
);
1199 return component_add(dev
, &cdn_dp_component_ops
);
1202 static int cdn_dp_remove(struct platform_device
*pdev
)
1204 struct cdn_dp_device
*dp
= platform_get_drvdata(pdev
);
1206 platform_device_unregister(dp
->audio_pdev
);
1207 cdn_dp_suspend(dp
->dev
);
1208 component_del(&pdev
->dev
, &cdn_dp_component_ops
);
1213 static void cdn_dp_shutdown(struct platform_device
*pdev
)
1215 struct cdn_dp_device
*dp
= platform_get_drvdata(pdev
);
1217 cdn_dp_suspend(dp
->dev
);
1220 static const struct dev_pm_ops cdn_dp_pm_ops
= {
1221 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend
,
1225 struct platform_driver cdn_dp_driver
= {
1226 .probe
= cdn_dp_probe
,
1227 .remove
= cdn_dp_remove
,
1228 .shutdown
= cdn_dp_shutdown
,
1231 .owner
= THIS_MODULE
,
1232 .of_match_table
= of_match_ptr(cdn_dp_dt_ids
),
1233 .pm
= &cdn_dp_pm_ops
,