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_gem.h"
17 #include "mtk_drm_plane.h"
19 static const u32 formats[] = {
33 static void mtk_plane_reset(struct drm_plane *plane)
35 struct mtk_plane_state *state;
38 __drm_atomic_helper_plane_destroy_state(plane->state);
40 state = to_mtk_plane_state(plane->state);
41 memset(state, 0, sizeof(*state));
43 state = kzalloc(sizeof(*state), GFP_KERNEL);
46 plane->state = &state->base;
49 state->base.plane = plane;
50 state->pending.format = DRM_FORMAT_RGB565;
53 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
55 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
56 struct mtk_plane_state *state;
58 state = kzalloc(sizeof(*state), GFP_KERNEL);
62 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
64 WARN_ON(state->base.plane != plane);
66 state->pending = old_state->pending;
71 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
72 struct drm_plane_state *state)
74 __drm_atomic_helper_plane_destroy_state(state);
75 kfree(to_mtk_plane_state(state));
78 static const struct drm_plane_funcs mtk_plane_funcs = {
79 .update_plane = drm_atomic_helper_update_plane,
80 .disable_plane = drm_atomic_helper_disable_plane,
81 .destroy = drm_plane_cleanup,
82 .reset = mtk_plane_reset,
83 .atomic_duplicate_state = mtk_plane_duplicate_state,
84 .atomic_destroy_state = mtk_drm_plane_destroy_state,
87 static int mtk_plane_atomic_check(struct drm_plane *plane,
88 struct drm_plane_state *state)
90 struct drm_framebuffer *fb = state->fb;
91 struct drm_crtc_state *crtc_state;
100 ret = mtk_drm_crtc_plane_check(state->crtc, plane,
101 to_mtk_plane_state(state));
105 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
106 if (IS_ERR(crtc_state))
107 return PTR_ERR(crtc_state);
109 return drm_atomic_helper_check_plane_state(state, crtc_state,
110 DRM_PLANE_HELPER_NO_SCALING,
111 DRM_PLANE_HELPER_NO_SCALING,
115 static void mtk_plane_atomic_update(struct drm_plane *plane,
116 struct drm_plane_state *old_state)
118 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
119 struct drm_crtc *crtc = plane->state->crtc;
120 struct drm_framebuffer *fb = plane->state->fb;
121 struct drm_gem_object *gem;
122 struct mtk_drm_gem_obj *mtk_gem;
123 unsigned int pitch, format;
126 if (!crtc || WARN_ON(!fb))
130 mtk_gem = to_mtk_gem_obj(gem);
131 addr = mtk_gem->dma_addr;
132 pitch = fb->pitches[0];
133 format = fb->format->format;
135 addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
136 addr += (plane->state->src.y1 >> 16) * pitch;
138 state->pending.enable = true;
139 state->pending.pitch = pitch;
140 state->pending.format = format;
141 state->pending.addr = addr;
142 state->pending.x = plane->state->dst.x1;
143 state->pending.y = plane->state->dst.y1;
144 state->pending.width = drm_rect_width(&plane->state->dst);
145 state->pending.height = drm_rect_height(&plane->state->dst);
146 state->pending.rotation = plane->state->rotation;
147 wmb(); /* Make sure the above parameters are set before update */
148 state->pending.dirty = true;
151 static void mtk_plane_atomic_disable(struct drm_plane *plane,
152 struct drm_plane_state *old_state)
154 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
156 state->pending.enable = false;
157 wmb(); /* Make sure the above parameter is set before update */
158 state->pending.dirty = true;
161 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
162 .prepare_fb = drm_gem_fb_prepare_fb,
163 .atomic_check = mtk_plane_atomic_check,
164 .atomic_update = mtk_plane_atomic_update,
165 .atomic_disable = mtk_plane_atomic_disable,
168 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
169 unsigned long possible_crtcs, enum drm_plane_type type,
170 unsigned int supported_rotations)
174 err = drm_universal_plane_init(dev, plane, possible_crtcs,
175 &mtk_plane_funcs, formats,
176 ARRAY_SIZE(formats), NULL, type, NULL);
178 DRM_ERROR("failed to initialize plane\n");
182 if (supported_rotations & ~DRM_MODE_ROTATE_0) {
183 err = drm_plane_create_rotation_property(plane,
185 supported_rotations);
187 DRM_INFO("Create rotation property failed\n");
190 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);