blob: c4e4fd293eee04f5b77a4fa890c1a3adbe996c47 [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>
Laurent Pinchart3e8da872015-02-20 11:30:59 +020015#include <drm/drm_atomic_helper.h>
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020016#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>
Laurent Pinchart920888a2015-02-18 12:18:05 +020020#include <drm/drm_plane_helper.h>
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020021
22#include "rcar_du_drv.h"
23#include "rcar_du_kms.h"
24#include "rcar_du_plane.h"
25#include "rcar_du_regs.h"
26
27#define RCAR_DU_COLORKEY_NONE (0 << 24)
28#define RCAR_DU_COLORKEY_SOURCE (1 << 24)
29#define RCAR_DU_COLORKEY_MASK (1 << 24)
30
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020031static inline struct rcar_du_plane *to_rcar_plane(struct drm_plane *plane)
32{
Laurent Pinchart917de182015-02-17 18:34:17 +020033 return container_of(plane, struct rcar_du_plane, plane);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020034}
35
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020036static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020037 unsigned int index, u32 reg)
38{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020039 return rcar_du_read(rgrp->dev,
40 rgrp->mmio_offset + index * PLANE_OFF + reg);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020041}
42
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020043static void rcar_du_plane_write(struct rcar_du_group *rgrp,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020044 unsigned int index, u32 reg, u32 data)
45{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020046 rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
47 data);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020048}
49
Laurent Pinchart920888a2015-02-18 12:18:05 +020050static int rcar_du_plane_reserve_check(struct rcar_du_plane *plane,
51 const struct rcar_du_format_info *format)
52{
53 struct rcar_du_group *rgrp = plane->group;
54 unsigned int free;
55 unsigned int i;
56 int ret;
57
58 mutex_lock(&rgrp->planes.lock);
59
60 free = rgrp->planes.free;
61
62 if (plane->hwindex != -1) {
63 free |= 1 << plane->hwindex;
64 if (plane->format->planes == 2)
65 free |= 1 << ((plane->hwindex + 1) % 8);
66 }
67
68 for (i = 0; i < ARRAY_SIZE(rgrp->planes.planes); ++i) {
69 if (!(free & (1 << i)))
70 continue;
71
72 if (format->planes == 1 || free & (1 << ((i + 1) % 8)))
73 break;
74 }
75
76 ret = i == ARRAY_SIZE(rgrp->planes.planes) ? -EBUSY : 0;
77
78 mutex_unlock(&rgrp->planes.lock);
79 return ret;
80}
81
Laurent Pinchart845f4632015-02-18 15:47:27 +020082static int rcar_du_plane_reserve(struct rcar_du_plane *plane,
83 const struct rcar_du_format_info *format)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020084{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020085 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020086 unsigned int i;
87 int ret = -EBUSY;
88
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020089 mutex_lock(&rgrp->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020090
Laurent Pinchart30534602015-02-25 18:38:25 +020091 for (i = 0; i < RCAR_DU_NUM_HW_PLANES; ++i) {
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020092 if (!(rgrp->planes.free & (1 << i)))
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020093 continue;
94
95 if (format->planes == 1 ||
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020096 rgrp->planes.free & (1 << ((i + 1) % 8)))
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020097 break;
98 }
99
Laurent Pinchart30534602015-02-25 18:38:25 +0200100 if (i == RCAR_DU_NUM_HW_PLANES)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200101 goto done;
102
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200103 rgrp->planes.free &= ~(1 << i);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200104 if (format->planes == 2)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200105 rgrp->planes.free &= ~(1 << ((i + 1) % 8));
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200106
107 plane->hwindex = i;
108
109 ret = 0;
110
111done:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200112 mutex_unlock(&rgrp->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200113 return ret;
114}
115
Laurent Pinchart845f4632015-02-18 15:47:27 +0200116static void rcar_du_plane_release(struct rcar_du_plane *plane)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200117{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200118 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200119
120 if (plane->hwindex == -1)
121 return;
122
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200123 mutex_lock(&rgrp->planes.lock);
124 rgrp->planes.free |= 1 << plane->hwindex;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200125 if (plane->format->planes == 2)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200126 rgrp->planes.free |= 1 << ((plane->hwindex + 1) % 8);
127 mutex_unlock(&rgrp->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200128
129 plane->hwindex = -1;
130}
131
132void rcar_du_plane_update_base(struct rcar_du_plane *plane)
133{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200134 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200135 unsigned int index = plane->hwindex;
Laurent Pinchart906eff72014-12-09 19:11:18 +0200136 bool interlaced;
Laurent Pincharteb863012013-11-13 14:26:01 +0100137 u32 mwr;
138
Laurent Pinchart906eff72014-12-09 19:11:18 +0200139 interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE;
140
141 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
142 * operation with 32bpp formats.
143 */
Laurent Pincharteb863012013-11-13 14:26:01 +0100144 if (plane->format->planes == 2)
145 mwr = plane->pitch;
146 else
147 mwr = plane->pitch * 8 / plane->format->bpp;
148
Laurent Pinchart906eff72014-12-09 19:11:18 +0200149 if (interlaced && plane->format->bpp == 32)
150 mwr *= 2;
151
Laurent Pincharteb863012013-11-13 14:26:01 +0100152 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200153
Laurent Pinchart9e7db062013-06-14 20:54:16 +0200154 /* The Y position is expressed in raster line units and must be doubled
155 * for 32bpp formats, according to the R8A7790 datasheet. No mention of
156 * doubling the Y position is found in the R8A7779 datasheet, but the
157 * rule seems to apply there as well.
158 *
Laurent Pinchart906eff72014-12-09 19:11:18 +0200159 * Despite not being documented, doubling seem not to be needed when
160 * operating in interlaced mode.
161 *
Laurent Pinchart9e7db062013-06-14 20:54:16 +0200162 * Similarly, for the second plane, NV12 and NV21 formats seem to
Laurent Pinchart906eff72014-12-09 19:11:18 +0200163 * require a halved Y position value, in both progressive and interlaced
164 * modes.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200165 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200166 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
167 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
Laurent Pinchart906eff72014-12-09 19:11:18 +0200168 (!interlaced && plane->format->bpp == 32 ? 2 : 1));
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200169 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200170
171 if (plane->format->planes == 2) {
172 index = (index + 1) % 8;
173
Laurent Pinchart49785e22014-12-09 22:45:11 +0200174 rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch);
175
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200176 rcar_du_plane_write(rgrp, index, PnSPXR, plane->src_x);
177 rcar_du_plane_write(rgrp, index, PnSPYR, plane->src_y *
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200178 (plane->format->bpp == 16 ? 2 : 1) / 2);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200179 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200180 }
181}
182
183void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
184 struct drm_framebuffer *fb)
185{
186 struct drm_gem_cma_object *gem;
187
Laurent Pincharteb863012013-11-13 14:26:01 +0100188 plane->pitch = fb->pitches[0];
189
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200190 gem = drm_fb_cma_get_gem_obj(fb, 0);
191 plane->dma[0] = gem->paddr + fb->offsets[0];
192
193 if (plane->format->planes == 2) {
194 gem = drm_fb_cma_get_gem_obj(fb, 1);
195 plane->dma[1] = gem->paddr + fb->offsets[1];
196 }
197}
198
199static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
200 unsigned int index)
201{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200202 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200203 u32 colorkey;
204 u32 pnmr;
205
206 /* The PnALPHAR register controls alpha-blending in 16bpp formats
207 * (ARGB1555 and XRGB1555).
208 *
209 * For ARGB, set the alpha value to 0, and enable alpha-blending when
210 * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
211 *
212 * For XRGB, set the alpha value to the plane-wide alpha value and
213 * enable alpha-blending regardless of the X bit value.
214 */
215 if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200216 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200217 else
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200218 rcar_du_plane_write(rgrp, index, PnALPHAR,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200219 PnALPHAR_ABIT_X | plane->alpha);
220
221 pnmr = PnMR_BM_MD | plane->format->pnmr;
222
223 /* Disable color keying when requested. YUV formats have the
224 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
225 * automatically.
226 */
227 if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
228 pnmr |= PnMR_SPIM_TP_OFF;
229
230 /* For packed YUV formats we need to select the U/V order. */
231 if (plane->format->fourcc == DRM_FORMAT_YUYV)
232 pnmr |= PnMR_YCDF_YUYV;
233
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200234 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200235
236 switch (plane->format->fourcc) {
237 case DRM_FORMAT_RGB565:
238 colorkey = ((plane->colorkey & 0xf80000) >> 8)
239 | ((plane->colorkey & 0x00fc00) >> 5)
240 | ((plane->colorkey & 0x0000f8) >> 3);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200241 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200242 break;
243
244 case DRM_FORMAT_ARGB1555:
245 case DRM_FORMAT_XRGB1555:
246 colorkey = ((plane->colorkey & 0xf80000) >> 9)
247 | ((plane->colorkey & 0x00f800) >> 6)
248 | ((plane->colorkey & 0x0000f8) >> 3);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200249 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200250 break;
251
252 case DRM_FORMAT_XRGB8888:
253 case DRM_FORMAT_ARGB8888:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200254 rcar_du_plane_write(rgrp, index, PnTC3R,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200255 PnTC3R_CODE | (plane->colorkey & 0xffffff));
256 break;
257 }
258}
259
260static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
261 unsigned int index)
262{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200263 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200264 u32 ddcr2 = PnDDCR2_CODE;
265 u32 ddcr4;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200266
267 /* Data format
268 *
269 * The data format is selected by the DDDF field in PnMR and the EDF
270 * field in DDCR4.
271 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200272 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200273 ddcr4 &= ~PnDDCR4_EDF_MASK;
274 ddcr4 |= plane->format->edf | PnDDCR4_CODE;
275
276 rcar_du_plane_setup_mode(plane, index);
277
278 if (plane->format->planes == 2) {
279 if (plane->hwindex != index) {
280 if (plane->format->fourcc == DRM_FORMAT_NV12 ||
281 plane->format->fourcc == DRM_FORMAT_NV21)
282 ddcr2 |= PnDDCR2_Y420;
283
284 if (plane->format->fourcc == DRM_FORMAT_NV21)
285 ddcr2 |= PnDDCR2_NV21;
286
287 ddcr2 |= PnDDCR2_DIVU;
288 } else {
289 ddcr2 |= PnDDCR2_DIVY;
290 }
291 }
292
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200293 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
294 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200295
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200296 /* Destination position and size */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200297 rcar_du_plane_write(rgrp, index, PnDSXR, plane->width);
298 rcar_du_plane_write(rgrp, index, PnDSYR, plane->height);
299 rcar_du_plane_write(rgrp, index, PnDPXR, plane->dst_x);
300 rcar_du_plane_write(rgrp, index, PnDPYR, plane->dst_y);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200301
302 /* Wrap-around and blinking, disabled */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200303 rcar_du_plane_write(rgrp, index, PnWASPR, 0);
304 rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
305 rcar_du_plane_write(rgrp, index, PnBTR, 0);
306 rcar_du_plane_write(rgrp, index, PnMLR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200307}
308
309void rcar_du_plane_setup(struct rcar_du_plane *plane)
310{
311 __rcar_du_plane_setup(plane, plane->hwindex);
312 if (plane->format->planes == 2)
313 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
314
315 rcar_du_plane_update_base(plane);
316}
317
Laurent Pinchart920888a2015-02-18 12:18:05 +0200318static int rcar_du_plane_atomic_check(struct drm_plane *plane,
319 struct drm_plane_state *state)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200320{
321 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200322 struct rcar_du_device *rcdu = rplane->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200323 const struct rcar_du_format_info *format;
324 unsigned int nplanes;
325 int ret;
326
Laurent Pinchart920888a2015-02-18 12:18:05 +0200327 if (!state->fb || !state->crtc)
328 return 0;
Laurent Pinchart917de182015-02-17 18:34:17 +0200329
Laurent Pinchart920888a2015-02-18 12:18:05 +0200330 if (state->src_w >> 16 != state->crtc_w ||
331 state->src_h >> 16 != state->crtc_h) {
332 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200333 return -EINVAL;
334 }
335
Laurent Pinchart920888a2015-02-18 12:18:05 +0200336 format = rcar_du_format_info(state->fb->pixel_format);
337 if (format == NULL) {
338 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
339 state->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200340 return -EINVAL;
341 }
342
343 nplanes = rplane->format ? rplane->format->planes : 0;
344
Laurent Pinchart920888a2015-02-18 12:18:05 +0200345 /* If the number of required planes has changed we will need to
346 * reallocate hardware planes. Ensure free planes are available.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200347 */
348 if (format->planes != nplanes) {
Laurent Pinchart920888a2015-02-18 12:18:05 +0200349 ret = rcar_du_plane_reserve_check(rplane, format);
350 if (ret < 0) {
351 dev_dbg(rcdu->dev, "%s: no available hardware plane\n",
352 __func__);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200353 return ret;
Laurent Pinchart920888a2015-02-18 12:18:05 +0200354 }
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200355 }
356
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200357 return 0;
358}
359
Laurent Pinchart920888a2015-02-18 12:18:05 +0200360static void rcar_du_plane_disable(struct rcar_du_plane *rplane)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200361{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200362 if (!rplane->enabled)
Laurent Pinchart920888a2015-02-18 12:18:05 +0200363 return;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200364
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200365 mutex_lock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200366 rplane->enabled = false;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200367 mutex_unlock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200368
369 rcar_du_plane_release(rplane);
370
371 rplane->crtc = NULL;
372 rplane->format = NULL;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200373}
374
Laurent Pinchart920888a2015-02-18 12:18:05 +0200375static void rcar_du_plane_atomic_update(struct drm_plane *plane,
376 struct drm_plane_state *old_state)
377{
378 struct rcar_du_plane *rplane = to_rcar_plane(plane);
379 struct drm_plane_state *state = plane->state;
380 const struct rcar_du_format_info *format;
381 unsigned int nplanes;
382
383 if (!state->crtc) {
384 rcar_du_plane_disable(rplane);
385 return;
386 }
387
388 format = rcar_du_format_info(state->fb->pixel_format);
389 nplanes = rplane->format ? rplane->format->planes : 0;
390
391 /* Reallocate hardware planes if the number of required planes has
392 * changed.
393 */
394 if (format->planes != nplanes) {
395 rcar_du_plane_release(rplane);
396 rcar_du_plane_reserve(rplane, format);
397 }
398
399 rplane->crtc = state->crtc;
400 rplane->format = format;
401
402 rplane->src_x = state->src_x >> 16;
403 rplane->src_y = state->src_y >> 16;
404 rplane->dst_x = state->crtc_x;
405 rplane->dst_y = state->crtc_y;
406 rplane->width = state->crtc_w;
407 rplane->height = state->crtc_h;
408
409 rcar_du_plane_compute_base(rplane, state->fb);
410 rcar_du_plane_setup(rplane);
411
412 mutex_lock(&rplane->group->planes.lock);
413 rplane->enabled = true;
Laurent Pinchart920888a2015-02-18 12:18:05 +0200414 mutex_unlock(&rplane->group->planes.lock);
415}
416
417static const struct drm_plane_helper_funcs rcar_du_plane_helper_funcs = {
418 .atomic_check = rcar_du_plane_atomic_check,
419 .atomic_update = rcar_du_plane_atomic_update,
420};
421
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200422/* Both the .set_property and the .update_plane operations are called with the
423 * mode_config lock held. There is this no need to explicitly protect access to
424 * the alpha and colorkey fields and the mode register.
425 */
426static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
427{
428 if (plane->alpha == alpha)
429 return;
430
431 plane->alpha = alpha;
432 if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
433 return;
434
435 rcar_du_plane_setup_mode(plane, plane->hwindex);
436}
437
438static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
439 u32 colorkey)
440{
441 if (plane->colorkey == colorkey)
442 return;
443
444 plane->colorkey = colorkey;
445 if (!plane->enabled)
446 return;
447
448 rcar_du_plane_setup_mode(plane, plane->hwindex);
449}
450
451static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
452 unsigned int zpos)
453{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200454 mutex_lock(&plane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200455 if (plane->zpos == zpos)
456 goto done;
457
458 plane->zpos = zpos;
459 if (!plane->enabled)
460 goto done;
461
462 rcar_du_crtc_update_planes(plane->crtc);
463
464done:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200465 mutex_unlock(&plane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200466}
467
468static int rcar_du_plane_set_property(struct drm_plane *plane,
469 struct drm_property *property,
470 uint64_t value)
471{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200472 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200473 struct rcar_du_group *rgrp = rplane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200474
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200475 if (property == rgrp->planes.alpha)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200476 rcar_du_plane_set_alpha(rplane, value);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200477 else if (property == rgrp->planes.colorkey)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200478 rcar_du_plane_set_colorkey(rplane, value);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200479 else if (property == rgrp->planes.zpos)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200480 rcar_du_plane_set_zpos(rplane, value);
481 else
482 return -EINVAL;
483
484 return 0;
485}
486
487static const struct drm_plane_funcs rcar_du_plane_funcs = {
Laurent Pinchart920888a2015-02-18 12:18:05 +0200488 .update_plane = drm_plane_helper_update,
489 .disable_plane = drm_plane_helper_disable,
Laurent Pinchart3e8da872015-02-20 11:30:59 +0200490 .reset = drm_atomic_helper_plane_reset,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200491 .set_property = rcar_du_plane_set_property,
492 .destroy = drm_plane_cleanup,
Laurent Pinchart3e8da872015-02-20 11:30:59 +0200493 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
494 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200495};
496
497static const uint32_t formats[] = {
498 DRM_FORMAT_RGB565,
499 DRM_FORMAT_ARGB1555,
500 DRM_FORMAT_XRGB1555,
501 DRM_FORMAT_XRGB8888,
502 DRM_FORMAT_ARGB8888,
503 DRM_FORMAT_UYVY,
504 DRM_FORMAT_YUYV,
505 DRM_FORMAT_NV12,
506 DRM_FORMAT_NV21,
507 DRM_FORMAT_NV16,
508};
509
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200510int rcar_du_planes_init(struct rcar_du_group *rgrp)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200511{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200512 struct rcar_du_planes *planes = &rgrp->planes;
513 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchart917de182015-02-17 18:34:17 +0200514 unsigned int num_planes;
515 unsigned int num_crtcs;
516 unsigned int crtcs;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200517 unsigned int i;
Laurent Pinchart917de182015-02-17 18:34:17 +0200518 int ret;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200519
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200520 mutex_init(&planes->lock);
521 planes->free = 0xff;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200522
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200523 planes->alpha =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200524 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200525 if (planes->alpha == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200526 return -ENOMEM;
527
528 /* The color key is expressed as an RGB888 triplet stored in a 32-bit
529 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
530 * or enable source color keying (1).
531 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200532 planes->colorkey =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200533 drm_property_create_range(rcdu->ddev, 0, "colorkey",
534 0, 0x01ffffff);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200535 if (planes->colorkey == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200536 return -ENOMEM;
537
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200538 planes->zpos =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200539 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200540 if (planes->zpos == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200541 return -ENOMEM;
542
Laurent Pinchart917de182015-02-17 18:34:17 +0200543 /* Create one primary plane per in this group CRTC and seven overlay
544 * planes.
545 */
546 num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
547 num_planes = num_crtcs + 7;
548
549 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
550
551 for (i = 0; i < num_planes; ++i) {
552 enum drm_plane_type type = i < num_crtcs
553 ? DRM_PLANE_TYPE_PRIMARY
554 : DRM_PLANE_TYPE_OVERLAY;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200555 struct rcar_du_plane *plane = &planes->planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200556
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200557 plane->group = rgrp;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200558 plane->hwindex = -1;
559 plane->alpha = 255;
560 plane->colorkey = RCAR_DU_COLORKEY_NONE;
Laurent Pinchart917de182015-02-17 18:34:17 +0200561 plane->zpos = type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200562
Laurent Pinchart917de182015-02-17 18:34:17 +0200563 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
564 &rcar_du_plane_funcs, formats,
565 ARRAY_SIZE(formats), type);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200566 if (ret < 0)
567 return ret;
568
Laurent Pinchart920888a2015-02-18 12:18:05 +0200569 drm_plane_helper_add(&plane->plane,
570 &rcar_du_plane_helper_funcs);
571
Laurent Pinchart917de182015-02-17 18:34:17 +0200572 if (type == DRM_PLANE_TYPE_PRIMARY)
573 continue;
574
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200575 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200576 planes->alpha, 255);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200577 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200578 planes->colorkey,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200579 RCAR_DU_COLORKEY_NONE);
580 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200581 planes->zpos, 1);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200582 }
583
584 return 0;
585}