blob: 030fc5e66c6cd474111bb2dea445a33a92c15bd6 [file] [log] [blame]
Alper Nebi Yasak53f20332020-10-22 22:43:13 +03001// SPDX-License-Identifier: GPL-2.0
Simon Glasse421bb82016-01-21 19:45:05 -07002/*
3 * Copyright (c) 2015 Google, Inc
4 * Copyright 2014 Rockchip Inc.
Simon Glasse421bb82016-01-21 19:45:05 -07005 */
6
7#include <common.h>
8#include <clk.h>
9#include <display.h>
10#include <dm.h>
11#include <edid.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glasse421bb82016-01-21 19:45:05 -070013#include <regmap.h>
14#include <syscon.h>
15#include <video.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glasse421bb82016-01-21 19:45:05 -070017#include <asm/gpio.h>
Simon Glasse421bb82016-01-21 19:45:05 -070018#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080019#include <asm/arch-rockchip/clock.h>
20#include <asm/arch-rockchip/edp_rk3288.h>
21#include <asm/arch-rockchip/vop_rk3288.h>
Simon Glasse421bb82016-01-21 19:45:05 -070022#include <dm/device-internal.h>
23#include <dm/uclass-internal.h>
Arnaud Patard (Rtp)1af703c2021-03-05 11:27:49 +010024#include <efi.h>
25#include <efi_loader.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070027#include <linux/err.h>
Simon Glasse421bb82016-01-21 19:45:05 -070028#include <power/regulator.h>
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020029#include "rk_vop.h"
Simon Glasse421bb82016-01-21 19:45:05 -070030
31DECLARE_GLOBAL_DATA_PTR;
32
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020033enum vop_pol {
34 HSYNC_POSITIVE = 0,
35 VSYNC_POSITIVE = 1,
36 DEN_NEGATIVE = 2,
37 DCLK_INVERT = 3
Simon Glasse421bb82016-01-21 19:45:05 -070038};
39
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020040static void rkvop_enable(struct rk3288_vop *regs, ulong fbbase,
41 int fb_bits_per_pixel,
42 const struct display_timing *edid)
Simon Glasse421bb82016-01-21 19:45:05 -070043{
44 u32 lb_mode;
45 u32 rgb_mode;
46 u32 hactive = edid->hactive.typ;
47 u32 vactive = edid->vactive.typ;
48
49 writel(V_ACT_WIDTH(hactive - 1) | V_ACT_HEIGHT(vactive - 1),
50 &regs->win0_act_info);
51
52 writel(V_DSP_XST(edid->hsync_len.typ + edid->hback_porch.typ) |
53 V_DSP_YST(edid->vsync_len.typ + edid->vback_porch.typ),
54 &regs->win0_dsp_st);
55
56 writel(V_DSP_WIDTH(hactive - 1) |
57 V_DSP_HEIGHT(vactive - 1),
58 &regs->win0_dsp_info);
59
60 clrsetbits_le32(&regs->win0_color_key, M_WIN0_KEY_EN | M_WIN0_KEY_COLOR,
61 V_WIN0_KEY_EN(0) | V_WIN0_KEY_COLOR(0));
62
63 switch (fb_bits_per_pixel) {
64 case 16:
65 rgb_mode = RGB565;
66 writel(V_RGB565_VIRWIDTH(hactive), &regs->win0_vir);
67 break;
68 case 24:
69 rgb_mode = RGB888;
70 writel(V_RGB888_VIRWIDTH(hactive), &regs->win0_vir);
71 break;
72 case 32:
73 default:
74 rgb_mode = ARGB8888;
75 writel(V_ARGB888_VIRWIDTH(hactive), &regs->win0_vir);
76 break;
77 }
78
79 if (hactive > 2560)
80 lb_mode = LB_RGB_3840X2;
81 else if (hactive > 1920)
82 lb_mode = LB_RGB_2560X4;
83 else if (hactive > 1280)
84 lb_mode = LB_RGB_1920X5;
85 else
86 lb_mode = LB_RGB_1280X8;
87
88 clrsetbits_le32(&regs->win0_ctrl0,
89 M_WIN0_LB_MODE | M_WIN0_DATA_FMT | M_WIN0_EN,
90 V_WIN0_LB_MODE(lb_mode) | V_WIN0_DATA_FMT(rgb_mode) |
91 V_WIN0_EN(1));
92
93 writel(fbbase, &regs->win0_yrgb_mst);
94 writel(0x01, &regs->reg_cfg_done); /* enable reg config */
95}
96
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020097static void rkvop_set_pin_polarity(struct udevice *dev,
98 enum vop_modes mode, u32 polarity)
Simon Glasse421bb82016-01-21 19:45:05 -070099{
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200100 struct rkvop_driverdata *ops =
101 (struct rkvop_driverdata *)dev_get_driver_data(dev);
102
103 if (ops->set_pin_polarity)
104 ops->set_pin_polarity(dev, mode, polarity);
105}
106
107static void rkvop_enable_output(struct udevice *dev, enum vop_modes mode)
108{
109 struct rk_vop_priv *priv = dev_get_priv(dev);
110 struct rk3288_vop *regs = priv->regs;
Simon Glasse421bb82016-01-21 19:45:05 -0700111
Simon Glassd7429502017-05-31 17:57:29 -0600112 /* remove from standby */
113 clrbits_le32(&regs->sys_ctrl, V_STANDBY_EN(1));
114
Simon Glasse421bb82016-01-21 19:45:05 -0700115 switch (mode) {
116 case VOP_MODE_HDMI:
117 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
118 V_HDMI_OUT_EN(1));
119 break;
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200120
Simon Glasse421bb82016-01-21 19:45:05 -0700121 case VOP_MODE_EDP:
Simon Glasse421bb82016-01-21 19:45:05 -0700122 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
123 V_EDP_OUT_EN(1));
124 break;
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200125
Jagan Teki5023ade2020-04-02 17:11:22 +0530126#if defined(CONFIG_ROCKCHIP_RK3288)
Jacob Chen0b6aee42016-03-14 11:20:18 +0800127 case VOP_MODE_LVDS:
128 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
129 V_RGB_OUT_EN(1));
130 break;
Jagan Teki5023ade2020-04-02 17:11:22 +0530131#endif
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200132
Eric Gao0f494072017-05-02 18:23:52 +0800133 case VOP_MODE_MIPI:
134 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
135 V_MIPI_OUT_EN(1));
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200136 break;
137
138 default:
139 debug("%s: unsupported output mode %x\n", __func__, mode);
Simon Glasse421bb82016-01-21 19:45:05 -0700140 }
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200141}
Simon Glasse421bb82016-01-21 19:45:05 -0700142
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200143static void rkvop_mode_set(struct udevice *dev,
144 const struct display_timing *edid,
145 enum vop_modes mode)
146{
147 struct rk_vop_priv *priv = dev_get_priv(dev);
148 struct rk3288_vop *regs = priv->regs;
149 struct rkvop_driverdata *data =
150 (struct rkvop_driverdata *)dev_get_driver_data(dev);
Jacob Chen0b6aee42016-03-14 11:20:18 +0800151
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200152 u32 hactive = edid->hactive.typ;
153 u32 vactive = edid->vactive.typ;
154 u32 hsync_len = edid->hsync_len.typ;
155 u32 hback_porch = edid->hback_porch.typ;
156 u32 vsync_len = edid->vsync_len.typ;
157 u32 vback_porch = edid->vback_porch.typ;
158 u32 hfront_porch = edid->hfront_porch.typ;
159 u32 vfront_porch = edid->vfront_porch.typ;
160 int mode_flags;
161 u32 pin_polarity;
162
163 pin_polarity = BIT(DCLK_INVERT);
164 if (edid->flags & DISPLAY_FLAGS_HSYNC_HIGH)
165 pin_polarity |= BIT(HSYNC_POSITIVE);
166 if (edid->flags & DISPLAY_FLAGS_VSYNC_HIGH)
167 pin_polarity |= BIT(VSYNC_POSITIVE);
168
169 rkvop_set_pin_polarity(dev, mode, pin_polarity);
170 rkvop_enable_output(dev, mode);
Simon Glasse421bb82016-01-21 19:45:05 -0700171
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200172 mode_flags = 0; /* RGB888 */
173 if ((data->features & VOP_FEATURE_OUTPUT_10BIT) &&
174 (mode == VOP_MODE_HDMI || mode == VOP_MODE_EDP))
175 mode_flags = 15; /* RGBaaa */
176
177 clrsetbits_le32(&regs->dsp_ctrl0, M_DSP_OUT_MODE,
178 V_DSP_OUT_MODE(mode_flags));
Simon Glasse421bb82016-01-21 19:45:05 -0700179
180 writel(V_HSYNC(hsync_len) |
181 V_HORPRD(hsync_len + hback_porch + hactive + hfront_porch),
182 &regs->dsp_htotal_hs_end);
183
184 writel(V_HEAP(hsync_len + hback_porch + hactive) |
185 V_HASP(hsync_len + hback_porch),
186 &regs->dsp_hact_st_end);
187
188 writel(V_VSYNC(vsync_len) |
189 V_VERPRD(vsync_len + vback_porch + vactive + vfront_porch),
190 &regs->dsp_vtotal_vs_end);
191
192 writel(V_VAEP(vsync_len + vback_porch + vactive)|
193 V_VASP(vsync_len + vback_porch),
194 &regs->dsp_vact_st_end);
195
196 writel(V_HEAP(hsync_len + hback_porch + hactive) |
197 V_HASP(hsync_len + hback_porch),
198 &regs->post_dsp_hact_info);
199
200 writel(V_VAEP(vsync_len + vback_porch + vactive)|
201 V_VASP(vsync_len + vback_porch),
202 &regs->post_dsp_vact_info);
203
204 writel(0x01, &regs->reg_cfg_done); /* enable reg config */
205}
206
207/**
208 * rk_display_init() - Try to enable the given display device
209 *
210 * This function performs many steps:
211 * - Finds the display device being referenced by @ep_node
212 * - Puts the VOP's ID into its uclass platform data
213 * - Probes the device to set it up
214 * - Reads the EDID timing information
215 * - Sets up the VOP clocks, etc. for the selected pixel clock and display mode
216 * - Enables the display (the display device handles this and will do different
217 * things depending on the display type)
218 * - Tells the uclass about the display resolution so that the console will
219 * appear correctly
220 *
221 * @dev: VOP device that we want to connect to the display
222 * @fbbase: Frame buffer address
Simon Glasse421bb82016-01-21 19:45:05 -0700223 * @ep_node: Device tree node to process - this is the offset of an endpoint
224 * node within the VOP's 'port' list.
225 * @return 0 if OK, -ve if something went wrong
226 */
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100227static int rk_display_init(struct udevice *dev, ulong fbbase, ofnode ep_node)
Simon Glasse421bb82016-01-21 19:45:05 -0700228{
229 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse421bb82016-01-21 19:45:05 -0700230 struct rk_vop_priv *priv = dev_get_priv(dev);
231 int vop_id, remote_vop_id;
232 struct rk3288_vop *regs = priv->regs;
233 struct display_timing timing;
234 struct udevice *disp;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100235 int ret;
236 u32 remote_phandle;
Simon Glasse421bb82016-01-21 19:45:05 -0700237 struct display_plat *disp_uc_plat;
Stephen Warrena9622432016-06-17 09:44:00 -0600238 struct clk clk;
Eric Gao58791c32017-05-02 18:23:53 +0800239 enum video_log2_bpp l2bpp;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100240 ofnode remote;
Arnaud Patard (Rtp)058ffd62021-03-05 11:27:46 +0100241 const char *compat;
Simon Glasse421bb82016-01-21 19:45:05 -0700242
Arnaud Patard (Rtp)6b81d6a2021-03-05 11:27:52 +0100243 debug("%s(%s, 0x%lx, %s)\n", __func__,
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100244 dev_read_name(dev), fbbase, ofnode_get_name(ep_node));
245
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100246 ret = ofnode_read_u32(ep_node, "remote-endpoint", &remote_phandle);
247 if (ret)
248 return ret;
249
250 remote = ofnode_get_by_phandle(remote_phandle);
251 if (!ofnode_valid(remote))
Simon Glasse421bb82016-01-21 19:45:05 -0700252 return -EINVAL;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100253 remote_vop_id = ofnode_read_u32_default(remote, "reg", -1);
Simon Glasse421bb82016-01-21 19:45:05 -0700254 debug("remote vop_id=%d\n", remote_vop_id);
255
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100256 /*
257 * The remote-endpoint references into a subnode of the encoder
258 * (i.e. HDMI, MIPI, etc.) with the DTS looking something like
259 * the following (assume 'hdmi_in_vopl' to be referenced):
260 *
261 * hdmi: hdmi@ff940000 {
262 * ports {
263 * hdmi_in: port {
264 * hdmi_in_vopb: endpoint@0 { ... };
265 * hdmi_in_vopl: endpoint@1 { ... };
266 * }
267 * }
268 * }
269 *
270 * The original code had 3 steps of "walking the parent", but
271 * a much better (as in: less likely to break if the DTS
272 * changes) way of doing this is to "find the enclosing device
273 * of UCLASS_DISPLAY".
274 */
275 while (ofnode_valid(remote)) {
276 remote = ofnode_get_parent(remote);
277 if (!ofnode_valid(remote)) {
278 debug("%s(%s): no UCLASS_DISPLAY for remote-endpoint\n",
279 __func__, dev_read_name(dev));
280 return -EINVAL;
281 }
Simon Glasse421bb82016-01-21 19:45:05 -0700282
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100283 uclass_find_device_by_ofnode(UCLASS_DISPLAY, remote, &disp);
284 if (disp)
285 break;
286 };
Arnaud Patard (Rtp)058ffd62021-03-05 11:27:46 +0100287 compat = ofnode_get_property(remote, "compatible", NULL);
288 if (!compat) {
289 debug("%s(%s): Failed to find compatible property\n",
290 __func__, dev_read_name(dev));
291 return -EINVAL;
292 }
293 if (strstr(compat, "edp")) {
294 vop_id = VOP_MODE_EDP;
295 } else if (strstr(compat, "mipi")) {
296 vop_id = VOP_MODE_MIPI;
297 } else if (strstr(compat, "hdmi")) {
298 vop_id = VOP_MODE_HDMI;
299 } else if (strstr(compat, "cdn-dp")) {
300 vop_id = VOP_MODE_DP;
301 } else if (strstr(compat, "lvds")) {
302 vop_id = VOP_MODE_LVDS;
303 } else {
304 debug("%s(%s): Failed to find vop mode for %s\n",
305 __func__, dev_read_name(dev), compat);
306 return -EINVAL;
307 }
308 debug("vop_id=%d\n", vop_id);
Simon Glasse421bb82016-01-21 19:45:05 -0700309
Simon Glass71fa5b42020-12-03 16:55:18 -0700310 disp_uc_plat = dev_get_uclass_plat(disp);
Simon Glasse421bb82016-01-21 19:45:05 -0700311 debug("Found device '%s', disp_uc_priv=%p\n", disp->name, disp_uc_plat);
Simon Glass86ad1b62016-11-13 14:22:08 -0700312 if (display_in_use(disp)) {
313 debug(" - device in use\n");
314 return -EBUSY;
315 }
316
Simon Glasse421bb82016-01-21 19:45:05 -0700317 disp_uc_plat->source_id = remote_vop_id;
318 disp_uc_plat->src_dev = dev;
319
320 ret = device_probe(disp);
321 if (ret) {
322 debug("%s: device '%s' display won't probe (ret=%d)\n",
323 __func__, dev->name, ret);
324 return ret;
325 }
326
327 ret = display_read_timing(disp, &timing);
328 if (ret) {
329 debug("%s: Failed to read timings\n", __func__);
330 return ret;
331 }
332
Simon Glass25891bc2016-11-13 14:21:56 -0700333 ret = clk_get_by_index(dev, 1, &clk);
Stephen Warrena9622432016-06-17 09:44:00 -0600334 if (!ret)
335 ret = clk_set_rate(&clk, timing.pixelclock.typ);
Eric Gao9ada0e62017-05-02 18:23:51 +0800336 if (IS_ERR_VALUE(ret)) {
Simon Glasse421bb82016-01-21 19:45:05 -0700337 debug("%s: Failed to set pixel clock: ret=%d\n", __func__, ret);
338 return ret;
339 }
340
Eric Gao58791c32017-05-02 18:23:53 +0800341 /* Set bitwidth for vop display according to vop mode */
342 switch (vop_id) {
343 case VOP_MODE_EDP:
Jagan Teki5023ade2020-04-02 17:11:22 +0530344#if defined(CONFIG_ROCKCHIP_RK3288)
Eric Gao58791c32017-05-02 18:23:53 +0800345 case VOP_MODE_LVDS:
Jagan Teki5023ade2020-04-02 17:11:22 +0530346#endif
Eric Gao58791c32017-05-02 18:23:53 +0800347 l2bpp = VIDEO_BPP16;
348 break;
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200349 case VOP_MODE_HDMI:
Eric Gao58791c32017-05-02 18:23:53 +0800350 case VOP_MODE_MIPI:
351 l2bpp = VIDEO_BPP32;
352 break;
353 default:
354 l2bpp = VIDEO_BPP16;
355 }
Simon Glasse421bb82016-01-21 19:45:05 -0700356
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200357 rkvop_mode_set(dev, &timing, vop_id);
Simon Glasse421bb82016-01-21 19:45:05 -0700358 rkvop_enable(regs, fbbase, 1 << l2bpp, &timing);
359
360 ret = display_enable(disp, 1 << l2bpp, &timing);
361 if (ret)
362 return ret;
363
364 uc_priv->xsize = timing.hactive.typ;
365 uc_priv->ysize = timing.vactive.typ;
366 uc_priv->bpix = l2bpp;
367 debug("fb=%lx, size=%d %d\n", fbbase, uc_priv->xsize, uc_priv->ysize);
368
369 return 0;
370}
371
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200372void rk_vop_probe_regulators(struct udevice *dev,
373 const char * const *names, int cnt)
374{
375 int i, ret;
376 const char *name;
377 struct udevice *reg;
378
379 for (i = 0; i < cnt; ++i) {
380 name = names[i];
381 debug("%s: probing regulator '%s'\n", dev->name, name);
382
383 ret = regulator_autoset_by_name(name, &reg);
384 if (!ret)
385 ret = regulator_set_enable(reg, true);
386 }
387}
388
389int rk_vop_probe(struct udevice *dev)
Simon Glasse421bb82016-01-21 19:45:05 -0700390{
Simon Glassb75b15b2020-12-03 16:55:23 -0700391 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glasse421bb82016-01-21 19:45:05 -0700392 struct rk_vop_priv *priv = dev_get_priv(dev);
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200393 int ret = 0;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100394 ofnode port, node;
Simon Glasse421bb82016-01-21 19:45:05 -0700395
396 /* Before relocation we don't need to do anything */
397 if (!(gd->flags & GD_FLG_RELOC))
398 return 0;
399
Arnaud Patard (Rtp)1af703c2021-03-05 11:27:49 +0100400#if defined(CONFIG_EFI_LOADER)
401 debug("Adding to EFI map %d @ %lx\n", plat->size, plat->base);
402 efi_add_memory_map(plat->base, plat->size, EFI_RESERVED_MEMORY_TYPE);
403#endif
404
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100405 priv->regs = (struct rk3288_vop *)dev_read_addr(dev);
Simon Glasse421bb82016-01-21 19:45:05 -0700406
Simon Glasse421bb82016-01-21 19:45:05 -0700407 /*
408 * Try all the ports until we find one that works. In practice this
409 * tries EDP first if available, then HDMI.
Simon Glass86ad1b62016-11-13 14:22:08 -0700410 *
411 * Note that rockchip_vop_set_clk() always uses NPLL as the source
412 * clock so it is currently not possible to use more than one display
413 * device simultaneously.
Simon Glasse421bb82016-01-21 19:45:05 -0700414 */
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100415 port = dev_read_subnode(dev, "port");
416 if (!ofnode_valid(port)) {
417 debug("%s(%s): 'port' subnode not found\n",
418 __func__, dev_read_name(dev));
Simon Glasse421bb82016-01-21 19:45:05 -0700419 return -EINVAL;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100420 }
421
422 for (node = ofnode_first_subnode(port);
423 ofnode_valid(node);
424 node = dev_read_next_subnode(node)) {
Eric Gao58791c32017-05-02 18:23:53 +0800425 ret = rk_display_init(dev, plat->base, node);
Simon Glasse421bb82016-01-21 19:45:05 -0700426 if (ret)
427 debug("Device failed: ret=%d\n", ret);
428 if (!ret)
429 break;
430 }
Simon Glass773ca822016-05-14 14:03:01 -0600431 video_set_flush_dcache(dev, 1);
Simon Glasse421bb82016-01-21 19:45:05 -0700432
433 return ret;
434}
435
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200436int rk_vop_bind(struct udevice *dev)
Simon Glasse421bb82016-01-21 19:45:05 -0700437{
Simon Glassb75b15b2020-12-03 16:55:23 -0700438 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glasse421bb82016-01-21 19:45:05 -0700439
Philipp Tomsichd3a58262017-05-31 17:59:29 +0200440 plat->size = 4 * (CONFIG_VIDEO_ROCKCHIP_MAX_XRES *
441 CONFIG_VIDEO_ROCKCHIP_MAX_YRES);
Simon Glasse421bb82016-01-21 19:45:05 -0700442
443 return 0;
444}