1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Rockchip SoC DP (Display Port) interface driver.
5 * Copyright (C) Fuzhou Rockchip Electronics Co., Ltd.
6 * Author: Andy Yan <andy.yan@rock-chips.com>
7 * Yakir Yang <ykk@rock-chips.com>
8 * Jeff Chen <jeff.chen@rock-chips.com>
11 #include <linux/component.h>
12 #include <linux/mfd/syscon.h>
14 #include <linux/of_graph.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 #include <linux/reset.h>
19 #include <linux/clk.h>
21 #include <video/of_videomode.h>
22 #include <video/videomode.h>
24 #include <drm/display/drm_dp_helper.h>
25 #include <drm/drm_atomic.h>
26 #include <drm/drm_atomic_helper.h>
27 #include <drm/bridge/analogix_dp.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_panel.h>
30 #include <drm/drm_probe_helper.h>
31 #include <drm/drm_simple_kms_helper.h>
33 #include "rockchip_drm_drv.h"
35 #define RK3288_GRF_SOC_CON6 0x25c
36 #define RK3288_EDP_LCDC_SEL BIT(5)
37 #define RK3399_GRF_SOC_CON20 0x6250
38 #define RK3399_EDP_LCDC_SEL BIT(5)
40 #define HIWORD_UPDATE(val, mask) (val | (mask) << 16)
42 #define PSR_WAIT_LINE_FLAG_TIMEOUT_MS 100
45 * struct rockchip_dp_chip_data - splite the grf setting of kind of chips
46 * @lcdsel_grf_reg: grf register offset of lcdc select
47 * @lcdsel_big: reg value of selecting vop big for eDP
48 * @lcdsel_lit: reg value of selecting vop little for eDP
49 * @chip_type: specific chip type
51 struct rockchip_dp_chip_data
{
58 struct rockchip_dp_device
{
59 struct drm_device
*drm_dev
;
61 struct rockchip_encoder encoder
;
62 struct drm_display_mode mode
;
67 struct reset_control
*rst
;
69 const struct rockchip_dp_chip_data
*data
;
71 struct analogix_dp_device
*adp
;
72 struct analogix_dp_plat_data plat_data
;
75 static struct rockchip_dp_device
*encoder_to_dp(struct drm_encoder
*encoder
)
77 struct rockchip_encoder
*rkencoder
= to_rockchip_encoder(encoder
);
79 return container_of(rkencoder
, struct rockchip_dp_device
, encoder
);
82 static struct rockchip_dp_device
*pdata_encoder_to_dp(struct analogix_dp_plat_data
*plat_data
)
84 return container_of(plat_data
, struct rockchip_dp_device
, plat_data
);
87 static int rockchip_dp_pre_init(struct rockchip_dp_device
*dp
)
89 reset_control_assert(dp
->rst
);
91 reset_control_deassert(dp
->rst
);
96 static int rockchip_dp_poweron(struct analogix_dp_plat_data
*plat_data
)
98 struct rockchip_dp_device
*dp
= pdata_encoder_to_dp(plat_data
);
101 ret
= clk_prepare_enable(dp
->pclk
);
103 DRM_DEV_ERROR(dp
->dev
, "failed to enable pclk %d\n", ret
);
107 ret
= rockchip_dp_pre_init(dp
);
109 DRM_DEV_ERROR(dp
->dev
, "failed to dp pre init %d\n", ret
);
110 clk_disable_unprepare(dp
->pclk
);
117 static int rockchip_dp_powerdown(struct analogix_dp_plat_data
*plat_data
)
119 struct rockchip_dp_device
*dp
= pdata_encoder_to_dp(plat_data
);
121 clk_disable_unprepare(dp
->pclk
);
126 static int rockchip_dp_get_modes(struct analogix_dp_plat_data
*plat_data
,
127 struct drm_connector
*connector
)
129 struct drm_display_info
*di
= &connector
->display_info
;
130 /* VOP couldn't output YUV video format for eDP rightly */
131 u32 mask
= DRM_COLOR_FORMAT_YCBCR444
| DRM_COLOR_FORMAT_YCBCR422
;
133 if ((di
->color_formats
& mask
)) {
134 DRM_DEBUG_KMS("Swapping display color format from YUV to RGB\n");
135 di
->color_formats
&= ~mask
;
136 di
->color_formats
|= DRM_COLOR_FORMAT_RGB444
;
144 rockchip_dp_drm_encoder_mode_fixup(struct drm_encoder
*encoder
,
145 const struct drm_display_mode
*mode
,
146 struct drm_display_mode
*adjusted_mode
)
152 static void rockchip_dp_drm_encoder_mode_set(struct drm_encoder
*encoder
,
153 struct drm_display_mode
*mode
,
154 struct drm_display_mode
*adjusted
)
160 struct drm_crtc
*rockchip_dp_drm_get_new_crtc(struct drm_encoder
*encoder
,
161 struct drm_atomic_state
*state
)
163 struct drm_connector
*connector
;
164 struct drm_connector_state
*conn_state
;
166 connector
= drm_atomic_get_new_connector_for_encoder(state
, encoder
);
170 conn_state
= drm_atomic_get_new_connector_state(state
, connector
);
174 return conn_state
->crtc
;
177 static void rockchip_dp_drm_encoder_enable(struct drm_encoder
*encoder
,
178 struct drm_atomic_state
*state
)
180 struct rockchip_dp_device
*dp
= encoder_to_dp(encoder
);
181 struct drm_crtc
*crtc
;
182 struct drm_crtc_state
*old_crtc_state
;
186 crtc
= rockchip_dp_drm_get_new_crtc(encoder
, state
);
190 old_crtc_state
= drm_atomic_get_old_crtc_state(state
, crtc
);
191 /* Coming back from self refresh, nothing to do */
192 if (old_crtc_state
&& old_crtc_state
->self_refresh_active
)
195 ret
= drm_of_encoder_active_endpoint_id(dp
->dev
->of_node
, encoder
);
200 val
= dp
->data
->lcdsel_lit
;
202 val
= dp
->data
->lcdsel_big
;
204 DRM_DEV_DEBUG(dp
->dev
, "vop %s output to dp\n", (ret
) ? "LIT" : "BIG");
206 ret
= clk_prepare_enable(dp
->grfclk
);
208 DRM_DEV_ERROR(dp
->dev
, "failed to enable grfclk %d\n", ret
);
212 ret
= regmap_write(dp
->grf
, dp
->data
->lcdsel_grf_reg
, val
);
214 DRM_DEV_ERROR(dp
->dev
, "Could not write to GRF: %d\n", ret
);
216 clk_disable_unprepare(dp
->grfclk
);
219 static void rockchip_dp_drm_encoder_disable(struct drm_encoder
*encoder
,
220 struct drm_atomic_state
*state
)
222 struct rockchip_dp_device
*dp
= encoder_to_dp(encoder
);
223 struct drm_crtc
*crtc
;
224 struct drm_crtc_state
*new_crtc_state
= NULL
;
227 crtc
= rockchip_dp_drm_get_new_crtc(encoder
, state
);
228 /* No crtc means we're doing a full shutdown */
232 new_crtc_state
= drm_atomic_get_new_crtc_state(state
, crtc
);
233 /* If we're not entering self-refresh, no need to wait for vact */
234 if (!new_crtc_state
|| !new_crtc_state
->self_refresh_active
)
237 ret
= rockchip_drm_wait_vact_end(crtc
, PSR_WAIT_LINE_FLAG_TIMEOUT_MS
);
239 DRM_DEV_ERROR(dp
->dev
, "line flag irq timed out\n");
243 rockchip_dp_drm_encoder_atomic_check(struct drm_encoder
*encoder
,
244 struct drm_crtc_state
*crtc_state
,
245 struct drm_connector_state
*conn_state
)
247 struct rockchip_crtc_state
*s
= to_rockchip_crtc_state(crtc_state
);
248 struct drm_display_info
*di
= &conn_state
->connector
->display_info
;
251 * The hardware IC designed that VOP must output the RGB10 video
252 * format to eDP controller, and if eDP panel only support RGB8,
253 * then eDP controller should cut down the video data, not via VOP
254 * controller, that's why we need to hardcode the VOP output mode
258 s
->output_mode
= ROCKCHIP_OUT_MODE_AAAA
;
259 s
->output_type
= DRM_MODE_CONNECTOR_eDP
;
260 s
->output_bpc
= di
->bpc
;
265 static const struct drm_encoder_helper_funcs rockchip_dp_encoder_helper_funcs
= {
266 .mode_fixup
= rockchip_dp_drm_encoder_mode_fixup
,
267 .mode_set
= rockchip_dp_drm_encoder_mode_set
,
268 .atomic_enable
= rockchip_dp_drm_encoder_enable
,
269 .atomic_disable
= rockchip_dp_drm_encoder_disable
,
270 .atomic_check
= rockchip_dp_drm_encoder_atomic_check
,
273 static int rockchip_dp_of_probe(struct rockchip_dp_device
*dp
)
275 struct device
*dev
= dp
->dev
;
276 struct device_node
*np
= dev
->of_node
;
278 dp
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
279 if (IS_ERR(dp
->grf
)) {
280 DRM_DEV_ERROR(dev
, "failed to get rockchip,grf property\n");
281 return PTR_ERR(dp
->grf
);
284 dp
->grfclk
= devm_clk_get(dev
, "grf");
285 if (PTR_ERR(dp
->grfclk
) == -ENOENT
) {
287 } else if (PTR_ERR(dp
->grfclk
) == -EPROBE_DEFER
) {
288 return -EPROBE_DEFER
;
289 } else if (IS_ERR(dp
->grfclk
)) {
290 DRM_DEV_ERROR(dev
, "failed to get grf clock\n");
291 return PTR_ERR(dp
->grfclk
);
294 dp
->pclk
= devm_clk_get(dev
, "pclk");
295 if (IS_ERR(dp
->pclk
)) {
296 DRM_DEV_ERROR(dev
, "failed to get pclk property\n");
297 return PTR_ERR(dp
->pclk
);
300 dp
->rst
= devm_reset_control_get(dev
, "dp");
301 if (IS_ERR(dp
->rst
)) {
302 DRM_DEV_ERROR(dev
, "failed to get dp reset control\n");
303 return PTR_ERR(dp
->rst
);
309 static int rockchip_dp_drm_create_encoder(struct rockchip_dp_device
*dp
)
311 struct drm_encoder
*encoder
= &dp
->encoder
.encoder
;
312 struct drm_device
*drm_dev
= dp
->drm_dev
;
313 struct device
*dev
= dp
->dev
;
316 encoder
->possible_crtcs
= drm_of_find_possible_crtcs(drm_dev
,
318 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder
->possible_crtcs
);
320 ret
= drm_simple_encoder_init(drm_dev
, encoder
,
321 DRM_MODE_ENCODER_TMDS
);
323 DRM_ERROR("failed to initialize encoder with drm\n");
327 drm_encoder_helper_add(encoder
, &rockchip_dp_encoder_helper_funcs
);
332 static int rockchip_dp_bind(struct device
*dev
, struct device
*master
,
335 struct rockchip_dp_device
*dp
= dev_get_drvdata(dev
);
336 struct drm_device
*drm_dev
= data
;
339 dp
->drm_dev
= drm_dev
;
341 ret
= rockchip_dp_drm_create_encoder(dp
);
343 DRM_ERROR("failed to create drm encoder\n");
347 rockchip_drm_encoder_set_crtc_endpoint_id(&dp
->encoder
,
350 dp
->plat_data
.encoder
= &dp
->encoder
.encoder
;
352 ret
= analogix_dp_bind(dp
->adp
, drm_dev
);
354 goto err_cleanup_encoder
;
358 dp
->encoder
.encoder
.funcs
->destroy(&dp
->encoder
.encoder
);
362 static void rockchip_dp_unbind(struct device
*dev
, struct device
*master
,
365 struct rockchip_dp_device
*dp
= dev_get_drvdata(dev
);
367 analogix_dp_unbind(dp
->adp
);
368 dp
->encoder
.encoder
.funcs
->destroy(&dp
->encoder
.encoder
);
371 static const struct component_ops rockchip_dp_component_ops
= {
372 .bind
= rockchip_dp_bind
,
373 .unbind
= rockchip_dp_unbind
,
376 static int rockchip_dp_probe(struct platform_device
*pdev
)
378 struct device
*dev
= &pdev
->dev
;
379 const struct rockchip_dp_chip_data
*dp_data
;
380 struct drm_panel
*panel
= NULL
;
381 struct rockchip_dp_device
*dp
;
384 dp_data
= of_device_get_match_data(dev
);
388 ret
= drm_of_find_panel_or_bridge(dev
->of_node
, 1, 0, &panel
, NULL
);
392 dp
= devm_kzalloc(dev
, sizeof(*dp
), GFP_KERNEL
);
397 dp
->adp
= ERR_PTR(-ENODEV
);
399 dp
->plat_data
.panel
= panel
;
400 dp
->plat_data
.dev_type
= dp
->data
->chip_type
;
401 dp
->plat_data
.power_on
= rockchip_dp_poweron
;
402 dp
->plat_data
.power_off
= rockchip_dp_powerdown
;
403 dp
->plat_data
.get_modes
= rockchip_dp_get_modes
;
405 ret
= rockchip_dp_of_probe(dp
);
409 platform_set_drvdata(pdev
, dp
);
411 dp
->adp
= analogix_dp_probe(dev
, &dp
->plat_data
);
413 return PTR_ERR(dp
->adp
);
415 ret
= component_add(dev
, &rockchip_dp_component_ops
);
422 static void rockchip_dp_remove(struct platform_device
*pdev
)
424 component_del(&pdev
->dev
, &rockchip_dp_component_ops
);
427 static int rockchip_dp_suspend(struct device
*dev
)
429 struct rockchip_dp_device
*dp
= dev_get_drvdata(dev
);
434 return analogix_dp_suspend(dp
->adp
);
437 static int rockchip_dp_resume(struct device
*dev
)
439 struct rockchip_dp_device
*dp
= dev_get_drvdata(dev
);
444 return analogix_dp_resume(dp
->adp
);
447 static DEFINE_RUNTIME_DEV_PM_OPS(rockchip_dp_pm_ops
, rockchip_dp_suspend
,
448 rockchip_dp_resume
, NULL
);
450 static const struct rockchip_dp_chip_data rk3399_edp
= {
451 .lcdsel_grf_reg
= RK3399_GRF_SOC_CON20
,
452 .lcdsel_big
= HIWORD_UPDATE(0, RK3399_EDP_LCDC_SEL
),
453 .lcdsel_lit
= HIWORD_UPDATE(RK3399_EDP_LCDC_SEL
, RK3399_EDP_LCDC_SEL
),
454 .chip_type
= RK3399_EDP
,
457 static const struct rockchip_dp_chip_data rk3288_dp
= {
458 .lcdsel_grf_reg
= RK3288_GRF_SOC_CON6
,
459 .lcdsel_big
= HIWORD_UPDATE(0, RK3288_EDP_LCDC_SEL
),
460 .lcdsel_lit
= HIWORD_UPDATE(RK3288_EDP_LCDC_SEL
, RK3288_EDP_LCDC_SEL
),
461 .chip_type
= RK3288_DP
,
464 static const struct of_device_id rockchip_dp_dt_ids
[] = {
465 {.compatible
= "rockchip,rk3288-dp", .data
= &rk3288_dp
},
466 {.compatible
= "rockchip,rk3399-edp", .data
= &rk3399_edp
},
469 MODULE_DEVICE_TABLE(of
, rockchip_dp_dt_ids
);
471 struct platform_driver rockchip_dp_driver
= {
472 .probe
= rockchip_dp_probe
,
473 .remove
= rockchip_dp_remove
,
475 .name
= "rockchip-dp",
476 .pm
= pm_ptr(&rockchip_dp_pm_ops
),
477 .of_match_table
= rockchip_dp_dt_ids
,