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_cma_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_cma_helper.h>
21 #include <drm/drm_plane_helper.h>
22 #include <drm/drm_probe_helper.h>
24 #include "sun8i_mixer.h"
25 #include "sun8i_ui_layer.h"
26 #include "sun8i_ui_scaler.h"
28 static void sun8i_ui_layer_enable(struct sun8i_mixer *mixer, int channel,
29 int overlay, bool enable, unsigned int zpos,
30 unsigned int old_zpos)
32 u32 val, bld_base, ch_base;
34 bld_base = sun8i_blender_base(mixer);
35 ch_base = sun8i_channel_base(mixer, channel);
37 DRM_DEBUG_DRIVER("%sabling channel %d overlay %d\n",
38 enable ? "En" : "Dis", channel, overlay);
41 val = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN;
45 regmap_update_bits(mixer->engine.regs,
46 SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
47 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_EN, val);
49 if (!enable || zpos != old_zpos) {
50 regmap_update_bits(mixer->engine.regs,
51 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
52 SUN8I_MIXER_BLEND_PIPE_CTL_EN(old_zpos),
55 regmap_update_bits(mixer->engine.regs,
56 SUN8I_MIXER_BLEND_ROUTE(bld_base),
57 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(old_zpos),
62 val = SUN8I_MIXER_BLEND_PIPE_CTL_EN(zpos);
64 regmap_update_bits(mixer->engine.regs,
65 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
68 val = channel << SUN8I_MIXER_BLEND_ROUTE_PIPE_SHIFT(zpos);
70 regmap_update_bits(mixer->engine.regs,
71 SUN8I_MIXER_BLEND_ROUTE(bld_base),
72 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(zpos),
77 static void sun8i_ui_layer_update_alpha(struct sun8i_mixer *mixer, int channel,
78 int overlay, struct drm_plane *plane)
80 u32 mask, val, ch_base;
82 ch_base = sun8i_channel_base(mixer, channel);
84 mask = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_MASK |
85 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MASK;
87 val = SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA(plane->state->alpha >> 8);
89 val |= (plane->state->alpha == DRM_BLEND_ALPHA_OPAQUE) ?
90 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_PIXEL :
91 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_ALPHA_MODE_COMBINED;
93 regmap_update_bits(mixer->engine.regs,
94 SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
98 static int sun8i_ui_layer_update_coord(struct sun8i_mixer *mixer, int channel,
99 int overlay, struct drm_plane *plane,
102 struct drm_plane_state *state = plane->state;
103 u32 src_w, src_h, dst_w, dst_h;
104 u32 bld_base, ch_base;
108 DRM_DEBUG_DRIVER("Updating UI channel %d overlay %d\n",
111 bld_base = sun8i_blender_base(mixer);
112 ch_base = sun8i_channel_base(mixer, channel);
114 src_w = drm_rect_width(&state->src) >> 16;
115 src_h = drm_rect_height(&state->src) >> 16;
116 dst_w = drm_rect_width(&state->dst);
117 dst_h = drm_rect_height(&state->dst);
119 hphase = state->src.x1 & 0xffff;
120 vphase = state->src.y1 & 0xffff;
122 insize = SUN8I_MIXER_SIZE(src_w, src_h);
123 outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
125 /* Set height and width */
126 DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
127 state->src.x1 >> 16, state->src.y1 >> 16);
128 DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
129 regmap_write(mixer->engine.regs,
130 SUN8I_MIXER_CHAN_UI_LAYER_SIZE(ch_base, overlay),
132 regmap_write(mixer->engine.regs,
133 SUN8I_MIXER_CHAN_UI_OVL_SIZE(ch_base),
136 if (insize != outsize || hphase || vphase) {
139 DRM_DEBUG_DRIVER("HW scaling is enabled\n");
141 hscale = state->src_w / state->crtc_w;
142 vscale = state->src_h / state->crtc_h;
144 sun8i_ui_scaler_setup(mixer, channel, src_w, src_h, dst_w,
145 dst_h, hscale, vscale, hphase, vphase);
146 sun8i_ui_scaler_enable(mixer, channel, true);
148 DRM_DEBUG_DRIVER("HW scaling is not needed\n");
149 sun8i_ui_scaler_enable(mixer, channel, false);
152 /* Set base coordinates */
153 DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
154 state->dst.x1, state->dst.y1);
155 DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
156 regmap_write(mixer->engine.regs,
157 SUN8I_MIXER_BLEND_ATTR_COORD(bld_base, zpos),
158 SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
159 regmap_write(mixer->engine.regs,
160 SUN8I_MIXER_BLEND_ATTR_INSIZE(bld_base, zpos),
166 static int sun8i_ui_layer_update_formats(struct sun8i_mixer *mixer, int channel,
167 int overlay, struct drm_plane *plane)
169 struct drm_plane_state *state = plane->state;
170 const struct drm_format_info *fmt;
171 u32 val, ch_base, hw_fmt;
174 ch_base = sun8i_channel_base(mixer, channel);
176 fmt = state->fb->format;
177 ret = sun8i_mixer_drm_format_to_hw(fmt->format, &hw_fmt);
178 if (ret || fmt->is_yuv) {
179 DRM_DEBUG_DRIVER("Invalid format\n");
183 val = hw_fmt << SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_OFFSET;
184 regmap_update_bits(mixer->engine.regs,
185 SUN8I_MIXER_CHAN_UI_LAYER_ATTR(ch_base, overlay),
186 SUN8I_MIXER_CHAN_UI_LAYER_ATTR_FBFMT_MASK, val);
191 static int sun8i_ui_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
192 int overlay, struct drm_plane *plane)
194 struct drm_plane_state *state = plane->state;
195 struct drm_framebuffer *fb = state->fb;
196 struct drm_gem_cma_object *gem;
201 ch_base = sun8i_channel_base(mixer, channel);
203 /* Get the physical address of the buffer in memory */
204 gem = drm_fb_cma_get_gem_obj(fb, 0);
206 DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->paddr);
208 /* Compute the start of the displayed memory */
209 bpp = fb->format->cpp[0];
210 paddr = gem->paddr + fb->offsets[0];
212 /* Fixup framebuffer address for src coordinates */
213 paddr += (state->src.x1 >> 16) * bpp;
214 paddr += (state->src.y1 >> 16) * fb->pitches[0];
216 /* Set the line width */
217 DRM_DEBUG_DRIVER("Layer line width: %d bytes\n", fb->pitches[0]);
218 regmap_write(mixer->engine.regs,
219 SUN8I_MIXER_CHAN_UI_LAYER_PITCH(ch_base, overlay),
222 DRM_DEBUG_DRIVER("Setting buffer address to %pad\n", &paddr);
224 regmap_write(mixer->engine.regs,
225 SUN8I_MIXER_CHAN_UI_LAYER_TOP_LADDR(ch_base, overlay),
226 lower_32_bits(paddr));
231 static int sun8i_ui_layer_atomic_check(struct drm_plane *plane,
232 struct drm_atomic_state *state)
234 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
236 struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
237 struct drm_crtc *crtc = new_plane_state->crtc;
238 struct drm_crtc_state *crtc_state;
239 int min_scale, max_scale;
244 crtc_state = drm_atomic_get_existing_crtc_state(state,
246 if (WARN_ON(!crtc_state))
249 min_scale = DRM_PLANE_HELPER_NO_SCALING;
250 max_scale = DRM_PLANE_HELPER_NO_SCALING;
252 if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
253 min_scale = SUN8I_UI_SCALER_SCALE_MIN;
254 max_scale = SUN8I_UI_SCALER_SCALE_MAX;
257 return drm_atomic_helper_check_plane_state(new_plane_state,
259 min_scale, max_scale,
263 static void sun8i_ui_layer_atomic_disable(struct drm_plane *plane,
264 struct drm_atomic_state *state)
266 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
268 struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
269 unsigned int old_zpos = old_state->normalized_zpos;
270 struct sun8i_mixer *mixer = layer->mixer;
272 sun8i_ui_layer_enable(mixer, layer->channel, layer->overlay, false, 0,
276 static void sun8i_ui_layer_atomic_update(struct drm_plane *plane,
277 struct drm_atomic_state *state)
279 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
281 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
283 struct sun8i_ui_layer *layer = plane_to_sun8i_ui_layer(plane);
284 unsigned int zpos = new_state->normalized_zpos;
285 unsigned int old_zpos = old_state->normalized_zpos;
286 struct sun8i_mixer *mixer = layer->mixer;
288 if (!new_state->visible) {
289 sun8i_ui_layer_enable(mixer, layer->channel,
290 layer->overlay, false, 0, old_zpos);
294 sun8i_ui_layer_update_coord(mixer, layer->channel,
295 layer->overlay, plane, zpos);
296 sun8i_ui_layer_update_alpha(mixer, layer->channel,
297 layer->overlay, plane);
298 sun8i_ui_layer_update_formats(mixer, layer->channel,
299 layer->overlay, plane);
300 sun8i_ui_layer_update_buffer(mixer, layer->channel,
301 layer->overlay, plane);
302 sun8i_ui_layer_enable(mixer, layer->channel, layer->overlay,
303 true, zpos, old_zpos);
306 static const struct drm_plane_helper_funcs sun8i_ui_layer_helper_funcs = {
307 .atomic_check = sun8i_ui_layer_atomic_check,
308 .atomic_disable = sun8i_ui_layer_atomic_disable,
309 .atomic_update = sun8i_ui_layer_atomic_update,
312 static const struct drm_plane_funcs sun8i_ui_layer_funcs = {
313 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
314 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
315 .destroy = drm_plane_cleanup,
316 .disable_plane = drm_atomic_helper_disable_plane,
317 .reset = drm_atomic_helper_plane_reset,
318 .update_plane = drm_atomic_helper_update_plane,
321 static const u32 sun8i_ui_layer_formats[] = {
344 static const uint64_t sun8i_layer_modifiers[] = {
345 DRM_FORMAT_MOD_LINEAR,
346 DRM_FORMAT_MOD_INVALID
349 struct sun8i_ui_layer *sun8i_ui_layer_init_one(struct drm_device *drm,
350 struct sun8i_mixer *mixer,
353 enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY;
354 int channel = mixer->cfg->vi_num + index;
355 struct sun8i_ui_layer *layer;
356 unsigned int plane_cnt;
359 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
361 return ERR_PTR(-ENOMEM);
364 type = DRM_PLANE_TYPE_PRIMARY;
366 /* possible crtcs are set later */
367 ret = drm_universal_plane_init(drm, &layer->plane, 0,
368 &sun8i_ui_layer_funcs,
369 sun8i_ui_layer_formats,
370 ARRAY_SIZE(sun8i_ui_layer_formats),
371 sun8i_layer_modifiers, type, NULL);
373 dev_err(drm->dev, "Couldn't initialize layer\n");
377 plane_cnt = mixer->cfg->ui_num + mixer->cfg->vi_num;
379 ret = drm_plane_create_alpha_property(&layer->plane);
381 dev_err(drm->dev, "Couldn't add alpha property\n");
385 ret = drm_plane_create_zpos_property(&layer->plane, channel,
388 dev_err(drm->dev, "Couldn't add zpos property\n");
392 drm_plane_helper_add(&layer->plane, &sun8i_ui_layer_helper_funcs);
393 layer->mixer = mixer;
394 layer->channel = channel;