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 struct rcar_du_kms_plane {
30 struct drm_plane plane;
31 struct rcar_du_plane *hwplane;
34 static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
36 return container_of(plane, struct rcar_du_kms_plane, plane)->hwplane;
39 static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
40 unsigned int index, u32 reg)
42 return rcar_du_read(rgrp->dev,
43 rgrp->mmio_offset + index * PLANE_OFF + reg);
46 static void rcar_du_plane_write(struct rcar_du_group *rgrp,
47 unsigned int index, u32 reg, u32 data)
49 rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
53 int rcar_du_plane_reserve(struct rcar_du_plane *plane,
54 const struct rcar_du_format_info *format)
56 struct rcar_du_group *rgrp = plane->group;
60 mutex_lock(&rgrp->planes.lock);
62 for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
63 if (!(rgrp->planes.free & (1 << i)))
66 if (format->planes == 1 ||
67 rgrp->planes.free & (1 << ((i + 1) % 8)))
71 if (i == ARRAY_SIZE(rgrp->planes.planes))
74 rgrp->planes.free &= ~(1 << i);
75 if (format->planes == 2)
76 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
83 mutex_unlock(&rgrp->planes.lock);
87 void rcar_du_plane_release(struct rcar_du_plane *plane)
89 struct rcar_du_group *rgrp = plane->group;
91 if (plane->hwindex == -1)
94 mutex_lock(&rgrp->planes.lock);
95 rgrp->planes.free |= 1 << plane->hwindex;
96 if (plane->format->planes == 2)
97 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
98 mutex_unlock(&rgrp->planes.lock);
103 void rcar_du_plane_update_base(struct rcar_du_plane *plane)
105 struct rcar_du_group *rgrp = plane->group;
106 unsigned int index = plane->hwindex;
109 /* Memory pitch (expressed in pixels) */
110 if (plane->format->planes == 2)
113 mwr = plane->pitch * 8 / plane->format->bpp;
115 if ((plane->interlace_flag) && (plane->format->bpp == 32))
116 rcar_du_plane_write(rgrp, index, PnMWR, mwr * 2);
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 * Similarly, for the second plane, NV12 and NV21 formats seem to
126 * require a halved Y position value.
128 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
129 if ((!plane->interlace_flag) && (plane->format->bpp == 32))
130 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y * 2);
132 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y);
133 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
135 if (plane->format->planes == 2) {
136 index = (index + 1) % 8;
138 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
139 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
140 (plane->format->bpp == 16 ? 2 : 1) / 2);
141 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
145 void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
146 struct drm_framebuffer *fb)
148 struct drm_gem_cma_object *gem;
150 plane->pitch = fb->pitches[0];
152 gem = drm_fb_cma_get_gem_obj(fb, 0);
153 plane->dma[0] = gem->paddr + fb->offsets[0];
155 if (plane->format->planes == 2) {
156 gem = drm_fb_cma_get_gem_obj(fb, 1);
157 plane->dma[1] = gem->paddr + fb->offsets[1];
161 static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
164 struct rcar_du_group *rgrp = plane->group;
168 /* The PnALPHAR register controls alpha-blending in 16bpp formats
169 * (ARGB1555 and XRGB1555).
171 * For ARGB, set the alpha value to 0, and enable alpha-blending when
172 * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
174 * For XRGB, set the alpha value to the plane-wide alpha value and
175 * enable alpha-blending regardless of the X bit value.
177 if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
178 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
180 rcar_du_plane_write(rgrp, index, PnALPHAR,
181 PnALPHAR_ABIT_X | plane->alpha);
183 pnmr = PnMR_BM_MD | plane->format->pnmr;
185 /* Disable color keying when requested. YUV formats have the
186 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
189 if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
190 pnmr |= PnMR_SPIM_TP_OFF;
192 /* For packed YUV formats we need to select the U/V order. */
193 if (plane->format->fourcc == DRM_FORMAT_YUYV)
194 pnmr |= PnMR_YCDF_YUYV;
196 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
198 switch (plane->format->fourcc) {
199 case DRM_FORMAT_RGB565:
200 colorkey = ((plane->colorkey & 0xf80000) >> 8)
201 | ((plane->colorkey & 0x00fc00) >> 5)
202 | ((plane->colorkey & 0x0000f8) >> 3);
203 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
206 case DRM_FORMAT_ARGB1555:
207 case DRM_FORMAT_XRGB1555:
208 colorkey = ((plane->colorkey & 0xf80000) >> 9)
209 | ((plane->colorkey & 0x00f800) >> 6)
210 | ((plane->colorkey & 0x0000f8) >> 3);
211 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
214 case DRM_FORMAT_XRGB8888:
215 case DRM_FORMAT_ARGB8888:
216 rcar_du_plane_write(rgrp, index, PnTC3R,
217 PnTC3R_CODE | (plane->colorkey & 0xffffff));
222 static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
225 struct rcar_du_group *rgrp = plane->group;
226 u32 ddcr2 = PnDDCR2_CODE;
231 * The data format is selected by the DDDF field in PnMR and the EDF
234 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
235 ddcr4 &= ~PnDDCR4_EDF_MASK;
236 ddcr4 |= plane->format->edf | PnDDCR4_CODE;
238 rcar_du_plane_setup_mode(plane, index);
240 if (plane->format->planes == 2) {
241 if (plane->hwindex != index) {
242 if (plane->format->fourcc == DRM_FORMAT_NV12 ||
243 plane->format->fourcc == DRM_FORMAT_NV21)
244 ddcr2 |= PnDDCR2_Y420;
246 if (plane->format->fourcc == DRM_FORMAT_NV21)
247 ddcr2 |= PnDDCR2_NV21;
249 ddcr2 |= PnDDCR2_DIVU;
251 ddcr2 |= PnDDCR2_DIVY;
255 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
256 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
258 /* Destination position and size */
259 rcar_du_plane_write(rgrp, index, PnDSXR, plane->width);
260 rcar_du_plane_write(rgrp, index, PnDSYR, plane->height);
261 rcar_du_plane_write(rgrp, index, PnDPXR, plane->dst_x);
262 rcar_du_plane_write(rgrp, index, PnDPYR, plane->dst_y);
264 /* Wrap-around and blinking, disabled */
265 rcar_du_plane_write(rgrp, index, PnWASPR, 0);
266 rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
267 rcar_du_plane_write(rgrp, index, PnBTR, 0);
268 rcar_du_plane_write(rgrp, index, PnMLR, 0);
271 void rcar_du_plane_setup(struct rcar_du_plane *plane)
273 __rcar_du_plane_setup(plane, plane->hwindex);
274 if (plane->format->planes == 2)
275 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
277 rcar_du_plane_update_base(plane);
281 rcar_du_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
282 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
283 unsigned int crtc_w, unsigned int crtc_h,
284 uint32_t src_x, uint32_t src_y,
285 uint32_t src_w, uint32_t src_h)
287 struct rcar_du_plane *rplane = to_rcar_plane(plane);
288 struct rcar_du_device *rcdu = rplane->group->dev;
289 const struct rcar_du_format_info *format;
290 unsigned int nplanes;
293 format = rcar_du_format_info(fb->pixel_format);
294 if (format == NULL) {
295 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
300 if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) {
301 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
305 nplanes = rplane->format ? rplane->format->planes : 0;
307 /* Reallocate hardware planes if the number of required planes has
310 if (format->planes != nplanes) {
311 rcar_du_plane_release(rplane);
312 ret = rcar_du_plane_reserve(rplane, format);
318 rplane->format = format;
320 rplane->src_x = src_x >> 16;
321 rplane->src_y = src_y >> 16;
322 rplane->dst_x = crtc_x;
323 rplane->dst_y = crtc_y;
324 rplane->width = crtc_w;
325 rplane->height = crtc_h;
327 rcar_du_plane_compute_base(rplane, fb);
329 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
330 rplane->interlace_flag = true;
332 rplane->interlace_flag = false;
333 rcar_du_plane_setup(rplane);
335 mutex_lock(&rplane->group->planes.lock);
336 rplane->enabled = true;
337 rcar_du_crtc_update_planes(rplane->crtc);
338 mutex_unlock(&rplane->group->planes.lock);
343 static int rcar_du_plane_disable(struct drm_plane *plane)
345 struct rcar_du_plane *rplane = to_rcar_plane(plane);
347 if (!rplane->enabled)
350 mutex_lock(&rplane->group->planes.lock);
351 rplane->enabled = false;
352 rcar_du_crtc_update_planes(rplane->crtc);
353 mutex_unlock(&rplane->group->planes.lock);
355 rcar_du_plane_release(rplane);
358 rplane->format = NULL;
363 /* Both the .set_property and the .update_plane operations are called with the
364 * mode_config lock held. There is this no need to explicitly protect access to
365 * the alpha and colorkey fields and the mode register.
367 static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
369 if (plane->alpha == alpha)
372 plane->alpha = alpha;
373 if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
376 rcar_du_plane_setup_mode(plane, plane->hwindex);
379 static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
382 if (plane->colorkey == colorkey)
385 plane->colorkey = colorkey;
389 rcar_du_plane_setup_mode(plane, plane->hwindex);
392 static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
395 mutex_lock(&plane->group->planes.lock);
396 if (plane->zpos == zpos)
403 rcar_du_crtc_update_planes(plane->crtc);
406 mutex_unlock(&plane->group->planes.lock);
409 static int rcar_du_plane_set_property(struct drm_plane *plane,
410 struct drm_property *property,
413 struct rcar_du_plane *rplane = to_rcar_plane(plane);
414 struct rcar_du_group *rgrp = rplane->group;
416 if (property == rgrp->planes.alpha)
417 rcar_du_plane_set_alpha(rplane, value);
418 else if (property == rgrp->planes.colorkey)
419 rcar_du_plane_set_colorkey(rplane, value);
420 else if (property == rgrp->planes.zpos)
421 rcar_du_plane_set_zpos(rplane, value);
428 static const struct drm_plane_funcs rcar_du_plane_funcs = {
429 .update_plane = rcar_du_plane_update,
430 .disable_plane = rcar_du_plane_disable,
431 .set_property = rcar_du_plane_set_property,
432 .destroy = drm_plane_cleanup,
435 static const uint32_t formats[] = {
448 int rcar_du_planes_init(struct rcar_du_group *rgrp)
450 struct rcar_du_planes *planes = &rgrp->planes;
451 struct rcar_du_device *rcdu = rgrp->dev;
454 mutex_init(&planes->lock);
458 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
459 if (planes->alpha == NULL)
462 /* The color key is expressed as an RGB888 triplet stored in a 32-bit
463 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
464 * or enable source color keying (1).
467 drm_property_create_range(rcdu->ddev, 0, "colorkey",
469 if (planes->colorkey == NULL)
473 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
474 if (planes->zpos == NULL)
477 for (i = 0; i < ARRAY_SIZE(planes->planes); ++i) {
478 struct rcar_du_plane *plane = &planes->planes[i];
483 plane->colorkey = RCAR_DU_COLORKEY_NONE;
490 int rcar_du_planes_register(struct rcar_du_group *rgrp)
492 struct rcar_du_planes *planes = &rgrp->planes;
493 struct rcar_du_device *rcdu = rgrp->dev;
498 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
500 for (i = 0; i < RCAR_DU_NUM_KMS_PLANES; ++i) {
501 struct rcar_du_kms_plane *plane;
503 plane = devm_kzalloc(rcdu->dev, sizeof(*plane), GFP_KERNEL);
507 plane->hwplane = &planes->planes[i + 2];
508 plane->hwplane->zpos = 1;
509 plane->hwplane->interlace_flag = false;
511 ret = drm_plane_init(rcdu->ddev, &plane->plane, crtcs,
512 &rcar_du_plane_funcs, formats,
513 ARRAY_SIZE(formats), false);
517 drm_object_attach_property(&plane->plane.base,
519 drm_object_attach_property(&plane->plane.base,
521 RCAR_DU_COLORKEY_NONE);
522 drm_object_attach_property(&plane->plane.base,