xref: /rk3399_rockchip-uboot/drivers/video/drm/rockchip_display.c (revision c2ba77d93f696c0ccb8f2b653571104e7b4afb4e)
1 /*
2  * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <asm/unaligned.h>
8 #include <config.h>
9 #include <common.h>
10 #include <errno.h>
11 #include <linux/libfdt.h>
12 #include <fdtdec.h>
13 #include <fdt_support.h>
14 #include <linux/hdmi.h>
15 #include <linux/list.h>
16 #include <linux/compat.h>
17 #include <linux/media-bus-format.h>
18 #include <malloc.h>
19 #include <video.h>
20 #include <video_rockchip.h>
21 #include <video_bridge.h>
22 #include <dm/device.h>
23 #include <dm/uclass-internal.h>
24 #include <asm/arch-rockchip/resource_img.h>
25 
26 #include "bmp_helper.h"
27 #include "rockchip_display.h"
28 #include "rockchip_crtc.h"
29 #include "rockchip_connector.h"
30 #include "rockchip_bridge.h"
31 #include "rockchip_phy.h"
32 #include "rockchip_panel.h"
33 #include <dm.h>
34 #include <dm/of_access.h>
35 #include <dm/ofnode.h>
36 
37 #define DRIVER_VERSION	"v1.0.1"
38 
39 /***********************************************************************
40  *  Rockchip UBOOT DRM driver version
41  *
42  *  v1.0.0	: add basic version for rockchip drm driver(hjc)
43  *  v1.0.1	: add much dsi update(hjc)
44  *
45  **********************************************************************/
46 
47 #define RK_BLK_SIZE 512
48 
49 DECLARE_GLOBAL_DATA_PTR;
50 static LIST_HEAD(rockchip_display_list);
51 static LIST_HEAD(logo_cache_list);
52 
53 static unsigned long memory_start;
54 static unsigned long memory_end;
55 
56 /*
57  * the phy types are used by different connectors in public.
58  * The current version only has inno hdmi phy for hdmi and tve.
59  */
60 enum public_use_phy {
61 	NONE,
62 	INNO_HDMI_PHY
63 };
64 
65 /* save public phy data */
66 struct public_phy_data {
67 	const struct rockchip_phy *phy_drv;
68 	int phy_node;
69 	int public_phy_type;
70 	bool phy_init;
71 };
72 
73 /* check which kind of public phy does connector use */
74 static int check_public_use_phy(struct display_state *state)
75 {
76 	int ret = NONE;
77 #ifdef CONFIG_ROCKCHIP_INNO_HDMI_PHY
78 	struct connector_state *conn_state = &state->conn_state;
79 
80 	if (!strncmp(dev_read_name(conn_state->dev), "tve", 3) ||
81 	    !strncmp(dev_read_name(conn_state->dev), "hdmi", 4))
82 		ret = INNO_HDMI_PHY;
83 #endif
84 
85 	return ret;
86 }
87 
88 /*
89  * get public phy driver and initialize it.
90  * The current version only has inno hdmi phy for hdmi and tve.
91  */
92 static int get_public_phy(struct display_state *state,
93 			  struct public_phy_data *data)
94 {
95 	struct connector_state *conn_state = &state->conn_state;
96 	struct rockchip_phy *phy;
97 	struct udevice *dev;
98 	int ret = 0;
99 
100 	switch (data->public_phy_type) {
101 	case INNO_HDMI_PHY:
102 #if defined(CONFIG_ROCKCHIP_RK3328)
103 		ret = uclass_get_device_by_name(UCLASS_PHY,
104 						"hdmiphy@ff430000", &dev);
105 #elif defined(CONFIG_ROCKCHIP_RK322X)
106 		ret = uclass_get_device_by_name(UCLASS_PHY,
107 						"hdmi-phy@12030000", &dev);
108 #else
109 		ret = -EINVAL;
110 #endif
111 		if (ret) {
112 			printf("Warn: can't find phy driver\n");
113 			return 0;
114 		}
115 
116 		phy = (struct rockchip_phy *)dev_get_driver_data(dev);
117 		if (!phy) {
118 			printf("failed to get phy driver\n");
119 			return 0;
120 		}
121 
122 		ret = rockchip_phy_init(phy);
123 		if (ret) {
124 			printf("failed to init phy driver\n");
125 			return ret;
126 		}
127 		conn_state->phy = phy;
128 
129 		printf("inno hdmi phy init success, save it\n");
130 		data->phy_drv = conn_state->phy;
131 		data->phy_init = true;
132 		return 0;
133 	default:
134 		return -EINVAL;
135 	}
136 }
137 
138 static void init_display_buffer(ulong base)
139 {
140 	memory_start = base + DRM_ROCKCHIP_FB_SIZE;
141 	memory_end = memory_start;
142 }
143 
144 static void *get_display_buffer(int size)
145 {
146 	unsigned long roundup_memory = roundup(memory_end, PAGE_SIZE);
147 	void *buf;
148 
149 	if (roundup_memory + size > memory_start + MEMORY_POOL_SIZE) {
150 		printf("failed to alloc %dbyte memory to display\n", size);
151 		return NULL;
152 	}
153 	buf = (void *)roundup_memory;
154 
155 	memory_end = roundup_memory + size;
156 
157 	return buf;
158 }
159 
160 static unsigned long get_display_size(void)
161 {
162 	return memory_end - memory_start;
163 }
164 
165 static bool can_direct_logo(int bpp)
166 {
167 	return bpp == 24 || bpp == 32;
168 }
169 
170 static int connector_phy_init(struct display_state *state,
171 			      struct public_phy_data *data)
172 {
173 	struct connector_state *conn_state = &state->conn_state;
174 	int type;
175 
176 	/* does this connector use public phy with others */
177 	type = check_public_use_phy(state);
178 	if (type == INNO_HDMI_PHY) {
179 		/* there is no public phy was initialized */
180 		if (!data->phy_init) {
181 			printf("start get public phy\n");
182 			data->public_phy_type = type;
183 			if (get_public_phy(state, data)) {
184 				printf("can't find correct public phy type\n");
185 				free(data);
186 				return -EINVAL;
187 			}
188 			return 0;
189 		}
190 
191 		/* if this phy has been initialized, get it directly */
192 		conn_state->phy = (struct rockchip_phy *)data->phy_drv;
193 		return 0;
194 	}
195 
196 	return 0;
197 }
198 
199 static int connector_panel_init(struct display_state *state)
200 {
201 	struct connector_state *conn_state = &state->conn_state;
202 	struct panel_state *panel_state = &state->panel_state;
203 	const struct rockchip_panel *panel = panel_state->panel;
204 	ofnode dsp_lut_node;
205 	int ret, len;
206 
207 	if (!panel)
208 		return 0;
209 
210 	dsp_lut_node = dev_read_subnode(panel->dev, "dsp-lut");
211 	if (!ofnode_valid(dsp_lut_node)) {
212 		debug("%s can not find dsp-lut node\n", __func__);
213 		return 0;
214 	}
215 
216 	ofnode_get_property(dsp_lut_node, "gamma-lut", &len);
217 	if (len > 0) {
218 		conn_state->gamma.size = len / sizeof(u32);
219 		conn_state->gamma.lut = malloc(len);
220 		if (!conn_state->gamma.lut) {
221 			printf("malloc gamma lut failed\n");
222 			return -ENOMEM;
223 		}
224 		ret = ofnode_read_u32_array(dsp_lut_node, "gamma-lut",
225 					    conn_state->gamma.lut,
226 					    conn_state->gamma.size);
227 		if (ret) {
228 			printf("Cannot decode gamma_lut\n");
229 			conn_state->gamma.lut = NULL;
230 			return -EINVAL;
231 		}
232 		panel_state->dsp_lut_node = dsp_lut_node;
233 	}
234 
235 	return 0;
236 }
237 
238 int drm_mode_vrefresh(const struct drm_display_mode *mode)
239 {
240 	int refresh = 0;
241 	unsigned int calc_val;
242 
243 	if (mode->vrefresh > 0) {
244 		refresh = mode->vrefresh;
245 	} else if (mode->htotal > 0 && mode->vtotal > 0) {
246 		int vtotal;
247 
248 		vtotal = mode->vtotal;
249 		/* work out vrefresh the value will be x1000 */
250 		calc_val = (mode->clock * 1000);
251 		calc_val /= mode->htotal;
252 		refresh = (calc_val + vtotal / 2) / vtotal;
253 
254 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
255 			refresh *= 2;
256 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
257 			refresh /= 2;
258 		if (mode->vscan > 1)
259 			refresh /= mode->vscan;
260 	}
261 	return refresh;
262 }
263 
264 static int display_get_timing_from_dts(struct panel_state *panel_state,
265 				       struct drm_display_mode *mode)
266 {
267 	struct rockchip_panel *panel = panel_state->panel;
268 	int phandle;
269 	int hactive, vactive, pixelclock;
270 	int hfront_porch, hback_porch, hsync_len;
271 	int vfront_porch, vback_porch, vsync_len;
272 	int val, flags = 0;
273 	ofnode timing, native_mode;
274 
275 	timing = dev_read_subnode(panel->dev, "display-timings");
276 	if (!ofnode_valid(timing))
277 		return -ENODEV;
278 
279 	native_mode = ofnode_find_subnode(timing, "timing");
280 	if (!ofnode_valid(native_mode)) {
281 		phandle = ofnode_read_u32_default(timing, "native-mode", -1);
282 		native_mode = np_to_ofnode(of_find_node_by_phandle(phandle));
283 		if (!ofnode_valid(native_mode)) {
284 			printf("failed to get display timings from DT\n");
285 			return -ENXIO;
286 		}
287 	}
288 
289 #define FDT_GET_INT(val, name) \
290 	val = ofnode_read_s32_default(native_mode, name, -1); \
291 	if (val < 0) { \
292 		printf("Can't get %s\n", name); \
293 		return -ENXIO; \
294 	}
295 
296 	FDT_GET_INT(hactive, "hactive");
297 	FDT_GET_INT(vactive, "vactive");
298 	FDT_GET_INT(pixelclock, "clock-frequency");
299 	FDT_GET_INT(hsync_len, "hsync-len");
300 	FDT_GET_INT(hfront_porch, "hfront-porch");
301 	FDT_GET_INT(hback_porch, "hback-porch");
302 	FDT_GET_INT(vsync_len, "vsync-len");
303 	FDT_GET_INT(vfront_porch, "vfront-porch");
304 	FDT_GET_INT(vback_porch, "vback-porch");
305 	FDT_GET_INT(val, "hsync-active");
306 	flags |= val ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
307 	FDT_GET_INT(val, "vsync-active");
308 	flags |= val ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
309 	FDT_GET_INT(val, "pixelclk-active");
310 	flags |= val ? DRM_MODE_FLAG_PPIXDATA : 0;
311 
312 	mode->hdisplay = hactive;
313 	mode->hsync_start = mode->hdisplay + hfront_porch;
314 	mode->hsync_end = mode->hsync_start + hsync_len;
315 	mode->htotal = mode->hsync_end + hback_porch;
316 
317 	mode->vdisplay = vactive;
318 	mode->vsync_start = mode->vdisplay + vfront_porch;
319 	mode->vsync_end = mode->vsync_start + vsync_len;
320 	mode->vtotal = mode->vsync_end + vback_porch;
321 
322 	mode->clock = pixelclock / 1000;
323 	mode->flags = flags;
324 
325 	return 0;
326 }
327 
328 /**
329  * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
330  * @p: mode
331  * @adjust_flags: a combination of adjustment flags
332  *
333  * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
334  *
335  * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
336  *   interlaced modes.
337  * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
338  *   buffers containing two eyes (only adjust the timings when needed, eg. for
339  *   "frame packing" or "side by side full").
340  * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
341  *   be performed for doublescan and vscan > 1 modes respectively.
342  */
343 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
344 {
345 	if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
346 		return;
347 
348 	if (p->flags & DRM_MODE_FLAG_DBLCLK)
349 		p->crtc_clock = 2 * p->clock;
350 	else
351 		p->crtc_clock = p->clock;
352 	p->crtc_hdisplay = p->hdisplay;
353 	p->crtc_hsync_start = p->hsync_start;
354 	p->crtc_hsync_end = p->hsync_end;
355 	p->crtc_htotal = p->htotal;
356 	p->crtc_hskew = p->hskew;
357 	p->crtc_vdisplay = p->vdisplay;
358 	p->crtc_vsync_start = p->vsync_start;
359 	p->crtc_vsync_end = p->vsync_end;
360 	p->crtc_vtotal = p->vtotal;
361 
362 	if (p->flags & DRM_MODE_FLAG_INTERLACE) {
363 		if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
364 			p->crtc_vdisplay /= 2;
365 			p->crtc_vsync_start /= 2;
366 			p->crtc_vsync_end /= 2;
367 			p->crtc_vtotal /= 2;
368 		}
369 	}
370 
371 	if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
372 		if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
373 			p->crtc_vdisplay *= 2;
374 			p->crtc_vsync_start *= 2;
375 			p->crtc_vsync_end *= 2;
376 			p->crtc_vtotal *= 2;
377 		}
378 	}
379 
380 	if (!(adjust_flags & CRTC_NO_VSCAN)) {
381 		if (p->vscan > 1) {
382 			p->crtc_vdisplay *= p->vscan;
383 			p->crtc_vsync_start *= p->vscan;
384 			p->crtc_vsync_end *= p->vscan;
385 			p->crtc_vtotal *= p->vscan;
386 		}
387 	}
388 
389 	if (adjust_flags & CRTC_STEREO_DOUBLE) {
390 		unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
391 
392 		switch (layout) {
393 		case DRM_MODE_FLAG_3D_FRAME_PACKING:
394 			p->crtc_clock *= 2;
395 			p->crtc_vdisplay += p->crtc_vtotal;
396 			p->crtc_vsync_start += p->crtc_vtotal;
397 			p->crtc_vsync_end += p->crtc_vtotal;
398 			p->crtc_vtotal += p->crtc_vtotal;
399 			break;
400 		}
401 	}
402 
403 	p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
404 	p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
405 	p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
406 	p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
407 }
408 
409 /**
410  * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
411  * output format
412  *
413  * @connector: drm connector under action.
414  * @mode: video mode to be tested.
415  *
416  * Returns:
417  * true if the mode can be supported in YCBCR420 format
418  * false if not.
419  */
420 bool drm_mode_is_420_only(const struct drm_display_info *display,
421 			  struct drm_display_mode *mode)
422 {
423 	u8 vic = drm_match_cea_mode(mode);
424 
425 	return test_bit(vic, display->hdmi.y420_vdb_modes);
426 }
427 
428 /**
429  * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
430  * output format also (along with RGB/YCBCR444/422)
431  *
432  * @display: display under action.
433  * @mode: video mode to be tested.
434  *
435  * Returns:
436  * true if the mode can be support YCBCR420 format
437  * false if not.
438  */
439 bool drm_mode_is_420_also(const struct drm_display_info *display,
440 			  struct drm_display_mode *mode)
441 {
442 	u8 vic = drm_match_cea_mode(mode);
443 
444 	return test_bit(vic, display->hdmi.y420_cmdb_modes);
445 }
446 
447 /**
448  * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
449  * output format
450  *
451  * @display: display under action.
452  * @mode: video mode to be tested.
453  *
454  * Returns:
455  * true if the mode can be supported in YCBCR420 format
456  * false if not.
457  */
458 bool drm_mode_is_420(const struct drm_display_info *display,
459 		     struct drm_display_mode *mode)
460 {
461 	return drm_mode_is_420_only(display, mode) ||
462 		drm_mode_is_420_also(display, mode);
463 }
464 
465 static int display_get_timing(struct display_state *state)
466 {
467 	struct connector_state *conn_state = &state->conn_state;
468 	const struct rockchip_connector *conn = conn_state->connector;
469 	const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
470 	struct drm_display_mode *mode = &conn_state->mode;
471 	const struct drm_display_mode *m;
472 	struct panel_state *panel_state = &state->panel_state;
473 	const struct rockchip_panel *panel = panel_state->panel;
474 
475 	if (dev_of_valid(panel->dev) &&
476 	    !display_get_timing_from_dts(panel_state, mode)) {
477 		printf("Using display timing dts\n");
478 		goto done;
479 	}
480 
481 	if (panel->data) {
482 		m = (const struct drm_display_mode *)panel->data;
483 		memcpy(mode, m, sizeof(*m));
484 		printf("Using display timing from compatible panel driver\n");
485 		goto done;
486 	}
487 
488 	if (conn_funcs->get_edid && !conn_funcs->get_edid(state)) {
489 		int panel_bits_per_colourp;
490 
491 		if (!edid_get_drm_mode((void *)&conn_state->edid,
492 				     sizeof(conn_state->edid), mode,
493 				     &panel_bits_per_colourp)) {
494 			printf("Using display timing from edid\n");
495 			edid_print_info((void *)&conn_state->edid);
496 			goto done;
497 		}
498 	}
499 
500 	printf("failed to find display timing\n");
501 	return -ENODEV;
502 done:
503 	printf("Detailed mode clock %u kHz, flags[%x]\n"
504 	       "    H: %04d %04d %04d %04d\n"
505 	       "    V: %04d %04d %04d %04d\n"
506 	       "bus_format: %x\n",
507 	       mode->clock, mode->flags,
508 	       mode->hdisplay, mode->hsync_start,
509 	       mode->hsync_end, mode->htotal,
510 	       mode->vdisplay, mode->vsync_start,
511 	       mode->vsync_end, mode->vtotal,
512 	       conn_state->bus_format);
513 
514 	return 0;
515 }
516 
517 static int display_init(struct display_state *state)
518 {
519 	struct connector_state *conn_state = &state->conn_state;
520 	struct panel_state *panel_state = &state->panel_state;
521 	const struct rockchip_connector *conn = conn_state->connector;
522 	const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
523 	struct crtc_state *crtc_state = &state->crtc_state;
524 	struct rockchip_crtc *crtc = crtc_state->crtc;
525 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
526 	struct drm_display_mode *mode = &conn_state->mode;
527 	int ret = 0;
528 	static bool __print_once = false;
529 
530 	if (!__print_once) {
531 		__print_once = true;
532 		printf("Rockchip UBOOT DRM driver version: %s\n", DRIVER_VERSION);
533 	}
534 
535 	if (state->is_init)
536 		return 0;
537 
538 	if (!conn_funcs || !crtc_funcs) {
539 		printf("failed to find connector or crtc functions\n");
540 		return -ENXIO;
541 	}
542 
543 	if (panel_state->panel)
544 		rockchip_panel_init(panel_state->panel);
545 
546 	if (conn_funcs->init) {
547 		ret = conn_funcs->init(state);
548 		if (ret)
549 			goto deinit;
550 	}
551 
552 	if (conn_state->phy)
553 		rockchip_phy_init(conn_state->phy);
554 
555 	/*
556 	 * support hotplug, but not connect;
557 	 */
558 #ifdef CONFIG_ROCKCHIP_DRM_TVE
559 	if (crtc->hdmi_hpd && conn_state->type == DRM_MODE_CONNECTOR_TV) {
560 		printf("hdmi plugin ,skip tve\n");
561 		goto deinit;
562 	}
563 #elif defined(CONFIG_ROCKCHIP_DRM_RK1000)
564 	if (crtc->hdmi_hpd && conn_state->type == DRM_MODE_CONNECTOR_LVDS) {
565 		printf("hdmi plugin ,skip tve\n");
566 		goto deinit;
567 	}
568 #endif
569 	if (conn_funcs->detect) {
570 		ret = conn_funcs->detect(state);
571 #if defined(CONFIG_ROCKCHIP_DRM_TVE) || defined(CONFIG_ROCKCHIP_DRM_RK1000)
572 		if (conn_state->type == DRM_MODE_CONNECTOR_HDMIA)
573 			crtc->hdmi_hpd = ret;
574 #endif
575 		if (!ret)
576 			goto deinit;
577 	}
578 
579 	if (conn_funcs->get_timing) {
580 		ret = conn_funcs->get_timing(state);
581 	} else if (panel_state->panel) {
582 		ret = display_get_timing(state);
583 	} else if (conn_state->bridge) {
584 		int bpc;
585 
586 		ret = video_bridge_read_edid(conn_state->bridge->dev,
587 					     conn_state->edid, EDID_SIZE);
588 		if (ret > 0) {
589 			ret = edid_get_drm_mode(conn_state->edid, ret, mode,
590 						&bpc);
591 			if (!ret)
592 				edid_print_info((void *)&conn_state->edid);
593 		}
594 	}
595 
596 	if (ret)
597 		goto deinit;
598 
599 	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
600 
601 	if (crtc_funcs->init) {
602 		ret = crtc_funcs->init(state);
603 		if (ret)
604 			goto deinit;
605 	}
606 	state->is_init = 1;
607 
608 	return 0;
609 
610 deinit:
611 	if (conn_funcs->deinit)
612 		conn_funcs->deinit(state);
613 	return ret;
614 }
615 
616 int display_send_mcu_cmd(struct display_state *state, u32 type, u32 val)
617 {
618 	struct crtc_state *crtc_state = &state->crtc_state;
619 	const struct rockchip_crtc *crtc = crtc_state->crtc;
620 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
621 	int ret;
622 
623 	if (!state->is_init)
624 		return -EINVAL;
625 
626 	if (crtc_funcs->send_mcu_cmd) {
627 		ret = crtc_funcs->send_mcu_cmd(state, type, val);
628 		if (ret)
629 			return ret;
630 	}
631 
632 	return 0;
633 }
634 
635 static int display_set_plane(struct display_state *state)
636 {
637 	struct crtc_state *crtc_state = &state->crtc_state;
638 	const struct rockchip_crtc *crtc = crtc_state->crtc;
639 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
640 	int ret;
641 
642 	if (!state->is_init)
643 		return -EINVAL;
644 
645 	if (crtc_funcs->set_plane) {
646 		ret = crtc_funcs->set_plane(state);
647 		if (ret)
648 			return ret;
649 	}
650 
651 	return 0;
652 }
653 
654 static int display_enable(struct display_state *state)
655 {
656 	struct connector_state *conn_state = &state->conn_state;
657 	const struct rockchip_connector *conn = conn_state->connector;
658 	const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
659 	struct crtc_state *crtc_state = &state->crtc_state;
660 	const struct rockchip_crtc *crtc = crtc_state->crtc;
661 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
662 	struct panel_state *panel_state = &state->panel_state;
663 	int ret = 0;
664 
665 	display_init(state);
666 
667 	if (!state->is_init)
668 		return -EINVAL;
669 
670 	if (state->is_enable)
671 		return 0;
672 
673 	if (crtc_funcs->prepare) {
674 		ret = crtc_funcs->prepare(state);
675 		if (ret)
676 			return ret;
677 	}
678 
679 	if (conn_funcs->prepare) {
680 		ret = conn_funcs->prepare(state);
681 		if (ret)
682 			goto unprepare_crtc;
683 	}
684 
685 	if (conn_state->bridge)
686 		rockchip_bridge_pre_enable(conn_state->bridge);
687 
688 	if (panel_state->panel)
689 		rockchip_panel_prepare(panel_state->panel);
690 
691 	if (crtc_funcs->enable) {
692 		ret = crtc_funcs->enable(state);
693 		if (ret)
694 			goto unprepare_conn;
695 	}
696 
697 	if (conn_funcs->enable) {
698 		ret = conn_funcs->enable(state);
699 		if (ret)
700 			goto disable_crtc;
701 	}
702 
703 	if (conn_state->bridge)
704 		rockchip_bridge_enable(conn_state->bridge);
705 
706 	if (panel_state->panel)
707 		rockchip_panel_enable(panel_state->panel);
708 
709 	state->is_enable = true;
710 	return 0;
711 
712 disable_crtc:
713 	if (crtc_funcs->disable)
714 		crtc_funcs->disable(state);
715 unprepare_conn:
716 	if (conn_funcs->unprepare)
717 		conn_funcs->unprepare(state);
718 unprepare_crtc:
719 	if (crtc_funcs->unprepare)
720 		crtc_funcs->unprepare(state);
721 	return ret;
722 }
723 
724 static int display_disable(struct display_state *state)
725 {
726 	struct connector_state *conn_state = &state->conn_state;
727 	const struct rockchip_connector *conn = conn_state->connector;
728 	const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
729 	struct crtc_state *crtc_state = &state->crtc_state;
730 	const struct rockchip_crtc *crtc = crtc_state->crtc;
731 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
732 	struct panel_state *panel_state = &state->panel_state;
733 
734 	if (!state->is_init)
735 		return 0;
736 
737 	if (!state->is_enable)
738 		return 0;
739 
740 	if (panel_state->panel)
741 		rockchip_panel_disable(panel_state->panel);
742 
743 	if (conn_state->bridge)
744 		rockchip_bridge_disable(conn_state->bridge);
745 
746 	if (conn_funcs->disable)
747 		conn_funcs->disable(state);
748 
749 	if (crtc_funcs->disable)
750 		crtc_funcs->disable(state);
751 
752 	if (panel_state->panel)
753 		rockchip_panel_unprepare(panel_state->panel);
754 
755 	if (conn_state->bridge)
756 		rockchip_bridge_post_disable(conn_state->bridge);
757 
758 	if (conn_funcs->unprepare)
759 		conn_funcs->unprepare(state);
760 
761 	state->is_enable = 0;
762 	state->is_init = 0;
763 
764 	return 0;
765 }
766 
767 static int display_logo(struct display_state *state)
768 {
769 	struct crtc_state *crtc_state = &state->crtc_state;
770 	struct connector_state *conn_state = &state->conn_state;
771 	struct logo_info *logo = &state->logo;
772 	int hdisplay, vdisplay;
773 
774 	display_init(state);
775 	if (!state->is_init)
776 		return -ENODEV;
777 
778 	switch (logo->bpp) {
779 	case 16:
780 		crtc_state->format = ROCKCHIP_FMT_RGB565;
781 		break;
782 	case 24:
783 		crtc_state->format = ROCKCHIP_FMT_RGB888;
784 		break;
785 	case 32:
786 		crtc_state->format = ROCKCHIP_FMT_ARGB8888;
787 		break;
788 	default:
789 		printf("can't support bmp bits[%d]\n", logo->bpp);
790 		return -EINVAL;
791 	}
792 	crtc_state->rb_swap = logo->bpp != 32;
793 	hdisplay = conn_state->mode.hdisplay;
794 	vdisplay = conn_state->mode.vdisplay;
795 	crtc_state->src_w = logo->width;
796 	crtc_state->src_h = logo->height;
797 	crtc_state->src_x = 0;
798 	crtc_state->src_y = 0;
799 	crtc_state->ymirror = logo->ymirror;
800 
801 	crtc_state->dma_addr = (u32)(unsigned long)logo->mem + logo->offset;
802 	crtc_state->xvir = ALIGN(crtc_state->src_w * logo->bpp, 32) >> 5;
803 
804 	if (logo->mode == ROCKCHIP_DISPLAY_FULLSCREEN) {
805 		crtc_state->crtc_x = 0;
806 		crtc_state->crtc_y = 0;
807 		crtc_state->crtc_w = hdisplay;
808 		crtc_state->crtc_h = vdisplay;
809 	} else {
810 		if (crtc_state->src_w >= hdisplay) {
811 			crtc_state->crtc_x = 0;
812 			crtc_state->crtc_w = hdisplay;
813 		} else {
814 			crtc_state->crtc_x = (hdisplay - crtc_state->src_w) / 2;
815 			crtc_state->crtc_w = crtc_state->src_w;
816 		}
817 
818 		if (crtc_state->src_h >= vdisplay) {
819 			crtc_state->crtc_y = 0;
820 			crtc_state->crtc_h = vdisplay;
821 		} else {
822 			crtc_state->crtc_y = (vdisplay - crtc_state->src_h) / 2;
823 			crtc_state->crtc_h = crtc_state->src_h;
824 		}
825 	}
826 
827 	display_set_plane(state);
828 	display_enable(state);
829 
830 	return 0;
831 }
832 
833 static int get_crtc_id(ofnode connect)
834 {
835 	int phandle;
836 	struct device_node *remote;
837 	int val;
838 
839 	phandle = ofnode_read_u32_default(connect, "remote-endpoint", -1);
840 	if (phandle < 0)
841 		goto err;
842 	remote = of_find_node_by_phandle(phandle);
843 	val = ofnode_read_u32_default(np_to_ofnode(remote), "reg", -1);
844 	if (val < 0)
845 		goto err;
846 
847 	return val;
848 err:
849 	printf("Can't get crtc id, default set to id = 0\n");
850 	return 0;
851 }
852 
853 static int get_crtc_mcu_mode(struct crtc_state *crtc_state)
854 {
855 	ofnode mcu_node;
856 	int total_pixel, cs_pst, cs_pend, rw_pst, rw_pend;
857 
858 	mcu_node = dev_read_subnode(crtc_state->dev, "mcu-timing");
859 	if (!ofnode_valid(mcu_node))
860 		return -ENODEV;
861 
862 #define FDT_GET_MCU_INT(val, name) \
863 	do { \
864 		val = ofnode_read_s32_default(mcu_node, name, -1); \
865 		if (val < 0) { \
866 			printf("Can't get %s\n", name); \
867 			return -ENXIO; \
868 		} \
869 	} while (0)
870 
871 	FDT_GET_MCU_INT(total_pixel, "mcu-pix-total");
872 	FDT_GET_MCU_INT(cs_pst, "mcu-cs-pst");
873 	FDT_GET_MCU_INT(cs_pend, "mcu-cs-pend");
874 	FDT_GET_MCU_INT(rw_pst, "mcu-rw-pst");
875 	FDT_GET_MCU_INT(rw_pend, "mcu-rw-pend");
876 
877 	crtc_state->mcu_timing.mcu_pix_total = total_pixel;
878 	crtc_state->mcu_timing.mcu_cs_pst = cs_pst;
879 	crtc_state->mcu_timing.mcu_cs_pend = cs_pend;
880 	crtc_state->mcu_timing.mcu_rw_pst = rw_pst;
881 	crtc_state->mcu_timing.mcu_rw_pend = rw_pend;
882 
883 	return 0;
884 }
885 
886 struct rockchip_logo_cache *find_or_alloc_logo_cache(const char *bmp)
887 {
888 	struct rockchip_logo_cache *tmp, *logo_cache = NULL;
889 
890 	list_for_each_entry(tmp, &logo_cache_list, head) {
891 		if (!strcmp(tmp->name, bmp)) {
892 			logo_cache = tmp;
893 			break;
894 		}
895 	}
896 
897 	if (!logo_cache) {
898 		logo_cache = malloc(sizeof(*logo_cache));
899 		if (!logo_cache) {
900 			printf("failed to alloc memory for logo cache\n");
901 			return NULL;
902 		}
903 		memset(logo_cache, 0, sizeof(*logo_cache));
904 		strcpy(logo_cache->name, bmp);
905 		INIT_LIST_HEAD(&logo_cache->head);
906 		list_add_tail(&logo_cache->head, &logo_cache_list);
907 	}
908 
909 	return logo_cache;
910 }
911 
912 /* Note: used only for rkfb kernel driver */
913 static int load_kernel_bmp_logo(struct logo_info *logo, const char *bmp_name)
914 {
915 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
916 	void *dst = NULL;
917 	int len, size;
918 	struct bmp_header *header;
919 
920 	if (!logo || !bmp_name)
921 		return -EINVAL;
922 
923 	header = malloc(RK_BLK_SIZE);
924 	if (!header)
925 		return -ENOMEM;
926 
927 	len = rockchip_read_resource_file(header, bmp_name, 0, RK_BLK_SIZE);
928 	if (len != RK_BLK_SIZE) {
929 		free(header);
930 		return -EINVAL;
931 	}
932 	size = get_unaligned_le32(&header->file_size);
933 	dst = (void *)(memory_start + MEMORY_POOL_SIZE / 2);
934 	len = rockchip_read_resource_file(dst, bmp_name, 0, size);
935 	if (len != size) {
936 		printf("failed to load bmp %s\n", bmp_name);
937 		free(header);
938 		return -ENOENT;
939 	}
940 
941 	logo->mem = dst;
942 
943 	return 0;
944 #endif
945 }
946 
947 static int load_bmp_logo(struct logo_info *logo, const char *bmp_name)
948 {
949 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
950 	struct rockchip_logo_cache *logo_cache;
951 	struct bmp_header *header;
952 	void *dst = NULL, *pdst;
953 	int size, len;
954 	int ret = 0;
955 
956 	if (!logo || !bmp_name)
957 		return -EINVAL;
958 	logo_cache = find_or_alloc_logo_cache(bmp_name);
959 	if (!logo_cache)
960 		return -ENOMEM;
961 
962 	if (logo_cache->logo.mem) {
963 		memcpy(logo, &logo_cache->logo, sizeof(*logo));
964 		return 0;
965 	}
966 
967 	header = malloc(RK_BLK_SIZE);
968 	if (!header)
969 		return -ENOMEM;
970 
971 	len = rockchip_read_resource_file(header, bmp_name, 0, RK_BLK_SIZE);
972 	if (len != RK_BLK_SIZE) {
973 		ret = -EINVAL;
974 		goto free_header;
975 	}
976 
977 	logo->bpp = get_unaligned_le16(&header->bit_count);
978 	logo->width = get_unaligned_le32(&header->width);
979 	logo->height = get_unaligned_le32(&header->height);
980 	size = get_unaligned_le32(&header->file_size);
981 	if (!can_direct_logo(logo->bpp)) {
982 		if (size > MEMORY_POOL_SIZE) {
983 			printf("failed to use boot buf as temp bmp buffer\n");
984 			ret = -ENOMEM;
985 			goto free_header;
986 		}
987 		pdst = get_display_buffer(size);
988 
989 	} else {
990 		pdst = get_display_buffer(size);
991 		dst = pdst;
992 	}
993 
994 	len = rockchip_read_resource_file(pdst, bmp_name, 0, size);
995 	if (len != size) {
996 		printf("failed to load bmp %s\n", bmp_name);
997 		ret = -ENOENT;
998 		goto free_header;
999 	}
1000 
1001 	if (!can_direct_logo(logo->bpp)) {
1002 		int dst_size;
1003 		/*
1004 		 * TODO: force use 16bpp if bpp less than 16;
1005 		 */
1006 		logo->bpp = (logo->bpp <= 16) ? 16 : logo->bpp;
1007 		dst_size = logo->width * logo->height * logo->bpp >> 3;
1008 
1009 		dst = get_display_buffer(dst_size);
1010 		if (!dst) {
1011 			ret = -ENOMEM;
1012 			goto free_header;
1013 		}
1014 		if (bmpdecoder(pdst, dst, logo->bpp)) {
1015 			printf("failed to decode bmp %s\n", bmp_name);
1016 			ret = -EINVAL;
1017 			goto free_header;
1018 		}
1019 		flush_dcache_range((ulong)dst,
1020 				   ALIGN((ulong)dst + dst_size,
1021 					 CONFIG_SYS_CACHELINE_SIZE));
1022 
1023 		logo->offset = 0;
1024 		logo->ymirror = 0;
1025 	} else {
1026 		logo->offset = get_unaligned_le32(&header->data_offset);
1027 		logo->ymirror = 1;
1028 	}
1029 	logo->mem = dst;
1030 
1031 	memcpy(&logo_cache->logo, logo, sizeof(*logo));
1032 
1033 free_header:
1034 
1035 	free(header);
1036 
1037 	return ret;
1038 #else
1039 	return -EINVAL;
1040 #endif
1041 }
1042 
1043 void rockchip_show_fbbase(ulong fbbase)
1044 {
1045 	struct display_state *s;
1046 
1047 	list_for_each_entry(s, &rockchip_display_list, head) {
1048 		s->logo.mode = ROCKCHIP_DISPLAY_FULLSCREEN;
1049 		s->logo.mem = (char *)fbbase;
1050 		s->logo.width = DRM_ROCKCHIP_FB_WIDTH;
1051 		s->logo.height = DRM_ROCKCHIP_FB_HEIGHT;
1052 		s->logo.bpp = 32;
1053 		s->logo.ymirror = 0;
1054 
1055 		display_logo(s);
1056 	}
1057 }
1058 
1059 void rockchip_show_bmp(const char *bmp)
1060 {
1061 	struct display_state *s;
1062 
1063 	if (!bmp) {
1064 		list_for_each_entry(s, &rockchip_display_list, head)
1065 			display_disable(s);
1066 		return;
1067 	}
1068 
1069 	list_for_each_entry(s, &rockchip_display_list, head) {
1070 		s->logo.mode = s->charge_logo_mode;
1071 		if (load_bmp_logo(&s->logo, bmp))
1072 			continue;
1073 		display_logo(s);
1074 	}
1075 }
1076 
1077 void rockchip_show_logo(void)
1078 {
1079 	struct display_state *s;
1080 
1081 	list_for_each_entry(s, &rockchip_display_list, head) {
1082 		s->logo.mode = s->logo_mode;
1083 		if (load_bmp_logo(&s->logo, s->ulogo_name))
1084 			printf("failed to display uboot logo\n");
1085 		else
1086 			display_logo(s);
1087 
1088 		/* Load kernel bmp in rockchip_display_fixup() later */
1089 	}
1090 }
1091 
1092 enum {
1093 	PORT_DIR_IN,
1094 	PORT_DIR_OUT,
1095 };
1096 
1097 static struct rockchip_panel *rockchip_of_find_panel(struct udevice *dev)
1098 {
1099 	ofnode panel_node, ports, port, ep;
1100 	struct udevice *panel_dev;
1101 	int ret;
1102 
1103 	panel_node = dev_read_subnode(dev, "panel");
1104 	if (ofnode_valid(panel_node) && ofnode_is_available(panel_node)) {
1105 		ret = uclass_get_device_by_ofnode(UCLASS_PANEL, panel_node,
1106 						  &panel_dev);
1107 		if (!ret)
1108 			goto found;
1109 	}
1110 
1111 	ports = dev_read_subnode(dev, "ports");
1112 	if (!ofnode_valid(ports))
1113 		return NULL;
1114 
1115 	ofnode_for_each_subnode(port, ports) {
1116 		u32 reg;
1117 
1118 		if (ofnode_read_u32(port, "reg", &reg))
1119 			continue;
1120 
1121 		if (reg != PORT_DIR_OUT)
1122 			continue;
1123 
1124 		ofnode_for_each_subnode(ep, port) {
1125 			ofnode _ep, _port;
1126 			uint phandle;
1127 
1128 			if (ofnode_read_u32(ep, "remote-endpoint", &phandle))
1129 				continue;
1130 
1131 			_ep = ofnode_get_by_phandle(phandle);
1132 			if (!ofnode_valid(_ep))
1133 				continue;
1134 
1135 			_port = ofnode_get_parent(_ep);
1136 			if (!ofnode_valid(_port))
1137 				continue;
1138 
1139 			panel_node = ofnode_get_parent(_port);
1140 			if (!ofnode_valid(panel_node))
1141 				continue;
1142 
1143 			ret = uclass_get_device_by_ofnode(UCLASS_PANEL,
1144 							  panel_node,
1145 							  &panel_dev);
1146 			if (!ret)
1147 				goto found;
1148 		}
1149 	}
1150 
1151 	return NULL;
1152 
1153 found:
1154 	return (struct rockchip_panel *)dev_get_driver_data(panel_dev);
1155 }
1156 
1157 static struct rockchip_bridge *rockchip_of_find_bridge(struct udevice *conn_dev)
1158 {
1159 	ofnode node, ports, port, ep;
1160 	struct udevice *dev;
1161 	int ret;
1162 
1163 	ports = dev_read_subnode(conn_dev, "ports");
1164 	if (!ofnode_valid(ports))
1165 		return NULL;
1166 
1167 	ofnode_for_each_subnode(port, ports) {
1168 		u32 reg;
1169 
1170 		if (ofnode_read_u32(port, "reg", &reg))
1171 			continue;
1172 
1173 		if (reg != PORT_DIR_OUT)
1174 			continue;
1175 
1176 		ofnode_for_each_subnode(ep, port) {
1177 			ofnode _ep, _port, _ports;
1178 			uint phandle;
1179 
1180 			if (ofnode_read_u32(ep, "remote-endpoint", &phandle))
1181 				continue;
1182 
1183 			_ep = ofnode_get_by_phandle(phandle);
1184 			if (!ofnode_valid(_ep))
1185 				continue;
1186 
1187 			_port = ofnode_get_parent(_ep);
1188 			if (!ofnode_valid(_port))
1189 				continue;
1190 
1191 			_ports = ofnode_get_parent(_port);
1192 			if (!ofnode_valid(_ports))
1193 				continue;
1194 
1195 			node = ofnode_get_parent(_ports);
1196 			if (!ofnode_valid(node))
1197 				continue;
1198 
1199 			ret = uclass_get_device_by_ofnode(UCLASS_VIDEO_BRIDGE,
1200 							  node, &dev);
1201 			if (!ret)
1202 				goto found;
1203 		}
1204 	}
1205 
1206 	return NULL;
1207 
1208 found:
1209 	return (struct rockchip_bridge *)dev_get_driver_data(dev);
1210 }
1211 
1212 static struct udevice *rockchip_of_find_connector(ofnode endpoint)
1213 {
1214 	ofnode ep, port, ports, conn;
1215 	uint phandle;
1216 	struct udevice *dev;
1217 	int ret;
1218 
1219 	if (ofnode_read_u32(endpoint, "remote-endpoint", &phandle))
1220 		return NULL;
1221 
1222 	ep = ofnode_get_by_phandle(phandle);
1223 	if (!ofnode_valid(ep) || !ofnode_is_available(ep))
1224 		return NULL;
1225 
1226 	port = ofnode_get_parent(ep);
1227 	if (!ofnode_valid(port))
1228 		return NULL;
1229 
1230 	ports = ofnode_get_parent(port);
1231 	if (!ofnode_valid(ports))
1232 		return NULL;
1233 
1234 	conn = ofnode_get_parent(ports);
1235 	if (!ofnode_valid(conn) || !ofnode_is_available(conn))
1236 		return NULL;
1237 
1238 	ret = uclass_get_device_by_ofnode(UCLASS_DISPLAY, conn, &dev);
1239 	if (ret)
1240 		return NULL;
1241 
1242 	return dev;
1243 }
1244 
1245 static struct rockchip_phy *rockchip_of_find_phy(struct udevice *dev)
1246 {
1247 	struct udevice *phy_dev;
1248 	int ret;
1249 
1250 	ret = uclass_get_device_by_phandle(UCLASS_PHY, dev, "phys", &phy_dev);
1251 	if (ret)
1252 		return NULL;
1253 
1254 	return (struct rockchip_phy *)dev_get_driver_data(phy_dev);
1255 }
1256 
1257 static int rockchip_display_probe(struct udevice *dev)
1258 {
1259 	struct video_priv *uc_priv = dev_get_uclass_priv(dev);
1260 	struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
1261 	const void *blob = gd->fdt_blob;
1262 	int phandle;
1263 	struct udevice *crtc_dev, *conn_dev;
1264 	struct rockchip_crtc *crtc;
1265 	const struct rockchip_connector *conn;
1266 	struct rockchip_panel *panel = NULL;
1267 	struct rockchip_bridge *bridge = NULL;
1268 	struct rockchip_phy *phy = NULL;
1269 	struct display_state *s;
1270 	const char *name;
1271 	int ret;
1272 	ofnode node, route_node;
1273 	struct device_node *port_node, *vop_node, *ep_node;
1274 	struct public_phy_data *data;
1275 
1276 	/* Before relocation we don't need to do anything */
1277 	if (!(gd->flags & GD_FLG_RELOC))
1278 		return 0;
1279 
1280 	data = malloc(sizeof(struct public_phy_data));
1281 	if (!data) {
1282 		printf("failed to alloc phy data\n");
1283 		return -ENOMEM;
1284 	}
1285 	data->phy_init = false;
1286 
1287 	init_display_buffer(plat->base);
1288 
1289 	route_node = dev_read_subnode(dev, "route");
1290 	if (!ofnode_valid(route_node))
1291 		return -ENODEV;
1292 
1293 	ofnode_for_each_subnode(node, route_node) {
1294 		if (!ofnode_is_available(node))
1295 			continue;
1296 		phandle = ofnode_read_u32_default(node, "connect", -1);
1297 		if (phandle < 0) {
1298 			printf("Warn: can't find connect node's handle\n");
1299 			continue;
1300 		}
1301 		ep_node = of_find_node_by_phandle(phandle);
1302 		if (!ofnode_valid(np_to_ofnode(ep_node))) {
1303 			printf("Warn: can't find endpoint node from phandle\n");
1304 			continue;
1305 		}
1306 		port_node = of_get_parent(ep_node);
1307 		if (!ofnode_valid(np_to_ofnode(port_node))) {
1308 			printf("Warn: can't find port node from phandle\n");
1309 			continue;
1310 		}
1311 		vop_node = of_get_parent(port_node);
1312 		if (!ofnode_valid(np_to_ofnode(vop_node))) {
1313 			printf("Warn: can't find crtc node from phandle\n");
1314 			continue;
1315 		}
1316 		ret = uclass_get_device_by_ofnode(UCLASS_VIDEO_CRTC,
1317 						  np_to_ofnode(vop_node),
1318 						  &crtc_dev);
1319 		if (ret) {
1320 			printf("Warn: can't find crtc driver %d\n", ret);
1321 			continue;
1322 		}
1323 		crtc = (struct rockchip_crtc *)dev_get_driver_data(crtc_dev);
1324 
1325 		conn_dev = rockchip_of_find_connector(np_to_ofnode(ep_node));
1326 		if (!conn_dev) {
1327 			printf("Warn: can't find connect driver\n");
1328 			continue;
1329 		}
1330 
1331 		conn = (const struct rockchip_connector *)dev_get_driver_data(conn_dev);
1332 
1333 		phy = rockchip_of_find_phy(conn_dev);
1334 
1335 		bridge = rockchip_of_find_bridge(conn_dev);
1336 		if (bridge)
1337 			panel = rockchip_of_find_panel(bridge->dev);
1338 		else
1339 			panel = rockchip_of_find_panel(conn_dev);
1340 
1341 		s = malloc(sizeof(*s));
1342 		if (!s)
1343 			continue;
1344 
1345 		memset(s, 0, sizeof(*s));
1346 
1347 		INIT_LIST_HEAD(&s->head);
1348 		ret = ofnode_read_string_index(node, "logo,uboot", 0, &name);
1349 		if (!ret)
1350 			memcpy(s->ulogo_name, name, strlen(name));
1351 		ret = ofnode_read_string_index(node, "logo,kernel", 0, &name);
1352 		if (!ret)
1353 			memcpy(s->klogo_name, name, strlen(name));
1354 		ret = ofnode_read_string_index(node, "logo,mode", 0, &name);
1355 		if (!strcmp(name, "fullscreen"))
1356 			s->logo_mode = ROCKCHIP_DISPLAY_FULLSCREEN;
1357 		else
1358 			s->logo_mode = ROCKCHIP_DISPLAY_CENTER;
1359 		ret = ofnode_read_string_index(node, "charge_logo,mode", 0, &name);
1360 		if (!strcmp(name, "fullscreen"))
1361 			s->charge_logo_mode = ROCKCHIP_DISPLAY_FULLSCREEN;
1362 		else
1363 			s->charge_logo_mode = ROCKCHIP_DISPLAY_CENTER;
1364 
1365 		s->blob = blob;
1366 		s->panel_state.panel = panel;
1367 		s->conn_state.node = conn_dev->node;
1368 		s->conn_state.dev = conn_dev;
1369 		s->conn_state.connector = conn;
1370 		s->conn_state.phy = phy;
1371 		s->conn_state.bridge = bridge;
1372 		s->conn_state.overscan.left_margin = 100;
1373 		s->conn_state.overscan.right_margin = 100;
1374 		s->conn_state.overscan.top_margin = 100;
1375 		s->conn_state.overscan.bottom_margin = 100;
1376 		s->crtc_state.node = np_to_ofnode(vop_node);
1377 		s->crtc_state.dev = crtc_dev;
1378 		s->crtc_state.crtc = crtc;
1379 		s->crtc_state.crtc_id = get_crtc_id(np_to_ofnode(ep_node));
1380 		s->node = node;
1381 
1382 		if (bridge)
1383 			bridge->state = s;
1384 
1385 		if (panel)
1386 			panel->state = s;
1387 
1388 		get_crtc_mcu_mode(&s->crtc_state);
1389 
1390 		if (connector_panel_init(s)) {
1391 			printf("Warn: Failed to init panel drivers\n");
1392 			free(s);
1393 			continue;
1394 		}
1395 
1396 		if (connector_phy_init(s, data)) {
1397 			printf("Warn: Failed to init phy drivers\n");
1398 			free(s);
1399 			continue;
1400 		}
1401 		list_add_tail(&s->head, &rockchip_display_list);
1402 	}
1403 
1404 	if (list_empty(&rockchip_display_list)) {
1405 		printf("Failed to found available display route\n");
1406 		return -ENODEV;
1407 	}
1408 
1409 	uc_priv->xsize = DRM_ROCKCHIP_FB_WIDTH;
1410 	uc_priv->ysize = DRM_ROCKCHIP_FB_HEIGHT;
1411 	uc_priv->bpix = VIDEO_BPP32;
1412 
1413 	#ifdef CONFIG_DRM_ROCKCHIP_VIDEO_FRAMEBUFFER
1414 	rockchip_show_fbbase(plat->base);
1415 	video_set_flush_dcache(dev, true);
1416 	#endif
1417 
1418 	return 0;
1419 }
1420 
1421 void rockchip_display_fixup(void *blob)
1422 {
1423 	const struct rockchip_connector_funcs *conn_funcs;
1424 	const struct rockchip_crtc_funcs *crtc_funcs;
1425 	const struct rockchip_connector *conn;
1426 	const struct rockchip_crtc *crtc;
1427 	struct display_state *s;
1428 	int offset;
1429 	const struct device_node *np;
1430 	const char *path;
1431 
1432 	if (!get_display_size())
1433 		return;
1434 
1435 	if (fdt_node_offset_by_compatible(blob, 0, "rockchip,drm-logo") >= 0) {
1436 		list_for_each_entry(s, &rockchip_display_list, head)
1437 			load_bmp_logo(&s->logo, s->klogo_name);
1438 		offset = fdt_update_reserved_memory(blob, "rockchip,drm-logo",
1439 						    (u64)memory_start,
1440 						    (u64)get_display_size());
1441 		if (offset < 0)
1442 			printf("failed to reserve drm-loader-logo memory\n");
1443 	} else {
1444 		printf("can't found rockchip,drm-logo, use rockchip,fb-logo\n");
1445 		/* Compatible with rkfb display, only need reserve memory */
1446 		offset = fdt_update_reserved_memory(blob, "rockchip,fb-logo",
1447 						    (u64)memory_start,
1448 						    MEMORY_POOL_SIZE);
1449 		if (offset < 0)
1450 			printf("failed to reserve fb-loader-logo memory\n");
1451 		else
1452 			list_for_each_entry(s, &rockchip_display_list, head)
1453 				load_kernel_bmp_logo(&s->logo, s->klogo_name);
1454 		return;
1455 	}
1456 
1457 	list_for_each_entry(s, &rockchip_display_list, head) {
1458 		conn = s->conn_state.connector;
1459 		if (!conn)
1460 			continue;
1461 		conn_funcs = conn->funcs;
1462 		if (!conn_funcs) {
1463 			printf("failed to get exist connector\n");
1464 			continue;
1465 		}
1466 
1467 		crtc = s->crtc_state.crtc;
1468 		if (!crtc)
1469 			continue;
1470 
1471 		crtc_funcs = crtc->funcs;
1472 		if (!crtc_funcs) {
1473 			printf("failed to get exist crtc\n");
1474 			continue;
1475 		}
1476 
1477 		if (crtc_funcs->fixup_dts)
1478 			crtc_funcs->fixup_dts(s, blob);
1479 
1480 		if (conn_funcs->fixup_dts)
1481 			conn_funcs->fixup_dts(s, blob);
1482 
1483 		np = ofnode_to_np(s->node);
1484 		path = np->full_name;
1485 		fdt_increase_size(blob, 0x400);
1486 #define FDT_SET_U32(name, val) \
1487 		do_fixup_by_path_u32(blob, path, name, val, 1);
1488 
1489 		offset = s->logo.offset + (u32)(unsigned long)s->logo.mem
1490 			 - memory_start;
1491 		FDT_SET_U32("logo,offset", offset);
1492 		FDT_SET_U32("logo,width", s->logo.width);
1493 		FDT_SET_U32("logo,height", s->logo.height);
1494 		FDT_SET_U32("logo,bpp", s->logo.bpp);
1495 		FDT_SET_U32("logo,ymirror", s->logo.ymirror);
1496 		FDT_SET_U32("video,hdisplay", s->conn_state.mode.hdisplay);
1497 		FDT_SET_U32("video,vdisplay", s->conn_state.mode.vdisplay);
1498 		FDT_SET_U32("video,crtc_hsync_end", s->conn_state.mode.crtc_hsync_end);
1499 		FDT_SET_U32("video,crtc_vsync_end", s->conn_state.mode.crtc_vsync_end);
1500 		FDT_SET_U32("video,vrefresh",
1501 			    drm_mode_vrefresh(&s->conn_state.mode));
1502 		FDT_SET_U32("video,flags", s->conn_state.mode.flags);
1503 		FDT_SET_U32("overscan,left_margin", s->conn_state.overscan.left_margin);
1504 		FDT_SET_U32("overscan,right_margin", s->conn_state.overscan.right_margin);
1505 		FDT_SET_U32("overscan,top_margin", s->conn_state.overscan.top_margin);
1506 		FDT_SET_U32("overscan,bottom_margin", s->conn_state.overscan.bottom_margin);
1507 #undef FDT_SET_U32
1508 	}
1509 }
1510 
1511 int rockchip_display_bind(struct udevice *dev)
1512 {
1513 	struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
1514 
1515 	plat->size = DRM_ROCKCHIP_FB_SIZE + MEMORY_POOL_SIZE;
1516 
1517 	return 0;
1518 }
1519 
1520 static const struct udevice_id rockchip_display_ids[] = {
1521 	{ .compatible = "rockchip,display-subsystem" },
1522 	{ }
1523 };
1524 
1525 U_BOOT_DRIVER(rockchip_display) = {
1526 	.name	= "rockchip_display",
1527 	.id	= UCLASS_VIDEO,
1528 	.of_match = rockchip_display_ids,
1529 	.bind	= rockchip_display_bind,
1530 	.probe	= rockchip_display_probe,
1531 };
1532 
1533 static int do_rockchip_logo_show(cmd_tbl_t *cmdtp, int flag, int argc,
1534 			char *const argv[])
1535 {
1536 	if (argc != 1)
1537 		return CMD_RET_USAGE;
1538 
1539 	rockchip_show_logo();
1540 
1541 	return 0;
1542 }
1543 
1544 static int do_rockchip_show_bmp(cmd_tbl_t *cmdtp, int flag, int argc,
1545 				char *const argv[])
1546 {
1547 	if (argc != 2)
1548 		return CMD_RET_USAGE;
1549 
1550 	rockchip_show_bmp(argv[1]);
1551 
1552 	return 0;
1553 }
1554 
1555 U_BOOT_CMD(
1556 	rockchip_show_logo, 1, 1, do_rockchip_logo_show,
1557 	"load and display log from resource partition",
1558 	NULL
1559 );
1560 
1561 U_BOOT_CMD(
1562 	rockchip_show_bmp, 2, 1, do_rockchip_show_bmp,
1563 	"load and display bmp from resource partition",
1564 	"    <bmp_name>"
1565 );
1566