drm/exynos: Stop using drm_framebuffer_unregister_private
[linux/fpc-iii.git] / drivers / gpu / drm / rockchip / analogix_dp-rockchip.c
blob8548e8271639c665be80420dc7e77905ded5cf9a
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 struct work_struct psr_work;
75 spinlock_t psr_lock;
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);
86 unsigned long flags;
88 if (!analogix_dp_psr_supported(dp->dev))
89 return;
91 dev_dbg(dp->dev, "%s PSR...\n", enabled ? "Entry" : "Exit");
93 spin_lock_irqsave(&dp->psr_lock, flags);
94 if (enabled)
95 dp->psr_state = EDP_VSC_PSR_STATE_ACTIVE;
96 else
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;
109 int vact_end;
110 int ret;
111 unsigned long flags;
113 if (!crtc)
114 return;
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);
120 if (ret) {
121 dev_err(dp->dev, "line flag interrupt did not arrive\n");
122 return;
125 spin_lock_irqsave(&dp->psr_lock, flags);
126 if (psr_state == EDP_VSC_PSR_STATE_ACTIVE)
127 analogix_dp_enable_psr(dp->dev);
128 else
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);
139 return 0;
142 static int rockchip_dp_poweron(struct analogix_dp_plat_data *plat_data)
144 struct rockchip_dp_device *dp = to_dp(plat_data);
145 int ret;
147 cancel_work_sync(&dp->psr_work);
149 ret = clk_prepare_enable(dp->pclk);
150 if (ret < 0) {
151 dev_err(dp->dev, "failed to enable pclk %d\n", ret);
152 return ret;
155 ret = rockchip_dp_pre_init(dp);
156 if (ret < 0) {
157 dev_err(dp->dev, "failed to dp pre init %d\n", ret);
158 clk_disable_unprepare(dp->pclk);
159 return ret;
162 return 0;
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);
171 return 0;
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;
185 di->bpc = 8;
188 return 0;
191 static bool
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)
196 /* do nothing */
197 return true;
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)
204 /* do nothing */
207 static void rockchip_dp_drm_encoder_enable(struct drm_encoder *encoder)
209 struct rockchip_dp_device *dp = to_dp(encoder);
210 int ret;
211 u32 val;
213 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
214 if (ret < 0)
215 return;
217 if (ret)
218 val = dp->data->lcdsel_lit;
219 else
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);
225 if (ret < 0) {
226 dev_err(dp->dev, "failed to enable grfclk %d\n", ret);
227 return;
230 ret = regmap_write(dp->grf, dp->data->lcdsel_grf_reg, val);
231 if (ret != 0)
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)
239 /* do nothing */
242 static int
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);
249 int ret;
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
256 * to RGA10 here.
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,
267 encoder);
268 if (ret > 0)
269 s->output_mode = ROCKCHIP_OUT_MODE_P888;
272 return 0;
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;
296 int ret;
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) {
306 dp->grfclk = NULL;
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);
327 if (ret < 0) {
328 dev_err(dp->dev, "failed to enable pclk %d\n", ret);
329 return ret;
332 ret = rockchip_dp_pre_init(dp);
333 if (ret < 0) {
334 dev_err(dp->dev, "failed to pre init %d\n", ret);
335 clk_disable_unprepare(dp->pclk);
336 return ret;
339 return 0;
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;
347 int ret;
349 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
350 dev->of_node);
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);
355 if (ret) {
356 DRM_ERROR("failed to initialize encoder with drm\n");
357 return ret;
360 drm_encoder_helper_add(encoder, &rockchip_dp_encoder_helper_funcs);
362 return 0;
365 static int rockchip_dp_bind(struct device *dev, struct device *master,
366 void *data)
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;
371 int ret;
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);
381 if (!dp_data)
382 return -ENODEV;
384 ret = rockchip_dp_init(dp);
385 if (ret < 0)
386 return ret;
388 dp->data = dp_data;
389 dp->drm_dev = drm_dev;
391 ret = rockchip_dp_drm_create_encoder(dp);
392 if (ret) {
393 DRM_ERROR("failed to create drm encoder\n");
394 return ret;
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,
414 void *data)
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);
436 if (port) {
437 endpoint = of_get_child_by_name(port, "endpoint");
438 of_node_put(port);
439 if (!endpoint) {
440 dev_err(dev, "no output endpoint found\n");
441 return -EINVAL;
444 panel_node = of_graph_get_remote_port_parent(endpoint);
445 of_node_put(endpoint);
446 if (!panel_node) {
447 dev_err(dev, "no output node found\n");
448 return -EINVAL;
451 panel = of_drm_find_panel(panel_node);
452 of_node_put(panel_node);
453 if (!panel)
454 return -EPROBE_DEFER;
457 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
458 if (!dp)
459 return -ENOMEM;
461 dp->dev = dev;
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);
479 return 0;
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,
486 #endif
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,
513 .driver = {
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");