1 /* 2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd 3 * Author:Mark Yao <mark.yao@rock-chips.com> 4 * 5 * This software is licensed under the terms of the GNU General Public 6 * License version 2, as published by the Free Software Foundation, and 7 * may be copied, distributed, and modified under those terms. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <drm/drm.h> 16 #include <drm/drmP.h> 17 #include <drm/drm_atomic.h> 18 #include <drm/drm_crtc.h> 19 #include <drm/drm_crtc_helper.h> 20 #include <drm/drm_flip_work.h> 21 #include <drm/drm_plane_helper.h> 22 #ifdef CONFIG_DRM_ANALOGIX_DP 23 #include <drm/bridge/analogix_dp.h> 24 #endif 25 26 #include <linux/kernel.h> 27 #include <linux/module.h> 28 #include <linux/platform_device.h> 29 #include <linux/clk.h> 30 #include <linux/iopoll.h> 31 #include <linux/of.h> 32 #include <linux/of_device.h> 33 #include <linux/pm_runtime.h> 34 #include <linux/component.h> 35 36 #include <linux/reset.h> 37 #include <linux/delay.h> 38 39 #include "rockchip_drm_drv.h" 40 #include "rockchip_drm_gem.h" 41 #include "rockchip_drm_fb.h" 42 #include "rockchip_drm_psr.h" 43 #include "rockchip_drm_vop.h" 44 45 #define VOP_WIN_SET(x, win, name, v) \ 46 vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name) 47 #define VOP_SCL_SET(x, win, name, v) \ 48 vop_reg_set(vop, &win->phy->scl->name, win->base, ~0, v, #name) 49 #define VOP_SCL_SET_EXT(x, win, name, v) \ 50 vop_reg_set(vop, &win->phy->scl->ext->name, \ 51 win->base, ~0, v, #name) 52 53 #define VOP_INTR_SET_MASK(vop, name, mask, v) \ 54 vop_reg_set(vop, &vop->data->intr->name, 0, mask, v, #name) 55 56 #define VOP_REG_SET(vop, group, name, v) \ 57 vop_reg_set(vop, &vop->data->group->name, 0, ~0, v, #name) 58 59 #define VOP_INTR_SET_TYPE(vop, name, type, v) \ 60 do { \ 61 int i, reg = 0, mask = 0; \ 62 for (i = 0; i < vop->data->intr->nintrs; i++) { \ 63 if (vop->data->intr->intrs[i] & type) { \ 64 reg |= (v) << i; \ 65 mask |= 1 << i; \ 66 } \ 67 } \ 68 VOP_INTR_SET_MASK(vop, name, mask, reg); \ 69 } while (0) 70 #define VOP_INTR_GET_TYPE(vop, name, type) \ 71 vop_get_intr_type(vop, &vop->data->intr->name, type) 72 73 #define VOP_WIN_GET(x, win, name) \ 74 vop_read_reg(x, win->offset, win->phy->name) 75 76 #define VOP_WIN_GET_YRGBADDR(vop, win) \ 77 vop_readl(vop, win->base + win->phy->yrgb_mst.offset) 78 79 #define VOP_WIN_TO_INDEX(vop_win) \ 80 ((vop_win) - (vop_win)->vop->win) 81 82 #define to_vop(x) container_of(x, struct vop, crtc) 83 #define to_vop_win(x) container_of(x, struct vop_win, base) 84 85 enum vop_pending { 86 VOP_PENDING_FB_UNREF, 87 }; 88 89 struct vop_win { 90 struct drm_plane base; 91 const struct vop_win_data *data; 92 struct vop *vop; 93 }; 94 95 struct vop { 96 struct drm_crtc crtc; 97 struct device *dev; 98 struct drm_device *drm_dev; 99 bool is_enabled; 100 101 struct completion dsp_hold_completion; 102 103 /* protected by dev->event_lock */ 104 struct drm_pending_vblank_event *event; 105 106 struct drm_flip_work fb_unref_work; 107 unsigned long pending; 108 109 struct completion line_flag_completion; 110 111 const struct vop_data *data; 112 113 uint32_t *regsbak; 114 void __iomem *regs; 115 116 /* physical map length of vop register */ 117 uint32_t len; 118 119 /* one time only one process allowed to config the register */ 120 spinlock_t reg_lock; 121 /* lock vop irq reg */ 122 spinlock_t irq_lock; 123 /* protects crtc enable/disable */ 124 struct mutex vop_lock; 125 126 unsigned int irq; 127 128 /* vop AHP clk */ 129 struct clk *hclk; 130 /* vop dclk */ 131 struct clk *dclk; 132 /* vop share memory frequency */ 133 struct clk *aclk; 134 135 /* vop dclk reset */ 136 struct reset_control *dclk_rst; 137 138 struct vop_win win[]; 139 }; 140 141 static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v) 142 { 143 writel(v, vop->regs + offset); 144 vop->regsbak[offset >> 2] = v; 145 } 146 147 static inline uint32_t vop_readl(struct vop *vop, uint32_t offset) 148 { 149 return readl(vop->regs + offset); 150 } 151 152 static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base, 153 const struct vop_reg *reg) 154 { 155 return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask; 156 } 157 158 static void vop_reg_set(struct vop *vop, const struct vop_reg *reg, 159 uint32_t _offset, uint32_t _mask, uint32_t v, 160 const char *reg_name) 161 { 162 int offset, mask, shift; 163 164 if (!reg || !reg->mask) { 165 DRM_DEV_DEBUG(vop->dev, "Warning: not support %s\n", reg_name); 166 return; 167 } 168 169 offset = reg->offset + _offset; 170 mask = reg->mask & _mask; 171 shift = reg->shift; 172 173 if (reg->write_mask) { 174 v = ((v << shift) & 0xffff) | (mask << (shift + 16)); 175 } else { 176 uint32_t cached_val = vop->regsbak[offset >> 2]; 177 178 v = (cached_val & ~(mask << shift)) | ((v & mask) << shift); 179 vop->regsbak[offset >> 2] = v; 180 } 181 182 if (reg->relaxed) 183 writel_relaxed(v, vop->regs + offset); 184 else 185 writel(v, vop->regs + offset); 186 } 187 188 static inline uint32_t vop_get_intr_type(struct vop *vop, 189 const struct vop_reg *reg, int type) 190 { 191 uint32_t i, ret = 0; 192 uint32_t regs = vop_read_reg(vop, 0, reg); 193 194 for (i = 0; i < vop->data->intr->nintrs; i++) { 195 if ((type & vop->data->intr->intrs[i]) && (regs & 1 << i)) 196 ret |= vop->data->intr->intrs[i]; 197 } 198 199 return ret; 200 } 201 202 static inline void vop_cfg_done(struct vop *vop) 203 { 204 VOP_REG_SET(vop, common, cfg_done, 1); 205 } 206 207 static bool has_rb_swapped(uint32_t format) 208 { 209 switch (format) { 210 case DRM_FORMAT_XBGR8888: 211 case DRM_FORMAT_ABGR8888: 212 case DRM_FORMAT_BGR888: 213 case DRM_FORMAT_BGR565: 214 return true; 215 default: 216 return false; 217 } 218 } 219 220 static enum vop_data_format vop_convert_format(uint32_t format) 221 { 222 switch (format) { 223 case DRM_FORMAT_XRGB8888: 224 case DRM_FORMAT_ARGB8888: 225 case DRM_FORMAT_XBGR8888: 226 case DRM_FORMAT_ABGR8888: 227 return VOP_FMT_ARGB8888; 228 case DRM_FORMAT_RGB888: 229 case DRM_FORMAT_BGR888: 230 return VOP_FMT_RGB888; 231 case DRM_FORMAT_RGB565: 232 case DRM_FORMAT_BGR565: 233 return VOP_FMT_RGB565; 234 case DRM_FORMAT_NV12: 235 return VOP_FMT_YUV420SP; 236 case DRM_FORMAT_NV16: 237 return VOP_FMT_YUV422SP; 238 case DRM_FORMAT_NV24: 239 return VOP_FMT_YUV444SP; 240 default: 241 DRM_ERROR("unsupported format[%08x]\n", format); 242 return -EINVAL; 243 } 244 } 245 246 static bool is_yuv_support(uint32_t format) 247 { 248 switch (format) { 249 case DRM_FORMAT_NV12: 250 case DRM_FORMAT_NV16: 251 case DRM_FORMAT_NV24: 252 return true; 253 default: 254 return false; 255 } 256 } 257 258 static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src, 259 uint32_t dst, bool is_horizontal, 260 int vsu_mode, int *vskiplines) 261 { 262 uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT; 263 264 if (vskiplines) 265 *vskiplines = 0; 266 267 if (is_horizontal) { 268 if (mode == SCALE_UP) 269 val = GET_SCL_FT_BIC(src, dst); 270 else if (mode == SCALE_DOWN) 271 val = GET_SCL_FT_BILI_DN(src, dst); 272 } else { 273 if (mode == SCALE_UP) { 274 if (vsu_mode == SCALE_UP_BIL) 275 val = GET_SCL_FT_BILI_UP(src, dst); 276 else 277 val = GET_SCL_FT_BIC(src, dst); 278 } else if (mode == SCALE_DOWN) { 279 if (vskiplines) { 280 *vskiplines = scl_get_vskiplines(src, dst); 281 val = scl_get_bili_dn_vskip(src, dst, 282 *vskiplines); 283 } else { 284 val = GET_SCL_FT_BILI_DN(src, dst); 285 } 286 } 287 } 288 289 return val; 290 } 291 292 static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win, 293 uint32_t src_w, uint32_t src_h, uint32_t dst_w, 294 uint32_t dst_h, uint32_t pixel_format) 295 { 296 uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode; 297 uint16_t cbcr_hor_scl_mode = SCALE_NONE; 298 uint16_t cbcr_ver_scl_mode = SCALE_NONE; 299 int hsub = drm_format_horz_chroma_subsampling(pixel_format); 300 int vsub = drm_format_vert_chroma_subsampling(pixel_format); 301 bool is_yuv = is_yuv_support(pixel_format); 302 uint16_t cbcr_src_w = src_w / hsub; 303 uint16_t cbcr_src_h = src_h / vsub; 304 uint16_t vsu_mode; 305 uint16_t lb_mode; 306 uint32_t val; 307 int vskiplines; 308 309 if (dst_w > 3840) { 310 DRM_DEV_ERROR(vop->dev, "Maximum dst width (3840) exceeded\n"); 311 return; 312 } 313 314 if (!win->phy->scl->ext) { 315 VOP_SCL_SET(vop, win, scale_yrgb_x, 316 scl_cal_scale2(src_w, dst_w)); 317 VOP_SCL_SET(vop, win, scale_yrgb_y, 318 scl_cal_scale2(src_h, dst_h)); 319 if (is_yuv) { 320 VOP_SCL_SET(vop, win, scale_cbcr_x, 321 scl_cal_scale2(cbcr_src_w, dst_w)); 322 VOP_SCL_SET(vop, win, scale_cbcr_y, 323 scl_cal_scale2(cbcr_src_h, dst_h)); 324 } 325 return; 326 } 327 328 yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w); 329 yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h); 330 331 if (is_yuv) { 332 cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w); 333 cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h); 334 if (cbcr_hor_scl_mode == SCALE_DOWN) 335 lb_mode = scl_vop_cal_lb_mode(dst_w, true); 336 else 337 lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true); 338 } else { 339 if (yrgb_hor_scl_mode == SCALE_DOWN) 340 lb_mode = scl_vop_cal_lb_mode(dst_w, false); 341 else 342 lb_mode = scl_vop_cal_lb_mode(src_w, false); 343 } 344 345 VOP_SCL_SET_EXT(vop, win, lb_mode, lb_mode); 346 if (lb_mode == LB_RGB_3840X2) { 347 if (yrgb_ver_scl_mode != SCALE_NONE) { 348 DRM_DEV_ERROR(vop->dev, "not allow yrgb ver scale\n"); 349 return; 350 } 351 if (cbcr_ver_scl_mode != SCALE_NONE) { 352 DRM_DEV_ERROR(vop->dev, "not allow cbcr ver scale\n"); 353 return; 354 } 355 vsu_mode = SCALE_UP_BIL; 356 } else if (lb_mode == LB_RGB_2560X4) { 357 vsu_mode = SCALE_UP_BIL; 358 } else { 359 vsu_mode = SCALE_UP_BIC; 360 } 361 362 val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w, 363 true, 0, NULL); 364 VOP_SCL_SET(vop, win, scale_yrgb_x, val); 365 val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h, 366 false, vsu_mode, &vskiplines); 367 VOP_SCL_SET(vop, win, scale_yrgb_y, val); 368 369 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt4, vskiplines == 4); 370 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt2, vskiplines == 2); 371 372 VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode); 373 VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode); 374 VOP_SCL_SET_EXT(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL); 375 VOP_SCL_SET_EXT(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL); 376 VOP_SCL_SET_EXT(vop, win, yrgb_vsu_mode, vsu_mode); 377 if (is_yuv) { 378 val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w, 379 dst_w, true, 0, NULL); 380 VOP_SCL_SET(vop, win, scale_cbcr_x, val); 381 val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h, 382 dst_h, false, vsu_mode, &vskiplines); 383 VOP_SCL_SET(vop, win, scale_cbcr_y, val); 384 385 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt4, vskiplines == 4); 386 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt2, vskiplines == 2); 387 VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode); 388 VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode); 389 VOP_SCL_SET_EXT(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL); 390 VOP_SCL_SET_EXT(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL); 391 VOP_SCL_SET_EXT(vop, win, cbcr_vsu_mode, vsu_mode); 392 } 393 } 394 395 static void vop_dsp_hold_valid_irq_enable(struct vop *vop) 396 { 397 unsigned long flags; 398 399 if (WARN_ON(!vop->is_enabled)) 400 return; 401 402 spin_lock_irqsave(&vop->irq_lock, flags); 403 404 VOP_INTR_SET_TYPE(vop, clear, DSP_HOLD_VALID_INTR, 1); 405 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 1); 406 407 spin_unlock_irqrestore(&vop->irq_lock, flags); 408 } 409 410 static void vop_dsp_hold_valid_irq_disable(struct vop *vop) 411 { 412 unsigned long flags; 413 414 if (WARN_ON(!vop->is_enabled)) 415 return; 416 417 spin_lock_irqsave(&vop->irq_lock, flags); 418 419 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 0); 420 421 spin_unlock_irqrestore(&vop->irq_lock, flags); 422 } 423 424 /* 425 * (1) each frame starts at the start of the Vsync pulse which is signaled by 426 * the "FRAME_SYNC" interrupt. 427 * (2) the active data region of each frame ends at dsp_vact_end 428 * (3) we should program this same number (dsp_vact_end) into dsp_line_frag_num, 429 * to get "LINE_FLAG" interrupt at the end of the active on screen data. 430 * 431 * VOP_INTR_CTRL0.dsp_line_frag_num = VOP_DSP_VACT_ST_END.dsp_vact_end 432 * Interrupts 433 * LINE_FLAG -------------------------------+ 434 * FRAME_SYNC ----+ | 435 * | | 436 * v v 437 * | Vsync | Vbp | Vactive | Vfp | 438 * ^ ^ ^ ^ 439 * | | | | 440 * | | | | 441 * dsp_vs_end ------------+ | | | VOP_DSP_VTOTAL_VS_END 442 * dsp_vact_start --------------+ | | VOP_DSP_VACT_ST_END 443 * dsp_vact_end ----------------------------+ | VOP_DSP_VACT_ST_END 444 * dsp_total -------------------------------------+ VOP_DSP_VTOTAL_VS_END 445 */ 446 static bool vop_line_flag_irq_is_enabled(struct vop *vop) 447 { 448 uint32_t line_flag_irq; 449 unsigned long flags; 450 451 spin_lock_irqsave(&vop->irq_lock, flags); 452 453 line_flag_irq = VOP_INTR_GET_TYPE(vop, enable, LINE_FLAG_INTR); 454 455 spin_unlock_irqrestore(&vop->irq_lock, flags); 456 457 return !!line_flag_irq; 458 } 459 460 static void vop_line_flag_irq_enable(struct vop *vop) 461 { 462 unsigned long flags; 463 464 if (WARN_ON(!vop->is_enabled)) 465 return; 466 467 spin_lock_irqsave(&vop->irq_lock, flags); 468 469 VOP_INTR_SET_TYPE(vop, clear, LINE_FLAG_INTR, 1); 470 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 1); 471 472 spin_unlock_irqrestore(&vop->irq_lock, flags); 473 } 474 475 static void vop_line_flag_irq_disable(struct vop *vop) 476 { 477 unsigned long flags; 478 479 if (WARN_ON(!vop->is_enabled)) 480 return; 481 482 spin_lock_irqsave(&vop->irq_lock, flags); 483 484 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 0); 485 486 spin_unlock_irqrestore(&vop->irq_lock, flags); 487 } 488 489 static int vop_core_clks_enable(struct vop *vop) 490 { 491 int ret; 492 493 ret = clk_enable(vop->hclk); 494 if (ret < 0) 495 return ret; 496 497 ret = clk_enable(vop->aclk); 498 if (ret < 0) 499 goto err_disable_hclk; 500 501 return 0; 502 503 err_disable_hclk: 504 clk_disable(vop->hclk); 505 return ret; 506 } 507 508 static void vop_core_clks_disable(struct vop *vop) 509 { 510 clk_disable(vop->aclk); 511 clk_disable(vop->hclk); 512 } 513 514 static int vop_enable(struct drm_crtc *crtc) 515 { 516 struct vop *vop = to_vop(crtc); 517 int ret, i; 518 519 ret = pm_runtime_get_sync(vop->dev); 520 if (ret < 0) { 521 DRM_DEV_ERROR(vop->dev, "failed to get pm runtime: %d\n", ret); 522 return ret; 523 } 524 525 ret = vop_core_clks_enable(vop); 526 if (WARN_ON(ret < 0)) 527 goto err_put_pm_runtime; 528 529 ret = clk_enable(vop->dclk); 530 if (WARN_ON(ret < 0)) 531 goto err_disable_core; 532 533 /* 534 * Slave iommu shares power, irq and clock with vop. It was associated 535 * automatically with this master device via common driver code. 536 * Now that we have enabled the clock we attach it to the shared drm 537 * mapping. 538 */ 539 ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev); 540 if (ret) { 541 DRM_DEV_ERROR(vop->dev, 542 "failed to attach dma mapping, %d\n", ret); 543 goto err_disable_dclk; 544 } 545 546 spin_lock(&vop->reg_lock); 547 for (i = 0; i < vop->len; i += 4) 548 writel_relaxed(vop->regsbak[i / 4], vop->regs + i); 549 550 /* 551 * We need to make sure that all windows are disabled before we 552 * enable the crtc. Otherwise we might try to scan from a destroyed 553 * buffer later. 554 */ 555 for (i = 0; i < vop->data->win_size; i++) { 556 struct vop_win *vop_win = &vop->win[i]; 557 const struct vop_win_data *win = vop_win->data; 558 559 VOP_WIN_SET(vop, win, enable, 0); 560 } 561 spin_unlock(&vop->reg_lock); 562 563 vop_cfg_done(vop); 564 565 /* 566 * At here, vop clock & iommu is enable, R/W vop regs would be safe. 567 */ 568 vop->is_enabled = true; 569 570 spin_lock(&vop->reg_lock); 571 572 VOP_REG_SET(vop, common, standby, 1); 573 574 spin_unlock(&vop->reg_lock); 575 576 drm_crtc_vblank_on(crtc); 577 578 return 0; 579 580 err_disable_dclk: 581 clk_disable(vop->dclk); 582 err_disable_core: 583 vop_core_clks_disable(vop); 584 err_put_pm_runtime: 585 pm_runtime_put_sync(vop->dev); 586 return ret; 587 } 588 589 static void vop_crtc_atomic_disable(struct drm_crtc *crtc, 590 struct drm_crtc_state *old_state) 591 { 592 struct vop *vop = to_vop(crtc); 593 594 WARN_ON(vop->event); 595 596 mutex_lock(&vop->vop_lock); 597 drm_crtc_vblank_off(crtc); 598 599 /* 600 * Vop standby will take effect at end of current frame, 601 * if dsp hold valid irq happen, it means standby complete. 602 * 603 * we must wait standby complete when we want to disable aclk, 604 * if not, memory bus maybe dead. 605 */ 606 reinit_completion(&vop->dsp_hold_completion); 607 vop_dsp_hold_valid_irq_enable(vop); 608 609 spin_lock(&vop->reg_lock); 610 611 VOP_REG_SET(vop, common, standby, 1); 612 613 spin_unlock(&vop->reg_lock); 614 615 wait_for_completion(&vop->dsp_hold_completion); 616 617 vop_dsp_hold_valid_irq_disable(vop); 618 619 vop->is_enabled = false; 620 621 /* 622 * vop standby complete, so iommu detach is safe. 623 */ 624 rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev); 625 626 clk_disable(vop->dclk); 627 vop_core_clks_disable(vop); 628 pm_runtime_put(vop->dev); 629 mutex_unlock(&vop->vop_lock); 630 631 if (crtc->state->event && !crtc->state->active) { 632 spin_lock_irq(&crtc->dev->event_lock); 633 drm_crtc_send_vblank_event(crtc, crtc->state->event); 634 spin_unlock_irq(&crtc->dev->event_lock); 635 636 crtc->state->event = NULL; 637 } 638 } 639 640 static void vop_plane_destroy(struct drm_plane *plane) 641 { 642 drm_plane_cleanup(plane); 643 } 644 645 static int vop_plane_atomic_check(struct drm_plane *plane, 646 struct drm_plane_state *state) 647 { 648 struct drm_crtc *crtc = state->crtc; 649 struct drm_crtc_state *crtc_state; 650 struct drm_framebuffer *fb = state->fb; 651 struct vop_win *vop_win = to_vop_win(plane); 652 const struct vop_win_data *win = vop_win->data; 653 int ret; 654 int min_scale = win->phy->scl ? FRAC_16_16(1, 8) : 655 DRM_PLANE_HELPER_NO_SCALING; 656 int max_scale = win->phy->scl ? FRAC_16_16(8, 1) : 657 DRM_PLANE_HELPER_NO_SCALING; 658 659 if (!crtc || !fb) 660 return 0; 661 662 crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc); 663 if (WARN_ON(!crtc_state)) 664 return -EINVAL; 665 666 ret = drm_atomic_helper_check_plane_state(state, crtc_state, 667 min_scale, max_scale, 668 true, true); 669 if (ret) 670 return ret; 671 672 if (!state->visible) 673 return 0; 674 675 ret = vop_convert_format(fb->format->format); 676 if (ret < 0) 677 return ret; 678 679 /* 680 * Src.x1 can be odd when do clip, but yuv plane start point 681 * need align with 2 pixel. 682 */ 683 if (is_yuv_support(fb->format->format) && ((state->src.x1 >> 16) % 2)) { 684 DRM_ERROR("Invalid Source: Yuv format not support odd xpos\n"); 685 return -EINVAL; 686 } 687 688 return 0; 689 } 690 691 static void vop_plane_atomic_disable(struct drm_plane *plane, 692 struct drm_plane_state *old_state) 693 { 694 struct vop_win *vop_win = to_vop_win(plane); 695 const struct vop_win_data *win = vop_win->data; 696 struct vop *vop = to_vop(old_state->crtc); 697 698 if (!old_state->crtc) 699 return; 700 701 spin_lock(&vop->reg_lock); 702 703 VOP_WIN_SET(vop, win, enable, 0); 704 705 spin_unlock(&vop->reg_lock); 706 } 707 708 static void vop_plane_atomic_update(struct drm_plane *plane, 709 struct drm_plane_state *old_state) 710 { 711 struct drm_plane_state *state = plane->state; 712 struct drm_crtc *crtc = state->crtc; 713 struct vop_win *vop_win = to_vop_win(plane); 714 const struct vop_win_data *win = vop_win->data; 715 struct vop *vop = to_vop(state->crtc); 716 struct drm_framebuffer *fb = state->fb; 717 unsigned int actual_w, actual_h; 718 unsigned int dsp_stx, dsp_sty; 719 uint32_t act_info, dsp_info, dsp_st; 720 struct drm_rect *src = &state->src; 721 struct drm_rect *dest = &state->dst; 722 struct drm_gem_object *obj, *uv_obj; 723 struct rockchip_gem_object *rk_obj, *rk_uv_obj; 724 unsigned long offset; 725 dma_addr_t dma_addr; 726 uint32_t val; 727 bool rb_swap; 728 int win_index = VOP_WIN_TO_INDEX(vop_win); 729 int format; 730 731 /* 732 * can't update plane when vop is disabled. 733 */ 734 if (WARN_ON(!crtc)) 735 return; 736 737 if (WARN_ON(!vop->is_enabled)) 738 return; 739 740 if (!state->visible) { 741 vop_plane_atomic_disable(plane, old_state); 742 return; 743 } 744 745 obj = fb->obj[0]; 746 rk_obj = to_rockchip_obj(obj); 747 748 actual_w = drm_rect_width(src) >> 16; 749 actual_h = drm_rect_height(src) >> 16; 750 act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff); 751 752 dsp_info = (drm_rect_height(dest) - 1) << 16; 753 dsp_info |= (drm_rect_width(dest) - 1) & 0xffff; 754 755 dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start; 756 dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start; 757 dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff); 758 759 offset = (src->x1 >> 16) * fb->format->cpp[0]; 760 offset += (src->y1 >> 16) * fb->pitches[0]; 761 dma_addr = rk_obj->dma_addr + offset + fb->offsets[0]; 762 763 format = vop_convert_format(fb->format->format); 764 765 spin_lock(&vop->reg_lock); 766 767 VOP_WIN_SET(vop, win, format, format); 768 VOP_WIN_SET(vop, win, yrgb_vir, DIV_ROUND_UP(fb->pitches[0], 4)); 769 VOP_WIN_SET(vop, win, yrgb_mst, dma_addr); 770 if (is_yuv_support(fb->format->format)) { 771 int hsub = drm_format_horz_chroma_subsampling(fb->format->format); 772 int vsub = drm_format_vert_chroma_subsampling(fb->format->format); 773 int bpp = fb->format->cpp[1]; 774 775 uv_obj = fb->obj[1]; 776 rk_uv_obj = to_rockchip_obj(uv_obj); 777 778 offset = (src->x1 >> 16) * bpp / hsub; 779 offset += (src->y1 >> 16) * fb->pitches[1] / vsub; 780 781 dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1]; 782 VOP_WIN_SET(vop, win, uv_vir, DIV_ROUND_UP(fb->pitches[1], 4)); 783 VOP_WIN_SET(vop, win, uv_mst, dma_addr); 784 } 785 786 if (win->phy->scl) 787 scl_vop_cal_scl_fac(vop, win, actual_w, actual_h, 788 drm_rect_width(dest), drm_rect_height(dest), 789 fb->format->format); 790 791 VOP_WIN_SET(vop, win, act_info, act_info); 792 VOP_WIN_SET(vop, win, dsp_info, dsp_info); 793 VOP_WIN_SET(vop, win, dsp_st, dsp_st); 794 795 rb_swap = has_rb_swapped(fb->format->format); 796 VOP_WIN_SET(vop, win, rb_swap, rb_swap); 797 798 /* 799 * Blending win0 with the background color doesn't seem to work 800 * correctly. We only get the background color, no matter the contents 801 * of the win0 framebuffer. However, blending pre-multiplied color 802 * with the default opaque black default background color is a no-op, 803 * so we can just disable blending to get the correct result. 804 */ 805 if (fb->format->has_alpha && win_index > 0) { 806 VOP_WIN_SET(vop, win, dst_alpha_ctl, 807 DST_FACTOR_M0(ALPHA_SRC_INVERSE)); 808 val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) | 809 SRC_ALPHA_M0(ALPHA_STRAIGHT) | 810 SRC_BLEND_M0(ALPHA_PER_PIX) | 811 SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) | 812 SRC_FACTOR_M0(ALPHA_ONE); 813 VOP_WIN_SET(vop, win, src_alpha_ctl, val); 814 } else { 815 VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0)); 816 } 817 818 VOP_WIN_SET(vop, win, enable, 1); 819 spin_unlock(&vop->reg_lock); 820 } 821 822 static const struct drm_plane_helper_funcs plane_helper_funcs = { 823 .atomic_check = vop_plane_atomic_check, 824 .atomic_update = vop_plane_atomic_update, 825 .atomic_disable = vop_plane_atomic_disable, 826 }; 827 828 static const struct drm_plane_funcs vop_plane_funcs = { 829 .update_plane = drm_atomic_helper_update_plane, 830 .disable_plane = drm_atomic_helper_disable_plane, 831 .destroy = vop_plane_destroy, 832 .reset = drm_atomic_helper_plane_reset, 833 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 834 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 835 }; 836 837 static int vop_crtc_enable_vblank(struct drm_crtc *crtc) 838 { 839 struct vop *vop = to_vop(crtc); 840 unsigned long flags; 841 842 if (WARN_ON(!vop->is_enabled)) 843 return -EPERM; 844 845 spin_lock_irqsave(&vop->irq_lock, flags); 846 847 VOP_INTR_SET_TYPE(vop, clear, FS_INTR, 1); 848 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 1); 849 850 spin_unlock_irqrestore(&vop->irq_lock, flags); 851 852 return 0; 853 } 854 855 static void vop_crtc_disable_vblank(struct drm_crtc *crtc) 856 { 857 struct vop *vop = to_vop(crtc); 858 unsigned long flags; 859 860 if (WARN_ON(!vop->is_enabled)) 861 return; 862 863 spin_lock_irqsave(&vop->irq_lock, flags); 864 865 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 0); 866 867 spin_unlock_irqrestore(&vop->irq_lock, flags); 868 } 869 870 static bool vop_crtc_mode_fixup(struct drm_crtc *crtc, 871 const struct drm_display_mode *mode, 872 struct drm_display_mode *adjusted_mode) 873 { 874 struct vop *vop = to_vop(crtc); 875 876 adjusted_mode->clock = 877 clk_round_rate(vop->dclk, mode->clock * 1000) / 1000; 878 879 return true; 880 } 881 882 static void vop_crtc_atomic_enable(struct drm_crtc *crtc, 883 struct drm_crtc_state *old_state) 884 { 885 struct vop *vop = to_vop(crtc); 886 const struct vop_data *vop_data = vop->data; 887 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc->state); 888 struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode; 889 u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start; 890 u16 hdisplay = adjusted_mode->hdisplay; 891 u16 htotal = adjusted_mode->htotal; 892 u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start; 893 u16 hact_end = hact_st + hdisplay; 894 u16 vdisplay = adjusted_mode->vdisplay; 895 u16 vtotal = adjusted_mode->vtotal; 896 u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start; 897 u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start; 898 u16 vact_end = vact_st + vdisplay; 899 uint32_t pin_pol, val; 900 int ret; 901 902 mutex_lock(&vop->vop_lock); 903 904 WARN_ON(vop->event); 905 906 ret = vop_enable(crtc); 907 if (ret) { 908 mutex_unlock(&vop->vop_lock); 909 DRM_DEV_ERROR(vop->dev, "Failed to enable vop (%d)\n", ret); 910 return; 911 } 912 913 pin_pol = BIT(DCLK_INVERT); 914 pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC) ? 915 BIT(HSYNC_POSITIVE) : 0; 916 pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC) ? 917 BIT(VSYNC_POSITIVE) : 0; 918 VOP_REG_SET(vop, output, pin_pol, pin_pol); 919 920 switch (s->output_type) { 921 case DRM_MODE_CONNECTOR_LVDS: 922 VOP_REG_SET(vop, output, rgb_en, 1); 923 VOP_REG_SET(vop, output, rgb_pin_pol, pin_pol); 924 break; 925 case DRM_MODE_CONNECTOR_eDP: 926 VOP_REG_SET(vop, output, edp_pin_pol, pin_pol); 927 VOP_REG_SET(vop, output, edp_en, 1); 928 break; 929 case DRM_MODE_CONNECTOR_HDMIA: 930 VOP_REG_SET(vop, output, hdmi_pin_pol, pin_pol); 931 VOP_REG_SET(vop, output, hdmi_en, 1); 932 break; 933 case DRM_MODE_CONNECTOR_DSI: 934 VOP_REG_SET(vop, output, mipi_pin_pol, pin_pol); 935 VOP_REG_SET(vop, output, mipi_en, 1); 936 break; 937 case DRM_MODE_CONNECTOR_DisplayPort: 938 pin_pol &= ~BIT(DCLK_INVERT); 939 VOP_REG_SET(vop, output, dp_pin_pol, pin_pol); 940 VOP_REG_SET(vop, output, dp_en, 1); 941 break; 942 default: 943 DRM_DEV_ERROR(vop->dev, "unsupported connector_type [%d]\n", 944 s->output_type); 945 } 946 947 /* 948 * if vop is not support RGB10 output, need force RGB10 to RGB888. 949 */ 950 if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA && 951 !(vop_data->feature & VOP_FEATURE_OUTPUT_RGB10)) 952 s->output_mode = ROCKCHIP_OUT_MODE_P888; 953 954 if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA && s->output_bpc == 8) 955 VOP_REG_SET(vop, common, pre_dither_down, 1); 956 else 957 VOP_REG_SET(vop, common, pre_dither_down, 0); 958 959 VOP_REG_SET(vop, common, out_mode, s->output_mode); 960 961 VOP_REG_SET(vop, modeset, htotal_pw, (htotal << 16) | hsync_len); 962 val = hact_st << 16; 963 val |= hact_end; 964 VOP_REG_SET(vop, modeset, hact_st_end, val); 965 VOP_REG_SET(vop, modeset, hpost_st_end, val); 966 967 VOP_REG_SET(vop, modeset, vtotal_pw, (vtotal << 16) | vsync_len); 968 val = vact_st << 16; 969 val |= vact_end; 970 VOP_REG_SET(vop, modeset, vact_st_end, val); 971 VOP_REG_SET(vop, modeset, vpost_st_end, val); 972 973 VOP_REG_SET(vop, intr, line_flag_num[0], vact_end); 974 975 clk_set_rate(vop->dclk, adjusted_mode->clock * 1000); 976 977 VOP_REG_SET(vop, common, standby, 0); 978 mutex_unlock(&vop->vop_lock); 979 } 980 981 static bool vop_fs_irq_is_pending(struct vop *vop) 982 { 983 return VOP_INTR_GET_TYPE(vop, status, FS_INTR); 984 } 985 986 static void vop_wait_for_irq_handler(struct vop *vop) 987 { 988 bool pending; 989 int ret; 990 991 /* 992 * Spin until frame start interrupt status bit goes low, which means 993 * that interrupt handler was invoked and cleared it. The timeout of 994 * 10 msecs is really too long, but it is just a safety measure if 995 * something goes really wrong. The wait will only happen in the very 996 * unlikely case of a vblank happening exactly at the same time and 997 * shouldn't exceed microseconds range. 998 */ 999 ret = readx_poll_timeout_atomic(vop_fs_irq_is_pending, vop, pending, 1000 !pending, 0, 10 * 1000); 1001 if (ret) 1002 DRM_DEV_ERROR(vop->dev, "VOP vblank IRQ stuck for 10 ms\n"); 1003 1004 synchronize_irq(vop->irq); 1005 } 1006 1007 static void vop_crtc_atomic_flush(struct drm_crtc *crtc, 1008 struct drm_crtc_state *old_crtc_state) 1009 { 1010 struct drm_atomic_state *old_state = old_crtc_state->state; 1011 struct drm_plane_state *old_plane_state, *new_plane_state; 1012 struct vop *vop = to_vop(crtc); 1013 struct drm_plane *plane; 1014 int i; 1015 1016 if (WARN_ON(!vop->is_enabled)) 1017 return; 1018 1019 spin_lock(&vop->reg_lock); 1020 1021 vop_cfg_done(vop); 1022 1023 spin_unlock(&vop->reg_lock); 1024 1025 /* 1026 * There is a (rather unlikely) possiblity that a vblank interrupt 1027 * fired before we set the cfg_done bit. To avoid spuriously 1028 * signalling flip completion we need to wait for it to finish. 1029 */ 1030 vop_wait_for_irq_handler(vop); 1031 1032 spin_lock_irq(&crtc->dev->event_lock); 1033 if (crtc->state->event) { 1034 WARN_ON(drm_crtc_vblank_get(crtc) != 0); 1035 WARN_ON(vop->event); 1036 1037 vop->event = crtc->state->event; 1038 crtc->state->event = NULL; 1039 } 1040 spin_unlock_irq(&crtc->dev->event_lock); 1041 1042 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, 1043 new_plane_state, i) { 1044 if (!old_plane_state->fb) 1045 continue; 1046 1047 if (old_plane_state->fb == new_plane_state->fb) 1048 continue; 1049 1050 drm_framebuffer_get(old_plane_state->fb); 1051 WARN_ON(drm_crtc_vblank_get(crtc) != 0); 1052 drm_flip_work_queue(&vop->fb_unref_work, old_plane_state->fb); 1053 set_bit(VOP_PENDING_FB_UNREF, &vop->pending); 1054 } 1055 } 1056 1057 static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = { 1058 .mode_fixup = vop_crtc_mode_fixup, 1059 .atomic_flush = vop_crtc_atomic_flush, 1060 .atomic_enable = vop_crtc_atomic_enable, 1061 .atomic_disable = vop_crtc_atomic_disable, 1062 }; 1063 1064 static void vop_crtc_destroy(struct drm_crtc *crtc) 1065 { 1066 drm_crtc_cleanup(crtc); 1067 } 1068 1069 static void vop_crtc_reset(struct drm_crtc *crtc) 1070 { 1071 if (crtc->state) 1072 __drm_atomic_helper_crtc_destroy_state(crtc->state); 1073 kfree(crtc->state); 1074 1075 crtc->state = kzalloc(sizeof(struct rockchip_crtc_state), GFP_KERNEL); 1076 if (crtc->state) 1077 crtc->state->crtc = crtc; 1078 } 1079 1080 static struct drm_crtc_state *vop_crtc_duplicate_state(struct drm_crtc *crtc) 1081 { 1082 struct rockchip_crtc_state *rockchip_state; 1083 1084 rockchip_state = kzalloc(sizeof(*rockchip_state), GFP_KERNEL); 1085 if (!rockchip_state) 1086 return NULL; 1087 1088 __drm_atomic_helper_crtc_duplicate_state(crtc, &rockchip_state->base); 1089 return &rockchip_state->base; 1090 } 1091 1092 static void vop_crtc_destroy_state(struct drm_crtc *crtc, 1093 struct drm_crtc_state *state) 1094 { 1095 struct rockchip_crtc_state *s = to_rockchip_crtc_state(state); 1096 1097 __drm_atomic_helper_crtc_destroy_state(&s->base); 1098 kfree(s); 1099 } 1100 1101 #ifdef CONFIG_DRM_ANALOGIX_DP 1102 static struct drm_connector *vop_get_edp_connector(struct vop *vop) 1103 { 1104 struct drm_connector *connector; 1105 struct drm_connector_list_iter conn_iter; 1106 1107 drm_connector_list_iter_begin(vop->drm_dev, &conn_iter); 1108 drm_for_each_connector_iter(connector, &conn_iter) { 1109 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) { 1110 drm_connector_list_iter_end(&conn_iter); 1111 return connector; 1112 } 1113 } 1114 drm_connector_list_iter_end(&conn_iter); 1115 1116 return NULL; 1117 } 1118 1119 static int vop_crtc_set_crc_source(struct drm_crtc *crtc, 1120 const char *source_name, size_t *values_cnt) 1121 { 1122 struct vop *vop = to_vop(crtc); 1123 struct drm_connector *connector; 1124 int ret; 1125 1126 connector = vop_get_edp_connector(vop); 1127 if (!connector) 1128 return -EINVAL; 1129 1130 *values_cnt = 3; 1131 1132 if (source_name && strcmp(source_name, "auto") == 0) 1133 ret = analogix_dp_start_crc(connector); 1134 else if (!source_name) 1135 ret = analogix_dp_stop_crc(connector); 1136 else 1137 ret = -EINVAL; 1138 1139 return ret; 1140 } 1141 #else 1142 static int vop_crtc_set_crc_source(struct drm_crtc *crtc, 1143 const char *source_name, size_t *values_cnt) 1144 { 1145 return -ENODEV; 1146 } 1147 #endif 1148 1149 static const struct drm_crtc_funcs vop_crtc_funcs = { 1150 .set_config = drm_atomic_helper_set_config, 1151 .page_flip = drm_atomic_helper_page_flip, 1152 .destroy = vop_crtc_destroy, 1153 .reset = vop_crtc_reset, 1154 .atomic_duplicate_state = vop_crtc_duplicate_state, 1155 .atomic_destroy_state = vop_crtc_destroy_state, 1156 .enable_vblank = vop_crtc_enable_vblank, 1157 .disable_vblank = vop_crtc_disable_vblank, 1158 .set_crc_source = vop_crtc_set_crc_source, 1159 }; 1160 1161 static void vop_fb_unref_worker(struct drm_flip_work *work, void *val) 1162 { 1163 struct vop *vop = container_of(work, struct vop, fb_unref_work); 1164 struct drm_framebuffer *fb = val; 1165 1166 drm_crtc_vblank_put(&vop->crtc); 1167 drm_framebuffer_put(fb); 1168 } 1169 1170 static void vop_handle_vblank(struct vop *vop) 1171 { 1172 struct drm_device *drm = vop->drm_dev; 1173 struct drm_crtc *crtc = &vop->crtc; 1174 1175 spin_lock(&drm->event_lock); 1176 if (vop->event) { 1177 drm_crtc_send_vblank_event(crtc, vop->event); 1178 drm_crtc_vblank_put(crtc); 1179 vop->event = NULL; 1180 } 1181 spin_unlock(&drm->event_lock); 1182 1183 if (test_and_clear_bit(VOP_PENDING_FB_UNREF, &vop->pending)) 1184 drm_flip_work_commit(&vop->fb_unref_work, system_unbound_wq); 1185 } 1186 1187 static irqreturn_t vop_isr(int irq, void *data) 1188 { 1189 struct vop *vop = data; 1190 struct drm_crtc *crtc = &vop->crtc; 1191 uint32_t active_irqs; 1192 int ret = IRQ_NONE; 1193 1194 /* 1195 * The irq is shared with the iommu. If the runtime-pm state of the 1196 * vop-device is disabled the irq has to be targeted at the iommu. 1197 */ 1198 if (!pm_runtime_get_if_in_use(vop->dev)) 1199 return IRQ_NONE; 1200 1201 if (vop_core_clks_enable(vop)) { 1202 DRM_DEV_ERROR_RATELIMITED(vop->dev, "couldn't enable clocks\n"); 1203 goto out; 1204 } 1205 1206 /* 1207 * interrupt register has interrupt status, enable and clear bits, we 1208 * must hold irq_lock to avoid a race with enable/disable_vblank(). 1209 */ 1210 spin_lock(&vop->irq_lock); 1211 1212 active_irqs = VOP_INTR_GET_TYPE(vop, status, INTR_MASK); 1213 /* Clear all active interrupt sources */ 1214 if (active_irqs) 1215 VOP_INTR_SET_TYPE(vop, clear, active_irqs, 1); 1216 1217 spin_unlock(&vop->irq_lock); 1218 1219 /* This is expected for vop iommu irqs, since the irq is shared */ 1220 if (!active_irqs) 1221 goto out_disable; 1222 1223 if (active_irqs & DSP_HOLD_VALID_INTR) { 1224 complete(&vop->dsp_hold_completion); 1225 active_irqs &= ~DSP_HOLD_VALID_INTR; 1226 ret = IRQ_HANDLED; 1227 } 1228 1229 if (active_irqs & LINE_FLAG_INTR) { 1230 complete(&vop->line_flag_completion); 1231 active_irqs &= ~LINE_FLAG_INTR; 1232 ret = IRQ_HANDLED; 1233 } 1234 1235 if (active_irqs & FS_INTR) { 1236 drm_crtc_handle_vblank(crtc); 1237 vop_handle_vblank(vop); 1238 active_irqs &= ~FS_INTR; 1239 ret = IRQ_HANDLED; 1240 } 1241 1242 /* Unhandled irqs are spurious. */ 1243 if (active_irqs) 1244 DRM_DEV_ERROR(vop->dev, "Unknown VOP IRQs: %#02x\n", 1245 active_irqs); 1246 1247 out_disable: 1248 vop_core_clks_disable(vop); 1249 out: 1250 pm_runtime_put(vop->dev); 1251 return ret; 1252 } 1253 1254 static int vop_create_crtc(struct vop *vop) 1255 { 1256 const struct vop_data *vop_data = vop->data; 1257 struct device *dev = vop->dev; 1258 struct drm_device *drm_dev = vop->drm_dev; 1259 struct drm_plane *primary = NULL, *cursor = NULL, *plane, *tmp; 1260 struct drm_crtc *crtc = &vop->crtc; 1261 struct device_node *port; 1262 int ret; 1263 int i; 1264 1265 /* 1266 * Create drm_plane for primary and cursor planes first, since we need 1267 * to pass them to drm_crtc_init_with_planes, which sets the 1268 * "possible_crtcs" to the newly initialized crtc. 1269 */ 1270 for (i = 0; i < vop_data->win_size; i++) { 1271 struct vop_win *vop_win = &vop->win[i]; 1272 const struct vop_win_data *win_data = vop_win->data; 1273 1274 if (win_data->type != DRM_PLANE_TYPE_PRIMARY && 1275 win_data->type != DRM_PLANE_TYPE_CURSOR) 1276 continue; 1277 1278 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base, 1279 0, &vop_plane_funcs, 1280 win_data->phy->data_formats, 1281 win_data->phy->nformats, 1282 NULL, win_data->type, NULL); 1283 if (ret) { 1284 DRM_DEV_ERROR(vop->dev, "failed to init plane %d\n", 1285 ret); 1286 goto err_cleanup_planes; 1287 } 1288 1289 plane = &vop_win->base; 1290 drm_plane_helper_add(plane, &plane_helper_funcs); 1291 if (plane->type == DRM_PLANE_TYPE_PRIMARY) 1292 primary = plane; 1293 else if (plane->type == DRM_PLANE_TYPE_CURSOR) 1294 cursor = plane; 1295 } 1296 1297 ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor, 1298 &vop_crtc_funcs, NULL); 1299 if (ret) 1300 goto err_cleanup_planes; 1301 1302 drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs); 1303 1304 /* 1305 * Create drm_planes for overlay windows with possible_crtcs restricted 1306 * to the newly created crtc. 1307 */ 1308 for (i = 0; i < vop_data->win_size; i++) { 1309 struct vop_win *vop_win = &vop->win[i]; 1310 const struct vop_win_data *win_data = vop_win->data; 1311 unsigned long possible_crtcs = drm_crtc_mask(crtc); 1312 1313 if (win_data->type != DRM_PLANE_TYPE_OVERLAY) 1314 continue; 1315 1316 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base, 1317 possible_crtcs, 1318 &vop_plane_funcs, 1319 win_data->phy->data_formats, 1320 win_data->phy->nformats, 1321 NULL, win_data->type, NULL); 1322 if (ret) { 1323 DRM_DEV_ERROR(vop->dev, "failed to init overlay %d\n", 1324 ret); 1325 goto err_cleanup_crtc; 1326 } 1327 drm_plane_helper_add(&vop_win->base, &plane_helper_funcs); 1328 } 1329 1330 port = of_get_child_by_name(dev->of_node, "port"); 1331 if (!port) { 1332 DRM_DEV_ERROR(vop->dev, "no port node found in %pOF\n", 1333 dev->of_node); 1334 ret = -ENOENT; 1335 goto err_cleanup_crtc; 1336 } 1337 1338 drm_flip_work_init(&vop->fb_unref_work, "fb_unref", 1339 vop_fb_unref_worker); 1340 1341 init_completion(&vop->dsp_hold_completion); 1342 init_completion(&vop->line_flag_completion); 1343 crtc->port = port; 1344 1345 return 0; 1346 1347 err_cleanup_crtc: 1348 drm_crtc_cleanup(crtc); 1349 err_cleanup_planes: 1350 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list, 1351 head) 1352 drm_plane_cleanup(plane); 1353 return ret; 1354 } 1355 1356 static void vop_destroy_crtc(struct vop *vop) 1357 { 1358 struct drm_crtc *crtc = &vop->crtc; 1359 struct drm_device *drm_dev = vop->drm_dev; 1360 struct drm_plane *plane, *tmp; 1361 1362 of_node_put(crtc->port); 1363 1364 /* 1365 * We need to cleanup the planes now. Why? 1366 * 1367 * The planes are "&vop->win[i].base". That means the memory is 1368 * all part of the big "struct vop" chunk of memory. That memory 1369 * was devm allocated and associated with this component. We need to 1370 * free it ourselves before vop_unbind() finishes. 1371 */ 1372 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list, 1373 head) 1374 vop_plane_destroy(plane); 1375 1376 /* 1377 * Destroy CRTC after vop_plane_destroy() since vop_disable_plane() 1378 * references the CRTC. 1379 */ 1380 drm_crtc_cleanup(crtc); 1381 drm_flip_work_cleanup(&vop->fb_unref_work); 1382 } 1383 1384 static int vop_initial(struct vop *vop) 1385 { 1386 const struct vop_data *vop_data = vop->data; 1387 struct reset_control *ahb_rst; 1388 int i, ret; 1389 1390 vop->hclk = devm_clk_get(vop->dev, "hclk_vop"); 1391 if (IS_ERR(vop->hclk)) { 1392 DRM_DEV_ERROR(vop->dev, "failed to get hclk source\n"); 1393 return PTR_ERR(vop->hclk); 1394 } 1395 vop->aclk = devm_clk_get(vop->dev, "aclk_vop"); 1396 if (IS_ERR(vop->aclk)) { 1397 DRM_DEV_ERROR(vop->dev, "failed to get aclk source\n"); 1398 return PTR_ERR(vop->aclk); 1399 } 1400 vop->dclk = devm_clk_get(vop->dev, "dclk_vop"); 1401 if (IS_ERR(vop->dclk)) { 1402 DRM_DEV_ERROR(vop->dev, "failed to get dclk source\n"); 1403 return PTR_ERR(vop->dclk); 1404 } 1405 1406 ret = pm_runtime_get_sync(vop->dev); 1407 if (ret < 0) { 1408 DRM_DEV_ERROR(vop->dev, "failed to get pm runtime: %d\n", ret); 1409 return ret; 1410 } 1411 1412 ret = clk_prepare(vop->dclk); 1413 if (ret < 0) { 1414 DRM_DEV_ERROR(vop->dev, "failed to prepare dclk\n"); 1415 goto err_put_pm_runtime; 1416 } 1417 1418 /* Enable both the hclk and aclk to setup the vop */ 1419 ret = clk_prepare_enable(vop->hclk); 1420 if (ret < 0) { 1421 DRM_DEV_ERROR(vop->dev, "failed to prepare/enable hclk\n"); 1422 goto err_unprepare_dclk; 1423 } 1424 1425 ret = clk_prepare_enable(vop->aclk); 1426 if (ret < 0) { 1427 DRM_DEV_ERROR(vop->dev, "failed to prepare/enable aclk\n"); 1428 goto err_disable_hclk; 1429 } 1430 1431 /* 1432 * do hclk_reset, reset all vop registers. 1433 */ 1434 ahb_rst = devm_reset_control_get(vop->dev, "ahb"); 1435 if (IS_ERR(ahb_rst)) { 1436 DRM_DEV_ERROR(vop->dev, "failed to get ahb reset\n"); 1437 ret = PTR_ERR(ahb_rst); 1438 goto err_disable_aclk; 1439 } 1440 reset_control_assert(ahb_rst); 1441 usleep_range(10, 20); 1442 reset_control_deassert(ahb_rst); 1443 1444 VOP_INTR_SET_TYPE(vop, clear, INTR_MASK, 1); 1445 VOP_INTR_SET_TYPE(vop, enable, INTR_MASK, 0); 1446 1447 for (i = 0; i < vop->len; i += sizeof(u32)) 1448 vop->regsbak[i / 4] = readl_relaxed(vop->regs + i); 1449 1450 VOP_REG_SET(vop, misc, global_regdone_en, 1); 1451 VOP_REG_SET(vop, common, dsp_blank, 0); 1452 1453 for (i = 0; i < vop_data->win_size; i++) { 1454 const struct vop_win_data *win = &vop_data->win[i]; 1455 int channel = i * 2 + 1; 1456 1457 VOP_WIN_SET(vop, win, channel, (channel + 1) << 4 | channel); 1458 VOP_WIN_SET(vop, win, enable, 0); 1459 VOP_WIN_SET(vop, win, gate, 1); 1460 } 1461 1462 vop_cfg_done(vop); 1463 1464 /* 1465 * do dclk_reset, let all config take affect. 1466 */ 1467 vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk"); 1468 if (IS_ERR(vop->dclk_rst)) { 1469 DRM_DEV_ERROR(vop->dev, "failed to get dclk reset\n"); 1470 ret = PTR_ERR(vop->dclk_rst); 1471 goto err_disable_aclk; 1472 } 1473 reset_control_assert(vop->dclk_rst); 1474 usleep_range(10, 20); 1475 reset_control_deassert(vop->dclk_rst); 1476 1477 clk_disable(vop->hclk); 1478 clk_disable(vop->aclk); 1479 1480 vop->is_enabled = false; 1481 1482 pm_runtime_put_sync(vop->dev); 1483 1484 return 0; 1485 1486 err_disable_aclk: 1487 clk_disable_unprepare(vop->aclk); 1488 err_disable_hclk: 1489 clk_disable_unprepare(vop->hclk); 1490 err_unprepare_dclk: 1491 clk_unprepare(vop->dclk); 1492 err_put_pm_runtime: 1493 pm_runtime_put_sync(vop->dev); 1494 return ret; 1495 } 1496 1497 /* 1498 * Initialize the vop->win array elements. 1499 */ 1500 static void vop_win_init(struct vop *vop) 1501 { 1502 const struct vop_data *vop_data = vop->data; 1503 unsigned int i; 1504 1505 for (i = 0; i < vop_data->win_size; i++) { 1506 struct vop_win *vop_win = &vop->win[i]; 1507 const struct vop_win_data *win_data = &vop_data->win[i]; 1508 1509 vop_win->data = win_data; 1510 vop_win->vop = vop; 1511 } 1512 } 1513 1514 /** 1515 * rockchip_drm_wait_vact_end 1516 * @crtc: CRTC to enable line flag 1517 * @mstimeout: millisecond for timeout 1518 * 1519 * Wait for vact_end line flag irq or timeout. 1520 * 1521 * Returns: 1522 * Zero on success, negative errno on failure. 1523 */ 1524 int rockchip_drm_wait_vact_end(struct drm_crtc *crtc, unsigned int mstimeout) 1525 { 1526 struct vop *vop = to_vop(crtc); 1527 unsigned long jiffies_left; 1528 int ret = 0; 1529 1530 if (!crtc || !vop->is_enabled) 1531 return -ENODEV; 1532 1533 mutex_lock(&vop->vop_lock); 1534 if (mstimeout <= 0) { 1535 ret = -EINVAL; 1536 goto out; 1537 } 1538 1539 if (vop_line_flag_irq_is_enabled(vop)) { 1540 ret = -EBUSY; 1541 goto out; 1542 } 1543 1544 reinit_completion(&vop->line_flag_completion); 1545 vop_line_flag_irq_enable(vop); 1546 1547 jiffies_left = wait_for_completion_timeout(&vop->line_flag_completion, 1548 msecs_to_jiffies(mstimeout)); 1549 vop_line_flag_irq_disable(vop); 1550 1551 if (jiffies_left == 0) { 1552 DRM_DEV_ERROR(vop->dev, "Timeout waiting for IRQ\n"); 1553 ret = -ETIMEDOUT; 1554 goto out; 1555 } 1556 1557 out: 1558 mutex_unlock(&vop->vop_lock); 1559 return ret; 1560 } 1561 EXPORT_SYMBOL(rockchip_drm_wait_vact_end); 1562 1563 static int vop_bind(struct device *dev, struct device *master, void *data) 1564 { 1565 struct platform_device *pdev = to_platform_device(dev); 1566 const struct vop_data *vop_data; 1567 struct drm_device *drm_dev = data; 1568 struct vop *vop; 1569 struct resource *res; 1570 size_t alloc_size; 1571 int ret, irq; 1572 1573 vop_data = of_device_get_match_data(dev); 1574 if (!vop_data) 1575 return -ENODEV; 1576 1577 /* Allocate vop struct and its vop_win array */ 1578 alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size; 1579 vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL); 1580 if (!vop) 1581 return -ENOMEM; 1582 1583 vop->dev = dev; 1584 vop->data = vop_data; 1585 vop->drm_dev = drm_dev; 1586 dev_set_drvdata(dev, vop); 1587 1588 vop_win_init(vop); 1589 1590 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1591 vop->len = resource_size(res); 1592 vop->regs = devm_ioremap_resource(dev, res); 1593 if (IS_ERR(vop->regs)) 1594 return PTR_ERR(vop->regs); 1595 1596 vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL); 1597 if (!vop->regsbak) 1598 return -ENOMEM; 1599 1600 irq = platform_get_irq(pdev, 0); 1601 if (irq < 0) { 1602 DRM_DEV_ERROR(dev, "cannot find irq for vop\n"); 1603 return irq; 1604 } 1605 vop->irq = (unsigned int)irq; 1606 1607 spin_lock_init(&vop->reg_lock); 1608 spin_lock_init(&vop->irq_lock); 1609 mutex_init(&vop->vop_lock); 1610 1611 ret = vop_create_crtc(vop); 1612 if (ret) 1613 return ret; 1614 1615 pm_runtime_enable(&pdev->dev); 1616 1617 ret = vop_initial(vop); 1618 if (ret < 0) { 1619 DRM_DEV_ERROR(&pdev->dev, 1620 "cannot initial vop dev - err %d\n", ret); 1621 goto err_disable_pm_runtime; 1622 } 1623 1624 ret = devm_request_irq(dev, vop->irq, vop_isr, 1625 IRQF_SHARED, dev_name(dev), vop); 1626 if (ret) 1627 goto err_disable_pm_runtime; 1628 1629 return 0; 1630 1631 err_disable_pm_runtime: 1632 pm_runtime_disable(&pdev->dev); 1633 vop_destroy_crtc(vop); 1634 return ret; 1635 } 1636 1637 static void vop_unbind(struct device *dev, struct device *master, void *data) 1638 { 1639 struct vop *vop = dev_get_drvdata(dev); 1640 1641 pm_runtime_disable(dev); 1642 vop_destroy_crtc(vop); 1643 1644 clk_unprepare(vop->aclk); 1645 clk_unprepare(vop->hclk); 1646 clk_unprepare(vop->dclk); 1647 } 1648 1649 const struct component_ops vop_component_ops = { 1650 .bind = vop_bind, 1651 .unbind = vop_unbind, 1652 }; 1653 EXPORT_SYMBOL_GPL(vop_component_ops); 1654