1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) Jernej Skrabec <jernej.skrabec@siol.net> 4 */ 5 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_dma_helper.h> 11 #include <drm/drm_framebuffer.h> 12 #include <drm/drm_gem_atomic_helper.h> 13 #include <drm/drm_gem_dma_helper.h> 14 #include <drm/drm_print.h> 15 #include <drm/drm_probe_helper.h> 16 17 #include "sun8i_csc.h" 18 #include "sun8i_mixer.h" 19 #include "sun8i_vi_layer.h" 20 #include "sun8i_vi_scaler.h" 21 22 static void sun8i_vi_layer_update_alpha(struct sun8i_mixer *mixer, int channel, 23 int overlay, struct drm_plane *plane) 24 { 25 u32 mask, val, ch_base; 26 27 ch_base = sun8i_channel_base(mixer, channel); 28 29 if (mixer->cfg->de_type >= SUN8I_MIXER_DE3) { 30 mask = SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MASK | 31 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_MASK; 32 val = SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA 33 (plane->state->alpha >> 8); 34 35 val |= (plane->state->alpha == DRM_BLEND_ALPHA_OPAQUE) ? 36 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_PIXEL : 37 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MODE_COMBINED; 38 39 regmap_update_bits(mixer->engine.regs, 40 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, 41 overlay), 42 mask, val); 43 } else if (mixer->cfg->vi_num == 1) { 44 regmap_update_bits(mixer->engine.regs, 45 SUN8I_MIXER_FCC_GLOBAL_ALPHA_REG, 46 SUN8I_MIXER_FCC_GLOBAL_ALPHA_MASK, 47 SUN8I_MIXER_FCC_GLOBAL_ALPHA 48 (plane->state->alpha >> 8)); 49 } 50 } 51 52 static int sun8i_vi_layer_update_coord(struct sun8i_mixer *mixer, int channel, 53 int overlay, struct drm_plane *plane, 54 unsigned int zpos) 55 { 56 struct drm_plane_state *state = plane->state; 57 const struct drm_format_info *format = state->fb->format; 58 u32 src_w, src_h, dst_w, dst_h; 59 struct regmap *bld_regs; 60 u32 bld_base, ch_base; 61 u32 outsize, insize; 62 u32 hphase, vphase; 63 u32 hn = 0, hm = 0; 64 u32 vn = 0, vm = 0; 65 bool subsampled; 66 67 DRM_DEBUG_DRIVER("Updating VI channel %d overlay %d\n", 68 channel, overlay); 69 70 bld_base = sun8i_blender_base(mixer); 71 bld_regs = sun8i_blender_regmap(mixer); 72 ch_base = sun8i_channel_base(mixer, channel); 73 74 src_w = drm_rect_width(&state->src) >> 16; 75 src_h = drm_rect_height(&state->src) >> 16; 76 dst_w = drm_rect_width(&state->dst); 77 dst_h = drm_rect_height(&state->dst); 78 79 hphase = state->src.x1 & 0xffff; 80 vphase = state->src.y1 & 0xffff; 81 82 /* make coordinates dividable by subsampling factor */ 83 if (format->hsub > 1) { 84 int mask, remainder; 85 86 mask = format->hsub - 1; 87 remainder = (state->src.x1 >> 16) & mask; 88 src_w = (src_w + remainder) & ~mask; 89 hphase += remainder << 16; 90 } 91 92 if (format->vsub > 1) { 93 int mask, remainder; 94 95 mask = format->vsub - 1; 96 remainder = (state->src.y1 >> 16) & mask; 97 src_h = (src_h + remainder) & ~mask; 98 vphase += remainder << 16; 99 } 100 101 insize = SUN8I_MIXER_SIZE(src_w, src_h); 102 outsize = SUN8I_MIXER_SIZE(dst_w, dst_h); 103 104 /* Set height and width */ 105 DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n", 106 (state->src.x1 >> 16) & ~(format->hsub - 1), 107 (state->src.y1 >> 16) & ~(format->vsub - 1)); 108 DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h); 109 regmap_write(mixer->engine.regs, 110 SUN8I_MIXER_CHAN_VI_LAYER_SIZE(ch_base, overlay), 111 insize); 112 regmap_write(mixer->engine.regs, 113 SUN8I_MIXER_CHAN_VI_OVL_SIZE(ch_base), 114 insize); 115 116 /* 117 * Scaler must be enabled for subsampled formats, so it scales 118 * chroma to same size as luma. 119 */ 120 subsampled = format->hsub > 1 || format->vsub > 1; 121 122 if (insize != outsize || subsampled || hphase || vphase) { 123 unsigned int scanline, required; 124 struct drm_display_mode *mode; 125 u32 hscale, vscale, fps; 126 u64 ability; 127 128 DRM_DEBUG_DRIVER("HW scaling is enabled\n"); 129 130 mode = &plane->state->crtc->state->mode; 131 fps = (mode->clock * 1000) / (mode->vtotal * mode->htotal); 132 ability = clk_get_rate(mixer->mod_clk); 133 /* BSP algorithm assumes 80% efficiency of VI scaler unit */ 134 ability *= 80; 135 do_div(ability, mode->vdisplay * fps * max(src_w, dst_w)); 136 137 required = src_h * 100 / dst_h; 138 139 if (ability < required) { 140 DRM_DEBUG_DRIVER("Using vertical coarse scaling\n"); 141 vm = src_h; 142 vn = (u32)ability * dst_h / 100; 143 src_h = vn; 144 } 145 146 /* it seems that every RGB scaler has buffer for 2048 pixels */ 147 scanline = subsampled ? mixer->cfg->scanline_yuv : 2048; 148 149 if (src_w > scanline) { 150 DRM_DEBUG_DRIVER("Using horizontal coarse scaling\n"); 151 hm = src_w; 152 hn = scanline; 153 src_w = hn; 154 } 155 156 hscale = (src_w << 16) / dst_w; 157 vscale = (src_h << 16) / dst_h; 158 159 sun8i_vi_scaler_setup(mixer, channel, src_w, src_h, dst_w, 160 dst_h, hscale, vscale, hphase, vphase, 161 format); 162 sun8i_vi_scaler_enable(mixer, channel, true); 163 } else { 164 DRM_DEBUG_DRIVER("HW scaling is not needed\n"); 165 sun8i_vi_scaler_enable(mixer, channel, false); 166 } 167 168 regmap_write(mixer->engine.regs, 169 SUN8I_MIXER_CHAN_VI_HDS_Y(ch_base), 170 SUN8I_MIXER_CHAN_VI_DS_N(hn) | 171 SUN8I_MIXER_CHAN_VI_DS_M(hm)); 172 regmap_write(mixer->engine.regs, 173 SUN8I_MIXER_CHAN_VI_HDS_UV(ch_base), 174 SUN8I_MIXER_CHAN_VI_DS_N(hn) | 175 SUN8I_MIXER_CHAN_VI_DS_M(hm)); 176 regmap_write(mixer->engine.regs, 177 SUN8I_MIXER_CHAN_VI_VDS_Y(ch_base), 178 SUN8I_MIXER_CHAN_VI_DS_N(vn) | 179 SUN8I_MIXER_CHAN_VI_DS_M(vm)); 180 regmap_write(mixer->engine.regs, 181 SUN8I_MIXER_CHAN_VI_VDS_UV(ch_base), 182 SUN8I_MIXER_CHAN_VI_DS_N(vn) | 183 SUN8I_MIXER_CHAN_VI_DS_M(vm)); 184 185 /* Set base coordinates */ 186 DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n", 187 state->dst.x1, state->dst.y1); 188 DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h); 189 regmap_write(bld_regs, 190 SUN8I_MIXER_BLEND_ATTR_COORD(bld_base, zpos), 191 SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1)); 192 regmap_write(bld_regs, 193 SUN8I_MIXER_BLEND_ATTR_INSIZE(bld_base, zpos), 194 outsize); 195 196 return 0; 197 } 198 199 static u32 sun8i_vi_layer_get_csc_mode(const struct drm_format_info *format) 200 { 201 if (!format->is_yuv) 202 return SUN8I_CSC_MODE_OFF; 203 204 switch (format->format) { 205 case DRM_FORMAT_YVU411: 206 case DRM_FORMAT_YVU420: 207 case DRM_FORMAT_YVU422: 208 case DRM_FORMAT_YVU444: 209 return SUN8I_CSC_MODE_YVU2RGB; 210 default: 211 return SUN8I_CSC_MODE_YUV2RGB; 212 } 213 } 214 215 static int sun8i_vi_layer_update_formats(struct sun8i_mixer *mixer, int channel, 216 int overlay, struct drm_plane *plane) 217 { 218 struct drm_plane_state *state = plane->state; 219 u32 val, ch_base, csc_mode, hw_fmt; 220 const struct drm_format_info *fmt; 221 int ret; 222 223 ch_base = sun8i_channel_base(mixer, channel); 224 225 fmt = state->fb->format; 226 ret = sun8i_mixer_drm_format_to_hw(fmt->format, &hw_fmt); 227 if (ret) { 228 DRM_DEBUG_DRIVER("Invalid format\n"); 229 return ret; 230 } 231 232 val = hw_fmt << SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_OFFSET; 233 regmap_update_bits(mixer->engine.regs, 234 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay), 235 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_MASK, val); 236 237 csc_mode = sun8i_vi_layer_get_csc_mode(fmt); 238 if (csc_mode != SUN8I_CSC_MODE_OFF) { 239 sun8i_csc_set_ccsc_coefficients(mixer, channel, csc_mode, 240 state->color_encoding, 241 state->color_range); 242 sun8i_csc_enable_ccsc(mixer, channel, true); 243 } else { 244 sun8i_csc_enable_ccsc(mixer, channel, false); 245 } 246 247 if (!fmt->is_yuv) 248 val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE; 249 else 250 val = 0; 251 252 regmap_update_bits(mixer->engine.regs, 253 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base, overlay), 254 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE, val); 255 256 return 0; 257 } 258 259 static int sun8i_vi_layer_update_buffer(struct sun8i_mixer *mixer, int channel, 260 int overlay, struct drm_plane *plane) 261 { 262 struct drm_plane_state *state = plane->state; 263 struct drm_framebuffer *fb = state->fb; 264 const struct drm_format_info *format = fb->format; 265 struct drm_gem_dma_object *gem; 266 u32 dx, dy, src_x, src_y; 267 dma_addr_t dma_addr; 268 u32 ch_base; 269 int i; 270 271 ch_base = sun8i_channel_base(mixer, channel); 272 273 /* Adjust x and y to be dividable by subsampling factor */ 274 src_x = (state->src.x1 >> 16) & ~(format->hsub - 1); 275 src_y = (state->src.y1 >> 16) & ~(format->vsub - 1); 276 277 for (i = 0; i < format->num_planes; i++) { 278 /* Get the physical address of the buffer in memory */ 279 gem = drm_fb_dma_get_gem_obj(fb, i); 280 281 DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->dma_addr); 282 283 /* Compute the start of the displayed memory */ 284 dma_addr = gem->dma_addr + fb->offsets[i]; 285 286 dx = src_x; 287 dy = src_y; 288 289 if (i > 0) { 290 dx /= format->hsub; 291 dy /= format->vsub; 292 } 293 294 /* Fixup framebuffer address for src coordinates */ 295 dma_addr += dx * format->cpp[i]; 296 dma_addr += dy * fb->pitches[i]; 297 298 /* Set the line width */ 299 DRM_DEBUG_DRIVER("Layer %d. line width: %d bytes\n", 300 i + 1, fb->pitches[i]); 301 regmap_write(mixer->engine.regs, 302 SUN8I_MIXER_CHAN_VI_LAYER_PITCH(ch_base, 303 overlay, i), 304 fb->pitches[i]); 305 306 DRM_DEBUG_DRIVER("Setting %d. buffer address to %pad\n", 307 i + 1, &dma_addr); 308 309 regmap_write(mixer->engine.regs, 310 SUN8I_MIXER_CHAN_VI_LAYER_TOP_LADDR(ch_base, 311 overlay, i), 312 lower_32_bits(dma_addr)); 313 } 314 315 return 0; 316 } 317 318 static int sun8i_vi_layer_atomic_check(struct drm_plane *plane, 319 struct drm_atomic_state *state) 320 { 321 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 322 plane); 323 struct sun8i_layer *layer = plane_to_sun8i_layer(plane); 324 struct drm_crtc *crtc = new_plane_state->crtc; 325 struct drm_crtc_state *crtc_state; 326 int min_scale, max_scale; 327 328 if (!crtc) 329 return 0; 330 331 crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 332 if (WARN_ON(!crtc_state)) 333 return -EINVAL; 334 335 min_scale = DRM_PLANE_NO_SCALING; 336 max_scale = DRM_PLANE_NO_SCALING; 337 338 if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) { 339 min_scale = SUN8I_VI_SCALER_SCALE_MIN; 340 max_scale = SUN8I_VI_SCALER_SCALE_MAX; 341 } 342 343 return drm_atomic_helper_check_plane_state(new_plane_state, 344 crtc_state, 345 min_scale, max_scale, 346 true, true); 347 } 348 349 static void sun8i_vi_layer_atomic_update(struct drm_plane *plane, 350 struct drm_atomic_state *state) 351 { 352 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 353 plane); 354 struct sun8i_layer *layer = plane_to_sun8i_layer(plane); 355 unsigned int zpos = new_state->normalized_zpos; 356 struct sun8i_mixer *mixer = layer->mixer; 357 358 if (!new_state->crtc || !new_state->visible) 359 return; 360 361 sun8i_vi_layer_update_coord(mixer, layer->channel, 362 layer->overlay, plane, zpos); 363 sun8i_vi_layer_update_alpha(mixer, layer->channel, 364 layer->overlay, plane); 365 sun8i_vi_layer_update_formats(mixer, layer->channel, 366 layer->overlay, plane); 367 sun8i_vi_layer_update_buffer(mixer, layer->channel, 368 layer->overlay, plane); 369 } 370 371 static const struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs = { 372 .atomic_check = sun8i_vi_layer_atomic_check, 373 .atomic_update = sun8i_vi_layer_atomic_update, 374 }; 375 376 static const struct drm_plane_funcs sun8i_vi_layer_funcs = { 377 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 378 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 379 .destroy = drm_plane_cleanup, 380 .disable_plane = drm_atomic_helper_disable_plane, 381 .reset = drm_atomic_helper_plane_reset, 382 .update_plane = drm_atomic_helper_update_plane, 383 }; 384 385 /* 386 * While DE2 VI layer supports same RGB formats as UI layer, alpha 387 * channel is ignored. This structure lists all unique variants 388 * where alpha channel is replaced with "don't care" (X) channel. 389 */ 390 static const u32 sun8i_vi_layer_formats[] = { 391 DRM_FORMAT_BGR565, 392 DRM_FORMAT_BGR888, 393 DRM_FORMAT_BGRX4444, 394 DRM_FORMAT_BGRX5551, 395 DRM_FORMAT_BGRX8888, 396 DRM_FORMAT_RGB565, 397 DRM_FORMAT_RGB888, 398 DRM_FORMAT_RGBX4444, 399 DRM_FORMAT_RGBX5551, 400 DRM_FORMAT_RGBX8888, 401 DRM_FORMAT_XBGR1555, 402 DRM_FORMAT_XBGR4444, 403 DRM_FORMAT_XBGR8888, 404 DRM_FORMAT_XRGB1555, 405 DRM_FORMAT_XRGB4444, 406 DRM_FORMAT_XRGB8888, 407 408 DRM_FORMAT_NV16, 409 DRM_FORMAT_NV12, 410 DRM_FORMAT_NV21, 411 DRM_FORMAT_NV61, 412 DRM_FORMAT_UYVY, 413 DRM_FORMAT_VYUY, 414 DRM_FORMAT_YUYV, 415 DRM_FORMAT_YVYU, 416 DRM_FORMAT_YUV411, 417 DRM_FORMAT_YUV420, 418 DRM_FORMAT_YUV422, 419 DRM_FORMAT_YVU411, 420 DRM_FORMAT_YVU420, 421 DRM_FORMAT_YVU422, 422 }; 423 424 static const u32 sun8i_vi_layer_de3_formats[] = { 425 DRM_FORMAT_ABGR1555, 426 DRM_FORMAT_ABGR2101010, 427 DRM_FORMAT_ABGR4444, 428 DRM_FORMAT_ABGR8888, 429 DRM_FORMAT_ARGB1555, 430 DRM_FORMAT_ARGB2101010, 431 DRM_FORMAT_ARGB4444, 432 DRM_FORMAT_ARGB8888, 433 DRM_FORMAT_BGR565, 434 DRM_FORMAT_BGR888, 435 DRM_FORMAT_BGRA1010102, 436 DRM_FORMAT_BGRA5551, 437 DRM_FORMAT_BGRA4444, 438 DRM_FORMAT_BGRA8888, 439 DRM_FORMAT_BGRX8888, 440 DRM_FORMAT_RGB565, 441 DRM_FORMAT_RGB888, 442 DRM_FORMAT_RGBA1010102, 443 DRM_FORMAT_RGBA4444, 444 DRM_FORMAT_RGBA5551, 445 DRM_FORMAT_RGBA8888, 446 DRM_FORMAT_RGBX8888, 447 DRM_FORMAT_XBGR8888, 448 DRM_FORMAT_XRGB8888, 449 450 DRM_FORMAT_NV16, 451 DRM_FORMAT_NV12, 452 DRM_FORMAT_NV21, 453 DRM_FORMAT_NV61, 454 DRM_FORMAT_P010, 455 DRM_FORMAT_P210, 456 DRM_FORMAT_UYVY, 457 DRM_FORMAT_VYUY, 458 DRM_FORMAT_YUYV, 459 DRM_FORMAT_YVYU, 460 DRM_FORMAT_YUV411, 461 DRM_FORMAT_YUV420, 462 DRM_FORMAT_YUV422, 463 DRM_FORMAT_YVU411, 464 DRM_FORMAT_YVU420, 465 DRM_FORMAT_YVU422, 466 }; 467 468 static const uint64_t sun8i_layer_modifiers[] = { 469 DRM_FORMAT_MOD_LINEAR, 470 DRM_FORMAT_MOD_INVALID 471 }; 472 473 struct sun8i_layer *sun8i_vi_layer_init_one(struct drm_device *drm, 474 struct sun8i_mixer *mixer, 475 int index) 476 { 477 enum drm_plane_type type = DRM_PLANE_TYPE_OVERLAY; 478 u32 supported_encodings, supported_ranges; 479 unsigned int plane_cnt, format_count; 480 struct sun8i_layer *layer; 481 const u32 *formats; 482 int ret; 483 484 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL); 485 if (!layer) 486 return ERR_PTR(-ENOMEM); 487 488 if (mixer->cfg->de_type >= SUN8I_MIXER_DE3) { 489 formats = sun8i_vi_layer_de3_formats; 490 format_count = ARRAY_SIZE(sun8i_vi_layer_de3_formats); 491 } else { 492 formats = sun8i_vi_layer_formats; 493 format_count = ARRAY_SIZE(sun8i_vi_layer_formats); 494 } 495 496 if (!mixer->cfg->ui_num && index == 0) 497 type = DRM_PLANE_TYPE_PRIMARY; 498 499 /* possible crtcs are set later */ 500 ret = drm_universal_plane_init(drm, &layer->plane, 0, 501 &sun8i_vi_layer_funcs, 502 formats, format_count, 503 sun8i_layer_modifiers, 504 type, NULL); 505 if (ret) { 506 dev_err(drm->dev, "Couldn't initialize layer\n"); 507 return ERR_PTR(ret); 508 } 509 510 plane_cnt = mixer->cfg->ui_num + mixer->cfg->vi_num; 511 512 if (mixer->cfg->vi_num == 1 || mixer->cfg->de_type >= SUN8I_MIXER_DE3) { 513 ret = drm_plane_create_alpha_property(&layer->plane); 514 if (ret) { 515 dev_err(drm->dev, "Couldn't add alpha property\n"); 516 return ERR_PTR(ret); 517 } 518 } 519 520 ret = drm_plane_create_zpos_property(&layer->plane, index, 521 0, plane_cnt - 1); 522 if (ret) { 523 dev_err(drm->dev, "Couldn't add zpos property\n"); 524 return ERR_PTR(ret); 525 } 526 527 supported_encodings = BIT(DRM_COLOR_YCBCR_BT601) | 528 BIT(DRM_COLOR_YCBCR_BT709); 529 if (mixer->cfg->de_type >= SUN8I_MIXER_DE3) 530 supported_encodings |= BIT(DRM_COLOR_YCBCR_BT2020); 531 532 supported_ranges = BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) | 533 BIT(DRM_COLOR_YCBCR_FULL_RANGE); 534 535 ret = drm_plane_create_color_properties(&layer->plane, 536 supported_encodings, 537 supported_ranges, 538 DRM_COLOR_YCBCR_BT709, 539 DRM_COLOR_YCBCR_LIMITED_RANGE); 540 if (ret) { 541 dev_err(drm->dev, "Couldn't add encoding and range properties!\n"); 542 return ERR_PTR(ret); 543 } 544 545 drm_plane_helper_add(&layer->plane, &sun8i_vi_layer_helper_funcs); 546 layer->mixer = mixer; 547 layer->type = SUN8I_LAYER_TYPE_VI; 548 layer->channel = index; 549 layer->overlay = 0; 550 551 return layer; 552 } 553