1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT) 2 /* 3 * Copyright (c) 2020 Rockchip Electronics Co., Ltd. 4 * Author: Andy Yan <andy.yan@rock-chips.com> 5 */ 6 #include <linux/bitfield.h> 7 #include <linux/clk.h> 8 #include <linux/component.h> 9 #include <linux/delay.h> 10 #include <linux/iopoll.h> 11 #include <linux/kernel.h> 12 #include <linux/mfd/syscon.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/of_device.h> 16 #include <linux/of_graph.h> 17 #include <linux/platform_device.h> 18 #include <linux/pm_runtime.h> 19 #include <linux/regmap.h> 20 #include <linux/swab.h> 21 22 #include <drm/drm.h> 23 #include <drm/drm_atomic.h> 24 #include <drm/drm_atomic_uapi.h> 25 #include <drm/drm_blend.h> 26 #include <drm/drm_crtc.h> 27 #include <drm/drm_crtc_helper.h> 28 #include <drm/drm_debugfs.h> 29 #include <drm/drm_flip_work.h> 30 #include <drm/drm_framebuffer.h> 31 #include <drm/drm_plane_helper.h> 32 #include <drm/drm_probe_helper.h> 33 #include <drm/drm_vblank.h> 34 35 #include <uapi/linux/videodev2.h> 36 #include <dt-bindings/soc/rockchip,vop2.h> 37 38 #include "rockchip_drm_drv.h" 39 #include "rockchip_drm_gem.h" 40 #include "rockchip_drm_fb.h" 41 #include "rockchip_drm_vop2.h" 42 43 /* 44 * VOP2 architecture 45 * 46 +----------+ +-------------+ +-----------+ 47 | Cluster | | Sel 1 from 6| | 1 from 3 | 48 | window0 | | Layer0 | | RGB | 49 +----------+ +-------------+ +---------------+ +-------------+ +-----------+ 50 +----------+ +-------------+ |N from 6 layers| | | 51 | Cluster | | Sel 1 from 6| | Overlay0 +--->| Video Port0 | +-----------+ 52 | window1 | | Layer1 | | | | | | 1 from 3 | 53 +----------+ +-------------+ +---------------+ +-------------+ | LVDS | 54 +----------+ +-------------+ +-----------+ 55 | Esmart | | Sel 1 from 6| 56 | window0 | | Layer2 | +---------------+ +-------------+ +-----------+ 57 +----------+ +-------------+ |N from 6 Layers| | | +--> | 1 from 3 | 58 +----------+ +-------------+ --------> | Overlay1 +--->| Video Port1 | | MIPI | 59 | Esmart | | Sel 1 from 6| --------> | | | | +-----------+ 60 | Window1 | | Layer3 | +---------------+ +-------------+ 61 +----------+ +-------------+ +-----------+ 62 +----------+ +-------------+ | 1 from 3 | 63 | Smart | | Sel 1 from 6| +---------------+ +-------------+ | HDMI | 64 | Window0 | | Layer4 | |N from 6 Layers| | | +-----------+ 65 +----------+ +-------------+ | Overlay2 +--->| Video Port2 | 66 +----------+ +-------------+ | | | | +-----------+ 67 | Smart | | Sel 1 from 6| +---------------+ +-------------+ | 1 from 3 | 68 | Window1 | | Layer5 | | eDP | 69 +----------+ +-------------+ +-----------+ 70 * 71 */ 72 73 enum vop2_data_format { 74 VOP2_FMT_ARGB8888 = 0, 75 VOP2_FMT_RGB888, 76 VOP2_FMT_RGB565, 77 VOP2_FMT_XRGB101010, 78 VOP2_FMT_YUV420SP, 79 VOP2_FMT_YUV422SP, 80 VOP2_FMT_YUV444SP, 81 VOP2_FMT_YUYV422 = 8, 82 VOP2_FMT_YUYV420, 83 VOP2_FMT_VYUY422, 84 VOP2_FMT_VYUY420, 85 VOP2_FMT_YUV420SP_TILE_8x4 = 0x10, 86 VOP2_FMT_YUV420SP_TILE_16x2, 87 VOP2_FMT_YUV422SP_TILE_8x4, 88 VOP2_FMT_YUV422SP_TILE_16x2, 89 VOP2_FMT_YUV420SP_10, 90 VOP2_FMT_YUV422SP_10, 91 VOP2_FMT_YUV444SP_10, 92 }; 93 94 enum vop2_afbc_format { 95 VOP2_AFBC_FMT_RGB565, 96 VOP2_AFBC_FMT_ARGB2101010 = 2, 97 VOP2_AFBC_FMT_YUV420_10BIT, 98 VOP2_AFBC_FMT_RGB888, 99 VOP2_AFBC_FMT_ARGB8888, 100 VOP2_AFBC_FMT_YUV420 = 9, 101 VOP2_AFBC_FMT_YUV422 = 0xb, 102 VOP2_AFBC_FMT_YUV422_10BIT = 0xe, 103 VOP2_AFBC_FMT_INVALID = -1, 104 }; 105 106 union vop2_alpha_ctrl { 107 u32 val; 108 struct { 109 /* [0:1] */ 110 u32 color_mode:1; 111 u32 alpha_mode:1; 112 /* [2:3] */ 113 u32 blend_mode:2; 114 u32 alpha_cal_mode:1; 115 /* [5:7] */ 116 u32 factor_mode:3; 117 /* [8:9] */ 118 u32 alpha_en:1; 119 u32 src_dst_swap:1; 120 u32 reserved:6; 121 /* [16:23] */ 122 u32 glb_alpha:8; 123 } bits; 124 }; 125 126 struct vop2_alpha { 127 union vop2_alpha_ctrl src_color_ctrl; 128 union vop2_alpha_ctrl dst_color_ctrl; 129 union vop2_alpha_ctrl src_alpha_ctrl; 130 union vop2_alpha_ctrl dst_alpha_ctrl; 131 }; 132 133 struct vop2_alpha_config { 134 bool src_premulti_en; 135 bool dst_premulti_en; 136 bool src_pixel_alpha_en; 137 bool dst_pixel_alpha_en; 138 u16 src_glb_alpha_value; 139 u16 dst_glb_alpha_value; 140 }; 141 142 struct vop2_win { 143 struct vop2 *vop2; 144 struct drm_plane base; 145 const struct vop2_win_data *data; 146 struct regmap_field *reg[VOP2_WIN_MAX_REG]; 147 148 /** 149 * @win_id: graphic window id, a cluster may be split into two 150 * graphics windows. 151 */ 152 u8 win_id; 153 u8 delay; 154 u32 offset; 155 156 enum drm_plane_type type; 157 }; 158 159 struct vop2_video_port { 160 struct drm_crtc crtc; 161 struct vop2 *vop2; 162 struct clk *dclk; 163 unsigned int id; 164 const struct vop2_video_port_regs *regs; 165 const struct vop2_video_port_data *data; 166 167 struct completion dsp_hold_completion; 168 169 /** 170 * @win_mask: Bitmask of windows attached to the video port; 171 */ 172 u32 win_mask; 173 174 struct vop2_win *primary_plane; 175 struct drm_pending_vblank_event *event; 176 177 unsigned int nlayers; 178 }; 179 180 struct vop2 { 181 struct device *dev; 182 struct drm_device *drm; 183 struct vop2_video_port vps[ROCKCHIP_MAX_CRTC]; 184 185 const struct vop2_data *data; 186 /* 187 * Number of windows that are registered as plane, may be less than the 188 * total number of hardware windows. 189 */ 190 u32 registered_num_wins; 191 192 void __iomem *regs; 193 struct regmap *map; 194 195 struct regmap *grf; 196 197 /* physical map length of vop2 register */ 198 u32 len; 199 200 void __iomem *lut_regs; 201 202 /* protects crtc enable/disable */ 203 struct mutex vop2_lock; 204 205 int irq; 206 207 /* 208 * Some global resources are shared between all video ports(crtcs), so 209 * we need a ref counter here. 210 */ 211 unsigned int enable_count; 212 struct clk *hclk; 213 struct clk *aclk; 214 215 /* must be put at the end of the struct */ 216 struct vop2_win win[]; 217 }; 218 219 static struct vop2_video_port *to_vop2_video_port(struct drm_crtc *crtc) 220 { 221 return container_of(crtc, struct vop2_video_port, crtc); 222 } 223 224 static struct vop2_win *to_vop2_win(struct drm_plane *p) 225 { 226 return container_of(p, struct vop2_win, base); 227 } 228 229 static void vop2_lock(struct vop2 *vop2) 230 { 231 mutex_lock(&vop2->vop2_lock); 232 } 233 234 static void vop2_unlock(struct vop2 *vop2) 235 { 236 mutex_unlock(&vop2->vop2_lock); 237 } 238 239 static void vop2_writel(struct vop2 *vop2, u32 offset, u32 v) 240 { 241 regmap_write(vop2->map, offset, v); 242 } 243 244 static void vop2_vp_write(struct vop2_video_port *vp, u32 offset, u32 v) 245 { 246 regmap_write(vp->vop2->map, vp->data->offset + offset, v); 247 } 248 249 static u32 vop2_readl(struct vop2 *vop2, u32 offset) 250 { 251 u32 val; 252 253 regmap_read(vop2->map, offset, &val); 254 255 return val; 256 } 257 258 static void vop2_win_write(const struct vop2_win *win, unsigned int reg, u32 v) 259 { 260 regmap_field_write(win->reg[reg], v); 261 } 262 263 static bool vop2_cluster_window(const struct vop2_win *win) 264 { 265 return win->data->feature & WIN_FEATURE_CLUSTER; 266 } 267 268 static void vop2_cfg_done(struct vop2_video_port *vp) 269 { 270 struct vop2 *vop2 = vp->vop2; 271 272 regmap_set_bits(vop2->map, RK3568_REG_CFG_DONE, 273 BIT(vp->id) | RK3568_REG_CFG_DONE__GLB_CFG_DONE_EN); 274 } 275 276 static void vop2_win_disable(struct vop2_win *win) 277 { 278 vop2_win_write(win, VOP2_WIN_ENABLE, 0); 279 280 if (vop2_cluster_window(win)) 281 vop2_win_write(win, VOP2_WIN_CLUSTER_ENABLE, 0); 282 } 283 284 static enum vop2_data_format vop2_convert_format(u32 format) 285 { 286 switch (format) { 287 case DRM_FORMAT_XRGB8888: 288 case DRM_FORMAT_ARGB8888: 289 case DRM_FORMAT_XBGR8888: 290 case DRM_FORMAT_ABGR8888: 291 return VOP2_FMT_ARGB8888; 292 case DRM_FORMAT_RGB888: 293 case DRM_FORMAT_BGR888: 294 return VOP2_FMT_RGB888; 295 case DRM_FORMAT_RGB565: 296 case DRM_FORMAT_BGR565: 297 return VOP2_FMT_RGB565; 298 case DRM_FORMAT_NV12: 299 return VOP2_FMT_YUV420SP; 300 case DRM_FORMAT_NV16: 301 return VOP2_FMT_YUV422SP; 302 case DRM_FORMAT_NV24: 303 return VOP2_FMT_YUV444SP; 304 case DRM_FORMAT_YUYV: 305 case DRM_FORMAT_YVYU: 306 return VOP2_FMT_VYUY422; 307 case DRM_FORMAT_VYUY: 308 case DRM_FORMAT_UYVY: 309 return VOP2_FMT_YUYV422; 310 default: 311 DRM_ERROR("unsupported format[%08x]\n", format); 312 return -EINVAL; 313 } 314 } 315 316 static enum vop2_afbc_format vop2_convert_afbc_format(u32 format) 317 { 318 switch (format) { 319 case DRM_FORMAT_XRGB8888: 320 case DRM_FORMAT_ARGB8888: 321 case DRM_FORMAT_XBGR8888: 322 case DRM_FORMAT_ABGR8888: 323 return VOP2_AFBC_FMT_ARGB8888; 324 case DRM_FORMAT_RGB888: 325 case DRM_FORMAT_BGR888: 326 return VOP2_AFBC_FMT_RGB888; 327 case DRM_FORMAT_RGB565: 328 case DRM_FORMAT_BGR565: 329 return VOP2_AFBC_FMT_RGB565; 330 case DRM_FORMAT_NV12: 331 return VOP2_AFBC_FMT_YUV420; 332 case DRM_FORMAT_NV16: 333 return VOP2_AFBC_FMT_YUV422; 334 default: 335 return VOP2_AFBC_FMT_INVALID; 336 } 337 338 return VOP2_AFBC_FMT_INVALID; 339 } 340 341 static bool vop2_win_rb_swap(u32 format) 342 { 343 switch (format) { 344 case DRM_FORMAT_XBGR8888: 345 case DRM_FORMAT_ABGR8888: 346 case DRM_FORMAT_BGR888: 347 case DRM_FORMAT_BGR565: 348 return true; 349 default: 350 return false; 351 } 352 } 353 354 static bool vop2_afbc_rb_swap(u32 format) 355 { 356 switch (format) { 357 case DRM_FORMAT_NV24: 358 return true; 359 default: 360 return false; 361 } 362 } 363 364 static bool vop2_afbc_uv_swap(u32 format) 365 { 366 switch (format) { 367 case DRM_FORMAT_NV12: 368 case DRM_FORMAT_NV16: 369 return true; 370 default: 371 return false; 372 } 373 } 374 375 static bool vop2_win_uv_swap(u32 format) 376 { 377 switch (format) { 378 case DRM_FORMAT_NV12: 379 case DRM_FORMAT_NV16: 380 case DRM_FORMAT_NV24: 381 return true; 382 default: 383 return false; 384 } 385 } 386 387 static bool vop2_win_dither_up(u32 format) 388 { 389 switch (format) { 390 case DRM_FORMAT_BGR565: 391 case DRM_FORMAT_RGB565: 392 return true; 393 default: 394 return false; 395 } 396 } 397 398 static bool vop2_output_uv_swap(u32 bus_format, u32 output_mode) 399 { 400 /* 401 * FIXME: 402 * 403 * There is no media type for YUV444 output, 404 * so when out_mode is AAAA or P888, assume output is YUV444 on 405 * yuv format. 406 * 407 * From H/W testing, YUV444 mode need a rb swap. 408 */ 409 if (bus_format == MEDIA_BUS_FMT_YVYU8_1X16 || 410 bus_format == MEDIA_BUS_FMT_VYUY8_1X16 || 411 bus_format == MEDIA_BUS_FMT_YVYU8_2X8 || 412 bus_format == MEDIA_BUS_FMT_VYUY8_2X8 || 413 ((bus_format == MEDIA_BUS_FMT_YUV8_1X24 || 414 bus_format == MEDIA_BUS_FMT_YUV10_1X30) && 415 (output_mode == ROCKCHIP_OUT_MODE_AAAA || 416 output_mode == ROCKCHIP_OUT_MODE_P888))) 417 return true; 418 else 419 return false; 420 } 421 422 static bool is_yuv_output(u32 bus_format) 423 { 424 switch (bus_format) { 425 case MEDIA_BUS_FMT_YUV8_1X24: 426 case MEDIA_BUS_FMT_YUV10_1X30: 427 case MEDIA_BUS_FMT_UYYVYY8_0_5X24: 428 case MEDIA_BUS_FMT_UYYVYY10_0_5X30: 429 case MEDIA_BUS_FMT_YUYV8_2X8: 430 case MEDIA_BUS_FMT_YVYU8_2X8: 431 case MEDIA_BUS_FMT_UYVY8_2X8: 432 case MEDIA_BUS_FMT_VYUY8_2X8: 433 case MEDIA_BUS_FMT_YUYV8_1X16: 434 case MEDIA_BUS_FMT_YVYU8_1X16: 435 case MEDIA_BUS_FMT_UYVY8_1X16: 436 case MEDIA_BUS_FMT_VYUY8_1X16: 437 return true; 438 default: 439 return false; 440 } 441 } 442 443 static bool rockchip_afbc(struct drm_plane *plane, u64 modifier) 444 { 445 int i; 446 447 if (modifier == DRM_FORMAT_MOD_LINEAR) 448 return false; 449 450 for (i = 0 ; i < plane->modifier_count; i++) 451 if (plane->modifiers[i] == modifier) 452 return true; 453 454 return false; 455 } 456 457 static bool rockchip_vop2_mod_supported(struct drm_plane *plane, u32 format, 458 u64 modifier) 459 { 460 struct vop2_win *win = to_vop2_win(plane); 461 struct vop2 *vop2 = win->vop2; 462 463 if (modifier == DRM_FORMAT_MOD_INVALID) 464 return false; 465 466 if (modifier == DRM_FORMAT_MOD_LINEAR) 467 return true; 468 469 if (!rockchip_afbc(plane, modifier)) { 470 drm_err(vop2->drm, "Unsupported format modifier 0x%llx\n", 471 modifier); 472 473 return false; 474 } 475 476 return vop2_convert_afbc_format(format) >= 0; 477 } 478 479 static u32 vop2_afbc_transform_offset(struct drm_plane_state *pstate, 480 bool afbc_half_block_en) 481 { 482 struct drm_rect *src = &pstate->src; 483 struct drm_framebuffer *fb = pstate->fb; 484 u32 bpp = fb->format->cpp[0] * 8; 485 u32 vir_width = (fb->pitches[0] << 3) / bpp; 486 u32 width = drm_rect_width(src) >> 16; 487 u32 height = drm_rect_height(src) >> 16; 488 u32 act_xoffset = src->x1 >> 16; 489 u32 act_yoffset = src->y1 >> 16; 490 u32 align16_crop = 0; 491 u32 align64_crop = 0; 492 u32 height_tmp; 493 u8 tx, ty; 494 u8 bottom_crop_line_num = 0; 495 496 /* 16 pixel align */ 497 if (height & 0xf) 498 align16_crop = 16 - (height & 0xf); 499 500 height_tmp = height + align16_crop; 501 502 /* 64 pixel align */ 503 if (height_tmp & 0x3f) 504 align64_crop = 64 - (height_tmp & 0x3f); 505 506 bottom_crop_line_num = align16_crop + align64_crop; 507 508 switch (pstate->rotation & 509 (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y | 510 DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_270)) { 511 case DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y: 512 tx = 16 - ((act_xoffset + width) & 0xf); 513 ty = bottom_crop_line_num - act_yoffset; 514 break; 515 case DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90: 516 tx = bottom_crop_line_num - act_yoffset; 517 ty = vir_width - width - act_xoffset; 518 break; 519 case DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_270: 520 tx = act_yoffset; 521 ty = act_xoffset; 522 break; 523 case DRM_MODE_REFLECT_X: 524 tx = 16 - ((act_xoffset + width) & 0xf); 525 ty = act_yoffset; 526 break; 527 case DRM_MODE_REFLECT_Y: 528 tx = act_xoffset; 529 ty = bottom_crop_line_num - act_yoffset; 530 break; 531 case DRM_MODE_ROTATE_90: 532 tx = bottom_crop_line_num - act_yoffset; 533 ty = act_xoffset; 534 break; 535 case DRM_MODE_ROTATE_270: 536 tx = act_yoffset; 537 ty = vir_width - width - act_xoffset; 538 break; 539 case 0: 540 tx = act_xoffset; 541 ty = act_yoffset; 542 break; 543 } 544 545 if (afbc_half_block_en) 546 ty &= 0x7f; 547 548 #define TRANSFORM_XOFFSET GENMASK(7, 0) 549 #define TRANSFORM_YOFFSET GENMASK(23, 16) 550 return FIELD_PREP(TRANSFORM_XOFFSET, tx) | 551 FIELD_PREP(TRANSFORM_YOFFSET, ty); 552 } 553 554 /* 555 * A Cluster window has 2048 x 16 line buffer, which can 556 * works at 2048 x 16(Full) or 4096 x 8 (Half) mode. 557 * for Cluster_lb_mode register: 558 * 0: half mode, for plane input width range 2048 ~ 4096 559 * 1: half mode, for cluster work at 2 * 2048 plane mode 560 * 2: half mode, for rotate_90/270 mode 561 * 562 */ 563 static int vop2_get_cluster_lb_mode(struct vop2_win *win, 564 struct drm_plane_state *pstate) 565 { 566 if ((pstate->rotation & DRM_MODE_ROTATE_270) || 567 (pstate->rotation & DRM_MODE_ROTATE_90)) 568 return 2; 569 else 570 return 0; 571 } 572 573 static u16 vop2_scale_factor(u32 src, u32 dst) 574 { 575 u32 fac; 576 int shift; 577 578 if (src == dst) 579 return 0; 580 581 if (dst < 2) 582 return U16_MAX; 583 584 if (src < 2) 585 return 0; 586 587 if (src > dst) 588 shift = 12; 589 else 590 shift = 16; 591 592 src--; 593 dst--; 594 595 fac = DIV_ROUND_UP(src << shift, dst) - 1; 596 597 if (fac > U16_MAX) 598 return U16_MAX; 599 600 return fac; 601 } 602 603 static void vop2_setup_scale(struct vop2 *vop2, const struct vop2_win *win, 604 u32 src_w, u32 src_h, u32 dst_w, 605 u32 dst_h, u32 pixel_format) 606 { 607 const struct drm_format_info *info; 608 u16 hor_scl_mode, ver_scl_mode; 609 u16 hscl_filter_mode, vscl_filter_mode; 610 u8 gt2 = 0; 611 u8 gt4 = 0; 612 u32 val; 613 614 info = drm_format_info(pixel_format); 615 616 if (src_h >= (4 * dst_h)) { 617 gt4 = 1; 618 src_h >>= 2; 619 } else if (src_h >= (2 * dst_h)) { 620 gt2 = 1; 621 src_h >>= 1; 622 } 623 624 hor_scl_mode = scl_get_scl_mode(src_w, dst_w); 625 ver_scl_mode = scl_get_scl_mode(src_h, dst_h); 626 627 if (hor_scl_mode == SCALE_UP) 628 hscl_filter_mode = VOP2_SCALE_UP_BIC; 629 else 630 hscl_filter_mode = VOP2_SCALE_DOWN_BIL; 631 632 if (ver_scl_mode == SCALE_UP) 633 vscl_filter_mode = VOP2_SCALE_UP_BIL; 634 else 635 vscl_filter_mode = VOP2_SCALE_DOWN_BIL; 636 637 /* 638 * RK3568 VOP Esmart/Smart dsp_w should be even pixel 639 * at scale down mode 640 */ 641 if (!(win->data->feature & WIN_FEATURE_AFBDC)) { 642 if ((hor_scl_mode == SCALE_DOWN) && (dst_w & 0x1)) { 643 drm_dbg(vop2->drm, "%s dst_w[%d] should align as 2 pixel\n", 644 win->data->name, dst_w); 645 dst_w++; 646 } 647 } 648 649 val = vop2_scale_factor(src_w, dst_w); 650 vop2_win_write(win, VOP2_WIN_SCALE_YRGB_X, val); 651 val = vop2_scale_factor(src_h, dst_h); 652 vop2_win_write(win, VOP2_WIN_SCALE_YRGB_Y, val); 653 654 vop2_win_write(win, VOP2_WIN_VSD_YRGB_GT4, gt4); 655 vop2_win_write(win, VOP2_WIN_VSD_YRGB_GT2, gt2); 656 657 vop2_win_write(win, VOP2_WIN_YRGB_HOR_SCL_MODE, hor_scl_mode); 658 vop2_win_write(win, VOP2_WIN_YRGB_VER_SCL_MODE, ver_scl_mode); 659 660 if (vop2_cluster_window(win)) 661 return; 662 663 vop2_win_write(win, VOP2_WIN_YRGB_HSCL_FILTER_MODE, hscl_filter_mode); 664 vop2_win_write(win, VOP2_WIN_YRGB_VSCL_FILTER_MODE, vscl_filter_mode); 665 666 if (info->is_yuv) { 667 src_w /= info->hsub; 668 src_h /= info->vsub; 669 670 gt4 = 0; 671 gt2 = 0; 672 673 if (src_h >= (4 * dst_h)) { 674 gt4 = 1; 675 src_h >>= 2; 676 } else if (src_h >= (2 * dst_h)) { 677 gt2 = 1; 678 src_h >>= 1; 679 } 680 681 hor_scl_mode = scl_get_scl_mode(src_w, dst_w); 682 ver_scl_mode = scl_get_scl_mode(src_h, dst_h); 683 684 val = vop2_scale_factor(src_w, dst_w); 685 vop2_win_write(win, VOP2_WIN_SCALE_CBCR_X, val); 686 687 val = vop2_scale_factor(src_h, dst_h); 688 vop2_win_write(win, VOP2_WIN_SCALE_CBCR_Y, val); 689 690 vop2_win_write(win, VOP2_WIN_VSD_CBCR_GT4, gt4); 691 vop2_win_write(win, VOP2_WIN_VSD_CBCR_GT2, gt2); 692 vop2_win_write(win, VOP2_WIN_CBCR_HOR_SCL_MODE, hor_scl_mode); 693 vop2_win_write(win, VOP2_WIN_CBCR_VER_SCL_MODE, ver_scl_mode); 694 vop2_win_write(win, VOP2_WIN_CBCR_HSCL_FILTER_MODE, hscl_filter_mode); 695 vop2_win_write(win, VOP2_WIN_CBCR_VSCL_FILTER_MODE, vscl_filter_mode); 696 } 697 } 698 699 static int vop2_convert_csc_mode(int csc_mode) 700 { 701 switch (csc_mode) { 702 case V4L2_COLORSPACE_SMPTE170M: 703 case V4L2_COLORSPACE_470_SYSTEM_M: 704 case V4L2_COLORSPACE_470_SYSTEM_BG: 705 return CSC_BT601L; 706 case V4L2_COLORSPACE_REC709: 707 case V4L2_COLORSPACE_SMPTE240M: 708 case V4L2_COLORSPACE_DEFAULT: 709 return CSC_BT709L; 710 case V4L2_COLORSPACE_JPEG: 711 return CSC_BT601F; 712 case V4L2_COLORSPACE_BT2020: 713 return CSC_BT2020; 714 default: 715 return CSC_BT709L; 716 } 717 } 718 719 /* 720 * colorspace path: 721 * Input Win csc Output 722 * 1. YUV(2020) --> Y2R->2020To709->R2Y --> YUV_OUTPUT(601/709) 723 * RGB --> R2Y __/ 724 * 725 * 2. YUV(2020) --> bypasss --> YUV_OUTPUT(2020) 726 * RGB --> 709To2020->R2Y __/ 727 * 728 * 3. YUV(2020) --> Y2R->2020To709 --> RGB_OUTPUT(709) 729 * RGB --> R2Y __/ 730 * 731 * 4. YUV(601/709)-> Y2R->709To2020->R2Y --> YUV_OUTPUT(2020) 732 * RGB --> 709To2020->R2Y __/ 733 * 734 * 5. YUV(601/709)-> bypass --> YUV_OUTPUT(709) 735 * RGB --> R2Y __/ 736 * 737 * 6. YUV(601/709)-> bypass --> YUV_OUTPUT(601) 738 * RGB --> R2Y(601) __/ 739 * 740 * 7. YUV --> Y2R(709) --> RGB_OUTPUT(709) 741 * RGB --> bypass __/ 742 * 743 * 8. RGB --> 709To2020->R2Y --> YUV_OUTPUT(2020) 744 * 745 * 9. RGB --> R2Y(709) --> YUV_OUTPUT(709) 746 * 747 * 10. RGB --> R2Y(601) --> YUV_OUTPUT(601) 748 * 749 * 11. RGB --> bypass --> RGB_OUTPUT(709) 750 */ 751 752 static void vop2_setup_csc_mode(struct vop2_video_port *vp, 753 struct vop2_win *win, 754 struct drm_plane_state *pstate) 755 { 756 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(vp->crtc.state); 757 int is_input_yuv = pstate->fb->format->is_yuv; 758 int is_output_yuv = is_yuv_output(vcstate->bus_format); 759 int input_csc = V4L2_COLORSPACE_DEFAULT; 760 int output_csc = vcstate->color_space; 761 bool r2y_en, y2r_en; 762 int csc_mode; 763 764 if (is_input_yuv && !is_output_yuv) { 765 y2r_en = true; 766 r2y_en = false; 767 csc_mode = vop2_convert_csc_mode(input_csc); 768 } else if (!is_input_yuv && is_output_yuv) { 769 y2r_en = false; 770 r2y_en = true; 771 csc_mode = vop2_convert_csc_mode(output_csc); 772 } else { 773 y2r_en = false; 774 r2y_en = false; 775 csc_mode = false; 776 } 777 778 vop2_win_write(win, VOP2_WIN_Y2R_EN, y2r_en); 779 vop2_win_write(win, VOP2_WIN_R2Y_EN, r2y_en); 780 vop2_win_write(win, VOP2_WIN_CSC_MODE, csc_mode); 781 } 782 783 static void vop2_crtc_enable_irq(struct vop2_video_port *vp, u32 irq) 784 { 785 struct vop2 *vop2 = vp->vop2; 786 787 vop2_writel(vop2, RK3568_VP_INT_CLR(vp->id), irq << 16 | irq); 788 vop2_writel(vop2, RK3568_VP_INT_EN(vp->id), irq << 16 | irq); 789 } 790 791 static void vop2_crtc_disable_irq(struct vop2_video_port *vp, u32 irq) 792 { 793 struct vop2 *vop2 = vp->vop2; 794 795 vop2_writel(vop2, RK3568_VP_INT_EN(vp->id), irq << 16); 796 } 797 798 static int vop2_core_clks_prepare_enable(struct vop2 *vop2) 799 { 800 int ret; 801 802 ret = clk_prepare_enable(vop2->hclk); 803 if (ret < 0) { 804 drm_err(vop2->drm, "failed to enable hclk - %d\n", ret); 805 return ret; 806 } 807 808 ret = clk_prepare_enable(vop2->aclk); 809 if (ret < 0) { 810 drm_err(vop2->drm, "failed to enable aclk - %d\n", ret); 811 goto err; 812 } 813 814 return 0; 815 err: 816 clk_disable_unprepare(vop2->hclk); 817 818 return ret; 819 } 820 821 static void vop2_enable(struct vop2 *vop2) 822 { 823 int ret; 824 825 ret = pm_runtime_get_sync(vop2->dev); 826 if (ret < 0) { 827 drm_err(vop2->drm, "failed to get pm runtime: %d\n", ret); 828 return; 829 } 830 831 ret = vop2_core_clks_prepare_enable(vop2); 832 if (ret) { 833 pm_runtime_put_sync(vop2->dev); 834 return; 835 } 836 837 ret = rockchip_drm_dma_attach_device(vop2->drm, vop2->dev); 838 if (ret) { 839 drm_err(vop2->drm, "failed to attach dma mapping, %d\n", ret); 840 return; 841 } 842 843 if (vop2->data->soc_id == 3566) 844 vop2_writel(vop2, RK3568_OTP_WIN_EN, 1); 845 846 vop2_writel(vop2, RK3568_REG_CFG_DONE, RK3568_REG_CFG_DONE__GLB_CFG_DONE_EN); 847 848 /* 849 * Disable auto gating, this is a workaround to 850 * avoid display image shift when a window enabled. 851 */ 852 regmap_clear_bits(vop2->map, RK3568_SYS_AUTO_GATING_CTRL, 853 RK3568_SYS_AUTO_GATING_CTRL__AUTO_GATING_EN); 854 855 vop2_writel(vop2, RK3568_SYS0_INT_CLR, 856 VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR); 857 vop2_writel(vop2, RK3568_SYS0_INT_EN, 858 VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR); 859 vop2_writel(vop2, RK3568_SYS1_INT_CLR, 860 VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR); 861 vop2_writel(vop2, RK3568_SYS1_INT_EN, 862 VOP2_INT_BUS_ERRPR << 16 | VOP2_INT_BUS_ERRPR); 863 } 864 865 static void vop2_disable(struct vop2 *vop2) 866 { 867 rockchip_drm_dma_detach_device(vop2->drm, vop2->dev); 868 869 pm_runtime_put_sync(vop2->dev); 870 871 clk_disable_unprepare(vop2->aclk); 872 clk_disable_unprepare(vop2->hclk); 873 } 874 875 static void vop2_crtc_atomic_disable(struct drm_crtc *crtc, 876 struct drm_atomic_state *state) 877 { 878 struct vop2_video_port *vp = to_vop2_video_port(crtc); 879 struct vop2 *vop2 = vp->vop2; 880 int ret; 881 882 vop2_lock(vop2); 883 884 drm_crtc_vblank_off(crtc); 885 886 /* 887 * Vop standby will take effect at end of current frame, 888 * if dsp hold valid irq happen, it means standby complete. 889 * 890 * we must wait standby complete when we want to disable aclk, 891 * if not, memory bus maybe dead. 892 */ 893 reinit_completion(&vp->dsp_hold_completion); 894 895 vop2_crtc_enable_irq(vp, VP_INT_DSP_HOLD_VALID); 896 897 vop2_vp_write(vp, RK3568_VP_DSP_CTRL, RK3568_VP_DSP_CTRL__STANDBY); 898 899 ret = wait_for_completion_timeout(&vp->dsp_hold_completion, 900 msecs_to_jiffies(50)); 901 if (!ret) 902 drm_info(vop2->drm, "wait for vp%d dsp_hold timeout\n", vp->id); 903 904 vop2_crtc_disable_irq(vp, VP_INT_DSP_HOLD_VALID); 905 906 clk_disable_unprepare(vp->dclk); 907 908 vop2->enable_count--; 909 910 if (!vop2->enable_count) 911 vop2_disable(vop2); 912 913 vop2_unlock(vop2); 914 915 if (crtc->state->event && !crtc->state->active) { 916 spin_lock_irq(&crtc->dev->event_lock); 917 drm_crtc_send_vblank_event(crtc, crtc->state->event); 918 spin_unlock_irq(&crtc->dev->event_lock); 919 920 crtc->state->event = NULL; 921 } 922 } 923 924 static int vop2_plane_atomic_check(struct drm_plane *plane, 925 struct drm_atomic_state *astate) 926 { 927 struct drm_plane_state *pstate = drm_atomic_get_new_plane_state(astate, plane); 928 struct drm_framebuffer *fb = pstate->fb; 929 struct drm_crtc *crtc = pstate->crtc; 930 struct drm_crtc_state *cstate; 931 struct vop2_video_port *vp; 932 struct vop2 *vop2; 933 const struct vop2_data *vop2_data; 934 struct drm_rect *dest = &pstate->dst; 935 struct drm_rect *src = &pstate->src; 936 int min_scale = FRAC_16_16(1, 8); 937 int max_scale = FRAC_16_16(8, 1); 938 int format; 939 int ret; 940 941 if (!crtc) 942 return 0; 943 944 vp = to_vop2_video_port(crtc); 945 vop2 = vp->vop2; 946 vop2_data = vop2->data; 947 948 cstate = drm_atomic_get_existing_crtc_state(pstate->state, crtc); 949 if (WARN_ON(!cstate)) 950 return -EINVAL; 951 952 ret = drm_atomic_helper_check_plane_state(pstate, cstate, 953 min_scale, max_scale, 954 true, true); 955 if (ret) 956 return ret; 957 958 if (!pstate->visible) 959 return 0; 960 961 format = vop2_convert_format(fb->format->format); 962 if (format < 0) 963 return format; 964 965 if (drm_rect_width(src) >> 16 < 4 || drm_rect_height(src) >> 16 < 4 || 966 drm_rect_width(dest) < 4 || drm_rect_width(dest) < 4) { 967 drm_err(vop2->drm, "Invalid size: %dx%d->%dx%d, min size is 4x4\n", 968 drm_rect_width(src) >> 16, drm_rect_height(src) >> 16, 969 drm_rect_width(dest), drm_rect_height(dest)); 970 pstate->visible = false; 971 return 0; 972 } 973 974 if (drm_rect_width(src) >> 16 > vop2_data->max_input.width || 975 drm_rect_height(src) >> 16 > vop2_data->max_input.height) { 976 drm_err(vop2->drm, "Invalid source: %dx%d. max input: %dx%d\n", 977 drm_rect_width(src) >> 16, 978 drm_rect_height(src) >> 16, 979 vop2_data->max_input.width, 980 vop2_data->max_input.height); 981 return -EINVAL; 982 } 983 984 /* 985 * Src.x1 can be odd when do clip, but yuv plane start point 986 * need align with 2 pixel. 987 */ 988 if (fb->format->is_yuv && ((pstate->src.x1 >> 16) % 2)) { 989 drm_err(vop2->drm, "Invalid Source: Yuv format not support odd xpos\n"); 990 return -EINVAL; 991 } 992 993 return 0; 994 } 995 996 static void vop2_plane_atomic_disable(struct drm_plane *plane, 997 struct drm_atomic_state *state) 998 { 999 struct drm_plane_state *old_pstate = drm_atomic_get_old_plane_state(state, plane); 1000 struct vop2_win *win = to_vop2_win(plane); 1001 struct vop2 *vop2 = win->vop2; 1002 1003 drm_dbg(vop2->drm, "%s disable\n", win->data->name); 1004 1005 if (!old_pstate->crtc) 1006 return; 1007 1008 vop2_win_disable(win); 1009 vop2_win_write(win, VOP2_WIN_YUV_CLIP, 0); 1010 } 1011 1012 /* 1013 * The color key is 10 bit, so all format should 1014 * convert to 10 bit here. 1015 */ 1016 static void vop2_plane_setup_color_key(struct drm_plane *plane, u32 color_key) 1017 { 1018 struct drm_plane_state *pstate = plane->state; 1019 struct drm_framebuffer *fb = pstate->fb; 1020 struct vop2_win *win = to_vop2_win(plane); 1021 u32 color_key_en = 0; 1022 u32 r = 0; 1023 u32 g = 0; 1024 u32 b = 0; 1025 1026 if (!(color_key & VOP2_COLOR_KEY_MASK) || fb->format->is_yuv) { 1027 vop2_win_write(win, VOP2_WIN_COLOR_KEY_EN, 0); 1028 return; 1029 } 1030 1031 switch (fb->format->format) { 1032 case DRM_FORMAT_RGB565: 1033 case DRM_FORMAT_BGR565: 1034 r = (color_key & 0xf800) >> 11; 1035 g = (color_key & 0x7e0) >> 5; 1036 b = (color_key & 0x1f); 1037 r <<= 5; 1038 g <<= 4; 1039 b <<= 5; 1040 color_key_en = 1; 1041 break; 1042 case DRM_FORMAT_XRGB8888: 1043 case DRM_FORMAT_ARGB8888: 1044 case DRM_FORMAT_XBGR8888: 1045 case DRM_FORMAT_ABGR8888: 1046 case DRM_FORMAT_RGB888: 1047 case DRM_FORMAT_BGR888: 1048 r = (color_key & 0xff0000) >> 16; 1049 g = (color_key & 0xff00) >> 8; 1050 b = (color_key & 0xff); 1051 r <<= 2; 1052 g <<= 2; 1053 b <<= 2; 1054 color_key_en = 1; 1055 break; 1056 } 1057 1058 vop2_win_write(win, VOP2_WIN_COLOR_KEY_EN, color_key_en); 1059 vop2_win_write(win, VOP2_WIN_COLOR_KEY, (r << 20) | (g << 10) | b); 1060 } 1061 1062 static void vop2_plane_atomic_update(struct drm_plane *plane, 1063 struct drm_atomic_state *state) 1064 { 1065 struct drm_plane_state *pstate = plane->state; 1066 struct drm_crtc *crtc = pstate->crtc; 1067 struct vop2_win *win = to_vop2_win(plane); 1068 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1069 struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode; 1070 struct vop2 *vop2 = win->vop2; 1071 struct drm_framebuffer *fb = pstate->fb; 1072 u32 bpp = fb->format->cpp[0] * 8; 1073 u32 actual_w, actual_h, dsp_w, dsp_h; 1074 u32 act_info, dsp_info; 1075 u32 format; 1076 u32 afbc_format; 1077 u32 rb_swap; 1078 u32 uv_swap; 1079 struct drm_rect *src = &pstate->src; 1080 struct drm_rect *dest = &pstate->dst; 1081 u32 afbc_tile_num; 1082 u32 transform_offset; 1083 bool dither_up; 1084 bool xmirror = pstate->rotation & DRM_MODE_REFLECT_X ? true : false; 1085 bool ymirror = pstate->rotation & DRM_MODE_REFLECT_Y ? true : false; 1086 bool rotate_270 = pstate->rotation & DRM_MODE_ROTATE_270; 1087 bool rotate_90 = pstate->rotation & DRM_MODE_ROTATE_90; 1088 struct rockchip_gem_object *rk_obj; 1089 unsigned long offset; 1090 bool afbc_en; 1091 dma_addr_t yrgb_mst; 1092 dma_addr_t uv_mst; 1093 1094 /* 1095 * can't update plane when vop2 is disabled. 1096 */ 1097 if (WARN_ON(!crtc)) 1098 return; 1099 1100 if (!pstate->visible) { 1101 vop2_plane_atomic_disable(plane, state); 1102 return; 1103 } 1104 1105 afbc_en = rockchip_afbc(plane, fb->modifier); 1106 1107 offset = (src->x1 >> 16) * fb->format->cpp[0]; 1108 1109 /* 1110 * AFBC HDR_PTR must set to the zero offset of the framebuffer. 1111 */ 1112 if (afbc_en) 1113 offset = 0; 1114 else if (pstate->rotation & DRM_MODE_REFLECT_Y) 1115 offset += ((src->y2 >> 16) - 1) * fb->pitches[0]; 1116 else 1117 offset += (src->y1 >> 16) * fb->pitches[0]; 1118 1119 rk_obj = to_rockchip_obj(fb->obj[0]); 1120 1121 yrgb_mst = rk_obj->dma_addr + offset + fb->offsets[0]; 1122 if (fb->format->is_yuv) { 1123 int hsub = fb->format->hsub; 1124 int vsub = fb->format->vsub; 1125 1126 offset = (src->x1 >> 16) * fb->format->cpp[1] / hsub; 1127 offset += (src->y1 >> 16) * fb->pitches[1] / vsub; 1128 1129 if ((pstate->rotation & DRM_MODE_REFLECT_Y) && !afbc_en) 1130 offset += fb->pitches[1] * ((pstate->src_h >> 16) - 2) / vsub; 1131 1132 rk_obj = to_rockchip_obj(fb->obj[0]); 1133 uv_mst = rk_obj->dma_addr + offset + fb->offsets[1]; 1134 } 1135 1136 actual_w = drm_rect_width(src) >> 16; 1137 actual_h = drm_rect_height(src) >> 16; 1138 dsp_w = drm_rect_width(dest); 1139 1140 if (dest->x1 + dsp_w > adjusted_mode->hdisplay) { 1141 drm_err(vop2->drm, "vp%d %s dest->x1[%d] + dsp_w[%d] exceed mode hdisplay[%d]\n", 1142 vp->id, win->data->name, dest->x1, dsp_w, adjusted_mode->hdisplay); 1143 dsp_w = adjusted_mode->hdisplay - dest->x1; 1144 if (dsp_w < 4) 1145 dsp_w = 4; 1146 actual_w = dsp_w * actual_w / drm_rect_width(dest); 1147 } 1148 1149 dsp_h = drm_rect_height(dest); 1150 1151 if (dest->y1 + dsp_h > adjusted_mode->vdisplay) { 1152 drm_err(vop2->drm, "vp%d %s dest->y1[%d] + dsp_h[%d] exceed mode vdisplay[%d]\n", 1153 vp->id, win->data->name, dest->y1, dsp_h, adjusted_mode->vdisplay); 1154 dsp_h = adjusted_mode->vdisplay - dest->y1; 1155 if (dsp_h < 4) 1156 dsp_h = 4; 1157 actual_h = dsp_h * actual_h / drm_rect_height(dest); 1158 } 1159 1160 /* 1161 * This is workaround solution for IC design: 1162 * esmart can't support scale down when actual_w % 16 == 1. 1163 */ 1164 if (!(win->data->feature & WIN_FEATURE_AFBDC)) { 1165 if (actual_w > dsp_w && (actual_w & 0xf) == 1) { 1166 drm_err(vop2->drm, "vp%d %s act_w[%d] MODE 16 == 1\n", 1167 vp->id, win->data->name, actual_w); 1168 actual_w -= 1; 1169 } 1170 } 1171 1172 if (afbc_en && actual_w % 4) { 1173 drm_err(vop2->drm, "vp%d %s actual_w[%d] not 4 pixel aligned\n", 1174 vp->id, win->data->name, actual_w); 1175 actual_w = ALIGN_DOWN(actual_w, 4); 1176 } 1177 1178 act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff); 1179 dsp_info = (dsp_h - 1) << 16 | ((dsp_w - 1) & 0xffff); 1180 1181 format = vop2_convert_format(fb->format->format); 1182 1183 drm_dbg(vop2->drm, "vp%d update %s[%dx%d->%dx%d@%dx%d] fmt[%p4cc_%s] addr[%pad]\n", 1184 vp->id, win->data->name, actual_w, actual_h, dsp_w, dsp_h, 1185 dest->x1, dest->y1, 1186 &fb->format->format, 1187 afbc_en ? "AFBC" : "", &yrgb_mst); 1188 1189 if (afbc_en) { 1190 u32 stride; 1191 1192 /* the afbc superblock is 16 x 16 */ 1193 afbc_format = vop2_convert_afbc_format(fb->format->format); 1194 1195 /* Enable color transform for YTR */ 1196 if (fb->modifier & AFBC_FORMAT_MOD_YTR) 1197 afbc_format |= (1 << 4); 1198 1199 afbc_tile_num = ALIGN(actual_w, 16) >> 4; 1200 1201 /* 1202 * AFBC pic_vir_width is count by pixel, this is different 1203 * with WIN_VIR_STRIDE. 1204 */ 1205 stride = (fb->pitches[0] << 3) / bpp; 1206 if ((stride & 0x3f) && (xmirror || rotate_90 || rotate_270)) 1207 drm_err(vop2->drm, "vp%d %s stride[%d] not 64 pixel aligned\n", 1208 vp->id, win->data->name, stride); 1209 1210 rb_swap = vop2_afbc_rb_swap(fb->format->format); 1211 uv_swap = vop2_afbc_uv_swap(fb->format->format); 1212 /* 1213 * This is a workaround for crazy IC design, Cluster 1214 * and Esmart/Smart use different format configuration map: 1215 * YUV420_10BIT: 0x10 for Cluster, 0x14 for Esmart/Smart. 1216 * 1217 * This is one thing we can make the convert simple: 1218 * AFBCD decode all the YUV data to YUV444. So we just 1219 * set all the yuv 10 bit to YUV444_10. 1220 */ 1221 if (fb->format->is_yuv && bpp == 10) 1222 format = VOP2_CLUSTER_YUV444_10; 1223 1224 if (vop2_cluster_window(win)) 1225 vop2_win_write(win, VOP2_WIN_AFBC_ENABLE, 1); 1226 vop2_win_write(win, VOP2_WIN_AFBC_FORMAT, afbc_format); 1227 vop2_win_write(win, VOP2_WIN_AFBC_RB_SWAP, rb_swap); 1228 vop2_win_write(win, VOP2_WIN_AFBC_UV_SWAP, uv_swap); 1229 vop2_win_write(win, VOP2_WIN_AFBC_AUTO_GATING_EN, 0); 1230 vop2_win_write(win, VOP2_WIN_AFBC_BLOCK_SPLIT_EN, 0); 1231 if (pstate->rotation & (DRM_MODE_ROTATE_270 | DRM_MODE_ROTATE_90)) { 1232 vop2_win_write(win, VOP2_WIN_AFBC_HALF_BLOCK_EN, 0); 1233 transform_offset = vop2_afbc_transform_offset(pstate, false); 1234 } else { 1235 vop2_win_write(win, VOP2_WIN_AFBC_HALF_BLOCK_EN, 1); 1236 transform_offset = vop2_afbc_transform_offset(pstate, true); 1237 } 1238 vop2_win_write(win, VOP2_WIN_AFBC_HDR_PTR, yrgb_mst); 1239 vop2_win_write(win, VOP2_WIN_AFBC_PIC_SIZE, act_info); 1240 vop2_win_write(win, VOP2_WIN_AFBC_TRANSFORM_OFFSET, transform_offset); 1241 vop2_win_write(win, VOP2_WIN_AFBC_PIC_OFFSET, ((src->x1 >> 16) | src->y1)); 1242 vop2_win_write(win, VOP2_WIN_AFBC_DSP_OFFSET, (dest->x1 | (dest->y1 << 16))); 1243 vop2_win_write(win, VOP2_WIN_AFBC_PIC_VIR_WIDTH, stride); 1244 vop2_win_write(win, VOP2_WIN_AFBC_TILE_NUM, afbc_tile_num); 1245 vop2_win_write(win, VOP2_WIN_XMIRROR, xmirror); 1246 vop2_win_write(win, VOP2_WIN_AFBC_ROTATE_270, rotate_270); 1247 vop2_win_write(win, VOP2_WIN_AFBC_ROTATE_90, rotate_90); 1248 } else { 1249 vop2_win_write(win, VOP2_WIN_YRGB_VIR, DIV_ROUND_UP(fb->pitches[0], 4)); 1250 } 1251 1252 vop2_win_write(win, VOP2_WIN_YMIRROR, ymirror); 1253 1254 if (rotate_90 || rotate_270) { 1255 act_info = swahw32(act_info); 1256 actual_w = drm_rect_height(src) >> 16; 1257 actual_h = drm_rect_width(src) >> 16; 1258 } 1259 1260 vop2_win_write(win, VOP2_WIN_FORMAT, format); 1261 vop2_win_write(win, VOP2_WIN_YRGB_MST, yrgb_mst); 1262 1263 rb_swap = vop2_win_rb_swap(fb->format->format); 1264 vop2_win_write(win, VOP2_WIN_RB_SWAP, rb_swap); 1265 if (!vop2_cluster_window(win)) { 1266 uv_swap = vop2_win_uv_swap(fb->format->format); 1267 vop2_win_write(win, VOP2_WIN_UV_SWAP, uv_swap); 1268 } 1269 1270 if (fb->format->is_yuv) { 1271 vop2_win_write(win, VOP2_WIN_UV_VIR, DIV_ROUND_UP(fb->pitches[1], 4)); 1272 vop2_win_write(win, VOP2_WIN_UV_MST, uv_mst); 1273 } 1274 1275 vop2_setup_scale(vop2, win, actual_w, actual_h, dsp_w, dsp_h, fb->format->format); 1276 if (!vop2_cluster_window(win)) 1277 vop2_plane_setup_color_key(plane, 0); 1278 vop2_win_write(win, VOP2_WIN_ACT_INFO, act_info); 1279 vop2_win_write(win, VOP2_WIN_DSP_INFO, dsp_info); 1280 vop2_win_write(win, VOP2_WIN_DSP_ST, dest->y1 << 16 | (dest->x1 & 0xffff)); 1281 1282 vop2_setup_csc_mode(vp, win, pstate); 1283 1284 dither_up = vop2_win_dither_up(fb->format->format); 1285 vop2_win_write(win, VOP2_WIN_DITHER_UP, dither_up); 1286 1287 vop2_win_write(win, VOP2_WIN_ENABLE, 1); 1288 1289 if (vop2_cluster_window(win)) { 1290 int lb_mode = vop2_get_cluster_lb_mode(win, pstate); 1291 1292 vop2_win_write(win, VOP2_WIN_CLUSTER_LB_MODE, lb_mode); 1293 vop2_win_write(win, VOP2_WIN_CLUSTER_ENABLE, 1); 1294 } 1295 } 1296 1297 static const struct drm_plane_helper_funcs vop2_plane_helper_funcs = { 1298 .atomic_check = vop2_plane_atomic_check, 1299 .atomic_update = vop2_plane_atomic_update, 1300 .atomic_disable = vop2_plane_atomic_disable, 1301 }; 1302 1303 static const struct drm_plane_funcs vop2_plane_funcs = { 1304 .update_plane = drm_atomic_helper_update_plane, 1305 .disable_plane = drm_atomic_helper_disable_plane, 1306 .destroy = drm_plane_cleanup, 1307 .reset = drm_atomic_helper_plane_reset, 1308 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 1309 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 1310 .format_mod_supported = rockchip_vop2_mod_supported, 1311 }; 1312 1313 static int vop2_crtc_enable_vblank(struct drm_crtc *crtc) 1314 { 1315 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1316 1317 vop2_crtc_enable_irq(vp, VP_INT_FS_FIELD); 1318 1319 return 0; 1320 } 1321 1322 static void vop2_crtc_disable_vblank(struct drm_crtc *crtc) 1323 { 1324 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1325 1326 vop2_crtc_disable_irq(vp, VP_INT_FS_FIELD); 1327 } 1328 1329 static bool vop2_crtc_mode_fixup(struct drm_crtc *crtc, 1330 const struct drm_display_mode *mode, 1331 struct drm_display_mode *adj_mode) 1332 { 1333 drm_mode_set_crtcinfo(adj_mode, CRTC_INTERLACE_HALVE_V | 1334 CRTC_STEREO_DOUBLE); 1335 1336 return true; 1337 } 1338 1339 static void vop2_dither_setup(struct drm_crtc *crtc, u32 *dsp_ctrl) 1340 { 1341 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state); 1342 1343 switch (vcstate->bus_format) { 1344 case MEDIA_BUS_FMT_RGB565_1X16: 1345 *dsp_ctrl |= RK3568_VP_DSP_CTRL__DITHER_DOWN_EN; 1346 break; 1347 case MEDIA_BUS_FMT_RGB666_1X18: 1348 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI: 1349 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG: 1350 *dsp_ctrl |= RK3568_VP_DSP_CTRL__DITHER_DOWN_EN; 1351 *dsp_ctrl |= RGB888_TO_RGB666; 1352 break; 1353 case MEDIA_BUS_FMT_YUV8_1X24: 1354 case MEDIA_BUS_FMT_UYYVYY8_0_5X24: 1355 *dsp_ctrl |= RK3568_VP_DSP_CTRL__PRE_DITHER_DOWN_EN; 1356 break; 1357 default: 1358 break; 1359 } 1360 1361 if (vcstate->output_mode != ROCKCHIP_OUT_MODE_AAAA) 1362 *dsp_ctrl |= RK3568_VP_DSP_CTRL__PRE_DITHER_DOWN_EN; 1363 1364 *dsp_ctrl |= FIELD_PREP(RK3568_VP_DSP_CTRL__DITHER_DOWN_SEL, 1365 DITHER_DOWN_ALLEGRO); 1366 } 1367 1368 static void vop2_post_config(struct drm_crtc *crtc) 1369 { 1370 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1371 struct drm_display_mode *mode = &crtc->state->adjusted_mode; 1372 u16 vtotal = mode->crtc_vtotal; 1373 u16 hdisplay = mode->crtc_hdisplay; 1374 u16 hact_st = mode->crtc_htotal - mode->crtc_hsync_start; 1375 u16 vdisplay = mode->crtc_vdisplay; 1376 u16 vact_st = mode->crtc_vtotal - mode->crtc_vsync_start; 1377 u32 left_margin = 100, right_margin = 100; 1378 u32 top_margin = 100, bottom_margin = 100; 1379 u16 hsize = hdisplay * (left_margin + right_margin) / 200; 1380 u16 vsize = vdisplay * (top_margin + bottom_margin) / 200; 1381 u16 hact_end, vact_end; 1382 u32 val; 1383 1384 vsize = rounddown(vsize, 2); 1385 hsize = rounddown(hsize, 2); 1386 hact_st += hdisplay * (100 - left_margin) / 200; 1387 hact_end = hact_st + hsize; 1388 val = hact_st << 16; 1389 val |= hact_end; 1390 vop2_vp_write(vp, RK3568_VP_POST_DSP_HACT_INFO, val); 1391 vact_st += vdisplay * (100 - top_margin) / 200; 1392 vact_end = vact_st + vsize; 1393 val = vact_st << 16; 1394 val |= vact_end; 1395 vop2_vp_write(vp, RK3568_VP_POST_DSP_VACT_INFO, val); 1396 val = scl_cal_scale2(vdisplay, vsize) << 16; 1397 val |= scl_cal_scale2(hdisplay, hsize); 1398 vop2_vp_write(vp, RK3568_VP_POST_SCL_FACTOR_YRGB, val); 1399 1400 val = 0; 1401 if (hdisplay != hsize) 1402 val |= RK3568_VP_POST_SCL_CTRL__HSCALEDOWN; 1403 if (vdisplay != vsize) 1404 val |= RK3568_VP_POST_SCL_CTRL__VSCALEDOWN; 1405 vop2_vp_write(vp, RK3568_VP_POST_SCL_CTRL, val); 1406 1407 if (mode->flags & DRM_MODE_FLAG_INTERLACE) { 1408 u16 vact_st_f1 = vtotal + vact_st + 1; 1409 u16 vact_end_f1 = vact_st_f1 + vsize; 1410 1411 val = vact_st_f1 << 16 | vact_end_f1; 1412 vop2_vp_write(vp, RK3568_VP_POST_DSP_VACT_INFO_F1, val); 1413 } 1414 1415 vop2_vp_write(vp, RK3568_VP_DSP_BG, 0); 1416 } 1417 1418 static void rk3568_set_intf_mux(struct vop2_video_port *vp, int id, 1419 u32 polflags) 1420 { 1421 struct vop2 *vop2 = vp->vop2; 1422 u32 die, dip; 1423 1424 die = vop2_readl(vop2, RK3568_DSP_IF_EN); 1425 dip = vop2_readl(vop2, RK3568_DSP_IF_POL); 1426 1427 switch (id) { 1428 case ROCKCHIP_VOP2_EP_RGB0: 1429 die &= ~RK3568_SYS_DSP_INFACE_EN_RGB_MUX; 1430 die |= RK3568_SYS_DSP_INFACE_EN_RGB | 1431 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_RGB_MUX, vp->id); 1432 if (polflags & POLFLAG_DCLK_INV) 1433 regmap_write(vop2->grf, RK3568_GRF_VO_CON1, BIT(3 + 16) | BIT(3)); 1434 else 1435 regmap_write(vop2->grf, RK3568_GRF_VO_CON1, BIT(3 + 16)); 1436 break; 1437 case ROCKCHIP_VOP2_EP_HDMI0: 1438 die &= ~RK3568_SYS_DSP_INFACE_EN_HDMI_MUX; 1439 die |= RK3568_SYS_DSP_INFACE_EN_HDMI | 1440 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_HDMI_MUX, vp->id); 1441 break; 1442 case ROCKCHIP_VOP2_EP_EDP0: 1443 die &= ~RK3568_SYS_DSP_INFACE_EN_EDP_MUX; 1444 die |= RK3568_SYS_DSP_INFACE_EN_EDP | 1445 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_EDP_MUX, vp->id); 1446 break; 1447 case ROCKCHIP_VOP2_EP_MIPI0: 1448 die &= ~RK3568_SYS_DSP_INFACE_EN_MIPI0_MUX; 1449 die |= RK3568_SYS_DSP_INFACE_EN_MIPI0 | 1450 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_MIPI0_MUX, vp->id); 1451 dip &= ~RK3568_DSP_IF_POL__MIPI_PIN_POL; 1452 dip |= FIELD_PREP(RK3568_DSP_IF_POL__MIPI_PIN_POL, polflags); 1453 break; 1454 case ROCKCHIP_VOP2_EP_MIPI1: 1455 die &= ~RK3568_SYS_DSP_INFACE_EN_MIPI1_MUX; 1456 die |= RK3568_SYS_DSP_INFACE_EN_MIPI1 | 1457 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_MIPI1_MUX, vp->id); 1458 dip &= ~RK3568_DSP_IF_POL__MIPI_PIN_POL; 1459 dip |= FIELD_PREP(RK3568_DSP_IF_POL__MIPI_PIN_POL, polflags); 1460 break; 1461 case ROCKCHIP_VOP2_EP_LVDS0: 1462 die &= ~RK3568_SYS_DSP_INFACE_EN_LVDS0_MUX; 1463 die |= RK3568_SYS_DSP_INFACE_EN_LVDS0 | 1464 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_LVDS0_MUX, vp->id); 1465 dip &= ~RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL; 1466 dip |= FIELD_PREP(RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL, polflags); 1467 break; 1468 case ROCKCHIP_VOP2_EP_LVDS1: 1469 die &= ~RK3568_SYS_DSP_INFACE_EN_LVDS1_MUX; 1470 die |= RK3568_SYS_DSP_INFACE_EN_LVDS1 | 1471 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_LVDS1_MUX, vp->id); 1472 dip &= ~RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL; 1473 dip |= FIELD_PREP(RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL, polflags); 1474 break; 1475 default: 1476 drm_err(vop2->drm, "Invalid interface id %d on vp%d\n", id, vp->id); 1477 return; 1478 } 1479 1480 dip |= RK3568_DSP_IF_POL__CFG_DONE_IMD; 1481 1482 vop2_writel(vop2, RK3568_DSP_IF_EN, die); 1483 vop2_writel(vop2, RK3568_DSP_IF_POL, dip); 1484 } 1485 1486 static int us_to_vertical_line(struct drm_display_mode *mode, int us) 1487 { 1488 return us * mode->clock / mode->htotal / 1000; 1489 } 1490 1491 static void vop2_crtc_atomic_enable(struct drm_crtc *crtc, 1492 struct drm_atomic_state *state) 1493 { 1494 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1495 struct vop2 *vop2 = vp->vop2; 1496 const struct vop2_data *vop2_data = vop2->data; 1497 const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id]; 1498 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 1499 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state); 1500 struct drm_display_mode *mode = &crtc->state->adjusted_mode; 1501 unsigned long clock = mode->crtc_clock * 1000; 1502 u16 hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start; 1503 u16 hdisplay = mode->crtc_hdisplay; 1504 u16 htotal = mode->crtc_htotal; 1505 u16 hact_st = mode->crtc_htotal - mode->crtc_hsync_start; 1506 u16 hact_end = hact_st + hdisplay; 1507 u16 vdisplay = mode->crtc_vdisplay; 1508 u16 vtotal = mode->crtc_vtotal; 1509 u16 vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start; 1510 u16 vact_st = mode->crtc_vtotal - mode->crtc_vsync_start; 1511 u16 vact_end = vact_st + vdisplay; 1512 u8 out_mode; 1513 u32 dsp_ctrl = 0; 1514 int act_end; 1515 u32 val, polflags; 1516 int ret; 1517 struct drm_encoder *encoder; 1518 1519 drm_dbg(vop2->drm, "Update mode to %dx%d%s%d, type: %d for vp%d\n", 1520 hdisplay, vdisplay, mode->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "p", 1521 drm_mode_vrefresh(mode), vcstate->output_type, vp->id); 1522 1523 vop2_lock(vop2); 1524 1525 ret = clk_prepare_enable(vp->dclk); 1526 if (ret < 0) { 1527 drm_err(vop2->drm, "failed to enable dclk for video port%d - %d\n", 1528 vp->id, ret); 1529 vop2_unlock(vop2); 1530 return; 1531 } 1532 1533 if (!vop2->enable_count) 1534 vop2_enable(vop2); 1535 1536 vop2->enable_count++; 1537 1538 vop2_crtc_enable_irq(vp, VP_INT_POST_BUF_EMPTY); 1539 1540 polflags = 0; 1541 if (vcstate->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE) 1542 polflags |= POLFLAG_DCLK_INV; 1543 if (mode->flags & DRM_MODE_FLAG_PHSYNC) 1544 polflags |= BIT(HSYNC_POSITIVE); 1545 if (mode->flags & DRM_MODE_FLAG_PVSYNC) 1546 polflags |= BIT(VSYNC_POSITIVE); 1547 1548 drm_for_each_encoder_mask(encoder, crtc->dev, crtc_state->encoder_mask) { 1549 struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder); 1550 1551 rk3568_set_intf_mux(vp, rkencoder->crtc_endpoint_id, polflags); 1552 } 1553 1554 if (vcstate->output_mode == ROCKCHIP_OUT_MODE_AAAA && 1555 !(vp_data->feature & VOP_FEATURE_OUTPUT_10BIT)) 1556 out_mode = ROCKCHIP_OUT_MODE_P888; 1557 else 1558 out_mode = vcstate->output_mode; 1559 1560 dsp_ctrl |= FIELD_PREP(RK3568_VP_DSP_CTRL__OUT_MODE, out_mode); 1561 1562 if (vop2_output_uv_swap(vcstate->bus_format, vcstate->output_mode)) 1563 dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_RB_SWAP; 1564 1565 if (is_yuv_output(vcstate->bus_format)) 1566 dsp_ctrl |= RK3568_VP_DSP_CTRL__POST_DSP_OUT_R2Y; 1567 1568 vop2_dither_setup(crtc, &dsp_ctrl); 1569 1570 vop2_vp_write(vp, RK3568_VP_DSP_HTOTAL_HS_END, (htotal << 16) | hsync_len); 1571 val = hact_st << 16; 1572 val |= hact_end; 1573 vop2_vp_write(vp, RK3568_VP_DSP_HACT_ST_END, val); 1574 1575 val = vact_st << 16; 1576 val |= vact_end; 1577 vop2_vp_write(vp, RK3568_VP_DSP_VACT_ST_END, val); 1578 1579 if (mode->flags & DRM_MODE_FLAG_INTERLACE) { 1580 u16 vact_st_f1 = vtotal + vact_st + 1; 1581 u16 vact_end_f1 = vact_st_f1 + vdisplay; 1582 1583 val = vact_st_f1 << 16 | vact_end_f1; 1584 vop2_vp_write(vp, RK3568_VP_DSP_VACT_ST_END_F1, val); 1585 1586 val = vtotal << 16 | (vtotal + vsync_len); 1587 vop2_vp_write(vp, RK3568_VP_DSP_VS_ST_END_F1, val); 1588 dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_INTERLACE; 1589 dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_FILED_POL; 1590 dsp_ctrl |= RK3568_VP_DSP_CTRL__P2I_EN; 1591 vtotal += vtotal + 1; 1592 act_end = vact_end_f1; 1593 } else { 1594 act_end = vact_end; 1595 } 1596 1597 vop2_writel(vop2, RK3568_VP_LINE_FLAG(vp->id), 1598 (act_end - us_to_vertical_line(mode, 0)) << 16 | act_end); 1599 1600 vop2_vp_write(vp, RK3568_VP_DSP_VTOTAL_VS_END, vtotal << 16 | vsync_len); 1601 1602 if (mode->flags & DRM_MODE_FLAG_DBLCLK) { 1603 dsp_ctrl |= RK3568_VP_DSP_CTRL__CORE_DCLK_DIV; 1604 clock *= 2; 1605 } 1606 1607 vop2_vp_write(vp, RK3568_VP_MIPI_CTRL, 0); 1608 1609 clk_set_rate(vp->dclk, clock); 1610 1611 vop2_post_config(crtc); 1612 1613 vop2_cfg_done(vp); 1614 1615 vop2_vp_write(vp, RK3568_VP_DSP_CTRL, dsp_ctrl); 1616 1617 drm_crtc_vblank_on(crtc); 1618 1619 vop2_unlock(vop2); 1620 } 1621 1622 static int vop2_crtc_atomic_check(struct drm_crtc *crtc, 1623 struct drm_atomic_state *state) 1624 { 1625 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1626 struct drm_plane *plane; 1627 int nplanes = 0; 1628 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 1629 1630 drm_atomic_crtc_state_for_each_plane(plane, crtc_state) 1631 nplanes++; 1632 1633 if (nplanes > vp->nlayers) 1634 return -EINVAL; 1635 1636 return 0; 1637 } 1638 1639 static bool is_opaque(u16 alpha) 1640 { 1641 return (alpha >> 8) == 0xff; 1642 } 1643 1644 static void vop2_parse_alpha(struct vop2_alpha_config *alpha_config, 1645 struct vop2_alpha *alpha) 1646 { 1647 int src_glb_alpha_en = is_opaque(alpha_config->src_glb_alpha_value) ? 0 : 1; 1648 int dst_glb_alpha_en = is_opaque(alpha_config->dst_glb_alpha_value) ? 0 : 1; 1649 int src_color_mode = alpha_config->src_premulti_en ? 1650 ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL; 1651 int dst_color_mode = alpha_config->dst_premulti_en ? 1652 ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL; 1653 1654 alpha->src_color_ctrl.val = 0; 1655 alpha->dst_color_ctrl.val = 0; 1656 alpha->src_alpha_ctrl.val = 0; 1657 alpha->dst_alpha_ctrl.val = 0; 1658 1659 if (!alpha_config->src_pixel_alpha_en) 1660 alpha->src_color_ctrl.bits.blend_mode = ALPHA_GLOBAL; 1661 else if (alpha_config->src_pixel_alpha_en && !src_glb_alpha_en) 1662 alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX; 1663 else 1664 alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL; 1665 1666 alpha->src_color_ctrl.bits.alpha_en = 1; 1667 1668 if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_GLOBAL) { 1669 alpha->src_color_ctrl.bits.color_mode = src_color_mode; 1670 alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL; 1671 } else if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_PER_PIX) { 1672 alpha->src_color_ctrl.bits.color_mode = src_color_mode; 1673 alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_ONE; 1674 } else { 1675 alpha->src_color_ctrl.bits.color_mode = ALPHA_SRC_PRE_MUL; 1676 alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL; 1677 } 1678 alpha->src_color_ctrl.bits.glb_alpha = alpha_config->src_glb_alpha_value >> 8; 1679 alpha->src_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1680 alpha->src_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION; 1681 1682 alpha->dst_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1683 alpha->dst_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION; 1684 alpha->dst_color_ctrl.bits.blend_mode = ALPHA_GLOBAL; 1685 alpha->dst_color_ctrl.bits.glb_alpha = alpha_config->dst_glb_alpha_value >> 8; 1686 alpha->dst_color_ctrl.bits.color_mode = dst_color_mode; 1687 alpha->dst_color_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE; 1688 1689 alpha->src_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1690 alpha->src_alpha_ctrl.bits.blend_mode = alpha->src_color_ctrl.bits.blend_mode; 1691 alpha->src_alpha_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION; 1692 alpha->src_alpha_ctrl.bits.factor_mode = ALPHA_ONE; 1693 1694 alpha->dst_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1695 if (alpha_config->dst_pixel_alpha_en && !dst_glb_alpha_en) 1696 alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX; 1697 else 1698 alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL; 1699 alpha->dst_alpha_ctrl.bits.alpha_cal_mode = ALPHA_NO_SATURATION; 1700 alpha->dst_alpha_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE; 1701 } 1702 1703 static int vop2_find_start_mixer_id_for_vp(struct vop2 *vop2, u8 port_id) 1704 { 1705 struct vop2_video_port *vp; 1706 int used_layer = 0; 1707 int i; 1708 1709 for (i = 0; i < port_id; i++) { 1710 vp = &vop2->vps[i]; 1711 used_layer += hweight32(vp->win_mask); 1712 } 1713 1714 return used_layer; 1715 } 1716 1717 static void vop2_setup_cluster_alpha(struct vop2 *vop2, struct vop2_win *main_win) 1718 { 1719 u32 offset = (main_win->data->phys_id * 0x10); 1720 struct vop2_alpha_config alpha_config; 1721 struct vop2_alpha alpha; 1722 struct drm_plane_state *bottom_win_pstate; 1723 bool src_pixel_alpha_en = false; 1724 u16 src_glb_alpha_val, dst_glb_alpha_val; 1725 bool premulti_en = false; 1726 bool swap = false; 1727 1728 /* At one win mode, win0 is dst/bottom win, and win1 is a all zero src/top win */ 1729 bottom_win_pstate = main_win->base.state; 1730 src_glb_alpha_val = 0; 1731 dst_glb_alpha_val = main_win->base.state->alpha; 1732 1733 if (!bottom_win_pstate->fb) 1734 return; 1735 1736 alpha_config.src_premulti_en = premulti_en; 1737 alpha_config.dst_premulti_en = false; 1738 alpha_config.src_pixel_alpha_en = src_pixel_alpha_en; 1739 alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */ 1740 alpha_config.src_glb_alpha_value = src_glb_alpha_val; 1741 alpha_config.dst_glb_alpha_value = dst_glb_alpha_val; 1742 vop2_parse_alpha(&alpha_config, &alpha); 1743 1744 alpha.src_color_ctrl.bits.src_dst_swap = swap; 1745 vop2_writel(vop2, RK3568_CLUSTER0_MIX_SRC_COLOR_CTRL + offset, 1746 alpha.src_color_ctrl.val); 1747 vop2_writel(vop2, RK3568_CLUSTER0_MIX_DST_COLOR_CTRL + offset, 1748 alpha.dst_color_ctrl.val); 1749 vop2_writel(vop2, RK3568_CLUSTER0_MIX_SRC_ALPHA_CTRL + offset, 1750 alpha.src_alpha_ctrl.val); 1751 vop2_writel(vop2, RK3568_CLUSTER0_MIX_DST_ALPHA_CTRL + offset, 1752 alpha.dst_alpha_ctrl.val); 1753 } 1754 1755 static void vop2_setup_alpha(struct vop2_video_port *vp) 1756 { 1757 struct vop2 *vop2 = vp->vop2; 1758 struct drm_framebuffer *fb; 1759 struct vop2_alpha_config alpha_config; 1760 struct vop2_alpha alpha; 1761 struct drm_plane *plane; 1762 int pixel_alpha_en; 1763 int premulti_en, gpremulti_en = 0; 1764 int mixer_id; 1765 u32 offset; 1766 bool bottom_layer_alpha_en = false; 1767 u32 dst_global_alpha = DRM_BLEND_ALPHA_OPAQUE; 1768 1769 mixer_id = vop2_find_start_mixer_id_for_vp(vop2, vp->id); 1770 alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */ 1771 1772 drm_atomic_crtc_for_each_plane(plane, &vp->crtc) { 1773 struct vop2_win *win = to_vop2_win(plane); 1774 1775 if (plane->state->normalized_zpos == 0 && 1776 !is_opaque(plane->state->alpha) && 1777 !vop2_cluster_window(win)) { 1778 /* 1779 * If bottom layer have global alpha effect [except cluster layer, 1780 * because cluster have deal with bottom layer global alpha value 1781 * at cluster mix], bottom layer mix need deal with global alpha. 1782 */ 1783 bottom_layer_alpha_en = true; 1784 dst_global_alpha = plane->state->alpha; 1785 } 1786 } 1787 1788 drm_atomic_crtc_for_each_plane(plane, &vp->crtc) { 1789 struct vop2_win *win = to_vop2_win(plane); 1790 int zpos = plane->state->normalized_zpos; 1791 1792 if (plane->state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) 1793 premulti_en = 1; 1794 else 1795 premulti_en = 0; 1796 1797 plane = &win->base; 1798 fb = plane->state->fb; 1799 1800 pixel_alpha_en = fb->format->has_alpha; 1801 1802 alpha_config.src_premulti_en = premulti_en; 1803 1804 if (bottom_layer_alpha_en && zpos == 1) { 1805 gpremulti_en = premulti_en; 1806 /* Cd = Cs + (1 - As) * Cd * Agd */ 1807 alpha_config.dst_premulti_en = false; 1808 alpha_config.src_pixel_alpha_en = pixel_alpha_en; 1809 alpha_config.src_glb_alpha_value = plane->state->alpha; 1810 alpha_config.dst_glb_alpha_value = dst_global_alpha; 1811 } else if (vop2_cluster_window(win)) { 1812 /* Mix output data only have pixel alpha */ 1813 alpha_config.dst_premulti_en = true; 1814 alpha_config.src_pixel_alpha_en = true; 1815 alpha_config.src_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1816 alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1817 } else { 1818 /* Cd = Cs + (1 - As) * Cd */ 1819 alpha_config.dst_premulti_en = true; 1820 alpha_config.src_pixel_alpha_en = pixel_alpha_en; 1821 alpha_config.src_glb_alpha_value = plane->state->alpha; 1822 alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1823 } 1824 1825 vop2_parse_alpha(&alpha_config, &alpha); 1826 1827 offset = (mixer_id + zpos - 1) * 0x10; 1828 vop2_writel(vop2, RK3568_MIX0_SRC_COLOR_CTRL + offset, 1829 alpha.src_color_ctrl.val); 1830 vop2_writel(vop2, RK3568_MIX0_DST_COLOR_CTRL + offset, 1831 alpha.dst_color_ctrl.val); 1832 vop2_writel(vop2, RK3568_MIX0_SRC_ALPHA_CTRL + offset, 1833 alpha.src_alpha_ctrl.val); 1834 vop2_writel(vop2, RK3568_MIX0_DST_ALPHA_CTRL + offset, 1835 alpha.dst_alpha_ctrl.val); 1836 } 1837 1838 if (vp->id == 0) { 1839 if (bottom_layer_alpha_en) { 1840 /* Transfer pixel alpha to hdr mix */ 1841 alpha_config.src_premulti_en = gpremulti_en; 1842 alpha_config.dst_premulti_en = true; 1843 alpha_config.src_pixel_alpha_en = true; 1844 alpha_config.src_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1845 alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1846 vop2_parse_alpha(&alpha_config, &alpha); 1847 1848 vop2_writel(vop2, RK3568_HDR0_SRC_COLOR_CTRL, 1849 alpha.src_color_ctrl.val); 1850 vop2_writel(vop2, RK3568_HDR0_DST_COLOR_CTRL, 1851 alpha.dst_color_ctrl.val); 1852 vop2_writel(vop2, RK3568_HDR0_SRC_ALPHA_CTRL, 1853 alpha.src_alpha_ctrl.val); 1854 vop2_writel(vop2, RK3568_HDR0_DST_ALPHA_CTRL, 1855 alpha.dst_alpha_ctrl.val); 1856 } else { 1857 vop2_writel(vop2, RK3568_HDR0_SRC_COLOR_CTRL, 0); 1858 } 1859 } 1860 } 1861 1862 static void vop2_setup_layer_mixer(struct vop2_video_port *vp) 1863 { 1864 struct vop2 *vop2 = vp->vop2; 1865 struct drm_plane *plane; 1866 u32 layer_sel = 0; 1867 u32 port_sel; 1868 unsigned int nlayer, ofs; 1869 struct drm_display_mode *adjusted_mode; 1870 u16 hsync_len; 1871 u16 hdisplay; 1872 u32 bg_dly; 1873 u32 pre_scan_dly; 1874 int i; 1875 struct vop2_video_port *vp0 = &vop2->vps[0]; 1876 struct vop2_video_port *vp1 = &vop2->vps[1]; 1877 struct vop2_video_port *vp2 = &vop2->vps[2]; 1878 1879 adjusted_mode = &vp->crtc.state->adjusted_mode; 1880 hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start; 1881 hdisplay = adjusted_mode->crtc_hdisplay; 1882 1883 bg_dly = vp->data->pre_scan_max_dly[3]; 1884 vop2_writel(vop2, RK3568_VP_BG_MIX_CTRL(vp->id), 1885 FIELD_PREP(RK3568_VP_BG_MIX_CTRL__BG_DLY, bg_dly)); 1886 1887 pre_scan_dly = ((bg_dly + (hdisplay >> 1) - 1) << 16) | hsync_len; 1888 vop2_vp_write(vp, RK3568_VP_PRE_SCAN_HTIMING, pre_scan_dly); 1889 1890 vop2_writel(vop2, RK3568_OVL_CTRL, 0); 1891 port_sel = vop2_readl(vop2, RK3568_OVL_PORT_SEL); 1892 port_sel &= RK3568_OVL_PORT_SEL__SEL_PORT; 1893 1894 if (vp0->nlayers) 1895 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT0_MUX, 1896 vp0->nlayers - 1); 1897 else 1898 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT0_MUX, 8); 1899 1900 if (vp1->nlayers) 1901 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 1902 (vp0->nlayers + vp1->nlayers - 1)); 1903 else 1904 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 8); 1905 1906 if (vp2->nlayers) 1907 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT2_MUX, 1908 (vp2->nlayers + vp1->nlayers + vp0->nlayers - 1)); 1909 else 1910 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 8); 1911 1912 layer_sel = vop2_readl(vop2, RK3568_OVL_LAYER_SEL); 1913 1914 ofs = 0; 1915 for (i = 0; i < vp->id; i++) 1916 ofs += vop2->vps[i].nlayers; 1917 1918 nlayer = 0; 1919 drm_atomic_crtc_for_each_plane(plane, &vp->crtc) { 1920 struct vop2_win *win = to_vop2_win(plane); 1921 1922 switch (win->data->phys_id) { 1923 case ROCKCHIP_VOP2_CLUSTER0: 1924 port_sel &= ~RK3568_OVL_PORT_SEL__CLUSTER0; 1925 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__CLUSTER0, vp->id); 1926 break; 1927 case ROCKCHIP_VOP2_CLUSTER1: 1928 port_sel &= ~RK3568_OVL_PORT_SEL__CLUSTER1; 1929 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__CLUSTER1, vp->id); 1930 break; 1931 case ROCKCHIP_VOP2_ESMART0: 1932 port_sel &= ~RK3568_OVL_PORT_SEL__ESMART0; 1933 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__ESMART0, vp->id); 1934 break; 1935 case ROCKCHIP_VOP2_ESMART1: 1936 port_sel &= ~RK3568_OVL_PORT_SEL__ESMART1; 1937 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__ESMART1, vp->id); 1938 break; 1939 case ROCKCHIP_VOP2_SMART0: 1940 port_sel &= ~RK3568_OVL_PORT_SEL__SMART0; 1941 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__SMART0, vp->id); 1942 break; 1943 case ROCKCHIP_VOP2_SMART1: 1944 port_sel &= ~RK3568_OVL_PORT_SEL__SMART1; 1945 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__SMART1, vp->id); 1946 break; 1947 } 1948 1949 layer_sel &= ~RK3568_OVL_LAYER_SEL__LAYER(plane->state->normalized_zpos + ofs, 1950 0x7); 1951 layer_sel |= RK3568_OVL_LAYER_SEL__LAYER(plane->state->normalized_zpos + ofs, 1952 win->data->layer_sel_id); 1953 nlayer++; 1954 } 1955 1956 /* configure unused layers to 0x5 (reserved) */ 1957 for (; nlayer < vp->nlayers; nlayer++) { 1958 layer_sel &= ~RK3568_OVL_LAYER_SEL__LAYER(nlayer + ofs, 0x7); 1959 layer_sel |= RK3568_OVL_LAYER_SEL__LAYER(nlayer + ofs, 5); 1960 } 1961 1962 vop2_writel(vop2, RK3568_OVL_LAYER_SEL, layer_sel); 1963 vop2_writel(vop2, RK3568_OVL_PORT_SEL, port_sel); 1964 vop2_writel(vop2, RK3568_OVL_CTRL, RK3568_OVL_CTRL__LAYERSEL_REGDONE_IMD); 1965 } 1966 1967 static void vop2_setup_dly_for_windows(struct vop2 *vop2) 1968 { 1969 struct vop2_win *win; 1970 int i = 0; 1971 u32 cdly = 0, sdly = 0; 1972 1973 for (i = 0; i < vop2->data->win_size; i++) { 1974 u32 dly; 1975 1976 win = &vop2->win[i]; 1977 dly = win->delay; 1978 1979 switch (win->data->phys_id) { 1980 case ROCKCHIP_VOP2_CLUSTER0: 1981 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER0_0, dly); 1982 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER0_1, dly); 1983 break; 1984 case ROCKCHIP_VOP2_CLUSTER1: 1985 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER1_0, dly); 1986 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER1_1, dly); 1987 break; 1988 case ROCKCHIP_VOP2_ESMART0: 1989 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__ESMART0, dly); 1990 break; 1991 case ROCKCHIP_VOP2_ESMART1: 1992 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__ESMART1, dly); 1993 break; 1994 case ROCKCHIP_VOP2_SMART0: 1995 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__SMART0, dly); 1996 break; 1997 case ROCKCHIP_VOP2_SMART1: 1998 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__SMART1, dly); 1999 break; 2000 } 2001 } 2002 2003 vop2_writel(vop2, RK3568_CLUSTER_DLY_NUM, cdly); 2004 vop2_writel(vop2, RK3568_SMART_DLY_NUM, sdly); 2005 } 2006 2007 static void vop2_crtc_atomic_begin(struct drm_crtc *crtc, 2008 struct drm_atomic_state *state) 2009 { 2010 struct vop2_video_port *vp = to_vop2_video_port(crtc); 2011 struct vop2 *vop2 = vp->vop2; 2012 struct drm_plane *plane; 2013 2014 vp->win_mask = 0; 2015 2016 drm_atomic_crtc_for_each_plane(plane, crtc) { 2017 struct vop2_win *win = to_vop2_win(plane); 2018 2019 win->delay = win->data->dly[VOP2_DLY_MODE_DEFAULT]; 2020 2021 vp->win_mask |= BIT(win->data->phys_id); 2022 2023 if (vop2_cluster_window(win)) 2024 vop2_setup_cluster_alpha(vop2, win); 2025 } 2026 2027 if (!vp->win_mask) 2028 return; 2029 2030 vop2_setup_layer_mixer(vp); 2031 vop2_setup_alpha(vp); 2032 vop2_setup_dly_for_windows(vop2); 2033 } 2034 2035 static void vop2_crtc_atomic_flush(struct drm_crtc *crtc, 2036 struct drm_atomic_state *state) 2037 { 2038 struct vop2_video_port *vp = to_vop2_video_port(crtc); 2039 2040 vop2_post_config(crtc); 2041 2042 vop2_cfg_done(vp); 2043 2044 spin_lock_irq(&crtc->dev->event_lock); 2045 2046 if (crtc->state->event) { 2047 WARN_ON(drm_crtc_vblank_get(crtc)); 2048 vp->event = crtc->state->event; 2049 crtc->state->event = NULL; 2050 } 2051 2052 spin_unlock_irq(&crtc->dev->event_lock); 2053 } 2054 2055 static const struct drm_crtc_helper_funcs vop2_crtc_helper_funcs = { 2056 .mode_fixup = vop2_crtc_mode_fixup, 2057 .atomic_check = vop2_crtc_atomic_check, 2058 .atomic_begin = vop2_crtc_atomic_begin, 2059 .atomic_flush = vop2_crtc_atomic_flush, 2060 .atomic_enable = vop2_crtc_atomic_enable, 2061 .atomic_disable = vop2_crtc_atomic_disable, 2062 }; 2063 2064 static void vop2_crtc_reset(struct drm_crtc *crtc) 2065 { 2066 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state); 2067 2068 if (crtc->state) { 2069 __drm_atomic_helper_crtc_destroy_state(crtc->state); 2070 kfree(vcstate); 2071 } 2072 2073 vcstate = kzalloc(sizeof(*vcstate), GFP_KERNEL); 2074 if (!vcstate) 2075 return; 2076 2077 crtc->state = &vcstate->base; 2078 crtc->state->crtc = crtc; 2079 } 2080 2081 static struct drm_crtc_state *vop2_crtc_duplicate_state(struct drm_crtc *crtc) 2082 { 2083 struct rockchip_crtc_state *vcstate, *old_vcstate; 2084 2085 old_vcstate = to_rockchip_crtc_state(crtc->state); 2086 2087 vcstate = kmemdup(old_vcstate, sizeof(*old_vcstate), GFP_KERNEL); 2088 if (!vcstate) 2089 return NULL; 2090 2091 __drm_atomic_helper_crtc_duplicate_state(crtc, &vcstate->base); 2092 2093 return &vcstate->base; 2094 } 2095 2096 static void vop2_crtc_destroy_state(struct drm_crtc *crtc, 2097 struct drm_crtc_state *state) 2098 { 2099 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(state); 2100 2101 __drm_atomic_helper_crtc_destroy_state(&vcstate->base); 2102 kfree(vcstate); 2103 } 2104 2105 static const struct drm_crtc_funcs vop2_crtc_funcs = { 2106 .set_config = drm_atomic_helper_set_config, 2107 .page_flip = drm_atomic_helper_page_flip, 2108 .destroy = drm_crtc_cleanup, 2109 .reset = vop2_crtc_reset, 2110 .atomic_duplicate_state = vop2_crtc_duplicate_state, 2111 .atomic_destroy_state = vop2_crtc_destroy_state, 2112 .enable_vblank = vop2_crtc_enable_vblank, 2113 .disable_vblank = vop2_crtc_disable_vblank, 2114 }; 2115 2116 static irqreturn_t vop2_isr(int irq, void *data) 2117 { 2118 struct vop2 *vop2 = data; 2119 const struct vop2_data *vop2_data = vop2->data; 2120 u32 axi_irqs[VOP2_SYS_AXI_BUS_NUM]; 2121 int ret = IRQ_NONE; 2122 int i; 2123 2124 /* 2125 * The irq is shared with the iommu. If the runtime-pm state of the 2126 * vop2-device is disabled the irq has to be targeted at the iommu. 2127 */ 2128 if (!pm_runtime_get_if_in_use(vop2->dev)) 2129 return IRQ_NONE; 2130 2131 for (i = 0; i < vop2_data->nr_vps; i++) { 2132 struct vop2_video_port *vp = &vop2->vps[i]; 2133 struct drm_crtc *crtc = &vp->crtc; 2134 u32 irqs; 2135 2136 irqs = vop2_readl(vop2, RK3568_VP_INT_STATUS(vp->id)); 2137 vop2_writel(vop2, RK3568_VP_INT_CLR(vp->id), irqs << 16 | irqs); 2138 2139 if (irqs & VP_INT_DSP_HOLD_VALID) { 2140 complete(&vp->dsp_hold_completion); 2141 ret = IRQ_HANDLED; 2142 } 2143 2144 if (irqs & VP_INT_FS_FIELD) { 2145 drm_crtc_handle_vblank(crtc); 2146 spin_lock(&crtc->dev->event_lock); 2147 if (vp->event) { 2148 u32 val = vop2_readl(vop2, RK3568_REG_CFG_DONE); 2149 2150 if (!(val & BIT(vp->id))) { 2151 drm_crtc_send_vblank_event(crtc, vp->event); 2152 vp->event = NULL; 2153 drm_crtc_vblank_put(crtc); 2154 } 2155 } 2156 spin_unlock(&crtc->dev->event_lock); 2157 2158 ret = IRQ_HANDLED; 2159 } 2160 2161 if (irqs & VP_INT_POST_BUF_EMPTY) { 2162 drm_err_ratelimited(vop2->drm, 2163 "POST_BUF_EMPTY irq err at vp%d\n", 2164 vp->id); 2165 ret = IRQ_HANDLED; 2166 } 2167 } 2168 2169 axi_irqs[0] = vop2_readl(vop2, RK3568_SYS0_INT_STATUS); 2170 vop2_writel(vop2, RK3568_SYS0_INT_CLR, axi_irqs[0] << 16 | axi_irqs[0]); 2171 axi_irqs[1] = vop2_readl(vop2, RK3568_SYS1_INT_STATUS); 2172 vop2_writel(vop2, RK3568_SYS1_INT_CLR, axi_irqs[1] << 16 | axi_irqs[1]); 2173 2174 for (i = 0; i < ARRAY_SIZE(axi_irqs); i++) { 2175 if (axi_irqs[i] & VOP2_INT_BUS_ERRPR) { 2176 drm_err_ratelimited(vop2->drm, "BUS_ERROR irq err\n"); 2177 ret = IRQ_HANDLED; 2178 } 2179 } 2180 2181 pm_runtime_put(vop2->dev); 2182 2183 return ret; 2184 } 2185 2186 static int vop2_plane_init(struct vop2 *vop2, struct vop2_win *win, 2187 unsigned long possible_crtcs) 2188 { 2189 const struct vop2_win_data *win_data = win->data; 2190 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) | 2191 BIT(DRM_MODE_BLEND_PREMULTI) | 2192 BIT(DRM_MODE_BLEND_COVERAGE); 2193 int ret; 2194 2195 ret = drm_universal_plane_init(vop2->drm, &win->base, possible_crtcs, 2196 &vop2_plane_funcs, win_data->formats, 2197 win_data->nformats, 2198 win_data->format_modifiers, 2199 win->type, win_data->name); 2200 if (ret) { 2201 drm_err(vop2->drm, "failed to initialize plane %d\n", ret); 2202 return ret; 2203 } 2204 2205 drm_plane_helper_add(&win->base, &vop2_plane_helper_funcs); 2206 2207 if (win->data->supported_rotations) 2208 drm_plane_create_rotation_property(&win->base, DRM_MODE_ROTATE_0, 2209 DRM_MODE_ROTATE_0 | 2210 win->data->supported_rotations); 2211 drm_plane_create_alpha_property(&win->base); 2212 drm_plane_create_blend_mode_property(&win->base, blend_caps); 2213 drm_plane_create_zpos_property(&win->base, win->win_id, 0, 2214 vop2->registered_num_wins - 1); 2215 2216 return 0; 2217 } 2218 2219 static struct vop2_video_port *find_vp_without_primary(struct vop2 *vop2) 2220 { 2221 int i; 2222 2223 for (i = 0; i < vop2->data->nr_vps; i++) { 2224 struct vop2_video_port *vp = &vop2->vps[i]; 2225 2226 if (!vp->crtc.port) 2227 continue; 2228 if (vp->primary_plane) 2229 continue; 2230 2231 return vp; 2232 } 2233 2234 return NULL; 2235 } 2236 2237 #define NR_LAYERS 6 2238 2239 static int vop2_create_crtc(struct vop2 *vop2) 2240 { 2241 const struct vop2_data *vop2_data = vop2->data; 2242 struct drm_device *drm = vop2->drm; 2243 struct device *dev = vop2->dev; 2244 struct drm_plane *plane; 2245 struct device_node *port; 2246 struct vop2_video_port *vp; 2247 int i, nvp, nvps = 0; 2248 int ret; 2249 2250 for (i = 0; i < vop2_data->nr_vps; i++) { 2251 const struct vop2_video_port_data *vp_data; 2252 struct device_node *np; 2253 char dclk_name[9]; 2254 2255 vp_data = &vop2_data->vp[i]; 2256 vp = &vop2->vps[i]; 2257 vp->vop2 = vop2; 2258 vp->id = vp_data->id; 2259 vp->regs = vp_data->regs; 2260 vp->data = vp_data; 2261 2262 snprintf(dclk_name, sizeof(dclk_name), "dclk_vp%d", vp->id); 2263 vp->dclk = devm_clk_get(vop2->dev, dclk_name); 2264 if (IS_ERR(vp->dclk)) { 2265 drm_err(vop2->drm, "failed to get %s\n", dclk_name); 2266 return PTR_ERR(vp->dclk); 2267 } 2268 2269 np = of_graph_get_remote_node(dev->of_node, i, -1); 2270 if (!np) { 2271 drm_dbg(vop2->drm, "%s: No remote for vp%d\n", __func__, i); 2272 continue; 2273 } 2274 of_node_put(np); 2275 2276 port = of_graph_get_port_by_id(dev->of_node, i); 2277 if (!port) { 2278 drm_err(vop2->drm, "no port node found for video_port%d\n", i); 2279 return -ENOENT; 2280 } 2281 2282 vp->crtc.port = port; 2283 nvps++; 2284 } 2285 2286 nvp = 0; 2287 for (i = 0; i < vop2->registered_num_wins; i++) { 2288 struct vop2_win *win = &vop2->win[i]; 2289 u32 possible_crtcs; 2290 2291 if (vop2->data->soc_id == 3566) { 2292 /* 2293 * On RK3566 these windows don't have an independent 2294 * framebuffer. They share the framebuffer with smart0, 2295 * esmart0 and cluster0 respectively. 2296 */ 2297 switch (win->data->phys_id) { 2298 case ROCKCHIP_VOP2_SMART1: 2299 case ROCKCHIP_VOP2_ESMART1: 2300 case ROCKCHIP_VOP2_CLUSTER1: 2301 continue; 2302 } 2303 } 2304 2305 if (win->type == DRM_PLANE_TYPE_PRIMARY) { 2306 vp = find_vp_without_primary(vop2); 2307 if (vp) { 2308 possible_crtcs = BIT(nvp); 2309 vp->primary_plane = win; 2310 nvp++; 2311 } else { 2312 /* change the unused primary window to overlay window */ 2313 win->type = DRM_PLANE_TYPE_OVERLAY; 2314 } 2315 } 2316 2317 if (win->type == DRM_PLANE_TYPE_OVERLAY) 2318 possible_crtcs = (1 << nvps) - 1; 2319 2320 ret = vop2_plane_init(vop2, win, possible_crtcs); 2321 if (ret) { 2322 drm_err(vop2->drm, "failed to init plane %s: %d\n", 2323 win->data->name, ret); 2324 return ret; 2325 } 2326 } 2327 2328 for (i = 0; i < vop2_data->nr_vps; i++) { 2329 vp = &vop2->vps[i]; 2330 2331 if (!vp->crtc.port) 2332 continue; 2333 2334 plane = &vp->primary_plane->base; 2335 2336 ret = drm_crtc_init_with_planes(drm, &vp->crtc, plane, NULL, 2337 &vop2_crtc_funcs, 2338 "video_port%d", vp->id); 2339 if (ret) { 2340 drm_err(vop2->drm, "crtc init for video_port%d failed\n", i); 2341 return ret; 2342 } 2343 2344 drm_crtc_helper_add(&vp->crtc, &vop2_crtc_helper_funcs); 2345 2346 init_completion(&vp->dsp_hold_completion); 2347 } 2348 2349 /* 2350 * On the VOP2 it's very hard to change the number of layers on a VP 2351 * during runtime, so we distribute the layers equally over the used 2352 * VPs 2353 */ 2354 for (i = 0; i < vop2->data->nr_vps; i++) { 2355 struct vop2_video_port *vp = &vop2->vps[i]; 2356 2357 if (vp->crtc.port) 2358 vp->nlayers = NR_LAYERS / nvps; 2359 } 2360 2361 return 0; 2362 } 2363 2364 static void vop2_destroy_crtc(struct drm_crtc *crtc) 2365 { 2366 of_node_put(crtc->port); 2367 2368 /* 2369 * Destroy CRTC after vop2_plane_destroy() since vop2_disable_plane() 2370 * references the CRTC. 2371 */ 2372 drm_crtc_cleanup(crtc); 2373 } 2374 2375 static struct reg_field vop2_cluster_regs[VOP2_WIN_MAX_REG] = { 2376 [VOP2_WIN_ENABLE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 0, 0), 2377 [VOP2_WIN_FORMAT] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 1, 5), 2378 [VOP2_WIN_RB_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 14, 14), 2379 [VOP2_WIN_DITHER_UP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 18, 18), 2380 [VOP2_WIN_ACT_INFO] = REG_FIELD(RK3568_CLUSTER_WIN_ACT_INFO, 0, 31), 2381 [VOP2_WIN_DSP_INFO] = REG_FIELD(RK3568_CLUSTER_WIN_DSP_INFO, 0, 31), 2382 [VOP2_WIN_DSP_ST] = REG_FIELD(RK3568_CLUSTER_WIN_DSP_ST, 0, 31), 2383 [VOP2_WIN_YRGB_MST] = REG_FIELD(RK3568_CLUSTER_WIN_YRGB_MST, 0, 31), 2384 [VOP2_WIN_UV_MST] = REG_FIELD(RK3568_CLUSTER_WIN_CBR_MST, 0, 31), 2385 [VOP2_WIN_YUV_CLIP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 19, 19), 2386 [VOP2_WIN_YRGB_VIR] = REG_FIELD(RK3568_CLUSTER_WIN_VIR, 0, 15), 2387 [VOP2_WIN_UV_VIR] = REG_FIELD(RK3568_CLUSTER_WIN_VIR, 16, 31), 2388 [VOP2_WIN_Y2R_EN] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 8, 8), 2389 [VOP2_WIN_R2Y_EN] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 9, 9), 2390 [VOP2_WIN_CSC_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 10, 11), 2391 2392 /* Scale */ 2393 [VOP2_WIN_SCALE_YRGB_X] = REG_FIELD(RK3568_CLUSTER_WIN_SCL_FACTOR_YRGB, 0, 15), 2394 [VOP2_WIN_SCALE_YRGB_Y] = REG_FIELD(RK3568_CLUSTER_WIN_SCL_FACTOR_YRGB, 16, 31), 2395 [VOP2_WIN_YRGB_VER_SCL_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 14, 15), 2396 [VOP2_WIN_YRGB_HOR_SCL_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 12, 13), 2397 [VOP2_WIN_BIC_COE_SEL] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 2, 3), 2398 [VOP2_WIN_VSD_YRGB_GT2] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 28, 28), 2399 [VOP2_WIN_VSD_YRGB_GT4] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 29, 29), 2400 2401 /* cluster regs */ 2402 [VOP2_WIN_AFBC_ENABLE] = REG_FIELD(RK3568_CLUSTER_CTRL, 1, 1), 2403 [VOP2_WIN_CLUSTER_ENABLE] = REG_FIELD(RK3568_CLUSTER_CTRL, 0, 0), 2404 [VOP2_WIN_CLUSTER_LB_MODE] = REG_FIELD(RK3568_CLUSTER_CTRL, 4, 7), 2405 2406 /* afbc regs */ 2407 [VOP2_WIN_AFBC_FORMAT] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 2, 6), 2408 [VOP2_WIN_AFBC_RB_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 9, 9), 2409 [VOP2_WIN_AFBC_UV_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 10, 10), 2410 [VOP2_WIN_AFBC_AUTO_GATING_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_OUTPUT_CTRL, 4, 4), 2411 [VOP2_WIN_AFBC_HALF_BLOCK_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 7, 7), 2412 [VOP2_WIN_AFBC_BLOCK_SPLIT_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 8, 8), 2413 [VOP2_WIN_AFBC_HDR_PTR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_HDR_PTR, 0, 31), 2414 [VOP2_WIN_AFBC_PIC_SIZE] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_PIC_SIZE, 0, 31), 2415 [VOP2_WIN_AFBC_PIC_VIR_WIDTH] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_VIR_WIDTH, 0, 15), 2416 [VOP2_WIN_AFBC_TILE_NUM] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_VIR_WIDTH, 16, 31), 2417 [VOP2_WIN_AFBC_PIC_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_PIC_OFFSET, 0, 31), 2418 [VOP2_WIN_AFBC_DSP_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_DSP_OFFSET, 0, 31), 2419 [VOP2_WIN_AFBC_TRANSFORM_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_TRANSFORM_OFFSET, 0, 31), 2420 [VOP2_WIN_AFBC_ROTATE_90] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 0, 0), 2421 [VOP2_WIN_AFBC_ROTATE_270] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 1, 1), 2422 [VOP2_WIN_XMIRROR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 2, 2), 2423 [VOP2_WIN_YMIRROR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 3, 3), 2424 [VOP2_WIN_UV_SWAP] = { .reg = 0xffffffff }, 2425 [VOP2_WIN_COLOR_KEY] = { .reg = 0xffffffff }, 2426 [VOP2_WIN_COLOR_KEY_EN] = { .reg = 0xffffffff }, 2427 [VOP2_WIN_SCALE_CBCR_X] = { .reg = 0xffffffff }, 2428 [VOP2_WIN_SCALE_CBCR_Y] = { .reg = 0xffffffff }, 2429 [VOP2_WIN_YRGB_HSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2430 [VOP2_WIN_YRGB_VSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2431 [VOP2_WIN_CBCR_VER_SCL_MODE] = { .reg = 0xffffffff }, 2432 [VOP2_WIN_CBCR_HSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2433 [VOP2_WIN_CBCR_HOR_SCL_MODE] = { .reg = 0xffffffff }, 2434 [VOP2_WIN_CBCR_VSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2435 [VOP2_WIN_VSD_CBCR_GT2] = { .reg = 0xffffffff }, 2436 [VOP2_WIN_VSD_CBCR_GT4] = { .reg = 0xffffffff }, 2437 }; 2438 2439 static int vop2_cluster_init(struct vop2_win *win) 2440 { 2441 struct vop2 *vop2 = win->vop2; 2442 struct reg_field *cluster_regs; 2443 int ret, i; 2444 2445 cluster_regs = kmemdup(vop2_cluster_regs, sizeof(vop2_cluster_regs), 2446 GFP_KERNEL); 2447 if (!cluster_regs) 2448 return -ENOMEM; 2449 2450 for (i = 0; i < ARRAY_SIZE(vop2_cluster_regs); i++) 2451 if (cluster_regs[i].reg != 0xffffffff) 2452 cluster_regs[i].reg += win->offset; 2453 2454 ret = devm_regmap_field_bulk_alloc(vop2->dev, vop2->map, win->reg, 2455 cluster_regs, 2456 ARRAY_SIZE(vop2_cluster_regs)); 2457 2458 kfree(cluster_regs); 2459 2460 return ret; 2461 }; 2462 2463 static struct reg_field vop2_esmart_regs[VOP2_WIN_MAX_REG] = { 2464 [VOP2_WIN_ENABLE] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 0, 0), 2465 [VOP2_WIN_FORMAT] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 1, 5), 2466 [VOP2_WIN_DITHER_UP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 12, 12), 2467 [VOP2_WIN_RB_SWAP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 14, 14), 2468 [VOP2_WIN_UV_SWAP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 16, 16), 2469 [VOP2_WIN_ACT_INFO] = REG_FIELD(RK3568_SMART_REGION0_ACT_INFO, 0, 31), 2470 [VOP2_WIN_DSP_INFO] = REG_FIELD(RK3568_SMART_REGION0_DSP_INFO, 0, 31), 2471 [VOP2_WIN_DSP_ST] = REG_FIELD(RK3568_SMART_REGION0_DSP_ST, 0, 28), 2472 [VOP2_WIN_YRGB_MST] = REG_FIELD(RK3568_SMART_REGION0_YRGB_MST, 0, 31), 2473 [VOP2_WIN_UV_MST] = REG_FIELD(RK3568_SMART_REGION0_CBR_MST, 0, 31), 2474 [VOP2_WIN_YUV_CLIP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 17, 17), 2475 [VOP2_WIN_YRGB_VIR] = REG_FIELD(RK3568_SMART_REGION0_VIR, 0, 15), 2476 [VOP2_WIN_UV_VIR] = REG_FIELD(RK3568_SMART_REGION0_VIR, 16, 31), 2477 [VOP2_WIN_Y2R_EN] = REG_FIELD(RK3568_SMART_CTRL0, 0, 0), 2478 [VOP2_WIN_R2Y_EN] = REG_FIELD(RK3568_SMART_CTRL0, 1, 1), 2479 [VOP2_WIN_CSC_MODE] = REG_FIELD(RK3568_SMART_CTRL0, 2, 3), 2480 [VOP2_WIN_YMIRROR] = REG_FIELD(RK3568_SMART_CTRL1, 31, 31), 2481 [VOP2_WIN_COLOR_KEY] = REG_FIELD(RK3568_SMART_COLOR_KEY_CTRL, 0, 29), 2482 [VOP2_WIN_COLOR_KEY_EN] = REG_FIELD(RK3568_SMART_COLOR_KEY_CTRL, 31, 31), 2483 2484 /* Scale */ 2485 [VOP2_WIN_SCALE_YRGB_X] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_YRGB, 0, 15), 2486 [VOP2_WIN_SCALE_YRGB_Y] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_YRGB, 16, 31), 2487 [VOP2_WIN_SCALE_CBCR_X] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_CBR, 0, 15), 2488 [VOP2_WIN_SCALE_CBCR_Y] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_CBR, 16, 31), 2489 [VOP2_WIN_YRGB_HOR_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 0, 1), 2490 [VOP2_WIN_YRGB_HSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 2, 3), 2491 [VOP2_WIN_YRGB_VER_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 4, 5), 2492 [VOP2_WIN_YRGB_VSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 6, 7), 2493 [VOP2_WIN_CBCR_HOR_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 8, 9), 2494 [VOP2_WIN_CBCR_HSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 10, 11), 2495 [VOP2_WIN_CBCR_VER_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 12, 13), 2496 [VOP2_WIN_CBCR_VSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 14, 15), 2497 [VOP2_WIN_BIC_COE_SEL] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 16, 17), 2498 [VOP2_WIN_VSD_YRGB_GT2] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 8, 8), 2499 [VOP2_WIN_VSD_YRGB_GT4] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 9, 9), 2500 [VOP2_WIN_VSD_CBCR_GT2] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 10, 10), 2501 [VOP2_WIN_VSD_CBCR_GT4] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 11, 11), 2502 [VOP2_WIN_XMIRROR] = { .reg = 0xffffffff }, 2503 [VOP2_WIN_CLUSTER_ENABLE] = { .reg = 0xffffffff }, 2504 [VOP2_WIN_AFBC_ENABLE] = { .reg = 0xffffffff }, 2505 [VOP2_WIN_CLUSTER_LB_MODE] = { .reg = 0xffffffff }, 2506 [VOP2_WIN_AFBC_FORMAT] = { .reg = 0xffffffff }, 2507 [VOP2_WIN_AFBC_RB_SWAP] = { .reg = 0xffffffff }, 2508 [VOP2_WIN_AFBC_UV_SWAP] = { .reg = 0xffffffff }, 2509 [VOP2_WIN_AFBC_AUTO_GATING_EN] = { .reg = 0xffffffff }, 2510 [VOP2_WIN_AFBC_BLOCK_SPLIT_EN] = { .reg = 0xffffffff }, 2511 [VOP2_WIN_AFBC_PIC_VIR_WIDTH] = { .reg = 0xffffffff }, 2512 [VOP2_WIN_AFBC_TILE_NUM] = { .reg = 0xffffffff }, 2513 [VOP2_WIN_AFBC_PIC_OFFSET] = { .reg = 0xffffffff }, 2514 [VOP2_WIN_AFBC_PIC_SIZE] = { .reg = 0xffffffff }, 2515 [VOP2_WIN_AFBC_DSP_OFFSET] = { .reg = 0xffffffff }, 2516 [VOP2_WIN_AFBC_TRANSFORM_OFFSET] = { .reg = 0xffffffff }, 2517 [VOP2_WIN_AFBC_HDR_PTR] = { .reg = 0xffffffff }, 2518 [VOP2_WIN_AFBC_HALF_BLOCK_EN] = { .reg = 0xffffffff }, 2519 [VOP2_WIN_AFBC_ROTATE_270] = { .reg = 0xffffffff }, 2520 [VOP2_WIN_AFBC_ROTATE_90] = { .reg = 0xffffffff }, 2521 }; 2522 2523 static int vop2_esmart_init(struct vop2_win *win) 2524 { 2525 struct vop2 *vop2 = win->vop2; 2526 struct reg_field *esmart_regs; 2527 int ret, i; 2528 2529 esmart_regs = kmemdup(vop2_esmart_regs, sizeof(vop2_esmart_regs), 2530 GFP_KERNEL); 2531 if (!esmart_regs) 2532 return -ENOMEM; 2533 2534 for (i = 0; i < ARRAY_SIZE(vop2_esmart_regs); i++) 2535 if (esmart_regs[i].reg != 0xffffffff) 2536 esmart_regs[i].reg += win->offset; 2537 2538 ret = devm_regmap_field_bulk_alloc(vop2->dev, vop2->map, win->reg, 2539 esmart_regs, 2540 ARRAY_SIZE(vop2_esmart_regs)); 2541 2542 kfree(esmart_regs); 2543 2544 return ret; 2545 }; 2546 2547 static int vop2_win_init(struct vop2 *vop2) 2548 { 2549 const struct vop2_data *vop2_data = vop2->data; 2550 struct vop2_win *win; 2551 int i, ret; 2552 2553 for (i = 0; i < vop2_data->win_size; i++) { 2554 const struct vop2_win_data *win_data = &vop2_data->win[i]; 2555 2556 win = &vop2->win[i]; 2557 win->data = win_data; 2558 win->type = win_data->type; 2559 win->offset = win_data->base; 2560 win->win_id = i; 2561 win->vop2 = vop2; 2562 if (vop2_cluster_window(win)) 2563 ret = vop2_cluster_init(win); 2564 else 2565 ret = vop2_esmart_init(win); 2566 if (ret) 2567 return ret; 2568 } 2569 2570 vop2->registered_num_wins = vop2_data->win_size; 2571 2572 return 0; 2573 } 2574 2575 /* 2576 * The window registers are only updated when config done is written. 2577 * Until that they read back the old value. As we read-modify-write 2578 * these registers mark them as non-volatile. This makes sure we read 2579 * the new values from the regmap register cache. 2580 */ 2581 static const struct regmap_range vop2_nonvolatile_range[] = { 2582 regmap_reg_range(0x1000, 0x23ff), 2583 }; 2584 2585 static const struct regmap_access_table vop2_volatile_table = { 2586 .no_ranges = vop2_nonvolatile_range, 2587 .n_no_ranges = ARRAY_SIZE(vop2_nonvolatile_range), 2588 }; 2589 2590 static const struct regmap_config vop2_regmap_config = { 2591 .reg_bits = 32, 2592 .val_bits = 32, 2593 .reg_stride = 4, 2594 .max_register = 0x3000, 2595 .name = "vop2", 2596 .volatile_table = &vop2_volatile_table, 2597 .cache_type = REGCACHE_RBTREE, 2598 }; 2599 2600 static int vop2_bind(struct device *dev, struct device *master, void *data) 2601 { 2602 struct platform_device *pdev = to_platform_device(dev); 2603 const struct vop2_data *vop2_data; 2604 struct drm_device *drm = data; 2605 struct vop2 *vop2; 2606 struct resource *res; 2607 size_t alloc_size; 2608 int ret; 2609 2610 vop2_data = of_device_get_match_data(dev); 2611 if (!vop2_data) 2612 return -ENODEV; 2613 2614 /* Allocate vop2 struct and its vop2_win array */ 2615 alloc_size = sizeof(*vop2) + sizeof(*vop2->win) * vop2_data->win_size; 2616 vop2 = devm_kzalloc(dev, alloc_size, GFP_KERNEL); 2617 if (!vop2) 2618 return -ENOMEM; 2619 2620 vop2->dev = dev; 2621 vop2->data = vop2_data; 2622 vop2->drm = drm; 2623 2624 dev_set_drvdata(dev, vop2); 2625 2626 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vop"); 2627 if (!res) { 2628 drm_err(vop2->drm, "failed to get vop2 register byname\n"); 2629 return -EINVAL; 2630 } 2631 2632 vop2->regs = devm_ioremap_resource(dev, res); 2633 if (IS_ERR(vop2->regs)) 2634 return PTR_ERR(vop2->regs); 2635 vop2->len = resource_size(res); 2636 2637 vop2->map = devm_regmap_init_mmio(dev, vop2->regs, &vop2_regmap_config); 2638 2639 ret = vop2_win_init(vop2); 2640 if (ret) 2641 return ret; 2642 2643 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gamma-lut"); 2644 if (res) { 2645 vop2->lut_regs = devm_ioremap_resource(dev, res); 2646 if (IS_ERR(vop2->lut_regs)) 2647 return PTR_ERR(vop2->lut_regs); 2648 } 2649 2650 vop2->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf"); 2651 2652 vop2->hclk = devm_clk_get(vop2->dev, "hclk"); 2653 if (IS_ERR(vop2->hclk)) { 2654 drm_err(vop2->drm, "failed to get hclk source\n"); 2655 return PTR_ERR(vop2->hclk); 2656 } 2657 2658 vop2->aclk = devm_clk_get(vop2->dev, "aclk"); 2659 if (IS_ERR(vop2->aclk)) { 2660 drm_err(vop2->drm, "failed to get aclk source\n"); 2661 return PTR_ERR(vop2->aclk); 2662 } 2663 2664 vop2->irq = platform_get_irq(pdev, 0); 2665 if (vop2->irq < 0) { 2666 drm_err(vop2->drm, "cannot find irq for vop2\n"); 2667 return vop2->irq; 2668 } 2669 2670 mutex_init(&vop2->vop2_lock); 2671 2672 ret = devm_request_irq(dev, vop2->irq, vop2_isr, IRQF_SHARED, dev_name(dev), vop2); 2673 if (ret) 2674 return ret; 2675 2676 ret = vop2_create_crtc(vop2); 2677 if (ret) 2678 return ret; 2679 2680 rockchip_drm_dma_init_device(vop2->drm, vop2->dev); 2681 2682 pm_runtime_enable(&pdev->dev); 2683 2684 return 0; 2685 } 2686 2687 static void vop2_unbind(struct device *dev, struct device *master, void *data) 2688 { 2689 struct vop2 *vop2 = dev_get_drvdata(dev); 2690 struct drm_device *drm = vop2->drm; 2691 struct list_head *plane_list = &drm->mode_config.plane_list; 2692 struct list_head *crtc_list = &drm->mode_config.crtc_list; 2693 struct drm_crtc *crtc, *tmpc; 2694 struct drm_plane *plane, *tmpp; 2695 2696 pm_runtime_disable(dev); 2697 2698 list_for_each_entry_safe(plane, tmpp, plane_list, head) 2699 drm_plane_cleanup(plane); 2700 2701 list_for_each_entry_safe(crtc, tmpc, crtc_list, head) 2702 vop2_destroy_crtc(crtc); 2703 } 2704 2705 const struct component_ops vop2_component_ops = { 2706 .bind = vop2_bind, 2707 .unbind = vop2_unbind, 2708 }; 2709 EXPORT_SYMBOL_GPL(vop2_component_ops); 2710