1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author:Mark Yao <mark.yao@rock-chips.com>
8 #include <linux/component.h>
9 #include <linux/delay.h>
10 #include <linux/iopoll.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/overflow.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_uapi.h>
23 #include <drm/drm_crtc.h>
24 #include <drm/drm_flip_work.h>
25 #include <drm/drm_fourcc.h>
26 #include <drm/drm_gem_framebuffer_helper.h>
27 #include <drm/drm_plane_helper.h>
28 #include <drm/drm_probe_helper.h>
29 #include <drm/drm_self_refresh_helper.h>
30 #include <drm/drm_vblank.h>
32 #ifdef CONFIG_DRM_ANALOGIX_DP
33 #include <drm/bridge/analogix_dp.h>
36 #include "rockchip_drm_drv.h"
37 #include "rockchip_drm_gem.h"
38 #include "rockchip_drm_fb.h"
39 #include "rockchip_drm_vop.h"
40 #include "rockchip_rgb.h"
42 #define VOP_WIN_SET(vop, win, name, v) \
43 vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
44 #define VOP_SCL_SET(vop, win, name, v) \
45 vop_reg_set(vop, &win->phy->scl->name, win->base, ~0, v, #name)
46 #define VOP_SCL_SET_EXT(vop, win, name, v) \
47 vop_reg_set(vop, &win->phy->scl->ext->name, \
48 win->base, ~0, v, #name)
50 #define VOP_WIN_YUV2YUV_SET(vop, win_yuv2yuv, name, v) \
52 if (win_yuv2yuv && win_yuv2yuv->name.mask) \
53 vop_reg_set(vop, &win_yuv2yuv->name, 0, ~0, v, #name); \
56 #define VOP_WIN_YUV2YUV_COEFFICIENT_SET(vop, win_yuv2yuv, name, v) \
58 if (win_yuv2yuv && win_yuv2yuv->phy->name.mask) \
59 vop_reg_set(vop, &win_yuv2yuv->phy->name, win_yuv2yuv->base, ~0, v, #name); \
62 #define VOP_INTR_SET_MASK(vop, name, mask, v) \
63 vop_reg_set(vop, &vop->data->intr->name, 0, mask, v, #name)
65 #define VOP_REG_SET(vop, group, name, v) \
66 vop_reg_set(vop, &vop->data->group->name, 0, ~0, v, #name)
68 #define VOP_INTR_SET_TYPE(vop, name, type, v) \
70 int i, reg = 0, mask = 0; \
71 for (i = 0; i < vop->data->intr->nintrs; i++) { \
72 if (vop->data->intr->intrs[i] & type) { \
77 VOP_INTR_SET_MASK(vop, name, mask, reg); \
79 #define VOP_INTR_GET_TYPE(vop, name, type) \
80 vop_get_intr_type(vop, &vop->data->intr->name, type)
82 #define VOP_WIN_GET(vop, win, name) \
83 vop_read_reg(vop, win->base, &win->phy->name)
85 #define VOP_WIN_HAS_REG(win, name) \
86 (!!(win->phy->name.mask))
88 #define VOP_WIN_GET_YRGBADDR(vop, win) \
89 vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
91 #define VOP_WIN_TO_INDEX(vop_win) \
92 ((vop_win) - (vop_win)->vop->win)
94 #define VOP_AFBC_SET(vop, name, v) \
96 if ((vop)->data->afbc) \
97 vop_reg_set((vop), &(vop)->data->afbc->name, \
101 #define to_vop(x) container_of(x, struct vop, crtc)
102 #define to_vop_win(x) container_of(x, struct vop_win, base)
104 #define AFBC_FMT_RGB565 0x0
105 #define AFBC_FMT_U8U8U8U8 0x5
106 #define AFBC_FMT_U8U8U8 0x4
108 #define AFBC_TILE_16x16 BIT(4)
111 * The coefficients of the following matrix are all fixed points.
112 * The format is S2.10 for the 3x3 part of the matrix, and S9.12 for the offsets.
113 * They are all represented in two's complement.
115 static const uint32_t bt601_yuv2rgb[] = {
117 0x4A8, 0x1E6F, 0x1CBF,
119 0x321168, 0x0877CF, 0x2EB127
123 VOP_PENDING_FB_UNREF,
127 struct drm_plane base;
128 const struct vop_win_data *data;
129 const struct vop_win_yuv2yuv_data *yuv2yuv_data;
135 struct drm_crtc crtc;
137 struct drm_device *drm_dev;
140 struct completion dsp_hold_completion;
141 unsigned int win_enabled;
143 /* protected by dev->event_lock */
144 struct drm_pending_vblank_event *event;
146 struct drm_flip_work fb_unref_work;
147 unsigned long pending;
149 struct completion line_flag_completion;
151 const struct vop_data *data;
155 void __iomem *lut_regs;
157 /* physical map length of vop register */
160 /* one time only one process allowed to config the register */
162 /* lock vop irq reg */
164 /* protects crtc enable/disable */
165 struct mutex vop_lock;
173 /* vop share memory frequency */
177 struct reset_control *dclk_rst;
179 /* optional internal rgb encoder */
180 struct rockchip_rgb *rgb;
182 struct vop_win win[];
185 static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
187 writel(v, vop->regs + offset);
188 vop->regsbak[offset >> 2] = v;
191 static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
193 return readl(vop->regs + offset);
196 static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
197 const struct vop_reg *reg)
199 return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
202 static void vop_reg_set(struct vop *vop, const struct vop_reg *reg,
203 uint32_t _offset, uint32_t _mask, uint32_t v,
204 const char *reg_name)
206 int offset, mask, shift;
208 if (!reg || !reg->mask) {
209 DRM_DEV_DEBUG(vop->dev, "Warning: not support %s\n", reg_name);
213 offset = reg->offset + _offset;
214 mask = reg->mask & _mask;
217 if (reg->write_mask) {
218 v = ((v << shift) & 0xffff) | (mask << (shift + 16));
220 uint32_t cached_val = vop->regsbak[offset >> 2];
222 v = (cached_val & ~(mask << shift)) | ((v & mask) << shift);
223 vop->regsbak[offset >> 2] = v;
227 writel_relaxed(v, vop->regs + offset);
229 writel(v, vop->regs + offset);
232 static inline uint32_t vop_get_intr_type(struct vop *vop,
233 const struct vop_reg *reg, int type)
236 uint32_t regs = vop_read_reg(vop, 0, reg);
238 for (i = 0; i < vop->data->intr->nintrs; i++) {
239 if ((type & vop->data->intr->intrs[i]) && (regs & 1 << i))
240 ret |= vop->data->intr->intrs[i];
246 static inline void vop_cfg_done(struct vop *vop)
248 VOP_REG_SET(vop, common, cfg_done, 1);
251 static bool has_rb_swapped(uint32_t format)
254 case DRM_FORMAT_XBGR8888:
255 case DRM_FORMAT_ABGR8888:
256 case DRM_FORMAT_BGR888:
257 case DRM_FORMAT_BGR565:
264 static enum vop_data_format vop_convert_format(uint32_t format)
267 case DRM_FORMAT_XRGB8888:
268 case DRM_FORMAT_ARGB8888:
269 case DRM_FORMAT_XBGR8888:
270 case DRM_FORMAT_ABGR8888:
271 return VOP_FMT_ARGB8888;
272 case DRM_FORMAT_RGB888:
273 case DRM_FORMAT_BGR888:
274 return VOP_FMT_RGB888;
275 case DRM_FORMAT_RGB565:
276 case DRM_FORMAT_BGR565:
277 return VOP_FMT_RGB565;
278 case DRM_FORMAT_NV12:
279 return VOP_FMT_YUV420SP;
280 case DRM_FORMAT_NV16:
281 return VOP_FMT_YUV422SP;
282 case DRM_FORMAT_NV24:
283 return VOP_FMT_YUV444SP;
285 DRM_ERROR("unsupported format[%08x]\n", format);
290 static int vop_convert_afbc_format(uint32_t format)
293 case DRM_FORMAT_XRGB8888:
294 case DRM_FORMAT_ARGB8888:
295 case DRM_FORMAT_XBGR8888:
296 case DRM_FORMAT_ABGR8888:
297 return AFBC_FMT_U8U8U8U8;
298 case DRM_FORMAT_RGB888:
299 case DRM_FORMAT_BGR888:
300 return AFBC_FMT_U8U8U8;
301 case DRM_FORMAT_RGB565:
302 case DRM_FORMAT_BGR565:
303 return AFBC_FMT_RGB565;
304 /* either of the below should not be reachable */
306 DRM_WARN_ONCE("unsupported AFBC format[%08x]\n", format);
313 static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src,
314 uint32_t dst, bool is_horizontal,
315 int vsu_mode, int *vskiplines)
317 uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT;
323 if (mode == SCALE_UP)
324 val = GET_SCL_FT_BIC(src, dst);
325 else if (mode == SCALE_DOWN)
326 val = GET_SCL_FT_BILI_DN(src, dst);
328 if (mode == SCALE_UP) {
329 if (vsu_mode == SCALE_UP_BIL)
330 val = GET_SCL_FT_BILI_UP(src, dst);
332 val = GET_SCL_FT_BIC(src, dst);
333 } else if (mode == SCALE_DOWN) {
335 *vskiplines = scl_get_vskiplines(src, dst);
336 val = scl_get_bili_dn_vskip(src, dst,
339 val = GET_SCL_FT_BILI_DN(src, dst);
347 static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win,
348 uint32_t src_w, uint32_t src_h, uint32_t dst_w,
349 uint32_t dst_h, const struct drm_format_info *info)
351 uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode;
352 uint16_t cbcr_hor_scl_mode = SCALE_NONE;
353 uint16_t cbcr_ver_scl_mode = SCALE_NONE;
355 uint16_t cbcr_src_w = src_w / info->hsub;
356 uint16_t cbcr_src_h = src_h / info->vsub;
366 DRM_DEV_ERROR(vop->dev, "Maximum dst width (3840) exceeded\n");
370 if (!win->phy->scl->ext) {
371 VOP_SCL_SET(vop, win, scale_yrgb_x,
372 scl_cal_scale2(src_w, dst_w));
373 VOP_SCL_SET(vop, win, scale_yrgb_y,
374 scl_cal_scale2(src_h, dst_h));
376 VOP_SCL_SET(vop, win, scale_cbcr_x,
377 scl_cal_scale2(cbcr_src_w, dst_w));
378 VOP_SCL_SET(vop, win, scale_cbcr_y,
379 scl_cal_scale2(cbcr_src_h, dst_h));
384 yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
385 yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
388 cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w);
389 cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h);
390 if (cbcr_hor_scl_mode == SCALE_DOWN)
391 lb_mode = scl_vop_cal_lb_mode(dst_w, true);
393 lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true);
395 if (yrgb_hor_scl_mode == SCALE_DOWN)
396 lb_mode = scl_vop_cal_lb_mode(dst_w, false);
398 lb_mode = scl_vop_cal_lb_mode(src_w, false);
401 VOP_SCL_SET_EXT(vop, win, lb_mode, lb_mode);
402 if (lb_mode == LB_RGB_3840X2) {
403 if (yrgb_ver_scl_mode != SCALE_NONE) {
404 DRM_DEV_ERROR(vop->dev, "not allow yrgb ver scale\n");
407 if (cbcr_ver_scl_mode != SCALE_NONE) {
408 DRM_DEV_ERROR(vop->dev, "not allow cbcr ver scale\n");
411 vsu_mode = SCALE_UP_BIL;
412 } else if (lb_mode == LB_RGB_2560X4) {
413 vsu_mode = SCALE_UP_BIL;
415 vsu_mode = SCALE_UP_BIC;
418 val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w,
420 VOP_SCL_SET(vop, win, scale_yrgb_x, val);
421 val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h,
422 false, vsu_mode, &vskiplines);
423 VOP_SCL_SET(vop, win, scale_yrgb_y, val);
425 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt4, vskiplines == 4);
426 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt2, vskiplines == 2);
428 VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode);
429 VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode);
430 VOP_SCL_SET_EXT(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL);
431 VOP_SCL_SET_EXT(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL);
432 VOP_SCL_SET_EXT(vop, win, yrgb_vsu_mode, vsu_mode);
434 val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w,
435 dst_w, true, 0, NULL);
436 VOP_SCL_SET(vop, win, scale_cbcr_x, val);
437 val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h,
438 dst_h, false, vsu_mode, &vskiplines);
439 VOP_SCL_SET(vop, win, scale_cbcr_y, val);
441 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt4, vskiplines == 4);
442 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt2, vskiplines == 2);
443 VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode);
444 VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode);
445 VOP_SCL_SET_EXT(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL);
446 VOP_SCL_SET_EXT(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL);
447 VOP_SCL_SET_EXT(vop, win, cbcr_vsu_mode, vsu_mode);
451 static void vop_dsp_hold_valid_irq_enable(struct vop *vop)
455 if (WARN_ON(!vop->is_enabled))
458 spin_lock_irqsave(&vop->irq_lock, flags);
460 VOP_INTR_SET_TYPE(vop, clear, DSP_HOLD_VALID_INTR, 1);
461 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 1);
463 spin_unlock_irqrestore(&vop->irq_lock, flags);
466 static void vop_dsp_hold_valid_irq_disable(struct vop *vop)
470 if (WARN_ON(!vop->is_enabled))
473 spin_lock_irqsave(&vop->irq_lock, flags);
475 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 0);
477 spin_unlock_irqrestore(&vop->irq_lock, flags);
481 * (1) each frame starts at the start of the Vsync pulse which is signaled by
482 * the "FRAME_SYNC" interrupt.
483 * (2) the active data region of each frame ends at dsp_vact_end
484 * (3) we should program this same number (dsp_vact_end) into dsp_line_frag_num,
485 * to get "LINE_FLAG" interrupt at the end of the active on screen data.
487 * VOP_INTR_CTRL0.dsp_line_frag_num = VOP_DSP_VACT_ST_END.dsp_vact_end
489 * LINE_FLAG -------------------------------+
493 * | Vsync | Vbp | Vactive | Vfp |
497 * dsp_vs_end ------------+ | | | VOP_DSP_VTOTAL_VS_END
498 * dsp_vact_start --------------+ | | VOP_DSP_VACT_ST_END
499 * dsp_vact_end ----------------------------+ | VOP_DSP_VACT_ST_END
500 * dsp_total -------------------------------------+ VOP_DSP_VTOTAL_VS_END
502 static bool vop_line_flag_irq_is_enabled(struct vop *vop)
504 uint32_t line_flag_irq;
507 spin_lock_irqsave(&vop->irq_lock, flags);
509 line_flag_irq = VOP_INTR_GET_TYPE(vop, enable, LINE_FLAG_INTR);
511 spin_unlock_irqrestore(&vop->irq_lock, flags);
513 return !!line_flag_irq;
516 static void vop_line_flag_irq_enable(struct vop *vop)
520 if (WARN_ON(!vop->is_enabled))
523 spin_lock_irqsave(&vop->irq_lock, flags);
525 VOP_INTR_SET_TYPE(vop, clear, LINE_FLAG_INTR, 1);
526 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 1);
528 spin_unlock_irqrestore(&vop->irq_lock, flags);
531 static void vop_line_flag_irq_disable(struct vop *vop)
535 if (WARN_ON(!vop->is_enabled))
538 spin_lock_irqsave(&vop->irq_lock, flags);
540 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 0);
542 spin_unlock_irqrestore(&vop->irq_lock, flags);
545 static int vop_core_clks_enable(struct vop *vop)
549 ret = clk_enable(vop->hclk);
553 ret = clk_enable(vop->aclk);
555 goto err_disable_hclk;
560 clk_disable(vop->hclk);
564 static void vop_core_clks_disable(struct vop *vop)
566 clk_disable(vop->aclk);
567 clk_disable(vop->hclk);
570 static void vop_win_disable(struct vop *vop, const struct vop_win *vop_win)
572 const struct vop_win_data *win = vop_win->data;
574 if (win->phy->scl && win->phy->scl->ext) {
575 VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, SCALE_NONE);
576 VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, SCALE_NONE);
577 VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, SCALE_NONE);
578 VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, SCALE_NONE);
581 VOP_WIN_SET(vop, win, enable, 0);
582 vop->win_enabled &= ~BIT(VOP_WIN_TO_INDEX(vop_win));
585 static int vop_enable(struct drm_crtc *crtc, struct drm_crtc_state *old_state)
587 struct vop *vop = to_vop(crtc);
590 ret = pm_runtime_get_sync(vop->dev);
592 DRM_DEV_ERROR(vop->dev, "failed to get pm runtime: %d\n", ret);
596 ret = vop_core_clks_enable(vop);
597 if (WARN_ON(ret < 0))
598 goto err_put_pm_runtime;
600 ret = clk_enable(vop->dclk);
601 if (WARN_ON(ret < 0))
602 goto err_disable_core;
605 * Slave iommu shares power, irq and clock with vop. It was associated
606 * automatically with this master device via common driver code.
607 * Now that we have enabled the clock we attach it to the shared drm
610 ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
612 DRM_DEV_ERROR(vop->dev,
613 "failed to attach dma mapping, %d\n", ret);
614 goto err_disable_dclk;
617 spin_lock(&vop->reg_lock);
618 for (i = 0; i < vop->len; i += 4)
619 writel_relaxed(vop->regsbak[i / 4], vop->regs + i);
622 * We need to make sure that all windows are disabled before we
623 * enable the crtc. Otherwise we might try to scan from a destroyed
626 * In the case of enable-after-PSR, we don't need to worry about this
627 * case since the buffer is guaranteed to be valid and disabling the
628 * window will result in screen glitches on PSR exit.
630 if (!old_state || !old_state->self_refresh_active) {
631 for (i = 0; i < vop->data->win_size; i++) {
632 struct vop_win *vop_win = &vop->win[i];
634 vop_win_disable(vop, vop_win);
638 if (vop->data->afbc) {
639 struct rockchip_crtc_state *s;
641 * Disable AFBC and forget there was a vop window with AFBC
643 VOP_AFBC_SET(vop, enable, 0);
644 s = to_rockchip_crtc_state(crtc->state);
645 s->enable_afbc = false;
650 spin_unlock(&vop->reg_lock);
653 * At here, vop clock & iommu is enable, R/W vop regs would be safe.
655 vop->is_enabled = true;
657 spin_lock(&vop->reg_lock);
659 VOP_REG_SET(vop, common, standby, 1);
661 spin_unlock(&vop->reg_lock);
663 drm_crtc_vblank_on(crtc);
668 clk_disable(vop->dclk);
670 vop_core_clks_disable(vop);
672 pm_runtime_put_sync(vop->dev);
676 static void rockchip_drm_set_win_enabled(struct drm_crtc *crtc, bool enabled)
678 struct vop *vop = to_vop(crtc);
681 spin_lock(&vop->reg_lock);
683 for (i = 0; i < vop->data->win_size; i++) {
684 struct vop_win *vop_win = &vop->win[i];
685 const struct vop_win_data *win = vop_win->data;
687 VOP_WIN_SET(vop, win, enable,
688 enabled && (vop->win_enabled & BIT(i)));
692 spin_unlock(&vop->reg_lock);
695 static void vop_crtc_atomic_disable(struct drm_crtc *crtc,
696 struct drm_crtc_state *old_state)
698 struct vop *vop = to_vop(crtc);
702 if (crtc->state->self_refresh_active)
703 rockchip_drm_set_win_enabled(crtc, false);
705 mutex_lock(&vop->vop_lock);
707 drm_crtc_vblank_off(crtc);
709 if (crtc->state->self_refresh_active)
713 * Vop standby will take effect at end of current frame,
714 * if dsp hold valid irq happen, it means standby complete.
716 * we must wait standby complete when we want to disable aclk,
717 * if not, memory bus maybe dead.
719 reinit_completion(&vop->dsp_hold_completion);
720 vop_dsp_hold_valid_irq_enable(vop);
722 spin_lock(&vop->reg_lock);
724 VOP_REG_SET(vop, common, standby, 1);
726 spin_unlock(&vop->reg_lock);
728 wait_for_completion(&vop->dsp_hold_completion);
730 vop_dsp_hold_valid_irq_disable(vop);
732 vop->is_enabled = false;
735 * vop standby complete, so iommu detach is safe.
737 rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
739 clk_disable(vop->dclk);
740 vop_core_clks_disable(vop);
741 pm_runtime_put(vop->dev);
744 mutex_unlock(&vop->vop_lock);
746 if (crtc->state->event && !crtc->state->active) {
747 spin_lock_irq(&crtc->dev->event_lock);
748 drm_crtc_send_vblank_event(crtc, crtc->state->event);
749 spin_unlock_irq(&crtc->dev->event_lock);
751 crtc->state->event = NULL;
755 static void vop_plane_destroy(struct drm_plane *plane)
757 drm_plane_cleanup(plane);
760 static inline bool rockchip_afbc(u64 modifier)
762 return modifier == ROCKCHIP_AFBC_MOD;
765 static bool rockchip_mod_supported(struct drm_plane *plane,
766 u32 format, u64 modifier)
768 if (modifier == DRM_FORMAT_MOD_LINEAR)
771 if (!rockchip_afbc(modifier)) {
772 DRM_DEBUG_KMS("Unsupported format modifier 0x%llx\n", modifier);
777 return vop_convert_afbc_format(format) >= 0;
780 static int vop_plane_atomic_check(struct drm_plane *plane,
781 struct drm_plane_state *state)
783 struct drm_crtc *crtc = state->crtc;
784 struct drm_crtc_state *crtc_state;
785 struct drm_framebuffer *fb = state->fb;
786 struct vop_win *vop_win = to_vop_win(plane);
787 const struct vop_win_data *win = vop_win->data;
789 int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
790 DRM_PLANE_HELPER_NO_SCALING;
791 int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
792 DRM_PLANE_HELPER_NO_SCALING;
794 if (!crtc || WARN_ON(!fb))
797 crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
798 if (WARN_ON(!crtc_state))
801 ret = drm_atomic_helper_check_plane_state(state, crtc_state,
802 min_scale, max_scale,
810 ret = vop_convert_format(fb->format->format);
815 * Src.x1 can be odd when do clip, but yuv plane start point
816 * need align with 2 pixel.
818 if (fb->format->is_yuv && ((state->src.x1 >> 16) % 2)) {
819 DRM_ERROR("Invalid Source: Yuv format not support odd xpos\n");
823 if (fb->format->is_yuv && state->rotation & DRM_MODE_REFLECT_Y) {
824 DRM_ERROR("Invalid Source: Yuv format does not support this rotation\n");
828 if (rockchip_afbc(fb->modifier)) {
829 struct vop *vop = to_vop(crtc);
831 if (!vop->data->afbc) {
832 DRM_ERROR("vop does not support AFBC\n");
836 ret = vop_convert_afbc_format(fb->format->format);
840 if (state->src.x1 || state->src.y1) {
841 DRM_ERROR("AFBC does not support offset display, xpos=%d, ypos=%d, offset=%d\n", state->src.x1, state->src.y1, fb->offsets[0]);
845 if (state->rotation && state->rotation != DRM_MODE_ROTATE_0) {
846 DRM_ERROR("No rotation support in AFBC, rotation=%d\n",
855 static void vop_plane_atomic_disable(struct drm_plane *plane,
856 struct drm_plane_state *old_state)
858 struct vop_win *vop_win = to_vop_win(plane);
859 struct vop *vop = to_vop(old_state->crtc);
861 if (!old_state->crtc)
864 spin_lock(&vop->reg_lock);
866 vop_win_disable(vop, vop_win);
868 spin_unlock(&vop->reg_lock);
871 static void vop_plane_atomic_update(struct drm_plane *plane,
872 struct drm_plane_state *old_state)
874 struct drm_plane_state *state = plane->state;
875 struct drm_crtc *crtc = state->crtc;
876 struct vop_win *vop_win = to_vop_win(plane);
877 const struct vop_win_data *win = vop_win->data;
878 const struct vop_win_yuv2yuv_data *win_yuv2yuv = vop_win->yuv2yuv_data;
879 struct vop *vop = to_vop(state->crtc);
880 struct drm_framebuffer *fb = state->fb;
881 unsigned int actual_w, actual_h;
882 unsigned int dsp_stx, dsp_sty;
883 uint32_t act_info, dsp_info, dsp_st;
884 struct drm_rect *src = &state->src;
885 struct drm_rect *dest = &state->dst;
886 struct drm_gem_object *obj, *uv_obj;
887 struct rockchip_gem_object *rk_obj, *rk_uv_obj;
888 unsigned long offset;
892 int win_index = VOP_WIN_TO_INDEX(vop_win);
894 int is_yuv = fb->format->is_yuv;
898 * can't update plane when vop is disabled.
903 if (WARN_ON(!vop->is_enabled))
906 if (!state->visible) {
907 vop_plane_atomic_disable(plane, old_state);
912 rk_obj = to_rockchip_obj(obj);
914 actual_w = drm_rect_width(src) >> 16;
915 actual_h = drm_rect_height(src) >> 16;
916 act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
918 dsp_info = (drm_rect_height(dest) - 1) << 16;
919 dsp_info |= (drm_rect_width(dest) - 1) & 0xffff;
921 dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start;
922 dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start;
923 dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff);
925 offset = (src->x1 >> 16) * fb->format->cpp[0];
926 offset += (src->y1 >> 16) * fb->pitches[0];
927 dma_addr = rk_obj->dma_addr + offset + fb->offsets[0];
930 * For y-mirroring we need to move address
931 * to the beginning of the last line.
933 if (state->rotation & DRM_MODE_REFLECT_Y)
934 dma_addr += (actual_h - 1) * fb->pitches[0];
936 format = vop_convert_format(fb->format->format);
938 spin_lock(&vop->reg_lock);
940 if (rockchip_afbc(fb->modifier)) {
941 int afbc_format = vop_convert_afbc_format(fb->format->format);
943 VOP_AFBC_SET(vop, format, afbc_format | AFBC_TILE_16x16);
944 VOP_AFBC_SET(vop, hreg_block_split, 0);
945 VOP_AFBC_SET(vop, win_sel, VOP_WIN_TO_INDEX(vop_win));
946 VOP_AFBC_SET(vop, hdr_ptr, dma_addr);
947 VOP_AFBC_SET(vop, pic_size, act_info);
950 VOP_WIN_SET(vop, win, format, format);
951 VOP_WIN_SET(vop, win, yrgb_vir, DIV_ROUND_UP(fb->pitches[0], 4));
952 VOP_WIN_SET(vop, win, yrgb_mst, dma_addr);
953 VOP_WIN_YUV2YUV_SET(vop, win_yuv2yuv, y2r_en, is_yuv);
954 VOP_WIN_SET(vop, win, y_mir_en,
955 (state->rotation & DRM_MODE_REFLECT_Y) ? 1 : 0);
956 VOP_WIN_SET(vop, win, x_mir_en,
957 (state->rotation & DRM_MODE_REFLECT_X) ? 1 : 0);
960 int hsub = fb->format->hsub;
961 int vsub = fb->format->vsub;
962 int bpp = fb->format->cpp[1];
965 rk_uv_obj = to_rockchip_obj(uv_obj);
967 offset = (src->x1 >> 16) * bpp / hsub;
968 offset += (src->y1 >> 16) * fb->pitches[1] / vsub;
970 dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1];
971 VOP_WIN_SET(vop, win, uv_vir, DIV_ROUND_UP(fb->pitches[1], 4));
972 VOP_WIN_SET(vop, win, uv_mst, dma_addr);
974 for (i = 0; i < NUM_YUV2YUV_COEFFICIENTS; i++) {
975 VOP_WIN_YUV2YUV_COEFFICIENT_SET(vop,
983 scl_vop_cal_scl_fac(vop, win, actual_w, actual_h,
984 drm_rect_width(dest), drm_rect_height(dest),
987 VOP_WIN_SET(vop, win, act_info, act_info);
988 VOP_WIN_SET(vop, win, dsp_info, dsp_info);
989 VOP_WIN_SET(vop, win, dsp_st, dsp_st);
991 rb_swap = has_rb_swapped(fb->format->format);
992 VOP_WIN_SET(vop, win, rb_swap, rb_swap);
995 * Blending win0 with the background color doesn't seem to work
996 * correctly. We only get the background color, no matter the contents
997 * of the win0 framebuffer. However, blending pre-multiplied color
998 * with the default opaque black default background color is a no-op,
999 * so we can just disable blending to get the correct result.
1001 if (fb->format->has_alpha && win_index > 0) {
1002 VOP_WIN_SET(vop, win, dst_alpha_ctl,
1003 DST_FACTOR_M0(ALPHA_SRC_INVERSE));
1004 val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
1005 SRC_ALPHA_M0(ALPHA_STRAIGHT) |
1006 SRC_BLEND_M0(ALPHA_PER_PIX) |
1007 SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
1008 SRC_FACTOR_M0(ALPHA_ONE);
1009 VOP_WIN_SET(vop, win, src_alpha_ctl, val);
1011 VOP_WIN_SET(vop, win, alpha_pre_mul, ALPHA_SRC_PRE_MUL);
1012 VOP_WIN_SET(vop, win, alpha_mode, ALPHA_PER_PIX);
1013 VOP_WIN_SET(vop, win, alpha_en, 1);
1015 VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
1016 VOP_WIN_SET(vop, win, alpha_en, 0);
1019 VOP_WIN_SET(vop, win, enable, 1);
1020 vop->win_enabled |= BIT(win_index);
1021 spin_unlock(&vop->reg_lock);
1024 static int vop_plane_atomic_async_check(struct drm_plane *plane,
1025 struct drm_plane_state *state)
1027 struct vop_win *vop_win = to_vop_win(plane);
1028 const struct vop_win_data *win = vop_win->data;
1029 int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
1030 DRM_PLANE_HELPER_NO_SCALING;
1031 int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
1032 DRM_PLANE_HELPER_NO_SCALING;
1033 struct drm_crtc_state *crtc_state;
1035 if (plane != state->crtc->cursor)
1041 if (!plane->state->fb)
1045 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
1047 else /* Special case for asynchronous cursor updates. */
1048 crtc_state = plane->crtc->state;
1050 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
1051 min_scale, max_scale,
1055 static void vop_plane_atomic_async_update(struct drm_plane *plane,
1056 struct drm_plane_state *new_state)
1058 struct vop *vop = to_vop(plane->state->crtc);
1059 struct drm_framebuffer *old_fb = plane->state->fb;
1061 plane->state->crtc_x = new_state->crtc_x;
1062 plane->state->crtc_y = new_state->crtc_y;
1063 plane->state->crtc_h = new_state->crtc_h;
1064 plane->state->crtc_w = new_state->crtc_w;
1065 plane->state->src_x = new_state->src_x;
1066 plane->state->src_y = new_state->src_y;
1067 plane->state->src_h = new_state->src_h;
1068 plane->state->src_w = new_state->src_w;
1069 swap(plane->state->fb, new_state->fb);
1071 if (vop->is_enabled) {
1072 vop_plane_atomic_update(plane, plane->state);
1073 spin_lock(&vop->reg_lock);
1075 spin_unlock(&vop->reg_lock);
1078 * A scanout can still be occurring, so we can't drop the
1079 * reference to the old framebuffer. To solve this we get a
1080 * reference to old_fb and set a worker to release it later.
1081 * FIXME: if we perform 500 async_update calls before the
1082 * vblank, then we can have 500 different framebuffers waiting
1085 if (old_fb && plane->state->fb != old_fb) {
1086 drm_framebuffer_get(old_fb);
1087 WARN_ON(drm_crtc_vblank_get(plane->state->crtc) != 0);
1088 drm_flip_work_queue(&vop->fb_unref_work, old_fb);
1089 set_bit(VOP_PENDING_FB_UNREF, &vop->pending);
1094 static const struct drm_plane_helper_funcs plane_helper_funcs = {
1095 .atomic_check = vop_plane_atomic_check,
1096 .atomic_update = vop_plane_atomic_update,
1097 .atomic_disable = vop_plane_atomic_disable,
1098 .atomic_async_check = vop_plane_atomic_async_check,
1099 .atomic_async_update = vop_plane_atomic_async_update,
1100 .prepare_fb = drm_gem_fb_prepare_fb,
1103 static const struct drm_plane_funcs vop_plane_funcs = {
1104 .update_plane = drm_atomic_helper_update_plane,
1105 .disable_plane = drm_atomic_helper_disable_plane,
1106 .destroy = vop_plane_destroy,
1107 .reset = drm_atomic_helper_plane_reset,
1108 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
1109 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
1110 .format_mod_supported = rockchip_mod_supported,
1113 static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
1115 struct vop *vop = to_vop(crtc);
1116 unsigned long flags;
1118 if (WARN_ON(!vop->is_enabled))
1121 spin_lock_irqsave(&vop->irq_lock, flags);
1123 VOP_INTR_SET_TYPE(vop, clear, FS_INTR, 1);
1124 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 1);
1126 spin_unlock_irqrestore(&vop->irq_lock, flags);
1131 static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
1133 struct vop *vop = to_vop(crtc);
1134 unsigned long flags;
1136 if (WARN_ON(!vop->is_enabled))
1139 spin_lock_irqsave(&vop->irq_lock, flags);
1141 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 0);
1143 spin_unlock_irqrestore(&vop->irq_lock, flags);
1146 static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
1147 const struct drm_display_mode *mode,
1148 struct drm_display_mode *adjusted_mode)
1150 struct vop *vop = to_vop(crtc);
1158 * - DRM works in in kHz.
1159 * - Clock framework works in Hz.
1160 * - Rockchip's clock driver picks the clock rate that is the
1161 * same _OR LOWER_ than the one requested.
1165 * 1. When DRM gives us a mode, we should add 999 Hz to it. That way
1166 * if the clock we need is 60000001 Hz (~60 MHz) and DRM tells us to
1167 * make 60000 kHz then the clock framework will actually give us
1170 * NOTE: if the PLL (maybe through a divider) could actually make
1171 * a clock rate 999 Hz higher instead of the one we want then this
1172 * could be a problem. Unfortunately there's not much we can do
1173 * since it's baked into DRM to use kHz. It shouldn't matter in
1174 * practice since Rockchip PLLs are controlled by tables and
1175 * even if there is a divider in the middle I wouldn't expect PLL
1176 * rates in the table that are just a few kHz different.
1178 * 2. Get the clock framework to round the rate for us to tell us
1179 * what it will actually make.
1181 * 3. Store the rounded up rate so that we don't need to worry about
1182 * this in the actual clk_set_rate().
1184 rate = clk_round_rate(vop->dclk, adjusted_mode->clock * 1000 + 999);
1185 adjusted_mode->clock = DIV_ROUND_UP(rate, 1000);
1190 static bool vop_dsp_lut_is_enabled(struct vop *vop)
1192 return vop_read_reg(vop, 0, &vop->data->common->dsp_lut_en);
1195 static void vop_crtc_write_gamma_lut(struct vop *vop, struct drm_crtc *crtc)
1197 struct drm_color_lut *lut = crtc->state->gamma_lut->data;
1200 for (i = 0; i < crtc->gamma_size; i++) {
1203 word = (drm_color_lut_extract(lut[i].red, 10) << 20) |
1204 (drm_color_lut_extract(lut[i].green, 10) << 10) |
1205 drm_color_lut_extract(lut[i].blue, 10);
1206 writel(word, vop->lut_regs + i * 4);
1210 static void vop_crtc_gamma_set(struct vop *vop, struct drm_crtc *crtc,
1211 struct drm_crtc_state *old_state)
1213 struct drm_crtc_state *state = crtc->state;
1220 * To disable gamma (gamma_lut is null) or to write
1221 * an update to the LUT, clear dsp_lut_en.
1223 spin_lock(&vop->reg_lock);
1224 VOP_REG_SET(vop, common, dsp_lut_en, 0);
1226 spin_unlock(&vop->reg_lock);
1229 * In order to write the LUT to the internal memory,
1230 * we need to first make sure the dsp_lut_en bit is cleared.
1232 ret = readx_poll_timeout(vop_dsp_lut_is_enabled, vop,
1233 idle, !idle, 5, 30 * 1000);
1235 DRM_DEV_ERROR(vop->dev, "display LUT RAM enable timeout!\n");
1239 if (!state->gamma_lut)
1242 spin_lock(&vop->reg_lock);
1243 vop_crtc_write_gamma_lut(vop, crtc);
1244 VOP_REG_SET(vop, common, dsp_lut_en, 1);
1246 spin_unlock(&vop->reg_lock);
1249 static void vop_crtc_atomic_begin(struct drm_crtc *crtc,
1250 struct drm_crtc_state *old_crtc_state)
1252 struct vop *vop = to_vop(crtc);
1255 * Only update GAMMA if the 'active' flag is not changed,
1256 * otherwise it's updated by .atomic_enable.
1258 if (crtc->state->color_mgmt_changed &&
1259 !crtc->state->active_changed)
1260 vop_crtc_gamma_set(vop, crtc, old_crtc_state);
1263 static void vop_crtc_atomic_enable(struct drm_crtc *crtc,
1264 struct drm_crtc_state *old_state)
1266 struct vop *vop = to_vop(crtc);
1267 const struct vop_data *vop_data = vop->data;
1268 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc->state);
1269 struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
1270 u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
1271 u16 hdisplay = adjusted_mode->hdisplay;
1272 u16 htotal = adjusted_mode->htotal;
1273 u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
1274 u16 hact_end = hact_st + hdisplay;
1275 u16 vdisplay = adjusted_mode->vdisplay;
1276 u16 vtotal = adjusted_mode->vtotal;
1277 u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
1278 u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
1279 u16 vact_end = vact_st + vdisplay;
1280 uint32_t pin_pol, val;
1281 int dither_bpc = s->output_bpc ? s->output_bpc : 10;
1284 if (old_state && old_state->self_refresh_active) {
1285 drm_crtc_vblank_on(crtc);
1286 rockchip_drm_set_win_enabled(crtc, true);
1291 * If we have a GAMMA LUT in the state, then let's make sure
1292 * it's updated. We might be coming out of suspend,
1293 * which means the LUT internal memory needs to be re-written.
1295 if (crtc->state->gamma_lut)
1296 vop_crtc_gamma_set(vop, crtc, old_state);
1298 mutex_lock(&vop->vop_lock);
1300 WARN_ON(vop->event);
1302 ret = vop_enable(crtc, old_state);
1304 mutex_unlock(&vop->vop_lock);
1305 DRM_DEV_ERROR(vop->dev, "Failed to enable vop (%d)\n", ret);
1308 pin_pol = (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC) ?
1309 BIT(HSYNC_POSITIVE) : 0;
1310 pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC) ?
1311 BIT(VSYNC_POSITIVE) : 0;
1312 VOP_REG_SET(vop, output, pin_pol, pin_pol);
1313 VOP_REG_SET(vop, output, mipi_dual_channel_en, 0);
1315 switch (s->output_type) {
1316 case DRM_MODE_CONNECTOR_LVDS:
1317 VOP_REG_SET(vop, output, rgb_dclk_pol, 1);
1318 VOP_REG_SET(vop, output, rgb_pin_pol, pin_pol);
1319 VOP_REG_SET(vop, output, rgb_en, 1);
1321 case DRM_MODE_CONNECTOR_eDP:
1322 VOP_REG_SET(vop, output, edp_dclk_pol, 1);
1323 VOP_REG_SET(vop, output, edp_pin_pol, pin_pol);
1324 VOP_REG_SET(vop, output, edp_en, 1);
1326 case DRM_MODE_CONNECTOR_HDMIA:
1327 VOP_REG_SET(vop, output, hdmi_dclk_pol, 1);
1328 VOP_REG_SET(vop, output, hdmi_pin_pol, pin_pol);
1329 VOP_REG_SET(vop, output, hdmi_en, 1);
1331 case DRM_MODE_CONNECTOR_DSI:
1332 VOP_REG_SET(vop, output, mipi_dclk_pol, 1);
1333 VOP_REG_SET(vop, output, mipi_pin_pol, pin_pol);
1334 VOP_REG_SET(vop, output, mipi_en, 1);
1335 VOP_REG_SET(vop, output, mipi_dual_channel_en,
1336 !!(s->output_flags & ROCKCHIP_OUTPUT_DSI_DUAL));
1338 case DRM_MODE_CONNECTOR_DisplayPort:
1339 VOP_REG_SET(vop, output, dp_dclk_pol, 0);
1340 VOP_REG_SET(vop, output, dp_pin_pol, pin_pol);
1341 VOP_REG_SET(vop, output, dp_en, 1);
1344 DRM_DEV_ERROR(vop->dev, "unsupported connector_type [%d]\n",
1349 * if vop is not support RGB10 output, need force RGB10 to RGB888.
1351 if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA &&
1352 !(vop_data->feature & VOP_FEATURE_OUTPUT_RGB10))
1353 s->output_mode = ROCKCHIP_OUT_MODE_P888;
1355 if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA && dither_bpc <= 8)
1356 VOP_REG_SET(vop, common, pre_dither_down, 1);
1358 VOP_REG_SET(vop, common, pre_dither_down, 0);
1360 if (dither_bpc == 6) {
1361 VOP_REG_SET(vop, common, dither_down_sel, DITHER_DOWN_ALLEGRO);
1362 VOP_REG_SET(vop, common, dither_down_mode, RGB888_TO_RGB666);
1363 VOP_REG_SET(vop, common, dither_down_en, 1);
1365 VOP_REG_SET(vop, common, dither_down_en, 0);
1368 VOP_REG_SET(vop, common, out_mode, s->output_mode);
1370 VOP_REG_SET(vop, modeset, htotal_pw, (htotal << 16) | hsync_len);
1371 val = hact_st << 16;
1373 VOP_REG_SET(vop, modeset, hact_st_end, val);
1374 VOP_REG_SET(vop, modeset, hpost_st_end, val);
1376 VOP_REG_SET(vop, modeset, vtotal_pw, (vtotal << 16) | vsync_len);
1377 val = vact_st << 16;
1379 VOP_REG_SET(vop, modeset, vact_st_end, val);
1380 VOP_REG_SET(vop, modeset, vpost_st_end, val);
1382 VOP_REG_SET(vop, intr, line_flag_num[0], vact_end);
1384 clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
1386 VOP_REG_SET(vop, common, standby, 0);
1387 mutex_unlock(&vop->vop_lock);
1390 static bool vop_fs_irq_is_pending(struct vop *vop)
1392 return VOP_INTR_GET_TYPE(vop, status, FS_INTR);
1395 static void vop_wait_for_irq_handler(struct vop *vop)
1401 * Spin until frame start interrupt status bit goes low, which means
1402 * that interrupt handler was invoked and cleared it. The timeout of
1403 * 10 msecs is really too long, but it is just a safety measure if
1404 * something goes really wrong. The wait will only happen in the very
1405 * unlikely case of a vblank happening exactly at the same time and
1406 * shouldn't exceed microseconds range.
1408 ret = readx_poll_timeout_atomic(vop_fs_irq_is_pending, vop, pending,
1409 !pending, 0, 10 * 1000);
1411 DRM_DEV_ERROR(vop->dev, "VOP vblank IRQ stuck for 10 ms\n");
1413 synchronize_irq(vop->irq);
1416 static int vop_crtc_atomic_check(struct drm_crtc *crtc,
1417 struct drm_crtc_state *crtc_state)
1419 struct vop *vop = to_vop(crtc);
1420 struct drm_plane *plane;
1421 struct drm_plane_state *plane_state;
1422 struct rockchip_crtc_state *s;
1423 int afbc_planes = 0;
1425 if (vop->lut_regs && crtc_state->color_mgmt_changed &&
1426 crtc_state->gamma_lut) {
1429 len = drm_color_lut_size(crtc_state->gamma_lut);
1430 if (len != crtc->gamma_size) {
1431 DRM_DEBUG_KMS("Invalid LUT size; got %d, expected %d\n",
1432 len, crtc->gamma_size);
1437 drm_atomic_crtc_state_for_each_plane(plane, crtc_state) {
1439 drm_atomic_get_plane_state(crtc_state->state, plane);
1440 if (IS_ERR(plane_state)) {
1441 DRM_DEBUG_KMS("Cannot get plane state for plane %s\n",
1443 return PTR_ERR(plane_state);
1446 if (drm_is_afbc(plane_state->fb->modifier))
1450 if (afbc_planes > 1) {
1451 DRM_DEBUG_KMS("Invalid number of AFBC planes; got %d, expected at most 1\n", afbc_planes);
1455 s = to_rockchip_crtc_state(crtc_state);
1456 s->enable_afbc = afbc_planes > 0;
1461 static void vop_crtc_atomic_flush(struct drm_crtc *crtc,
1462 struct drm_crtc_state *old_crtc_state)
1464 struct drm_atomic_state *old_state = old_crtc_state->state;
1465 struct drm_plane_state *old_plane_state, *new_plane_state;
1466 struct vop *vop = to_vop(crtc);
1467 struct drm_plane *plane;
1468 struct rockchip_crtc_state *s;
1471 if (WARN_ON(!vop->is_enabled))
1474 spin_lock(&vop->reg_lock);
1476 /* Enable AFBC if there is some AFBC window, disable otherwise. */
1477 s = to_rockchip_crtc_state(crtc->state);
1478 VOP_AFBC_SET(vop, enable, s->enable_afbc);
1481 spin_unlock(&vop->reg_lock);
1484 * There is a (rather unlikely) possiblity that a vblank interrupt
1485 * fired before we set the cfg_done bit. To avoid spuriously
1486 * signalling flip completion we need to wait for it to finish.
1488 vop_wait_for_irq_handler(vop);
1490 spin_lock_irq(&crtc->dev->event_lock);
1491 if (crtc->state->event) {
1492 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1493 WARN_ON(vop->event);
1495 vop->event = crtc->state->event;
1496 crtc->state->event = NULL;
1498 spin_unlock_irq(&crtc->dev->event_lock);
1500 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state,
1501 new_plane_state, i) {
1502 if (!old_plane_state->fb)
1505 if (old_plane_state->fb == new_plane_state->fb)
1508 drm_framebuffer_get(old_plane_state->fb);
1509 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1510 drm_flip_work_queue(&vop->fb_unref_work, old_plane_state->fb);
1511 set_bit(VOP_PENDING_FB_UNREF, &vop->pending);
1515 static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
1516 .mode_fixup = vop_crtc_mode_fixup,
1517 .atomic_check = vop_crtc_atomic_check,
1518 .atomic_begin = vop_crtc_atomic_begin,
1519 .atomic_flush = vop_crtc_atomic_flush,
1520 .atomic_enable = vop_crtc_atomic_enable,
1521 .atomic_disable = vop_crtc_atomic_disable,
1524 static void vop_crtc_destroy(struct drm_crtc *crtc)
1526 drm_crtc_cleanup(crtc);
1529 static struct drm_crtc_state *vop_crtc_duplicate_state(struct drm_crtc *crtc)
1531 struct rockchip_crtc_state *rockchip_state;
1533 if (WARN_ON(!crtc->state))
1536 rockchip_state = kzalloc(sizeof(*rockchip_state), GFP_KERNEL);
1537 if (!rockchip_state)
1540 __drm_atomic_helper_crtc_duplicate_state(crtc, &rockchip_state->base);
1541 return &rockchip_state->base;
1544 static void vop_crtc_destroy_state(struct drm_crtc *crtc,
1545 struct drm_crtc_state *state)
1547 struct rockchip_crtc_state *s = to_rockchip_crtc_state(state);
1549 __drm_atomic_helper_crtc_destroy_state(&s->base);
1553 static void vop_crtc_reset(struct drm_crtc *crtc)
1555 struct rockchip_crtc_state *crtc_state =
1556 kzalloc(sizeof(*crtc_state), GFP_KERNEL);
1559 vop_crtc_destroy_state(crtc, crtc->state);
1561 __drm_atomic_helper_crtc_reset(crtc, &crtc_state->base);
1564 #ifdef CONFIG_DRM_ANALOGIX_DP
1565 static struct drm_connector *vop_get_edp_connector(struct vop *vop)
1567 struct drm_connector *connector;
1568 struct drm_connector_list_iter conn_iter;
1570 drm_connector_list_iter_begin(vop->drm_dev, &conn_iter);
1571 drm_for_each_connector_iter(connector, &conn_iter) {
1572 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1573 drm_connector_list_iter_end(&conn_iter);
1577 drm_connector_list_iter_end(&conn_iter);
1582 static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
1583 const char *source_name)
1585 struct vop *vop = to_vop(crtc);
1586 struct drm_connector *connector;
1589 connector = vop_get_edp_connector(vop);
1593 if (source_name && strcmp(source_name, "auto") == 0)
1594 ret = analogix_dp_start_crc(connector);
1595 else if (!source_name)
1596 ret = analogix_dp_stop_crc(connector);
1604 vop_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
1607 if (source_name && strcmp(source_name, "auto") != 0)
1615 static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
1616 const char *source_name)
1622 vop_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
1629 static const struct drm_crtc_funcs vop_crtc_funcs = {
1630 .set_config = drm_atomic_helper_set_config,
1631 .page_flip = drm_atomic_helper_page_flip,
1632 .destroy = vop_crtc_destroy,
1633 .reset = vop_crtc_reset,
1634 .atomic_duplicate_state = vop_crtc_duplicate_state,
1635 .atomic_destroy_state = vop_crtc_destroy_state,
1636 .enable_vblank = vop_crtc_enable_vblank,
1637 .disable_vblank = vop_crtc_disable_vblank,
1638 .set_crc_source = vop_crtc_set_crc_source,
1639 .verify_crc_source = vop_crtc_verify_crc_source,
1640 .gamma_set = drm_atomic_helper_legacy_gamma_set,
1643 static void vop_fb_unref_worker(struct drm_flip_work *work, void *val)
1645 struct vop *vop = container_of(work, struct vop, fb_unref_work);
1646 struct drm_framebuffer *fb = val;
1648 drm_crtc_vblank_put(&vop->crtc);
1649 drm_framebuffer_put(fb);
1652 static void vop_handle_vblank(struct vop *vop)
1654 struct drm_device *drm = vop->drm_dev;
1655 struct drm_crtc *crtc = &vop->crtc;
1657 spin_lock(&drm->event_lock);
1659 drm_crtc_send_vblank_event(crtc, vop->event);
1660 drm_crtc_vblank_put(crtc);
1663 spin_unlock(&drm->event_lock);
1665 if (test_and_clear_bit(VOP_PENDING_FB_UNREF, &vop->pending))
1666 drm_flip_work_commit(&vop->fb_unref_work, system_unbound_wq);
1669 static irqreturn_t vop_isr(int irq, void *data)
1671 struct vop *vop = data;
1672 struct drm_crtc *crtc = &vop->crtc;
1673 uint32_t active_irqs;
1677 * The irq is shared with the iommu. If the runtime-pm state of the
1678 * vop-device is disabled the irq has to be targeted at the iommu.
1680 if (!pm_runtime_get_if_in_use(vop->dev))
1683 if (vop_core_clks_enable(vop)) {
1684 DRM_DEV_ERROR_RATELIMITED(vop->dev, "couldn't enable clocks\n");
1689 * interrupt register has interrupt status, enable and clear bits, we
1690 * must hold irq_lock to avoid a race with enable/disable_vblank().
1692 spin_lock(&vop->irq_lock);
1694 active_irqs = VOP_INTR_GET_TYPE(vop, status, INTR_MASK);
1695 /* Clear all active interrupt sources */
1697 VOP_INTR_SET_TYPE(vop, clear, active_irqs, 1);
1699 spin_unlock(&vop->irq_lock);
1701 /* This is expected for vop iommu irqs, since the irq is shared */
1705 if (active_irqs & DSP_HOLD_VALID_INTR) {
1706 complete(&vop->dsp_hold_completion);
1707 active_irqs &= ~DSP_HOLD_VALID_INTR;
1711 if (active_irqs & LINE_FLAG_INTR) {
1712 complete(&vop->line_flag_completion);
1713 active_irqs &= ~LINE_FLAG_INTR;
1717 if (active_irqs & FS_INTR) {
1718 drm_crtc_handle_vblank(crtc);
1719 vop_handle_vblank(vop);
1720 active_irqs &= ~FS_INTR;
1724 /* Unhandled irqs are spurious. */
1726 DRM_DEV_ERROR(vop->dev, "Unknown VOP IRQs: %#02x\n",
1730 vop_core_clks_disable(vop);
1732 pm_runtime_put(vop->dev);
1736 static void vop_plane_add_properties(struct drm_plane *plane,
1737 const struct vop_win_data *win_data)
1739 unsigned int flags = 0;
1741 flags |= VOP_WIN_HAS_REG(win_data, x_mir_en) ? DRM_MODE_REFLECT_X : 0;
1742 flags |= VOP_WIN_HAS_REG(win_data, y_mir_en) ? DRM_MODE_REFLECT_Y : 0;
1744 drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
1745 DRM_MODE_ROTATE_0 | flags);
1748 static int vop_create_crtc(struct vop *vop)
1750 const struct vop_data *vop_data = vop->data;
1751 struct device *dev = vop->dev;
1752 struct drm_device *drm_dev = vop->drm_dev;
1753 struct drm_plane *primary = NULL, *cursor = NULL, *plane, *tmp;
1754 struct drm_crtc *crtc = &vop->crtc;
1755 struct device_node *port;
1760 * Create drm_plane for primary and cursor planes first, since we need
1761 * to pass them to drm_crtc_init_with_planes, which sets the
1762 * "possible_crtcs" to the newly initialized crtc.
1764 for (i = 0; i < vop_data->win_size; i++) {
1765 struct vop_win *vop_win = &vop->win[i];
1766 const struct vop_win_data *win_data = vop_win->data;
1768 if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
1769 win_data->type != DRM_PLANE_TYPE_CURSOR)
1772 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1773 0, &vop_plane_funcs,
1774 win_data->phy->data_formats,
1775 win_data->phy->nformats,
1776 win_data->phy->format_modifiers,
1777 win_data->type, NULL);
1779 DRM_DEV_ERROR(vop->dev, "failed to init plane %d\n",
1781 goto err_cleanup_planes;
1784 plane = &vop_win->base;
1785 drm_plane_helper_add(plane, &plane_helper_funcs);
1786 vop_plane_add_properties(plane, win_data);
1787 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1789 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
1793 ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
1794 &vop_crtc_funcs, NULL);
1796 goto err_cleanup_planes;
1798 drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
1799 if (vop->lut_regs) {
1800 drm_mode_crtc_set_gamma_size(crtc, vop_data->lut_size);
1801 drm_crtc_enable_color_mgmt(crtc, 0, false, vop_data->lut_size);
1805 * Create drm_planes for overlay windows with possible_crtcs restricted
1806 * to the newly created crtc.
1808 for (i = 0; i < vop_data->win_size; i++) {
1809 struct vop_win *vop_win = &vop->win[i];
1810 const struct vop_win_data *win_data = vop_win->data;
1811 unsigned long possible_crtcs = drm_crtc_mask(crtc);
1813 if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
1816 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1819 win_data->phy->data_formats,
1820 win_data->phy->nformats,
1821 win_data->phy->format_modifiers,
1822 win_data->type, NULL);
1824 DRM_DEV_ERROR(vop->dev, "failed to init overlay %d\n",
1826 goto err_cleanup_crtc;
1828 drm_plane_helper_add(&vop_win->base, &plane_helper_funcs);
1829 vop_plane_add_properties(&vop_win->base, win_data);
1832 port = of_get_child_by_name(dev->of_node, "port");
1834 DRM_DEV_ERROR(vop->dev, "no port node found in %pOF\n",
1837 goto err_cleanup_crtc;
1840 drm_flip_work_init(&vop->fb_unref_work, "fb_unref",
1841 vop_fb_unref_worker);
1843 init_completion(&vop->dsp_hold_completion);
1844 init_completion(&vop->line_flag_completion);
1847 ret = drm_self_refresh_helper_init(crtc);
1849 DRM_DEV_DEBUG_KMS(vop->dev,
1850 "Failed to init %s with SR helpers %d, ignoring\n",
1856 drm_crtc_cleanup(crtc);
1858 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
1860 drm_plane_cleanup(plane);
1864 static void vop_destroy_crtc(struct vop *vop)
1866 struct drm_crtc *crtc = &vop->crtc;
1867 struct drm_device *drm_dev = vop->drm_dev;
1868 struct drm_plane *plane, *tmp;
1870 drm_self_refresh_helper_cleanup(crtc);
1872 of_node_put(crtc->port);
1875 * We need to cleanup the planes now. Why?
1877 * The planes are "&vop->win[i].base". That means the memory is
1878 * all part of the big "struct vop" chunk of memory. That memory
1879 * was devm allocated and associated with this component. We need to
1880 * free it ourselves before vop_unbind() finishes.
1882 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
1884 vop_plane_destroy(plane);
1887 * Destroy CRTC after vop_plane_destroy() since vop_disable_plane()
1888 * references the CRTC.
1890 drm_crtc_cleanup(crtc);
1891 drm_flip_work_cleanup(&vop->fb_unref_work);
1894 static int vop_initial(struct vop *vop)
1896 struct reset_control *ahb_rst;
1899 vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
1900 if (IS_ERR(vop->hclk)) {
1901 DRM_DEV_ERROR(vop->dev, "failed to get hclk source\n");
1902 return PTR_ERR(vop->hclk);
1904 vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
1905 if (IS_ERR(vop->aclk)) {
1906 DRM_DEV_ERROR(vop->dev, "failed to get aclk source\n");
1907 return PTR_ERR(vop->aclk);
1909 vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
1910 if (IS_ERR(vop->dclk)) {
1911 DRM_DEV_ERROR(vop->dev, "failed to get dclk source\n");
1912 return PTR_ERR(vop->dclk);
1915 ret = pm_runtime_get_sync(vop->dev);
1917 DRM_DEV_ERROR(vop->dev, "failed to get pm runtime: %d\n", ret);
1921 ret = clk_prepare(vop->dclk);
1923 DRM_DEV_ERROR(vop->dev, "failed to prepare dclk\n");
1924 goto err_put_pm_runtime;
1927 /* Enable both the hclk and aclk to setup the vop */
1928 ret = clk_prepare_enable(vop->hclk);
1930 DRM_DEV_ERROR(vop->dev, "failed to prepare/enable hclk\n");
1931 goto err_unprepare_dclk;
1934 ret = clk_prepare_enable(vop->aclk);
1936 DRM_DEV_ERROR(vop->dev, "failed to prepare/enable aclk\n");
1937 goto err_disable_hclk;
1941 * do hclk_reset, reset all vop registers.
1943 ahb_rst = devm_reset_control_get(vop->dev, "ahb");
1944 if (IS_ERR(ahb_rst)) {
1945 DRM_DEV_ERROR(vop->dev, "failed to get ahb reset\n");
1946 ret = PTR_ERR(ahb_rst);
1947 goto err_disable_aclk;
1949 reset_control_assert(ahb_rst);
1950 usleep_range(10, 20);
1951 reset_control_deassert(ahb_rst);
1953 VOP_INTR_SET_TYPE(vop, clear, INTR_MASK, 1);
1954 VOP_INTR_SET_TYPE(vop, enable, INTR_MASK, 0);
1956 for (i = 0; i < vop->len; i += sizeof(u32))
1957 vop->regsbak[i / 4] = readl_relaxed(vop->regs + i);
1959 VOP_REG_SET(vop, misc, global_regdone_en, 1);
1960 VOP_REG_SET(vop, common, dsp_blank, 0);
1962 for (i = 0; i < vop->data->win_size; i++) {
1963 struct vop_win *vop_win = &vop->win[i];
1964 const struct vop_win_data *win = vop_win->data;
1965 int channel = i * 2 + 1;
1967 VOP_WIN_SET(vop, win, channel, (channel + 1) << 4 | channel);
1968 vop_win_disable(vop, vop_win);
1969 VOP_WIN_SET(vop, win, gate, 1);
1975 * do dclk_reset, let all config take affect.
1977 vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
1978 if (IS_ERR(vop->dclk_rst)) {
1979 DRM_DEV_ERROR(vop->dev, "failed to get dclk reset\n");
1980 ret = PTR_ERR(vop->dclk_rst);
1981 goto err_disable_aclk;
1983 reset_control_assert(vop->dclk_rst);
1984 usleep_range(10, 20);
1985 reset_control_deassert(vop->dclk_rst);
1987 clk_disable(vop->hclk);
1988 clk_disable(vop->aclk);
1990 vop->is_enabled = false;
1992 pm_runtime_put_sync(vop->dev);
1997 clk_disable_unprepare(vop->aclk);
1999 clk_disable_unprepare(vop->hclk);
2001 clk_unprepare(vop->dclk);
2003 pm_runtime_put_sync(vop->dev);
2008 * Initialize the vop->win array elements.
2010 static void vop_win_init(struct vop *vop)
2012 const struct vop_data *vop_data = vop->data;
2015 for (i = 0; i < vop_data->win_size; i++) {
2016 struct vop_win *vop_win = &vop->win[i];
2017 const struct vop_win_data *win_data = &vop_data->win[i];
2019 vop_win->data = win_data;
2022 if (vop_data->win_yuv2yuv)
2023 vop_win->yuv2yuv_data = &vop_data->win_yuv2yuv[i];
2028 * rockchip_drm_wait_vact_end
2029 * @crtc: CRTC to enable line flag
2030 * @mstimeout: millisecond for timeout
2032 * Wait for vact_end line flag irq or timeout.
2035 * Zero on success, negative errno on failure.
2037 int rockchip_drm_wait_vact_end(struct drm_crtc *crtc, unsigned int mstimeout)
2039 struct vop *vop = to_vop(crtc);
2040 unsigned long jiffies_left;
2043 if (!crtc || !vop->is_enabled)
2046 mutex_lock(&vop->vop_lock);
2047 if (mstimeout <= 0) {
2052 if (vop_line_flag_irq_is_enabled(vop)) {
2057 reinit_completion(&vop->line_flag_completion);
2058 vop_line_flag_irq_enable(vop);
2060 jiffies_left = wait_for_completion_timeout(&vop->line_flag_completion,
2061 msecs_to_jiffies(mstimeout));
2062 vop_line_flag_irq_disable(vop);
2064 if (jiffies_left == 0) {
2065 DRM_DEV_ERROR(vop->dev, "Timeout waiting for IRQ\n");
2071 mutex_unlock(&vop->vop_lock);
2074 EXPORT_SYMBOL(rockchip_drm_wait_vact_end);
2076 static int vop_bind(struct device *dev, struct device *master, void *data)
2078 struct platform_device *pdev = to_platform_device(dev);
2079 const struct vop_data *vop_data;
2080 struct drm_device *drm_dev = data;
2082 struct resource *res;
2085 vop_data = of_device_get_match_data(dev);
2089 /* Allocate vop struct and its vop_win array */
2090 vop = devm_kzalloc(dev, struct_size(vop, win, vop_data->win_size),
2096 vop->data = vop_data;
2097 vop->drm_dev = drm_dev;
2098 dev_set_drvdata(dev, vop);
2102 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2103 vop->regs = devm_ioremap_resource(dev, res);
2104 if (IS_ERR(vop->regs))
2105 return PTR_ERR(vop->regs);
2106 vop->len = resource_size(res);
2108 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2110 if (!vop_data->lut_size) {
2111 DRM_DEV_ERROR(dev, "no gamma LUT size defined\n");
2114 vop->lut_regs = devm_ioremap_resource(dev, res);
2115 if (IS_ERR(vop->lut_regs))
2116 return PTR_ERR(vop->lut_regs);
2119 vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
2123 irq = platform_get_irq(pdev, 0);
2125 DRM_DEV_ERROR(dev, "cannot find irq for vop\n");
2128 vop->irq = (unsigned int)irq;
2130 spin_lock_init(&vop->reg_lock);
2131 spin_lock_init(&vop->irq_lock);
2132 mutex_init(&vop->vop_lock);
2134 ret = vop_create_crtc(vop);
2138 pm_runtime_enable(&pdev->dev);
2140 ret = vop_initial(vop);
2142 DRM_DEV_ERROR(&pdev->dev,
2143 "cannot initial vop dev - err %d\n", ret);
2144 goto err_disable_pm_runtime;
2147 ret = devm_request_irq(dev, vop->irq, vop_isr,
2148 IRQF_SHARED, dev_name(dev), vop);
2150 goto err_disable_pm_runtime;
2152 if (vop->data->feature & VOP_FEATURE_INTERNAL_RGB) {
2153 vop->rgb = rockchip_rgb_init(dev, &vop->crtc, vop->drm_dev);
2154 if (IS_ERR(vop->rgb)) {
2155 ret = PTR_ERR(vop->rgb);
2156 goto err_disable_pm_runtime;
2162 err_disable_pm_runtime:
2163 pm_runtime_disable(&pdev->dev);
2164 vop_destroy_crtc(vop);
2168 static void vop_unbind(struct device *dev, struct device *master, void *data)
2170 struct vop *vop = dev_get_drvdata(dev);
2173 rockchip_rgb_fini(vop->rgb);
2175 pm_runtime_disable(dev);
2176 vop_destroy_crtc(vop);
2178 clk_unprepare(vop->aclk);
2179 clk_unprepare(vop->hclk);
2180 clk_unprepare(vop->dclk);
2183 const struct component_ops vop_component_ops = {
2185 .unbind = vop_unbind,
2187 EXPORT_SYMBOL_GPL(vop_component_ops);