1 // SPDX-License-Identifier: GPL-2.0-or-later
6 #include <drm/drm_atomic.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_crtc.h>
9 #include <drm/drm_fb_cma_helper.h>
10 #include <drm/drm_gem_atomic_helper.h>
11 #include <drm/drm_gem_cma_helper.h>
12 #include <drm/drm_plane_helper.h>
13 #include <drm/drm_probe_helper.h>
15 #include "sun8i_csc.h"
16 #include "sun8i_mixer.h"
17 #include "sun8i_vi_layer.h"
18 #include "sun8i_vi_scaler.h"
20 static void sun8i_vi_layer_enable(struct sun8i_mixer *mixer, int channel,
21 int overlay, bool enable, unsigned int zpos,
22 unsigned int old_zpos)
24 u32 val, bld_base, ch_base;
26 bld_base = sun8i_blender_base(mixer);
27 ch_base = sun8i_channel_base(mixer, channel);
29 DRM_DEBUG_DRIVER("%sabling VI channel %d overlay %d\n",
30 enable ? "En" : "Dis", channel, overlay);
33 val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN;
37 regmap_update_bits(mixer->engine.regs,
38 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay),
39 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN, val);
41 if (!enable || zpos != old_zpos) {
42 regmap_update_bits(mixer->engine.regs,
43 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
44 SUN8I_MIXER_BLEND_PIPE_CTL_EN(old_zpos),
47 regmap_update_bits(mixer->engine.regs,
48 SUN8I_MIXER_BLEND_ROUTE(bld_base),
49 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(old_zpos),
54 val = SUN8I_MIXER_BLEND_PIPE_CTL_EN(zpos);
56 regmap_update_bits(mixer->engine.regs,
57 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
60 val = channel << SUN8I_MIXER_BLEND_ROUTE_PIPE_SHIFT(zpos);
62 regmap_update_bits(mixer->engine.regs,
63 SUN8I_MIXER_BLEND_ROUTE(bld_base),
64 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(zpos),
69 static void sun8i_vi_layer_update_alpha(struct sun8i_mixer *mixer, int channel,
70 int overlay, struct drm_plane *plane)
72 u32 mask, val, ch_base;
74 ch_base = sun8i_channel_base(mixer, channel);
76 if (mixer->cfg->is_de3) {
77 mask = SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MASK |
78 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_MASK;
79 val = SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA
80 (plane->state->alpha >> 8);
82 val |= (plane->state->alpha == DRM_BLEND_ALPHA_OPAQUE) ?
83 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_PIXEL :
84 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_COMBINED;
86 regmap_update_bits(mixer->engine.regs,
87 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base,
90 } else if (mixer->cfg->vi_num == 1) {
91 regmap_update_bits(mixer->engine.regs,
92 SUN8I_MIXER_FCC_GLOBAL_ALPHA_REG,
93 SUN8I_MIXER_FCC_GLOBAL_ALPHA_MASK,
94 SUN8I_MIXER_FCC_GLOBAL_ALPHA
95 (plane->state->alpha >> 8));
99 static int sun8i_vi_layer_update_coord(struct sun8i_mixer *mixer, int channel,
100 int overlay, struct drm_plane *plane,
103 struct drm_plane_state *state = plane->state;
104 const struct drm_format_info *format = state->fb->format;
105 u32 src_w, src_h, dst_w, dst_h;
106 u32 bld_base, ch_base;
113 DRM_DEBUG_DRIVER("Updating VI channel %d overlay %d\n",
116 bld_base = sun8i_blender_base(mixer);
117 ch_base = sun8i_channel_base(mixer, channel);
119 src_w = drm_rect_width(&state->src) >> 16;
120 src_h = drm_rect_height(&state->src) >> 16;
121 dst_w = drm_rect_width(&state->dst);
122 dst_h = drm_rect_height(&state->dst);
124 hphase = state->src.x1 & 0xffff;
125 vphase = state->src.y1 & 0xffff;
127 /* make coordinates dividable by subsampling factor */
128 if (format->hsub > 1) {
131 mask = format->hsub - 1;
132 remainder = (state->src.x1 >> 16) & mask;
133 src_w = (src_w + remainder) & ~mask;
134 hphase += remainder << 16;
137 if (format->vsub > 1) {
140 mask = format->vsub - 1;
141 remainder = (state->src.y1 >> 16) & mask;
142 src_h = (src_h + remainder) & ~mask;
143 vphase += remainder << 16;
146 insize = SUN8I_MIXER_SIZE(src_w, src_h);
147 outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
149 /* Set height and width */
150 DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
151 (state->src.x1 >> 16) & ~(format->hsub - 1),
152 (state->src.y1 >> 16) & ~(format->vsub - 1));
153 DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
154 regmap_write(mixer->engine.regs,
155 SUN8I_MIXER_CHAN_VI_LAYER_SIZE(ch_base, overlay),
157 regmap_write(mixer->engine.regs,
158 SUN8I_MIXER_CHAN_VI_OVL_SIZE(ch_base),
162 * Scaler must be enabled for subsampled formats, so it scales
163 * chroma to same size as luma.
165 subsampled = format->hsub > 1 || format->vsub > 1;
167 if (insize != outsize || subsampled || hphase || vphase) {
168 unsigned int scanline, required;
169 struct drm_display_mode *mode;
170 u32 hscale, vscale, fps;
173 DRM_DEBUG_DRIVER("HW scaling is enabled\n");
175 mode = &plane->state->crtc->state->mode;
176 fps = (mode->clock * 1000) / (mode->vtotal * mode->htotal);
177 ability = clk_get_rate(mixer->mod_clk);
178 /* BSP algorithm assumes 80% efficiency of VI scaler unit */
180 do_div(ability, mode->vdisplay * fps * max(src_w, dst_w));
182 required = src_h * 100 / dst_h;
184 if (ability < required) {
185 DRM_DEBUG_DRIVER("Using vertical coarse scaling\n");
187 vn = (u32)ability * dst_h / 100;
191 /* it seems that every RGB scaler has buffer for 2048 pixels */
192 scanline = subsampled ? mixer->cfg->scanline_yuv : 2048;
194 if (src_w > scanline) {
195 DRM_DEBUG_DRIVER("Using horizontal coarse scaling\n");
201 hscale = (src_w << 16) / dst_w;
202 vscale = (src_h << 16) / dst_h;
204 sun8i_vi_scaler_setup(mixer, channel, src_w, src_h, dst_w,
205 dst_h, hscale, vscale, hphase, vphase,
207 sun8i_vi_scaler_enable(mixer, channel, true);
209 DRM_DEBUG_DRIVER("HW scaling is not needed\n");
210 sun8i_vi_scaler_enable(mixer, channel, false);
213 regmap_write(mixer->engine.regs,
214 SUN8I_MIXER_CHAN_VI_HDS_Y(ch_base),
215 SUN8I_MIXER_CHAN_VI_DS_N(hn) |
216 SUN8I_MIXER_CHAN_VI_DS_M(hm));
217 regmap_write(mixer->engine.regs,
218 SUN8I_MIXER_CHAN_VI_HDS_UV(ch_base),
219 SUN8I_MIXER_CHAN_VI_DS_N(hn) |
220 SUN8I_MIXER_CHAN_VI_DS_M(hm));
221 regmap_write(mixer->engine.regs,
222 SUN8I_MIXER_CHAN_VI_VDS_Y(ch_base),
223 SUN8I_MIXER_CHAN_VI_DS_N(vn) |
224 SUN8I_MIXER_CHAN_VI_DS_M(vm));
225 regmap_write(mixer->engine.regs,
226 SUN8I_MIXER_CHAN_VI_VDS_UV(ch_base),
227 SUN8I_MIXER_CHAN_VI_DS_N(vn) |
228 SUN8I_MIXER_CHAN_VI_DS_M(vm));
230 /* Set base coordinates */
231 DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
232 state->dst.x1, state->dst.y1);
233 DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
234 regmap_write(mixer->engine.regs,
235 SUN8I_MIXER_BLEND_ATTR_COORD(bld_base, zpos),
236 SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
237 regmap_write(mixer->engine.regs,
238 SUN8I_MIXER_BLEND_ATTR_INSIZE(bld_base, zpos),
244 static u32 sun8i_vi_layer_get_csc_mode(const struct drm_format_info *format)
247 return SUN8I_CSC_MODE_OFF;
249 switch (format->format) {
250 case DRM_FORMAT_YVU411:
251 case DRM_FORMAT_YVU420:
252 case DRM_FORMAT_YVU422:
253 case DRM_FORMAT_YVU444:
254 return SUN8I_CSC_MODE_YVU2RGB;
256 return SUN8I_CSC_MODE_YUV2RGB;
260 static int sun8i_vi_layer_update_formats(struct sun8i_mixer *mixer, int channel,
261 int overlay, struct drm_plane *plane)
263 struct drm_plane_state *state = plane->state;
264 u32 val, ch_base, csc_mode, hw_fmt;
265 const struct drm_format_info *fmt;
268 ch_base = sun8i_channel_base(mixer, channel);
270 fmt = state->fb->format;
271 ret = sun8i_mixer_drm_format_to_hw(fmt->format, &hw_fmt);
273 DRM_DEBUG_DRIVER("Invalid format\n");
277 val = hw_fmt << SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_OFFSET;
278 regmap_update_bits(mixer->engine.regs,
279 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay),
280 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_MASK, val);
282 csc_mode = sun8i_vi_layer_get_csc_mode(fmt);
283 if (csc_mode != SUN8I_CSC_MODE_OFF) {
284 sun8i_csc_set_ccsc_coefficients(mixer, channel, csc_mode,
285 state->color_encoding,
287 sun8i_csc_enable_ccsc(mixer, channel, true);
289 sun8i_csc_enable_ccsc(mixer, channel, false);
293 val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE;
297 regmap_update_bits(mixer->engine.regs,
298 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay),
299 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE, val);
304 static int sun8i_vi_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
305 int overlay, struct drm_plane *plane)
307 struct drm_plane_state *state = plane->state;
308 struct drm_framebuffer *fb = state->fb;
309 const struct drm_format_info *format = fb->format;
310 struct drm_gem_cma_object *gem;
311 u32 dx, dy, src_x, src_y;
316 ch_base = sun8i_channel_base(mixer, channel);
318 /* Adjust x and y to be dividable by subsampling factor */
319 src_x = (state->src.x1 >> 16) & ~(format->hsub - 1);
320 src_y = (state->src.y1 >> 16) & ~(format->vsub - 1);
322 for (i = 0; i < format->num_planes; i++) {
323 /* Get the physical address of the buffer in memory */
324 gem = drm_fb_cma_get_gem_obj(fb, i);
326 DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->paddr);
328 /* Compute the start of the displayed memory */
329 paddr = gem->paddr + fb->offsets[i];
339 /* Fixup framebuffer address for src coordinates */
340 paddr += dx * format->cpp[i];
341 paddr += dy * fb->pitches[i];
343 /* Set the line width */
344 DRM_DEBUG_DRIVER("Layer %d. line width: %d bytes\n",
345 i + 1, fb->pitches[i]);
346 regmap_write(mixer->engine.regs,
347 SUN8I_MIXER_CHAN_VI_LAYER_PITCH(ch_base,
351 DRM_DEBUG_DRIVER("Setting %d. buffer address to %pad\n",
354 regmap_write(mixer->engine.regs,
355 SUN8I_MIXER_CHAN_VI_LAYER_TOP_LADDR(ch_base,
357 lower_32_bits(paddr));
363 static int sun8i_vi_layer_atomic_check(struct drm_plane *plane,
364 struct drm_atomic_state *state)
366 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
368 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
369 struct drm_crtc *crtc = new_plane_state->crtc;
370 struct drm_crtc_state *crtc_state;
371 int min_scale, max_scale;
376 crtc_state = drm_atomic_get_existing_crtc_state(state,
378 if (WARN_ON(!crtc_state))
381 min_scale = DRM_PLANE_HELPER_NO_SCALING;
382 max_scale = DRM_PLANE_HELPER_NO_SCALING;
384 if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
385 min_scale = SUN8I_VI_SCALER_SCALE_MIN;
386 max_scale = SUN8I_VI_SCALER_SCALE_MAX;
389 return drm_atomic_helper_check_plane_state(new_plane_state,
391 min_scale, max_scale,
395 static void sun8i_vi_layer_atomic_disable(struct drm_plane *plane,
396 struct drm_atomic_state *state)
398 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
400 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
401 unsigned int old_zpos = old_state->normalized_zpos;
402 struct sun8i_mixer *mixer = layer->mixer;
404 sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, false, 0,
408 static void sun8i_vi_layer_atomic_update(struct drm_plane *plane,
409 struct drm_atomic_state *state)
411 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
413 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
415 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
416 unsigned int zpos = new_state->normalized_zpos;
417 unsigned int old_zpos = old_state->normalized_zpos;
418 struct sun8i_mixer *mixer = layer->mixer;
420 if (!new_state->visible) {
421 sun8i_vi_layer_enable(mixer, layer->channel,
422 layer->overlay, false, 0, old_zpos);
426 sun8i_vi_layer_update_coord(mixer, layer->channel,
427 layer->overlay, plane, zpos);
428 sun8i_vi_layer_update_alpha(mixer, layer->channel,
429 layer->overlay, plane);
430 sun8i_vi_layer_update_formats(mixer, layer->channel,
431 layer->overlay, plane);
432 sun8i_vi_layer_update_buffer(mixer, layer->channel,
433 layer->overlay, plane);
434 sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay,
435 true, zpos, old_zpos);
438 static const struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs = {
439 .prepare_fb = drm_gem_plane_helper_prepare_fb,
440 .atomic_check = sun8i_vi_layer_atomic_check,
441 .atomic_disable = sun8i_vi_layer_atomic_disable,
442 .atomic_update = sun8i_vi_layer_atomic_update,
445 static const struct drm_plane_funcs sun8i_vi_layer_funcs = {
446 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
447 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
448 .destroy = drm_plane_cleanup,
449 .disable_plane = drm_atomic_helper_disable_plane,
450 .reset = drm_atomic_helper_plane_reset,
451 .update_plane = drm_atomic_helper_update_plane,
455 * While DE2 VI layer supports same RGB formats as UI layer, alpha
456 * channel is ignored. This structure lists all unique variants
457 * where alpha channel is replaced with "don't care" (X) channel.
459 static const u32 sun8i_vi_layer_formats[] = {
493 static const u32 sun8i_vi_layer_de3_formats[] = {
495 DRM_FORMAT_ABGR2101010,
499 DRM_FORMAT_ARGB2101010,
504 DRM_FORMAT_BGRA1010102,
511 DRM_FORMAT_RGBA1010102,
537 struct sun8i_vi_layer *sun8i_vi_layer_init_one(struct drm_device *drm,
538 struct sun8i_mixer *mixer,
541 u32 supported_encodings, supported_ranges;
542 unsigned int plane_cnt, format_count;
543 struct sun8i_vi_layer *layer;
547 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
549 return ERR_PTR(-ENOMEM);
551 if (mixer->cfg->is_de3) {
552 formats = sun8i_vi_layer_de3_formats;
553 format_count = ARRAY_SIZE(sun8i_vi_layer_de3_formats);
555 formats = sun8i_vi_layer_formats;
556 format_count = ARRAY_SIZE(sun8i_vi_layer_formats);
559 /* possible crtcs are set later */
560 ret = drm_universal_plane_init(drm, &layer->plane, 0,
561 &sun8i_vi_layer_funcs,
562 formats, format_count,
563 NULL, DRM_PLANE_TYPE_OVERLAY, NULL);
565 dev_err(drm->dev, "Couldn't initialize layer\n");
569 plane_cnt = mixer->cfg->ui_num + mixer->cfg->vi_num;
571 if (mixer->cfg->vi_num == 1 || mixer->cfg->is_de3) {
572 ret = drm_plane_create_alpha_property(&layer->plane);
574 dev_err(drm->dev, "Couldn't add alpha property\n");
579 ret = drm_plane_create_zpos_property(&layer->plane, index,
582 dev_err(drm->dev, "Couldn't add zpos property\n");
586 supported_encodings = BIT(DRM_COLOR_YCBCR_BT601) |
587 BIT(DRM_COLOR_YCBCR_BT709);
588 if (mixer->cfg->is_de3)
589 supported_encodings |= BIT(DRM_COLOR_YCBCR_BT2020);
591 supported_ranges = BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
592 BIT(DRM_COLOR_YCBCR_FULL_RANGE);
594 ret = drm_plane_create_color_properties(&layer->plane,
597 DRM_COLOR_YCBCR_BT709,
598 DRM_COLOR_YCBCR_LIMITED_RANGE);
600 dev_err(drm->dev, "Couldn't add encoding and range properties!\n");
604 drm_plane_helper_add(&layer->plane, &sun8i_vi_layer_helper_funcs);
605 layer->mixer = mixer;
606 layer->channel = index;