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_atomic_helper.h>
27 #include <drm/drm_gem_framebuffer_helper.h>
28 #include <drm/drm_plane_helper.h>
29 #include <drm/drm_probe_helper.h>
30 #include <drm/drm_self_refresh_helper.h>
31 #include <drm/drm_vblank.h>
33 #ifdef CONFIG_DRM_ANALOGIX_DP
34 #include <drm/bridge/analogix_dp.h>
37 #include "rockchip_drm_drv.h"
38 #include "rockchip_drm_gem.h"
39 #include "rockchip_drm_fb.h"
40 #include "rockchip_drm_vop.h"
41 #include "rockchip_rgb.h"
43 #define VOP_WIN_SET(vop, win, name, v) \
44 vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
45 #define VOP_SCL_SET(vop, win, name, v) \
46 vop_reg_set(vop, &win->phy->scl->name, win->base, ~0, v, #name)
47 #define VOP_SCL_SET_EXT(vop, win, name, v) \
48 vop_reg_set(vop, &win->phy->scl->ext->name, \
49 win->base, ~0, v, #name)
51 #define VOP_WIN_YUV2YUV_SET(vop, win_yuv2yuv, name, v) \
53 if (win_yuv2yuv && win_yuv2yuv->name.mask) \
54 vop_reg_set(vop, &win_yuv2yuv->name, 0, ~0, v, #name); \
57 #define VOP_WIN_YUV2YUV_COEFFICIENT_SET(vop, win_yuv2yuv, name, v) \
59 if (win_yuv2yuv && win_yuv2yuv->phy->name.mask) \
60 vop_reg_set(vop, &win_yuv2yuv->phy->name, win_yuv2yuv->base, ~0, v, #name); \
63 #define VOP_INTR_SET_MASK(vop, name, mask, v) \
64 vop_reg_set(vop, &vop->data->intr->name, 0, mask, v, #name)
66 #define VOP_REG_SET(vop, group, name, v) \
67 vop_reg_set(vop, &vop->data->group->name, 0, ~0, v, #name)
69 #define VOP_INTR_SET_TYPE(vop, name, type, v) \
71 int i, reg = 0, mask = 0; \
72 for (i = 0; i < vop->data->intr->nintrs; i++) { \
73 if (vop->data->intr->intrs[i] & type) { \
78 VOP_INTR_SET_MASK(vop, name, mask, reg); \
80 #define VOP_INTR_GET_TYPE(vop, name, type) \
81 vop_get_intr_type(vop, &vop->data->intr->name, type)
83 #define VOP_WIN_GET(vop, win, name) \
84 vop_read_reg(vop, win->base, &win->phy->name)
86 #define VOP_WIN_HAS_REG(win, name) \
87 (!!(win->phy->name.mask))
89 #define VOP_WIN_GET_YRGBADDR(vop, win) \
90 vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
92 #define VOP_WIN_TO_INDEX(vop_win) \
93 ((vop_win) - (vop_win)->vop->win)
95 #define VOP_AFBC_SET(vop, name, v) \
97 if ((vop)->data->afbc) \
98 vop_reg_set((vop), &(vop)->data->afbc->name, \
102 #define to_vop(x) container_of(x, struct vop, crtc)
103 #define to_vop_win(x) container_of(x, struct vop_win, base)
105 #define AFBC_FMT_RGB565 0x0
106 #define AFBC_FMT_U8U8U8U8 0x5
107 #define AFBC_FMT_U8U8U8 0x4
109 #define AFBC_TILE_16x16 BIT(4)
112 * The coefficients of the following matrix are all fixed points.
113 * The format is S2.10 for the 3x3 part of the matrix, and S9.12 for the offsets.
114 * They are all represented in two's complement.
116 static const uint32_t bt601_yuv2rgb[] = {
118 0x4A8, 0x1E6F, 0x1CBF,
120 0x321168, 0x0877CF, 0x2EB127
124 VOP_PENDING_FB_UNREF,
128 struct drm_plane base;
129 const struct vop_win_data *data;
130 const struct vop_win_yuv2yuv_data *yuv2yuv_data;
136 struct drm_crtc crtc;
138 struct drm_device *drm_dev;
141 struct completion dsp_hold_completion;
142 unsigned int win_enabled;
144 /* protected by dev->event_lock */
145 struct drm_pending_vblank_event *event;
147 struct drm_flip_work fb_unref_work;
148 unsigned long pending;
150 struct completion line_flag_completion;
152 const struct vop_data *data;
156 void __iomem *lut_regs;
158 /* physical map length of vop register */
161 /* one time only one process allowed to config the register */
163 /* lock vop irq reg */
165 /* protects crtc enable/disable */
166 struct mutex vop_lock;
174 /* vop share memory frequency */
178 struct reset_control *dclk_rst;
180 /* optional internal rgb encoder */
181 struct rockchip_rgb *rgb;
183 struct vop_win win[];
186 static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
188 writel(v, vop->regs + offset);
189 vop->regsbak[offset >> 2] = v;
192 static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
194 return readl(vop->regs + offset);
197 static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
198 const struct vop_reg *reg)
200 return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
203 static void vop_reg_set(struct vop *vop, const struct vop_reg *reg,
204 uint32_t _offset, uint32_t _mask, uint32_t v,
205 const char *reg_name)
207 int offset, mask, shift;
209 if (!reg || !reg->mask) {
210 DRM_DEV_DEBUG(vop->dev, "Warning: not support %s\n", reg_name);
214 offset = reg->offset + _offset;
215 mask = reg->mask & _mask;
218 if (reg->write_mask) {
219 v = ((v << shift) & 0xffff) | (mask << (shift + 16));
221 uint32_t cached_val = vop->regsbak[offset >> 2];
223 v = (cached_val & ~(mask << shift)) | ((v & mask) << shift);
224 vop->regsbak[offset >> 2] = v;
228 writel_relaxed(v, vop->regs + offset);
230 writel(v, vop->regs + offset);
233 static inline uint32_t vop_get_intr_type(struct vop *vop,
234 const struct vop_reg *reg, int type)
237 uint32_t regs = vop_read_reg(vop, 0, reg);
239 for (i = 0; i < vop->data->intr->nintrs; i++) {
240 if ((type & vop->data->intr->intrs[i]) && (regs & 1 << i))
241 ret |= vop->data->intr->intrs[i];
247 static inline void vop_cfg_done(struct vop *vop)
249 VOP_REG_SET(vop, common, cfg_done, 1);
252 static bool has_rb_swapped(uint32_t format)
255 case DRM_FORMAT_XBGR8888:
256 case DRM_FORMAT_ABGR8888:
257 case DRM_FORMAT_BGR888:
258 case DRM_FORMAT_BGR565:
265 static enum vop_data_format vop_convert_format(uint32_t format)
268 case DRM_FORMAT_XRGB8888:
269 case DRM_FORMAT_ARGB8888:
270 case DRM_FORMAT_XBGR8888:
271 case DRM_FORMAT_ABGR8888:
272 return VOP_FMT_ARGB8888;
273 case DRM_FORMAT_RGB888:
274 case DRM_FORMAT_BGR888:
275 return VOP_FMT_RGB888;
276 case DRM_FORMAT_RGB565:
277 case DRM_FORMAT_BGR565:
278 return VOP_FMT_RGB565;
279 case DRM_FORMAT_NV12:
280 return VOP_FMT_YUV420SP;
281 case DRM_FORMAT_NV16:
282 return VOP_FMT_YUV422SP;
283 case DRM_FORMAT_NV24:
284 return VOP_FMT_YUV444SP;
286 DRM_ERROR("unsupported format[%08x]\n", format);
291 static int vop_convert_afbc_format(uint32_t format)
294 case DRM_FORMAT_XRGB8888:
295 case DRM_FORMAT_ARGB8888:
296 case DRM_FORMAT_XBGR8888:
297 case DRM_FORMAT_ABGR8888:
298 return AFBC_FMT_U8U8U8U8;
299 case DRM_FORMAT_RGB888:
300 case DRM_FORMAT_BGR888:
301 return AFBC_FMT_U8U8U8;
302 case DRM_FORMAT_RGB565:
303 case DRM_FORMAT_BGR565:
304 return AFBC_FMT_RGB565;
305 /* either of the below should not be reachable */
307 DRM_WARN_ONCE("unsupported AFBC format[%08x]\n", format);
314 static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src,
315 uint32_t dst, bool is_horizontal,
316 int vsu_mode, int *vskiplines)
318 uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT;
324 if (mode == SCALE_UP)
325 val = GET_SCL_FT_BIC(src, dst);
326 else if (mode == SCALE_DOWN)
327 val = GET_SCL_FT_BILI_DN(src, dst);
329 if (mode == SCALE_UP) {
330 if (vsu_mode == SCALE_UP_BIL)
331 val = GET_SCL_FT_BILI_UP(src, dst);
333 val = GET_SCL_FT_BIC(src, dst);
334 } else if (mode == SCALE_DOWN) {
336 *vskiplines = scl_get_vskiplines(src, dst);
337 val = scl_get_bili_dn_vskip(src, dst,
340 val = GET_SCL_FT_BILI_DN(src, dst);
348 static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win,
349 uint32_t src_w, uint32_t src_h, uint32_t dst_w,
350 uint32_t dst_h, const struct drm_format_info *info)
352 uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode;
353 uint16_t cbcr_hor_scl_mode = SCALE_NONE;
354 uint16_t cbcr_ver_scl_mode = SCALE_NONE;
356 uint16_t cbcr_src_w = src_w / info->hsub;
357 uint16_t cbcr_src_h = src_h / info->vsub;
367 DRM_DEV_ERROR(vop->dev, "Maximum dst width (3840) exceeded\n");
371 if (!win->phy->scl->ext) {
372 VOP_SCL_SET(vop, win, scale_yrgb_x,
373 scl_cal_scale2(src_w, dst_w));
374 VOP_SCL_SET(vop, win, scale_yrgb_y,
375 scl_cal_scale2(src_h, dst_h));
377 VOP_SCL_SET(vop, win, scale_cbcr_x,
378 scl_cal_scale2(cbcr_src_w, dst_w));
379 VOP_SCL_SET(vop, win, scale_cbcr_y,
380 scl_cal_scale2(cbcr_src_h, dst_h));
385 yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
386 yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
389 cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w);
390 cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h);
391 if (cbcr_hor_scl_mode == SCALE_DOWN)
392 lb_mode = scl_vop_cal_lb_mode(dst_w, true);
394 lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true);
396 if (yrgb_hor_scl_mode == SCALE_DOWN)
397 lb_mode = scl_vop_cal_lb_mode(dst_w, false);
399 lb_mode = scl_vop_cal_lb_mode(src_w, false);
402 VOP_SCL_SET_EXT(vop, win, lb_mode, lb_mode);
403 if (lb_mode == LB_RGB_3840X2) {
404 if (yrgb_ver_scl_mode != SCALE_NONE) {
405 DRM_DEV_ERROR(vop->dev, "not allow yrgb ver scale\n");
408 if (cbcr_ver_scl_mode != SCALE_NONE) {
409 DRM_DEV_ERROR(vop->dev, "not allow cbcr ver scale\n");
412 vsu_mode = SCALE_UP_BIL;
413 } else if (lb_mode == LB_RGB_2560X4) {
414 vsu_mode = SCALE_UP_BIL;
416 vsu_mode = SCALE_UP_BIC;
419 val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w,
421 VOP_SCL_SET(vop, win, scale_yrgb_x, val);
422 val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h,
423 false, vsu_mode, &vskiplines);
424 VOP_SCL_SET(vop, win, scale_yrgb_y, val);
426 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt4, vskiplines == 4);
427 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt2, vskiplines == 2);
429 VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode);
430 VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode);
431 VOP_SCL_SET_EXT(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL);
432 VOP_SCL_SET_EXT(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL);
433 VOP_SCL_SET_EXT(vop, win, yrgb_vsu_mode, vsu_mode);
435 val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w,
436 dst_w, true, 0, NULL);
437 VOP_SCL_SET(vop, win, scale_cbcr_x, val);
438 val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h,
439 dst_h, false, vsu_mode, &vskiplines);
440 VOP_SCL_SET(vop, win, scale_cbcr_y, val);
442 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt4, vskiplines == 4);
443 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt2, vskiplines == 2);
444 VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode);
445 VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode);
446 VOP_SCL_SET_EXT(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL);
447 VOP_SCL_SET_EXT(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL);
448 VOP_SCL_SET_EXT(vop, win, cbcr_vsu_mode, vsu_mode);
452 static void vop_dsp_hold_valid_irq_enable(struct vop *vop)
456 if (WARN_ON(!vop->is_enabled))
459 spin_lock_irqsave(&vop->irq_lock, flags);
461 VOP_INTR_SET_TYPE(vop, clear, DSP_HOLD_VALID_INTR, 1);
462 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 1);
464 spin_unlock_irqrestore(&vop->irq_lock, flags);
467 static void vop_dsp_hold_valid_irq_disable(struct vop *vop)
471 if (WARN_ON(!vop->is_enabled))
474 spin_lock_irqsave(&vop->irq_lock, flags);
476 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 0);
478 spin_unlock_irqrestore(&vop->irq_lock, flags);
482 * (1) each frame starts at the start of the Vsync pulse which is signaled by
483 * the "FRAME_SYNC" interrupt.
484 * (2) the active data region of each frame ends at dsp_vact_end
485 * (3) we should program this same number (dsp_vact_end) into dsp_line_frag_num,
486 * to get "LINE_FLAG" interrupt at the end of the active on screen data.
488 * VOP_INTR_CTRL0.dsp_line_frag_num = VOP_DSP_VACT_ST_END.dsp_vact_end
490 * LINE_FLAG -------------------------------+
494 * | Vsync | Vbp | Vactive | Vfp |
498 * dsp_vs_end ------------+ | | | VOP_DSP_VTOTAL_VS_END
499 * dsp_vact_start --------------+ | | VOP_DSP_VACT_ST_END
500 * dsp_vact_end ----------------------------+ | VOP_DSP_VACT_ST_END
501 * dsp_total -------------------------------------+ VOP_DSP_VTOTAL_VS_END
503 static bool vop_line_flag_irq_is_enabled(struct vop *vop)
505 uint32_t line_flag_irq;
508 spin_lock_irqsave(&vop->irq_lock, flags);
510 line_flag_irq = VOP_INTR_GET_TYPE(vop, enable, LINE_FLAG_INTR);
512 spin_unlock_irqrestore(&vop->irq_lock, flags);
514 return !!line_flag_irq;
517 static void vop_line_flag_irq_enable(struct vop *vop)
521 if (WARN_ON(!vop->is_enabled))
524 spin_lock_irqsave(&vop->irq_lock, flags);
526 VOP_INTR_SET_TYPE(vop, clear, LINE_FLAG_INTR, 1);
527 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 1);
529 spin_unlock_irqrestore(&vop->irq_lock, flags);
532 static void vop_line_flag_irq_disable(struct vop *vop)
536 if (WARN_ON(!vop->is_enabled))
539 spin_lock_irqsave(&vop->irq_lock, flags);
541 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 0);
543 spin_unlock_irqrestore(&vop->irq_lock, flags);
546 static int vop_core_clks_enable(struct vop *vop)
550 ret = clk_enable(vop->hclk);
554 ret = clk_enable(vop->aclk);
556 goto err_disable_hclk;
561 clk_disable(vop->hclk);
565 static void vop_core_clks_disable(struct vop *vop)
567 clk_disable(vop->aclk);
568 clk_disable(vop->hclk);
571 static void vop_win_disable(struct vop *vop, const struct vop_win *vop_win)
573 const struct vop_win_data *win = vop_win->data;
575 if (win->phy->scl && win->phy->scl->ext) {
576 VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, SCALE_NONE);
577 VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, SCALE_NONE);
578 VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, SCALE_NONE);
579 VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, SCALE_NONE);
582 VOP_WIN_SET(vop, win, enable, 0);
583 vop->win_enabled &= ~BIT(VOP_WIN_TO_INDEX(vop_win));
586 static int vop_enable(struct drm_crtc *crtc, struct drm_crtc_state *old_state)
588 struct vop *vop = to_vop(crtc);
591 ret = pm_runtime_get_sync(vop->dev);
593 DRM_DEV_ERROR(vop->dev, "failed to get pm runtime: %d\n", ret);
597 ret = vop_core_clks_enable(vop);
598 if (WARN_ON(ret < 0))
599 goto err_put_pm_runtime;
601 ret = clk_enable(vop->dclk);
602 if (WARN_ON(ret < 0))
603 goto err_disable_core;
606 * Slave iommu shares power, irq and clock with vop. It was associated
607 * automatically with this master device via common driver code.
608 * Now that we have enabled the clock we attach it to the shared drm
611 ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
613 DRM_DEV_ERROR(vop->dev,
614 "failed to attach dma mapping, %d\n", ret);
615 goto err_disable_dclk;
618 spin_lock(&vop->reg_lock);
619 for (i = 0; i < vop->len; i += 4)
620 writel_relaxed(vop->regsbak[i / 4], vop->regs + i);
623 * We need to make sure that all windows are disabled before we
624 * enable the crtc. Otherwise we might try to scan from a destroyed
627 * In the case of enable-after-PSR, we don't need to worry about this
628 * case since the buffer is guaranteed to be valid and disabling the
629 * window will result in screen glitches on PSR exit.
631 if (!old_state || !old_state->self_refresh_active) {
632 for (i = 0; i < vop->data->win_size; i++) {
633 struct vop_win *vop_win = &vop->win[i];
635 vop_win_disable(vop, vop_win);
639 if (vop->data->afbc) {
640 struct rockchip_crtc_state *s;
642 * Disable AFBC and forget there was a vop window with AFBC
644 VOP_AFBC_SET(vop, enable, 0);
645 s = to_rockchip_crtc_state(crtc->state);
646 s->enable_afbc = false;
651 spin_unlock(&vop->reg_lock);
654 * At here, vop clock & iommu is enable, R/W vop regs would be safe.
656 vop->is_enabled = true;
658 spin_lock(&vop->reg_lock);
660 VOP_REG_SET(vop, common, standby, 1);
662 spin_unlock(&vop->reg_lock);
664 drm_crtc_vblank_on(crtc);
669 clk_disable(vop->dclk);
671 vop_core_clks_disable(vop);
673 pm_runtime_put_sync(vop->dev);
677 static void rockchip_drm_set_win_enabled(struct drm_crtc *crtc, bool enabled)
679 struct vop *vop = to_vop(crtc);
682 spin_lock(&vop->reg_lock);
684 for (i = 0; i < vop->data->win_size; i++) {
685 struct vop_win *vop_win = &vop->win[i];
686 const struct vop_win_data *win = vop_win->data;
688 VOP_WIN_SET(vop, win, enable,
689 enabled && (vop->win_enabled & BIT(i)));
693 spin_unlock(&vop->reg_lock);
696 static void vop_crtc_atomic_disable(struct drm_crtc *crtc,
697 struct drm_atomic_state *state)
699 struct vop *vop = to_vop(crtc);
703 if (crtc->state->self_refresh_active)
704 rockchip_drm_set_win_enabled(crtc, false);
706 mutex_lock(&vop->vop_lock);
708 drm_crtc_vblank_off(crtc);
710 if (crtc->state->self_refresh_active)
714 * Vop standby will take effect at end of current frame,
715 * if dsp hold valid irq happen, it means standby complete.
717 * we must wait standby complete when we want to disable aclk,
718 * if not, memory bus maybe dead.
720 reinit_completion(&vop->dsp_hold_completion);
721 vop_dsp_hold_valid_irq_enable(vop);
723 spin_lock(&vop->reg_lock);
725 VOP_REG_SET(vop, common, standby, 1);
727 spin_unlock(&vop->reg_lock);
729 wait_for_completion(&vop->dsp_hold_completion);
731 vop_dsp_hold_valid_irq_disable(vop);
733 vop->is_enabled = false;
736 * vop standby complete, so iommu detach is safe.
738 rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
740 clk_disable(vop->dclk);
741 vop_core_clks_disable(vop);
742 pm_runtime_put(vop->dev);
745 mutex_unlock(&vop->vop_lock);
747 if (crtc->state->event && !crtc->state->active) {
748 spin_lock_irq(&crtc->dev->event_lock);
749 drm_crtc_send_vblank_event(crtc, crtc->state->event);
750 spin_unlock_irq(&crtc->dev->event_lock);
752 crtc->state->event = NULL;
756 static void vop_plane_destroy(struct drm_plane *plane)
758 drm_plane_cleanup(plane);
761 static inline bool rockchip_afbc(u64 modifier)
763 return modifier == ROCKCHIP_AFBC_MOD;
766 static bool rockchip_mod_supported(struct drm_plane *plane,
767 u32 format, u64 modifier)
769 if (modifier == DRM_FORMAT_MOD_LINEAR)
772 if (!rockchip_afbc(modifier)) {
773 DRM_DEBUG_KMS("Unsupported format modifier 0x%llx\n", modifier);
778 return vop_convert_afbc_format(format) >= 0;
781 static int vop_plane_atomic_check(struct drm_plane *plane,
782 struct drm_atomic_state *state)
784 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
786 struct drm_crtc *crtc = new_plane_state->crtc;
787 struct drm_crtc_state *crtc_state;
788 struct drm_framebuffer *fb = new_plane_state->fb;
789 struct vop_win *vop_win = to_vop_win(plane);
790 const struct vop_win_data *win = vop_win->data;
792 int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
793 DRM_PLANE_HELPER_NO_SCALING;
794 int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
795 DRM_PLANE_HELPER_NO_SCALING;
797 if (!crtc || WARN_ON(!fb))
800 crtc_state = drm_atomic_get_existing_crtc_state(state,
802 if (WARN_ON(!crtc_state))
805 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
806 min_scale, max_scale,
811 if (!new_plane_state->visible)
814 ret = vop_convert_format(fb->format->format);
819 * Src.x1 can be odd when do clip, but yuv plane start point
820 * need align with 2 pixel.
822 if (fb->format->is_yuv && ((new_plane_state->src.x1 >> 16) % 2)) {
823 DRM_ERROR("Invalid Source: Yuv format not support odd xpos\n");
827 if (fb->format->is_yuv && new_plane_state->rotation & DRM_MODE_REFLECT_Y) {
828 DRM_ERROR("Invalid Source: Yuv format does not support this rotation\n");
832 if (rockchip_afbc(fb->modifier)) {
833 struct vop *vop = to_vop(crtc);
835 if (!vop->data->afbc) {
836 DRM_ERROR("vop does not support AFBC\n");
840 ret = vop_convert_afbc_format(fb->format->format);
844 if (new_plane_state->src.x1 || new_plane_state->src.y1) {
845 DRM_ERROR("AFBC does not support offset display, xpos=%d, ypos=%d, offset=%d\n",
846 new_plane_state->src.x1,
847 new_plane_state->src.y1, fb->offsets[0]);
851 if (new_plane_state->rotation && new_plane_state->rotation != DRM_MODE_ROTATE_0) {
852 DRM_ERROR("No rotation support in AFBC, rotation=%d\n",
853 new_plane_state->rotation);
861 static void vop_plane_atomic_disable(struct drm_plane *plane,
862 struct drm_atomic_state *state)
864 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
866 struct vop_win *vop_win = to_vop_win(plane);
867 struct vop *vop = to_vop(old_state->crtc);
869 if (!old_state->crtc)
872 spin_lock(&vop->reg_lock);
874 vop_win_disable(vop, vop_win);
876 spin_unlock(&vop->reg_lock);
879 static void vop_plane_atomic_update(struct drm_plane *plane,
880 struct drm_atomic_state *state)
882 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
884 struct drm_crtc *crtc = new_state->crtc;
885 struct vop_win *vop_win = to_vop_win(plane);
886 const struct vop_win_data *win = vop_win->data;
887 const struct vop_win_yuv2yuv_data *win_yuv2yuv = vop_win->yuv2yuv_data;
888 struct vop *vop = to_vop(new_state->crtc);
889 struct drm_framebuffer *fb = new_state->fb;
890 unsigned int actual_w, actual_h;
891 unsigned int dsp_stx, dsp_sty;
892 uint32_t act_info, dsp_info, dsp_st;
893 struct drm_rect *src = &new_state->src;
894 struct drm_rect *dest = &new_state->dst;
895 struct drm_gem_object *obj, *uv_obj;
896 struct rockchip_gem_object *rk_obj, *rk_uv_obj;
897 unsigned long offset;
901 int win_index = VOP_WIN_TO_INDEX(vop_win);
903 int is_yuv = fb->format->is_yuv;
907 * can't update plane when vop is disabled.
912 if (WARN_ON(!vop->is_enabled))
915 if (!new_state->visible) {
916 vop_plane_atomic_disable(plane, state);
921 rk_obj = to_rockchip_obj(obj);
923 actual_w = drm_rect_width(src) >> 16;
924 actual_h = drm_rect_height(src) >> 16;
925 act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
927 dsp_info = (drm_rect_height(dest) - 1) << 16;
928 dsp_info |= (drm_rect_width(dest) - 1) & 0xffff;
930 dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start;
931 dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start;
932 dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff);
934 offset = (src->x1 >> 16) * fb->format->cpp[0];
935 offset += (src->y1 >> 16) * fb->pitches[0];
936 dma_addr = rk_obj->dma_addr + offset + fb->offsets[0];
939 * For y-mirroring we need to move address
940 * to the beginning of the last line.
942 if (new_state->rotation & DRM_MODE_REFLECT_Y)
943 dma_addr += (actual_h - 1) * fb->pitches[0];
945 format = vop_convert_format(fb->format->format);
947 spin_lock(&vop->reg_lock);
949 if (rockchip_afbc(fb->modifier)) {
950 int afbc_format = vop_convert_afbc_format(fb->format->format);
952 VOP_AFBC_SET(vop, format, afbc_format | AFBC_TILE_16x16);
953 VOP_AFBC_SET(vop, hreg_block_split, 0);
954 VOP_AFBC_SET(vop, win_sel, VOP_WIN_TO_INDEX(vop_win));
955 VOP_AFBC_SET(vop, hdr_ptr, dma_addr);
956 VOP_AFBC_SET(vop, pic_size, act_info);
959 VOP_WIN_SET(vop, win, format, format);
960 VOP_WIN_SET(vop, win, yrgb_vir, DIV_ROUND_UP(fb->pitches[0], 4));
961 VOP_WIN_SET(vop, win, yrgb_mst, dma_addr);
962 VOP_WIN_YUV2YUV_SET(vop, win_yuv2yuv, y2r_en, is_yuv);
963 VOP_WIN_SET(vop, win, y_mir_en,
964 (new_state->rotation & DRM_MODE_REFLECT_Y) ? 1 : 0);
965 VOP_WIN_SET(vop, win, x_mir_en,
966 (new_state->rotation & DRM_MODE_REFLECT_X) ? 1 : 0);
969 int hsub = fb->format->hsub;
970 int vsub = fb->format->vsub;
971 int bpp = fb->format->cpp[1];
974 rk_uv_obj = to_rockchip_obj(uv_obj);
976 offset = (src->x1 >> 16) * bpp / hsub;
977 offset += (src->y1 >> 16) * fb->pitches[1] / vsub;
979 dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1];
980 VOP_WIN_SET(vop, win, uv_vir, DIV_ROUND_UP(fb->pitches[1], 4));
981 VOP_WIN_SET(vop, win, uv_mst, dma_addr);
983 for (i = 0; i < NUM_YUV2YUV_COEFFICIENTS; i++) {
984 VOP_WIN_YUV2YUV_COEFFICIENT_SET(vop,
992 scl_vop_cal_scl_fac(vop, win, actual_w, actual_h,
993 drm_rect_width(dest), drm_rect_height(dest),
996 VOP_WIN_SET(vop, win, act_info, act_info);
997 VOP_WIN_SET(vop, win, dsp_info, dsp_info);
998 VOP_WIN_SET(vop, win, dsp_st, dsp_st);
1000 rb_swap = has_rb_swapped(fb->format->format);
1001 VOP_WIN_SET(vop, win, rb_swap, rb_swap);
1004 * Blending win0 with the background color doesn't seem to work
1005 * correctly. We only get the background color, no matter the contents
1006 * of the win0 framebuffer. However, blending pre-multiplied color
1007 * with the default opaque black default background color is a no-op,
1008 * so we can just disable blending to get the correct result.
1010 if (fb->format->has_alpha && win_index > 0) {
1011 VOP_WIN_SET(vop, win, dst_alpha_ctl,
1012 DST_FACTOR_M0(ALPHA_SRC_INVERSE));
1013 val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
1014 SRC_ALPHA_M0(ALPHA_STRAIGHT) |
1015 SRC_BLEND_M0(ALPHA_PER_PIX) |
1016 SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
1017 SRC_FACTOR_M0(ALPHA_ONE);
1018 VOP_WIN_SET(vop, win, src_alpha_ctl, val);
1020 VOP_WIN_SET(vop, win, alpha_pre_mul, ALPHA_SRC_PRE_MUL);
1021 VOP_WIN_SET(vop, win, alpha_mode, ALPHA_PER_PIX);
1022 VOP_WIN_SET(vop, win, alpha_en, 1);
1024 VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
1025 VOP_WIN_SET(vop, win, alpha_en, 0);
1028 VOP_WIN_SET(vop, win, enable, 1);
1029 vop->win_enabled |= BIT(win_index);
1030 spin_unlock(&vop->reg_lock);
1033 static int vop_plane_atomic_async_check(struct drm_plane *plane,
1034 struct drm_atomic_state *state)
1036 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
1038 struct vop_win *vop_win = to_vop_win(plane);
1039 const struct vop_win_data *win = vop_win->data;
1040 int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
1041 DRM_PLANE_HELPER_NO_SCALING;
1042 int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
1043 DRM_PLANE_HELPER_NO_SCALING;
1044 struct drm_crtc_state *crtc_state;
1046 if (plane != new_plane_state->crtc->cursor)
1052 if (!plane->state->fb)
1056 crtc_state = drm_atomic_get_existing_crtc_state(state,
1057 new_plane_state->crtc);
1058 else /* Special case for asynchronous cursor updates. */
1059 crtc_state = plane->crtc->state;
1061 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
1062 min_scale, max_scale,
1066 static void vop_plane_atomic_async_update(struct drm_plane *plane,
1067 struct drm_atomic_state *state)
1069 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
1071 struct vop *vop = to_vop(plane->state->crtc);
1072 struct drm_framebuffer *old_fb = plane->state->fb;
1074 plane->state->crtc_x = new_state->crtc_x;
1075 plane->state->crtc_y = new_state->crtc_y;
1076 plane->state->crtc_h = new_state->crtc_h;
1077 plane->state->crtc_w = new_state->crtc_w;
1078 plane->state->src_x = new_state->src_x;
1079 plane->state->src_y = new_state->src_y;
1080 plane->state->src_h = new_state->src_h;
1081 plane->state->src_w = new_state->src_w;
1082 swap(plane->state->fb, new_state->fb);
1084 if (vop->is_enabled) {
1085 vop_plane_atomic_update(plane, state);
1086 spin_lock(&vop->reg_lock);
1088 spin_unlock(&vop->reg_lock);
1091 * A scanout can still be occurring, so we can't drop the
1092 * reference to the old framebuffer. To solve this we get a
1093 * reference to old_fb and set a worker to release it later.
1094 * FIXME: if we perform 500 async_update calls before the
1095 * vblank, then we can have 500 different framebuffers waiting
1098 if (old_fb && plane->state->fb != old_fb) {
1099 drm_framebuffer_get(old_fb);
1100 WARN_ON(drm_crtc_vblank_get(plane->state->crtc) != 0);
1101 drm_flip_work_queue(&vop->fb_unref_work, old_fb);
1102 set_bit(VOP_PENDING_FB_UNREF, &vop->pending);
1107 static const struct drm_plane_helper_funcs plane_helper_funcs = {
1108 .atomic_check = vop_plane_atomic_check,
1109 .atomic_update = vop_plane_atomic_update,
1110 .atomic_disable = vop_plane_atomic_disable,
1111 .atomic_async_check = vop_plane_atomic_async_check,
1112 .atomic_async_update = vop_plane_atomic_async_update,
1115 static const struct drm_plane_funcs vop_plane_funcs = {
1116 .update_plane = drm_atomic_helper_update_plane,
1117 .disable_plane = drm_atomic_helper_disable_plane,
1118 .destroy = vop_plane_destroy,
1119 .reset = drm_atomic_helper_plane_reset,
1120 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
1121 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
1122 .format_mod_supported = rockchip_mod_supported,
1125 static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
1127 struct vop *vop = to_vop(crtc);
1128 unsigned long flags;
1130 if (WARN_ON(!vop->is_enabled))
1133 spin_lock_irqsave(&vop->irq_lock, flags);
1135 VOP_INTR_SET_TYPE(vop, clear, FS_INTR, 1);
1136 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 1);
1138 spin_unlock_irqrestore(&vop->irq_lock, flags);
1143 static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
1145 struct vop *vop = to_vop(crtc);
1146 unsigned long flags;
1148 if (WARN_ON(!vop->is_enabled))
1151 spin_lock_irqsave(&vop->irq_lock, flags);
1153 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 0);
1155 spin_unlock_irqrestore(&vop->irq_lock, flags);
1158 static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
1159 const struct drm_display_mode *mode,
1160 struct drm_display_mode *adjusted_mode)
1162 struct vop *vop = to_vop(crtc);
1170 * - DRM works in in kHz.
1171 * - Clock framework works in Hz.
1172 * - Rockchip's clock driver picks the clock rate that is the
1173 * same _OR LOWER_ than the one requested.
1177 * 1. When DRM gives us a mode, we should add 999 Hz to it. That way
1178 * if the clock we need is 60000001 Hz (~60 MHz) and DRM tells us to
1179 * make 60000 kHz then the clock framework will actually give us
1182 * NOTE: if the PLL (maybe through a divider) could actually make
1183 * a clock rate 999 Hz higher instead of the one we want then this
1184 * could be a problem. Unfortunately there's not much we can do
1185 * since it's baked into DRM to use kHz. It shouldn't matter in
1186 * practice since Rockchip PLLs are controlled by tables and
1187 * even if there is a divider in the middle I wouldn't expect PLL
1188 * rates in the table that are just a few kHz different.
1190 * 2. Get the clock framework to round the rate for us to tell us
1191 * what it will actually make.
1193 * 3. Store the rounded up rate so that we don't need to worry about
1194 * this in the actual clk_set_rate().
1196 rate = clk_round_rate(vop->dclk, adjusted_mode->clock * 1000 + 999);
1197 adjusted_mode->clock = DIV_ROUND_UP(rate, 1000);
1202 static bool vop_dsp_lut_is_enabled(struct vop *vop)
1204 return vop_read_reg(vop, 0, &vop->data->common->dsp_lut_en);
1207 static void vop_crtc_write_gamma_lut(struct vop *vop, struct drm_crtc *crtc)
1209 struct drm_color_lut *lut = crtc->state->gamma_lut->data;
1212 for (i = 0; i < crtc->gamma_size; i++) {
1215 word = (drm_color_lut_extract(lut[i].red, 10) << 20) |
1216 (drm_color_lut_extract(lut[i].green, 10) << 10) |
1217 drm_color_lut_extract(lut[i].blue, 10);
1218 writel(word, vop->lut_regs + i * 4);
1222 static void vop_crtc_gamma_set(struct vop *vop, struct drm_crtc *crtc,
1223 struct drm_crtc_state *old_state)
1225 struct drm_crtc_state *state = crtc->state;
1232 * To disable gamma (gamma_lut is null) or to write
1233 * an update to the LUT, clear dsp_lut_en.
1235 spin_lock(&vop->reg_lock);
1236 VOP_REG_SET(vop, common, dsp_lut_en, 0);
1238 spin_unlock(&vop->reg_lock);
1241 * In order to write the LUT to the internal memory,
1242 * we need to first make sure the dsp_lut_en bit is cleared.
1244 ret = readx_poll_timeout(vop_dsp_lut_is_enabled, vop,
1245 idle, !idle, 5, 30 * 1000);
1247 DRM_DEV_ERROR(vop->dev, "display LUT RAM enable timeout!\n");
1251 if (!state->gamma_lut)
1254 spin_lock(&vop->reg_lock);
1255 vop_crtc_write_gamma_lut(vop, crtc);
1256 VOP_REG_SET(vop, common, dsp_lut_en, 1);
1258 spin_unlock(&vop->reg_lock);
1261 static void vop_crtc_atomic_begin(struct drm_crtc *crtc,
1262 struct drm_atomic_state *state)
1264 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1266 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
1268 struct vop *vop = to_vop(crtc);
1271 * Only update GAMMA if the 'active' flag is not changed,
1272 * otherwise it's updated by .atomic_enable.
1274 if (crtc_state->color_mgmt_changed &&
1275 !crtc_state->active_changed)
1276 vop_crtc_gamma_set(vop, crtc, old_crtc_state);
1279 static void vop_crtc_atomic_enable(struct drm_crtc *crtc,
1280 struct drm_atomic_state *state)
1282 struct drm_crtc_state *old_state = drm_atomic_get_old_crtc_state(state,
1284 struct vop *vop = to_vop(crtc);
1285 const struct vop_data *vop_data = vop->data;
1286 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc->state);
1287 struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
1288 u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
1289 u16 hdisplay = adjusted_mode->hdisplay;
1290 u16 htotal = adjusted_mode->htotal;
1291 u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
1292 u16 hact_end = hact_st + hdisplay;
1293 u16 vdisplay = adjusted_mode->vdisplay;
1294 u16 vtotal = adjusted_mode->vtotal;
1295 u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
1296 u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
1297 u16 vact_end = vact_st + vdisplay;
1298 uint32_t pin_pol, val;
1299 int dither_bpc = s->output_bpc ? s->output_bpc : 10;
1302 if (old_state && old_state->self_refresh_active) {
1303 drm_crtc_vblank_on(crtc);
1304 rockchip_drm_set_win_enabled(crtc, true);
1309 * If we have a GAMMA LUT in the state, then let's make sure
1310 * it's updated. We might be coming out of suspend,
1311 * which means the LUT internal memory needs to be re-written.
1313 if (crtc->state->gamma_lut)
1314 vop_crtc_gamma_set(vop, crtc, old_state);
1316 mutex_lock(&vop->vop_lock);
1318 WARN_ON(vop->event);
1320 ret = vop_enable(crtc, old_state);
1322 mutex_unlock(&vop->vop_lock);
1323 DRM_DEV_ERROR(vop->dev, "Failed to enable vop (%d)\n", ret);
1326 pin_pol = (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC) ?
1327 BIT(HSYNC_POSITIVE) : 0;
1328 pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC) ?
1329 BIT(VSYNC_POSITIVE) : 0;
1330 VOP_REG_SET(vop, output, pin_pol, pin_pol);
1331 VOP_REG_SET(vop, output, mipi_dual_channel_en, 0);
1333 switch (s->output_type) {
1334 case DRM_MODE_CONNECTOR_LVDS:
1335 VOP_REG_SET(vop, output, rgb_dclk_pol, 1);
1336 VOP_REG_SET(vop, output, rgb_pin_pol, pin_pol);
1337 VOP_REG_SET(vop, output, rgb_en, 1);
1339 case DRM_MODE_CONNECTOR_eDP:
1340 VOP_REG_SET(vop, output, edp_dclk_pol, 1);
1341 VOP_REG_SET(vop, output, edp_pin_pol, pin_pol);
1342 VOP_REG_SET(vop, output, edp_en, 1);
1344 case DRM_MODE_CONNECTOR_HDMIA:
1345 VOP_REG_SET(vop, output, hdmi_dclk_pol, 1);
1346 VOP_REG_SET(vop, output, hdmi_pin_pol, pin_pol);
1347 VOP_REG_SET(vop, output, hdmi_en, 1);
1349 case DRM_MODE_CONNECTOR_DSI:
1350 VOP_REG_SET(vop, output, mipi_dclk_pol, 1);
1351 VOP_REG_SET(vop, output, mipi_pin_pol, pin_pol);
1352 VOP_REG_SET(vop, output, mipi_en, 1);
1353 VOP_REG_SET(vop, output, mipi_dual_channel_en,
1354 !!(s->output_flags & ROCKCHIP_OUTPUT_DSI_DUAL));
1356 case DRM_MODE_CONNECTOR_DisplayPort:
1357 VOP_REG_SET(vop, output, dp_dclk_pol, 0);
1358 VOP_REG_SET(vop, output, dp_pin_pol, pin_pol);
1359 VOP_REG_SET(vop, output, dp_en, 1);
1362 DRM_DEV_ERROR(vop->dev, "unsupported connector_type [%d]\n",
1367 * if vop is not support RGB10 output, need force RGB10 to RGB888.
1369 if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA &&
1370 !(vop_data->feature & VOP_FEATURE_OUTPUT_RGB10))
1371 s->output_mode = ROCKCHIP_OUT_MODE_P888;
1373 if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA && dither_bpc <= 8)
1374 VOP_REG_SET(vop, common, pre_dither_down, 1);
1376 VOP_REG_SET(vop, common, pre_dither_down, 0);
1378 if (dither_bpc == 6) {
1379 VOP_REG_SET(vop, common, dither_down_sel, DITHER_DOWN_ALLEGRO);
1380 VOP_REG_SET(vop, common, dither_down_mode, RGB888_TO_RGB666);
1381 VOP_REG_SET(vop, common, dither_down_en, 1);
1383 VOP_REG_SET(vop, common, dither_down_en, 0);
1386 VOP_REG_SET(vop, common, out_mode, s->output_mode);
1388 VOP_REG_SET(vop, modeset, htotal_pw, (htotal << 16) | hsync_len);
1389 val = hact_st << 16;
1391 VOP_REG_SET(vop, modeset, hact_st_end, val);
1392 VOP_REG_SET(vop, modeset, hpost_st_end, val);
1394 VOP_REG_SET(vop, modeset, vtotal_pw, (vtotal << 16) | vsync_len);
1395 val = vact_st << 16;
1397 VOP_REG_SET(vop, modeset, vact_st_end, val);
1398 VOP_REG_SET(vop, modeset, vpost_st_end, val);
1400 VOP_REG_SET(vop, intr, line_flag_num[0], vact_end);
1402 clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
1404 VOP_REG_SET(vop, common, standby, 0);
1405 mutex_unlock(&vop->vop_lock);
1408 static bool vop_fs_irq_is_pending(struct vop *vop)
1410 return VOP_INTR_GET_TYPE(vop, status, FS_INTR);
1413 static void vop_wait_for_irq_handler(struct vop *vop)
1419 * Spin until frame start interrupt status bit goes low, which means
1420 * that interrupt handler was invoked and cleared it. The timeout of
1421 * 10 msecs is really too long, but it is just a safety measure if
1422 * something goes really wrong. The wait will only happen in the very
1423 * unlikely case of a vblank happening exactly at the same time and
1424 * shouldn't exceed microseconds range.
1426 ret = readx_poll_timeout_atomic(vop_fs_irq_is_pending, vop, pending,
1427 !pending, 0, 10 * 1000);
1429 DRM_DEV_ERROR(vop->dev, "VOP vblank IRQ stuck for 10 ms\n");
1431 synchronize_irq(vop->irq);
1434 static int vop_crtc_atomic_check(struct drm_crtc *crtc,
1435 struct drm_atomic_state *state)
1437 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
1439 struct vop *vop = to_vop(crtc);
1440 struct drm_plane *plane;
1441 struct drm_plane_state *plane_state;
1442 struct rockchip_crtc_state *s;
1443 int afbc_planes = 0;
1445 if (vop->lut_regs && crtc_state->color_mgmt_changed &&
1446 crtc_state->gamma_lut) {
1449 len = drm_color_lut_size(crtc_state->gamma_lut);
1450 if (len != crtc->gamma_size) {
1451 DRM_DEBUG_KMS("Invalid LUT size; got %d, expected %d\n",
1452 len, crtc->gamma_size);
1457 drm_atomic_crtc_state_for_each_plane(plane, crtc_state) {
1459 drm_atomic_get_plane_state(crtc_state->state, plane);
1460 if (IS_ERR(plane_state)) {
1461 DRM_DEBUG_KMS("Cannot get plane state for plane %s\n",
1463 return PTR_ERR(plane_state);
1466 if (drm_is_afbc(plane_state->fb->modifier))
1470 if (afbc_planes > 1) {
1471 DRM_DEBUG_KMS("Invalid number of AFBC planes; got %d, expected at most 1\n", afbc_planes);
1475 s = to_rockchip_crtc_state(crtc_state);
1476 s->enable_afbc = afbc_planes > 0;
1481 static void vop_crtc_atomic_flush(struct drm_crtc *crtc,
1482 struct drm_atomic_state *state)
1484 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
1486 struct drm_atomic_state *old_state = old_crtc_state->state;
1487 struct drm_plane_state *old_plane_state, *new_plane_state;
1488 struct vop *vop = to_vop(crtc);
1489 struct drm_plane *plane;
1490 struct rockchip_crtc_state *s;
1493 if (WARN_ON(!vop->is_enabled))
1496 spin_lock(&vop->reg_lock);
1498 /* Enable AFBC if there is some AFBC window, disable otherwise. */
1499 s = to_rockchip_crtc_state(crtc->state);
1500 VOP_AFBC_SET(vop, enable, s->enable_afbc);
1503 spin_unlock(&vop->reg_lock);
1506 * There is a (rather unlikely) possiblity that a vblank interrupt
1507 * fired before we set the cfg_done bit. To avoid spuriously
1508 * signalling flip completion we need to wait for it to finish.
1510 vop_wait_for_irq_handler(vop);
1512 spin_lock_irq(&crtc->dev->event_lock);
1513 if (crtc->state->event) {
1514 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1515 WARN_ON(vop->event);
1517 vop->event = crtc->state->event;
1518 crtc->state->event = NULL;
1520 spin_unlock_irq(&crtc->dev->event_lock);
1522 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state,
1523 new_plane_state, i) {
1524 if (!old_plane_state->fb)
1527 if (old_plane_state->fb == new_plane_state->fb)
1530 drm_framebuffer_get(old_plane_state->fb);
1531 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1532 drm_flip_work_queue(&vop->fb_unref_work, old_plane_state->fb);
1533 set_bit(VOP_PENDING_FB_UNREF, &vop->pending);
1537 static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
1538 .mode_fixup = vop_crtc_mode_fixup,
1539 .atomic_check = vop_crtc_atomic_check,
1540 .atomic_begin = vop_crtc_atomic_begin,
1541 .atomic_flush = vop_crtc_atomic_flush,
1542 .atomic_enable = vop_crtc_atomic_enable,
1543 .atomic_disable = vop_crtc_atomic_disable,
1546 static void vop_crtc_destroy(struct drm_crtc *crtc)
1548 drm_crtc_cleanup(crtc);
1551 static struct drm_crtc_state *vop_crtc_duplicate_state(struct drm_crtc *crtc)
1553 struct rockchip_crtc_state *rockchip_state;
1555 rockchip_state = kzalloc(sizeof(*rockchip_state), GFP_KERNEL);
1556 if (!rockchip_state)
1559 __drm_atomic_helper_crtc_duplicate_state(crtc, &rockchip_state->base);
1560 return &rockchip_state->base;
1563 static void vop_crtc_destroy_state(struct drm_crtc *crtc,
1564 struct drm_crtc_state *state)
1566 struct rockchip_crtc_state *s = to_rockchip_crtc_state(state);
1568 __drm_atomic_helper_crtc_destroy_state(&s->base);
1572 static void vop_crtc_reset(struct drm_crtc *crtc)
1574 struct rockchip_crtc_state *crtc_state =
1575 kzalloc(sizeof(*crtc_state), GFP_KERNEL);
1578 vop_crtc_destroy_state(crtc, crtc->state);
1580 __drm_atomic_helper_crtc_reset(crtc, &crtc_state->base);
1583 #ifdef CONFIG_DRM_ANALOGIX_DP
1584 static struct drm_connector *vop_get_edp_connector(struct vop *vop)
1586 struct drm_connector *connector;
1587 struct drm_connector_list_iter conn_iter;
1589 drm_connector_list_iter_begin(vop->drm_dev, &conn_iter);
1590 drm_for_each_connector_iter(connector, &conn_iter) {
1591 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
1592 drm_connector_list_iter_end(&conn_iter);
1596 drm_connector_list_iter_end(&conn_iter);
1601 static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
1602 const char *source_name)
1604 struct vop *vop = to_vop(crtc);
1605 struct drm_connector *connector;
1608 connector = vop_get_edp_connector(vop);
1612 if (source_name && strcmp(source_name, "auto") == 0)
1613 ret = analogix_dp_start_crc(connector);
1614 else if (!source_name)
1615 ret = analogix_dp_stop_crc(connector);
1623 vop_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
1626 if (source_name && strcmp(source_name, "auto") != 0)
1634 static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
1635 const char *source_name)
1641 vop_crtc_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
1648 static const struct drm_crtc_funcs vop_crtc_funcs = {
1649 .set_config = drm_atomic_helper_set_config,
1650 .page_flip = drm_atomic_helper_page_flip,
1651 .destroy = vop_crtc_destroy,
1652 .reset = vop_crtc_reset,
1653 .atomic_duplicate_state = vop_crtc_duplicate_state,
1654 .atomic_destroy_state = vop_crtc_destroy_state,
1655 .enable_vblank = vop_crtc_enable_vblank,
1656 .disable_vblank = vop_crtc_disable_vblank,
1657 .set_crc_source = vop_crtc_set_crc_source,
1658 .verify_crc_source = vop_crtc_verify_crc_source,
1661 static void vop_fb_unref_worker(struct drm_flip_work *work, void *val)
1663 struct vop *vop = container_of(work, struct vop, fb_unref_work);
1664 struct drm_framebuffer *fb = val;
1666 drm_crtc_vblank_put(&vop->crtc);
1667 drm_framebuffer_put(fb);
1670 static void vop_handle_vblank(struct vop *vop)
1672 struct drm_device *drm = vop->drm_dev;
1673 struct drm_crtc *crtc = &vop->crtc;
1675 spin_lock(&drm->event_lock);
1677 drm_crtc_send_vblank_event(crtc, vop->event);
1678 drm_crtc_vblank_put(crtc);
1681 spin_unlock(&drm->event_lock);
1683 if (test_and_clear_bit(VOP_PENDING_FB_UNREF, &vop->pending))
1684 drm_flip_work_commit(&vop->fb_unref_work, system_unbound_wq);
1687 static irqreturn_t vop_isr(int irq, void *data)
1689 struct vop *vop = data;
1690 struct drm_crtc *crtc = &vop->crtc;
1691 uint32_t active_irqs;
1695 * The irq is shared with the iommu. If the runtime-pm state of the
1696 * vop-device is disabled the irq has to be targeted at the iommu.
1698 if (!pm_runtime_get_if_in_use(vop->dev))
1701 if (vop_core_clks_enable(vop)) {
1702 DRM_DEV_ERROR_RATELIMITED(vop->dev, "couldn't enable clocks\n");
1707 * interrupt register has interrupt status, enable and clear bits, we
1708 * must hold irq_lock to avoid a race with enable/disable_vblank().
1710 spin_lock(&vop->irq_lock);
1712 active_irqs = VOP_INTR_GET_TYPE(vop, status, INTR_MASK);
1713 /* Clear all active interrupt sources */
1715 VOP_INTR_SET_TYPE(vop, clear, active_irqs, 1);
1717 spin_unlock(&vop->irq_lock);
1719 /* This is expected for vop iommu irqs, since the irq is shared */
1723 if (active_irqs & DSP_HOLD_VALID_INTR) {
1724 complete(&vop->dsp_hold_completion);
1725 active_irqs &= ~DSP_HOLD_VALID_INTR;
1729 if (active_irqs & LINE_FLAG_INTR) {
1730 complete(&vop->line_flag_completion);
1731 active_irqs &= ~LINE_FLAG_INTR;
1735 if (active_irqs & FS_INTR) {
1736 drm_crtc_handle_vblank(crtc);
1737 vop_handle_vblank(vop);
1738 active_irqs &= ~FS_INTR;
1742 /* Unhandled irqs are spurious. */
1744 DRM_DEV_ERROR(vop->dev, "Unknown VOP IRQs: %#02x\n",
1748 vop_core_clks_disable(vop);
1750 pm_runtime_put(vop->dev);
1754 static void vop_plane_add_properties(struct drm_plane *plane,
1755 const struct vop_win_data *win_data)
1757 unsigned int flags = 0;
1759 flags |= VOP_WIN_HAS_REG(win_data, x_mir_en) ? DRM_MODE_REFLECT_X : 0;
1760 flags |= VOP_WIN_HAS_REG(win_data, y_mir_en) ? DRM_MODE_REFLECT_Y : 0;
1762 drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
1763 DRM_MODE_ROTATE_0 | flags);
1766 static int vop_create_crtc(struct vop *vop)
1768 const struct vop_data *vop_data = vop->data;
1769 struct device *dev = vop->dev;
1770 struct drm_device *drm_dev = vop->drm_dev;
1771 struct drm_plane *primary = NULL, *cursor = NULL, *plane, *tmp;
1772 struct drm_crtc *crtc = &vop->crtc;
1773 struct device_node *port;
1778 * Create drm_plane for primary and cursor planes first, since we need
1779 * to pass them to drm_crtc_init_with_planes, which sets the
1780 * "possible_crtcs" to the newly initialized crtc.
1782 for (i = 0; i < vop_data->win_size; i++) {
1783 struct vop_win *vop_win = &vop->win[i];
1784 const struct vop_win_data *win_data = vop_win->data;
1786 if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
1787 win_data->type != DRM_PLANE_TYPE_CURSOR)
1790 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1791 0, &vop_plane_funcs,
1792 win_data->phy->data_formats,
1793 win_data->phy->nformats,
1794 win_data->phy->format_modifiers,
1795 win_data->type, NULL);
1797 DRM_DEV_ERROR(vop->dev, "failed to init plane %d\n",
1799 goto err_cleanup_planes;
1802 plane = &vop_win->base;
1803 drm_plane_helper_add(plane, &plane_helper_funcs);
1804 vop_plane_add_properties(plane, win_data);
1805 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1807 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
1811 ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
1812 &vop_crtc_funcs, NULL);
1814 goto err_cleanup_planes;
1816 drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
1817 if (vop->lut_regs) {
1818 drm_mode_crtc_set_gamma_size(crtc, vop_data->lut_size);
1819 drm_crtc_enable_color_mgmt(crtc, 0, false, vop_data->lut_size);
1823 * Create drm_planes for overlay windows with possible_crtcs restricted
1824 * to the newly created crtc.
1826 for (i = 0; i < vop_data->win_size; i++) {
1827 struct vop_win *vop_win = &vop->win[i];
1828 const struct vop_win_data *win_data = vop_win->data;
1829 unsigned long possible_crtcs = drm_crtc_mask(crtc);
1831 if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
1834 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1837 win_data->phy->data_formats,
1838 win_data->phy->nformats,
1839 win_data->phy->format_modifiers,
1840 win_data->type, NULL);
1842 DRM_DEV_ERROR(vop->dev, "failed to init overlay %d\n",
1844 goto err_cleanup_crtc;
1846 drm_plane_helper_add(&vop_win->base, &plane_helper_funcs);
1847 vop_plane_add_properties(&vop_win->base, win_data);
1850 port = of_get_child_by_name(dev->of_node, "port");
1852 DRM_DEV_ERROR(vop->dev, "no port node found in %pOF\n",
1855 goto err_cleanup_crtc;
1858 drm_flip_work_init(&vop->fb_unref_work, "fb_unref",
1859 vop_fb_unref_worker);
1861 init_completion(&vop->dsp_hold_completion);
1862 init_completion(&vop->line_flag_completion);
1865 ret = drm_self_refresh_helper_init(crtc);
1867 DRM_DEV_DEBUG_KMS(vop->dev,
1868 "Failed to init %s with SR helpers %d, ignoring\n",
1874 drm_crtc_cleanup(crtc);
1876 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
1878 drm_plane_cleanup(plane);
1882 static void vop_destroy_crtc(struct vop *vop)
1884 struct drm_crtc *crtc = &vop->crtc;
1885 struct drm_device *drm_dev = vop->drm_dev;
1886 struct drm_plane *plane, *tmp;
1888 drm_self_refresh_helper_cleanup(crtc);
1890 of_node_put(crtc->port);
1893 * We need to cleanup the planes now. Why?
1895 * The planes are "&vop->win[i].base". That means the memory is
1896 * all part of the big "struct vop" chunk of memory. That memory
1897 * was devm allocated and associated with this component. We need to
1898 * free it ourselves before vop_unbind() finishes.
1900 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
1902 vop_plane_destroy(plane);
1905 * Destroy CRTC after vop_plane_destroy() since vop_disable_plane()
1906 * references the CRTC.
1908 drm_crtc_cleanup(crtc);
1909 drm_flip_work_cleanup(&vop->fb_unref_work);
1912 static int vop_initial(struct vop *vop)
1914 struct reset_control *ahb_rst;
1917 vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
1918 if (IS_ERR(vop->hclk)) {
1919 DRM_DEV_ERROR(vop->dev, "failed to get hclk source\n");
1920 return PTR_ERR(vop->hclk);
1922 vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
1923 if (IS_ERR(vop->aclk)) {
1924 DRM_DEV_ERROR(vop->dev, "failed to get aclk source\n");
1925 return PTR_ERR(vop->aclk);
1927 vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
1928 if (IS_ERR(vop->dclk)) {
1929 DRM_DEV_ERROR(vop->dev, "failed to get dclk source\n");
1930 return PTR_ERR(vop->dclk);
1933 ret = pm_runtime_get_sync(vop->dev);
1935 DRM_DEV_ERROR(vop->dev, "failed to get pm runtime: %d\n", ret);
1939 ret = clk_prepare(vop->dclk);
1941 DRM_DEV_ERROR(vop->dev, "failed to prepare dclk\n");
1942 goto err_put_pm_runtime;
1945 /* Enable both the hclk and aclk to setup the vop */
1946 ret = clk_prepare_enable(vop->hclk);
1948 DRM_DEV_ERROR(vop->dev, "failed to prepare/enable hclk\n");
1949 goto err_unprepare_dclk;
1952 ret = clk_prepare_enable(vop->aclk);
1954 DRM_DEV_ERROR(vop->dev, "failed to prepare/enable aclk\n");
1955 goto err_disable_hclk;
1959 * do hclk_reset, reset all vop registers.
1961 ahb_rst = devm_reset_control_get(vop->dev, "ahb");
1962 if (IS_ERR(ahb_rst)) {
1963 DRM_DEV_ERROR(vop->dev, "failed to get ahb reset\n");
1964 ret = PTR_ERR(ahb_rst);
1965 goto err_disable_aclk;
1967 reset_control_assert(ahb_rst);
1968 usleep_range(10, 20);
1969 reset_control_deassert(ahb_rst);
1971 VOP_INTR_SET_TYPE(vop, clear, INTR_MASK, 1);
1972 VOP_INTR_SET_TYPE(vop, enable, INTR_MASK, 0);
1974 for (i = 0; i < vop->len; i += sizeof(u32))
1975 vop->regsbak[i / 4] = readl_relaxed(vop->regs + i);
1977 VOP_REG_SET(vop, misc, global_regdone_en, 1);
1978 VOP_REG_SET(vop, common, dsp_blank, 0);
1980 for (i = 0; i < vop->data->win_size; i++) {
1981 struct vop_win *vop_win = &vop->win[i];
1982 const struct vop_win_data *win = vop_win->data;
1983 int channel = i * 2 + 1;
1985 VOP_WIN_SET(vop, win, channel, (channel + 1) << 4 | channel);
1986 vop_win_disable(vop, vop_win);
1987 VOP_WIN_SET(vop, win, gate, 1);
1993 * do dclk_reset, let all config take affect.
1995 vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
1996 if (IS_ERR(vop->dclk_rst)) {
1997 DRM_DEV_ERROR(vop->dev, "failed to get dclk reset\n");
1998 ret = PTR_ERR(vop->dclk_rst);
1999 goto err_disable_aclk;
2001 reset_control_assert(vop->dclk_rst);
2002 usleep_range(10, 20);
2003 reset_control_deassert(vop->dclk_rst);
2005 clk_disable(vop->hclk);
2006 clk_disable(vop->aclk);
2008 vop->is_enabled = false;
2010 pm_runtime_put_sync(vop->dev);
2015 clk_disable_unprepare(vop->aclk);
2017 clk_disable_unprepare(vop->hclk);
2019 clk_unprepare(vop->dclk);
2021 pm_runtime_put_sync(vop->dev);
2026 * Initialize the vop->win array elements.
2028 static void vop_win_init(struct vop *vop)
2030 const struct vop_data *vop_data = vop->data;
2033 for (i = 0; i < vop_data->win_size; i++) {
2034 struct vop_win *vop_win = &vop->win[i];
2035 const struct vop_win_data *win_data = &vop_data->win[i];
2037 vop_win->data = win_data;
2040 if (vop_data->win_yuv2yuv)
2041 vop_win->yuv2yuv_data = &vop_data->win_yuv2yuv[i];
2046 * rockchip_drm_wait_vact_end
2047 * @crtc: CRTC to enable line flag
2048 * @mstimeout: millisecond for timeout
2050 * Wait for vact_end line flag irq or timeout.
2053 * Zero on success, negative errno on failure.
2055 int rockchip_drm_wait_vact_end(struct drm_crtc *crtc, unsigned int mstimeout)
2057 struct vop *vop = to_vop(crtc);
2058 unsigned long jiffies_left;
2061 if (!crtc || !vop->is_enabled)
2064 mutex_lock(&vop->vop_lock);
2065 if (mstimeout <= 0) {
2070 if (vop_line_flag_irq_is_enabled(vop)) {
2075 reinit_completion(&vop->line_flag_completion);
2076 vop_line_flag_irq_enable(vop);
2078 jiffies_left = wait_for_completion_timeout(&vop->line_flag_completion,
2079 msecs_to_jiffies(mstimeout));
2080 vop_line_flag_irq_disable(vop);
2082 if (jiffies_left == 0) {
2083 DRM_DEV_ERROR(vop->dev, "Timeout waiting for IRQ\n");
2089 mutex_unlock(&vop->vop_lock);
2092 EXPORT_SYMBOL(rockchip_drm_wait_vact_end);
2094 static int vop_bind(struct device *dev, struct device *master, void *data)
2096 struct platform_device *pdev = to_platform_device(dev);
2097 const struct vop_data *vop_data;
2098 struct drm_device *drm_dev = data;
2100 struct resource *res;
2103 vop_data = of_device_get_match_data(dev);
2107 /* Allocate vop struct and its vop_win array */
2108 vop = devm_kzalloc(dev, struct_size(vop, win, vop_data->win_size),
2114 vop->data = vop_data;
2115 vop->drm_dev = drm_dev;
2116 dev_set_drvdata(dev, vop);
2120 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2121 vop->len = resource_size(res);
2122 vop->regs = devm_ioremap_resource(dev, res);
2123 if (IS_ERR(vop->regs))
2124 return PTR_ERR(vop->regs);
2126 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2128 if (!vop_data->lut_size) {
2129 DRM_DEV_ERROR(dev, "no gamma LUT size defined\n");
2132 vop->lut_regs = devm_ioremap_resource(dev, res);
2133 if (IS_ERR(vop->lut_regs))
2134 return PTR_ERR(vop->lut_regs);
2137 vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
2141 irq = platform_get_irq(pdev, 0);
2143 DRM_DEV_ERROR(dev, "cannot find irq for vop\n");
2146 vop->irq = (unsigned int)irq;
2148 spin_lock_init(&vop->reg_lock);
2149 spin_lock_init(&vop->irq_lock);
2150 mutex_init(&vop->vop_lock);
2152 ret = vop_create_crtc(vop);
2156 pm_runtime_enable(&pdev->dev);
2158 ret = vop_initial(vop);
2160 DRM_DEV_ERROR(&pdev->dev,
2161 "cannot initial vop dev - err %d\n", ret);
2162 goto err_disable_pm_runtime;
2165 ret = devm_request_irq(dev, vop->irq, vop_isr,
2166 IRQF_SHARED, dev_name(dev), vop);
2168 goto err_disable_pm_runtime;
2170 if (vop->data->feature & VOP_FEATURE_INTERNAL_RGB) {
2171 vop->rgb = rockchip_rgb_init(dev, &vop->crtc, vop->drm_dev);
2172 if (IS_ERR(vop->rgb)) {
2173 ret = PTR_ERR(vop->rgb);
2174 goto err_disable_pm_runtime;
2180 err_disable_pm_runtime:
2181 pm_runtime_disable(&pdev->dev);
2182 vop_destroy_crtc(vop);
2186 static void vop_unbind(struct device *dev, struct device *master, void *data)
2188 struct vop *vop = dev_get_drvdata(dev);
2191 rockchip_rgb_fini(vop->rgb);
2193 pm_runtime_disable(dev);
2194 vop_destroy_crtc(vop);
2196 clk_unprepare(vop->aclk);
2197 clk_unprepare(vop->hclk);
2198 clk_unprepare(vop->dclk);
2201 const struct component_ops vop_component_ops = {
2203 .unbind = vop_unbind,
2205 EXPORT_SYMBOL_GPL(vop_component_ops);