blob: 45385b175b8b364819db48f7507333dd31f3539f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// 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>
16#include <asm/gpio.h>
Simon Glasse421bb82016-01-21 19:45:05 -070017#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080018#include <asm/arch-rockchip/clock.h>
19#include <asm/arch-rockchip/edp_rk3288.h>
20#include <asm/arch-rockchip/vop_rk3288.h>
Simon Glasse421bb82016-01-21 19:45:05 -070021#include <dm/device-internal.h>
22#include <dm/uclass-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070023#include <linux/err.h>
Simon Glasse421bb82016-01-21 19:45:05 -070024#include <power/regulator.h>
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020025#include "rk_vop.h"
Simon Glasse421bb82016-01-21 19:45:05 -070026
27DECLARE_GLOBAL_DATA_PTR;
28
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020029enum vop_pol {
30 HSYNC_POSITIVE = 0,
31 VSYNC_POSITIVE = 1,
32 DEN_NEGATIVE = 2,
33 DCLK_INVERT = 3
Simon Glasse421bb82016-01-21 19:45:05 -070034};
35
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020036static void rkvop_enable(struct rk3288_vop *regs, ulong fbbase,
37 int fb_bits_per_pixel,
38 const struct display_timing *edid)
Simon Glasse421bb82016-01-21 19:45:05 -070039{
40 u32 lb_mode;
41 u32 rgb_mode;
42 u32 hactive = edid->hactive.typ;
43 u32 vactive = edid->vactive.typ;
44
45 writel(V_ACT_WIDTH(hactive - 1) | V_ACT_HEIGHT(vactive - 1),
46 &regs->win0_act_info);
47
48 writel(V_DSP_XST(edid->hsync_len.typ + edid->hback_porch.typ) |
49 V_DSP_YST(edid->vsync_len.typ + edid->vback_porch.typ),
50 &regs->win0_dsp_st);
51
52 writel(V_DSP_WIDTH(hactive - 1) |
53 V_DSP_HEIGHT(vactive - 1),
54 &regs->win0_dsp_info);
55
56 clrsetbits_le32(&regs->win0_color_key, M_WIN0_KEY_EN | M_WIN0_KEY_COLOR,
57 V_WIN0_KEY_EN(0) | V_WIN0_KEY_COLOR(0));
58
59 switch (fb_bits_per_pixel) {
60 case 16:
61 rgb_mode = RGB565;
62 writel(V_RGB565_VIRWIDTH(hactive), &regs->win0_vir);
63 break;
64 case 24:
65 rgb_mode = RGB888;
66 writel(V_RGB888_VIRWIDTH(hactive), &regs->win0_vir);
67 break;
68 case 32:
69 default:
70 rgb_mode = ARGB8888;
71 writel(V_ARGB888_VIRWIDTH(hactive), &regs->win0_vir);
72 break;
73 }
74
75 if (hactive > 2560)
76 lb_mode = LB_RGB_3840X2;
77 else if (hactive > 1920)
78 lb_mode = LB_RGB_2560X4;
79 else if (hactive > 1280)
80 lb_mode = LB_RGB_1920X5;
81 else
82 lb_mode = LB_RGB_1280X8;
83
84 clrsetbits_le32(&regs->win0_ctrl0,
85 M_WIN0_LB_MODE | M_WIN0_DATA_FMT | M_WIN0_EN,
86 V_WIN0_LB_MODE(lb_mode) | V_WIN0_DATA_FMT(rgb_mode) |
87 V_WIN0_EN(1));
88
89 writel(fbbase, &regs->win0_yrgb_mst);
90 writel(0x01, &regs->reg_cfg_done); /* enable reg config */
91}
92
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020093static void rkvop_set_pin_polarity(struct udevice *dev,
94 enum vop_modes mode, u32 polarity)
Simon Glasse421bb82016-01-21 19:45:05 -070095{
Philipp Tomsicha354c2d2017-05-31 17:59:30 +020096 struct rkvop_driverdata *ops =
97 (struct rkvop_driverdata *)dev_get_driver_data(dev);
98
99 if (ops->set_pin_polarity)
100 ops->set_pin_polarity(dev, mode, polarity);
101}
102
103static void rkvop_enable_output(struct udevice *dev, enum vop_modes mode)
104{
105 struct rk_vop_priv *priv = dev_get_priv(dev);
106 struct rk3288_vop *regs = priv->regs;
Simon Glasse421bb82016-01-21 19:45:05 -0700107
Simon Glassd7429502017-05-31 17:57:29 -0600108 /* remove from standby */
109 clrbits_le32(&regs->sys_ctrl, V_STANDBY_EN(1));
110
Simon Glasse421bb82016-01-21 19:45:05 -0700111 switch (mode) {
112 case VOP_MODE_HDMI:
113 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
114 V_HDMI_OUT_EN(1));
115 break;
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200116
Simon Glasse421bb82016-01-21 19:45:05 -0700117 case VOP_MODE_EDP:
Simon Glasse421bb82016-01-21 19:45:05 -0700118 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
119 V_EDP_OUT_EN(1));
120 break;
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200121
Jagan Teki5023ade2020-04-02 17:11:22 +0530122#if defined(CONFIG_ROCKCHIP_RK3288)
Jacob Chen0b6aee42016-03-14 11:20:18 +0800123 case VOP_MODE_LVDS:
124 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
125 V_RGB_OUT_EN(1));
126 break;
Jagan Teki5023ade2020-04-02 17:11:22 +0530127#endif
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200128
Eric Gao0f494072017-05-02 18:23:52 +0800129 case VOP_MODE_MIPI:
130 clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
131 V_MIPI_OUT_EN(1));
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200132 break;
133
134 default:
135 debug("%s: unsupported output mode %x\n", __func__, mode);
Simon Glasse421bb82016-01-21 19:45:05 -0700136 }
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200137}
Simon Glasse421bb82016-01-21 19:45:05 -0700138
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200139static void rkvop_mode_set(struct udevice *dev,
140 const struct display_timing *edid,
141 enum vop_modes mode)
142{
143 struct rk_vop_priv *priv = dev_get_priv(dev);
144 struct rk3288_vop *regs = priv->regs;
145 struct rkvop_driverdata *data =
146 (struct rkvop_driverdata *)dev_get_driver_data(dev);
Jacob Chen0b6aee42016-03-14 11:20:18 +0800147
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200148 u32 hactive = edid->hactive.typ;
149 u32 vactive = edid->vactive.typ;
150 u32 hsync_len = edid->hsync_len.typ;
151 u32 hback_porch = edid->hback_porch.typ;
152 u32 vsync_len = edid->vsync_len.typ;
153 u32 vback_porch = edid->vback_porch.typ;
154 u32 hfront_porch = edid->hfront_porch.typ;
155 u32 vfront_porch = edid->vfront_porch.typ;
156 int mode_flags;
157 u32 pin_polarity;
158
159 pin_polarity = BIT(DCLK_INVERT);
160 if (edid->flags & DISPLAY_FLAGS_HSYNC_HIGH)
161 pin_polarity |= BIT(HSYNC_POSITIVE);
162 if (edid->flags & DISPLAY_FLAGS_VSYNC_HIGH)
163 pin_polarity |= BIT(VSYNC_POSITIVE);
164
165 rkvop_set_pin_polarity(dev, mode, pin_polarity);
166 rkvop_enable_output(dev, mode);
Simon Glasse421bb82016-01-21 19:45:05 -0700167
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200168 mode_flags = 0; /* RGB888 */
169 if ((data->features & VOP_FEATURE_OUTPUT_10BIT) &&
170 (mode == VOP_MODE_HDMI || mode == VOP_MODE_EDP))
171 mode_flags = 15; /* RGBaaa */
172
173 clrsetbits_le32(&regs->dsp_ctrl0, M_DSP_OUT_MODE,
174 V_DSP_OUT_MODE(mode_flags));
Simon Glasse421bb82016-01-21 19:45:05 -0700175
176 writel(V_HSYNC(hsync_len) |
177 V_HORPRD(hsync_len + hback_porch + hactive + hfront_porch),
178 &regs->dsp_htotal_hs_end);
179
180 writel(V_HEAP(hsync_len + hback_porch + hactive) |
181 V_HASP(hsync_len + hback_porch),
182 &regs->dsp_hact_st_end);
183
184 writel(V_VSYNC(vsync_len) |
185 V_VERPRD(vsync_len + vback_porch + vactive + vfront_porch),
186 &regs->dsp_vtotal_vs_end);
187
188 writel(V_VAEP(vsync_len + vback_porch + vactive)|
189 V_VASP(vsync_len + vback_porch),
190 &regs->dsp_vact_st_end);
191
192 writel(V_HEAP(hsync_len + hback_porch + hactive) |
193 V_HASP(hsync_len + hback_porch),
194 &regs->post_dsp_hact_info);
195
196 writel(V_VAEP(vsync_len + vback_porch + vactive)|
197 V_VASP(vsync_len + vback_porch),
198 &regs->post_dsp_vact_info);
199
200 writel(0x01, &regs->reg_cfg_done); /* enable reg config */
201}
202
203/**
204 * rk_display_init() - Try to enable the given display device
205 *
206 * This function performs many steps:
207 * - Finds the display device being referenced by @ep_node
208 * - Puts the VOP's ID into its uclass platform data
209 * - Probes the device to set it up
210 * - Reads the EDID timing information
211 * - Sets up the VOP clocks, etc. for the selected pixel clock and display mode
212 * - Enables the display (the display device handles this and will do different
213 * things depending on the display type)
214 * - Tells the uclass about the display resolution so that the console will
215 * appear correctly
216 *
217 * @dev: VOP device that we want to connect to the display
218 * @fbbase: Frame buffer address
Simon Glasse421bb82016-01-21 19:45:05 -0700219 * @ep_node: Device tree node to process - this is the offset of an endpoint
220 * node within the VOP's 'port' list.
221 * @return 0 if OK, -ve if something went wrong
222 */
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100223static int rk_display_init(struct udevice *dev, ulong fbbase, ofnode ep_node)
Simon Glasse421bb82016-01-21 19:45:05 -0700224{
225 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glasse421bb82016-01-21 19:45:05 -0700226 struct rk_vop_priv *priv = dev_get_priv(dev);
227 int vop_id, remote_vop_id;
228 struct rk3288_vop *regs = priv->regs;
229 struct display_timing timing;
230 struct udevice *disp;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100231 int ret;
232 u32 remote_phandle;
Simon Glasse421bb82016-01-21 19:45:05 -0700233 struct display_plat *disp_uc_plat;
Stephen Warrena9622432016-06-17 09:44:00 -0600234 struct clk clk;
Eric Gao58791c32017-05-02 18:23:53 +0800235 enum video_log2_bpp l2bpp;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100236 ofnode remote;
Simon Glasse421bb82016-01-21 19:45:05 -0700237
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100238 debug("%s(%s, %lu, %s)\n", __func__,
239 dev_read_name(dev), fbbase, ofnode_get_name(ep_node));
240
241 vop_id = ofnode_read_s32_default(ep_node, "reg", -1);
Simon Glasse421bb82016-01-21 19:45:05 -0700242 debug("vop_id=%d\n", vop_id);
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100243 ret = ofnode_read_u32(ep_node, "remote-endpoint", &remote_phandle);
244 if (ret)
245 return ret;
246
247 remote = ofnode_get_by_phandle(remote_phandle);
248 if (!ofnode_valid(remote))
Simon Glasse421bb82016-01-21 19:45:05 -0700249 return -EINVAL;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100250 remote_vop_id = ofnode_read_u32_default(remote, "reg", -1);
Simon Glasse421bb82016-01-21 19:45:05 -0700251 debug("remote vop_id=%d\n", remote_vop_id);
252
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100253 /*
254 * The remote-endpoint references into a subnode of the encoder
255 * (i.e. HDMI, MIPI, etc.) with the DTS looking something like
256 * the following (assume 'hdmi_in_vopl' to be referenced):
257 *
258 * hdmi: hdmi@ff940000 {
259 * ports {
260 * hdmi_in: port {
261 * hdmi_in_vopb: endpoint@0 { ... };
262 * hdmi_in_vopl: endpoint@1 { ... };
263 * }
264 * }
265 * }
266 *
267 * The original code had 3 steps of "walking the parent", but
268 * a much better (as in: less likely to break if the DTS
269 * changes) way of doing this is to "find the enclosing device
270 * of UCLASS_DISPLAY".
271 */
272 while (ofnode_valid(remote)) {
273 remote = ofnode_get_parent(remote);
274 if (!ofnode_valid(remote)) {
275 debug("%s(%s): no UCLASS_DISPLAY for remote-endpoint\n",
276 __func__, dev_read_name(dev));
277 return -EINVAL;
278 }
Simon Glasse421bb82016-01-21 19:45:05 -0700279
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100280 uclass_find_device_by_ofnode(UCLASS_DISPLAY, remote, &disp);
281 if (disp)
282 break;
283 };
Simon Glasse421bb82016-01-21 19:45:05 -0700284
285 disp_uc_plat = dev_get_uclass_platdata(disp);
286 debug("Found device '%s', disp_uc_priv=%p\n", disp->name, disp_uc_plat);
Simon Glass86ad1b62016-11-13 14:22:08 -0700287 if (display_in_use(disp)) {
288 debug(" - device in use\n");
289 return -EBUSY;
290 }
291
Simon Glasse421bb82016-01-21 19:45:05 -0700292 disp_uc_plat->source_id = remote_vop_id;
293 disp_uc_plat->src_dev = dev;
294
295 ret = device_probe(disp);
296 if (ret) {
297 debug("%s: device '%s' display won't probe (ret=%d)\n",
298 __func__, dev->name, ret);
299 return ret;
300 }
301
302 ret = display_read_timing(disp, &timing);
303 if (ret) {
304 debug("%s: Failed to read timings\n", __func__);
305 return ret;
306 }
307
Simon Glass25891bc2016-11-13 14:21:56 -0700308 ret = clk_get_by_index(dev, 1, &clk);
Stephen Warrena9622432016-06-17 09:44:00 -0600309 if (!ret)
310 ret = clk_set_rate(&clk, timing.pixelclock.typ);
Eric Gao9ada0e62017-05-02 18:23:51 +0800311 if (IS_ERR_VALUE(ret)) {
Simon Glasse421bb82016-01-21 19:45:05 -0700312 debug("%s: Failed to set pixel clock: ret=%d\n", __func__, ret);
313 return ret;
314 }
315
Eric Gao58791c32017-05-02 18:23:53 +0800316 /* Set bitwidth for vop display according to vop mode */
317 switch (vop_id) {
318 case VOP_MODE_EDP:
Jagan Teki5023ade2020-04-02 17:11:22 +0530319#if defined(CONFIG_ROCKCHIP_RK3288)
Eric Gao58791c32017-05-02 18:23:53 +0800320 case VOP_MODE_LVDS:
Jagan Teki5023ade2020-04-02 17:11:22 +0530321#endif
Eric Gao58791c32017-05-02 18:23:53 +0800322 l2bpp = VIDEO_BPP16;
323 break;
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200324 case VOP_MODE_HDMI:
Eric Gao58791c32017-05-02 18:23:53 +0800325 case VOP_MODE_MIPI:
326 l2bpp = VIDEO_BPP32;
327 break;
328 default:
329 l2bpp = VIDEO_BPP16;
330 }
Simon Glasse421bb82016-01-21 19:45:05 -0700331
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200332 rkvop_mode_set(dev, &timing, vop_id);
Simon Glasse421bb82016-01-21 19:45:05 -0700333 rkvop_enable(regs, fbbase, 1 << l2bpp, &timing);
334
335 ret = display_enable(disp, 1 << l2bpp, &timing);
336 if (ret)
337 return ret;
338
339 uc_priv->xsize = timing.hactive.typ;
340 uc_priv->ysize = timing.vactive.typ;
341 uc_priv->bpix = l2bpp;
342 debug("fb=%lx, size=%d %d\n", fbbase, uc_priv->xsize, uc_priv->ysize);
343
344 return 0;
345}
346
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200347void rk_vop_probe_regulators(struct udevice *dev,
348 const char * const *names, int cnt)
349{
350 int i, ret;
351 const char *name;
352 struct udevice *reg;
353
354 for (i = 0; i < cnt; ++i) {
355 name = names[i];
356 debug("%s: probing regulator '%s'\n", dev->name, name);
357
358 ret = regulator_autoset_by_name(name, &reg);
359 if (!ret)
360 ret = regulator_set_enable(reg, true);
361 }
362}
363
364int rk_vop_probe(struct udevice *dev)
Simon Glasse421bb82016-01-21 19:45:05 -0700365{
366 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
Simon Glasse421bb82016-01-21 19:45:05 -0700367 struct rk_vop_priv *priv = dev_get_priv(dev);
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200368 int ret = 0;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100369 ofnode port, node;
Simon Glasse421bb82016-01-21 19:45:05 -0700370
371 /* Before relocation we don't need to do anything */
372 if (!(gd->flags & GD_FLG_RELOC))
373 return 0;
374
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100375 priv->regs = (struct rk3288_vop *)dev_read_addr(dev);
Simon Glasse421bb82016-01-21 19:45:05 -0700376
Simon Glasse421bb82016-01-21 19:45:05 -0700377 /*
378 * Try all the ports until we find one that works. In practice this
379 * tries EDP first if available, then HDMI.
Simon Glass86ad1b62016-11-13 14:22:08 -0700380 *
381 * Note that rockchip_vop_set_clk() always uses NPLL as the source
382 * clock so it is currently not possible to use more than one display
383 * device simultaneously.
Simon Glasse421bb82016-01-21 19:45:05 -0700384 */
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100385 port = dev_read_subnode(dev, "port");
386 if (!ofnode_valid(port)) {
387 debug("%s(%s): 'port' subnode not found\n",
388 __func__, dev_read_name(dev));
Simon Glasse421bb82016-01-21 19:45:05 -0700389 return -EINVAL;
Philipp Tomsich13b016d2018-02-23 17:38:52 +0100390 }
391
392 for (node = ofnode_first_subnode(port);
393 ofnode_valid(node);
394 node = dev_read_next_subnode(node)) {
Eric Gao58791c32017-05-02 18:23:53 +0800395 ret = rk_display_init(dev, plat->base, node);
Simon Glasse421bb82016-01-21 19:45:05 -0700396 if (ret)
397 debug("Device failed: ret=%d\n", ret);
398 if (!ret)
399 break;
400 }
Simon Glass773ca822016-05-14 14:03:01 -0600401 video_set_flush_dcache(dev, 1);
Simon Glasse421bb82016-01-21 19:45:05 -0700402
403 return ret;
404}
405
Philipp Tomsicha354c2d2017-05-31 17:59:30 +0200406int rk_vop_bind(struct udevice *dev)
Simon Glasse421bb82016-01-21 19:45:05 -0700407{
408 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
409
Philipp Tomsichd3a58262017-05-31 17:59:29 +0200410 plat->size = 4 * (CONFIG_VIDEO_ROCKCHIP_MAX_XRES *
411 CONFIG_VIDEO_ROCKCHIP_MAX_YRES);
Simon Glasse421bb82016-01-21 19:45:05 -0700412
413 return 0;
414}