1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
7 #include <drm/drm_atomic.h>
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_atomic_uapi.h>
10 #include <drm/drm_blend.h>
11 #include <drm/drm_fourcc.h>
12 #include <drm/drm_framebuffer.h>
13 #include <drm/drm_gem_atomic_helper.h>
14 #include <drm/drm_plane_helper.h>
16 #include "mtk_drm_crtc.h"
17 #include "mtk_drm_ddp_comp.h"
18 #include "mtk_drm_drv.h"
19 #include "mtk_drm_gem.h"
20 #include "mtk_drm_plane.h"
22 static const u32 formats[] = {
36 static void mtk_plane_reset(struct drm_plane *plane)
38 struct mtk_plane_state *state;
41 __drm_atomic_helper_plane_destroy_state(plane->state);
43 state = to_mtk_plane_state(plane->state);
44 memset(state, 0, sizeof(*state));
46 state = kzalloc(sizeof(*state), GFP_KERNEL);
51 __drm_atomic_helper_plane_reset(plane, &state->base);
53 state->base.plane = plane;
54 state->pending.format = DRM_FORMAT_RGB565;
57 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
59 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
60 struct mtk_plane_state *state;
62 state = kmalloc(sizeof(*state), GFP_KERNEL);
66 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
68 WARN_ON(state->base.plane != plane);
70 state->pending = old_state->pending;
75 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
76 struct drm_plane_state *state)
78 __drm_atomic_helper_plane_destroy_state(state);
79 kfree(to_mtk_plane_state(state));
82 static int mtk_plane_atomic_async_check(struct drm_plane *plane,
83 struct drm_atomic_state *state)
85 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
87 struct drm_crtc_state *crtc_state;
90 if (plane != new_plane_state->crtc->cursor)
96 if (!plane->state->fb)
99 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
100 to_mtk_plane_state(new_plane_state));
105 crtc_state = drm_atomic_get_existing_crtc_state(state,
106 new_plane_state->crtc);
107 else /* Special case for asynchronous cursor updates. */
108 crtc_state = new_plane_state->crtc->state;
110 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
111 DRM_PLANE_HELPER_NO_SCALING,
112 DRM_PLANE_HELPER_NO_SCALING,
116 static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
117 struct mtk_plane_state *mtk_plane_state)
119 struct drm_framebuffer *fb = new_state->fb;
120 struct drm_gem_object *gem;
121 struct mtk_drm_gem_obj *mtk_gem;
122 unsigned int pitch, format;
126 mtk_gem = to_mtk_gem_obj(gem);
127 addr = mtk_gem->dma_addr;
128 pitch = fb->pitches[0];
129 format = fb->format->format;
131 addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
132 addr += (new_state->src.y1 >> 16) * pitch;
134 mtk_plane_state->pending.enable = true;
135 mtk_plane_state->pending.pitch = pitch;
136 mtk_plane_state->pending.format = format;
137 mtk_plane_state->pending.addr = addr;
138 mtk_plane_state->pending.x = new_state->dst.x1;
139 mtk_plane_state->pending.y = new_state->dst.y1;
140 mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
141 mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
142 mtk_plane_state->pending.rotation = new_state->rotation;
143 mtk_plane_state->pending.color_encoding = new_state->color_encoding;
146 static void mtk_plane_atomic_async_update(struct drm_plane *plane,
147 struct drm_atomic_state *state)
149 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
151 struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
153 plane->state->crtc_x = new_state->crtc_x;
154 plane->state->crtc_y = new_state->crtc_y;
155 plane->state->crtc_h = new_state->crtc_h;
156 plane->state->crtc_w = new_state->crtc_w;
157 plane->state->src_x = new_state->src_x;
158 plane->state->src_y = new_state->src_y;
159 plane->state->src_h = new_state->src_h;
160 plane->state->src_w = new_state->src_w;
161 swap(plane->state->fb, new_state->fb);
163 mtk_plane_update_new_state(new_state, new_plane_state);
164 wmb(); /* Make sure the above parameters are set before update */
165 new_plane_state->pending.async_dirty = true;
166 mtk_drm_crtc_async_update(new_state->crtc, plane, state);
169 static const struct drm_plane_funcs mtk_plane_funcs = {
170 .update_plane = drm_atomic_helper_update_plane,
171 .disable_plane = drm_atomic_helper_disable_plane,
172 .destroy = drm_plane_cleanup,
173 .reset = mtk_plane_reset,
174 .atomic_duplicate_state = mtk_plane_duplicate_state,
175 .atomic_destroy_state = mtk_drm_plane_destroy_state,
178 static int mtk_plane_atomic_check(struct drm_plane *plane,
179 struct drm_atomic_state *state)
181 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
183 struct drm_framebuffer *fb = new_plane_state->fb;
184 struct drm_crtc_state *crtc_state;
190 if (WARN_ON(!new_plane_state->crtc))
193 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
194 to_mtk_plane_state(new_plane_state));
198 crtc_state = drm_atomic_get_crtc_state(state,
199 new_plane_state->crtc);
200 if (IS_ERR(crtc_state))
201 return PTR_ERR(crtc_state);
203 return drm_atomic_helper_check_plane_state(new_plane_state,
205 DRM_PLANE_HELPER_NO_SCALING,
206 DRM_PLANE_HELPER_NO_SCALING,
210 static void mtk_plane_atomic_disable(struct drm_plane *plane,
211 struct drm_atomic_state *state)
213 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
215 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
216 mtk_plane_state->pending.enable = false;
217 wmb(); /* Make sure the above parameter is set before update */
218 mtk_plane_state->pending.dirty = true;
221 static void mtk_plane_atomic_update(struct drm_plane *plane,
222 struct drm_atomic_state *state)
224 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
226 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
228 if (!new_state->crtc || WARN_ON(!new_state->fb))
231 if (!new_state->visible) {
232 mtk_plane_atomic_disable(plane, state);
236 mtk_plane_update_new_state(new_state, mtk_plane_state);
237 wmb(); /* Make sure the above parameters are set before update */
238 mtk_plane_state->pending.dirty = true;
241 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
242 .atomic_check = mtk_plane_atomic_check,
243 .atomic_update = mtk_plane_atomic_update,
244 .atomic_disable = mtk_plane_atomic_disable,
245 .atomic_async_update = mtk_plane_atomic_async_update,
246 .atomic_async_check = mtk_plane_atomic_async_check,
249 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
250 unsigned long possible_crtcs, enum drm_plane_type type,
251 unsigned int supported_rotations)
255 err = drm_universal_plane_init(dev, plane, possible_crtcs,
256 &mtk_plane_funcs, formats,
257 ARRAY_SIZE(formats), NULL, type, NULL);
259 DRM_ERROR("failed to initialize plane\n");
263 if (supported_rotations & ~DRM_MODE_ROTATE_0) {
264 err = drm_plane_create_rotation_property(plane,
266 supported_rotations);
268 DRM_INFO("Create rotation property failed\n");
271 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);