blob: 35a2f04ab799a9afe78fb6de14d10b04c645d1d5 [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 Pinchartcb2025d2013-06-16 21:01:02 +020031static u32 rcar_du_plane_read(struct rcar_du_group *rgrp,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020032 unsigned int index, u32 reg)
33{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020034 return rcar_du_read(rgrp->dev,
35 rgrp->mmio_offset + index * PLANE_OFF + reg);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020036}
37
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020038static void rcar_du_plane_write(struct rcar_du_group *rgrp,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020039 unsigned int index, u32 reg, u32 data)
40{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020041 rcar_du_write(rgrp->dev, rgrp->mmio_offset + index * PLANE_OFF + reg,
42 data);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020043}
44
Laurent Pinchartf398f342015-02-23 01:25:19 +020045static void rcar_du_plane_setup_fb(struct rcar_du_plane *plane)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020046{
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020047 struct rcar_du_plane_state *state =
48 to_rcar_du_plane_state(plane->plane.state);
Laurent Pinchartf398f342015-02-23 01:25:19 +020049 struct drm_framebuffer *fb = plane->plane.state->fb;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +020050 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020051 unsigned int src_x = state->state.src_x >> 16;
52 unsigned int src_y = state->state.src_y >> 16;
Laurent Pinchart5ee5a812015-02-25 18:27:19 +020053 unsigned int index = state->hwindex;
Laurent Pinchartf398f342015-02-23 01:25:19 +020054 struct drm_gem_cma_object *gem;
Laurent Pinchart906eff72014-12-09 19:11:18 +020055 bool interlaced;
Laurent Pincharteb863012013-11-13 14:26:01 +010056 u32 mwr;
57
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020058 interlaced = state->state.crtc->state->adjusted_mode.flags
Laurent Pinchart47094192015-02-22 19:24:59 +020059 & DRM_MODE_FLAG_INTERLACE;
Laurent Pinchart906eff72014-12-09 19:11:18 +020060
61 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
62 * operation with 32bpp formats.
63 */
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020064 if (state->format->planes == 2)
Laurent Pinchartf398f342015-02-23 01:25:19 +020065 mwr = fb->pitches[0];
Laurent Pincharteb863012013-11-13 14:26:01 +010066 else
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020067 mwr = fb->pitches[0] * 8 / state->format->bpp;
Laurent Pincharteb863012013-11-13 14:26:01 +010068
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020069 if (interlaced && state->format->bpp == 32)
Laurent Pinchart906eff72014-12-09 19:11:18 +020070 mwr *= 2;
71
Laurent Pincharteb863012013-11-13 14:26:01 +010072 rcar_du_plane_write(rgrp, index, PnMWR, mwr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020073
Laurent Pinchart9e7db062013-06-14 20:54:16 +020074 /* The Y position is expressed in raster line units and must be doubled
75 * for 32bpp formats, according to the R8A7790 datasheet. No mention of
76 * doubling the Y position is found in the R8A7779 datasheet, but the
77 * rule seems to apply there as well.
78 *
Laurent Pinchart906eff72014-12-09 19:11:18 +020079 * Despite not being documented, doubling seem not to be needed when
80 * operating in interlaced mode.
81 *
Laurent Pinchart9e7db062013-06-14 20:54:16 +020082 * Similarly, for the second plane, NV12 and NV21 formats seem to
Laurent Pinchart906eff72014-12-09 19:11:18 +020083 * require a halved Y position value, in both progressive and interlaced
84 * modes.
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020085 */
Laurent Pinchart287bdf02015-02-20 15:58:38 +020086 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
87 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020088 (!interlaced && state->format->bpp == 32 ? 2 : 1));
Laurent Pinchartf398f342015-02-23 01:25:19 +020089
90 gem = drm_fb_cma_get_gem_obj(fb, 0);
91 rcar_du_plane_write(rgrp, index, PnDSA0R, gem->paddr + fb->offsets[0]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020092
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +020093 if (state->format->planes == 2) {
Laurent Pinchart4bf8e192013-06-19 13:54:11 +020094 index = (index + 1) % 8;
95
Laurent Pinchartf398f342015-02-23 01:25:19 +020096 rcar_du_plane_write(rgrp, index, PnMWR, fb->pitches[0]);
Laurent Pinchart49785e22014-12-09 22:45:11 +020097
Laurent Pinchart287bdf02015-02-20 15:58:38 +020098 rcar_du_plane_write(rgrp, index, PnSPXR, src_x);
99 rcar_du_plane_write(rgrp, index, PnSPYR, src_y *
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200100 (state->format->bpp == 16 ? 2 : 1) / 2);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200101
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200102 gem = drm_fb_cma_get_gem_obj(fb, 1);
Laurent Pinchartf398f342015-02-23 01:25:19 +0200103 rcar_du_plane_write(rgrp, index, PnDSA0R,
104 gem->paddr + fb->offsets[1]);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200105 }
106}
107
108static void rcar_du_plane_setup_mode(struct rcar_du_plane *plane,
109 unsigned int index)
110{
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200111 struct rcar_du_plane_state *state =
112 to_rcar_du_plane_state(plane->plane.state);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200113 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200114 u32 colorkey;
115 u32 pnmr;
116
117 /* The PnALPHAR register controls alpha-blending in 16bpp formats
118 * (ARGB1555 and XRGB1555).
119 *
120 * For ARGB, set the alpha value to 0, and enable alpha-blending when
121 * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
122 *
123 * For XRGB, set the alpha value to the plane-wide alpha value and
124 * enable alpha-blending regardless of the X bit value.
125 */
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200126 if (state->format->fourcc != DRM_FORMAT_XRGB1555)
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200127 rcar_du_plane_write(rgrp, index, PnALPHAR, PnALPHAR_ABIT_0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200128 else
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200129 rcar_du_plane_write(rgrp, index, PnALPHAR,
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200130 PnALPHAR_ABIT_X | state->alpha);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200131
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200132 pnmr = PnMR_BM_MD | state->format->pnmr;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200133
134 /* Disable color keying when requested. YUV formats have the
135 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
136 * automatically.
137 */
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200138 if ((state->colorkey & RCAR_DU_COLORKEY_MASK) == RCAR_DU_COLORKEY_NONE)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200139 pnmr |= PnMR_SPIM_TP_OFF;
140
141 /* For packed YUV formats we need to select the U/V order. */
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200142 if (state->format->fourcc == DRM_FORMAT_YUYV)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200143 pnmr |= PnMR_YCDF_YUYV;
144
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200145 rcar_du_plane_write(rgrp, index, PnMR, pnmr);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200146
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200147 switch (state->format->fourcc) {
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200148 case DRM_FORMAT_RGB565:
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200149 colorkey = ((state->colorkey & 0xf80000) >> 8)
150 | ((state->colorkey & 0x00fc00) >> 5)
151 | ((state->colorkey & 0x0000f8) >> 3);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200152 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200153 break;
154
155 case DRM_FORMAT_ARGB1555:
156 case DRM_FORMAT_XRGB1555:
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200157 colorkey = ((state->colorkey & 0xf80000) >> 9)
158 | ((state->colorkey & 0x00f800) >> 6)
159 | ((state->colorkey & 0x0000f8) >> 3);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200160 rcar_du_plane_write(rgrp, index, PnTC2R, colorkey);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200161 break;
162
163 case DRM_FORMAT_XRGB8888:
164 case DRM_FORMAT_ARGB8888:
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200165 rcar_du_plane_write(rgrp, index, PnTC3R,
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200166 PnTC3R_CODE | (state->colorkey & 0xffffff));
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200167 break;
168 }
169}
170
171static void __rcar_du_plane_setup(struct rcar_du_plane *plane,
172 unsigned int index)
173{
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200174 struct rcar_du_plane_state *state =
175 to_rcar_du_plane_state(plane->plane.state);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200176 struct rcar_du_group *rgrp = plane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200177 u32 ddcr2 = PnDDCR2_CODE;
178 u32 ddcr4;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200179
180 /* Data format
181 *
182 * The data format is selected by the DDDF field in PnMR and the EDF
183 * field in DDCR4.
184 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200185 ddcr4 = rcar_du_plane_read(rgrp, index, PnDDCR4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200186 ddcr4 &= ~PnDDCR4_EDF_MASK;
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200187 ddcr4 |= state->format->edf | PnDDCR4_CODE;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200188
189 rcar_du_plane_setup_mode(plane, index);
190
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200191 if (state->format->planes == 2) {
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200192 if (state->hwindex != index) {
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200193 if (state->format->fourcc == DRM_FORMAT_NV12 ||
194 state->format->fourcc == DRM_FORMAT_NV21)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200195 ddcr2 |= PnDDCR2_Y420;
196
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200197 if (state->format->fourcc == DRM_FORMAT_NV21)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200198 ddcr2 |= PnDDCR2_NV21;
199
200 ddcr2 |= PnDDCR2_DIVU;
201 } else {
202 ddcr2 |= PnDDCR2_DIVY;
203 }
204 }
205
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200206 rcar_du_plane_write(rgrp, index, PnDDCR2, ddcr2);
207 rcar_du_plane_write(rgrp, index, PnDDCR4, ddcr4);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200208
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200209 /* Destination position and size */
Laurent Pinchart287bdf02015-02-20 15:58:38 +0200210 rcar_du_plane_write(rgrp, index, PnDSXR, plane->plane.state->crtc_w);
211 rcar_du_plane_write(rgrp, index, PnDSYR, plane->plane.state->crtc_h);
212 rcar_du_plane_write(rgrp, index, PnDPXR, plane->plane.state->crtc_x);
213 rcar_du_plane_write(rgrp, index, PnDPYR, plane->plane.state->crtc_y);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200214
215 /* Wrap-around and blinking, disabled */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200216 rcar_du_plane_write(rgrp, index, PnWASPR, 0);
217 rcar_du_plane_write(rgrp, index, PnWAMWR, 4095);
218 rcar_du_plane_write(rgrp, index, PnBTR, 0);
219 rcar_du_plane_write(rgrp, index, PnMLR, 0);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200220}
221
222void rcar_du_plane_setup(struct rcar_du_plane *plane)
223{
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200224 struct rcar_du_plane_state *state =
225 to_rcar_du_plane_state(plane->plane.state);
226
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200227 __rcar_du_plane_setup(plane, state->hwindex);
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200228 if (state->format->planes == 2)
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200229 __rcar_du_plane_setup(plane, (state->hwindex + 1) % 8);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200230
Laurent Pinchartf398f342015-02-23 01:25:19 +0200231 rcar_du_plane_setup_fb(plane);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200232}
233
Laurent Pinchart920888a2015-02-18 12:18:05 +0200234static int rcar_du_plane_atomic_check(struct drm_plane *plane,
235 struct drm_plane_state *state)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200236{
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200237 struct rcar_du_plane_state *rstate = to_rcar_du_plane_state(state);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200238 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200239 struct rcar_du_device *rcdu = rplane->group->dev;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200240
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200241 if (!state->fb || !state->crtc) {
242 rstate->format = NULL;
Laurent Pinchart920888a2015-02-18 12:18:05 +0200243 return 0;
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200244 }
Laurent Pinchart917de182015-02-17 18:34:17 +0200245
Laurent Pinchart920888a2015-02-18 12:18:05 +0200246 if (state->src_w >> 16 != state->crtc_w ||
247 state->src_h >> 16 != state->crtc_h) {
248 dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200249 return -EINVAL;
250 }
251
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200252 rstate->format = rcar_du_format_info(state->fb->pixel_format);
253 if (rstate->format == NULL) {
Laurent Pinchart920888a2015-02-18 12:18:05 +0200254 dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
255 state->fb->pixel_format);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200256 return -EINVAL;
257 }
258
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200259 return 0;
260}
261
Laurent Pinchart920888a2015-02-18 12:18:05 +0200262static void rcar_du_plane_atomic_update(struct drm_plane *plane,
263 struct drm_plane_state *old_state)
264{
265 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchart5bfcbce2015-02-23 02:59:35 +0200266
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200267 if (plane->state->crtc)
268 rcar_du_plane_setup(rplane);
Laurent Pinchart920888a2015-02-18 12:18:05 +0200269}
270
271static const struct drm_plane_helper_funcs rcar_du_plane_helper_funcs = {
272 .atomic_check = rcar_du_plane_atomic_check,
273 .atomic_update = rcar_du_plane_atomic_update,
274};
275
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200276static void rcar_du_plane_reset(struct drm_plane *plane)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200277{
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200278 struct rcar_du_plane_state *state;
279
280 if (plane->state && plane->state->fb)
281 drm_framebuffer_unreference(plane->state->fb);
282
283 kfree(plane->state);
284 plane->state = NULL;
285
286 state = kzalloc(sizeof(*state), GFP_KERNEL);
287 if (state == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200288 return;
289
Laurent Pinchart5ee5a812015-02-25 18:27:19 +0200290 state->hwindex = -1;
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200291 state->alpha = 255;
292 state->colorkey = RCAR_DU_COLORKEY_NONE;
293 state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200294
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200295 plane->state = &state->state;
296 plane->state->plane = plane;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200297}
298
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200299static struct drm_plane_state *
300rcar_du_plane_atomic_duplicate_state(struct drm_plane *plane)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200301{
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200302 struct rcar_du_plane_state *state;
303 struct rcar_du_plane_state *copy;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200304
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200305 state = to_rcar_du_plane_state(plane->state);
306 copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
307 if (copy == NULL)
308 return NULL;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200309
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200310 if (copy->state.fb)
311 drm_framebuffer_reference(copy->state.fb);
312
313 return &copy->state;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200314}
315
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200316static void rcar_du_plane_atomic_destroy_state(struct drm_plane *plane,
317 struct drm_plane_state *state)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200318{
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200319 kfree(to_rcar_du_plane_state(state));
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200320}
321
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200322static int rcar_du_plane_atomic_set_property(struct drm_plane *plane,
323 struct drm_plane_state *state,
324 struct drm_property *property,
325 uint64_t val)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200326{
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200327 struct rcar_du_plane_state *rstate = to_rcar_du_plane_state(state);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200328 struct rcar_du_plane *rplane = to_rcar_plane(plane);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200329 struct rcar_du_group *rgrp = rplane->group;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200330
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200331 if (property == rgrp->planes.alpha)
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200332 rstate->alpha = val;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200333 else if (property == rgrp->planes.colorkey)
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200334 rstate->colorkey = val;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200335 else if (property == rgrp->planes.zpos)
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200336 rstate->zpos = val;
337 else
338 return -EINVAL;
339
340 return 0;
341}
342
343static int rcar_du_plane_atomic_get_property(struct drm_plane *plane,
344 const struct drm_plane_state *state, struct drm_property *property,
345 uint64_t *val)
346{
347 const struct rcar_du_plane_state *rstate =
348 container_of(state, const struct rcar_du_plane_state, state);
349 struct rcar_du_plane *rplane = to_rcar_plane(plane);
350 struct rcar_du_group *rgrp = rplane->group;
351
352 if (property == rgrp->planes.alpha)
353 *val = rstate->alpha;
354 else if (property == rgrp->planes.colorkey)
355 *val = rstate->colorkey;
356 else if (property == rgrp->planes.zpos)
357 *val = rstate->zpos;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200358 else
359 return -EINVAL;
360
361 return 0;
362}
363
364static const struct drm_plane_funcs rcar_du_plane_funcs = {
Laurent Pinchart336d04a2015-02-20 13:18:56 +0200365 .update_plane = drm_atomic_helper_update_plane,
366 .disable_plane = drm_atomic_helper_disable_plane,
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200367 .reset = rcar_du_plane_reset,
368 .set_property = drm_atomic_helper_plane_set_property,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200369 .destroy = drm_plane_cleanup,
Laurent Pinchart4407cc02015-02-23 02:36:31 +0200370 .atomic_duplicate_state = rcar_du_plane_atomic_duplicate_state,
371 .atomic_destroy_state = rcar_du_plane_atomic_destroy_state,
372 .atomic_set_property = rcar_du_plane_atomic_set_property,
373 .atomic_get_property = rcar_du_plane_atomic_get_property,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200374};
375
376static const uint32_t formats[] = {
377 DRM_FORMAT_RGB565,
378 DRM_FORMAT_ARGB1555,
379 DRM_FORMAT_XRGB1555,
380 DRM_FORMAT_XRGB8888,
381 DRM_FORMAT_ARGB8888,
382 DRM_FORMAT_UYVY,
383 DRM_FORMAT_YUYV,
384 DRM_FORMAT_NV12,
385 DRM_FORMAT_NV21,
386 DRM_FORMAT_NV16,
387};
388
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200389int rcar_du_planes_init(struct rcar_du_group *rgrp)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200390{
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200391 struct rcar_du_planes *planes = &rgrp->planes;
392 struct rcar_du_device *rcdu = rgrp->dev;
Laurent Pinchart917de182015-02-17 18:34:17 +0200393 unsigned int num_planes;
394 unsigned int num_crtcs;
395 unsigned int crtcs;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200396 unsigned int i;
Laurent Pinchart917de182015-02-17 18:34:17 +0200397 int ret;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200398
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200399 planes->alpha =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200400 drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200401 if (planes->alpha == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200402 return -ENOMEM;
403
404 /* The color key is expressed as an RGB888 triplet stored in a 32-bit
405 * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
406 * or enable source color keying (1).
407 */
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200408 planes->colorkey =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200409 drm_property_create_range(rcdu->ddev, 0, "colorkey",
410 0, 0x01ffffff);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200411 if (planes->colorkey == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200412 return -ENOMEM;
413
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200414 planes->zpos =
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200415 drm_property_create_range(rcdu->ddev, 0, "zpos", 1, 7);
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200416 if (planes->zpos == NULL)
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200417 return -ENOMEM;
418
Laurent Pinchart917de182015-02-17 18:34:17 +0200419 /* Create one primary plane per in this group CRTC and seven overlay
420 * planes.
421 */
422 num_crtcs = min(rcdu->num_crtcs - 2 * rgrp->index, 2U);
423 num_planes = num_crtcs + 7;
424
425 crtcs = ((1 << rcdu->num_crtcs) - 1) & (3 << (2 * rgrp->index));
426
427 for (i = 0; i < num_planes; ++i) {
428 enum drm_plane_type type = i < num_crtcs
429 ? DRM_PLANE_TYPE_PRIMARY
430 : DRM_PLANE_TYPE_OVERLAY;
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200431 struct rcar_du_plane *plane = &planes->planes[i];
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200432
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200433 plane->group = rgrp;
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200434
Laurent Pinchart917de182015-02-17 18:34:17 +0200435 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
436 &rcar_du_plane_funcs, formats,
437 ARRAY_SIZE(formats), type);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200438 if (ret < 0)
439 return ret;
440
Laurent Pinchart920888a2015-02-18 12:18:05 +0200441 drm_plane_helper_add(&plane->plane,
442 &rcar_du_plane_helper_funcs);
443
Laurent Pinchart917de182015-02-17 18:34:17 +0200444 if (type == DRM_PLANE_TYPE_PRIMARY)
445 continue;
446
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200447 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200448 planes->alpha, 255);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200449 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200450 planes->colorkey,
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200451 RCAR_DU_COLORKEY_NONE);
452 drm_object_attach_property(&plane->plane.base,
Laurent Pinchartcb2025d2013-06-16 21:01:02 +0200453 planes->zpos, 1);
Laurent Pinchart4bf8e192013-06-19 13:54:11 +0200454 }
455
456 return 0;
457}