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 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\n");
131 cdn_dp_set_fw_clk(dp
, rate
);
132 cdn_dp_clock_reset(dp
);
137 pm_runtime_put(dp
->dev
);
139 clk_disable_unprepare(dp
->core_clk
);
141 clk_disable_unprepare(dp
->pclk
);
146 static void cdn_dp_clk_disable(struct cdn_dp_device
*dp
)
148 pm_runtime_put_sync(dp
->dev
);
149 clk_disable_unprepare(dp
->pclk
);
150 clk_disable_unprepare(dp
->core_clk
);
153 static int cdn_dp_get_port_lanes(struct cdn_dp_port
*port
)
155 struct extcon_dev
*edev
= port
->extcon
;
156 union extcon_property_value property
;
160 dptx
= extcon_get_state(edev
, EXTCON_DISP_DP
);
162 extcon_get_property(edev
, EXTCON_DISP_DP
,
163 EXTCON_PROP_USB_SS
, &property
);
175 static int cdn_dp_get_sink_count(struct cdn_dp_device
*dp
, u8
*sink_count
)
181 ret
= cdn_dp_dpcd_read(dp
, DP_SINK_COUNT
, &value
, 1);
185 *sink_count
= DP_GET_SINK_COUNT(value
);
189 static struct cdn_dp_port
*cdn_dp_connected_port(struct cdn_dp_device
*dp
)
191 struct cdn_dp_port
*port
;
194 for (i
= 0; i
< dp
->ports
; i
++) {
196 lanes
= cdn_dp_get_port_lanes(port
);
203 static bool cdn_dp_check_sink_connection(struct cdn_dp_device
*dp
)
205 unsigned long timeout
= jiffies
+ msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS
);
206 struct cdn_dp_port
*port
;
209 if (dp
->active_port
< 0 || dp
->active_port
>= dp
->ports
) {
210 DRM_DEV_ERROR(dp
->dev
, "active_port is wrong!\n");
214 port
= dp
->port
[dp
->active_port
];
217 * Attempt to read sink count, retry in case the sink may not be ready.
219 * Sinks are *supposed* to come up within 1ms from an off state, but
220 * some docks need more time to power up.
222 while (time_before(jiffies
, timeout
)) {
223 if (!extcon_get_state(port
->extcon
, EXTCON_DISP_DP
))
226 if (!cdn_dp_get_sink_count(dp
, &sink_count
))
227 return sink_count
? true : false;
229 usleep_range(5000, 10000);
232 DRM_DEV_ERROR(dp
->dev
, "Get sink capability timed out\n");
236 static enum drm_connector_status
237 cdn_dp_connector_detect(struct drm_connector
*connector
, bool force
)
239 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
240 enum drm_connector_status status
= connector_status_disconnected
;
242 mutex_lock(&dp
->lock
);
244 status
= connector_status_connected
;
245 mutex_unlock(&dp
->lock
);
250 static void cdn_dp_connector_destroy(struct drm_connector
*connector
)
252 drm_connector_unregister(connector
);
253 drm_connector_cleanup(connector
);
256 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs
= {
257 .dpms
= drm_atomic_helper_connector_dpms
,
258 .detect
= cdn_dp_connector_detect
,
259 .destroy
= cdn_dp_connector_destroy
,
260 .fill_modes
= drm_helper_probe_single_connector_modes
,
261 .reset
= drm_atomic_helper_connector_reset
,
262 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
263 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
266 static int cdn_dp_connector_get_modes(struct drm_connector
*connector
)
268 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
272 mutex_lock(&dp
->lock
);
275 DRM_DEV_DEBUG_KMS(dp
->dev
, "got edid: width[%d] x height[%d]\n",
276 edid
->width_cm
, edid
->height_cm
);
278 dp
->sink_has_audio
= drm_detect_monitor_audio(edid
);
279 ret
= drm_add_edid_modes(connector
, edid
);
281 drm_mode_connector_update_edid_property(connector
,
283 drm_edid_to_eld(connector
, edid
);
286 mutex_unlock(&dp
->lock
);
291 static struct drm_encoder
*
292 cdn_dp_connector_best_encoder(struct drm_connector
*connector
)
294 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
299 static int cdn_dp_connector_mode_valid(struct drm_connector
*connector
,
300 struct drm_display_mode
*mode
)
302 struct cdn_dp_device
*dp
= connector_to_dp(connector
);
303 struct drm_display_info
*display_info
= &dp
->connector
.display_info
;
304 u32 requested
, actual
, rate
, sink_max
, source_max
= 0;
307 /* If DP is disconnected, every mode is invalid */
311 switch (display_info
->bpc
) {
323 requested
= mode
->clock
* bpc
* 3 / 1000;
325 source_max
= dp
->lanes
;
326 sink_max
= drm_dp_max_lane_count(dp
->dpcd
);
327 lanes
= min(source_max
, sink_max
);
329 source_max
= drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE
);
330 sink_max
= drm_dp_max_link_rate(dp
->dpcd
);
331 rate
= min(source_max
, sink_max
);
333 actual
= rate
* lanes
/ 100;
335 /* efficiency is about 0.8 */
336 actual
= actual
* 8 / 10;
338 if (requested
> actual
) {
339 DRM_DEV_DEBUG_KMS(dp
->dev
,
340 "requested=%d, actual=%d, clock=%d\n",
341 requested
, actual
, mode
->clock
);
342 return MODE_CLOCK_HIGH
;
348 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs
= {
349 .get_modes
= cdn_dp_connector_get_modes
,
350 .best_encoder
= cdn_dp_connector_best_encoder
,
351 .mode_valid
= cdn_dp_connector_mode_valid
,
354 static int cdn_dp_firmware_init(struct cdn_dp_device
*dp
)
357 const u32
*iram_data
, *dram_data
;
358 const struct firmware
*fw
= dp
->fw
;
359 const struct cdn_firmware_header
*hdr
;
361 hdr
= (struct cdn_firmware_header
*)fw
->data
;
362 if (fw
->size
!= le32_to_cpu(hdr
->size_bytes
)) {
363 DRM_DEV_ERROR(dp
->dev
, "firmware is invalid\n");
367 iram_data
= (const u32
*)(fw
->data
+ hdr
->header_size
);
368 dram_data
= (const u32
*)(fw
->data
+ hdr
->header_size
+ hdr
->iram_size
);
370 ret
= cdn_dp_load_firmware(dp
, iram_data
, hdr
->iram_size
,
371 dram_data
, hdr
->dram_size
);
375 ret
= cdn_dp_set_firmware_active(dp
, true);
377 DRM_DEV_ERROR(dp
->dev
, "active ucpu failed: %d\n", ret
);
381 return cdn_dp_event_config(dp
);
384 static int cdn_dp_get_sink_capability(struct cdn_dp_device
*dp
)
388 if (!cdn_dp_check_sink_connection(dp
))
391 ret
= cdn_dp_dpcd_read(dp
, DP_DPCD_REV
, dp
->dpcd
,
392 DP_RECEIVER_CAP_SIZE
);
394 DRM_DEV_ERROR(dp
->dev
, "Failed to get caps %d\n", ret
);
399 dp
->edid
= drm_do_get_edid(&dp
->connector
,
400 cdn_dp_get_edid_block
, dp
);
404 static int cdn_dp_enable_phy(struct cdn_dp_device
*dp
, struct cdn_dp_port
*port
)
406 union extcon_property_value property
;
409 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
410 (port
->id
<< UPHY_SEL_BIT
) | UPHY_SEL_MASK
);
414 if (!port
->phy_enabled
) {
415 ret
= phy_power_on(port
->phy
);
417 DRM_DEV_ERROR(dp
->dev
, "phy power on failed: %d\n",
421 port
->phy_enabled
= true;
424 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
425 DPTX_HPD_SEL_MASK
| DPTX_HPD_SEL
);
427 DRM_DEV_ERROR(dp
->dev
, "Failed to write HPD_SEL %d\n", ret
);
431 ret
= cdn_dp_get_hpd_status(dp
);
434 DRM_DEV_ERROR(dp
->dev
, "hpd does not exist\n");
438 ret
= extcon_get_property(port
->extcon
, EXTCON_DISP_DP
,
439 EXTCON_PROP_USB_TYPEC_POLARITY
, &property
);
441 DRM_DEV_ERROR(dp
->dev
, "get property failed\n");
445 port
->lanes
= cdn_dp_get_port_lanes(port
);
446 ret
= cdn_dp_set_host_cap(dp
, port
->lanes
, property
.intval
);
448 DRM_DEV_ERROR(dp
->dev
, "set host capabilities failed: %d\n",
453 dp
->active_port
= port
->id
;
457 if (phy_power_off(port
->phy
))
458 DRM_DEV_ERROR(dp
->dev
, "phy power off failed: %d", ret
);
460 port
->phy_enabled
= false;
463 cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
464 DPTX_HPD_SEL_MASK
| DPTX_HPD_DEL
);
468 static int cdn_dp_disable_phy(struct cdn_dp_device
*dp
,
469 struct cdn_dp_port
*port
)
473 if (port
->phy_enabled
) {
474 ret
= phy_power_off(port
->phy
);
476 DRM_DEV_ERROR(dp
->dev
, "phy power off failed: %d", ret
);
481 port
->phy_enabled
= false;
483 dp
->active_port
= -1;
487 static int cdn_dp_disable(struct cdn_dp_device
*dp
)
494 for (i
= 0; i
< dp
->ports
; i
++)
495 cdn_dp_disable_phy(dp
, dp
->port
[i
]);
497 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON26
,
498 DPTX_HPD_SEL_MASK
| DPTX_HPD_DEL
);
500 DRM_DEV_ERROR(dp
->dev
, "Failed to clear hpd sel %d\n",
505 cdn_dp_set_firmware_active(dp
, false);
506 cdn_dp_clk_disable(dp
);
509 dp
->link
.num_lanes
= 0;
510 if (!dp
->connected
) {
518 static int cdn_dp_enable(struct cdn_dp_device
*dp
)
521 struct cdn_dp_port
*port
;
523 port
= cdn_dp_connected_port(dp
);
525 DRM_DEV_ERROR(dp
->dev
,
526 "Can't enable without connection\n");
533 ret
= cdn_dp_clk_enable(dp
);
537 ret
= cdn_dp_firmware_init(dp
);
539 DRM_DEV_ERROR(dp
->dev
, "firmware init failed: %d", ret
);
540 goto err_clk_disable
;
543 /* only enable the port that connected with downstream device */
544 for (i
= port
->id
; i
< dp
->ports
; i
++) {
546 lanes
= cdn_dp_get_port_lanes(port
);
548 ret
= cdn_dp_enable_phy(dp
, port
);
552 ret
= cdn_dp_get_sink_capability(dp
);
554 cdn_dp_disable_phy(dp
, port
);
557 dp
->lanes
= port
->lanes
;
564 cdn_dp_clk_disable(dp
);
568 static void cdn_dp_encoder_mode_set(struct drm_encoder
*encoder
,
569 struct drm_display_mode
*mode
,
570 struct drm_display_mode
*adjusted
)
572 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
573 struct drm_display_info
*display_info
= &dp
->connector
.display_info
;
574 struct video_info
*video
= &dp
->video_info
;
576 switch (display_info
->bpc
) {
578 video
->color_depth
= 10;
581 video
->color_depth
= 6;
584 video
->color_depth
= 8;
588 video
->color_fmt
= PXL_RGB
;
589 video
->v_sync_polarity
= !!(mode
->flags
& DRM_MODE_FLAG_NVSYNC
);
590 video
->h_sync_polarity
= !!(mode
->flags
& DRM_MODE_FLAG_NHSYNC
);
592 memcpy(&dp
->mode
, adjusted
, sizeof(*mode
));
595 static bool cdn_dp_check_link_status(struct cdn_dp_device
*dp
)
597 u8 link_status
[DP_LINK_STATUS_SIZE
];
598 struct cdn_dp_port
*port
= cdn_dp_connected_port(dp
);
599 u8 sink_lanes
= drm_dp_max_lane_count(dp
->dpcd
);
601 if (!port
|| !dp
->link
.rate
|| !dp
->link
.num_lanes
)
604 if (cdn_dp_dpcd_read(dp
, DP_LANE0_1_STATUS
, link_status
,
605 DP_LINK_STATUS_SIZE
)) {
606 DRM_ERROR("Failed to get link status\n");
610 /* if link training is requested we should perform it always */
611 return drm_dp_channel_eq_ok(link_status
, min(port
->lanes
, sink_lanes
));
614 static void cdn_dp_encoder_enable(struct drm_encoder
*encoder
)
616 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
618 struct rockchip_crtc_state
*state
;
620 ret
= drm_of_encoder_active_endpoint_id(dp
->dev
->of_node
, encoder
);
622 DRM_DEV_ERROR(dp
->dev
, "Could not get vop id, %d", ret
);
626 DRM_DEV_DEBUG_KMS(dp
->dev
, "vop %s output to cdn-dp\n",
627 (ret
) ? "LIT" : "BIG");
628 state
= to_rockchip_crtc_state(encoder
->crtc
->state
);
630 val
= DP_SEL_VOP_LIT
| (DP_SEL_VOP_LIT
<< 16);
631 state
->output_mode
= ROCKCHIP_OUT_MODE_P888
;
633 val
= DP_SEL_VOP_LIT
<< 16;
634 state
->output_mode
= ROCKCHIP_OUT_MODE_AAAA
;
637 ret
= cdn_dp_grf_write(dp
, GRF_SOC_CON9
, val
);
641 mutex_lock(&dp
->lock
);
643 ret
= cdn_dp_enable(dp
);
645 DRM_DEV_ERROR(dp
->dev
, "Failed to enable encoder %d\n",
649 if (!cdn_dp_check_link_status(dp
)) {
650 ret
= cdn_dp_train_link(dp
);
652 DRM_DEV_ERROR(dp
->dev
, "Failed link train %d\n", ret
);
657 ret
= cdn_dp_set_video_status(dp
, CONTROL_VIDEO_IDLE
);
659 DRM_DEV_ERROR(dp
->dev
, "Failed to idle video %d\n", ret
);
663 ret
= cdn_dp_config_video(dp
);
665 DRM_DEV_ERROR(dp
->dev
, "Failed to config video %d\n", ret
);
669 ret
= cdn_dp_set_video_status(dp
, CONTROL_VIDEO_VALID
);
671 DRM_DEV_ERROR(dp
->dev
, "Failed to valid video %d\n", ret
);
675 mutex_unlock(&dp
->lock
);
678 static void cdn_dp_encoder_disable(struct drm_encoder
*encoder
)
680 struct cdn_dp_device
*dp
= encoder_to_dp(encoder
);
683 mutex_lock(&dp
->lock
);
685 ret
= cdn_dp_disable(dp
);
687 DRM_DEV_ERROR(dp
->dev
, "Failed to disable encoder %d\n",
691 mutex_unlock(&dp
->lock
);
694 * In the following 2 cases, we need to run the event_work to re-enable
696 * 1. If there is not just one port device is connected, and remove one
697 * device from a port, the DP will be disabled here, at this case,
698 * run the event_work to re-open DP for the other port.
699 * 2. If re-training or re-config failed, the DP will be disabled here.
700 * run the event_work to re-connect it.
702 if (!dp
->connected
&& cdn_dp_connected_port(dp
))
703 schedule_work(&dp
->event_work
);
706 static int cdn_dp_encoder_atomic_check(struct drm_encoder
*encoder
,
707 struct drm_crtc_state
*crtc_state
,
708 struct drm_connector_state
*conn_state
)
710 struct rockchip_crtc_state
*s
= to_rockchip_crtc_state(crtc_state
);
712 s
->output_mode
= ROCKCHIP_OUT_MODE_AAAA
;
713 s
->output_type
= DRM_MODE_CONNECTOR_DisplayPort
;
718 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs
= {
719 .mode_set
= cdn_dp_encoder_mode_set
,
720 .enable
= cdn_dp_encoder_enable
,
721 .disable
= cdn_dp_encoder_disable
,
722 .atomic_check
= cdn_dp_encoder_atomic_check
,
725 static const struct drm_encoder_funcs cdn_dp_encoder_funcs
= {
726 .destroy
= drm_encoder_cleanup
,
729 static int cdn_dp_parse_dt(struct cdn_dp_device
*dp
)
731 struct device
*dev
= dp
->dev
;
732 struct device_node
*np
= dev
->of_node
;
733 struct platform_device
*pdev
= to_platform_device(dev
);
734 struct resource
*res
;
736 dp
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
737 if (IS_ERR(dp
->grf
)) {
738 DRM_DEV_ERROR(dev
, "cdn-dp needs rockchip,grf property\n");
739 return PTR_ERR(dp
->grf
);
742 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
743 dp
->regs
= devm_ioremap_resource(dev
, res
);
744 if (IS_ERR(dp
->regs
)) {
745 DRM_DEV_ERROR(dev
, "ioremap reg failed\n");
746 return PTR_ERR(dp
->regs
);
749 dp
->core_clk
= devm_clk_get(dev
, "core-clk");
750 if (IS_ERR(dp
->core_clk
)) {
751 DRM_DEV_ERROR(dev
, "cannot get core_clk_dp\n");
752 return PTR_ERR(dp
->core_clk
);
755 dp
->pclk
= devm_clk_get(dev
, "pclk");
756 if (IS_ERR(dp
->pclk
)) {
757 DRM_DEV_ERROR(dev
, "cannot get pclk\n");
758 return PTR_ERR(dp
->pclk
);
761 dp
->spdif_clk
= devm_clk_get(dev
, "spdif");
762 if (IS_ERR(dp
->spdif_clk
)) {
763 DRM_DEV_ERROR(dev
, "cannot get spdif_clk\n");
764 return PTR_ERR(dp
->spdif_clk
);
767 dp
->grf_clk
= devm_clk_get(dev
, "grf");
768 if (IS_ERR(dp
->grf_clk
)) {
769 DRM_DEV_ERROR(dev
, "cannot get grf clk\n");
770 return PTR_ERR(dp
->grf_clk
);
773 dp
->spdif_rst
= devm_reset_control_get(dev
, "spdif");
774 if (IS_ERR(dp
->spdif_rst
)) {
775 DRM_DEV_ERROR(dev
, "no spdif reset control found\n");
776 return PTR_ERR(dp
->spdif_rst
);
779 dp
->dptx_rst
= devm_reset_control_get(dev
, "dptx");
780 if (IS_ERR(dp
->dptx_rst
)) {
781 DRM_DEV_ERROR(dev
, "no uphy reset control found\n");
782 return PTR_ERR(dp
->dptx_rst
);
785 dp
->core_rst
= devm_reset_control_get(dev
, "core");
786 if (IS_ERR(dp
->core_rst
)) {
787 DRM_DEV_ERROR(dev
, "no core reset control found\n");
788 return PTR_ERR(dp
->core_rst
);
791 dp
->apb_rst
= devm_reset_control_get(dev
, "apb");
792 if (IS_ERR(dp
->apb_rst
)) {
793 DRM_DEV_ERROR(dev
, "no apb reset control found\n");
794 return PTR_ERR(dp
->apb_rst
);
800 static int cdn_dp_audio_hw_params(struct device
*dev
, void *data
,
801 struct hdmi_codec_daifmt
*daifmt
,
802 struct hdmi_codec_params
*params
)
804 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
805 struct audio_info audio
= {
806 .sample_width
= params
->sample_width
,
807 .sample_rate
= params
->sample_rate
,
808 .channels
= params
->channels
,
812 mutex_lock(&dp
->lock
);
818 switch (daifmt
->fmt
) {
820 audio
.format
= AFMT_I2S
;
823 audio
.format
= AFMT_SPDIF
;
826 DRM_DEV_ERROR(dev
, "Invalid format %d\n", daifmt
->fmt
);
831 ret
= cdn_dp_audio_config(dp
, &audio
);
833 dp
->audio_info
= audio
;
836 mutex_unlock(&dp
->lock
);
840 static void cdn_dp_audio_shutdown(struct device
*dev
, void *data
)
842 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
845 mutex_lock(&dp
->lock
);
849 ret
= cdn_dp_audio_stop(dp
, &dp
->audio_info
);
851 dp
->audio_info
.format
= AFMT_UNUSED
;
853 mutex_unlock(&dp
->lock
);
856 static int cdn_dp_audio_digital_mute(struct device
*dev
, void *data
,
859 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
862 mutex_lock(&dp
->lock
);
868 ret
= cdn_dp_audio_mute(dp
, enable
);
871 mutex_unlock(&dp
->lock
);
875 static int cdn_dp_audio_get_eld(struct device
*dev
, void *data
,
878 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
880 memcpy(buf
, dp
->connector
.eld
, min(sizeof(dp
->connector
.eld
), len
));
885 static const struct hdmi_codec_ops audio_codec_ops
= {
886 .hw_params
= cdn_dp_audio_hw_params
,
887 .audio_shutdown
= cdn_dp_audio_shutdown
,
888 .digital_mute
= cdn_dp_audio_digital_mute
,
889 .get_eld
= cdn_dp_audio_get_eld
,
892 static int cdn_dp_audio_codec_init(struct cdn_dp_device
*dp
,
895 struct hdmi_codec_pdata codec_data
= {
898 .ops
= &audio_codec_ops
,
899 .max_i2s_channels
= 8,
902 dp
->audio_pdev
= platform_device_register_data(
903 dev
, HDMI_CODEC_DRV_NAME
, PLATFORM_DEVID_AUTO
,
904 &codec_data
, sizeof(codec_data
));
906 return PTR_ERR_OR_ZERO(dp
->audio_pdev
);
909 static int cdn_dp_request_firmware(struct cdn_dp_device
*dp
)
912 unsigned long timeout
= jiffies
+ msecs_to_jiffies(CDN_FW_TIMEOUT_MS
);
913 unsigned long sleep
= 1000;
915 WARN_ON(!mutex_is_locked(&dp
->lock
));
920 /* Drop the lock before getting the firmware to avoid blocking boot */
921 mutex_unlock(&dp
->lock
);
923 while (time_before(jiffies
, timeout
)) {
924 ret
= request_firmware(&dp
->fw
, CDN_DP_FIRMWARE
, dp
->dev
);
925 if (ret
== -ENOENT
) {
930 DRM_DEV_ERROR(dp
->dev
,
931 "failed to request firmware: %d\n", ret
);
935 dp
->fw_loaded
= true;
940 DRM_DEV_ERROR(dp
->dev
, "Timed out trying to load firmware\n");
943 mutex_lock(&dp
->lock
);
947 static void cdn_dp_pd_event_work(struct work_struct
*work
)
949 struct cdn_dp_device
*dp
= container_of(work
, struct cdn_dp_device
,
951 struct drm_connector
*connector
= &dp
->connector
;
952 enum drm_connector_status old_status
;
956 mutex_lock(&dp
->lock
);
961 ret
= cdn_dp_request_firmware(dp
);
965 dp
->connected
= true;
967 /* Not connected, notify userspace to disable the block */
968 if (!cdn_dp_connected_port(dp
)) {
969 DRM_DEV_INFO(dp
->dev
, "Not connected. Disabling cdn\n");
970 dp
->connected
= false;
972 /* Connected but not enabled, enable the block */
973 } else if (!dp
->active
) {
974 DRM_DEV_INFO(dp
->dev
, "Connected, not enabled. Enabling cdn\n");
975 ret
= cdn_dp_enable(dp
);
977 DRM_DEV_ERROR(dp
->dev
, "Enable dp failed %d\n", ret
);
978 dp
->connected
= false;
981 /* Enabled and connected to a dongle without a sink, notify userspace */
982 } else if (!cdn_dp_check_sink_connection(dp
)) {
983 DRM_DEV_INFO(dp
->dev
, "Connected without sink. Assert hpd\n");
984 dp
->connected
= false;
986 /* Enabled and connected with a sink, re-train if requested */
987 } else if (!cdn_dp_check_link_status(dp
)) {
988 unsigned int rate
= dp
->link
.rate
;
989 unsigned int lanes
= dp
->link
.num_lanes
;
990 struct drm_display_mode
*mode
= &dp
->mode
;
992 DRM_DEV_INFO(dp
->dev
, "Connected with sink. Re-train link\n");
993 ret
= cdn_dp_train_link(dp
);
995 dp
->connected
= false;
996 DRM_DEV_ERROR(dp
->dev
, "Train link failed %d\n", ret
);
1000 /* If training result is changed, update the video config */
1002 (rate
!= dp
->link
.rate
|| lanes
!= dp
->link
.num_lanes
)) {
1003 ret
= cdn_dp_config_video(dp
);
1005 dp
->connected
= false;
1006 DRM_DEV_ERROR(dp
->dev
,
1007 "Failed to config video %d\n",
1014 mutex_unlock(&dp
->lock
);
1016 old_status
= connector
->status
;
1017 connector
->status
= connector
->funcs
->detect(connector
, false);
1018 if (old_status
!= connector
->status
)
1019 drm_kms_helper_hotplug_event(dp
->drm_dev
);
1022 static int cdn_dp_pd_event(struct notifier_block
*nb
,
1023 unsigned long event
, void *priv
)
1025 struct cdn_dp_port
*port
= container_of(nb
, struct cdn_dp_port
,
1027 struct cdn_dp_device
*dp
= port
->dp
;
1030 * It would be nice to be able to just do the work inline right here.
1031 * However, we need to make a bunch of calls that might sleep in order
1032 * to turn on the block/phy, so use a worker instead.
1034 schedule_work(&dp
->event_work
);
1039 static int cdn_dp_bind(struct device
*dev
, struct device
*master
, void *data
)
1041 struct cdn_dp_device
*dp
= dev_get_drvdata(dev
);
1042 struct drm_encoder
*encoder
;
1043 struct drm_connector
*connector
;
1044 struct cdn_dp_port
*port
;
1045 struct drm_device
*drm_dev
= data
;
1048 ret
= cdn_dp_parse_dt(dp
);
1052 dp
->drm_dev
= drm_dev
;
1053 dp
->connected
= false;
1055 dp
->active_port
= -1;
1056 dp
->fw_loaded
= false;
1058 INIT_WORK(&dp
->event_work
, cdn_dp_pd_event_work
);
1060 encoder
= &dp
->encoder
;
1062 encoder
->possible_crtcs
= drm_of_find_possible_crtcs(drm_dev
,
1064 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder
->possible_crtcs
);
1066 ret
= drm_encoder_init(drm_dev
, encoder
, &cdn_dp_encoder_funcs
,
1067 DRM_MODE_ENCODER_TMDS
, NULL
);
1069 DRM_ERROR("failed to initialize encoder with drm\n");
1073 drm_encoder_helper_add(encoder
, &cdn_dp_encoder_helper_funcs
);
1075 connector
= &dp
->connector
;
1076 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
1077 connector
->dpms
= DRM_MODE_DPMS_OFF
;
1079 ret
= drm_connector_init(drm_dev
, connector
,
1080 &cdn_dp_atomic_connector_funcs
,
1081 DRM_MODE_CONNECTOR_DisplayPort
);
1083 DRM_ERROR("failed to initialize connector with drm\n");
1084 goto err_free_encoder
;
1087 drm_connector_helper_add(connector
, &cdn_dp_connector_helper_funcs
);
1089 ret
= drm_mode_connector_attach_encoder(connector
, encoder
);
1091 DRM_ERROR("failed to attach connector and encoder\n");
1092 goto err_free_connector
;
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 cdn_dp_encoder_disable(encoder
);
1130 encoder
->funcs
->destroy(encoder
);
1131 connector
->funcs
->destroy(connector
);
1133 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 cdn_dp_audio_codec_init(dp
, dev
);
1223 return component_add(dev
, &cdn_dp_component_ops
);
1226 static int cdn_dp_remove(struct platform_device
*pdev
)
1228 struct cdn_dp_device
*dp
= platform_get_drvdata(pdev
);
1230 platform_device_unregister(dp
->audio_pdev
);
1231 cdn_dp_suspend(dp
->dev
);
1232 component_del(&pdev
->dev
, &cdn_dp_component_ops
);
1237 static void cdn_dp_shutdown(struct platform_device
*pdev
)
1239 struct cdn_dp_device
*dp
= platform_get_drvdata(pdev
);
1241 cdn_dp_suspend(dp
->dev
);
1244 static const struct dev_pm_ops cdn_dp_pm_ops
= {
1245 SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend
,
1249 struct platform_driver cdn_dp_driver
= {
1250 .probe
= cdn_dp_probe
,
1251 .remove
= cdn_dp_remove
,
1252 .shutdown
= cdn_dp_shutdown
,
1255 .owner
= THIS_MODULE
,
1256 .of_match_table
= of_match_ptr(cdn_dp_dt_ids
),
1257 .pm
= &cdn_dp_pm_ops
,