drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / rockchip / analogix_dp-rockchip.c
blob080f0535219502306774ea799c10cc30793006ca
1 /*
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>
23 #include <drm/drmP.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)
49 /**
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 {
57 u32 lcdsel_grf_reg;
58 u32 lcdsel_big;
59 u32 lcdsel_lit;
60 u32 chip_type;
63 struct rockchip_dp_device {
64 struct drm_device *drm_dev;
65 struct device *dev;
66 struct drm_encoder encoder;
67 struct drm_display_mode mode;
69 struct clk *pclk;
70 struct clk *grfclk;
71 struct regmap *grf;
72 struct reset_control *rst;
74 const struct rockchip_dp_chip_data *data;
76 struct analogix_dp_device *adp;
77 struct analogix_dp_plat_data plat_data;
80 static int analogix_dp_psr_set(struct drm_encoder *encoder, bool enabled)
82 struct rockchip_dp_device *dp = to_dp(encoder);
83 int ret;
85 if (!analogix_dp_psr_enabled(dp->adp))
86 return 0;
88 DRM_DEV_DEBUG(dp->dev, "%s PSR...\n", enabled ? "Entry" : "Exit");
90 ret = rockchip_drm_wait_vact_end(dp->encoder.crtc,
91 PSR_WAIT_LINE_FLAG_TIMEOUT_MS);
92 if (ret) {
93 DRM_DEV_ERROR(dp->dev, "line flag interrupt did not arrive\n");
94 return -ETIMEDOUT;
97 if (enabled)
98 return analogix_dp_enable_psr(dp->adp);
99 else
100 return analogix_dp_disable_psr(dp->adp);
103 static int rockchip_dp_pre_init(struct rockchip_dp_device *dp)
105 reset_control_assert(dp->rst);
106 usleep_range(10, 20);
107 reset_control_deassert(dp->rst);
109 return 0;
112 static int rockchip_dp_poweron_start(struct analogix_dp_plat_data *plat_data)
114 struct rockchip_dp_device *dp = to_dp(plat_data);
115 int ret;
117 ret = clk_prepare_enable(dp->pclk);
118 if (ret < 0) {
119 DRM_DEV_ERROR(dp->dev, "failed to enable pclk %d\n", ret);
120 return ret;
123 ret = rockchip_dp_pre_init(dp);
124 if (ret < 0) {
125 DRM_DEV_ERROR(dp->dev, "failed to dp pre init %d\n", ret);
126 clk_disable_unprepare(dp->pclk);
127 return ret;
130 return ret;
133 static int rockchip_dp_poweron_end(struct analogix_dp_plat_data *plat_data)
135 struct rockchip_dp_device *dp = to_dp(plat_data);
137 return rockchip_drm_psr_inhibit_put(&dp->encoder);
140 static int rockchip_dp_powerdown(struct analogix_dp_plat_data *plat_data)
142 struct rockchip_dp_device *dp = to_dp(plat_data);
143 int ret;
145 ret = rockchip_drm_psr_inhibit_get(&dp->encoder);
146 if (ret != 0)
147 return ret;
149 clk_disable_unprepare(dp->pclk);
151 return 0;
154 static int rockchip_dp_get_modes(struct analogix_dp_plat_data *plat_data,
155 struct drm_connector *connector)
157 struct drm_display_info *di = &connector->display_info;
158 /* VOP couldn't output YUV video format for eDP rightly */
159 u32 mask = DRM_COLOR_FORMAT_YCRCB444 | DRM_COLOR_FORMAT_YCRCB422;
161 if ((di->color_formats & mask)) {
162 DRM_DEBUG_KMS("Swapping display color format from YUV to RGB\n");
163 di->color_formats &= ~mask;
164 di->color_formats |= DRM_COLOR_FORMAT_RGB444;
165 di->bpc = 8;
168 return 0;
171 static bool
172 rockchip_dp_drm_encoder_mode_fixup(struct drm_encoder *encoder,
173 const struct drm_display_mode *mode,
174 struct drm_display_mode *adjusted_mode)
176 /* do nothing */
177 return true;
180 static void rockchip_dp_drm_encoder_mode_set(struct drm_encoder *encoder,
181 struct drm_display_mode *mode,
182 struct drm_display_mode *adjusted)
184 /* do nothing */
187 static void rockchip_dp_drm_encoder_enable(struct drm_encoder *encoder)
189 struct rockchip_dp_device *dp = to_dp(encoder);
190 int ret;
191 u32 val;
193 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
194 if (ret < 0)
195 return;
197 if (ret)
198 val = dp->data->lcdsel_lit;
199 else
200 val = dp->data->lcdsel_big;
202 DRM_DEV_DEBUG(dp->dev, "vop %s output to dp\n", (ret) ? "LIT" : "BIG");
204 ret = clk_prepare_enable(dp->grfclk);
205 if (ret < 0) {
206 DRM_DEV_ERROR(dp->dev, "failed to enable grfclk %d\n", ret);
207 return;
210 ret = regmap_write(dp->grf, dp->data->lcdsel_grf_reg, val);
211 if (ret != 0)
212 DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
214 clk_disable_unprepare(dp->grfclk);
217 static void rockchip_dp_drm_encoder_nop(struct drm_encoder *encoder)
219 /* do nothing */
222 static int
223 rockchip_dp_drm_encoder_atomic_check(struct drm_encoder *encoder,
224 struct drm_crtc_state *crtc_state,
225 struct drm_connector_state *conn_state)
227 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
228 struct drm_display_info *di = &conn_state->connector->display_info;
231 * The hardware IC designed that VOP must output the RGB10 video
232 * format to eDP controller, and if eDP panel only support RGB8,
233 * then eDP controller should cut down the video data, not via VOP
234 * controller, that's why we need to hardcode the VOP output mode
235 * to RGA10 here.
238 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
239 s->output_type = DRM_MODE_CONNECTOR_eDP;
240 s->output_bpc = di->bpc;
242 return 0;
245 static struct drm_encoder_helper_funcs rockchip_dp_encoder_helper_funcs = {
246 .mode_fixup = rockchip_dp_drm_encoder_mode_fixup,
247 .mode_set = rockchip_dp_drm_encoder_mode_set,
248 .enable = rockchip_dp_drm_encoder_enable,
249 .disable = rockchip_dp_drm_encoder_nop,
250 .atomic_check = rockchip_dp_drm_encoder_atomic_check,
253 static struct drm_encoder_funcs rockchip_dp_encoder_funcs = {
254 .destroy = drm_encoder_cleanup,
257 static int rockchip_dp_of_probe(struct rockchip_dp_device *dp)
259 struct device *dev = dp->dev;
260 struct device_node *np = dev->of_node;
262 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
263 if (IS_ERR(dp->grf)) {
264 DRM_DEV_ERROR(dev, "failed to get rockchip,grf property\n");
265 return PTR_ERR(dp->grf);
268 dp->grfclk = devm_clk_get(dev, "grf");
269 if (PTR_ERR(dp->grfclk) == -ENOENT) {
270 dp->grfclk = NULL;
271 } else if (PTR_ERR(dp->grfclk) == -EPROBE_DEFER) {
272 return -EPROBE_DEFER;
273 } else if (IS_ERR(dp->grfclk)) {
274 DRM_DEV_ERROR(dev, "failed to get grf clock\n");
275 return PTR_ERR(dp->grfclk);
278 dp->pclk = devm_clk_get(dev, "pclk");
279 if (IS_ERR(dp->pclk)) {
280 DRM_DEV_ERROR(dev, "failed to get pclk property\n");
281 return PTR_ERR(dp->pclk);
284 dp->rst = devm_reset_control_get(dev, "dp");
285 if (IS_ERR(dp->rst)) {
286 DRM_DEV_ERROR(dev, "failed to get dp reset control\n");
287 return PTR_ERR(dp->rst);
290 return 0;
293 static int rockchip_dp_drm_create_encoder(struct rockchip_dp_device *dp)
295 struct drm_encoder *encoder = &dp->encoder;
296 struct drm_device *drm_dev = dp->drm_dev;
297 struct device *dev = dp->dev;
298 int ret;
300 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
301 dev->of_node);
302 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
304 ret = drm_encoder_init(drm_dev, encoder, &rockchip_dp_encoder_funcs,
305 DRM_MODE_ENCODER_TMDS, NULL);
306 if (ret) {
307 DRM_ERROR("failed to initialize encoder with drm\n");
308 return ret;
311 drm_encoder_helper_add(encoder, &rockchip_dp_encoder_helper_funcs);
313 return 0;
316 static int rockchip_dp_bind(struct device *dev, struct device *master,
317 void *data)
319 struct rockchip_dp_device *dp = dev_get_drvdata(dev);
320 const struct rockchip_dp_chip_data *dp_data;
321 struct drm_device *drm_dev = data;
322 int ret;
324 dp_data = of_device_get_match_data(dev);
325 if (!dp_data)
326 return -ENODEV;
328 dp->data = dp_data;
329 dp->drm_dev = drm_dev;
331 ret = rockchip_dp_drm_create_encoder(dp);
332 if (ret) {
333 DRM_ERROR("failed to create drm encoder\n");
334 return ret;
337 dp->plat_data.encoder = &dp->encoder;
339 dp->plat_data.dev_type = dp->data->chip_type;
340 dp->plat_data.power_on_start = rockchip_dp_poweron_start;
341 dp->plat_data.power_on_end = rockchip_dp_poweron_end;
342 dp->plat_data.power_off = rockchip_dp_powerdown;
343 dp->plat_data.get_modes = rockchip_dp_get_modes;
345 ret = rockchip_drm_psr_register(&dp->encoder, analogix_dp_psr_set);
346 if (ret < 0)
347 goto err_cleanup_encoder;
349 dp->adp = analogix_dp_bind(dev, dp->drm_dev, &dp->plat_data);
350 if (IS_ERR(dp->adp)) {
351 ret = PTR_ERR(dp->adp);
352 goto err_unreg_psr;
355 return 0;
356 err_unreg_psr:
357 rockchip_drm_psr_unregister(&dp->encoder);
358 err_cleanup_encoder:
359 dp->encoder.funcs->destroy(&dp->encoder);
360 return ret;
363 static void rockchip_dp_unbind(struct device *dev, struct device *master,
364 void *data)
366 struct rockchip_dp_device *dp = dev_get_drvdata(dev);
368 analogix_dp_unbind(dp->adp);
369 rockchip_drm_psr_unregister(&dp->encoder);
370 dp->encoder.funcs->destroy(&dp->encoder);
372 dp->adp = ERR_PTR(-ENODEV);
375 static const struct component_ops rockchip_dp_component_ops = {
376 .bind = rockchip_dp_bind,
377 .unbind = rockchip_dp_unbind,
380 static int rockchip_dp_probe(struct platform_device *pdev)
382 struct device *dev = &pdev->dev;
383 struct drm_panel *panel = NULL;
384 struct rockchip_dp_device *dp;
385 int ret;
387 ret = drm_of_find_panel_or_bridge(dev->of_node, 1, 0, &panel, NULL);
388 if (ret < 0)
389 return ret;
391 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
392 if (!dp)
393 return -ENOMEM;
395 dp->dev = dev;
396 dp->adp = ERR_PTR(-ENODEV);
397 dp->plat_data.panel = panel;
399 ret = rockchip_dp_of_probe(dp);
400 if (ret < 0)
401 return ret;
403 platform_set_drvdata(pdev, dp);
405 return component_add(dev, &rockchip_dp_component_ops);
408 static int rockchip_dp_remove(struct platform_device *pdev)
410 component_del(&pdev->dev, &rockchip_dp_component_ops);
412 return 0;
415 #ifdef CONFIG_PM_SLEEP
416 static int rockchip_dp_suspend(struct device *dev)
418 struct rockchip_dp_device *dp = dev_get_drvdata(dev);
420 if (IS_ERR(dp->adp))
421 return 0;
423 return analogix_dp_suspend(dp->adp);
426 static int rockchip_dp_resume(struct device *dev)
428 struct rockchip_dp_device *dp = dev_get_drvdata(dev);
430 if (IS_ERR(dp->adp))
431 return 0;
433 return analogix_dp_resume(dp->adp);
435 #endif
437 static const struct dev_pm_ops rockchip_dp_pm_ops = {
438 #ifdef CONFIG_PM_SLEEP
439 .suspend = rockchip_dp_suspend,
440 .resume_early = rockchip_dp_resume,
441 #endif
444 static const struct rockchip_dp_chip_data rk3399_edp = {
445 .lcdsel_grf_reg = RK3399_GRF_SOC_CON20,
446 .lcdsel_big = HIWORD_UPDATE(0, RK3399_EDP_LCDC_SEL),
447 .lcdsel_lit = HIWORD_UPDATE(RK3399_EDP_LCDC_SEL, RK3399_EDP_LCDC_SEL),
448 .chip_type = RK3399_EDP,
451 static const struct rockchip_dp_chip_data rk3288_dp = {
452 .lcdsel_grf_reg = RK3288_GRF_SOC_CON6,
453 .lcdsel_big = HIWORD_UPDATE(0, RK3288_EDP_LCDC_SEL),
454 .lcdsel_lit = HIWORD_UPDATE(RK3288_EDP_LCDC_SEL, RK3288_EDP_LCDC_SEL),
455 .chip_type = RK3288_DP,
458 static const struct of_device_id rockchip_dp_dt_ids[] = {
459 {.compatible = "rockchip,rk3288-dp", .data = &rk3288_dp },
460 {.compatible = "rockchip,rk3399-edp", .data = &rk3399_edp },
463 MODULE_DEVICE_TABLE(of, rockchip_dp_dt_ids);
465 struct platform_driver rockchip_dp_driver = {
466 .probe = rockchip_dp_probe,
467 .remove = rockchip_dp_remove,
468 .driver = {
469 .name = "rockchip-dp",
470 .pm = &rockchip_dp_pm_ops,
471 .of_match_table = of_match_ptr(rockchip_dp_dt_ids),