1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: CK Hu <ck.hu@mediatek.com>
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_fourcc.h>
10 #include <drm/drm_plane_helper.h>
11 #include <drm/drm_gem_framebuffer_helper.h>
13 #include "mtk_drm_crtc.h"
14 #include "mtk_drm_ddp_comp.h"
15 #include "mtk_drm_drv.h"
16 #include "mtk_drm_fb.h"
17 #include "mtk_drm_gem.h"
18 #include "mtk_drm_plane.h"
20 static const u32 formats[] = {
28 static void mtk_plane_reset(struct drm_plane *plane)
30 struct mtk_plane_state *state;
33 __drm_atomic_helper_plane_destroy_state(plane->state);
35 state = to_mtk_plane_state(plane->state);
36 memset(state, 0, sizeof(*state));
38 state = kzalloc(sizeof(*state), GFP_KERNEL);
41 plane->state = &state->base;
44 state->base.plane = plane;
45 state->pending.format = DRM_FORMAT_RGB565;
48 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
50 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
51 struct mtk_plane_state *state;
53 state = kzalloc(sizeof(*state), GFP_KERNEL);
57 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
59 WARN_ON(state->base.plane != plane);
61 state->pending = old_state->pending;
66 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
67 struct drm_plane_state *state)
69 __drm_atomic_helper_plane_destroy_state(state);
70 kfree(to_mtk_plane_state(state));
73 static const struct drm_plane_funcs mtk_plane_funcs = {
74 .update_plane = drm_atomic_helper_update_plane,
75 .disable_plane = drm_atomic_helper_disable_plane,
76 .destroy = drm_plane_cleanup,
77 .reset = mtk_plane_reset,
78 .atomic_duplicate_state = mtk_plane_duplicate_state,
79 .atomic_destroy_state = mtk_drm_plane_destroy_state,
82 static int mtk_plane_atomic_check(struct drm_plane *plane,
83 struct drm_plane_state *state)
85 struct drm_framebuffer *fb = state->fb;
86 struct drm_crtc_state *crtc_state;
94 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
95 if (IS_ERR(crtc_state))
96 return PTR_ERR(crtc_state);
98 return drm_atomic_helper_check_plane_state(state, crtc_state,
99 DRM_PLANE_HELPER_NO_SCALING,
100 DRM_PLANE_HELPER_NO_SCALING,
104 static void mtk_plane_atomic_update(struct drm_plane *plane,
105 struct drm_plane_state *old_state)
107 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
108 struct drm_crtc *crtc = plane->state->crtc;
109 struct drm_framebuffer *fb = plane->state->fb;
110 struct drm_gem_object *gem;
111 struct mtk_drm_gem_obj *mtk_gem;
112 unsigned int pitch, format;
115 if (!crtc || WARN_ON(!fb))
119 mtk_gem = to_mtk_gem_obj(gem);
120 addr = mtk_gem->dma_addr;
121 pitch = fb->pitches[0];
122 format = fb->format->format;
124 addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
125 addr += (plane->state->src.y1 >> 16) * pitch;
127 state->pending.enable = true;
128 state->pending.pitch = pitch;
129 state->pending.format = format;
130 state->pending.addr = addr;
131 state->pending.x = plane->state->dst.x1;
132 state->pending.y = plane->state->dst.y1;
133 state->pending.width = drm_rect_width(&plane->state->dst);
134 state->pending.height = drm_rect_height(&plane->state->dst);
135 wmb(); /* Make sure the above parameters are set before update */
136 state->pending.dirty = true;
139 static void mtk_plane_atomic_disable(struct drm_plane *plane,
140 struct drm_plane_state *old_state)
142 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
144 state->pending.enable = false;
145 wmb(); /* Make sure the above parameter is set before update */
146 state->pending.dirty = true;
149 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
150 .prepare_fb = drm_gem_fb_prepare_fb,
151 .atomic_check = mtk_plane_atomic_check,
152 .atomic_update = mtk_plane_atomic_update,
153 .atomic_disable = mtk_plane_atomic_disable,
156 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
157 unsigned long possible_crtcs, enum drm_plane_type type)
161 err = drm_universal_plane_init(dev, plane, possible_crtcs,
162 &mtk_plane_funcs, formats,
163 ARRAY_SIZE(formats), NULL, type, NULL);
165 DRM_ERROR("failed to initialize plane\n");
169 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);