Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * rcar_du_vsp.h  --  R-Car Display Unit VSP-Based Compositor
  3 *
  4 * Copyright (C) 2015 Renesas Electronics Corporation
  5 *
  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>
 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>
 21
 
 
 22#include <linux/of_platform.h>
 
 23#include <linux/videodev2.h>
 24
 25#include <media/vsp1.h>
 26
 27#include "rcar_du_drv.h"
 28#include "rcar_du_kms.h"
 29#include "rcar_du_vsp.h"
 30
 
 
 
 
 
 
 
 
 
 
 
 31void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
 32{
 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 = {
 36		.state = {
 37			.crtc = &crtc->crtc,
 38			.crtc_x = 0,
 39			.crtc_y = 0,
 40			.crtc_w = mode->hdisplay,
 41			.crtc_h = mode->vdisplay,
 42			.src_x = 0,
 43			.src_y = 0,
 44			.src_w = mode->hdisplay << 16,
 45			.src_h = mode->vdisplay << 16,
 
 46		},
 47		.format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
 48		.source = RCAR_DU_PLANE_VSPD1,
 49		.alpha = 255,
 50		.colorkey = 0,
 51		.zpos = 0,
 52	};
 53
 54	if (rcdu->info->gen >= 3)
 55		state.hwindex = (crtc->index % 2) ? 2 : 0;
 56	else
 57		state.hwindex = crtc->index % 2;
 58
 59	__rcar_du_plane_setup(crtc->group, &state);
 60
 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.
 64	 *
 65	 * TODO: Check whether this is still needed on Gen3.
 66	 */
 67	crtc->group->need_restart = true;
 68
 69	vsp1_du_setup_lif(crtc->vsp->vsp, mode->hdisplay, mode->vdisplay);
 70}
 71
 72void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
 73{
 74	vsp1_du_setup_lif(crtc->vsp->vsp, 0, 0);
 75}
 76
 77void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
 78{
 79	vsp1_du_atomic_begin(crtc->vsp->vsp);
 80}
 81
 82void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
 83{
 84	vsp1_du_atomic_flush(crtc->vsp->vsp);
 85}
 86
 87/* Keep the two tables in sync. */
 88static const u32 formats_kms[] = {
 89	DRM_FORMAT_RGB332,
 90	DRM_FORMAT_ARGB4444,
 91	DRM_FORMAT_XRGB4444,
 92	DRM_FORMAT_ARGB1555,
 93	DRM_FORMAT_XRGB1555,
 94	DRM_FORMAT_RGB565,
 95	DRM_FORMAT_BGR888,
 96	DRM_FORMAT_RGB888,
 97	DRM_FORMAT_BGRA8888,
 98	DRM_FORMAT_BGRX8888,
 99	DRM_FORMAT_ARGB8888,
100	DRM_FORMAT_XRGB8888,
101	DRM_FORMAT_UYVY,
102	DRM_FORMAT_VYUY,
103	DRM_FORMAT_YUYV,
104	DRM_FORMAT_YVYU,
105	DRM_FORMAT_NV12,
106	DRM_FORMAT_NV21,
107	DRM_FORMAT_NV16,
108	DRM_FORMAT_NV61,
109	DRM_FORMAT_YUV420,
110	DRM_FORMAT_YVU420,
111	DRM_FORMAT_YUV422,
112	DRM_FORMAT_YVU422,
113	DRM_FORMAT_YUV444,
114	DRM_FORMAT_YVU444,
115};
116
117static const u32 formats_v4l2[] = {
118	V4L2_PIX_FMT_RGB332,
119	V4L2_PIX_FMT_ARGB444,
120	V4L2_PIX_FMT_XRGB444,
121	V4L2_PIX_FMT_ARGB555,
122	V4L2_PIX_FMT_XRGB555,
123	V4L2_PIX_FMT_RGB565,
124	V4L2_PIX_FMT_RGB24,
125	V4L2_PIX_FMT_BGR24,
126	V4L2_PIX_FMT_ARGB32,
127	V4L2_PIX_FMT_XRGB32,
128	V4L2_PIX_FMT_ABGR32,
129	V4L2_PIX_FMT_XBGR32,
130	V4L2_PIX_FMT_UYVY,
131	V4L2_PIX_FMT_VYUY,
132	V4L2_PIX_FMT_YUYV,
133	V4L2_PIX_FMT_YVYU,
134	V4L2_PIX_FMT_NV12M,
135	V4L2_PIX_FMT_NV21M,
136	V4L2_PIX_FMT_NV16M,
137	V4L2_PIX_FMT_NV61M,
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,
144};
145
146static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
147{
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 v4l2_rect src;
152	struct v4l2_rect dst;
153	dma_addr_t paddr[2] = { 0, };
154	u32 pixelformat = 0;
 
 
155	unsigned int i;
156
157	src.left = state->state.src_x >> 16;
158	src.top = state->state.src_y >> 16;
159	src.width = state->state.src_w >> 16;
160	src.height = state->state.src_h >> 16;
161
162	dst.left = state->state.crtc_x;
163	dst.top = state->state.crtc_y;
164	dst.width = state->state.crtc_w;
165	dst.height = state->state.crtc_h;
166
167	for (i = 0; i < state->format->planes; ++i) {
168		struct drm_gem_cma_object *gem;
169
170		gem = drm_fb_cma_get_gem_obj(fb, i);
171		paddr[i] = gem->paddr + fb->offsets[i];
172	}
173
174	for (i = 0; i < ARRAY_SIZE(formats_kms); ++i) {
175		if (formats_kms[i] == state->format->fourcc) {
176			pixelformat = formats_v4l2[i];
177			break;
178		}
179	}
180
181	WARN_ON(!pixelformat);
182
183	vsp1_du_atomic_update(plane->vsp->vsp, plane->index, pixelformat,
184			      fb->pitches[0], paddr, &src, &dst);
185}
186
187static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
188					  struct drm_plane_state *state)
189{
190	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
191	struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
192	struct rcar_du_device *rcdu = rplane->vsp->dev;
 
 
193
194	if (!state->fb || !state->crtc) {
195		rstate->format = NULL;
 
 
 
196		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197	}
198
199	if (state->src_w >> 16 != state->crtc_w ||
200	    state->src_h >> 16 != state->crtc_h) {
201		dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
202		return -EINVAL;
 
 
 
 
203	}
204
205	rstate->format = rcar_du_format_info(state->fb->pixel_format);
206	if (rstate->format == NULL) {
207		dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
208			state->fb->pixel_format);
209		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
210	}
 
