12048e328SMark Yao /* 22048e328SMark Yao * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd 32048e328SMark Yao * Author:Mark Yao <mark.yao@rock-chips.com> 42048e328SMark Yao * 52048e328SMark Yao * This software is licensed under the terms of the GNU General Public 62048e328SMark Yao * License version 2, as published by the Free Software Foundation, and 72048e328SMark Yao * may be copied, distributed, and modified under those terms. 82048e328SMark Yao * 92048e328SMark Yao * This program is distributed in the hope that it will be useful, 102048e328SMark Yao * but WITHOUT ANY WARRANTY; without even the implied warranty of 112048e328SMark Yao * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 122048e328SMark Yao * GNU General Public License for more details. 132048e328SMark Yao */ 142048e328SMark Yao 152048e328SMark Yao #include <drm/drm.h> 162048e328SMark Yao #include <drm/drmP.h> 1763ebb9faSMark Yao #include <drm/drm_atomic.h> 182048e328SMark Yao #include <drm/drm_crtc.h> 192048e328SMark Yao #include <drm/drm_crtc_helper.h> 202048e328SMark Yao #include <drm/drm_plane_helper.h> 212048e328SMark Yao 222048e328SMark Yao #include <linux/kernel.h> 2300fe6148SPaul Gortmaker #include <linux/module.h> 242048e328SMark Yao #include <linux/platform_device.h> 252048e328SMark Yao #include <linux/clk.h> 262048e328SMark Yao #include <linux/of.h> 272048e328SMark Yao #include <linux/of_device.h> 282048e328SMark Yao #include <linux/pm_runtime.h> 292048e328SMark Yao #include <linux/component.h> 302048e328SMark Yao 312048e328SMark Yao #include <linux/reset.h> 322048e328SMark Yao #include <linux/delay.h> 332048e328SMark Yao 342048e328SMark Yao #include "rockchip_drm_drv.h" 352048e328SMark Yao #include "rockchip_drm_gem.h" 362048e328SMark Yao #include "rockchip_drm_fb.h" 372048e328SMark Yao #include "rockchip_drm_vop.h" 382048e328SMark Yao 392048e328SMark Yao #define __REG_SET_RELAXED(x, off, mask, shift, v) \ 402048e328SMark Yao vop_mask_write_relaxed(x, off, (mask) << shift, (v) << shift) 412048e328SMark Yao #define __REG_SET_NORMAL(x, off, mask, shift, v) \ 422048e328SMark Yao vop_mask_write(x, off, (mask) << shift, (v) << shift) 432048e328SMark Yao 442048e328SMark Yao #define REG_SET(x, base, reg, v, mode) \ 452048e328SMark Yao __REG_SET_##mode(x, base + reg.offset, reg.mask, reg.shift, v) 46c7647f86SJohn Keeping #define REG_SET_MASK(x, base, reg, mask, v, mode) \ 47c7647f86SJohn Keeping __REG_SET_##mode(x, base + reg.offset, mask, reg.shift, v) 482048e328SMark Yao 492048e328SMark Yao #define VOP_WIN_SET(x, win, name, v) \ 502048e328SMark Yao REG_SET(x, win->base, win->phy->name, v, RELAXED) 514c156c21SMark Yao #define VOP_SCL_SET(x, win, name, v) \ 524c156c21SMark Yao REG_SET(x, win->base, win->phy->scl->name, v, RELAXED) 531194fffbSMark Yao #define VOP_SCL_SET_EXT(x, win, name, v) \ 541194fffbSMark Yao REG_SET(x, win->base, win->phy->scl->ext->name, v, RELAXED) 552048e328SMark Yao #define VOP_CTRL_SET(x, name, v) \ 562048e328SMark Yao REG_SET(x, 0, (x)->data->ctrl->name, v, NORMAL) 572048e328SMark Yao 58dbb3d944SMark Yao #define VOP_INTR_GET(vop, name) \ 59dbb3d944SMark Yao vop_read_reg(vop, 0, &vop->data->ctrl->name) 60dbb3d944SMark Yao 61c7647f86SJohn Keeping #define VOP_INTR_SET(vop, name, mask, v) \ 62c7647f86SJohn Keeping REG_SET_MASK(vop, 0, vop->data->intr->name, mask, v, NORMAL) 63dbb3d944SMark Yao #define VOP_INTR_SET_TYPE(vop, name, type, v) \ 64dbb3d944SMark Yao do { \ 65c7647f86SJohn Keeping int i, reg = 0, mask = 0; \ 66dbb3d944SMark Yao for (i = 0; i < vop->data->intr->nintrs; i++) { \ 67c7647f86SJohn Keeping if (vop->data->intr->intrs[i] & type) { \ 68dbb3d944SMark Yao reg |= (v) << i; \ 69c7647f86SJohn Keeping mask |= 1 << i; \ 70dbb3d944SMark Yao } \ 71c7647f86SJohn Keeping } \ 72c7647f86SJohn Keeping VOP_INTR_SET(vop, name, mask, reg); \ 73dbb3d944SMark Yao } while (0) 74dbb3d944SMark Yao #define VOP_INTR_GET_TYPE(vop, name, type) \ 75dbb3d944SMark Yao vop_get_intr_type(vop, &vop->data->intr->name, type) 76dbb3d944SMark Yao 772048e328SMark Yao #define VOP_WIN_GET(x, win, name) \ 782048e328SMark Yao vop_read_reg(x, win->base, &win->phy->name) 792048e328SMark Yao 802048e328SMark Yao #define VOP_WIN_GET_YRGBADDR(vop, win) \ 812048e328SMark Yao vop_readl(vop, win->base + win->phy->yrgb_mst.offset) 822048e328SMark Yao 832048e328SMark Yao #define to_vop(x) container_of(x, struct vop, crtc) 842048e328SMark Yao #define to_vop_win(x) container_of(x, struct vop_win, base) 8563ebb9faSMark Yao #define to_vop_plane_state(x) container_of(x, struct vop_plane_state, base) 862048e328SMark Yao 8763ebb9faSMark Yao struct vop_plane_state { 8863ebb9faSMark Yao struct drm_plane_state base; 8963ebb9faSMark Yao int format; 902048e328SMark Yao dma_addr_t yrgb_mst; 9163ebb9faSMark Yao bool enable; 922048e328SMark Yao }; 932048e328SMark Yao 942048e328SMark Yao struct vop_win { 952048e328SMark Yao struct drm_plane base; 962048e328SMark Yao const struct vop_win_data *data; 972048e328SMark Yao struct vop *vop; 982048e328SMark Yao 994f9d39a7SDaniel Vetter /* protected by dev->event_lock */ 1004f9d39a7SDaniel Vetter bool enable; 1014f9d39a7SDaniel Vetter dma_addr_t yrgb_mst; 1022048e328SMark Yao }; 1032048e328SMark Yao 1042048e328SMark Yao struct vop { 1052048e328SMark Yao struct drm_crtc crtc; 1062048e328SMark Yao struct device *dev; 1072048e328SMark Yao struct drm_device *drm_dev; 10831e980c5SMark Yao bool is_enabled; 1092048e328SMark Yao 1102048e328SMark Yao /* mutex vsync_ work */ 1112048e328SMark Yao struct mutex vsync_mutex; 1122048e328SMark Yao bool vsync_work_pending; 1131067219bSMark Yao struct completion dsp_hold_completion; 11463ebb9faSMark Yao struct completion wait_update_complete; 1154f9d39a7SDaniel Vetter 1164f9d39a7SDaniel Vetter /* protected by dev->event_lock */ 11763ebb9faSMark Yao struct drm_pending_vblank_event *event; 1182048e328SMark Yao 1192048e328SMark Yao const struct vop_data *data; 1202048e328SMark Yao 1212048e328SMark Yao uint32_t *regsbak; 1222048e328SMark Yao void __iomem *regs; 1232048e328SMark Yao 1242048e328SMark Yao /* physical map length of vop register */ 1252048e328SMark Yao uint32_t len; 1262048e328SMark Yao 1272048e328SMark Yao /* one time only one process allowed to config the register */ 1282048e328SMark Yao spinlock_t reg_lock; 1292048e328SMark Yao /* lock vop irq reg */ 1302048e328SMark Yao spinlock_t irq_lock; 1312048e328SMark Yao 1322048e328SMark Yao unsigned int irq; 1332048e328SMark Yao 1342048e328SMark Yao /* vop AHP clk */ 1352048e328SMark Yao struct clk *hclk; 1362048e328SMark Yao /* vop dclk */ 1372048e328SMark Yao struct clk *dclk; 1382048e328SMark Yao /* vop share memory frequency */ 1392048e328SMark Yao struct clk *aclk; 1402048e328SMark Yao 1412048e328SMark Yao /* vop dclk reset */ 1422048e328SMark Yao struct reset_control *dclk_rst; 1432048e328SMark Yao 1442048e328SMark Yao struct vop_win win[]; 1452048e328SMark Yao }; 1462048e328SMark Yao 1472048e328SMark Yao static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v) 1482048e328SMark Yao { 1492048e328SMark Yao writel(v, vop->regs + offset); 1502048e328SMark Yao vop->regsbak[offset >> 2] = v; 1512048e328SMark Yao } 1522048e328SMark Yao 1532048e328SMark Yao static inline uint32_t vop_readl(struct vop *vop, uint32_t offset) 1542048e328SMark Yao { 1552048e328SMark Yao return readl(vop->regs + offset); 1562048e328SMark Yao } 1572048e328SMark Yao 1582048e328SMark Yao static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base, 1592048e328SMark Yao const struct vop_reg *reg) 1602048e328SMark Yao { 1612048e328SMark Yao return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask; 1622048e328SMark Yao } 1632048e328SMark Yao 1642048e328SMark Yao static inline void vop_mask_write(struct vop *vop, uint32_t offset, 1652048e328SMark Yao uint32_t mask, uint32_t v) 1662048e328SMark Yao { 1672048e328SMark Yao if (mask) { 1682048e328SMark Yao uint32_t cached_val = vop->regsbak[offset >> 2]; 1692048e328SMark Yao 1702048e328SMark Yao cached_val = (cached_val & ~mask) | v; 1712048e328SMark Yao writel(cached_val, vop->regs + offset); 1722048e328SMark Yao vop->regsbak[offset >> 2] = cached_val; 1732048e328SMark Yao } 1742048e328SMark Yao } 1752048e328SMark Yao 1762048e328SMark Yao static inline void vop_mask_write_relaxed(struct vop *vop, uint32_t offset, 1772048e328SMark Yao uint32_t mask, uint32_t v) 1782048e328SMark Yao { 1792048e328SMark Yao if (mask) { 1802048e328SMark Yao uint32_t cached_val = vop->regsbak[offset >> 2]; 1812048e328SMark Yao 1822048e328SMark Yao cached_val = (cached_val & ~mask) | v; 1832048e328SMark Yao writel_relaxed(cached_val, vop->regs + offset); 1842048e328SMark Yao vop->regsbak[offset >> 2] = cached_val; 1852048e328SMark Yao } 1862048e328SMark Yao } 1872048e328SMark Yao 188dbb3d944SMark Yao static inline uint32_t vop_get_intr_type(struct vop *vop, 189dbb3d944SMark Yao const struct vop_reg *reg, int type) 190dbb3d944SMark Yao { 191dbb3d944SMark Yao uint32_t i, ret = 0; 192dbb3d944SMark Yao uint32_t regs = vop_read_reg(vop, 0, reg); 193dbb3d944SMark Yao 194dbb3d944SMark Yao for (i = 0; i < vop->data->intr->nintrs; i++) { 195dbb3d944SMark Yao if ((type & vop->data->intr->intrs[i]) && (regs & 1 << i)) 196dbb3d944SMark Yao ret |= vop->data->intr->intrs[i]; 197dbb3d944SMark Yao } 198dbb3d944SMark Yao 199dbb3d944SMark Yao return ret; 200dbb3d944SMark Yao } 201dbb3d944SMark Yao 2020cf33fe3SMark Yao static inline void vop_cfg_done(struct vop *vop) 2030cf33fe3SMark Yao { 2040cf33fe3SMark Yao VOP_CTRL_SET(vop, cfg_done, 1); 2050cf33fe3SMark Yao } 2060cf33fe3SMark Yao 20785a359f2STomasz Figa static bool has_rb_swapped(uint32_t format) 20885a359f2STomasz Figa { 20985a359f2STomasz Figa switch (format) { 21085a359f2STomasz Figa case DRM_FORMAT_XBGR8888: 21185a359f2STomasz Figa case DRM_FORMAT_ABGR8888: 21285a359f2STomasz Figa case DRM_FORMAT_BGR888: 21385a359f2STomasz Figa case DRM_FORMAT_BGR565: 21485a359f2STomasz Figa return true; 21585a359f2STomasz Figa default: 21685a359f2STomasz Figa return false; 21785a359f2STomasz Figa } 21885a359f2STomasz Figa } 21985a359f2STomasz Figa 2202048e328SMark Yao static enum vop_data_format vop_convert_format(uint32_t format) 2212048e328SMark Yao { 2222048e328SMark Yao switch (format) { 2232048e328SMark Yao case DRM_FORMAT_XRGB8888: 2242048e328SMark Yao case DRM_FORMAT_ARGB8888: 22585a359f2STomasz Figa case DRM_FORMAT_XBGR8888: 22685a359f2STomasz Figa case DRM_FORMAT_ABGR8888: 2272048e328SMark Yao return VOP_FMT_ARGB8888; 2282048e328SMark Yao case DRM_FORMAT_RGB888: 22985a359f2STomasz Figa case DRM_FORMAT_BGR888: 2302048e328SMark Yao return VOP_FMT_RGB888; 2312048e328SMark Yao case DRM_FORMAT_RGB565: 23285a359f2STomasz Figa case DRM_FORMAT_BGR565: 2332048e328SMark Yao return VOP_FMT_RGB565; 2342048e328SMark Yao case DRM_FORMAT_NV12: 2352048e328SMark Yao return VOP_FMT_YUV420SP; 2362048e328SMark Yao case DRM_FORMAT_NV16: 2372048e328SMark Yao return VOP_FMT_YUV422SP; 2382048e328SMark Yao case DRM_FORMAT_NV24: 2392048e328SMark Yao return VOP_FMT_YUV444SP; 2402048e328SMark Yao default: 2412048e328SMark Yao DRM_ERROR("unsupport format[%08x]\n", format); 2422048e328SMark Yao return -EINVAL; 2432048e328SMark Yao } 2442048e328SMark Yao } 2452048e328SMark Yao 24684c7f8caSMark Yao static bool is_yuv_support(uint32_t format) 24784c7f8caSMark Yao { 24884c7f8caSMark Yao switch (format) { 24984c7f8caSMark Yao case DRM_FORMAT_NV12: 25084c7f8caSMark Yao case DRM_FORMAT_NV16: 25184c7f8caSMark Yao case DRM_FORMAT_NV24: 25284c7f8caSMark Yao return true; 25384c7f8caSMark Yao default: 25484c7f8caSMark Yao return false; 25584c7f8caSMark Yao } 25684c7f8caSMark Yao } 25784c7f8caSMark Yao 2582048e328SMark Yao static bool is_alpha_support(uint32_t format) 2592048e328SMark Yao { 2602048e328SMark Yao switch (format) { 2612048e328SMark Yao case DRM_FORMAT_ARGB8888: 26285a359f2STomasz Figa case DRM_FORMAT_ABGR8888: 2632048e328SMark Yao return true; 2642048e328SMark Yao default: 2652048e328SMark Yao return false; 2662048e328SMark Yao } 2672048e328SMark Yao } 2682048e328SMark Yao 2694c156c21SMark Yao static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src, 2704c156c21SMark Yao uint32_t dst, bool is_horizontal, 2714c156c21SMark Yao int vsu_mode, int *vskiplines) 2724c156c21SMark Yao { 2734c156c21SMark Yao uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT; 2744c156c21SMark Yao 2754c156c21SMark Yao if (is_horizontal) { 2764c156c21SMark Yao if (mode == SCALE_UP) 2774c156c21SMark Yao val = GET_SCL_FT_BIC(src, dst); 2784c156c21SMark Yao else if (mode == SCALE_DOWN) 2794c156c21SMark Yao val = GET_SCL_FT_BILI_DN(src, dst); 2804c156c21SMark Yao } else { 2814c156c21SMark Yao if (mode == SCALE_UP) { 2824c156c21SMark Yao if (vsu_mode == SCALE_UP_BIL) 2834c156c21SMark Yao val = GET_SCL_FT_BILI_UP(src, dst); 2844c156c21SMark Yao else 2854c156c21SMark Yao val = GET_SCL_FT_BIC(src, dst); 2864c156c21SMark Yao } else if (mode == SCALE_DOWN) { 2874c156c21SMark Yao if (vskiplines) { 2884c156c21SMark Yao *vskiplines = scl_get_vskiplines(src, dst); 2894c156c21SMark Yao val = scl_get_bili_dn_vskip(src, dst, 2904c156c21SMark Yao *vskiplines); 2914c156c21SMark Yao } else { 2924c156c21SMark Yao val = GET_SCL_FT_BILI_DN(src, dst); 2934c156c21SMark Yao } 2944c156c21SMark Yao } 2954c156c21SMark Yao } 2964c156c21SMark Yao 2974c156c21SMark Yao return val; 2984c156c21SMark Yao } 2994c156c21SMark Yao 3004c156c21SMark Yao static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win, 3014c156c21SMark Yao uint32_t src_w, uint32_t src_h, uint32_t dst_w, 3024c156c21SMark Yao uint32_t dst_h, uint32_t pixel_format) 3034c156c21SMark Yao { 3044c156c21SMark Yao uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode; 3054c156c21SMark Yao uint16_t cbcr_hor_scl_mode = SCALE_NONE; 3064c156c21SMark Yao uint16_t cbcr_ver_scl_mode = SCALE_NONE; 3074c156c21SMark Yao int hsub = drm_format_horz_chroma_subsampling(pixel_format); 3084c156c21SMark Yao int vsub = drm_format_vert_chroma_subsampling(pixel_format); 3094c156c21SMark Yao bool is_yuv = is_yuv_support(pixel_format); 3104c156c21SMark Yao uint16_t cbcr_src_w = src_w / hsub; 3114c156c21SMark Yao uint16_t cbcr_src_h = src_h / vsub; 3124c156c21SMark Yao uint16_t vsu_mode; 3134c156c21SMark Yao uint16_t lb_mode; 3144c156c21SMark Yao uint32_t val; 3152db00cf5SMark Yao int vskiplines = 0; 3164c156c21SMark Yao 3174c156c21SMark Yao if (dst_w > 3840) { 3184c156c21SMark Yao DRM_ERROR("Maximum destination width (3840) exceeded\n"); 3194c156c21SMark Yao return; 3204c156c21SMark Yao } 3214c156c21SMark Yao 3221194fffbSMark Yao if (!win->phy->scl->ext) { 3231194fffbSMark Yao VOP_SCL_SET(vop, win, scale_yrgb_x, 3241194fffbSMark Yao scl_cal_scale2(src_w, dst_w)); 3251194fffbSMark Yao VOP_SCL_SET(vop, win, scale_yrgb_y, 3261194fffbSMark Yao scl_cal_scale2(src_h, dst_h)); 3271194fffbSMark Yao if (is_yuv) { 3281194fffbSMark Yao VOP_SCL_SET(vop, win, scale_cbcr_x, 329ee8662fcSMark Yao scl_cal_scale2(cbcr_src_w, dst_w)); 3301194fffbSMark Yao VOP_SCL_SET(vop, win, scale_cbcr_y, 331ee8662fcSMark Yao scl_cal_scale2(cbcr_src_h, dst_h)); 3321194fffbSMark Yao } 3331194fffbSMark Yao return; 3341194fffbSMark Yao } 3351194fffbSMark Yao 3364c156c21SMark Yao yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w); 3374c156c21SMark Yao yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h); 3384c156c21SMark Yao 3394c156c21SMark Yao if (is_yuv) { 3404c156c21SMark Yao cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w); 3414c156c21SMark Yao cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h); 3424c156c21SMark Yao if (cbcr_hor_scl_mode == SCALE_DOWN) 3434c156c21SMark Yao lb_mode = scl_vop_cal_lb_mode(dst_w, true); 3444c156c21SMark Yao else 3454c156c21SMark Yao lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true); 3464c156c21SMark Yao } else { 3474c156c21SMark Yao if (yrgb_hor_scl_mode == SCALE_DOWN) 3484c156c21SMark Yao lb_mode = scl_vop_cal_lb_mode(dst_w, false); 3494c156c21SMark Yao else 3504c156c21SMark Yao lb_mode = scl_vop_cal_lb_mode(src_w, false); 3514c156c21SMark Yao } 3524c156c21SMark Yao 3531194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, lb_mode, lb_mode); 3544c156c21SMark Yao if (lb_mode == LB_RGB_3840X2) { 3554c156c21SMark Yao if (yrgb_ver_scl_mode != SCALE_NONE) { 3564c156c21SMark Yao DRM_ERROR("ERROR : not allow yrgb ver scale\n"); 3574c156c21SMark Yao return; 3584c156c21SMark Yao } 3594c156c21SMark Yao if (cbcr_ver_scl_mode != SCALE_NONE) { 3604c156c21SMark Yao DRM_ERROR("ERROR : not allow cbcr ver scale\n"); 3614c156c21SMark Yao return; 3624c156c21SMark Yao } 3634c156c21SMark Yao vsu_mode = SCALE_UP_BIL; 3644c156c21SMark Yao } else if (lb_mode == LB_RGB_2560X4) { 3654c156c21SMark Yao vsu_mode = SCALE_UP_BIL; 3664c156c21SMark Yao } else { 3674c156c21SMark Yao vsu_mode = SCALE_UP_BIC; 3684c156c21SMark Yao } 3694c156c21SMark Yao 3704c156c21SMark Yao val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w, 3714c156c21SMark Yao true, 0, NULL); 3724c156c21SMark Yao VOP_SCL_SET(vop, win, scale_yrgb_x, val); 3734c156c21SMark Yao val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h, 3744c156c21SMark Yao false, vsu_mode, &vskiplines); 3754c156c21SMark Yao VOP_SCL_SET(vop, win, scale_yrgb_y, val); 3764c156c21SMark Yao 3771194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt4, vskiplines == 4); 3781194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt2, vskiplines == 2); 3794c156c21SMark Yao 3801194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode); 3811194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode); 3821194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL); 3831194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL); 3841194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, yrgb_vsu_mode, vsu_mode); 3854c156c21SMark Yao if (is_yuv) { 3864c156c21SMark Yao val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w, 3874c156c21SMark Yao dst_w, true, 0, NULL); 3884c156c21SMark Yao VOP_SCL_SET(vop, win, scale_cbcr_x, val); 3894c156c21SMark Yao val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h, 3904c156c21SMark Yao dst_h, false, vsu_mode, &vskiplines); 3914c156c21SMark Yao VOP_SCL_SET(vop, win, scale_cbcr_y, val); 3924c156c21SMark Yao 3931194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt4, vskiplines == 4); 3941194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt2, vskiplines == 2); 3951194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode); 3961194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode); 3971194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL); 3981194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL); 3991194fffbSMark Yao VOP_SCL_SET_EXT(vop, win, cbcr_vsu_mode, vsu_mode); 4004c156c21SMark Yao } 4014c156c21SMark Yao } 4024c156c21SMark Yao 4031067219bSMark Yao static void vop_dsp_hold_valid_irq_enable(struct vop *vop) 4041067219bSMark Yao { 4051067219bSMark Yao unsigned long flags; 4061067219bSMark Yao 4071067219bSMark Yao if (WARN_ON(!vop->is_enabled)) 4081067219bSMark Yao return; 4091067219bSMark Yao 4101067219bSMark Yao spin_lock_irqsave(&vop->irq_lock, flags); 4111067219bSMark Yao 412dbb3d944SMark Yao VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 1); 4131067219bSMark Yao 4141067219bSMark Yao spin_unlock_irqrestore(&vop->irq_lock, flags); 4151067219bSMark Yao } 4161067219bSMark Yao 4171067219bSMark Yao static void vop_dsp_hold_valid_irq_disable(struct vop *vop) 4181067219bSMark Yao { 4191067219bSMark Yao unsigned long flags; 4201067219bSMark Yao 4211067219bSMark Yao if (WARN_ON(!vop->is_enabled)) 4221067219bSMark Yao return; 4231067219bSMark Yao 4241067219bSMark Yao spin_lock_irqsave(&vop->irq_lock, flags); 4251067219bSMark Yao 426dbb3d944SMark Yao VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 0); 4271067219bSMark Yao 4281067219bSMark Yao spin_unlock_irqrestore(&vop->irq_lock, flags); 4291067219bSMark Yao } 4301067219bSMark Yao 43163ebb9faSMark Yao static void vop_enable(struct drm_crtc *crtc) 4322048e328SMark Yao { 4332048e328SMark Yao struct vop *vop = to_vop(crtc); 4342048e328SMark Yao int ret; 4352048e328SMark Yao 4365d82d1a7SMark Yao ret = pm_runtime_get_sync(vop->dev); 4375d82d1a7SMark Yao if (ret < 0) { 4385d82d1a7SMark Yao dev_err(vop->dev, "failed to get pm runtime: %d\n", ret); 4395d82d1a7SMark Yao return; 4405d82d1a7SMark Yao } 4415d82d1a7SMark Yao 4422048e328SMark Yao ret = clk_enable(vop->hclk); 4432048e328SMark Yao if (ret < 0) { 4442048e328SMark Yao dev_err(vop->dev, "failed to enable hclk - %d\n", ret); 4452048e328SMark Yao return; 4462048e328SMark Yao } 4472048e328SMark Yao 4482048e328SMark Yao ret = clk_enable(vop->dclk); 4492048e328SMark Yao if (ret < 0) { 4502048e328SMark Yao dev_err(vop->dev, "failed to enable dclk - %d\n", ret); 4512048e328SMark Yao goto err_disable_hclk; 4522048e328SMark Yao } 4532048e328SMark Yao 4542048e328SMark Yao ret = clk_enable(vop->aclk); 4552048e328SMark Yao if (ret < 0) { 4562048e328SMark Yao dev_err(vop->dev, "failed to enable aclk - %d\n", ret); 4572048e328SMark Yao goto err_disable_dclk; 4582048e328SMark Yao } 4592048e328SMark Yao 4602048e328SMark Yao /* 4612048e328SMark Yao * Slave iommu shares power, irq and clock with vop. It was associated 4622048e328SMark Yao * automatically with this master device via common driver code. 4632048e328SMark Yao * Now that we have enabled the clock we attach it to the shared drm 4642048e328SMark Yao * mapping. 4652048e328SMark Yao */ 4662048e328SMark Yao ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev); 4672048e328SMark Yao if (ret) { 4682048e328SMark Yao dev_err(vop->dev, "failed to attach dma mapping, %d\n", ret); 4692048e328SMark Yao goto err_disable_aclk; 4702048e328SMark Yao } 4712048e328SMark Yao 47277faa161SMark Yao memcpy(vop->regs, vop->regsbak, vop->len); 47352ab7891SMark Yao /* 47452ab7891SMark Yao * At here, vop clock & iommu is enable, R/W vop regs would be safe. 47552ab7891SMark Yao */ 47652ab7891SMark Yao vop->is_enabled = true; 47752ab7891SMark Yao 4782048e328SMark Yao spin_lock(&vop->reg_lock); 4792048e328SMark Yao 4802048e328SMark Yao VOP_CTRL_SET(vop, standby, 0); 4812048e328SMark Yao 4822048e328SMark Yao spin_unlock(&vop->reg_lock); 4832048e328SMark Yao 4842048e328SMark Yao enable_irq(vop->irq); 4852048e328SMark Yao 486b5f7b755SMark Yao drm_crtc_vblank_on(crtc); 4872048e328SMark Yao 4882048e328SMark Yao return; 4892048e328SMark Yao 4902048e328SMark Yao err_disable_aclk: 4912048e328SMark Yao clk_disable(vop->aclk); 4922048e328SMark Yao err_disable_dclk: 4932048e328SMark Yao clk_disable(vop->dclk); 4942048e328SMark Yao err_disable_hclk: 4952048e328SMark Yao clk_disable(vop->hclk); 4962048e328SMark Yao } 4972048e328SMark Yao 4980ad3675dSMark Yao static void vop_crtc_disable(struct drm_crtc *crtc) 4992048e328SMark Yao { 5002048e328SMark Yao struct vop *vop = to_vop(crtc); 5013ed6c649STomeu Vizoso int i; 5022048e328SMark Yao 503893b6cadSDaniel Vetter WARN_ON(vop->event); 504893b6cadSDaniel Vetter 5053ed6c649STomeu Vizoso /* 5063ed6c649STomeu Vizoso * We need to make sure that all windows are disabled before we 5073ed6c649STomeu Vizoso * disable that crtc. Otherwise we might try to scan from a destroyed 5083ed6c649STomeu Vizoso * buffer later. 5093ed6c649STomeu Vizoso */ 5103ed6c649STomeu Vizoso for (i = 0; i < vop->data->win_size; i++) { 5113ed6c649STomeu Vizoso struct vop_win *vop_win = &vop->win[i]; 5123ed6c649STomeu Vizoso const struct vop_win_data *win = vop_win->data; 5133ed6c649STomeu Vizoso 5143ed6c649STomeu Vizoso spin_lock(&vop->reg_lock); 5153ed6c649STomeu Vizoso VOP_WIN_SET(vop, win, enable, 0); 5163ed6c649STomeu Vizoso spin_unlock(&vop->reg_lock); 5173ed6c649STomeu Vizoso } 5183ed6c649STomeu Vizoso 519b5f7b755SMark Yao drm_crtc_vblank_off(crtc); 5202048e328SMark Yao 5212048e328SMark Yao /* 5221067219bSMark Yao * Vop standby will take effect at end of current frame, 5231067219bSMark Yao * if dsp hold valid irq happen, it means standby complete. 5241067219bSMark Yao * 5251067219bSMark Yao * we must wait standby complete when we want to disable aclk, 5261067219bSMark Yao * if not, memory bus maybe dead. 5272048e328SMark Yao */ 5281067219bSMark Yao reinit_completion(&vop->dsp_hold_completion); 5291067219bSMark Yao vop_dsp_hold_valid_irq_enable(vop); 5301067219bSMark Yao 5312048e328SMark Yao spin_lock(&vop->reg_lock); 5322048e328SMark Yao 5332048e328SMark Yao VOP_CTRL_SET(vop, standby, 1); 5342048e328SMark Yao 5352048e328SMark Yao spin_unlock(&vop->reg_lock); 53652ab7891SMark Yao 5371067219bSMark Yao wait_for_completion(&vop->dsp_hold_completion); 5382048e328SMark Yao 5391067219bSMark Yao vop_dsp_hold_valid_irq_disable(vop); 5401067219bSMark Yao 5411067219bSMark Yao disable_irq(vop->irq); 5421067219bSMark Yao 5431067219bSMark Yao vop->is_enabled = false; 5441067219bSMark Yao 5451067219bSMark Yao /* 5461067219bSMark Yao * vop standby complete, so iommu detach is safe. 5471067219bSMark Yao */ 5482048e328SMark Yao rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev); 5492048e328SMark Yao 5501067219bSMark Yao clk_disable(vop->dclk); 5512048e328SMark Yao clk_disable(vop->aclk); 5522048e328SMark Yao clk_disable(vop->hclk); 5535d82d1a7SMark Yao pm_runtime_put(vop->dev); 554893b6cadSDaniel Vetter 555893b6cadSDaniel Vetter if (crtc->state->event && !crtc->state->active) { 556893b6cadSDaniel Vetter spin_lock_irq(&crtc->dev->event_lock); 557893b6cadSDaniel Vetter drm_crtc_send_vblank_event(crtc, crtc->state->event); 558893b6cadSDaniel Vetter spin_unlock_irq(&crtc->dev->event_lock); 559893b6cadSDaniel Vetter 560893b6cadSDaniel Vetter crtc->state->event = NULL; 561893b6cadSDaniel Vetter } 5622048e328SMark Yao } 5632048e328SMark Yao 56463ebb9faSMark Yao static void vop_plane_destroy(struct drm_plane *plane) 5652048e328SMark Yao { 56663ebb9faSMark Yao drm_plane_cleanup(plane); 5672048e328SMark Yao } 5682048e328SMark Yao 56944d0237aSMark Yao static int vop_plane_prepare_fb(struct drm_plane *plane, 57044d0237aSMark Yao const struct drm_plane_state *new_state) 57144d0237aSMark Yao { 57244d0237aSMark Yao if (plane->state->fb) 57344d0237aSMark Yao drm_framebuffer_reference(plane->state->fb); 57444d0237aSMark Yao 57544d0237aSMark Yao return 0; 57644d0237aSMark Yao } 57744d0237aSMark Yao 57844d0237aSMark Yao static void vop_plane_cleanup_fb(struct drm_plane *plane, 57944d0237aSMark Yao const struct drm_plane_state *old_state) 58044d0237aSMark Yao { 58144d0237aSMark Yao if (old_state->fb) 58244d0237aSMark Yao drm_framebuffer_unreference(old_state->fb); 58344d0237aSMark Yao } 58444d0237aSMark Yao 58563ebb9faSMark Yao static int vop_plane_atomic_check(struct drm_plane *plane, 58663ebb9faSMark Yao struct drm_plane_state *state) 5872048e328SMark Yao { 58863ebb9faSMark Yao struct drm_crtc *crtc = state->crtc; 58992915da6SJohn Keeping struct drm_crtc_state *crtc_state; 59063ebb9faSMark Yao struct drm_framebuffer *fb = state->fb; 5912048e328SMark Yao struct vop_win *vop_win = to_vop_win(plane); 59263ebb9faSMark Yao struct vop_plane_state *vop_plane_state = to_vop_plane_state(state); 5932048e328SMark Yao const struct vop_win_data *win = vop_win->data; 5942048e328SMark Yao bool visible; 5952048e328SMark Yao int ret; 596ac92028eSVille Syrjälä struct drm_rect *dest = &state->dst; 597ac92028eSVille Syrjälä struct drm_rect *src = &state->src; 59863ebb9faSMark Yao struct drm_rect clip; 5994c156c21SMark Yao int min_scale = win->phy->scl ? FRAC_16_16(1, 8) : 6004c156c21SMark Yao DRM_PLANE_HELPER_NO_SCALING; 6014c156c21SMark Yao int max_scale = win->phy->scl ? FRAC_16_16(8, 1) : 6024c156c21SMark Yao DRM_PLANE_HELPER_NO_SCALING; 6032048e328SMark Yao 60463ebb9faSMark Yao if (!crtc || !fb) 60563ebb9faSMark Yao goto out_disable; 60692915da6SJohn Keeping 60792915da6SJohn Keeping crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc); 60892915da6SJohn Keeping if (WARN_ON(!crtc_state)) 60992915da6SJohn Keeping return -EINVAL; 61092915da6SJohn Keeping 61163ebb9faSMark Yao src->x1 = state->src_x; 61263ebb9faSMark Yao src->y1 = state->src_y; 61363ebb9faSMark Yao src->x2 = state->src_x + state->src_w; 61463ebb9faSMark Yao src->y2 = state->src_y + state->src_h; 61563ebb9faSMark Yao dest->x1 = state->crtc_x; 61663ebb9faSMark Yao dest->y1 = state->crtc_y; 61763ebb9faSMark Yao dest->x2 = state->crtc_x + state->crtc_w; 61863ebb9faSMark Yao dest->y2 = state->crtc_y + state->crtc_h; 61963ebb9faSMark Yao 62063ebb9faSMark Yao clip.x1 = 0; 62163ebb9faSMark Yao clip.y1 = 0; 62292915da6SJohn Keeping clip.x2 = crtc_state->adjusted_mode.hdisplay; 62392915da6SJohn Keeping clip.y2 = crtc_state->adjusted_mode.vdisplay; 62463ebb9faSMark Yao 62563ebb9faSMark Yao ret = drm_plane_helper_check_update(plane, crtc, state->fb, 62663ebb9faSMark Yao src, dest, &clip, 6279b8b013dSVille Syrjälä state->rotation, 6284c156c21SMark Yao min_scale, 6294c156c21SMark Yao max_scale, 63063ebb9faSMark Yao true, true, &visible); 6312048e328SMark Yao if (ret) 6322048e328SMark Yao return ret; 6332048e328SMark Yao 6342048e328SMark Yao if (!visible) 63563ebb9faSMark Yao goto out_disable; 6362048e328SMark Yao 63763ebb9faSMark Yao vop_plane_state->format = vop_convert_format(fb->pixel_format); 63863ebb9faSMark Yao if (vop_plane_state->format < 0) 63963ebb9faSMark Yao return vop_plane_state->format; 64084c7f8caSMark Yao 64184c7f8caSMark Yao /* 64284c7f8caSMark Yao * Src.x1 can be odd when do clip, but yuv plane start point 64384c7f8caSMark Yao * need align with 2 pixel. 64484c7f8caSMark Yao */ 64563ebb9faSMark Yao if (is_yuv_support(fb->pixel_format) && ((src->x1 >> 16) % 2)) 64663ebb9faSMark Yao return -EINVAL; 64763ebb9faSMark Yao 64863ebb9faSMark Yao vop_plane_state->enable = true; 64963ebb9faSMark Yao 65063ebb9faSMark Yao return 0; 65163ebb9faSMark Yao 65263ebb9faSMark Yao out_disable: 65363ebb9faSMark Yao vop_plane_state->enable = false; 65463ebb9faSMark Yao return 0; 65584c7f8caSMark Yao } 65684c7f8caSMark Yao 65763ebb9faSMark Yao static void vop_plane_atomic_disable(struct drm_plane *plane, 65863ebb9faSMark Yao struct drm_plane_state *old_state) 65963ebb9faSMark Yao { 66063ebb9faSMark Yao struct vop_plane_state *vop_plane_state = to_vop_plane_state(old_state); 66163ebb9faSMark Yao struct vop_win *vop_win = to_vop_win(plane); 66263ebb9faSMark Yao const struct vop_win_data *win = vop_win->data; 66363ebb9faSMark Yao struct vop *vop = to_vop(old_state->crtc); 6642048e328SMark Yao 66563ebb9faSMark Yao if (!old_state->crtc) 66663ebb9faSMark Yao return; 6672048e328SMark Yao 6684f9d39a7SDaniel Vetter spin_lock_irq(&plane->dev->event_lock); 6694f9d39a7SDaniel Vetter vop_win->enable = false; 6704f9d39a7SDaniel Vetter vop_win->yrgb_mst = 0; 6714f9d39a7SDaniel Vetter spin_unlock_irq(&plane->dev->event_lock); 6724f9d39a7SDaniel Vetter 67363ebb9faSMark Yao spin_lock(&vop->reg_lock); 6742048e328SMark Yao 67563ebb9faSMark Yao VOP_WIN_SET(vop, win, enable, 0); 6762048e328SMark Yao 67763ebb9faSMark Yao spin_unlock(&vop->reg_lock); 67863ebb9faSMark Yao 67963ebb9faSMark Yao vop_plane_state->enable = false; 68063ebb9faSMark Yao } 68163ebb9faSMark Yao 68263ebb9faSMark Yao static void vop_plane_atomic_update(struct drm_plane *plane, 68363ebb9faSMark Yao struct drm_plane_state *old_state) 68463ebb9faSMark Yao { 68563ebb9faSMark Yao struct drm_plane_state *state = plane->state; 68663ebb9faSMark Yao struct drm_crtc *crtc = state->crtc; 68763ebb9faSMark Yao struct vop_win *vop_win = to_vop_win(plane); 68863ebb9faSMark Yao struct vop_plane_state *vop_plane_state = to_vop_plane_state(state); 68963ebb9faSMark Yao const struct vop_win_data *win = vop_win->data; 69063ebb9faSMark Yao struct vop *vop = to_vop(state->crtc); 69163ebb9faSMark Yao struct drm_framebuffer *fb = state->fb; 69263ebb9faSMark Yao unsigned int actual_w, actual_h; 69363ebb9faSMark Yao unsigned int dsp_stx, dsp_sty; 69463ebb9faSMark Yao uint32_t act_info, dsp_info, dsp_st; 695ac92028eSVille Syrjälä struct drm_rect *src = &state->src; 696ac92028eSVille Syrjälä struct drm_rect *dest = &state->dst; 69763ebb9faSMark Yao struct drm_gem_object *obj, *uv_obj; 69863ebb9faSMark Yao struct rockchip_gem_object *rk_obj, *rk_uv_obj; 69963ebb9faSMark Yao unsigned long offset; 70063ebb9faSMark Yao dma_addr_t dma_addr; 70163ebb9faSMark Yao uint32_t val; 70263ebb9faSMark Yao bool rb_swap; 70363ebb9faSMark Yao 70463ebb9faSMark Yao /* 70563ebb9faSMark Yao * can't update plane when vop is disabled. 70663ebb9faSMark Yao */ 7074f9d39a7SDaniel Vetter if (WARN_ON(!crtc)) 70863ebb9faSMark Yao return; 70963ebb9faSMark Yao 71063ebb9faSMark Yao if (WARN_ON(!vop->is_enabled)) 71163ebb9faSMark Yao return; 71263ebb9faSMark Yao 71363ebb9faSMark Yao if (!vop_plane_state->enable) { 71463ebb9faSMark Yao vop_plane_atomic_disable(plane, old_state); 71563ebb9faSMark Yao return; 71663ebb9faSMark Yao } 71763ebb9faSMark Yao 71863ebb9faSMark Yao obj = rockchip_fb_get_gem_obj(fb, 0); 71963ebb9faSMark Yao rk_obj = to_rockchip_obj(obj); 72063ebb9faSMark Yao 72163ebb9faSMark Yao actual_w = drm_rect_width(src) >> 16; 72263ebb9faSMark Yao actual_h = drm_rect_height(src) >> 16; 72363ebb9faSMark Yao act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff); 72463ebb9faSMark Yao 72563ebb9faSMark Yao dsp_info = (drm_rect_height(dest) - 1) << 16; 72663ebb9faSMark Yao dsp_info |= (drm_rect_width(dest) - 1) & 0xffff; 72763ebb9faSMark Yao 72863ebb9faSMark Yao dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start; 72963ebb9faSMark Yao dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start; 73063ebb9faSMark Yao dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff); 73163ebb9faSMark Yao 73263ebb9faSMark Yao offset = (src->x1 >> 16) * drm_format_plane_cpp(fb->pixel_format, 0); 73363ebb9faSMark Yao offset += (src->y1 >> 16) * fb->pitches[0]; 73463ebb9faSMark Yao vop_plane_state->yrgb_mst = rk_obj->dma_addr + offset + fb->offsets[0]; 73563ebb9faSMark Yao 7364f9d39a7SDaniel Vetter spin_lock_irq(&plane->dev->event_lock); 7374f9d39a7SDaniel Vetter vop_win->enable = true; 7384f9d39a7SDaniel Vetter vop_win->yrgb_mst = vop_plane_state->yrgb_mst; 7394f9d39a7SDaniel Vetter spin_unlock_irq(&plane->dev->event_lock); 7404f9d39a7SDaniel Vetter 74163ebb9faSMark Yao spin_lock(&vop->reg_lock); 74263ebb9faSMark Yao 74363ebb9faSMark Yao VOP_WIN_SET(vop, win, format, vop_plane_state->format); 74463ebb9faSMark Yao VOP_WIN_SET(vop, win, yrgb_vir, fb->pitches[0] >> 2); 74563ebb9faSMark Yao VOP_WIN_SET(vop, win, yrgb_mst, vop_plane_state->yrgb_mst); 74663ebb9faSMark Yao if (is_yuv_support(fb->pixel_format)) { 74784c7f8caSMark Yao int hsub = drm_format_horz_chroma_subsampling(fb->pixel_format); 74884c7f8caSMark Yao int vsub = drm_format_vert_chroma_subsampling(fb->pixel_format); 74984c7f8caSMark Yao int bpp = drm_format_plane_cpp(fb->pixel_format, 1); 75084c7f8caSMark Yao 75184c7f8caSMark Yao uv_obj = rockchip_fb_get_gem_obj(fb, 1); 75284c7f8caSMark Yao rk_uv_obj = to_rockchip_obj(uv_obj); 75384c7f8caSMark Yao 75463ebb9faSMark Yao offset = (src->x1 >> 16) * bpp / hsub; 75563ebb9faSMark Yao offset += (src->y1 >> 16) * fb->pitches[1] / vsub; 75684c7f8caSMark Yao 75763ebb9faSMark Yao dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1]; 75863ebb9faSMark Yao VOP_WIN_SET(vop, win, uv_vir, fb->pitches[1] >> 2); 75963ebb9faSMark Yao VOP_WIN_SET(vop, win, uv_mst, dma_addr); 76084c7f8caSMark Yao } 7614c156c21SMark Yao 7624c156c21SMark Yao if (win->phy->scl) 7634c156c21SMark Yao scl_vop_cal_scl_fac(vop, win, actual_w, actual_h, 76463ebb9faSMark Yao drm_rect_width(dest), drm_rect_height(dest), 7654c156c21SMark Yao fb->pixel_format); 7664c156c21SMark Yao 76763ebb9faSMark Yao VOP_WIN_SET(vop, win, act_info, act_info); 76863ebb9faSMark Yao VOP_WIN_SET(vop, win, dsp_info, dsp_info); 76963ebb9faSMark Yao VOP_WIN_SET(vop, win, dsp_st, dsp_st); 7704c156c21SMark Yao 77163ebb9faSMark Yao rb_swap = has_rb_swapped(fb->pixel_format); 77285a359f2STomasz Figa VOP_WIN_SET(vop, win, rb_swap, rb_swap); 7732048e328SMark Yao 77463ebb9faSMark Yao if (is_alpha_support(fb->pixel_format)) { 7752048e328SMark Yao VOP_WIN_SET(vop, win, dst_alpha_ctl, 7762048e328SMark Yao DST_FACTOR_M0(ALPHA_SRC_INVERSE)); 7772048e328SMark Yao val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) | 7782048e328SMark Yao SRC_ALPHA_M0(ALPHA_STRAIGHT) | 7792048e328SMark Yao SRC_BLEND_M0(ALPHA_PER_PIX) | 7802048e328SMark Yao SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) | 7812048e328SMark Yao SRC_FACTOR_M0(ALPHA_ONE); 7822048e328SMark Yao VOP_WIN_SET(vop, win, src_alpha_ctl, val); 7832048e328SMark Yao } else { 7842048e328SMark Yao VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0)); 7852048e328SMark Yao } 7862048e328SMark Yao 7872048e328SMark Yao VOP_WIN_SET(vop, win, enable, 1); 7882048e328SMark Yao spin_unlock(&vop->reg_lock); 7892048e328SMark Yao } 7902048e328SMark Yao 79163ebb9faSMark Yao static const struct drm_plane_helper_funcs plane_helper_funcs = { 79244d0237aSMark Yao .prepare_fb = vop_plane_prepare_fb, 79344d0237aSMark Yao .cleanup_fb = vop_plane_cleanup_fb, 79463ebb9faSMark Yao .atomic_check = vop_plane_atomic_check, 79563ebb9faSMark Yao .atomic_update = vop_plane_atomic_update, 79663ebb9faSMark Yao .atomic_disable = vop_plane_atomic_disable, 79763ebb9faSMark Yao }; 79863ebb9faSMark Yao 7998ff490aeSJohn Keeping static void vop_atomic_plane_reset(struct drm_plane *plane) 8002048e328SMark Yao { 80163ebb9faSMark Yao struct vop_plane_state *vop_plane_state = 80263ebb9faSMark Yao to_vop_plane_state(plane->state); 80363ebb9faSMark Yao 80463ebb9faSMark Yao if (plane->state && plane->state->fb) 80563ebb9faSMark Yao drm_framebuffer_unreference(plane->state->fb); 80663ebb9faSMark Yao 80763ebb9faSMark Yao kfree(vop_plane_state); 80863ebb9faSMark Yao vop_plane_state = kzalloc(sizeof(*vop_plane_state), GFP_KERNEL); 80963ebb9faSMark Yao if (!vop_plane_state) 81063ebb9faSMark Yao return; 81163ebb9faSMark Yao 81263ebb9faSMark Yao plane->state = &vop_plane_state->base; 81363ebb9faSMark Yao plane->state->plane = plane; 8142048e328SMark Yao } 8152048e328SMark Yao 8168ff490aeSJohn Keeping static struct drm_plane_state * 81763ebb9faSMark Yao vop_atomic_plane_duplicate_state(struct drm_plane *plane) 8182048e328SMark Yao { 81963ebb9faSMark Yao struct vop_plane_state *old_vop_plane_state; 82063ebb9faSMark Yao struct vop_plane_state *vop_plane_state; 8212048e328SMark Yao 82263ebb9faSMark Yao if (WARN_ON(!plane->state)) 82363ebb9faSMark Yao return NULL; 8242048e328SMark Yao 82563ebb9faSMark Yao old_vop_plane_state = to_vop_plane_state(plane->state); 82663ebb9faSMark Yao vop_plane_state = kmemdup(old_vop_plane_state, 82763ebb9faSMark Yao sizeof(*vop_plane_state), GFP_KERNEL); 82863ebb9faSMark Yao if (!vop_plane_state) 82963ebb9faSMark Yao return NULL; 83063ebb9faSMark Yao 83163ebb9faSMark Yao __drm_atomic_helper_plane_duplicate_state(plane, 83263ebb9faSMark Yao &vop_plane_state->base); 83363ebb9faSMark Yao 83463ebb9faSMark Yao return &vop_plane_state->base; 8352048e328SMark Yao } 8362048e328SMark Yao 83763ebb9faSMark Yao static void vop_atomic_plane_destroy_state(struct drm_plane *plane, 83863ebb9faSMark Yao struct drm_plane_state *state) 8392048e328SMark Yao { 84063ebb9faSMark Yao struct vop_plane_state *vop_state = to_vop_plane_state(state); 8412048e328SMark Yao 8422f701695SDaniel Vetter __drm_atomic_helper_plane_destroy_state(state); 8432048e328SMark Yao 84463ebb9faSMark Yao kfree(vop_state); 8452048e328SMark Yao } 8462048e328SMark Yao 8472048e328SMark Yao static const struct drm_plane_funcs vop_plane_funcs = { 84863ebb9faSMark Yao .update_plane = drm_atomic_helper_update_plane, 84963ebb9faSMark Yao .disable_plane = drm_atomic_helper_disable_plane, 8502048e328SMark Yao .destroy = vop_plane_destroy, 85163ebb9faSMark Yao .reset = vop_atomic_plane_reset, 85263ebb9faSMark Yao .atomic_duplicate_state = vop_atomic_plane_duplicate_state, 85363ebb9faSMark Yao .atomic_destroy_state = vop_atomic_plane_destroy_state, 8542048e328SMark Yao }; 8552048e328SMark Yao 8562048e328SMark Yao static int vop_crtc_enable_vblank(struct drm_crtc *crtc) 8572048e328SMark Yao { 8582048e328SMark Yao struct vop *vop = to_vop(crtc); 8592048e328SMark Yao unsigned long flags; 8602048e328SMark Yao 86163ebb9faSMark Yao if (WARN_ON(!vop->is_enabled)) 8622048e328SMark Yao return -EPERM; 8632048e328SMark Yao 8642048e328SMark Yao spin_lock_irqsave(&vop->irq_lock, flags); 8652048e328SMark Yao 866dbb3d944SMark Yao VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 1); 8672048e328SMark Yao 8682048e328SMark Yao spin_unlock_irqrestore(&vop->irq_lock, flags); 8692048e328SMark Yao 8702048e328SMark Yao return 0; 8712048e328SMark Yao } 8722048e328SMark Yao 8732048e328SMark Yao static void vop_crtc_disable_vblank(struct drm_crtc *crtc) 8742048e328SMark Yao { 8752048e328SMark Yao struct vop *vop = to_vop(crtc); 8762048e328SMark Yao unsigned long flags; 8772048e328SMark Yao 87863ebb9faSMark Yao if (WARN_ON(!vop->is_enabled)) 8792048e328SMark Yao return; 88031e980c5SMark Yao 8812048e328SMark Yao spin_lock_irqsave(&vop->irq_lock, flags); 882dbb3d944SMark Yao 883dbb3d944SMark Yao VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 0); 884dbb3d944SMark Yao 8852048e328SMark Yao spin_unlock_irqrestore(&vop->irq_lock, flags); 8862048e328SMark Yao } 8872048e328SMark Yao 88863ebb9faSMark Yao static void vop_crtc_wait_for_update(struct drm_crtc *crtc) 88963ebb9faSMark Yao { 89063ebb9faSMark Yao struct vop *vop = to_vop(crtc); 89163ebb9faSMark Yao 89263ebb9faSMark Yao reinit_completion(&vop->wait_update_complete); 89363ebb9faSMark Yao WARN_ON(!wait_for_completion_timeout(&vop->wait_update_complete, 100)); 89463ebb9faSMark Yao } 89563ebb9faSMark Yao 8962048e328SMark Yao static const struct rockchip_crtc_funcs private_crtc_funcs = { 8972048e328SMark Yao .enable_vblank = vop_crtc_enable_vblank, 8982048e328SMark Yao .disable_vblank = vop_crtc_disable_vblank, 89963ebb9faSMark Yao .wait_for_update = vop_crtc_wait_for_update, 9002048e328SMark Yao }; 9012048e328SMark Yao 9022048e328SMark Yao static bool vop_crtc_mode_fixup(struct drm_crtc *crtc, 9032048e328SMark Yao const struct drm_display_mode *mode, 9042048e328SMark Yao struct drm_display_mode *adjusted_mode) 9052048e328SMark Yao { 906b59b8de3SChris Zhong struct vop *vop = to_vop(crtc); 907b59b8de3SChris Zhong 908b59b8de3SChris Zhong adjusted_mode->clock = 909b59b8de3SChris Zhong clk_round_rate(vop->dclk, mode->clock * 1000) / 1000; 910b59b8de3SChris Zhong 9112048e328SMark Yao return true; 9122048e328SMark Yao } 9132048e328SMark Yao 91463ebb9faSMark Yao static void vop_crtc_enable(struct drm_crtc *crtc) 9152048e328SMark Yao { 9162048e328SMark Yao struct vop *vop = to_vop(crtc); 9174e257d9eSMark Yao struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc->state); 91863ebb9faSMark Yao struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode; 9192048e328SMark Yao u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start; 9202048e328SMark Yao u16 hdisplay = adjusted_mode->hdisplay; 9212048e328SMark Yao u16 htotal = adjusted_mode->htotal; 9222048e328SMark Yao u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start; 9232048e328SMark Yao u16 hact_end = hact_st + hdisplay; 9242048e328SMark Yao u16 vdisplay = adjusted_mode->vdisplay; 9252048e328SMark Yao u16 vtotal = adjusted_mode->vtotal; 9262048e328SMark Yao u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start; 9272048e328SMark Yao u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start; 9282048e328SMark Yao u16 vact_end = vact_st + vdisplay; 9292048e328SMark Yao uint32_t val; 9302048e328SMark Yao 931893b6cadSDaniel Vetter WARN_ON(vop->event); 932893b6cadSDaniel Vetter 93363ebb9faSMark Yao vop_enable(crtc); 9342048e328SMark Yao /* 935ce3887edSMark Yao * If dclk rate is zero, mean that scanout is stop, 936ce3887edSMark Yao * we don't need wait any more. 9372048e328SMark Yao */ 938ce3887edSMark Yao if (clk_get_rate(vop->dclk)) { 939ce3887edSMark Yao /* 940ce3887edSMark Yao * Rk3288 vop timing register is immediately, when configure 941ce3887edSMark Yao * display timing on display time, may cause tearing. 942ce3887edSMark Yao * 943ce3887edSMark Yao * Vop standby will take effect at end of current frame, 944ce3887edSMark Yao * if dsp hold valid irq happen, it means standby complete. 945ce3887edSMark Yao * 946ce3887edSMark Yao * mode set: 947ce3887edSMark Yao * standby and wait complete --> |---- 948ce3887edSMark Yao * | display time 949ce3887edSMark Yao * |---- 950ce3887edSMark Yao * |---> dsp hold irq 951ce3887edSMark Yao * configure display timing --> | 952ce3887edSMark Yao * standby exit | 953ce3887edSMark Yao * | new frame start. 954ce3887edSMark Yao */ 955ce3887edSMark Yao 956ce3887edSMark Yao reinit_completion(&vop->dsp_hold_completion); 957ce3887edSMark Yao vop_dsp_hold_valid_irq_enable(vop); 958ce3887edSMark Yao 959ce3887edSMark Yao spin_lock(&vop->reg_lock); 960ce3887edSMark Yao 961ce3887edSMark Yao VOP_CTRL_SET(vop, standby, 1); 962ce3887edSMark Yao 963ce3887edSMark Yao spin_unlock(&vop->reg_lock); 964ce3887edSMark Yao 965ce3887edSMark Yao wait_for_completion(&vop->dsp_hold_completion); 966ce3887edSMark Yao 967ce3887edSMark Yao vop_dsp_hold_valid_irq_disable(vop); 968ce3887edSMark Yao } 9692048e328SMark Yao 9702048e328SMark Yao val = 0x8; 97144ddb7efSMark Yao val |= (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) ? 0 : 1; 97244ddb7efSMark Yao val |= (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC) ? 0 : (1 << 1); 9732048e328SMark Yao VOP_CTRL_SET(vop, pin_pol, val); 9744e257d9eSMark Yao switch (s->output_type) { 9754e257d9eSMark Yao case DRM_MODE_CONNECTOR_LVDS: 9764e257d9eSMark Yao VOP_CTRL_SET(vop, rgb_en, 1); 9774e257d9eSMark Yao break; 9784e257d9eSMark Yao case DRM_MODE_CONNECTOR_eDP: 9794e257d9eSMark Yao VOP_CTRL_SET(vop, edp_en, 1); 9804e257d9eSMark Yao break; 9814e257d9eSMark Yao case DRM_MODE_CONNECTOR_HDMIA: 9824e257d9eSMark Yao VOP_CTRL_SET(vop, hdmi_en, 1); 9834e257d9eSMark Yao break; 9844e257d9eSMark Yao case DRM_MODE_CONNECTOR_DSI: 9854e257d9eSMark Yao VOP_CTRL_SET(vop, mipi_en, 1); 9864e257d9eSMark Yao break; 9874e257d9eSMark Yao default: 9884e257d9eSMark Yao DRM_ERROR("unsupport connector_type[%d]\n", s->output_type); 9894e257d9eSMark Yao } 9904e257d9eSMark Yao VOP_CTRL_SET(vop, out_mode, s->output_mode); 9912048e328SMark Yao 9922048e328SMark Yao VOP_CTRL_SET(vop, htotal_pw, (htotal << 16) | hsync_len); 9932048e328SMark Yao val = hact_st << 16; 9942048e328SMark Yao val |= hact_end; 9952048e328SMark Yao VOP_CTRL_SET(vop, hact_st_end, val); 9962048e328SMark Yao VOP_CTRL_SET(vop, hpost_st_end, val); 9972048e328SMark Yao 9982048e328SMark Yao VOP_CTRL_SET(vop, vtotal_pw, (vtotal << 16) | vsync_len); 9992048e328SMark Yao val = vact_st << 16; 10002048e328SMark Yao val |= vact_end; 10012048e328SMark Yao VOP_CTRL_SET(vop, vact_st_end, val); 10022048e328SMark Yao VOP_CTRL_SET(vop, vpost_st_end, val); 10032048e328SMark Yao 10042048e328SMark Yao clk_set_rate(vop->dclk, adjusted_mode->clock * 1000); 1005ce3887edSMark Yao 1006ce3887edSMark Yao VOP_CTRL_SET(vop, standby, 0); 10072048e328SMark Yao } 10082048e328SMark Yao 100963ebb9faSMark Yao static void vop_crtc_atomic_flush(struct drm_crtc *crtc, 101063ebb9faSMark Yao struct drm_crtc_state *old_crtc_state) 101163ebb9faSMark Yao { 101263ebb9faSMark Yao struct vop *vop = to_vop(crtc); 101363ebb9faSMark Yao 101463ebb9faSMark Yao if (WARN_ON(!vop->is_enabled)) 101563ebb9faSMark Yao return; 101663ebb9faSMark Yao 101763ebb9faSMark Yao spin_lock(&vop->reg_lock); 101863ebb9faSMark Yao 101963ebb9faSMark Yao vop_cfg_done(vop); 102063ebb9faSMark Yao 102163ebb9faSMark Yao spin_unlock(&vop->reg_lock); 102263ebb9faSMark Yao } 102363ebb9faSMark Yao 102463ebb9faSMark Yao static void vop_crtc_atomic_begin(struct drm_crtc *crtc, 102563ebb9faSMark Yao struct drm_crtc_state *old_crtc_state) 102663ebb9faSMark Yao { 102763ebb9faSMark Yao struct vop *vop = to_vop(crtc); 102863ebb9faSMark Yao 1029893b6cadSDaniel Vetter spin_lock_irq(&crtc->dev->event_lock); 103063ebb9faSMark Yao if (crtc->state->event) { 103163ebb9faSMark Yao WARN_ON(drm_crtc_vblank_get(crtc) != 0); 1032893b6cadSDaniel Vetter WARN_ON(vop->event); 103363ebb9faSMark Yao 103463ebb9faSMark Yao vop->event = crtc->state->event; 103563ebb9faSMark Yao crtc->state->event = NULL; 103663ebb9faSMark Yao } 1037893b6cadSDaniel Vetter spin_unlock_irq(&crtc->dev->event_lock); 10382048e328SMark Yao } 10392048e328SMark Yao 10402048e328SMark Yao static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = { 10410ad3675dSMark Yao .enable = vop_crtc_enable, 10420ad3675dSMark Yao .disable = vop_crtc_disable, 10432048e328SMark Yao .mode_fixup = vop_crtc_mode_fixup, 104463ebb9faSMark Yao .atomic_flush = vop_crtc_atomic_flush, 104563ebb9faSMark Yao .atomic_begin = vop_crtc_atomic_begin, 10462048e328SMark Yao }; 10472048e328SMark Yao 10482048e328SMark Yao static void vop_crtc_destroy(struct drm_crtc *crtc) 10492048e328SMark Yao { 10502048e328SMark Yao drm_crtc_cleanup(crtc); 10512048e328SMark Yao } 10522048e328SMark Yao 1053dc0b408fSJohn Keeping static void vop_crtc_reset(struct drm_crtc *crtc) 1054dc0b408fSJohn Keeping { 1055dc0b408fSJohn Keeping if (crtc->state) 1056dc0b408fSJohn Keeping __drm_atomic_helper_crtc_destroy_state(crtc->state); 1057dc0b408fSJohn Keeping kfree(crtc->state); 1058dc0b408fSJohn Keeping 1059dc0b408fSJohn Keeping crtc->state = kzalloc(sizeof(struct rockchip_crtc_state), GFP_KERNEL); 1060dc0b408fSJohn Keeping if (crtc->state) 1061dc0b408fSJohn Keeping crtc->state->crtc = crtc; 1062dc0b408fSJohn Keeping } 1063dc0b408fSJohn Keeping 10644e257d9eSMark Yao static struct drm_crtc_state *vop_crtc_duplicate_state(struct drm_crtc *crtc) 10654e257d9eSMark Yao { 10664e257d9eSMark Yao struct rockchip_crtc_state *rockchip_state; 10674e257d9eSMark Yao 10684e257d9eSMark Yao rockchip_state = kzalloc(sizeof(*rockchip_state), GFP_KERNEL); 10694e257d9eSMark Yao if (!rockchip_state) 10704e257d9eSMark Yao return NULL; 10714e257d9eSMark Yao 10724e257d9eSMark Yao __drm_atomic_helper_crtc_duplicate_state(crtc, &rockchip_state->base); 10734e257d9eSMark Yao return &rockchip_state->base; 10744e257d9eSMark Yao } 10754e257d9eSMark Yao 10764e257d9eSMark Yao static void vop_crtc_destroy_state(struct drm_crtc *crtc, 10774e257d9eSMark Yao struct drm_crtc_state *state) 10784e257d9eSMark Yao { 10794e257d9eSMark Yao struct rockchip_crtc_state *s = to_rockchip_crtc_state(state); 10804e257d9eSMark Yao 1081ec2dc6a0SDaniel Vetter __drm_atomic_helper_crtc_destroy_state(&s->base); 10824e257d9eSMark Yao kfree(s); 10834e257d9eSMark Yao } 10844e257d9eSMark Yao 10852048e328SMark Yao static const struct drm_crtc_funcs vop_crtc_funcs = { 108663ebb9faSMark Yao .set_config = drm_atomic_helper_set_config, 108763ebb9faSMark Yao .page_flip = drm_atomic_helper_page_flip, 10882048e328SMark Yao .destroy = vop_crtc_destroy, 1089dc0b408fSJohn Keeping .reset = vop_crtc_reset, 10904e257d9eSMark Yao .atomic_duplicate_state = vop_crtc_duplicate_state, 10914e257d9eSMark Yao .atomic_destroy_state = vop_crtc_destroy_state, 10922048e328SMark Yao }; 10932048e328SMark Yao 109463ebb9faSMark Yao static bool vop_win_pending_is_complete(struct vop_win *vop_win) 10952048e328SMark Yao { 10962048e328SMark Yao dma_addr_t yrgb_mst; 10972048e328SMark Yao 10984f9d39a7SDaniel Vetter if (!vop_win->enable) 109963ebb9faSMark Yao return VOP_WIN_GET(vop_win->vop, vop_win->data, enable) == 0; 110063ebb9faSMark Yao 11012048e328SMark Yao yrgb_mst = VOP_WIN_GET_YRGBADDR(vop_win->vop, vop_win->data); 11022048e328SMark Yao 11034f9d39a7SDaniel Vetter return yrgb_mst == vop_win->yrgb_mst; 11042048e328SMark Yao } 11052048e328SMark Yao 110663ebb9faSMark Yao static void vop_handle_vblank(struct vop *vop) 11072048e328SMark Yao { 110863ebb9faSMark Yao struct drm_device *drm = vop->drm_dev; 110963ebb9faSMark Yao struct drm_crtc *crtc = &vop->crtc; 111063ebb9faSMark Yao unsigned long flags; 111163ebb9faSMark Yao int i; 11122048e328SMark Yao 111363ebb9faSMark Yao for (i = 0; i < vop->data->win_size; i++) { 111463ebb9faSMark Yao if (!vop_win_pending_is_complete(&vop->win[i])) 11152048e328SMark Yao return; 11162048e328SMark Yao } 11172048e328SMark Yao 111863ebb9faSMark Yao spin_lock_irqsave(&drm->event_lock, flags); 1119893b6cadSDaniel Vetter if (vop->event) { 11202048e328SMark Yao 112163ebb9faSMark Yao drm_crtc_send_vblank_event(crtc, vop->event); 112263ebb9faSMark Yao drm_crtc_vblank_put(crtc); 112363ebb9faSMark Yao vop->event = NULL; 112463ebb9faSMark Yao 11252048e328SMark Yao } 1126893b6cadSDaniel Vetter spin_unlock_irqrestore(&drm->event_lock, flags); 1127893b6cadSDaniel Vetter 112863ebb9faSMark Yao if (!completion_done(&vop->wait_update_complete)) 112963ebb9faSMark Yao complete(&vop->wait_update_complete); 11302048e328SMark Yao } 11312048e328SMark Yao 11322048e328SMark Yao static irqreturn_t vop_isr(int irq, void *data) 11332048e328SMark Yao { 11342048e328SMark Yao struct vop *vop = data; 1135b5f7b755SMark Yao struct drm_crtc *crtc = &vop->crtc; 1136dbb3d944SMark Yao uint32_t active_irqs; 11372048e328SMark Yao unsigned long flags; 11381067219bSMark Yao int ret = IRQ_NONE; 11392048e328SMark Yao 11402048e328SMark Yao /* 1141dbb3d944SMark Yao * interrupt register has interrupt status, enable and clear bits, we 11422048e328SMark Yao * must hold irq_lock to avoid a race with enable/disable_vblank(). 11432048e328SMark Yao */ 11442048e328SMark Yao spin_lock_irqsave(&vop->irq_lock, flags); 1145dbb3d944SMark Yao 1146dbb3d944SMark Yao active_irqs = VOP_INTR_GET_TYPE(vop, status, INTR_MASK); 11472048e328SMark Yao /* Clear all active interrupt sources */ 11482048e328SMark Yao if (active_irqs) 1149dbb3d944SMark Yao VOP_INTR_SET_TYPE(vop, clear, active_irqs, 1); 1150dbb3d944SMark Yao 11512048e328SMark Yao spin_unlock_irqrestore(&vop->irq_lock, flags); 11522048e328SMark Yao 11532048e328SMark Yao /* This is expected for vop iommu irqs, since the irq is shared */ 11542048e328SMark Yao if (!active_irqs) 11552048e328SMark Yao return IRQ_NONE; 11562048e328SMark Yao 11571067219bSMark Yao if (active_irqs & DSP_HOLD_VALID_INTR) { 11581067219bSMark Yao complete(&vop->dsp_hold_completion); 11591067219bSMark Yao active_irqs &= ~DSP_HOLD_VALID_INTR; 11601067219bSMark Yao ret = IRQ_HANDLED; 11612048e328SMark Yao } 11622048e328SMark Yao 11631067219bSMark Yao if (active_irqs & FS_INTR) { 1164b5f7b755SMark Yao drm_crtc_handle_vblank(crtc); 116563ebb9faSMark Yao vop_handle_vblank(vop); 11661067219bSMark Yao active_irqs &= ~FS_INTR; 116763ebb9faSMark Yao ret = IRQ_HANDLED; 11681067219bSMark Yao } 11692048e328SMark Yao 11701067219bSMark Yao /* Unhandled irqs are spurious. */ 11711067219bSMark Yao if (active_irqs) 11721067219bSMark Yao DRM_ERROR("Unknown VOP IRQs: %#02x\n", active_irqs); 11731067219bSMark Yao 11741067219bSMark Yao return ret; 11752048e328SMark Yao } 11762048e328SMark Yao 11772048e328SMark Yao static int vop_create_crtc(struct vop *vop) 11782048e328SMark Yao { 11792048e328SMark Yao const struct vop_data *vop_data = vop->data; 11802048e328SMark Yao struct device *dev = vop->dev; 11812048e328SMark Yao struct drm_device *drm_dev = vop->drm_dev; 1182328b51c0SDouglas Anderson struct drm_plane *primary = NULL, *cursor = NULL, *plane, *tmp; 11832048e328SMark Yao struct drm_crtc *crtc = &vop->crtc; 11842048e328SMark Yao struct device_node *port; 11852048e328SMark Yao int ret; 11862048e328SMark Yao int i; 11872048e328SMark Yao 11882048e328SMark Yao /* 11892048e328SMark Yao * Create drm_plane for primary and cursor planes first, since we need 11902048e328SMark Yao * to pass them to drm_crtc_init_with_planes, which sets the 11912048e328SMark Yao * "possible_crtcs" to the newly initialized crtc. 11922048e328SMark Yao */ 11932048e328SMark Yao for (i = 0; i < vop_data->win_size; i++) { 11942048e328SMark Yao struct vop_win *vop_win = &vop->win[i]; 11952048e328SMark Yao const struct vop_win_data *win_data = vop_win->data; 11962048e328SMark Yao 11972048e328SMark Yao if (win_data->type != DRM_PLANE_TYPE_PRIMARY && 11982048e328SMark Yao win_data->type != DRM_PLANE_TYPE_CURSOR) 11992048e328SMark Yao continue; 12002048e328SMark Yao 12012048e328SMark Yao ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base, 12022048e328SMark Yao 0, &vop_plane_funcs, 12032048e328SMark Yao win_data->phy->data_formats, 12042048e328SMark Yao win_data->phy->nformats, 1205b0b3b795SVille Syrjälä win_data->type, NULL); 12062048e328SMark Yao if (ret) { 12072048e328SMark Yao DRM_ERROR("failed to initialize plane\n"); 12082048e328SMark Yao goto err_cleanup_planes; 12092048e328SMark Yao } 12102048e328SMark Yao 12112048e328SMark Yao plane = &vop_win->base; 121263ebb9faSMark Yao drm_plane_helper_add(plane, &plane_helper_funcs); 12132048e328SMark Yao if (plane->type == DRM_PLANE_TYPE_PRIMARY) 12142048e328SMark Yao primary = plane; 12152048e328SMark Yao else if (plane->type == DRM_PLANE_TYPE_CURSOR) 12162048e328SMark Yao cursor = plane; 12172048e328SMark Yao } 12182048e328SMark Yao 12192048e328SMark Yao ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor, 1220f9882876SVille Syrjälä &vop_crtc_funcs, NULL); 12212048e328SMark Yao if (ret) 1222328b51c0SDouglas Anderson goto err_cleanup_planes; 12232048e328SMark Yao 12242048e328SMark Yao drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs); 12252048e328SMark Yao 12262048e328SMark Yao /* 12272048e328SMark Yao * Create drm_planes for overlay windows with possible_crtcs restricted 12282048e328SMark Yao * to the newly created crtc. 12292048e328SMark Yao */ 12302048e328SMark Yao for (i = 0; i < vop_data->win_size; i++) { 12312048e328SMark Yao struct vop_win *vop_win = &vop->win[i]; 12322048e328SMark Yao const struct vop_win_data *win_data = vop_win->data; 12332048e328SMark Yao unsigned long possible_crtcs = 1 << drm_crtc_index(crtc); 12342048e328SMark Yao 12352048e328SMark Yao if (win_data->type != DRM_PLANE_TYPE_OVERLAY) 12362048e328SMark Yao continue; 12372048e328SMark Yao 12382048e328SMark Yao ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base, 12392048e328SMark Yao possible_crtcs, 12402048e328SMark Yao &vop_plane_funcs, 12412048e328SMark Yao win_data->phy->data_formats, 12422048e328SMark Yao win_data->phy->nformats, 1243b0b3b795SVille Syrjälä win_data->type, NULL); 12442048e328SMark Yao if (ret) { 12452048e328SMark Yao DRM_ERROR("failed to initialize overlay plane\n"); 12462048e328SMark Yao goto err_cleanup_crtc; 12472048e328SMark Yao } 124863ebb9faSMark Yao drm_plane_helper_add(&vop_win->base, &plane_helper_funcs); 12492048e328SMark Yao } 12502048e328SMark Yao 12512048e328SMark Yao port = of_get_child_by_name(dev->of_node, "port"); 12522048e328SMark Yao if (!port) { 12532048e328SMark Yao DRM_ERROR("no port node found in %s\n", 12542048e328SMark Yao dev->of_node->full_name); 1255328b51c0SDouglas Anderson ret = -ENOENT; 12562048e328SMark Yao goto err_cleanup_crtc; 12572048e328SMark Yao } 12582048e328SMark Yao 12591067219bSMark Yao init_completion(&vop->dsp_hold_completion); 126063ebb9faSMark Yao init_completion(&vop->wait_update_complete); 12612048e328SMark Yao crtc->port = port; 1262b5f7b755SMark Yao rockchip_register_crtc_funcs(crtc, &private_crtc_funcs); 12632048e328SMark Yao 12642048e328SMark Yao return 0; 12652048e328SMark Yao 12662048e328SMark Yao err_cleanup_crtc: 12672048e328SMark Yao drm_crtc_cleanup(crtc); 12682048e328SMark Yao err_cleanup_planes: 1269328b51c0SDouglas Anderson list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list, 1270328b51c0SDouglas Anderson head) 12712048e328SMark Yao drm_plane_cleanup(plane); 12722048e328SMark Yao return ret; 12732048e328SMark Yao } 12742048e328SMark Yao 12752048e328SMark Yao static void vop_destroy_crtc(struct vop *vop) 12762048e328SMark Yao { 12772048e328SMark Yao struct drm_crtc *crtc = &vop->crtc; 1278328b51c0SDouglas Anderson struct drm_device *drm_dev = vop->drm_dev; 1279328b51c0SDouglas Anderson struct drm_plane *plane, *tmp; 12802048e328SMark Yao 1281b5f7b755SMark Yao rockchip_unregister_crtc_funcs(crtc); 12822048e328SMark Yao of_node_put(crtc->port); 1283328b51c0SDouglas Anderson 1284328b51c0SDouglas Anderson /* 1285328b51c0SDouglas Anderson * We need to cleanup the planes now. Why? 1286328b51c0SDouglas Anderson * 1287328b51c0SDouglas Anderson * The planes are "&vop->win[i].base". That means the memory is 1288328b51c0SDouglas Anderson * all part of the big "struct vop" chunk of memory. That memory 1289328b51c0SDouglas Anderson * was devm allocated and associated with this component. We need to 1290328b51c0SDouglas Anderson * free it ourselves before vop_unbind() finishes. 1291328b51c0SDouglas Anderson */ 1292328b51c0SDouglas Anderson list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list, 1293328b51c0SDouglas Anderson head) 1294328b51c0SDouglas Anderson vop_plane_destroy(plane); 1295328b51c0SDouglas Anderson 1296328b51c0SDouglas Anderson /* 1297328b51c0SDouglas Anderson * Destroy CRTC after vop_plane_destroy() since vop_disable_plane() 1298328b51c0SDouglas Anderson * references the CRTC. 1299328b51c0SDouglas Anderson */ 13002048e328SMark Yao drm_crtc_cleanup(crtc); 13012048e328SMark Yao } 13022048e328SMark Yao 13032048e328SMark Yao static int vop_initial(struct vop *vop) 13042048e328SMark Yao { 13052048e328SMark Yao const struct vop_data *vop_data = vop->data; 13062048e328SMark Yao const struct vop_reg_data *init_table = vop_data->init_table; 13072048e328SMark Yao struct reset_control *ahb_rst; 13082048e328SMark Yao int i, ret; 13092048e328SMark Yao 13102048e328SMark Yao vop->hclk = devm_clk_get(vop->dev, "hclk_vop"); 13112048e328SMark Yao if (IS_ERR(vop->hclk)) { 13122048e328SMark Yao dev_err(vop->dev, "failed to get hclk source\n"); 13132048e328SMark Yao return PTR_ERR(vop->hclk); 13142048e328SMark Yao } 13152048e328SMark Yao vop->aclk = devm_clk_get(vop->dev, "aclk_vop"); 13162048e328SMark Yao if (IS_ERR(vop->aclk)) { 13172048e328SMark Yao dev_err(vop->dev, "failed to get aclk source\n"); 13182048e328SMark Yao return PTR_ERR(vop->aclk); 13192048e328SMark Yao } 13202048e328SMark Yao vop->dclk = devm_clk_get(vop->dev, "dclk_vop"); 13212048e328SMark Yao if (IS_ERR(vop->dclk)) { 13222048e328SMark Yao dev_err(vop->dev, "failed to get dclk source\n"); 13232048e328SMark Yao return PTR_ERR(vop->dclk); 13242048e328SMark Yao } 13252048e328SMark Yao 13262048e328SMark Yao ret = clk_prepare(vop->dclk); 13272048e328SMark Yao if (ret < 0) { 13282048e328SMark Yao dev_err(vop->dev, "failed to prepare dclk\n"); 1329d7b53fd9SSjoerd Simons return ret; 13302048e328SMark Yao } 13312048e328SMark Yao 1332d7b53fd9SSjoerd Simons /* Enable both the hclk and aclk to setup the vop */ 1333d7b53fd9SSjoerd Simons ret = clk_prepare_enable(vop->hclk); 13342048e328SMark Yao if (ret < 0) { 1335d7b53fd9SSjoerd Simons dev_err(vop->dev, "failed to prepare/enable hclk\n"); 13362048e328SMark Yao goto err_unprepare_dclk; 13372048e328SMark Yao } 13382048e328SMark Yao 1339d7b53fd9SSjoerd Simons ret = clk_prepare_enable(vop->aclk); 13402048e328SMark Yao if (ret < 0) { 1341d7b53fd9SSjoerd Simons dev_err(vop->dev, "failed to prepare/enable aclk\n"); 1342d7b53fd9SSjoerd Simons goto err_disable_hclk; 13432048e328SMark Yao } 1344d7b53fd9SSjoerd Simons 13452048e328SMark Yao /* 13462048e328SMark Yao * do hclk_reset, reset all vop registers. 13472048e328SMark Yao */ 13482048e328SMark Yao ahb_rst = devm_reset_control_get(vop->dev, "ahb"); 13492048e328SMark Yao if (IS_ERR(ahb_rst)) { 13502048e328SMark Yao dev_err(vop->dev, "failed to get ahb reset\n"); 13512048e328SMark Yao ret = PTR_ERR(ahb_rst); 1352d7b53fd9SSjoerd Simons goto err_disable_aclk; 13532048e328SMark Yao } 13542048e328SMark Yao reset_control_assert(ahb_rst); 13552048e328SMark Yao usleep_range(10, 20); 13562048e328SMark Yao reset_control_deassert(ahb_rst); 13572048e328SMark Yao 13582048e328SMark Yao memcpy(vop->regsbak, vop->regs, vop->len); 13592048e328SMark Yao 13602048e328SMark Yao for (i = 0; i < vop_data->table_size; i++) 13612048e328SMark Yao vop_writel(vop, init_table[i].offset, init_table[i].value); 13622048e328SMark Yao 13632048e328SMark Yao for (i = 0; i < vop_data->win_size; i++) { 13642048e328SMark Yao const struct vop_win_data *win = &vop_data->win[i]; 13652048e328SMark Yao 13662048e328SMark Yao VOP_WIN_SET(vop, win, enable, 0); 13672048e328SMark Yao } 13682048e328SMark Yao 13692048e328SMark Yao vop_cfg_done(vop); 13702048e328SMark Yao 13712048e328SMark Yao /* 13722048e328SMark Yao * do dclk_reset, let all config take affect. 13732048e328SMark Yao */ 13742048e328SMark Yao vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk"); 13752048e328SMark Yao if (IS_ERR(vop->dclk_rst)) { 13762048e328SMark Yao dev_err(vop->dev, "failed to get dclk reset\n"); 13772048e328SMark Yao ret = PTR_ERR(vop->dclk_rst); 1378d7b53fd9SSjoerd Simons goto err_disable_aclk; 13792048e328SMark Yao } 13802048e328SMark Yao reset_control_assert(vop->dclk_rst); 13812048e328SMark Yao usleep_range(10, 20); 13822048e328SMark Yao reset_control_deassert(vop->dclk_rst); 13832048e328SMark Yao 13842048e328SMark Yao clk_disable(vop->hclk); 1385d7b53fd9SSjoerd Simons clk_disable(vop->aclk); 13862048e328SMark Yao 138731e980c5SMark Yao vop->is_enabled = false; 13882048e328SMark Yao 13892048e328SMark Yao return 0; 13902048e328SMark Yao 1391d7b53fd9SSjoerd Simons err_disable_aclk: 1392d7b53fd9SSjoerd Simons clk_disable_unprepare(vop->aclk); 13932048e328SMark Yao err_disable_hclk: 1394d7b53fd9SSjoerd Simons clk_disable_unprepare(vop->hclk); 13952048e328SMark Yao err_unprepare_dclk: 13962048e328SMark Yao clk_unprepare(vop->dclk); 13972048e328SMark Yao return ret; 13982048e328SMark Yao } 13992048e328SMark Yao 14002048e328SMark Yao /* 14012048e328SMark Yao * Initialize the vop->win array elements. 14022048e328SMark Yao */ 14032048e328SMark Yao static void vop_win_init(struct vop *vop) 14042048e328SMark Yao { 14052048e328SMark Yao const struct vop_data *vop_data = vop->data; 14062048e328SMark Yao unsigned int i; 14072048e328SMark Yao 14082048e328SMark Yao for (i = 0; i < vop_data->win_size; i++) { 14092048e328SMark Yao struct vop_win *vop_win = &vop->win[i]; 14102048e328SMark Yao const struct vop_win_data *win_data = &vop_data->win[i]; 14112048e328SMark Yao 14122048e328SMark Yao vop_win->data = win_data; 14132048e328SMark Yao vop_win->vop = vop; 14142048e328SMark Yao } 14152048e328SMark Yao } 14162048e328SMark Yao 14172048e328SMark Yao static int vop_bind(struct device *dev, struct device *master, void *data) 14182048e328SMark Yao { 14192048e328SMark Yao struct platform_device *pdev = to_platform_device(dev); 14202048e328SMark Yao const struct vop_data *vop_data; 14212048e328SMark Yao struct drm_device *drm_dev = data; 14222048e328SMark Yao struct vop *vop; 14232048e328SMark Yao struct resource *res; 14242048e328SMark Yao size_t alloc_size; 14253ea68922SHeiko Stuebner int ret, irq; 14262048e328SMark Yao 1427a67719d1SMark Yao vop_data = of_device_get_match_data(dev); 14282048e328SMark Yao if (!vop_data) 14292048e328SMark Yao return -ENODEV; 14302048e328SMark Yao 14312048e328SMark Yao /* Allocate vop struct and its vop_win array */ 14322048e328SMark Yao alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size; 14332048e328SMark Yao vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL); 14342048e328SMark Yao if (!vop) 14352048e328SMark Yao return -ENOMEM; 14362048e328SMark Yao 14372048e328SMark Yao vop->dev = dev; 14382048e328SMark Yao vop->data = vop_data; 14392048e328SMark Yao vop->drm_dev = drm_dev; 14402048e328SMark Yao dev_set_drvdata(dev, vop); 14412048e328SMark Yao 14422048e328SMark Yao vop_win_init(vop); 14432048e328SMark Yao 14442048e328SMark Yao res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 14452048e328SMark Yao vop->len = resource_size(res); 14462048e328SMark Yao vop->regs = devm_ioremap_resource(dev, res); 14472048e328SMark Yao if (IS_ERR(vop->regs)) 14482048e328SMark Yao return PTR_ERR(vop->regs); 14492048e328SMark Yao 14502048e328SMark Yao vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL); 14512048e328SMark Yao if (!vop->regsbak) 14522048e328SMark Yao return -ENOMEM; 14532048e328SMark Yao 14542048e328SMark Yao ret = vop_initial(vop); 14552048e328SMark Yao if (ret < 0) { 14562048e328SMark Yao dev_err(&pdev->dev, "cannot initial vop dev - err %d\n", ret); 14572048e328SMark Yao return ret; 14582048e328SMark Yao } 14592048e328SMark Yao 14603ea68922SHeiko Stuebner irq = platform_get_irq(pdev, 0); 14613ea68922SHeiko Stuebner if (irq < 0) { 14622048e328SMark Yao dev_err(dev, "cannot find irq for vop\n"); 14633ea68922SHeiko Stuebner return irq; 14642048e328SMark Yao } 14653ea68922SHeiko Stuebner vop->irq = (unsigned int)irq; 14662048e328SMark Yao 14672048e328SMark Yao spin_lock_init(&vop->reg_lock); 14682048e328SMark Yao spin_lock_init(&vop->irq_lock); 14692048e328SMark Yao 14702048e328SMark Yao mutex_init(&vop->vsync_mutex); 14712048e328SMark Yao 147263ebb9faSMark Yao ret = devm_request_irq(dev, vop->irq, vop_isr, 14732048e328SMark Yao IRQF_SHARED, dev_name(dev), vop); 14742048e328SMark Yao if (ret) 14752048e328SMark Yao return ret; 14762048e328SMark Yao 14772048e328SMark Yao /* IRQ is initially disabled; it gets enabled in power_on */ 14782048e328SMark Yao disable_irq(vop->irq); 14792048e328SMark Yao 14802048e328SMark Yao ret = vop_create_crtc(vop); 14812048e328SMark Yao if (ret) 14822048e328SMark Yao return ret; 14832048e328SMark Yao 14842048e328SMark Yao pm_runtime_enable(&pdev->dev); 14852048e328SMark Yao return 0; 14862048e328SMark Yao } 14872048e328SMark Yao 14882048e328SMark Yao static void vop_unbind(struct device *dev, struct device *master, void *data) 14892048e328SMark Yao { 14902048e328SMark Yao struct vop *vop = dev_get_drvdata(dev); 14912048e328SMark Yao 14922048e328SMark Yao pm_runtime_disable(dev); 14932048e328SMark Yao vop_destroy_crtc(vop); 14942048e328SMark Yao } 14952048e328SMark Yao 1496a67719d1SMark Yao const struct component_ops vop_component_ops = { 14972048e328SMark Yao .bind = vop_bind, 14982048e328SMark Yao .unbind = vop_unbind, 14992048e328SMark Yao }; 150054255e81SStephen Rothwell EXPORT_SYMBOL_GPL(vop_component_ops); 1501