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_crtc.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_plane_helper.h>
21 #include <linux/kernel.h>
22 #include <linux/platform_device.h>
23 #include <linux/clk.h>
25 #include <linux/of_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/component.h>
29 #include <linux/reset.h>
30 #include <linux/delay.h>
32 #include "rockchip_drm_drv.h"
33 #include "rockchip_drm_gem.h"
34 #include "rockchip_drm_fb.h"
35 #include "rockchip_drm_vop.h"
37 #define VOP_REG(off, _mask, s) \
42 #define __REG_SET_RELAXED(x, off, mask, shift, v) \
43 vop_mask_write_relaxed(x, off, (mask) << shift, (v) << shift)
44 #define __REG_SET_NORMAL(x, off, mask, shift, v) \
45 vop_mask_write(x, off, (mask) << shift, (v) << shift)
47 #define REG_SET(x, base, reg, v, mode) \
48 __REG_SET_##mode(x, base + reg.offset, reg.mask, reg.shift, v)
50 #define VOP_WIN_SET(x, win, name, v) \
51 REG_SET(x, win->base, win->phy->name, v, RELAXED)
52 #define VOP_CTRL_SET(x, name, v) \
53 REG_SET(x, 0, (x)->data->ctrl->name, v, NORMAL)
55 #define VOP_WIN_GET(x, win, name) \
56 vop_read_reg(x, win->base, &win->phy->name)
58 #define VOP_WIN_GET_YRGBADDR(vop, win) \
59 vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
61 #define to_vop(x) container_of(x, struct vop, crtc)
62 #define to_vop_win(x) container_of(x, struct vop_win, base)
64 struct vop_win_state
{
65 struct list_head head
;
66 struct drm_framebuffer
*fb
;
68 struct drm_pending_vblank_event
*event
;
72 struct drm_plane base
;
73 const struct vop_win_data
*data
;
76 struct list_head pending
;
77 struct vop_win_state
*active
;
83 struct drm_device
*drm_dev
;
87 int connector_out_mode
;
89 /* mutex vsync_ work */
90 struct mutex vsync_mutex
;
91 bool vsync_work_pending
;
93 const struct vop_data
*data
;
98 /* physical map length of vop register */
101 /* one time only one process allowed to config the register */
103 /* lock vop irq reg */
112 /* vop share memory frequency */
116 struct reset_control
*dclk_rst
;
120 struct vop_win win
[];
123 enum vop_data_format
{
124 VOP_FMT_ARGB8888
= 0,
127 VOP_FMT_YUV420SP
= 4,
132 struct vop_reg_data
{
144 struct vop_reg standby
;
145 struct vop_reg data_blank
;
146 struct vop_reg gate_en
;
147 struct vop_reg mmu_en
;
148 struct vop_reg rgb_en
;
149 struct vop_reg edp_en
;
150 struct vop_reg hdmi_en
;
151 struct vop_reg mipi_en
;
152 struct vop_reg out_mode
;
153 struct vop_reg dither_down
;
154 struct vop_reg dither_up
;
155 struct vop_reg pin_pol
;
157 struct vop_reg htotal_pw
;
158 struct vop_reg hact_st_end
;
159 struct vop_reg vtotal_pw
;
160 struct vop_reg vact_st_end
;
161 struct vop_reg hpost_st_end
;
162 struct vop_reg vpost_st_end
;
166 const uint32_t *data_formats
;
169 struct vop_reg enable
;
170 struct vop_reg format
;
171 struct vop_reg act_info
;
172 struct vop_reg dsp_info
;
173 struct vop_reg dsp_st
;
174 struct vop_reg yrgb_mst
;
175 struct vop_reg uv_mst
;
176 struct vop_reg yrgb_vir
;
177 struct vop_reg uv_vir
;
179 struct vop_reg dst_alpha_ctl
;
180 struct vop_reg src_alpha_ctl
;
183 struct vop_win_data
{
185 const struct vop_win_phy
*phy
;
186 enum drm_plane_type type
;
190 const struct vop_reg_data
*init_table
;
191 unsigned int table_size
;
192 const struct vop_ctrl
*ctrl
;
193 const struct vop_win_data
*win
;
194 unsigned int win_size
;
197 static const uint32_t formats_01
[] = {
207 static const uint32_t formats_234
[] = {
214 static const struct vop_win_phy win01_data
= {
215 .data_formats
= formats_01
,
216 .nformats
= ARRAY_SIZE(formats_01
),
217 .enable
= VOP_REG(WIN0_CTRL0
, 0x1, 0),
218 .format
= VOP_REG(WIN0_CTRL0
, 0x7, 1),
219 .act_info
= VOP_REG(WIN0_ACT_INFO
, 0x1fff1fff, 0),
220 .dsp_info
= VOP_REG(WIN0_DSP_INFO
, 0x0fff0fff, 0),
221 .dsp_st
= VOP_REG(WIN0_DSP_ST
, 0x1fff1fff, 0),
222 .yrgb_mst
= VOP_REG(WIN0_YRGB_MST
, 0xffffffff, 0),
223 .uv_mst
= VOP_REG(WIN0_CBR_MST
, 0xffffffff, 0),
224 .yrgb_vir
= VOP_REG(WIN0_VIR
, 0x3fff, 0),
225 .uv_vir
= VOP_REG(WIN0_VIR
, 0x3fff, 16),
226 .src_alpha_ctl
= VOP_REG(WIN0_SRC_ALPHA_CTRL
, 0xff, 0),
227 .dst_alpha_ctl
= VOP_REG(WIN0_DST_ALPHA_CTRL
, 0xff, 0),
230 static const struct vop_win_phy win23_data
= {
231 .data_formats
= formats_234
,
232 .nformats
= ARRAY_SIZE(formats_234
),
233 .enable
= VOP_REG(WIN2_CTRL0
, 0x1, 0),
234 .format
= VOP_REG(WIN2_CTRL0
, 0x7, 1),
235 .dsp_info
= VOP_REG(WIN2_DSP_INFO0
, 0x0fff0fff, 0),
236 .dsp_st
= VOP_REG(WIN2_DSP_ST0
, 0x1fff1fff, 0),
237 .yrgb_mst
= VOP_REG(WIN2_MST0
, 0xffffffff, 0),
238 .yrgb_vir
= VOP_REG(WIN2_VIR0_1
, 0x1fff, 0),
239 .src_alpha_ctl
= VOP_REG(WIN2_SRC_ALPHA_CTRL
, 0xff, 0),
240 .dst_alpha_ctl
= VOP_REG(WIN2_DST_ALPHA_CTRL
, 0xff, 0),
243 static const struct vop_win_phy cursor_data
= {
244 .data_formats
= formats_234
,
245 .nformats
= ARRAY_SIZE(formats_234
),
246 .enable
= VOP_REG(HWC_CTRL0
, 0x1, 0),
247 .format
= VOP_REG(HWC_CTRL0
, 0x7, 1),
248 .dsp_st
= VOP_REG(HWC_DSP_ST
, 0x1fff1fff, 0),
249 .yrgb_mst
= VOP_REG(HWC_MST
, 0xffffffff, 0),
252 static const struct vop_ctrl ctrl_data
= {
253 .standby
= VOP_REG(SYS_CTRL
, 0x1, 22),
254 .gate_en
= VOP_REG(SYS_CTRL
, 0x1, 23),
255 .mmu_en
= VOP_REG(SYS_CTRL
, 0x1, 20),
256 .rgb_en
= VOP_REG(SYS_CTRL
, 0x1, 12),
257 .hdmi_en
= VOP_REG(SYS_CTRL
, 0x1, 13),
258 .edp_en
= VOP_REG(SYS_CTRL
, 0x1, 14),
259 .mipi_en
= VOP_REG(SYS_CTRL
, 0x1, 15),
260 .dither_down
= VOP_REG(DSP_CTRL1
, 0xf, 1),
261 .dither_up
= VOP_REG(DSP_CTRL1
, 0x1, 6),
262 .data_blank
= VOP_REG(DSP_CTRL0
, 0x1, 19),
263 .out_mode
= VOP_REG(DSP_CTRL0
, 0xf, 0),
264 .pin_pol
= VOP_REG(DSP_CTRL0
, 0xf, 4),
265 .htotal_pw
= VOP_REG(DSP_HTOTAL_HS_END
, 0x1fff1fff, 0),
266 .hact_st_end
= VOP_REG(DSP_HACT_ST_END
, 0x1fff1fff, 0),
267 .vtotal_pw
= VOP_REG(DSP_VTOTAL_VS_END
, 0x1fff1fff, 0),
268 .vact_st_end
= VOP_REG(DSP_VACT_ST_END
, 0x1fff1fff, 0),
269 .hpost_st_end
= VOP_REG(POST_DSP_HACT_INFO
, 0x1fff1fff, 0),
270 .vpost_st_end
= VOP_REG(POST_DSP_VACT_INFO
, 0x1fff1fff, 0),
273 static const struct vop_reg_data vop_init_reg_table
[] = {
274 {SYS_CTRL
, 0x00c00000},
275 {DSP_CTRL0
, 0x00000000},
276 {WIN0_CTRL0
, 0x00000080},
277 {WIN1_CTRL0
, 0x00000080},
281 * Note: rk3288 has a dedicated 'cursor' window, however, that window requires
282 * special support to get alpha blending working. For now, just use overlay
283 * window 1 for the drm cursor.
285 static const struct vop_win_data rk3288_vop_win_data
[] = {
286 { .base
= 0x00, .phy
= &win01_data
, .type
= DRM_PLANE_TYPE_PRIMARY
},
287 { .base
= 0x40, .phy
= &win01_data
, .type
= DRM_PLANE_TYPE_CURSOR
},
288 { .base
= 0x00, .phy
= &win23_data
, .type
= DRM_PLANE_TYPE_OVERLAY
},
289 { .base
= 0x50, .phy
= &win23_data
, .type
= DRM_PLANE_TYPE_OVERLAY
},
290 { .base
= 0x00, .phy
= &cursor_data
, .type
= DRM_PLANE_TYPE_OVERLAY
},
293 static const struct vop_data rk3288_vop
= {
294 .init_table
= vop_init_reg_table
,
295 .table_size
= ARRAY_SIZE(vop_init_reg_table
),
297 .win
= rk3288_vop_win_data
,
298 .win_size
= ARRAY_SIZE(rk3288_vop_win_data
),
301 static const struct of_device_id vop_driver_dt_match
[] = {
302 { .compatible
= "rockchip,rk3288-vop",
303 .data
= &rk3288_vop
},
307 static inline void vop_writel(struct vop
*vop
, uint32_t offset
, uint32_t v
)
309 writel(v
, vop
->regs
+ offset
);
310 vop
->regsbak
[offset
>> 2] = v
;
313 static inline uint32_t vop_readl(struct vop
*vop
, uint32_t offset
)
315 return readl(vop
->regs
+ offset
);
318 static inline uint32_t vop_read_reg(struct vop
*vop
, uint32_t base
,
319 const struct vop_reg
*reg
)
321 return (vop_readl(vop
, base
+ reg
->offset
) >> reg
->shift
) & reg
->mask
;
324 static inline void vop_cfg_done(struct vop
*vop
)
326 writel(0x01, vop
->regs
+ REG_CFG_DONE
);
329 static inline void vop_mask_write(struct vop
*vop
, uint32_t offset
,
330 uint32_t mask
, uint32_t v
)
333 uint32_t cached_val
= vop
->regsbak
[offset
>> 2];
335 cached_val
= (cached_val
& ~mask
) | v
;
336 writel(cached_val
, vop
->regs
+ offset
);
337 vop
->regsbak
[offset
>> 2] = cached_val
;
341 static inline void vop_mask_write_relaxed(struct vop
*vop
, uint32_t offset
,
342 uint32_t mask
, uint32_t v
)
345 uint32_t cached_val
= vop
->regsbak
[offset
>> 2];
347 cached_val
= (cached_val
& ~mask
) | v
;
348 writel_relaxed(cached_val
, vop
->regs
+ offset
);
349 vop
->regsbak
[offset
>> 2] = cached_val
;
353 static enum vop_data_format
vop_convert_format(uint32_t format
)
356 case DRM_FORMAT_XRGB8888
:
357 case DRM_FORMAT_ARGB8888
:
358 return VOP_FMT_ARGB8888
;
359 case DRM_FORMAT_RGB888
:
360 return VOP_FMT_RGB888
;
361 case DRM_FORMAT_RGB565
:
362 return VOP_FMT_RGB565
;
363 case DRM_FORMAT_NV12
:
364 return VOP_FMT_YUV420SP
;
365 case DRM_FORMAT_NV16
:
366 return VOP_FMT_YUV422SP
;
367 case DRM_FORMAT_NV24
:
368 return VOP_FMT_YUV444SP
;
370 DRM_ERROR("unsupport format[%08x]\n", format
);
375 static bool is_alpha_support(uint32_t format
)
378 case DRM_FORMAT_ARGB8888
:
385 static void vop_enable(struct drm_crtc
*crtc
)
387 struct vop
*vop
= to_vop(crtc
);
393 ret
= clk_enable(vop
->hclk
);
395 dev_err(vop
->dev
, "failed to enable hclk - %d\n", ret
);
399 ret
= clk_enable(vop
->dclk
);
401 dev_err(vop
->dev
, "failed to enable dclk - %d\n", ret
);
402 goto err_disable_hclk
;
405 ret
= clk_enable(vop
->aclk
);
407 dev_err(vop
->dev
, "failed to enable aclk - %d\n", ret
);
408 goto err_disable_dclk
;
412 * Slave iommu shares power, irq and clock with vop. It was associated
413 * automatically with this master device via common driver code.
414 * Now that we have enabled the clock we attach it to the shared drm
417 ret
= rockchip_drm_dma_attach_device(vop
->drm_dev
, vop
->dev
);
419 dev_err(vop
->dev
, "failed to attach dma mapping, %d\n", ret
);
420 goto err_disable_aclk
;
424 * At here, vop clock & iommu is enable, R/W vop regs would be safe.
426 vop
->is_enabled
= true;
428 spin_lock(&vop
->reg_lock
);
430 VOP_CTRL_SET(vop
, standby
, 0);
432 spin_unlock(&vop
->reg_lock
);
434 enable_irq(vop
->irq
);
436 drm_vblank_on(vop
->drm_dev
, vop
->pipe
);
441 clk_disable(vop
->aclk
);
443 clk_disable(vop
->dclk
);
445 clk_disable(vop
->hclk
);
448 static void vop_disable(struct drm_crtc
*crtc
)
450 struct vop
*vop
= to_vop(crtc
);
452 if (!vop
->is_enabled
)
455 drm_vblank_off(crtc
->dev
, vop
->pipe
);
457 disable_irq(vop
->irq
);
460 * TODO: Since standby doesn't take effect until the next vblank,
461 * when we turn off dclk below, the vop is probably still active.
463 spin_lock(&vop
->reg_lock
);
465 VOP_CTRL_SET(vop
, standby
, 1);
467 spin_unlock(&vop
->reg_lock
);
469 vop
->is_enabled
= false;
471 * disable dclk to stop frame scan, so we can safely detach iommu,
473 clk_disable(vop
->dclk
);
475 rockchip_drm_dma_detach_device(vop
->drm_dev
, vop
->dev
);
477 clk_disable(vop
->aclk
);
478 clk_disable(vop
->hclk
);
482 * Caller must hold vsync_mutex.
484 static struct drm_framebuffer
*vop_win_last_pending_fb(struct vop_win
*vop_win
)
486 struct vop_win_state
*last
;
487 struct vop_win_state
*active
= vop_win
->active
;
489 if (list_empty(&vop_win
->pending
))
490 return active
? active
->fb
: NULL
;
492 last
= list_last_entry(&vop_win
->pending
, struct vop_win_state
, head
);
493 return last
? last
->fb
: NULL
;
497 * Caller must hold vsync_mutex.
499 static int vop_win_queue_fb(struct vop_win
*vop_win
,
500 struct drm_framebuffer
*fb
, dma_addr_t yrgb_mst
,
501 struct drm_pending_vblank_event
*event
)
503 struct vop_win_state
*state
;
505 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
510 state
->yrgb_mst
= yrgb_mst
;
511 state
->event
= event
;
513 list_add_tail(&state
->head
, &vop_win
->pending
);
518 static int vop_update_plane_event(struct drm_plane
*plane
,
519 struct drm_crtc
*crtc
,
520 struct drm_framebuffer
*fb
, int crtc_x
,
521 int crtc_y
, unsigned int crtc_w
,
522 unsigned int crtc_h
, uint32_t src_x
,
523 uint32_t src_y
, uint32_t src_w
,
525 struct drm_pending_vblank_event
*event
)
527 struct vop_win
*vop_win
= to_vop_win(plane
);
528 const struct vop_win_data
*win
= vop_win
->data
;
529 struct vop
*vop
= to_vop(crtc
);
530 struct drm_gem_object
*obj
;
531 struct rockchip_gem_object
*rk_obj
;
532 unsigned long offset
;
533 unsigned int actual_w
;
534 unsigned int actual_h
;
535 unsigned int dsp_stx
;
536 unsigned int dsp_sty
;
537 unsigned int y_vir_stride
;
539 enum vop_data_format format
;
544 struct drm_rect dest
= {
547 .x2
= crtc_x
+ crtc_w
,
548 .y2
= crtc_y
+ crtc_h
,
550 struct drm_rect src
= {
551 /* 16.16 fixed point */
557 const struct drm_rect clip
= {
558 .x2
= crtc
->mode
.hdisplay
,
559 .y2
= crtc
->mode
.vdisplay
,
561 bool can_position
= plane
->type
!= DRM_PLANE_TYPE_PRIMARY
;
563 ret
= drm_plane_helper_check_update(plane
, crtc
, fb
,
565 DRM_PLANE_HELPER_NO_SCALING
,
566 DRM_PLANE_HELPER_NO_SCALING
,
567 can_position
, false, &visible
);
574 is_alpha
= is_alpha_support(fb
->pixel_format
);
575 format
= vop_convert_format(fb
->pixel_format
);
579 obj
= rockchip_fb_get_gem_obj(fb
, 0);
581 DRM_ERROR("fail to get rockchip gem object from framebuffer\n");
585 rk_obj
= to_rockchip_obj(obj
);
587 actual_w
= (src
.x2
- src
.x1
) >> 16;
588 actual_h
= (src
.y2
- src
.y1
) >> 16;
589 crtc_x
= max(0, crtc_x
);
590 crtc_y
= max(0, crtc_y
);
592 dsp_stx
= crtc_x
+ crtc
->mode
.htotal
- crtc
->mode
.hsync_start
;
593 dsp_sty
= crtc_y
+ crtc
->mode
.vtotal
- crtc
->mode
.vsync_start
;
595 offset
= (src
.x1
>> 16) * (fb
->bits_per_pixel
>> 3);
596 offset
+= (src
.y1
>> 16) * fb
->pitches
[0];
597 yrgb_mst
= rk_obj
->dma_addr
+ offset
;
599 y_vir_stride
= fb
->pitches
[0] / (fb
->bits_per_pixel
>> 3);
602 * If this plane update changes the plane's framebuffer, (or more
603 * precisely, if this update has a different framebuffer than the last
604 * update), enqueue it so we can track when it completes.
606 * Only when we discover that this update has completed, can we
607 * unreference any previous framebuffers.
609 mutex_lock(&vop
->vsync_mutex
);
610 if (fb
!= vop_win_last_pending_fb(vop_win
)) {
611 ret
= drm_vblank_get(plane
->dev
, vop
->pipe
);
613 DRM_ERROR("failed to get vblank, %d\n", ret
);
614 mutex_unlock(&vop
->vsync_mutex
);
618 drm_framebuffer_reference(fb
);
620 ret
= vop_win_queue_fb(vop_win
, fb
, yrgb_mst
, event
);
622 drm_vblank_put(plane
->dev
, vop
->pipe
);
623 mutex_unlock(&vop
->vsync_mutex
);
627 vop
->vsync_work_pending
= true;
629 mutex_unlock(&vop
->vsync_mutex
);
631 spin_lock(&vop
->reg_lock
);
633 VOP_WIN_SET(vop
, win
, format
, format
);
634 VOP_WIN_SET(vop
, win
, yrgb_vir
, y_vir_stride
);
635 VOP_WIN_SET(vop
, win
, yrgb_mst
, yrgb_mst
);
636 val
= (actual_h
- 1) << 16;
637 val
|= (actual_w
- 1) & 0xffff;
638 VOP_WIN_SET(vop
, win
, act_info
, val
);
639 VOP_WIN_SET(vop
, win
, dsp_info
, val
);
640 val
= (dsp_sty
- 1) << 16;
641 val
|= (dsp_stx
- 1) & 0xffff;
642 VOP_WIN_SET(vop
, win
, dsp_st
, val
);
645 VOP_WIN_SET(vop
, win
, dst_alpha_ctl
,
646 DST_FACTOR_M0(ALPHA_SRC_INVERSE
));
647 val
= SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL
) |
648 SRC_ALPHA_M0(ALPHA_STRAIGHT
) |
649 SRC_BLEND_M0(ALPHA_PER_PIX
) |
650 SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION
) |
651 SRC_FACTOR_M0(ALPHA_ONE
);
652 VOP_WIN_SET(vop
, win
, src_alpha_ctl
, val
);
654 VOP_WIN_SET(vop
, win
, src_alpha_ctl
, SRC_ALPHA_EN(0));
657 VOP_WIN_SET(vop
, win
, enable
, 1);
660 spin_unlock(&vop
->reg_lock
);
665 static int vop_update_plane(struct drm_plane
*plane
, struct drm_crtc
*crtc
,
666 struct drm_framebuffer
*fb
, int crtc_x
, int crtc_y
,
667 unsigned int crtc_w
, unsigned int crtc_h
,
668 uint32_t src_x
, uint32_t src_y
, uint32_t src_w
,
671 return vop_update_plane_event(plane
, crtc
, fb
, crtc_x
, crtc_y
, crtc_w
,
672 crtc_h
, src_x
, src_y
, src_w
, src_h
,
676 static int vop_update_primary_plane(struct drm_crtc
*crtc
,
677 struct drm_pending_vblank_event
*event
)
679 unsigned int crtc_w
, crtc_h
;
681 crtc_w
= crtc
->primary
->fb
->width
- crtc
->x
;
682 crtc_h
= crtc
->primary
->fb
->height
- crtc
->y
;
684 return vop_update_plane_event(crtc
->primary
, crtc
, crtc
->primary
->fb
,
685 0, 0, crtc_w
, crtc_h
, crtc
->x
<< 16,
686 crtc
->y
<< 16, crtc_w
<< 16,
687 crtc_h
<< 16, event
);
690 static int vop_disable_plane(struct drm_plane
*plane
)
692 struct vop_win
*vop_win
= to_vop_win(plane
);
693 const struct vop_win_data
*win
= vop_win
->data
;
700 vop
= to_vop(plane
->crtc
);
702 ret
= drm_vblank_get(plane
->dev
, vop
->pipe
);
704 DRM_ERROR("failed to get vblank, %d\n", ret
);
708 mutex_lock(&vop
->vsync_mutex
);
710 ret
= vop_win_queue_fb(vop_win
, NULL
, 0, NULL
);
712 drm_vblank_put(plane
->dev
, vop
->pipe
);
713 mutex_unlock(&vop
->vsync_mutex
);
717 vop
->vsync_work_pending
= true;
718 mutex_unlock(&vop
->vsync_mutex
);
720 spin_lock(&vop
->reg_lock
);
721 VOP_WIN_SET(vop
, win
, enable
, 0);
723 spin_unlock(&vop
->reg_lock
);
728 static void vop_plane_destroy(struct drm_plane
*plane
)
730 vop_disable_plane(plane
);
731 drm_plane_cleanup(plane
);
734 static const struct drm_plane_funcs vop_plane_funcs
= {
735 .update_plane
= vop_update_plane
,
736 .disable_plane
= vop_disable_plane
,
737 .destroy
= vop_plane_destroy
,
740 int rockchip_drm_crtc_mode_config(struct drm_crtc
*crtc
,
744 struct vop
*vop
= to_vop(crtc
);
746 vop
->connector_type
= connector_type
;
747 vop
->connector_out_mode
= out_mode
;
751 EXPORT_SYMBOL_GPL(rockchip_drm_crtc_mode_config
);
753 static int vop_crtc_enable_vblank(struct drm_crtc
*crtc
)
755 struct vop
*vop
= to_vop(crtc
);
758 if (!vop
->is_enabled
)
761 spin_lock_irqsave(&vop
->irq_lock
, flags
);
763 vop_mask_write(vop
, INTR_CTRL0
, FS_INTR_MASK
, FS_INTR_EN(1));
765 spin_unlock_irqrestore(&vop
->irq_lock
, flags
);
770 static void vop_crtc_disable_vblank(struct drm_crtc
*crtc
)
772 struct vop
*vop
= to_vop(crtc
);
775 if (!vop
->is_enabled
)
778 spin_lock_irqsave(&vop
->irq_lock
, flags
);
779 vop_mask_write(vop
, INTR_CTRL0
, FS_INTR_MASK
, FS_INTR_EN(0));
780 spin_unlock_irqrestore(&vop
->irq_lock
, flags
);
783 static const struct rockchip_crtc_funcs private_crtc_funcs
= {
784 .enable_vblank
= vop_crtc_enable_vblank
,
785 .disable_vblank
= vop_crtc_disable_vblank
,
788 static void vop_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
790 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc
->base
.id
, mode
);
793 case DRM_MODE_DPMS_ON
:
796 case DRM_MODE_DPMS_STANDBY
:
797 case DRM_MODE_DPMS_SUSPEND
:
798 case DRM_MODE_DPMS_OFF
:
802 DRM_DEBUG_KMS("unspecified mode %d\n", mode
);
807 static void vop_crtc_prepare(struct drm_crtc
*crtc
)
809 vop_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
812 static bool vop_crtc_mode_fixup(struct drm_crtc
*crtc
,
813 const struct drm_display_mode
*mode
,
814 struct drm_display_mode
*adjusted_mode
)
816 if (adjusted_mode
->htotal
== 0 || adjusted_mode
->vtotal
== 0)
822 static int vop_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
823 struct drm_framebuffer
*old_fb
)
830 ret
= vop_update_primary_plane(crtc
, NULL
);
832 DRM_ERROR("fail to update plane\n");
839 static int vop_crtc_mode_set(struct drm_crtc
*crtc
,
840 struct drm_display_mode
*mode
,
841 struct drm_display_mode
*adjusted_mode
,
842 int x
, int y
, struct drm_framebuffer
*fb
)
844 struct vop
*vop
= to_vop(crtc
);
845 u16 hsync_len
= adjusted_mode
->hsync_end
- adjusted_mode
->hsync_start
;
846 u16 hdisplay
= adjusted_mode
->hdisplay
;
847 u16 htotal
= adjusted_mode
->htotal
;
848 u16 hact_st
= adjusted_mode
->htotal
- adjusted_mode
->hsync_start
;
849 u16 hact_end
= hact_st
+ hdisplay
;
850 u16 vdisplay
= adjusted_mode
->vdisplay
;
851 u16 vtotal
= adjusted_mode
->vtotal
;
852 u16 vsync_len
= adjusted_mode
->vsync_end
- adjusted_mode
->vsync_start
;
853 u16 vact_st
= adjusted_mode
->vtotal
- adjusted_mode
->vsync_start
;
854 u16 vact_end
= vact_st
+ vdisplay
;
859 * disable dclk to stop frame scan, so that we can safe config mode and
862 clk_disable(vop
->dclk
);
864 switch (vop
->connector_type
) {
865 case DRM_MODE_CONNECTOR_LVDS
:
866 VOP_CTRL_SET(vop
, rgb_en
, 1);
868 case DRM_MODE_CONNECTOR_eDP
:
869 VOP_CTRL_SET(vop
, edp_en
, 1);
871 case DRM_MODE_CONNECTOR_HDMIA
:
872 VOP_CTRL_SET(vop
, hdmi_en
, 1);
875 DRM_ERROR("unsupport connector_type[%d]\n",
876 vop
->connector_type
);
879 VOP_CTRL_SET(vop
, out_mode
, vop
->connector_out_mode
);
882 val
|= (adjusted_mode
->flags
& DRM_MODE_FLAG_NHSYNC
) ? 0 : 1;
883 val
|= (adjusted_mode
->flags
& DRM_MODE_FLAG_NVSYNC
) ? 0 : (1 << 1);
884 VOP_CTRL_SET(vop
, pin_pol
, val
);
886 VOP_CTRL_SET(vop
, htotal_pw
, (htotal
<< 16) | hsync_len
);
889 VOP_CTRL_SET(vop
, hact_st_end
, val
);
890 VOP_CTRL_SET(vop
, hpost_st_end
, val
);
892 VOP_CTRL_SET(vop
, vtotal_pw
, (vtotal
<< 16) | vsync_len
);
895 VOP_CTRL_SET(vop
, vact_st_end
, val
);
896 VOP_CTRL_SET(vop
, vpost_st_end
, val
);
898 ret
= vop_crtc_mode_set_base(crtc
, x
, y
, fb
);
903 * reset dclk, take all mode config affect, so the clk would run in
906 reset_control_assert(vop
->dclk_rst
);
907 usleep_range(10, 20);
908 reset_control_deassert(vop
->dclk_rst
);
910 clk_set_rate(vop
->dclk
, adjusted_mode
->clock
* 1000);
911 ret
= clk_enable(vop
->dclk
);
913 dev_err(vop
->dev
, "failed to enable dclk - %d\n", ret
);
920 static void vop_crtc_commit(struct drm_crtc
*crtc
)
924 static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs
= {
925 .dpms
= vop_crtc_dpms
,
926 .prepare
= vop_crtc_prepare
,
927 .mode_fixup
= vop_crtc_mode_fixup
,
928 .mode_set
= vop_crtc_mode_set
,
929 .mode_set_base
= vop_crtc_mode_set_base
,
930 .commit
= vop_crtc_commit
,
933 static int vop_crtc_page_flip(struct drm_crtc
*crtc
,
934 struct drm_framebuffer
*fb
,
935 struct drm_pending_vblank_event
*event
,
936 uint32_t page_flip_flags
)
938 struct vop
*vop
= to_vop(crtc
);
939 struct drm_framebuffer
*old_fb
= crtc
->primary
->fb
;
942 /* when the page flip is requested, crtc should be on */
943 if (!vop
->is_enabled
) {
944 DRM_DEBUG("page flip request rejected because crtc is off.\n");
948 crtc
->primary
->fb
= fb
;
950 ret
= vop_update_primary_plane(crtc
, event
);
952 crtc
->primary
->fb
= old_fb
;
957 static void vop_win_state_complete(struct vop_win
*vop_win
,
958 struct vop_win_state
*state
)
960 struct vop
*vop
= vop_win
->vop
;
961 struct drm_crtc
*crtc
= &vop
->crtc
;
962 struct drm_device
*drm
= crtc
->dev
;
966 spin_lock_irqsave(&drm
->event_lock
, flags
);
967 drm_send_vblank_event(drm
, -1, state
->event
);
968 spin_unlock_irqrestore(&drm
->event_lock
, flags
);
971 list_del(&state
->head
);
972 drm_vblank_put(crtc
->dev
, vop
->pipe
);
975 static void vop_crtc_destroy(struct drm_crtc
*crtc
)
977 drm_crtc_cleanup(crtc
);
980 static const struct drm_crtc_funcs vop_crtc_funcs
= {
981 .set_config
= drm_crtc_helper_set_config
,
982 .page_flip
= vop_crtc_page_flip
,
983 .destroy
= vop_crtc_destroy
,
986 static bool vop_win_state_is_active(struct vop_win
*vop_win
,
987 struct vop_win_state
*state
)
994 /* check yrgb_mst to tell if pending_fb is now front */
995 yrgb_mst
= VOP_WIN_GET_YRGBADDR(vop_win
->vop
, vop_win
->data
);
997 active
= (yrgb_mst
== state
->yrgb_mst
);
1001 /* if enable bit is clear, plane is now disabled */
1002 enabled
= VOP_WIN_GET(vop_win
->vop
, vop_win
->data
, enable
);
1004 active
= (enabled
== 0);
1010 static void vop_win_state_destroy(struct vop_win_state
*state
)
1012 struct drm_framebuffer
*fb
= state
->fb
;
1015 drm_framebuffer_unreference(fb
);
1020 static void vop_win_update_state(struct vop_win
*vop_win
)
1022 struct vop_win_state
*state
, *n
, *new_active
= NULL
;
1024 /* Check if any pending states are now active */
1025 list_for_each_entry(state
, &vop_win
->pending
, head
)
1026 if (vop_win_state_is_active(vop_win
, state
)) {
1035 * Destroy any 'skipped' pending states - states that were queued
1036 * before the newly active state.
1038 list_for_each_entry_safe(state
, n
, &vop_win
->pending
, head
) {
1039 if (state
== new_active
)
1041 vop_win_state_complete(vop_win
, state
);
1042 vop_win_state_destroy(state
);
1045 vop_win_state_complete(vop_win
, new_active
);
1047 if (vop_win
->active
)
1048 vop_win_state_destroy(vop_win
->active
);
1049 vop_win
->active
= new_active
;
1052 static bool vop_win_has_pending_state(struct vop_win
*vop_win
)
1054 return !list_empty(&vop_win
->pending
);
1057 static irqreturn_t
vop_isr_thread(int irq
, void *data
)
1059 struct vop
*vop
= data
;
1060 const struct vop_data
*vop_data
= vop
->data
;
1063 mutex_lock(&vop
->vsync_mutex
);
1065 if (!vop
->vsync_work_pending
)
1068 vop
->vsync_work_pending
= false;
1070 for (i
= 0; i
< vop_data
->win_size
; i
++) {
1071 struct vop_win
*vop_win
= &vop
->win
[i
];
1073 vop_win_update_state(vop_win
);
1074 if (vop_win_has_pending_state(vop_win
))
1075 vop
->vsync_work_pending
= true;
1079 mutex_unlock(&vop
->vsync_mutex
);
1084 static irqreturn_t
vop_isr(int irq
, void *data
)
1086 struct vop
*vop
= data
;
1087 uint32_t intr0_reg
, active_irqs
;
1088 unsigned long flags
;
1091 * INTR_CTRL0 register has interrupt status, enable and clear bits, we
1092 * must hold irq_lock to avoid a race with enable/disable_vblank().
1094 spin_lock_irqsave(&vop
->irq_lock
, flags
);
1095 intr0_reg
= vop_readl(vop
, INTR_CTRL0
);
1096 active_irqs
= intr0_reg
& INTR_MASK
;
1097 /* Clear all active interrupt sources */
1099 vop_writel(vop
, INTR_CTRL0
,
1100 intr0_reg
| (active_irqs
<< INTR_CLR_SHIFT
));
1101 spin_unlock_irqrestore(&vop
->irq_lock
, flags
);
1103 /* This is expected for vop iommu irqs, since the irq is shared */
1107 /* Only Frame Start Interrupt is enabled; other irqs are spurious. */
1108 if (!(active_irqs
& FS_INTR
)) {
1109 DRM_ERROR("Unknown VOP IRQs: %#02x\n", active_irqs
);
1113 drm_handle_vblank(vop
->drm_dev
, vop
->pipe
);
1115 return (vop
->vsync_work_pending
) ? IRQ_WAKE_THREAD
: IRQ_HANDLED
;
1118 static int vop_create_crtc(struct vop
*vop
)
1120 const struct vop_data
*vop_data
= vop
->data
;
1121 struct device
*dev
= vop
->dev
;
1122 struct drm_device
*drm_dev
= vop
->drm_dev
;
1123 struct drm_plane
*primary
= NULL
, *cursor
= NULL
, *plane
;
1124 struct drm_crtc
*crtc
= &vop
->crtc
;
1125 struct device_node
*port
;
1130 * Create drm_plane for primary and cursor planes first, since we need
1131 * to pass them to drm_crtc_init_with_planes, which sets the
1132 * "possible_crtcs" to the newly initialized crtc.
1134 for (i
= 0; i
< vop_data
->win_size
; i
++) {
1135 struct vop_win
*vop_win
= &vop
->win
[i
];
1136 const struct vop_win_data
*win_data
= vop_win
->data
;
1138 if (win_data
->type
!= DRM_PLANE_TYPE_PRIMARY
&&
1139 win_data
->type
!= DRM_PLANE_TYPE_CURSOR
)
1142 ret
= drm_universal_plane_init(vop
->drm_dev
, &vop_win
->base
,
1143 0, &vop_plane_funcs
,
1144 win_data
->phy
->data_formats
,
1145 win_data
->phy
->nformats
,
1148 DRM_ERROR("failed to initialize plane\n");
1149 goto err_cleanup_planes
;
1152 plane
= &vop_win
->base
;
1153 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
1155 else if (plane
->type
== DRM_PLANE_TYPE_CURSOR
)
1159 ret
= drm_crtc_init_with_planes(drm_dev
, crtc
, primary
, cursor
,
1164 drm_crtc_helper_add(crtc
, &vop_crtc_helper_funcs
);
1167 * Create drm_planes for overlay windows with possible_crtcs restricted
1168 * to the newly created crtc.
1170 for (i
= 0; i
< vop_data
->win_size
; i
++) {
1171 struct vop_win
*vop_win
= &vop
->win
[i
];
1172 const struct vop_win_data
*win_data
= vop_win
->data
;
1173 unsigned long possible_crtcs
= 1 << drm_crtc_index(crtc
);
1175 if (win_data
->type
!= DRM_PLANE_TYPE_OVERLAY
)
1178 ret
= drm_universal_plane_init(vop
->drm_dev
, &vop_win
->base
,
1181 win_data
->phy
->data_formats
,
1182 win_data
->phy
->nformats
,
1185 DRM_ERROR("failed to initialize overlay plane\n");
1186 goto err_cleanup_crtc
;
1190 port
= of_get_child_by_name(dev
->of_node
, "port");
1192 DRM_ERROR("no port node found in %s\n",
1193 dev
->of_node
->full_name
);
1194 goto err_cleanup_crtc
;
1198 vop
->pipe
= drm_crtc_index(crtc
);
1199 rockchip_register_crtc_funcs(drm_dev
, &private_crtc_funcs
, vop
->pipe
);
1204 drm_crtc_cleanup(crtc
);
1206 list_for_each_entry(plane
, &drm_dev
->mode_config
.plane_list
, head
)
1207 drm_plane_cleanup(plane
);
1211 static void vop_destroy_crtc(struct vop
*vop
)
1213 struct drm_crtc
*crtc
= &vop
->crtc
;
1215 rockchip_unregister_crtc_funcs(vop
->drm_dev
, vop
->pipe
);
1216 of_node_put(crtc
->port
);
1217 drm_crtc_cleanup(crtc
);
1220 static int vop_initial(struct vop
*vop
)
1222 const struct vop_data
*vop_data
= vop
->data
;
1223 const struct vop_reg_data
*init_table
= vop_data
->init_table
;
1224 struct reset_control
*ahb_rst
;
1227 vop
->hclk
= devm_clk_get(vop
->dev
, "hclk_vop");
1228 if (IS_ERR(vop
->hclk
)) {
1229 dev_err(vop
->dev
, "failed to get hclk source\n");
1230 return PTR_ERR(vop
->hclk
);
1232 vop
->aclk
= devm_clk_get(vop
->dev
, "aclk_vop");
1233 if (IS_ERR(vop
->aclk
)) {
1234 dev_err(vop
->dev
, "failed to get aclk source\n");
1235 return PTR_ERR(vop
->aclk
);
1237 vop
->dclk
= devm_clk_get(vop
->dev
, "dclk_vop");
1238 if (IS_ERR(vop
->dclk
)) {
1239 dev_err(vop
->dev
, "failed to get dclk source\n");
1240 return PTR_ERR(vop
->dclk
);
1243 ret
= clk_prepare(vop
->hclk
);
1245 dev_err(vop
->dev
, "failed to prepare hclk\n");
1249 ret
= clk_prepare(vop
->dclk
);
1251 dev_err(vop
->dev
, "failed to prepare dclk\n");
1252 goto err_unprepare_hclk
;
1255 ret
= clk_prepare(vop
->aclk
);
1257 dev_err(vop
->dev
, "failed to prepare aclk\n");
1258 goto err_unprepare_dclk
;
1262 * enable hclk, so that we can config vop register.
1264 ret
= clk_enable(vop
->hclk
);
1266 dev_err(vop
->dev
, "failed to prepare aclk\n");
1267 goto err_unprepare_aclk
;
1270 * do hclk_reset, reset all vop registers.
1272 ahb_rst
= devm_reset_control_get(vop
->dev
, "ahb");
1273 if (IS_ERR(ahb_rst
)) {
1274 dev_err(vop
->dev
, "failed to get ahb reset\n");
1275 ret
= PTR_ERR(ahb_rst
);
1276 goto err_disable_hclk
;
1278 reset_control_assert(ahb_rst
);
1279 usleep_range(10, 20);
1280 reset_control_deassert(ahb_rst
);
1282 memcpy(vop
->regsbak
, vop
->regs
, vop
->len
);
1284 for (i
= 0; i
< vop_data
->table_size
; i
++)
1285 vop_writel(vop
, init_table
[i
].offset
, init_table
[i
].value
);
1287 for (i
= 0; i
< vop_data
->win_size
; i
++) {
1288 const struct vop_win_data
*win
= &vop_data
->win
[i
];
1290 VOP_WIN_SET(vop
, win
, enable
, 0);
1296 * do dclk_reset, let all config take affect.
1298 vop
->dclk_rst
= devm_reset_control_get(vop
->dev
, "dclk");
1299 if (IS_ERR(vop
->dclk_rst
)) {
1300 dev_err(vop
->dev
, "failed to get dclk reset\n");
1301 ret
= PTR_ERR(vop
->dclk_rst
);
1302 goto err_unprepare_aclk
;
1304 reset_control_assert(vop
->dclk_rst
);
1305 usleep_range(10, 20);
1306 reset_control_deassert(vop
->dclk_rst
);
1308 clk_disable(vop
->hclk
);
1310 vop
->is_enabled
= false;
1315 clk_disable(vop
->hclk
);
1317 clk_unprepare(vop
->aclk
);
1319 clk_unprepare(vop
->dclk
);
1321 clk_unprepare(vop
->hclk
);
1326 * Initialize the vop->win array elements.
1328 static void vop_win_init(struct vop
*vop
)
1330 const struct vop_data
*vop_data
= vop
->data
;
1333 for (i
= 0; i
< vop_data
->win_size
; i
++) {
1334 struct vop_win
*vop_win
= &vop
->win
[i
];
1335 const struct vop_win_data
*win_data
= &vop_data
->win
[i
];
1337 vop_win
->data
= win_data
;
1339 INIT_LIST_HEAD(&vop_win
->pending
);
1343 static int vop_bind(struct device
*dev
, struct device
*master
, void *data
)
1345 struct platform_device
*pdev
= to_platform_device(dev
);
1346 const struct of_device_id
*of_id
;
1347 const struct vop_data
*vop_data
;
1348 struct drm_device
*drm_dev
= data
;
1350 struct resource
*res
;
1354 of_id
= of_match_device(vop_driver_dt_match
, dev
);
1355 vop_data
= of_id
->data
;
1359 /* Allocate vop struct and its vop_win array */
1360 alloc_size
= sizeof(*vop
) + sizeof(*vop
->win
) * vop_data
->win_size
;
1361 vop
= devm_kzalloc(dev
, alloc_size
, GFP_KERNEL
);
1366 vop
->data
= vop_data
;
1367 vop
->drm_dev
= drm_dev
;
1368 dev_set_drvdata(dev
, vop
);
1372 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1373 vop
->len
= resource_size(res
);
1374 vop
->regs
= devm_ioremap_resource(dev
, res
);
1375 if (IS_ERR(vop
->regs
))
1376 return PTR_ERR(vop
->regs
);
1378 vop
->regsbak
= devm_kzalloc(dev
, vop
->len
, GFP_KERNEL
);
1382 ret
= vop_initial(vop
);
1384 dev_err(&pdev
->dev
, "cannot initial vop dev - err %d\n", ret
);
1388 vop
->irq
= platform_get_irq(pdev
, 0);
1390 dev_err(dev
, "cannot find irq for vop\n");
1394 spin_lock_init(&vop
->reg_lock
);
1395 spin_lock_init(&vop
->irq_lock
);
1397 mutex_init(&vop
->vsync_mutex
);
1399 ret
= devm_request_threaded_irq(dev
, vop
->irq
, vop_isr
, vop_isr_thread
,
1400 IRQF_SHARED
, dev_name(dev
), vop
);
1404 /* IRQ is initially disabled; it gets enabled in power_on */
1405 disable_irq(vop
->irq
);
1407 ret
= vop_create_crtc(vop
);
1411 pm_runtime_enable(&pdev
->dev
);
1415 static void vop_unbind(struct device
*dev
, struct device
*master
, void *data
)
1417 struct vop
*vop
= dev_get_drvdata(dev
);
1419 pm_runtime_disable(dev
);
1420 vop_destroy_crtc(vop
);
1423 static const struct component_ops vop_component_ops
= {
1425 .unbind
= vop_unbind
,
1428 static int vop_probe(struct platform_device
*pdev
)
1430 struct device
*dev
= &pdev
->dev
;
1432 if (!dev
->of_node
) {
1433 dev_err(dev
, "can't find vop devices\n");
1437 return component_add(dev
, &vop_component_ops
);
1440 static int vop_remove(struct platform_device
*pdev
)
1442 component_del(&pdev
->dev
, &vop_component_ops
);
1447 struct platform_driver vop_platform_driver
= {
1449 .remove
= vop_remove
,
1451 .name
= "rockchip-vop",
1452 .owner
= THIS_MODULE
,
1453 .of_match_table
= of_match_ptr(vop_driver_dt_match
),
1457 module_platform_driver(vop_platform_driver
);
1459 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1460 MODULE_DESCRIPTION("ROCKCHIP VOP Driver");
1461 MODULE_LICENSE("GPL v2");