2 * rcar_du_plane.c -- R-Car Display Unit Planes
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_plane_helper.h>
22 #include "rcar_du_drv.h"
23 #include "rcar_du_kms.h"
24 #include "rcar_du_plane.h"
25 #include "rcar_du_regs.h"
27 #define RCAR_DU_COLORKEY_NONE (0 << 24)
28 #define RCAR_DU_COLORKEY_SOURCE (1 << 24)
29 #define RCAR_DU_COLORKEY_MASK (1 << 24)
31 static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
33 return container_of(plane, struct rcar_du_plane, plane);
36 static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
37 unsigned int index, u32 reg)
39 return rcar_du_read(rgrp->dev,
40 rgrp->mmio_offset + index * PLANE_OFF + reg);
43 static void rcar_du_plane_write(struct rcar_du_group *rgrp,
44 unsigned int index, u32 reg, u32 data)
46 rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
50 static int rcar_du_plane_reserve_check(struct rcar_du_plane *plane,
51 const struct rcar_du_format_info *format)
53 struct rcar_du_group *rgrp = plane->group;
58 mutex_lock(&rgrp->planes.lock);
60 free = rgrp->planes.free;
62 if (plane->hwindex != -1) {
63 free |= 1 << plane->hwindex;
64 if (plane->format->planes == 2)
65 free |= 1 << ((plane->hwindex + 1) % 8);
68 for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
69 if (!(free & (1 << i)))
72 if (format->planes == 1 || free & (1 << ((i + 1) % 8)))
76 ret = i == ARRAY_SIZE(rgrp->planes.planes) ? -EBUSY : 0;
78 mutex_unlock(&rgrp->planes.lock);
82 static int rcar_du_plane_reserve(struct rcar_du_plane *plane,
83 const struct rcar_du_format_info *format)
85 struct rcar_du_group *rgrp = plane->group;
89 mutex_lock(&rgrp->planes.lock);
91 for (i = 0; i < RCAR_DU_NUM_HW_PLANES; ++i) {
92 if (!(rgrp->planes.free & (1 << i)))
95 if (format->planes == 1 ||
96 rgrp->planes.free & (1 << ((i + 1) % 8)))
100 if (i == RCAR_DU_NUM_HW_PLANES)
103 rgrp->planes.free &= ~(1 << i);
104 if (format->planes == 2)
105 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
112 mutex_unlock(&rgrp->planes.lock);
116 static void rcar_du_plane_release(struct rcar_du_plane *plane)
118 struct rcar_du_group *rgrp = plane->group;
120 if (plane->hwindex == -1)
123 mutex_lock(&rgrp->planes.lock);
124 rgrp->planes.free |= 1 << plane->hwindex;
125 if (plane->format->planes == 2)
126 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
127 mutex_unlock(&rgrp->planes.lock);
132 void rcar_du_plane_update_base(struct rcar_du_plane *plane)
134 struct rcar_du_group *rgrp = plane->group;
135 unsigned int src_x = plane->plane.state->src_x >> 16;
136 unsigned int src_y = plane->plane.state->src_y >> 16;
137 unsigned int index = plane->hwindex;
141 interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE;
143 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
144 * operation with 32bpp formats.
146 if (plane->format->planes == 2)
149 mwr = plane->pitch * 8 / plane->format->bpp;
151 if (interlaced && plane->format->bpp == 32)
154 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
156 /* The Y position is expressed in raster line units and must be doubled
157 * for 32bpp formats, according to the R8A7790 datasheet. No mention of
158 * doubling the Y position is found in the R8A7779 datasheet, but the
159 * rule seems to apply there as well.
161 * Despite not being documented, doubling seem not to be needed when
162 * operating in interlaced mode.
164 * Similarly, for the second plane, NV12 and NV21 formats seem to
165 * require a halved Y position value, in both progressive and interlaced
168 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
169 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
170 (!interlaced && plane->format->bpp == 32 ? 2 : 1));
171 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
173 if (plane->format->planes == 2) {
174 index = (index + 1) % 8;
176 rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch);
178 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
179 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
180 (plane->format->bpp == 16 ? 2 : 1) / 2);
181 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
185 void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
186 struct drm_framebuffer *fb)
188 struct drm_gem_cma_object *gem;
190 plane->pitch = fb->pitches[0];
192 gem = drm_fb_cma_get_gem_obj(fb, 0);
193 plane->dma[0] = gem->paddr + fb->offsets[0];
195 if (plane->format->planes == 2) {
196 gem = drm_fb_cma_get_gem_obj(fb, 1);
197 plane->dma[1] = gem->paddr + fb->offsets[1];
201 static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
204 struct rcar_du_group *rgrp = plane->group;
208 /* The PnALPHAR register controls alpha-blending in 16bpp formats
209 * (ARGB1555 and XRGB1555).
211 * For ARGB, set the alpha value to 0, and enable alpha-blending when
212 * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
214 * For XRGB, set the alpha value to the plane-wide alpha value and
215 * enable alpha-blending regardless of the X bit value.
217 if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
218 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
220 rcar_du_plane_write(rgrp, index, PnALPHAR,
221 PnALPHAR_ABIT_X | plane->alpha);
223 pnmr = PnMR_BM_MD | plane->format->pnmr;
225 /* Disable color keying when requested. YUV formats have the
226 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
229 if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
230 pnmr |= PnMR_SPIM_TP_OFF;
232 /* For packed YUV formats we need to select the U/V order. */
233 if (plane->format->fourcc == DRM_FORMAT_YUYV)
234 pnmr |= PnMR_YCDF_YUYV;
236 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
238 switch (plane->format->fourcc) {
239 case DRM_FORMAT_RGB565:
240 colorkey = ((plane->colorkey & 0xf80000) >> 8)
241 | ((plane->colorkey & 0x00fc00) >> 5)
242 | ((plane->colorkey & 0x0000f8) >> 3);
243 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
246 case DRM_FORMAT_ARGB1555:
247 case DRM_FORMAT_XRGB1555:
248 colorkey = ((plane->colorkey & 0xf80000) >> 9)
249 | ((plane->colorkey & 0x00f800) >> 6)
250 | ((plane->colorkey & 0x0000f8) >> 3);
251 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
254 case DRM_FORMAT_XRGB8888:
255 case DRM_FORMAT_ARGB8888:
256 rcar_du_plane_write(rgrp, index, PnTC3R,
257 PnTC3R_CODE | (plane->colorkey & 0xffffff));
262 static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
265 struct rcar_du_group *rgrp = plane->group;
266 u32 ddcr2 = PnDDCR2_CODE;
271 * The data format is selected by the DDDF field in PnMR and the EDF
274 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
275 ddcr4 &= ~PnDDCR4_EDF_MASK;
276 ddcr4 |= plane->format->edf | PnDDCR4_CODE;
278 rcar_du_plane_setup_mode(plane, index);
280 if (plane->format->planes == 2) {
281 if (plane->hwindex != index) {
282 if (plane->format->fourcc == DRM_FORMAT_NV12 ||
283 plane->format->fourcc == DRM_FORMAT_NV21)
284 ddcr2 |= PnDDCR2_Y420;
286 if (plane->format->fourcc == DRM_FORMAT_NV21)
287 ddcr2 |= PnDDCR2_NV21;
289 ddcr2 |= PnDDCR2_DIVU;
291 ddcr2 |= PnDDCR2_DIVY;
295 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
296 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
298 /* Destination position and size */
299 rcar_du_plane_write(rgrp, index, PnDSXR, plane->plane.state->crtc_w);
300 rcar_du_plane_write(rgrp, index, PnDSYR, plane->plane.state->crtc_h);
301 rcar_du_plane_write(rgrp, index, PnDPXR, plane->plane.state->crtc_x);
302 rcar_du_plane_write(rgrp, index, PnDPYR, plane->plane.state->crtc_y);
304 /* Wrap-around and blinking, disabled */
305 rcar_du_plane_write(rgrp, index, PnWASPR, 0);
306 rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
307 rcar_du_plane_write(rgrp, index, PnBTR, 0);
308 rcar_du_plane_write(rgrp, index, PnMLR, 0);
311 void rcar_du_plane_setup(struct rcar_du_plane *plane)
313 __rcar_du_plane_setup(plane, plane->hwindex);
314 if (plane->format->planes == 2)
315 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
317 rcar_du_plane_update_base(plane);
320 static int rcar_du_plane_atomic_check(struct drm_plane *plane,
321 struct drm_plane_state *state)
323 struct rcar_du_plane *rplane = to_rcar_plane(plane);
324 struct rcar_du_device *rcdu = rplane->group->dev;
325 const struct rcar_du_format_info *format;
326 unsigned int nplanes;
329 if (!state->fb || !state->crtc)
332 if (state->src_w >> 16 != state->crtc_w ||
333 state->src_h >> 16 != state->crtc_h) {
334 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
338 format = rcar_du_format_info(state->fb->pixel_format);
339 if (format == NULL) {
340 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
341 state->fb->pixel_format);
345 nplanes = rplane->format ? rplane->format->planes : 0;
347 /* If the number of required planes has changed we will need to
348 * reallocate hardware planes. Ensure free planes are available.
350 if (format->planes != nplanes) {
351 ret = rcar_du_plane_reserve_check(rplane, format);
353 dev_dbg(rcdu->dev, "%s: no available hardware plane\n",
362 static void rcar_du_plane_disable(struct rcar_du_plane *rplane)
364 if (!rplane->enabled)
367 mutex_lock(&rplane->group->planes.lock);
368 rplane->enabled = false;
369 mutex_unlock(&rplane->group->planes.lock);
371 rcar_du_plane_release(rplane);
374 rplane->format = NULL;
377 static void rcar_du_plane_atomic_update(struct drm_plane *plane,
378 struct drm_plane_state *old_state)
380 struct rcar_du_plane *rplane = to_rcar_plane(plane);
381 struct drm_plane_state *state = plane->state;
382 const struct rcar_du_format_info *format;
383 unsigned int nplanes;
386 rcar_du_plane_disable(rplane);
390 format = rcar_du_format_info(state->fb->pixel_format);
391 nplanes = rplane->format ? rplane->format->planes : 0;
393 /* Reallocate hardware planes if the number of required planes has
396 if (format->planes != nplanes) {
397 rcar_du_plane_release(rplane);
398 rcar_du_plane_reserve(rplane, format);
401 rplane->crtc = state->crtc;
402 rplane->format = format;
404 rcar_du_plane_compute_base(rplane, state->fb);
405 rcar_du_plane_setup(rplane);
407 mutex_lock(&rplane->group->planes.lock);
408 rplane->enabled = true;
409 mutex_unlock(&rplane->group->planes.lock);
412 static const struct drm_plane_helper_funcs rcar_du_plane_helper_funcs = {
413 .atomic_check = rcar_du_plane_atomic_check,
414 .atomic_update = rcar_du_plane_atomic_update,
417 /* Both the .set_property and the .update_plane operations are called with the
418 * mode_config lock held. There is this no need to explicitly protect access to
419 * the alpha and colorkey fields and the mode register.
421 static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
423 if (plane->alpha == alpha)
426 plane->alpha = alpha;
427 if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
430 rcar_du_plane_setup_mode(plane, plane->hwindex);
433 static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
436 if (plane->colorkey == colorkey)
439 plane->colorkey = colorkey;
443 rcar_du_plane_setup_mode(plane, plane->hwindex);
446 static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
449 mutex_lock(&plane->group->planes.lock);
450 if (plane->zpos == zpos)
457 rcar_du_crtc_update_planes(plane->crtc);
460 mutex_unlock(&plane->group->planes.lock);
463 static int rcar_du_plane_set_property(struct drm_plane *plane,
464 struct drm_property *property,
467 struct rcar_du_plane *rplane = to_rcar_plane(plane);
468 struct rcar_du_group *rgrp = rplane->group;
470 if (property == rgrp->planes.alpha)
471 rcar_du_plane_set_alpha(rplane, value);
472 else if (property == rgrp->planes.colorkey)
473 rcar_du_plane_set_colorkey(rplane, value);
474 else if (property == rgrp->planes.zpos)
475 rcar_du_plane_set_zpos(rplane, value);
482 static const struct drm_plane_funcs rcar_du_plane_funcs = {
483 .update_plane = drm_atomic_helper_update_plane,
484 .disable_plane = drm_atomic_helper_disable_plane,
485 .reset = drm_atomic_helper_plane_reset,
486 .set_property = rcar_du_plane_set_property,
487 .destroy = drm_plane_cleanup,
488 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
489 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
492 static const uint32_t formats[] = {
505 int rcar_du_planes_init(struct rcar_du_group *rgrp)
507 struct rcar_du_planes *planes = &rgrp->planes;
508 struct rcar_du_device *rcdu = rgrp->dev;
509 unsigned int num_planes;
510 unsigned int num_crtcs;
515 mutex_init(&planes->lock);
519 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
520 if (planes->alpha == NULL)
523 /* The color key is expressed as an RGB888 triplet stored in a 32-bit
524 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
525 * or enable source color keying (1).
528 drm_property_create_range(rcdu->ddev, 0, "colorkey",
530 if (planes->colorkey == NULL)
534 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
535 if (planes->zpos == NULL)
538 /* Create one primary plane per in this group CRTC and seven overlay
541 num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
542 num_planes = num_crtcs + 7;
544 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
546 for (i = 0; i < num_planes; ++i) {
547 enum drm_plane_type type = i < num_crtcs
548 ? DRM_PLANE_TYPE_PRIMARY
549 : DRM_PLANE_TYPE_OVERLAY;
550 struct rcar_du_plane *plane = &planes->planes[i];
555 plane->colorkey = RCAR_DU_COLORKEY_NONE;
556 plane->zpos = type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
558 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
559 &rcar_du_plane_funcs, formats,
560 ARRAY_SIZE(formats), type);
564 drm_plane_helper_add(&plane->plane,
565 &rcar_du_plane_helper_funcs);
567 if (type == DRM_PLANE_TYPE_PRIMARY)
570 drm_object_attach_property(&plane->plane.base,
572 drm_object_attach_property(&plane->plane.base,
574 RCAR_DU_COLORKEY_NONE);
575 drm_object_attach_property(&plane->plane.base,