211
212	return 0;
 
 
 
 
 
213}
214
215static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
216					struct drm_plane_state *old_state)
217{
218	struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
 
219
220	if (plane->state->crtc)
221		rcar_du_vsp_plane_setup(rplane);
222	else
223		vsp1_du_atomic_update(rplane->vsp->vsp, rplane->index, 0, 0, 0,
224				      NULL, NULL);
225}
226
227static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
 
 
228	.atomic_check = rcar_du_vsp_plane_atomic_check,
229	.atomic_update = rcar_du_vsp_plane_atomic_update,
230};
231
232static struct drm_plane_state *
233rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
234{
235	struct rcar_du_vsp_plane_state *state;
236	struct rcar_du_vsp_plane_state *copy;
237
238	if (WARN_ON(!plane->state))
239		return NULL;
240
241	state = to_rcar_vsp_plane_state(plane->state);
242	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
243	if (copy == NULL)
244		return NULL;
245
246	__drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
247
248	return &copy->state;
249}
250
251static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
252						   struct drm_plane_state *state)
253{
254	__drm_atomic_helper_plane_destroy_state(plane, state);
255	kfree(to_rcar_vsp_plane_state(state));
256}
257
258static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
259{
260	struct rcar_du_vsp_plane_state *state;
261
262	if (plane->state) {
263		rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
264		plane->state = NULL;
265	}
266
267	state = kzalloc(sizeof(*state), GFP_KERNEL);
268	if (state == NULL)
269		return;
270
271	state->alpha = 255;
 
272
273	plane->state = &state->state;
274	plane->state->plane = plane;
275}
276
277static int rcar_du_vsp_plane_atomic_set_property(struct drm_plane *plane,
278	struct drm_plane_state *state, struct drm_property *property,
279	uint64_t val)
280{
281	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
282	struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
283
284	if (property == rcdu->props.alpha)
285		rstate->alpha = val;
286	else
287		return -EINVAL;
288
289	return 0;
290}
291
292static int rcar_du_vsp_plane_atomic_get_property(struct drm_plane *plane,
293	const struct drm_plane_state *state, struct drm_property *property,
294	uint64_t *val)
295{
296	const struct rcar_du_vsp_plane_state *rstate =
297		container_of(state, const struct rcar_du_vsp_plane_state, state);
298	struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
299
300	if (property == rcdu->props.alpha)
301		*val = rstate->alpha;
302	else
303		return -EINVAL;
304
305	return 0;
306}
307
308static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
309	.update_plane = drm_atomic_helper_update_plane,
310	.disable_plane = drm_atomic_helper_disable_plane,
311	.reset = rcar_du_vsp_plane_reset,
312	.set_property = drm_atomic_helper_plane_set_property,
313	.destroy = drm_plane_cleanup,
314	.atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
315	.atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
316	.atomic_set_property = rcar_du_vsp_plane_atomic_set_property,
317	.atomic_get_property = rcar_du_vsp_plane_atomic_get_property,
318};
319
320int rcar_du_vsp_init(struct rcar_du_vsp *vsp)
 
