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