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_atomic_uapi.h>
10 #include <drm/drm_fourcc.h>
11 #include <drm/drm_gem_atomic_helper.h>
12 #include <drm/drm_plane_helper.h>
14 #include "mtk_drm_crtc.h"
15 #include "mtk_drm_ddp_comp.h"
16 #include "mtk_drm_drv.h"
17 #include "mtk_drm_gem.h"
18 #include "mtk_drm_plane.h"
20 static const u32 formats[] = {
34 static void mtk_plane_reset(struct drm_plane *plane)
36 struct mtk_plane_state *state;
39 __drm_atomic_helper_plane_destroy_state(plane->state);
41 state = to_mtk_plane_state(plane->state);
42 memset(state, 0, sizeof(*state));
44 state = kzalloc(sizeof(*state), GFP_KERNEL);
47 plane->state = &state->base;
50 state->base.plane = plane;
51 state->pending.format = DRM_FORMAT_RGB565;
54 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
56 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
57 struct mtk_plane_state *state;
59 state = kzalloc(sizeof(*state), GFP_KERNEL);
63 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
65 WARN_ON(state->base.plane != plane);
67 state->pending = old_state->pending;
72 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
73 struct drm_plane_state *state)
75 __drm_atomic_helper_plane_destroy_state(state);
76 kfree(to_mtk_plane_state(state));
79 static int mtk_plane_atomic_async_check(struct drm_plane *plane,
80 struct drm_atomic_state *state)
82 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
84 struct drm_crtc_state *crtc_state;
87 if (plane != new_plane_state->crtc->cursor)
93 if (!plane->state->fb)
96 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
97 to_mtk_plane_state(new_plane_state));
102 crtc_state = drm_atomic_get_existing_crtc_state(state,
103 new_plane_state->crtc);
104 else /* Special case for asynchronous cursor updates. */
105 crtc_state = new_plane_state->crtc->state;
107 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
108 DRM_PLANE_HELPER_NO_SCALING,
109 DRM_PLANE_HELPER_NO_SCALING,
113 static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
114 struct mtk_plane_state *mtk_plane_state)
116 struct drm_framebuffer *fb = new_state->fb;
117 struct drm_gem_object *gem;
118 struct mtk_drm_gem_obj *mtk_gem;
119 unsigned int pitch, format;
123 mtk_gem = to_mtk_gem_obj(gem);
124 addr = mtk_gem->dma_addr;
125 pitch = fb->pitches[0];
126 format = fb->format->format;
128 addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
129 addr += (new_state->src.y1 >> 16) * pitch;
131 mtk_plane_state->pending.enable = true;
132 mtk_plane_state->pending.pitch = pitch;
133 mtk_plane_state->pending.format = format;
134 mtk_plane_state->pending.addr = addr;
135 mtk_plane_state->pending.x = new_state->dst.x1;
136 mtk_plane_state->pending.y = new_state->dst.y1;
137 mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
138 mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
139 mtk_plane_state->pending.rotation = new_state->rotation;
142 static void mtk_plane_atomic_async_update(struct drm_plane *plane,
143 struct drm_atomic_state *state)
145 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
147 struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
149 plane->state->crtc_x = new_state->crtc_x;
150 plane->state->crtc_y = new_state->crtc_y;
151 plane->state->crtc_h = new_state->crtc_h;
152 plane->state->crtc_w = new_state->crtc_w;
153 plane->state->src_x = new_state->src_x;
154 plane->state->src_y = new_state->src_y;
155 plane->state->src_h = new_state->src_h;
156 plane->state->src_w = new_state->src_w;
157 swap(plane->state->fb, new_state->fb);
159 mtk_plane_update_new_state(new_state, new_plane_state);
160 wmb(); /* Make sure the above parameters are set before update */
161 new_plane_state->pending.async_dirty = true;
162 mtk_drm_crtc_async_update(new_state->crtc, plane, state);
165 static const struct drm_plane_funcs mtk_plane_funcs = {
166 .update_plane = drm_atomic_helper_update_plane,
167 .disable_plane = drm_atomic_helper_disable_plane,
168 .destroy = drm_plane_cleanup,
169 .reset = mtk_plane_reset,
170 .atomic_duplicate_state = mtk_plane_duplicate_state,
171 .atomic_destroy_state = mtk_drm_plane_destroy_state,
174 static int mtk_plane_atomic_check(struct drm_plane *plane,
175 struct drm_atomic_state *state)
177 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
179 struct drm_framebuffer *fb = new_plane_state->fb;
180 struct drm_crtc_state *crtc_state;
186 if (WARN_ON(!new_plane_state->crtc))
189 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
190 to_mtk_plane_state(new_plane_state));
194 crtc_state = drm_atomic_get_crtc_state(state,
195 new_plane_state->crtc);
196 if (IS_ERR(crtc_state))
197 return PTR_ERR(crtc_state);
199 return drm_atomic_helper_check_plane_state(new_plane_state,
201 DRM_PLANE_HELPER_NO_SCALING,
202 DRM_PLANE_HELPER_NO_SCALING,
206 static void mtk_plane_atomic_disable(struct drm_plane *plane,
207 struct drm_atomic_state *state)
209 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
211 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
212 mtk_plane_state->pending.enable = false;
213 wmb(); /* Make sure the above parameter is set before update */
214 mtk_plane_state->pending.dirty = true;
217 static void mtk_plane_atomic_update(struct drm_plane *plane,
218 struct drm_atomic_state *state)
220 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
222 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
224 if (!new_state->crtc || WARN_ON(!new_state->fb))
227 if (!new_state->visible) {
228 mtk_plane_atomic_disable(plane, state);
232 mtk_plane_update_new_state(new_state, mtk_plane_state);
233 wmb(); /* Make sure the above parameters are set before update */
234 mtk_plane_state->pending.dirty = true;
237 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
238 .atomic_check = mtk_plane_atomic_check,
239 .atomic_update = mtk_plane_atomic_update,
240 .atomic_disable = mtk_plane_atomic_disable,
241 .atomic_async_update = mtk_plane_atomic_async_update,
242 .atomic_async_check = mtk_plane_atomic_async_check,
245 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
246 unsigned long possible_crtcs, enum drm_plane_type type,
247 unsigned int supported_rotations)
251 err = drm_universal_plane_init(dev, plane, possible_crtcs,
252 &mtk_plane_funcs, formats,
253 ARRAY_SIZE(formats), NULL, type, NULL);
255 DRM_ERROR("failed to initialize plane\n");
259 if (supported_rotations & ~DRM_MODE_ROTATE_0) {
260 err = drm_plane_create_rotation_property(plane,
262 supported_rotations);
264 DRM_INFO("Create rotation property failed\n");
267 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);