1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Based on sun4i_layer.h, which is:
6 * Copyright (C) 2015 Free Electrons
7 * Copyright (C) 2015 NextThing Co
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_probe_helper.h>
23 #include "sun8i_mixer.h"
24 #include "sun8i_ui_layer.h"
25 #include "sun8i_ui_scaler.h"
27 static void sun8i_ui_layer_enable(struct sun8i_mixer *mixer, int channel,
28 int overlay, bool enable, unsigned int zpos,
29 unsigned int old_zpos)
31 u32 val, bld_base, ch_base;
33 bld_base = sun8i_blender_base(mixer);
34 ch_base = sun8i_channel_base(mixer, channel);
36 DRM_DEBUG_DRIVER("%sabling channel %d overlay %d\n",
37 enable ? "En" : "Dis", channel, overlay);
40 val = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN;
44 regmap_update_bits(mixer->engine.regs,
45 SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
46 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN, val);
48 if (!enable || zpos != old_zpos) {
49 regmap_update_bits(mixer->engine.regs,
50 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
51 SUN8I_MIXER_BLEND_PIPE_CTL_EN(old_zpos),
54 regmap_update_bits(mixer->engine.regs,
55 SUN8I_MIXER_BLEND_ROUTE(bld_base),
56 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(old_zpos),
61 val = SUN8I_MIXER_BLEND_PIPE_CTL_EN(zpos);
63 regmap_update_bits(mixer->engine.regs,
64 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
67 val = channel << SUN8I_MIXER_BLEND_ROUTE_PIPE_SHIFT(zpos);
69 regmap_update_bits(mixer->engine.regs,
70 SUN8I_MIXER_BLEND_ROUTE(bld_base),
71 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(zpos),
76 static void sun8i_ui_layer_update_alpha(struct sun8i_mixer *mixer, int channel,
77 int overlay, struct drm_plane *plane)
79 u32 mask, val, ch_base;
81 ch_base = sun8i_channel_base(mixer, channel);
83 mask = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_MASK |
84 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MASK;
86 val = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA(plane->state->alpha >> 8);
88 val |= (plane->state->alpha == DRM_BLEND_ALPHA_OPAQUE) ?
89 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_PIXEL :
90 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_COMBINED;
92 regmap_update_bits(mixer->engine.regs,
93 SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
97 static int sun8i_ui_layer_update_coord(struct sun8i_mixer *mixer, int channel,
98 int overlay, struct drm_plane *plane,
101 struct drm_plane_state *state = plane->state;
102 u32 src_w, src_h, dst_w, dst_h;
103 u32 bld_base, ch_base;
107 DRM_DEBUG_DRIVER("Updating UI channel %d overlay %d\n",
110 bld_base = sun8i_blender_base(mixer);
111 ch_base = sun8i_channel_base(mixer, channel);
113 src_w = drm_rect_width(&state->src) >> 16;
114 src_h = drm_rect_height(&state->src) >> 16;
115 dst_w = drm_rect_width(&state->dst);
116 dst_h = drm_rect_height(&state->dst);
118 hphase = state->src.x1 & 0xffff;
119 vphase = state->src.y1 & 0xffff;
121 insize = SUN8I_MIXER_SIZE(src_w, src_h);
122 outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
124 /* Set height and width */
125 DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
126 state->src.x1 >> 16, state->src.y1 >> 16);
127 DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
128 regmap_write(mixer->engine.regs,
129 SUN8I_MIXER_CHAN_UI_LAYER_SIZE(ch_base, overlay),
131 regmap_write(mixer->engine.regs,
132 SUN8I_MIXER_CHAN_UI_OVL_SIZE(ch_base),
135 if (insize != outsize || hphase || vphase) {
138 DRM_DEBUG_DRIVER("HW scaling is enabled\n");
140 hscale = state->src_w / state->crtc_w;
141 vscale = state->src_h / state->crtc_h;
143 sun8i_ui_scaler_setup(mixer, channel, src_w, src_h, dst_w,
144 dst_h, hscale, vscale, hphase, vphase);
145 sun8i_ui_scaler_enable(mixer, channel, true);
147 DRM_DEBUG_DRIVER("HW scaling is not needed\n");
148 sun8i_ui_scaler_enable(mixer, channel, false);
151 /* Set base coordinates */
152 DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
153 state->dst.x1, state->dst.y1);
154 DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
155 regmap_write(mixer->engine.regs,
156 SUN8I_MIXER_BLEND_ATTR_COORD(bld_base, zpos),
157 SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
158 regmap_write(mixer->engine.regs,
159 SUN8I_MIXER_BLEND_ATTR_INSIZE(bld_base, zpos),
165 static int sun8i_ui_layer_update_formats(struct sun8i_mixer *mixer, int channel,
166 int overlay, struct drm_plane *plane)
168 struct drm_plane_state *state = plane->state;
169 const struct drm_format_info *fmt;
170 u32 val, ch_base, hw_fmt;
173 ch_base = sun8i_channel_base(mixer, channel);
175 fmt = state->fb->format;
176 ret = sun8i_mixer_drm_format_to_hw(fmt->format, &hw_fmt);
177 if (ret || fmt->is_yuv) {
178 DRM_DEBUG_DRIVER("Invalid format\n");
182 val = hw_fmt << SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_OFFSET;
183 regmap_update_bits(mixer->engine.regs,
184 SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
185 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_MASK, val);
190 static int sun8i_ui_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
191 int overlay, struct drm_plane *plane)
193 struct drm_plane_state *state = plane->state;
194 struct drm_framebuffer *fb = state->fb;
195 struct drm_gem_dma_object *gem;
200 ch_base = sun8i_channel_base(mixer, channel);
202 /* Get the physical address of the buffer in memory */
203 gem = drm_fb_dma_get_gem_obj(fb, 0);
205 DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->dma_addr);
207 /* Compute the start of the displayed memory */
208 bpp = fb->format->cpp[0];
209 dma_addr = gem->dma_addr + fb->offsets[0];
211 /* Fixup framebuffer address for src coordinates */
212 dma_addr += (state->src.x1 >> 16) * bpp;
213 dma_addr += (state->src.y1 >> 16) * fb->pitches[0];
215 /* Set the line width */
216 DRM_DEBUG_DRIVER("Layer line width: %d bytes\n", fb->pitches[0]);
217 regmap_write(mixer->engine.regs,
218 SUN8I_MIXER_CHAN_UI_LAYER_PITCH(ch_base, overlay),
221 DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &dma_addr);
223 regmap_write(mixer->engine.regs,
224 SUN8I_MIXER_CHAN_UI_LAYER_TOP_LADDR(ch_base, overlay),
225 lower_32_bits(dma_addr));
230 static int sun8i_ui_layer_atomic_check(struct drm_plane *plane,
231 struct drm_atomic_state *state)
233 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
235 struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
236 struct drm_crtc *crtc = new_plane_state->crtc;
237 struct drm_crtc_state *crtc_state;
238 int min_scale, max_scale;
243 crtc_state = drm_atomic_get_existing_crtc_state(state,
245 if (WARN_ON(!crtc_state))
248 min_scale = DRM_PLANE_NO_SCALING;
249 max_scale = DRM_PLANE_NO_SCALING;
251 if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
252 min_scale = SUN8I_UI_SCALER_SCALE_MIN;
253 max_scale = SUN8I_UI_SCALER_SCALE_MAX;
256 return drm_atomic_helper_check_plane_state(new_plane_state,
258 min_scale, max_scale,
262 static void sun8i_ui_layer_atomic_disable(struct drm_plane *plane,
263 struct drm_atomic_state *state)
265 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
267 struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
268 unsigned int old_zpos = old_state->normalized_zpos;
269 struct sun8i_mixer *mixer = layer->mixer;
271 sun8i_ui_layer_enable(mixer, layer->channel, layer->overlay, false, 0,
275 static void sun8i_ui_layer_atomic_update(struct drm_plane *plane,
276 struct drm_atomic_state *state)
278 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
280 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
282 struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
283 unsigned int zpos = new_state->normalized_zpos;
284 unsigned int old_zpos = old_state->normalized_zpos;
285 struct sun8i_mixer *mixer = layer->mixer;
287 if (!new_state->visible) {
288 sun8i_ui_layer_enable(mixer, layer->channel,
289 layer->overlay, false, 0, old_zpos);
293 sun8i_ui_layer_update_coord(mixer, layer->channel,
294 layer->overlay, plane, zpos);
295 sun8i_ui_layer_update_alpha(mixer, layer->channel,
296 layer->overlay, plane);
297 sun8i_ui_layer_update_formats(mixer, layer->channel,
298 layer->overlay, plane);
299 sun8i_ui_layer_update_buffer(mixer, layer->channel,
300 layer->overlay, plane);
301 sun8i_ui_layer_enable(mixer, layer->channel, layer->overlay,
302 true, zpos, old_zpos);
305 static const struct drm_plane_helper_funcs sun8i_ui_layer_helper_funcs = {
306 .atomic_check = sun8i_ui_layer_atomic_check,
307 .atomic_disable = sun8i_ui_layer_atomic_disable,
308 .atomic_update = sun8i_ui_layer_atomic_update,
311 static const struct drm_plane_funcs sun8i_ui_layer_funcs = {
312 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
313 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
314 .destroy = drm_plane_cleanup,
315 .disable_plane = drm_atomic_helper_disable_plane,
316 .reset = drm_atomic_helper_plane_reset,
317 .update_plane = drm_atomic_helper_update_plane,
320 static const u32 sun8i_ui_layer_formats[] = {
343 static const uint64_t sun8i_layer_modifiers[] = {
344 DRM_FORMAT_MOD_LINEAR,
345 DRM_FORMAT_MOD_INVALID
348 struct sun8i_ui_layer *sun8i_ui_layer_init_one(struct drm_device *drm,
349 struct sun8i_mixer *mixer,
352 enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY;
353 int channel = mixer->cfg->vi_num + index;
354 struct sun8i_ui_layer *layer;
355 unsigned int plane_cnt;
358 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
360 return ERR_PTR(-ENOMEM);
363 type = DRM_PLANE_TYPE_PRIMARY;
365 /* possible crtcs are set later */
366 ret = drm_universal_plane_init(drm, &layer->plane, 0,
367 &sun8i_ui_layer_funcs,
368 sun8i_ui_layer_formats,
369 ARRAY_SIZE(sun8i_ui_layer_formats),
370 sun8i_layer_modifiers, type, NULL);
372 dev_err(drm->dev, "Couldn't initialize layer\n");
376 plane_cnt = mixer->cfg->ui_num + mixer->cfg->vi_num;
378 ret = drm_plane_create_alpha_property(&layer->plane);
380 dev_err(drm->dev, "Couldn't add alpha property\n");
384 ret = drm_plane_create_zpos_property(&layer->plane, channel,
387 dev_err(drm->dev, "Couldn't add zpos property\n");
391 drm_plane_helper_add(&layer->plane, &sun8i_ui_layer_helper_funcs);
392 layer->mixer = mixer;
393 layer->channel = channel;