xref: /linux/drivers/gpu/drm/sun4i/sun8i_ui_layer.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) Icenowy Zheng <icenowy@aosc.io>
4  *
5  * Based on sun4i_layer.h, which is:
6  *   Copyright (C) 2015 Free Electrons
7  *   Copyright (C) 2015 NextThing Co
8  *
9  *   Maxime Ripard <maxime.ripard@free-electrons.com>
10  */
11 
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_blend.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_fb_dma_helper.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_framebuffer.h>
19 #include <drm/drm_gem_atomic_helper.h>
20 #include <drm/drm_gem_dma_helper.h>
21 #include <drm/drm_print.h>
22 #include <drm/drm_probe_helper.h>
23 
24 #include "sun8i_mixer.h"
25 #include "sun8i_ui_layer.h"
26 #include "sun8i_ui_scaler.h"
27 #include "sun8i_vi_scaler.h"
28 
29 static void sun8i_ui_layer_update_alpha(struct sun8i_mixer *mixer, int channel,
30 					int overlay, struct drm_plane *plane)
31 {
32 	u32 mask, val, ch_base;
33 
34 	ch_base = sun8i_channel_base(mixer, channel);
35 
36 	mask = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_MASK |
37 		SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MASK;
38 
39 	val = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA(plane->state->alpha >> 8);
40 
41 	val |= (plane->state->alpha == DRM_BLEND_ALPHA_OPAQUE) ?
42 		SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_PIXEL :
43 		SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_COMBINED;
44 
45 	regmap_update_bits(mixer->engine.regs,
46 			   SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
47 			   mask, val);
48 }
49 
50 static int sun8i_ui_layer_update_coord(struct sun8i_mixer *mixer, int channel,
51 				       int overlay, struct drm_plane *plane,
52 				       unsigned int zpos)
53 {
54 	struct drm_plane_state *state = plane->state;
55 	u32 src_w, src_h, dst_w, dst_h;
56 	struct regmap *bld_regs;
57 	u32 bld_base, ch_base;
58 	u32 outsize, insize;
59 	u32 hphase, vphase;
60 
61 	DRM_DEBUG_DRIVER("Updating UI channel %d overlay %d\n",
62 			 channel, overlay);
63 
64 	bld_base = sun8i_blender_base(mixer);
65 	bld_regs = sun8i_blender_regmap(mixer);
66 	ch_base = sun8i_channel_base(mixer, channel);
67 
68 	src_w = drm_rect_width(&state->src) >> 16;
69 	src_h = drm_rect_height(&state->src) >> 16;
70 	dst_w = drm_rect_width(&state->dst);
71 	dst_h = drm_rect_height(&state->dst);
72 
73 	hphase = state->src.x1 & 0xffff;
74 	vphase = state->src.y1 & 0xffff;
75 
76 	insize = SUN8I_MIXER_SIZE(src_w, src_h);
77 	outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
78 
79 	/* Set height and width */
80 	DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
81 			 state->src.x1 >> 16, state->src.y1 >> 16);
82 	DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
83 	regmap_write(mixer->engine.regs,
84 		     SUN8I_MIXER_CHAN_UI_LAYER_SIZE(ch_base, overlay),
85 		     insize);
86 	regmap_write(mixer->engine.regs,
87 		     SUN8I_MIXER_CHAN_UI_OVL_SIZE(ch_base),
88 		     insize);
89 
90 	if (insize != outsize || hphase || vphase) {
91 		u32 hscale, vscale;
92 
93 		DRM_DEBUG_DRIVER("HW scaling is enabled\n");
94 
95 		hscale = state->src_w / state->crtc_w;
96 		vscale = state->src_h / state->crtc_h;
97 
98 		if (mixer->cfg->de_type == SUN8I_MIXER_DE33) {
99 			sun8i_vi_scaler_setup(mixer, channel, src_w, src_h,
100 					      dst_w, dst_h, hscale, vscale,
101 					      hphase, vphase,
102 					      state->fb->format);
103 			sun8i_vi_scaler_enable(mixer, channel, true);
104 		} else {
105 			sun8i_ui_scaler_setup(mixer, channel, src_w, src_h,
106 					      dst_w, dst_h, hscale, vscale,
107 					      hphase, vphase);
108 			sun8i_ui_scaler_enable(mixer, channel, true);
109 		}
110 	} else {
111 		DRM_DEBUG_DRIVER("HW scaling is not needed\n");
112 		if (mixer->cfg->de_type == SUN8I_MIXER_DE33)
113 			sun8i_vi_scaler_enable(mixer, channel, false);
114 		else
115 			sun8i_ui_scaler_enable(mixer, channel, false);
116 	}
117 
118 	/* Set base coordinates */
119 	DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
120 			 state->dst.x1, state->dst.y1);
121 	DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
122 	regmap_write(bld_regs,
123 		     SUN8I_MIXER_BLEND_ATTR_COORD(bld_base, zpos),
124 		     SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
125 	regmap_write(bld_regs,
126 		     SUN8I_MIXER_BLEND_ATTR_INSIZE(bld_base, zpos),
127 		     outsize);
128 
129 	return 0;
130 }
131 
132 static int sun8i_ui_layer_update_formats(struct sun8i_mixer *mixer, int channel,
133 					 int overlay, struct drm_plane *plane)
134 {
135 	struct drm_plane_state *state = plane->state;
136 	const struct drm_format_info *fmt;
137 	u32 val, ch_base, hw_fmt;
138 	int ret;
139 
140 	ch_base = sun8i_channel_base(mixer, channel);
141 
142 	fmt = state->fb->format;
143 	ret = sun8i_mixer_drm_format_to_hw(fmt->format, &hw_fmt);
144 	if (ret || fmt->is_yuv) {
145 		DRM_DEBUG_DRIVER("Invalid format\n");
146 		return -EINVAL;
147 	}
148 
149 	val = hw_fmt << SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_OFFSET;
150 	regmap_update_bits(mixer->engine.regs,
151 			   SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
152 			   SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_MASK, val);
153 
154 	return 0;
155 }
156 
157 static int sun8i_ui_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
158 					int overlay, struct drm_plane *plane)
159 {
160 	struct drm_plane_state *state = plane->state;
161 	struct drm_framebuffer *fb = state->fb;
162 	struct drm_gem_dma_object *gem;
163 	dma_addr_t dma_addr;
164 	u32 ch_base;
165 	int bpp;
166 
167 	ch_base = sun8i_channel_base(mixer, channel);
168 
169 	/* Get the physical address of the buffer in memory */
170 	gem = drm_fb_dma_get_gem_obj(fb, 0);
171 
172 	DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->dma_addr);
173 
174 	/* Compute the start of the displayed memory */
175 	bpp = fb->format->cpp[0];
176 	dma_addr = gem->dma_addr + fb->offsets[0];
177 
178 	/* Fixup framebuffer address for src coordinates */
179 	dma_addr += (state->src.x1 >> 16) * bpp;
180 	dma_addr += (state->src.y1 >> 16) * fb->pitches[0];
181 
182 	/* Set the line width */
183 	DRM_DEBUG_DRIVER("Layer line width: %d bytes\n", fb->pitches[0]);
184 	regmap_write(mixer->engine.regs,
185 		     SUN8I_MIXER_CHAN_UI_LAYER_PITCH(ch_base, overlay),
186 		     fb->pitches[0]);
187 
188 	DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &dma_addr);
189 
190 	regmap_write(mixer->engine.regs,
191 		     SUN8I_MIXER_CHAN_UI_LAYER_TOP_LADDR(ch_base, overlay),
192 		     lower_32_bits(dma_addr));
193 
194 	return 0;
195 }
196 
197 static int sun8i_ui_layer_atomic_check(struct drm_plane *plane,
198 				       struct drm_atomic_state *state)
199 {
200 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
201 										 plane);
202 	struct sun8i_layer *layer = plane_to_sun8i_layer(plane);
203 	struct drm_crtc *crtc = new_plane_state->crtc;
204 	struct drm_crtc_state *crtc_state;
205 	int min_scale, max_scale;
206 
207 	if (!crtc)
208 		return 0;
209 
210 	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
211 	if (WARN_ON(!crtc_state))
212 		return -EINVAL;
213 
214 	min_scale = DRM_PLANE_NO_SCALING;
215 	max_scale = DRM_PLANE_NO_SCALING;
216 
217 	if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
218 		min_scale = SUN8I_UI_SCALER_SCALE_MIN;
219 		max_scale = SUN8I_UI_SCALER_SCALE_MAX;
220 	}
221 
222 	return drm_atomic_helper_check_plane_state(new_plane_state,
223 						   crtc_state,
224 						   min_scale, max_scale,
225 						   true, true);
226 }
227 
228 
229 static void sun8i_ui_layer_atomic_update(struct drm_plane *plane,
230 					 struct drm_atomic_state *state)
231 {
232 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
233 									   plane);
234 	struct sun8i_layer *layer = plane_to_sun8i_layer(plane);
235 	unsigned int zpos = new_state->normalized_zpos;
236 	struct sun8i_mixer *mixer = layer->mixer;
237 
238 	if (!new_state->crtc || !new_state->visible)
239 		return;
240 
241 	sun8i_ui_layer_update_coord(mixer, layer->channel,
242 				    layer->overlay, plane, zpos);
243 	sun8i_ui_layer_update_alpha(mixer, layer->channel,
244 				    layer->overlay, plane);
245 	sun8i_ui_layer_update_formats(mixer, layer->channel,
246 				      layer->overlay, plane);
247 	sun8i_ui_layer_update_buffer(mixer, layer->channel,
248 				     layer->overlay, plane);
249 }
250 
251 static const struct drm_plane_helper_funcs sun8i_ui_layer_helper_funcs = {
252 	.atomic_check	= sun8i_ui_layer_atomic_check,
253 	.atomic_update	= sun8i_ui_layer_atomic_update,
254 };
255 
256 static const struct drm_plane_funcs sun8i_ui_layer_funcs = {
257 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
258 	.atomic_duplicate_state	= drm_atomic_helper_plane_duplicate_state,
259 	.destroy		= drm_plane_cleanup,
260 	.disable_plane		= drm_atomic_helper_disable_plane,
261 	.reset			= drm_atomic_helper_plane_reset,
262 	.update_plane		= drm_atomic_helper_update_plane,
263 };
264 
265 static const u32 sun8i_ui_layer_formats[] = {
266 	DRM_FORMAT_ABGR1555,
267 	DRM_FORMAT_ABGR4444,
268 	DRM_FORMAT_ABGR8888,
269 	DRM_FORMAT_ARGB1555,
270 	DRM_FORMAT_ARGB4444,
271 	DRM_FORMAT_ARGB8888,
272 	DRM_FORMAT_BGR565,
273 	DRM_FORMAT_BGR888,
274 	DRM_FORMAT_BGRA5551,
275 	DRM_FORMAT_BGRA4444,
276 	DRM_FORMAT_BGRA8888,
277 	DRM_FORMAT_BGRX8888,
278 	DRM_FORMAT_RGB565,
279 	DRM_FORMAT_RGB888,
280 	DRM_FORMAT_RGBA4444,
281 	DRM_FORMAT_RGBA5551,
282 	DRM_FORMAT_RGBA8888,
283 	DRM_FORMAT_RGBX8888,
284 	DRM_FORMAT_XBGR8888,
285 	DRM_FORMAT_XRGB8888,
286 };
287 
288 static const uint64_t sun8i_layer_modifiers[] = {
289 	DRM_FORMAT_MOD_LINEAR,
290 	DRM_FORMAT_MOD_INVALID
291 };
292 
293 struct sun8i_layer *sun8i_ui_layer_init_one(struct drm_device *drm,
294 					    struct sun8i_mixer *mixer,
295 					    int index)
296 {
297 	enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY;
298 	int channel = mixer->cfg->vi_num + index;
299 	struct sun8i_layer *layer;
300 	unsigned int plane_cnt;
301 	int ret;
302 
303 	layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
304 	if (!layer)
305 		return ERR_PTR(-ENOMEM);
306 
307 	if (index == 0)
308 		type = DRM_PLANE_TYPE_PRIMARY;
309 
310 	/* possible crtcs are set later */
311 	ret = drm_universal_plane_init(drm, &layer->plane, 0,
312 				       &sun8i_ui_layer_funcs,
313 				       sun8i_ui_layer_formats,
314 				       ARRAY_SIZE(sun8i_ui_layer_formats),
315 				       sun8i_layer_modifiers, type, NULL);
316 	if (ret) {
317 		dev_err(drm->dev, "Couldn't initialize layer\n");
318 		return ERR_PTR(ret);
319 	}
320 
321 	plane_cnt = mixer->cfg->ui_num + mixer->cfg->vi_num;
322 
323 	ret = drm_plane_create_alpha_property(&layer->plane);
324 	if (ret) {
325 		dev_err(drm->dev, "Couldn't add alpha property\n");
326 		return ERR_PTR(ret);
327 	}
328 
329 	ret = drm_plane_create_zpos_property(&layer->plane, channel,
330 					     0, plane_cnt - 1);
331 	if (ret) {
332 		dev_err(drm->dev, "Couldn't add zpos property\n");
333 		return ERR_PTR(ret);
334 	}
335 
336 	drm_plane_helper_add(&layer->plane, &sun8i_ui_layer_helper_funcs);
337 	layer->mixer = mixer;
338 	layer->type = SUN8I_LAYER_TYPE_UI;
339 	layer->channel = channel;
340 	layer->overlay = 0;
341 
342 	return layer;
343 }
344