2 * Copyright (c) 2015 MediaTek Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_plane_helper.h>
20 #include "mtk_drm_crtc.h"
21 #include "mtk_drm_ddp_comp.h"
22 #include "mtk_drm_drv.h"
23 #include "mtk_drm_fb.h"
24 #include "mtk_drm_gem.h"
25 #include "mtk_drm_plane.h"
27 static const u32 formats[] = {
35 static void mtk_plane_reset(struct drm_plane *plane)
37 struct mtk_plane_state *state;
40 __drm_atomic_helper_plane_destroy_state(plane->state);
42 state = to_mtk_plane_state(plane->state);
43 memset(state, 0, sizeof(*state));
45 state = kzalloc(sizeof(*state), GFP_KERNEL);
48 plane->state = &state->base;
51 state->base.plane = plane;
52 state->pending.format = DRM_FORMAT_RGB565;
55 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
57 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
58 struct mtk_plane_state *state;
60 state = kzalloc(sizeof(*state), GFP_KERNEL);
64 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
66 WARN_ON(state->base.plane != plane);
68 state->pending = old_state->pending;
73 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
74 struct drm_plane_state *state)
76 __drm_atomic_helper_plane_destroy_state(state);
77 kfree(to_mtk_plane_state(state));
80 static const struct drm_plane_funcs mtk_plane_funcs = {
81 .update_plane = drm_atomic_helper_update_plane,
82 .disable_plane = drm_atomic_helper_disable_plane,
83 .destroy = drm_plane_cleanup,
84 .reset = mtk_plane_reset,
85 .atomic_duplicate_state = mtk_plane_duplicate_state,
86 .atomic_destroy_state = mtk_drm_plane_destroy_state,
89 static int mtk_plane_atomic_check(struct drm_plane *plane,
90 struct drm_plane_state *state)
92 struct drm_framebuffer *fb = state->fb;
93 struct drm_crtc_state *crtc_state;
101 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
102 if (IS_ERR(crtc_state))
103 return PTR_ERR(crtc_state);
105 return drm_atomic_helper_check_plane_state(state, crtc_state,
106 DRM_PLANE_HELPER_NO_SCALING,
107 DRM_PLANE_HELPER_NO_SCALING,
111 static void mtk_plane_atomic_update(struct drm_plane *plane,
112 struct drm_plane_state *old_state)
114 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
115 struct drm_crtc *crtc = plane->state->crtc;
116 struct drm_framebuffer *fb = plane->state->fb;
117 struct drm_gem_object *gem;
118 struct mtk_drm_gem_obj *mtk_gem;
119 unsigned int pitch, format;
122 if (!crtc || WARN_ON(!fb))
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 += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
132 addr += (plane->state->src.y1 >> 16) * pitch;
134 state->pending.enable = true;
135 state->pending.pitch = pitch;
136 state->pending.format = format;
137 state->pending.addr = addr;
138 state->pending.x = plane->state->dst.x1;
139 state->pending.y = plane->state->dst.y1;
140 state->pending.width = drm_rect_width(&plane->state->dst);
141 state->pending.height = drm_rect_height(&plane->state->dst);
142 wmb(); /* Make sure the above parameters are set before update */
143 state->pending.dirty = true;
146 static void mtk_plane_atomic_disable(struct drm_plane *plane,
147 struct drm_plane_state *old_state)
149 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
151 state->pending.enable = false;
152 wmb(); /* Make sure the above parameter is set before update */
153 state->pending.dirty = true;
156 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
157 .atomic_check = mtk_plane_atomic_check,
158 .atomic_update = mtk_plane_atomic_update,
159 .atomic_disable = mtk_plane_atomic_disable,
162 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
163 unsigned long possible_crtcs, enum drm_plane_type type)
167 err = drm_universal_plane_init(dev, plane, possible_crtcs,
168 &mtk_plane_funcs, formats,
169 ARRAY_SIZE(formats), NULL, type, NULL);
171 DRM_ERROR("failed to initialize plane\n");
175 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);