blob: ff087f7de88eb2133648853d18d9b8e3dcf1a04a [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 Pinchart287bdf02015-02-20 15:58:38 +0200135 unsigned int src_x = plane->plane.state->src_x >> 16;
136 unsigned int src_y = plane->plane.state->src_y >> 16;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200137 unsigned int index = plane->hwindex;
Laurent Pinchart906eff72014-12-09 19:11:18 +0200138 bool interlaced;
Laurent Pincharteb863012013-11-13 14:26:01 +0100139 u32 mwr;
140
Laurent Pinchart906eff72014-12-09 19:11:18 +0200141 interlaced = plane->crtc->mode.flags & DRM_MODE_FLAG_INTERLACE;
142
143 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
144 * operation with 32bpp formats.
145 */
Laurent Pincharteb863012013-11-13 14:26:01 +0100146 if (plane->format->planes == 2)
147 mwr = plane->pitch;
148 else
149 mwr = plane->pitch * 8 / plane->format->bpp;
150
Laurent Pinchart906eff72014-12-09 19:11:18 +0200151 if (interlaced && plane->format->bpp == 32)
152 mwr *= 2;
153
Laurent Pincharteb863012013-11-13 14:26:01 +0100154 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200155
Laurent Pinchart9e7db062013-06-14 20:54:16 +0200156 /* 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.
160 *
Laurent Pinchart906eff72014-12-09 19:11:18 +0200161 * Despite not being documented, doubling seem not to be needed when
162 * operating in interlaced mode.
163 *
Laurent Pinchart9e7db062013-06-14 20:54:16 +0200164 * Similarly, for the second plane, NV12 and NV21 formats seem to
Laurent Pinchart906eff72014-12-09 19:11:18 +0200165 * require a halved Y position value, in both progressive and interlaced
166 * modes.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200167 */
Laurent Pinchart287bdf02015-02-20 15:58:38 +0200168 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
169 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
Laurent Pinchart906eff72014-12-09 19:11:18 +0200170 (!interlaced && plane->format->bpp == 32 ? 2 : 1));
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200171 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[0]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200172
173 if (plane->format->planes == 2) {
174 index = (index + 1) % 8;
175
Laurent Pinchart49785e22014-12-09 22:45:11 +0200176 rcar_du_plane_write(rgrp, index, PnMWR, plane->pitch);
177
Laurent Pinchart287bdf02015-02-20 15:58:38 +0200178 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
179 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200180 (plane->format->bpp == 16 ? 2 : 1) / 2);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200181 rcar_du_plane_write(rgrp, index, PnDSA0R, plane->dma[1]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200182 }
183}
184
185void rcar_du_plane_compute_base(struct rcar_du_plane *plane,
186 struct drm_framebuffer *fb)
187{
188 struct drm_gem_cma_object *gem;
189
Laurent Pincharteb863012013-11-13 14:26:01 +0100190 plane->pitch = fb->pitches[0];
191
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200192 gem = drm_fb_cma_get_gem_obj(fb, 0);
193 plane->dma[0] = gem->paddr + fb->offsets[0];
194
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];
198 }
199}
200
201static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
202 unsigned int index)
203{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200204 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200205 u32 colorkey;
206 u32 pnmr;
207
208 /* The PnALPHAR register controls alpha-blending in 16bpp formats
209 * (ARGB1555 and XRGB1555).
210 *
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.
213 *
214 * For XRGB, set the alpha value to the plane-wide alpha value and
215 * enable alpha-blending regardless of the X bit value.
216 */
217 if (plane->format->fourcc != DRM_FORMAT_XRGB1555)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200218 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200219 else
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200220 rcar_du_plane_write(rgrp, index, PnALPHAR,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200221 PnALPHAR_ABIT_X | plane->alpha);
222
223 pnmr = PnMR_BM_MD | plane->format->pnmr;
224
225 /* Disable color keying when requested. YUV formats have the
226 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
227 * automatically.
228 */
229 if ((plane->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
230 pnmr |= PnMR_SPIM_TP_OFF;
231
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;
235
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200236 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200237
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);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200243 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200244 break;
245
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);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200251 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200252 break;
253
254 case DRM_FORMAT_XRGB8888:
255 case DRM_FORMAT_ARGB8888:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200256 rcar_du_plane_write(rgrp, index, PnTC3R,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200257 PnTC3R_CODE | (plane->colorkey & 0xffffff));
258 break;
259 }
260}
261
262static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
263 unsigned int index)
264{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200265 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200266 u32 ddcr2 = PnDDCR2_CODE;
267 u32 ddcr4;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200268
269 /* Data format
270 *
271 * The data format is selected by the DDDF field in PnMR and the EDF
272 * field in DDCR4.
273 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200274 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200275 ddcr4 &= ~PnDDCR4_EDF_MASK;
276 ddcr4 |= plane->format->edf | PnDDCR4_CODE;
277
278 rcar_du_plane_setup_mode(plane, index);
279
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;
285
286 if (plane->format->fourcc == DRM_FORMAT_NV21)
287 ddcr2 |= PnDDCR2_NV21;
288
289 ddcr2 |= PnDDCR2_DIVU;
290 } else {
291 ddcr2 |= PnDDCR2_DIVY;
292 }
293 }
294
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200295 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
296 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200297
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200298 /* Destination position and size */
Laurent Pinchart287bdf02015-02-20 15:58:38 +0200299 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);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200303
304 /* Wrap-around and blinking, disabled */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200305 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);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200309}
310
311void rcar_du_plane_setup(struct rcar_du_plane *plane)
312{
313 __rcar_du_plane_setup(plane, plane->hwindex);
314 if (plane->format->planes == 2)
315 __rcar_du_plane_setup(plane, (plane->hwindex + 1) % 8);
316
317 rcar_du_plane_update_base(plane);
318}
319
Laurent Pinchart920888a2015-02-18 12:18:05 +0200320static int rcar_du_plane_atomic_check(struct drm_plane *plane,
321 struct drm_plane_state *state)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200322{
323 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200324 struct rcar_du_device *rcdu = rplane->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200325 const struct rcar_du_format_info *format;
326 unsigned int nplanes;
327 int ret;
328
Laurent Pinchart920888a2015-02-18 12:18:05 +0200329 if (!state->fb || !state->crtc)
330 return 0;
Laurent Pinchart917de182015-02-17 18:34:17 +0200331
Laurent Pinchart920888a2015-02-18 12:18:05 +0200332 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__);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200335 return -EINVAL;
336 }
337
Laurent Pinchart920888a2015-02-18 12:18:05 +0200338 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);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200342 return -EINVAL;
343 }
344
345 nplanes = rplane->format ? rplane->format->planes : 0;
346
Laurent Pinchart920888a2015-02-18 12:18:05 +0200347 /* If the number of required planes has changed we will need to
348 * reallocate hardware planes. Ensure free planes are available.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200349 */
350 if (format->planes != nplanes) {
Laurent Pinchart920888a2015-02-18 12:18:05 +0200351 ret = rcar_du_plane_reserve_check(rplane, format);
352 if (ret < 0) {
353 dev_dbg(rcdu->dev, "%s: no available hardware plane\n",
354 __func__);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200355 return ret;
Laurent Pinchart920888a2015-02-18 12:18:05 +0200356 }
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200357 }
358
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200359 return 0;
360}
361
Laurent Pinchart920888a2015-02-18 12:18:05 +0200362static void rcar_du_plane_disable(struct rcar_du_plane *rplane)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200363{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200364 if (!rplane->enabled)
Laurent Pinchart920888a2015-02-18 12:18:05 +0200365 return;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200366
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200367 mutex_lock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200368 rplane->enabled = false;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200369 mutex_unlock(&rplane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200370
371 rcar_du_plane_release(rplane);
372
373 rplane->crtc = NULL;
374 rplane->format = NULL;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200375}
376
Laurent Pinchart920888a2015-02-18 12:18:05 +0200377static void rcar_du_plane_atomic_update(struct drm_plane *plane,
378 struct drm_plane_state *old_state)
379{
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;
384
385 if (!state->crtc) {
386 rcar_du_plane_disable(rplane);
387 return;
388 }
389
390 format = rcar_du_format_info(state->fb->pixel_format);
391 nplanes = rplane->format ? rplane->format->planes : 0;
392
393 /* Reallocate hardware planes if the number of required planes has
394 * changed.
395 */
396 if (format->planes != nplanes) {
397 rcar_du_plane_release(rplane);
398 rcar_du_plane_reserve(rplane, format);
399 }
400
401 rplane->crtc = state->crtc;
402 rplane->format = format;
403
Laurent Pinchart920888a2015-02-18 12:18:05 +0200404 rcar_du_plane_compute_base(rplane, state->fb);
405 rcar_du_plane_setup(rplane);
406
407 mutex_lock(&rplane->group->planes.lock);
408 rplane->enabled = true;
Laurent Pinchart920888a2015-02-18 12:18:05 +0200409 mutex_unlock(&rplane->group->planes.lock);
410}
411
412static 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,
415};
416
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200417/* 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.
420 */
421static void rcar_du_plane_set_alpha(struct rcar_du_plane *plane, u32 alpha)
422{
423 if (plane->alpha == alpha)
424 return;
425
426 plane->alpha = alpha;
427 if (!plane->enabled || plane->format->fourcc != DRM_FORMAT_XRGB1555)
428 return;
429
430 rcar_du_plane_setup_mode(plane, plane->hwindex);
431}
432
433static void rcar_du_plane_set_colorkey(struct rcar_du_plane *plane,
434 u32 colorkey)
435{
436 if (plane->colorkey == colorkey)
437 return;
438
439 plane->colorkey = colorkey;
440 if (!plane->enabled)
441 return;
442
443 rcar_du_plane_setup_mode(plane, plane->hwindex);
444}
445
446static void rcar_du_plane_set_zpos(struct rcar_du_plane *plane,
447 unsigned int zpos)
448{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200449 mutex_lock(&plane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200450 if (plane->zpos == zpos)
451 goto done;
452
453 plane->zpos = zpos;
454 if (!plane->enabled)
455 goto done;
456
457 rcar_du_crtc_update_planes(plane->crtc);
458
459done:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200460 mutex_unlock(&plane->group->planes.lock);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200461}
462
463static int rcar_du_plane_set_property(struct drm_plane *plane,
464 struct drm_property *property,
465 uint64_t value)
466{
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200467 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200468 struct rcar_du_group *rgrp = rplane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200469
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200470 if (property == rgrp->planes.alpha)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200471 rcar_du_plane_set_alpha(rplane, value);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200472 else if (property == rgrp->planes.colorkey)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200473 rcar_du_plane_set_colorkey(rplane, value);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200474 else if (property == rgrp->planes.zpos)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200475 rcar_du_plane_set_zpos(rplane, value);
476 else
477 return -EINVAL;
478
479 return 0;
480}
481
482static const struct drm_plane_funcs rcar_du_plane_funcs = {
Laurent Pinchart336d04a2015-02-20 13:18:56 +0200483 .update_plane = drm_atomic_helper_update_plane,
484 .disable_plane = drm_atomic_helper_disable_plane,
Laurent Pinchart3e8da872015-02-20 11:30:59 +0200485 .reset = drm_atomic_helper_plane_reset,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200486 .set_property = rcar_du_plane_set_property,
487 .destroy = drm_plane_cleanup,
Laurent Pinchart3e8da872015-02-20 11:30:59 +0200488 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
489 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200490};
491
492static const uint32_t formats[] = {
493 DRM_FORMAT_RGB565,
494 DRM_FORMAT_ARGB1555,
495 DRM_FORMAT_XRGB1555,
496 DRM_FORMAT_XRGB8888,
497 DRM_FORMAT_ARGB8888,
498 DRM_FORMAT_UYVY,
499 DRM_FORMAT_YUYV,
500 DRM_FORMAT_NV12,
501 DRM_FORMAT_NV21,
502 DRM_FORMAT_NV16,
503};
504
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200505int rcar_du_planes_init(struct rcar_du_group *rgrp)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200506{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200507 struct rcar_du_planes *planes = &rgrp->planes;
508 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchart917de182015-02-17 18:34:17 +0200509 unsigned int num_planes;
510 unsigned int num_crtcs;
511 unsigned int crtcs;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200512 unsigned int i;
Laurent Pinchart917de182015-02-17 18:34:17 +0200513 int ret;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200514
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200515 mutex_init(&planes->lock);
516 planes->free = 0xff;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200517
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200518 planes->alpha =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200519 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200520 if (planes->alpha == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200521 return -ENOMEM;
522
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).
526 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200527 planes->colorkey =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200528 drm_property_create_range(rcdu->ddev, 0, "colorkey",
529 0, 0x01ffffff);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200530 if (planes->colorkey == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200531 return -ENOMEM;
532
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200533 planes->zpos =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200534 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200535 if (planes->zpos == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200536 return -ENOMEM;
537
Laurent Pinchart917de182015-02-17 18:34:17 +0200538 /* Create one primary plane per in this group CRTC and seven overlay
539 * planes.
540 */
541 num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
542 num_planes = num_crtcs + 7;
543
544 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
545
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;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200550 struct rcar_du_plane *plane = &planes->planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200551
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200552 plane->group = rgrp;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200553 plane->hwindex = -1;
554 plane->alpha = 255;
555 plane->colorkey = RCAR_DU_COLORKEY_NONE;
Laurent Pinchart917de182015-02-17 18:34:17 +0200556 plane->zpos = type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200557
Laurent Pinchart917de182015-02-17 18:34:17 +0200558 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
559 &rcar_du_plane_funcs, formats,
560 ARRAY_SIZE(formats), type);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200561 if (ret < 0)
562 return ret;
563
Laurent Pinchart920888a2015-02-18 12:18:05 +0200564 drm_plane_helper_add(&plane->plane,
565 &rcar_du_plane_helper_funcs);
566
Laurent Pinchart917de182015-02-17 18:34:17 +0200567 if (type == DRM_PLANE_TYPE_PRIMARY)
568 continue;
569
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200570 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200571 planes->alpha, 255);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200572 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200573 planes->colorkey,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200574 RCAR_DU_COLORKEY_NONE);
575 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200576 planes->zpos, 1);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200577 }
578
579 return 0;
580}