Loading...
1/*
2 * Copyright (c) 2015 MediaTek Inc.
3 * Author: CK Hu <ck.hu@mediatek.com>
4 *
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.
8 *
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.
13 */
14
15#include <drm/drmP.h>
16#include <drm/drm_atomic.h>
17#include <drm/drm_atomic_helper.h>
18#include <drm/drm_plane_helper.h>
19
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"
26
27static const u32 formats[] = {
28 DRM_FORMAT_XRGB8888,
29 DRM_FORMAT_ARGB8888,
30 DRM_FORMAT_RGB565,
31 DRM_FORMAT_UYVY,
32 DRM_FORMAT_YUYV,
33};
34
35static void mtk_plane_reset(struct drm_plane *plane)
36{
37 struct mtk_plane_state *state;
38
39 if (plane->state) {
40 __drm_atomic_helper_plane_destroy_state(plane->state);
41
42 state = to_mtk_plane_state(plane->state);
43 memset(state, 0, sizeof(*state));
44 } else {
45 state = kzalloc(sizeof(*state), GFP_KERNEL);
46 if (!state)
47 return;
48 plane->state = &state->base;
49 }
50
51 state->base.plane = plane;
52 state->pending.format = DRM_FORMAT_RGB565;
53}
54
55static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
56{
57 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
58 struct mtk_plane_state *state;
59
60 state = kzalloc(sizeof(*state), GFP_KERNEL);
61 if (!state)
62 return NULL;
63
64 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
65
66 WARN_ON(state->base.plane != plane);
67
68 state->pending = old_state->pending;
69
70 return &state->base;
71}
72
73static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
74 struct drm_plane_state *state)
75{
76 __drm_atomic_helper_plane_destroy_state(state);
77 kfree(to_mtk_plane_state(state));
78}
79
80static 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,
87};
88
89static int mtk_plane_atomic_check(struct drm_plane *plane,
90 struct drm_plane_state *state)
91{
92 struct drm_framebuffer *fb = state->fb;
93 struct drm_crtc_state *crtc_state;
94
95 if (!fb)
96 return 0;
97
98 if (!mtk_fb_get_gem_obj(fb)) {
99 DRM_DEBUG_KMS("buffer is null\n");
100 return -EFAULT;
101 }
102
103 if (!state->crtc)
104 return 0;
105
106 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
107 if (IS_ERR(crtc_state))
108 return PTR_ERR(crtc_state);
109
110 return drm_atomic_helper_check_plane_state(state, crtc_state,
111 DRM_PLANE_HELPER_NO_SCALING,
112 DRM_PLANE_HELPER_NO_SCALING,
113 true, true);
114}
115
116static void mtk_plane_atomic_update(struct drm_plane *plane,
117 struct drm_plane_state *old_state)
118{
119 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
120 struct drm_crtc *crtc = plane->state->crtc;
121 struct drm_framebuffer *fb = plane->state->fb;
122 struct drm_gem_object *gem;
123 struct mtk_drm_gem_obj *mtk_gem;
124 unsigned int pitch, format;
125 dma_addr_t addr;
126
127 if (!crtc || WARN_ON(!fb))
128 return;
129
130 gem = mtk_fb_get_gem_obj(fb);
131 mtk_gem = to_mtk_gem_obj(gem);
132 addr = mtk_gem->dma_addr;
133 pitch = fb->pitches[0];
134 format = fb->format->format;
135
136 addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
137 addr += (plane->state->src.y1 >> 16) * pitch;
138
139 state->pending.enable = true;
140 state->pending.pitch = pitch;
141 state->pending.format = format;
142 state->pending.addr = addr;
143 state->pending.x = plane->state->dst.x1;
144 state->pending.y = plane->state->dst.y1;
145 state->pending.width = drm_rect_width(&plane->state->dst);
146 state->pending.height = drm_rect_height(&plane->state->dst);
147 wmb(); /* Make sure the above parameters are set before update */
148 state->pending.dirty = true;
149}
150
151static void mtk_plane_atomic_disable(struct drm_plane *plane,
152 struct drm_plane_state *old_state)
153{
154 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
155
156 state->pending.enable = false;
157 wmb(); /* Make sure the above parameter is set before update */
158 state->pending.dirty = true;
159}
160
161static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
162 .atomic_check = mtk_plane_atomic_check,
163 .atomic_update = mtk_plane_atomic_update,
164 .atomic_disable = mtk_plane_atomic_disable,
165};
166
167int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
168 unsigned long possible_crtcs, enum drm_plane_type type)
169{
170 int err;
171
172 err = drm_universal_plane_init(dev, plane, possible_crtcs,
173 &mtk_plane_funcs, formats,
174 ARRAY_SIZE(formats), NULL, type, NULL);
175 if (err) {
176 DRM_ERROR("failed to initialize plane\n");
177 return err;
178 }
179
180 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
181
182 return 0;
183}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: CK Hu <ck.hu@mediatek.com>
5 */
6
7#include <drm/drm_atomic.h>
8#include <drm/drm_atomic_helper.h>
9#include <drm/drm_fourcc.h>
10#include <drm/drm_atomic_uapi.h>
11#include <drm/drm_plane_helper.h>
12#include <drm/drm_gem_framebuffer_helper.h>
13
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"
19
20static const u32 formats[] = {
21 DRM_FORMAT_XRGB8888,
22 DRM_FORMAT_ARGB8888,
23 DRM_FORMAT_BGRX8888,
24 DRM_FORMAT_BGRA8888,
25 DRM_FORMAT_ABGR8888,
26 DRM_FORMAT_XBGR8888,
27 DRM_FORMAT_RGB888,
28 DRM_FORMAT_BGR888,
29 DRM_FORMAT_RGB565,
30 DRM_FORMAT_UYVY,
31 DRM_FORMAT_YUYV,
32};
33
34static void mtk_plane_reset(struct drm_plane *plane)
35{
36 struct mtk_plane_state *state;
37
38 if (plane->state) {
39 __drm_atomic_helper_plane_destroy_state(plane->state);
40
41 state = to_mtk_plane_state(plane->state);
42 memset(state, 0, sizeof(*state));
43 } else {
44 state = kzalloc(sizeof(*state), GFP_KERNEL);
45 if (!state)
46 return;
47 plane->state = &state->base;
48 }
49
50 state->base.plane = plane;
51 state->pending.format = DRM_FORMAT_RGB565;
52}
53
54static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
55{
56 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
57 struct mtk_plane_state *state;
58
59 state = kzalloc(sizeof(*state), GFP_KERNEL);
60 if (!state)
61 return NULL;
62
63 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
64
65 WARN_ON(state->base.plane != plane);
66
67 state->pending = old_state->pending;
68
69 return &state->base;
70}
71
72static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
73 struct drm_plane_state *state)
74{
75 __drm_atomic_helper_plane_destroy_state(state);
76 kfree(to_mtk_plane_state(state));
77}
78
79static int mtk_plane_atomic_async_check(struct drm_plane *plane,
80 struct drm_plane_state *state)
81{
82 struct drm_crtc_state *crtc_state;
83 int ret;
84
85 if (plane != state->crtc->cursor)
86 return -EINVAL;
87
88 if (!plane->state)
89 return -EINVAL;
90
91 if (!plane->state->fb)
92 return -EINVAL;
93
94 ret = mtk_drm_crtc_plane_check(state->crtc, plane,
95 to_mtk_plane_state(state));
96 if (ret)
97 return ret;
98
99 if (state->state)
100 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
101 state->crtc);
102 else /* Special case for asynchronous cursor updates. */
103 crtc_state = state->crtc->state;
104
105 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
106 DRM_PLANE_HELPER_NO_SCALING,
107 DRM_PLANE_HELPER_NO_SCALING,
108 true, true);
109}
110
111static void mtk_plane_atomic_async_update(struct drm_plane *plane,
112 struct drm_plane_state *new_state)
113{
114 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
115
116 plane->state->crtc_x = new_state->crtc_x;
117 plane->state->crtc_y = new_state->crtc_y;
118 plane->state->crtc_h = new_state->crtc_h;
119 plane->state->crtc_w = new_state->crtc_w;
120 plane->state->src_x = new_state->src_x;
121 plane->state->src_y = new_state->src_y;
122 plane->state->src_h = new_state->src_h;
123 plane->state->src_w = new_state->src_w;
124 swap(plane->state->fb, new_state->fb);
125 state->pending.async_dirty = true;
126
127 mtk_drm_crtc_async_update(new_state->crtc, plane, new_state);
128}
129
130static const struct drm_plane_funcs mtk_plane_funcs = {
131 .update_plane = drm_atomic_helper_update_plane,
132 .disable_plane = drm_atomic_helper_disable_plane,
133 .destroy = drm_plane_cleanup,
134 .reset = mtk_plane_reset,
135 .atomic_duplicate_state = mtk_plane_duplicate_state,
136 .atomic_destroy_state = mtk_drm_plane_destroy_state,
137};
138
139static int mtk_plane_atomic_check(struct drm_plane *plane,
140 struct drm_plane_state *state)
141{
142 struct drm_framebuffer *fb = state->fb;
143 struct drm_crtc_state *crtc_state;
144 int ret;
145
146 if (!fb)
147 return 0;
148
149 if (WARN_ON(!state->crtc))
150 return 0;
151
152 ret = mtk_drm_crtc_plane_check(state->crtc, plane,
153 to_mtk_plane_state(state));
154 if (ret)
155 return ret;
156
157 crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
158 if (IS_ERR(crtc_state))
159 return PTR_ERR(crtc_state);
160
161 return drm_atomic_helper_check_plane_state(state, crtc_state,
162 DRM_PLANE_HELPER_NO_SCALING,
163 DRM_PLANE_HELPER_NO_SCALING,
164 true, true);
165}
166
167static void mtk_plane_atomic_disable(struct drm_plane *plane,
168 struct drm_plane_state *old_state)
169{
170 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
171
172 state->pending.enable = false;
173 wmb(); /* Make sure the above parameter is set before update */
174 state->pending.dirty = true;
175}
176
177static void mtk_plane_atomic_update(struct drm_plane *plane,
178 struct drm_plane_state *old_state)
179{
180 struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
181 struct drm_crtc *crtc = plane->state->crtc;
182 struct drm_framebuffer *fb = plane->state->fb;
183 struct drm_gem_object *gem;
184 struct mtk_drm_gem_obj *mtk_gem;
185 unsigned int pitch, format;
186 dma_addr_t addr;
187
188 if (!crtc || WARN_ON(!fb))
189 return;
190
191 if (!plane->state->visible) {
192 mtk_plane_atomic_disable(plane, old_state);
193 return;
194 }
195
196 gem = fb->obj[0];
197 mtk_gem = to_mtk_gem_obj(gem);
198 addr = mtk_gem->dma_addr;
199 pitch = fb->pitches[0];
200 format = fb->format->format;
201
202 addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
203 addr += (plane->state->src.y1 >> 16) * pitch;
204
205 state->pending.enable = true;
206 state->pending.pitch = pitch;
207 state->pending.format = format;
208 state->pending.addr = addr;
209 state->pending.x = plane->state->dst.x1;
210 state->pending.y = plane->state->dst.y1;
211 state->pending.width = drm_rect_width(&plane->state->dst);
212 state->pending.height = drm_rect_height(&plane->state->dst);
213 state->pending.rotation = plane->state->rotation;
214 wmb(); /* Make sure the above parameters are set before update */
215 state->pending.dirty = true;
216}
217
218static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
219 .prepare_fb = drm_gem_fb_prepare_fb,
220 .atomic_check = mtk_plane_atomic_check,
221 .atomic_update = mtk_plane_atomic_update,
222 .atomic_disable = mtk_plane_atomic_disable,
223 .atomic_async_update = mtk_plane_atomic_async_update,
224 .atomic_async_check = mtk_plane_atomic_async_check,
225};
226
227int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
228 unsigned long possible_crtcs, enum drm_plane_type type,
229 unsigned int supported_rotations)
230{
231 int err;
232
233 err = drm_universal_plane_init(dev, plane, possible_crtcs,
234 &mtk_plane_funcs, formats,
235 ARRAY_SIZE(formats), NULL, type, NULL);
236 if (err) {
237 DRM_ERROR("failed to initialize plane\n");
238 return err;
239 }
240
241 if (supported_rotations & ~DRM_MODE_ROTATE_0) {
242 err = drm_plane_create_rotation_property(plane,
243 DRM_MODE_ROTATE_0,
244 supported_rotations);
245 if (err)
246 DRM_INFO("Create rotation property failed\n");
247 }
248
249 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
250
251 return 0;
252}