xref: /rk3399_rockchip-uboot/drivers/video/drm/rockchip_display.c (revision 4e0fa9f6a6afd1c4db979a3d2e9f1e67d6e1f06f)
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 <boot_rkimg.h>
9 #include <config.h>
10 #include <common.h>
11 #include <errno.h>
12 #include <linux/libfdt.h>
13 #include <fdtdec.h>
14 #include <fdt_support.h>
15 #include <linux/hdmi.h>
16 #include <linux/list.h>
17 #include <linux/compat.h>
18 #include <linux/media-bus-format.h>
19 #include <malloc.h>
20 #include <video.h>
21 #include <video_rockchip.h>
22 #include <video_bridge.h>
23 #include <dm/device.h>
24 #include <dm/uclass-internal.h>
25 #include <asm/arch-rockchip/resource_img.h>
26 
27 #include "bmp_helper.h"
28 #include "rockchip_display.h"
29 #include "rockchip_crtc.h"
30 #include "rockchip_connector.h"
31 #include "rockchip_bridge.h"
32 #include "rockchip_phy.h"
33 #include "rockchip_panel.h"
34 #include <dm.h>
35 #include <dm/of_access.h>
36 #include <dm/ofnode.h>
37 #include <asm/io.h>
38 
39 #define DRIVER_VERSION	"v1.0.1"
40 
41 /***********************************************************************
42  *  Rockchip UBOOT DRM driver version
43  *
44  *  v1.0.0	: add basic version for rockchip drm driver(hjc)
45  *  v1.0.1	: add much dsi update(hjc)
46  *
47  **********************************************************************/
48 
49 #define RK_BLK_SIZE 512
50 #define BMP_PROCESSED_FLAG 8399
51 
52 DECLARE_GLOBAL_DATA_PTR;
53 static LIST_HEAD(rockchip_display_list);
54 static LIST_HEAD(logo_cache_list);
55 
56 static unsigned long memory_start;
57 static unsigned long cubic_lut_memory_start;
58 static unsigned long memory_end;
59 static struct base2_info base_parameter;
60 static uint32_t crc32_table[256];
61 
62 /*
63  * the phy types are used by different connectors in public.
64  * The current version only has inno hdmi phy for hdmi and tve.
65  */
66 enum public_use_phy {
67 	NONE,
68 	INNO_HDMI_PHY
69 };
70 
71 /* save public phy data */
72 struct public_phy_data {
73 	const struct rockchip_phy *phy_drv;
74 	int phy_node;
75 	int public_phy_type;
76 	bool phy_init;
77 };
78 
79 void rockchip_display_make_crc32_table(void)
80 {
81 	uint32_t c;
82 	int n, k;
83 	unsigned long poly;		/* polynomial exclusive-or pattern */
84 	/* terms of polynomial defining this crc (except x^32): */
85 	static const char p[] = {0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26};
86 
87 	/* make exclusive-or pattern from polynomial (0xedb88320L) */
88 	poly = 0L;
89 	for (n = 0; n < sizeof(p) / sizeof(char); n++)
90 		poly |= 1L << (31 - p[n]);
91 
92 	for (n = 0; n < 256; n++) {
93 		c = (unsigned long)n;
94 		for (k = 0; k < 8; k++)
95 		c = c & 1 ? poly ^ (c >> 1) : c >> 1;
96 		crc32_table[n] = cpu_to_le32(c);
97 	}
98 }
99 
100 uint32_t rockchip_display_crc32c_cal(unsigned char *data, int length)
101 {
102 	int i;
103 	uint32_t crc;
104 	crc = 0xFFFFFFFF;
105 
106 	for (i = 0; i < length; i++) {
107 		crc = crc32_table[(crc ^ *data) & 0xff] ^ (crc >> 8);
108 		data++;
109 	}
110 
111 	return crc ^ 0xffffffff;
112 }
113 
114 int rockchip_get_baseparameter(void)
115 {
116 	struct blk_desc *dev_desc;
117 	disk_partition_t part_info;
118 	int block_num = 2048;
119 	char baseparameter_buf[block_num * RK_BLK_SIZE] __aligned(ARCH_DMA_MINALIGN);
120 	int ret = 0;
121 
122 	dev_desc = rockchip_get_bootdev();
123 	if (!dev_desc) {
124 		printf("%s: Could not find device\n", __func__);
125 		return -ENOENT;
126 	}
127 
128 	if (part_get_info_by_name(dev_desc, "baseparameter", &part_info) < 0) {
129 		printf("Could not find baseparameter partition\n");
130 		return -ENOENT;
131 	}
132 
133 	ret = blk_dread(dev_desc, part_info.start, block_num, (void *)baseparameter_buf);
134 	if (ret < 0) {
135 		printf("read baseparameter failed\n");
136 		return ret;
137 	}
138 
139 	memcpy(&base_parameter, baseparameter_buf, sizeof(base_parameter));
140 	if (strncasecmp(base_parameter.head_flag, "BASP", 4)) {
141 		printf("warning: bad baseparameter\n");
142 		memset(&base_parameter, 0, sizeof(base_parameter));
143 	}
144 	rockchip_display_make_crc32_table();
145 
146 	return ret;
147 }
148 
149 struct base2_disp_info *rockchip_get_disp_info(int type, int id)
150 {
151 	struct base2_disp_info *disp_info;
152 	struct base2_disp_header *disp_header;
153 	int i = 0, offset = -1;
154 	u32 crc_val;
155 	void *base_parameter_addr = (void *)&base_parameter;
156 
157 	for (i = 0; i < 8; i++) {
158 		disp_header = &base_parameter.disp_header[i];
159 		if (disp_header->connector_type == type &&
160 		    disp_header->connector_id == id) {
161 			printf("disp info %d, type:%d, id:%d\n", i, type, id);
162 			offset = disp_header->offset;
163 			break;
164 		}
165 	}
166 
167 	if (offset < 0)
168 		return NULL;
169 	disp_info = base_parameter_addr + offset;
170 	if (disp_info->screen_info[0].type != type ||
171 	    disp_info->screen_info[0].id != id) {
172 		printf("connector type or id is error, type:%d, id:%d\n",
173 		       disp_info->screen_info[0].type,
174 		       disp_info->screen_info[0].id);
175 		return NULL;
176 	}
177 
178 	if (strncasecmp(disp_info->disp_head_flag, "DISP", 4))
179 		return NULL;
180 
181 	crc_val = rockchip_display_crc32c_cal((unsigned char *)disp_info, sizeof(struct base2_disp_info) - 4);
182 
183 	if (crc_val != disp_info->crc) {
184 		printf("error: connector type[%d], id[%d] disp info crc check error\n", type, id);
185 		return NULL;
186 	}
187 
188 	return disp_info;
189 }
190 
191 /* check which kind of public phy does connector use */
192 static int check_public_use_phy(struct display_state *state)
193 {
194 	int ret = NONE;
195 #ifdef CONFIG_ROCKCHIP_INNO_HDMI_PHY
196 	struct connector_state *conn_state = &state->conn_state;
197 
198 	if (!strncmp(dev_read_name(conn_state->dev), "tve", 3) ||
199 	    !strncmp(dev_read_name(conn_state->dev), "hdmi", 4))
200 		ret = INNO_HDMI_PHY;
201 #endif
202 
203 	return ret;
204 }
205 
206 /*
207  * get public phy driver and initialize it.
208  * The current version only has inno hdmi phy for hdmi and tve.
209  */
210 static int get_public_phy(struct display_state *state,
211 			  struct public_phy_data *data)
212 {
213 	struct connector_state *conn_state = &state->conn_state;
214 	struct rockchip_phy *phy;
215 	struct udevice *dev;
216 	int ret = 0;
217 
218 	switch (data->public_phy_type) {
219 	case INNO_HDMI_PHY:
220 #if defined(CONFIG_ROCKCHIP_RK3328)
221 		ret = uclass_get_device_by_name(UCLASS_PHY,
222 						"hdmiphy@ff430000", &dev);
223 #elif defined(CONFIG_ROCKCHIP_RK322X)
224 		ret = uclass_get_device_by_name(UCLASS_PHY,
225 						"hdmi-phy@12030000", &dev);
226 #else
227 		ret = -EINVAL;
228 #endif
229 		if (ret) {
230 			printf("Warn: can't find phy driver\n");
231 			return 0;
232 		}
233 
234 		phy = (struct rockchip_phy *)dev_get_driver_data(dev);
235 		if (!phy) {
236 			printf("failed to get phy driver\n");
237 			return 0;
238 		}
239 
240 		ret = rockchip_phy_init(phy);
241 		if (ret) {
242 			printf("failed to init phy driver\n");
243 			return ret;
244 		}
245 		conn_state->phy = phy;
246 
247 		debug("inno hdmi phy init success, save it\n");
248 		data->phy_drv = conn_state->phy;
249 		data->phy_init = true;
250 		return 0;
251 	default:
252 		return -EINVAL;
253 	}
254 }
255 
256 static void init_display_buffer(ulong base)
257 {
258 	memory_start = base + DRM_ROCKCHIP_FB_SIZE;
259 	memory_end = memory_start;
260 	cubic_lut_memory_start = memory_start + MEMORY_POOL_SIZE;
261 }
262 
263 void *get_display_buffer(int size)
264 {
265 	unsigned long roundup_memory = roundup(memory_end, PAGE_SIZE);
266 	void *buf;
267 
268 	if (roundup_memory + size > memory_start + MEMORY_POOL_SIZE) {
269 		printf("failed to alloc %dbyte memory to display\n", size);
270 		return NULL;
271 	}
272 	buf = (void *)roundup_memory;
273 
274 	memory_end = roundup_memory + size;
275 
276 	return buf;
277 }
278 
279 static unsigned long get_display_size(void)
280 {
281 	return memory_end - memory_start;
282 }
283 
284 static unsigned long get_single_cubic_lut_size(void)
285 {
286 	ulong cubic_lut_size;
287 	int cubic_lut_step = CONFIG_ROCKCHIP_CUBIC_LUT_SIZE;
288 
289 	/* This is depend on IC designed */
290 	cubic_lut_size = (cubic_lut_step * cubic_lut_step * cubic_lut_step + 1) / 2 * 16;
291 	cubic_lut_size = roundup(cubic_lut_size, PAGE_SIZE);
292 
293 	return cubic_lut_size;
294 }
295 
296 static unsigned long get_cubic_lut_offset(int crtc_id)
297 {
298 	return crtc_id * get_single_cubic_lut_size();
299 }
300 
301 unsigned long get_cubic_lut_buffer(int crtc_id)
302 {
303 	return cubic_lut_memory_start + crtc_id * get_single_cubic_lut_size();
304 }
305 
306 static unsigned long get_cubic_memory_size(void)
307 {
308 	/* Max support 4 cubic lut */
309 	return get_single_cubic_lut_size() * 4;
310 }
311 
312 bool can_direct_logo(int bpp)
313 {
314 	return bpp == 24 || bpp == 32;
315 }
316 
317 static int connector_phy_init(struct display_state *state,
318 			      struct public_phy_data *data)
319 {
320 	struct connector_state *conn_state = &state->conn_state;
321 	int type;
322 
323 	/* does this connector use public phy with others */
324 	type = check_public_use_phy(state);
325 	if (type == INNO_HDMI_PHY) {
326 		/* there is no public phy was initialized */
327 		if (!data->phy_init) {
328 			debug("start get public phy\n");
329 			data->public_phy_type = type;
330 			if (get_public_phy(state, data)) {
331 				printf("can't find correct public phy type\n");
332 				free(data);
333 				return -EINVAL;
334 			}
335 			return 0;
336 		}
337 
338 		/* if this phy has been initialized, get it directly */
339 		conn_state->phy = (struct rockchip_phy *)data->phy_drv;
340 		return 0;
341 	}
342 
343 	return 0;
344 }
345 
346 static int connector_panel_init(struct display_state *state)
347 {
348 	struct connector_state *conn_state = &state->conn_state;
349 	struct panel_state *panel_state = &state->panel_state;
350 	const struct rockchip_panel *panel = panel_state->panel;
351 	ofnode dsp_lut_node;
352 	int ret, len;
353 
354 	if (!panel)
355 		return 0;
356 
357 	dsp_lut_node = dev_read_subnode(panel->dev, "dsp-lut");
358 	if (!ofnode_valid(dsp_lut_node)) {
359 		debug("%s can not find dsp-lut node\n", __func__);
360 		return 0;
361 	}
362 
363 	ofnode_get_property(dsp_lut_node, "gamma-lut", &len);
364 	if (len > 0) {
365 		conn_state->gamma.size = len / sizeof(u32);
366 		conn_state->gamma.lut = malloc(len);
367 		if (!conn_state->gamma.lut) {
368 			printf("malloc gamma lut failed\n");
369 			return -ENOMEM;
370 		}
371 		ret = ofnode_read_u32_array(dsp_lut_node, "gamma-lut",
372 					    conn_state->gamma.lut,
373 					    conn_state->gamma.size);
374 		if (ret) {
375 			printf("Cannot decode gamma_lut\n");
376 			conn_state->gamma.lut = NULL;
377 			return -EINVAL;
378 		}
379 		panel_state->dsp_lut_node = dsp_lut_node;
380 	}
381 
382 	return 0;
383 }
384 
385 int drm_mode_vrefresh(const struct drm_display_mode *mode)
386 {
387 	int refresh = 0;
388 	unsigned int calc_val;
389 
390 	if (mode->vrefresh > 0) {
391 		refresh = mode->vrefresh;
392 	} else if (mode->htotal > 0 && mode->vtotal > 0) {
393 		int vtotal;
394 
395 		vtotal = mode->vtotal;
396 		/* work out vrefresh the value will be x1000 */
397 		calc_val = (mode->clock * 1000);
398 		calc_val /= mode->htotal;
399 		refresh = (calc_val + vtotal / 2) / vtotal;
400 
401 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
402 			refresh *= 2;
403 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
404 			refresh /= 2;
405 		if (mode->vscan > 1)
406 			refresh /= mode->vscan;
407 	}
408 	return refresh;
409 }
410 
411 static int display_get_detail_timing(ofnode node, struct drm_display_mode *mode)
412 {
413 	int hactive, vactive, pixelclock;
414 	int hfront_porch, hback_porch, hsync_len;
415 	int vfront_porch, vback_porch, vsync_len;
416 	int val, flags = 0;
417 
418 #define FDT_GET_INT(val, name) \
419 	val = ofnode_read_s32_default(node, name, -1); \
420 	if (val < 0) { \
421 		printf("Can't get %s\n", name); \
422 		return -ENXIO; \
423 	}
424 
425 #define FDT_GET_INT_DEFAULT(val, name, default) \
426 	val = ofnode_read_s32_default(node, name, default);
427 
428 	FDT_GET_INT(hactive, "hactive");
429 	FDT_GET_INT(vactive, "vactive");
430 	FDT_GET_INT(pixelclock, "clock-frequency");
431 	FDT_GET_INT(hsync_len, "hsync-len");
432 	FDT_GET_INT(hfront_porch, "hfront-porch");
433 	FDT_GET_INT(hback_porch, "hback-porch");
434 	FDT_GET_INT(vsync_len, "vsync-len");
435 	FDT_GET_INT(vfront_porch, "vfront-porch");
436 	FDT_GET_INT(vback_porch, "vback-porch");
437 	FDT_GET_INT(val, "hsync-active");
438 	flags |= val ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
439 	FDT_GET_INT(val, "vsync-active");
440 	flags |= val ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
441 	FDT_GET_INT(val, "pixelclk-active");
442 	flags |= val ? DRM_MODE_FLAG_PPIXDATA : 0;
443 
444 	FDT_GET_INT_DEFAULT(val, "screen-rotate", 0);
445 	if (val == DRM_MODE_FLAG_XMIRROR) {
446 		flags |= DRM_MODE_FLAG_XMIRROR;
447 	} else if (val == DRM_MODE_FLAG_YMIRROR) {
448 		flags |= DRM_MODE_FLAG_YMIRROR;
449 	} else if (val == DRM_MODE_FLAG_XYMIRROR) {
450 		flags |= DRM_MODE_FLAG_XMIRROR;
451 		flags |= DRM_MODE_FLAG_YMIRROR;
452 	}
453 	mode->hdisplay = hactive;
454 	mode->hsync_start = mode->hdisplay + hfront_porch;
455 	mode->hsync_end = mode->hsync_start + hsync_len;
456 	mode->htotal = mode->hsync_end + hback_porch;
457 
458 	mode->vdisplay = vactive;
459 	mode->vsync_start = mode->vdisplay + vfront_porch;
460 	mode->vsync_end = mode->vsync_start + vsync_len;
461 	mode->vtotal = mode->vsync_end + vback_porch;
462 
463 	mode->clock = pixelclock / 1000;
464 	mode->flags = flags;
465 
466 	return 0;
467 }
468 
469 static int display_get_force_timing_from_dts(ofnode node, struct drm_display_mode *mode)
470 {
471 	int ret = 0;
472 
473 	ret = display_get_detail_timing(node, mode);
474 
475 	if (ret) {
476 		mode->clock = 74250;
477 		mode->flags = 0x5;
478 		mode->hdisplay = 1280;
479 		mode->hsync_start = 1390;
480 		mode->hsync_end = 1430;
481 		mode->htotal = 1650;
482 		mode->hskew = 0;
483 		mode->vdisplay = 720;
484 		mode->vsync_start = 725;
485 		mode->vsync_end = 730;
486 		mode->vtotal = 750;
487 		mode->vrefresh = 60;
488 		mode->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9;
489 		mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
490 	}
491 
492 	printf("route node %s force_timing, use %dx%dp%d as default mode\n",
493 	       ret ? "undefine" : "define", mode->hdisplay, mode->vdisplay,
494 	       mode->vscan);
495 
496 	return 0;
497 }
498 
499 static int display_get_timing_from_dts(struct panel_state *panel_state,
500 				       struct drm_display_mode *mode)
501 {
502 	struct rockchip_panel *panel = panel_state->panel;
503 	struct ofnode_phandle_args args;
504 	ofnode dt, timing;
505 	int ret;
506 
507 	dt = dev_read_subnode(panel->dev, "display-timings");
508 	if (ofnode_valid(dt)) {
509 		ret = ofnode_parse_phandle_with_args(dt, "native-mode", NULL,
510 						     0, 0, &args);
511 		if (ret)
512 			return ret;
513 
514 		timing = args.node;
515 	} else {
516 		timing = dev_read_subnode(panel->dev, "panel-timing");
517 	}
518 
519 	if (!ofnode_valid(timing)) {
520 		printf("failed to get display timings from DT\n");
521 		return -ENXIO;
522 	}
523 
524 	display_get_detail_timing(timing, mode);
525 
526 	return 0;
527 }
528 
529 /**
530  * drm_mode_max_resolution_filter - mark modes out of vop max resolution
531  * @edid_data: structure store mode list
532  * @max_output: vop max output resolution
533  */
534 void drm_mode_max_resolution_filter(struct hdmi_edid_data *edid_data,
535 				    struct vop_rect *max_output)
536 {
537 	int i;
538 
539 	for (i = 0; i < edid_data->modes; i++) {
540 		if (edid_data->mode_buf[i].hdisplay > max_output->width ||
541 		    edid_data->mode_buf[i].vdisplay > max_output->height)
542 			edid_data->mode_buf[i].invalid = true;
543 	}
544 }
545 
546 /**
547  * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
548  * @p: mode
549  * @adjust_flags: a combination of adjustment flags
550  *
551  * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
552  *
553  * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
554  *   interlaced modes.
555  * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
556  *   buffers containing two eyes (only adjust the timings when needed, eg. for
557  *   "frame packing" or "side by side full").
558  * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
559  *   be performed for doublescan and vscan > 1 modes respectively.
560  */
561 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
562 {
563 	if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
564 		return;
565 
566 	if (p->flags & DRM_MODE_FLAG_DBLCLK)
567 		p->crtc_clock = 2 * p->clock;
568 	else
569 		p->crtc_clock = p->clock;
570 	p->crtc_hdisplay = p->hdisplay;
571 	p->crtc_hsync_start = p->hsync_start;
572 	p->crtc_hsync_end = p->hsync_end;
573 	p->crtc_htotal = p->htotal;
574 	p->crtc_hskew = p->hskew;
575 	p->crtc_vdisplay = p->vdisplay;
576 	p->crtc_vsync_start = p->vsync_start;
577 	p->crtc_vsync_end = p->vsync_end;
578 	p->crtc_vtotal = p->vtotal;
579 
580 	if (p->flags & DRM_MODE_FLAG_INTERLACE) {
581 		if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
582 			p->crtc_vdisplay /= 2;
583 			p->crtc_vsync_start /= 2;
584 			p->crtc_vsync_end /= 2;
585 			p->crtc_vtotal /= 2;
586 		}
587 	}
588 
589 	if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
590 		if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
591 			p->crtc_vdisplay *= 2;
592 			p->crtc_vsync_start *= 2;
593 			p->crtc_vsync_end *= 2;
594 			p->crtc_vtotal *= 2;
595 		}
596 	}
597 
598 	if (!(adjust_flags & CRTC_NO_VSCAN)) {
599 		if (p->vscan > 1) {
600 			p->crtc_vdisplay *= p->vscan;
601 			p->crtc_vsync_start *= p->vscan;
602 			p->crtc_vsync_end *= p->vscan;
603 			p->crtc_vtotal *= p->vscan;
604 		}
605 	}
606 
607 	if (adjust_flags & CRTC_STEREO_DOUBLE) {
608 		unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
609 
610 		switch (layout) {
611 		case DRM_MODE_FLAG_3D_FRAME_PACKING:
612 			p->crtc_clock *= 2;
613 			p->crtc_vdisplay += p->crtc_vtotal;
614 			p->crtc_vsync_start += p->crtc_vtotal;
615 			p->crtc_vsync_end += p->crtc_vtotal;
616 			p->crtc_vtotal += p->crtc_vtotal;
617 			break;
618 		}
619 	}
620 
621 	p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
622 	p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
623 	p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
624 	p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
625 }
626 
627 /**
628  * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
629  * output format
630  *
631  * @connector: drm connector under action.
632  * @mode: video mode to be tested.
633  *
634  * Returns:
635  * true if the mode can be supported in YCBCR420 format
636  * false if not.
637  */
638 bool drm_mode_is_420_only(const struct drm_display_info *display,
639 			  struct drm_display_mode *mode)
640 {
641 	u8 vic = drm_match_cea_mode(mode);
642 
643 	return test_bit(vic, display->hdmi.y420_vdb_modes);
644 }
645 
646 /**
647  * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
648  * output format also (along with RGB/YCBCR444/422)
649  *
650  * @display: display under action.
651  * @mode: video mode to be tested.
652  *
653  * Returns:
654  * true if the mode can be support YCBCR420 format
655  * false if not.
656  */
657 bool drm_mode_is_420_also(const struct drm_display_info *display,
658 			  struct drm_display_mode *mode)
659 {
660 	u8 vic = drm_match_cea_mode(mode);
661 
662 	return test_bit(vic, display->hdmi.y420_cmdb_modes);
663 }
664 
665 /**
666  * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
667  * output format
668  *
669  * @display: display under action.
670  * @mode: video mode to be tested.
671  *
672  * Returns:
673  * true if the mode can be supported in YCBCR420 format
674  * false if not.
675  */
676 bool drm_mode_is_420(const struct drm_display_info *display,
677 		     struct drm_display_mode *mode)
678 {
679 	return drm_mode_is_420_only(display, mode) ||
680 		drm_mode_is_420_also(display, mode);
681 }
682 
683 static int display_get_timing(struct display_state *state)
684 {
685 	struct connector_state *conn_state = &state->conn_state;
686 	struct drm_display_mode *mode = &conn_state->mode;
687 	const struct drm_display_mode *m;
688 	struct panel_state *panel_state = &state->panel_state;
689 	const struct rockchip_panel *panel = panel_state->panel;
690 
691 	if (dev_of_valid(panel->dev) &&
692 	    !display_get_timing_from_dts(panel_state, mode)) {
693 		printf("Using display timing dts\n");
694 		return 0;
695 	}
696 
697 	if (panel->data) {
698 		m = (const struct drm_display_mode *)panel->data;
699 		memcpy(mode, m, sizeof(*m));
700 		printf("Using display timing from compatible panel driver\n");
701 		return 0;
702 	}
703 
704 	return -ENODEV;
705 }
706 
707 static int display_pre_init(void)
708 {
709 	struct display_state *state;
710 	int ret = 0;
711 
712 	list_for_each_entry(state, &rockchip_display_list, head) {
713 		struct connector_state *conn_state = &state->conn_state;
714 		const struct rockchip_connector *conn = conn_state->connector;
715 		const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
716 		struct crtc_state *crtc_state = &state->crtc_state;
717 		struct rockchip_crtc *crtc = crtc_state->crtc;
718 
719 		if (conn_funcs->pre_init) {
720 			ret = conn_funcs->pre_init(state);
721 			if (ret)
722 				printf("pre init conn error\n");
723 		}
724 		crtc->vps[crtc_state->crtc_id].output_type = conn_state->type;
725 	}
726 	return ret;
727 }
728 
729 static int display_use_force_mode(struct display_state *state)
730 {
731 	struct connector_state *conn_state = &state->conn_state;
732 	struct drm_display_mode *mode = &conn_state->mode;
733 
734 	conn_state->bpc = 8;
735 	memcpy(mode, &state->force_mode, sizeof(struct drm_display_mode));
736 	conn_state->bus_format = state->force_bus_format;
737 
738 	return 0;
739 }
740 
741 static int display_get_edid_mode(struct display_state *state)
742 {
743 	int ret = 0;
744 	struct connector_state *conn_state = &state->conn_state;
745 	struct drm_display_mode *mode = &conn_state->mode;
746 	int bpc;
747 
748 	ret = edid_get_drm_mode(conn_state->edid, ret, mode, &bpc);
749 	if (!ret) {
750 		conn_state->bpc = bpc;
751 		edid_print_info((void *)&conn_state->edid);
752 	} else {
753 		conn_state->bpc = 8;
754 		mode->clock = 74250;
755 		mode->flags = 0x5;
756 		mode->hdisplay = 1280;
757 		mode->hsync_start = 1390;
758 		mode->hsync_end = 1430;
759 		mode->htotal = 1650;
760 		mode->hskew = 0;
761 		mode->vdisplay = 720;
762 		mode->vsync_start = 725;
763 		mode->vsync_end = 730;
764 		mode->vtotal = 750;
765 		mode->vrefresh = 60;
766 		mode->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9;
767 		mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
768 
769 		printf("error: %s get mode from edid failed, use 720p60 as default mode\n", conn_state->dev->name);
770 	}
771 
772 	return ret;
773 }
774 
775 static int display_init(struct display_state *state)
776 {
777 	struct connector_state *conn_state = &state->conn_state;
778 	struct panel_state *panel_state = &state->panel_state;
779 	const struct rockchip_connector *conn = conn_state->connector;
780 	const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
781 	struct crtc_state *crtc_state = &state->crtc_state;
782 	struct rockchip_crtc *crtc = crtc_state->crtc;
783 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
784 	struct drm_display_mode *mode = &conn_state->mode;
785 	const char *compatible;
786 	int ret = 0;
787 	static bool __print_once = false;
788 
789 	if (!__print_once) {
790 		__print_once = true;
791 		printf("Rockchip UBOOT DRM driver version: %s\n", DRIVER_VERSION);
792 	}
793 
794 	if (state->is_init)
795 		return 0;
796 
797 	if (!conn_funcs || !crtc_funcs) {
798 		printf("failed to find connector or crtc functions\n");
799 		return -ENXIO;
800 	}
801 
802 	if (crtc_state->crtc->active && !crtc_state->ports_node &&
803 	    memcmp(&crtc_state->crtc->active_mode, &conn_state->mode,
804 		   sizeof(struct drm_display_mode))) {
805 		printf("%s has been used for output type: %d, mode: %dx%dp%d\n",
806 			crtc_state->dev->name,
807 			crtc_state->crtc->active_mode.type,
808 			crtc_state->crtc->active_mode.hdisplay,
809 			crtc_state->crtc->active_mode.vdisplay,
810 			crtc_state->crtc->active_mode.vrefresh);
811 		return -ENODEV;
812 	}
813 
814 	if (crtc_funcs->preinit) {
815 		ret = crtc_funcs->preinit(state);
816 		if (ret)
817 			return ret;
818 	}
819 
820 	if (panel_state->panel)
821 		rockchip_panel_init(panel_state->panel);
822 
823 	if (conn_funcs->init) {
824 		ret = conn_funcs->init(state);
825 		if (ret)
826 			goto deinit;
827 	}
828 
829 	if (conn_state->phy)
830 		rockchip_phy_init(conn_state->phy);
831 
832 	/*
833 	 * support hotplug, but not connect;
834 	 */
835 #ifdef CONFIG_ROCKCHIP_DRM_TVE
836 	if (crtc->hdmi_hpd && conn_state->type == DRM_MODE_CONNECTOR_TV) {
837 		printf("hdmi plugin ,skip tve\n");
838 		goto deinit;
839 	}
840 #elif defined(CONFIG_DRM_ROCKCHIP_RK1000)
841 	if (crtc->hdmi_hpd && conn_state->type == DRM_MODE_CONNECTOR_LVDS) {
842 		printf("hdmi plugin ,skip tve\n");
843 		goto deinit;
844 	}
845 #endif
846 	if (conn_funcs->detect) {
847 		ret = conn_funcs->detect(state);
848 #if defined(CONFIG_ROCKCHIP_DRM_TVE) || defined(CONFIG_DRM_ROCKCHIP_RK1000)
849 		if (conn_state->type == DRM_MODE_CONNECTOR_HDMIA)
850 			crtc->hdmi_hpd = ret;
851 #endif
852 		if (!ret && !state->force_output) {
853 			printf("%s disconnected\n", conn_state->dev->name);
854 			goto deinit;
855 		}
856 	}
857 
858 	if (panel_state->panel) {
859 		ret = display_get_timing(state);
860 		if (!ret)
861 			conn_state->bpc = panel_state->panel->bpc;
862 #if defined(CONFIG_I2C_EDID)
863 		if (ret < 0 && conn_funcs->get_edid) {
864 			rockchip_panel_prepare(panel_state->panel);
865 			ret = conn_funcs->get_edid(state);
866 			if (!ret)
867 				display_get_edid_mode(state);
868 		}
869 #endif
870 	} else if (conn_state->bridge) {
871 		ret = video_bridge_read_edid(conn_state->bridge->dev,
872 					     conn_state->edid, EDID_SIZE);
873 		if (ret > 0) {
874 #if defined(CONFIG_I2C_EDID)
875 			display_get_edid_mode(state);
876 #endif
877 		} else {
878 			ret = video_bridge_get_timing(conn_state->bridge->dev);
879 		}
880 	} else if (conn_funcs->get_timing) {
881 		ret = conn_funcs->get_timing(state);
882 	} else if (conn_funcs->get_edid) {
883 		ret = conn_funcs->get_edid(state);
884 #if defined(CONFIG_I2C_EDID)
885 		if (!ret)
886 			display_get_edid_mode(state);
887 #endif
888 	}
889 
890 	if (ret && !state->force_output)
891 		goto deinit;
892 	if (state->force_output)
893 		display_use_force_mode(state);
894 
895 	/* rk356x series drive mipi pixdata on posedge */
896 	compatible = dev_read_string(conn_state->dev, "compatible");
897 	if (!strcmp(compatible, "rockchip,rk3568-mipi-dsi"))
898 		conn_state->mode.flags |= DRM_MODE_FLAG_PPIXDATA;
899 
900 	printf("%s: %s detailed mode clock %u kHz, flags[%x]\n"
901 	       "    H: %04d %04d %04d %04d\n"
902 	       "    V: %04d %04d %04d %04d\n"
903 	       "bus_format: %x\n",
904 	       conn_state->dev->name,
905 	       state->force_output ? "use force output" : "",
906 	       mode->clock, mode->flags,
907 	       mode->hdisplay, mode->hsync_start,
908 	       mode->hsync_end, mode->htotal,
909 	       mode->vdisplay, mode->vsync_start,
910 	       mode->vsync_end, mode->vtotal,
911 	       conn_state->bus_format);
912 
913 	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
914 
915 	if (conn_state->bridge)
916 		rockchip_bridge_mode_set(conn_state->bridge, &conn_state->mode);
917 
918 	if (crtc_funcs->init) {
919 		ret = crtc_funcs->init(state);
920 		if (ret)
921 			goto deinit;
922 	}
923 	state->is_init = 1;
924 
925 	crtc_state->crtc->active = true;
926 	memcpy(&crtc_state->crtc->active_mode,
927 	       &conn_state->mode, sizeof(struct drm_display_mode));
928 
929 	return 0;
930 
931 deinit:
932 	if (conn_funcs->deinit)
933 		conn_funcs->deinit(state);
934 	return ret;
935 }
936 
937 int display_send_mcu_cmd(struct display_state *state, u32 type, u32 val)
938 {
939 	struct crtc_state *crtc_state = &state->crtc_state;
940 	const struct rockchip_crtc *crtc = crtc_state->crtc;
941 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
942 	int ret;
943 
944 	if (!state->is_init)
945 		return -EINVAL;
946 
947 	if (crtc_funcs->send_mcu_cmd) {
948 		ret = crtc_funcs->send_mcu_cmd(state, type, val);
949 		if (ret)
950 			return ret;
951 	}
952 
953 	return 0;
954 }
955 
956 static int display_set_plane(struct display_state *state)
957 {
958 	struct crtc_state *crtc_state = &state->crtc_state;
959 	const struct rockchip_crtc *crtc = crtc_state->crtc;
960 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
961 	int ret;
962 
963 	if (!state->is_init)
964 		return -EINVAL;
965 
966 	if (crtc_funcs->set_plane) {
967 		ret = crtc_funcs->set_plane(state);
968 		if (ret)
969 			return ret;
970 	}
971 
972 	return 0;
973 }
974 
975 static int display_enable(struct display_state *state)
976 {
977 	struct connector_state *conn_state = &state->conn_state;
978 	const struct rockchip_connector *conn = conn_state->connector;
979 	const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
980 	struct crtc_state *crtc_state = &state->crtc_state;
981 	const struct rockchip_crtc *crtc = crtc_state->crtc;
982 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
983 	struct panel_state *panel_state = &state->panel_state;
984 
985 	if (!state->is_init)
986 		return -EINVAL;
987 
988 	if (state->is_enable)
989 		return 0;
990 
991 	if (crtc_funcs->prepare)
992 		crtc_funcs->prepare(state);
993 
994 	if (conn_funcs->prepare)
995 		conn_funcs->prepare(state);
996 
997 	if (conn_state->bridge)
998 		rockchip_bridge_pre_enable(conn_state->bridge);
999 
1000 	if (panel_state->panel)
1001 		rockchip_panel_prepare(panel_state->panel);
1002 
1003 	if (crtc_funcs->enable)
1004 		crtc_funcs->enable(state);
1005 
1006 	if (conn_funcs->enable)
1007 		conn_funcs->enable(state);
1008 
1009 	if (conn_state->bridge)
1010 		rockchip_bridge_enable(conn_state->bridge);
1011 
1012 	if (panel_state->panel)
1013 		rockchip_panel_enable(panel_state->panel);
1014 
1015 	state->is_enable = true;
1016 
1017 	return 0;
1018 }
1019 
1020 static int display_disable(struct display_state *state)
1021 {
1022 	struct connector_state *conn_state = &state->conn_state;
1023 	const struct rockchip_connector *conn = conn_state->connector;
1024 	const struct rockchip_connector_funcs *conn_funcs = conn->funcs;
1025 	struct crtc_state *crtc_state = &state->crtc_state;
1026 	const struct rockchip_crtc *crtc = crtc_state->crtc;
1027 	const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs;
1028 	struct panel_state *panel_state = &state->panel_state;
1029 
1030 	if (!state->is_init)
1031 		return 0;
1032 
1033 	if (!state->is_enable)
1034 		return 0;
1035 
1036 	if (panel_state->panel)
1037 		rockchip_panel_disable(panel_state->panel);
1038 
1039 	if (conn_state->bridge)
1040 		rockchip_bridge_disable(conn_state->bridge);
1041 
1042 	if (conn_funcs->disable)
1043 		conn_funcs->disable(state);
1044 
1045 	if (crtc_funcs->disable)
1046 		crtc_funcs->disable(state);
1047 
1048 	if (panel_state->panel)
1049 		rockchip_panel_unprepare(panel_state->panel);
1050 
1051 	if (conn_state->bridge)
1052 		rockchip_bridge_post_disable(conn_state->bridge);
1053 
1054 	if (conn_funcs->unprepare)
1055 		conn_funcs->unprepare(state);
1056 
1057 	state->is_enable = 0;
1058 	state->is_init = 0;
1059 
1060 	return 0;
1061 }
1062 
1063 static int display_logo(struct display_state *state)
1064 {
1065 	struct crtc_state *crtc_state = &state->crtc_state;
1066 	struct connector_state *conn_state = &state->conn_state;
1067 	struct logo_info *logo = &state->logo;
1068 	int hdisplay, vdisplay, ret;
1069 
1070 	ret = display_init(state);
1071 	if (!state->is_init || ret)
1072 		return -ENODEV;
1073 
1074 	switch (logo->bpp) {
1075 	case 16:
1076 		crtc_state->format = ROCKCHIP_FMT_RGB565;
1077 		break;
1078 	case 24:
1079 		crtc_state->format = ROCKCHIP_FMT_RGB888;
1080 		break;
1081 	case 32:
1082 		crtc_state->format = ROCKCHIP_FMT_ARGB8888;
1083 		break;
1084 	default:
1085 		printf("can't support bmp bits[%d]\n", logo->bpp);
1086 		return -EINVAL;
1087 	}
1088 	hdisplay = conn_state->mode.hdisplay;
1089 	vdisplay = conn_state->mode.vdisplay;
1090 	crtc_state->src_w = logo->width;
1091 	crtc_state->src_h = logo->height;
1092 	crtc_state->src_x = 0;
1093 	crtc_state->src_y = 0;
1094 	crtc_state->ymirror = logo->ymirror;
1095 	crtc_state->rb_swap = 0;
1096 
1097 	crtc_state->dma_addr = (u32)(unsigned long)logo->mem + logo->offset;
1098 	crtc_state->xvir = ALIGN(crtc_state->src_w * logo->bpp, 32) >> 5;
1099 
1100 	if (logo->mode == ROCKCHIP_DISPLAY_FULLSCREEN) {
1101 		crtc_state->crtc_x = 0;
1102 		crtc_state->crtc_y = 0;
1103 		crtc_state->crtc_w = hdisplay;
1104 		crtc_state->crtc_h = vdisplay;
1105 	} else {
1106 		if (crtc_state->src_w >= hdisplay) {
1107 			crtc_state->crtc_x = 0;
1108 			crtc_state->crtc_w = hdisplay;
1109 		} else {
1110 			crtc_state->crtc_x = (hdisplay - crtc_state->src_w) / 2;
1111 			crtc_state->crtc_w = crtc_state->src_w;
1112 		}
1113 
1114 		if (crtc_state->src_h >= vdisplay) {
1115 			crtc_state->crtc_y = 0;
1116 			crtc_state->crtc_h = vdisplay;
1117 		} else {
1118 			crtc_state->crtc_y = (vdisplay - crtc_state->src_h) / 2;
1119 			crtc_state->crtc_h = crtc_state->src_h;
1120 		}
1121 	}
1122 
1123 	display_set_plane(state);
1124 	display_enable(state);
1125 
1126 	return 0;
1127 }
1128 
1129 static int get_crtc_id(ofnode connect)
1130 {
1131 	int phandle;
1132 	struct device_node *remote;
1133 	int val;
1134 
1135 	phandle = ofnode_read_u32_default(connect, "remote-endpoint", -1);
1136 	if (phandle < 0)
1137 		goto err;
1138 	remote = of_find_node_by_phandle(phandle);
1139 	val = ofnode_read_u32_default(np_to_ofnode(remote), "reg", -1);
1140 	if (val < 0)
1141 		goto err;
1142 
1143 	return val;
1144 err:
1145 	printf("Can't get crtc id, default set to id = 0\n");
1146 	return 0;
1147 }
1148 
1149 static int get_crtc_mcu_mode(struct crtc_state *crtc_state)
1150 {
1151 	ofnode mcu_node;
1152 	int total_pixel, cs_pst, cs_pend, rw_pst, rw_pend;
1153 
1154 	mcu_node = dev_read_subnode(crtc_state->dev, "mcu-timing");
1155 	if (!ofnode_valid(mcu_node))
1156 		return -ENODEV;
1157 
1158 #define FDT_GET_MCU_INT(val, name) \
1159 	do { \
1160 		val = ofnode_read_s32_default(mcu_node, name, -1); \
1161 		if (val < 0) { \
1162 			printf("Can't get %s\n", name); \
1163 			return -ENXIO; \
1164 		} \
1165 	} while (0)
1166 
1167 	FDT_GET_MCU_INT(total_pixel, "mcu-pix-total");
1168 	FDT_GET_MCU_INT(cs_pst, "mcu-cs-pst");
1169 	FDT_GET_MCU_INT(cs_pend, "mcu-cs-pend");
1170 	FDT_GET_MCU_INT(rw_pst, "mcu-rw-pst");
1171 	FDT_GET_MCU_INT(rw_pend, "mcu-rw-pend");
1172 
1173 	crtc_state->mcu_timing.mcu_pix_total = total_pixel;
1174 	crtc_state->mcu_timing.mcu_cs_pst = cs_pst;
1175 	crtc_state->mcu_timing.mcu_cs_pend = cs_pend;
1176 	crtc_state->mcu_timing.mcu_rw_pst = rw_pst;
1177 	crtc_state->mcu_timing.mcu_rw_pend = rw_pend;
1178 
1179 	return 0;
1180 }
1181 
1182 struct rockchip_logo_cache *find_or_alloc_logo_cache(const char *bmp)
1183 {
1184 	struct rockchip_logo_cache *tmp, *logo_cache = NULL;
1185 
1186 	list_for_each_entry(tmp, &logo_cache_list, head) {
1187 		if (!strcmp(tmp->name, bmp)) {
1188 			logo_cache = tmp;
1189 			break;
1190 		}
1191 	}
1192 
1193 	if (!logo_cache) {
1194 		logo_cache = malloc(sizeof(*logo_cache));
1195 		if (!logo_cache) {
1196 			printf("failed to alloc memory for logo cache\n");
1197 			return NULL;
1198 		}
1199 		memset(logo_cache, 0, sizeof(*logo_cache));
1200 		strcpy(logo_cache->name, bmp);
1201 		INIT_LIST_HEAD(&logo_cache->head);
1202 		list_add_tail(&logo_cache->head, &logo_cache_list);
1203 	}
1204 
1205 	return logo_cache;
1206 }
1207 
1208 /* Note: used only for rkfb kernel driver */
1209 static int load_kernel_bmp_logo(struct logo_info *logo, const char *bmp_name)
1210 {
1211 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
1212 	void *dst = NULL;
1213 	int len, size;
1214 	struct bmp_header *header;
1215 
1216 	if (!logo || !bmp_name)
1217 		return -EINVAL;
1218 
1219 	header = malloc(RK_BLK_SIZE);
1220 	if (!header)
1221 		return -ENOMEM;
1222 
1223 	len = rockchip_read_resource_file(header, bmp_name, 0, RK_BLK_SIZE);
1224 	if (len != RK_BLK_SIZE) {
1225 		free(header);
1226 		return -EINVAL;
1227 	}
1228 	size = get_unaligned_le32(&header->file_size);
1229 	dst = (void *)(memory_start + MEMORY_POOL_SIZE / 2);
1230 	len = rockchip_read_resource_file(dst, bmp_name, 0, size);
1231 	if (len != size) {
1232 		printf("failed to load bmp %s\n", bmp_name);
1233 		free(header);
1234 		return -ENOENT;
1235 	}
1236 
1237 	logo->mem = dst;
1238 #endif
1239 
1240 	return 0;
1241 }
1242 
1243 static int load_bmp_logo(struct logo_info *logo, const char *bmp_name)
1244 {
1245 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE
1246 	struct rockchip_logo_cache *logo_cache;
1247 	struct bmp_header *header;
1248 	void *dst = NULL, *pdst;
1249 	int size, len;
1250 	int ret = 0;
1251 	int reserved = 0;
1252 	int dst_size;
1253 
1254 	if (!logo || !bmp_name)
1255 		return -EINVAL;
1256 	logo_cache = find_or_alloc_logo_cache(bmp_name);
1257 	if (!logo_cache)
1258 		return -ENOMEM;
1259 
1260 	if (logo_cache->logo.mem) {
1261 		memcpy(logo, &logo_cache->logo, sizeof(*logo));
1262 		return 0;
1263 	}
1264 
1265 	header = malloc(RK_BLK_SIZE);
1266 	if (!header)
1267 		return -ENOMEM;
1268 
1269 	len = rockchip_read_resource_file(header, bmp_name, 0, RK_BLK_SIZE);
1270 	if (len != RK_BLK_SIZE) {
1271 		ret = -EINVAL;
1272 		goto free_header;
1273 	}
1274 
1275 	logo->bpp = get_unaligned_le16(&header->bit_count);
1276 	logo->width = get_unaligned_le32(&header->width);
1277 	logo->height = get_unaligned_le32(&header->height);
1278 	dst_size = logo->width * logo->height * logo->bpp >> 3;
1279 	reserved = get_unaligned_le32(&header->reserved);
1280 	if (logo->height < 0)
1281 	    logo->height = -logo->height;
1282 	size = get_unaligned_le32(&header->file_size);
1283 	if (!can_direct_logo(logo->bpp)) {
1284 		if (size > MEMORY_POOL_SIZE) {
1285 			printf("failed to use boot buf as temp bmp buffer\n");
1286 			ret = -ENOMEM;
1287 			goto free_header;
1288 		}
1289 		pdst = get_display_buffer(size);
1290 
1291 	} else {
1292 		pdst = get_display_buffer(size);
1293 		dst = pdst;
1294 	}
1295 
1296 	len = rockchip_read_resource_file(pdst, bmp_name, 0, size);
1297 	if (len != size) {
1298 		printf("failed to load bmp %s\n", bmp_name);
1299 		ret = -ENOENT;
1300 		goto free_header;
1301 	}
1302 
1303 	if (!can_direct_logo(logo->bpp)) {
1304 		/*
1305 		 * TODO: force use 16bpp if bpp less than 16;
1306 		 */
1307 		logo->bpp = (logo->bpp <= 16) ? 16 : logo->bpp;
1308 		dst_size = logo->width * logo->height * logo->bpp >> 3;
1309 		dst = get_display_buffer(dst_size);
1310 		if (!dst) {
1311 			ret = -ENOMEM;
1312 			goto free_header;
1313 		}
1314 		if (bmpdecoder(pdst, dst, logo->bpp)) {
1315 			printf("failed to decode bmp %s\n", bmp_name);
1316 			ret = -EINVAL;
1317 			goto free_header;
1318 		}
1319 
1320 		logo->offset = 0;
1321 		logo->ymirror = 0;
1322 	} else {
1323 		logo->offset = get_unaligned_le32(&header->data_offset);
1324 		if (reserved == BMP_PROCESSED_FLAG)
1325 			logo->ymirror = 0;
1326 		else
1327 			logo->ymirror = 1;
1328 	}
1329 	logo->mem = dst;
1330 
1331 	memcpy(&logo_cache->logo, logo, sizeof(*logo));
1332 
1333 	flush_dcache_range((ulong)dst, ALIGN((ulong)dst + dst_size, CONFIG_SYS_CACHELINE_SIZE));
1334 
1335 free_header:
1336 
1337 	free(header);
1338 
1339 	return ret;
1340 #else
1341 	return -EINVAL;
1342 #endif
1343 }
1344 
1345 void rockchip_show_fbbase(ulong fbbase)
1346 {
1347 	struct display_state *s;
1348 
1349 	list_for_each_entry(s, &rockchip_display_list, head) {
1350 		s->logo.mode = ROCKCHIP_DISPLAY_FULLSCREEN;
1351 		s->logo.mem = (char *)fbbase;
1352 		s->logo.width = DRM_ROCKCHIP_FB_WIDTH;
1353 		s->logo.height = DRM_ROCKCHIP_FB_HEIGHT;
1354 		s->logo.bpp = 32;
1355 		s->logo.ymirror = 0;
1356 
1357 		display_logo(s);
1358 	}
1359 }
1360 
1361 int rockchip_show_bmp(const char *bmp)
1362 {
1363 	struct display_state *s;
1364 	int ret = 0;
1365 
1366 	if (!bmp) {
1367 		list_for_each_entry(s, &rockchip_display_list, head)
1368 			display_disable(s);
1369 		return -ENOENT;
1370 	}
1371 
1372 	list_for_each_entry(s, &rockchip_display_list, head) {
1373 		s->logo.mode = s->charge_logo_mode;
1374 		if (load_bmp_logo(&s->logo, bmp))
1375 			continue;
1376 		ret = display_logo(s);
1377 	}
1378 
1379 	return ret;
1380 }
1381 
1382 int rockchip_show_logo(void)
1383 {
1384 	struct display_state *s;
1385 	int ret = 0;
1386 
1387 	list_for_each_entry(s, &rockchip_display_list, head) {
1388 		s->logo.mode = s->logo_mode;
1389 		if (load_bmp_logo(&s->logo, s->ulogo_name))
1390 			printf("failed to display uboot logo\n");
1391 		else
1392 			ret = display_logo(s);
1393 
1394 		/* Load kernel bmp in rockchip_display_fixup() later */
1395 	}
1396 
1397 	return ret;
1398 }
1399 
1400 enum {
1401 	PORT_DIR_IN,
1402 	PORT_DIR_OUT,
1403 };
1404 
1405 static struct rockchip_panel *rockchip_of_find_panel(struct udevice *dev)
1406 {
1407 	ofnode panel_node, ports, port, ep, port_parent_node;
1408 	struct udevice *panel_dev;
1409 	int ret;
1410 
1411 	panel_node = dev_read_subnode(dev, "panel");
1412 	if (ofnode_valid(panel_node) && ofnode_is_available(panel_node)) {
1413 		ret = uclass_get_device_by_ofnode(UCLASS_PANEL, panel_node,
1414 						  &panel_dev);
1415 		if (!ret)
1416 			goto found;
1417 	}
1418 
1419 	ports = dev_read_subnode(dev, "ports");
1420 	if (!ofnode_valid(ports))
1421 		return NULL;
1422 
1423 	ofnode_for_each_subnode(port, ports) {
1424 		u32 reg;
1425 
1426 		if (ofnode_read_u32(port, "reg", &reg))
1427 			continue;
1428 
1429 		if (reg != PORT_DIR_OUT)
1430 			continue;
1431 
1432 		ofnode_for_each_subnode(ep, port) {
1433 			ofnode _ep, _port;
1434 			uint phandle;
1435 			bool is_ports_node = false;
1436 
1437 			if (ofnode_read_u32(ep, "remote-endpoint", &phandle))
1438 				continue;
1439 
1440 			_ep = ofnode_get_by_phandle(phandle);
1441 			if (!ofnode_valid(_ep))
1442 				continue;
1443 
1444 			_port = ofnode_get_parent(_ep);
1445 			if (!ofnode_valid(_port))
1446 				continue;
1447 
1448 			port_parent_node = ofnode_get_parent(_port);
1449 			is_ports_node = strstr(port_parent_node.np->full_name, "ports") ? 1 : 0;
1450 			if (is_ports_node)
1451 				panel_node = ofnode_get_parent(port_parent_node);
1452 			else
1453 				panel_node = ofnode_get_parent(_port);
1454 			if (!ofnode_valid(panel_node))
1455 				continue;
1456 
1457 			ret = uclass_get_device_by_ofnode(UCLASS_PANEL,
1458 							  panel_node,
1459 							  &panel_dev);
1460 			if (!ret)
1461 				goto found;
1462 		}
1463 	}
1464 
1465 	return NULL;
1466 
1467 found:
1468 	return (struct rockchip_panel *)dev_get_driver_data(panel_dev);
1469 }
1470 
1471 static struct rockchip_bridge *rockchip_of_find_bridge(struct udevice *conn_dev)
1472 {
1473 	ofnode node, ports, port, ep;
1474 	struct udevice *dev;
1475 	int ret;
1476 
1477 	ports = dev_read_subnode(conn_dev, "ports");
1478 	if (!ofnode_valid(ports))
1479 		return NULL;
1480 
1481 	ofnode_for_each_subnode(port, ports) {
1482 		u32 reg;
1483 
1484 		if (ofnode_read_u32(port, "reg", &reg))
1485 			continue;
1486 
1487 		if (reg != PORT_DIR_OUT)
1488 			continue;
1489 
1490 		ofnode_for_each_subnode(ep, port) {
1491 			ofnode _ep, _port, _ports;
1492 			uint phandle;
1493 
1494 			if (ofnode_read_u32(ep, "remote-endpoint", &phandle))
1495 				continue;
1496 
1497 			_ep = ofnode_get_by_phandle(phandle);
1498 			if (!ofnode_valid(_ep))
1499 				continue;
1500 
1501 			_port = ofnode_get_parent(_ep);
1502 			if (!ofnode_valid(_port))
1503 				continue;
1504 
1505 			_ports = ofnode_get_parent(_port);
1506 			if (!ofnode_valid(_ports))
1507 				continue;
1508 
1509 			node = ofnode_get_parent(_ports);
1510 			if (!ofnode_valid(node))
1511 				continue;
1512 
1513 			ret = uclass_get_device_by_ofnode(UCLASS_VIDEO_BRIDGE,
1514 							  node, &dev);
1515 			if (!ret)
1516 				goto found;
1517 		}
1518 	}
1519 
1520 	return NULL;
1521 
1522 found:
1523 	return (struct rockchip_bridge *)dev_get_driver_data(dev);
1524 }
1525 
1526 static struct udevice *rockchip_of_find_connector(ofnode endpoint)
1527 {
1528 	ofnode ep, port, ports, conn;
1529 	uint phandle;
1530 	struct udevice *dev;
1531 	int ret;
1532 
1533 	if (ofnode_read_u32(endpoint, "remote-endpoint", &phandle))
1534 		return NULL;
1535 
1536 	ep = ofnode_get_by_phandle(phandle);
1537 	if (!ofnode_valid(ep) || !ofnode_is_available(ep))
1538 		return NULL;
1539 
1540 	port = ofnode_get_parent(ep);
1541 	if (!ofnode_valid(port))
1542 		return NULL;
1543 
1544 	ports = ofnode_get_parent(port);
1545 	if (!ofnode_valid(ports))
1546 		return NULL;
1547 
1548 	conn = ofnode_get_parent(ports);
1549 	if (!ofnode_valid(conn) || !ofnode_is_available(conn))
1550 		return NULL;
1551 
1552 	ret = uclass_get_device_by_ofnode(UCLASS_DISPLAY, conn, &dev);
1553 	if (ret)
1554 		return NULL;
1555 
1556 	return dev;
1557 }
1558 
1559 static bool rockchip_get_display_path_status(ofnode endpoint)
1560 {
1561 	ofnode ep;
1562 	uint phandle;
1563 
1564 	if (ofnode_read_u32(endpoint, "remote-endpoint", &phandle))
1565 		return false;
1566 
1567 	ep = ofnode_get_by_phandle(phandle);
1568 	if (!ofnode_valid(ep) || !ofnode_is_available(ep))
1569 		return false;
1570 
1571 	return true;
1572 }
1573 
1574 static struct rockchip_phy *rockchip_of_find_phy(struct udevice *dev)
1575 {
1576 	struct udevice *phy_dev;
1577 	int ret;
1578 
1579 	ret = uclass_get_device_by_phandle(UCLASS_PHY, dev, "phys", &phy_dev);
1580 	if (ret)
1581 		return NULL;
1582 
1583 	return (struct rockchip_phy *)dev_get_driver_data(phy_dev);
1584 }
1585 
1586 #if defined(CONFIG_ROCKCHIP_RK3568)
1587 static int rockchip_display_fixup_dts(void *blob)
1588 {
1589 	ofnode route_node, route_subnode, conn_ep, conn_port;
1590 	const struct device_node *route_sub_devnode;
1591 	const struct device_node *ep_node, *conn_ep_dev_node;
1592 	u32 phandle;
1593 	int conn_ep_offset;
1594 	const char *route_sub_path, *path;
1595 
1596 	/* Don't go further if new variant after
1597 	 * reading PMUGRF_SOC_CON15
1598 	 */
1599 	if ((readl(0xfdc20100) & GENMASK(15, 14)))
1600 		return 0;
1601 
1602 	route_node = ofnode_path("/display-subsystem/route");
1603 	if (!ofnode_valid(route_node))
1604 		return -EINVAL;
1605 
1606 	ofnode_for_each_subnode(route_subnode, route_node) {
1607 		if (!ofnode_is_available(route_subnode))
1608 			continue;
1609 
1610 		route_sub_devnode = ofnode_to_np(route_subnode);
1611 		route_sub_path = route_sub_devnode->full_name;
1612 		if (!strstr(ofnode_get_name(route_subnode), "dsi") &&
1613 		    !strstr(ofnode_get_name(route_subnode), "edp"))
1614 			return 0;
1615 
1616 		phandle = ofnode_read_u32_default(route_subnode, "connect", -1);
1617 		if (phandle < 0) {
1618 			printf("Warn: can't find connect node's handle\n");
1619 			continue;
1620 		}
1621 
1622 		ep_node = of_find_node_by_phandle(phandle);
1623 		if (!ofnode_valid(np_to_ofnode(ep_node))) {
1624 			printf("Warn: can't find endpoint node from phandle\n");
1625 			continue;
1626 		}
1627 
1628 		ofnode_read_u32(np_to_ofnode(ep_node), "remote-endpoint", &phandle);
1629 		conn_ep = ofnode_get_by_phandle(phandle);
1630 		if (!ofnode_valid(conn_ep) || !ofnode_is_available(conn_ep))
1631 			return -ENODEV;
1632 
1633 		conn_port = ofnode_get_parent(conn_ep);
1634 		if (!ofnode_valid(conn_port))
1635 			return -ENODEV;
1636 
1637 		ofnode_for_each_subnode(conn_ep, conn_port) {
1638 			conn_ep_dev_node = ofnode_to_np(conn_ep);
1639 			path = conn_ep_dev_node->full_name;
1640 			ofnode_read_u32(conn_ep, "remote-endpoint", &phandle);
1641 			conn_ep_offset = fdt_path_offset(blob, path);
1642 
1643 			if (!ofnode_is_available(conn_ep) &&
1644 			    strstr(ofnode_get_name(conn_ep), "endpoint@0")) {
1645 				do_fixup_by_path_u32(blob, route_sub_path,
1646 						     "connect", phandle, 1);
1647 				fdt_status_okay(blob, conn_ep_offset);
1648 
1649 			} else if (ofnode_is_available(conn_ep) &&
1650 				   strstr(ofnode_get_name(conn_ep), "endpoint@1")) {
1651 				fdt_status_disabled(blob, conn_ep_offset);
1652 			}
1653 		}
1654 	}
1655 
1656 	return 0;
1657 }
1658 #endif
1659 
1660 static int rockchip_display_probe(struct udevice *dev)
1661 {
1662 	struct video_priv *uc_priv = dev_get_uclass_priv(dev);
1663 	struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
1664 	const void *blob = gd->fdt_blob;
1665 	int phandle;
1666 	struct udevice *crtc_dev, *conn_dev;
1667 	struct rockchip_crtc *crtc;
1668 	const struct rockchip_connector *conn;
1669 	struct rockchip_panel *panel = NULL;
1670 	struct rockchip_bridge *bridge = NULL;
1671 	struct rockchip_phy *phy = NULL;
1672 	struct display_state *s;
1673 	const char *name;
1674 	int ret;
1675 	ofnode node, route_node, timing_node;
1676 	struct device_node *port_node, *vop_node, *ep_node, *port_parent_node;
1677 	struct public_phy_data *data;
1678 	bool is_ports_node = false;
1679 
1680 #if defined(CONFIG_ROCKCHIP_RK3568)
1681 	rockchip_display_fixup_dts((void *)blob);
1682 #endif
1683 
1684 	/* Before relocation we don't need to do anything */
1685 	if (!(gd->flags & GD_FLG_RELOC))
1686 		return 0;
1687 
1688 	data = malloc(sizeof(struct public_phy_data));
1689 	if (!data) {
1690 		printf("failed to alloc phy data\n");
1691 		return -ENOMEM;
1692 	}
1693 	data->phy_init = false;
1694 
1695 	init_display_buffer(plat->base);
1696 
1697 	route_node = dev_read_subnode(dev, "route");
1698 	if (!ofnode_valid(route_node))
1699 		return -ENODEV;
1700 
1701 	ofnode_for_each_subnode(node, route_node) {
1702 		if (!ofnode_is_available(node))
1703 			continue;
1704 		phandle = ofnode_read_u32_default(node, "connect", -1);
1705 		if (phandle < 0) {
1706 			printf("Warn: can't find connect node's handle\n");
1707 			continue;
1708 		}
1709 		ep_node = of_find_node_by_phandle(phandle);
1710 		if (!ofnode_valid(np_to_ofnode(ep_node))) {
1711 			printf("Warn: can't find endpoint node from phandle\n");
1712 			continue;
1713 		}
1714 		port_node = of_get_parent(ep_node);
1715 		if (!ofnode_valid(np_to_ofnode(port_node))) {
1716 			printf("Warn: can't find port node from phandle\n");
1717 			continue;
1718 		}
1719 
1720 		port_parent_node = of_get_parent(port_node);
1721 		if (!ofnode_valid(np_to_ofnode(port_parent_node))) {
1722 			printf("Warn: can't find port parent node from phandle\n");
1723 			continue;
1724 		}
1725 
1726 		is_ports_node = strstr(port_parent_node->full_name, "ports") ? 1 : 0;
1727 		if (is_ports_node) {
1728 			vop_node = of_get_parent(port_parent_node);
1729 			if (!ofnode_valid(np_to_ofnode(vop_node))) {
1730 				printf("Warn: can't find crtc node from phandle\n");
1731 				continue;
1732 			}
1733 		} else {
1734 			vop_node = port_parent_node;
1735 		}
1736 
1737 		ret = uclass_get_device_by_ofnode(UCLASS_VIDEO_CRTC,
1738 						  np_to_ofnode(vop_node),
1739 						  &crtc_dev);
1740 		if (ret) {
1741 			printf("Warn: can't find crtc driver %d\n", ret);
1742 			continue;
1743 		}
1744 		crtc = (struct rockchip_crtc *)dev_get_driver_data(crtc_dev);
1745 
1746 		conn_dev = rockchip_of_find_connector(np_to_ofnode(ep_node));
1747 		if (!conn_dev) {
1748 			printf("Warn: can't find connect driver\n");
1749 			continue;
1750 		}
1751 
1752 		conn = (const struct rockchip_connector *)dev_get_driver_data(conn_dev);
1753 
1754 		phy = rockchip_of_find_phy(conn_dev);
1755 
1756 		bridge = rockchip_of_find_bridge(conn_dev);
1757 		if (bridge)
1758 			panel = rockchip_of_find_panel(bridge->dev);
1759 		else
1760 			panel = rockchip_of_find_panel(conn_dev);
1761 
1762 		s = malloc(sizeof(*s));
1763 		if (!s)
1764 			continue;
1765 
1766 		memset(s, 0, sizeof(*s));
1767 
1768 		INIT_LIST_HEAD(&s->head);
1769 		ret = ofnode_read_string_index(node, "logo,uboot", 0, &name);
1770 		if (!ret)
1771 			memcpy(s->ulogo_name, name, strlen(name));
1772 		ret = ofnode_read_string_index(node, "logo,kernel", 0, &name);
1773 		if (!ret)
1774 			memcpy(s->klogo_name, name, strlen(name));
1775 		ret = ofnode_read_string_index(node, "logo,mode", 0, &name);
1776 		if (!strcmp(name, "fullscreen"))
1777 			s->logo_mode = ROCKCHIP_DISPLAY_FULLSCREEN;
1778 		else
1779 			s->logo_mode = ROCKCHIP_DISPLAY_CENTER;
1780 		ret = ofnode_read_string_index(node, "charge_logo,mode", 0, &name);
1781 		if (!strcmp(name, "fullscreen"))
1782 			s->charge_logo_mode = ROCKCHIP_DISPLAY_FULLSCREEN;
1783 		else
1784 			s->charge_logo_mode = ROCKCHIP_DISPLAY_CENTER;
1785 
1786 		s->force_output = ofnode_read_bool(node, "force-output");
1787 
1788 		if (s->force_output) {
1789 			timing_node = ofnode_find_subnode(node, "force_timing");
1790 			ret = display_get_force_timing_from_dts(timing_node, &s->force_mode);
1791 			if (ofnode_read_u32(node, "force-bus-format", &s->force_bus_format))
1792 				s->force_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
1793 		}
1794 
1795 		s->blob = blob;
1796 		s->panel_state.panel = panel;
1797 		s->conn_state.node = conn_dev->node;
1798 		s->conn_state.dev = conn_dev;
1799 		s->conn_state.connector = conn;
1800 		s->conn_state.phy = phy;
1801 		s->conn_state.bridge = bridge;
1802 		s->conn_state.overscan.left_margin = 100;
1803 		s->conn_state.overscan.right_margin = 100;
1804 		s->conn_state.overscan.top_margin = 100;
1805 		s->conn_state.overscan.bottom_margin = 100;
1806 		s->crtc_state.node = np_to_ofnode(vop_node);
1807 		s->crtc_state.dev = crtc_dev;
1808 		s->crtc_state.crtc = crtc;
1809 		s->crtc_state.crtc_id = get_crtc_id(np_to_ofnode(ep_node));
1810 		s->node = node;
1811 
1812 		if (is_ports_node) { /* only vop2 will get into here */
1813 			ofnode vp_node = np_to_ofnode(port_node);
1814 			static bool get_plane_mask_from_dts;
1815 
1816 			s->crtc_state.ports_node = port_parent_node;
1817 			if (!get_plane_mask_from_dts) {
1818 				ofnode vp_sub_node;
1819 				int vp_id = 0;
1820 				bool vp_enable = false;
1821 
1822 				ofnode_for_each_subnode(vp_node, np_to_ofnode(port_parent_node)) {
1823 					int cursor_plane = -1;
1824 
1825 					vp_id = ofnode_read_u32_default(vp_node, "reg", 0);
1826 					ret = ofnode_read_u32_default(vp_node, "rockchip,plane-mask", 0);
1827 
1828 					cursor_plane = ofnode_read_u32_default(vp_node, "cursor-win-id", -1);
1829 					s->crtc_state.crtc->vps[vp_id].cursor_plane = cursor_plane;
1830 					if (ret) {
1831 						int primary_plane = 0;
1832 
1833 						s->crtc_state.crtc->vps[vp_id].plane_mask = ret;
1834 						s->crtc_state.crtc->assign_plane |= true;
1835 						primary_plane = ofnode_read_u32_default(vp_node, "rockchip,primary-plane", 0);
1836 						printf("get vp%d plane mask:0x%x, primary id:%d, cursor_plane:%d, from dts\n",
1837 						       vp_id,
1838 						       s->crtc_state.crtc->vps[vp_id].plane_mask,
1839 						       primary_plane,
1840 						       cursor_plane);
1841 					}
1842 
1843 					/* To check current vp status */
1844 					vp_enable = false;
1845 					ofnode_for_each_subnode(vp_sub_node, vp_node)
1846 						vp_enable |= rockchip_get_display_path_status(vp_sub_node);
1847 					s->crtc_state.crtc->vps[vp_id].enable = vp_enable;
1848 				}
1849 				get_plane_mask_from_dts = true;
1850 			}
1851 		}
1852 
1853 		if (bridge)
1854 			bridge->state = s;
1855 
1856 		if (panel)
1857 			panel->state = s;
1858 
1859 		get_crtc_mcu_mode(&s->crtc_state);
1860 
1861 		ret = ofnode_read_u32_default(s->crtc_state.node,
1862 					      "rockchip,dual-channel-swap", 0);
1863 		s->crtc_state.dual_channel_swap = ret;
1864 		if (connector_panel_init(s)) {
1865 			printf("Warn: Failed to init panel drivers\n");
1866 			free(s);
1867 			continue;
1868 		}
1869 
1870 		if (connector_phy_init(s, data)) {
1871 			printf("Warn: Failed to init phy drivers\n");
1872 			free(s);
1873 			continue;
1874 		}
1875 		list_add_tail(&s->head, &rockchip_display_list);
1876 	}
1877 
1878 	if (list_empty(&rockchip_display_list)) {
1879 		debug("Failed to found available display route\n");
1880 		return -ENODEV;
1881 	}
1882 	rockchip_get_baseparameter();
1883 	display_pre_init();
1884 
1885 	uc_priv->xsize = DRM_ROCKCHIP_FB_WIDTH;
1886 	uc_priv->ysize = DRM_ROCKCHIP_FB_HEIGHT;
1887 	uc_priv->bpix = VIDEO_BPP32;
1888 
1889 	#ifdef CONFIG_DRM_ROCKCHIP_VIDEO_FRAMEBUFFER
1890 	rockchip_show_fbbase(plat->base);
1891 	video_set_flush_dcache(dev, true);
1892 	#endif
1893 
1894 	return 0;
1895 }
1896 
1897 void rockchip_display_fixup(void *blob)
1898 {
1899 	const struct rockchip_connector_funcs *conn_funcs;
1900 	const struct rockchip_crtc_funcs *crtc_funcs;
1901 	const struct rockchip_connector *conn;
1902 	const struct rockchip_crtc *crtc;
1903 	struct display_state *s;
1904 	int offset;
1905 	const struct device_node *np;
1906 	const char *path;
1907 
1908 	if (fdt_node_offset_by_compatible(blob, 0, "rockchip,drm-logo") >= 0) {
1909 		list_for_each_entry(s, &rockchip_display_list, head)
1910 			load_bmp_logo(&s->logo, s->klogo_name);
1911 
1912 		if (!get_display_size())
1913 			return;
1914 
1915 		offset = fdt_update_reserved_memory(blob, "rockchip,drm-logo",
1916 						    (u64)memory_start,
1917 						    (u64)get_display_size());
1918 		if (offset < 0)
1919 			printf("failed to reserve drm-loader-logo memory\n");
1920 
1921 		offset = fdt_update_reserved_memory(blob, "rockchip,drm-cubic-lut",
1922 						    (u64)cubic_lut_memory_start,
1923 						    (u64)get_cubic_memory_size());
1924 		if (offset < 0)
1925 			printf("failed to reserve drm-cubic-lut memory\n");
1926 	} else {
1927 		printf("can't found rockchip,drm-logo, use rockchip,fb-logo\n");
1928 		/* Compatible with rkfb display, only need reserve memory */
1929 		offset = fdt_update_reserved_memory(blob, "rockchip,fb-logo",
1930 						    (u64)memory_start,
1931 						    MEMORY_POOL_SIZE);
1932 		if (offset < 0)
1933 			printf("failed to reserve fb-loader-logo memory\n");
1934 		else
1935 			list_for_each_entry(s, &rockchip_display_list, head)
1936 				load_kernel_bmp_logo(&s->logo, s->klogo_name);
1937 		return;
1938 	}
1939 
1940 	list_for_each_entry(s, &rockchip_display_list, head) {
1941 		conn = s->conn_state.connector;
1942 		if (!conn)
1943 			continue;
1944 		conn_funcs = conn->funcs;
1945 		if (!conn_funcs) {
1946 			printf("failed to get exist connector\n");
1947 			continue;
1948 		}
1949 
1950 		crtc = s->crtc_state.crtc;
1951 		if (!crtc)
1952 			continue;
1953 
1954 		crtc_funcs = crtc->funcs;
1955 		if (!crtc_funcs) {
1956 			printf("failed to get exist crtc\n");
1957 			continue;
1958 		}
1959 
1960 		if (crtc_funcs->fixup_dts)
1961 			crtc_funcs->fixup_dts(s, blob);
1962 
1963 		if (conn_funcs->fixup_dts)
1964 			conn_funcs->fixup_dts(s, blob);
1965 
1966 		np = ofnode_to_np(s->node);
1967 		path = np->full_name;
1968 		fdt_increase_size(blob, 0x400);
1969 #define FDT_SET_U32(name, val) \
1970 		do_fixup_by_path_u32(blob, path, name, val, 1);
1971 
1972 		offset = s->logo.offset + (u32)(unsigned long)s->logo.mem
1973 			 - memory_start;
1974 		FDT_SET_U32("logo,offset", offset);
1975 		FDT_SET_U32("logo,width", s->logo.width);
1976 		FDT_SET_U32("logo,height", s->logo.height);
1977 		FDT_SET_U32("logo,bpp", s->logo.bpp);
1978 		FDT_SET_U32("logo,ymirror", s->logo.ymirror);
1979 		FDT_SET_U32("video,clock", s->conn_state.mode.clock);
1980 		FDT_SET_U32("video,hdisplay", s->conn_state.mode.hdisplay);
1981 		FDT_SET_U32("video,vdisplay", s->conn_state.mode.vdisplay);
1982 		FDT_SET_U32("video,crtc_hsync_end", s->conn_state.mode.crtc_hsync_end);
1983 		FDT_SET_U32("video,crtc_vsync_end", s->conn_state.mode.crtc_vsync_end);
1984 		FDT_SET_U32("video,vrefresh",
1985 			    drm_mode_vrefresh(&s->conn_state.mode));
1986 		FDT_SET_U32("video,flags", s->conn_state.mode.flags);
1987 		FDT_SET_U32("video,aspect_ratio", s->conn_state.mode.picture_aspect_ratio);
1988 		FDT_SET_U32("overscan,left_margin", s->conn_state.overscan.left_margin);
1989 		FDT_SET_U32("overscan,right_margin", s->conn_state.overscan.right_margin);
1990 		FDT_SET_U32("overscan,top_margin", s->conn_state.overscan.top_margin);
1991 		FDT_SET_U32("overscan,bottom_margin", s->conn_state.overscan.bottom_margin);
1992 
1993 		if (s->conn_state.disp_info) {
1994 			FDT_SET_U32("bcsh,brightness", s->conn_state.disp_info->bcsh_info.brightness);
1995 			FDT_SET_U32("bcsh,contrast", s->conn_state.disp_info->bcsh_info.contrast);
1996 			FDT_SET_U32("bcsh,saturation", s->conn_state.disp_info->bcsh_info.saturation);
1997 			FDT_SET_U32("bcsh,hue", s->conn_state.disp_info->bcsh_info.hue);
1998 		}
1999 
2000 		if (s->conn_state.disp_info->cubic_lut_data.size &&
2001 		    CONFIG_ROCKCHIP_CUBIC_LUT_SIZE)
2002 			FDT_SET_U32("cubic_lut,offset", get_cubic_lut_offset(s->crtc_state.crtc_id));
2003 
2004 #undef FDT_SET_U32
2005 	}
2006 }
2007 
2008 int rockchip_display_bind(struct udevice *dev)
2009 {
2010 	struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
2011 
2012 	plat->size = DRM_ROCKCHIP_FB_SIZE + MEMORY_POOL_SIZE;
2013 
2014 	return 0;
2015 }
2016 
2017 static const struct udevice_id rockchip_display_ids[] = {
2018 	{ .compatible = "rockchip,display-subsystem" },
2019 	{ }
2020 };
2021 
2022 U_BOOT_DRIVER(rockchip_display) = {
2023 	.name	= "rockchip_display",
2024 	.id	= UCLASS_VIDEO,
2025 	.of_match = rockchip_display_ids,
2026 	.bind	= rockchip_display_bind,
2027 	.probe	= rockchip_display_probe,
2028 };
2029 
2030 static int do_rockchip_logo_show(cmd_tbl_t *cmdtp, int flag, int argc,
2031 			char *const argv[])
2032 {
2033 	if (argc != 1)
2034 		return CMD_RET_USAGE;
2035 
2036 	rockchip_show_logo();
2037 
2038 	return 0;
2039 }
2040 
2041 static int do_rockchip_show_bmp(cmd_tbl_t *cmdtp, int flag, int argc,
2042 				char *const argv[])
2043 {
2044 	if (argc != 2)
2045 		return CMD_RET_USAGE;
2046 
2047 	rockchip_show_bmp(argv[1]);
2048 
2049 	return 0;
2050 }
2051 
2052 U_BOOT_CMD(
2053 	rockchip_show_logo, 1, 1, do_rockchip_logo_show,
2054 	"load and display log from resource partition",
2055 	NULL
2056 );
2057 
2058 U_BOOT_CMD(
2059 	rockchip_show_bmp, 2, 1, do_rockchip_show_bmp,
2060 	"load and display bmp from resource partition",
2061 	"    <bmp_name>"
2062 );
2063