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/media-bus-format.h> 13 #include <linux/mfd/syscon.h> 14 #include <linux/module.h> 15 #include <linux/of.h> 16 #include <linux/of_device.h> 17 #include <linux/of_graph.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/regmap.h> 21 #include <linux/swab.h> 22 23 #include <drm/drm.h> 24 #include <drm/drm_atomic.h> 25 #include <drm/drm_atomic_uapi.h> 26 #include <drm/drm_blend.h> 27 #include <drm/drm_crtc.h> 28 #include <drm/drm_crtc_helper.h> 29 #include <drm/drm_debugfs.h> 30 #include <drm/drm_flip_work.h> 31 #include <drm/drm_framebuffer.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 dip &= ~RK3568_DSP_IF_POL__HDMI_PIN_POL; 1442 dip |= FIELD_PREP(RK3568_DSP_IF_POL__HDMI_PIN_POL, polflags); 1443 break; 1444 case ROCKCHIP_VOP2_EP_EDP0: 1445 die &= ~RK3568_SYS_DSP_INFACE_EN_EDP_MUX; 1446 die |= RK3568_SYS_DSP_INFACE_EN_EDP | 1447 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_EDP_MUX, vp->id); 1448 dip &= ~RK3568_DSP_IF_POL__EDP_PIN_POL; 1449 dip |= FIELD_PREP(RK3568_DSP_IF_POL__EDP_PIN_POL, polflags); 1450 break; 1451 case ROCKCHIP_VOP2_EP_MIPI0: 1452 die &= ~RK3568_SYS_DSP_INFACE_EN_MIPI0_MUX; 1453 die |= RK3568_SYS_DSP_INFACE_EN_MIPI0 | 1454 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_MIPI0_MUX, vp->id); 1455 dip &= ~RK3568_DSP_IF_POL__MIPI_PIN_POL; 1456 dip |= FIELD_PREP(RK3568_DSP_IF_POL__MIPI_PIN_POL, polflags); 1457 break; 1458 case ROCKCHIP_VOP2_EP_MIPI1: 1459 die &= ~RK3568_SYS_DSP_INFACE_EN_MIPI1_MUX; 1460 die |= RK3568_SYS_DSP_INFACE_EN_MIPI1 | 1461 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_MIPI1_MUX, vp->id); 1462 dip &= ~RK3568_DSP_IF_POL__MIPI_PIN_POL; 1463 dip |= FIELD_PREP(RK3568_DSP_IF_POL__MIPI_PIN_POL, polflags); 1464 break; 1465 case ROCKCHIP_VOP2_EP_LVDS0: 1466 die &= ~RK3568_SYS_DSP_INFACE_EN_LVDS0_MUX; 1467 die |= RK3568_SYS_DSP_INFACE_EN_LVDS0 | 1468 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_LVDS0_MUX, vp->id); 1469 dip &= ~RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL; 1470 dip |= FIELD_PREP(RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL, polflags); 1471 break; 1472 case ROCKCHIP_VOP2_EP_LVDS1: 1473 die &= ~RK3568_SYS_DSP_INFACE_EN_LVDS1_MUX; 1474 die |= RK3568_SYS_DSP_INFACE_EN_LVDS1 | 1475 FIELD_PREP(RK3568_SYS_DSP_INFACE_EN_LVDS1_MUX, vp->id); 1476 dip &= ~RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL; 1477 dip |= FIELD_PREP(RK3568_DSP_IF_POL__RGB_LVDS_PIN_POL, polflags); 1478 break; 1479 default: 1480 drm_err(vop2->drm, "Invalid interface id %d on vp%d\n", id, vp->id); 1481 return; 1482 } 1483 1484 dip |= RK3568_DSP_IF_POL__CFG_DONE_IMD; 1485 1486 vop2_writel(vop2, RK3568_DSP_IF_EN, die); 1487 vop2_writel(vop2, RK3568_DSP_IF_POL, dip); 1488 } 1489 1490 static int us_to_vertical_line(struct drm_display_mode *mode, int us) 1491 { 1492 return us * mode->clock / mode->htotal / 1000; 1493 } 1494 1495 static void vop2_crtc_atomic_enable(struct drm_crtc *crtc, 1496 struct drm_atomic_state *state) 1497 { 1498 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1499 struct vop2 *vop2 = vp->vop2; 1500 const struct vop2_data *vop2_data = vop2->data; 1501 const struct vop2_video_port_data *vp_data = &vop2_data->vp[vp->id]; 1502 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 1503 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state); 1504 struct drm_display_mode *mode = &crtc->state->adjusted_mode; 1505 unsigned long clock = mode->crtc_clock * 1000; 1506 u16 hsync_len = mode->crtc_hsync_end - mode->crtc_hsync_start; 1507 u16 hdisplay = mode->crtc_hdisplay; 1508 u16 htotal = mode->crtc_htotal; 1509 u16 hact_st = mode->crtc_htotal - mode->crtc_hsync_start; 1510 u16 hact_end = hact_st + hdisplay; 1511 u16 vdisplay = mode->crtc_vdisplay; 1512 u16 vtotal = mode->crtc_vtotal; 1513 u16 vsync_len = mode->crtc_vsync_end - mode->crtc_vsync_start; 1514 u16 vact_st = mode->crtc_vtotal - mode->crtc_vsync_start; 1515 u16 vact_end = vact_st + vdisplay; 1516 u8 out_mode; 1517 u32 dsp_ctrl = 0; 1518 int act_end; 1519 u32 val, polflags; 1520 int ret; 1521 struct drm_encoder *encoder; 1522 1523 drm_dbg(vop2->drm, "Update mode to %dx%d%s%d, type: %d for vp%d\n", 1524 hdisplay, vdisplay, mode->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "p", 1525 drm_mode_vrefresh(mode), vcstate->output_type, vp->id); 1526 1527 vop2_lock(vop2); 1528 1529 ret = clk_prepare_enable(vp->dclk); 1530 if (ret < 0) { 1531 drm_err(vop2->drm, "failed to enable dclk for video port%d - %d\n", 1532 vp->id, ret); 1533 vop2_unlock(vop2); 1534 return; 1535 } 1536 1537 if (!vop2->enable_count) 1538 vop2_enable(vop2); 1539 1540 vop2->enable_count++; 1541 1542 vop2_crtc_enable_irq(vp, VP_INT_POST_BUF_EMPTY); 1543 1544 polflags = 0; 1545 if (vcstate->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE) 1546 polflags |= POLFLAG_DCLK_INV; 1547 if (mode->flags & DRM_MODE_FLAG_PHSYNC) 1548 polflags |= BIT(HSYNC_POSITIVE); 1549 if (mode->flags & DRM_MODE_FLAG_PVSYNC) 1550 polflags |= BIT(VSYNC_POSITIVE); 1551 1552 drm_for_each_encoder_mask(encoder, crtc->dev, crtc_state->encoder_mask) { 1553 struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder); 1554 1555 rk3568_set_intf_mux(vp, rkencoder->crtc_endpoint_id, polflags); 1556 } 1557 1558 if (vcstate->output_mode == ROCKCHIP_OUT_MODE_AAAA && 1559 !(vp_data->feature & VOP_FEATURE_OUTPUT_10BIT)) 1560 out_mode = ROCKCHIP_OUT_MODE_P888; 1561 else 1562 out_mode = vcstate->output_mode; 1563 1564 dsp_ctrl |= FIELD_PREP(RK3568_VP_DSP_CTRL__OUT_MODE, out_mode); 1565 1566 if (vop2_output_uv_swap(vcstate->bus_format, vcstate->output_mode)) 1567 dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_RB_SWAP; 1568 1569 if (is_yuv_output(vcstate->bus_format)) 1570 dsp_ctrl |= RK3568_VP_DSP_CTRL__POST_DSP_OUT_R2Y; 1571 1572 vop2_dither_setup(crtc, &dsp_ctrl); 1573 1574 vop2_vp_write(vp, RK3568_VP_DSP_HTOTAL_HS_END, (htotal << 16) | hsync_len); 1575 val = hact_st << 16; 1576 val |= hact_end; 1577 vop2_vp_write(vp, RK3568_VP_DSP_HACT_ST_END, val); 1578 1579 val = vact_st << 16; 1580 val |= vact_end; 1581 vop2_vp_write(vp, RK3568_VP_DSP_VACT_ST_END, val); 1582 1583 if (mode->flags & DRM_MODE_FLAG_INTERLACE) { 1584 u16 vact_st_f1 = vtotal + vact_st + 1; 1585 u16 vact_end_f1 = vact_st_f1 + vdisplay; 1586 1587 val = vact_st_f1 << 16 | vact_end_f1; 1588 vop2_vp_write(vp, RK3568_VP_DSP_VACT_ST_END_F1, val); 1589 1590 val = vtotal << 16 | (vtotal + vsync_len); 1591 vop2_vp_write(vp, RK3568_VP_DSP_VS_ST_END_F1, val); 1592 dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_INTERLACE; 1593 dsp_ctrl |= RK3568_VP_DSP_CTRL__DSP_FILED_POL; 1594 dsp_ctrl |= RK3568_VP_DSP_CTRL__P2I_EN; 1595 vtotal += vtotal + 1; 1596 act_end = vact_end_f1; 1597 } else { 1598 act_end = vact_end; 1599 } 1600 1601 vop2_writel(vop2, RK3568_VP_LINE_FLAG(vp->id), 1602 (act_end - us_to_vertical_line(mode, 0)) << 16 | act_end); 1603 1604 vop2_vp_write(vp, RK3568_VP_DSP_VTOTAL_VS_END, vtotal << 16 | vsync_len); 1605 1606 if (mode->flags & DRM_MODE_FLAG_DBLCLK) { 1607 dsp_ctrl |= RK3568_VP_DSP_CTRL__CORE_DCLK_DIV; 1608 clock *= 2; 1609 } 1610 1611 vop2_vp_write(vp, RK3568_VP_MIPI_CTRL, 0); 1612 1613 clk_set_rate(vp->dclk, clock); 1614 1615 vop2_post_config(crtc); 1616 1617 vop2_cfg_done(vp); 1618 1619 vop2_vp_write(vp, RK3568_VP_DSP_CTRL, dsp_ctrl); 1620 1621 drm_crtc_vblank_on(crtc); 1622 1623 vop2_unlock(vop2); 1624 } 1625 1626 static int vop2_crtc_atomic_check(struct drm_crtc *crtc, 1627 struct drm_atomic_state *state) 1628 { 1629 struct vop2_video_port *vp = to_vop2_video_port(crtc); 1630 struct drm_plane *plane; 1631 int nplanes = 0; 1632 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 1633 1634 drm_atomic_crtc_state_for_each_plane(plane, crtc_state) 1635 nplanes++; 1636 1637 if (nplanes > vp->nlayers) 1638 return -EINVAL; 1639 1640 return 0; 1641 } 1642 1643 static bool is_opaque(u16 alpha) 1644 { 1645 return (alpha >> 8) == 0xff; 1646 } 1647 1648 static void vop2_parse_alpha(struct vop2_alpha_config *alpha_config, 1649 struct vop2_alpha *alpha) 1650 { 1651 int src_glb_alpha_en = is_opaque(alpha_config->src_glb_alpha_value) ? 0 : 1; 1652 int dst_glb_alpha_en = is_opaque(alpha_config->dst_glb_alpha_value) ? 0 : 1; 1653 int src_color_mode = alpha_config->src_premulti_en ? 1654 ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL; 1655 int dst_color_mode = alpha_config->dst_premulti_en ? 1656 ALPHA_SRC_PRE_MUL : ALPHA_SRC_NO_PRE_MUL; 1657 1658 alpha->src_color_ctrl.val = 0; 1659 alpha->dst_color_ctrl.val = 0; 1660 alpha->src_alpha_ctrl.val = 0; 1661 alpha->dst_alpha_ctrl.val = 0; 1662 1663 if (!alpha_config->src_pixel_alpha_en) 1664 alpha->src_color_ctrl.bits.blend_mode = ALPHA_GLOBAL; 1665 else if (alpha_config->src_pixel_alpha_en && !src_glb_alpha_en) 1666 alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX; 1667 else 1668 alpha->src_color_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL; 1669 1670 alpha->src_color_ctrl.bits.alpha_en = 1; 1671 1672 if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_GLOBAL) { 1673 alpha->src_color_ctrl.bits.color_mode = src_color_mode; 1674 alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL; 1675 } else if (alpha->src_color_ctrl.bits.blend_mode == ALPHA_PER_PIX) { 1676 alpha->src_color_ctrl.bits.color_mode = src_color_mode; 1677 alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_ONE; 1678 } else { 1679 alpha->src_color_ctrl.bits.color_mode = ALPHA_SRC_PRE_MUL; 1680 alpha->src_color_ctrl.bits.factor_mode = SRC_FAC_ALPHA_SRC_GLOBAL; 1681 } 1682 alpha->src_color_ctrl.bits.glb_alpha = alpha_config->src_glb_alpha_value >> 8; 1683 alpha->src_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1684 alpha->src_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION; 1685 1686 alpha->dst_color_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1687 alpha->dst_color_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION; 1688 alpha->dst_color_ctrl.bits.blend_mode = ALPHA_GLOBAL; 1689 alpha->dst_color_ctrl.bits.glb_alpha = alpha_config->dst_glb_alpha_value >> 8; 1690 alpha->dst_color_ctrl.bits.color_mode = dst_color_mode; 1691 alpha->dst_color_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE; 1692 1693 alpha->src_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1694 alpha->src_alpha_ctrl.bits.blend_mode = alpha->src_color_ctrl.bits.blend_mode; 1695 alpha->src_alpha_ctrl.bits.alpha_cal_mode = ALPHA_SATURATION; 1696 alpha->src_alpha_ctrl.bits.factor_mode = ALPHA_ONE; 1697 1698 alpha->dst_alpha_ctrl.bits.alpha_mode = ALPHA_STRAIGHT; 1699 if (alpha_config->dst_pixel_alpha_en && !dst_glb_alpha_en) 1700 alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX; 1701 else 1702 alpha->dst_alpha_ctrl.bits.blend_mode = ALPHA_PER_PIX_GLOBAL; 1703 alpha->dst_alpha_ctrl.bits.alpha_cal_mode = ALPHA_NO_SATURATION; 1704 alpha->dst_alpha_ctrl.bits.factor_mode = ALPHA_SRC_INVERSE; 1705 } 1706 1707 static int vop2_find_start_mixer_id_for_vp(struct vop2 *vop2, u8 port_id) 1708 { 1709 struct vop2_video_port *vp; 1710 int used_layer = 0; 1711 int i; 1712 1713 for (i = 0; i < port_id; i++) { 1714 vp = &vop2->vps[i]; 1715 used_layer += hweight32(vp->win_mask); 1716 } 1717 1718 return used_layer; 1719 } 1720 1721 static void vop2_setup_cluster_alpha(struct vop2 *vop2, struct vop2_win *main_win) 1722 { 1723 u32 offset = (main_win->data->phys_id * 0x10); 1724 struct vop2_alpha_config alpha_config; 1725 struct vop2_alpha alpha; 1726 struct drm_plane_state *bottom_win_pstate; 1727 bool src_pixel_alpha_en = false; 1728 u16 src_glb_alpha_val, dst_glb_alpha_val; 1729 bool premulti_en = false; 1730 bool swap = false; 1731 1732 /* At one win mode, win0 is dst/bottom win, and win1 is a all zero src/top win */ 1733 bottom_win_pstate = main_win->base.state; 1734 src_glb_alpha_val = 0; 1735 dst_glb_alpha_val = main_win->base.state->alpha; 1736 1737 if (!bottom_win_pstate->fb) 1738 return; 1739 1740 alpha_config.src_premulti_en = premulti_en; 1741 alpha_config.dst_premulti_en = false; 1742 alpha_config.src_pixel_alpha_en = src_pixel_alpha_en; 1743 alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */ 1744 alpha_config.src_glb_alpha_value = src_glb_alpha_val; 1745 alpha_config.dst_glb_alpha_value = dst_glb_alpha_val; 1746 vop2_parse_alpha(&alpha_config, &alpha); 1747 1748 alpha.src_color_ctrl.bits.src_dst_swap = swap; 1749 vop2_writel(vop2, RK3568_CLUSTER0_MIX_SRC_COLOR_CTRL + offset, 1750 alpha.src_color_ctrl.val); 1751 vop2_writel(vop2, RK3568_CLUSTER0_MIX_DST_COLOR_CTRL + offset, 1752 alpha.dst_color_ctrl.val); 1753 vop2_writel(vop2, RK3568_CLUSTER0_MIX_SRC_ALPHA_CTRL + offset, 1754 alpha.src_alpha_ctrl.val); 1755 vop2_writel(vop2, RK3568_CLUSTER0_MIX_DST_ALPHA_CTRL + offset, 1756 alpha.dst_alpha_ctrl.val); 1757 } 1758 1759 static void vop2_setup_alpha(struct vop2_video_port *vp) 1760 { 1761 struct vop2 *vop2 = vp->vop2; 1762 struct drm_framebuffer *fb; 1763 struct vop2_alpha_config alpha_config; 1764 struct vop2_alpha alpha; 1765 struct drm_plane *plane; 1766 int pixel_alpha_en; 1767 int premulti_en, gpremulti_en = 0; 1768 int mixer_id; 1769 u32 offset; 1770 bool bottom_layer_alpha_en = false; 1771 u32 dst_global_alpha = DRM_BLEND_ALPHA_OPAQUE; 1772 1773 mixer_id = vop2_find_start_mixer_id_for_vp(vop2, vp->id); 1774 alpha_config.dst_pixel_alpha_en = true; /* alpha value need transfer to next mix */ 1775 1776 drm_atomic_crtc_for_each_plane(plane, &vp->crtc) { 1777 struct vop2_win *win = to_vop2_win(plane); 1778 1779 if (plane->state->normalized_zpos == 0 && 1780 !is_opaque(plane->state->alpha) && 1781 !vop2_cluster_window(win)) { 1782 /* 1783 * If bottom layer have global alpha effect [except cluster layer, 1784 * because cluster have deal with bottom layer global alpha value 1785 * at cluster mix], bottom layer mix need deal with global alpha. 1786 */ 1787 bottom_layer_alpha_en = true; 1788 dst_global_alpha = plane->state->alpha; 1789 } 1790 } 1791 1792 drm_atomic_crtc_for_each_plane(plane, &vp->crtc) { 1793 struct vop2_win *win = to_vop2_win(plane); 1794 int zpos = plane->state->normalized_zpos; 1795 1796 if (plane->state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) 1797 premulti_en = 1; 1798 else 1799 premulti_en = 0; 1800 1801 plane = &win->base; 1802 fb = plane->state->fb; 1803 1804 pixel_alpha_en = fb->format->has_alpha; 1805 1806 alpha_config.src_premulti_en = premulti_en; 1807 1808 if (bottom_layer_alpha_en && zpos == 1) { 1809 gpremulti_en = premulti_en; 1810 /* Cd = Cs + (1 - As) * Cd * Agd */ 1811 alpha_config.dst_premulti_en = false; 1812 alpha_config.src_pixel_alpha_en = pixel_alpha_en; 1813 alpha_config.src_glb_alpha_value = plane->state->alpha; 1814 alpha_config.dst_glb_alpha_value = dst_global_alpha; 1815 } else if (vop2_cluster_window(win)) { 1816 /* Mix output data only have pixel alpha */ 1817 alpha_config.dst_premulti_en = true; 1818 alpha_config.src_pixel_alpha_en = true; 1819 alpha_config.src_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1820 alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1821 } else { 1822 /* Cd = Cs + (1 - As) * Cd */ 1823 alpha_config.dst_premulti_en = true; 1824 alpha_config.src_pixel_alpha_en = pixel_alpha_en; 1825 alpha_config.src_glb_alpha_value = plane->state->alpha; 1826 alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1827 } 1828 1829 vop2_parse_alpha(&alpha_config, &alpha); 1830 1831 offset = (mixer_id + zpos - 1) * 0x10; 1832 vop2_writel(vop2, RK3568_MIX0_SRC_COLOR_CTRL + offset, 1833 alpha.src_color_ctrl.val); 1834 vop2_writel(vop2, RK3568_MIX0_DST_COLOR_CTRL + offset, 1835 alpha.dst_color_ctrl.val); 1836 vop2_writel(vop2, RK3568_MIX0_SRC_ALPHA_CTRL + offset, 1837 alpha.src_alpha_ctrl.val); 1838 vop2_writel(vop2, RK3568_MIX0_DST_ALPHA_CTRL + offset, 1839 alpha.dst_alpha_ctrl.val); 1840 } 1841 1842 if (vp->id == 0) { 1843 if (bottom_layer_alpha_en) { 1844 /* Transfer pixel alpha to hdr mix */ 1845 alpha_config.src_premulti_en = gpremulti_en; 1846 alpha_config.dst_premulti_en = true; 1847 alpha_config.src_pixel_alpha_en = true; 1848 alpha_config.src_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1849 alpha_config.dst_glb_alpha_value = DRM_BLEND_ALPHA_OPAQUE; 1850 vop2_parse_alpha(&alpha_config, &alpha); 1851 1852 vop2_writel(vop2, RK3568_HDR0_SRC_COLOR_CTRL, 1853 alpha.src_color_ctrl.val); 1854 vop2_writel(vop2, RK3568_HDR0_DST_COLOR_CTRL, 1855 alpha.dst_color_ctrl.val); 1856 vop2_writel(vop2, RK3568_HDR0_SRC_ALPHA_CTRL, 1857 alpha.src_alpha_ctrl.val); 1858 vop2_writel(vop2, RK3568_HDR0_DST_ALPHA_CTRL, 1859 alpha.dst_alpha_ctrl.val); 1860 } else { 1861 vop2_writel(vop2, RK3568_HDR0_SRC_COLOR_CTRL, 0); 1862 } 1863 } 1864 } 1865 1866 static void vop2_setup_layer_mixer(struct vop2_video_port *vp) 1867 { 1868 struct vop2 *vop2 = vp->vop2; 1869 struct drm_plane *plane; 1870 u32 layer_sel = 0; 1871 u32 port_sel; 1872 unsigned int nlayer, ofs; 1873 struct drm_display_mode *adjusted_mode; 1874 u16 hsync_len; 1875 u16 hdisplay; 1876 u32 bg_dly; 1877 u32 pre_scan_dly; 1878 int i; 1879 struct vop2_video_port *vp0 = &vop2->vps[0]; 1880 struct vop2_video_port *vp1 = &vop2->vps[1]; 1881 struct vop2_video_port *vp2 = &vop2->vps[2]; 1882 1883 adjusted_mode = &vp->crtc.state->adjusted_mode; 1884 hsync_len = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start; 1885 hdisplay = adjusted_mode->crtc_hdisplay; 1886 1887 bg_dly = vp->data->pre_scan_max_dly[3]; 1888 vop2_writel(vop2, RK3568_VP_BG_MIX_CTRL(vp->id), 1889 FIELD_PREP(RK3568_VP_BG_MIX_CTRL__BG_DLY, bg_dly)); 1890 1891 pre_scan_dly = ((bg_dly + (hdisplay >> 1) - 1) << 16) | hsync_len; 1892 vop2_vp_write(vp, RK3568_VP_PRE_SCAN_HTIMING, pre_scan_dly); 1893 1894 vop2_writel(vop2, RK3568_OVL_CTRL, 0); 1895 port_sel = vop2_readl(vop2, RK3568_OVL_PORT_SEL); 1896 port_sel &= RK3568_OVL_PORT_SEL__SEL_PORT; 1897 1898 if (vp0->nlayers) 1899 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT0_MUX, 1900 vp0->nlayers - 1); 1901 else 1902 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT0_MUX, 8); 1903 1904 if (vp1->nlayers) 1905 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 1906 (vp0->nlayers + vp1->nlayers - 1)); 1907 else 1908 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 8); 1909 1910 if (vp2->nlayers) 1911 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT2_MUX, 1912 (vp2->nlayers + vp1->nlayers + vp0->nlayers - 1)); 1913 else 1914 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SET__PORT1_MUX, 8); 1915 1916 layer_sel = vop2_readl(vop2, RK3568_OVL_LAYER_SEL); 1917 1918 ofs = 0; 1919 for (i = 0; i < vp->id; i++) 1920 ofs += vop2->vps[i].nlayers; 1921 1922 nlayer = 0; 1923 drm_atomic_crtc_for_each_plane(plane, &vp->crtc) { 1924 struct vop2_win *win = to_vop2_win(plane); 1925 1926 switch (win->data->phys_id) { 1927 case ROCKCHIP_VOP2_CLUSTER0: 1928 port_sel &= ~RK3568_OVL_PORT_SEL__CLUSTER0; 1929 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__CLUSTER0, vp->id); 1930 break; 1931 case ROCKCHIP_VOP2_CLUSTER1: 1932 port_sel &= ~RK3568_OVL_PORT_SEL__CLUSTER1; 1933 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__CLUSTER1, vp->id); 1934 break; 1935 case ROCKCHIP_VOP2_ESMART0: 1936 port_sel &= ~RK3568_OVL_PORT_SEL__ESMART0; 1937 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__ESMART0, vp->id); 1938 break; 1939 case ROCKCHIP_VOP2_ESMART1: 1940 port_sel &= ~RK3568_OVL_PORT_SEL__ESMART1; 1941 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__ESMART1, vp->id); 1942 break; 1943 case ROCKCHIP_VOP2_SMART0: 1944 port_sel &= ~RK3568_OVL_PORT_SEL__SMART0; 1945 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__SMART0, vp->id); 1946 break; 1947 case ROCKCHIP_VOP2_SMART1: 1948 port_sel &= ~RK3568_OVL_PORT_SEL__SMART1; 1949 port_sel |= FIELD_PREP(RK3568_OVL_PORT_SEL__SMART1, vp->id); 1950 break; 1951 } 1952 1953 layer_sel &= ~RK3568_OVL_LAYER_SEL__LAYER(plane->state->normalized_zpos + ofs, 1954 0x7); 1955 layer_sel |= RK3568_OVL_LAYER_SEL__LAYER(plane->state->normalized_zpos + ofs, 1956 win->data->layer_sel_id); 1957 nlayer++; 1958 } 1959 1960 /* configure unused layers to 0x5 (reserved) */ 1961 for (; nlayer < vp->nlayers; nlayer++) { 1962 layer_sel &= ~RK3568_OVL_LAYER_SEL__LAYER(nlayer + ofs, 0x7); 1963 layer_sel |= RK3568_OVL_LAYER_SEL__LAYER(nlayer + ofs, 5); 1964 } 1965 1966 vop2_writel(vop2, RK3568_OVL_LAYER_SEL, layer_sel); 1967 vop2_writel(vop2, RK3568_OVL_PORT_SEL, port_sel); 1968 vop2_writel(vop2, RK3568_OVL_CTRL, RK3568_OVL_CTRL__LAYERSEL_REGDONE_IMD); 1969 } 1970 1971 static void vop2_setup_dly_for_windows(struct vop2 *vop2) 1972 { 1973 struct vop2_win *win; 1974 int i = 0; 1975 u32 cdly = 0, sdly = 0; 1976 1977 for (i = 0; i < vop2->data->win_size; i++) { 1978 u32 dly; 1979 1980 win = &vop2->win[i]; 1981 dly = win->delay; 1982 1983 switch (win->data->phys_id) { 1984 case ROCKCHIP_VOP2_CLUSTER0: 1985 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER0_0, dly); 1986 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER0_1, dly); 1987 break; 1988 case ROCKCHIP_VOP2_CLUSTER1: 1989 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER1_0, dly); 1990 cdly |= FIELD_PREP(RK3568_CLUSTER_DLY_NUM__CLUSTER1_1, dly); 1991 break; 1992 case ROCKCHIP_VOP2_ESMART0: 1993 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__ESMART0, dly); 1994 break; 1995 case ROCKCHIP_VOP2_ESMART1: 1996 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__ESMART1, dly); 1997 break; 1998 case ROCKCHIP_VOP2_SMART0: 1999 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__SMART0, dly); 2000 break; 2001 case ROCKCHIP_VOP2_SMART1: 2002 sdly |= FIELD_PREP(RK3568_SMART_DLY_NUM__SMART1, dly); 2003 break; 2004 } 2005 } 2006 2007 vop2_writel(vop2, RK3568_CLUSTER_DLY_NUM, cdly); 2008 vop2_writel(vop2, RK3568_SMART_DLY_NUM, sdly); 2009 } 2010 2011 static void vop2_crtc_atomic_begin(struct drm_crtc *crtc, 2012 struct drm_atomic_state *state) 2013 { 2014 struct vop2_video_port *vp = to_vop2_video_port(crtc); 2015 struct vop2 *vop2 = vp->vop2; 2016 struct drm_plane *plane; 2017 2018 vp->win_mask = 0; 2019 2020 drm_atomic_crtc_for_each_plane(plane, crtc) { 2021 struct vop2_win *win = to_vop2_win(plane); 2022 2023 win->delay = win->data->dly[VOP2_DLY_MODE_DEFAULT]; 2024 2025 vp->win_mask |= BIT(win->data->phys_id); 2026 2027 if (vop2_cluster_window(win)) 2028 vop2_setup_cluster_alpha(vop2, win); 2029 } 2030 2031 if (!vp->win_mask) 2032 return; 2033 2034 vop2_setup_layer_mixer(vp); 2035 vop2_setup_alpha(vp); 2036 vop2_setup_dly_for_windows(vop2); 2037 } 2038 2039 static void vop2_crtc_atomic_flush(struct drm_crtc *crtc, 2040 struct drm_atomic_state *state) 2041 { 2042 struct vop2_video_port *vp = to_vop2_video_port(crtc); 2043 2044 vop2_post_config(crtc); 2045 2046 vop2_cfg_done(vp); 2047 2048 spin_lock_irq(&crtc->dev->event_lock); 2049 2050 if (crtc->state->event) { 2051 WARN_ON(drm_crtc_vblank_get(crtc)); 2052 vp->event = crtc->state->event; 2053 crtc->state->event = NULL; 2054 } 2055 2056 spin_unlock_irq(&crtc->dev->event_lock); 2057 } 2058 2059 static const struct drm_crtc_helper_funcs vop2_crtc_helper_funcs = { 2060 .mode_fixup = vop2_crtc_mode_fixup, 2061 .atomic_check = vop2_crtc_atomic_check, 2062 .atomic_begin = vop2_crtc_atomic_begin, 2063 .atomic_flush = vop2_crtc_atomic_flush, 2064 .atomic_enable = vop2_crtc_atomic_enable, 2065 .atomic_disable = vop2_crtc_atomic_disable, 2066 }; 2067 2068 static void vop2_crtc_reset(struct drm_crtc *crtc) 2069 { 2070 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(crtc->state); 2071 2072 if (crtc->state) { 2073 __drm_atomic_helper_crtc_destroy_state(crtc->state); 2074 kfree(vcstate); 2075 } 2076 2077 vcstate = kzalloc(sizeof(*vcstate), GFP_KERNEL); 2078 if (!vcstate) 2079 return; 2080 2081 crtc->state = &vcstate->base; 2082 crtc->state->crtc = crtc; 2083 } 2084 2085 static struct drm_crtc_state *vop2_crtc_duplicate_state(struct drm_crtc *crtc) 2086 { 2087 struct rockchip_crtc_state *vcstate, *old_vcstate; 2088 2089 old_vcstate = to_rockchip_crtc_state(crtc->state); 2090 2091 vcstate = kmemdup(old_vcstate, sizeof(*old_vcstate), GFP_KERNEL); 2092 if (!vcstate) 2093 return NULL; 2094 2095 __drm_atomic_helper_crtc_duplicate_state(crtc, &vcstate->base); 2096 2097 return &vcstate->base; 2098 } 2099 2100 static void vop2_crtc_destroy_state(struct drm_crtc *crtc, 2101 struct drm_crtc_state *state) 2102 { 2103 struct rockchip_crtc_state *vcstate = to_rockchip_crtc_state(state); 2104 2105 __drm_atomic_helper_crtc_destroy_state(&vcstate->base); 2106 kfree(vcstate); 2107 } 2108 2109 static const struct drm_crtc_funcs vop2_crtc_funcs = { 2110 .set_config = drm_atomic_helper_set_config, 2111 .page_flip = drm_atomic_helper_page_flip, 2112 .destroy = drm_crtc_cleanup, 2113 .reset = vop2_crtc_reset, 2114 .atomic_duplicate_state = vop2_crtc_duplicate_state, 2115 .atomic_destroy_state = vop2_crtc_destroy_state, 2116 .enable_vblank = vop2_crtc_enable_vblank, 2117 .disable_vblank = vop2_crtc_disable_vblank, 2118 }; 2119 2120 static irqreturn_t vop2_isr(int irq, void *data) 2121 { 2122 struct vop2 *vop2 = data; 2123 const struct vop2_data *vop2_data = vop2->data; 2124 u32 axi_irqs[VOP2_SYS_AXI_BUS_NUM]; 2125 int ret = IRQ_NONE; 2126 int i; 2127 2128 /* 2129 * The irq is shared with the iommu. If the runtime-pm state of the 2130 * vop2-device is disabled the irq has to be targeted at the iommu. 2131 */ 2132 if (!pm_runtime_get_if_in_use(vop2->dev)) 2133 return IRQ_NONE; 2134 2135 for (i = 0; i < vop2_data->nr_vps; i++) { 2136 struct vop2_video_port *vp = &vop2->vps[i]; 2137 struct drm_crtc *crtc = &vp->crtc; 2138 u32 irqs; 2139 2140 irqs = vop2_readl(vop2, RK3568_VP_INT_STATUS(vp->id)); 2141 vop2_writel(vop2, RK3568_VP_INT_CLR(vp->id), irqs << 16 | irqs); 2142 2143 if (irqs & VP_INT_DSP_HOLD_VALID) { 2144 complete(&vp->dsp_hold_completion); 2145 ret = IRQ_HANDLED; 2146 } 2147 2148 if (irqs & VP_INT_FS_FIELD) { 2149 drm_crtc_handle_vblank(crtc); 2150 spin_lock(&crtc->dev->event_lock); 2151 if (vp->event) { 2152 u32 val = vop2_readl(vop2, RK3568_REG_CFG_DONE); 2153 2154 if (!(val & BIT(vp->id))) { 2155 drm_crtc_send_vblank_event(crtc, vp->event); 2156 vp->event = NULL; 2157 drm_crtc_vblank_put(crtc); 2158 } 2159 } 2160 spin_unlock(&crtc->dev->event_lock); 2161 2162 ret = IRQ_HANDLED; 2163 } 2164 2165 if (irqs & VP_INT_POST_BUF_EMPTY) { 2166 drm_err_ratelimited(vop2->drm, 2167 "POST_BUF_EMPTY irq err at vp%d\n", 2168 vp->id); 2169 ret = IRQ_HANDLED; 2170 } 2171 } 2172 2173 axi_irqs[0] = vop2_readl(vop2, RK3568_SYS0_INT_STATUS); 2174 vop2_writel(vop2, RK3568_SYS0_INT_CLR, axi_irqs[0] << 16 | axi_irqs[0]); 2175 axi_irqs[1] = vop2_readl(vop2, RK3568_SYS1_INT_STATUS); 2176 vop2_writel(vop2, RK3568_SYS1_INT_CLR, axi_irqs[1] << 16 | axi_irqs[1]); 2177 2178 for (i = 0; i < ARRAY_SIZE(axi_irqs); i++) { 2179 if (axi_irqs[i] & VOP2_INT_BUS_ERRPR) { 2180 drm_err_ratelimited(vop2->drm, "BUS_ERROR irq err\n"); 2181 ret = IRQ_HANDLED; 2182 } 2183 } 2184 2185 pm_runtime_put(vop2->dev); 2186 2187 return ret; 2188 } 2189 2190 static int vop2_plane_init(struct vop2 *vop2, struct vop2_win *win, 2191 unsigned long possible_crtcs) 2192 { 2193 const struct vop2_win_data *win_data = win->data; 2194 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) | 2195 BIT(DRM_MODE_BLEND_PREMULTI) | 2196 BIT(DRM_MODE_BLEND_COVERAGE); 2197 int ret; 2198 2199 ret = drm_universal_plane_init(vop2->drm, &win->base, possible_crtcs, 2200 &vop2_plane_funcs, win_data->formats, 2201 win_data->nformats, 2202 win_data->format_modifiers, 2203 win->type, win_data->name); 2204 if (ret) { 2205 drm_err(vop2->drm, "failed to initialize plane %d\n", ret); 2206 return ret; 2207 } 2208 2209 drm_plane_helper_add(&win->base, &vop2_plane_helper_funcs); 2210 2211 if (win->data->supported_rotations) 2212 drm_plane_create_rotation_property(&win->base, DRM_MODE_ROTATE_0, 2213 DRM_MODE_ROTATE_0 | 2214 win->data->supported_rotations); 2215 drm_plane_create_alpha_property(&win->base); 2216 drm_plane_create_blend_mode_property(&win->base, blend_caps); 2217 drm_plane_create_zpos_property(&win->base, win->win_id, 0, 2218 vop2->registered_num_wins - 1); 2219 2220 return 0; 2221 } 2222 2223 static struct vop2_video_port *find_vp_without_primary(struct vop2 *vop2) 2224 { 2225 int i; 2226 2227 for (i = 0; i < vop2->data->nr_vps; i++) { 2228 struct vop2_video_port *vp = &vop2->vps[i]; 2229 2230 if (!vp->crtc.port) 2231 continue; 2232 if (vp->primary_plane) 2233 continue; 2234 2235 return vp; 2236 } 2237 2238 return NULL; 2239 } 2240 2241 #define NR_LAYERS 6 2242 2243 static int vop2_create_crtc(struct vop2 *vop2) 2244 { 2245 const struct vop2_data *vop2_data = vop2->data; 2246 struct drm_device *drm = vop2->drm; 2247 struct device *dev = vop2->dev; 2248 struct drm_plane *plane; 2249 struct device_node *port; 2250 struct vop2_video_port *vp; 2251 int i, nvp, nvps = 0; 2252 int ret; 2253 2254 for (i = 0; i < vop2_data->nr_vps; i++) { 2255 const struct vop2_video_port_data *vp_data; 2256 struct device_node *np; 2257 char dclk_name[9]; 2258 2259 vp_data = &vop2_data->vp[i]; 2260 vp = &vop2->vps[i]; 2261 vp->vop2 = vop2; 2262 vp->id = vp_data->id; 2263 vp->regs = vp_data->regs; 2264 vp->data = vp_data; 2265 2266 snprintf(dclk_name, sizeof(dclk_name), "dclk_vp%d", vp->id); 2267 vp->dclk = devm_clk_get(vop2->dev, dclk_name); 2268 if (IS_ERR(vp->dclk)) { 2269 drm_err(vop2->drm, "failed to get %s\n", dclk_name); 2270 return PTR_ERR(vp->dclk); 2271 } 2272 2273 np = of_graph_get_remote_node(dev->of_node, i, -1); 2274 if (!np) { 2275 drm_dbg(vop2->drm, "%s: No remote for vp%d\n", __func__, i); 2276 continue; 2277 } 2278 of_node_put(np); 2279 2280 port = of_graph_get_port_by_id(dev->of_node, i); 2281 if (!port) { 2282 drm_err(vop2->drm, "no port node found for video_port%d\n", i); 2283 return -ENOENT; 2284 } 2285 2286 vp->crtc.port = port; 2287 nvps++; 2288 } 2289 2290 nvp = 0; 2291 for (i = 0; i < vop2->registered_num_wins; i++) { 2292 struct vop2_win *win = &vop2->win[i]; 2293 u32 possible_crtcs; 2294 2295 if (vop2->data->soc_id == 3566) { 2296 /* 2297 * On RK3566 these windows don't have an independent 2298 * framebuffer. They share the framebuffer with smart0, 2299 * esmart0 and cluster0 respectively. 2300 */ 2301 switch (win->data->phys_id) { 2302 case ROCKCHIP_VOP2_SMART1: 2303 case ROCKCHIP_VOP2_ESMART1: 2304 case ROCKCHIP_VOP2_CLUSTER1: 2305 continue; 2306 } 2307 } 2308 2309 if (win->type == DRM_PLANE_TYPE_PRIMARY) { 2310 vp = find_vp_without_primary(vop2); 2311 if (vp) { 2312 possible_crtcs = BIT(nvp); 2313 vp->primary_plane = win; 2314 nvp++; 2315 } else { 2316 /* change the unused primary window to overlay window */ 2317 win->type = DRM_PLANE_TYPE_OVERLAY; 2318 } 2319 } 2320 2321 if (win->type == DRM_PLANE_TYPE_OVERLAY) 2322 possible_crtcs = (1 << nvps) - 1; 2323 2324 ret = vop2_plane_init(vop2, win, possible_crtcs); 2325 if (ret) { 2326 drm_err(vop2->drm, "failed to init plane %s: %d\n", 2327 win->data->name, ret); 2328 return ret; 2329 } 2330 } 2331 2332 for (i = 0; i < vop2_data->nr_vps; i++) { 2333 vp = &vop2->vps[i]; 2334 2335 if (!vp->crtc.port) 2336 continue; 2337 2338 plane = &vp->primary_plane->base; 2339 2340 ret = drm_crtc_init_with_planes(drm, &vp->crtc, plane, NULL, 2341 &vop2_crtc_funcs, 2342 "video_port%d", vp->id); 2343 if (ret) { 2344 drm_err(vop2->drm, "crtc init for video_port%d failed\n", i); 2345 return ret; 2346 } 2347 2348 drm_crtc_helper_add(&vp->crtc, &vop2_crtc_helper_funcs); 2349 2350 init_completion(&vp->dsp_hold_completion); 2351 } 2352 2353 /* 2354 * On the VOP2 it's very hard to change the number of layers on a VP 2355 * during runtime, so we distribute the layers equally over the used 2356 * VPs 2357 */ 2358 for (i = 0; i < vop2->data->nr_vps; i++) { 2359 struct vop2_video_port *vp = &vop2->vps[i]; 2360 2361 if (vp->crtc.port) 2362 vp->nlayers = NR_LAYERS / nvps; 2363 } 2364 2365 return 0; 2366 } 2367 2368 static void vop2_destroy_crtc(struct drm_crtc *crtc) 2369 { 2370 of_node_put(crtc->port); 2371 2372 /* 2373 * Destroy CRTC after vop2_plane_destroy() since vop2_disable_plane() 2374 * references the CRTC. 2375 */ 2376 drm_crtc_cleanup(crtc); 2377 } 2378 2379 static struct reg_field vop2_cluster_regs[VOP2_WIN_MAX_REG] = { 2380 [VOP2_WIN_ENABLE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 0, 0), 2381 [VOP2_WIN_FORMAT] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 1, 5), 2382 [VOP2_WIN_RB_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 14, 14), 2383 [VOP2_WIN_DITHER_UP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 18, 18), 2384 [VOP2_WIN_ACT_INFO] = REG_FIELD(RK3568_CLUSTER_WIN_ACT_INFO, 0, 31), 2385 [VOP2_WIN_DSP_INFO] = REG_FIELD(RK3568_CLUSTER_WIN_DSP_INFO, 0, 31), 2386 [VOP2_WIN_DSP_ST] = REG_FIELD(RK3568_CLUSTER_WIN_DSP_ST, 0, 31), 2387 [VOP2_WIN_YRGB_MST] = REG_FIELD(RK3568_CLUSTER_WIN_YRGB_MST, 0, 31), 2388 [VOP2_WIN_UV_MST] = REG_FIELD(RK3568_CLUSTER_WIN_CBR_MST, 0, 31), 2389 [VOP2_WIN_YUV_CLIP] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 19, 19), 2390 [VOP2_WIN_YRGB_VIR] = REG_FIELD(RK3568_CLUSTER_WIN_VIR, 0, 15), 2391 [VOP2_WIN_UV_VIR] = REG_FIELD(RK3568_CLUSTER_WIN_VIR, 16, 31), 2392 [VOP2_WIN_Y2R_EN] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 8, 8), 2393 [VOP2_WIN_R2Y_EN] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 9, 9), 2394 [VOP2_WIN_CSC_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL0, 10, 11), 2395 2396 /* Scale */ 2397 [VOP2_WIN_SCALE_YRGB_X] = REG_FIELD(RK3568_CLUSTER_WIN_SCL_FACTOR_YRGB, 0, 15), 2398 [VOP2_WIN_SCALE_YRGB_Y] = REG_FIELD(RK3568_CLUSTER_WIN_SCL_FACTOR_YRGB, 16, 31), 2399 [VOP2_WIN_YRGB_VER_SCL_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 14, 15), 2400 [VOP2_WIN_YRGB_HOR_SCL_MODE] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 12, 13), 2401 [VOP2_WIN_BIC_COE_SEL] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 2, 3), 2402 [VOP2_WIN_VSD_YRGB_GT2] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 28, 28), 2403 [VOP2_WIN_VSD_YRGB_GT4] = REG_FIELD(RK3568_CLUSTER_WIN_CTRL1, 29, 29), 2404 2405 /* cluster regs */ 2406 [VOP2_WIN_AFBC_ENABLE] = REG_FIELD(RK3568_CLUSTER_CTRL, 1, 1), 2407 [VOP2_WIN_CLUSTER_ENABLE] = REG_FIELD(RK3568_CLUSTER_CTRL, 0, 0), 2408 [VOP2_WIN_CLUSTER_LB_MODE] = REG_FIELD(RK3568_CLUSTER_CTRL, 4, 7), 2409 2410 /* afbc regs */ 2411 [VOP2_WIN_AFBC_FORMAT] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 2, 6), 2412 [VOP2_WIN_AFBC_RB_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 9, 9), 2413 [VOP2_WIN_AFBC_UV_SWAP] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 10, 10), 2414 [VOP2_WIN_AFBC_AUTO_GATING_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_OUTPUT_CTRL, 4, 4), 2415 [VOP2_WIN_AFBC_HALF_BLOCK_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 7, 7), 2416 [VOP2_WIN_AFBC_BLOCK_SPLIT_EN] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_CTRL, 8, 8), 2417 [VOP2_WIN_AFBC_HDR_PTR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_HDR_PTR, 0, 31), 2418 [VOP2_WIN_AFBC_PIC_SIZE] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_PIC_SIZE, 0, 31), 2419 [VOP2_WIN_AFBC_PIC_VIR_WIDTH] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_VIR_WIDTH, 0, 15), 2420 [VOP2_WIN_AFBC_TILE_NUM] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_VIR_WIDTH, 16, 31), 2421 [VOP2_WIN_AFBC_PIC_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_PIC_OFFSET, 0, 31), 2422 [VOP2_WIN_AFBC_DSP_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_DSP_OFFSET, 0, 31), 2423 [VOP2_WIN_AFBC_TRANSFORM_OFFSET] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_TRANSFORM_OFFSET, 0, 31), 2424 [VOP2_WIN_AFBC_ROTATE_90] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 0, 0), 2425 [VOP2_WIN_AFBC_ROTATE_270] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 1, 1), 2426 [VOP2_WIN_XMIRROR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 2, 2), 2427 [VOP2_WIN_YMIRROR] = REG_FIELD(RK3568_CLUSTER_WIN_AFBCD_ROTATE_MODE, 3, 3), 2428 [VOP2_WIN_UV_SWAP] = { .reg = 0xffffffff }, 2429 [VOP2_WIN_COLOR_KEY] = { .reg = 0xffffffff }, 2430 [VOP2_WIN_COLOR_KEY_EN] = { .reg = 0xffffffff }, 2431 [VOP2_WIN_SCALE_CBCR_X] = { .reg = 0xffffffff }, 2432 [VOP2_WIN_SCALE_CBCR_Y] = { .reg = 0xffffffff }, 2433 [VOP2_WIN_YRGB_HSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2434 [VOP2_WIN_YRGB_VSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2435 [VOP2_WIN_CBCR_VER_SCL_MODE] = { .reg = 0xffffffff }, 2436 [VOP2_WIN_CBCR_HSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2437 [VOP2_WIN_CBCR_HOR_SCL_MODE] = { .reg = 0xffffffff }, 2438 [VOP2_WIN_CBCR_VSCL_FILTER_MODE] = { .reg = 0xffffffff }, 2439 [VOP2_WIN_VSD_CBCR_GT2] = { .reg = 0xffffffff }, 2440 [VOP2_WIN_VSD_CBCR_GT4] = { .reg = 0xffffffff }, 2441 }; 2442 2443 static int vop2_cluster_init(struct vop2_win *win) 2444 { 2445 struct vop2 *vop2 = win->vop2; 2446 struct reg_field *cluster_regs; 2447 int ret, i; 2448 2449 cluster_regs = kmemdup(vop2_cluster_regs, sizeof(vop2_cluster_regs), 2450 GFP_KERNEL); 2451 if (!cluster_regs) 2452 return -ENOMEM; 2453 2454 for (i = 0; i < ARRAY_SIZE(vop2_cluster_regs); i++) 2455 if (cluster_regs[i].reg != 0xffffffff) 2456 cluster_regs[i].reg += win->offset; 2457 2458 ret = devm_regmap_field_bulk_alloc(vop2->dev, vop2->map, win->reg, 2459 cluster_regs, 2460 ARRAY_SIZE(vop2_cluster_regs)); 2461 2462 kfree(cluster_regs); 2463 2464 return ret; 2465 }; 2466 2467 static struct reg_field vop2_esmart_regs[VOP2_WIN_MAX_REG] = { 2468 [VOP2_WIN_ENABLE] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 0, 0), 2469 [VOP2_WIN_FORMAT] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 1, 5), 2470 [VOP2_WIN_DITHER_UP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 12, 12), 2471 [VOP2_WIN_RB_SWAP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 14, 14), 2472 [VOP2_WIN_UV_SWAP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 16, 16), 2473 [VOP2_WIN_ACT_INFO] = REG_FIELD(RK3568_SMART_REGION0_ACT_INFO, 0, 31), 2474 [VOP2_WIN_DSP_INFO] = REG_FIELD(RK3568_SMART_REGION0_DSP_INFO, 0, 31), 2475 [VOP2_WIN_DSP_ST] = REG_FIELD(RK3568_SMART_REGION0_DSP_ST, 0, 28), 2476 [VOP2_WIN_YRGB_MST] = REG_FIELD(RK3568_SMART_REGION0_YRGB_MST, 0, 31), 2477 [VOP2_WIN_UV_MST] = REG_FIELD(RK3568_SMART_REGION0_CBR_MST, 0, 31), 2478 [VOP2_WIN_YUV_CLIP] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 17, 17), 2479 [VOP2_WIN_YRGB_VIR] = REG_FIELD(RK3568_SMART_REGION0_VIR, 0, 15), 2480 [VOP2_WIN_UV_VIR] = REG_FIELD(RK3568_SMART_REGION0_VIR, 16, 31), 2481 [VOP2_WIN_Y2R_EN] = REG_FIELD(RK3568_SMART_CTRL0, 0, 0), 2482 [VOP2_WIN_R2Y_EN] = REG_FIELD(RK3568_SMART_CTRL0, 1, 1), 2483 [VOP2_WIN_CSC_MODE] = REG_FIELD(RK3568_SMART_CTRL0, 2, 3), 2484 [VOP2_WIN_YMIRROR] = REG_FIELD(RK3568_SMART_CTRL1, 31, 31), 2485 [VOP2_WIN_COLOR_KEY] = REG_FIELD(RK3568_SMART_COLOR_KEY_CTRL, 0, 29), 2486 [VOP2_WIN_COLOR_KEY_EN] = REG_FIELD(RK3568_SMART_COLOR_KEY_CTRL, 31, 31), 2487 2488 /* Scale */ 2489 [VOP2_WIN_SCALE_YRGB_X] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_YRGB, 0, 15), 2490 [VOP2_WIN_SCALE_YRGB_Y] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_YRGB, 16, 31), 2491 [VOP2_WIN_SCALE_CBCR_X] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_CBR, 0, 15), 2492 [VOP2_WIN_SCALE_CBCR_Y] = REG_FIELD(RK3568_SMART_REGION0_SCL_FACTOR_CBR, 16, 31), 2493 [VOP2_WIN_YRGB_HOR_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 0, 1), 2494 [VOP2_WIN_YRGB_HSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 2, 3), 2495 [VOP2_WIN_YRGB_VER_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 4, 5), 2496 [VOP2_WIN_YRGB_VSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 6, 7), 2497 [VOP2_WIN_CBCR_HOR_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 8, 9), 2498 [VOP2_WIN_CBCR_HSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 10, 11), 2499 [VOP2_WIN_CBCR_VER_SCL_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 12, 13), 2500 [VOP2_WIN_CBCR_VSCL_FILTER_MODE] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 14, 15), 2501 [VOP2_WIN_BIC_COE_SEL] = REG_FIELD(RK3568_SMART_REGION0_SCL_CTRL, 16, 17), 2502 [VOP2_WIN_VSD_YRGB_GT2] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 8, 8), 2503 [VOP2_WIN_VSD_YRGB_GT4] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 9, 9), 2504 [VOP2_WIN_VSD_CBCR_GT2] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 10, 10), 2505 [VOP2_WIN_VSD_CBCR_GT4] = REG_FIELD(RK3568_SMART_REGION0_CTRL, 11, 11), 2506 [VOP2_WIN_XMIRROR] = { .reg = 0xffffffff }, 2507 [VOP2_WIN_CLUSTER_ENABLE] = { .reg = 0xffffffff }, 2508 [VOP2_WIN_AFBC_ENABLE] = { .reg = 0xffffffff }, 2509 [VOP2_WIN_CLUSTER_LB_MODE] = { .reg = 0xffffffff }, 2510 [VOP2_WIN_AFBC_FORMAT] = { .reg = 0xffffffff }, 2511 [VOP2_WIN_AFBC_RB_SWAP] = { .reg = 0xffffffff }, 2512 [VOP2_WIN_AFBC_UV_SWAP] = { .reg = 0xffffffff }, 2513 [VOP2_WIN_AFBC_AUTO_GATING_EN] = { .reg = 0xffffffff }, 2514 [VOP2_WIN_AFBC_BLOCK_SPLIT_EN] = { .reg = 0xffffffff }, 2515 [VOP2_WIN_AFBC_PIC_VIR_WIDTH] = { .reg = 0xffffffff }, 2516 [VOP2_WIN_AFBC_TILE_NUM] = { .reg = 0xffffffff }, 2517 [VOP2_WIN_AFBC_PIC_OFFSET] = { .reg = 0xffffffff }, 2518 [VOP2_WIN_AFBC_PIC_SIZE] = { .reg = 0xffffffff }, 2519 [VOP2_WIN_AFBC_DSP_OFFSET] = { .reg = 0xffffffff }, 2520 [VOP2_WIN_AFBC_TRANSFORM_OFFSET] = { .reg = 0xffffffff }, 2521 [VOP2_WIN_AFBC_HDR_PTR] = { .reg = 0xffffffff }, 2522 [VOP2_WIN_AFBC_HALF_BLOCK_EN] = { .reg = 0xffffffff }, 2523 [VOP2_WIN_AFBC_ROTATE_270] = { .reg = 0xffffffff }, 2524 [VOP2_WIN_AFBC_ROTATE_90] = { .reg = 0xffffffff }, 2525 }; 2526 2527 static int vop2_esmart_init(struct vop2_win *win) 2528 { 2529 struct vop2 *vop2 = win->vop2; 2530 struct reg_field *esmart_regs; 2531 int ret, i; 2532 2533 esmart_regs = kmemdup(vop2_esmart_regs, sizeof(vop2_esmart_regs), 2534 GFP_KERNEL); 2535 if (!esmart_regs) 2536 return -ENOMEM; 2537 2538 for (i = 0; i < ARRAY_SIZE(vop2_esmart_regs); i++) 2539 if (esmart_regs[i].reg != 0xffffffff) 2540 esmart_regs[i].reg += win->offset; 2541 2542 ret = devm_regmap_field_bulk_alloc(vop2->dev, vop2->map, win->reg, 2543 esmart_regs, 2544 ARRAY_SIZE(vop2_esmart_regs)); 2545 2546 kfree(esmart_regs); 2547 2548 return ret; 2549 }; 2550 2551 static int vop2_win_init(struct vop2 *vop2) 2552 { 2553 const struct vop2_data *vop2_data = vop2->data; 2554 struct vop2_win *win; 2555 int i, ret; 2556 2557 for (i = 0; i < vop2_data->win_size; i++) { 2558 const struct vop2_win_data *win_data = &vop2_data->win[i]; 2559 2560 win = &vop2->win[i]; 2561 win->data = win_data; 2562 win->type = win_data->type; 2563 win->offset = win_data->base; 2564 win->win_id = i; 2565 win->vop2 = vop2; 2566 if (vop2_cluster_window(win)) 2567 ret = vop2_cluster_init(win); 2568 else 2569 ret = vop2_esmart_init(win); 2570 if (ret) 2571 return ret; 2572 } 2573 2574 vop2->registered_num_wins = vop2_data->win_size; 2575 2576 return 0; 2577 } 2578 2579 /* 2580 * The window registers are only updated when config done is written. 2581 * Until that they read back the old value. As we read-modify-write 2582 * these registers mark them as non-volatile. This makes sure we read 2583 * the new values from the regmap register cache. 2584 */ 2585 static const struct regmap_range vop2_nonvolatile_range[] = { 2586 regmap_reg_range(0x1000, 0x23ff), 2587 }; 2588 2589 static const struct regmap_access_table vop2_volatile_table = { 2590 .no_ranges = vop2_nonvolatile_range, 2591 .n_no_ranges = ARRAY_SIZE(vop2_nonvolatile_range), 2592 }; 2593 2594 static const struct regmap_config vop2_regmap_config = { 2595 .reg_bits = 32, 2596 .val_bits = 32, 2597 .reg_stride = 4, 2598 .max_register = 0x3000, 2599 .name = "vop2", 2600 .volatile_table = &vop2_volatile_table, 2601 .cache_type = REGCACHE_RBTREE, 2602 }; 2603 2604 static int vop2_bind(struct device *dev, struct device *master, void *data) 2605 { 2606 struct platform_device *pdev = to_platform_device(dev); 2607 const struct vop2_data *vop2_data; 2608 struct drm_device *drm = data; 2609 struct vop2 *vop2; 2610 struct resource *res; 2611 size_t alloc_size; 2612 int ret; 2613 2614 vop2_data = of_device_get_match_data(dev); 2615 if (!vop2_data) 2616 return -ENODEV; 2617 2618 /* Allocate vop2 struct and its vop2_win array */ 2619 alloc_size = sizeof(*vop2) + sizeof(*vop2->win) * vop2_data->win_size; 2620 vop2 = devm_kzalloc(dev, alloc_size, GFP_KERNEL); 2621 if (!vop2) 2622 return -ENOMEM; 2623 2624 vop2->dev = dev; 2625 vop2->data = vop2_data; 2626 vop2->drm = drm; 2627 2628 dev_set_drvdata(dev, vop2); 2629 2630 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vop"); 2631 if (!res) { 2632 drm_err(vop2->drm, "failed to get vop2 register byname\n"); 2633 return -EINVAL; 2634 } 2635 2636 vop2->regs = devm_ioremap_resource(dev, res); 2637 if (IS_ERR(vop2->regs)) 2638 return PTR_ERR(vop2->regs); 2639 vop2->len = resource_size(res); 2640 2641 vop2->map = devm_regmap_init_mmio(dev, vop2->regs, &vop2_regmap_config); 2642 2643 ret = vop2_win_init(vop2); 2644 if (ret) 2645 return ret; 2646 2647 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "gamma-lut"); 2648 if (res) { 2649 vop2->lut_regs = devm_ioremap_resource(dev, res); 2650 if (IS_ERR(vop2->lut_regs)) 2651 return PTR_ERR(vop2->lut_regs); 2652 } 2653 2654 vop2->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf"); 2655 2656 vop2->hclk = devm_clk_get(vop2->dev, "hclk"); 2657 if (IS_ERR(vop2->hclk)) { 2658 drm_err(vop2->drm, "failed to get hclk source\n"); 2659 return PTR_ERR(vop2->hclk); 2660 } 2661 2662 vop2->aclk = devm_clk_get(vop2->dev, "aclk"); 2663 if (IS_ERR(vop2->aclk)) { 2664 drm_err(vop2->drm, "failed to get aclk source\n"); 2665 return PTR_ERR(vop2->aclk); 2666 } 2667 2668 vop2->irq = platform_get_irq(pdev, 0); 2669 if (vop2->irq < 0) { 2670 drm_err(vop2->drm, "cannot find irq for vop2\n"); 2671 return vop2->irq; 2672 } 2673 2674 mutex_init(&vop2->vop2_lock); 2675 2676 ret = devm_request_irq(dev, vop2->irq, vop2_isr, IRQF_SHARED, dev_name(dev), vop2); 2677 if (ret) 2678 return ret; 2679 2680 ret = vop2_create_crtc(vop2); 2681 if (ret) 2682 return ret; 2683 2684 rockchip_drm_dma_init_device(vop2->drm, vop2->dev); 2685 2686 pm_runtime_enable(&pdev->dev); 2687 2688 return 0; 2689 } 2690 2691 static void vop2_unbind(struct device *dev, struct device *master, void *data) 2692 { 2693 struct vop2 *vop2 = dev_get_drvdata(dev); 2694 struct drm_device *drm = vop2->drm; 2695 struct list_head *plane_list = &drm->mode_config.plane_list; 2696 struct list_head *crtc_list = &drm->mode_config.crtc_list; 2697 struct drm_crtc *crtc, *tmpc; 2698 struct drm_plane *plane, *tmpp; 2699 2700 pm_runtime_disable(dev); 2701 2702 list_for_each_entry_safe(plane, tmpp, plane_list, head) 2703 drm_plane_cleanup(plane); 2704 2705 list_for_each_entry_safe(crtc, tmpc, crtc_list, head) 2706 vop2_destroy_crtc(crtc); 2707 } 2708 2709 const struct component_ops vop2_component_ops = { 2710 .bind = vop2_bind, 2711 .unbind = vop2_unbind, 2712 }; 2713 EXPORT_SYMBOL_GPL(vop2_component_ops); 2714