xref: /linux/drivers/media/platform/renesas/vsp1/vsp1_video.c (revision de52d7a161290e64947c795195a231aa66a73c16)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vsp1_video.c  --  R-Car VSP1 Video Node
4  *
5  * Copyright (C) 2013-2015 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9 
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/slab.h>
14 #include <linux/v4l2-mediabus.h>
15 #include <linux/videodev2.h>
16 #include <linux/wait.h>
17 
18 #include <media/media-entity.h>
19 #include <media/v4l2-dev.h>
20 #include <media/v4l2-fh.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-subdev.h>
23 #include <media/videobuf2-v4l2.h>
24 #include <media/videobuf2-dma-contig.h>
25 
26 #include "vsp1.h"
27 #include "vsp1_brx.h"
28 #include "vsp1_dl.h"
29 #include "vsp1_entity.h"
30 #include "vsp1_hgo.h"
31 #include "vsp1_hgt.h"
32 #include "vsp1_pipe.h"
33 #include "vsp1_rwpf.h"
34 #include "vsp1_uds.h"
35 #include "vsp1_video.h"
36 
37 #define VSP1_VIDEO_DEF_FORMAT		V4L2_PIX_FMT_YUYV
38 #define VSP1_VIDEO_DEF_WIDTH		1024
39 #define VSP1_VIDEO_DEF_HEIGHT		768
40 
41 #define VSP1_VIDEO_MAX_WIDTH		8190U
42 #define VSP1_VIDEO_MAX_HEIGHT		8190U
43 
44 /* -----------------------------------------------------------------------------
45  * Helper functions
46  */
47 
48 static struct v4l2_subdev *
49 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
50 {
51 	struct media_pad *remote;
52 
53 	remote = media_pad_remote_pad_first(local);
54 	if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
55 		return NULL;
56 
57 	if (pad)
58 		*pad = remote->index;
59 
60 	return media_entity_to_v4l2_subdev(remote->entity);
61 }
62 
63 static int vsp1_video_verify_format(struct vsp1_video *video)
64 {
65 	struct v4l2_subdev_format fmt = {
66 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
67 	};
68 	struct v4l2_subdev *subdev;
69 	int ret;
70 
71 	subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
72 	if (subdev == NULL)
73 		return -EINVAL;
74 
75 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
76 	if (ret < 0)
77 		return ret == -ENOIOCTLCMD ? -EINVAL : ret;
78 
79 	if (video->rwpf->fmtinfo->mbus != fmt.format.code ||
80 	    video->rwpf->format.height != fmt.format.height ||
81 	    video->rwpf->format.width != fmt.format.width) {
82 		dev_dbg(video->vsp1->dev,
83 			"Format mismatch: 0x%04x/%ux%u != 0x%04x/%ux%u\n",
84 			video->rwpf->fmtinfo->mbus, video->rwpf->format.width,
85 			video->rwpf->format.height, fmt.format.code,
86 			fmt.format.width, fmt.format.height);
87 		return -EPIPE;
88 	}
89 
90 	return 0;
91 }
92 
93 static int __vsp1_video_try_format(struct vsp1_video *video,
94 				   struct v4l2_pix_format_mplane *pix,
95 				   const struct vsp1_format_info **fmtinfo)
96 {
97 	static const u32 xrgb_formats[][2] = {
98 		{ V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 },
99 		{ V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 },
100 		{ V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 },
101 		{ V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 },
102 	};
103 
104 	const struct vsp1_format_info *info;
105 	unsigned int width = pix->width;
106 	unsigned int height = pix->height;
107 	unsigned int i;
108 
109 	/*
110 	 * Backward compatibility: replace deprecated RGB formats by their XRGB
111 	 * equivalent. This selects the format older userspace applications want
112 	 * while still exposing the new format.
113 	 */
114 	for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) {
115 		if (xrgb_formats[i][0] == pix->pixelformat) {
116 			pix->pixelformat = xrgb_formats[i][1];
117 			break;
118 		}
119 	}
120 
121 	/*
122 	 * Retrieve format information and select the default format if the
123 	 * requested format isn't supported.
124 	 */
125 	info = vsp1_get_format_info(video->vsp1, pix->pixelformat);
126 	if (info == NULL)
127 		info = vsp1_get_format_info(video->vsp1, VSP1_VIDEO_DEF_FORMAT);
128 
129 	pix->pixelformat = info->fourcc;
130 	pix->field = V4L2_FIELD_NONE;
131 
132 	/*
133 	 * Adjust the colour space fields. On capture devices, userspace needs
134 	 * to set the V4L2_PIX_FMT_FLAG_SET_CSC to override the defaults. Reset
135 	 * all fields to *_DEFAULT if the flag isn't set, to then handle
136 	 * capture and output devices in the same way.
137 	 */
138 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
139 	    !(pix->flags & V4L2_PIX_FMT_FLAG_SET_CSC)) {
140 		pix->colorspace = V4L2_COLORSPACE_DEFAULT;
141 		pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
142 		pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
143 		pix->quantization = V4L2_QUANTIZATION_DEFAULT;
144 	}
145 
146 	vsp1_adjust_color_space(info->mbus, &pix->colorspace, &pix->xfer_func,
147 				&pix->ycbcr_enc, &pix->quantization);
148 
149 	memset(pix->reserved, 0, sizeof(pix->reserved));
150 
151 	/* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
152 	width = round_down(width, info->hsub);
153 	height = round_down(height, info->vsub);
154 
155 	/* Clamp the width and height. */
156 	pix->width = clamp(width, info->hsub, VSP1_VIDEO_MAX_WIDTH);
157 	pix->height = clamp(height, info->vsub, VSP1_VIDEO_MAX_HEIGHT);
158 
159 	/*
160 	 * Compute and clamp the stride and image size. While not documented in
161 	 * the datasheet, strides not aligned to a multiple of 128 bytes result
162 	 * in image corruption.
163 	 */
164 	for (i = 0; i < min(info->planes, 2U); ++i) {
165 		unsigned int hsub = i > 0 ? info->hsub : 1;
166 		unsigned int vsub = i > 0 ? info->vsub : 1;
167 		unsigned int align = 128;
168 		unsigned int bpl;
169 
170 		bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
171 			      pix->width / hsub * info->bpp[i] / 8,
172 			      round_down(65535U, align));
173 
174 		pix->plane_fmt[i].bytesperline = round_up(bpl, align);
175 		pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
176 					    * pix->height / vsub;
177 	}
178 
179 	if (info->planes == 3) {
180 		/* The second and third planes must have the same stride. */
181 		pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
182 		pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
183 	}
184 
185 	pix->num_planes = info->planes;
186 
187 	if (fmtinfo)
188 		*fmtinfo = info;
189 
190 	return 0;
191 }
192 
193 /* -----------------------------------------------------------------------------
194  * Pipeline Management
195  */
196 
197 /*
198  * vsp1_video_complete_buffer - Complete the current buffer
199  * @video: the video node
200  *
201  * This function completes the current buffer by filling its sequence number,
202  * time stamp and payload size, and hands it back to the vb2 core.
203  *
204  * Return the next queued buffer or NULL if the queue is empty.
205  */
206 static struct vsp1_vb2_buffer *
207 vsp1_video_complete_buffer(struct vsp1_video *video)
208 {
209 	struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
210 	struct vsp1_vb2_buffer *next = NULL;
211 	struct vsp1_vb2_buffer *done;
212 	unsigned int i;
213 
214 	scoped_guard(spinlock_irqsave, &video->irqlock) {
215 		if (list_empty(&video->irqqueue))
216 			return NULL;
217 
218 		done = list_first_entry(&video->irqqueue,
219 					struct vsp1_vb2_buffer, queue);
220 
221 		list_del(&done->queue);
222 
223 		if (!list_empty(&video->irqqueue))
224 			next = list_first_entry(&video->irqqueue,
225 						struct vsp1_vb2_buffer, queue);
226 	}
227 
228 	done->buf.sequence = pipe->sequence;
229 	done->buf.vb2_buf.timestamp = ktime_get_ns();
230 	for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
231 		vb2_set_plane_payload(&done->buf.vb2_buf, i,
232 				      vb2_plane_size(&done->buf.vb2_buf, i));
233 	vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE);
234 
235 	return next;
236 }
237 
238 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
239 				 struct vsp1_rwpf *rwpf)
240 {
241 	struct vsp1_video *video = rwpf->video;
242 	struct vsp1_vb2_buffer *buf;
243 
244 	buf = vsp1_video_complete_buffer(video);
245 	if (buf == NULL)
246 		return;
247 
248 	video->rwpf->mem = buf->mem;
249 	pipe->buffers_ready |= 1 << video->pipe_index;
250 }
251 
252 static void vsp1_video_pipeline_run_partition(struct vsp1_pipeline *pipe,
253 					      struct vsp1_dl_list *dl,
254 					      unsigned int partition)
255 {
256 	struct vsp1_partition *part = &pipe->part_table[partition];
257 	struct vsp1_dl_body *dlb = vsp1_dl_list_get_body0(dl);
258 	struct vsp1_entity *entity;
259 
260 	list_for_each_entry(entity, &pipe->entities, list_pipe)
261 		vsp1_entity_configure_partition(entity, pipe, part, dl, dlb);
262 }
263 
264 static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
265 {
266 	struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
267 	struct vsp1_entity *entity;
268 	struct vsp1_dl_body *dlb;
269 	struct vsp1_dl_list *dl;
270 	unsigned int partition;
271 
272 	dl = vsp1_dl_list_get(pipe->output->dlm);
273 
274 	/*
275 	 * If the VSP hardware isn't configured yet (which occurs either when
276 	 * processing the first frame or after a system suspend/resume), add the
277 	 * cached stream configuration to the display list to perform a full
278 	 * initialisation.
279 	 */
280 	if (!pipe->configured)
281 		vsp1_dl_list_add_body(dl, pipe->stream_config);
282 
283 	dlb = vsp1_dl_list_get_body0(dl);
284 
285 	list_for_each_entry(entity, &pipe->entities, list_pipe)
286 		vsp1_entity_configure_frame(entity, pipe, dl, dlb);
287 
288 	/* Run the first partition. */
289 	vsp1_video_pipeline_run_partition(pipe, dl, 0);
290 
291 	/* Process consecutive partitions as necessary. */
292 	for (partition = 1; partition < pipe->partitions; ++partition) {
293 		struct vsp1_dl_list *dl_next;
294 
295 		dl_next = vsp1_dl_list_get(pipe->output->dlm);
296 
297 		/*
298 		 * An incomplete chain will still function, but output only
299 		 * the partitions that had a dl available. The frame end
300 		 * interrupt will be marked on the last dl in the chain.
301 		 */
302 		if (!dl_next) {
303 			dev_err(vsp1->dev, "Failed to obtain a dl list. Frame will be incomplete\n");
304 			break;
305 		}
306 
307 		vsp1_video_pipeline_run_partition(pipe, dl_next, partition);
308 		vsp1_dl_list_add_chain(dl, dl_next);
309 	}
310 
311 	/* Complete, and commit the head display list. */
312 	vsp1_dl_list_commit(dl, 0);
313 	pipe->configured = true;
314 
315 	vsp1_pipeline_run(pipe);
316 }
317 
318 static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe,
319 					  unsigned int completion)
320 {
321 	struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
322 	enum vsp1_pipeline_state state;
323 	unsigned long flags;
324 	unsigned int i;
325 
326 	/* M2M Pipelines should never call here with an incomplete frame. */
327 	WARN_ON_ONCE(!(completion & VSP1_DL_FRAME_END_COMPLETED));
328 
329 	spin_lock_irqsave(&pipe->irqlock, flags);
330 
331 	/* Complete buffers on all video nodes. */
332 	for (i = 0; i < vsp1->info->rpf_count; ++i) {
333 		if (!pipe->inputs[i])
334 			continue;
335 
336 		vsp1_video_frame_end(pipe, pipe->inputs[i]);
337 	}
338 
339 	vsp1_video_frame_end(pipe, pipe->output);
340 
341 	state = pipe->state;
342 	pipe->state = VSP1_PIPELINE_STOPPED;
343 
344 	/*
345 	 * If a stop has been requested, mark the pipeline as stopped and
346 	 * return. Otherwise restart the pipeline if ready.
347 	 */
348 	if (state == VSP1_PIPELINE_STOPPING)
349 		wake_up(&pipe->wq);
350 	else if (vsp1_pipeline_ready(pipe))
351 		vsp1_video_pipeline_run(pipe);
352 
353 	spin_unlock_irqrestore(&pipe->irqlock, flags);
354 }
355 
356 static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline *pipe,
357 					    struct vsp1_rwpf *input,
358 					    struct vsp1_rwpf *output)
359 {
360 	struct media_entity_enum ent_enum;
361 	struct vsp1_entity *entity;
362 	struct media_pad *pad;
363 	struct vsp1_brx *brx = NULL;
364 	int ret;
365 
366 	ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev);
367 	if (ret < 0)
368 		return ret;
369 
370 	/*
371 	 * The main data path doesn't include the HGO or HGT, use
372 	 * vsp1_entity_remote_pad() to traverse the graph.
373 	 */
374 
375 	pad = vsp1_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
376 
377 	while (1) {
378 		if (pad == NULL) {
379 			ret = -EPIPE;
380 			goto out;
381 		}
382 
383 		/* We've reached a video node, that shouldn't have happened. */
384 		if (!is_media_entity_v4l2_subdev(pad->entity)) {
385 			ret = -EPIPE;
386 			goto out;
387 		}
388 
389 		entity = to_vsp1_entity(
390 			media_entity_to_v4l2_subdev(pad->entity));
391 
392 		/*
393 		 * A BRU or BRS is present in the pipeline, store its input pad
394 		 * number in the input RPF for use when configuring the RPF.
395 		 */
396 		if (entity->type == VSP1_ENTITY_BRU ||
397 		    entity->type == VSP1_ENTITY_BRS) {
398 			/* BRU and BRS can't be chained. */
399 			if (brx) {
400 				ret = -EPIPE;
401 				goto out;
402 			}
403 
404 			brx = to_brx(&entity->subdev);
405 			brx->inputs[pad->index].rpf = input;
406 			input->brx_input = pad->index;
407 		}
408 
409 		/* We've reached the WPF, we're done. */
410 		if (entity->type == VSP1_ENTITY_WPF)
411 			break;
412 
413 		/* Ensure the branch has no loop. */
414 		if (media_entity_enum_test_and_set(&ent_enum,
415 						   &entity->subdev.entity)) {
416 			ret = -EPIPE;
417 			goto out;
418 		}
419 
420 		/* UDS can't be chained. */
421 		if (entity->type == VSP1_ENTITY_UDS) {
422 			if (pipe->uds) {
423 				ret = -EPIPE;
424 				goto out;
425 			}
426 
427 			pipe->uds = entity;
428 			pipe->uds_input = brx ? &brx->entity : &input->entity;
429 		}
430 
431 		/* Follow the source link, ignoring any HGO or HGT. */
432 		pad = &entity->pads[entity->source_pad];
433 		pad = vsp1_entity_remote_pad(pad);
434 	}
435 
436 	/* The last entity must be the output WPF. */
437 	if (entity != &output->entity)
438 		ret = -EPIPE;
439 
440 out:
441 	media_entity_enum_cleanup(&ent_enum);
442 
443 	return ret;
444 }
445 
446 static int vsp1_video_pipeline_build(struct vsp1_pipeline *pipe,
447 				     struct vsp1_video *video)
448 {
449 	struct media_graph graph;
450 	struct media_entity *entity = &video->video.entity;
451 	struct media_device *mdev = entity->graph_obj.mdev;
452 	unsigned int i;
453 	int ret;
454 
455 	/* Walk the graph to locate the entities and video nodes. */
456 	ret = media_graph_walk_init(&graph, mdev);
457 	if (ret)
458 		return ret;
459 
460 	media_graph_walk_start(&graph, entity);
461 
462 	while ((entity = media_graph_walk_next(&graph))) {
463 		struct v4l2_subdev *subdev;
464 		struct vsp1_rwpf *rwpf;
465 		struct vsp1_entity *e;
466 
467 		if (!is_media_entity_v4l2_subdev(entity))
468 			continue;
469 
470 		subdev = media_entity_to_v4l2_subdev(entity);
471 		e = to_vsp1_entity(subdev);
472 		list_add_tail(&e->list_pipe, &pipe->entities);
473 		e->pipe = pipe;
474 
475 		switch (e->type) {
476 		case VSP1_ENTITY_RPF:
477 			rwpf = to_rwpf(subdev);
478 			pipe->inputs[rwpf->entity.index] = rwpf;
479 			rwpf->video->pipe_index = ++pipe->num_inputs;
480 			break;
481 
482 		case VSP1_ENTITY_WPF:
483 			rwpf = to_rwpf(subdev);
484 			pipe->output = rwpf;
485 			rwpf->video->pipe_index = 0;
486 			break;
487 
488 		case VSP1_ENTITY_LIF:
489 			pipe->lif = e;
490 			break;
491 
492 		case VSP1_ENTITY_BRU:
493 		case VSP1_ENTITY_BRS:
494 			pipe->brx = e;
495 			break;
496 
497 		case VSP1_ENTITY_HGO:
498 			pipe->hgo = e;
499 			break;
500 
501 		case VSP1_ENTITY_HGT:
502 			pipe->hgt = e;
503 			break;
504 
505 		default:
506 			break;
507 		}
508 	}
509 
510 	media_graph_walk_cleanup(&graph);
511 
512 	/* We need one output and at least one input. */
513 	if (pipe->num_inputs == 0 || !pipe->output)
514 		return -EPIPE;
515 
516 	/*
517 	 * Follow links downstream for each input and make sure the graph
518 	 * contains no loop and that all branches end at the output WPF.
519 	 */
520 	for (i = 0; i < video->vsp1->info->rpf_count; ++i) {
521 		if (!pipe->inputs[i])
522 			continue;
523 
524 		ret = vsp1_video_pipeline_build_branch(pipe, pipe->inputs[i],
525 						       pipe->output);
526 		if (ret < 0)
527 			return ret;
528 	}
529 
530 	return 0;
531 }
532 
533 static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe,
534 				    struct vsp1_video *video)
535 {
536 	int ret;
537 
538 	vsp1_pipeline_init(pipe);
539 
540 	pipe->frame_end = vsp1_video_pipeline_frame_end;
541 
542 	ret = vsp1_video_pipeline_build(pipe, video);
543 	if (ret)
544 		return ret;
545 
546 	vsp1_pipeline_dump(pipe, "video");
547 
548 	return 0;
549 }
550 
551 static struct vsp1_pipeline *vsp1_video_pipeline_get(struct vsp1_video *video)
552 {
553 	struct vsp1_pipeline *pipe;
554 	int ret;
555 
556 	/*
557 	 * Get a pipeline object for the video node. If a pipeline has already
558 	 * been allocated just increment its reference count and return it.
559 	 * Otherwise allocate a new pipeline and initialize it, it will be freed
560 	 * when the last reference is released.
561 	 */
562 	if (!video->rwpf->entity.pipe) {
563 		pipe = kzalloc_obj(*pipe);
564 		if (!pipe)
565 			return ERR_PTR(-ENOMEM);
566 
567 		ret = vsp1_video_pipeline_init(pipe, video);
568 		if (ret < 0) {
569 			vsp1_pipeline_reset(pipe);
570 			kfree(pipe);
571 			return ERR_PTR(ret);
572 		}
573 	} else {
574 		pipe = video->rwpf->entity.pipe;
575 		kref_get(&pipe->kref);
576 	}
577 
578 	return pipe;
579 }
580 
581 static void vsp1_video_pipeline_release(struct kref *kref)
582 {
583 	struct vsp1_pipeline *pipe = container_of(kref, typeof(*pipe), kref);
584 
585 	vsp1_pipeline_reset(pipe);
586 	kfree(pipe);
587 }
588 
589 static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe)
590 {
591 	struct media_device *mdev = &pipe->output->entity.vsp1->media_dev;
592 
593 	guard(mutex)(&mdev->graph_mutex);
594 
595 	kref_put(&pipe->kref, vsp1_video_pipeline_release);
596 }
597 
598 /* -----------------------------------------------------------------------------
599  * videobuf2 Queue Operations
600  */
601 
602 static int
603 vsp1_video_queue_setup(struct vb2_queue *vq,
604 		       unsigned int *nbuffers, unsigned int *nplanes,
605 		       unsigned int sizes[], struct device *alloc_devs[])
606 {
607 	struct vsp1_video *video = vb2_get_drv_priv(vq);
608 	const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
609 	unsigned int i;
610 
611 	if (*nplanes) {
612 		if (*nplanes != format->num_planes)
613 			return -EINVAL;
614 
615 		for (i = 0; i < *nplanes; i++)
616 			if (sizes[i] < format->plane_fmt[i].sizeimage)
617 				return -EINVAL;
618 		return 0;
619 	}
620 
621 	*nplanes = format->num_planes;
622 
623 	for (i = 0; i < format->num_planes; ++i)
624 		sizes[i] = format->plane_fmt[i].sizeimage;
625 
626 	return 0;
627 }
628 
629 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
630 {
631 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
632 	struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
633 	struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
634 	const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
635 	unsigned int i;
636 
637 	if (vb->num_planes < format->num_planes)
638 		return -EINVAL;
639 
640 	for (i = 0; i < vb->num_planes; ++i) {
641 		buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
642 
643 		if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage)
644 			return -EINVAL;
645 	}
646 
647 	for ( ; i < 3; ++i)
648 		buf->mem.addr[i] = 0;
649 
650 	return 0;
651 }
652 
653 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
654 {
655 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
656 	struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
657 	struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
658 	struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
659 	bool empty;
660 
661 	scoped_guard(spinlock_irqsave, &video->irqlock) {
662 		empty = list_empty(&video->irqqueue);
663 		list_add_tail(&buf->queue, &video->irqqueue);
664 	}
665 
666 	if (!empty)
667 		return;
668 
669 	guard(spinlock_irqsave)(&pipe->irqlock);
670 
671 	video->rwpf->mem = buf->mem;
672 	pipe->buffers_ready |= 1 << video->pipe_index;
673 
674 	if (vb2_start_streaming_called(&video->queue) &&
675 	    vsp1_pipeline_ready(pipe))
676 		vsp1_video_pipeline_run(pipe);
677 }
678 
679 static int vsp1_video_pipeline_setup_partitions(struct vsp1_pipeline *pipe)
680 {
681 	struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
682 	const struct v4l2_mbus_framefmt *format;
683 	struct vsp1_entity *entity;
684 	unsigned int div_size;
685 	unsigned int i;
686 
687 	/*
688 	 * Partitions are computed on the size before rotation, use the format
689 	 * at the WPF sink.
690 	 */
691 	format = v4l2_subdev_state_get_format(pipe->output->entity.state,
692 					      RWPF_PAD_SINK);
693 	div_size = format->width;
694 
695 	/*
696 	 * Only Gen3+ hardware requires image partitioning, Gen2 will operate
697 	 * with a single partition that covers the whole output.
698 	 */
699 	if (vsp1->info->gen >= 3) {
700 		list_for_each_entry(entity, &pipe->entities, list_pipe) {
701 			unsigned int entity_max;
702 
703 			if (!entity->ops->max_width)
704 				continue;
705 
706 			entity_max = entity->ops->max_width(entity,
707 							    entity->state,
708 							    pipe);
709 			if (entity_max)
710 				div_size = min(div_size, entity_max);
711 		}
712 	}
713 
714 	pipe->partitions = DIV_ROUND_UP(format->width, div_size);
715 	pipe->part_table = kzalloc_objs(*pipe->part_table, pipe->partitions);
716 	if (!pipe->part_table)
717 		return -ENOMEM;
718 
719 	for (i = 0; i < pipe->partitions; ++i)
720 		vsp1_pipeline_calculate_partition(pipe, &pipe->part_table[i],
721 						  div_size, i);
722 
723 	return 0;
724 }
725 
726 static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
727 {
728 	struct vsp1_entity *entity;
729 	int ret;
730 
731 	/* Determine this pipelines sizes for image partitioning support. */
732 	ret = vsp1_video_pipeline_setup_partitions(pipe);
733 	if (ret < 0)
734 		return ret;
735 
736 	if (pipe->uds) {
737 		struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
738 
739 		/*
740 		 * If a BRU or BRS is present in the pipeline before the UDS,
741 		 * the alpha component doesn't need to be scaled as the BRU and
742 		 * BRS output alpha value is fixed to 255. Otherwise we need to
743 		 * scale the alpha component only when available at the input
744 		 * RPF.
745 		 */
746 		if (pipe->uds_input->type == VSP1_ENTITY_BRU ||
747 		    pipe->uds_input->type == VSP1_ENTITY_BRS) {
748 			uds->scale_alpha = false;
749 		} else {
750 			struct vsp1_rwpf *rpf =
751 				to_rwpf(&pipe->uds_input->subdev);
752 
753 			uds->scale_alpha = rpf->fmtinfo->alpha;
754 		}
755 	}
756 
757 	/*
758 	 * Compute and cache the stream configuration into a body. The cached
759 	 * body will be added to the display list by vsp1_video_pipeline_run()
760 	 * whenever the pipeline needs to be fully reconfigured.
761 	 */
762 	pipe->stream_config = vsp1_dlm_dl_body_get(pipe->output->dlm);
763 	if (!pipe->stream_config)
764 		return -ENOMEM;
765 
766 	list_for_each_entry(entity, &pipe->entities, list_pipe) {
767 		vsp1_entity_route_setup(entity, pipe, pipe->stream_config);
768 		vsp1_entity_configure_stream(entity, entity->state, pipe, NULL,
769 					     pipe->stream_config);
770 	}
771 
772 	return 0;
773 }
774 
775 static void vsp1_video_release_buffers(struct vsp1_video *video)
776 {
777 	struct vsp1_vb2_buffer *buffer;
778 
779 	/* Remove all buffers from the IRQ queue. */
780 	guard(spinlock_irqsave)(&video->irqlock);
781 
782 	list_for_each_entry(buffer, &video->irqqueue, queue)
783 		vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
784 	INIT_LIST_HEAD(&video->irqqueue);
785 }
786 
787 static void vsp1_video_cleanup_pipeline(struct vsp1_pipeline *pipe)
788 {
789 	lockdep_assert_held(&pipe->lock);
790 
791 	/* Release any cached configuration from our output video. */
792 	vsp1_dl_body_put(pipe->stream_config);
793 	pipe->stream_config = NULL;
794 	pipe->configured = false;
795 
796 	/* Release our partition table allocation. */
797 	kfree(pipe->part_table);
798 	pipe->part_table = NULL;
799 }
800 
801 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
802 {
803 	struct vsp1_video *video = vb2_get_drv_priv(vq);
804 	struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
805 	bool start_pipeline = false;
806 	int ret;
807 
808 	scoped_guard(mutex, &pipe->lock) {
809 		if (pipe->stream_count == pipe->num_inputs) {
810 			ret = vsp1_video_setup_pipeline(pipe);
811 			if (ret < 0) {
812 				vsp1_video_release_buffers(video);
813 				vsp1_video_cleanup_pipeline(pipe);
814 				return ret;
815 			}
816 
817 			start_pipeline = true;
818 		}
819 
820 		pipe->stream_count++;
821 	}
822 
823 	/*
824 	 * vsp1_pipeline_ready() is not sufficient to establish that all streams
825 	 * are prepared and the pipeline is configured, as multiple streams
826 	 * can race through streamon with buffers already queued; Therefore we
827 	 * don't even attempt to start the pipeline until the last stream has
828 	 * called through here.
829 	 */
830 	if (!start_pipeline)
831 		return 0;
832 
833 	guard(spinlock_irqsave)(&pipe->irqlock);
834 
835 	if (vsp1_pipeline_ready(pipe))
836 		vsp1_video_pipeline_run(pipe);
837 
838 	return 0;
839 }
840 
841 static void vsp1_video_stop_streaming(struct vb2_queue *vq)
842 {
843 	struct vsp1_video *video = vb2_get_drv_priv(vq);
844 	struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
845 	int ret;
846 
847 	/*
848 	 * Clear the buffers ready flag to make sure the device won't be started
849 	 * by a QBUF on the video node on the other side of the pipeline.
850 	 */
851 	scoped_guard(spinlock_irqsave, &video->irqlock) {
852 		pipe->buffers_ready &= ~(1 << video->pipe_index);
853 	}
854 
855 	scoped_guard(mutex, &pipe->lock) {
856 		if (--pipe->stream_count == pipe->num_inputs) {
857 			/* Stop the pipeline. */
858 			ret = vsp1_pipeline_stop(pipe);
859 			if (ret == -ETIMEDOUT)
860 				dev_err(video->vsp1->dev,
861 					"pipeline stop timeout\n");
862 
863 			vsp1_video_cleanup_pipeline(pipe);
864 		}
865 	}
866 
867 	video_device_pipeline_stop(&video->video);
868 	vsp1_video_release_buffers(video);
869 	vsp1_video_pipeline_put(pipe);
870 }
871 
872 static const struct vb2_ops vsp1_video_queue_qops = {
873 	.queue_setup = vsp1_video_queue_setup,
874 	.buf_prepare = vsp1_video_buffer_prepare,
875 	.buf_queue = vsp1_video_buffer_queue,
876 	.start_streaming = vsp1_video_start_streaming,
877 	.stop_streaming = vsp1_video_stop_streaming,
878 };
879 
880 /* -----------------------------------------------------------------------------
881  * V4L2 ioctls
882  */
883 
884 static int
885 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
886 {
887 	struct v4l2_fh *vfh = file_to_v4l2_fh(file);
888 	struct vsp1_video *video = to_vsp1_video(vfh->vdev);
889 
890 	cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
891 			  | V4L2_CAP_IO_MC | V4L2_CAP_VIDEO_CAPTURE_MPLANE
892 			  | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
893 
894 	strscpy(cap->driver, "vsp1", sizeof(cap->driver));
895 	strscpy(cap->card, video->video.name, sizeof(cap->card));
896 
897 	return 0;
898 }
899 
900 static int vsp1_video_enum_format(struct file *file, void *fh,
901 				  struct v4l2_fmtdesc *f)
902 {
903 	struct v4l2_fh *vfh = file_to_v4l2_fh(file);
904 	struct vsp1_video *video = to_vsp1_video(vfh->vdev);
905 	const struct vsp1_format_info *info;
906 
907 	info = vsp1_get_format_info_by_index(video->vsp1, f->index, f->mbus_code);
908 	if (!info)
909 		return -EINVAL;
910 
911 	f->pixelformat = info->fourcc;
912 
913 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
914 	    info->mbus == MEDIA_BUS_FMT_AYUV8_1X32)
915 		f->flags = V4L2_FMT_FLAG_CSC_YCBCR_ENC
916 			 | V4L2_FMT_FLAG_CSC_QUANTIZATION;
917 
918 	return 0;
919 }
920 
921 static int
922 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
923 {
924 	struct v4l2_fh *vfh = file_to_v4l2_fh(file);
925 	struct vsp1_video *video = to_vsp1_video(vfh->vdev);
926 
927 	if (format->type != video->queue.type)
928 		return -EINVAL;
929 
930 	guard(mutex)(&video->lock);
931 
932 	format->fmt.pix_mp = video->rwpf->format;
933 
934 	return 0;
935 }
936 
937 static int
938 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
939 {
940 	struct v4l2_fh *vfh = file_to_v4l2_fh(file);
941 	struct vsp1_video *video = to_vsp1_video(vfh->vdev);
942 
943 	if (format->type != video->queue.type)
944 		return -EINVAL;
945 
946 	return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
947 }
948 
949 static int
950 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
951 {
952 	struct v4l2_fh *vfh = file_to_v4l2_fh(file);
953 	struct vsp1_video *video = to_vsp1_video(vfh->vdev);
954 	const struct vsp1_format_info *info;
955 	int ret;
956 
957 	if (format->type != video->queue.type)
958 		return -EINVAL;
959 
960 	ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
961 	if (ret < 0)
962 		return ret;
963 
964 	guard(mutex)(&video->lock);
965 
966 	if (vb2_is_busy(&video->queue))
967 		return -EBUSY;
968 
969 	video->rwpf->format = format->fmt.pix_mp;
970 	video->rwpf->fmtinfo = info;
971 
972 	return 0;
973 }
974 
975 static int
976 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
977 {
978 	struct v4l2_fh *vfh = file_to_v4l2_fh(file);
979 	struct vsp1_video *video = to_vsp1_video(vfh->vdev);
980 	struct media_device *mdev = &video->vsp1->media_dev;
981 	struct vsp1_pipeline *pipe;
982 	int ret;
983 
984 	if (vb2_queue_is_busy(&video->queue, file))
985 		return -EBUSY;
986 
987 	/*
988 	 * Get a pipeline for the video node and start streaming on it. No link
989 	 * touching an entity in the pipeline can be activated or deactivated
990 	 * once streaming is started.
991 	 */
992 	scoped_guard(mutex, &mdev->graph_mutex) {
993 		pipe = vsp1_video_pipeline_get(video);
994 		if (IS_ERR(pipe))
995 			return PTR_ERR(pipe);
996 
997 		ret = __video_device_pipeline_start(&video->video, &pipe->pipe);
998 		if (ret < 0)
999 			goto err_pipe;
1000 	}
1001 
1002 	/*
1003 	 * Verify that the configured format matches the output of the connected
1004 	 * subdev.
1005 	 */
1006 	ret = vsp1_video_verify_format(video);
1007 	if (ret < 0)
1008 		goto err_stop;
1009 
1010 	/* Start the queue. */
1011 	ret = vb2_streamon(&video->queue, type);
1012 	if (ret < 0)
1013 		goto err_stop;
1014 
1015 	return 0;
1016 
1017 err_stop:
1018 	video_device_pipeline_stop(&video->video);
1019 err_pipe:
1020 	vsp1_video_pipeline_put(pipe);
1021 	return ret;
1022 }
1023 
1024 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
1025 	.vidioc_querycap		= vsp1_video_querycap,
1026 	.vidioc_enum_fmt_vid_cap	= vsp1_video_enum_format,
1027 	.vidioc_enum_fmt_vid_out	= vsp1_video_enum_format,
1028 	.vidioc_g_fmt_vid_cap_mplane	= vsp1_video_get_format,
1029 	.vidioc_s_fmt_vid_cap_mplane	= vsp1_video_set_format,
1030 	.vidioc_try_fmt_vid_cap_mplane	= vsp1_video_try_format,
1031 	.vidioc_g_fmt_vid_out_mplane	= vsp1_video_get_format,
1032 	.vidioc_s_fmt_vid_out_mplane	= vsp1_video_set_format,
1033 	.vidioc_try_fmt_vid_out_mplane	= vsp1_video_try_format,
1034 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
1035 	.vidioc_querybuf		= vb2_ioctl_querybuf,
1036 	.vidioc_qbuf			= vb2_ioctl_qbuf,
1037 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1038 	.vidioc_expbuf			= vb2_ioctl_expbuf,
1039 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
1040 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
1041 	.vidioc_streamon		= vsp1_video_streamon,
1042 	.vidioc_streamoff		= vb2_ioctl_streamoff,
1043 };
1044 
1045 /* -----------------------------------------------------------------------------
1046  * V4L2 File Operations
1047  */
1048 
1049 static int vsp1_video_open(struct file *file)
1050 {
1051 	struct vsp1_video *video = video_drvdata(file);
1052 	struct v4l2_fh *vfh;
1053 	int ret = 0;
1054 
1055 	vfh = kzalloc_obj(*vfh);
1056 	if (vfh == NULL)
1057 		return -ENOMEM;
1058 
1059 	v4l2_fh_init(vfh, &video->video);
1060 	v4l2_fh_add(vfh, file);
1061 
1062 	ret = vsp1_device_get(video->vsp1);
1063 	if (ret < 0) {
1064 		v4l2_fh_del(vfh, file);
1065 		v4l2_fh_exit(vfh);
1066 		kfree(vfh);
1067 	}
1068 
1069 	return ret;
1070 }
1071 
1072 static int vsp1_video_release(struct file *file)
1073 {
1074 	struct vsp1_video *video = video_drvdata(file);
1075 
1076 	vb2_fop_release(file);
1077 
1078 	vsp1_device_put(video->vsp1);
1079 
1080 	return 0;
1081 }
1082 
1083 static const struct v4l2_file_operations vsp1_video_fops = {
1084 	.owner = THIS_MODULE,
1085 	.unlocked_ioctl = video_ioctl2,
1086 	.open = vsp1_video_open,
1087 	.release = vsp1_video_release,
1088 	.poll = vb2_fop_poll,
1089 	.mmap = vb2_fop_mmap,
1090 };
1091 
1092 /* -----------------------------------------------------------------------------
1093  * Media entity operations
1094  */
1095 
1096 static int vsp1_video_link_validate(struct media_link *link)
1097 {
1098 	/*
1099 	 * Ideally, link validation should be implemented here instead of
1100 	 * calling vsp1_video_verify_format() in vsp1_video_streamon()
1101 	 * manually. That would however break userspace that start one video
1102 	 * device before configures formats on other video devices in the
1103 	 * pipeline. This operation is just a no-op to silence the warnings
1104 	 * from v4l2_subdev_link_validate().
1105 	 */
1106 	return 0;
1107 }
1108 
1109 static const struct media_entity_operations vsp1_video_media_ops = {
1110 	.link_validate = vsp1_video_link_validate,
1111 };
1112 
1113 /* -----------------------------------------------------------------------------
1114  * Suspend and Resume
1115  */
1116 
1117 void vsp1_video_suspend(struct vsp1_device *vsp1)
1118 {
1119 	unsigned int i;
1120 	int ret;
1121 
1122 	/*
1123 	 * To avoid increasing the system suspend time needlessly, loop over the
1124 	 * pipelines twice, first to set them all to the stopping state, and
1125 	 * then to wait for the stop to complete.
1126 	 */
1127 	for (i = 0; i < vsp1->info->wpf_count; ++i) {
1128 		struct vsp1_rwpf *wpf = vsp1->wpf[i];
1129 		struct vsp1_pipeline *pipe;
1130 
1131 		if (wpf == NULL)
1132 			continue;
1133 
1134 		pipe = wpf->entity.pipe;
1135 		if (pipe == NULL)
1136 			continue;
1137 
1138 		scoped_guard(spinlock_irqsave, &pipe->irqlock) {
1139 			if (pipe->state == VSP1_PIPELINE_RUNNING)
1140 				pipe->state = VSP1_PIPELINE_STOPPING;
1141 		}
1142 	}
1143 
1144 	for (i = 0; i < vsp1->info->wpf_count; ++i) {
1145 		struct vsp1_rwpf *wpf = vsp1->wpf[i];
1146 		struct vsp1_pipeline *pipe;
1147 
1148 		if (wpf == NULL)
1149 			continue;
1150 
1151 		pipe = wpf->entity.pipe;
1152 		if (pipe == NULL)
1153 			continue;
1154 
1155 		ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe),
1156 					 msecs_to_jiffies(500));
1157 		if (ret == 0)
1158 			dev_warn(vsp1->dev, "pipeline %u stop timeout\n",
1159 				 wpf->entity.index);
1160 	}
1161 }
1162 
1163 void vsp1_video_resume(struct vsp1_device *vsp1)
1164 {
1165 	unsigned int i;
1166 
1167 	/* Resume all running pipelines. */
1168 	for (i = 0; i < vsp1->info->wpf_count; ++i) {
1169 		struct vsp1_rwpf *wpf = vsp1->wpf[i];
1170 		struct vsp1_pipeline *pipe;
1171 
1172 		if (wpf == NULL)
1173 			continue;
1174 
1175 		pipe = wpf->entity.pipe;
1176 		if (pipe == NULL)
1177 			continue;
1178 
1179 		/*
1180 		 * The hardware may have been reset during a suspend and will
1181 		 * need a full reconfiguration.
1182 		 */
1183 		pipe->configured = false;
1184 
1185 		scoped_guard(spinlock_irqsave, &pipe->irqlock) {
1186 			if (vsp1_pipeline_ready(pipe))
1187 				vsp1_video_pipeline_run(pipe);
1188 		}
1189 	}
1190 }
1191 
1192 /* -----------------------------------------------------------------------------
1193  * Initialization and Cleanup
1194  */
1195 
1196 struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
1197 				     struct vsp1_rwpf *rwpf)
1198 {
1199 	struct vsp1_video *video;
1200 	const char *direction;
1201 	int ret;
1202 
1203 	video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL);
1204 	if (!video)
1205 		return ERR_PTR(-ENOMEM);
1206 
1207 	rwpf->video = video;
1208 
1209 	video->vsp1 = vsp1;
1210 	video->rwpf = rwpf;
1211 
1212 	if (rwpf->entity.type == VSP1_ENTITY_RPF) {
1213 		direction = "input";
1214 		video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1215 		video->pad.flags = MEDIA_PAD_FL_SOURCE;
1216 		video->video.vfl_dir = VFL_DIR_TX;
1217 		video->video.device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1218 					   V4L2_CAP_STREAMING | V4L2_CAP_IO_MC;
1219 	} else {
1220 		direction = "output";
1221 		video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1222 		video->pad.flags = MEDIA_PAD_FL_SINK;
1223 		video->video.vfl_dir = VFL_DIR_RX;
1224 		video->video.device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1225 					   V4L2_CAP_STREAMING | V4L2_CAP_IO_MC;
1226 	}
1227 
1228 	mutex_init(&video->lock);
1229 	spin_lock_init(&video->irqlock);
1230 	INIT_LIST_HEAD(&video->irqqueue);
1231 
1232 	/* Initialize the media entity... */
1233 	ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1234 	if (ret < 0)
1235 		return ERR_PTR(ret);
1236 
1237 	/* ... and the format ... */
1238 	rwpf->format.pixelformat = VSP1_VIDEO_DEF_FORMAT;
1239 	rwpf->format.width = VSP1_VIDEO_DEF_WIDTH;
1240 	rwpf->format.height = VSP1_VIDEO_DEF_HEIGHT;
1241 	__vsp1_video_try_format(video, &rwpf->format, &rwpf->fmtinfo);
1242 
1243 	/* ... and the video node... */
1244 	video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1245 	video->video.entity.ops = &vsp1_video_media_ops;
1246 	video->video.fops = &vsp1_video_fops;
1247 	snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1248 		 rwpf->entity.subdev.name, direction);
1249 	video->video.vfl_type = VFL_TYPE_VIDEO;
1250 	video->video.release = video_device_release_empty;
1251 	video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1252 
1253 	video_set_drvdata(&video->video, video);
1254 
1255 	video->queue.type = video->type;
1256 	video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1257 	video->queue.lock = &video->lock;
1258 	video->queue.drv_priv = video;
1259 	video->queue.buf_struct_size = sizeof(struct vsp1_vb2_buffer);
1260 	video->queue.ops = &vsp1_video_queue_qops;
1261 	video->queue.mem_ops = &vb2_dma_contig_memops;
1262 	video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1263 	video->queue.dev = video->vsp1->bus_master;
1264 	ret = vb2_queue_init(&video->queue);
1265 	if (ret < 0) {
1266 		dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1267 		goto error;
1268 	}
1269 
1270 	/* ... and register the video device. */
1271 	video->video.queue = &video->queue;
1272 	ret = video_register_device(&video->video, VFL_TYPE_VIDEO, -1);
1273 	if (ret < 0) {
1274 		dev_err(video->vsp1->dev, "failed to register video device\n");
1275 		goto error;
1276 	}
1277 
1278 	return video;
1279 
1280 error:
1281 	vsp1_video_cleanup(video);
1282 	return ERR_PTR(ret);
1283 }
1284 
1285 void vsp1_video_cleanup(struct vsp1_video *video)
1286 {
1287 	if (video_is_registered(&video->video))
1288 		video_unregister_device(&video->video);
1289 
1290 	media_entity_cleanup(&video->video.entity);
1291 }
1292