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_crtc.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_fb_cma_helper.h>
18 #include <drm/drm_gem_cma_helper.h>
20 #include "rcar_du_drv.h"
21 #include "rcar_du_kms.h"
22 #include "rcar_du_plane.h"
23 #include "rcar_du_regs.h"
25 #define RCAR_DU_COLORKEY_NONE (0 << 24)
26 #define RCAR_DU_COLORKEY_SOURCE (1 << 24)
27 #define RCAR_DU_COLORKEY_MASK (1 << 24)
29 static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
31 return container_of(plane, struct rcar_du_plane, plane);
34 static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
35 unsigned int index, u32 reg)
37 return rcar_du_read(rgrp->dev,
38 rgrp->mmio_offset + index * PLANE_OFF + reg);
41 static void rcar_du_plane_write(struct rcar_du_group *rgrp,
42 unsigned int index, u32 reg, u32 data)
44 rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
48 int rcar_du_plane_reserve(struct rcar_du_plane *plane,
49 const struct rcar_du_format_info *format)
51 struct rcar_du_group *rgrp = plane->group;
55 mutex_lock(&rgrp->planes.lock);
57 for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
58 if (!(rgrp->planes.free & (1 << i)))
61 if (format->planes == 1 ||
62 rgrp->planes.free & (1 << ((i + 1) % 8)))
66 if (i == ARRAY_SIZE(rgrp->planes.planes))
69 rgrp->planes.free &= ~(1 << i);
70 if (format->planes == 2)
71 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
78 mutex_unlock(&rgrp->planes.lock);
82 void rcar_du_plane_release(struct rcar_du_plane *plane)
84 struct rcar_du_group *rgrp = plane->group;
86 if (plane->hwindex == -1)
89 mutex_lock(&rgrp->planes.lock);
90 rgrp->planes.free |= 1 << plane->hwindex;
91 if (plane->format->planes == 2)
92 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
93 mutex_unlock(&rgrp->planes.lock);
98 void rcar_du_plane_update_base(struct rcar_du_plane *plane)
100 struct rcar_du_group *rgrp = plane->group;
101 unsigned int index = plane->hwindex;
105 interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE;
107 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
108 * operation with 32bpp formats.
110 if (plane->format->planes == 2)
113 mwr = plane->pitch * 8 / plane->format->bpp;
115 if (interlaced && plane->format->bpp == 32)
118 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
120 /* The Y position is expressed in raster line units and must be doubled
121 * for 32bpp formats, according to the R8A7790 datasheet. No mention of
122 * doubling the Y position is found in the R8A7779 datasheet, but the
123 * rule seems to apply there as well.
125 * Despite not being documented, doubling seem not to be needed when
126 * operating in interlaced mode.
128 * Similarly, for the second plane, NV12 and NV21 formats seem to
129 * require a halved Y position value, in both progressive and interlaced
132 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
133 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
134 (!interlaced && plane->format->bpp == 32 ? 2 : 1));
135 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
137 if (plane->format->planes == 2) {
138 index = (index + 1) % 8;
140 rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch);
142 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
143 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
144 (plane->format->bpp == 16 ? 2 : 1) / 2);
145 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
149 void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
150 struct drm_framebuffer *fb)
152 struct drm_gem_cma_object *gem;
154 plane->pitch = fb->pitches[0];
156 gem = drm_fb_cma_get_gem_obj(fb, 0);
157 plane->dma[0] = gem->paddr + fb->offsets[0];
159 if (plane->format->planes == 2) {
160 gem = drm_fb_cma_get_gem_obj(fb, 1);
161 plane->dma[1] = gem->paddr + fb->offsets[1];
165 static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
168 struct rcar_du_group *rgrp = plane->group;
172 /* The PnALPHAR register controls alpha-blending in 16bpp formats
173 * (ARGB1555 and XRGB1555).
175 * For ARGB, set the alpha value to 0, and enable alpha-blending when
176 * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
178 * For XRGB, set the alpha value to the plane-wide alpha value and
179 * enable alpha-blending regardless of the X bit value.
181 if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
182 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
184 rcar_du_plane_write(rgrp, index, PnALPHAR,
185 PnALPHAR_ABIT_X | plane->alpha);
187 pnmr = PnMR_BM_MD | plane->format->pnmr;
189 /* Disable color keying when requested. YUV formats have the
190 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
193 if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
194 pnmr |= PnMR_SPIM_TP_OFF;
196 /* For packed YUV formats we need to select the U/V order. */
197 if (plane->format->fourcc == DRM_FORMAT_YUYV)
198 pnmr |= PnMR_YCDF_YUYV;
200 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
202 switch (plane->format->fourcc) {
203 case DRM_FORMAT_RGB565:
204 colorkey = ((plane->colorkey & 0xf80000) >> 8)
205 | ((plane->colorkey & 0x00fc00) >> 5)
206 | ((plane->colorkey & 0x0000f8) >> 3);
207 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
210 case DRM_FORMAT_ARGB1555:
211 case DRM_FORMAT_XRGB1555:
212 colorkey = ((plane->colorkey & 0xf80000) >> 9)
213 | ((plane->colorkey & 0x00f800) >> 6)
214 | ((plane->colorkey & 0x0000f8) >> 3);
215 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
218 case DRM_FORMAT_XRGB8888:
219 case DRM_FORMAT_ARGB8888:
220 rcar_du_plane_write(rgrp, index, PnTC3R,
221 PnTC3R_CODE | (plane->colorkey & 0xffffff));
226 static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
229 struct rcar_du_group *rgrp = plane->group;
230 u32 ddcr2 = PnDDCR2_CODE;
235 * The data format is selected by the DDDF field in PnMR and the EDF
238 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
239 ddcr4 &= ~PnDDCR4_EDF_MASK;
240 ddcr4 |= plane->format->edf | PnDDCR4_CODE;
242 rcar_du_plane_setup_mode(plane, index);
244 if (plane->format->planes == 2) {
245 if (plane->hwindex != index) {
246 if (plane->format->fourcc == DRM_FORMAT_NV12 ||
247 plane->format->fourcc == DRM_FORMAT_NV21)
248 ddcr2 |= PnDDCR2_Y420;
250 if (plane->format->fourcc == DRM_FORMAT_NV21)
251 ddcr2 |= PnDDCR2_NV21;
253 ddcr2 |= PnDDCR2_DIVU;
255 ddcr2 |= PnDDCR2_DIVY;
259 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
260 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
262 /* Destination position and size */
263 rcar_du_plane_write(rgrp, index, PnDSXR, plane->width);
264 rcar_du_plane_write(rgrp, index, PnDSYR, plane->height);
265 rcar_du_plane_write(rgrp, index, PnDPXR, plane->dst_x);
266 rcar_du_plane_write(rgrp, index, PnDPYR, plane->dst_y);
268 /* Wrap-around and blinking, disabled */
269 rcar_du_plane_write(rgrp, index, PnWASPR, 0);
270 rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
271 rcar_du_plane_write(rgrp, index, PnBTR, 0);
272 rcar_du_plane_write(rgrp, index, PnMLR, 0);
275 void rcar_du_plane_setup(struct rcar_du_plane *plane)
277 __rcar_du_plane_setup(plane, plane->hwindex);
278 if (plane->format->planes == 2)
279 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
281 rcar_du_plane_update_base(plane);
285 rcar_du_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
286 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
287 unsigned int crtc_w, unsigned int crtc_h,
288 uint32_t src_x, uint32_t src_y,
289 uint32_t src_w, uint32_t src_h)
291 struct rcar_du_plane *rplane = to_rcar_plane(plane);
292 struct rcar_du_device *rcdu = rplane->group->dev;
293 const struct rcar_du_format_info *format;
294 unsigned int nplanes;
297 if (plane->type != DRM_PLANE_TYPE_OVERLAY)
300 format = rcar_du_format_info(fb->pixel_format);
301 if (format == NULL) {
302 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
307 if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) {
308 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
312 nplanes = rplane->format ? rplane->format->planes : 0;
314 /* Reallocate hardware planes if the number of required planes has
317 if (format->planes != nplanes) {
318 rcar_du_plane_release(rplane);
319 ret = rcar_du_plane_reserve(rplane, format);
325 rplane->format = format;
327 rplane->src_x = src_x >> 16;
328 rplane->src_y = src_y >> 16;
329 rplane->dst_x = crtc_x;
330 rplane->dst_y = crtc_y;
331 rplane->width = crtc_w;
332 rplane->height = crtc_h;
334 rcar_du_plane_compute_base(rplane, fb);
335 rcar_du_plane_setup(rplane);
337 mutex_lock(&rplane->group->planes.lock);
338 rplane->enabled = true;
339 rcar_du_crtc_update_planes(rplane->crtc);
340 mutex_unlock(&rplane->group->planes.lock);
345 static int rcar_du_plane_disable(struct drm_plane *plane)
347 struct rcar_du_plane *rplane = to_rcar_plane(plane);
349 if (plane->type != DRM_PLANE_TYPE_OVERLAY)
352 if (!rplane->enabled)
355 mutex_lock(&rplane->group->planes.lock);
356 rplane->enabled = false;
357 rcar_du_crtc_update_planes(rplane->crtc);
358 mutex_unlock(&rplane->group->planes.lock);
360 rcar_du_plane_release(rplane);
363 rplane->format = NULL;
368 /* Both the .set_property and the .update_plane operations are called with the
369 * mode_config lock held. There is this no need to explicitly protect access to
370 * the alpha and colorkey fields and the mode register.
372 static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
374 if (plane->alpha == alpha)
377 plane->alpha = alpha;
378 if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
381 rcar_du_plane_setup_mode(plane, plane->hwindex);
384 static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
387 if (plane->colorkey == colorkey)
390 plane->colorkey = colorkey;
394 rcar_du_plane_setup_mode(plane, plane->hwindex);
397 static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
400 mutex_lock(&plane->group->planes.lock);
401 if (plane->zpos == zpos)
408 rcar_du_crtc_update_planes(plane->crtc);
411 mutex_unlock(&plane->group->planes.lock);
414 static int rcar_du_plane_set_property(struct drm_plane *plane,
415 struct drm_property *property,
418 struct rcar_du_plane *rplane = to_rcar_plane(plane);
419 struct rcar_du_group *rgrp = rplane->group;
421 if (property == rgrp->planes.alpha)
422 rcar_du_plane_set_alpha(rplane, value);
423 else if (property == rgrp->planes.colorkey)
424 rcar_du_plane_set_colorkey(rplane, value);
425 else if (property == rgrp->planes.zpos)
426 rcar_du_plane_set_zpos(rplane, value);
433 static const struct drm_plane_funcs rcar_du_plane_funcs = {
434 .update_plane = rcar_du_plane_update,
435 .disable_plane = rcar_du_plane_disable,
436 .set_property = rcar_du_plane_set_property,
437 .destroy = drm_plane_cleanup,
440 static const uint32_t formats[] = {
453 int rcar_du_planes_init(struct rcar_du_group *rgrp)
455 struct rcar_du_planes *planes = &rgrp->planes;
456 struct rcar_du_device *rcdu = rgrp->dev;
457 unsigned int num_planes;
458 unsigned int num_crtcs;
463 mutex_init(&planes->lock);
467 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
468 if (planes->alpha == NULL)
471 /* The color key is expressed as an RGB888 triplet stored in a 32-bit
472 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
473 * or enable source color keying (1).
476 drm_property_create_range(rcdu->ddev, 0, "colorkey",
478 if (planes->colorkey == NULL)
482 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
483 if (planes->zpos == NULL)
486 /* Create one primary plane per in this group CRTC and seven overlay
489 num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
490 num_planes = num_crtcs + 7;
492 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
494 for (i = 0; i < num_planes; ++i) {
495 enum drm_plane_type type = i < num_crtcs
496 ? DRM_PLANE_TYPE_PRIMARY
497 : DRM_PLANE_TYPE_OVERLAY;
498 struct rcar_du_plane *plane = &planes->planes[i];
503 plane->colorkey = RCAR_DU_COLORKEY_NONE;
504 plane->zpos = type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
506 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
507 &rcar_du_plane_funcs, formats,
508 ARRAY_SIZE(formats), type);
512 if (type == DRM_PLANE_TYPE_PRIMARY)
515 drm_object_attach_property(&plane->plane.base,
517 drm_object_attach_property(&plane->plane.base,
519 RCAR_DU_COLORKEY_NONE);
520 drm_object_attach_property(&plane->plane.base,