2 * rcar_du_vsp.h -- R-Car Display Unit VSP-Based Compositor
4 * Copyright (C) 2015 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 <linux/of_platform.h>
23 #include <linux/videodev2.h>
25 #include <media/vsp1.h>
27 #include "rcar_du_drv.h"
28 #include "rcar_du_kms.h"
29 #include "rcar_du_vsp.h"
31 void rcar_du_vsp_enable(struct rcar_du_crtc
*crtc
)
33 const struct drm_display_mode
*mode
= &crtc
->crtc
.state
->adjusted_mode
;
34 struct rcar_du_device
*rcdu
= crtc
->group
->dev
;
35 struct rcar_du_plane_state state
= {
40 .crtc_w
= mode
->hdisplay
,
41 .crtc_h
= mode
->vdisplay
,
44 .src_w
= mode
->hdisplay
<< 16,
45 .src_h
= mode
->vdisplay
<< 16,
48 .format
= rcar_du_format_info(DRM_FORMAT_ARGB8888
),
49 .source
= RCAR_DU_PLANE_VSPD1
,
54 if (rcdu
->info
->gen
>= 3)
55 state
.hwindex
= (crtc
->index
% 2) ? 2 : 0;
57 state
.hwindex
= crtc
->index
% 2;
59 __rcar_du_plane_setup(crtc
->group
, &state
);
61 /* Ensure that the plane source configuration takes effect by requesting
62 * a restart of the group. See rcar_du_plane_atomic_update() for a more
63 * detailed explanation.
65 * TODO: Check whether this is still needed on Gen3.
67 crtc
->group
->need_restart
= true;
69 vsp1_du_setup_lif(crtc
->vsp
->vsp
, mode
->hdisplay
, mode
->vdisplay
);
72 void rcar_du_vsp_disable(struct rcar_du_crtc
*crtc
)
74 vsp1_du_setup_lif(crtc
->vsp
->vsp
, 0, 0);
77 void rcar_du_vsp_atomic_begin(struct rcar_du_crtc
*crtc
)
79 vsp1_du_atomic_begin(crtc
->vsp
->vsp
);
82 void rcar_du_vsp_atomic_flush(struct rcar_du_crtc
*crtc
)
84 vsp1_du_atomic_flush(crtc
->vsp
->vsp
);
87 /* Keep the two tables in sync. */
88 static const u32 formats_kms
[] = {
117 static const u32 formats_v4l2
[] = {
119 V4L2_PIX_FMT_ARGB444
,
120 V4L2_PIX_FMT_XRGB444
,
121 V4L2_PIX_FMT_ARGB555
,
122 V4L2_PIX_FMT_XRGB555
,
138 V4L2_PIX_FMT_YUV420M
,
139 V4L2_PIX_FMT_YVU420M
,
140 V4L2_PIX_FMT_YUV422M
,
141 V4L2_PIX_FMT_YVU422M
,
142 V4L2_PIX_FMT_YUV444M
,
143 V4L2_PIX_FMT_YVU444M
,
146 static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane
*plane
)
148 struct rcar_du_vsp_plane_state
*state
=
149 to_rcar_vsp_plane_state(plane
->plane
.state
);
150 struct drm_framebuffer
*fb
= plane
->plane
.state
->fb
;
151 struct vsp1_du_atomic_config cfg
= {
153 .pitch
= fb
->pitches
[0],
154 .alpha
= state
->alpha
,
155 .zpos
= state
->state
.zpos
,
159 cfg
.src
.left
= state
->state
.src_x
>> 16;
160 cfg
.src
.top
= state
->state
.src_y
>> 16;
161 cfg
.src
.width
= state
->state
.src_w
>> 16;
162 cfg
.src
.height
= state
->state
.src_h
>> 16;
164 cfg
.dst
.left
= state
->state
.crtc_x
;
165 cfg
.dst
.top
= state
->state
.crtc_y
;
166 cfg
.dst
.width
= state
->state
.crtc_w
;
167 cfg
.dst
.height
= state
->state
.crtc_h
;
169 for (i
= 0; i
< state
->format
->planes
; ++i
) {
170 struct drm_gem_cma_object
*gem
;
172 gem
= drm_fb_cma_get_gem_obj(fb
, i
);
173 cfg
.mem
[i
] = gem
->paddr
+ fb
->offsets
[i
];
176 for (i
= 0; i
< ARRAY_SIZE(formats_kms
); ++i
) {
177 if (formats_kms
[i
] == state
->format
->fourcc
) {
178 cfg
.pixelformat
= formats_v4l2
[i
];
183 vsp1_du_atomic_update(plane
->vsp
->vsp
, plane
->index
, &cfg
);
186 static int rcar_du_vsp_plane_atomic_check(struct drm_plane
*plane
,
187 struct drm_plane_state
*state
)
189 struct rcar_du_vsp_plane_state
*rstate
= to_rcar_vsp_plane_state(state
);
190 struct rcar_du_vsp_plane
*rplane
= to_rcar_vsp_plane(plane
);
191 struct rcar_du_device
*rcdu
= rplane
->vsp
->dev
;
193 if (!state
->fb
|| !state
->crtc
) {
194 rstate
->format
= NULL
;
198 if (state
->src_w
>> 16 != state
->crtc_w
||
199 state
->src_h
>> 16 != state
->crtc_h
) {
200 dev_dbg(rcdu
->dev
, "%s: scaling not supported\n", __func__
);
204 rstate
->format
= rcar_du_format_info(state
->fb
->pixel_format
);
205 if (rstate
->format
== NULL
) {
206 dev_dbg(rcdu
->dev
, "%s: unsupported format %08x\n", __func__
,
207 state
->fb
->pixel_format
);
214 static void rcar_du_vsp_plane_atomic_update(struct drm_plane
*plane
,
215 struct drm_plane_state
*old_state
)
217 struct rcar_du_vsp_plane
*rplane
= to_rcar_vsp_plane(plane
);
219 if (plane
->state
->crtc
)
220 rcar_du_vsp_plane_setup(rplane
);
222 vsp1_du_atomic_update(rplane
->vsp
->vsp
, rplane
->index
, NULL
);
225 static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs
= {
226 .atomic_check
= rcar_du_vsp_plane_atomic_check
,
227 .atomic_update
= rcar_du_vsp_plane_atomic_update
,
230 static struct drm_plane_state
*
231 rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane
*plane
)
233 struct rcar_du_vsp_plane_state
*state
;
234 struct rcar_du_vsp_plane_state
*copy
;
236 if (WARN_ON(!plane
->state
))
239 state
= to_rcar_vsp_plane_state(plane
->state
);
240 copy
= kmemdup(state
, sizeof(*state
), GFP_KERNEL
);
244 __drm_atomic_helper_plane_duplicate_state(plane
, ©
->state
);
249 static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane
*plane
,
250 struct drm_plane_state
*state
)
252 __drm_atomic_helper_plane_destroy_state(state
);
253 kfree(to_rcar_vsp_plane_state(state
));
256 static void rcar_du_vsp_plane_reset(struct drm_plane
*plane
)
258 struct rcar_du_vsp_plane_state
*state
;
261 rcar_du_vsp_plane_atomic_destroy_state(plane
, plane
->state
);
265 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
270 state
->state
.zpos
= plane
->type
== DRM_PLANE_TYPE_PRIMARY
? 0 : 1;
272 plane
->state
= &state
->state
;
273 plane
->state
->plane
= plane
;
276 static int rcar_du_vsp_plane_atomic_set_property(struct drm_plane
*plane
,
277 struct drm_plane_state
*state
, struct drm_property
*property
,
280 struct rcar_du_vsp_plane_state
*rstate
= to_rcar_vsp_plane_state(state
);
281 struct rcar_du_device
*rcdu
= to_rcar_vsp_plane(plane
)->vsp
->dev
;
283 if (property
== rcdu
->props
.alpha
)
291 static int rcar_du_vsp_plane_atomic_get_property(struct drm_plane
*plane
,
292 const struct drm_plane_state
*state
, struct drm_property
*property
,
295 const struct rcar_du_vsp_plane_state
*rstate
=
296 container_of(state
, const struct rcar_du_vsp_plane_state
, state
);
297 struct rcar_du_device
*rcdu
= to_rcar_vsp_plane(plane
)->vsp
->dev
;
299 if (property
== rcdu
->props
.alpha
)
300 *val
= rstate
->alpha
;
307 static const struct drm_plane_funcs rcar_du_vsp_plane_funcs
= {
308 .update_plane
= drm_atomic_helper_update_plane
,
309 .disable_plane
= drm_atomic_helper_disable_plane
,
310 .reset
= rcar_du_vsp_plane_reset
,
311 .set_property
= drm_atomic_helper_plane_set_property
,
312 .destroy
= drm_plane_cleanup
,
313 .atomic_duplicate_state
= rcar_du_vsp_plane_atomic_duplicate_state
,
314 .atomic_destroy_state
= rcar_du_vsp_plane_atomic_destroy_state
,
315 .atomic_set_property
= rcar_du_vsp_plane_atomic_set_property
,
316 .atomic_get_property
= rcar_du_vsp_plane_atomic_get_property
,
319 int rcar_du_vsp_init(struct rcar_du_vsp
*vsp
)
321 struct rcar_du_device
*rcdu
= vsp
->dev
;
322 struct platform_device
*pdev
;
323 struct device_node
*np
;
327 /* Find the VSP device and initialize it. */
328 np
= of_parse_phandle(rcdu
->dev
->of_node
, "vsps", vsp
->index
);
330 dev_err(rcdu
->dev
, "vsps node not found\n");
334 pdev
= of_find_device_by_node(np
);
339 vsp
->vsp
= &pdev
->dev
;
341 ret
= vsp1_du_init(vsp
->vsp
);
345 /* The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
348 vsp
->num_planes
= rcdu
->info
->gen
>= 3 ? 5 : 4;
350 vsp
->planes
= devm_kcalloc(rcdu
->dev
, vsp
->num_planes
,
351 sizeof(*vsp
->planes
), GFP_KERNEL
);
355 for (i
= 0; i
< vsp
->num_planes
; ++i
) {
356 enum drm_plane_type type
= i
? DRM_PLANE_TYPE_OVERLAY
357 : DRM_PLANE_TYPE_PRIMARY
;
358 struct rcar_du_vsp_plane
*plane
= &vsp
->planes
[i
];
363 ret
= drm_universal_plane_init(rcdu
->ddev
, &plane
->plane
,
365 &rcar_du_vsp_plane_funcs
,
367 ARRAY_SIZE(formats_kms
), type
,
372 drm_plane_helper_add(&plane
->plane
,
373 &rcar_du_vsp_plane_helper_funcs
);
375 if (type
== DRM_PLANE_TYPE_PRIMARY
)
378 drm_object_attach_property(&plane
->plane
.base
,
379 rcdu
->props
.alpha
, 255);
380 drm_plane_create_zpos_property(&plane
->plane
, 1, 1,
381 vsp
->num_planes
- 1);