321{
322	struct rcar_du_device *rcdu = vsp->dev;
323	struct platform_device *pdev;
324	struct device_node *np;
325	unsigned int i;
326	int ret;
327
328	/* Find the VSP device and initialize it. */
329	np = of_parse_phandle(rcdu->dev->of_node, "vsps", vsp->index);
330	if (!np) {
331		dev_err(rcdu->dev, "vsps node not found\n");
332		return -ENXIO;
333	}
334
335	pdev = of_find_device_by_node(np);
336	of_node_put(np);
337	if (!pdev)
338		return -ENXIO;
339
340	vsp->vsp = &pdev->dev;
341
342	ret = vsp1_du_init(vsp->vsp);
343	if (ret < 0)
344		return ret;
345
346	 /* The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
 
347	  * 4 RPFs.
348	  */
349	vsp->num_planes = rcdu->info->gen >= 3 ? 5 : 4;
350
351	vsp->planes = devm_kcalloc(rcdu->dev, vsp->num_planes,
352				   sizeof(*vsp->planes), GFP_KERNEL);
353	if (!vsp->planes)
354		return -ENOMEM;
355
356	for (i = 0; i < vsp->num_planes; ++i) {
357		enum drm_plane_type type = i ? DRM_PLANE_TYPE_OVERLAY
358					 : DRM_PLANE_TYPE_PRIMARY;
 
359		struct rcar_du_vsp_plane *plane = &vsp->planes[i];
360
361		plane->vsp = vsp;
362		plane->index = i;
363
364		ret = drm_universal_plane_init(rcdu->ddev, &plane->plane,
365					       1 << vsp->index,
366					       &rcar_du_vsp_plane_funcs,
367					       formats_kms,
368					       ARRAY_SIZE(formats_kms), type,
369					       NULL);
370		if (ret < 0)
371			return ret;
372
373		drm_plane_helper_add(&plane->plane,
374				     &rcar_du_vsp_plane_helper_funcs);
375
376		if (type == DRM_PLANE_TYPE_PRIMARY)
377			continue;
378
379		drm_object_attach_property(&plane->plane.base,
380					   rcdu->props.alpha, 255);
 
 
381	}
382
383	return 0;
384}
v4.17
  1/*
  2 * rcar_du_vsp.h  --  R-Car Display Unit VSP-Based Compositor
  3 *
  4 * Copyright (C) 2015 Renesas Electronics Corporation
  5 *
  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>
 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>
 21
 22#include <linux/bitops.h>
 23#include <linux/dma-mapping.h>
 24#include <linux/of_platform.h>
 25#include <linux/scatterlist.h>
 26#include <linux/videodev2.h>
 27
 28#include <media/vsp1.h>
 29
 30#include "rcar_du_drv.h"
 31#include "rcar_du_kms.h"
 32#include "rcar_du_vsp.h"
 33
 34static void rcar_du_vsp_complete(void *private, bool completed)
 35{
 36	struct rcar_du_crtc *crtc = private;
 37
 38	if (crtc->vblank_enable)
 39		drm_crtc_handle_vblank(&crtc->crtc);
 40
 41	if (completed)
 42		rcar_du_crtc_finish_page_flip(crtc);
 43}
 44
 45void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
 46{
 47	const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
 48	struct rcar_du_device *rcdu = crtc->group->dev;
 49	struct vsp1_du_lif_config cfg = {
 50		.width = mode->hdisplay,
 51		.height = mode->vdisplay,
 52		.callback = rcar_du_vsp_complete,
 53		.callback_data = crtc,
 54	};
 55	struct rcar_du_plane_state state = {
 56		.state = {
 57			.crtc = &crtc->crtc,
 58			.dst.x1 = 0,
 59			.dst.y1 = 0,
 60			.dst.x2 = mode->hdisplay,
 61			.dst.y2 = mode->vdisplay,
 62			.src.x1 = 0,
 63			.src.y1 = 0,
 64			.src.x2 = mode->hdisplay << 16,
 65			.src.y2 = mode->vdisplay << 16,
 66			.zpos = 0,
 67		},
 68		.format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
 69		.source = RCAR_DU_PLANE_VSPD1,
 70		.alpha = 255,
 71		.colorkey = 0,
 
 72	};
 73
 74	if (rcdu->info->gen >= 3)
 75		state.hwindex = (crtc->index % 2) ? 2 : 0;
 76	else
 77		state.hwindex = crtc->index % 2;
 78
 79	__rcar_du_plane_setup(crtc->group, &state);
 80
 81	/*
 82	 * Ensure that the plane source configuration takes effect by requesting
 83	 * a restart of the group. See rcar_du_plane_atomic_update() for a more
 84	 * detailed explanation.
 85	 *
 86	 * TODO: Check whether this is still needed on Gen3.
 87	 */
 88	crtc->group->need_restart = true;
 89
 90	vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
 91}
 92
 93void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
 94{
 95	vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
 96}
 97
 98void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
 99{
100	vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
101}
102
103void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
104{
105	vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe);
106}
107
108/* Keep the two tables in sync. */
109static const u32 formats_kms[] = {
110	DRM_FORMAT_RGB332,
111	DRM_FORMAT_ARGB4444,
112	DRM_FORMAT_XRGB4444,
113	DRM_FORMAT_ARGB1555,
114	DRM_FORMAT_XRGB1555,
115	DRM_FORMAT_RGB565,
116	DRM_FORMAT_BGR888,
117	DRM_FORMAT_RGB888,
118	DRM_FORMAT_BGRA8888,
119	DRM_FORMAT_BGRX8888,
120	DRM_FORMAT_ARGB8888,
121	DRM_FORMAT_XRGB8888,
122	DRM_FORMAT_UYVY,
123	DRM_FORMAT_VYUY,
124	DRM_FORMAT_YUYV,
125	DRM_FORMAT_YVYU,
126	DRM_FORMAT_NV12,
127	DRM_FORMAT_NV21,
128	DRM_FORMAT_NV16,
129	DRM_FORMAT_NV61,
130	DRM_FORMAT_YUV420,
131	DRM_FORMAT_YVU420,
132	DRM_FORMAT_YUV422,
133	DRM_FORMAT_YVU422,
134	DRM_FORMAT_YUV444,
135	DRM_FORMAT_YVU444,
136};
137
138static const u32 formats_v4l2[] = {
139	V4L2_PIX_FMT_RGB332,
140	V4L2_PIX_FMT_ARGB444,
141	V4L2_PIX_FMT_XRGB444,
142	V4L2_PIX_FMT_ARGB555,
143	V4L2_PIX_FMT_XRGB555,
144	V4L2_PIX_FMT_RGB565,
145	V4L2_PIX_FMT_RGB24,
146	V4L2_PIX_FMT_BGR24,
147	V4L2_PIX_FMT_ARGB32,
148	V4L2_PIX_FMT_XRGB32,
149	V4L2_PIX_FMT_ABGR32,
150	V4L2_PIX_FMT_XBGR32,
151	V4L2_PIX_FMT_UYVY,
152	V4L2_PIX_FMT_VYUY,
153	V4L2_PIX_FMT_YUYV,
154	V4L2_PIX_FMT_YVYU,
155	V4L2_PIX_FMT_NV12M,
156	V4L2_PIX_FMT_NV21M,
157	V4L2_PIX_FMT_NV16M,
158	V4L2_PIX_FMT_NV61M,
159	V4L2_PIX_FMT_YUV420M,
160	V4L2_PIX_FMT_YVU420M,
161	V4L2_PIX_FMT_YUV422M,
162	V4L2_PIX_FMT_YVU422M,
163	V4L2_PIX_FMT_YUV444M,
164	V4L2_PIX_FMT_YVU444M,
165};
166
167static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
168{
169	struct rcar_du_vsp_plane_state *state =
170		to_rcar_vsp_plane_state(plane->plane.state);
171	struct rcar_du_crtc *crtc = to_rcar_crtc(state->state.crtc);
172	struct drm_framebuffer *fb = plane->plane.state->fb;
173	struct vsp1_du_atomic_config cfg = {
174		.pixelformat = 0,
175		.pitch = fb->pitches[0],
176		.alpha = state->alpha,
177		.zpos = state->state.zpos,
178	};
179	unsigned int i;
180
181	cfg.src.left = state->state.src.x1 >> 16;
182	cfg.src.top = state->state.src.y1 >> 16;
183	cfg.src.width = drm_rect_width(&state->state.src) >> 16;
184	cfg.src.height = drm_rect_height(&state->state.src) >> 16;
185
186	cfg.dst.left = state->state.dst.x1;
187	cfg.dst.top = state->state.dst.y1;
188	cfg.dst.width = drm_rect_width(&state->state.dst);
189	cfg.dst.height = drm_rect_height(&state->state.dst);
190
191	for (i = 0; i < state->format->planes; ++i)
192		cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
193			   + fb->offsets[i];
 
 
 
194
195	for (i = 0; i < ARRAY_SIZE(formats_kms); ++i) {
196		if (formats_kms[i] == state->format->fourcc) {
197			cfg.pixelformat = formats_v4l2[i];
198			break;
199		}
200	}
201
202	vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
203			      plane->index, &cfg);
 
 
204}
205
206static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
207					struct drm_plane_state *state)
208{
209	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
210	struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
211	struct rcar_du_device *rcdu = vsp->dev;
212	unsigned int i;
213	int ret;
214
215	/*
216	 * There's no need to prepare (and unprepare) the framebuffer when the
217	 * plane is not visible, as it will not be displayed.
218	 */
219	if (!state->visible)
220		return 0;
221
222	for (i = 0; i < rstate->format->planes; ++i) {
223		struct drm_gem_cma_object *gem =
224			drm_fb_cma_get_gem_obj(state->fb, i);
225		struct sg_table *sgt = &rstate->sg_tables[i];
226
227		ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr, gem->paddr,
228				      gem->base.size);
229		if (ret)
230			goto fail;
231
232		ret = vsp1_du_map_sg(vsp->vsp, sgt);
233		if (!ret) {
234			sg_free_table(sgt);
235			ret = -ENOMEM;
236			goto fail;
237		}
238	}
239
240	return 0;
241
242fail:
243	while (i--) {
244		struct sg_table *sgt = &rstate->sg_tables[i];
245
246		vsp1_du_unmap_sg(vsp->vsp, sgt);
247		sg_free_table(sgt);
248	}
249
250	return ret;
251}
252
253static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
254					 struct drm_plane_state *state)
255{
256	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
257	struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
258	unsigned int i;
259
260	if (!state->visible)
261		return;
262
263	for (i = 0; i < rstate->format->planes; ++i) {
264		struct sg_table *sgt = &rstate->sg_tables[i];
265
266		vsp1_du_unmap_sg(vsp->vsp, sgt);
267		sg_free_table(sgt);
268	}
269}
270
271static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
272					  struct drm_plane_state *state)
273{
274	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
275
276	return __rcar_du_plane_atomic_check(plane, state, &rstate->format);
277}
278
279static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
280					struct drm_plane_state *old_state)
281{
282	struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
283	struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
284
285	if (plane->state->visible)
286		rcar_du_vsp_plane_setup(rplane);
287	else
288		vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
289				      rplane->index, NULL);
290}
291
292static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
293	.prepare_fb = rcar_du_vsp_plane_prepare_fb,
294	.cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
295	.atomic_check = rcar_du_vsp_plane_atomic_check,
296	.atomic_update = rcar_du_vsp_plane_atomic_update,
297};
298
299static struct drm_plane_state *
300rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
301{
302	struct rcar_du_vsp_plane_state *state;
303	struct rcar_du_vsp_plane_state *copy;
304
305	if (WARN_ON(!plane->state))
306		return NULL;
307
308	state = to_rcar_vsp_plane_state(plane->state);
309	copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
310	if (copy == NULL)
311		return NULL;
312
313	__drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
314
315	return &copy->state;
316}
317
318static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
319						   struct drm_plane_state *state)
320{
321	__drm_atomic_helper_plane_destroy_state(state);
322	kfree(to_rcar_vsp_plane_state(state));
323}
324
325static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
326{
327	struct rcar_du_vsp_plane_state *state;
328
329	if (plane->state) {
330		rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
331		plane->state = NULL;
332	}
333
334	state = kzalloc(sizeof(*state), GFP_KERNEL);
335	if (state == NULL)
336		return;
337
338	state->alpha = 255;
339	state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
340
341	plane->state = &state->state;
342	plane->state->plane = plane;
343}
344
345static int rcar_du_vsp_plane_atomic_set_property(struct drm_plane *plane,
346	struct drm_plane_state *state, struct drm_property *property,
347	uint64_t val)
348{
349	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
350	struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
351
352	if (property == rcdu->props.alpha)
353		rstate->alpha = val;
354	else
355		return -EINVAL;
356
357	return 0;
358}
359
360static int rcar_du_vsp_plane_atomic_get_property(struct drm_plane *plane,
361	const struct drm_plane_state *state, struct drm_property *property,
362	uint64_t *val)
363{
364	const struct rcar_du_vsp_plane_state *rstate =
365		container_of(state, const struct rcar_du_vsp_plane_state, state);
366	struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
367
368	if (property == rcdu->props.alpha)
369		*val = rstate->alpha;
370	else
371		return -EINVAL;
372
373	return 0;
374}
375
376static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
377	.update_plane = drm_atomic_helper_update_plane,
378	.disable_plane = drm_atomic_helper_disable_plane,
379	.reset = rcar_du_vsp_plane_reset,
 
