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_graph.h>
18 #include <linux/regmap.h>
19 #include <linux/reset.h>
20 #include <linux/clk.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_dp_helper.h>
25 #include <drm/drm_of.h>
26 #include <drm/drm_panel.h>
28 #include <video/of_videomode.h>
29 #include <video/videomode.h>
31 #include <drm/bridge/analogix_dp.h>
33 #include "rockchip_drm_drv.h"
34 #include "rockchip_drm_vop.h"
36 #define to_dp(nm) container_of(nm, struct rockchip_dp_device, nm)
38 /* dp grf register offset */
39 #define GRF_SOC_CON6 0x025c
40 #define GRF_EDP_LCD_SEL_MASK BIT(5)
41 #define GRF_EDP_SEL_VOP_LIT BIT(5)
42 #define GRF_EDP_SEL_VOP_BIG 0
44 struct rockchip_dp_device {
45 struct drm_device *drm_dev;
47 struct drm_encoder encoder;
48 struct drm_display_mode mode;
52 struct reset_control *rst;
54 struct analogix_dp_plat_data plat_data;
57 static int rockchip_dp_pre_init(struct rockchip_dp_device *dp)
59 reset_control_assert(dp->rst);
61 reset_control_deassert(dp->rst);
66 static int rockchip_dp_poweron(struct analogix_dp_plat_data *plat_data)
68 struct rockchip_dp_device *dp = to_dp(plat_data);
71 ret = clk_prepare_enable(dp->pclk);
73 dev_err(dp->dev, "failed to enable pclk %d\n", ret);
77 ret = rockchip_dp_pre_init(dp);
79 dev_err(dp->dev, "failed to dp pre init %d\n", ret);
86 static int rockchip_dp_powerdown(struct analogix_dp_plat_data *plat_data)
88 struct rockchip_dp_device *dp = to_dp(plat_data);
90 clk_disable_unprepare(dp->pclk);
96 rockchip_dp_drm_encoder_mode_fixup(struct drm_encoder *encoder,
97 const struct drm_display_mode *mode,
98 struct drm_display_mode *adjusted_mode)
104 static void rockchip_dp_drm_encoder_mode_set(struct drm_encoder *encoder,
105 struct drm_display_mode *mode,
106 struct drm_display_mode *adjusted)
111 static void rockchip_dp_drm_encoder_enable(struct drm_encoder *encoder)
113 struct rockchip_dp_device *dp = to_dp(encoder);
118 * FIXME(Yakir): driver should configure the CRTC output video
119 * mode with the display information which indicated the monitor
120 * support colorimetry.
122 * But don't know why the CRTC driver seems could only output the
123 * RGBaaa rightly. For example, if connect the "innolux,n116bge"
124 * eDP screen, EDID would indicated that screen only accepted the
125 * 6bpc mode. But if I configure CRTC to RGB666 output, then eDP
126 * screen would show a blue picture (RGB888 show a green picture).
127 * But if I configure CTRC to RGBaaa, and eDP driver still keep
128 * RGB666 input video mode, then screen would works prefect.
130 ret = rockchip_drm_crtc_mode_config(encoder->crtc,
131 DRM_MODE_CONNECTOR_eDP,
132 ROCKCHIP_OUT_MODE_AAAA);
134 dev_err(dp->dev, "Could not set crtc mode config (%d)\n", ret);
138 ret = rockchip_drm_encoder_get_mux_id(dp->dev->of_node, encoder);
143 val = GRF_EDP_SEL_VOP_LIT | (GRF_EDP_LCD_SEL_MASK << 16);
145 val = GRF_EDP_SEL_VOP_BIG | (GRF_EDP_LCD_SEL_MASK << 16);
147 dev_dbg(dp->dev, "vop %s output to dp\n", (ret) ? "LIT" : "BIG");
149 ret = regmap_write(dp->grf, GRF_SOC_CON6, val);
151 dev_err(dp->dev, "Could not write to GRF: %d\n", ret);
156 static void rockchip_dp_drm_encoder_nop(struct drm_encoder *encoder)
161 static struct drm_encoder_helper_funcs rockchip_dp_encoder_helper_funcs = {
162 .mode_fixup = rockchip_dp_drm_encoder_mode_fixup,
163 .mode_set = rockchip_dp_drm_encoder_mode_set,
164 .enable = rockchip_dp_drm_encoder_enable,
165 .disable = rockchip_dp_drm_encoder_nop,
168 static void rockchip_dp_drm_encoder_destroy(struct drm_encoder *encoder)
170 drm_encoder_cleanup(encoder);
173 static struct drm_encoder_funcs rockchip_dp_encoder_funcs = {
174 .destroy = rockchip_dp_drm_encoder_destroy,
177 static int rockchip_dp_init(struct rockchip_dp_device *dp)
179 struct device *dev = dp->dev;
180 struct device_node *np = dev->of_node;
183 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
184 if (IS_ERR(dp->grf)) {
185 dev_err(dev, "failed to get rockchip,grf property\n");
186 return PTR_ERR(dp->grf);
189 dp->pclk = devm_clk_get(dev, "pclk");
190 if (IS_ERR(dp->pclk)) {
191 dev_err(dev, "failed to get pclk property\n");
192 return PTR_ERR(dp->pclk);
195 dp->rst = devm_reset_control_get(dev, "dp");
196 if (IS_ERR(dp->rst)) {
197 dev_err(dev, "failed to get dp reset control\n");
198 return PTR_ERR(dp->rst);
201 ret = clk_prepare_enable(dp->pclk);
203 dev_err(dp->dev, "failed to enable pclk %d\n", ret);
207 ret = rockchip_dp_pre_init(dp);
209 dev_err(dp->dev, "failed to pre init %d\n", ret);
216 static int rockchip_dp_drm_create_encoder(struct rockchip_dp_device *dp)
218 struct drm_encoder *encoder = &dp->encoder;
219 struct drm_device *drm_dev = dp->drm_dev;
220 struct device *dev = dp->dev;
223 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
225 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
227 ret = drm_encoder_init(drm_dev, encoder, &rockchip_dp_encoder_funcs,
228 DRM_MODE_ENCODER_TMDS, NULL);
230 DRM_ERROR("failed to initialize encoder with drm\n");
234 drm_encoder_helper_add(encoder, &rockchip_dp_encoder_helper_funcs);
239 static int rockchip_dp_bind(struct device *dev, struct device *master,
242 struct rockchip_dp_device *dp = dev_get_drvdata(dev);
243 struct drm_device *drm_dev = data;
247 * Just like the probe function said, we don't need the
248 * device drvrate anymore, we should leave the charge to
249 * analogix dp driver, set the device drvdata to NULL.
251 dev_set_drvdata(dev, NULL);
253 ret = rockchip_dp_init(dp);
257 dp->drm_dev = drm_dev;
259 ret = rockchip_dp_drm_create_encoder(dp);
261 DRM_ERROR("failed to create drm encoder\n");
265 dp->plat_data.encoder = &dp->encoder;
267 dp->plat_data.dev_type = RK3288_DP;
268 dp->plat_data.power_on = rockchip_dp_poweron;
269 dp->plat_data.power_off = rockchip_dp_powerdown;
271 return analogix_dp_bind(dev, dp->drm_dev, &dp->plat_data);
274 static void rockchip_dp_unbind(struct device *dev, struct device *master,
277 return analogix_dp_unbind(dev, master, data);
280 static const struct component_ops rockchip_dp_component_ops = {
281 .bind = rockchip_dp_bind,
282 .unbind = rockchip_dp_unbind,
285 static int rockchip_dp_probe(struct platform_device *pdev)
287 struct device *dev = &pdev->dev;
288 struct device_node *panel_node, *port, *endpoint;
289 struct rockchip_dp_device *dp;
290 struct drm_panel *panel;
292 port = of_graph_get_port_by_id(dev->of_node, 1);
294 dev_err(dev, "can't find output port\n");
298 endpoint = of_get_child_by_name(port, "endpoint");
301 dev_err(dev, "no output endpoint found\n");
305 panel_node = of_graph_get_remote_port_parent(endpoint);
306 of_node_put(endpoint);
308 dev_err(dev, "no output node found\n");
312 panel = of_drm_find_panel(panel_node);
314 DRM_ERROR("failed to find panel\n");
315 of_node_put(panel_node);
316 return -EPROBE_DEFER;
319 of_node_put(panel_node);
321 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
327 dp->plat_data.panel = panel;
330 * We just use the drvdata until driver run into component
331 * add function, and then we would set drvdata to null, so
332 * that analogix dp driver could take charge of the drvdata.
334 platform_set_drvdata(pdev, dp);
336 return component_add(dev, &rockchip_dp_component_ops);
339 static int rockchip_dp_remove(struct platform_device *pdev)
341 component_del(&pdev->dev, &rockchip_dp_component_ops);
346 #ifdef CONFIG_PM_SLEEP
347 static int rockchip_dp_suspend(struct device *dev)
349 return analogix_dp_suspend(dev);
352 static int rockchip_dp_resume(struct device *dev)
354 return analogix_dp_resume(dev);
358 static const struct dev_pm_ops rockchip_dp_pm_ops = {
359 SET_SYSTEM_SLEEP_PM_OPS(rockchip_dp_suspend, rockchip_dp_resume)
362 static const struct of_device_id rockchip_dp_dt_ids[] = {
363 {.compatible = "rockchip,rk3288-dp",},
364 {.compatible = "rockchip,rk3399-edp",},
367 MODULE_DEVICE_TABLE(of, rockchip_dp_dt_ids);
369 static struct platform_driver rockchip_dp_driver = {
370 .probe = rockchip_dp_probe,
371 .remove = rockchip_dp_remove,
373 .name = "rockchip-dp",
374 .owner = THIS_MODULE,
375 .pm = &rockchip_dp_pm_ops,
376 .of_match_table = of_match_ptr(rockchip_dp_dt_ids),
380 module_platform_driver(rockchip_dp_driver);
382 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
383 MODULE_AUTHOR("Jeff chen <jeff.chen@rock-chips.com>");
384 MODULE_DESCRIPTION("Rockchip Specific Analogix-DP Driver Extension");
385 MODULE_LICENSE("GPL v2");