1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 Google, Inc
4 * Copyright 2014 Rockchip Inc.
10 #include <dm/device_compat.h>
17 #include <asm/global_data.h>
20 #include <asm/arch-rockchip/clock.h>
21 #include <asm/arch-rockchip/edp_rk3288.h>
22 #include <asm/arch-rockchip/vop_rk3288.h>
23 #include <dm/device-internal.h>
24 #include <dm/uclass-internal.h>
26 #include <efi_loader.h>
27 #include <linux/bitops.h>
28 #include <linux/err.h>
29 #include <power/regulator.h>
32 DECLARE_GLOBAL_DATA_PTR;
41 static void rkvop_enable(struct udevice *dev, ulong fbbase,
42 int fb_bits_per_pixel,
43 const struct display_timing *edid,
44 struct reset_ctl *dclk_rst)
46 struct rk_vop_priv *priv = dev_get_priv(dev);
47 struct rk3288_vop *regs = priv->regs;
48 struct rk3288_vop *win_regs = priv->regs + priv->win_offset;
51 u32 hactive = edid->hactive.typ;
52 u32 vactive = edid->vactive.typ;
55 writel(V_ACT_WIDTH(hactive - 1) | V_ACT_HEIGHT(vactive - 1),
56 &win_regs->win0_act_info);
58 writel(V_DSP_XST(edid->hsync_len.typ + edid->hback_porch.typ) |
59 V_DSP_YST(edid->vsync_len.typ + edid->vback_porch.typ),
60 &win_regs->win0_dsp_st);
62 writel(V_DSP_WIDTH(hactive - 1) |
63 V_DSP_HEIGHT(vactive - 1),
64 &win_regs->win0_dsp_info);
66 clrsetbits_le32(&win_regs->win0_color_key, M_WIN0_KEY_EN | M_WIN0_KEY_COLOR,
67 V_WIN0_KEY_EN(0) | V_WIN0_KEY_COLOR(0));
69 switch (fb_bits_per_pixel) {
72 writel(V_RGB565_VIRWIDTH(hactive), &win_regs->win0_vir);
76 writel(V_RGB888_VIRWIDTH(hactive), &win_regs->win0_vir);
81 writel(V_ARGB888_VIRWIDTH(hactive), &win_regs->win0_vir);
86 lb_mode = LB_RGB_3840X2;
87 else if (hactive > 1920)
88 lb_mode = LB_RGB_2560X4;
89 else if (hactive > 1280)
90 lb_mode = LB_RGB_1920X5;
92 lb_mode = LB_RGB_1280X8;
94 clrsetbits_le32(&win_regs->win0_ctrl0,
95 M_WIN0_LB_MODE | M_WIN0_DATA_FMT | M_WIN0_EN,
96 V_WIN0_LB_MODE(lb_mode) | V_WIN0_DATA_FMT(rgb_mode) |
99 writel(fbbase, &win_regs->win0_yrgb_mst);
100 writel(0x01, ®s->reg_cfg_done); /* enable reg config */
102 ret = reset_assert(dclk_rst);
104 dev_warn(dev, "failed to assert dclk reset (ret=%d)\n", ret);
109 ret = reset_deassert(dclk_rst);
111 dev_warn(dev, "failed to deassert dclk reset (ret=%d)\n", ret);
115 static void rkvop_set_pin_polarity(struct udevice *dev,
116 enum vop_modes mode, u32 polarity)
118 struct rkvop_driverdata *ops =
119 (struct rkvop_driverdata *)dev_get_driver_data(dev);
121 if (ops->set_pin_polarity)
122 ops->set_pin_polarity(dev, mode, polarity);
125 static void rkvop_enable_output(struct udevice *dev, enum vop_modes mode)
127 struct rk_vop_priv *priv = dev_get_priv(dev);
128 struct rk3288_vop *regs = priv->regs;
130 /* remove from standby */
131 clrbits_le32(®s->sys_ctrl, V_STANDBY_EN(1));
135 clrsetbits_le32(®s->sys_ctrl, M_ALL_OUT_EN,
140 clrsetbits_le32(®s->sys_ctrl, M_ALL_OUT_EN,
144 #if defined(CONFIG_ROCKCHIP_RK3288)
146 clrsetbits_le32(®s->sys_ctrl, M_ALL_OUT_EN,
152 clrsetbits_le32(®s->sys_ctrl, M_ALL_OUT_EN,
157 debug("%s: unsupported output mode %x\n", __func__, mode);
161 static void rkvop_mode_set(struct udevice *dev,
162 const struct display_timing *edid,
165 struct rk_vop_priv *priv = dev_get_priv(dev);
166 struct rk3288_vop *regs = priv->regs;
167 struct rk3288_vop *dsp_regs = priv->regs + priv->dsp_offset;
168 struct rkvop_driverdata *data =
169 (struct rkvop_driverdata *)dev_get_driver_data(dev);
171 u32 hactive = edid->hactive.typ;
172 u32 vactive = edid->vactive.typ;
173 u32 hsync_len = edid->hsync_len.typ;
174 u32 hback_porch = edid->hback_porch.typ;
175 u32 vsync_len = edid->vsync_len.typ;
176 u32 vback_porch = edid->vback_porch.typ;
177 u32 hfront_porch = edid->hfront_porch.typ;
178 u32 vfront_porch = edid->vfront_porch.typ;
182 pin_polarity = BIT(DCLK_INVERT);
183 if (edid->flags & DISPLAY_FLAGS_HSYNC_HIGH)
184 pin_polarity |= BIT(HSYNC_POSITIVE);
185 if (edid->flags & DISPLAY_FLAGS_VSYNC_HIGH)
186 pin_polarity |= BIT(VSYNC_POSITIVE);
188 rkvop_set_pin_polarity(dev, mode, pin_polarity);
189 rkvop_enable_output(dev, mode);
191 mode_flags = 0; /* RGB888 */
192 if ((data->features & VOP_FEATURE_OUTPUT_10BIT) &&
193 (mode == VOP_MODE_HDMI || mode == VOP_MODE_EDP))
194 mode_flags = 15; /* RGBaaa */
196 clrsetbits_le32(®s->dsp_ctrl0, M_DSP_OUT_MODE,
197 V_DSP_OUT_MODE(mode_flags));
199 writel(V_HSYNC(hsync_len) |
200 V_HORPRD(hsync_len + hback_porch + hactive + hfront_porch),
201 &dsp_regs->dsp_htotal_hs_end);
203 writel(V_HEAP(hsync_len + hback_porch + hactive) |
204 V_HASP(hsync_len + hback_porch),
205 &dsp_regs->dsp_hact_st_end);
207 writel(V_VSYNC(vsync_len) |
208 V_VERPRD(vsync_len + vback_porch + vactive + vfront_porch),
209 &dsp_regs->dsp_vtotal_vs_end);
211 writel(V_VAEP(vsync_len + vback_porch + vactive)|
212 V_VASP(vsync_len + vback_porch),
213 &dsp_regs->dsp_vact_st_end);
215 writel(V_HEAP(hsync_len + hback_porch + hactive) |
216 V_HASP(hsync_len + hback_porch),
217 &dsp_regs->post_dsp_hact_info);
219 writel(V_VAEP(vsync_len + vback_porch + vactive)|
220 V_VASP(vsync_len + vback_porch),
221 &dsp_regs->post_dsp_vact_info);
223 writel(0x01, ®s->reg_cfg_done); /* enable reg config */
227 * rk_display_init() - Try to enable the given display device
229 * This function performs many steps:
230 * - Finds the display device being referenced by @ep_node
231 * - Puts the VOP's ID into its uclass platform data
232 * - Probes the device to set it up
233 * - Reads the EDID timing information
234 * - Sets up the VOP clocks, etc. for the selected pixel clock and display mode
235 * - Enables the display (the display device handles this and will do different
236 * things depending on the display type)
237 * - Tells the uclass about the display resolution so that the console will
240 * @dev: VOP device that we want to connect to the display
241 * @fbbase: Frame buffer address
242 * @ep_node: Device tree node to process - this is the offset of an endpoint
243 * node within the VOP's 'port' list.
244 * Return: 0 if OK, -ve if something went wrong
246 static int rk_display_init(struct udevice *dev, ulong fbbase, ofnode ep_node)
248 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
249 int vop_id, remote_vop_id;
250 struct display_timing timing;
251 struct udevice *disp;
254 struct display_plat *disp_uc_plat;
256 enum video_log2_bpp l2bpp;
259 struct reset_ctl dclk_rst;
261 debug("%s(%s, 0x%lx, %s)\n", __func__,
262 dev_read_name(dev), fbbase, ofnode_get_name(ep_node));
264 ret = ofnode_read_u32(ep_node, "remote-endpoint", &remote_phandle);
268 remote = ofnode_get_by_phandle(remote_phandle);
269 if (!ofnode_valid(remote))
271 remote_vop_id = ofnode_read_u32_default(remote, "reg", -1);
272 debug("remote vop_id=%d\n", remote_vop_id);
275 * The remote-endpoint references into a subnode of the encoder
276 * (i.e. HDMI, MIPI, etc.) with the DTS looking something like
277 * the following (assume 'hdmi_in_vopl' to be referenced):
279 * hdmi: hdmi@ff940000 {
282 * hdmi_in_vopb: endpoint@0 { ... };
283 * hdmi_in_vopl: endpoint@1 { ... };
288 * The original code had 3 steps of "walking the parent", but
289 * a much better (as in: less likely to break if the DTS
290 * changes) way of doing this is to "find the enclosing device
291 * of UCLASS_DISPLAY".
293 while (ofnode_valid(remote)) {
294 remote = ofnode_get_parent(remote);
295 if (!ofnode_valid(remote)) {
296 debug("%s(%s): no UCLASS_DISPLAY for remote-endpoint\n",
297 __func__, dev_read_name(dev));
301 uclass_find_device_by_ofnode(UCLASS_DISPLAY, remote, &disp);
305 compat = ofnode_get_property(remote, "compatible", NULL);
307 debug("%s(%s): Failed to find compatible property\n",
308 __func__, dev_read_name(dev));
311 if (strstr(compat, "edp") ||
312 strstr(compat, "rk3288-dp")) {
313 vop_id = VOP_MODE_EDP;
314 } else if (strstr(compat, "mipi")) {
315 vop_id = VOP_MODE_MIPI;
316 } else if (strstr(compat, "hdmi")) {
317 vop_id = VOP_MODE_HDMI;
318 } else if (strstr(compat, "cdn-dp")) {
319 vop_id = VOP_MODE_DP;
320 } else if (strstr(compat, "lvds")) {
321 vop_id = VOP_MODE_LVDS;
323 debug("%s(%s): Failed to find vop mode for %s\n",
324 __func__, dev_read_name(dev), compat);
327 debug("vop_id=%d\n", vop_id);
329 disp_uc_plat = dev_get_uclass_plat(disp);
330 debug("Found device '%s', disp_uc_priv=%p\n", disp->name, disp_uc_plat);
331 if (display_in_use(disp)) {
332 debug(" - device in use\n");
336 disp_uc_plat->source_id = remote_vop_id;
337 disp_uc_plat->src_dev = dev;
339 ret = device_probe(disp);
341 debug("%s: device '%s' display won't probe (ret=%d)\n",
342 __func__, dev->name, ret);
346 ret = display_read_timing(disp, &timing);
348 debug("%s: Failed to read timings\n", __func__);
352 ret = clk_get_by_index(dev, 1, &clk);
354 ret = clk_set_rate(&clk, timing.pixelclock.typ);
355 if (IS_ERR_VALUE(ret)) {
356 debug("%s: Failed to set pixel clock: ret=%d\n", __func__, ret);
360 /* Set bitwidth for vop display according to vop mode */
363 #if defined(CONFIG_ROCKCHIP_RK3288)
376 rkvop_mode_set(dev, &timing, vop_id);
378 ret = reset_get_by_name(dev, "dclk", &dclk_rst);
380 dev_err(dev, "failed to get dclk reset (ret=%d)\n", ret);
384 rkvop_enable(dev, fbbase, 1 << l2bpp, &timing, &dclk_rst);
386 ret = display_enable(disp, 1 << l2bpp, &timing);
390 uc_priv->xsize = timing.hactive.typ;
391 uc_priv->ysize = timing.vactive.typ;
392 uc_priv->bpix = l2bpp;
393 debug("fb=%lx, size=%d %d\n", fbbase, uc_priv->xsize, uc_priv->ysize);
398 void rk_vop_probe_regulators(struct udevice *dev,
399 const char * const *names, int cnt)
405 for (i = 0; i < cnt; ++i) {
407 debug("%s: probing regulator '%s'\n", dev->name, name);
409 ret = regulator_autoset_by_name(name, ®);
411 ret = regulator_set_enable(reg, true);
415 int rk_vop_probe(struct udevice *dev)
417 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
418 struct rk_vop_priv *priv = dev_get_priv(dev);
419 struct rkvop_driverdata *ops =
420 (struct rkvop_driverdata *)dev_get_driver_data(dev);
423 struct reset_ctl ahb_rst;
425 /* Before relocation we don't need to do anything */
426 if (!(gd->flags & GD_FLG_RELOC))
429 ret = reset_get_by_name(dev, "ahb", &ahb_rst);
431 dev_err(dev, "failed to get ahb reset (ret=%d)\n", ret);
435 ret = reset_assert(&ahb_rst);
437 dev_err(dev, "failed to assert ahb reset (ret=%d)\n", ret);
442 ret = reset_deassert(&ahb_rst);
444 dev_err(dev, "failed to deassert ahb reset (ret=%d)\n", ret);
448 #if defined(CONFIG_EFI_LOADER)
449 debug("Adding to EFI map %d @ %lx\n", plat->size, plat->base);
450 efi_add_memory_map(plat->base, plat->size, EFI_RESERVED_MEMORY_TYPE);
453 priv->regs = dev_read_addr_ptr(dev);
454 priv->win_offset = ops->win_offset;
455 priv->dsp_offset = ops->dsp_offset;
458 * Try all the ports until we find one that works. In practice this
459 * tries EDP first if available, then HDMI.
461 * Note that rockchip_vop_set_clk() always uses NPLL as the source
462 * clock so it is currently not possible to use more than one display
463 * device simultaneously.
465 port = dev_read_subnode(dev, "port");
466 if (!ofnode_valid(port)) {
467 debug("%s(%s): 'port' subnode not found\n",
468 __func__, dev_read_name(dev));
472 for (node = ofnode_first_subnode(port);
474 node = dev_read_next_subnode(node)) {
475 ret = rk_display_init(dev, plat->base, node);
477 debug("Device failed: ret=%d\n", ret);
481 video_set_flush_dcache(dev, 1);
486 int rk_vop_bind(struct udevice *dev)
488 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
490 plat->size = 4 * (CONFIG_VIDEO_ROCKCHIP_MAX_XRES *
491 CONFIG_VIDEO_ROCKCHIP_MAX_YRES);