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_blend.h>
9 #include <drm/drm_crtc.h>
10 #include <drm/drm_fb_cma_helper.h>
11 #include <drm/drm_framebuffer.h>
12 #include <drm/drm_gem_atomic_helper.h>
13 #include <drm/drm_gem_cma_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_probe_helper.h>
17 #include "sun8i_csc.h"
18 #include "sun8i_mixer.h"
19 #include "sun8i_vi_layer.h"
20 #include "sun8i_vi_scaler.h"
22 static void sun8i_vi_layer_enable(struct sun8i_mixer *mixer, int channel,
23 int overlay, bool enable, unsigned int zpos,
24 unsigned int old_zpos)
26 u32 val, bld_base, ch_base;
28 bld_base = sun8i_blender_base(mixer);
29 ch_base = sun8i_channel_base(mixer, channel);
31 DRM_DEBUG_DRIVER("%sabling VI channel %d overlay %d\n",
32 enable ? "En" : "Dis", channel, overlay);
35 val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN;
39 regmap_update_bits(mixer->engine.regs,
40 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay),
41 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN, val);
43 if (!enable || zpos != old_zpos) {
44 regmap_update_bits(mixer->engine.regs,
45 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
46 SUN8I_MIXER_BLEND_PIPE_CTL_EN(old_zpos),
49 regmap_update_bits(mixer->engine.regs,
50 SUN8I_MIXER_BLEND_ROUTE(bld_base),
51 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(old_zpos),
56 val = SUN8I_MIXER_BLEND_PIPE_CTL_EN(zpos);
58 regmap_update_bits(mixer->engine.regs,
59 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base),
62 val = channel << SUN8I_MIXER_BLEND_ROUTE_PIPE_SHIFT(zpos);
64 regmap_update_bits(mixer->engine.regs,
65 SUN8I_MIXER_BLEND_ROUTE(bld_base),
66 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(zpos),
71 static void sun8i_vi_layer_update_alpha(struct sun8i_mixer *mixer, int channel,
72 int overlay, struct drm_plane *plane)
74 u32 mask, val, ch_base;
76 ch_base = sun8i_channel_base(mixer, channel);
78 if (mixer->cfg->is_de3) {
79 mask = SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MASK |
80 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_MASK;
81 val = SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA
82 (plane->state->alpha >> 8);
84 val |= (plane->state->alpha == DRM_BLEND_ALPHA_OPAQUE) ?
85 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_PIXEL :
86 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_COMBINED;
88 regmap_update_bits(mixer->engine.regs,
89 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base,
92 } else if (mixer->cfg->vi_num == 1) {
93 regmap_update_bits(mixer->engine.regs,
94 SUN8I_MIXER_FCC_GLOBAL_ALPHA_REG,
95 SUN8I_MIXER_FCC_GLOBAL_ALPHA_MASK,
96 SUN8I_MIXER_FCC_GLOBAL_ALPHA
97 (plane->state->alpha >> 8));
101 static int sun8i_vi_layer_update_coord(struct sun8i_mixer *mixer, int channel,
102 int overlay, struct drm_plane *plane,
105 struct drm_plane_state *state = plane->state;
106 const struct drm_format_info *format = state->fb->format;
107 u32 src_w, src_h, dst_w, dst_h;
108 u32 bld_base, ch_base;
115 DRM_DEBUG_DRIVER("Updating VI channel %d overlay %d\n",
118 bld_base = sun8i_blender_base(mixer);
119 ch_base = sun8i_channel_base(mixer, channel);
121 src_w = drm_rect_width(&state->src) >> 16;
122 src_h = drm_rect_height(&state->src) >> 16;
123 dst_w = drm_rect_width(&state->dst);
124 dst_h = drm_rect_height(&state->dst);
126 hphase = state->src.x1 & 0xffff;
127 vphase = state->src.y1 & 0xffff;
129 /* make coordinates dividable by subsampling factor */
130 if (format->hsub > 1) {
133 mask = format->hsub - 1;
134 remainder = (state->src.x1 >> 16) & mask;
135 src_w = (src_w + remainder) & ~mask;
136 hphase += remainder << 16;
139 if (format->vsub > 1) {
142 mask = format->vsub - 1;
143 remainder = (state->src.y1 >> 16) & mask;
144 src_h = (src_h + remainder) & ~mask;
145 vphase += remainder << 16;
148 insize = SUN8I_MIXER_SIZE(src_w, src_h);
149 outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
151 /* Set height and width */
152 DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
153 (state->src.x1 >> 16) & ~(format->hsub - 1),
154 (state->src.y1 >> 16) & ~(format->vsub - 1));
155 DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
156 regmap_write(mixer->engine.regs,
157 SUN8I_MIXER_CHAN_VI_LAYER_SIZE(ch_base, overlay),
159 regmap_write(mixer->engine.regs,
160 SUN8I_MIXER_CHAN_VI_OVL_SIZE(ch_base),
164 * Scaler must be enabled for subsampled formats, so it scales
165 * chroma to same size as luma.
167 subsampled = format->hsub > 1 || format->vsub > 1;
169 if (insize != outsize || subsampled || hphase || vphase) {
170 unsigned int scanline, required;
171 struct drm_display_mode *mode;
172 u32 hscale, vscale, fps;
175 DRM_DEBUG_DRIVER("HW scaling is enabled\n");
177 mode = &plane->state->crtc->state->mode;
178 fps = (mode->clock * 1000) / (mode->vtotal * mode->htotal);
179 ability = clk_get_rate(mixer->mod_clk);
180 /* BSP algorithm assumes 80% efficiency of VI scaler unit */
182 do_div(ability, mode->vdisplay * fps * max(src_w, dst_w));
184 required = src_h * 100 / dst_h;
186 if (ability < required) {
187 DRM_DEBUG_DRIVER("Using vertical coarse scaling\n");
189 vn = (u32)ability * dst_h / 100;
193 /* it seems that every RGB scaler has buffer for 2048 pixels */
194 scanline = subsampled ? mixer->cfg->scanline_yuv : 2048;
196 if (src_w > scanline) {
197 DRM_DEBUG_DRIVER("Using horizontal coarse scaling\n");
203 hscale = (src_w << 16) / dst_w;
204 vscale = (src_h << 16) / dst_h;
206 sun8i_vi_scaler_setup(mixer, channel, src_w, src_h, dst_w,
207 dst_h, hscale, vscale, hphase, vphase,
209 sun8i_vi_scaler_enable(mixer, channel, true);
211 DRM_DEBUG_DRIVER("HW scaling is not needed\n");
212 sun8i_vi_scaler_enable(mixer, channel, false);
215 regmap_write(mixer->engine.regs,
216 SUN8I_MIXER_CHAN_VI_HDS_Y(ch_base),
217 SUN8I_MIXER_CHAN_VI_DS_N(hn) |
218 SUN8I_MIXER_CHAN_VI_DS_M(hm));
219 regmap_write(mixer->engine.regs,
220 SUN8I_MIXER_CHAN_VI_HDS_UV(ch_base),
221 SUN8I_MIXER_CHAN_VI_DS_N(hn) |
222 SUN8I_MIXER_CHAN_VI_DS_M(hm));
223 regmap_write(mixer->engine.regs,
224 SUN8I_MIXER_CHAN_VI_VDS_Y(ch_base),
225 SUN8I_MIXER_CHAN_VI_DS_N(vn) |
226 SUN8I_MIXER_CHAN_VI_DS_M(vm));
227 regmap_write(mixer->engine.regs,
228 SUN8I_MIXER_CHAN_VI_VDS_UV(ch_base),
229 SUN8I_MIXER_CHAN_VI_DS_N(vn) |
230 SUN8I_MIXER_CHAN_VI_DS_M(vm));
232 /* Set base coordinates */
233 DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
234 state->dst.x1, state->dst.y1);
235 DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
236 regmap_write(mixer->engine.regs,
237 SUN8I_MIXER_BLEND_ATTR_COORD(bld_base, zpos),
238 SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
239 regmap_write(mixer->engine.regs,
240 SUN8I_MIXER_BLEND_ATTR_INSIZE(bld_base, zpos),
246 static u32 sun8i_vi_layer_get_csc_mode(const struct drm_format_info *format)
249 return SUN8I_CSC_MODE_OFF;
251 switch (format->format) {
252 case DRM_FORMAT_YVU411:
253 case DRM_FORMAT_YVU420:
254 case DRM_FORMAT_YVU422:
255 case DRM_FORMAT_YVU444:
256 return SUN8I_CSC_MODE_YVU2RGB;
258 return SUN8I_CSC_MODE_YUV2RGB;
262 static int sun8i_vi_layer_update_formats(struct sun8i_mixer *mixer, int channel,
263 int overlay, struct drm_plane *plane)
265 struct drm_plane_state *state = plane->state;
266 u32 val, ch_base, csc_mode, hw_fmt;
267 const struct drm_format_info *fmt;
270 ch_base = sun8i_channel_base(mixer, channel);
272 fmt = state->fb->format;
273 ret = sun8i_mixer_drm_format_to_hw(fmt->format, &hw_fmt);
275 DRM_DEBUG_DRIVER("Invalid format\n");
279 val = hw_fmt << SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_OFFSET;
280 regmap_update_bits(mixer->engine.regs,
281 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay),
282 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_MASK, val);
284 csc_mode = sun8i_vi_layer_get_csc_mode(fmt);
285 if (csc_mode != SUN8I_CSC_MODE_OFF) {
286 sun8i_csc_set_ccsc_coefficients(mixer, channel, csc_mode,
287 state->color_encoding,
289 sun8i_csc_enable_ccsc(mixer, channel, true);
291 sun8i_csc_enable_ccsc(mixer, channel, false);
295 val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE;
299 regmap_update_bits(mixer->engine.regs,
300 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay),
301 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE, val);
306 static int sun8i_vi_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
307 int overlay, struct drm_plane *plane)
309 struct drm_plane_state *state = plane->state;
310 struct drm_framebuffer *fb = state->fb;
311 const struct drm_format_info *format = fb->format;
312 struct drm_gem_cma_object *gem;
313 u32 dx, dy, src_x, src_y;
318 ch_base = sun8i_channel_base(mixer, channel);
320 /* Adjust x and y to be dividable by subsampling factor */
321 src_x = (state->src.x1 >> 16) & ~(format->hsub - 1);
322 src_y = (state->src.y1 >> 16) & ~(format->vsub - 1);
324 for (i = 0; i < format->num_planes; i++) {
325 /* Get the physical address of the buffer in memory */
326 gem = drm_fb_cma_get_gem_obj(fb, i);
328 DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->paddr);
330 /* Compute the start of the displayed memory */
331 paddr = gem->paddr + fb->offsets[i];
341 /* Fixup framebuffer address for src coordinates */
342 paddr += dx * format->cpp[i];
343 paddr += dy * fb->pitches[i];
345 /* Set the line width */
346 DRM_DEBUG_DRIVER("Layer %d. line width: %d bytes\n",
347 i + 1, fb->pitches[i]);
348 regmap_write(mixer->engine.regs,
349 SUN8I_MIXER_CHAN_VI_LAYER_PITCH(ch_base,
353 DRM_DEBUG_DRIVER("Setting %d. buffer address to %pad\n",
356 regmap_write(mixer->engine.regs,
357 SUN8I_MIXER_CHAN_VI_LAYER_TOP_LADDR(ch_base,
359 lower_32_bits(paddr));
365 static int sun8i_vi_layer_atomic_check(struct drm_plane *plane,
366 struct drm_atomic_state *state)
368 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
370 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
371 struct drm_crtc *crtc = new_plane_state->crtc;
372 struct drm_crtc_state *crtc_state;
373 int min_scale, max_scale;
378 crtc_state = drm_atomic_get_existing_crtc_state(state,
380 if (WARN_ON(!crtc_state))
383 min_scale = DRM_PLANE_HELPER_NO_SCALING;
384 max_scale = DRM_PLANE_HELPER_NO_SCALING;
386 if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
387 min_scale = SUN8I_VI_SCALER_SCALE_MIN;
388 max_scale = SUN8I_VI_SCALER_SCALE_MAX;
391 return drm_atomic_helper_check_plane_state(new_plane_state,
393 min_scale, max_scale,
397 static void sun8i_vi_layer_atomic_disable(struct drm_plane *plane,
398 struct drm_atomic_state *state)
400 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
402 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
403 unsigned int old_zpos = old_state->normalized_zpos;
404 struct sun8i_mixer *mixer = layer->mixer;
406 sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, false, 0,
410 static void sun8i_vi_layer_atomic_update(struct drm_plane *plane,
411 struct drm_atomic_state *state)
413 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
415 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
417 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
418 unsigned int zpos = new_state->normalized_zpos;
419 unsigned int old_zpos = old_state->normalized_zpos;
420 struct sun8i_mixer *mixer = layer->mixer;
422 if (!new_state->visible) {
423 sun8i_vi_layer_enable(mixer, layer->channel,
424 layer->overlay, false, 0, old_zpos);
428 sun8i_vi_layer_update_coord(mixer, layer->channel,
429 layer->overlay, plane, zpos);
430 sun8i_vi_layer_update_alpha(mixer, layer->channel,
431 layer->overlay, plane);
432 sun8i_vi_layer_update_formats(mixer, layer->channel,
433 layer->overlay, plane);
434 sun8i_vi_layer_update_buffer(mixer, layer->channel,
435 layer->overlay, plane);
436 sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay,
437 true, zpos, old_zpos);
440 static const struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs = {
441 .atomic_check = sun8i_vi_layer_atomic_check,
442 .atomic_disable = sun8i_vi_layer_atomic_disable,
443 .atomic_update = sun8i_vi_layer_atomic_update,
446 static const struct drm_plane_funcs sun8i_vi_layer_funcs = {
447 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
448 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
449 .destroy = drm_plane_cleanup,
450 .disable_plane = drm_atomic_helper_disable_plane,
451 .reset = drm_atomic_helper_plane_reset,
452 .update_plane = drm_atomic_helper_update_plane,
456 * While DE2 VI layer supports same RGB formats as UI layer, alpha
457 * channel is ignored. This structure lists all unique variants
458 * where alpha channel is replaced with "don't care" (X) channel.
460 static const u32 sun8i_vi_layer_formats[] = {
494 static const u32 sun8i_vi_layer_de3_formats[] = {
496 DRM_FORMAT_ABGR2101010,
500 DRM_FORMAT_ARGB2101010,
505 DRM_FORMAT_BGRA1010102,
512 DRM_FORMAT_RGBA1010102,
538 static const uint64_t sun8i_layer_modifiers[] = {
539 DRM_FORMAT_MOD_LINEAR,
540 DRM_FORMAT_MOD_INVALID
543 struct sun8i_vi_layer *sun8i_vi_layer_init_one(struct drm_device *drm,
544 struct sun8i_mixer *mixer,
547 enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY;
548 u32 supported_encodings, supported_ranges;
549 unsigned int plane_cnt, format_count;
550 struct sun8i_vi_layer *layer;
554 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
556 return ERR_PTR(-ENOMEM);
558 if (mixer->cfg->is_de3) {
559 formats = sun8i_vi_layer_de3_formats;
560 format_count = ARRAY_SIZE(sun8i_vi_layer_de3_formats);
562 formats = sun8i_vi_layer_formats;
563 format_count = ARRAY_SIZE(sun8i_vi_layer_formats);
566 if (!mixer->cfg->ui_num && index == 0)
567 type = DRM_PLANE_TYPE_PRIMARY;
569 /* possible crtcs are set later */
570 ret = drm_universal_plane_init(drm, &layer->plane, 0,
571 &sun8i_vi_layer_funcs,
572 formats, format_count,
573 sun8i_layer_modifiers,
576 dev_err(drm->dev, "Couldn't initialize layer\n");
580 plane_cnt = mixer->cfg->ui_num + mixer->cfg->vi_num;
582 if (mixer->cfg->vi_num == 1 || mixer->cfg->is_de3) {
583 ret = drm_plane_create_alpha_property(&layer->plane);
585 dev_err(drm->dev, "Couldn't add alpha property\n");
590 ret = drm_plane_create_zpos_property(&layer->plane, index,
593 dev_err(drm->dev, "Couldn't add zpos property\n");
597 supported_encodings = BIT(DRM_COLOR_YCBCR_BT601) |
598 BIT(DRM_COLOR_YCBCR_BT709);
599 if (mixer->cfg->is_de3)
600 supported_encodings |= BIT(DRM_COLOR_YCBCR_BT2020);
602 supported_ranges = BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
603 BIT(DRM_COLOR_YCBCR_FULL_RANGE);
605 ret = drm_plane_create_color_properties(&layer->plane,
608 DRM_COLOR_YCBCR_BT709,
609 DRM_COLOR_YCBCR_LIMITED_RANGE);
611 dev_err(drm->dev, "Couldn't add encoding and range properties!\n");
615 drm_plane_helper_add(&layer->plane, &sun8i_vi_layer_helper_funcs);
616 layer->mixer = mixer;
617 layer->channel = index;