380	.destroy = drm_plane_cleanup,
381	.atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
382	.atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
383	.atomic_set_property = rcar_du_vsp_plane_atomic_set_property,
384	.atomic_get_property = rcar_du_vsp_plane_atomic_get_property,
385};
386
387int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
388		     unsigned int crtcs)
389{
390	struct rcar_du_device *rcdu = vsp->dev;
391	struct platform_device *pdev;
392	unsigned int num_crtcs = hweight32(crtcs);
393	unsigned int i;
394	int ret;
395
396	/* Find the VSP device and initialize it. */
 
 
 
 
 
 
397	pdev = of_find_device_by_node(np);
 
398	if (!pdev)
399		return -ENXIO;
400
401	vsp->vsp = &pdev->dev;
402
403	ret = vsp1_du_init(vsp->vsp);
404	if (ret < 0)
405		return ret;
406
407	 /*
408	  * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
409	  * 4 RPFs.
410	  */
411	vsp->num_planes = rcdu->info->gen >= 3 ? 5 : 4;
412
413	vsp->planes = devm_kcalloc(rcdu->dev, vsp->num_planes,
414				   sizeof(*vsp->planes), GFP_KERNEL);
415	if (!vsp->planes)
416		return -ENOMEM;
417
418	for (i = 0; i < vsp->num_planes; ++i) {
419		enum drm_plane_type type = i < num_crtcs
420					 ? DRM_PLANE_TYPE_PRIMARY
421					 : DRM_PLANE_TYPE_OVERLAY;
422		struct rcar_du_vsp_plane *plane = &vsp->planes[i];
423
424		plane->vsp = vsp;
425		plane->index = i;
426
427		ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
 
428					       &rcar_du_vsp_plane_funcs,
429					       formats_kms,
430					       ARRAY_SIZE(formats_kms),
431					       NULL, type, NULL);
432		if (ret < 0)
433			return ret;
434
435		drm_plane_helper_add(&plane->plane,
436				     &rcar_du_vsp_plane_helper_funcs);
437
438		if (type == DRM_PLANE_TYPE_PRIMARY)
439			continue;
440
441		drm_object_attach_property(&plane->plane.base,
442					   rcdu->props.alpha, 255);
443		drm_plane_create_zpos_property(&plane->plane, 1, 1,
444					       vsp->num_planes - 1);
445	}
446
447	return 0;
448}