blob: 4a0669fd81767b9868b5b35d66f9c3166e509681 [file] [log] [blame]
Laurent Pinchart4bf8e192013-06-19 13:54:11 +02001/*
2 * rcar_du_plane.c -- R-Car Display Unit Planes
3 *
Laurent Pinchart36d50462014-02-06 18:13:52 +01004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart4bf8e192013-06-19 13:54:11 +02005 *
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7 *
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.
12 */
13
14#include <drm/drmP.h>
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>
19
20#include "rcar_du_drv.h"
21#include "rcar_du_kms.h"
22#include "rcar_du_plane.h"
23#include "rcar_du_regs.h"
24
25#define RCAR_DU_COLORKEY_NONE (0 << 24)
26#define RCAR_DU_COLORKEY_SOURCE (1 << 24)
27#define RCAR_DU_COLORKEY_MASK (1 << 24)
28
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020029static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
30{
Laurent Pinchart917de182015-02-17 18:34:17 +020031 return container_of(plane, struct rcar_du_plane, plane);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020032}
33
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020034static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020035 unsigned int index, u32 reg)
36{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020037 return rcar_du_read(rgrp->dev,
38 rgrp->mmio_offset + index * PLANE_OFF + reg);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020039}
40
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020041static void rcar_du_plane_write(struct rcar_du_group *rgrp,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020042 unsigned int index, u32 reg, u32 data)
43{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020044 rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
45 data);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020046}
47
48int rcar_du_plane_reserve(struct rcar_du_plane *plane,
49 const struct rcar_du_format_info *format)
50{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020051 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020052 unsigned int i;
53 int ret = -EBUSY;
54
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020055 mutex_lock(&rgrp->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020056
Laurent Pinchart30534602015-02-25 18:38:25 +020057 for (i = 0; i < RCAR_DU_NUM_HW_PLANES; ++i) {
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020058 if (!(rgrp->planes.free & (1 << i)))
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020059 continue;
60
61 if (format->planes == 1 ||
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020062 rgrp->planes.free & (1 << ((i + 1) % 8)))
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020063 break;
64 }
65
Laurent Pinchart30534602015-02-25 18:38:25 +020066 if (i == RCAR_DU_NUM_HW_PLANES)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020067 goto done;
68
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020069 rgrp->planes.free &= ~(1 << i);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020070 if (format->planes == 2)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020071 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020072
73 plane->hwindex = i;
74
75 ret = 0;
76
77done:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020078 mutex_unlock(&rgrp->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020079 return ret;
80}
81
82void rcar_du_plane_release(struct rcar_du_plane *plane)
83{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020084 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020085
86 if (plane->hwindex == -1)
87 return;
88
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020089 mutex_lock(&rgrp->planes.lock);
90 rgrp->planes.free |= 1 << plane->hwindex;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020091 if (plane->format->planes == 2)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020092 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
93 mutex_unlock(&rgrp->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020094
95 plane->hwindex = -1;
96}
97
98void rcar_du_plane_update_base(struct rcar_du_plane *plane)
99{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200100 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200101 unsigned int index = plane->hwindex;
Laurent Pinchart906eff72014-12-09 19:11:18 +0200102 bool interlaced;
Laurent Pincharteb863012013-11-13 14:26:01 +0100103 u32 mwr;
104
Laurent Pinchart906eff72014-12-09 19:11:18 +0200105 interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE;
106
107 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
108 * operation with 32bpp formats.
109 */
Laurent Pincharteb863012013-11-13 14:26:01 +0100110 if (plane->format->planes == 2)
111 mwr = plane->pitch;
112 else
113 mwr = plane->pitch * 8 / plane->format->bpp;
114
Laurent Pinchart906eff72014-12-09 19:11:18 +0200115 if (interlaced && plane->format->bpp == 32)
116 mwr *= 2;
117
Laurent Pincharteb863012013-11-13 14:26:01 +0100118 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200119
Laurent Pinchart9e7db062013-06-14 20:54:16 +0200120 /* 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.
124 *
Laurent Pinchart906eff72014-12-09 19:11:18 +0200125 * Despite not being documented, doubling seem not to be needed when
126 * operating in interlaced mode.
127 *
Laurent Pinchart9e7db062013-06-14 20:54:16 +0200128 * Similarly, for the second plane, NV12 and NV21 formats seem to
Laurent Pinchart906eff72014-12-09 19:11:18 +0200129 * require a halved Y position value, in both progressive and interlaced
130 * modes.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200131 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200132 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
133 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
Laurent Pinchart906eff72014-12-09 19:11:18 +0200134 (!interlaced && plane->format->bpp == 32 ? 2 : 1));
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200135 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200136
137 if (plane->format->planes == 2) {
138 index = (index + 1) % 8;
139
Laurent Pinchart49785e22014-12-09 22:45:11 +0200140 rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch);
141
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200142 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
143 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200144 (plane->format->bpp == 16 ? 2 : 1) / 2);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200145 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200146 }
147}
148
149void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
150 struct drm_framebuffer *fb)
151{
152 struct drm_gem_cma_object *gem;
153
Laurent Pincharteb863012013-11-13 14:26:01 +0100154 plane->pitch = fb->pitches[0];
155
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200156 gem = drm_fb_cma_get_gem_obj(fb, 0);
157 plane->dma[0] = gem->paddr + fb->offsets[0];
158
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];
162 }
163}
164
165static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
166 unsigned int index)
167{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200168 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200169 u32 colorkey;
170 u32 pnmr;
171
172 /* The PnALPHAR register controls alpha-blending in 16bpp formats
173 * (ARGB1555 and XRGB1555).
174 *
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.
177 *
178 * For XRGB, set the alpha value to the plane-wide alpha value and
179 * enable alpha-blending regardless of the X bit value.
180 */
181 if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200182 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200183 else
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200184 rcar_du_plane_write(rgrp, index, PnALPHAR,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200185 PnALPHAR_ABIT_X | plane->alpha);
186
187 pnmr = PnMR_BM_MD | plane->format->pnmr;
188
189 /* Disable color keying when requested. YUV formats have the
190 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
191 * automatically.
192 */
193 if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
194 pnmr |= PnMR_SPIM_TP_OFF;
195
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;
199
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200200 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200201
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);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200207 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200208 break;
209
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);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200215 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200216 break;
217
218 case DRM_FORMAT_XRGB8888:
219 case DRM_FORMAT_ARGB8888:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200220 rcar_du_plane_write(rgrp, index, PnTC3R,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200221 PnTC3R_CODE | (plane->colorkey & 0xffffff));
222 break;
223 }
224}
225
226static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
227 unsigned int index)
228{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200229 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200230 u32 ddcr2 = PnDDCR2_CODE;
231 u32 ddcr4;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200232
233 /* Data format
234 *
235 * The data format is selected by the DDDF field in PnMR and the EDF
236 * field in DDCR4.
237 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200238 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200239 ddcr4 &= ~PnDDCR4_EDF_MASK;
240 ddcr4 |= plane->format->edf | PnDDCR4_CODE;
241
242 rcar_du_plane_setup_mode(plane, index);
243
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;
249
250 if (plane->format->fourcc == DRM_FORMAT_NV21)
251 ddcr2 |= PnDDCR2_NV21;
252
253 ddcr2 |= PnDDCR2_DIVU;
254 } else {
255 ddcr2 |= PnDDCR2_DIVY;
256 }
257 }
258
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200259 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
260 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200261
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200262 /* Destination position and size */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200263 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);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200267
268 /* Wrap-around and blinking, disabled */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200269 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);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200273}
274
275void rcar_du_plane_setup(struct rcar_du_plane *plane)
276{
277 __rcar_du_plane_setup(plane, plane->hwindex);
278 if (plane->format->planes == 2)
279 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
280
281 rcar_du_plane_update_base(plane);
282}
283
284static int
285rcar_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)
290{
291 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200292 struct rcar_du_device *rcdu = rplane->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200293 const struct rcar_du_format_info *format;
294 unsigned int nplanes;
295 int ret;
296
Laurent Pinchart917de182015-02-17 18:34:17 +0200297 if (plane->type != DRM_PLANE_TYPE_OVERLAY)
298 return -EINVAL;
299
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200300 format = rcar_du_format_info(fb->pixel_format);
301 if (format == NULL) {
302 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
303 fb->pixel_format);
304 return -EINVAL;
305 }
306
307 if (src_w >> 16 != crtc_w || src_h >> 16 != crtc_h) {
308 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
309 return -EINVAL;
310 }
311
312 nplanes = rplane->format ? rplane->format->planes : 0;
313
314 /* Reallocate hardware planes if the number of required planes has
315 * changed.
316 */
317 if (format->planes != nplanes) {
318 rcar_du_plane_release(rplane);
319 ret = rcar_du_plane_reserve(rplane, format);
320 if (ret < 0)
321 return ret;
322 }
323
324 rplane->crtc = crtc;
325 rplane->format = format;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200326
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;
333
334 rcar_du_plane_compute_base(rplane, fb);
335 rcar_du_plane_setup(rplane);
336
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200337 mutex_lock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200338 rplane->enabled = true;
339 rcar_du_crtc_update_planes(rplane->crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200340 mutex_unlock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200341
342 return 0;
343}
344
345static int rcar_du_plane_disable(struct drm_plane *plane)
346{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200347 struct rcar_du_plane *rplane = to_rcar_plane(plane);
348
Laurent Pinchart917de182015-02-17 18:34:17 +0200349 if (plane->type != DRM_PLANE_TYPE_OVERLAY)
350 return -EINVAL;
351
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200352 if (!rplane->enabled)
353 return 0;
354
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200355 mutex_lock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200356 rplane->enabled = false;
357 rcar_du_crtc_update_planes(rplane->crtc);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200358 mutex_unlock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200359
360 rcar_du_plane_release(rplane);
361
362 rplane->crtc = NULL;
363 rplane->format = NULL;
364
365 return 0;
366}
367
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.
371 */
372static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
373{
374 if (plane->alpha == alpha)
375 return;
376
377 plane->alpha = alpha;
378 if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
379 return;
380
381 rcar_du_plane_setup_mode(plane, plane->hwindex);
382}
383
384static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
385 u32 colorkey)
386{
387 if (plane->colorkey == colorkey)
388 return;
389
390 plane->colorkey = colorkey;
391 if (!plane->enabled)
392 return;
393
394 rcar_du_plane_setup_mode(plane, plane->hwindex);
395}
396
397static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
398 unsigned int zpos)
399{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200400 mutex_lock(&plane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200401 if (plane->zpos == zpos)
402 goto done;
403
404 plane->zpos = zpos;
405 if (!plane->enabled)
406 goto done;
407
408 rcar_du_crtc_update_planes(plane->crtc);
409
410done:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200411 mutex_unlock(&plane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200412}
413
414static int rcar_du_plane_set_property(struct drm_plane *plane,
415 struct drm_property *property,
416 uint64_t value)
417{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200418 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200419 struct rcar_du_group *rgrp = rplane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200420
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200421 if (property == rgrp->planes.alpha)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200422 rcar_du_plane_set_alpha(rplane, value);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200423 else if (property == rgrp->planes.colorkey)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200424 rcar_du_plane_set_colorkey(rplane, value);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200425 else if (property == rgrp->planes.zpos)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200426 rcar_du_plane_set_zpos(rplane, value);
427 else
428 return -EINVAL;
429
430 return 0;
431}
432
433static 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,
438};
439
440static const uint32_t formats[] = {
441 DRM_FORMAT_RGB565,
442 DRM_FORMAT_ARGB1555,
443 DRM_FORMAT_XRGB1555,
444 DRM_FORMAT_XRGB8888,
445 DRM_FORMAT_ARGB8888,
446 DRM_FORMAT_UYVY,
447 DRM_FORMAT_YUYV,
448 DRM_FORMAT_NV12,
449 DRM_FORMAT_NV21,
450 DRM_FORMAT_NV16,
451};
452
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200453int rcar_du_planes_init(struct rcar_du_group *rgrp)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200454{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200455 struct rcar_du_planes *planes = &rgrp->planes;
456 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchart917de182015-02-17 18:34:17 +0200457 unsigned int num_planes;
458 unsigned int num_crtcs;
459 unsigned int crtcs;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200460 unsigned int i;
Laurent Pinchart917de182015-02-17 18:34:17 +0200461 int ret;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200462
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200463 mutex_init(&planes->lock);
464 planes->free = 0xff;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200465
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200466 planes->alpha =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200467 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200468 if (planes->alpha == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200469 return -ENOMEM;
470
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).
474 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200475 planes->colorkey =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200476 drm_property_create_range(rcdu->ddev, 0, "colorkey",
477 0, 0x01ffffff);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200478 if (planes->colorkey == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200479 return -ENOMEM;
480
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200481 planes->zpos =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200482 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200483 if (planes->zpos == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200484 return -ENOMEM;
485
Laurent Pinchart917de182015-02-17 18:34:17 +0200486 /* Create one primary plane per in this group CRTC and seven overlay
487 * planes.
488 */
489 num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
490 num_planes = num_crtcs + 7;
491
492 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
493
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;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200498 struct rcar_du_plane *plane = &planes->planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200499
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200500 plane->group = rgrp;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200501 plane->hwindex = -1;
502 plane->alpha = 255;
503 plane->colorkey = RCAR_DU_COLORKEY_NONE;
Laurent Pinchart917de182015-02-17 18:34:17 +0200504 plane->zpos = type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200505
Laurent Pinchart917de182015-02-17 18:34:17 +0200506 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
507 &rcar_du_plane_funcs, formats,
508 ARRAY_SIZE(formats), type);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200509 if (ret < 0)
510 return ret;
511
Laurent Pinchart917de182015-02-17 18:34:17 +0200512 if (type == DRM_PLANE_TYPE_PRIMARY)
513 continue;
514
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200515 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200516 planes->alpha, 255);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200517 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200518 planes->colorkey,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200519 RCAR_DU_COLORKEY_NONE);
520 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200521 planes->zpos, 1);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200522 }
523
524 return 0;
525}