2 * rcar_du_plane.c -- R-Car Display Unit Planes
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 #include <drm/drm_atomic_helper.h>
16 #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>
20 #include <drm/drm_plane_helper.h>
22 #include "rcar_du_drv.h"
23 #include "rcar_du_kms.h"
24 #include "rcar_du_plane.h"
25 #include "rcar_du_regs.h"
27 #define RCAR_DU_COLORKEY_NONE (0 << 24)
28 #define RCAR_DU_COLORKEY_SOURCE (1 << 24)
29 #define RCAR_DU_COLORKEY_MASK (1 << 24)
31 static void rcar_du_plane_write(struct rcar_du_group
*rgrp
,
32 unsigned int index
, u32 reg
, u32 data
)
34 rcar_du_write(rgrp
->dev
, rgrp
->mmio_offset
+ index
* PLANE_OFF
+ reg
,
38 static void rcar_du_plane_setup_scanout(struct rcar_du_plane
*plane
)
40 struct rcar_du_plane_state
*state
=
41 to_rcar_plane_state(plane
->plane
.state
);
42 struct drm_framebuffer
*fb
= plane
->plane
.state
->fb
;
43 struct rcar_du_group
*rgrp
= plane
->group
;
44 unsigned int src_x
= state
->state
.src_x
>> 16;
45 unsigned int src_y
= state
->state
.src_y
>> 16;
46 unsigned int index
= state
->hwindex
;
47 struct drm_gem_cma_object
*gem
;
53 interlaced
= state
->state
.crtc
->state
->adjusted_mode
.flags
54 & DRM_MODE_FLAG_INTERLACE
;
56 /* Memory pitch (expressed in pixels). Must be doubled for interlaced
57 * operation with 32bpp formats.
59 if (state
->format
->planes
== 2)
60 pitch
= fb
->pitches
[0];
62 pitch
= fb
->pitches
[0] * 8 / state
->format
->bpp
;
64 for (i
= 0; i
< state
->format
->planes
; ++i
) {
65 gem
= drm_fb_cma_get_gem_obj(fb
, i
);
66 dma
[i
] = gem
->paddr
+ fb
->offsets
[i
];
69 rcar_du_plane_write(rgrp
, index
, PnMWR
,
70 (interlaced
&& state
->format
->bpp
== 32) ?
73 /* The Y position is expressed in raster line units and must be doubled
74 * for 32bpp formats, according to the R8A7790 datasheet. No mention of
75 * doubling the Y position is found in the R8A7779 datasheet, but the
76 * rule seems to apply there as well.
78 * Despite not being documented, doubling seem not to be needed when
79 * operating in interlaced mode.
81 * Similarly, for the second plane, NV12 and NV21 formats seem to
82 * require a halved Y position value, in both progressive and interlaced
85 rcar_du_plane_write(rgrp
, index
, PnSPXR
, src_x
);
86 rcar_du_plane_write(rgrp
, index
, PnSPYR
, src_y
*
87 (!interlaced
&& state
->format
->bpp
== 32 ? 2 : 1));
89 rcar_du_plane_write(rgrp
, index
, PnDSA0R
, dma
[0]);
91 if (state
->format
->planes
== 2) {
92 index
= (index
+ 1) % 8;
94 rcar_du_plane_write(rgrp
, index
, PnMWR
, pitch
);
96 rcar_du_plane_write(rgrp
, index
, PnSPXR
, src_x
);
97 rcar_du_plane_write(rgrp
, index
, PnSPYR
, src_y
*
98 (state
->format
->bpp
== 16 ? 2 : 1) / 2);
100 rcar_du_plane_write(rgrp
, index
, PnDSA0R
, dma
[1]);
104 static void rcar_du_plane_setup_mode(struct rcar_du_plane
*plane
,
107 struct rcar_du_plane_state
*state
=
108 to_rcar_plane_state(plane
->plane
.state
);
109 struct rcar_du_group
*rgrp
= plane
->group
;
113 /* The PnALPHAR register controls alpha-blending in 16bpp formats
114 * (ARGB1555 and XRGB1555).
116 * For ARGB, set the alpha value to 0, and enable alpha-blending when
117 * the A bit is 0. This maps A=0 to alpha=0 and A=1 to alpha=255.
119 * For XRGB, set the alpha value to the plane-wide alpha value and
120 * enable alpha-blending regardless of the X bit value.
122 if (state
->format
->fourcc
!= DRM_FORMAT_XRGB1555
)
123 rcar_du_plane_write(rgrp
, index
, PnALPHAR
, PnALPHAR_ABIT_0
);
125 rcar_du_plane_write(rgrp
, index
, PnALPHAR
,
126 PnALPHAR_ABIT_X
| state
->alpha
);
128 pnmr
= PnMR_BM_MD
| state
->format
->pnmr
;
130 /* Disable color keying when requested. YUV formats have the
131 * PnMR_SPIM_TP_OFF bit set in their pnmr field, disabling color keying
134 if ((state
->colorkey
& RCAR_DU_COLORKEY_MASK
) == RCAR_DU_COLORKEY_NONE
)
135 pnmr
|= PnMR_SPIM_TP_OFF
;
137 /* For packed YUV formats we need to select the U/V order. */
138 if (state
->format
->fourcc
== DRM_FORMAT_YUYV
)
139 pnmr
|= PnMR_YCDF_YUYV
;
141 rcar_du_plane_write(rgrp
, index
, PnMR
, pnmr
);
143 switch (state
->format
->fourcc
) {
144 case DRM_FORMAT_RGB565
:
145 colorkey
= ((state
->colorkey
& 0xf80000) >> 8)
146 | ((state
->colorkey
& 0x00fc00) >> 5)
147 | ((state
->colorkey
& 0x0000f8) >> 3);
148 rcar_du_plane_write(rgrp
, index
, PnTC2R
, colorkey
);
151 case DRM_FORMAT_ARGB1555
:
152 case DRM_FORMAT_XRGB1555
:
153 colorkey
= ((state
->colorkey
& 0xf80000) >> 9)
154 | ((state
->colorkey
& 0x00f800) >> 6)
155 | ((state
->colorkey
& 0x0000f8) >> 3);
156 rcar_du_plane_write(rgrp
, index
, PnTC2R
, colorkey
);
159 case DRM_FORMAT_XRGB8888
:
160 case DRM_FORMAT_ARGB8888
:
161 rcar_du_plane_write(rgrp
, index
, PnTC3R
,
162 PnTC3R_CODE
| (state
->colorkey
& 0xffffff));
167 static void rcar_du_plane_setup_format(struct rcar_du_plane
*plane
,
170 struct rcar_du_plane_state
*state
=
171 to_rcar_plane_state(plane
->plane
.state
);
172 struct rcar_du_group
*rgrp
= plane
->group
;
173 u32 ddcr2
= PnDDCR2_CODE
;
178 * The data format is selected by the DDDF field in PnMR and the EDF
182 rcar_du_plane_setup_mode(plane
, index
);
184 if (state
->format
->planes
== 2) {
185 if (state
->hwindex
!= index
) {
186 if (state
->format
->fourcc
== DRM_FORMAT_NV12
||
187 state
->format
->fourcc
== DRM_FORMAT_NV21
)
188 ddcr2
|= PnDDCR2_Y420
;
190 if (state
->format
->fourcc
== DRM_FORMAT_NV21
)
191 ddcr2
|= PnDDCR2_NV21
;
193 ddcr2
|= PnDDCR2_DIVU
;
195 ddcr2
|= PnDDCR2_DIVY
;
199 rcar_du_plane_write(rgrp
, index
, PnDDCR2
, ddcr2
);
201 ddcr4
= state
->format
->edf
| PnDDCR4_CODE
;
203 rcar_du_plane_write(rgrp
, index
, PnDDCR4
, ddcr4
);
205 /* Destination position and size */
206 rcar_du_plane_write(rgrp
, index
, PnDSXR
, plane
->plane
.state
->crtc_w
);
207 rcar_du_plane_write(rgrp
, index
, PnDSYR
, plane
->plane
.state
->crtc_h
);
208 rcar_du_plane_write(rgrp
, index
, PnDPXR
, plane
->plane
.state
->crtc_x
);
209 rcar_du_plane_write(rgrp
, index
, PnDPYR
, plane
->plane
.state
->crtc_y
);
211 /* Wrap-around and blinking, disabled */
212 rcar_du_plane_write(rgrp
, index
, PnWASPR
, 0);
213 rcar_du_plane_write(rgrp
, index
, PnWAMWR
, 4095);
214 rcar_du_plane_write(rgrp
, index
, PnBTR
, 0);
215 rcar_du_plane_write(rgrp
, index
, PnMLR
, 0);
218 void rcar_du_plane_setup(struct rcar_du_plane
*plane
)
220 struct rcar_du_plane_state
*state
=
221 to_rcar_plane_state(plane
->plane
.state
);
223 rcar_du_plane_setup_format(plane
, state
->hwindex
);
224 if (state
->format
->planes
== 2)
225 rcar_du_plane_setup_format(plane
, (state
->hwindex
+ 1) % 8);
227 rcar_du_plane_setup_scanout(plane
);
230 static int rcar_du_plane_atomic_check(struct drm_plane
*plane
,
231 struct drm_plane_state
*state
)
233 struct rcar_du_plane_state
*rstate
= to_rcar_plane_state(state
);
234 struct rcar_du_plane
*rplane
= to_rcar_plane(plane
);
235 struct rcar_du_device
*rcdu
= rplane
->group
->dev
;
237 if (!state
->fb
|| !state
->crtc
) {
238 rstate
->format
= NULL
;
242 if (state
->src_w
>> 16 != state
->crtc_w
||
243 state
->src_h
>> 16 != state
->crtc_h
) {
244 dev_dbg(rcdu
->dev
, "%s: scaling not supported\n", __func__
);
248 rstate
->format
= rcar_du_format_info(state
->fb
->pixel_format
);
249 if (rstate
->format
== NULL
) {
250 dev_dbg(rcdu
->dev
, "%s: unsupported format %08x\n", __func__
,
251 state
->fb
->pixel_format
);
258 static void rcar_du_plane_atomic_update(struct drm_plane
*plane
,
259 struct drm_plane_state
*old_state
)
261 struct rcar_du_plane
*rplane
= to_rcar_plane(plane
);
263 if (plane
->state
->crtc
)
264 rcar_du_plane_setup(rplane
);
267 static const struct drm_plane_helper_funcs rcar_du_plane_helper_funcs
= {
268 .atomic_check
= rcar_du_plane_atomic_check
,
269 .atomic_update
= rcar_du_plane_atomic_update
,
272 static struct drm_plane_state
*
273 rcar_du_plane_atomic_duplicate_state(struct drm_plane
*plane
)
275 struct rcar_du_plane_state
*state
;
276 struct rcar_du_plane_state
*copy
;
278 if (WARN_ON(!plane
->state
))
281 state
= to_rcar_plane_state(plane
->state
);
282 copy
= kmemdup(state
, sizeof(*state
), GFP_KERNEL
);
286 __drm_atomic_helper_plane_duplicate_state(plane
, ©
->state
);
291 static void rcar_du_plane_atomic_destroy_state(struct drm_plane
*plane
,
292 struct drm_plane_state
*state
)
294 __drm_atomic_helper_plane_destroy_state(plane
, state
);
295 kfree(to_rcar_plane_state(state
));
298 static void rcar_du_plane_reset(struct drm_plane
*plane
)
300 struct rcar_du_plane_state
*state
;
303 rcar_du_plane_atomic_destroy_state(plane
, plane
->state
);
307 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
312 state
->source
= RCAR_DU_PLANE_MEMORY
;
314 state
->colorkey
= RCAR_DU_COLORKEY_NONE
;
315 state
->zpos
= plane
->type
== DRM_PLANE_TYPE_PRIMARY
? 0 : 1;
317 plane
->state
= &state
->state
;
318 plane
->state
->plane
= plane
;
321 static int rcar_du_plane_atomic_set_property(struct drm_plane
*plane
,
322 struct drm_plane_state
*state
,
323 struct drm_property
*property
,
326 struct rcar_du_plane_state
*rstate
= to_rcar_plane_state(state
);
327 struct rcar_du_device
*rcdu
= to_rcar_plane(plane
)->group
->dev
;
329 if (property
== rcdu
->props
.alpha
)
331 else if (property
== rcdu
->props
.colorkey
)
332 rstate
->colorkey
= val
;
333 else if (property
== rcdu
->props
.zpos
)
341 static int rcar_du_plane_atomic_get_property(struct drm_plane
*plane
,
342 const struct drm_plane_state
*state
, struct drm_property
*property
,
345 const struct rcar_du_plane_state
*rstate
=
346 container_of(state
, const struct rcar_du_plane_state
, state
);
347 struct rcar_du_device
*rcdu
= to_rcar_plane(plane
)->group
->dev
;
349 if (property
== rcdu
->props
.alpha
)
350 *val
= rstate
->alpha
;
351 else if (property
== rcdu
->props
.colorkey
)
352 *val
= rstate
->colorkey
;
353 else if (property
== rcdu
->props
.zpos
)
361 static const struct drm_plane_funcs rcar_du_plane_funcs
= {
362 .update_plane
= drm_atomic_helper_update_plane
,
363 .disable_plane
= drm_atomic_helper_disable_plane
,
364 .reset
= rcar_du_plane_reset
,
365 .set_property
= drm_atomic_helper_plane_set_property
,
366 .destroy
= drm_plane_cleanup
,
367 .atomic_duplicate_state
= rcar_du_plane_atomic_duplicate_state
,
368 .atomic_destroy_state
= rcar_du_plane_atomic_destroy_state
,
369 .atomic_set_property
= rcar_du_plane_atomic_set_property
,
370 .atomic_get_property
= rcar_du_plane_atomic_get_property
,
373 static const uint32_t formats
[] = {
386 int rcar_du_planes_init(struct rcar_du_group
*rgrp
)
388 struct rcar_du_device
*rcdu
= rgrp
->dev
;
393 /* Create one primary plane per CRTC in this group and seven overlay
396 rgrp
->num_planes
= rgrp
->num_crtcs
+ 7;
398 crtcs
= ((1 << rcdu
->num_crtcs
) - 1) & (3 << (2 * rgrp
->index
));
400 for (i
= 0; i
< rgrp
->num_planes
; ++i
) {
401 enum drm_plane_type type
= i
< rgrp
->num_crtcs
402 ? DRM_PLANE_TYPE_PRIMARY
403 : DRM_PLANE_TYPE_OVERLAY
;
404 struct rcar_du_plane
*plane
= &rgrp
->planes
[i
];
408 ret
= drm_universal_plane_init(rcdu
->ddev
, &plane
->plane
, crtcs
,
409 &rcar_du_plane_funcs
, formats
,
410 ARRAY_SIZE(formats
), type
,
415 drm_plane_helper_add(&plane
->plane
,
416 &rcar_du_plane_helper_funcs
);
418 if (type
== DRM_PLANE_TYPE_PRIMARY
)
421 drm_object_attach_property(&plane
->plane
.base
,
422 rcdu
->props
.alpha
, 255);
423 drm_object_attach_property(&plane
->plane
.base
,
424 rcdu
->props
.colorkey
,
425 RCAR_DU_COLORKEY_NONE
);
426 drm_object_attach_property(&plane
->plane
.base
,
427 rcdu
->props
.zpos
, 1);