2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_plane_helper.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
27 #include <linux/of_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/component.h>
31 #include <linux/reset.h>
32 #include <linux/delay.h>
34 #include "rockchip_drm_drv.h"
35 #include "rockchip_drm_gem.h"
36 #include "rockchip_drm_fb.h"
37 #include "rockchip_drm_vop.h"
39 #define VOP_REG(off, _mask, s) \
44 #define __REG_SET_RELAXED(x, off, mask, shift, v) \
45 vop_mask_write_relaxed(x, off, (mask) << shift, (v) << shift)
46 #define __REG_SET_NORMAL(x, off, mask, shift, v) \
47 vop_mask_write(x, off, (mask) << shift, (v) << shift)
49 #define REG_SET(x, base, reg, v, mode) \
50 __REG_SET_##mode(x, base + reg.offset, reg.mask, reg.shift, v)
52 #define VOP_WIN_SET(x, win, name, v) \
53 REG_SET(x, win->base, win->phy->name, v, RELAXED)
54 #define VOP_SCL_SET(x, win, name, v) \
55 REG_SET(x, win->base, win->phy->scl->name, v, RELAXED)
56 #define VOP_CTRL_SET(x, name, v) \
57 REG_SET(x, 0, (x)->data->ctrl->name, v, NORMAL)
59 #define VOP_WIN_GET(x, win, name) \
60 vop_read_reg(x, win->base, &win->phy->name)
62 #define VOP_WIN_GET_YRGBADDR(vop, win) \
63 vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
65 #define to_vop(x) container_of(x, struct vop, crtc)
66 #define to_vop_win(x) container_of(x, struct vop_win, base)
67 #define to_vop_plane_state(x) container_of(x, struct vop_plane_state, base)
69 struct vop_plane_state {
70 struct drm_plane_state base;
79 struct drm_plane base;
80 const struct vop_win_data *data;
83 struct vop_plane_state state;
89 struct drm_device *drm_dev;
92 /* mutex vsync_ work */
93 struct mutex vsync_mutex;
94 bool vsync_work_pending;
95 struct completion dsp_hold_completion;
96 struct completion wait_update_complete;
97 struct drm_pending_vblank_event *event;
99 const struct vop_data *data;
104 /* physical map length of vop register */
107 /* one time only one process allowed to config the register */
109 /* lock vop irq reg */
118 /* vop share memory frequency */
122 struct reset_control *dclk_rst;
124 struct vop_win win[];
127 enum vop_data_format {
128 VOP_FMT_ARGB8888 = 0,
131 VOP_FMT_YUV420SP = 4,
136 struct vop_reg_data {
148 struct vop_reg standby;
149 struct vop_reg data_blank;
150 struct vop_reg gate_en;
151 struct vop_reg mmu_en;
152 struct vop_reg rgb_en;
153 struct vop_reg edp_en;
154 struct vop_reg hdmi_en;
155 struct vop_reg mipi_en;
156 struct vop_reg out_mode;
157 struct vop_reg dither_down;
158 struct vop_reg dither_up;
159 struct vop_reg pin_pol;
161 struct vop_reg htotal_pw;
162 struct vop_reg hact_st_end;
163 struct vop_reg vtotal_pw;
164 struct vop_reg vact_st_end;
165 struct vop_reg hpost_st_end;
166 struct vop_reg vpost_st_end;
169 struct vop_scl_regs {
170 struct vop_reg cbcr_vsd_mode;
171 struct vop_reg cbcr_vsu_mode;
172 struct vop_reg cbcr_hsd_mode;
173 struct vop_reg cbcr_ver_scl_mode;
174 struct vop_reg cbcr_hor_scl_mode;
175 struct vop_reg yrgb_vsd_mode;
176 struct vop_reg yrgb_vsu_mode;
177 struct vop_reg yrgb_hsd_mode;
178 struct vop_reg yrgb_ver_scl_mode;
179 struct vop_reg yrgb_hor_scl_mode;
180 struct vop_reg line_load_mode;
181 struct vop_reg cbcr_axi_gather_num;
182 struct vop_reg yrgb_axi_gather_num;
183 struct vop_reg vsd_cbcr_gt2;
184 struct vop_reg vsd_cbcr_gt4;
185 struct vop_reg vsd_yrgb_gt2;
186 struct vop_reg vsd_yrgb_gt4;
187 struct vop_reg bic_coe_sel;
188 struct vop_reg cbcr_axi_gather_en;
189 struct vop_reg yrgb_axi_gather_en;
191 struct vop_reg lb_mode;
192 struct vop_reg scale_yrgb_x;
193 struct vop_reg scale_yrgb_y;
194 struct vop_reg scale_cbcr_x;
195 struct vop_reg scale_cbcr_y;
199 const struct vop_scl_regs *scl;
200 const uint32_t *data_formats;
203 struct vop_reg enable;
204 struct vop_reg format;
205 struct vop_reg rb_swap;
206 struct vop_reg act_info;
207 struct vop_reg dsp_info;
208 struct vop_reg dsp_st;
209 struct vop_reg yrgb_mst;
210 struct vop_reg uv_mst;
211 struct vop_reg yrgb_vir;
212 struct vop_reg uv_vir;
214 struct vop_reg dst_alpha_ctl;
215 struct vop_reg src_alpha_ctl;
218 struct vop_win_data {
220 const struct vop_win_phy *phy;
221 enum drm_plane_type type;
225 const struct vop_reg_data *init_table;
226 unsigned int table_size;
227 const struct vop_ctrl *ctrl;
228 const struct vop_win_data *win;
229 unsigned int win_size;
232 static const uint32_t formats_01[] = {
246 static const uint32_t formats_234[] = {
257 static const struct vop_scl_regs win_full_scl = {
258 .cbcr_vsd_mode = VOP_REG(WIN0_CTRL1, 0x1, 31),
259 .cbcr_vsu_mode = VOP_REG(WIN0_CTRL1, 0x1, 30),
260 .cbcr_hsd_mode = VOP_REG(WIN0_CTRL1, 0x3, 28),
261 .cbcr_ver_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 26),
262 .cbcr_hor_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 24),
263 .yrgb_vsd_mode = VOP_REG(WIN0_CTRL1, 0x1, 23),
264 .yrgb_vsu_mode = VOP_REG(WIN0_CTRL1, 0x1, 22),
265 .yrgb_hsd_mode = VOP_REG(WIN0_CTRL1, 0x3, 20),
266 .yrgb_ver_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 18),
267 .yrgb_hor_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 16),
268 .line_load_mode = VOP_REG(WIN0_CTRL1, 0x1, 15),
269 .cbcr_axi_gather_num = VOP_REG(WIN0_CTRL1, 0x7, 12),
270 .yrgb_axi_gather_num = VOP_REG(WIN0_CTRL1, 0xf, 8),
271 .vsd_cbcr_gt2 = VOP_REG(WIN0_CTRL1, 0x1, 7),
272 .vsd_cbcr_gt4 = VOP_REG(WIN0_CTRL1, 0x1, 6),
273 .vsd_yrgb_gt2 = VOP_REG(WIN0_CTRL1, 0x1, 5),
274 .vsd_yrgb_gt4 = VOP_REG(WIN0_CTRL1, 0x1, 4),
275 .bic_coe_sel = VOP_REG(WIN0_CTRL1, 0x3, 2),
276 .cbcr_axi_gather_en = VOP_REG(WIN0_CTRL1, 0x1, 1),
277 .yrgb_axi_gather_en = VOP_REG(WIN0_CTRL1, 0x1, 0),
278 .lb_mode = VOP_REG(WIN0_CTRL0, 0x7, 5),
279 .scale_yrgb_x = VOP_REG(WIN0_SCL_FACTOR_YRGB, 0xffff, 0x0),
280 .scale_yrgb_y = VOP_REG(WIN0_SCL_FACTOR_YRGB, 0xffff, 16),
281 .scale_cbcr_x = VOP_REG(WIN0_SCL_FACTOR_CBR, 0xffff, 0x0),
282 .scale_cbcr_y = VOP_REG(WIN0_SCL_FACTOR_CBR, 0xffff, 16),
285 static const struct vop_win_phy win01_data = {
286 .scl = &win_full_scl,
287 .data_formats = formats_01,
288 .nformats = ARRAY_SIZE(formats_01),
289 .enable = VOP_REG(WIN0_CTRL0, 0x1, 0),
290 .format = VOP_REG(WIN0_CTRL0, 0x7, 1),
291 .rb_swap = VOP_REG(WIN0_CTRL0, 0x1, 12),
292 .act_info = VOP_REG(WIN0_ACT_INFO, 0x1fff1fff, 0),
293 .dsp_info = VOP_REG(WIN0_DSP_INFO, 0x0fff0fff, 0),
294 .dsp_st = VOP_REG(WIN0_DSP_ST, 0x1fff1fff, 0),
295 .yrgb_mst = VOP_REG(WIN0_YRGB_MST, 0xffffffff, 0),
296 .uv_mst = VOP_REG(WIN0_CBR_MST, 0xffffffff, 0),
297 .yrgb_vir = VOP_REG(WIN0_VIR, 0x3fff, 0),
298 .uv_vir = VOP_REG(WIN0_VIR, 0x3fff, 16),
299 .src_alpha_ctl = VOP_REG(WIN0_SRC_ALPHA_CTRL, 0xff, 0),
300 .dst_alpha_ctl = VOP_REG(WIN0_DST_ALPHA_CTRL, 0xff, 0),
303 static const struct vop_win_phy win23_data = {
304 .data_formats = formats_234,
305 .nformats = ARRAY_SIZE(formats_234),
306 .enable = VOP_REG(WIN2_CTRL0, 0x1, 0),
307 .format = VOP_REG(WIN2_CTRL0, 0x7, 1),
308 .rb_swap = VOP_REG(WIN2_CTRL0, 0x1, 12),
309 .dsp_info = VOP_REG(WIN2_DSP_INFO0, 0x0fff0fff, 0),
310 .dsp_st = VOP_REG(WIN2_DSP_ST0, 0x1fff1fff, 0),
311 .yrgb_mst = VOP_REG(WIN2_MST0, 0xffffffff, 0),
312 .yrgb_vir = VOP_REG(WIN2_VIR0_1, 0x1fff, 0),
313 .src_alpha_ctl = VOP_REG(WIN2_SRC_ALPHA_CTRL, 0xff, 0),
314 .dst_alpha_ctl = VOP_REG(WIN2_DST_ALPHA_CTRL, 0xff, 0),
317 static const struct vop_ctrl ctrl_data = {
318 .standby = VOP_REG(SYS_CTRL, 0x1, 22),
319 .gate_en = VOP_REG(SYS_CTRL, 0x1, 23),
320 .mmu_en = VOP_REG(SYS_CTRL, 0x1, 20),
321 .rgb_en = VOP_REG(SYS_CTRL, 0x1, 12),
322 .hdmi_en = VOP_REG(SYS_CTRL, 0x1, 13),
323 .edp_en = VOP_REG(SYS_CTRL, 0x1, 14),
324 .mipi_en = VOP_REG(SYS_CTRL, 0x1, 15),
325 .dither_down = VOP_REG(DSP_CTRL1, 0xf, 1),
326 .dither_up = VOP_REG(DSP_CTRL1, 0x1, 6),
327 .data_blank = VOP_REG(DSP_CTRL0, 0x1, 19),
328 .out_mode = VOP_REG(DSP_CTRL0, 0xf, 0),
329 .pin_pol = VOP_REG(DSP_CTRL0, 0xf, 4),
330 .htotal_pw = VOP_REG(DSP_HTOTAL_HS_END, 0x1fff1fff, 0),
331 .hact_st_end = VOP_REG(DSP_HACT_ST_END, 0x1fff1fff, 0),
332 .vtotal_pw = VOP_REG(DSP_VTOTAL_VS_END, 0x1fff1fff, 0),
333 .vact_st_end = VOP_REG(DSP_VACT_ST_END, 0x1fff1fff, 0),
334 .hpost_st_end = VOP_REG(POST_DSP_HACT_INFO, 0x1fff1fff, 0),
335 .vpost_st_end = VOP_REG(POST_DSP_VACT_INFO, 0x1fff1fff, 0),
338 static const struct vop_reg_data vop_init_reg_table[] = {
339 {SYS_CTRL, 0x00c00000},
340 {DSP_CTRL0, 0x00000000},
341 {WIN0_CTRL0, 0x00000080},
342 {WIN1_CTRL0, 0x00000080},
343 /* TODO: Win2/3 support multiple area function, but we haven't found
344 * a suitable way to use it yet, so let's just use them as other windows
345 * with only area 0 enabled.
347 {WIN2_CTRL0, 0x00000010},
348 {WIN3_CTRL0, 0x00000010},
352 * Note: rk3288 has a dedicated 'cursor' window, however, that window requires
353 * special support to get alpha blending working. For now, just use overlay
354 * window 3 for the drm cursor.
357 static const struct vop_win_data rk3288_vop_win_data[] = {
358 { .base = 0x00, .phy = &win01_data, .type = DRM_PLANE_TYPE_PRIMARY },
359 { .base = 0x40, .phy = &win01_data, .type = DRM_PLANE_TYPE_OVERLAY },
360 { .base = 0x00, .phy = &win23_data, .type = DRM_PLANE_TYPE_OVERLAY },
361 { .base = 0x50, .phy = &win23_data, .type = DRM_PLANE_TYPE_CURSOR },
364 static const struct vop_data rk3288_vop = {
365 .init_table = vop_init_reg_table,
366 .table_size = ARRAY_SIZE(vop_init_reg_table),
368 .win = rk3288_vop_win_data,
369 .win_size = ARRAY_SIZE(rk3288_vop_win_data),
372 static const struct of_device_id vop_driver_dt_match[] = {
373 { .compatible = "rockchip,rk3288-vop",
374 .data = &rk3288_vop },
377 MODULE_DEVICE_TABLE(of, vop_driver_dt_match);
379 static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
381 writel(v, vop->regs + offset);
382 vop->regsbak[offset >> 2] = v;
385 static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
387 return readl(vop->regs + offset);
390 static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
391 const struct vop_reg *reg)
393 return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
396 static inline void vop_cfg_done(struct vop *vop)
398 writel(0x01, vop->regs + REG_CFG_DONE);
401 static inline void vop_mask_write(struct vop *vop, uint32_t offset,
402 uint32_t mask, uint32_t v)
405 uint32_t cached_val = vop->regsbak[offset >> 2];
407 cached_val = (cached_val & ~mask) | v;
408 writel(cached_val, vop->regs + offset);
409 vop->regsbak[offset >> 2] = cached_val;
413 static inline void vop_mask_write_relaxed(struct vop *vop, uint32_t offset,
414 uint32_t mask, uint32_t v)
417 uint32_t cached_val = vop->regsbak[offset >> 2];
419 cached_val = (cached_val & ~mask) | v;
420 writel_relaxed(cached_val, vop->regs + offset);
421 vop->regsbak[offset >> 2] = cached_val;
425 static bool has_rb_swapped(uint32_t format)
428 case DRM_FORMAT_XBGR8888:
429 case DRM_FORMAT_ABGR8888:
430 case DRM_FORMAT_BGR888:
431 case DRM_FORMAT_BGR565:
438 static enum vop_data_format vop_convert_format(uint32_t format)
441 case DRM_FORMAT_XRGB8888:
442 case DRM_FORMAT_ARGB8888:
443 case DRM_FORMAT_XBGR8888:
444 case DRM_FORMAT_ABGR8888:
445 return VOP_FMT_ARGB8888;
446 case DRM_FORMAT_RGB888:
447 case DRM_FORMAT_BGR888:
448 return VOP_FMT_RGB888;
449 case DRM_FORMAT_RGB565:
450 case DRM_FORMAT_BGR565:
451 return VOP_FMT_RGB565;
452 case DRM_FORMAT_NV12:
453 return VOP_FMT_YUV420SP;
454 case DRM_FORMAT_NV16:
455 return VOP_FMT_YUV422SP;
456 case DRM_FORMAT_NV24:
457 return VOP_FMT_YUV444SP;
459 DRM_ERROR("unsupport format[%08x]\n", format);
464 static bool is_yuv_support(uint32_t format)
467 case DRM_FORMAT_NV12:
468 case DRM_FORMAT_NV16:
469 case DRM_FORMAT_NV24:
476 static bool is_alpha_support(uint32_t format)
479 case DRM_FORMAT_ARGB8888:
480 case DRM_FORMAT_ABGR8888:
487 static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src,
488 uint32_t dst, bool is_horizontal,
489 int vsu_mode, int *vskiplines)
491 uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT;
494 if (mode == SCALE_UP)
495 val = GET_SCL_FT_BIC(src, dst);
496 else if (mode == SCALE_DOWN)
497 val = GET_SCL_FT_BILI_DN(src, dst);
499 if (mode == SCALE_UP) {
500 if (vsu_mode == SCALE_UP_BIL)
501 val = GET_SCL_FT_BILI_UP(src, dst);
503 val = GET_SCL_FT_BIC(src, dst);
504 } else if (mode == SCALE_DOWN) {
506 *vskiplines = scl_get_vskiplines(src, dst);
507 val = scl_get_bili_dn_vskip(src, dst,
510 val = GET_SCL_FT_BILI_DN(src, dst);
518 static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win,
519 uint32_t src_w, uint32_t src_h, uint32_t dst_w,
520 uint32_t dst_h, uint32_t pixel_format)
522 uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode;
523 uint16_t cbcr_hor_scl_mode = SCALE_NONE;
524 uint16_t cbcr_ver_scl_mode = SCALE_NONE;
525 int hsub = drm_format_horz_chroma_subsampling(pixel_format);
526 int vsub = drm_format_vert_chroma_subsampling(pixel_format);
527 bool is_yuv = is_yuv_support(pixel_format);
528 uint16_t cbcr_src_w = src_w / hsub;
529 uint16_t cbcr_src_h = src_h / vsub;
536 DRM_ERROR("Maximum destination width (3840) exceeded\n");
540 yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
541 yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
544 cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w);
545 cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h);
546 if (cbcr_hor_scl_mode == SCALE_DOWN)
547 lb_mode = scl_vop_cal_lb_mode(dst_w, true);
549 lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true);
551 if (yrgb_hor_scl_mode == SCALE_DOWN)
552 lb_mode = scl_vop_cal_lb_mode(dst_w, false);
554 lb_mode = scl_vop_cal_lb_mode(src_w, false);
557 VOP_SCL_SET(vop, win, lb_mode, lb_mode);
558 if (lb_mode == LB_RGB_3840X2) {
559 if (yrgb_ver_scl_mode != SCALE_NONE) {
560 DRM_ERROR("ERROR : not allow yrgb ver scale\n");
563 if (cbcr_ver_scl_mode != SCALE_NONE) {
564 DRM_ERROR("ERROR : not allow cbcr ver scale\n");
567 vsu_mode = SCALE_UP_BIL;
568 } else if (lb_mode == LB_RGB_2560X4) {
569 vsu_mode = SCALE_UP_BIL;
571 vsu_mode = SCALE_UP_BIC;
574 val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w,
576 VOP_SCL_SET(vop, win, scale_yrgb_x, val);
577 val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h,
578 false, vsu_mode, &vskiplines);
579 VOP_SCL_SET(vop, win, scale_yrgb_y, val);
581 VOP_SCL_SET(vop, win, vsd_yrgb_gt4, vskiplines == 4);
582 VOP_SCL_SET(vop, win, vsd_yrgb_gt2, vskiplines == 2);
584 VOP_SCL_SET(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode);
585 VOP_SCL_SET(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode);
586 VOP_SCL_SET(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL);
587 VOP_SCL_SET(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL);
588 VOP_SCL_SET(vop, win, yrgb_vsu_mode, vsu_mode);
590 val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w,
591 dst_w, true, 0, NULL);
592 VOP_SCL_SET(vop, win, scale_cbcr_x, val);
593 val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h,
594 dst_h, false, vsu_mode, &vskiplines);
595 VOP_SCL_SET(vop, win, scale_cbcr_y, val);
597 VOP_SCL_SET(vop, win, vsd_cbcr_gt4, vskiplines == 4);
598 VOP_SCL_SET(vop, win, vsd_cbcr_gt2, vskiplines == 2);
599 VOP_SCL_SET(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode);
600 VOP_SCL_SET(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode);
601 VOP_SCL_SET(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL);
602 VOP_SCL_SET(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL);
603 VOP_SCL_SET(vop, win, cbcr_vsu_mode, vsu_mode);
607 static void vop_dsp_hold_valid_irq_enable(struct vop *vop)
611 if (WARN_ON(!vop->is_enabled))
614 spin_lock_irqsave(&vop->irq_lock, flags);
616 vop_mask_write(vop, INTR_CTRL0, DSP_HOLD_VALID_INTR_MASK,
617 DSP_HOLD_VALID_INTR_EN(1));
619 spin_unlock_irqrestore(&vop->irq_lock, flags);
622 static void vop_dsp_hold_valid_irq_disable(struct vop *vop)
626 if (WARN_ON(!vop->is_enabled))
629 spin_lock_irqsave(&vop->irq_lock, flags);
631 vop_mask_write(vop, INTR_CTRL0, DSP_HOLD_VALID_INTR_MASK,
632 DSP_HOLD_VALID_INTR_EN(0));
634 spin_unlock_irqrestore(&vop->irq_lock, flags);
637 static void vop_enable(struct drm_crtc *crtc)
639 struct vop *vop = to_vop(crtc);
645 ret = pm_runtime_get_sync(vop->dev);
647 dev_err(vop->dev, "failed to get pm runtime: %d\n", ret);
651 ret = clk_enable(vop->hclk);
653 dev_err(vop->dev, "failed to enable hclk - %d\n", ret);
657 ret = clk_enable(vop->dclk);
659 dev_err(vop->dev, "failed to enable dclk - %d\n", ret);
660 goto err_disable_hclk;
663 ret = clk_enable(vop->aclk);
665 dev_err(vop->dev, "failed to enable aclk - %d\n", ret);
666 goto err_disable_dclk;
670 * Slave iommu shares power, irq and clock with vop. It was associated
671 * automatically with this master device via common driver code.
672 * Now that we have enabled the clock we attach it to the shared drm
675 ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
677 dev_err(vop->dev, "failed to attach dma mapping, %d\n", ret);
678 goto err_disable_aclk;
681 memcpy(vop->regs, vop->regsbak, vop->len);
683 * At here, vop clock & iommu is enable, R/W vop regs would be safe.
685 vop->is_enabled = true;
687 spin_lock(&vop->reg_lock);
689 VOP_CTRL_SET(vop, standby, 0);
691 spin_unlock(&vop->reg_lock);
693 enable_irq(vop->irq);
695 drm_crtc_vblank_on(crtc);
700 clk_disable(vop->aclk);
702 clk_disable(vop->dclk);
704 clk_disable(vop->hclk);
707 static void vop_crtc_disable(struct drm_crtc *crtc)
709 struct vop *vop = to_vop(crtc);
711 if (!vop->is_enabled)
714 drm_crtc_vblank_off(crtc);
717 * Vop standby will take effect at end of current frame,
718 * if dsp hold valid irq happen, it means standby complete.
720 * we must wait standby complete when we want to disable aclk,
721 * if not, memory bus maybe dead.
723 reinit_completion(&vop->dsp_hold_completion);
724 vop_dsp_hold_valid_irq_enable(vop);
726 spin_lock(&vop->reg_lock);
728 VOP_CTRL_SET(vop, standby, 1);
730 spin_unlock(&vop->reg_lock);
732 wait_for_completion(&vop->dsp_hold_completion);
734 vop_dsp_hold_valid_irq_disable(vop);
736 disable_irq(vop->irq);
738 vop->is_enabled = false;
741 * vop standby complete, so iommu detach is safe.
743 rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
745 clk_disable(vop->dclk);
746 clk_disable(vop->aclk);
747 clk_disable(vop->hclk);
748 pm_runtime_put(vop->dev);
751 static void vop_plane_destroy(struct drm_plane *plane)
753 drm_plane_cleanup(plane);
756 static int vop_plane_atomic_check(struct drm_plane *plane,
757 struct drm_plane_state *state)
759 struct drm_crtc *crtc = state->crtc;
760 struct drm_framebuffer *fb = state->fb;
761 struct vop_win *vop_win = to_vop_win(plane);
762 struct vop_plane_state *vop_plane_state = to_vop_plane_state(state);
763 const struct vop_win_data *win = vop_win->data;
766 struct drm_rect *dest = &vop_plane_state->dest;
767 struct drm_rect *src = &vop_plane_state->src;
768 struct drm_rect clip;
769 int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
770 DRM_PLANE_HELPER_NO_SCALING;
771 int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
772 DRM_PLANE_HELPER_NO_SCALING;
774 crtc = crtc ? crtc : plane->state->crtc;
776 * Both crtc or plane->state->crtc can be null.
780 src->x1 = state->src_x;
781 src->y1 = state->src_y;
782 src->x2 = state->src_x + state->src_w;
783 src->y2 = state->src_y + state->src_h;
784 dest->x1 = state->crtc_x;
785 dest->y1 = state->crtc_y;
786 dest->x2 = state->crtc_x + state->crtc_w;
787 dest->y2 = state->crtc_y + state->crtc_h;
791 clip.x2 = crtc->mode.hdisplay;
792 clip.y2 = crtc->mode.vdisplay;
794 ret = drm_plane_helper_check_update(plane, crtc, state->fb,
798 true, true, &visible);
805 vop_plane_state->format = vop_convert_format(fb->pixel_format);
806 if (vop_plane_state->format < 0)
807 return vop_plane_state->format;
810 * Src.x1 can be odd when do clip, but yuv plane start point
811 * need align with 2 pixel.
813 if (is_yuv_support(fb->pixel_format) && ((src->x1 >> 16) % 2))
816 vop_plane_state->enable = true;
821 vop_plane_state->enable = false;
825 static void vop_plane_atomic_disable(struct drm_plane *plane,
826 struct drm_plane_state *old_state)
828 struct vop_plane_state *vop_plane_state = to_vop_plane_state(old_state);
829 struct vop_win *vop_win = to_vop_win(plane);
830 const struct vop_win_data *win = vop_win->data;
831 struct vop *vop = to_vop(old_state->crtc);
833 if (!old_state->crtc)
836 spin_lock(&vop->reg_lock);
838 VOP_WIN_SET(vop, win, enable, 0);
840 spin_unlock(&vop->reg_lock);
842 vop_plane_state->enable = false;
845 static void vop_plane_atomic_update(struct drm_plane *plane,
846 struct drm_plane_state *old_state)
848 struct drm_plane_state *state = plane->state;
849 struct drm_crtc *crtc = state->crtc;
850 struct vop_win *vop_win = to_vop_win(plane);
851 struct vop_plane_state *vop_plane_state = to_vop_plane_state(state);
852 const struct vop_win_data *win = vop_win->data;
853 struct vop *vop = to_vop(state->crtc);
854 struct drm_framebuffer *fb = state->fb;
855 unsigned int actual_w, actual_h;
856 unsigned int dsp_stx, dsp_sty;
857 uint32_t act_info, dsp_info, dsp_st;
858 struct drm_rect *src = &vop_plane_state->src;
859 struct drm_rect *dest = &vop_plane_state->dest;
860 struct drm_gem_object *obj, *uv_obj;
861 struct rockchip_gem_object *rk_obj, *rk_uv_obj;
862 unsigned long offset;
868 * can't update plane when vop is disabled.
873 if (WARN_ON(!vop->is_enabled))
876 if (!vop_plane_state->enable) {
877 vop_plane_atomic_disable(plane, old_state);
881 obj = rockchip_fb_get_gem_obj(fb, 0);
882 rk_obj = to_rockchip_obj(obj);
884 actual_w = drm_rect_width(src) >> 16;
885 actual_h = drm_rect_height(src) >> 16;
886 act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
888 dsp_info = (drm_rect_height(dest) - 1) << 16;
889 dsp_info |= (drm_rect_width(dest) - 1) & 0xffff;
891 dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start;
892 dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start;
893 dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff);
895 offset = (src->x1 >> 16) * drm_format_plane_cpp(fb->pixel_format, 0);
896 offset += (src->y1 >> 16) * fb->pitches[0];
897 vop_plane_state->yrgb_mst = rk_obj->dma_addr + offset + fb->offsets[0];
899 spin_lock(&vop->reg_lock);
901 VOP_WIN_SET(vop, win, format, vop_plane_state->format);
902 VOP_WIN_SET(vop, win, yrgb_vir, fb->pitches[0] >> 2);
903 VOP_WIN_SET(vop, win, yrgb_mst, vop_plane_state->yrgb_mst);
904 if (is_yuv_support(fb->pixel_format)) {
905 int hsub = drm_format_horz_chroma_subsampling(fb->pixel_format);
906 int vsub = drm_format_vert_chroma_subsampling(fb->pixel_format);
907 int bpp = drm_format_plane_cpp(fb->pixel_format, 1);
909 uv_obj = rockchip_fb_get_gem_obj(fb, 1);
910 rk_uv_obj = to_rockchip_obj(uv_obj);
912 offset = (src->x1 >> 16) * bpp / hsub;
913 offset += (src->y1 >> 16) * fb->pitches[1] / vsub;
915 dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1];
916 VOP_WIN_SET(vop, win, uv_vir, fb->pitches[1] >> 2);
917 VOP_WIN_SET(vop, win, uv_mst, dma_addr);
921 scl_vop_cal_scl_fac(vop, win, actual_w, actual_h,
922 drm_rect_width(dest), drm_rect_height(dest),
925 VOP_WIN_SET(vop, win, act_info, act_info);
926 VOP_WIN_SET(vop, win, dsp_info, dsp_info);
927 VOP_WIN_SET(vop, win, dsp_st, dsp_st);
929 rb_swap = has_rb_swapped(fb->pixel_format);
930 VOP_WIN_SET(vop, win, rb_swap, rb_swap);
932 if (is_alpha_support(fb->pixel_format)) {
933 VOP_WIN_SET(vop, win, dst_alpha_ctl,
934 DST_FACTOR_M0(ALPHA_SRC_INVERSE));
935 val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
936 SRC_ALPHA_M0(ALPHA_STRAIGHT) |
937 SRC_BLEND_M0(ALPHA_PER_PIX) |
938 SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
939 SRC_FACTOR_M0(ALPHA_ONE);
940 VOP_WIN_SET(vop, win, src_alpha_ctl, val);
942 VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
945 VOP_WIN_SET(vop, win, enable, 1);
946 spin_unlock(&vop->reg_lock);
949 static const struct drm_plane_helper_funcs plane_helper_funcs = {
950 .atomic_check = vop_plane_atomic_check,
951 .atomic_update = vop_plane_atomic_update,
952 .atomic_disable = vop_plane_atomic_disable,
955 void vop_atomic_plane_reset(struct drm_plane *plane)
957 struct vop_plane_state *vop_plane_state =
958 to_vop_plane_state(plane->state);
960 if (plane->state && plane->state->fb)
961 drm_framebuffer_unreference(plane->state->fb);
963 kfree(vop_plane_state);
964 vop_plane_state = kzalloc(sizeof(*vop_plane_state), GFP_KERNEL);
965 if (!vop_plane_state)
968 plane->state = &vop_plane_state->base;
969 plane->state->plane = plane;
972 struct drm_plane_state *
973 vop_atomic_plane_duplicate_state(struct drm_plane *plane)
975 struct vop_plane_state *old_vop_plane_state;
976 struct vop_plane_state *vop_plane_state;
978 if (WARN_ON(!plane->state))
981 old_vop_plane_state = to_vop_plane_state(plane->state);
982 vop_plane_state = kmemdup(old_vop_plane_state,
983 sizeof(*vop_plane_state), GFP_KERNEL);
984 if (!vop_plane_state)
987 __drm_atomic_helper_plane_duplicate_state(plane,
988 &vop_plane_state->base);
990 return &vop_plane_state->base;
993 static void vop_atomic_plane_destroy_state(struct drm_plane *plane,
994 struct drm_plane_state *state)
996 struct vop_plane_state *vop_state = to_vop_plane_state(state);
998 __drm_atomic_helper_plane_destroy_state(plane, state);
1003 static const struct drm_plane_funcs vop_plane_funcs = {
1004 .update_plane = drm_atomic_helper_update_plane,
1005 .disable_plane = drm_atomic_helper_disable_plane,
1006 .destroy = vop_plane_destroy,
1007 .reset = vop_atomic_plane_reset,
1008 .atomic_duplicate_state = vop_atomic_plane_duplicate_state,
1009 .atomic_destroy_state = vop_atomic_plane_destroy_state,
1012 int rockchip_drm_crtc_mode_config(struct drm_crtc *crtc,
1016 struct vop *vop = to_vop(crtc);
1018 if (WARN_ON(!vop->is_enabled))
1021 switch (connector_type) {
1022 case DRM_MODE_CONNECTOR_LVDS:
1023 VOP_CTRL_SET(vop, rgb_en, 1);
1025 case DRM_MODE_CONNECTOR_eDP:
1026 VOP_CTRL_SET(vop, edp_en, 1);
1028 case DRM_MODE_CONNECTOR_HDMIA:
1029 VOP_CTRL_SET(vop, hdmi_en, 1);
1032 DRM_ERROR("unsupport connector_type[%d]\n", connector_type);
1035 VOP_CTRL_SET(vop, out_mode, out_mode);
1039 EXPORT_SYMBOL_GPL(rockchip_drm_crtc_mode_config);
1041 static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
1043 struct vop *vop = to_vop(crtc);
1044 unsigned long flags;
1046 if (WARN_ON(!vop->is_enabled))
1049 spin_lock_irqsave(&vop->irq_lock, flags);
1051 vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(1));
1053 spin_unlock_irqrestore(&vop->irq_lock, flags);
1058 static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
1060 struct vop *vop = to_vop(crtc);
1061 unsigned long flags;
1063 if (WARN_ON(!vop->is_enabled))
1066 spin_lock_irqsave(&vop->irq_lock, flags);
1067 vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(0));
1068 spin_unlock_irqrestore(&vop->irq_lock, flags);
1071 static void vop_crtc_wait_for_update(struct drm_crtc *crtc)
1073 struct vop *vop = to_vop(crtc);
1075 reinit_completion(&vop->wait_update_complete);
1076 WARN_ON(!wait_for_completion_timeout(&vop->wait_update_complete, 100));
1079 static const struct rockchip_crtc_funcs private_crtc_funcs = {
1080 .enable_vblank = vop_crtc_enable_vblank,
1081 .disable_vblank = vop_crtc_disable_vblank,
1082 .wait_for_update = vop_crtc_wait_for_update,
1085 static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
1086 const struct drm_display_mode *mode,
1087 struct drm_display_mode *adjusted_mode)
1089 if (adjusted_mode->htotal == 0 || adjusted_mode->vtotal == 0)
1095 static void vop_crtc_enable(struct drm_crtc *crtc)
1097 struct vop *vop = to_vop(crtc);
1098 struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
1099 u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
1100 u16 hdisplay = adjusted_mode->hdisplay;
1101 u16 htotal = adjusted_mode->htotal;
1102 u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
1103 u16 hact_end = hact_st + hdisplay;
1104 u16 vdisplay = adjusted_mode->vdisplay;
1105 u16 vtotal = adjusted_mode->vtotal;
1106 u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
1107 u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
1108 u16 vact_end = vact_st + vdisplay;
1113 * If dclk rate is zero, mean that scanout is stop,
1114 * we don't need wait any more.
1116 if (clk_get_rate(vop->dclk)) {
1118 * Rk3288 vop timing register is immediately, when configure
1119 * display timing on display time, may cause tearing.
1121 * Vop standby will take effect at end of current frame,
1122 * if dsp hold valid irq happen, it means standby complete.
1125 * standby and wait complete --> |----
1128 * |---> dsp hold irq
1129 * configure display timing --> |
1131 * | new frame start.
1134 reinit_completion(&vop->dsp_hold_completion);
1135 vop_dsp_hold_valid_irq_enable(vop);
1137 spin_lock(&vop->reg_lock);
1139 VOP_CTRL_SET(vop, standby, 1);
1141 spin_unlock(&vop->reg_lock);
1143 wait_for_completion(&vop->dsp_hold_completion);
1145 vop_dsp_hold_valid_irq_disable(vop);
1149 val |= (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) ? 0 : 1;
1150 val |= (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC) ? 0 : (1 << 1);
1151 VOP_CTRL_SET(vop, pin_pol, val);
1153 VOP_CTRL_SET(vop, htotal_pw, (htotal << 16) | hsync_len);
1154 val = hact_st << 16;
1156 VOP_CTRL_SET(vop, hact_st_end, val);
1157 VOP_CTRL_SET(vop, hpost_st_end, val);
1159 VOP_CTRL_SET(vop, vtotal_pw, (vtotal << 16) | vsync_len);
1160 val = vact_st << 16;
1162 VOP_CTRL_SET(vop, vact_st_end, val);
1163 VOP_CTRL_SET(vop, vpost_st_end, val);
1165 clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
1167 VOP_CTRL_SET(vop, standby, 0);
1170 static void vop_crtc_atomic_flush(struct drm_crtc *crtc,
1171 struct drm_crtc_state *old_crtc_state)
1173 struct vop *vop = to_vop(crtc);
1175 if (WARN_ON(!vop->is_enabled))
1178 spin_lock(&vop->reg_lock);
1182 spin_unlock(&vop->reg_lock);
1185 static void vop_crtc_atomic_begin(struct drm_crtc *crtc,
1186 struct drm_crtc_state *old_crtc_state)
1188 struct vop *vop = to_vop(crtc);
1190 if (crtc->state->event) {
1191 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1193 vop->event = crtc->state->event;
1194 crtc->state->event = NULL;
1198 static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
1199 .enable = vop_crtc_enable,
1200 .disable = vop_crtc_disable,
1201 .mode_fixup = vop_crtc_mode_fixup,
1202 .atomic_flush = vop_crtc_atomic_flush,
1203 .atomic_begin = vop_crtc_atomic_begin,
1206 static void vop_crtc_destroy(struct drm_crtc *crtc)
1208 drm_crtc_cleanup(crtc);
1211 static const struct drm_crtc_funcs vop_crtc_funcs = {
1212 .set_config = drm_atomic_helper_set_config,
1213 .page_flip = drm_atomic_helper_page_flip,
1214 .destroy = vop_crtc_destroy,
1215 .reset = drm_atomic_helper_crtc_reset,
1216 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
1217 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
1220 static bool vop_win_pending_is_complete(struct vop_win *vop_win)
1222 struct drm_plane *plane = &vop_win->base;
1223 struct vop_plane_state *state = to_vop_plane_state(plane->state);
1224 dma_addr_t yrgb_mst;
1227 return VOP_WIN_GET(vop_win->vop, vop_win->data, enable) == 0;
1229 yrgb_mst = VOP_WIN_GET_YRGBADDR(vop_win->vop, vop_win->data);
1231 return yrgb_mst == state->yrgb_mst;
1234 static void vop_handle_vblank(struct vop *vop)
1236 struct drm_device *drm = vop->drm_dev;
1237 struct drm_crtc *crtc = &vop->crtc;
1238 unsigned long flags;
1241 for (i = 0; i < vop->data->win_size; i++) {
1242 if (!vop_win_pending_is_complete(&vop->win[i]))
1247 spin_lock_irqsave(&drm->event_lock, flags);
1249 drm_crtc_send_vblank_event(crtc, vop->event);
1250 drm_crtc_vblank_put(crtc);
1253 spin_unlock_irqrestore(&drm->event_lock, flags);
1255 if (!completion_done(&vop->wait_update_complete))
1256 complete(&vop->wait_update_complete);
1259 static irqreturn_t vop_isr(int irq, void *data)
1261 struct vop *vop = data;
1262 struct drm_crtc *crtc = &vop->crtc;
1263 uint32_t intr0_reg, active_irqs;
1264 unsigned long flags;
1268 * INTR_CTRL0 register has interrupt status, enable and clear bits, we
1269 * must hold irq_lock to avoid a race with enable/disable_vblank().
1271 spin_lock_irqsave(&vop->irq_lock, flags);
1272 intr0_reg = vop_readl(vop, INTR_CTRL0);
1273 active_irqs = intr0_reg & INTR_MASK;
1274 /* Clear all active interrupt sources */
1276 vop_writel(vop, INTR_CTRL0,
1277 intr0_reg | (active_irqs << INTR_CLR_SHIFT));
1278 spin_unlock_irqrestore(&vop->irq_lock, flags);
1280 /* This is expected for vop iommu irqs, since the irq is shared */
1284 if (active_irqs & DSP_HOLD_VALID_INTR) {
1285 complete(&vop->dsp_hold_completion);
1286 active_irqs &= ~DSP_HOLD_VALID_INTR;
1290 if (active_irqs & FS_INTR) {
1291 drm_crtc_handle_vblank(crtc);
1292 vop_handle_vblank(vop);
1293 active_irqs &= ~FS_INTR;
1297 /* Unhandled irqs are spurious. */
1299 DRM_ERROR("Unknown VOP IRQs: %#02x\n", active_irqs);
1304 static int vop_create_crtc(struct vop *vop)
1306 const struct vop_data *vop_data = vop->data;
1307 struct device *dev = vop->dev;
1308 struct drm_device *drm_dev = vop->drm_dev;
1309 struct drm_plane *primary = NULL, *cursor = NULL, *plane;
1310 struct drm_crtc *crtc = &vop->crtc;
1311 struct device_node *port;
1316 * Create drm_plane for primary and cursor planes first, since we need
1317 * to pass them to drm_crtc_init_with_planes, which sets the
1318 * "possible_crtcs" to the newly initialized crtc.
1320 for (i = 0; i < vop_data->win_size; i++) {
1321 struct vop_win *vop_win = &vop->win[i];
1322 const struct vop_win_data *win_data = vop_win->data;
1324 if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
1325 win_data->type != DRM_PLANE_TYPE_CURSOR)
1328 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1329 0, &vop_plane_funcs,
1330 win_data->phy->data_formats,
1331 win_data->phy->nformats,
1332 win_data->type, NULL);
1334 DRM_ERROR("failed to initialize plane\n");
1335 goto err_cleanup_planes;
1338 plane = &vop_win->base;
1339 drm_plane_helper_add(plane, &plane_helper_funcs);
1340 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1342 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
1346 ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
1347 &vop_crtc_funcs, NULL);
1351 drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
1354 * Create drm_planes for overlay windows with possible_crtcs restricted
1355 * to the newly created crtc.
1357 for (i = 0; i < vop_data->win_size; i++) {
1358 struct vop_win *vop_win = &vop->win[i];
1359 const struct vop_win_data *win_data = vop_win->data;
1360 unsigned long possible_crtcs = 1 << drm_crtc_index(crtc);
1362 if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
1365 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1368 win_data->phy->data_formats,
1369 win_data->phy->nformats,
1370 win_data->type, NULL);
1372 DRM_ERROR("failed to initialize overlay plane\n");
1373 goto err_cleanup_crtc;
1375 drm_plane_helper_add(&vop_win->base, &plane_helper_funcs);
1378 port = of_get_child_by_name(dev->of_node, "port");
1380 DRM_ERROR("no port node found in %s\n",
1381 dev->of_node->full_name);
1382 goto err_cleanup_crtc;
1385 init_completion(&vop->dsp_hold_completion);
1386 init_completion(&vop->wait_update_complete);
1388 rockchip_register_crtc_funcs(crtc, &private_crtc_funcs);
1393 drm_crtc_cleanup(crtc);
1395 list_for_each_entry(plane, &drm_dev->mode_config.plane_list, head)
1396 drm_plane_cleanup(plane);
1400 static void vop_destroy_crtc(struct vop *vop)
1402 struct drm_crtc *crtc = &vop->crtc;
1404 rockchip_unregister_crtc_funcs(crtc);
1405 of_node_put(crtc->port);
1406 drm_crtc_cleanup(crtc);
1409 static int vop_initial(struct vop *vop)
1411 const struct vop_data *vop_data = vop->data;
1412 const struct vop_reg_data *init_table = vop_data->init_table;
1413 struct reset_control *ahb_rst;
1416 vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
1417 if (IS_ERR(vop->hclk)) {
1418 dev_err(vop->dev, "failed to get hclk source\n");
1419 return PTR_ERR(vop->hclk);
1421 vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
1422 if (IS_ERR(vop->aclk)) {
1423 dev_err(vop->dev, "failed to get aclk source\n");
1424 return PTR_ERR(vop->aclk);
1426 vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
1427 if (IS_ERR(vop->dclk)) {
1428 dev_err(vop->dev, "failed to get dclk source\n");
1429 return PTR_ERR(vop->dclk);
1432 ret = clk_prepare(vop->dclk);
1434 dev_err(vop->dev, "failed to prepare dclk\n");
1438 /* Enable both the hclk and aclk to setup the vop */
1439 ret = clk_prepare_enable(vop->hclk);
1441 dev_err(vop->dev, "failed to prepare/enable hclk\n");
1442 goto err_unprepare_dclk;
1445 ret = clk_prepare_enable(vop->aclk);
1447 dev_err(vop->dev, "failed to prepare/enable aclk\n");
1448 goto err_disable_hclk;
1452 * do hclk_reset, reset all vop registers.
1454 ahb_rst = devm_reset_control_get(vop->dev, "ahb");
1455 if (IS_ERR(ahb_rst)) {
1456 dev_err(vop->dev, "failed to get ahb reset\n");
1457 ret = PTR_ERR(ahb_rst);
1458 goto err_disable_aclk;
1460 reset_control_assert(ahb_rst);
1461 usleep_range(10, 20);
1462 reset_control_deassert(ahb_rst);
1464 memcpy(vop->regsbak, vop->regs, vop->len);
1466 for (i = 0; i < vop_data->table_size; i++)
1467 vop_writel(vop, init_table[i].offset, init_table[i].value);
1469 for (i = 0; i < vop_data->win_size; i++) {
1470 const struct vop_win_data *win = &vop_data->win[i];
1472 VOP_WIN_SET(vop, win, enable, 0);
1478 * do dclk_reset, let all config take affect.
1480 vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
1481 if (IS_ERR(vop->dclk_rst)) {
1482 dev_err(vop->dev, "failed to get dclk reset\n");
1483 ret = PTR_ERR(vop->dclk_rst);
1484 goto err_disable_aclk;
1486 reset_control_assert(vop->dclk_rst);
1487 usleep_range(10, 20);
1488 reset_control_deassert(vop->dclk_rst);
1490 clk_disable(vop->hclk);
1491 clk_disable(vop->aclk);
1493 vop->is_enabled = false;
1498 clk_disable_unprepare(vop->aclk);
1500 clk_disable_unprepare(vop->hclk);
1502 clk_unprepare(vop->dclk);
1507 * Initialize the vop->win array elements.
1509 static void vop_win_init(struct vop *vop)
1511 const struct vop_data *vop_data = vop->data;
1514 for (i = 0; i < vop_data->win_size; i++) {
1515 struct vop_win *vop_win = &vop->win[i];
1516 const struct vop_win_data *win_data = &vop_data->win[i];
1518 vop_win->data = win_data;
1523 static int vop_bind(struct device *dev, struct device *master, void *data)
1525 struct platform_device *pdev = to_platform_device(dev);
1526 const struct of_device_id *of_id;
1527 const struct vop_data *vop_data;
1528 struct drm_device *drm_dev = data;
1530 struct resource *res;
1534 of_id = of_match_device(vop_driver_dt_match, dev);
1535 vop_data = of_id->data;
1539 /* Allocate vop struct and its vop_win array */
1540 alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size;
1541 vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
1546 vop->data = vop_data;
1547 vop->drm_dev = drm_dev;
1548 dev_set_drvdata(dev, vop);
1552 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1553 vop->len = resource_size(res);
1554 vop->regs = devm_ioremap_resource(dev, res);
1555 if (IS_ERR(vop->regs))
1556 return PTR_ERR(vop->regs);
1558 vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
1562 ret = vop_initial(vop);
1564 dev_err(&pdev->dev, "cannot initial vop dev - err %d\n", ret);
1568 irq = platform_get_irq(pdev, 0);
1570 dev_err(dev, "cannot find irq for vop\n");
1573 vop->irq = (unsigned int)irq;
1575 spin_lock_init(&vop->reg_lock);
1576 spin_lock_init(&vop->irq_lock);
1578 mutex_init(&vop->vsync_mutex);
1580 ret = devm_request_irq(dev, vop->irq, vop_isr,
1581 IRQF_SHARED, dev_name(dev), vop);
1585 /* IRQ is initially disabled; it gets enabled in power_on */
1586 disable_irq(vop->irq);
1588 ret = vop_create_crtc(vop);
1592 pm_runtime_enable(&pdev->dev);
1596 static void vop_unbind(struct device *dev, struct device *master, void *data)
1598 struct vop *vop = dev_get_drvdata(dev);
1600 pm_runtime_disable(dev);
1601 vop_destroy_crtc(vop);
1604 static const struct component_ops vop_component_ops = {
1606 .unbind = vop_unbind,
1609 static int vop_probe(struct platform_device *pdev)
1611 struct device *dev = &pdev->dev;
1613 if (!dev->of_node) {
1614 dev_err(dev, "can't find vop devices\n");
1618 return component_add(dev, &vop_component_ops);
1621 static int vop_remove(struct platform_device *pdev)
1623 component_del(&pdev->dev, &vop_component_ops);
1628 struct platform_driver vop_platform_driver = {
1630 .remove = vop_remove,
1632 .name = "rockchip-vop",
1633 .owner = THIS_MODULE,
1634 .of_match_table = of_match_ptr(vop_driver_dt_match),
1638 module_platform_driver(vop_platform_driver);
1640 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1641 MODULE_DESCRIPTION("ROCKCHIP VOP Driver");
1642 MODULE_LICENSE("GPL v2");