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 <memalign.h> 21 #include <video.h> 22 #include <video_rockchip.h> 23 #include <video_bridge.h> 24 #include <dm/device.h> 25 #include <dm/uclass-internal.h> 26 #include <asm/arch-rockchip/resource_img.h> 27 #include <asm/arch-rockchip/cpu.h> 28 29 #include "bmp_helper.h" 30 #include "libnsbmp.h" 31 #include "rockchip_display.h" 32 #include "rockchip_crtc.h" 33 #include "rockchip_connector.h" 34 #include "rockchip_bridge.h" 35 #include "rockchip_phy.h" 36 #include "rockchip_panel.h" 37 #include <dm.h> 38 #include <dm/of_access.h> 39 #include <dm/ofnode.h> 40 #include <asm/io.h> 41 42 #define DRIVER_VERSION "v1.0.1" 43 44 /*********************************************************************** 45 * Rockchip UBOOT DRM driver version 46 * 47 * v1.0.0 : add basic version for rockchip drm driver(hjc) 48 * v1.0.1 : add much dsi update(hjc) 49 * 50 **********************************************************************/ 51 52 #define RK_BLK_SIZE 512 53 #define BMP_PROCESSED_FLAG 8399 54 #define BYTES_PER_PIXEL sizeof(uint32_t) 55 #define MAX_IMAGE_BYTES (8 * 1024 * 1024) 56 57 DECLARE_GLOBAL_DATA_PTR; 58 static LIST_HEAD(rockchip_display_list); 59 static LIST_HEAD(logo_cache_list); 60 61 static unsigned long memory_start; 62 static unsigned long cubic_lut_memory_start; 63 static unsigned long memory_end; 64 static struct base2_info base_parameter; 65 static u32 align_size = PAGE_SIZE; 66 67 /* 68 * the phy types are used by different connectors in public. 69 * The current version only has inno hdmi phy for hdmi and tve. 70 */ 71 enum public_use_phy { 72 NONE, 73 INNO_HDMI_PHY 74 }; 75 76 /* save public phy data */ 77 struct public_phy_data { 78 const struct rockchip_phy *phy_drv; 79 int phy_node; 80 int public_phy_type; 81 bool phy_init; 82 }; 83 84 char* rockchip_get_output_if_name(u32 output_if, char *name) 85 { 86 if (output_if & VOP_OUTPUT_IF_RGB) 87 strcat(name, " RGB"); 88 if (output_if & VOP_OUTPUT_IF_BT1120) 89 strcat(name, " BT1120"); 90 if (output_if & VOP_OUTPUT_IF_BT656) 91 strcat(name, " BT656"); 92 if (output_if & VOP_OUTPUT_IF_LVDS0) 93 strcat(name, " LVDS0"); 94 if (output_if & VOP_OUTPUT_IF_LVDS1) 95 strcat(name, " LVDS1"); 96 if (output_if & VOP_OUTPUT_IF_MIPI0) 97 strcat(name, " MIPI0"); 98 if (output_if & VOP_OUTPUT_IF_MIPI1) 99 strcat(name, " MIPI1"); 100 if (output_if & VOP_OUTPUT_IF_eDP0) 101 strcat(name, " eDP0"); 102 if (output_if & VOP_OUTPUT_IF_eDP1) 103 strcat(name, " eDP1"); 104 if (output_if & VOP_OUTPUT_IF_DP0) 105 strcat(name, " DP0"); 106 if (output_if & VOP_OUTPUT_IF_DP1) 107 strcat(name, " DP1"); 108 if (output_if & VOP_OUTPUT_IF_HDMI0) 109 strcat(name, " HDMI0"); 110 if (output_if & VOP_OUTPUT_IF_HDMI1) 111 strcat(name, " HDMI1"); 112 113 return name; 114 } 115 116 u32 rockchip_drm_get_cycles_per_pixel(u32 bus_format) 117 { 118 switch (bus_format) { 119 case MEDIA_BUS_FMT_RGB565_1X16: 120 case MEDIA_BUS_FMT_RGB666_1X18: 121 case MEDIA_BUS_FMT_RGB888_1X24: 122 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI: 123 return 1; 124 case MEDIA_BUS_FMT_RGB565_2X8_LE: 125 case MEDIA_BUS_FMT_BGR565_2X8_LE: 126 return 2; 127 case MEDIA_BUS_FMT_RGB666_3X6: 128 case MEDIA_BUS_FMT_RGB888_3X8: 129 case MEDIA_BUS_FMT_BGR888_3X8: 130 return 3; 131 case MEDIA_BUS_FMT_RGB888_DUMMY_4X8: 132 case MEDIA_BUS_FMT_BGR888_DUMMY_4X8: 133 return 4; 134 default: 135 return 1; 136 } 137 } 138 139 int rockchip_get_baseparameter(void) 140 { 141 struct blk_desc *dev_desc; 142 disk_partition_t part_info; 143 int block_num; 144 char *baseparameter_buf; 145 int ret = 0; 146 147 dev_desc = rockchip_get_bootdev(); 148 if (!dev_desc) { 149 printf("%s: Could not find device\n", __func__); 150 return -ENOENT; 151 } 152 153 if (part_get_info_by_name(dev_desc, "baseparameter", &part_info) < 0) { 154 printf("Could not find baseparameter partition\n"); 155 return -ENOENT; 156 } 157 158 block_num = BLOCK_CNT(sizeof(base_parameter), dev_desc); 159 baseparameter_buf = memalign(ARCH_DMA_MINALIGN, block_num * dev_desc->blksz); 160 if (!baseparameter_buf) { 161 printf("failed to alloc memory for baseparameter buffer\n"); 162 return -ENOMEM; 163 } 164 165 ret = blk_dread(dev_desc, part_info.start, block_num, (void *)baseparameter_buf); 166 if (ret < 0) { 167 printf("read baseparameter failed\n"); 168 goto out; 169 } 170 171 memcpy(&base_parameter, baseparameter_buf, sizeof(base_parameter)); 172 if (strncasecmp(base_parameter.head_flag, "BASP", 4)) { 173 printf("warning: bad baseparameter\n"); 174 memset(&base_parameter, 0, sizeof(base_parameter)); 175 } 176 rockchip_display_make_crc32_table(); 177 178 out: 179 free(baseparameter_buf); 180 return ret; 181 } 182 183 struct base2_disp_info *rockchip_get_disp_info(int type, int id) 184 { 185 struct base2_disp_info *disp_info; 186 struct base2_disp_header *disp_header; 187 int i = 0, offset = -1; 188 u32 crc_val; 189 u32 base2_length; 190 void *base_parameter_addr = (void *)&base_parameter; 191 192 for (i = 0; i < 8; i++) { 193 disp_header = &base_parameter.disp_header[i]; 194 if (disp_header->connector_type == type && 195 disp_header->connector_id == id) { 196 printf("disp info %d, type:%d, id:%d\n", i, type, id); 197 offset = disp_header->offset; 198 break; 199 } 200 } 201 202 if (offset < 0) 203 return NULL; 204 disp_info = base_parameter_addr + offset; 205 if (disp_info->screen_info[0].type != type || 206 disp_info->screen_info[0].id != id) { 207 printf("base2_disp_info couldn't be found, screen_info type[%d] or id[%d] mismatched\n", 208 disp_info->screen_info[0].type, 209 disp_info->screen_info[0].id); 210 return NULL; 211 } 212 213 if (strncasecmp(disp_info->disp_head_flag, "DISP", 4)) 214 return NULL; 215 216 if (base_parameter.major_version == 3 && base_parameter.minor_version == 0) { 217 crc_val = rockchip_display_crc32c_cal((unsigned char *)disp_info, 218 sizeof(struct base2_disp_info) - 4); 219 if (crc_val != disp_info->crc2) { 220 printf("error: connector type[%d], id[%d] disp info crc2 check error\n", 221 type, id); 222 return NULL; 223 } 224 } else { 225 base2_length = sizeof(struct base2_disp_info) - sizeof(struct csc_info) - 226 sizeof(struct acm_data) - 10 * 1024 - 4; 227 crc_val = rockchip_display_crc32c_cal((unsigned char *)disp_info, base2_length - 4); 228 if (crc_val != disp_info->crc) { 229 printf("error: connector type[%d], id[%d] disp info crc check error\n", 230 type, id); 231 return NULL; 232 } 233 } 234 235 return disp_info; 236 } 237 238 /* check which kind of public phy does connector use */ 239 static int check_public_use_phy(struct rockchip_connector *conn) 240 { 241 int ret = NONE; 242 #ifdef CONFIG_ROCKCHIP_INNO_HDMI_PHY 243 244 if (!strncmp(dev_read_name(conn->dev), "tve", 3) || 245 !strncmp(dev_read_name(conn->dev), "hdmi", 4)) 246 ret = INNO_HDMI_PHY; 247 #endif 248 249 return ret; 250 } 251 252 /* 253 * get public phy driver and initialize it. 254 * The current version only has inno hdmi phy for hdmi and tve. 255 */ 256 static int get_public_phy(struct rockchip_connector *conn, 257 struct public_phy_data *data) 258 { 259 struct rockchip_phy *phy; 260 struct udevice *dev; 261 int ret = 0; 262 263 switch (data->public_phy_type) { 264 case INNO_HDMI_PHY: 265 #if defined(CONFIG_ROCKCHIP_RK3328) 266 ret = uclass_get_device_by_name(UCLASS_PHY, 267 "hdmiphy@ff430000", &dev); 268 #elif defined(CONFIG_ROCKCHIP_RK322X) 269 ret = uclass_get_device_by_name(UCLASS_PHY, 270 "hdmi-phy@12030000", &dev); 271 #else 272 ret = -EINVAL; 273 #endif 274 if (ret) { 275 printf("Warn: can't find phy driver\n"); 276 return 0; 277 } 278 279 phy = (struct rockchip_phy *)dev_get_driver_data(dev); 280 if (!phy) { 281 printf("failed to get phy driver\n"); 282 return 0; 283 } 284 285 ret = rockchip_phy_init(phy); 286 if (ret) { 287 printf("failed to init phy driver\n"); 288 return ret; 289 } 290 conn->phy = phy; 291 292 debug("inno hdmi phy init success, save it\n"); 293 data->phy_drv = conn->phy; 294 data->phy_init = true; 295 return 0; 296 default: 297 return -EINVAL; 298 } 299 } 300 301 static void init_display_buffer(ulong base) 302 { 303 memory_start = ALIGN(base + DRM_ROCKCHIP_FB_SIZE, align_size); 304 memory_end = memory_start; 305 cubic_lut_memory_start = ALIGN(memory_start + MEMORY_POOL_SIZE, align_size); 306 } 307 308 void *get_display_buffer(int size) 309 { 310 unsigned long roundup_memory = roundup(memory_end, PAGE_SIZE); 311 void *buf; 312 313 if (roundup_memory + size > memory_start + MEMORY_POOL_SIZE) { 314 printf("failed to alloc %dbyte memory to display\n", size); 315 return NULL; 316 } 317 buf = (void *)roundup_memory; 318 319 memory_end = roundup_memory + size; 320 321 return buf; 322 } 323 324 static unsigned long get_display_size(void) 325 { 326 return memory_end - memory_start; 327 } 328 329 static unsigned long get_single_cubic_lut_size(void) 330 { 331 ulong cubic_lut_size; 332 int cubic_lut_step = CONFIG_ROCKCHIP_CUBIC_LUT_SIZE; 333 334 /* This is depend on IC designed */ 335 cubic_lut_size = (cubic_lut_step * cubic_lut_step * cubic_lut_step + 1) / 2 * 16; 336 cubic_lut_size = roundup(cubic_lut_size, PAGE_SIZE); 337 338 return cubic_lut_size; 339 } 340 341 static unsigned long get_cubic_lut_offset(int crtc_id) 342 { 343 return crtc_id * get_single_cubic_lut_size(); 344 } 345 346 unsigned long get_cubic_lut_buffer(int crtc_id) 347 { 348 return cubic_lut_memory_start + crtc_id * get_single_cubic_lut_size(); 349 } 350 351 static unsigned long get_cubic_memory_size(void) 352 { 353 /* Max support 4 cubic lut */ 354 return get_single_cubic_lut_size() * 4; 355 } 356 357 bool can_direct_logo(int bpp) 358 { 359 return bpp == 16 || bpp == 32; 360 } 361 362 static int connector_phy_init(struct rockchip_connector *conn, 363 struct public_phy_data *data) 364 { 365 int type; 366 367 /* does this connector use public phy with others */ 368 type = check_public_use_phy(conn); 369 if (type == INNO_HDMI_PHY) { 370 /* there is no public phy was initialized */ 371 if (!data->phy_init) { 372 debug("start get public phy\n"); 373 data->public_phy_type = type; 374 if (get_public_phy(conn, data)) { 375 printf("can't find correct public phy type\n"); 376 free(data); 377 return -EINVAL; 378 } 379 return 0; 380 } 381 382 /* if this phy has been initialized, get it directly */ 383 conn->phy = (struct rockchip_phy *)data->phy_drv; 384 return 0; 385 } 386 387 return 0; 388 } 389 390 int rockchip_ofnode_get_display_mode(ofnode node, struct drm_display_mode *mode, u32 *bus_flags) 391 { 392 int hactive, vactive, pixelclock; 393 int hfront_porch, hback_porch, hsync_len; 394 int vfront_porch, vback_porch, vsync_len; 395 int val, flags = 0; 396 397 #define FDT_GET_BOOL(val, name) \ 398 val = ofnode_read_bool(node, name); 399 400 #define FDT_GET_INT(val, name) \ 401 val = ofnode_read_s32_default(node, name, -1); \ 402 if (val < 0) { \ 403 printf("Can't get %s\n", name); \ 404 return -ENXIO; \ 405 } 406 407 #define FDT_GET_INT_DEFAULT(val, name, default) \ 408 val = ofnode_read_s32_default(node, name, default); 409 410 FDT_GET_INT(hactive, "hactive"); 411 FDT_GET_INT(vactive, "vactive"); 412 FDT_GET_INT(pixelclock, "clock-frequency"); 413 FDT_GET_INT(hsync_len, "hsync-len"); 414 FDT_GET_INT(hfront_porch, "hfront-porch"); 415 FDT_GET_INT(hback_porch, "hback-porch"); 416 FDT_GET_INT(vsync_len, "vsync-len"); 417 FDT_GET_INT(vfront_porch, "vfront-porch"); 418 FDT_GET_INT(vback_porch, "vback-porch"); 419 FDT_GET_INT(val, "hsync-active"); 420 flags |= val ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC; 421 FDT_GET_INT(val, "vsync-active"); 422 flags |= val ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC; 423 424 FDT_GET_BOOL(val, "interlaced"); 425 flags |= val ? DRM_MODE_FLAG_INTERLACE : 0; 426 FDT_GET_BOOL(val, "doublescan"); 427 flags |= val ? DRM_MODE_FLAG_DBLSCAN : 0; 428 FDT_GET_BOOL(val, "doubleclk"); 429 flags |= val ? DISPLAY_FLAGS_DOUBLECLK : 0; 430 431 FDT_GET_INT(val, "de-active"); 432 *bus_flags |= val ? DRM_BUS_FLAG_DE_HIGH : DRM_BUS_FLAG_DE_LOW; 433 FDT_GET_INT(val, "pixelclk-active"); 434 *bus_flags |= val ? DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE : DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE; 435 436 FDT_GET_INT_DEFAULT(val, "screen-rotate", 0); 437 if (val == DRM_MODE_FLAG_XMIRROR) { 438 flags |= DRM_MODE_FLAG_XMIRROR; 439 } else if (val == DRM_MODE_FLAG_YMIRROR) { 440 flags |= DRM_MODE_FLAG_YMIRROR; 441 } else if (val == DRM_MODE_FLAG_XYMIRROR) { 442 flags |= DRM_MODE_FLAG_XMIRROR; 443 flags |= DRM_MODE_FLAG_YMIRROR; 444 } 445 mode->hdisplay = hactive; 446 mode->hsync_start = mode->hdisplay + hfront_porch; 447 mode->hsync_end = mode->hsync_start + hsync_len; 448 mode->htotal = mode->hsync_end + hback_porch; 449 450 mode->vdisplay = vactive; 451 mode->vsync_start = mode->vdisplay + vfront_porch; 452 mode->vsync_end = mode->vsync_start + vsync_len; 453 mode->vtotal = mode->vsync_end + vback_porch; 454 455 mode->clock = pixelclock / 1000; 456 mode->flags = flags; 457 mode->vrefresh = drm_mode_vrefresh(mode); 458 459 return 0; 460 } 461 462 static int display_get_force_timing_from_dts(ofnode node, 463 struct drm_display_mode *mode, 464 u32 *bus_flags) 465 { 466 int ret = 0; 467 468 ret = rockchip_ofnode_get_display_mode(node, mode, bus_flags); 469 470 if (ret) { 471 mode->clock = 74250; 472 mode->flags = 0x5; 473 mode->hdisplay = 1280; 474 mode->hsync_start = 1390; 475 mode->hsync_end = 1430; 476 mode->htotal = 1650; 477 mode->hskew = 0; 478 mode->vdisplay = 720; 479 mode->vsync_start = 725; 480 mode->vsync_end = 730; 481 mode->vtotal = 750; 482 mode->vrefresh = 60; 483 mode->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9; 484 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 485 } 486 487 printf("route node %s force_timing, use %dx%dp%d as default mode\n", 488 ret ? "undefine" : "define", mode->hdisplay, mode->vdisplay, 489 mode->vscan); 490 491 return 0; 492 } 493 494 static int display_get_timing_from_dts(struct rockchip_panel *panel, 495 struct drm_display_mode *mode, 496 u32 *bus_flags) 497 { 498 struct ofnode_phandle_args args; 499 ofnode dt, timing, mcu_panel; 500 int ret; 501 502 mcu_panel = dev_read_subnode(panel->dev, "mcu-panel"); 503 dt = dev_read_subnode(panel->dev, "display-timings"); 504 if (ofnode_valid(dt)) { 505 ret = ofnode_parse_phandle_with_args(dt, "native-mode", NULL, 506 0, 0, &args); 507 if (ret) 508 return ret; 509 510 timing = args.node; 511 } else if (ofnode_valid(mcu_panel)) { 512 dt = ofnode_find_subnode(mcu_panel, "display-timings"); 513 ret = ofnode_parse_phandle_with_args(dt, "native-mode", NULL, 514 0, 0, &args); 515 if (ret) 516 return ret; 517 518 timing = args.node; 519 } else { 520 timing = dev_read_subnode(panel->dev, "panel-timing"); 521 } 522 523 if (!ofnode_valid(timing)) { 524 printf("failed to get display timings from DT\n"); 525 return -ENXIO; 526 } 527 528 rockchip_ofnode_get_display_mode(timing, mode, bus_flags); 529 530 if (IS_ENABLED(CONFIG_ROCKCHIP_RK3568) || IS_ENABLED(CONFIG_ROCKCHIP_RK3588)) { 531 if (mode->hdisplay % 4) { 532 int old_hdisplay = mode->hdisplay; 533 int align = 4 - (mode->hdisplay % 4); 534 535 mode->hdisplay += align; 536 mode->hsync_start += align; 537 mode->hsync_end += align; 538 mode->htotal += align; 539 540 ofnode_write_u32_array(timing, "hactive", (u32 *)&mode->hdisplay, 1); 541 542 printf("WARN: hactive need to be aligned with 4-pixel, %d -> %d\n", 543 old_hdisplay, mode->hdisplay); 544 } 545 } 546 547 return 0; 548 } 549 550 static int display_get_timing(struct display_state *state) 551 { 552 struct connector_state *conn_state = &state->conn_state; 553 struct drm_display_mode *mode = &conn_state->mode; 554 const struct drm_display_mode *m; 555 struct rockchip_panel *panel = conn_state->connector->panel; 556 557 if (panel->funcs->get_mode) 558 return panel->funcs->get_mode(panel, mode); 559 560 if (dev_of_valid(panel->dev) && 561 !display_get_timing_from_dts(panel, mode, &conn_state->bus_flags)) { 562 printf("Using display timing dts\n"); 563 return 0; 564 } 565 566 if (panel->data) { 567 m = (const struct drm_display_mode *)panel->data; 568 memcpy(mode, m, sizeof(*m)); 569 printf("Using display timing from compatible panel driver\n"); 570 return 0; 571 } 572 573 return -ENODEV; 574 } 575 576 static int display_pre_init(void) 577 { 578 struct display_state *state; 579 int ret = 0; 580 581 list_for_each_entry(state, &rockchip_display_list, head) { 582 struct connector_state *conn_state = &state->conn_state; 583 struct crtc_state *crtc_state = &state->crtc_state; 584 struct rockchip_crtc *crtc = crtc_state->crtc; 585 586 ret = rockchip_connector_pre_init(state); 587 if (ret) 588 printf("pre init conn error\n"); 589 590 crtc->vps[crtc_state->crtc_id].output_type = conn_state->type; 591 } 592 return ret; 593 } 594 595 static int display_use_force_mode(struct display_state *state) 596 { 597 struct connector_state *conn_state = &state->conn_state; 598 struct drm_display_mode *mode = &conn_state->mode; 599 600 conn_state->bpc = 8; 601 memcpy(mode, &state->force_mode, sizeof(struct drm_display_mode)); 602 conn_state->bus_format = state->force_bus_format; 603 604 return 0; 605 } 606 607 static int display_get_edid_mode(struct display_state *state) 608 { 609 int ret = 0; 610 struct connector_state *conn_state = &state->conn_state; 611 struct drm_display_mode *mode = &conn_state->mode; 612 int bpc; 613 614 ret = edid_get_drm_mode(conn_state->edid, sizeof(conn_state->edid), mode, &bpc); 615 if (!ret) { 616 conn_state->bpc = bpc; 617 edid_print_info((void *)&conn_state->edid); 618 } else { 619 conn_state->bpc = 8; 620 mode->clock = 74250; 621 mode->flags = 0x5; 622 mode->hdisplay = 1280; 623 mode->hsync_start = 1390; 624 mode->hsync_end = 1430; 625 mode->htotal = 1650; 626 mode->hskew = 0; 627 mode->vdisplay = 720; 628 mode->vsync_start = 725; 629 mode->vsync_end = 730; 630 mode->vtotal = 750; 631 mode->vrefresh = 60; 632 mode->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9; 633 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 634 635 printf("error: %s get mode from edid failed, use 720p60 as default mode\n", 636 state->conn_state.connector->dev->name); 637 } 638 639 return ret; 640 } 641 642 static int display_mode_valid(struct display_state *state) 643 { 644 struct connector_state *conn_state = &state->conn_state; 645 struct rockchip_connector *conn = conn_state->connector; 646 const struct rockchip_connector_funcs *conn_funcs = conn->funcs; 647 struct crtc_state *crtc_state = &state->crtc_state; 648 const struct rockchip_crtc *crtc = crtc_state->crtc; 649 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 650 int ret; 651 652 if (conn_funcs->mode_valid && state->enabled_at_spl == false) { 653 ret = conn_funcs->mode_valid(conn, state); 654 if (ret) 655 return ret; 656 } 657 658 if (crtc_funcs->mode_valid) { 659 ret = crtc_funcs->mode_valid(state); 660 if (ret) 661 return ret; 662 } 663 664 return 0; 665 } 666 667 static int display_mode_fixup(struct display_state *state) 668 { 669 struct crtc_state *crtc_state = &state->crtc_state; 670 const struct rockchip_crtc *crtc = crtc_state->crtc; 671 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 672 int ret; 673 674 if (crtc_funcs->mode_fixup) { 675 ret = crtc_funcs->mode_fixup(state); 676 if (ret) 677 return ret; 678 } 679 680 return 0; 681 } 682 683 static int display_init(struct display_state *state) 684 { 685 struct connector_state *conn_state = &state->conn_state; 686 struct rockchip_connector *conn = conn_state->connector; 687 struct crtc_state *crtc_state = &state->crtc_state; 688 struct rockchip_crtc *crtc = crtc_state->crtc; 689 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 690 struct drm_display_mode *mode = &conn_state->mode; 691 const char *compatible; 692 int ret = 0; 693 static bool __print_once = false; 694 #ifdef CONFIG_SPL_BUILD 695 struct spl_display_info *spl_disp_info = (struct spl_display_info *)CONFIG_SPL_VIDEO_BUF; 696 #endif 697 if (!__print_once) { 698 __print_once = true; 699 printf("Rockchip UBOOT DRM driver version: %s\n", DRIVER_VERSION); 700 } 701 702 if (state->is_init) 703 return 0; 704 705 if (!crtc_funcs) { 706 printf("failed to find crtc functions\n"); 707 return -ENXIO; 708 } 709 710 #ifdef CONFIG_SPL_BUILD 711 if (state->conn_state.type == DRM_MODE_CONNECTOR_HDMIA) 712 state->enabled_at_spl = spl_disp_info->enabled == 1 ? true : false; 713 if (state->enabled_at_spl) 714 printf("HDMI enabled at SPL\n"); 715 #endif 716 if (crtc_state->crtc->active && !crtc_state->ports_node && 717 memcmp(&crtc_state->crtc->active_mode, &conn_state->mode, 718 sizeof(struct drm_display_mode))) { 719 printf("%s has been used for output type: %d, mode: %dx%dp%d\n", 720 crtc_state->dev->name, 721 crtc_state->crtc->active_mode.type, 722 crtc_state->crtc->active_mode.hdisplay, 723 crtc_state->crtc->active_mode.vdisplay, 724 crtc_state->crtc->active_mode.vrefresh); 725 return -ENODEV; 726 } 727 728 if (crtc_funcs->preinit) { 729 ret = crtc_funcs->preinit(state); 730 if (ret) 731 return ret; 732 } 733 734 if (state->enabled_at_spl == false) { 735 ret = rockchip_connector_init(state); 736 if (ret) 737 goto deinit; 738 } 739 740 /* 741 * support hotplug, but not connect; 742 */ 743 #ifdef CONFIG_DRM_ROCKCHIP_TVE 744 if (crtc->hdmi_hpd && conn_state->type == DRM_MODE_CONNECTOR_TV) { 745 printf("hdmi plugin ,skip tve\n"); 746 goto deinit; 747 } 748 #elif defined(CONFIG_DRM_ROCKCHIP_RK1000) 749 if (crtc->hdmi_hpd && conn_state->type == DRM_MODE_CONNECTOR_LVDS) { 750 printf("hdmi plugin ,skip tve\n"); 751 goto deinit; 752 } 753 #endif 754 755 ret = rockchip_connector_detect(state); 756 #if defined(CONFIG_DRM_ROCKCHIP_TVE) || defined(CONFIG_DRM_ROCKCHIP_RK1000) 757 if (conn_state->type == DRM_MODE_CONNECTOR_HDMIA) 758 crtc->hdmi_hpd = ret; 759 if (state->enabled_at_spl) 760 crtc->hdmi_hpd = true; 761 #endif 762 if (!ret && !state->force_output) 763 goto deinit; 764 765 ret = 0; 766 if (state->enabled_at_spl == true) { 767 #ifdef CONFIG_SPL_BUILD 768 struct drm_display_mode *mode = &conn_state->mode; 769 770 memcpy(mode, &spl_disp_info->mode, sizeof(*mode)); 771 conn_state->bus_format = spl_disp_info->bus_format; 772 773 printf("%s get display mode from spl:%dx%d, bus format:0x%x\n", 774 conn->dev->name, mode->hdisplay, mode->vdisplay, conn_state->bus_format); 775 #endif 776 } else if (conn->panel) { 777 ret = display_get_timing(state); 778 if (!ret) 779 conn_state->bpc = conn->panel->bpc; 780 #if defined(CONFIG_I2C_EDID) 781 if (ret < 0 && conn->funcs->get_edid) { 782 rockchip_panel_prepare(conn->panel); 783 ret = conn->funcs->get_edid(conn, state); 784 if (!ret) 785 display_get_edid_mode(state); 786 } 787 #endif 788 } else if (conn->bridge) { 789 ret = video_bridge_read_edid(conn->bridge->dev, 790 conn_state->edid, EDID_SIZE); 791 if (ret > 0) { 792 #if defined(CONFIG_I2C_EDID) 793 ret = display_get_edid_mode(state); 794 #endif 795 } else { 796 ret = video_bridge_get_timing(conn->bridge->dev); 797 } 798 } else if (conn->funcs->get_timing) { 799 ret = conn->funcs->get_timing(conn, state); 800 } else if (conn->funcs->get_edid) { 801 ret = conn->funcs->get_edid(conn, state); 802 #if defined(CONFIG_I2C_EDID) 803 if (!ret) 804 display_get_edid_mode(state); 805 #endif 806 } 807 808 if (!ret && conn_state->secondary) { 809 struct rockchip_connector *connector = conn_state->secondary; 810 811 if (connector->panel) { 812 if (connector->panel->funcs->get_mode) { 813 struct drm_display_mode *_mode = drm_mode_create(); 814 815 ret = connector->panel->funcs->get_mode(connector->panel, _mode); 816 if (!ret && !drm_mode_equal(_mode, mode)) 817 ret = -EINVAL; 818 819 drm_mode_destroy(_mode); 820 } 821 } 822 } 823 824 if (ret && !state->force_output) 825 goto deinit; 826 if (state->force_output) 827 display_use_force_mode(state); 828 829 if (display_mode_valid(state)) 830 goto deinit; 831 832 /* rk356x series drive mipi pixdata on posedge */ 833 compatible = dev_read_string(conn->dev, "compatible"); 834 if (!strcmp(compatible, "rockchip,rk3568-mipi-dsi")) { 835 conn_state->bus_flags &= ~DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE; 836 conn_state->bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE; 837 } 838 839 if (display_mode_fixup(state)) 840 goto deinit; 841 842 if (conn->bridge) 843 rockchip_bridge_mode_set(conn->bridge, &conn_state->mode); 844 845 printf("%s: %s detailed mode clock %u kHz, flags[%x]\n" 846 " H: %04d %04d %04d %04d\n" 847 " V: %04d %04d %04d %04d\n" 848 "bus_format: %x\n", 849 conn->dev->name, 850 state->force_output ? "use force output" : "", 851 mode->clock, mode->flags, 852 mode->hdisplay, mode->hsync_start, 853 mode->hsync_end, mode->htotal, 854 mode->vdisplay, mode->vsync_start, 855 mode->vsync_end, mode->vtotal, 856 conn_state->bus_format); 857 858 if (crtc_funcs->init && state->enabled_at_spl == false) { 859 ret = crtc_funcs->init(state); 860 if (ret) 861 goto deinit; 862 } 863 state->is_init = 1; 864 865 crtc_state->crtc->active = true; 866 memcpy(&crtc_state->crtc->active_mode, 867 &conn_state->mode, sizeof(struct drm_display_mode)); 868 869 return 0; 870 871 deinit: 872 rockchip_connector_deinit(state); 873 return ret; 874 } 875 876 int display_send_mcu_cmd(struct display_state *state, u32 type, u32 val) 877 { 878 struct crtc_state *crtc_state = &state->crtc_state; 879 const struct rockchip_crtc *crtc = crtc_state->crtc; 880 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 881 int ret; 882 883 if (!state->is_init) 884 return -EINVAL; 885 886 if (crtc_funcs->send_mcu_cmd) { 887 ret = crtc_funcs->send_mcu_cmd(state, type, val); 888 if (ret) 889 return ret; 890 } 891 892 return 0; 893 } 894 895 static int display_set_plane(struct display_state *state) 896 { 897 struct crtc_state *crtc_state = &state->crtc_state; 898 const struct rockchip_crtc *crtc = crtc_state->crtc; 899 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 900 int ret; 901 902 if (!state->is_init) 903 return -EINVAL; 904 905 if (crtc_funcs->set_plane) { 906 ret = crtc_funcs->set_plane(state); 907 if (ret) 908 return ret; 909 } 910 911 return 0; 912 } 913 914 static int display_enable(struct display_state *state) 915 { 916 struct crtc_state *crtc_state = &state->crtc_state; 917 const struct rockchip_crtc *crtc = crtc_state->crtc; 918 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 919 920 if (!state->is_init) 921 return -EINVAL; 922 923 if (state->is_enable) 924 return 0; 925 926 if (crtc_funcs->prepare) 927 crtc_funcs->prepare(state); 928 929 if (state->enabled_at_spl == false) 930 rockchip_connector_pre_enable(state); 931 932 if (crtc_funcs->enable) 933 crtc_funcs->enable(state); 934 935 if (state->enabled_at_spl == false) 936 rockchip_connector_enable(state); 937 938 if (crtc_state->soft_te) 939 crtc_funcs->apply_soft_te(state); 940 941 state->is_enable = true; 942 943 return 0; 944 } 945 946 static int display_disable(struct display_state *state) 947 { 948 struct crtc_state *crtc_state = &state->crtc_state; 949 const struct rockchip_crtc *crtc = crtc_state->crtc; 950 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 951 952 if (!state->is_init) 953 return 0; 954 955 if (!state->is_enable) 956 return 0; 957 958 rockchip_connector_disable(state); 959 960 if (crtc_funcs->disable) 961 crtc_funcs->disable(state); 962 963 rockchip_connector_post_disable(state); 964 965 state->is_enable = 0; 966 state->is_init = 0; 967 968 return 0; 969 } 970 971 static int display_check(struct display_state *state) 972 { 973 struct connector_state *conn_state = &state->conn_state; 974 struct rockchip_connector *conn = conn_state->connector; 975 const struct rockchip_connector_funcs *conn_funcs = conn->funcs; 976 struct crtc_state *crtc_state = &state->crtc_state; 977 const struct rockchip_crtc *crtc = crtc_state->crtc; 978 const struct rockchip_crtc_funcs *crtc_funcs = crtc->funcs; 979 int ret; 980 981 if (!state->is_init) 982 return 0; 983 984 if (conn_funcs->check) { 985 ret = conn_funcs->check(conn, state); 986 if (ret) 987 goto check_fail; 988 } 989 990 if (crtc_funcs->check) { 991 ret = crtc_funcs->check(state); 992 if (ret) 993 goto check_fail; 994 } 995 996 if (crtc_funcs->plane_check) { 997 ret = crtc_funcs->plane_check(state); 998 if (ret) 999 goto check_fail; 1000 } 1001 1002 return 0; 1003 1004 check_fail: 1005 state->is_init = false; 1006 return ret; 1007 } 1008 1009 static int display_logo(struct display_state *state) 1010 { 1011 struct crtc_state *crtc_state = &state->crtc_state; 1012 struct connector_state *conn_state = &state->conn_state; 1013 struct logo_info *logo = &state->logo; 1014 int hdisplay, vdisplay, ret; 1015 1016 ret = display_init(state); 1017 if (!state->is_init || ret) 1018 return -ENODEV; 1019 1020 switch (logo->bpp) { 1021 case 16: 1022 crtc_state->format = ROCKCHIP_FMT_RGB565; 1023 break; 1024 case 24: 1025 crtc_state->format = ROCKCHIP_FMT_RGB888; 1026 break; 1027 case 32: 1028 crtc_state->format = ROCKCHIP_FMT_ARGB8888; 1029 break; 1030 default: 1031 printf("can't support bmp bits[%d]\n", logo->bpp); 1032 return -EINVAL; 1033 } 1034 hdisplay = conn_state->mode.crtc_hdisplay; 1035 vdisplay = conn_state->mode.vdisplay; 1036 crtc_state->src_rect.w = logo->width; 1037 crtc_state->src_rect.h = logo->height; 1038 crtc_state->src_rect.x = 0; 1039 crtc_state->src_rect.y = 0; 1040 crtc_state->ymirror = logo->ymirror; 1041 crtc_state->rb_swap = 0; 1042 1043 crtc_state->dma_addr = (u32)(unsigned long)logo->mem + logo->offset; 1044 crtc_state->xvir = ALIGN(crtc_state->src_rect.w * logo->bpp, 32) >> 5; 1045 1046 if (state->logo_mode == ROCKCHIP_DISPLAY_FULLSCREEN) { 1047 crtc_state->crtc_rect.x = 0; 1048 crtc_state->crtc_rect.y = 0; 1049 crtc_state->crtc_rect.w = hdisplay; 1050 crtc_state->crtc_rect.h = vdisplay; 1051 } else { 1052 if (crtc_state->src_rect.w >= hdisplay) { 1053 crtc_state->crtc_rect.x = 0; 1054 crtc_state->crtc_rect.w = hdisplay; 1055 } else { 1056 crtc_state->crtc_rect.x = (hdisplay - crtc_state->src_rect.w) / 2; 1057 crtc_state->crtc_rect.w = crtc_state->src_rect.w; 1058 } 1059 1060 if (crtc_state->src_rect.h >= vdisplay) { 1061 crtc_state->crtc_rect.y = 0; 1062 crtc_state->crtc_rect.h = vdisplay; 1063 } else { 1064 crtc_state->crtc_rect.y = (vdisplay - crtc_state->src_rect.h) / 2; 1065 crtc_state->crtc_rect.h = crtc_state->src_rect.h; 1066 } 1067 } 1068 1069 display_check(state); 1070 ret = display_set_plane(state); 1071 if (ret) 1072 return ret; 1073 display_enable(state); 1074 1075 return 0; 1076 } 1077 1078 static int get_crtc_id(ofnode connect, bool is_ports_node) 1079 { 1080 struct device_node *port_node; 1081 struct device_node *remote; 1082 int phandle; 1083 int val; 1084 1085 if (is_ports_node) { 1086 port_node = of_get_parent(connect.np); 1087 if (!port_node) 1088 goto err; 1089 1090 val = ofnode_read_u32_default(np_to_ofnode(port_node), "reg", -1); 1091 if (val < 0) 1092 goto err; 1093 } else { 1094 phandle = ofnode_read_u32_default(connect, "remote-endpoint", -1); 1095 if (phandle < 0) 1096 goto err; 1097 1098 remote = of_find_node_by_phandle(phandle); 1099 if (!remote) 1100 goto err; 1101 1102 val = ofnode_read_u32_default(np_to_ofnode(remote), "reg", -1); 1103 if (val < 0) 1104 goto err; 1105 } 1106 1107 return val; 1108 err: 1109 printf("Can't get crtc id, default set to id = 0\n"); 1110 return 0; 1111 } 1112 1113 static int get_crtc_mcu_mode(struct crtc_state *crtc_state, struct device_node *port_node, 1114 bool is_ports_node) 1115 { 1116 ofnode mcu_node, vp_node; 1117 int total_pixel, cs_pst, cs_pend, rw_pst, rw_pend; 1118 1119 if (is_ports_node) { 1120 vp_node = np_to_ofnode(port_node); 1121 mcu_node = ofnode_find_subnode(vp_node, "mcu-timing"); 1122 if (!ofnode_valid(mcu_node)) 1123 return -ENODEV; 1124 } else { 1125 mcu_node = dev_read_subnode(crtc_state->dev, "mcu-timing"); 1126 if (!ofnode_valid(mcu_node)) 1127 return -ENODEV; 1128 } 1129 1130 #define FDT_GET_MCU_INT(val, name) \ 1131 do { \ 1132 val = ofnode_read_s32_default(mcu_node, name, -1); \ 1133 if (val < 0) { \ 1134 printf("Can't get %s\n", name); \ 1135 return -ENXIO; \ 1136 } \ 1137 } while (0) 1138 1139 FDT_GET_MCU_INT(total_pixel, "mcu-pix-total"); 1140 FDT_GET_MCU_INT(cs_pst, "mcu-cs-pst"); 1141 FDT_GET_MCU_INT(cs_pend, "mcu-cs-pend"); 1142 FDT_GET_MCU_INT(rw_pst, "mcu-rw-pst"); 1143 FDT_GET_MCU_INT(rw_pend, "mcu-rw-pend"); 1144 1145 crtc_state->mcu_timing.mcu_pix_total = total_pixel; 1146 crtc_state->mcu_timing.mcu_cs_pst = cs_pst; 1147 crtc_state->mcu_timing.mcu_cs_pend = cs_pend; 1148 crtc_state->mcu_timing.mcu_rw_pst = rw_pst; 1149 crtc_state->mcu_timing.mcu_rw_pend = rw_pend; 1150 1151 return 0; 1152 } 1153 1154 struct rockchip_logo_cache *find_or_alloc_logo_cache(const char *bmp, int rotate) 1155 { 1156 struct rockchip_logo_cache *tmp, *logo_cache = NULL; 1157 1158 list_for_each_entry(tmp, &logo_cache_list, head) { 1159 if ((!strcmp(tmp->name, bmp) && rotate == tmp->logo_rotate) || 1160 (soc_is_rk3566() && tmp->logo_rotate)) { 1161 logo_cache = tmp; 1162 break; 1163 } 1164 } 1165 1166 if (!logo_cache) { 1167 logo_cache = malloc(sizeof(*logo_cache)); 1168 if (!logo_cache) { 1169 printf("failed to alloc memory for logo cache\n"); 1170 return NULL; 1171 } 1172 memset(logo_cache, 0, sizeof(*logo_cache)); 1173 strcpy(logo_cache->name, bmp); 1174 INIT_LIST_HEAD(&logo_cache->head); 1175 list_add_tail(&logo_cache->head, &logo_cache_list); 1176 } 1177 1178 return logo_cache; 1179 } 1180 1181 /* Note: used only for rkfb kernel driver */ 1182 static int load_kernel_bmp_logo(struct logo_info *logo, const char *bmp_name) 1183 { 1184 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE 1185 void *dst = NULL; 1186 int len, size; 1187 struct bmp_header *header; 1188 1189 if (!logo || !bmp_name) 1190 return -EINVAL; 1191 1192 header = malloc(RK_BLK_SIZE); 1193 if (!header) 1194 return -ENOMEM; 1195 1196 len = rockchip_read_resource_file(header, bmp_name, 0, RK_BLK_SIZE); 1197 if (len != RK_BLK_SIZE) { 1198 free(header); 1199 return -EINVAL; 1200 } 1201 size = get_unaligned_le32(&header->file_size); 1202 dst = (void *)(memory_start + MEMORY_POOL_SIZE / 2); 1203 len = rockchip_read_resource_file(dst, bmp_name, 0, size); 1204 if (len != size) { 1205 printf("failed to load bmp %s\n", bmp_name); 1206 free(header); 1207 return -ENOENT; 1208 } 1209 1210 logo->mem = dst; 1211 #endif 1212 1213 return 0; 1214 } 1215 1216 #ifdef BMP_DECODEER_LEGACY 1217 static int load_bmp_logo_legacy(struct logo_info *logo, const char *bmp_name) 1218 { 1219 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE 1220 struct rockchip_logo_cache *logo_cache; 1221 struct bmp_header *header; 1222 void *dst = NULL, *pdst; 1223 int size, len; 1224 int ret = 0; 1225 int reserved = 0; 1226 int dst_size; 1227 1228 if (!logo || !bmp_name) 1229 return -EINVAL; 1230 logo_cache = find_or_alloc_logo_cache(bmp_name, logo->rotate); 1231 if (!logo_cache) 1232 return -ENOMEM; 1233 1234 if (logo_cache->logo.mem) { 1235 memcpy(logo, &logo_cache->logo, sizeof(*logo)); 1236 return 0; 1237 } 1238 1239 header = malloc(RK_BLK_SIZE); 1240 if (!header) 1241 return -ENOMEM; 1242 1243 len = rockchip_read_resource_file(header, bmp_name, 0, RK_BLK_SIZE); 1244 if (len != RK_BLK_SIZE) { 1245 ret = -EINVAL; 1246 goto free_header; 1247 } 1248 1249 logo->bpp = get_unaligned_le16(&header->bit_count); 1250 logo->width = get_unaligned_le32(&header->width); 1251 logo->height = get_unaligned_le32(&header->height); 1252 dst_size = logo->width * logo->height * logo->bpp >> 3; 1253 reserved = get_unaligned_le32(&header->reserved); 1254 if (logo->height < 0) 1255 logo->height = -logo->height; 1256 size = get_unaligned_le32(&header->file_size); 1257 if (!can_direct_logo(logo->bpp)) { 1258 if (size > MEMORY_POOL_SIZE) { 1259 printf("failed to use boot buf as temp bmp buffer\n"); 1260 ret = -ENOMEM; 1261 goto free_header; 1262 } 1263 pdst = get_display_buffer(size); 1264 1265 } else { 1266 pdst = get_display_buffer(size); 1267 dst = pdst; 1268 } 1269 1270 len = rockchip_read_resource_file(pdst, bmp_name, 0, size); 1271 if (len != size) { 1272 printf("failed to load bmp %s\n", bmp_name); 1273 ret = -ENOENT; 1274 goto free_header; 1275 } 1276 1277 if (!can_direct_logo(logo->bpp)) { 1278 /* 1279 * TODO: force use 16bpp if bpp less than 16; 1280 */ 1281 logo->bpp = (logo->bpp <= 16) ? 16 : logo->bpp; 1282 dst_size = logo->width * logo->height * logo->bpp >> 3; 1283 dst = get_display_buffer(dst_size); 1284 if (!dst) { 1285 ret = -ENOMEM; 1286 goto free_header; 1287 } 1288 if (bmpdecoder(pdst, dst, logo->bpp)) { 1289 printf("failed to decode bmp %s\n", bmp_name); 1290 ret = -EINVAL; 1291 goto free_header; 1292 } 1293 1294 logo->offset = 0; 1295 logo->ymirror = 0; 1296 } else { 1297 logo->offset = get_unaligned_le32(&header->data_offset); 1298 if (reserved == BMP_PROCESSED_FLAG) 1299 logo->ymirror = 0; 1300 else 1301 logo->ymirror = 1; 1302 } 1303 logo->mem = dst; 1304 1305 memcpy(&logo_cache->logo, logo, sizeof(*logo)); 1306 1307 flush_dcache_range((ulong)dst, ALIGN((ulong)dst + dst_size, CONFIG_SYS_CACHELINE_SIZE)); 1308 1309 free_header: 1310 1311 free(header); 1312 1313 return ret; 1314 #else 1315 return -EINVAL; 1316 #endif 1317 } 1318 #endif 1319 1320 static void *bitmap_create(int width, int height, unsigned int state) 1321 { 1322 /* Ensure a stupidly large bitmap is not created */ 1323 if (width > 4096 || height > 4096) 1324 return NULL; 1325 1326 return calloc(width * height, BYTES_PER_PIXEL); 1327 } 1328 1329 static unsigned char *bitmap_get_buffer(void *bitmap) 1330 { 1331 return bitmap; 1332 } 1333 1334 static void bitmap_destroy(void *bitmap) 1335 { 1336 free(bitmap); 1337 } 1338 1339 static void bmp_copy(void *dst, bmp_image *bmp) 1340 { 1341 u16 row, col; 1342 u8 *image; 1343 u8 *pdst = (u8 *)dst; 1344 1345 image = (u8 *)bmp->bitmap; 1346 for (row = 0; row != bmp->height; row++) { 1347 for (col = 0; col != bmp->width; col++) { 1348 size_t z = (row * bmp->width + col) * BYTES_PER_PIXEL; 1349 1350 *pdst++ = image[z + 2]; 1351 *pdst++ = image[z + 1]; 1352 *pdst++ = image[z + 0]; 1353 *pdst++ = image[z + 3]; 1354 } 1355 } 1356 } 1357 1358 static void *rockchip_logo_rotate(struct logo_info *logo, void *src) 1359 { 1360 void *dst_rotate; 1361 int width = logo->width; 1362 int height = logo->height; 1363 int width_rotate = logo->height & 0x3 ? (logo->height & ~0x3) + 4 : logo->height; 1364 int height_rotate = logo->width; 1365 int dst_size = width * height * logo->bpp >> 3; 1366 int dst_size_rotate = width_rotate * height_rotate * logo->bpp >> 3; 1367 int bytes_per_pixel = logo->bpp >> 3; 1368 int padded_width; 1369 int i, j; 1370 char *img_data; 1371 1372 if (!(logo->rotate == 90 || logo->rotate == 180 || logo->rotate == 270)) { 1373 printf("Unsupported rotation angle\n"); 1374 return NULL; 1375 } 1376 1377 img_data = (char *)malloc(dst_size); 1378 if (!img_data) { 1379 printf("failed to alloc memory for image data\n"); 1380 return NULL; 1381 } 1382 memcpy(img_data, src, dst_size); 1383 1384 dst_rotate = get_display_buffer(dst_size_rotate); 1385 if (!dst_rotate) 1386 return NULL; 1387 memset(dst_rotate, 0, dst_size_rotate); 1388 1389 switch (logo->rotate) { 1390 case 90: 1391 logo->width = width_rotate; 1392 logo->height = height_rotate; 1393 padded_width = height & 0x3 ? (height & ~0x3) + 4 : height; 1394 for (i = 0; i < height; i++) { 1395 for (j = 0; j < width; j++) { 1396 memcpy(dst_rotate + (j * padded_width * bytes_per_pixel) + 1397 (height - i - 1) * bytes_per_pixel, 1398 img_data + i * width * bytes_per_pixel + j * bytes_per_pixel, 1399 bytes_per_pixel); 1400 } 1401 } 1402 break; 1403 case 180: 1404 for (i = 0; i < height; i++) { 1405 for (j = 0; j < width; j++) { 1406 memcpy(dst_rotate + (height - i - 1) * width * bytes_per_pixel + 1407 (width - j - 1) * bytes_per_pixel, 1408 img_data + i * width * bytes_per_pixel + j * bytes_per_pixel, 1409 bytes_per_pixel); 1410 } 1411 } 1412 break; 1413 case 270: 1414 logo->width = width_rotate; 1415 logo->height = height_rotate; 1416 padded_width = height & 0x3 ? (height & ~0x3) + 4 : height; 1417 for (i = 0; i < height; i++) { 1418 for (j = 0; j < width; j++) { 1419 memcpy(dst_rotate + (width - j - 1) * padded_width * bytes_per_pixel + 1420 i * bytes_per_pixel, 1421 img_data + i * width * bytes_per_pixel + j * bytes_per_pixel, 1422 bytes_per_pixel); 1423 } 1424 } 1425 break; 1426 default: 1427 break; 1428 } 1429 1430 free(img_data); 1431 1432 return dst_rotate; 1433 } 1434 1435 static int load_bmp_logo(struct logo_info *logo, const char *bmp_name) 1436 { 1437 #ifdef CONFIG_ROCKCHIP_RESOURCE_IMAGE 1438 struct rockchip_logo_cache *logo_cache; 1439 bmp_bitmap_callback_vt bitmap_callbacks = { 1440 bitmap_create, 1441 bitmap_destroy, 1442 bitmap_get_buffer, 1443 }; 1444 bmp_result code; 1445 bmp_image bmp; 1446 void *bmp_data; 1447 void *dst = NULL; 1448 void *dst_rotate = NULL; 1449 int len, dst_size; 1450 int ret = 0; 1451 1452 if (!logo || !bmp_name) 1453 return -EINVAL; 1454 1455 logo_cache = find_or_alloc_logo_cache(bmp_name, logo->rotate); 1456 if (!logo_cache) 1457 return -ENOMEM; 1458 1459 if (logo_cache->logo.mem) { 1460 memcpy(logo, &logo_cache->logo, sizeof(*logo)); 1461 return 0; 1462 } 1463 1464 bmp_data = malloc(MAX_IMAGE_BYTES); 1465 if (!bmp_data) 1466 return -ENOMEM; 1467 1468 bmp_create(&bmp, &bitmap_callbacks); 1469 1470 len = rockchip_read_resource_file(bmp_data, bmp_name, 0, MAX_IMAGE_BYTES); 1471 if (len < 0) { 1472 ret = -EINVAL; 1473 goto free_bmp_data; 1474 } 1475 1476 /* analyse the BMP */ 1477 code = bmp_analyse(&bmp, len, bmp_data); 1478 if (code != BMP_OK) { 1479 printf("failed to parse bmp:%s header\n", bmp_name); 1480 ret = -EINVAL; 1481 goto free_bmp_data; 1482 } 1483 /* fix bpp to 32 */ 1484 logo->bpp = 32; 1485 logo->offset = 0; 1486 logo->ymirror = 0; 1487 logo->width = get_unaligned_le32(&bmp.width); 1488 logo->height = get_unaligned_le32(&bmp.height); 1489 dst_size = logo->width * logo->height * logo->bpp >> 3; 1490 /* decode the image to RGBA8888 format */ 1491 code = bmp_decode(&bmp); 1492 if (code != BMP_OK) { 1493 /* allow partially decoded images */ 1494 if (code != BMP_INSUFFICIENT_DATA && code != BMP_DATA_ERROR) { 1495 printf("failed to allocate the buffer of bmp:%s\n", bmp_name); 1496 ret = -EINVAL; 1497 goto free_bmp_data; 1498 } 1499 1500 /* skip if the partially decoded image would be ridiculously large */ 1501 if ((bmp.width * bmp.height) > 200000) { 1502 printf("partially decoded bmp:%s can not be too large\n", bmp_name); 1503 ret = -EINVAL; 1504 goto free_bmp_data; 1505 } 1506 } 1507 1508 dst = get_display_buffer(dst_size); 1509 if (!dst) { 1510 ret = -ENOMEM; 1511 goto free_bmp_data; 1512 } 1513 bmp_copy(dst, &bmp); 1514 1515 if (logo->rotate) { 1516 dst_rotate = rockchip_logo_rotate(logo, dst); 1517 if (dst_rotate) { 1518 dst = dst_rotate; 1519 dst_size = logo->width * logo->height * logo->bpp >> 3; 1520 } 1521 printf("logo ratate %d\n", logo->rotate); 1522 } 1523 logo->mem = dst; 1524 1525 memcpy(&logo_cache->logo, logo, sizeof(*logo)); 1526 logo_cache->logo_rotate = logo->rotate; 1527 1528 flush_dcache_range((ulong)dst, ALIGN((ulong)dst + dst_size, CONFIG_SYS_CACHELINE_SIZE)); 1529 free_bmp_data: 1530 /* clean up */ 1531 bmp_finalise(&bmp); 1532 free(bmp_data); 1533 1534 return ret; 1535 #else 1536 return -EINVAL; 1537 #endif 1538 } 1539 1540 void rockchip_show_fbbase(ulong fbbase) 1541 { 1542 struct display_state *s; 1543 1544 list_for_each_entry(s, &rockchip_display_list, head) { 1545 s->logo.mode = ROCKCHIP_DISPLAY_FULLSCREEN; 1546 s->logo.mem = (char *)fbbase; 1547 s->logo.width = DRM_ROCKCHIP_FB_WIDTH; 1548 s->logo.height = DRM_ROCKCHIP_FB_HEIGHT; 1549 s->logo.bpp = 32; 1550 s->logo.ymirror = 0; 1551 1552 display_logo(s); 1553 } 1554 } 1555 1556 int rockchip_show_bmp(const char *bmp) 1557 { 1558 struct display_state *s; 1559 int ret = 0; 1560 1561 if (!bmp) { 1562 list_for_each_entry(s, &rockchip_display_list, head) 1563 display_disable(s); 1564 return -ENOENT; 1565 } 1566 1567 list_for_each_entry(s, &rockchip_display_list, head) { 1568 s->logo.mode = s->charge_logo_mode; 1569 if (load_bmp_logo(&s->logo, bmp)) 1570 continue; 1571 ret = display_logo(s); 1572 } 1573 1574 return ret; 1575 } 1576 1577 int rockchip_show_logo(void) 1578 { 1579 struct display_state *s; 1580 struct display_state *ms = NULL; 1581 int ret = 0; 1582 int count = 0; 1583 1584 list_for_each_entry(s, &rockchip_display_list, head) { 1585 s->logo.mode = s->logo_mode; 1586 s->logo.rotate = s->logo_rotate; 1587 if (load_bmp_logo(&s->logo, s->ulogo_name)) { 1588 printf("failed to display uboot logo\n"); 1589 } else { 1590 ret = display_logo(s); 1591 if (ret == -EAGAIN) 1592 ms = s; 1593 } 1594 /* Load kernel bmp in rockchip_display_fixup() later */ 1595 } 1596 1597 /* 1598 * For rk3566, the mirror win must be enabled after the related 1599 * source win. If error code is EAGAIN, the mirror win may be 1600 * first enabled unexpectedly, and we will move the enabling process 1601 * as follows. 1602 */ 1603 if (ms) { 1604 while (count < 5) { 1605 ret = display_logo(ms); 1606 if (ret != -EAGAIN) 1607 break; 1608 mdelay(10); 1609 count++; 1610 } 1611 } 1612 1613 return ret; 1614 } 1615 1616 int rockchip_vop_dump(const char *cmd) 1617 { 1618 struct display_state *state; 1619 struct crtc_state *crtc_state; 1620 struct rockchip_crtc *crtc; 1621 const struct rockchip_crtc_funcs *crtc_funcs; 1622 int ret = -EINVAL; 1623 1624 list_for_each_entry(state, &rockchip_display_list, head) { 1625 if (!state->is_init) 1626 continue; 1627 crtc_state = &state->crtc_state; 1628 crtc = crtc_state->crtc; 1629 crtc_funcs = crtc->funcs; 1630 1631 if (!cmd) 1632 ret = crtc_funcs->active_regs_dump(state); 1633 else if (!strcmp(cmd, "a") || !strcmp(cmd, "all")) 1634 ret = crtc_funcs->regs_dump(state); 1635 if (!ret) 1636 break; 1637 } 1638 1639 if (ret) 1640 ret = CMD_RET_USAGE; 1641 1642 return ret; 1643 } 1644 1645 enum { 1646 PORT_DIR_IN, 1647 PORT_DIR_OUT, 1648 }; 1649 1650 const struct device_node *rockchip_of_graph_get_port_by_id(ofnode node, int id) 1651 { 1652 ofnode ports, port; 1653 u32 reg; 1654 1655 ports = ofnode_find_subnode(node, "ports"); 1656 if (!ofnode_valid(ports)) 1657 return NULL; 1658 1659 ofnode_for_each_subnode(port, ports) { 1660 if (ofnode_read_u32(port, "reg", ®)) 1661 continue; 1662 1663 if (reg == id) 1664 break; 1665 } 1666 1667 if (reg == id) 1668 return ofnode_to_np(port); 1669 1670 return NULL; 1671 } 1672 1673 static const struct device_node *rockchip_of_graph_get_port_parent(ofnode port) 1674 { 1675 ofnode parent; 1676 int is_ports_node; 1677 1678 parent = ofnode_get_parent(port); 1679 is_ports_node = strstr(ofnode_to_np(parent)->full_name, "ports") ? 1 : 0; 1680 if (is_ports_node) 1681 parent = ofnode_get_parent(parent); 1682 1683 return ofnode_to_np(parent); 1684 } 1685 1686 const struct device_node * 1687 rockchip_of_graph_get_endpoint_by_regs(ofnode node, int port, int endpoint) 1688 { 1689 const struct device_node *port_node; 1690 ofnode ep; 1691 u32 reg; 1692 1693 port_node = rockchip_of_graph_get_port_by_id(node, port); 1694 if (!port_node) 1695 return NULL; 1696 1697 ofnode_for_each_subnode(ep, np_to_ofnode(port_node)) { 1698 if (ofnode_read_u32(ep, "reg", ®)) 1699 break; 1700 if (reg == endpoint) 1701 break; 1702 } 1703 1704 if (!ofnode_valid(ep)) 1705 return NULL; 1706 1707 return ofnode_to_np(ep); 1708 } 1709 1710 static const struct device_node * 1711 rockchip_of_graph_get_remote_node(ofnode node, int port, int endpoint) 1712 { 1713 const struct device_node *ep_node; 1714 ofnode ep; 1715 uint phandle; 1716 1717 ep_node = rockchip_of_graph_get_endpoint_by_regs(node, port, endpoint); 1718 if (!ep_node) 1719 return NULL; 1720 1721 if (ofnode_read_u32(np_to_ofnode(ep_node), "remote-endpoint", &phandle)) 1722 return NULL; 1723 1724 ep = ofnode_get_by_phandle(phandle); 1725 if (!ofnode_valid(ep)) 1726 return NULL; 1727 1728 return ofnode_to_np(ep); 1729 } 1730 1731 static int rockchip_of_find_panel(struct udevice *dev, struct rockchip_panel **panel) 1732 { 1733 const struct device_node *ep_node, *panel_node; 1734 ofnode panel_ofnode, port; 1735 struct udevice *panel_dev; 1736 int ret = 0; 1737 1738 *panel = NULL; 1739 panel_ofnode = dev_read_subnode(dev, "panel"); 1740 if (ofnode_valid(panel_ofnode) && ofnode_is_available(panel_ofnode)) { 1741 ret = uclass_get_device_by_ofnode(UCLASS_PANEL, panel_ofnode, 1742 &panel_dev); 1743 if (!ret) 1744 goto found; 1745 } 1746 1747 ep_node = rockchip_of_graph_get_remote_node(dev->node, PORT_DIR_OUT, 0); 1748 if (!ep_node) 1749 return -ENODEV; 1750 1751 port = ofnode_get_parent(np_to_ofnode(ep_node)); 1752 if (!ofnode_valid(port)) 1753 return -ENODEV; 1754 1755 panel_node = rockchip_of_graph_get_port_parent(port); 1756 if (!panel_node) 1757 return -ENODEV; 1758 1759 ret = uclass_get_device_by_ofnode(UCLASS_PANEL, np_to_ofnode(panel_node), &panel_dev); 1760 if (!ret) 1761 goto found; 1762 1763 return -ENODEV; 1764 1765 found: 1766 *panel = (struct rockchip_panel *)dev_get_driver_data(panel_dev); 1767 return 0; 1768 } 1769 1770 static int rockchip_of_find_bridge(struct udevice *dev, struct rockchip_bridge **bridge) 1771 { 1772 const struct device_node *ep_node, *bridge_node; 1773 ofnode port; 1774 struct udevice *bridge_dev; 1775 int ret = 0; 1776 1777 ep_node = rockchip_of_graph_get_remote_node(dev->node, PORT_DIR_OUT, 0); 1778 if (!ep_node) 1779 return -ENODEV; 1780 1781 port = ofnode_get_parent(np_to_ofnode(ep_node)); 1782 if (!ofnode_valid(port)) 1783 return -ENODEV; 1784 1785 bridge_node = rockchip_of_graph_get_port_parent(port); 1786 if (!bridge_node) 1787 return -ENODEV; 1788 1789 ret = uclass_get_device_by_ofnode(UCLASS_VIDEO_BRIDGE, np_to_ofnode(bridge_node), 1790 &bridge_dev); 1791 if (!ret) 1792 goto found; 1793 1794 return -ENODEV; 1795 1796 found: 1797 *bridge = (struct rockchip_bridge *)dev_get_driver_data(bridge_dev); 1798 return 0; 1799 } 1800 1801 static int rockchip_of_find_panel_or_bridge(struct udevice *dev, struct rockchip_panel **panel, 1802 struct rockchip_bridge **bridge) 1803 { 1804 int ret = 0; 1805 1806 if (*panel) 1807 return 0; 1808 1809 *panel = NULL; 1810 *bridge = NULL; 1811 1812 if (panel) { 1813 ret = rockchip_of_find_panel(dev, panel); 1814 if (!ret) 1815 return 0; 1816 } 1817 1818 if (ret) { 1819 ret = rockchip_of_find_bridge(dev, bridge); 1820 if (!ret) 1821 ret = rockchip_of_find_panel_or_bridge((*bridge)->dev, panel, 1822 &(*bridge)->next_bridge); 1823 } 1824 1825 return ret; 1826 } 1827 1828 static struct rockchip_phy *rockchip_of_find_phy(struct udevice *dev) 1829 { 1830 struct udevice *phy_dev; 1831 int ret; 1832 1833 ret = uclass_get_device_by_phandle(UCLASS_PHY, dev, "phys", &phy_dev); 1834 if (ret) 1835 return NULL; 1836 1837 return (struct rockchip_phy *)dev_get_driver_data(phy_dev); 1838 } 1839 1840 static struct udevice *rockchip_of_find_connector_device(ofnode endpoint) 1841 { 1842 ofnode ep, port, ports, conn; 1843 uint phandle; 1844 struct udevice *dev; 1845 int ret; 1846 1847 if (ofnode_read_u32(endpoint, "remote-endpoint", &phandle)) 1848 return NULL; 1849 1850 ep = ofnode_get_by_phandle(phandle); 1851 if (!ofnode_valid(ep) || !ofnode_is_available(ep)) 1852 return NULL; 1853 1854 port = ofnode_get_parent(ep); 1855 if (!ofnode_valid(port)) 1856 return NULL; 1857 1858 ports = ofnode_get_parent(port); 1859 if (!ofnode_valid(ports)) 1860 return NULL; 1861 1862 conn = ofnode_get_parent(ports); 1863 if (!ofnode_valid(conn) || !ofnode_is_available(conn)) 1864 return NULL; 1865 1866 ret = uclass_get_device_by_ofnode(UCLASS_DISPLAY, conn, &dev); 1867 if (ret) { 1868 /* 1869 * for DP-MST, ports node->parent node->parent node is the device node */ 1870 conn = ofnode_get_parent(conn); 1871 if (!ofnode_valid(conn) || !ofnode_is_available(conn)) 1872 return NULL; 1873 1874 ret = uclass_get_device_by_ofnode(UCLASS_DISPLAY, conn, &dev); 1875 if (ret) 1876 return NULL; 1877 1878 } 1879 1880 return dev; 1881 } 1882 1883 static struct rockchip_connector *rockchip_of_get_connector(ofnode endpoint) 1884 { 1885 struct rockchip_connector *conn; 1886 struct udevice *dev; 1887 int ret; 1888 1889 dev = rockchip_of_find_connector_device(endpoint); 1890 if (!dev) { 1891 printf("Warn: can't find connect driver\n"); 1892 return NULL; 1893 } 1894 1895 conn = get_rockchip_connector_by_device(dev); 1896 if (!conn) 1897 return NULL; 1898 ret = rockchip_of_find_panel_or_bridge(dev, &conn->panel, &conn->bridge); 1899 if (ret) 1900 debug("Warn: no find panel or bridge\n"); 1901 1902 conn->phy = rockchip_of_find_phy(dev); 1903 1904 return conn; 1905 } 1906 1907 static struct rockchip_connector *rockchip_get_split_connector(struct rockchip_connector *conn) 1908 { 1909 char *conn_name; 1910 struct device_node *split_node; 1911 struct udevice *split_dev; 1912 struct rockchip_connector *split_conn; 1913 bool split_mode; 1914 int ret; 1915 1916 split_mode = ofnode_read_bool(conn->dev->node, "split-mode"); 1917 split_mode |= ofnode_read_bool(conn->dev->node, "dual-channel"); 1918 if (!split_mode) 1919 return NULL; 1920 1921 switch (conn->type) { 1922 case DRM_MODE_CONNECTOR_DisplayPort: 1923 conn_name = "dp"; 1924 break; 1925 case DRM_MODE_CONNECTOR_eDP: 1926 conn_name = "edp"; 1927 break; 1928 case DRM_MODE_CONNECTOR_HDMIA: 1929 conn_name = "hdmi"; 1930 break; 1931 case DRM_MODE_CONNECTOR_LVDS: 1932 conn_name = "lvds"; 1933 break; 1934 default: 1935 return NULL; 1936 } 1937 1938 split_node = of_alias_get_dev(conn_name, !conn->id); 1939 if (!split_node || !of_device_is_available(split_node)) 1940 return NULL; 1941 1942 ret = uclass_get_device_by_ofnode(UCLASS_DISPLAY, np_to_ofnode(split_node), &split_dev); 1943 if (ret) 1944 return NULL; 1945 1946 split_conn = get_rockchip_connector_by_device(split_dev); 1947 if (!split_conn) 1948 return NULL; 1949 ret = rockchip_of_find_panel_or_bridge(split_dev, &split_conn->panel, &split_conn->bridge); 1950 if (ret) 1951 debug("Warn: no find panel or bridge\n"); 1952 1953 split_conn->phy = rockchip_of_find_phy(split_dev); 1954 1955 return split_conn; 1956 } 1957 1958 static bool rockchip_get_display_path_status(ofnode endpoint) 1959 { 1960 ofnode ep; 1961 uint phandle; 1962 1963 if (ofnode_read_u32(endpoint, "remote-endpoint", &phandle)) 1964 return false; 1965 1966 ep = ofnode_get_by_phandle(phandle); 1967 if (!ofnode_valid(ep) || !ofnode_is_available(ep)) 1968 return false; 1969 1970 return true; 1971 } 1972 1973 #if defined(CONFIG_ROCKCHIP_RK3568) 1974 static int rockchip_display_fixup_dts(void *blob) 1975 { 1976 ofnode route_node, route_subnode, conn_ep, conn_port; 1977 const struct device_node *route_sub_devnode; 1978 const struct device_node *ep_node, *conn_ep_dev_node; 1979 u32 phandle; 1980 int conn_ep_offset; 1981 const char *route_sub_path, *path; 1982 1983 /* Don't go further if new variant after 1984 * reading PMUGRF_SOC_CON15 1985 */ 1986 if ((readl(0xfdc20100) & GENMASK(15, 14))) 1987 return 0; 1988 1989 route_node = ofnode_path("/display-subsystem/route"); 1990 if (!ofnode_valid(route_node)) 1991 return -EINVAL; 1992 1993 ofnode_for_each_subnode(route_subnode, route_node) { 1994 if (!ofnode_is_available(route_subnode)) 1995 continue; 1996 1997 route_sub_devnode = ofnode_to_np(route_subnode); 1998 route_sub_path = route_sub_devnode->full_name; 1999 if (!strstr(ofnode_get_name(route_subnode), "dsi") && 2000 !strstr(ofnode_get_name(route_subnode), "edp")) 2001 return 0; 2002 2003 phandle = ofnode_read_u32_default(route_subnode, "connect", -1); 2004 if (phandle < 0) { 2005 printf("Warn: can't find connect node's handle\n"); 2006 continue; 2007 } 2008 2009 ep_node = of_find_node_by_phandle(phandle); 2010 if (!ofnode_valid(np_to_ofnode(ep_node))) { 2011 printf("Warn: can't find endpoint node from phandle\n"); 2012 continue; 2013 } 2014 2015 ofnode_read_u32(np_to_ofnode(ep_node), "remote-endpoint", &phandle); 2016 conn_ep = ofnode_get_by_phandle(phandle); 2017 if (!ofnode_valid(conn_ep) || !ofnode_is_available(conn_ep)) 2018 return -ENODEV; 2019 2020 conn_port = ofnode_get_parent(conn_ep); 2021 if (!ofnode_valid(conn_port)) 2022 return -ENODEV; 2023 2024 ofnode_for_each_subnode(conn_ep, conn_port) { 2025 conn_ep_dev_node = ofnode_to_np(conn_ep); 2026 path = conn_ep_dev_node->full_name; 2027 ofnode_read_u32(conn_ep, "remote-endpoint", &phandle); 2028 conn_ep_offset = fdt_path_offset(blob, path); 2029 2030 if (!ofnode_is_available(conn_ep) && 2031 strstr(ofnode_get_name(conn_ep), "endpoint@0")) { 2032 do_fixup_by_path_u32(blob, route_sub_path, 2033 "connect", phandle, 1); 2034 fdt_status_okay(blob, conn_ep_offset); 2035 2036 } else if (ofnode_is_available(conn_ep) && 2037 strstr(ofnode_get_name(conn_ep), "endpoint@1")) { 2038 fdt_status_disabled(blob, conn_ep_offset); 2039 } 2040 } 2041 } 2042 2043 return 0; 2044 } 2045 #endif 2046 2047 static int rockchip_display_probe(struct udevice *dev) 2048 { 2049 struct video_priv *uc_priv = dev_get_uclass_priv(dev); 2050 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev); 2051 const void *blob = gd->fdt_blob; 2052 int phandle; 2053 struct udevice *crtc_dev; 2054 struct rockchip_crtc *crtc; 2055 struct rockchip_connector *conn, *split_conn; 2056 struct display_state *s; 2057 const char *name; 2058 int ret; 2059 ofnode node, route_node, timing_node; 2060 struct device_node *port_node, *vop_node, *ep_node, *port_parent_node; 2061 struct public_phy_data *data; 2062 bool is_ports_node = false; 2063 2064 #if defined(CONFIG_ROCKCHIP_RK3568) 2065 rockchip_display_fixup_dts((void *)blob); 2066 #endif 2067 /* Before relocation we don't need to do anything */ 2068 if (!(gd->flags & GD_FLG_RELOC)) 2069 return 0; 2070 2071 data = malloc(sizeof(struct public_phy_data)); 2072 if (!data) { 2073 printf("failed to alloc phy data\n"); 2074 return -ENOMEM; 2075 } 2076 data->phy_init = false; 2077 2078 init_display_buffer(plat->base); 2079 2080 route_node = dev_read_subnode(dev, "route"); 2081 if (!ofnode_valid(route_node)) 2082 return -ENODEV; 2083 2084 ofnode_for_each_subnode(node, route_node) { 2085 if (!ofnode_is_available(node)) 2086 continue; 2087 phandle = ofnode_read_u32_default(node, "connect", -1); 2088 if (phandle < 0) { 2089 printf("Warn: can't find connect node's handle\n"); 2090 continue; 2091 } 2092 ep_node = of_find_node_by_phandle(phandle); 2093 if (!ofnode_valid(np_to_ofnode(ep_node))) { 2094 printf("Warn: can't find endpoint node from phandle\n"); 2095 continue; 2096 } 2097 port_node = of_get_parent(ep_node); 2098 if (!ofnode_valid(np_to_ofnode(port_node))) { 2099 printf("Warn: can't find port node from phandle\n"); 2100 continue; 2101 } 2102 2103 port_parent_node = of_get_parent(port_node); 2104 if (!ofnode_valid(np_to_ofnode(port_parent_node))) { 2105 printf("Warn: can't find port parent node from phandle\n"); 2106 continue; 2107 } 2108 2109 is_ports_node = strstr(port_parent_node->full_name, "ports") ? 1 : 0; 2110 if (is_ports_node) { 2111 vop_node = of_get_parent(port_parent_node); 2112 if (!ofnode_valid(np_to_ofnode(vop_node))) { 2113 printf("Warn: can't find crtc node from phandle\n"); 2114 continue; 2115 } 2116 } else { 2117 vop_node = port_parent_node; 2118 } 2119 2120 ret = uclass_get_device_by_ofnode(UCLASS_VIDEO_CRTC, 2121 np_to_ofnode(vop_node), 2122 &crtc_dev); 2123 if (ret) { 2124 printf("Warn: can't find crtc driver %d\n", ret); 2125 continue; 2126 } 2127 crtc = (struct rockchip_crtc *)dev_get_driver_data(crtc_dev); 2128 2129 conn = rockchip_of_get_connector(np_to_ofnode(ep_node)); 2130 if (!conn) { 2131 printf("Warn: can't get connect driver\n"); 2132 continue; 2133 } 2134 split_conn = rockchip_get_split_connector(conn); 2135 2136 s = malloc(sizeof(*s)); 2137 if (!s) 2138 continue; 2139 2140 memset(s, 0, sizeof(*s)); 2141 2142 INIT_LIST_HEAD(&s->head); 2143 ret = ofnode_read_string_index(node, "logo,uboot", 0, &name); 2144 if (!ret) 2145 memcpy(s->ulogo_name, name, strlen(name)); 2146 ret = ofnode_read_string_index(node, "logo,kernel", 0, &name); 2147 if (!ret) 2148 memcpy(s->klogo_name, name, strlen(name)); 2149 ret = ofnode_read_string_index(node, "logo,mode", 0, &name); 2150 if (!strcmp(name, "fullscreen")) 2151 s->logo_mode = ROCKCHIP_DISPLAY_FULLSCREEN; 2152 else 2153 s->logo_mode = ROCKCHIP_DISPLAY_CENTER; 2154 ret = ofnode_read_string_index(node, "charge_logo,mode", 0, &name); 2155 if (!strcmp(name, "fullscreen")) 2156 s->charge_logo_mode = ROCKCHIP_DISPLAY_FULLSCREEN; 2157 else 2158 s->charge_logo_mode = ROCKCHIP_DISPLAY_CENTER; 2159 2160 s->logo_rotate = ofnode_read_u32_default(node, "logo,rotate", 0); 2161 2162 s->force_output = ofnode_read_bool(node, "force-output"); 2163 2164 if (s->force_output) { 2165 timing_node = ofnode_find_subnode(node, "force_timing"); 2166 ret = display_get_force_timing_from_dts(timing_node, 2167 &s->force_mode, 2168 &s->conn_state.bus_flags); 2169 if (ofnode_read_u32(node, "force-bus-format", &s->force_bus_format)) 2170 s->force_bus_format = MEDIA_BUS_FMT_RGB888_1X24; 2171 } 2172 2173 s->blob = blob; 2174 s->conn_state.connector = conn; 2175 s->conn_state.secondary = NULL; 2176 s->conn_state.type = conn->type; 2177 if (split_conn) { 2178 s->conn_state.secondary = split_conn; 2179 s->conn_state.output_flags |= ROCKCHIP_OUTPUT_DUAL_CHANNEL_LEFT_RIGHT_MODE; 2180 s->conn_state.output_flags |= conn->id ? ROCKCHIP_OUTPUT_DATA_SWAP : 0; 2181 } 2182 s->conn_state.overscan.left_margin = 100; 2183 s->conn_state.overscan.right_margin = 100; 2184 s->conn_state.overscan.top_margin = 100; 2185 s->conn_state.overscan.bottom_margin = 100; 2186 s->crtc_state.node = np_to_ofnode(vop_node); 2187 s->crtc_state.dev = crtc_dev; 2188 s->crtc_state.crtc = crtc; 2189 s->crtc_state.crtc_id = get_crtc_id(np_to_ofnode(ep_node), is_ports_node); 2190 s->node = node; 2191 2192 if (is_ports_node) { /* only vop2 will get into here */ 2193 ofnode vp_node = np_to_ofnode(port_node); 2194 static bool get_plane_mask_from_dts; 2195 2196 s->crtc_state.ports_node = port_parent_node; 2197 if (!get_plane_mask_from_dts) { 2198 ofnode vp_sub_node; 2199 int vp_id = 0; 2200 bool vp_enable = false; 2201 2202 ofnode_for_each_subnode(vp_node, np_to_ofnode(port_parent_node)) { 2203 int cursor_plane = -1; 2204 2205 vp_id = ofnode_read_u32_default(vp_node, "reg", 0); 2206 2207 s->crtc_state.crtc->vps[vp_id].xmirror_en = 2208 ofnode_read_bool(vp_node, "xmirror-enable"); 2209 2210 ret = ofnode_read_u32_default(vp_node, "rockchip,plane-mask", 0); 2211 2212 cursor_plane = ofnode_read_u32_default(vp_node, "cursor-win-id", -1); 2213 s->crtc_state.crtc->vps[vp_id].cursor_plane = cursor_plane; 2214 if (ret) { 2215 s->crtc_state.crtc->vps[vp_id].plane_mask = ret; 2216 s->crtc_state.crtc->assign_plane |= true; 2217 s->crtc_state.crtc->vps[vp_id].primary_plane_id = 2218 ofnode_read_u32_default(vp_node, "rockchip,primary-plane", U8_MAX); 2219 printf("get vp%d plane mask:0x%x, primary id:%d, cursor_plane:%d, from dts\n", 2220 vp_id, 2221 s->crtc_state.crtc->vps[vp_id].plane_mask, 2222 s->crtc_state.crtc->vps[vp_id].primary_plane_id == U8_MAX ? -1 : 2223 s->crtc_state.crtc->vps[vp_id].primary_plane_id, 2224 cursor_plane); 2225 } 2226 2227 /* To check current vp status */ 2228 vp_enable = false; 2229 ofnode_for_each_subnode(vp_sub_node, vp_node) 2230 vp_enable |= rockchip_get_display_path_status(vp_sub_node); 2231 s->crtc_state.crtc->vps[vp_id].enable = vp_enable; 2232 } 2233 get_plane_mask_from_dts = true; 2234 } 2235 } 2236 2237 get_crtc_mcu_mode(&s->crtc_state, port_node, is_ports_node); 2238 2239 ret = ofnode_read_u32_default(s->crtc_state.node, 2240 "rockchip,dual-channel-swap", 0); 2241 s->crtc_state.dual_channel_swap = ret; 2242 2243 if (connector_phy_init(conn, data)) { 2244 printf("Warn: Failed to init phy drivers\n"); 2245 free(s); 2246 continue; 2247 } 2248 list_add_tail(&s->head, &rockchip_display_list); 2249 } 2250 2251 if (list_empty(&rockchip_display_list)) { 2252 debug("Failed to found available display route\n"); 2253 return -ENODEV; 2254 } 2255 rockchip_get_baseparameter(); 2256 display_pre_init(); 2257 2258 uc_priv->xsize = DRM_ROCKCHIP_FB_WIDTH; 2259 uc_priv->ysize = DRM_ROCKCHIP_FB_HEIGHT; 2260 uc_priv->bpix = VIDEO_BPP32; 2261 2262 #ifdef CONFIG_DRM_ROCKCHIP_VIDEO_FRAMEBUFFER 2263 rockchip_show_fbbase(plat->base); 2264 video_set_flush_dcache(dev, true); 2265 #endif 2266 2267 return 0; 2268 } 2269 2270 void rockchip_display_fixup(void *blob) 2271 { 2272 const struct rockchip_connector_funcs *conn_funcs; 2273 const struct rockchip_crtc_funcs *crtc_funcs; 2274 struct rockchip_connector *conn; 2275 const struct rockchip_crtc *crtc; 2276 struct display_state *s; 2277 int offset; 2278 int ret; 2279 const struct device_node *np; 2280 const char *path; 2281 const char *cacm_header; 2282 u64 aligned_memory_size; 2283 2284 if (fdt_node_offset_by_compatible(blob, 0, "rockchip,drm-logo") >= 0) { 2285 list_for_each_entry(s, &rockchip_display_list, head) { 2286 ret = load_bmp_logo(&s->logo, s->klogo_name); 2287 if (ret < 0) { 2288 s->is_klogo_valid = false; 2289 printf("VP%d fail to load kernel logo\n", s->crtc_state.crtc_id); 2290 } else { 2291 s->is_klogo_valid = true; 2292 } 2293 } 2294 2295 if (!get_display_size()) 2296 return; 2297 2298 aligned_memory_size = (u64)ALIGN(get_display_size(), align_size); 2299 offset = fdt_update_reserved_memory(blob, "rockchip,drm-logo", 2300 (u64)memory_start, 2301 aligned_memory_size); 2302 if (offset < 0) 2303 printf("failed to reserve drm-loader-logo memory\n"); 2304 2305 if (get_cubic_memory_size()) { 2306 aligned_memory_size = (u64)ALIGN(get_cubic_memory_size(), align_size); 2307 offset = fdt_update_reserved_memory(blob, "rockchip,drm-cubic-lut", 2308 (u64)cubic_lut_memory_start, 2309 aligned_memory_size); 2310 if (offset < 0) 2311 printf("failed to reserve drm-cubic-lut memory\n"); 2312 } 2313 } else { 2314 printf("can't found rockchip,drm-logo, use rockchip,fb-logo\n"); 2315 /* Compatible with rkfb display, only need reserve memory */ 2316 offset = fdt_update_reserved_memory(blob, "rockchip,fb-logo", 2317 (u64)memory_start, 2318 MEMORY_POOL_SIZE); 2319 if (offset < 0) 2320 printf("failed to reserve fb-loader-logo memory\n"); 2321 else 2322 list_for_each_entry(s, &rockchip_display_list, head) 2323 load_kernel_bmp_logo(&s->logo, s->klogo_name); 2324 return; 2325 } 2326 2327 list_for_each_entry(s, &rockchip_display_list, head) { 2328 /* 2329 * If plane mask is not set in dts, fixup dts to assign it 2330 * whether crtc is initialized or not. 2331 */ 2332 if (s->crtc_state.crtc->funcs->fixup_dts && !s->crtc_state.crtc->assign_plane) 2333 s->crtc_state.crtc->funcs->fixup_dts(s, blob); 2334 2335 if (!s->is_init || !s->is_klogo_valid) 2336 continue; 2337 2338 conn = s->conn_state.connector; 2339 if (!conn) 2340 continue; 2341 conn_funcs = conn->funcs; 2342 if (!conn_funcs) { 2343 printf("failed to get exist connector\n"); 2344 continue; 2345 } 2346 2347 if (s->conn_state.secondary && 2348 s->conn_state.secondary->type != DRM_MODE_CONNECTOR_LVDS) { 2349 s->conn_state.mode.clock *= 2; 2350 s->conn_state.mode.hdisplay *= 2; 2351 } 2352 2353 crtc = s->crtc_state.crtc; 2354 if (!crtc) 2355 continue; 2356 2357 crtc_funcs = crtc->funcs; 2358 if (!crtc_funcs) { 2359 printf("failed to get exist crtc\n"); 2360 continue; 2361 } 2362 2363 np = ofnode_to_np(s->node); 2364 path = np->full_name; 2365 fdt_increase_size(blob, 0x400); 2366 #define FDT_SET_U32(name, val) \ 2367 do_fixup_by_path_u32(blob, path, name, val, 1); 2368 2369 offset = s->logo.offset + (u32)(unsigned long)s->logo.mem 2370 - memory_start; 2371 FDT_SET_U32("logo,offset", offset); 2372 FDT_SET_U32("logo,width", s->logo.width); 2373 FDT_SET_U32("logo,height", s->logo.height); 2374 FDT_SET_U32("logo,bpp", s->logo.bpp); 2375 FDT_SET_U32("logo,ymirror", s->logo.ymirror); 2376 FDT_SET_U32("video,clock", s->conn_state.mode.clock); 2377 FDT_SET_U32("video,hdisplay", s->conn_state.mode.hdisplay); 2378 FDT_SET_U32("video,vdisplay", s->conn_state.mode.vdisplay); 2379 FDT_SET_U32("video,crtc_hsync_end", s->conn_state.mode.crtc_hsync_end); 2380 FDT_SET_U32("video,crtc_vsync_end", s->conn_state.mode.crtc_vsync_end); 2381 FDT_SET_U32("video,vrefresh", 2382 drm_mode_vrefresh(&s->conn_state.mode)); 2383 FDT_SET_U32("video,flags", s->conn_state.mode.flags); 2384 FDT_SET_U32("video,aspect_ratio", s->conn_state.mode.picture_aspect_ratio); 2385 FDT_SET_U32("overscan,left_margin", s->conn_state.overscan.left_margin); 2386 FDT_SET_U32("overscan,right_margin", s->conn_state.overscan.right_margin); 2387 FDT_SET_U32("overscan,top_margin", s->conn_state.overscan.top_margin); 2388 FDT_SET_U32("overscan,bottom_margin", s->conn_state.overscan.bottom_margin); 2389 2390 if (s->conn_state.disp_info) { 2391 cacm_header = (const char*)&s->conn_state.disp_info->cacm_header; 2392 2393 FDT_SET_U32("bcsh,brightness", s->conn_state.disp_info->bcsh_info.brightness); 2394 FDT_SET_U32("bcsh,contrast", s->conn_state.disp_info->bcsh_info.contrast); 2395 FDT_SET_U32("bcsh,saturation", s->conn_state.disp_info->bcsh_info.saturation); 2396 FDT_SET_U32("bcsh,hue", s->conn_state.disp_info->bcsh_info.hue); 2397 2398 if (!strncasecmp(cacm_header, "CACM", 4)) { 2399 FDT_SET_U32("post-csc,hue", 2400 s->conn_state.disp_info->csc_info.hue); 2401 FDT_SET_U32("post-csc,saturation", 2402 s->conn_state.disp_info->csc_info.saturation); 2403 FDT_SET_U32("post-csc,contrast", 2404 s->conn_state.disp_info->csc_info.contrast); 2405 FDT_SET_U32("post-csc,brightness", 2406 s->conn_state.disp_info->csc_info.brightness); 2407 FDT_SET_U32("post-csc,r-gain", 2408 s->conn_state.disp_info->csc_info.r_gain); 2409 FDT_SET_U32("post-csc,g-gain", 2410 s->conn_state.disp_info->csc_info.g_gain); 2411 FDT_SET_U32("post-csc,b-gain", 2412 s->conn_state.disp_info->csc_info.b_gain); 2413 FDT_SET_U32("post-csc,r-offset", 2414 s->conn_state.disp_info->csc_info.r_offset); 2415 FDT_SET_U32("post-csc,g-offset", 2416 s->conn_state.disp_info->csc_info.g_offset); 2417 FDT_SET_U32("post-csc,b-offset", 2418 s->conn_state.disp_info->csc_info.b_offset); 2419 FDT_SET_U32("post-csc,enable", 2420 s->conn_state.disp_info->csc_info.csc_enable); 2421 } 2422 } 2423 2424 if (s->conn_state.disp_info->cubic_lut_data.size && 2425 CONFIG_ROCKCHIP_CUBIC_LUT_SIZE) 2426 FDT_SET_U32("cubic_lut,offset", get_cubic_lut_offset(s->crtc_state.crtc_id)); 2427 2428 #undef FDT_SET_U32 2429 } 2430 } 2431 2432 int rockchip_display_bind(struct udevice *dev) 2433 { 2434 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev); 2435 2436 plat->size = DRM_ROCKCHIP_FB_SIZE + MEMORY_POOL_SIZE; 2437 2438 return 0; 2439 } 2440 2441 static const struct udevice_id rockchip_display_ids[] = { 2442 { .compatible = "rockchip,display-subsystem" }, 2443 { } 2444 }; 2445 2446 U_BOOT_DRIVER(rockchip_display) = { 2447 .name = "rockchip_display", 2448 .id = UCLASS_VIDEO, 2449 .of_match = rockchip_display_ids, 2450 .bind = rockchip_display_bind, 2451 .probe = rockchip_display_probe, 2452 }; 2453 2454 static int do_rockchip_logo_show(cmd_tbl_t *cmdtp, int flag, int argc, 2455 char *const argv[]) 2456 { 2457 if (argc != 1) 2458 return CMD_RET_USAGE; 2459 2460 rockchip_show_logo(); 2461 2462 return 0; 2463 } 2464 2465 static int do_rockchip_show_bmp(cmd_tbl_t *cmdtp, int flag, int argc, 2466 char *const argv[]) 2467 { 2468 if (argc != 2) 2469 return CMD_RET_USAGE; 2470 2471 rockchip_show_bmp(argv[1]); 2472 2473 return 0; 2474 } 2475 2476 static int do_rockchip_vop_dump(cmd_tbl_t *cmdtp, int flag, int argc, 2477 char *const argv[]) 2478 { 2479 int ret; 2480 2481 if (argc < 1 || argc > 2) 2482 return CMD_RET_USAGE; 2483 2484 ret = rockchip_vop_dump(argv[1]); 2485 2486 return ret; 2487 } 2488 2489 U_BOOT_CMD( 2490 rockchip_show_logo, 1, 1, do_rockchip_logo_show, 2491 "load and display log from resource partition", 2492 NULL 2493 ); 2494 2495 U_BOOT_CMD( 2496 rockchip_show_bmp, 2, 1, do_rockchip_show_bmp, 2497 "load and display bmp from resource partition", 2498 " <bmp_name>" 2499 ); 2500 2501 U_BOOT_CMD( 2502 vop_dump, 2, 1, do_rockchip_vop_dump, 2503 "dump vop regs", 2504 " [a/all]" 2505 ); 2506