2 * Rockchip SoC DP (Display Port) interface driver.
4 * Copyright (C) Fuzhou Rockchip Electronics Co., Ltd.
5 * Author: Andy Yan <andy.yan@rock-chips.com>
6 * Yakir Yang <ykk@rock-chips.com>
7 * Jeff Chen <jeff.chen@rock-chips.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
15 #include <linux/component.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/of_device.h>
18 #include <linux/of_graph.h>
19 #include <linux/regmap.h>
20 #include <linux/reset.h>
21 #include <linux/clk.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_dp_helper.h>
26 #include <drm/drm_of.h>
27 #include <drm/drm_panel.h>
29 #include <video/of_videomode.h>
30 #include <video/videomode.h>
32 #include <drm/bridge/analogix_dp.h>
34 #include "rockchip_drm_drv.h"
35 #include "rockchip_drm_psr.h"
36 #include "rockchip_drm_vop.h"
38 #define RK3288_GRF_SOC_CON6 0x25c
39 #define RK3288_EDP_LCDC_SEL BIT(5)
40 #define RK3399_GRF_SOC_CON20 0x6250
41 #define RK3399_EDP_LCDC_SEL BIT(5)
43 #define HIWORD_UPDATE(val, mask) (val | (mask) << 16)
45 #define PSR_WAIT_LINE_FLAG_TIMEOUT_MS 100
47 #define to_dp(nm) container_of(nm, struct rockchip_dp_device, nm)
50 * struct rockchip_dp_chip_data - splite the grf setting of kind of chips
51 * @lcdsel_grf_reg: grf register offset of lcdc select
52 * @lcdsel_big: reg value of selecting vop big for eDP
53 * @lcdsel_lit: reg value of selecting vop little for eDP
54 * @chip_type: specific chip type
56 struct rockchip_dp_chip_data
{
63 struct rockchip_dp_device
{
64 struct drm_device
*drm_dev
;
66 struct drm_encoder encoder
;
67 struct drm_display_mode mode
;
72 struct reset_control
*rst
;
74 struct work_struct psr_work
;
76 unsigned int psr_state
;
78 const struct rockchip_dp_chip_data
*data
;
80 struct analogix_dp_plat_data plat_data
;
83 static void analogix_dp_psr_set(struct drm_encoder
*encoder
, bool enabled
)
85 struct rockchip_dp_device
*dp
= to_dp(encoder
);
88 if (!analogix_dp_psr_supported(dp
->dev
))
91 dev_dbg(dp
->dev
, "%s PSR...\n", enabled
? "Entry" : "Exit");
93 spin_lock_irqsave(&dp
->psr_lock
, flags
);
95 dp
->psr_state
= EDP_VSC_PSR_STATE_ACTIVE
;
97 dp
->psr_state
= ~EDP_VSC_PSR_STATE_ACTIVE
;
99 schedule_work(&dp
->psr_work
);
100 spin_unlock_irqrestore(&dp
->psr_lock
, flags
);
103 static void analogix_dp_psr_work(struct work_struct
*work
)
105 struct rockchip_dp_device
*dp
=
106 container_of(work
, typeof(*dp
), psr_work
);
107 struct drm_crtc
*crtc
= dp
->encoder
.crtc
;
108 int psr_state
= dp
->psr_state
;
116 vact_end
= crtc
->mode
.vtotal
- crtc
->mode
.vsync_start
+ crtc
->mode
.vdisplay
;
118 ret
= rockchip_drm_wait_line_flag(dp
->encoder
.crtc
, vact_end
,
119 PSR_WAIT_LINE_FLAG_TIMEOUT_MS
);
121 dev_err(dp
->dev
, "line flag interrupt did not arrive\n");
125 spin_lock_irqsave(&dp
->psr_lock
, flags
);
126 if (psr_state
== EDP_VSC_PSR_STATE_ACTIVE
)
127 analogix_dp_enable_psr(dp
->dev
);
129 analogix_dp_disable_psr(dp
->dev
);
130 spin_unlock_irqrestore(&dp
->psr_lock
, flags
);
133 static int rockchip_dp_pre_init(struct rockchip_dp_device
*dp
)
135 reset_control_assert(dp
->rst
);
136 usleep_range(10, 20);
137 reset_control_deassert(dp
->rst
);
142 static int rockchip_dp_poweron(struct analogix_dp_plat_data
*plat_data
)
144 struct rockchip_dp_device
*dp
= to_dp(plat_data
);
147 cancel_work_sync(&dp
->psr_work
);
149 ret
= clk_prepare_enable(dp
->pclk
);
151 dev_err(dp
->dev
, "failed to enable pclk %d\n", ret
);
155 ret
= rockchip_dp_pre_init(dp
);
157 dev_err(dp
->dev
, "failed to dp pre init %d\n", ret
);
158 clk_disable_unprepare(dp
->pclk
);
165 static int rockchip_dp_powerdown(struct analogix_dp_plat_data
*plat_data
)
167 struct rockchip_dp_device
*dp
= to_dp(plat_data
);
169 clk_disable_unprepare(dp
->pclk
);
174 static int rockchip_dp_get_modes(struct analogix_dp_plat_data
*plat_data
,
175 struct drm_connector
*connector
)
177 struct drm_display_info
*di
= &connector
->display_info
;
178 /* VOP couldn't output YUV video format for eDP rightly */
179 u32 mask
= DRM_COLOR_FORMAT_YCRCB444
| DRM_COLOR_FORMAT_YCRCB422
;
181 if ((di
->color_formats
& mask
)) {
182 DRM_DEBUG_KMS("Swapping display color format from YUV to RGB\n");
183 di
->color_formats
&= ~mask
;
184 di
->color_formats
|= DRM_COLOR_FORMAT_RGB444
;
192 rockchip_dp_drm_encoder_mode_fixup(struct drm_encoder
*encoder
,
193 const struct drm_display_mode
*mode
,
194 struct drm_display_mode
*adjusted_mode
)
200 static void rockchip_dp_drm_encoder_mode_set(struct drm_encoder
*encoder
,
201 struct drm_display_mode
*mode
,
202 struct drm_display_mode
*adjusted
)
207 static void rockchip_dp_drm_encoder_enable(struct drm_encoder
*encoder
)
209 struct rockchip_dp_device
*dp
= to_dp(encoder
);
213 ret
= drm_of_encoder_active_endpoint_id(dp
->dev
->of_node
, encoder
);
218 val
= dp
->data
->lcdsel_lit
;
220 val
= dp
->data
->lcdsel_big
;
222 dev_dbg(dp
->dev
, "vop %s output to dp\n", (ret
) ? "LIT" : "BIG");
224 ret
= clk_prepare_enable(dp
->grfclk
);
226 dev_err(dp
->dev
, "failed to enable grfclk %d\n", ret
);
230 ret
= regmap_write(dp
->grf
, dp
->data
->lcdsel_grf_reg
, val
);
232 dev_err(dp
->dev
, "Could not write to GRF: %d\n", ret
);
234 clk_disable_unprepare(dp
->grfclk
);
237 static void rockchip_dp_drm_encoder_nop(struct drm_encoder
*encoder
)
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 rockchip_dp_device
*dp
= to_dp(encoder
);
252 * The hardware IC designed that VOP must output the RGB10 video
253 * format to eDP controller, and if eDP panel only support RGB8,
254 * then eDP controller should cut down the video data, not via VOP
255 * controller, that's why we need to hardcode the VOP output mode
259 s
->output_mode
= ROCKCHIP_OUT_MODE_AAAA
;
260 s
->output_type
= DRM_MODE_CONNECTOR_eDP
;
261 if (dp
->data
->chip_type
== RK3399_EDP
) {
263 * For RK3399, VOP Lit must code the out mode to RGB888,
264 * VOP Big must code the out mode to RGB10.
266 ret
= drm_of_encoder_active_endpoint_id(dp
->dev
->of_node
,
269 s
->output_mode
= ROCKCHIP_OUT_MODE_P888
;
275 static struct drm_encoder_helper_funcs rockchip_dp_encoder_helper_funcs
= {
276 .mode_fixup
= rockchip_dp_drm_encoder_mode_fixup
,
277 .mode_set
= rockchip_dp_drm_encoder_mode_set
,
278 .enable
= rockchip_dp_drm_encoder_enable
,
279 .disable
= rockchip_dp_drm_encoder_nop
,
280 .atomic_check
= rockchip_dp_drm_encoder_atomic_check
,
283 static void rockchip_dp_drm_encoder_destroy(struct drm_encoder
*encoder
)
285 drm_encoder_cleanup(encoder
);
288 static struct drm_encoder_funcs rockchip_dp_encoder_funcs
= {
289 .destroy
= rockchip_dp_drm_encoder_destroy
,
292 static int rockchip_dp_init(struct rockchip_dp_device
*dp
)
294 struct device
*dev
= dp
->dev
;
295 struct device_node
*np
= dev
->of_node
;
298 dp
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
299 if (IS_ERR(dp
->grf
)) {
300 dev_err(dev
, "failed to get rockchip,grf property\n");
301 return PTR_ERR(dp
->grf
);
304 dp
->grfclk
= devm_clk_get(dev
, "grf");
305 if (PTR_ERR(dp
->grfclk
) == -ENOENT
) {
307 } else if (PTR_ERR(dp
->grfclk
) == -EPROBE_DEFER
) {
308 return -EPROBE_DEFER
;
309 } else if (IS_ERR(dp
->grfclk
)) {
310 dev_err(dev
, "failed to get grf clock\n");
311 return PTR_ERR(dp
->grfclk
);
314 dp
->pclk
= devm_clk_get(dev
, "pclk");
315 if (IS_ERR(dp
->pclk
)) {
316 dev_err(dev
, "failed to get pclk property\n");
317 return PTR_ERR(dp
->pclk
);
320 dp
->rst
= devm_reset_control_get(dev
, "dp");
321 if (IS_ERR(dp
->rst
)) {
322 dev_err(dev
, "failed to get dp reset control\n");
323 return PTR_ERR(dp
->rst
);
326 ret
= clk_prepare_enable(dp
->pclk
);
328 dev_err(dp
->dev
, "failed to enable pclk %d\n", ret
);
332 ret
= rockchip_dp_pre_init(dp
);
334 dev_err(dp
->dev
, "failed to pre init %d\n", ret
);
335 clk_disable_unprepare(dp
->pclk
);
342 static int rockchip_dp_drm_create_encoder(struct rockchip_dp_device
*dp
)
344 struct drm_encoder
*encoder
= &dp
->encoder
;
345 struct drm_device
*drm_dev
= dp
->drm_dev
;
346 struct device
*dev
= dp
->dev
;
349 encoder
->possible_crtcs
= drm_of_find_possible_crtcs(drm_dev
,
351 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder
->possible_crtcs
);
353 ret
= drm_encoder_init(drm_dev
, encoder
, &rockchip_dp_encoder_funcs
,
354 DRM_MODE_ENCODER_TMDS
, NULL
);
356 DRM_ERROR("failed to initialize encoder with drm\n");
360 drm_encoder_helper_add(encoder
, &rockchip_dp_encoder_helper_funcs
);
365 static int rockchip_dp_bind(struct device
*dev
, struct device
*master
,
368 struct rockchip_dp_device
*dp
= dev_get_drvdata(dev
);
369 const struct rockchip_dp_chip_data
*dp_data
;
370 struct drm_device
*drm_dev
= data
;
374 * Just like the probe function said, we don't need the
375 * device drvrate anymore, we should leave the charge to
376 * analogix dp driver, set the device drvdata to NULL.
378 dev_set_drvdata(dev
, NULL
);
380 dp_data
= of_device_get_match_data(dev
);
384 ret
= rockchip_dp_init(dp
);
389 dp
->drm_dev
= drm_dev
;
391 ret
= rockchip_dp_drm_create_encoder(dp
);
393 DRM_ERROR("failed to create drm encoder\n");
397 dp
->plat_data
.encoder
= &dp
->encoder
;
399 dp
->plat_data
.dev_type
= dp
->data
->chip_type
;
400 dp
->plat_data
.power_on
= rockchip_dp_poweron
;
401 dp
->plat_data
.power_off
= rockchip_dp_powerdown
;
402 dp
->plat_data
.get_modes
= rockchip_dp_get_modes
;
404 spin_lock_init(&dp
->psr_lock
);
405 dp
->psr_state
= ~EDP_VSC_PSR_STATE_ACTIVE
;
406 INIT_WORK(&dp
->psr_work
, analogix_dp_psr_work
);
408 rockchip_drm_psr_register(&dp
->encoder
, analogix_dp_psr_set
);
410 return analogix_dp_bind(dev
, dp
->drm_dev
, &dp
->plat_data
);
413 static void rockchip_dp_unbind(struct device
*dev
, struct device
*master
,
416 struct rockchip_dp_device
*dp
= dev_get_drvdata(dev
);
418 rockchip_drm_psr_unregister(&dp
->encoder
);
420 return analogix_dp_unbind(dev
, master
, data
);
423 static const struct component_ops rockchip_dp_component_ops
= {
424 .bind
= rockchip_dp_bind
,
425 .unbind
= rockchip_dp_unbind
,
428 static int rockchip_dp_probe(struct platform_device
*pdev
)
430 struct device
*dev
= &pdev
->dev
;
431 struct device_node
*panel_node
, *port
, *endpoint
;
432 struct drm_panel
*panel
= NULL
;
433 struct rockchip_dp_device
*dp
;
435 port
= of_graph_get_port_by_id(dev
->of_node
, 1);
437 endpoint
= of_get_child_by_name(port
, "endpoint");
440 dev_err(dev
, "no output endpoint found\n");
444 panel_node
= of_graph_get_remote_port_parent(endpoint
);
445 of_node_put(endpoint
);
447 dev_err(dev
, "no output node found\n");
451 panel
= of_drm_find_panel(panel_node
);
452 of_node_put(panel_node
);
454 return -EPROBE_DEFER
;
457 dp
= devm_kzalloc(dev
, sizeof(*dp
), GFP_KERNEL
);
463 dp
->plat_data
.panel
= panel
;
466 * We just use the drvdata until driver run into component
467 * add function, and then we would set drvdata to null, so
468 * that analogix dp driver could take charge of the drvdata.
470 platform_set_drvdata(pdev
, dp
);
472 return component_add(dev
, &rockchip_dp_component_ops
);
475 static int rockchip_dp_remove(struct platform_device
*pdev
)
477 component_del(&pdev
->dev
, &rockchip_dp_component_ops
);
482 static const struct dev_pm_ops rockchip_dp_pm_ops
= {
483 #ifdef CONFIG_PM_SLEEP
484 .suspend
= analogix_dp_suspend
,
485 .resume_early
= analogix_dp_resume
,
489 static const struct rockchip_dp_chip_data rk3399_edp
= {
490 .lcdsel_grf_reg
= RK3399_GRF_SOC_CON20
,
491 .lcdsel_big
= HIWORD_UPDATE(0, RK3399_EDP_LCDC_SEL
),
492 .lcdsel_lit
= HIWORD_UPDATE(RK3399_EDP_LCDC_SEL
, RK3399_EDP_LCDC_SEL
),
493 .chip_type
= RK3399_EDP
,
496 static const struct rockchip_dp_chip_data rk3288_dp
= {
497 .lcdsel_grf_reg
= RK3288_GRF_SOC_CON6
,
498 .lcdsel_big
= HIWORD_UPDATE(0, RK3288_EDP_LCDC_SEL
),
499 .lcdsel_lit
= HIWORD_UPDATE(RK3288_EDP_LCDC_SEL
, RK3288_EDP_LCDC_SEL
),
500 .chip_type
= RK3288_DP
,
503 static const struct of_device_id rockchip_dp_dt_ids
[] = {
504 {.compatible
= "rockchip,rk3288-dp", .data
= &rk3288_dp
},
505 {.compatible
= "rockchip,rk3399-edp", .data
= &rk3399_edp
},
508 MODULE_DEVICE_TABLE(of
, rockchip_dp_dt_ids
);
510 static struct platform_driver rockchip_dp_driver
= {
511 .probe
= rockchip_dp_probe
,
512 .remove
= rockchip_dp_remove
,
514 .name
= "rockchip-dp",
515 .pm
= &rockchip_dp_pm_ops
,
516 .of_match_table
= of_match_ptr(rockchip_dp_dt_ids
),
520 module_platform_driver(rockchip_dp_driver
);
522 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
523 MODULE_AUTHOR("Jeff chen <jeff.chen@rock-chips.com>");
524 MODULE_DESCRIPTION("Rockchip Specific Analogix-DP Driver Extension");
525 MODULE_LICENSE("GPL v2");