1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: CK Hu <ck.hu@mediatek.com>
8 #include <drm/drm_atomic.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_plane_helper.h>
12 #include "mtk_drm_crtc.h"
13 #include "mtk_drm_ddp_comp.h"
14 #include "mtk_drm_drv.h"
15 #include "mtk_drm_fb.h"
16 #include "mtk_drm_gem.h"
17 #include "mtk_drm_plane.h"
19 static const u32 formats[] = {
27 static void mtk_plane_reset(struct drm_plane *plane)
29 struct mtk_plane_state *state;
32 __drm_atomic_helper_plane_destroy_state(plane->state);
34 state = to_mtk_plane_state(plane->state);
35 memset(state, 0, sizeof(*state));
37 state = kzalloc(sizeof(*state), GFP_KERNEL);
40 plane->state = &state->base;
43 state->base.plane = plane;
44 state->pending.format = DRM_FORMAT_RGB565;
47 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
49 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
50 struct mtk_plane_state *state;
52 state = kzalloc(sizeof(*state), GFP_KERNEL);
56 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
58 WARN_ON(state->base.plane != plane);
60 state->pending = old_state->pending;
65 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
66 struct drm_plane_state *state)
68 __drm_atomic_helper_plane_destroy_state(state);
69 kfree(to_mtk_plane_state(state));
72 static const struct drm_plane_funcs mtk_plane_funcs = {
73 .update_plane = drm_atomic_helper_update_plane,
74 .disable_plane = drm_atomic_helper_disable_plane,
75 .destroy = drm_plane_cleanup,
76 .reset = mtk_plane_reset,
77 .atomic_duplicate_state = mtk_plane_duplicate_state,
78 .atomic_destroy_state = mtk_drm_plane_destroy_state,
81 static int mtk_plane_atomic_check(struct drm_plane *plane,
82 struct drm_plane_state *state)
84 struct drm_framebuffer *fb = state->fb;
85 struct drm_crtc_state *crtc_state;
93 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
94 if (IS_ERR(crtc_state))
95 return PTR_ERR(crtc_state);
97 return drm_atomic_helper_check_plane_state(state, crtc_state,
98 DRM_PLANE_HELPER_NO_SCALING,
99 DRM_PLANE_HELPER_NO_SCALING,
103 static void mtk_plane_atomic_update(struct drm_plane *plane,
104 struct drm_plane_state *old_state)
106 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
107 struct drm_crtc *crtc = plane->state->crtc;
108 struct drm_framebuffer *fb = plane->state->fb;
109 struct drm_gem_object *gem;
110 struct mtk_drm_gem_obj *mtk_gem;
111 unsigned int pitch, format;
114 if (!crtc || WARN_ON(!fb))
118 mtk_gem = to_mtk_gem_obj(gem);
119 addr = mtk_gem->dma_addr;
120 pitch = fb->pitches[0];
121 format = fb->format->format;
123 addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
124 addr += (plane->state->src.y1 >> 16) * pitch;
126 state->pending.enable = true;
127 state->pending.pitch = pitch;
128 state->pending.format = format;
129 state->pending.addr = addr;
130 state->pending.x = plane->state->dst.x1;
131 state->pending.y = plane->state->dst.y1;
132 state->pending.width = drm_rect_width(&plane->state->dst);
133 state->pending.height = drm_rect_height(&plane->state->dst);
134 wmb(); /* Make sure the above parameters are set before update */
135 state->pending.dirty = true;
138 static void mtk_plane_atomic_disable(struct drm_plane *plane,
139 struct drm_plane_state *old_state)
141 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
143 state->pending.enable = false;
144 wmb(); /* Make sure the above parameter is set before update */
145 state->pending.dirty = true;
148 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
149 .atomic_check = mtk_plane_atomic_check,
150 .atomic_update = mtk_plane_atomic_update,
151 .atomic_disable = mtk_plane_atomic_disable,
154 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
155 unsigned long possible_crtcs, enum drm_plane_type type)
159 err = drm_universal_plane_init(dev, plane, possible_crtcs,
160 &mtk_plane_funcs, formats,
161 ARRAY_SIZE(formats), NULL, type, NULL);
163 DRM_ERROR("failed to initialize plane\n");
167 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);